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
35 my $newtags = ""; # new style build tags
36 my $extraimports = "";
38 if($ARGV[0] eq "-b32") {
39 $_32bit = "big-endian";
41 } elsif($ARGV[0] eq "-l32") {
42 $_32bit = "little-endian";
45 if($ARGV[0] eq "-plan9") {
49 if($ARGV[0] eq "-darwin") {
54 if($ARGV[0] eq "-openbsd") {
58 if($ARGV[0] eq "-netbsd") {
62 if($ARGV[0] eq "-dragonfly") {
66 if($ARGV[0] eq "-arm") {
70 if($ARGV[0] eq "-libc") {
74 if($ARGV[0] eq "-tags") {
80 if($ARGV[0] =~ /^-/) {
81 print STDERR "usage: mksyscall.pl [-b32 | -l32] [-tags x,y] [file ...]\n";
86 $extraimports = 'import "internal/abi"';
89 sub parseparamlist($) {
96 return split(/\s*,\s*/, $list);
101 if($p !~ /^(\S*) (\S*)$/) {
102 print STDERR "$ARGV:$.: malformed parameter: $p\n";
104 return ("xx", "int");
109 # set of trampolines we've already generated
118 my $nonblock = /^\/\/sysnb /;
119 next if !/^\/\/sys / && !$nonblock;
121 # Line must be of the form
122 # func Open(path string, mode int, perm int) (fd int, errno error)
123 # Split into name, in params, out params.
124 if(!/^\/\/sys(nb)? (\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*((?i)_?SYS_[A-Z0-9_]+))?$/) {
125 print STDERR "$ARGV:$.: malformed //sys declaration\n";
129 my ($func, $in, $out, $sysname) = ($2, $3, $4, $5);
131 # Split argument lists on comma.
132 my @in = parseparamlist($in);
133 my @out = parseparamlist($out);
135 # Try in vain to keep people from editing this file.
136 # The theory is that they jump into the middle of the file
137 # without reading the header.
138 $text .= "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n";
140 if (($darwin && $func =~ /^ptrace1(Ptr)?$/) || (($openbsd && $libc) && $func =~ /^ptrace(Ptr)?$/)) {
141 # The ptrace function is called from forkAndExecInChild where stack
142 # growth is forbidden.
143 $text .= "//go:nosplit\n"
146 # Go function header.
147 my $out_decl = @out ? sprintf(" (%s)", join(', ', @out)) : "";
148 $text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out_decl;
150 # Check if err return available
152 foreach my $p (@out) {
153 my ($name, $type) = parseparam($p);
154 if($type eq "error") {
160 # Prepare arguments to Syscall.
163 foreach my $p (@in) {
164 my ($name, $type) = parseparam($p);
166 push @args, "uintptr(unsafe.Pointer($name))";
167 } elsif($type eq "string" && $errvar ne "") {
168 $text .= "\tvar _p$n *byte\n";
169 $text .= "\t_p$n, $errvar = BytePtrFromString($name)\n";
170 $text .= "\tif $errvar != nil {\n\t\treturn\n\t}\n";
171 push @args, "uintptr(unsafe.Pointer(_p$n))";
173 } elsif($type eq "string") {
174 print STDERR "$ARGV:$.: $func uses string arguments, but has no error return\n";
175 $text .= "\tvar _p$n *byte\n";
176 $text .= "\t_p$n, _ = BytePtrFromString($name)\n";
177 push @args, "uintptr(unsafe.Pointer(_p$n))";
179 } elsif($type =~ /^\[\](.*)/) {
180 # Convert slice into pointer, length.
181 # Have to be careful not to take address of &a[0] if len == 0:
182 # pass dummy pointer in that case.
183 # Used to pass nil, but some OSes or simulators reject write(fd, nil, 0).
184 $text .= "\tvar _p$n unsafe.Pointer\n";
185 $text .= "\tif len($name) > 0 {\n\t\t_p$n = unsafe.Pointer(\&${name}[0])\n\t}";
186 $text .= " else {\n\t\t_p$n = unsafe.Pointer(&_zero)\n\t}";
188 push @args, "uintptr(_p$n)", "uintptr(len($name))";
190 } elsif($type eq "int64" && ($openbsd || $netbsd)) {
194 if($libc && $arm && @args % 2) {
195 # arm abi specifies 64 bit argument must be 64 bit aligned.
198 if($_32bit eq "big-endian") {
199 push @args, "uintptr($name>>32)", "uintptr($name)";
200 } elsif($_32bit eq "little-endian") {
201 push @args, "uintptr($name)", "uintptr($name>>32)";
203 push @args, "uintptr($name)";
205 } elsif($type eq "int64" && $dragonfly) {
206 if ($func !~ /^extp(read|write)/i) {
209 if($_32bit eq "big-endian") {
210 push @args, "uintptr($name>>32)", "uintptr($name)";
211 } elsif($_32bit eq "little-endian") {
212 push @args, "uintptr($name)", "uintptr($name>>32)";
214 push @args, "uintptr($name)";
216 } elsif($type eq "int64" && $_32bit ne "") {
217 if(@args % 2 && $arm) {
218 # arm abi specifies 64-bit argument uses
222 if($_32bit eq "big-endian") {
223 push @args, "uintptr($name>>32)", "uintptr($name)";
225 push @args, "uintptr($name)", "uintptr($name>>32)";
228 push @args, "uintptr($name)";
232 # Determine which form to use; pad args with zeros.
235 if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
236 $asm = "rawSyscallNoError";
242 # Call unexported syscall functions (which take
243 # libc functions instead of syscall numbers).
244 $asm = lcfirst($asm);
250 } elsif(@args <= 6) {
255 } elsif(@args <= 9) {
261 print STDERR "$ARGV:$.: too many arguments to system call\n";
264 if ($darwin || ($openbsd && $libc)) {
265 # Use extended versions for calls that generate a 64-bit result.
266 my ($name, $type) = parseparam($out[0]);
267 if ($type eq "int64" || ($type eq "uintptr" && $_32bit eq "")) {
272 # System call number.
275 $sysname = "SYS_$func";
276 $sysname =~ s/([a-z])([A-Z])/${1}_$2/g; # turn FooBar into Foo_Bar
277 $sysname =~ y/a-z/A-Z/;
279 $sysname =~ y/A-Z/a-z/;
280 $sysname = substr $sysname, 4;
281 $funcname = "libc_$sysname";
285 if($funcname eq "") {
286 $sysname = substr $sysname, 4;
287 $sysname =~ y/A-Z/a-z/;
288 $funcname = "libc_$sysname";
290 $sysname = "abi.FuncPCABI0(${funcname}_trampoline)";
294 my $args = join(', ', @args);
295 my $call = "$asm($sysname, $args)";
297 # Assign return values.
299 my @ret = ("_", "_", "_");
301 for(my $i=0; $i<@out; $i++) {
303 my ($name, $type) = parseparam($p);
305 if($name eq "err" && !$plan9) {
309 } elsif($name eq "err" && $plan9) {
314 $reg = sprintf("r%d", $i);
317 if($type eq "bool") {
320 if($type eq "int64" && $_32bit ne "") {
321 # 64-bit number in r1:r0 or r0:r1.
323 print STDERR "$ARGV:$.: not enough registers for int64 return\n";
325 if($_32bit eq "big-endian") {
326 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1);
328 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i);
330 $ret[$i] = sprintf("r%d", $i);
331 $ret[$i+1] = sprintf("r%d", $i+1);
333 if($reg ne "e1" || $plan9) {
334 $body .= "\t$name = $type($reg)\n";
337 if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") {
338 $text .= "\t$call\n";
340 if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
341 # raw syscall without error on Linux, see golang.org/issue/22924
342 $text .= "\t$ret[0], $ret[1] := $call\n";
344 $text .= "\t$ret[0], $ret[1], $ret[2] := $call\n";
349 if ($plan9 && $ret[2] eq "e1") {
350 $text .= "\tif int32(r0) == -1 {\n";
351 $text .= "\t\terr = e1\n";
353 } elsif ($do_errno) {
354 $text .= "\tif e1 != 0 {\n";
355 $text .= "\t\terr = errnoErr(e1)\n";
358 $text .= "\treturn\n";
361 if (not exists $trampolines{$funcname}) {
362 $trampolines{$funcname} = 1;
363 # The assembly trampoline that jumps to the libc routine.
364 $text .= "func ${funcname}_trampoline()\n\n";
365 # Tell the linker that funcname can be found in libSystem using varname without the libc_ prefix.
366 my $basename = substr $funcname, 5;
367 my $libc = "libc.so";
369 $libc = "/usr/lib/libSystem.B.dylib";
371 $text .= "//go:cgo_import_dynamic $funcname $basename \"$libc\"\n\n";
383 # TODO: this assumes tags are just simply comma separated. For now this is all the uses.
384 $newtags = $tags =~ s/,/ && /r;
388 // Code generated by the command above; DO NOT EDIT.