2 # Copyright 2009 The Go Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style
4 # license that can be found in the LICENSE file.
6 # This program reads a file containing function prototypes
7 # (like syscall_darwin.go) and generates system call bodies.
8 # The prototypes are marked by lines beginning with "//sys"
9 # and read like func declarations if //sys is replaced by func, but:
10 # * The parameter lists must give a name for each argument.
11 # This includes return parameters.
12 # * The parameter lists must give a type for each argument:
13 # the (x, y, z int) shorthand is not allowed.
14 # * If the return parameter is an error number, it must be named errno.
16 # A line beginning with //sysnb is like //sys, except that the
17 # goroutine will not be suspended during the execution of the system
18 # call. This must only be used for system calls which can never
19 # block, as otherwise the system call could cause all goroutines to
24 my $cmdline = "mksyscall.pl " . join(' ', @ARGV);
32 my $arm = 0; # 64-bit value should use (even, odd)-pair
33 my $tags = ""; # build tags
35 if($ARGV[0] eq "-b32") {
36 $_32bit = "big-endian";
38 } elsif($ARGV[0] eq "-l32") {
39 $_32bit = "little-endian";
42 if($ARGV[0] eq "-plan9") {
46 if($ARGV[0] eq "-darwin") {
50 if($ARGV[0] eq "-openbsd") {
54 if($ARGV[0] eq "-netbsd") {
58 if($ARGV[0] eq "-dragonfly") {
62 if($ARGV[0] eq "-arm") {
66 if($ARGV[0] eq "-tags") {
72 if($ARGV[0] =~ /^-/) {
73 print STDERR "usage: mksyscall.pl [-b32 | -l32] [-tags x,y] [file ...]\n";
77 sub parseparamlist($) {
84 return split(/\s*,\s*/, $list);
89 if($p !~ /^(\S*) (\S*)$/) {
90 print STDERR "$ARGV:$.: malformed parameter: $p\n";
97 # set of trampolines we've already generated
106 my $nonblock = /^\/\/sysnb /;
107 next if !/^\/\/sys / && !$nonblock;
109 # Line must be of the form
110 # func Open(path string, mode int, perm int) (fd int, errno error)
111 # Split into name, in params, out params.
112 if(!/^\/\/sys(nb)? (\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*((?i)_?SYS_[A-Z0-9_]+))?$/) {
113 print STDERR "$ARGV:$.: malformed //sys declaration\n";
117 my ($func, $in, $out, $sysname) = ($2, $3, $4, $5);
119 # Split argument lists on comma.
120 my @in = parseparamlist($in);
121 my @out = parseparamlist($out);
123 # Try in vain to keep people from editing this file.
124 # The theory is that they jump into the middle of the file
125 # without reading the header.
126 $text .= "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n";
128 if ($darwin && $func eq "ptrace1") {
129 # The ptrace function is called from forkAndExecInChild where stack
130 # growth is forbidden.
131 $text .= "//go:nosplit\n"
134 # Go function header.
135 my $out_decl = @out ? sprintf(" (%s)", join(', ', @out)) : "";
136 $text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out_decl;
138 # Check if err return available
140 foreach my $p (@out) {
141 my ($name, $type) = parseparam($p);
142 if($type eq "error") {
148 # Prepare arguments to Syscall.
151 foreach my $p (@in) {
152 my ($name, $type) = parseparam($p);
154 push @args, "uintptr(unsafe.Pointer($name))";
155 } elsif($type eq "string" && $errvar ne "") {
156 $text .= "\tvar _p$n *byte\n";
157 $text .= "\t_p$n, $errvar = BytePtrFromString($name)\n";
158 $text .= "\tif $errvar != nil {\n\t\treturn\n\t}\n";
159 push @args, "uintptr(unsafe.Pointer(_p$n))";
161 } elsif($type eq "string") {
162 print STDERR "$ARGV:$.: $func uses string arguments, but has no error return\n";
163 $text .= "\tvar _p$n *byte\n";
164 $text .= "\t_p$n, _ = BytePtrFromString($name)\n";
165 push @args, "uintptr(unsafe.Pointer(_p$n))";
167 } elsif($type =~ /^\[\](.*)/) {
168 # Convert slice into pointer, length.
169 # Have to be careful not to take address of &a[0] if len == 0:
170 # pass dummy pointer in that case.
171 # Used to pass nil, but some OSes or simulators reject write(fd, nil, 0).
172 $text .= "\tvar _p$n unsafe.Pointer\n";
173 $text .= "\tif len($name) > 0 {\n\t\t_p$n = unsafe.Pointer(\&${name}[0])\n\t}";
174 $text .= " else {\n\t\t_p$n = unsafe.Pointer(&_zero)\n\t}";
176 push @args, "uintptr(_p$n)", "uintptr(len($name))";
178 } elsif($type eq "int64" && ($openbsd || $netbsd)) {
180 if($_32bit eq "big-endian") {
181 push @args, "uintptr($name>>32)", "uintptr($name)";
182 } elsif($_32bit eq "little-endian") {
183 push @args, "uintptr($name)", "uintptr($name>>32)";
185 push @args, "uintptr($name)";
187 } elsif($type eq "int64" && $dragonfly) {
188 if ($func !~ /^extp(read|write)/i) {
191 if($_32bit eq "big-endian") {
192 push @args, "uintptr($name>>32)", "uintptr($name)";
193 } elsif($_32bit eq "little-endian") {
194 push @args, "uintptr($name)", "uintptr($name>>32)";
196 push @args, "uintptr($name)";
198 } elsif($type eq "int64" && $_32bit ne "") {
199 if(@args % 2 && $arm) {
200 # arm abi specifies 64-bit argument uses
204 if($_32bit eq "big-endian") {
205 push @args, "uintptr($name>>32)", "uintptr($name)";
207 push @args, "uintptr($name)", "uintptr($name>>32)";
210 push @args, "uintptr($name)";
214 # Determine which form to use; pad args with zeros.
217 if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
218 $asm = "rawSyscallNoError";
224 # Call unexported syscall functions (which take
225 # libc functions instead of syscall numbers).
226 $asm = lcfirst($asm);
232 } elsif(@args <= 6) {
237 } elsif(@args <= 9) {
243 print STDERR "$ARGV:$.: too many arguments to system call\n";
247 # Use extended versions for calls that generate a 64-bit result.
248 my ($name, $type) = parseparam($out[0]);
249 if ($type eq "int64" || ($type eq "uintptr" && $_32bit eq "")) {
254 # System call number.
257 $sysname = "SYS_$func";
258 $sysname =~ s/([a-z])([A-Z])/${1}_$2/g; # turn FooBar into Foo_Bar
259 $sysname =~ y/a-z/A-Z/;
261 $sysname =~ y/A-Z/a-z/;
262 $sysname = substr $sysname, 4;
263 $funcname = "libc_$sysname";
267 if($funcname eq "") {
268 $sysname = substr $sysname, 4;
269 $funcname = "libc_$sysname";
271 $sysname = "funcPC(${funcname}_trampoline)";
275 my $args = join(', ', @args);
276 my $call = "$asm($sysname, $args)";
278 # Assign return values.
280 my @ret = ("_", "_", "_");
282 for(my $i=0; $i<@out; $i++) {
284 my ($name, $type) = parseparam($p);
286 if($name eq "err" && !$plan9) {
290 } elsif($name eq "err" && $plan9) {
295 $reg = sprintf("r%d", $i);
298 if($type eq "bool") {
301 if($type eq "int64" && $_32bit ne "") {
302 # 64-bit number in r1:r0 or r0:r1.
304 print STDERR "$ARGV:$.: not enough registers for int64 return\n";
306 if($_32bit eq "big-endian") {
307 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1);
309 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i);
311 $ret[$i] = sprintf("r%d", $i);
312 $ret[$i+1] = sprintf("r%d", $i+1);
314 if($reg ne "e1" || $plan9) {
315 $body .= "\t$name = $type($reg)\n";
318 if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") {
319 $text .= "\t$call\n";
321 if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
322 # raw syscall without error on Linux, see golang.org/issue/22924
323 $text .= "\t$ret[0], $ret[1] := $call\n";
325 $text .= "\t$ret[0], $ret[1], $ret[2] := $call\n";
330 if ($plan9 && $ret[2] eq "e1") {
331 $text .= "\tif int32(r0) == -1 {\n";
332 $text .= "\t\terr = e1\n";
334 } elsif ($do_errno) {
335 $text .= "\tif e1 != 0 {\n";
336 $text .= "\t\terr = errnoErr(e1)\n";
339 $text .= "\treturn\n";
342 if (not exists $trampolines{$funcname}) {
343 $trampolines{$funcname} = 1;
344 # The assembly trampoline that jumps to the libc routine.
345 $text .= "func ${funcname}_trampoline()\n";
346 # Tell the linker that funcname can be found in libSystem using varname without the libc_ prefix.
347 my $basename = substr $funcname, 5;
348 $text .= "//go:cgo_import_dynamic $funcname $basename \"/usr/lib/libSystem.B.dylib\"\n\n";
362 // Code generated by the command above; DO NOT EDIT.