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  	# Go function header.
   145  	my $out_decl = @out ? sprintf(" (%s)", join(', ', @out)) : "";
   146  	$text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out_decl;
   147  
   148  	# Disable ptrace on iOS.
   149  	if ($darwin && $func =~ /^ptrace(Ptr)?$/) {
   150  		$text .= "\tif runtime.GOOS == \"ios\" {\n";
   151  		$text .= "\t\tpanic(\"unimplemented\")\n";
   152  		$text .= "\t}\n";
   153  	}
   154  
   155  	# Check if err return available
   156  	my $errvar = "";
   157  	foreach my $p (@out) {
   158  		my ($name, $type) = parseparam($p);
   159  		if($type eq "error") {
   160  			$errvar = $name;
   161  			last;
   162  		}
   163  	}
   164  
   165  	# Prepare arguments to Syscall.
   166  	my @args = ();
   167  	my $n = 0;
   168  	foreach my $p (@in) {
   169  		my ($name, $type) = parseparam($p);
   170  		if($type =~ /^\*/) {
   171  			push @args, "uintptr(unsafe.Pointer($name))";
   172  		} elsif($type eq "string" && $errvar ne "") {
   173  			$text .= "\tvar _p$n *byte\n";
   174  			$text .= "\t_p$n, $errvar = BytePtrFromString($name)\n";
   175  			$text .= "\tif $errvar != nil {\n\t\treturn\n\t}\n";
   176  			push @args, "uintptr(unsafe.Pointer(_p$n))";
   177  			$n++;
   178  		} elsif($type eq "string") {
   179  			print STDERR "$ARGV:$.: $func uses string arguments, but has no error return\n";
   180  			$text .= "\tvar _p$n *byte\n";
   181  			$text .= "\t_p$n, _ = BytePtrFromString($name)\n";
   182  			push @args, "uintptr(unsafe.Pointer(_p$n))";
   183  			$n++;
   184  		} elsif($type =~ /^\[\](.*)/) {
   185  			# Convert slice into pointer, length.
   186  			# Have to be careful not to take address of &a[0] if len == 0:
   187  			# pass dummy pointer in that case.
   188  			# Used to pass nil, but some OSes or simulators reject write(fd, nil, 0).
   189  			$text .= "\tvar _p$n unsafe.Pointer\n";
   190  			$text .= "\tif len($name) > 0 {\n\t\t_p$n = unsafe.Pointer(\&${name}[0])\n\t}";
   191  			$text .= " else {\n\t\t_p$n = unsafe.Pointer(&_zero)\n\t}";
   192  			$text .= "\n";
   193  			push @args, "uintptr(_p$n)", "uintptr(len($name))";
   194  			$n++;
   195  		} elsif($type eq "int64" && ($openbsd || $netbsd)) {
   196  			if (!$libc) {
   197  				push @args, "0";
   198  			}
   199  			if($libc && $arm && @args % 2) {
   200  				# arm abi specifies 64 bit argument must be 64 bit aligned.
   201  				push @args, "0"
   202  			}
   203  			if($_32bit eq "big-endian") {
   204  				push @args, "uintptr($name>>32)", "uintptr($name)";
   205  			} elsif($_32bit eq "little-endian") {
   206  				push @args, "uintptr($name)", "uintptr($name>>32)";
   207  			} else {
   208  				push @args, "uintptr($name)";
   209  			}
   210  		} elsif($type eq "int64" && $dragonfly) {
   211  			if ($func !~ /^extp(read|write)/i) {
   212  				push @args, "0";
   213  			}
   214  			if($_32bit eq "big-endian") {
   215  				push @args, "uintptr($name>>32)", "uintptr($name)";
   216  			} elsif($_32bit eq "little-endian") {
   217  				push @args, "uintptr($name)", "uintptr($name>>32)";
   218  			} else {
   219  				push @args, "uintptr($name)";
   220  			}
   221  		} elsif($type eq "int64" && $_32bit ne "") {
   222  			if(@args % 2 && $arm) {
   223  				# arm abi specifies 64-bit argument uses
   224  				# (even, odd) pair
   225  				push @args, "0"
   226  			}
   227  			if($_32bit eq "big-endian") {
   228  				push @args, "uintptr($name>>32)", "uintptr($name)";
   229  			} else {
   230  				push @args, "uintptr($name)", "uintptr($name>>32)";
   231  			}
   232  		} else {
   233  			push @args, "uintptr($name)";
   234  		}
   235  	}
   236  
   237  	# Determine which form to use; pad args with zeros.
   238  	my $asm = "Syscall";
   239  	if ($nonblock) {
   240  		if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
   241  			$asm = "rawSyscallNoError";
   242  		} else {
   243  			$asm = "RawSyscall";
   244  		}
   245  	}
   246  	if ($libc) {
   247  		# Call unexported syscall functions (which take
   248  		# libc functions instead of syscall numbers).
   249  		$asm = lcfirst($asm);
   250  	}
   251  	if(@args <= 3) {
   252  		while(@args < 3) {
   253  			push @args, "0";
   254  		}
   255  	} elsif(@args <= 6) {
   256  		$asm .= "6";
   257  		while(@args < 6) {
   258  			push @args, "0";
   259  		}
   260  	} elsif(@args <= 9) {
   261  		$asm .= "9";
   262  		while(@args < 9) {
   263  			push @args, "0";
   264  		}
   265  	} else {
   266  		print STDERR "$ARGV:$.: too many arguments to system call\n";
   267  	}
   268  
   269  	if ($darwin || ($openbsd && $libc)) {
   270  		# Use extended versions for calls that generate a 64-bit result.
   271  		my ($name, $type) = parseparam($out[0]);
   272  		if ($type eq "int64" || ($type eq "uintptr" && $_32bit eq "")) {
   273  			$asm .= "X";
   274  		}
   275  	}
   276  
   277  	# System call number.
   278  	my $funcname = "";
   279  	if($sysname eq "") {
   280  		$sysname = "SYS_$func";
   281  		$sysname =~ s/([a-z])([A-Z])/${1}_$2/g;	# turn FooBar into Foo_Bar
   282  		$sysname =~ y/a-z/A-Z/;
   283  		if($libc) {
   284  			$sysname =~ y/A-Z/a-z/;
   285  			$sysname = substr $sysname, 4;
   286  			$funcname = "libc_$sysname";
   287  		}
   288  	}
   289  	if($libc) {
   290  		if($funcname eq "") {
   291  			$sysname = substr $sysname, 4;
   292  			$sysname =~ y/A-Z/a-z/;
   293  			$funcname = "libc_$sysname";
   294  		}
   295  		$sysname = "abi.FuncPCABI0(${funcname}_trampoline)";
   296  	}
   297  
   298  	# Actual call.
   299  	my $args = join(', ', @args);
   300  	my $call = "$asm($sysname, $args)";
   301  
   302  	# Assign return values.
   303  	my $body = "";
   304  	my @ret = ("_", "_", "_");
   305  	my $do_errno = 0;
   306  	for(my $i=0; $i<@out; $i++) {
   307  		my $p = $out[$i];
   308  		my ($name, $type) = parseparam($p);
   309  		my $reg = "";
   310  		if($name eq "err" && !$plan9) {
   311  			$reg = "e1";
   312  			$ret[2] = $reg;
   313  			$do_errno = 1;
   314  		} elsif($name eq "err" && $plan9) {
   315  			$ret[0] = "r0";
   316  			$ret[2] = "e1";
   317  			next;
   318  		} else {
   319  			$reg = sprintf("r%d", $i);
   320  			$ret[$i] = $reg;
   321  		}
   322  		if($type eq "bool") {
   323  			$reg = "$reg != 0";
   324  		}
   325  		if($type eq "int64" && $_32bit ne "") {
   326  			# 64-bit number in r1:r0 or r0:r1.
   327  			if($i+2 > @out) {
   328  				print STDERR "$ARGV:$.: not enough registers for int64 return\n";
   329  			}
   330  			if($_32bit eq "big-endian") {
   331  				$reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1);
   332  			} else {
   333  				$reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i);
   334  			}
   335  			$ret[$i] = sprintf("r%d", $i);
   336  			$ret[$i+1] = sprintf("r%d", $i+1);
   337  		}
   338  		if($reg ne "e1" || $plan9) {
   339  			$body .= "\t$name = $type($reg)\n";
   340  		}
   341  	}
   342  	if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") {
   343  		$text .= "\t$call\n";
   344  	} else {
   345  		if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
   346  			# raw syscall without error on Linux, see golang.org/issue/22924
   347  			$text .= "\t$ret[0], $ret[1] := $call\n";
   348  		} else {
   349  			$text .= "\t$ret[0], $ret[1], $ret[2] := $call\n";
   350  		}
   351  	}
   352  	$text .= $body;
   353  
   354  	if ($plan9 && $ret[2] eq "e1") {
   355  		$text .= "\tif int32(r0) == -1 {\n";
   356  		$text .= "\t\terr = e1\n";
   357  		$text .= "\t}\n";
   358  	} elsif ($do_errno) {
   359  		$text .= "\tif e1 != 0 {\n";
   360  		$text .= "\t\terr = errnoErr(e1)\n";
   361  		$text .= "\t}\n";
   362  	}
   363  	$text .= "\treturn\n";
   364  	$text .= "}\n\n";
   365  	if($libc) {
   366  		if (not exists $trampolines{$funcname}) {
   367  			$trampolines{$funcname} = 1;
   368  			# The assembly trampoline that jumps to the libc routine.
   369  			$text .= "func ${funcname}_trampoline()\n\n";
   370  			# Tell the linker that funcname can be found in libSystem using varname without the libc_ prefix.
   371  			my $basename = substr $funcname, 5;
   372  			my $libc = "libc.so";
   373  			if ($darwin) {
   374  				$libc = "/usr/lib/libSystem.B.dylib";
   375  			}
   376  			$text .= "//go:cgo_import_dynamic $funcname $basename \"$libc\"\n\n";
   377  		}
   378  	}
   379  }
   380  
   381  chomp $text;
   382  chomp $text;
   383  
   384  if($errors) {
   385  	exit 1;
   386  }
   387  
   388  if($extraimports ne "") {
   389      $stdimports .= "\n$extraimports";
   390  }
   391  
   392  # TODO: this assumes tags are just simply comma separated. For now this is all the uses.
   393  $newtags = $tags =~ s/,/ && /r;
   394  
   395  print <<EOF;
   396  // $cmdline
   397  // Code generated by the command above; DO NOT EDIT.
   398  
   399  //go:build $newtags
   400  
   401  package syscall
   402  
   403  $stdimports
   404  
   405  $text
   406  EOF
   407  exit 0;
   408  

View as plain text