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