1*c54f35caSApple OSS Distributions#!/usr/bin/perl 2*c54f35caSApple OSS Distributions# 3*c54f35caSApple OSS Distributions# Copyright (c) 2006-2014 Apple Inc. All rights reserved. 4*c54f35caSApple OSS Distributions# 5*c54f35caSApple OSS Distributions# @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 6*c54f35caSApple OSS Distributions# 7*c54f35caSApple OSS Distributions# This file contains Original Code and/or Modifications of Original Code 8*c54f35caSApple OSS Distributions# as defined in and that are subject to the Apple Public Source License 9*c54f35caSApple OSS Distributions# Version 2.0 (the 'License'). You may not use this file except in 10*c54f35caSApple OSS Distributions# compliance with the License. Please obtain a copy of the License at 11*c54f35caSApple OSS Distributions# http://www.opensource.apple.com/apsl/ and read it before using this 12*c54f35caSApple OSS Distributions# file. 13*c54f35caSApple OSS Distributions# 14*c54f35caSApple OSS Distributions# The Original Code and all software distributed under the License are 15*c54f35caSApple OSS Distributions# distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 16*c54f35caSApple OSS Distributions# EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 17*c54f35caSApple OSS Distributions# INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 18*c54f35caSApple OSS Distributions# FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 19*c54f35caSApple OSS Distributions# Please see the License for the specific language governing rights and 20*c54f35caSApple OSS Distributions# limitations under the License. 21*c54f35caSApple OSS Distributions# 22*c54f35caSApple OSS Distributions# @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 23*c54f35caSApple OSS Distributions# 24*c54f35caSApple OSS Distributions########################################################################## 25*c54f35caSApple OSS Distributions# 26*c54f35caSApple OSS Distributions# % create-syscalls.pl syscalls.master custom-directory platforms-directory platform-name out-directory 27*c54f35caSApple OSS Distributions# 28*c54f35caSApple OSS Distributions# This script fills the the out-directory with a Makefile.inc and *.s 29*c54f35caSApple OSS Distributions# files to create the double-underbar syscall stubs. It reads the 30*c54f35caSApple OSS Distributions# syscall.master file to get the symbol names and number of arguments, 31*c54f35caSApple OSS Distributions# and whether Libsystem should automatically create the (non-double-underbar) 32*c54f35caSApple OSS Distributions# stubs if Libc doesn't provide a wrapper. Which system calls will get 33*c54f35caSApple OSS Distributions# the automatic treatment is writen to the libsyscall.list file, also 34*c54f35caSApple OSS Distributions# written to the out-directory. 35*c54f35caSApple OSS Distributions# 36*c54f35caSApple OSS Distributions# The custom-directory contains: 37*c54f35caSApple OSS Distributions# 1. SYS.h - used by the automatically created *.s and custom files 38*c54f35caSApple OSS Distributions# 2. custom.s - contains architecture specific additional system calls and 39*c54f35caSApple OSS Distributions# auxilliary routines (like cerror) 40*c54f35caSApple OSS Distributions# 3. special case double-underbar stub files - which are copied into 41*c54f35caSApple OSS Distributions# the out-directory 42*c54f35caSApple OSS Distributions# 43*c54f35caSApple OSS Distributions########################################################################## 44*c54f35caSApple OSS Distributions 45*c54f35caSApple OSS Distributionsuse strict; 46*c54f35caSApple OSS Distributionsuse File::Basename (); 47*c54f35caSApple OSS Distributionsuse File::Copy (); 48*c54f35caSApple OSS Distributionsuse File::Spec; 49*c54f35caSApple OSS Distributionsuse IO::File; 50*c54f35caSApple OSS Distributions 51*c54f35caSApple OSS Distributionsmy $MyName = File::Basename::basename($0); 52*c54f35caSApple OSS Distributions 53*c54f35caSApple OSS Distributionsmy @CustomSrc = qw(custom.s); 54*c54f35caSApple OSS Distributions 55*c54f35caSApple OSS Distributionsmy @Architectures = split /\s/, $ENV{"ARCHS"}; 56*c54f35caSApple OSS Distributionsmy @Copy = (qw(SYS.h), @CustomSrc); 57*c54f35caSApple OSS Distributionsmy $CustomDir; 58*c54f35caSApple OSS Distributionsmy $PlatformsDir; 59*c54f35caSApple OSS Distributionsmy $PlatformName; 60*c54f35caSApple OSS Distributionsmy $OutDir; 61*c54f35caSApple OSS Distributions# size in bytes of known types (only used for i386) 62*c54f35caSApple OSS Distributionsmy %TypeBytes = ( 63*c54f35caSApple OSS Distributions 'au_asid_t' => 4, 64*c54f35caSApple OSS Distributions 'sae_associd_t' => 4, 65*c54f35caSApple OSS Distributions 'caddr_t' => 4, 66*c54f35caSApple OSS Distributions 'sae_connid_t' => 4, 67*c54f35caSApple OSS Distributions 'gid_t' => 4, 68*c54f35caSApple OSS Distributions 'id_t' => 4, 69*c54f35caSApple OSS Distributions 'idtype_t' => 4, 70*c54f35caSApple OSS Distributions 'int' => 4, 71*c54f35caSApple OSS Distributions 'int32_t' => 4, 72*c54f35caSApple OSS Distributions 'int64_t' => 8, 73*c54f35caSApple OSS Distributions 'key_t' => 4, 74*c54f35caSApple OSS Distributions 'long' => 4, 75*c54f35caSApple OSS Distributions 'mach_port_name_t' => 4, 76*c54f35caSApple OSS Distributions 'mode_t' => 4, 77*c54f35caSApple OSS Distributions 'off_t' => 8, 78*c54f35caSApple OSS Distributions 'pid_t' => 4, 79*c54f35caSApple OSS Distributions 'semun_t' => 4, 80*c54f35caSApple OSS Distributions 'sigset_t' => 4, 81*c54f35caSApple OSS Distributions 'size_t' => 4, 82*c54f35caSApple OSS Distributions 'socklen_t' => 4, 83*c54f35caSApple OSS Distributions 'ssize_t' => 4, 84*c54f35caSApple OSS Distributions 'u_int' => 4, 85*c54f35caSApple OSS Distributions 'u_long' => 4, 86*c54f35caSApple OSS Distributions 'uid_t' => 4, 87*c54f35caSApple OSS Distributions 'uint32_t' => 4, 88*c54f35caSApple OSS Distributions 'uint64_t' => 8, 89*c54f35caSApple OSS Distributions 'user_addr_t' => 4, 90*c54f35caSApple OSS Distributions 'user_long_t' => 4, 91*c54f35caSApple OSS Distributions 'user_size_t' => 4, 92*c54f35caSApple OSS Distributions 'user_ssize_t' => 4, 93*c54f35caSApple OSS Distributions 'user_ulong_t' => 4, 94*c54f35caSApple OSS Distributions 'uuid_t' => 4, 95*c54f35caSApple OSS Distributions); 96*c54f35caSApple OSS Distributions 97*c54f35caSApple OSS Distributions# Types that potentially have different sizes in user-space compared to 98*c54f35caSApple OSS Distributions# kernel-space as well as whether the value should be sign/zero-extended when 99*c54f35caSApple OSS Distributions# passing the user/kernel boundary. 100*c54f35caSApple OSS Distributionsmy %UserKernelMismatchTypes = ( 101*c54f35caSApple OSS Distributions 'long' => 'SIGN_EXTEND', 102*c54f35caSApple OSS Distributions 'size_t' => 'ZERO_EXTEND', 103*c54f35caSApple OSS Distributions 'u_long' => 'ZERO_EXTEND', 104*c54f35caSApple OSS Distributions 'user_size_t' => 'ZERO_EXTEND', 105*c54f35caSApple OSS Distributions 'user_ssize_t' => 'SIGN_EXTEND' 106*c54f35caSApple OSS Distributions); 107*c54f35caSApple OSS Distributions 108*c54f35caSApple OSS Distributions# Moving towards storing all data in this hash, then we always know 109*c54f35caSApple OSS Distributions# if data is aliased or not, or promoted or not. 110*c54f35caSApple OSS Distributionsmy %Symbols = ( 111*c54f35caSApple OSS Distributions "syscall" => { 112*c54f35caSApple OSS Distributions c_sym => "syscall", 113*c54f35caSApple OSS Distributions syscall => "syscall", 114*c54f35caSApple OSS Distributions asm_sym => "_syscall", 115*c54f35caSApple OSS Distributions is_private => undef, 116*c54f35caSApple OSS Distributions is_custom => undef, 117*c54f35caSApple OSS Distributions nargs => 0, 118*c54f35caSApple OSS Distributions bytes => 0, 119*c54f35caSApple OSS Distributions aliases => {}, 120*c54f35caSApple OSS Distributions mismatch_args => {}, # Arguments that might need to be zero/sign-extended 121*c54f35caSApple OSS Distributions }, 122*c54f35caSApple OSS Distributions); 123*c54f35caSApple OSS Distributions 124*c54f35caSApple OSS Distributions# An explicit list of cancelable syscalls. For creating stubs that call the 125*c54f35caSApple OSS Distributions# cancellable version of cerror. 126*c54f35caSApple OSS Distributionsmy @Cancelable = qw/ 127*c54f35caSApple OSS Distributions accept access aio_suspend 128*c54f35caSApple OSS Distributions close connect connectx 129*c54f35caSApple OSS Distributions disconnectx 130*c54f35caSApple OSS Distributions faccessat fcntl fdatasync fpathconf fstat fstatat fsync 131*c54f35caSApple OSS Distributions getlogin 132*c54f35caSApple OSS Distributions ioctl 133*c54f35caSApple OSS Distributions link linkat lseek lstat 134*c54f35caSApple OSS Distributions msgrcv msgsnd msync 135*c54f35caSApple OSS Distributions open openat 136*c54f35caSApple OSS Distributions pathconf peeloff poll posix_spawn pread preadv pselect pwrite pwritev 137*c54f35caSApple OSS Distributions read readv recvfrom recvmsg rename renameat 138*c54f35caSApple OSS Distributions rename_ext 139*c54f35caSApple OSS Distributions __semwait_signal __sigwait 140*c54f35caSApple OSS Distributions select sem_wait semop sendmsg sendto sigsuspend stat symlink symlinkat sync 141*c54f35caSApple OSS Distributions unlink unlinkat 142*c54f35caSApple OSS Distributions wait4 waitid write writev 143*c54f35caSApple OSS Distributions/; 144*c54f35caSApple OSS Distributions 145*c54f35caSApple OSS Distributionssub usage { 146*c54f35caSApple OSS Distributions die "Usage: $MyName syscalls.master custom-directory platforms-directory platform-name out-directory\n"; 147*c54f35caSApple OSS Distributions} 148*c54f35caSApple OSS Distributions 149*c54f35caSApple OSS Distributions########################################################################## 150*c54f35caSApple OSS Distributions# Read the syscall.master file and collect the system call names and number 151*c54f35caSApple OSS Distributions# of arguments. It looks for the NO_SYSCALL_STUB quailifier following the 152*c54f35caSApple OSS Distributions# prototype to determine if no automatic stub should be created by Libsystem. 153*c54f35caSApple OSS Distributions# 154*c54f35caSApple OSS Distributions# The `sys_` prefix is stripped from syscall names, and is only kept for 155*c54f35caSApple OSS Distributions# the kernel symbol in order to avoid namespace clashes and identify 156*c54f35caSApple OSS Distributions# syscalls more easily. 157*c54f35caSApple OSS Distributions# 158*c54f35caSApple OSS Distributions# For the #if lines in syscall.master, all macros are assumed to be defined, 159*c54f35caSApple OSS Distributions# except COMPAT_GETFSSTAT (assumed undefined). 160*c54f35caSApple OSS Distributions########################################################################## 161*c54f35caSApple OSS Distributionssub readMaster { 162*c54f35caSApple OSS Distributions my $file = shift; 163*c54f35caSApple OSS Distributions local $_; 164*c54f35caSApple OSS Distributions my $f = IO::File->new($file, 'r'); 165*c54f35caSApple OSS Distributions die "$MyName: $file: $!\n" unless defined($f); 166*c54f35caSApple OSS Distributions my $line = 0; 167*c54f35caSApple OSS Distributions my $skip = 0; 168*c54f35caSApple OSS Distributions my $allow_missing = 0; 169*c54f35caSApple OSS Distributions while(<$f>) { 170*c54f35caSApple OSS Distributions $line++; 171*c54f35caSApple OSS Distributions if(/^#\s*endif/) { 172*c54f35caSApple OSS Distributions $skip = 0; 173*c54f35caSApple OSS Distributions $allow_missing = 0; 174*c54f35caSApple OSS Distributions next; 175*c54f35caSApple OSS Distributions } 176*c54f35caSApple OSS Distributions if(/^#\s*else/) { 177*c54f35caSApple OSS Distributions $skip = -$skip; 178*c54f35caSApple OSS Distributions $allow_missing = 0; 179*c54f35caSApple OSS Distributions next; 180*c54f35caSApple OSS Distributions } 181*c54f35caSApple OSS Distributions chomp; 182*c54f35caSApple OSS Distributions if(/^#\s*ifndef\s+(RC_HIDE\S+)$/) { 183*c54f35caSApple OSS Distributions $skip = 1; 184*c54f35caSApple OSS Distributions $allow_missing = 1; 185*c54f35caSApple OSS Distributions } 186*c54f35caSApple OSS Distributions if(/^#\s*if\s+(\S+)$/) { 187*c54f35caSApple OSS Distributions $skip = ($1 eq 'COMPAT_GETFSSTAT') ? -1 : 1; 188*c54f35caSApple OSS Distributions next; 189*c54f35caSApple OSS Distributions } 190*c54f35caSApple OSS Distributions next if $skip < 0; 191*c54f35caSApple OSS Distributions next unless /^\d/; 192*c54f35caSApple OSS Distributions s/^[^{]*{\s*//; 193*c54f35caSApple OSS Distributions s/\s*}.*$//; # } 194*c54f35caSApple OSS Distributions die "$MyName: no function prototype on line $line\n" unless length($_) > 0 && /;$/; 195*c54f35caSApple OSS Distributions my $no_syscall_stub = /\)\s*NO_SYSCALL_STUB\s*;/; 196*c54f35caSApple OSS Distributions my($name, $args) = /\s(\S+)\s*\(([^)]*)\)/; 197*c54f35caSApple OSS Distributions next if $name =~ /e?nosys/; 198*c54f35caSApple OSS Distributions $name =~ s/^sys_//; 199*c54f35caSApple OSS Distributions $args =~ s/^\s+//; 200*c54f35caSApple OSS Distributions $args =~ s/\s+$//; 201*c54f35caSApple OSS Distributions my $argbytes = 0; 202*c54f35caSApple OSS Distributions my $nargs = 0; 203*c54f35caSApple OSS Distributions my %mismatch_args; 204*c54f35caSApple OSS Distributions if($args ne '' && $args ne 'void') { 205*c54f35caSApple OSS Distributions my @a = split(',', $args); 206*c54f35caSApple OSS Distributions $nargs = scalar(@a); 207*c54f35caSApple OSS Distributions my $index = 0; 208*c54f35caSApple OSS Distributions for my $type (@a) { 209*c54f35caSApple OSS Distributions $type =~ s/\s*\w+$//; # remove the argument name 210*c54f35caSApple OSS Distributions 211*c54f35caSApple OSS Distributions # Calculate the size of all the arguments (only used for i386) 212*c54f35caSApple OSS Distributions if($type =~ /\*$/) { 213*c54f35caSApple OSS Distributions $argbytes += 4; # a pointer type 214*c54f35caSApple OSS Distributions } else { 215*c54f35caSApple OSS Distributions $type =~ s/^.*\s//; # remove any type qualifier, like unsigned 216*c54f35caSApple OSS Distributions my $b = $TypeBytes{$type}; 217*c54f35caSApple OSS Distributions die "$MyName: $name: unknown type '$type'\n" unless defined($b); 218*c54f35caSApple OSS Distributions $argbytes += $b; 219*c54f35caSApple OSS Distributions } 220*c54f35caSApple OSS Distributions # Determine which arguments might need to be zero/sign-extended 221*c54f35caSApple OSS Distributions if(exists $UserKernelMismatchTypes{$type}) { 222*c54f35caSApple OSS Distributions $mismatch_args{$index} = $UserKernelMismatchTypes{$type}; 223*c54f35caSApple OSS Distributions } 224*c54f35caSApple OSS Distributions 225*c54f35caSApple OSS Distributions $index++; 226*c54f35caSApple OSS Distributions } 227*c54f35caSApple OSS Distributions } 228*c54f35caSApple OSS Distributions $Symbols{$name} = { 229*c54f35caSApple OSS Distributions c_sym => $name, 230*c54f35caSApple OSS Distributions syscall => $name, 231*c54f35caSApple OSS Distributions asm_sym => $no_syscall_stub ? "___$name" : "_$name", 232*c54f35caSApple OSS Distributions is_private => $no_syscall_stub, 233*c54f35caSApple OSS Distributions is_custom => undef, 234*c54f35caSApple OSS Distributions nargs => $nargs, 235*c54f35caSApple OSS Distributions bytes => $argbytes, 236*c54f35caSApple OSS Distributions aliases => {}, 237*c54f35caSApple OSS Distributions mismatch_args => \%mismatch_args, # Arguments that might need to be zero/sign-extended 238*c54f35caSApple OSS Distributions except => [], 239*c54f35caSApple OSS Distributions allow_missing => $allow_missing, 240*c54f35caSApple OSS Distributions }; 241*c54f35caSApple OSS Distributions } 242*c54f35caSApple OSS Distributions} 243*c54f35caSApple OSS Distributions 244*c54f35caSApple OSS Distributionssub checkForCustomStubs { 245*c54f35caSApple OSS Distributions my ($dir) = @_; 246*c54f35caSApple OSS Distributions 247*c54f35caSApple OSS Distributions my ($c_sym_name, $sym); 248*c54f35caSApple OSS Distributions while (($c_sym_name, $sym) = each %Symbols) { 249*c54f35caSApple OSS Distributions my $source = "__".$$sym{c_sym}.".s"; 250*c54f35caSApple OSS Distributions my $custom = File::Spec->join($dir, $source); 251*c54f35caSApple OSS Distributions next unless -f $custom; 252*c54f35caSApple OSS Distributions 253*c54f35caSApple OSS Distributions $$sym{is_custom} = $source; 254*c54f35caSApple OSS Distributions if (!$$sym{is_private}) { 255*c54f35caSApple OSS Distributions foreach my $subarch (@Architectures) { 256*c54f35caSApple OSS Distributions (my $arch = $subarch) =~ s/arm(v.*)/arm/; 257*c54f35caSApple OSS Distributions $arch =~ s/x86_64(.*)/x86_64/; 258*c54f35caSApple OSS Distributions $arch =~ s/arm64(.*)/arm64/; 259*c54f35caSApple OSS Distributions $$sym{aliases}{$arch} = [] unless $$sym{aliases}{$arch}; 260*c54f35caSApple OSS Distributions push(@{$$sym{aliases}{$arch}}, $$sym{asm_sym}); 261*c54f35caSApple OSS Distributions } 262*c54f35caSApple OSS Distributions $$sym{asm_sym} = "__".$$sym{asm_sym}; 263*c54f35caSApple OSS Distributions $$sym{is_private} = 1; 264*c54f35caSApple OSS Distributions } 265*c54f35caSApple OSS Distributions } 266*c54f35caSApple OSS Distributions} 267*c54f35caSApple OSS Distributions 268*c54f35caSApple OSS Distributionssub readAliases { 269*c54f35caSApple OSS Distributions my ($platformDir, $platformName) = @_; 270*c54f35caSApple OSS Distributions my $genericMap = File::Spec->join($platformDir, "syscall.map"); 271*c54f35caSApple OSS Distributions 272*c54f35caSApple OSS Distributions my %sym_to_c; 273*c54f35caSApple OSS Distributions foreach my $k (keys %Symbols) { 274*c54f35caSApple OSS Distributions $sym_to_c{$Symbols{$k}{asm_sym}} = $k; 275*c54f35caSApple OSS Distributions } 276*c54f35caSApple OSS Distributions 277*c54f35caSApple OSS Distributions my @a = (); 278*c54f35caSApple OSS Distributions for my $arch (@Architectures) { 279*c54f35caSApple OSS Distributions (my $new_arch = $arch) =~ s/arm(v.*)/arm/g; 280*c54f35caSApple OSS Distributions $new_arch =~ s/x86_64(.*)/x86_64/g; 281*c54f35caSApple OSS Distributions $new_arch =~ s/arm64(.*)/arm64/g; 282*c54f35caSApple OSS Distributions push(@a, $new_arch) unless grep { $_ eq $new_arch } @a; 283*c54f35caSApple OSS Distributions } 284*c54f35caSApple OSS Distributions 285*c54f35caSApple OSS Distributions foreach my $arch (@a) { 286*c54f35caSApple OSS Distributions my $syscallFile = File::Spec->join($platformDir, $platformName, $arch, "syscall.map"); 287*c54f35caSApple OSS Distributions 288*c54f35caSApple OSS Distributions my @files = (); 289*c54f35caSApple OSS Distributions push(@files, IO::File->new($syscallFile, 'r')); 290*c54f35caSApple OSS Distributions die "$MyName: $syscallFile: $!\n" unless defined($files[$#files]); 291*c54f35caSApple OSS Distributions push(@files, IO::File->new($genericMap, 'r')); 292*c54f35caSApple OSS Distributions die "$MyName: $genericMap: $!\n" unless defined($files[$#files]); 293*c54f35caSApple OSS Distributions 294*c54f35caSApple OSS Distributions foreach my $f (@files) { 295*c54f35caSApple OSS Distributions while (<$f>) { 296*c54f35caSApple OSS Distributions next if /^#/; 297*c54f35caSApple OSS Distributions chomp; 298*c54f35caSApple OSS Distributions 299*c54f35caSApple OSS Distributions my ($alias, $target_symbol) = split; 300*c54f35caSApple OSS Distributions if (defined($target_symbol)) { 301*c54f35caSApple OSS Distributions foreach my $sym (values %Symbols) { 302*c54f35caSApple OSS Distributions # I've eliminated most of the ugly from this script except 303*c54f35caSApple OSS Distributions # the need to try stripping underbars here. 304*c54f35caSApple OSS Distributions if ($$sym{is_private}) { 305*c54f35caSApple OSS Distributions next unless $$sym{asm_sym} eq $target_symbol; 306*c54f35caSApple OSS Distributions } else { 307*c54f35caSApple OSS Distributions (my $target = $target_symbol) =~ s/^__//; 308*c54f35caSApple OSS Distributions next unless ($$sym{asm_sym} eq $target || $$sym{asm_sym} eq $target_symbol); 309*c54f35caSApple OSS Distributions } 310*c54f35caSApple OSS Distributions $$sym{aliases}{$arch} = [] unless $$sym{aliases}{$arch}; 311*c54f35caSApple OSS Distributions 312*c54f35caSApple OSS Distributions die "$MyName: $arch $$sym{asm_sym} -> $alias: Duplicate alias.\n" if grep { $_ eq $alias } @{$$sym{aliases}{$arch}}; 313*c54f35caSApple OSS Distributions push(@{$$sym{aliases}{$arch}}, $alias); 314*c54f35caSApple OSS Distributions 315*c54f35caSApple OSS Distributions # last thing to do, if we aliased over a first class symbol, we need 316*c54f35caSApple OSS Distributions # to mark it 317*c54f35caSApple OSS Distributions my $c = $sym_to_c{$alias}; 318*c54f35caSApple OSS Distributions if ($Symbols{$c}) { 319*c54f35caSApple OSS Distributions push(@{$Symbols{$c}{except}}, $arch); 320*c54f35caSApple OSS Distributions } 321*c54f35caSApple OSS Distributions } 322*c54f35caSApple OSS Distributions } 323*c54f35caSApple OSS Distributions } 324*c54f35caSApple OSS Distributions } 325*c54f35caSApple OSS Distributions } 326*c54f35caSApple OSS Distributions} 327*c54f35caSApple OSS Distributions 328*c54f35caSApple OSS Distributions########################################################################## 329*c54f35caSApple OSS Distributions# Make a __xxx.s file: if it exists in the $CustomDir, just copy it, otherwise 330*c54f35caSApple OSS Distributions# create one. We define the macro __SYSCALL_32BIT_ARG_BYTES so that SYS.h could 331*c54f35caSApple OSS Distributions# use that to define __SYSCALL dependent on the arguments' total size. 332*c54f35caSApple OSS Distributions########################################################################## 333*c54f35caSApple OSS Distributionssub writeStubForSymbol { 334*c54f35caSApple OSS Distributions my ($f, $symbol) = @_; 335*c54f35caSApple OSS Distributions 336*c54f35caSApple OSS Distributions my @conditions; 337*c54f35caSApple OSS Distributions my $has_arm64 = 0; 338*c54f35caSApple OSS Distributions for my $subarch (@Architectures) { 339*c54f35caSApple OSS Distributions (my $arch = $subarch) =~ s/arm(v.*)/arm/; 340*c54f35caSApple OSS Distributions $arch =~ s/x86_64(.*)/x86_64/; 341*c54f35caSApple OSS Distributions $arch =~ s/arm64(.*)/arm64/; 342*c54f35caSApple OSS Distributions push(@conditions, "defined(__${arch}__)") unless grep { $_ eq $arch } @{$$symbol{except}}; 343*c54f35caSApple OSS Distributions 344*c54f35caSApple OSS Distributions if($arch eq "arm64") { 345*c54f35caSApple OSS Distributions $has_arm64 = 1 unless grep { $_ eq $arch } @{$$symbol{except}}; 346*c54f35caSApple OSS Distributions } 347*c54f35caSApple OSS Distributions } 348*c54f35caSApple OSS Distributions 349*c54f35caSApple OSS Distributions my %is_cancel; 350*c54f35caSApple OSS Distributions for (@Cancelable) { $is_cancel{$_} = 1 }; 351*c54f35caSApple OSS Distributions 352*c54f35caSApple OSS Distributions print $f "#define __SYSCALL_32BIT_ARG_BYTES $$symbol{bytes}\n"; 353*c54f35caSApple OSS Distributions print $f "#include \"SYS.h\"\n\n"; 354*c54f35caSApple OSS Distributions if ($$symbol{allow_missing}) { 355*c54f35caSApple OSS Distributions printf $f "#ifdef SYS_%s\n", $$symbol{syscall}; 356*c54f35caSApple OSS Distributions } 357*c54f35caSApple OSS Distributions 358*c54f35caSApple OSS Distributions if (scalar(@conditions)) { 359*c54f35caSApple OSS Distributions printf $f "#ifndef SYS_%s\n", $$symbol{syscall}; 360*c54f35caSApple OSS Distributions printf $f "#error \"SYS_%s not defined. The header files libsyscall is building against do not match syscalls.master.\"\n", $$symbol{syscall}; 361*c54f35caSApple OSS Distributions printf $f "#endif\n\n"; 362*c54f35caSApple OSS Distributions } 363*c54f35caSApple OSS Distributions 364*c54f35caSApple OSS Distributions my $nc = ($is_cancel{$$symbol{syscall}} ? "cerror" : "cerror_nocancel"); 365*c54f35caSApple OSS Distributions 366*c54f35caSApple OSS Distributions if($has_arm64) { 367*c54f35caSApple OSS Distributions printf $f "#if defined(__arm64__)\n"; 368*c54f35caSApple OSS Distributions printf $f "MI_ENTRY_POINT(%s)\n", $$symbol{asm_sym}; 369*c54f35caSApple OSS Distributions if(keys %{$$symbol{mismatch_args}}) { 370*c54f35caSApple OSS Distributions while(my($argnum, $extend) = each %{$$symbol{mismatch_args}}) { 371*c54f35caSApple OSS Distributions printf $f "%s(%d)\n", $extend, $argnum; 372*c54f35caSApple OSS Distributions } 373*c54f35caSApple OSS Distributions } 374*c54f35caSApple OSS Distributions 375*c54f35caSApple OSS Distributions printf $f "SYSCALL_NONAME(%s, %d, %s)\n", $$symbol{syscall}, $$symbol{nargs}, $nc; 376*c54f35caSApple OSS Distributions printf $f "ret\n"; 377*c54f35caSApple OSS Distributions printf $f "#else\n"; 378*c54f35caSApple OSS Distributions } 379*c54f35caSApple OSS Distributions 380*c54f35caSApple OSS Distributions if (scalar(@conditions)) { 381*c54f35caSApple OSS Distributions printf $f "#if " . join(" || ", @conditions) . "\n"; 382*c54f35caSApple OSS Distributions printf $f "__SYSCALL2(%s, %s, %d, %s)\n", $$symbol{asm_sym}, $$symbol{syscall}, $$symbol{nargs}, $nc; 383*c54f35caSApple OSS Distributions if (!$$symbol{is_private} && (scalar(@conditions) < scalar(@Architectures))) { 384*c54f35caSApple OSS Distributions printf $f "#else\n"; 385*c54f35caSApple OSS Distributions printf $f "__SYSCALL2(%s, %s, %d, %s)\n", "__".$$symbol{asm_sym}, $$symbol{syscall}, $$symbol{nargs}, $nc; 386*c54f35caSApple OSS Distributions } 387*c54f35caSApple OSS Distributions printf $f "#endif\n\n"; 388*c54f35caSApple OSS Distributions } else { 389*c54f35caSApple OSS Distributions # actually this isnt an inconsistency. kernel can expose what it wants but if all our arches 390*c54f35caSApple OSS Distributions # override it we need to honour that. 391*c54f35caSApple OSS Distributions } 392*c54f35caSApple OSS Distributions 393*c54f35caSApple OSS Distributions if ($$symbol{allow_missing}) { 394*c54f35caSApple OSS Distributions printf $f "#endif\n"; 395*c54f35caSApple OSS Distributions } 396*c54f35caSApple OSS Distributions 397*c54f35caSApple OSS Distributions if($has_arm64) { 398*c54f35caSApple OSS Distributions printf $f "#endif\n\n"; 399*c54f35caSApple OSS Distributions } 400*c54f35caSApple OSS Distributions} 401*c54f35caSApple OSS Distributions 402*c54f35caSApple OSS Distributionssub writeAliasesForSymbol { 403*c54f35caSApple OSS Distributions my ($f, $symbol) = @_; 404*c54f35caSApple OSS Distributions 405*c54f35caSApple OSS Distributions if ($$symbol{allow_missing}) { 406*c54f35caSApple OSS Distributions printf $f "#ifdef SYS_%s\n", $$symbol{syscall}; 407*c54f35caSApple OSS Distributions } 408*c54f35caSApple OSS Distributions 409*c54f35caSApple OSS Distributions foreach my $subarch (@Architectures) { 410*c54f35caSApple OSS Distributions (my $arch = $subarch) =~ s/arm(v.*)/arm/; 411*c54f35caSApple OSS Distributions $arch =~ s/x86_64(.*)/x86_64/; 412*c54f35caSApple OSS Distributions $arch =~ s/arm64(.*)/arm64/; 413*c54f35caSApple OSS Distributions 414*c54f35caSApple OSS Distributions next unless scalar($$symbol{aliases}{$arch}); 415*c54f35caSApple OSS Distributions 416*c54f35caSApple OSS Distributions printf $f "#if defined(__${arch}__)\n"; 417*c54f35caSApple OSS Distributions foreach my $alias_sym (@{$$symbol{aliases}{$arch}}) { 418*c54f35caSApple OSS Distributions my $sym = (grep { $_ eq $arch } @{$$symbol{except}}) ? "__".$$symbol{asm_sym} : $$symbol{asm_sym}; 419*c54f35caSApple OSS Distributions 420*c54f35caSApple OSS Distributions printf $f "\t.globl\t$alias_sym\n"; 421*c54f35caSApple OSS Distributions printf $f "\t.set\t$alias_sym, $sym\n"; 422*c54f35caSApple OSS Distributions } 423*c54f35caSApple OSS Distributions printf $f "#endif\n\n"; 424*c54f35caSApple OSS Distributions } 425*c54f35caSApple OSS Distributions if ($$symbol{allow_missing}) { 426*c54f35caSApple OSS Distributions printf $f "#endif\n"; 427*c54f35caSApple OSS Distributions } 428*c54f35caSApple OSS Distributions} 429*c54f35caSApple OSS Distributions 430*c54f35caSApple OSS Distributionsusage() unless scalar(@ARGV) == 5; 431*c54f35caSApple OSS Distributions$CustomDir = $ARGV[1]; 432*c54f35caSApple OSS Distributionsdie "$MyName: $CustomDir: No such directory\n" unless -d $CustomDir; 433*c54f35caSApple OSS Distributions$PlatformsDir = $ARGV[2]; 434*c54f35caSApple OSS Distributionsdie "$MyName: $PlatformsDir: No such directory\n" unless -d $PlatformsDir; 435*c54f35caSApple OSS Distributions$PlatformName = $ARGV[3]; 436*c54f35caSApple OSS Distributionsdie "$MyName: $PlatformsDir/$PlatformName: No such directory\n" unless -d "$PlatformsDir/$PlatformName"; 437*c54f35caSApple OSS Distributions$OutDir = $ARGV[4]; 438*c54f35caSApple OSS Distributionsdie "$MyName: $OutDir: No such directory\n" unless -d $OutDir; 439*c54f35caSApple OSS Distributions 440*c54f35caSApple OSS DistributionsreadMaster($ARGV[0]); 441*c54f35caSApple OSS DistributionscheckForCustomStubs($CustomDir); 442*c54f35caSApple OSS DistributionsreadAliases($PlatformsDir, $PlatformName); 443*c54f35caSApple OSS Distributions 444*c54f35caSApple OSS Distributions########################################################################## 445*c54f35caSApple OSS Distributions# copy the files specified in @Copy from the $CustomDir to $OutDir 446*c54f35caSApple OSS Distributions########################################################################## 447*c54f35caSApple OSS Distributionsfor(@Copy) { 448*c54f35caSApple OSS Distributions my $custom = File::Spec->join($CustomDir, $_); 449*c54f35caSApple OSS Distributions my $path = File::Spec->join($OutDir, $_); 450*c54f35caSApple OSS Distributions print "Copy $custom -> $path\n"; 451*c54f35caSApple OSS Distributions File::Copy::copy($custom, $path) || die "$MyName: copy($custom, $path): $!\n"; 452*c54f35caSApple OSS Distributions} 453*c54f35caSApple OSS Distributions 454*c54f35caSApple OSS Distributions########################################################################## 455*c54f35caSApple OSS Distributions# make all the *.s files 456*c54f35caSApple OSS Distributions########################################################################## 457*c54f35caSApple OSS Distributionsmy @src; 458*c54f35caSApple OSS Distributionsmy($k, $sym); 459*c54f35caSApple OSS Distributionswhile (($k, $sym) = each %Symbols) 460*c54f35caSApple OSS Distributions{ 461*c54f35caSApple OSS Distributions my $srcname = $$sym{asm_sym} . ".s"; 462*c54f35caSApple OSS Distributions my $outpath = File::Spec->join($OutDir, $srcname); 463*c54f35caSApple OSS Distributions 464*c54f35caSApple OSS Distributions if ($$sym{is_custom}) { 465*c54f35caSApple OSS Distributions my $custom = File::Spec->join($CustomDir, $$sym{is_custom}); 466*c54f35caSApple OSS Distributions File::Copy::copy($custom, $outpath); 467*c54f35caSApple OSS Distributions print "Copied $outpath\n"; 468*c54f35caSApple OSS Distributions 469*c54f35caSApple OSS Distributions print "Writing aliases for $srcname\n"; 470*c54f35caSApple OSS Distributions my $f = IO::File->new($outpath, 'a'); 471*c54f35caSApple OSS Distributions die "$MyName: $outpath: $!\n" unless defined($f); 472*c54f35caSApple OSS Distributions writeAliasesForSymbol($f, $sym); 473*c54f35caSApple OSS Distributions undef $f; 474*c54f35caSApple OSS Distributions } else { 475*c54f35caSApple OSS Distributions my $f = IO::File->new($outpath, 'w'); 476*c54f35caSApple OSS Distributions die "$MyName: $outpath: $!\n" unless defined($f); 477*c54f35caSApple OSS Distributions 478*c54f35caSApple OSS Distributions printf "Creating $outpath\n"; 479*c54f35caSApple OSS Distributions writeStubForSymbol($f, $sym); 480*c54f35caSApple OSS Distributions writeAliasesForSymbol($f, $sym); 481*c54f35caSApple OSS Distributions undef $f; 482*c54f35caSApple OSS Distributions } 483*c54f35caSApple OSS Distributions push(@src, $srcname); 484*c54f35caSApple OSS Distributions} 485*c54f35caSApple OSS Distributions 486*c54f35caSApple OSS Distributions########################################################################## 487*c54f35caSApple OSS Distributions# create the Makefile.inc file from the list for files in @src and @CustomSrc 488*c54f35caSApple OSS Distributions########################################################################## 489*c54f35caSApple OSS Distributionsmy $path = File::Spec->join($OutDir, 'stubs.list'); 490*c54f35caSApple OSS Distributionsmy $f = IO::File->new($path, 'w'); 491*c54f35caSApple OSS Distributionsmy @sources = sort(@src, @CustomSrc); 492*c54f35caSApple OSS Distributionsfor my $s (@sources) { 493*c54f35caSApple OSS Distributions printf $f File::Spec->join($OutDir, $s) . "\n"; 494*c54f35caSApple OSS Distributions} 495*c54f35caSApple OSS Distributionsundef $f; 496*c54f35caSApple OSS Distributionsundef $path; 497*c54f35caSApple OSS Distributions 498