...

Text file src/pkg/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 $nacl = 0;
    33	my $arm = 0; # 64-bit value should use (even, odd)-pair
    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		shift;
    50	}
    51	if($ARGV[0] eq "-openbsd") {
    52		$openbsd = 1;
    53		shift;
    54	}
    55	if($ARGV[0] eq "-netbsd") {
    56		$netbsd = 1;
    57		shift;
    58	}
    59	if($ARGV[0] eq "-dragonfly") {
    60		$dragonfly = 1;
    61		shift;
    62	}
    63	if($ARGV[0] eq "-nacl") {
    64		$nacl = 1;
    65		shift;
    66	}
    67	if($ARGV[0] eq "-arm") {
    68		$arm = 1;
    69		shift;
    70	}
    71	if($ARGV[0] eq "-tags") {
    72		shift;
    73		$tags = $ARGV[0];
    74		shift;
    75	}
    76	
    77	if($ARGV[0] =~ /^-/) {
    78		print STDERR "usage: mksyscall.pl [-b32 | -l32] [-tags x,y] [file ...]\n";
    79		exit 1;
    80	}
    81	
    82	sub parseparamlist($) {
    83		my ($list) = @_;
    84		$list =~ s/^\s*//;
    85		$list =~ s/\s*$//;
    86		if($list eq "") {
    87			return ();
    88		}
    89		return split(/\s*,\s*/, $list);
    90	}
    91	
    92	sub parseparam($) {
    93		my ($p) = @_;
    94		if($p !~ /^(\S*) (\S*)$/) {
    95			print STDERR "$ARGV:$.: malformed parameter: $p\n";
    96			$errors = 1;
    97			return ("xx", "int");
    98		}
    99		return ($1, $2);
   100	}
   101	
   102	# set of trampolines we've already generated
   103	my %trampolines;
   104	
   105	my $text = "";
   106	while(<>) {
   107		chomp;
   108		s/\s+/ /g;
   109		s/^\s+//;
   110		s/\s+$//;
   111		my $nonblock = /^\/\/sysnb /;
   112		next if !/^\/\/sys / && !$nonblock;
   113	
   114		# Line must be of the form
   115		#	func Open(path string, mode int, perm int) (fd int, errno error)
   116		# Split into name, in params, out params.
   117		if(!/^\/\/sys(nb)? (\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*((?i)_?SYS_[A-Z0-9_]+))?$/) {
   118			print STDERR "$ARGV:$.: malformed //sys declaration\n";
   119			$errors = 1;
   120			next;
   121		}
   122		my ($func, $in, $out, $sysname) = ($2, $3, $4, $5);
   123	
   124		# Split argument lists on comma.
   125		my @in = parseparamlist($in);
   126		my @out = parseparamlist($out);
   127	
   128		# Try in vain to keep people from editing this file.
   129		# The theory is that they jump into the middle of the file
   130		# without reading the header.
   131		$text .= "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n";
   132	
   133		if ($darwin && $func eq "ptrace") {
   134			# The ptrace function is called from forkAndExecInChild where stack
   135			# growth is forbidden.
   136			$text .= "//go:nosplit\n"
   137		}
   138	
   139		# Go function header.
   140		my $out_decl = @out ? sprintf(" (%s)", join(', ', @out)) : "";
   141		$text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out_decl;
   142	
   143		# Check if err return available
   144		my $errvar = "";
   145		foreach my $p (@out) {
   146			my ($name, $type) = parseparam($p);
   147			if($type eq "error") {
   148				$errvar = $name;
   149				last;
   150			}
   151		}
   152	
   153		# Prepare arguments to Syscall.
   154		my @args = ();
   155		my $n = 0;
   156		foreach my $p (@in) {
   157			my ($name, $type) = parseparam($p);
   158			if($type =~ /^\*/) {
   159				push @args, "uintptr(unsafe.Pointer($name))";
   160			} elsif($type eq "string" && $errvar ne "") {
   161				$text .= "\tvar _p$n *byte\n";
   162				$text .= "\t_p$n, $errvar = BytePtrFromString($name)\n";
   163				$text .= "\tif $errvar != nil {\n\t\treturn\n\t}\n";
   164				push @args, "uintptr(unsafe.Pointer(_p$n))";
   165				$n++;
   166			} elsif($type eq "string") {
   167				print STDERR "$ARGV:$.: $func uses string arguments, but has no error return\n";
   168				$text .= "\tvar _p$n *byte\n";
   169				$text .= "\t_p$n, _ = BytePtrFromString($name)\n";
   170				push @args, "uintptr(unsafe.Pointer(_p$n))";
   171				$n++;
   172			} elsif($type =~ /^\[\](.*)/) {
   173				# Convert slice into pointer, length.
   174				# Have to be careful not to take address of &a[0] if len == 0:
   175				# pass dummy pointer in that case.
   176				# Used to pass nil, but some OSes or simulators reject write(fd, nil, 0).
   177				$text .= "\tvar _p$n unsafe.Pointer\n";
   178				$text .= "\tif len($name) > 0 {\n\t\t_p$n = unsafe.Pointer(\&${name}[0])\n\t}";
   179				$text .= " else {\n\t\t_p$n = unsafe.Pointer(&_zero)\n\t}";
   180				$text .= "\n";
   181				push @args, "uintptr(_p$n)", "uintptr(len($name))";
   182				$n++;
   183			} elsif($type eq "int64" && ($openbsd || $netbsd)) {
   184				push @args, "0";
   185				if($_32bit eq "big-endian") {
   186					push @args, "uintptr($name>>32)", "uintptr($name)";
   187				} elsif($_32bit eq "little-endian") {
   188					push @args, "uintptr($name)", "uintptr($name>>32)";
   189				} else {
   190					push @args, "uintptr($name)";
   191				}
   192			} elsif($type eq "int64" && $dragonfly) {
   193				if ($func !~ /^extp(read|write)/i) {
   194					push @args, "0";
   195				}
   196				if($_32bit eq "big-endian") {
   197					push @args, "uintptr($name>>32)", "uintptr($name)";
   198				} elsif($_32bit eq "little-endian") {
   199					push @args, "uintptr($name)", "uintptr($name>>32)";
   200				} else {
   201					push @args, "uintptr($name)";
   202				}
   203			} elsif($type eq "int64" && $_32bit ne "") {
   204				if(@args % 2 && $arm) {
   205					# arm abi specifies 64-bit argument uses
   206					# (even, odd) pair
   207					push @args, "0"
   208				}
   209				if($_32bit eq "big-endian") {
   210					push @args, "uintptr($name>>32)", "uintptr($name)";
   211				} else {
   212					push @args, "uintptr($name)", "uintptr($name>>32)";
   213				}
   214			} else {
   215				push @args, "uintptr($name)";
   216			}
   217		}
   218	
   219		# Determine which form to use; pad args with zeros.
   220		my $asm = "Syscall";
   221		if ($nonblock) {
   222			if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
   223				$asm = "rawSyscallNoError";
   224			} else {
   225				$asm = "RawSyscall";
   226			}
   227		}
   228		if ($darwin) {
   229			# Call unexported syscall functions (which take
   230			# libc functions instead of syscall numbers).
   231			$asm = lcfirst($asm);
   232		}
   233		if(@args <= 3) {
   234			while(@args < 3) {
   235				push @args, "0";
   236			}
   237		} elsif(@args <= 6) {
   238			$asm .= "6";
   239			while(@args < 6) {
   240				push @args, "0";
   241			}
   242		} elsif(@args <= 9) {
   243			$asm .= "9";
   244			while(@args < 9) {
   245				push @args, "0";
   246			}
   247		} else {
   248			print STDERR "$ARGV:$.: too many arguments to system call\n";
   249		}
   250	
   251		if ($darwin) {
   252			# Use extended versions for calls that generate a 64-bit result.
   253			my ($name, $type) = parseparam($out[0]);
   254			if ($type eq "int64" || ($type eq "uintptr" && $_32bit eq "")) {
   255				$asm .= "X";
   256			}
   257		}
   258	
   259		# System call number.
   260		my $funcname = "";
   261		if($sysname eq "") {
   262			$sysname = "SYS_$func";
   263			$sysname =~ s/([a-z])([A-Z])/${1}_$2/g;	# turn FooBar into Foo_Bar
   264			$sysname =~ y/a-z/A-Z/;
   265			if($nacl) {
   266				$sysname =~ y/A-Z/a-z/;
   267			}
   268			if($darwin) {
   269				$sysname =~ y/A-Z/a-z/;
   270				$sysname = substr $sysname, 4;
   271				$funcname = "libc_$sysname";
   272			}
   273		}
   274		if($darwin) {
   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($darwin) {
   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				# Map syscall.funcname to just plain funcname.
   355				# (The jump to this function is in the assembly trampoline, generated by mksyscallasm_darwin.go.)
   356				$text .= "//go:linkname $funcname $funcname\n";
   357				# Tell the linker that funcname can be found in libSystem using varname without the libc_ prefix.
   358				my $basename = substr $funcname, 5;
   359				$text .= "//go:cgo_import_dynamic $funcname $basename \"/usr/lib/libSystem.B.dylib\"\n\n";
   360			}
   361		}
   362	}
   363	
   364	chomp $text;
   365	chomp $text;
   366	
   367	if($errors) {
   368		exit 1;
   369	}
   370	
   371	print <<EOF;
   372	// $cmdline
   373	// Code generated by the command above; DO NOT EDIT.
   374	
   375	// +build $tags
   376	
   377	package syscall
   378	
   379	import "unsafe"
   380	
   381	$text
   382	EOF
   383	exit 0;

View as plain text