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