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
34 my $tags = ""; # build tags
36 if($ARGV[0] eq "-b32") {
37 $_32bit = "big-endian";
39 } elsif($ARGV[0] eq "-l32") {
40 $_32bit = "little-endian";
43 if($ARGV[0] eq "-plan9") {
47 if($ARGV[0] eq "-darwin") {
52 if($ARGV[0] eq "-openbsd") {
56 if($ARGV[0] eq "-netbsd") {
60 if($ARGV[0] eq "-dragonfly") {
64 if($ARGV[0] eq "-arm") {
68 if($ARGV[0] eq "-libc") {
72 if($ARGV[0] eq "-tags") {
78 if($ARGV[0] =~ /^-/) {
79 print STDERR "usage: mksyscall.pl [-b32 | -l32] [-tags x,y] [file ...]\n";
83 sub parseparamlist($) {
90 return split(/\s*,\s*/, $list);
95 if($p !~ /^(\S*) (\S*)$/) {
96 print STDERR "$ARGV:$.: malformed parameter: $p\n";
103 # set of trampolines we've already generated
112 my $nonblock = /^\/\/sysnb /;
113 next if !/^\/\/sys / && !$nonblock;
115 # Line must be of the form
116 # func Open(path string, mode int, perm int) (fd int, errno error)
117 # Split into name, in params, out params.
118 if(!/^\/\/sys(nb)? (\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*((?i)_?SYS_[A-Z0-9_]+))?$/) {
119 print STDERR "$ARGV:$.: malformed //sys declaration\n";
123 my ($func, $in, $out, $sysname) = ($2, $3, $4, $5);
125 # Split argument lists on comma.
126 my @in = parseparamlist($in);
127 my @out = parseparamlist($out);
129 # Try in vain to keep people from editing this file.
130 # The theory is that they jump into the middle of the file
131 # without reading the header.
132 $text .= "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n";
134 if (($darwin || ($openbsd && $libc)) && $func eq "ptrace") {
135 # The ptrace function is called from forkAndExecInChild where stack
136 # growth is forbidden.
137 $text .= "//go:nosplit\n"
140 # Go function header.
141 my $out_decl = @out ? sprintf(" (%s)", join(', ', @out)) : "";
142 $text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out_decl;
144 # Check if err return available
146 foreach my $p (@out) {
147 my ($name, $type) = parseparam($p);
148 if($type eq "error") {
154 # Prepare arguments to Syscall.
157 foreach my $p (@in) {
158 my ($name, $type) = parseparam($p);
160 push @args, "uintptr(unsafe.Pointer($name))";
161 } elsif($type eq "string" && $errvar ne "") {
162 $text .= "\tvar _p$n *byte\n";
163 $text .= "\t_p$n, $errvar = BytePtrFromString($name)\n";
164 $text .= "\tif $errvar != nil {\n\t\treturn\n\t}\n";
165 push @args, "uintptr(unsafe.Pointer(_p$n))";
167 } elsif($type eq "string") {
168 print STDERR "$ARGV:$.: $func uses string arguments, but has no error return\n";
169 $text .= "\tvar _p$n *byte\n";
170 $text .= "\t_p$n, _ = BytePtrFromString($name)\n";
171 push @args, "uintptr(unsafe.Pointer(_p$n))";
173 } elsif($type =~ /^\[\](.*)/) {
174 # Convert slice into pointer, length.
175 # Have to be careful not to take address of &a[0] if len == 0:
176 # pass dummy pointer in that case.
177 # Used to pass nil, but some OSes or simulators reject write(fd, nil, 0).
178 $text .= "\tvar _p$n unsafe.Pointer\n";
179 $text .= "\tif len($name) > 0 {\n\t\t_p$n = unsafe.Pointer(\&${name}[0])\n\t}";
180 $text .= " else {\n\t\t_p$n = unsafe.Pointer(&_zero)\n\t}";
182 push @args, "uintptr(_p$n)", "uintptr(len($name))";
184 } elsif($type eq "int64" && ($openbsd || $netbsd)) {
188 if($_32bit eq "big-endian") {
189 push @args, "uintptr($name>>32)", "uintptr($name)";
190 } elsif($_32bit eq "little-endian") {
191 push @args, "uintptr($name)", "uintptr($name>>32)";
193 push @args, "uintptr($name)";
195 } elsif($type eq "int64" && $dragonfly) {
196 if ($func !~ /^extp(read|write)/i) {
199 if($_32bit eq "big-endian") {
200 push @args, "uintptr($name>>32)", "uintptr($name)";
201 } elsif($_32bit eq "little-endian") {
202 push @args, "uintptr($name)", "uintptr($name>>32)";
204 push @args, "uintptr($name)";
206 } elsif($type eq "int64" && $_32bit ne "") {
207 if(@args % 2 && $arm) {
208 # arm abi specifies 64-bit argument uses
212 if($_32bit eq "big-endian") {
213 push @args, "uintptr($name>>32)", "uintptr($name)";
215 push @args, "uintptr($name)", "uintptr($name>>32)";
218 push @args, "uintptr($name)";
222 # Determine which form to use; pad args with zeros.
225 if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
226 $asm = "rawSyscallNoError";
232 # Call unexported syscall functions (which take
233 # libc functions instead of syscall numbers).
234 $asm = lcfirst($asm);
240 } elsif(@args <= 6) {
245 } elsif(@args <= 9) {
251 print STDERR "$ARGV:$.: too many arguments to system call\n";
254 if ($darwin || ($openbsd && $libc)) {
255 # Use extended versions for calls that generate a 64-bit result.
256 my ($name, $type) = parseparam($out[0]);
257 if ($type eq "int64" || ($type eq "uintptr" && $_32bit eq "")) {
262 # System call number.
265 $sysname = "SYS_$func";
266 $sysname =~ s/([a-z])([A-Z])/${1}_$2/g; # turn FooBar into Foo_Bar
267 $sysname =~ y/a-z/A-Z/;
269 $sysname =~ y/A-Z/a-z/;
270 $sysname = substr $sysname, 4;
271 $funcname = "libc_$sysname";
275 if($funcname eq "") {
276 $sysname = substr $sysname, 4;
277 $funcname = "libc_$sysname";
279 $sysname = "funcPC(${funcname}_trampoline)";
283 my $args = join(', ', @args);
284 my $call = "$asm($sysname, $args)";
286 # Assign return values.
288 my @ret = ("_", "_", "_");
290 for(my $i=0; $i<@out; $i++) {
292 my ($name, $type) = parseparam($p);
294 if($name eq "err" && !$plan9) {
298 } elsif($name eq "err" && $plan9) {
303 $reg = sprintf("r%d", $i);
306 if($type eq "bool") {
309 if($type eq "int64" && $_32bit ne "") {
310 # 64-bit number in r1:r0 or r0:r1.
312 print STDERR "$ARGV:$.: not enough registers for int64 return\n";
314 if($_32bit eq "big-endian") {
315 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1);
317 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i);
319 $ret[$i] = sprintf("r%d", $i);
320 $ret[$i+1] = sprintf("r%d", $i+1);
322 if($reg ne "e1" || $plan9) {
323 $body .= "\t$name = $type($reg)\n";
326 if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") {
327 $text .= "\t$call\n";
329 if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
330 # raw syscall without error on Linux, see golang.org/issue/22924
331 $text .= "\t$ret[0], $ret[1] := $call\n";
333 $text .= "\t$ret[0], $ret[1], $ret[2] := $call\n";
338 if ($plan9 && $ret[2] eq "e1") {
339 $text .= "\tif int32(r0) == -1 {\n";
340 $text .= "\t\terr = e1\n";
342 } elsif ($do_errno) {
343 $text .= "\tif e1 != 0 {\n";
344 $text .= "\t\terr = errnoErr(e1)\n";
347 $text .= "\treturn\n";
350 if (not exists $trampolines{$funcname}) {
351 $trampolines{$funcname} = 1;
352 # The assembly trampoline that jumps to the libc routine.
353 $text .= "func ${funcname}_trampoline()\n";
354 # Map syscall.funcname to just plain funcname.
355 # (The jump to this function is in the assembly trampoline, generated by mkasm.go.)
356 $text .= "//go:linkname $funcname $funcname\n";
357 # Tell the linker that funcname can be found in libSystem using varname without the libc_ prefix.
358 my $basename = substr $funcname, 5;
359 my $libc = "libc.so";
361 $libc = "/usr/lib/libSystem.B.dylib";
363 $text .= "//go:cgo_import_dynamic $funcname $basename \"$libc\"\n\n";
377 // Code generated by the command above; DO NOT EDIT.