mksyscall.pl 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. # This program reads a file containing function prototypes
  6. # (like syscall_darwin.go) and generates system call bodies.
  7. # The prototypes are marked by lines beginning with "//sys"
  8. # and read like func declarations if //sys is replaced by func, but:
  9. # * The parameter lists must give a name for each argument.
  10. # This includes return parameters.
  11. # * The parameter lists must give a type for each argument:
  12. # the (x, y, z int) shorthand is not allowed.
  13. # * If the return parameter is an error number, it must be named errno.
  14. # A line beginning with //sysnb is like //sys, except that the
  15. # goroutine will not be suspended during the execution of the system
  16. # call. This must only be used for system calls which can never
  17. # block, as otherwise the system call could cause all goroutines to
  18. # hang.
  19. use strict;
  20. my $cmdline = "mksyscall.pl " . join(' ', @ARGV);
  21. my $errors = 0;
  22. my $_32bit = "";
  23. my $plan9 = 0;
  24. my $openbsd = 0;
  25. my $netbsd = 0;
  26. my $dragonfly = 0;
  27. my $arm = 0; # 64-bit value should use (even, odd)-pair
  28. my $tags = ""; # build tags
  29. if($ARGV[0] eq "-b32") {
  30. $_32bit = "big-endian";
  31. shift;
  32. } elsif($ARGV[0] eq "-l32") {
  33. $_32bit = "little-endian";
  34. shift;
  35. }
  36. if($ARGV[0] eq "-plan9") {
  37. $plan9 = 1;
  38. shift;
  39. }
  40. if($ARGV[0] eq "-openbsd") {
  41. $openbsd = 1;
  42. shift;
  43. }
  44. if($ARGV[0] eq "-netbsd") {
  45. $netbsd = 1;
  46. shift;
  47. }
  48. if($ARGV[0] eq "-dragonfly") {
  49. $dragonfly = 1;
  50. shift;
  51. }
  52. if($ARGV[0] eq "-arm") {
  53. $arm = 1;
  54. shift;
  55. }
  56. if($ARGV[0] eq "-tags") {
  57. shift;
  58. $tags = $ARGV[0];
  59. shift;
  60. }
  61. if($ARGV[0] =~ /^-/) {
  62. print STDERR "usage: mksyscall.pl [-b32 | -l32] [-tags x,y] [file ...]\n";
  63. exit 1;
  64. }
  65. # Check that we are using the new build system if we should
  66. if($ENV{'GOOS'} eq "linux" && $ENV{'GOARCH'} ne "sparc64") {
  67. if($ENV{'GOLANG_SYS_BUILD'} ne "docker") {
  68. print STDERR "In the new build system, mksyscall should not be called directly.\n";
  69. print STDERR "See README.md\n";
  70. exit 1;
  71. }
  72. }
  73. sub parseparamlist($) {
  74. my ($list) = @_;
  75. $list =~ s/^\s*//;
  76. $list =~ s/\s*$//;
  77. if($list eq "") {
  78. return ();
  79. }
  80. return split(/\s*,\s*/, $list);
  81. }
  82. sub parseparam($) {
  83. my ($p) = @_;
  84. if($p !~ /^(\S*) (\S*)$/) {
  85. print STDERR "$ARGV:$.: malformed parameter: $p\n";
  86. $errors = 1;
  87. return ("xx", "int");
  88. }
  89. return ($1, $2);
  90. }
  91. my $text = "";
  92. while(<>) {
  93. chomp;
  94. s/\s+/ /g;
  95. s/^\s+//;
  96. s/\s+$//;
  97. my $nonblock = /^\/\/sysnb /;
  98. next if !/^\/\/sys / && !$nonblock;
  99. # Line must be of the form
  100. # func Open(path string, mode int, perm int) (fd int, errno error)
  101. # Split into name, in params, out params.
  102. if(!/^\/\/sys(nb)? (\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*((?i)SYS_[A-Z0-9_]+))?$/) {
  103. print STDERR "$ARGV:$.: malformed //sys declaration\n";
  104. $errors = 1;
  105. next;
  106. }
  107. my ($func, $in, $out, $sysname) = ($2, $3, $4, $5);
  108. # Split argument lists on comma.
  109. my @in = parseparamlist($in);
  110. my @out = parseparamlist($out);
  111. # Try in vain to keep people from editing this file.
  112. # The theory is that they jump into the middle of the file
  113. # without reading the header.
  114. $text .= "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n";
  115. # Go function header.
  116. my $out_decl = @out ? sprintf(" (%s)", join(', ', @out)) : "";
  117. $text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out_decl;
  118. # Check if err return available
  119. my $errvar = "";
  120. foreach my $p (@out) {
  121. my ($name, $type) = parseparam($p);
  122. if($type eq "error") {
  123. $errvar = $name;
  124. last;
  125. }
  126. }
  127. # Prepare arguments to Syscall.
  128. my @args = ();
  129. my $n = 0;
  130. foreach my $p (@in) {
  131. my ($name, $type) = parseparam($p);
  132. if($type =~ /^\*/) {
  133. push @args, "uintptr(unsafe.Pointer($name))";
  134. } elsif($type eq "string" && $errvar ne "") {
  135. $text .= "\tvar _p$n *byte\n";
  136. $text .= "\t_p$n, $errvar = BytePtrFromString($name)\n";
  137. $text .= "\tif $errvar != nil {\n\t\treturn\n\t}\n";
  138. push @args, "uintptr(unsafe.Pointer(_p$n))";
  139. $n++;
  140. } elsif($type eq "string") {
  141. print STDERR "$ARGV:$.: $func uses string arguments, but has no error return\n";
  142. $text .= "\tvar _p$n *byte\n";
  143. $text .= "\t_p$n, _ = BytePtrFromString($name)\n";
  144. push @args, "uintptr(unsafe.Pointer(_p$n))";
  145. $n++;
  146. } elsif($type =~ /^\[\](.*)/) {
  147. # Convert slice into pointer, length.
  148. # Have to be careful not to take address of &a[0] if len == 0:
  149. # pass dummy pointer in that case.
  150. # Used to pass nil, but some OSes or simulators reject write(fd, nil, 0).
  151. $text .= "\tvar _p$n unsafe.Pointer\n";
  152. $text .= "\tif len($name) > 0 {\n\t\t_p$n = unsafe.Pointer(\&${name}[0])\n\t}";
  153. $text .= " else {\n\t\t_p$n = unsafe.Pointer(&_zero)\n\t}";
  154. $text .= "\n";
  155. push @args, "uintptr(_p$n)", "uintptr(len($name))";
  156. $n++;
  157. } elsif($type eq "int64" && ($openbsd || $netbsd)) {
  158. push @args, "0";
  159. if($_32bit eq "big-endian") {
  160. push @args, "uintptr($name>>32)", "uintptr($name)";
  161. } elsif($_32bit eq "little-endian") {
  162. push @args, "uintptr($name)", "uintptr($name>>32)";
  163. } else {
  164. push @args, "uintptr($name)";
  165. }
  166. } elsif($type eq "int64" && $dragonfly) {
  167. if ($func !~ /^extp(read|write)/i) {
  168. push @args, "0";
  169. }
  170. if($_32bit eq "big-endian") {
  171. push @args, "uintptr($name>>32)", "uintptr($name)";
  172. } elsif($_32bit eq "little-endian") {
  173. push @args, "uintptr($name)", "uintptr($name>>32)";
  174. } else {
  175. push @args, "uintptr($name)";
  176. }
  177. } elsif($type eq "int64" && $_32bit ne "") {
  178. if(@args % 2 && $arm) {
  179. # arm abi specifies 64-bit argument uses
  180. # (even, odd) pair
  181. push @args, "0"
  182. }
  183. if($_32bit eq "big-endian") {
  184. push @args, "uintptr($name>>32)", "uintptr($name)";
  185. } else {
  186. push @args, "uintptr($name)", "uintptr($name>>32)";
  187. }
  188. } else {
  189. push @args, "uintptr($name)";
  190. }
  191. }
  192. # Determine which form to use; pad args with zeros.
  193. my $asm = "Syscall";
  194. if ($nonblock) {
  195. if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
  196. $asm = "RawSyscallNoError";
  197. } else {
  198. $asm = "RawSyscall";
  199. }
  200. } else {
  201. if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
  202. $asm = "SyscallNoError";
  203. }
  204. }
  205. if(@args <= 3) {
  206. while(@args < 3) {
  207. push @args, "0";
  208. }
  209. } elsif(@args <= 6) {
  210. $asm .= "6";
  211. while(@args < 6) {
  212. push @args, "0";
  213. }
  214. } elsif(@args <= 9) {
  215. $asm .= "9";
  216. while(@args < 9) {
  217. push @args, "0";
  218. }
  219. } else {
  220. print STDERR "$ARGV:$.: too many arguments to system call\n";
  221. }
  222. # System call number.
  223. if($sysname eq "") {
  224. $sysname = "SYS_$func";
  225. $sysname =~ s/([a-z])([A-Z])/${1}_$2/g; # turn FooBar into Foo_Bar
  226. $sysname =~ y/a-z/A-Z/;
  227. }
  228. # Actual call.
  229. my $args = join(', ', @args);
  230. my $call = "$asm($sysname, $args)";
  231. # Assign return values.
  232. my $body = "";
  233. my @ret = ("_", "_", "_");
  234. my $do_errno = 0;
  235. for(my $i=0; $i<@out; $i++) {
  236. my $p = $out[$i];
  237. my ($name, $type) = parseparam($p);
  238. my $reg = "";
  239. if($name eq "err" && !$plan9) {
  240. $reg = "e1";
  241. $ret[2] = $reg;
  242. $do_errno = 1;
  243. } elsif($name eq "err" && $plan9) {
  244. $ret[0] = "r0";
  245. $ret[2] = "e1";
  246. next;
  247. } else {
  248. $reg = sprintf("r%d", $i);
  249. $ret[$i] = $reg;
  250. }
  251. if($type eq "bool") {
  252. $reg = "$reg != 0";
  253. }
  254. if($type eq "int64" && $_32bit ne "") {
  255. # 64-bit number in r1:r0 or r0:r1.
  256. if($i+2 > @out) {
  257. print STDERR "$ARGV:$.: not enough registers for int64 return\n";
  258. }
  259. if($_32bit eq "big-endian") {
  260. $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1);
  261. } else {
  262. $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i);
  263. }
  264. $ret[$i] = sprintf("r%d", $i);
  265. $ret[$i+1] = sprintf("r%d", $i+1);
  266. }
  267. if($reg ne "e1" || $plan9) {
  268. $body .= "\t$name = $type($reg)\n";
  269. }
  270. }
  271. if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") {
  272. $text .= "\t$call\n";
  273. } else {
  274. if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
  275. # raw syscall without error on Linux, see golang.org/issue/22924
  276. $text .= "\t$ret[0], $ret[1] := $call\n";
  277. } else {
  278. $text .= "\t$ret[0], $ret[1], $ret[2] := $call\n";
  279. }
  280. }
  281. $text .= $body;
  282. if ($plan9 && $ret[2] eq "e1") {
  283. $text .= "\tif int32(r0) == -1 {\n";
  284. $text .= "\t\terr = e1\n";
  285. $text .= "\t}\n";
  286. } elsif ($do_errno) {
  287. $text .= "\tif e1 != 0 {\n";
  288. $text .= "\t\terr = errnoErr(e1)\n";
  289. $text .= "\t}\n";
  290. }
  291. $text .= "\treturn\n";
  292. $text .= "}\n\n";
  293. }
  294. chomp $text;
  295. chomp $text;
  296. if($errors) {
  297. exit 1;
  298. }
  299. print <<EOF;
  300. // $cmdline
  301. // Code generated by the command above; see README.md. DO NOT EDIT.
  302. // +build $tags
  303. package unix
  304. import (
  305. "syscall"
  306. "unsafe"
  307. )
  308. var _ syscall.Errno
  309. $text
  310. EOF
  311. exit 0;