Text file
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 my $newtags = ""; # new style build tags
36 my $stdimports = 'import "unsafe"';
37 my $extraimports = "";
38
39 if($ARGV[0] eq "-b32") {
40 $_32bit = "big-endian";
41 shift;
42 } elsif($ARGV[0] eq "-l32") {
43 $_32bit = "little-endian";
44 shift;
45 }
46 if($ARGV[0] eq "-plan9") {
47 $plan9 = 1;
48 shift;
49 }
50 if($ARGV[0] eq "-darwin") {
51 $darwin = 1;
52 $libc = 1;
53 shift;
54 }
55 if($ARGV[0] eq "-openbsd") {
56 $openbsd = 1;
57 shift;
58 }
59 if($ARGV[0] eq "-netbsd") {
60 $netbsd = 1;
61 shift;
62 }
63 if($ARGV[0] eq "-dragonfly") {
64 $dragonfly = 1;
65 shift;
66 }
67 if($ARGV[0] eq "-arm") {
68 $arm = 1;
69 shift;
70 }
71 if($ARGV[0] eq "-libc") {
72 $libc = 1;
73 shift;
74 }
75 if($ARGV[0] eq "-tags") {
76 shift;
77 $tags = $ARGV[0];
78 shift;
79 }
80
81 if($ARGV[0] =~ /^-/) {
82 print STDERR "usage: mksyscall.pl [-b32 | -l32] [-tags x,y] [file ...]\n";
83 exit 1;
84 }
85
86 if($libc) {
87 $extraimports = 'import "internal/abi"';
88 }
89 if($darwin) {
90 $extraimports .= "\nimport \"runtime\"";
91 }
92
93 sub parseparamlist($) {
94 my ($list) = @_;
95 $list =~ s/^\s*//;
96 $list =~ s/\s*$//;
97 if($list eq "") {
98 return ();
99 }
100 return split(/\s*,\s*/, $list);
101 }
102
103 sub parseparam($) {
104 my ($p) = @_;
105 if($p !~ /^(\S*) (\S*)$/) {
106 print STDERR "$ARGV:$.: malformed parameter: $p\n";
107 $errors = 1;
108 return ("xx", "int");
109 }
110 return ($1, $2);
111 }
112
113 # set of trampolines we've already generated
114 my %trampolines;
115
116 my $text = "";
117 while(<>) {
118 chomp;
119 s/\s+/ /g;
120 s/^\s+//;
121 s/\s+$//;
122 my $nonblock = /^\/\/sysnb /;
123 next if !/^\/\/sys / && !$nonblock;
124
125 # Line must be of the form
126 # func Open(path string, mode int, perm int) (fd int, errno error)
127 # Split into name, in params, out params.
128 if(!/^\/\/sys(nb)? (\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*((?i)_?SYS_[A-Z0-9_]+))?$/) {
129 print STDERR "$ARGV:$.: malformed //sys declaration\n";
130 $errors = 1;
131 next;
132 }
133 my ($func, $in, $out, $sysname) = ($2, $3, $4, $5);
134
135 # Split argument lists on comma.
136 my @in = parseparamlist($in);
137 my @out = parseparamlist($out);
138
139 # Try in vain to keep people from editing this file.
140 # The theory is that they jump into the middle of the file
141 # without reading the header.
142 $text .= "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n";
143
144 if ((($darwin || ($openbsd && $libc)) && $func =~ /^ptrace(Ptr)?$/)) {
145 # The ptrace function is called from forkAndExecInChild where stack
146 # growth is forbidden.
147 $text .= "//go:nosplit\n"
148 }
149
150 # Go function header.
151 my $out_decl = @out ? sprintf(" (%s)", join(', ', @out)) : "";
152 $text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out_decl;
153
154 # Disable ptrace on iOS.
155 if ($darwin && $func =~ /^ptrace(Ptr)?$/) {
156 $text .= "\tif runtime.GOOS == \"ios\" {\n";
157 $text .= "\t\tpanic(\"unimplemented\")\n";
158 $text .= "\t}\n";
159 }
160
161 # Check if err return available
162 my $errvar = "";
163 foreach my $p (@out) {
164 my ($name, $type) = parseparam($p);
165 if($type eq "error") {
166 $errvar = $name;
167 last;
168 }
169 }
170
171 # Prepare arguments to Syscall.
172 my @args = ();
173 my $n = 0;
174 foreach my $p (@in) {
175 my ($name, $type) = parseparam($p);
176 if($type =~ /^\*/) {
177 push @args, "uintptr(unsafe.Pointer($name))";
178 } elsif($type eq "string" && $errvar ne "") {
179 $text .= "\tvar _p$n *byte\n";
180 $text .= "\t_p$n, $errvar = BytePtrFromString($name)\n";
181 $text .= "\tif $errvar != nil {\n\t\treturn\n\t}\n";
182 push @args, "uintptr(unsafe.Pointer(_p$n))";
183 $n++;
184 } elsif($type eq "string") {
185 print STDERR "$ARGV:$.: $func uses string arguments, but has no error return\n";
186 $text .= "\tvar _p$n *byte\n";
187 $text .= "\t_p$n, _ = BytePtrFromString($name)\n";
188 push @args, "uintptr(unsafe.Pointer(_p$n))";
189 $n++;
190 } elsif($type =~ /^\[\](.*)/) {
191 # Convert slice into pointer, length.
192 # Have to be careful not to take address of &a[0] if len == 0:
193 # pass dummy pointer in that case.
194 # Used to pass nil, but some OSes or simulators reject write(fd, nil, 0).
195 $text .= "\tvar _p$n unsafe.Pointer\n";
196 $text .= "\tif len($name) > 0 {\n\t\t_p$n = unsafe.Pointer(\&${name}[0])\n\t}";
197 $text .= " else {\n\t\t_p$n = unsafe.Pointer(&_zero)\n\t}";
198 $text .= "\n";
199 push @args, "uintptr(_p$n)", "uintptr(len($name))";
200 $n++;
201 } elsif($type eq "int64" && ($openbsd || $netbsd)) {
202 if (!$libc) {
203 push @args, "0";
204 }
205 if($libc && $arm && @args % 2) {
206 # arm abi specifies 64 bit argument must be 64 bit aligned.
207 push @args, "0"
208 }
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)";
213 } else {
214 push @args, "uintptr($name)";
215 }
216 } elsif($type eq "int64" && $dragonfly) {
217 if ($func !~ /^extp(read|write)/i) {
218 push @args, "0";
219 }
220 if($_32bit eq "big-endian") {
221 push @args, "uintptr($name>>32)", "uintptr($name)";
222 } elsif($_32bit eq "little-endian") {
223 push @args, "uintptr($name)", "uintptr($name>>32)";
224 } else {
225 push @args, "uintptr($name)";
226 }
227 } elsif($type eq "int64" && $_32bit ne "") {
228 if(@args % 2 && $arm) {
229 # arm abi specifies 64-bit argument uses
230 # (even, odd) pair
231 push @args, "0"
232 }
233 if($_32bit eq "big-endian") {
234 push @args, "uintptr($name>>32)", "uintptr($name)";
235 } else {
236 push @args, "uintptr($name)", "uintptr($name>>32)";
237 }
238 } else {
239 push @args, "uintptr($name)";
240 }
241 }
242
243 # Determine which form to use; pad args with zeros.
244 my $asm = "Syscall";
245 if ($nonblock) {
246 if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
247 $asm = "rawSyscallNoError";
248 } else {
249 $asm = "RawSyscall";
250 }
251 }
252 if ($libc) {
253 # Call unexported syscall functions (which take
254 # libc functions instead of syscall numbers).
255 $asm = lcfirst($asm);
256 }
257 if(@args <= 3) {
258 while(@args < 3) {
259 push @args, "0";
260 }
261 } elsif(@args <= 6) {
262 $asm .= "6";
263 while(@args < 6) {
264 push @args, "0";
265 }
266 } elsif(@args <= 9) {
267 $asm .= "9";
268 while(@args < 9) {
269 push @args, "0";
270 }
271 } else {
272 print STDERR "$ARGV:$.: too many arguments to system call\n";
273 }
274
275 if ($darwin || ($openbsd && $libc)) {
276 # Use extended versions for calls that generate a 64-bit result.
277 my ($name, $type) = parseparam($out[0]);
278 if ($type eq "int64" || ($type eq "uintptr" && $_32bit eq "")) {
279 $asm .= "X";
280 }
281 }
282
283 # System call number.
284 my $funcname = "";
285 if($sysname eq "") {
286 $sysname = "SYS_$func";
287 $sysname =~ s/([a-z])([A-Z])/${1}_$2/g; # turn FooBar into Foo_Bar
288 $sysname =~ y/a-z/A-Z/;
289 if($libc) {
290 $sysname =~ y/A-Z/a-z/;
291 $sysname = substr $sysname, 4;
292 $funcname = "libc_$sysname";
293 }
294 }
295 if($libc) {
296 if($funcname eq "") {
297 $sysname = substr $sysname, 4;
298 $sysname =~ y/A-Z/a-z/;
299 $funcname = "libc_$sysname";
300 }
301 $sysname = "abi.FuncPCABI0(${funcname}_trampoline)";
302 }
303
304 # Actual call.
305 my $args = join(', ', @args);
306 my $call = "$asm($sysname, $args)";
307
308 # Assign return values.
309 my $body = "";
310 my @ret = ("_", "_", "_");
311 my $do_errno = 0;
312 for(my $i=0; $i<@out; $i++) {
313 my $p = $out[$i];
314 my ($name, $type) = parseparam($p);
315 my $reg = "";
316 if($name eq "err" && !$plan9) {
317 $reg = "e1";
318 $ret[2] = $reg;
319 $do_errno = 1;
320 } elsif($name eq "err" && $plan9) {
321 $ret[0] = "r0";
322 $ret[2] = "e1";
323 next;
324 } else {
325 $reg = sprintf("r%d", $i);
326 $ret[$i] = $reg;
327 }
328 if($type eq "bool") {
329 $reg = "$reg != 0";
330 }
331 if($type eq "int64" && $_32bit ne "") {
332 # 64-bit number in r1:r0 or r0:r1.
333 if($i+2 > @out) {
334 print STDERR "$ARGV:$.: not enough registers for int64 return\n";
335 }
336 if($_32bit eq "big-endian") {
337 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1);
338 } else {
339 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i);
340 }
341 $ret[$i] = sprintf("r%d", $i);
342 $ret[$i+1] = sprintf("r%d", $i+1);
343 }
344 if($reg ne "e1" || $plan9) {
345 $body .= "\t$name = $type($reg)\n";
346 }
347 }
348 if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") {
349 $text .= "\t$call\n";
350 } else {
351 if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
352 # raw syscall without error on Linux, see golang.org/issue/22924
353 $text .= "\t$ret[0], $ret[1] := $call\n";
354 } else {
355 $text .= "\t$ret[0], $ret[1], $ret[2] := $call\n";
356 }
357 }
358 $text .= $body;
359
360 if ($plan9 && $ret[2] eq "e1") {
361 $text .= "\tif int32(r0) == -1 {\n";
362 $text .= "\t\terr = e1\n";
363 $text .= "\t}\n";
364 } elsif ($do_errno) {
365 $text .= "\tif e1 != 0 {\n";
366 $text .= "\t\terr = errnoErr(e1)\n";
367 $text .= "\t}\n";
368 }
369 $text .= "\treturn\n";
370 $text .= "}\n\n";
371 if($libc) {
372 if (not exists $trampolines{$funcname}) {
373 $trampolines{$funcname} = 1;
374 # The assembly trampoline that jumps to the libc routine.
375 $text .= "func ${funcname}_trampoline()\n\n";
376 # Tell the linker that funcname can be found in libSystem using varname without the libc_ prefix.
377 my $basename = substr $funcname, 5;
378 my $libc = "libc.so";
379 if ($darwin) {
380 $libc = "/usr/lib/libSystem.B.dylib";
381 }
382 $text .= "//go:cgo_import_dynamic $funcname $basename \"$libc\"\n\n";
383 }
384 }
385 }
386
387 chomp $text;
388 chomp $text;
389
390 if($errors) {
391 exit 1;
392 }
393
394 if($extraimports ne "") {
395 $stdimports .= "\n$extraimports";
396 }
397
398 # TODO: this assumes tags are just simply comma separated. For now this is all the uses.
399 $newtags = $tags =~ s/,/ && /r;
400
401 print <<EOF;
402 // $cmdline
403 // Code generated by the command above; DO NOT EDIT.
404
405 //go:build $newtags
406
407 package syscall
408
409 $stdimports
410
411 $text
412 EOF
413 exit 0;
414
View as plain text