]> git.saurik.com Git - apple/xnu.git/blob - libsyscall/xcodescripts/create-syscalls.pl
xnu-1699.32.7.tar.gz
[apple/xnu.git] / libsyscall / xcodescripts / create-syscalls.pl
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
4 #
5 # @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 #
7 # This file contains Original Code and/or Modifications of Original Code
8 # as defined in and that are subject to the Apple Public Source License
9 # Version 2.0 (the 'License'). You may not use this file except in
10 # compliance with the License. Please obtain a copy of the License at
11 # http://www.opensource.apple.com/apsl/ and read it before using this
12 # file.
13 #
14 # The Original Code and all software distributed under the License are
15 # distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 # EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 # INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 # Please see the License for the specific language governing rights and
20 # limitations under the License.
21 #
22 # @APPLE_OSREFERENCE_LICENSE_HEADER_END@
23 #
24 ##########################################################################
25 #
26 # % create-syscalls.pl syscalls.master custom-directory out-directory
27 #
28 # This script fills the the out-directory with a Makefile.inc and *.s
29 # files to create the double-underbar syscall stubs. It reads the
30 # syscall.master file to get the symbol names and number of arguments,
31 # and whether Libsystem should automatically create the (non-double-underbar)
32 # stubs if Libc doesn't provide a wrapper. Which system calls will get
33 # the automatic treatment is writen to the libsyscall.list file, also
34 # written to the out-directory.
35 #
36 # The custom-directory contains:
37 # 1. SYS.h - used by the automatically created *.s and custom files
38 # 2. custom.s - contains architecture specific additional system calls and
39 # auxilliary routines (like cerror)
40 # 3. special case double-underbar stub files - which are copied into
41 # the out-directory
42 #
43 ##########################################################################
44
45 use strict;
46 use File::Basename ();
47 use File::Copy ();
48 use File::Spec;
49 use IO::File;
50
51 my $MyName = File::Basename::basename($0);
52
53 my @CustomSrc = qw(custom.s);
54
55 my @Architectures = split /\s/, $ENV{"ARCHS"};
56 my @Copy = (qw(SYS.h), @CustomSrc);
57 my $CustomDir;
58 my $PlatformsDir;
59 my $PlatformName;
60 my $OutDir;
61 # size in bytes of known types (only used for i386)
62 my %TypeBytes = (
63 'au_asid_t' => 4,
64 'caddr_t' => 4,
65 'gid_t' => 4,
66 'id_t' => 4,
67 'idtype_t' => 4,
68 'int' => 4,
69 'int32_t' => 4,
70 'int64_t' => 8,
71 'key_t' => 4,
72 'long' => 4,
73 'mach_port_name_t' => 4,
74 'mode_t' => 4,
75 'off_t' => 8,
76 'pid_t' => 4,
77 'semun_t' => 4,
78 'sigset_t' => 4,
79 'size_t' => 4,
80 'socklen_t' => 4,
81 'ssize_t' => 4,
82 'u_int' => 4,
83 'u_long' => 4,
84 'uid_t' => 4,
85 'uint32_t' => 4,
86 'uint64_t' => 8,
87 'user_addr_t' => 4,
88 'user_long_t' => 4,
89 'user_size_t' => 4,
90 'user_ssize_t' => 4,
91 'user_ulong_t' => 4,
92 );
93
94 # Moving towards storing all data in this hash, then we always know
95 # if data is aliased or not, or promoted or not.
96 my %Symbols = (
97 "quota" => {
98 c_sym => "quota",
99 syscall => "quota",
100 asm_sym => "_quota",
101 is_private => undef,
102 is_custom => undef,
103 nargs => 4,
104 bytes => 0,
105 aliases => {},
106 },
107 "setquota" => {
108 c_sym => "setquota",
109 syscall => "setquota",
110 asm_sym => "_setquota",
111 is_private => undef,
112 is_custom => undef,
113 nargs => 2,
114 bytes => 0,
115 aliases => {},
116 },
117 "syscall" => {
118 c_sym => "syscall",
119 syscall => "syscall",
120 asm_sym => "_syscall",
121 is_private => undef,
122 is_custom => undef,
123 nargs => 0,
124 bytes => 0,
125 aliases => {},
126 },
127 );
128
129 sub usage {
130 die "Usage: $MyName syscalls.master custom-directory platforms-directory out-directory\n";
131 }
132
133 ##########################################################################
134 # Read the syscall.master file and collect the system call names and number
135 # of arguments. It looks for the NO_SYSCALL_STUB quailifier following the
136 # prototype to determine if no automatic stub should be created by Libsystem.
137 # System call name that are already prefixed with double-underbar are set as
138 # if the NO_SYSCALL_STUB qualifier were specified (whether it is or not).
139 #
140 # For the #if lines in syscall.master, all macros are assumed to be defined,
141 # except COMPAT_GETFSSTAT (assumed undefined).
142 ##########################################################################
143 sub readMaster {
144 my $file = shift;
145 local $_;
146 my $f = IO::File->new($file, 'r');
147 die "$MyName: $file: $!\n" unless defined($f);
148 my $line = 0;
149 my $skip = 0;
150 while(<$f>) {
151 $line++;
152 if(/^#\s*endif/) {
153 $skip = 0;
154 next;
155 }
156 if(/^#\s*else/) {
157 $skip = -$skip;
158 next;
159 }
160 chomp;
161 if(/^#\s*if\s+(\S+)$/) {
162 $skip = ($1 eq 'COMPAT_GETFSSTAT') ? -1 : 1;
163 next;
164 }
165 next if $skip < 0;
166 next unless /^\d/;
167 s/^[^{]*{\s*//;
168 s/\s*}.*$//; # }
169 die "$MyName: no function prototype on line $line\n" unless length($_) > 0 && /;$/;
170 my $no_syscall_stub = /\)\s*NO_SYSCALL_STUB\s*;/;
171 my($name, $args) = /\s(\S+)\s*\(([^)]*)\)/;
172 next if $name =~ /e?nosys/;
173 $args =~ s/^\s+//;
174 $args =~ s/\s+$//;
175 my $argbytes = 0;
176 my $nargs = 0;
177 if($args ne '' && $args ne 'void') {
178 my @a = split(',', $args);
179 $nargs = scalar(@a);
180 # Calculate the size of all the arguments (only used for i386)
181 for my $type (@a) {
182 $type =~ s/\s*\w+$//; # remove the argument name
183 if($type =~ /\*$/) {
184 $argbytes += 4; # a pointer type
185 } else {
186 $type =~ s/^.*\s//; # remove any type qualifier, like unsigned
187 my $b = $TypeBytes{$type};
188 die "$MyName: $name: unknown type '$type'\n" unless defined($b);
189 $argbytes += $b;
190 }
191 }
192 }
193 $Symbols{$name} = {
194 c_sym => $name,
195 syscall => $name,
196 asm_sym => $no_syscall_stub ? "___$name" : "_$name",
197 is_private => $no_syscall_stub,
198 is_custom => undef,
199 nargs => $nargs,
200 bytes => $argbytes,
201 aliases => {},
202 except => [],
203 };
204 }
205 }
206
207 sub checkForCustomStubs {
208 my ($dir) = @_;
209
210 my ($c_sym_name, $sym);
211 while (($c_sym_name, $sym) = each %Symbols) {
212 my $source = "__".$$sym{c_sym}.".s";
213 my $custom = File::Spec->join($dir, $source);
214 next unless -f $custom;
215
216 $$sym{is_custom} = $source;
217 if (!$$sym{is_private}) {
218 foreach my $subarch (@Architectures) {
219 (my $arch = $subarch) =~ s/arm(.*)/arm/;
220 $$sym{aliases}{$arch} = [] unless $$sym{aliases}{$arch};
221 push(@{$$sym{aliases}{$arch}}, $$sym{asm_sym});
222 }
223 $$sym{asm_sym} = "__".$$sym{asm_sym};
224 $$sym{is_private} = 1;
225 }
226 }
227 }
228
229 sub readAliases {
230 my ($platformDir, $platformName) = @_;
231 my $genericMap = File::Spec->join($platformDir, "syscall.map");
232
233 my %sym_to_c;
234 foreach my $k (keys %Symbols) {
235 $sym_to_c{$Symbols{$k}{asm_sym}} = $k;
236 }
237
238 my @a = ();
239 for my $arch (@Architectures) {
240 (my $new_arch = $arch) =~ s/arm(.*)/arm/g;
241 push(@a, $new_arch) unless grep { $_ eq $new_arch } @a;
242 }
243
244 foreach my $arch (@a) {
245 my $syscallFile = File::Spec->join($platformDir, $platformName, $arch, "syscall.map");
246
247 my @files = ();
248 push(@files, IO::File->new($syscallFile, 'r'));
249 die "$MyName: $syscallFile: $!\n" unless defined($files[$#files]);
250 push(@files, IO::File->new($genericMap, 'r'));
251 die "$MyName: $genericMap: $!\n" unless defined($files[$#files]);
252
253 foreach my $f (@files) {
254 while (<$f>) {
255 next if /^#/;
256 chomp;
257
258 my ($alias, $target_symbol) = split;
259 if (defined($target_symbol)) {
260 foreach my $sym (values %Symbols) {
261 # I've eliminated most of the ugly from this script except
262 # the need to try stripping underbars here.
263 if ($$sym{is_private}) {
264 next unless $$sym{asm_sym} eq $target_symbol;
265 } else {
266 (my $target = $target_symbol) =~ s/^__//;
267 next unless ($$sym{asm_sym} eq $target || $$sym{asm_sym} eq $target_symbol);
268 }
269 $$sym{aliases}{$arch} = [] unless $$sym{aliases}{$arch};
270
271 die "$MyName: $arch $$sym{asm_sym} -> $alias: Duplicate alias.\n" if grep { $_ eq $alias } @{$$sym{aliases}{$arch}};
272 push(@{$$sym{aliases}{$arch}}, $alias);
273
274 # last thing to do, if we aliased over a first class symbol, we need
275 # to mark it
276 my $c = $sym_to_c{$alias};
277 if ($Symbols{$c}) {
278 push(@{$Symbols{$c}{except}}, $arch);
279 }
280 }
281 }
282 }
283 }
284 }
285 }
286
287 ##########################################################################
288 # Make a __xxx.s file: if it exists in the $CustomDir, just copy it, otherwise
289 # create one. We define the macro __SYSCALL_32BIT_ARG_BYTES so that SYS.h could
290 # use that to define __SYSCALL dependent on the arguments' total size.
291 ##########################################################################
292 sub writeStubForSymbol {
293 my ($f, $symbol) = @_;
294
295 my @conditions;
296 for my $subarch (@Architectures) {
297 (my $arch = $subarch) =~ s/arm(.*)/arm/;
298 push(@conditions, "defined(__${arch}__)") unless grep { $_ eq $arch } @{$$symbol{except}};
299 }
300
301 print $f "#define __SYSCALL_32BIT_ARG_BYTES $$symbol{bytes}\n";
302 print $f "#include \"SYS.h\"\n\n";
303 if (scalar(@conditions)) {
304 printf $f "#if " . join(" || ", @conditions) . "\n";
305 printf $f "__SYSCALL(%s, %s, %d)\n", $$symbol{asm_sym}, $$symbol{syscall}, $$symbol{nargs};
306 if (!$$symbol{is_private} && (scalar(@conditions) < scalar(@Architectures))) {
307 printf $f "#else\n";
308 printf $f "__SYSCALL(%s, %s, %d)\n", "__".$$symbol{asm_sym}, $$symbol{syscall}, $$symbol{nargs};
309 }
310 printf $f "#endif\n\n";
311 } else {
312 # actually this isnt an inconsistency. kernel can expose what it wants but if all our arches
313 # override it we need to honour that.
314 }
315 }
316
317 sub writeAliasesForSymbol {
318 my ($f, $symbol) = @_;
319
320 foreach my $subarch (@Architectures) {
321 (my $arch = $subarch) =~ s/arm(.*)/arm/;
322
323 next unless scalar($$symbol{aliases}{$arch});
324
325 printf $f "#if defined(__${arch}__)\n";
326 foreach my $alias_sym (@{$$symbol{aliases}{$arch}}) {
327 my $sym = (grep { $_ eq $arch } @{$$symbol{except}}) ? "__".$$symbol{asm_sym} : $$symbol{asm_sym};
328
329 printf $f "\t.globl\t$alias_sym\n";
330 printf $f "\t.set\t$alias_sym, $sym\n";
331 }
332 printf $f "#endif\n\n";
333 }
334 }
335
336 usage() unless scalar(@ARGV) == 5;
337 $CustomDir = $ARGV[1];
338 die "$MyName: $CustomDir: No such directory\n" unless -d $CustomDir;
339 $PlatformsDir = $ARGV[2];
340 die "$MyName: $PlatformsDir: No such directory\n" unless -d $PlatformsDir;
341 $PlatformName = $ARGV[3];
342 die "$MyName: $PlatformsDir/$PlatformName: No such directory\n" unless -d "$PlatformsDir/$PlatformName";
343 $OutDir = $ARGV[4];
344 die "$MyName: $OutDir: No such directory\n" unless -d $OutDir;
345
346 readMaster($ARGV[0]);
347 checkForCustomStubs($CustomDir);
348 readAliases($PlatformsDir, $PlatformName);
349
350 ##########################################################################
351 # copy the files specified in @Copy from the $CustomDir to $OutDir
352 ##########################################################################
353 for(@Copy) {
354 my $custom = File::Spec->join($CustomDir, $_);
355 my $path = File::Spec->join($OutDir, $_);
356 print "Copy $custom -> $path\n";
357 File::Copy::copy($custom, $path) || die "$MyName: copy($custom, $path): $!\n";
358 }
359
360 ##########################################################################
361 # make all the *.s files
362 ##########################################################################
363 my @src;
364 my($k, $sym);
365 while (($k, $sym) = each %Symbols)
366 {
367 my $srcname = $$sym{asm_sym} . ".s";
368 my $outpath = File::Spec->join($OutDir, $srcname);
369
370 if ($$sym{is_custom}) {
371 my $custom = File::Spec->join($CustomDir, $$sym{is_custom});
372 File::Copy::copy($custom, $outpath);
373 print "Copied $outpath\n";
374
375 print "Writing aliases for $srcname\n";
376 my $f = IO::File->new($outpath, 'a');
377 die "$MyName: $outpath: $!\n" unless defined($f);
378 writeAliasesForSymbol($f, $sym);
379 undef $f;
380 } else {
381 my $f = IO::File->new($outpath, 'w');
382 die "$MyName: $outpath: $!\n" unless defined($f);
383
384 printf "Creating $outpath\n";
385 writeStubForSymbol($f, $sym);
386 writeAliasesForSymbol($f, $sym);
387 undef $f;
388 }
389 push(@src, $srcname);
390 }
391
392 ##########################################################################
393 # create the Makefile.inc file from the list for files in @src and @CustomSrc
394 ##########################################################################
395 my $path = File::Spec->join($OutDir, 'stubs.list');
396 my $f = IO::File->new($path, 'w');
397 my @sources = sort(@src, @CustomSrc);
398 for my $s (@sources) {
399 printf $f File::Spec->join($OutDir, $s) . "\n";
400 }
401 undef $f;
402 undef $path;
403