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 # Types that potentially have different sizes in user-space compared to
98 # kernel-space as well as whether the value should be sign/zero-extended when
99 # passing the user/kernel boundary.
100 my %UserKernelMismatchTypes = (
101 'long' => 'SIGN_EXTEND',
102 'size_t' => 'ZERO_EXTEND',
103 'u_long' => 'ZERO_EXTEND',
104 'user_size_t' => 'ZERO_EXTEND',
105 'user_ssize_t' => 'SIGN_EXTEND'
108 # Moving towards storing all data in this hash, then we always know
109 # if data is aliased or not, or promoted or not.
113 syscall => "syscall",
114 asm_sym
=> "_syscall",
120 mismatch_args
=> {}, # Arguments that might need to be zero/sign-extended
124 # An explicit list of cancelable syscalls. For creating stubs that call the
125 # cancellable version of cerror.
127 accept access aio_suspend
128 close connect connectx
130 faccessat
fcntl fdatasync fpathconf fstat fstatat fsync
133 link linkat lseek
lstat
136 pathconf peeloff poll posix_spawn pread pselect pwrite
137 read readv recvfrom recvmsg
rename renameat
139 __semwait_signal __sigwait
140 select sem_wait
semop sendmsg sendto sigsuspend
stat symlink symlinkat sync
142 wait4 waitid
write writev
146 die "Usage: $MyName syscalls.master custom-directory platforms-directory platform-name out-directory\n";
149 ##########################################################################
150 # Read the syscall.master file and collect the system call names and number
151 # of arguments. It looks for the NO_SYSCALL_STUB quailifier following the
152 # prototype to determine if no automatic stub should be created by Libsystem.
153 # System call name that are already prefixed with double-underbar are set as
154 # if the NO_SYSCALL_STUB qualifier were specified (whether it is or not).
156 # For the #if lines in syscall.master, all macros are assumed to be defined,
157 # except COMPAT_GETFSSTAT (assumed undefined).
158 ##########################################################################
162 my $f = IO
::File-
>new($file, 'r');
163 die "$MyName: $file: $!\n" unless defined($f);
177 if(/^#\s*if\s+(\S+)$/) {
178 $skip = ($1 eq 'COMPAT_GETFSSTAT') ? -1 : 1;
185 die "$MyName: no function prototype on line $line\n" unless length($_) > 0 && /;$/;
186 my $no_syscall_stub = /\)\s*NO_SYSCALL_STUB\s*;/;
187 my($name, $args) = /\s(\S+)\s*\(([^)]*)\)/;
188 next if $name =~ /e?nosys/;
194 if($args ne '' && $args ne 'void') {
195 my @a = split(',', $args);
199 $type =~ s/\s*\w+$//; # remove the argument name
201 # Calculate the size of all the arguments (only used for i386)
203 $argbytes += 4; # a pointer type
205 $type =~ s/^.*\s//; # remove any type qualifier, like unsigned
206 my $b = $TypeBytes{$type};
207 die "$MyName: $name: unknown type '$type'\n" unless defined($b);
210 # Determine which arguments might need to be zero/sign-extended
211 if(exists $UserKernelMismatchTypes{$type}) {
212 $mismatch_args{$index} = $UserKernelMismatchTypes{$type};
221 asm_sym
=> $no_syscall_stub ? "___$name" : "_$name",
222 is_private
=> $no_syscall_stub,
227 mismatch_args
=> \
%mismatch_args, # Arguments that might need to be zero/sign-extended
233 sub checkForCustomStubs
{
236 my ($c_sym_name, $sym);
237 while (($c_sym_name, $sym) = each %Symbols) {
238 my $source = "__".$$sym{c_sym
}.".s";
239 my $custom = File
::Spec-
>join($dir, $source);
240 next unless -f
$custom;
242 $$sym{is_custom
} = $source;
243 if (!$$sym{is_private
}) {
244 foreach my $subarch (@Architectures) {
245 (my $arch = $subarch) =~ s/arm(v.*)/arm/;
246 $arch =~ s/x86_64(.*)/x86_64/;
247 $arch =~ s/arm64(.*)/arm64/;
248 $$sym{aliases
}{$arch} = [] unless $$sym{aliases
}{$arch};
249 push(@{$$sym{aliases
}{$arch}}, $$sym{asm_sym
});
251 $$sym{asm_sym
} = "__".$$sym{asm_sym
};
252 $$sym{is_private
} = 1;
258 my ($platformDir, $platformName) = @_;
259 my $genericMap = File
::Spec-
>join($platformDir, "syscall.map");
262 foreach my $k (keys %Symbols) {
263 $sym_to_c{$Symbols{$k}{asm_sym
}} = $k;
267 for my $arch (@Architectures) {
268 (my $new_arch = $arch) =~ s/arm(v.*)/arm/g;
269 $new_arch =~ s/x86_64(.*)/x86_64/g;
270 $new_arch =~ s/arm64(.*)/arm64/g;
271 push(@a, $new_arch) unless grep { $_ eq $new_arch } @a;
274 foreach my $arch (@a) {
275 my $syscallFile = File
::Spec-
>join($platformDir, $platformName, $arch, "syscall.map");
278 push(@files, IO
::File-
>new($syscallFile, 'r'));
279 die "$MyName: $syscallFile: $!\n" unless defined($files[$#files]);
280 push(@files, IO
::File-
>new($genericMap, 'r'));
281 die "$MyName: $genericMap: $!\n" unless defined($files[$#files]);
283 foreach my $f (@files) {
288 my ($alias, $target_symbol) = split;
289 if (defined($target_symbol)) {
290 foreach my $sym (values %Symbols) {
291 # I've eliminated most of the ugly from this script except
292 # the need to try stripping underbars here.
293 if ($$sym{is_private
}) {
294 next unless $$sym{asm_sym
} eq $target_symbol;
296 (my $target = $target_symbol) =~ s/^__//;
297 next unless ($$sym{asm_sym
} eq $target || $$sym{asm_sym
} eq $target_symbol);
299 $$sym{aliases
}{$arch} = [] unless $$sym{aliases
}{$arch};
301 die "$MyName: $arch $$sym{asm_sym} -> $alias: Duplicate alias.\n" if grep { $_ eq $alias } @{$$sym{aliases
}{$arch}};
302 push(@{$$sym{aliases
}{$arch}}, $alias);
304 # last thing to do, if we aliased over a first class symbol, we need
306 my $c = $sym_to_c{$alias};
308 push(@{$Symbols{$c}{except
}}, $arch);
317 ##########################################################################
318 # Make a __xxx.s file: if it exists in the $CustomDir, just copy it, otherwise
319 # create one. We define the macro __SYSCALL_32BIT_ARG_BYTES so that SYS.h could
320 # use that to define __SYSCALL dependent on the arguments' total size.
321 ##########################################################################
322 sub writeStubForSymbol
{
323 my ($f, $symbol) = @_;
327 for my $subarch (@Architectures) {
328 (my $arch = $subarch) =~ s/arm(v.*)/arm/;
329 $arch =~ s/x86_64(.*)/x86_64/;
330 $arch =~ s/arm64(.*)/arm64/;
331 push(@conditions, "defined(__${arch}__)") unless grep { $_ eq $arch } @{$$symbol{except
}};
333 if($arch == 'arm64') {
334 $has_arm64 = 1 unless grep { $_ eq $arch } @{$$symbol{except
}};
339 for (@Cancelable) { $is_cancel{$_} = 1 };
341 print $f "#define __SYSCALL_32BIT_ARG_BYTES $$symbol{bytes}\n";
342 print $f "#include \"SYS.h\"\n\n";
344 if (scalar(@conditions)) {
345 printf $f "#ifndef SYS_%s\n", $$symbol{syscall};
346 printf $f "#error \"SYS_%s not defined. The header files libsyscall is building against do not match syscalls.master.\"\n", $$symbol{syscall};
347 printf $f "#endif\n\n";
350 my $nc = ($is_cancel{$$symbol{syscall}} ? "cerror" : "cerror_nocancel");
353 printf $f "#if defined(__arm64__)\n";
354 printf $f "MI_ENTRY_POINT(%s)\n", $$symbol{asm_sym
};
355 if(keys %{$$symbol{mismatch_args
}}) {
356 while(my($argnum, $extend) = each %{$$symbol{mismatch_args
}}) {
357 printf $f "%s(%d)\n", $extend, $argnum;
361 printf $f "SYSCALL_NONAME(%s, %d, %s)\n", $$symbol{syscall}, $$symbol{nargs
}, $nc;
366 if (scalar(@conditions)) {
367 printf $f "#if " . join(" || ", @conditions) . "\n";
368 printf $f "__SYSCALL2(%s, %s, %d, %s)\n", $$symbol{asm_sym
}, $$symbol{syscall}, $$symbol{nargs
}, $nc;
369 if (!$$symbol{is_private
} && (scalar(@conditions) < scalar(@Architectures))) {
371 printf $f "__SYSCALL2(%s, %s, %d, %s)\n", "__".$$symbol{asm_sym
}, $$symbol{syscall}, $$symbol{nargs
}, $nc;
373 printf $f "#endif\n\n";
375 # actually this isnt an inconsistency. kernel can expose what it wants but if all our arches
376 # override it we need to honour that.
380 printf $f "#endif\n\n";
384 sub writeAliasesForSymbol
{
385 my ($f, $symbol) = @_;
387 foreach my $subarch (@Architectures) {
388 (my $arch = $subarch) =~ s/arm(v.*)/arm/;
389 $arch =~ s/x86_64(.*)/x86_64/;
390 $arch =~ s/arm64(.*)/arm64/;
392 next unless scalar($$symbol{aliases
}{$arch});
394 printf $f "#if defined(__${arch}__)\n";
395 foreach my $alias_sym (@{$$symbol{aliases
}{$arch}}) {
396 my $sym = (grep { $_ eq $arch } @{$$symbol{except
}}) ? "__".$$symbol{asm_sym
} : $$symbol{asm_sym
};
398 printf $f "\t.globl\t$alias_sym\n";
399 printf $f "\t.set\t$alias_sym, $sym\n";
401 printf $f "#endif\n\n";
405 usage
() unless scalar(@ARGV) == 5;
406 $CustomDir = $ARGV[1];
407 die "$MyName: $CustomDir: No such directory\n" unless -d
$CustomDir;
408 $PlatformsDir = $ARGV[2];
409 die "$MyName: $PlatformsDir: No such directory\n" unless -d
$PlatformsDir;
410 $PlatformName = $ARGV[3];
411 die "$MyName: $PlatformsDir/$PlatformName: No such directory\n" unless -d
"$PlatformsDir/$PlatformName";
413 die "$MyName: $OutDir: No such directory\n" unless -d
$OutDir;
415 readMaster
($ARGV[0]);
416 checkForCustomStubs
($CustomDir);
417 readAliases
($PlatformsDir, $PlatformName);
419 ##########################################################################
420 # copy the files specified in @Copy from the $CustomDir to $OutDir
421 ##########################################################################
423 my $custom = File
::Spec-
>join($CustomDir, $_);
424 my $path = File
::Spec-
>join($OutDir, $_);
425 print "Copy $custom -> $path\n";
426 File
::Copy
::copy
($custom, $path) || die "$MyName: copy($custom, $path): $!\n";
429 ##########################################################################
430 # make all the *.s files
431 ##########################################################################
434 while (($k, $sym) = each %Symbols)
436 my $srcname = $$sym{asm_sym
} . ".s";
437 my $outpath = File
::Spec-
>join($OutDir, $srcname);
439 if ($$sym{is_custom
}) {
440 my $custom = File
::Spec-
>join($CustomDir, $$sym{is_custom
});
441 File
::Copy
::copy
($custom, $outpath);
442 print "Copied $outpath\n";
444 print "Writing aliases for $srcname\n";
445 my $f = IO
::File-
>new($outpath, 'a');
446 die "$MyName: $outpath: $!\n" unless defined($f);
447 writeAliasesForSymbol
($f, $sym);
450 my $f = IO
::File-
>new($outpath, 'w');
451 die "$MyName: $outpath: $!\n" unless defined($f);
453 printf "Creating $outpath\n";
454 writeStubForSymbol
($f, $sym);
455 writeAliasesForSymbol
($f, $sym);
458 push(@src, $srcname);
461 ##########################################################################
462 # create the Makefile.inc file from the list for files in @src and @CustomSrc
463 ##########################################################################
464 my $path = File
::Spec-
>join($OutDir, 'stubs.list');
465 my $f = IO
::File-
>new($path, 'w');
466 my @sources = sort(@src, @CustomSrc);
467 for my $s (@sources) {
468 printf $f File
::Spec-
>join($OutDir, $s) . "\n";