]> Cypherpunks.ru repositories - gostls13.git/blob - src/syscall/mksyscall.pl
[dev.regabi] all: merge master (fca94ab) into dev.regabi
[gostls13.git] / src / syscall / mksyscall.pl
1 #!/usr/bin/env perl
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.
5
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.
15
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
20 # hang.
21
22 use strict;
23
24 my $cmdline = "mksyscall.pl " . join(' ', @ARGV);
25 my $errors = 0;
26 my $_32bit = "";
27 my $plan9 = 0;
28 my $darwin = 0;
29 my $openbsd = 0;
30 my $netbsd = 0;
31 my $dragonfly = 0;
32 my $arm = 0; # 64-bit value should use (even, odd)-pair
33 my $libc = 0;
34 my $tags = "";  # build tags
35
36 if($ARGV[0] eq "-b32") {
37         $_32bit = "big-endian";
38         shift;
39 } elsif($ARGV[0] eq "-l32") {
40         $_32bit = "little-endian";
41         shift;
42 }
43 if($ARGV[0] eq "-plan9") {
44         $plan9 = 1;
45         shift;
46 }
47 if($ARGV[0] eq "-darwin") {
48         $darwin = 1;
49         $libc = 1;
50         shift;
51 }
52 if($ARGV[0] eq "-openbsd") {
53         $openbsd = 1;
54         shift;
55 }
56 if($ARGV[0] eq "-netbsd") {
57         $netbsd = 1;
58         shift;
59 }
60 if($ARGV[0] eq "-dragonfly") {
61         $dragonfly = 1;
62         shift;
63 }
64 if($ARGV[0] eq "-arm") {
65         $arm = 1;
66         shift;
67 }
68 if($ARGV[0] eq "-libc") {
69         $libc = 1;
70         shift;
71 }
72 if($ARGV[0] eq "-tags") {
73         shift;
74         $tags = $ARGV[0];
75         shift;
76 }
77
78 if($ARGV[0] =~ /^-/) {
79         print STDERR "usage: mksyscall.pl [-b32 | -l32] [-tags x,y] [file ...]\n";
80         exit 1;
81 }
82
83 sub parseparamlist($) {
84         my ($list) = @_;
85         $list =~ s/^\s*//;
86         $list =~ s/\s*$//;
87         if($list eq "") {
88                 return ();
89         }
90         return split(/\s*,\s*/, $list);
91 }
92
93 sub parseparam($) {
94         my ($p) = @_;
95         if($p !~ /^(\S*) (\S*)$/) {
96                 print STDERR "$ARGV:$.: malformed parameter: $p\n";
97                 $errors = 1;
98                 return ("xx", "int");
99         }
100         return ($1, $2);
101 }
102
103 # set of trampolines we've already generated
104 my %trampolines;
105
106 my $text = "";
107 while(<>) {
108         chomp;
109         s/\s+/ /g;
110         s/^\s+//;
111         s/\s+$//;
112         my $nonblock = /^\/\/sysnb /;
113         next if !/^\/\/sys / && !$nonblock;
114
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";
120                 $errors = 1;
121                 next;
122         }
123         my ($func, $in, $out, $sysname) = ($2, $3, $4, $5);
124
125         # Split argument lists on comma.
126         my @in = parseparamlist($in);
127         my @out = parseparamlist($out);
128
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";
133
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"
138         }
139
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;
143
144         # Check if err return available
145         my $errvar = "";
146         foreach my $p (@out) {
147                 my ($name, $type) = parseparam($p);
148                 if($type eq "error") {
149                         $errvar = $name;
150                         last;
151                 }
152         }
153
154         # Prepare arguments to Syscall.
155         my @args = ();
156         my $n = 0;
157         foreach my $p (@in) {
158                 my ($name, $type) = parseparam($p);
159                 if($type =~ /^\*/) {
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))";
166                         $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))";
172                         $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}";
181                         $text .= "\n";
182                         push @args, "uintptr(_p$n)", "uintptr(len($name))";
183                         $n++;
184                 } elsif($type eq "int64" && ($openbsd || $netbsd)) {
185                         if (!$libc) {
186                                 push @args, "0";
187                         }
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)";
192                         } else {
193                                 push @args, "uintptr($name)";
194                         }
195                 } elsif($type eq "int64" && $dragonfly) {
196                         if ($func !~ /^extp(read|write)/i) {
197                                 push @args, "0";
198                         }
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)";
203                         } else {
204                                 push @args, "uintptr($name)";
205                         }
206                 } elsif($type eq "int64" && $_32bit ne "") {
207                         if(@args % 2 && $arm) {
208                                 # arm abi specifies 64-bit argument uses
209                                 # (even, odd) pair
210                                 push @args, "0"
211                         }
212                         if($_32bit eq "big-endian") {
213                                 push @args, "uintptr($name>>32)", "uintptr($name)";
214                         } else {
215                                 push @args, "uintptr($name)", "uintptr($name>>32)";
216                         }
217                 } else {
218                         push @args, "uintptr($name)";
219                 }
220         }
221
222         # Determine which form to use; pad args with zeros.
223         my $asm = "Syscall";
224         if ($nonblock) {
225                 if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
226                         $asm = "rawSyscallNoError";
227                 } else {
228                         $asm = "RawSyscall";
229                 }
230         }
231         if ($libc) {
232                 # Call unexported syscall functions (which take
233                 # libc functions instead of syscall numbers).
234                 $asm = lcfirst($asm);
235         }
236         if(@args <= 3) {
237                 while(@args < 3) {
238                         push @args, "0";
239                 }
240         } elsif(@args <= 6) {
241                 $asm .= "6";
242                 while(@args < 6) {
243                         push @args, "0";
244                 }
245         } elsif(@args <= 9) {
246                 $asm .= "9";
247                 while(@args < 9) {
248                         push @args, "0";
249                 }
250         } else {
251                 print STDERR "$ARGV:$.: too many arguments to system call\n";
252         }
253
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 "")) {
258                         $asm .= "X";
259                 }
260         }
261
262         # System call number.
263         my $funcname = "";
264         if($sysname eq "") {
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/;
268                 if($libc) {
269                         $sysname =~ y/A-Z/a-z/;
270                         $sysname = substr $sysname, 4;
271                         $funcname = "libc_$sysname";
272                 }
273         }
274         if($libc) {
275                 if($funcname eq "") {
276                         $sysname = substr $sysname, 4;
277                         $funcname = "libc_$sysname";
278                 }
279                 $sysname = "funcPC(${funcname}_trampoline)";
280         }
281
282         # Actual call.
283         my $args = join(', ', @args);
284         my $call = "$asm($sysname, $args)";
285
286         # Assign return values.
287         my $body = "";
288         my @ret = ("_", "_", "_");
289         my $do_errno = 0;
290         for(my $i=0; $i<@out; $i++) {
291                 my $p = $out[$i];
292                 my ($name, $type) = parseparam($p);
293                 my $reg = "";
294                 if($name eq "err" && !$plan9) {
295                         $reg = "e1";
296                         $ret[2] = $reg;
297                         $do_errno = 1;
298                 } elsif($name eq "err" && $plan9) {
299                         $ret[0] = "r0";
300                         $ret[2] = "e1";
301                         next;
302                 } else {
303                         $reg = sprintf("r%d", $i);
304                         $ret[$i] = $reg;
305                 }
306                 if($type eq "bool") {
307                         $reg = "$reg != 0";
308                 }
309                 if($type eq "int64" && $_32bit ne "") {
310                         # 64-bit number in r1:r0 or r0:r1.
311                         if($i+2 > @out) {
312                                 print STDERR "$ARGV:$.: not enough registers for int64 return\n";
313                         }
314                         if($_32bit eq "big-endian") {
315                                 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1);
316                         } else {
317                                 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i);
318                         }
319                         $ret[$i] = sprintf("r%d", $i);
320                         $ret[$i+1] = sprintf("r%d", $i+1);
321                 }
322                 if($reg ne "e1" || $plan9) {
323                         $body .= "\t$name = $type($reg)\n";
324                 }
325         }
326         if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") {
327                 $text .= "\t$call\n";
328         } else {
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";
332                 } else {
333                         $text .= "\t$ret[0], $ret[1], $ret[2] := $call\n";
334                 }
335         }
336         $text .= $body;
337
338         if ($plan9 && $ret[2] eq "e1") {
339                 $text .= "\tif int32(r0) == -1 {\n";
340                 $text .= "\t\terr = e1\n";
341                 $text .= "\t}\n";
342         } elsif ($do_errno) {
343                 $text .= "\tif e1 != 0 {\n";
344                 $text .= "\t\terr = errnoErr(e1)\n";
345                 $text .= "\t}\n";
346         }
347         $text .= "\treturn\n";
348         $text .= "}\n\n";
349         if($libc) {
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                         # Tell the linker that funcname can be found in libSystem using varname without the libc_ prefix.
355                         my $basename = substr $funcname, 5;
356                         my $libc = "libc.so";
357                         if ($darwin) {
358                                 $libc = "/usr/lib/libSystem.B.dylib";
359                         }
360                         $text .= "//go:cgo_import_dynamic $funcname $basename \"$libc\"\n\n";
361                 }
362         }
363 }
364
365 chomp $text;
366 chomp $text;
367
368 if($errors) {
369         exit 1;
370 }
371
372 print <<EOF;
373 // $cmdline
374 // Code generated by the command above; DO NOT EDIT.
375
376 // +build $tags
377
378 package syscall
379
380 import "unsafe"
381
382 $text
383 EOF
384 exit 0;