| // 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.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") |
| } |
| |
| func printAsm(buf *bytes.Buffer, def abigen.SysDef) { |
| fmt.Fprintf(buf, "TEXT ·%s(SB),NOSPLIT,$0\n", def.GoName()) |
| fmt.Fprintf(buf, "\tJMP runtime·vdsoCall_zx_%s(SB)\n", def.Name) |
| } |