Black Lives Matter. Support the Equal Justice Initiative.

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  
    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 $extraimports = "";
    37  
    38  if($ARGV[0] eq "-b32") {
    39  	$_32bit = "big-endian";
    40  	shift;
    41  } elsif($ARGV[0] eq "-l32") {
    42  	$_32bit = "little-endian";
    43  	shift;
    44  }
    45  if($ARGV[0] eq "-plan9") {
    46  	$plan9 = 1;
    47  	shift;
    48  }
    49  if($ARGV[0] eq "-darwin") {
    50  	$darwin = 1;
    51  	$libc = 1;
    52  	shift;
    53  }
    54  if($ARGV[0] eq "-openbsd") {
    55  	$openbsd = 1;
    56  	shift;
    57  }
    58  if($ARGV[0] eq "-netbsd") {
    59  	$netbsd = 1;
    60  	shift;
    61  }
    62  if($ARGV[0] eq "-dragonfly") {
    63  	$dragonfly = 1;
    64  	shift;
    65  }
    66  if($ARGV[0] eq "-arm") {
    67  	$arm = 1;
    68  	shift;
    69  }
    70  if($ARGV[0] eq "-libc") {
    71  	$libc = 1;
    72  	shift;
    73  }
    74  if($ARGV[0] eq "-tags") {
    75  	shift;
    76  	$tags = $ARGV[0];
    77  	shift;
    78  }
    79  
    80  if($ARGV[0] =~ /^-/) {
    81  	print STDERR "usage: mksyscall.pl [-b32 | -l32] [-tags x,y] [file ...]\n";
    82  	exit 1;
    83  }
    84  
    85  if($libc) {
    86  	$extraimports = 'import "internal/abi"';
    87  }
    88  
    89  sub parseparamlist($) {
    90  	my ($list) = @_;
    91  	$list =~ s/^\s*//;
    92  	$list =~ s/\s*$//;
    93  	if($list eq "") {
    94  		return ();
    95  	}
    96  	return split(/\s*,\s*/, $list);
    97  }
    98  
    99  sub parseparam($) {
   100  	my ($p) = @_;
   101  	if($p !~ /^(\S*) (\S*)$/) {
   102  		print STDERR "$ARGV:$.: malformed parameter: $p\n";
   103  		$errors = 1;
   104  		return ("xx", "int");
   105  	}
   106  	return ($1, $2);
   107  }
   108  
   109  # set of trampolines we've already generated
   110  my %trampolines;
   111  
   112  my $text = "";
   113  while(<>) {
   114  	chomp;
   115  	s/\s+/ /g;
   116  	s/^\s+//;
   117  	s/\s+$//;
   118  	my $nonblock = /^\/\/sysnb /;
   119  	next if !/^\/\/sys / && !$nonblock;
   120  
   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";
   126  		$errors = 1;
   127  		next;
   128  	}
   129  	my ($func, $in, $out, $sysname) = ($2, $3, $4, $5);
   130  
   131  	# Split argument lists on comma.
   132  	my @in = parseparamlist($in);
   133  	my @out = parseparamlist($out);
   134  
   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";
   139  
   140  	if (($darwin && $func eq "ptrace1") || (($openbsd && $libc) && $func eq "ptrace")) {
   141  		# The ptrace function is called from forkAndExecInChild where stack
   142  		# growth is forbidden.
   143  		$text .= "//go:nosplit\n"
   144  	}
   145  
   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;
   149  
   150  	# Check if err return available
   151  	my $errvar = "";
   152  	foreach my $p (@out) {
   153  		my ($name, $type) = parseparam($p);
   154  		if($type eq "error") {
   155  			$errvar = $name;
   156  			last;
   157  		}
   158  	}
   159  
   160  	# Prepare arguments to Syscall.
   161  	my @args = ();
   162  	my $n = 0;
   163  	foreach my $p (@in) {
   164  		my ($name, $type) = parseparam($p);
   165  		if($type =~ /^\*/) {
   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))";
   172  			$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))";
   178  			$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}";
   187  			$text .= "\n";
   188  			push @args, "uintptr(_p$n)", "uintptr(len($name))";
   189  			$n++;
   190  		} elsif($type eq "int64" && ($openbsd || $netbsd)) {
   191  			if (!$libc) {
   192  				push @args, "0";
   193  			}
   194  			if($libc && $arm && @args % 2) {
   195  				# arm abi specifies 64 bit argument must be 64 bit aligned.
   196  				push @args, "0"
   197  			}
   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)";
   202  			} else {
   203  				push @args, "uintptr($name)";
   204  			}
   205  		} elsif($type eq "int64" && $dragonfly) {
   206  			if ($func !~ /^extp(read|write)/i) {
   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" && $_32bit ne "") {
   217  			if(@args % 2 && $arm) {
   218  				# arm abi specifies 64-bit argument uses
   219  				# (even, odd) pair
   220  				push @args, "0"
   221  			}
   222  			if($_32bit eq "big-endian") {
   223  				push @args, "uintptr($name>>32)", "uintptr($name)";
   224  			} else {
   225  				push @args, "uintptr($name)", "uintptr($name>>32)";
   226  			}
   227  		} else {
   228  			push @args, "uintptr($name)";
   229  		}
   230  	}
   231  
   232  	# Determine which form to use; pad args with zeros.
   233  	my $asm = "Syscall";
   234  	if ($nonblock) {
   235  		if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
   236  			$asm = "rawSyscallNoError";
   237  		} else {
   238  			$asm = "RawSyscall";
   239  		}
   240  	}
   241  	if ($libc) {
   242  		# Call unexported syscall functions (which take
   243  		# libc functions instead of syscall numbers).
   244  		$asm = lcfirst($asm);
   245  	}
   246  	if(@args <= 3) {
   247  		while(@args < 3) {
   248  			push @args, "0";
   249  		}
   250  	} elsif(@args <= 6) {
   251  		$asm .= "6";
   252  		while(@args < 6) {
   253  			push @args, "0";
   254  		}
   255  	} elsif(@args <= 9) {
   256  		$asm .= "9";
   257  		while(@args < 9) {
   258  			push @args, "0";
   259  		}
   260  	} else {
   261  		print STDERR "$ARGV:$.: too many arguments to system call\n";
   262  	}
   263  
   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 "")) {
   268  			$asm .= "X";
   269  		}
   270  	}
   271  
   272  	# System call number.
   273  	my $funcname = "";
   274  	if($sysname eq "") {
   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/;
   278  		if($libc) {
   279  			$sysname =~ y/A-Z/a-z/;
   280  			$sysname = substr $sysname, 4;
   281  			$funcname = "libc_$sysname";
   282  		}
   283  	}
   284  	if($libc) {
   285  		if($funcname eq "") {
   286  			$sysname = substr $sysname, 4;
   287  			$funcname = "libc_$sysname";
   288  		}
   289  		$sysname = "abi.FuncPCABI0(${funcname}_trampoline)";
   290  	}
   291  
   292  	# Actual call.
   293  	my $args = join(', ', @args);
   294  	my $call = "$asm($sysname, $args)";
   295  
   296  	# Assign return values.
   297  	my $body = "";
   298  	my @ret = ("_", "_", "_");
   299  	my $do_errno = 0;
   300  	for(my $i=0; $i<@out; $i++) {
   301  		my $p = $out[$i];
   302  		my ($name, $type) = parseparam($p);
   303  		my $reg = "";
   304  		if($name eq "err" && !$plan9) {
   305  			$reg = "e1";
   306  			$ret[2] = $reg;
   307  			$do_errno = 1;
   308  		} elsif($name eq "err" && $plan9) {
   309  			$ret[0] = "r0";
   310  			$ret[2] = "e1";
   311  			next;
   312  		} else {
   313  			$reg = sprintf("r%d", $i);
   314  			$ret[$i] = $reg;
   315  		}
   316  		if($type eq "bool") {
   317  			$reg = "$reg != 0";
   318  		}
   319  		if($type eq "int64" && $_32bit ne "") {
   320  			# 64-bit number in r1:r0 or r0:r1.
   321  			if($i+2 > @out) {
   322  				print STDERR "$ARGV:$.: not enough registers for int64 return\n";
   323  			}
   324  			if($_32bit eq "big-endian") {
   325  				$reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1);
   326  			} else {
   327  				$reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i);
   328  			}
   329  			$ret[$i] = sprintf("r%d", $i);
   330  			$ret[$i+1] = sprintf("r%d", $i+1);
   331  		}
   332  		if($reg ne "e1" || $plan9) {
   333  			$body .= "\t$name = $type($reg)\n";
   334  		}
   335  	}
   336  	if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") {
   337  		$text .= "\t$call\n";
   338  	} else {
   339  		if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
   340  			# raw syscall without error on Linux, see golang.org/issue/22924
   341  			$text .= "\t$ret[0], $ret[1] := $call\n";
   342  		} else {
   343  			$text .= "\t$ret[0], $ret[1], $ret[2] := $call\n";
   344  		}
   345  	}
   346  	$text .= $body;
   347  
   348  	if ($plan9 && $ret[2] eq "e1") {
   349  		$text .= "\tif int32(r0) == -1 {\n";
   350  		$text .= "\t\terr = e1\n";
   351  		$text .= "\t}\n";
   352  	} elsif ($do_errno) {
   353  		$text .= "\tif e1 != 0 {\n";
   354  		$text .= "\t\terr = errnoErr(e1)\n";
   355  		$text .= "\t}\n";
   356  	}
   357  	$text .= "\treturn\n";
   358  	$text .= "}\n\n";
   359  	if($libc) {
   360  		if (not exists $trampolines{$funcname}) {
   361  			$trampolines{$funcname} = 1;
   362  			# The assembly trampoline that jumps to the libc routine.
   363  			$text .= "func ${funcname}_trampoline()\n\n";
   364  			# Tell the linker that funcname can be found in libSystem using varname without the libc_ prefix.
   365  			my $basename = substr $funcname, 5;
   366  			my $libc = "libc.so";
   367  			if ($darwin) {
   368  				$libc = "/usr/lib/libSystem.B.dylib";
   369  			}
   370  			$text .= "//go:cgo_import_dynamic $funcname $basename \"$libc\"\n\n";
   371  		}
   372  	}
   373  }
   374  
   375  chomp $text;
   376  chomp $text;
   377  
   378  if($errors) {
   379  	exit 1;
   380  }
   381  
   382  # TODO: this assumes tags are just simply comma separated. For now this is all the uses.
   383  $newtags = $tags =~ s/,/ && /r;
   384  
   385  print <<EOF;
   386  // $cmdline
   387  // Code generated by the command above; DO NOT EDIT.
   388  
   389  //go:build $newtags
   390  // +build $tags
   391  
   392  package syscall
   393  
   394  import "unsafe"
   395  $extraimports
   396  
   397  $text
   398  EOF
   399  exit 0;
   400  

View as plain text