3 # Copyright (c) 2006-2014 Apple Inc. All rights reserved.
5 # @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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
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.
22 # @APPLE_OSREFERENCE_LICENSE_HEADER_END@
24 ##########################################################################
26 # % create-syscalls.pl syscalls.master custom-directory platforms-directory platform-name out-directory
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.
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
43 ##########################################################################
46 use File
::Basename
();
51 my $MyName = File
::Basename
::basename
($0);
53 my @CustomSrc = qw(custom.s);
55 my @Architectures = split /\s/, $ENV{"ARCHS"};
56 my @Copy = (qw(SYS.h), @CustomSrc);
61 # size in bytes of known types (only used for i386)
75 'mach_port_name_t' => 4,
97 # Moving towards storing all data in this hash, then we always know
98 # if data is aliased or not, or promoted or not.
102 syscall => "syscall",
103 asm_sym
=> "_syscall",
112 # An explicit list of cancelable syscalls. For creating stubs that call the
113 # cancellable version of cerror.
115 accept access aio_suspend
116 close connect connectx
118 faccessat
fcntl fdatasync fpathconf fstat fstatat fsync
121 link linkat lseek
lstat
124 pathconf peeloff poll posix_spawn pread pselect pwrite
125 read readv recvfrom recvmsg
rename renameat
127 __semwait_signal __sigwait
128 select sem_wait
semop sendmsg sendto sigsuspend
stat symlink symlinkat sync
130 wait4 waitid
write writev
134 die "Usage: $MyName syscalls.master custom-directory platforms-directory platform-name out-directory\n";
137 ##########################################################################
138 # Read the syscall.master file and collect the system call names and number
139 # of arguments. It looks for the NO_SYSCALL_STUB quailifier following the
140 # prototype to determine if no automatic stub should be created by Libsystem.
141 # System call name that are already prefixed with double-underbar are set as
142 # if the NO_SYSCALL_STUB qualifier were specified (whether it is or not).
144 # For the #if lines in syscall.master, all macros are assumed to be defined,
145 # except COMPAT_GETFSSTAT (assumed undefined).
146 ##########################################################################
150 my $f = IO
::File-
>new($file, 'r');
151 die "$MyName: $file: $!\n" unless defined($f);
165 if(/^#\s*if\s+(\S+)$/) {
166 $skip = ($1 eq 'COMPAT_GETFSSTAT') ? -1 : 1;
173 die "$MyName: no function prototype on line $line\n" unless length($_) > 0 && /;$/;
174 my $no_syscall_stub = /\)\s*NO_SYSCALL_STUB\s*;/;
175 my($name, $args) = /\s(\S+)\s*\(([^)]*)\)/;
176 next if $name =~ /e?nosys/;
181 if($args ne '' && $args ne 'void') {
182 my @a = split(',', $args);
184 # Calculate the size of all the arguments (only used for i386)
186 $type =~ s/\s*\w+$//; # remove the argument name
188 $argbytes += 4; # a pointer type
190 $type =~ s/^.*\s//; # remove any type qualifier, like unsigned
191 my $b = $TypeBytes{$type};
192 die "$MyName: $name: unknown type '$type'\n" unless defined($b);
200 asm_sym
=> $no_syscall_stub ? "___$name" : "_$name",
201 is_private
=> $no_syscall_stub,
211 sub checkForCustomStubs
{
214 my ($c_sym_name, $sym);
215 while (($c_sym_name, $sym) = each %Symbols) {
216 my $source = "__".$$sym{c_sym
}.".s";
217 my $custom = File
::Spec-
>join($dir, $source);
218 next unless -f
$custom;
220 $$sym{is_custom
} = $source;
221 if (!$$sym{is_private
}) {
222 foreach my $subarch (@Architectures) {
223 (my $arch = $subarch) =~ s/arm(v.*)/arm/;
224 $arch =~ s/x86_64(.*)/x86_64/;
225 $arch =~ s/arm64(.*)/arm64/;
226 $$sym{aliases
}{$arch} = [] unless $$sym{aliases
}{$arch};
227 push(@{$$sym{aliases
}{$arch}}, $$sym{asm_sym
});
229 $$sym{asm_sym
} = "__".$$sym{asm_sym
};
230 $$sym{is_private
} = 1;
236 my ($platformDir, $platformName) = @_;
237 my $genericMap = File
::Spec-
>join($platformDir, "syscall.map");
240 foreach my $k (keys %Symbols) {
241 $sym_to_c{$Symbols{$k}{asm_sym
}} = $k;
245 for my $arch (@Architectures) {
246 (my $new_arch = $arch) =~ s/arm(v.*)/arm/g;
247 $new_arch =~ s/x86_64(.*)/x86_64/g;
248 $new_arch =~ s/arm64(.*)/arm64/g;
249 push(@a, $new_arch) unless grep { $_ eq $new_arch } @a;
252 foreach my $arch (@a) {
253 my $syscallFile = File
::Spec-
>join($platformDir, $platformName, $arch, "syscall.map");
256 push(@files, IO
::File-
>new($syscallFile, 'r'));
257 die "$MyName: $syscallFile: $!\n" unless defined($files[$#files]);
258 push(@files, IO
::File-
>new($genericMap, 'r'));
259 die "$MyName: $genericMap: $!\n" unless defined($files[$#files]);
261 foreach my $f (@files) {
266 my ($alias, $target_symbol) = split;
267 if (defined($target_symbol)) {
268 foreach my $sym (values %Symbols) {
269 # I've eliminated most of the ugly from this script except
270 # the need to try stripping underbars here.
271 if ($$sym{is_private
}) {
272 next unless $$sym{asm_sym
} eq $target_symbol;
274 (my $target = $target_symbol) =~ s/^__//;
275 next unless ($$sym{asm_sym
} eq $target || $$sym{asm_sym
} eq $target_symbol);
277 $$sym{aliases
}{$arch} = [] unless $$sym{aliases
}{$arch};
279 die "$MyName: $arch $$sym{asm_sym} -> $alias: Duplicate alias.\n" if grep { $_ eq $alias } @{$$sym{aliases
}{$arch}};
280 push(@{$$sym{aliases
}{$arch}}, $alias);
282 # last thing to do, if we aliased over a first class symbol, we need
284 my $c = $sym_to_c{$alias};
286 push(@{$Symbols{$c}{except
}}, $arch);
295 ##########################################################################
296 # Make a __xxx.s file: if it exists in the $CustomDir, just copy it, otherwise
297 # create one. We define the macro __SYSCALL_32BIT_ARG_BYTES so that SYS.h could
298 # use that to define __SYSCALL dependent on the arguments' total size.
299 ##########################################################################
300 sub writeStubForSymbol
{
301 my ($f, $symbol) = @_;
304 for my $subarch (@Architectures) {
305 (my $arch = $subarch) =~ s/arm(v.*)/arm/;
306 $arch =~ s/x86_64(.*)/x86_64/;
307 $arch =~ s/arm64(.*)/arm64/;
308 push(@conditions, "defined(__${arch}__)") unless grep { $_ eq $arch } @{$$symbol{except
}};
312 for (@Cancelable) { $is_cancel{$_} = 1 };
314 print $f "#define __SYSCALL_32BIT_ARG_BYTES $$symbol{bytes}\n";
315 print $f "#include \"SYS.h\"\n\n";
316 if (scalar(@conditions)) {
317 printf $f "#ifndef SYS_%s\n", $$symbol{syscall};
318 printf $f "#error \"SYS_%s not defined. The header files libsyscall is building against do not match syscalls.master.\"\n", $$symbol{syscall};
319 printf $f "#endif\n\n";
320 my $nc = ($is_cancel{$$symbol{syscall}} ? "cerror" : "cerror_nocancel");
321 printf $f "#if " . join(" || ", @conditions) . "\n";
322 printf $f "__SYSCALL2(%s, %s, %d, %s)\n", $$symbol{asm_sym
}, $$symbol{syscall}, $$symbol{nargs
}, $nc;
323 if (!$$symbol{is_private
} && (scalar(@conditions) < scalar(@Architectures))) {
325 printf $f "__SYSCALL2(%s, %s, %d, %s)\n", "__".$$symbol{asm_sym
}, $$symbol{syscall}, $$symbol{nargs
}, $nc;
327 printf $f "#endif\n\n";
329 # actually this isnt an inconsistency. kernel can expose what it wants but if all our arches
330 # override it we need to honour that.
334 sub writeAliasesForSymbol
{
335 my ($f, $symbol) = @_;
337 foreach my $subarch (@Architectures) {
338 (my $arch = $subarch) =~ s/arm(v.*)/arm/;
339 $arch =~ s/x86_64(.*)/x86_64/;
340 $arch =~ s/arm64(.*)/arm64/;
342 next unless scalar($$symbol{aliases
}{$arch});
344 printf $f "#if defined(__${arch}__)\n";
345 foreach my $alias_sym (@{$$symbol{aliases
}{$arch}}) {
346 my $sym = (grep { $_ eq $arch } @{$$symbol{except
}}) ? "__".$$symbol{asm_sym
} : $$symbol{asm_sym
};
348 printf $f "\t.globl\t$alias_sym\n";
349 printf $f "\t.set\t$alias_sym, $sym\n";
351 printf $f "#endif\n\n";
355 usage
() unless scalar(@ARGV) == 5;
356 $CustomDir = $ARGV[1];
357 die "$MyName: $CustomDir: No such directory\n" unless -d
$CustomDir;
358 $PlatformsDir = $ARGV[2];
359 die "$MyName: $PlatformsDir: No such directory\n" unless -d
$PlatformsDir;
360 $PlatformName = $ARGV[3];
361 die "$MyName: $PlatformsDir/$PlatformName: No such directory\n" unless -d
"$PlatformsDir/$PlatformName";
363 die "$MyName: $OutDir: No such directory\n" unless -d
$OutDir;
365 readMaster
($ARGV[0]);
366 checkForCustomStubs
($CustomDir);
367 readAliases
($PlatformsDir, $PlatformName);
369 ##########################################################################
370 # copy the files specified in @Copy from the $CustomDir to $OutDir
371 ##########################################################################
373 my $custom = File
::Spec-
>join($CustomDir, $_);
374 my $path = File
::Spec-
>join($OutDir, $_);
375 print "Copy $custom -> $path\n";
376 File
::Copy
::copy
($custom, $path) || die "$MyName: copy($custom, $path): $!\n";
379 ##########################################################################
380 # make all the *.s files
381 ##########################################################################
384 while (($k, $sym) = each %Symbols)
386 my $srcname = $$sym{asm_sym
} . ".s";
387 my $outpath = File
::Spec-
>join($OutDir, $srcname);
389 if ($$sym{is_custom
}) {
390 my $custom = File
::Spec-
>join($CustomDir, $$sym{is_custom
});
391 File
::Copy
::copy
($custom, $outpath);
392 print "Copied $outpath\n";
394 print "Writing aliases for $srcname\n";
395 my $f = IO
::File-
>new($outpath, 'a');
396 die "$MyName: $outpath: $!\n" unless defined($f);
397 writeAliasesForSymbol
($f, $sym);
400 my $f = IO
::File-
>new($outpath, 'w');
401 die "$MyName: $outpath: $!\n" unless defined($f);
403 printf "Creating $outpath\n";
404 writeStubForSymbol
($f, $sym);
405 writeAliasesForSymbol
($f, $sym);
408 push(@src, $srcname);
411 ##########################################################################
412 # create the Makefile.inc file from the list for files in @src and @CustomSrc
413 ##########################################################################
414 my $path = File
::Spec-
>join($OutDir, 'stubs.list');
415 my $f = IO
::File-
>new($path, 'w');
416 my @sources = sort(@src, @CustomSrc);
417 for my $s (@sources) {
418 printf $f File
::Spec-
>join($OutDir, $s) . "\n";