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