| // Copyright 2016 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| // +build ignore |
| |
| // Parses syscalls.abigen of Fuchsia's zircon kernel and produces Go assembly |
| // for calling the system calls. |
| // |
| // Regenerate syscall/zx with: |
| // |
| // go run mkfuchsia.go -stubs > syscalls_fuchsia.go |
| // go run mkfuchsia.go -goarch=amd64 > syscalls_fuchsia_amd64.s |
| // go run mkfuchsia.go -goarch=arm64 > syscalls_fuchsia_arm64.s |
| // gofmt -w syscalls_fuchsia.go |
| package main |
| |
| import ( |
| "bytes" |
| "flag" |
| "fmt" |
| "io/ioutil" |
| "log" |
| "os" |
| "path/filepath" |
| |
| "internal/fuchsia/abigen" |
| ) |
| |
| var ( |
| stubs = flag.Bool("stubs", false, "print only Go function stubs") |
| goarch = flag.String("goarch", "amd64", "arch to print asm for") |
| fuchsiaRoot = flag.String("fuchsia_root", filepath.Join(os.Getenv("HOME"), "fuchsia"), "path to fuchsia root") |
| ) |
| |
| func main() { |
| flag.Parse() |
| sdkArch := "" |
| switch *goarch { |
| case "amd64": |
| sdkArch = "x86_64" |
| case "arm64": |
| sdkArch = "aarch64" |
| default: |
| log.Fatalf("GOARCH=%s not supported", *goarch) |
| } |
| _ = sdkArch |
| |
| syscallsFile := filepath.Join(*fuchsiaRoot, "/zircon/system/public/zircon/syscalls.abigen") |
| if args := flag.Args(); len(args) != 0 { |
| syscallsFile = args[0] |
| } |
| |
| b, err := ioutil.ReadFile(syscallsFile) |
| if err != nil { |
| log.Fatal(err) |
| } |
| |
| p := abigen.NewParser(b, syscallsFile) |
| defs, err := p.Parse() |
| if err != nil { |
| log.Fatal(err) |
| } |
| |
| buf := new(bytes.Buffer) |
| if *stubs { |
| fmt.Fprint(buf, stubsHeader[1:]) |
| for _, def := range defs { |
| fmt.Fprintf(buf, "//go:cgo_import_dynamic vdso_zx_%s zx_%s\n", def.Name, def.Name) |
| } |
| fmt.Fprint(buf, "\n") |
| for _, def := range defs { |
| fmt.Fprintf(buf, "//go:linkname vdso_zx_%s vdso_zx_%s\n", def.Name, def.Name) |
| } |
| fmt.Fprint(buf, "\nvar (\n") |
| for _, def := range defs { |
| fmt.Fprintf(buf, "\tvdso_zx_%s uintptr\n", def.Name) |
| } |
| fmt.Fprint(buf, ")\n\n") |
| for _, def := range defs { |
| fmt.Fprint(buf, "//go:noescape\n") |
| fmt.Fprint(buf, "//go:nosplit\n") |
| printStub(buf, def) |
| fmt.Fprint(buf, "\n") |
| } |
| } else { |
| fmt.Fprint(buf, asmHeader[1:]) |
| for _, def := range defs { |
| fmt.Fprint(buf, "// ") |
| printStub(buf, def) |
| printAsm(buf, def) |
| fmt.Fprint(buf, "\n") |
| } |
| } |
| buf.WriteTo(os.Stdout) |
| } |
| |
| const stubsHeader = ` |
| // Copyright 2018 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| // Zircon system calls for the Fuchsia OS. |
| // Generated by mkfuchsia.go, do not edit. |
| |
| package zx |
| |
| import "unsafe" |
| |
| ` |
| |
| const asmHeader = ` |
| // Copyright 2016 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| // Zircon system calls for the Fuchsia OS. |
| // Generated by mkfuchsia.go, do not edit. |
| |
| #include "textflag.h" |
| |
| ` |
| |
| func printStub(buf *bytes.Buffer, def abigen.SysDef) { |
| fmt.Fprintf(buf, "func %s(", def.GoName()) |
| for i, arg := range def.Args { |
| if arg.Type == (abigen.SysType{}) { |
| continue |
| } |
| if i > 0 { |
| fmt.Fprint(buf, ", ") |
| } |
| fmt.Fprintf(buf, "%s ", arg.Name) |
| fmt.Fprintf(buf, "%s", arg.Type.GoType()) |
| } |
| fmt.Fprint(buf, ")") |
| if def.Ret != (abigen.SysType{}) { |
| fmt.Fprint(buf, " ") |
| fmt.Fprintf(buf, "%s", def.Ret.GoType()) |
| } |
| fmt.Fprint(buf, "\n") |
| } |
| |
| // amd64RegArgs is the amd64 registers in function argument calling convention order |
| var amd64RegArgs = []string{"DI", "SI", "DX", "CX", "R8", "R9", "R12", "R13"} |
| |
| // arm64RegArgs is the arm64 registers in function argument calling convention order |
| var arm64RegArgs = []string{"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7"} |
| |
| // blockingSyscalls is a map of known syscalls which may block. |
| var blockingSyscalls = map[string]bool{ |
| "port_wait": true, |
| "object_wait_one": true, |
| "object_wait_many": true, |
| } |
| |
| func printAsm(buf *bytes.Buffer, def abigen.SysDef) { |
| // The summed size of all arguments |
| argSize := 0 |
| for _, arg := range def.Args { |
| sz := arg.Type.Size() |
| if *goarch == "arm64" && sz == 1 { |
| sz = 8 |
| } |
| for argSize%sz != 0 { |
| // Add padding until the 'argSize' is aligned to the type we are adding |
| argSize++ |
| } |
| argSize += sz |
| } |
| if argSize%8 == 4 { |
| // Force the return argument on the stack to be 8-byte aligned, not 4-byte aligned |
| argSize += 4 |
| } |
| retSize := def.Ret.Size() |
| |
| var frameSize int |
| var regArgs []string |
| var callIns, retReg, suffix4, suffix8 string |
| switch *goarch { |
| case "amd64": |
| regArgs = amd64RegArgs |
| callIns = "CALL" |
| retReg = "AX" |
| suffix8 = "Q" |
| suffix4 = "L" |
| switch len(def.Args) { |
| case 7: |
| frameSize = 16 + 8 |
| case 8: |
| frameSize = 16 + 2*8 |
| } |
| case "arm64": |
| regArgs = arm64RegArgs |
| callIns = "BL" |
| retReg = "R0" |
| suffix8 = "D" |
| suffix4 = "W" |
| default: |
| panic(fmt.Sprintf("goarch=%s not supported", *goarch)) |
| } |
| |
| fmt.Fprintf(buf, "TEXT ·%s(SB),NOSPLIT,$%d-%d\n", def.GoName(), frameSize, argSize+retSize) |
| |
| if _, ok := blockingSyscalls[def.Name]; ok { |
| fmt.Fprintf(buf, "\tCALL runtime·entersyscall(SB)\n") |
| } |
| off := 0 |
| for i, arg := range def.Args { |
| name := arg.Name |
| suffix := suffix8 |
| t := arg.Type |
| sz := t.Size() |
| if sz == 4 { |
| suffix = suffix4 |
| } else if *goarch == "arm64" && sz == 1 { |
| sz = 8 |
| } |
| for off%sz != 0 { |
| // Add padding until the offset is aligned to the type we are accessing |
| off++ |
| } |
| |
| fmt.Fprintf(buf, "\tMOV%s %s+%d(FP), %s\n", suffix, name, off, regArgs[i]) |
| off += sz |
| } |
| switch *goarch { |
| case "amd64": |
| if len(def.Args) >= 7 { |
| fmt.Fprintf(buf, "\tMOVQ SP, BP // BP is preserved across vsdo call by the x86-64 ABI\n") |
| fmt.Fprintf(buf, "\tANDQ $~15, SP // stack alignment for x86-64 ABI\n") |
| if len(def.Args) == 8 { |
| fmt.Fprintf(buf, "\tPUSHQ R13\n") |
| } |
| fmt.Fprintf(buf, "\tPUSHQ R12\n") |
| } |
| fmt.Fprintf(buf, "\tMOVQ vdso_zx_%s(SB), AX\n", def.Name) |
| fmt.Fprintf(buf, "\tCALL AX\n") |
| if len(def.Args) >= 7 { |
| fmt.Fprintf(buf, "\tPOPQ R12\n") |
| if len(def.Args) == 8 { |
| fmt.Fprintf(buf, "\tPOPQ R13\n") |
| } |
| fmt.Fprintf(buf, "\tMOVQ BP, SP\n") |
| } |
| case "arm64": |
| fmt.Fprintf(buf, "\tBL vdso_zx_%s(SB)\n", def.Name) |
| } |
| if retSize := def.Ret.Size(); retSize > 0 { |
| suffix := suffix8 |
| if retSize == 4 { |
| suffix = suffix4 |
| } |
| fmt.Fprintf(buf, "\tMOV%s %s, ret+%d(FP)\n", suffix, retReg, argSize) |
| } |
| if _, ok := blockingSyscalls[def.Name]; ok { |
| fmt.Fprintf(buf, "\t%s runtime·exitsyscall(SB)\n", callIns) |
| } |
| fmt.Fprintf(buf, "\tRET\n") |
| } |