1/* Copyright (c) 2005-2008, Google Inc. 2 * All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 * --- 31 * Author: Markus Gutschke 32 */ 33 34/* This file includes Linux-specific support functions common to the 35 * coredumper and the thread lister; primarily, this is a collection 36 * of direct system calls, and a couple of symbols missing from 37 * standard header files. 38 * There are a few options that the including file can set to control 39 * the behavior of this file: 40 * 41 * SYS_CPLUSPLUS: 42 * The entire header file will normally be wrapped in 'extern "C" { }", 43 * making it suitable for compilation as both C and C++ source. If you 44 * do not want to do this, you can set the SYS_CPLUSPLUS macro to inhibit 45 * the wrapping. N.B. doing so will suppress inclusion of all prerequisite 46 * system header files, too. It is the caller's responsibility to provide 47 * the necessary definitions. 48 * 49 * SYS_ERRNO: 50 * All system calls will update "errno" unless overriden by setting the 51 * SYS_ERRNO macro prior to including this file. SYS_ERRNO should be 52 * an l-value. 53 * 54 * SYS_INLINE: 55 * New symbols will be defined "static inline", unless overridden by 56 * the SYS_INLINE macro. 57 * 58 * SYS_LINUX_SYSCALL_SUPPORT_H 59 * This macro is used to avoid multiple inclusions of this header file. 60 * If you need to include this file more than once, make sure to 61 * unset SYS_LINUX_SYSCALL_SUPPORT_H before each inclusion. 62 * 63 * SYS_PREFIX: 64 * New system calls will have a prefix of "sys_" unless overridden by 65 * the SYS_PREFIX macro. Valid values for this macro are [0..9] which 66 * results in prefixes "sys[0..9]_". It is also possible to set this 67 * macro to -1, which avoids all prefixes. 68 * 69 * This file defines a few internal symbols that all start with "LSS_". 70 * Do not access these symbols from outside this file. They are not part 71 * of the supported API. 72 * 73 * NOTE: This is a stripped down version of the official opensource 74 * version of linux_syscall_support.h, which lives at 75 * http://code.google.com/p/linux-syscall-support/ 76 * It includes only the syscalls that are used in perftools, plus a 77 * few extra. Here's the breakdown: 78 * 1) Perftools uses these: grep -rho 'sys_[a-z0-9_A-Z]* *(' src | sort -u 79 * sys__exit( 80 * sys_clone( 81 * sys_close( 82 * sys_fcntl( 83 * sys_fstat( 84 * sys_futex( 85 * sys_futex1( 86 * sys_getcpu( 87 * sys_getdents( 88 * sys_getppid( 89 * sys_gettid( 90 * sys_lseek( 91 * sys_mmap( 92 * sys_mremap( 93 * sys_munmap( 94 * sys_open( 95 * sys_pipe( 96 * sys_prctl( 97 * sys_ptrace( 98 * sys_ptrace_detach( 99 * sys_read( 100 * sys_sched_yield( 101 * sys_sigaction( 102 * sys_sigaltstack( 103 * sys_sigdelset( 104 * sys_sigfillset( 105 * sys_sigprocmask( 106 * sys_socket( 107 * sys_stat( 108 * sys_waitpid( 109 * 2) These are used as subroutines of the above: 110 * sys_getpid -- gettid 111 * sys_kill -- ptrace_detach 112 * sys_restore -- sigaction 113 * sys_restore_rt -- sigaction 114 * sys_socketcall -- socket 115 * sys_wait4 -- waitpid 116 * 3) I left these in even though they're not used. They either 117 * complement the above (write vs read) or are variants (rt_sigaction): 118 * sys_fstat64 119 * sys_getdents64 120 * sys_llseek 121 * sys_mmap2 122 * sys_openat 123 * sys_rt_sigaction 124 * sys_rt_sigprocmask 125 * sys_sigaddset 126 * sys_sigemptyset 127 * sys_stat64 128 * sys_write 129 */ 130#ifndef SYS_LINUX_SYSCALL_SUPPORT_H 131#define SYS_LINUX_SYSCALL_SUPPORT_H 132 133/* We currently only support x86-32, x86-64, ARM, MIPS, and PPC on Linux. 134 * Porting to other related platforms should not be difficult. 135 */ 136#if (defined(__i386__) || defined(__x86_64__) || defined(__arm__) || \ 137 defined(__mips__) || defined(__PPC__)) && defined(__linux) 138 139#ifndef SYS_CPLUSPLUS 140#ifdef __cplusplus 141/* Some system header files in older versions of gcc neglect to properly 142 * handle being included from C++. As it appears to be harmless to have 143 * multiple nested 'extern "C"' blocks, just add another one here. 144 */ 145extern "C" { 146#endif 147 148#include <errno.h> 149#include <signal.h> 150#include <stdarg.h> 151#include <string.h> 152#include <sys/ptrace.h> 153#include <sys/resource.h> 154#include <sys/time.h> 155#include <sys/types.h> 156#include <syscall.h> 157#include <unistd.h> 158#include <linux/unistd.h> 159#include <endian.h> 160 161#ifdef __mips__ 162/* Include definitions of the ABI currently in use. */ 163#include <sgidefs.h> 164#endif 165 166#endif 167 168/* As glibc often provides subtly incompatible data structures (and implicit 169 * wrapper functions that convert them), we provide our own kernel data 170 * structures for use by the system calls. 171 * These structures have been developed by using Linux 2.6.23 headers for 172 * reference. Note though, we do not care about exact API compatibility 173 * with the kernel, and in fact the kernel often does not have a single 174 * API that works across architectures. Instead, we try to mimic the glibc 175 * API where reasonable, and only guarantee ABI compatibility with the 176 * kernel headers. 177 * Most notably, here are a few changes that were made to the structures 178 * defined by kernel headers: 179 * 180 * - we only define structures, but not symbolic names for kernel data 181 * types. For the latter, we directly use the native C datatype 182 * (i.e. "unsigned" instead of "mode_t"). 183 * - in a few cases, it is possible to define identical structures for 184 * both 32bit (e.g. i386) and 64bit (e.g. x86-64) platforms by 185 * standardizing on the 64bit version of the data types. In particular, 186 * this means that we use "unsigned" where the 32bit headers say 187 * "unsigned long". 188 * - overall, we try to minimize the number of cases where we need to 189 * conditionally define different structures. 190 * - the "struct kernel_sigaction" class of structures have been 191 * modified to more closely mimic glibc's API by introducing an 192 * anonymous union for the function pointer. 193 * - a small number of field names had to have an underscore appended to 194 * them, because glibc defines a global macro by the same name. 195 */ 196 197/* include/linux/dirent.h */ 198struct kernel_dirent64 { 199 unsigned long long d_ino; 200 long long d_off; 201 unsigned short d_reclen; 202 unsigned char d_type; 203 char d_name[256]; 204}; 205 206/* include/linux/dirent.h */ 207struct kernel_dirent { 208 long d_ino; 209 long d_off; 210 unsigned short d_reclen; 211 char d_name[256]; 212}; 213 214/* include/linux/time.h */ 215struct kernel_timespec { 216 long tv_sec; 217 long tv_nsec; 218}; 219 220/* include/linux/time.h */ 221struct kernel_timeval { 222 long tv_sec; 223 long tv_usec; 224}; 225 226/* include/linux/resource.h */ 227struct kernel_rusage { 228 struct kernel_timeval ru_utime; 229 struct kernel_timeval ru_stime; 230 long ru_maxrss; 231 long ru_ixrss; 232 long ru_idrss; 233 long ru_isrss; 234 long ru_minflt; 235 long ru_majflt; 236 long ru_nswap; 237 long ru_inblock; 238 long ru_oublock; 239 long ru_msgsnd; 240 long ru_msgrcv; 241 long ru_nsignals; 242 long ru_nvcsw; 243 long ru_nivcsw; 244}; 245 246#if defined(__i386__) || defined(__arm__) || defined(__PPC__) 247 248/* include/asm-{arm,i386,mips,ppc}/signal.h */ 249struct kernel_old_sigaction { 250 union { 251 void (*sa_handler_)(int); 252 void (*sa_sigaction_)(int, siginfo_t *, void *); 253 }; 254 unsigned long sa_mask; 255 unsigned long sa_flags; 256 void (*sa_restorer)(void); 257} __attribute__((packed,aligned(4))); 258#elif (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) 259 #define kernel_old_sigaction kernel_sigaction 260#endif 261 262/* Some kernel functions (e.g. sigaction() in 2.6.23) require that the 263 * exactly match the size of the signal set, even though the API was 264 * intended to be extensible. We define our own KERNEL_NSIG to deal with 265 * this. 266 * Please note that glibc provides signals [1.._NSIG-1], whereas the 267 * kernel (and this header) provides the range [1..KERNEL_NSIG]. The 268 * actual number of signals is obviously the same, but the constants 269 * differ by one. 270 */ 271#ifdef __mips__ 272#define KERNEL_NSIG 128 273#else 274#define KERNEL_NSIG 64 275#endif 276 277/* include/asm-{arm,i386,mips,x86_64}/signal.h */ 278struct kernel_sigset_t { 279 unsigned long sig[(KERNEL_NSIG + 8*sizeof(unsigned long) - 1)/ 280 (8*sizeof(unsigned long))]; 281}; 282 283/* include/asm-{arm,i386,mips,x86_64,ppc}/signal.h */ 284struct kernel_sigaction { 285#ifdef __mips__ 286 unsigned long sa_flags; 287 union { 288 void (*sa_handler_)(int); 289 void (*sa_sigaction_)(int, siginfo_t *, void *); 290 }; 291 struct kernel_sigset_t sa_mask; 292#else 293 union { 294 void (*sa_handler_)(int); 295 void (*sa_sigaction_)(int, siginfo_t *, void *); 296 }; 297 unsigned long sa_flags; 298 void (*sa_restorer)(void); 299 struct kernel_sigset_t sa_mask; 300#endif 301}; 302 303/* include/asm-{arm,i386,mips,ppc}/stat.h */ 304#ifdef __mips__ 305#if _MIPS_SIM == _MIPS_SIM_ABI64 306struct kernel_stat { 307#else 308struct kernel_stat64 { 309#endif 310 unsigned st_dev; 311 unsigned __pad0[3]; 312 unsigned long long st_ino; 313 unsigned st_mode; 314 unsigned st_nlink; 315 unsigned st_uid; 316 unsigned st_gid; 317 unsigned st_rdev; 318 unsigned __pad1[3]; 319 long long st_size; 320 unsigned st_atime_; 321 unsigned st_atime_nsec_; 322 unsigned st_mtime_; 323 unsigned st_mtime_nsec_; 324 unsigned st_ctime_; 325 unsigned st_ctime_nsec_; 326 unsigned st_blksize; 327 unsigned __pad2; 328 unsigned long long st_blocks; 329}; 330#elif defined __PPC__ 331struct kernel_stat64 { 332 unsigned long long st_dev; 333 unsigned long long st_ino; 334 unsigned st_mode; 335 unsigned st_nlink; 336 unsigned st_uid; 337 unsigned st_gid; 338 unsigned long long st_rdev; 339 unsigned short int __pad2; 340 long long st_size; 341 long st_blksize; 342 long long st_blocks; 343 long st_atime_; 344 unsigned long st_atime_nsec_; 345 long st_mtime_; 346 unsigned long st_mtime_nsec_; 347 long st_ctime_; 348 unsigned long st_ctime_nsec_; 349 unsigned long __unused4; 350 unsigned long __unused5; 351}; 352#else 353struct kernel_stat64 { 354 unsigned long long st_dev; 355 unsigned char __pad0[4]; 356 unsigned __st_ino; 357 unsigned st_mode; 358 unsigned st_nlink; 359 unsigned st_uid; 360 unsigned st_gid; 361 unsigned long long st_rdev; 362 unsigned char __pad3[4]; 363 long long st_size; 364 unsigned st_blksize; 365 unsigned long long st_blocks; 366 unsigned st_atime_; 367 unsigned st_atime_nsec_; 368 unsigned st_mtime_; 369 unsigned st_mtime_nsec_; 370 unsigned st_ctime_; 371 unsigned st_ctime_nsec_; 372 unsigned long long st_ino; 373}; 374#endif 375 376/* include/asm-{arm,i386,mips,x86_64,ppc}/stat.h */ 377#if defined(__i386__) || defined(__arm__) 378struct kernel_stat { 379 /* The kernel headers suggest that st_dev and st_rdev should be 32bit 380 * quantities encoding 12bit major and 20bit minor numbers in an interleaved 381 * format. In reality, we do not see useful data in the top bits. So, 382 * we'll leave the padding in here, until we find a better solution. 383 */ 384 unsigned short st_dev; 385 short pad1; 386 unsigned st_ino; 387 unsigned short st_mode; 388 unsigned short st_nlink; 389 unsigned short st_uid; 390 unsigned short st_gid; 391 unsigned short st_rdev; 392 short pad2; 393 unsigned st_size; 394 unsigned st_blksize; 395 unsigned st_blocks; 396 unsigned st_atime_; 397 unsigned st_atime_nsec_; 398 unsigned st_mtime_; 399 unsigned st_mtime_nsec_; 400 unsigned st_ctime_; 401 unsigned st_ctime_nsec_; 402 unsigned __unused4; 403 unsigned __unused5; 404}; 405#elif defined(__x86_64__) 406struct kernel_stat { 407 unsigned long st_dev; 408 unsigned long st_ino; 409 unsigned long st_nlink; 410 unsigned st_mode; 411 unsigned st_uid; 412 unsigned st_gid; 413 unsigned __pad0; 414 unsigned long st_rdev; 415 long st_size; 416 long st_blksize; 417 long st_blocks; 418 unsigned long st_atime_; 419 unsigned long st_atime_nsec_; 420 unsigned long st_mtime_; 421 unsigned long st_mtime_nsec_; 422 unsigned long st_ctime_; 423 unsigned long st_ctime_nsec_; 424 long __unused[3]; 425}; 426#elif defined(__PPC__) 427struct kernel_stat { 428 unsigned st_dev; 429 unsigned long st_ino; // ino_t 430 unsigned long st_mode; // mode_t 431 unsigned short st_nlink; // nlink_t 432 unsigned st_uid; // uid_t 433 unsigned st_gid; // gid_t 434 unsigned st_rdev; 435 long st_size; // off_t 436 unsigned long st_blksize; 437 unsigned long st_blocks; 438 unsigned long st_atime_; 439 unsigned long st_atime_nsec_; 440 unsigned long st_mtime_; 441 unsigned long st_mtime_nsec_; 442 unsigned long st_ctime_; 443 unsigned long st_ctime_nsec_; 444 unsigned long __unused4; 445 unsigned long __unused5; 446}; 447#elif (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64) 448struct kernel_stat { 449 unsigned st_dev; 450 int st_pad1[3]; 451 unsigned st_ino; 452 unsigned st_mode; 453 unsigned st_nlink; 454 unsigned st_uid; 455 unsigned st_gid; 456 unsigned st_rdev; 457 int st_pad2[2]; 458 long st_size; 459 int st_pad3; 460 long st_atime_; 461 long st_atime_nsec_; 462 long st_mtime_; 463 long st_mtime_nsec_; 464 long st_ctime_; 465 long st_ctime_nsec_; 466 int st_blksize; 467 int st_blocks; 468 int st_pad4[14]; 469}; 470#endif 471 472 473/* Definitions missing from the standard header files */ 474#ifndef O_DIRECTORY 475#if defined(__arm__) 476#define O_DIRECTORY 0040000 477#else 478#define O_DIRECTORY 0200000 479#endif 480#endif 481#ifndef PR_GET_DUMPABLE 482#define PR_GET_DUMPABLE 3 483#endif 484#ifndef PR_SET_DUMPABLE 485#define PR_SET_DUMPABLE 4 486#endif 487#ifndef AT_FDCWD 488#define AT_FDCWD (-100) 489#endif 490#ifndef AT_SYMLINK_NOFOLLOW 491#define AT_SYMLINK_NOFOLLOW 0x100 492#endif 493#ifndef AT_REMOVEDIR 494#define AT_REMOVEDIR 0x200 495#endif 496#ifndef MREMAP_FIXED 497#define MREMAP_FIXED 2 498#endif 499#ifndef SA_RESTORER 500#define SA_RESTORER 0x04000000 501#endif 502 503#if defined(__i386__) 504#ifndef __NR_rt_sigaction 505#define __NR_rt_sigaction 174 506#define __NR_rt_sigprocmask 175 507#endif 508#ifndef __NR_stat64 509#define __NR_stat64 195 510#endif 511#ifndef __NR_fstat64 512#define __NR_fstat64 197 513#endif 514#ifndef __NR_getdents64 515#define __NR_getdents64 220 516#endif 517#ifndef __NR_gettid 518#define __NR_gettid 224 519#endif 520#ifndef __NR_futex 521#define __NR_futex 240 522#endif 523#ifndef __NR_openat 524#define __NR_openat 295 525#endif 526#ifndef __NR_getcpu 527#define __NR_getcpu 318 528#endif 529/* End of i386 definitions */ 530#elif defined(__arm__) 531#ifndef __syscall 532#if defined(__thumb__) || defined(__ARM_EABI__) 533#define __SYS_REG(name) register long __sysreg __asm__("r6") = __NR_##name; 534#define __SYS_REG_LIST(regs...) [sysreg] "r" (__sysreg) , ##regs 535#define __syscall(name) "swi\t0" 536#define __syscall_safe(name) \ 537 "push {r7}\n" \ 538 "mov r7,%[sysreg]\n" \ 539 __syscall(name)"\n" \ 540 "pop {r7}" 541#else 542#define __SYS_REG(name) 543#define __SYS_REG_LIST(regs...) regs 544#define __syscall(name) "swi\t" __sys1(__NR_##name) "" 545#define __syscall_safe(name) __syscall(name) 546#endif 547#endif 548#ifndef __NR_rt_sigaction 549#define __NR_rt_sigaction (__NR_SYSCALL_BASE + 174) 550#define __NR_rt_sigprocmask (__NR_SYSCALL_BASE + 175) 551#endif 552#ifndef __NR_stat64 553#define __NR_stat64 (__NR_SYSCALL_BASE + 195) 554#endif 555#ifndef __NR_fstat64 556#define __NR_fstat64 (__NR_SYSCALL_BASE + 197) 557#endif 558#ifndef __NR_getdents64 559#define __NR_getdents64 (__NR_SYSCALL_BASE + 217) 560#endif 561#ifndef __NR_gettid 562#define __NR_gettid (__NR_SYSCALL_BASE + 224) 563#endif 564#ifndef __NR_futex 565#define __NR_futex (__NR_SYSCALL_BASE + 240) 566#endif 567/* End of ARM definitions */ 568#elif defined(__x86_64__) 569#ifndef __NR_gettid 570#define __NR_gettid 186 571#endif 572#ifndef __NR_futex 573#define __NR_futex 202 574#endif 575#ifndef __NR_getdents64 576#define __NR_getdents64 217 577#endif 578#ifndef __NR_openat 579#define __NR_openat 257 580#endif 581/* End of x86-64 definitions */ 582#elif defined(__mips__) 583#if _MIPS_SIM == _MIPS_SIM_ABI32 584#ifndef __NR_rt_sigaction 585#define __NR_rt_sigaction (__NR_Linux + 194) 586#define __NR_rt_sigprocmask (__NR_Linux + 195) 587#endif 588#ifndef __NR_stat64 589#define __NR_stat64 (__NR_Linux + 213) 590#endif 591#ifndef __NR_fstat64 592#define __NR_fstat64 (__NR_Linux + 215) 593#endif 594#ifndef __NR_getdents64 595#define __NR_getdents64 (__NR_Linux + 219) 596#endif 597#ifndef __NR_gettid 598#define __NR_gettid (__NR_Linux + 222) 599#endif 600#ifndef __NR_futex 601#define __NR_futex (__NR_Linux + 238) 602#endif 603#ifndef __NR_openat 604#define __NR_openat (__NR_Linux + 288) 605#endif 606#ifndef __NR_fstatat 607#define __NR_fstatat (__NR_Linux + 293) 608#endif 609#ifndef __NR_getcpu 610#define __NR_getcpu (__NR_Linux + 312) 611#endif 612/* End of MIPS (old 32bit API) definitions */ 613#elif _MIPS_SIM == _MIPS_SIM_ABI64 614#ifndef __NR_gettid 615#define __NR_gettid (__NR_Linux + 178) 616#endif 617#ifndef __NR_futex 618#define __NR_futex (__NR_Linux + 194) 619#endif 620#ifndef __NR_openat 621#define __NR_openat (__NR_Linux + 247) 622#endif 623#ifndef __NR_fstatat 624#define __NR_fstatat (__NR_Linux + 252) 625#endif 626#ifndef __NR_getcpu 627#define __NR_getcpu (__NR_Linux + 271) 628#endif 629/* End of MIPS (64bit API) definitions */ 630#else 631#ifndef __NR_gettid 632#define __NR_gettid (__NR_Linux + 178) 633#endif 634#ifndef __NR_futex 635#define __NR_futex (__NR_Linux + 194) 636#endif 637#ifndef __NR_openat 638#define __NR_openat (__NR_Linux + 251) 639#endif 640#ifndef __NR_fstatat 641#define __NR_fstatat (__NR_Linux + 256) 642#endif 643#ifndef __NR_getcpu 644#define __NR_getcpu (__NR_Linux + 275) 645#endif 646/* End of MIPS (new 32bit API) definitions */ 647#endif 648/* End of MIPS definitions */ 649#elif defined(__PPC__) 650#ifndef __NR_rt_sigaction 651#define __NR_rt_sigaction 173 652#define __NR_rt_sigprocmask 174 653#endif 654#ifndef __NR_stat64 655#define __NR_stat64 195 656#endif 657#ifndef __NR_fstat64 658#define __NR_fstat64 197 659#endif 660#ifndef __NR_getdents64 661#define __NR_getdents64 202 662#endif 663#ifndef __NR_gettid 664#define __NR_gettid 207 665#endif 666#ifndef __NR_futex 667#define __NR_futex 221 668#endif 669#ifndef __NR_openat 670#define __NR_openat 286 671#endif 672#ifndef __NR_getcpu 673#define __NR_getcpu 302 674#endif 675/* End of powerpc defininitions */ 676#endif 677 678 679/* After forking, we must make sure to only call system calls. */ 680#if __BOUNDED_POINTERS__ 681 #error "Need to port invocations of syscalls for bounded ptrs" 682#else 683 /* The core dumper and the thread lister get executed after threads 684 * have been suspended. As a consequence, we cannot call any functions 685 * that acquire locks. Unfortunately, libc wraps most system calls 686 * (e.g. in order to implement pthread_atfork, and to make calls 687 * cancellable), which means we cannot call these functions. Instead, 688 * we have to call syscall() directly. 689 */ 690 #undef LSS_ERRNO 691 #ifdef SYS_ERRNO 692 /* Allow the including file to override the location of errno. This can 693 * be useful when using clone() with the CLONE_VM option. 694 */ 695 #define LSS_ERRNO SYS_ERRNO 696 #else 697 #define LSS_ERRNO errno 698 #endif 699 700 #undef LSS_INLINE 701 #ifdef SYS_INLINE 702 #define LSS_INLINE SYS_INLINE 703 #else 704 #define LSS_INLINE static inline 705 #endif 706 707 /* Allow the including file to override the prefix used for all new 708 * system calls. By default, it will be set to "sys_". 709 */ 710 #undef LSS_NAME 711 #ifndef SYS_PREFIX 712 #define LSS_NAME(name) sys_##name 713 #elif SYS_PREFIX < 0 714 #define LSS_NAME(name) name 715 #elif SYS_PREFIX == 0 716 #define LSS_NAME(name) sys0_##name 717 #elif SYS_PREFIX == 1 718 #define LSS_NAME(name) sys1_##name 719 #elif SYS_PREFIX == 2 720 #define LSS_NAME(name) sys2_##name 721 #elif SYS_PREFIX == 3 722 #define LSS_NAME(name) sys3_##name 723 #elif SYS_PREFIX == 4 724 #define LSS_NAME(name) sys4_##name 725 #elif SYS_PREFIX == 5 726 #define LSS_NAME(name) sys5_##name 727 #elif SYS_PREFIX == 6 728 #define LSS_NAME(name) sys6_##name 729 #elif SYS_PREFIX == 7 730 #define LSS_NAME(name) sys7_##name 731 #elif SYS_PREFIX == 8 732 #define LSS_NAME(name) sys8_##name 733 #elif SYS_PREFIX == 9 734 #define LSS_NAME(name) sys9_##name 735 #endif 736 737 #undef LSS_RETURN 738 #if (defined(__i386__) || defined(__x86_64__) || defined(__arm__)) 739 /* Failing system calls return a negative result in the range of 740 * -1..-4095. These are "errno" values with the sign inverted. 741 */ 742 #define LSS_RETURN(type, res) \ 743 do { \ 744 if ((unsigned long)(res) >= (unsigned long)(-4095)) { \ 745 LSS_ERRNO = -(res); \ 746 res = -1; \ 747 } \ 748 return (type) (res); \ 749 } while (0) 750 #elif defined(__mips__) 751 /* On MIPS, failing system calls return -1, and set errno in a 752 * separate CPU register. 753 */ 754 #define LSS_RETURN(type, res, err) \ 755 do { \ 756 if (err) { \ 757 LSS_ERRNO = (res); \ 758 res = -1; \ 759 } \ 760 return (type) (res); \ 761 } while (0) 762 #elif defined(__PPC__) 763 /* On PPC, failing system calls return -1, and set errno in a 764 * separate CPU register. See linux/unistd.h. 765 */ 766 #define LSS_RETURN(type, res, err) \ 767 do { \ 768 if (err & 0x10000000 ) { \ 769 LSS_ERRNO = (res); \ 770 res = -1; \ 771 } \ 772 return (type) (res); \ 773 } while (0) 774 #endif 775 #if defined(__i386__) 776 #if defined(NO_FRAME_POINTER) && (100 * __GNUC__ + __GNUC_MINOR__ >= 404) 777 /* This only works for GCC-4.4 and above -- the first version to use 778 .cfi directives for dwarf unwind info. */ 779 #define CFI_ADJUST_CFA_OFFSET(adjust) \ 780 ".cfi_adjust_cfa_offset " #adjust "\n" 781 #else 782 #define CFI_ADJUST_CFA_OFFSET(adjust) /**/ 783 #endif 784 785 /* In PIC mode (e.g. when building shared libraries), gcc for i386 786 * reserves ebx. Unfortunately, most distribution ship with implementations 787 * of _syscallX() which clobber ebx. 788 * Also, most definitions of _syscallX() neglect to mark "memory" as being 789 * clobbered. This causes problems with compilers, that do a better job 790 * at optimizing across __asm__ calls. 791 * So, we just have to redefine all of the _syscallX() macros. 792 */ 793 #undef LSS_BODY 794 #define LSS_BODY(type,args...) \ 795 long __res; \ 796 __asm__ __volatile__("push %%ebx\n" \ 797 CFI_ADJUST_CFA_OFFSET(4) \ 798 "movl %2,%%ebx\n" \ 799 "int $0x80\n" \ 800 "pop %%ebx\n" \ 801 CFI_ADJUST_CFA_OFFSET(-4) \ 802 args \ 803 : "esp", "memory"); \ 804 LSS_RETURN(type,__res) 805 #undef _syscall0 806 #define _syscall0(type,name) \ 807 type LSS_NAME(name)(void) { \ 808 long __res; \ 809 __asm__ volatile("int $0x80" \ 810 : "=a" (__res) \ 811 : "0" (__NR_##name) \ 812 : "memory"); \ 813 LSS_RETURN(type,__res); \ 814 } 815 #undef _syscall1 816 #define _syscall1(type,name,type1,arg1) \ 817 type LSS_NAME(name)(type1 arg1) { \ 818 LSS_BODY(type, \ 819 : "=a" (__res) \ 820 : "0" (__NR_##name), "ri" ((long)(arg1))); \ 821 } 822 #undef _syscall2 823 #define _syscall2(type,name,type1,arg1,type2,arg2) \ 824 type LSS_NAME(name)(type1 arg1,type2 arg2) { \ 825 LSS_BODY(type, \ 826 : "=a" (__res) \ 827 : "0" (__NR_##name),"ri" ((long)(arg1)), "c" ((long)(arg2))); \ 828 } 829 #undef _syscall3 830 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \ 831 type LSS_NAME(name)(type1 arg1,type2 arg2,type3 arg3) { \ 832 LSS_BODY(type, \ 833 : "=a" (__res) \ 834 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \ 835 "d" ((long)(arg3))); \ 836 } 837 #undef _syscall4 838 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \ 839 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \ 840 LSS_BODY(type, \ 841 : "=a" (__res) \ 842 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \ 843 "d" ((long)(arg3)),"S" ((long)(arg4))); \ 844 } 845 #undef _syscall5 846 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ 847 type5,arg5) \ 848 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ 849 type5 arg5) { \ 850 long __res; \ 851 __asm__ __volatile__("push %%ebx\n" \ 852 "movl %2,%%ebx\n" \ 853 "movl %1,%%eax\n" \ 854 "int $0x80\n" \ 855 "pop %%ebx" \ 856 : "=a" (__res) \ 857 : "i" (__NR_##name), "ri" ((long)(arg1)), \ 858 "c" ((long)(arg2)), "d" ((long)(arg3)), \ 859 "S" ((long)(arg4)), "D" ((long)(arg5)) \ 860 : "esp", "memory"); \ 861 LSS_RETURN(type,__res); \ 862 } 863 #undef _syscall6 864 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ 865 type5,arg5,type6,arg6) \ 866 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ 867 type5 arg5, type6 arg6) { \ 868 long __res; \ 869 struct { long __a1; long __a6; } __s = { (long)arg1, (long) arg6 }; \ 870 __asm__ __volatile__("push %%ebp\n" \ 871 "push %%ebx\n" \ 872 "movl 4(%2),%%ebp\n" \ 873 "movl 0(%2), %%ebx\n" \ 874 "movl %1,%%eax\n" \ 875 "int $0x80\n" \ 876 "pop %%ebx\n" \ 877 "pop %%ebp" \ 878 : "=a" (__res) \ 879 : "i" (__NR_##name), "0" ((long)(&__s)), \ 880 "c" ((long)(arg2)), "d" ((long)(arg3)), \ 881 "S" ((long)(arg4)), "D" ((long)(arg5)) \ 882 : "esp", "memory"); \ 883 LSS_RETURN(type,__res); \ 884 } 885 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack, 886 int flags, void *arg, int *parent_tidptr, 887 void *newtls, int *child_tidptr) { 888 long __res; 889 __asm__ __volatile__(/* if (fn == NULL) 890 * return -EINVAL; 891 */ 892 "movl %3,%%ecx\n" 893 "jecxz 1f\n" 894 895 /* if (child_stack == NULL) 896 * return -EINVAL; 897 */ 898 "movl %4,%%ecx\n" 899 "jecxz 1f\n" 900 901 /* Set up alignment of the child stack: 902 * child_stack = (child_stack & ~0xF) - 20; 903 */ 904 "andl $-16,%%ecx\n" 905 "subl $20,%%ecx\n" 906 907 /* Push "arg" and "fn" onto the stack that will be 908 * used by the child. 909 */ 910 "movl %6,%%eax\n" 911 "movl %%eax,4(%%ecx)\n" 912 "movl %3,%%eax\n" 913 "movl %%eax,(%%ecx)\n" 914 915 /* %eax = syscall(%eax = __NR_clone, 916 * %ebx = flags, 917 * %ecx = child_stack, 918 * %edx = parent_tidptr, 919 * %esi = newtls, 920 * %edi = child_tidptr) 921 * Also, make sure that %ebx gets preserved as it is 922 * used in PIC mode. 923 */ 924 "movl %8,%%esi\n" 925 "movl %7,%%edx\n" 926 "movl %5,%%eax\n" 927 "movl %9,%%edi\n" 928 "pushl %%ebx\n" 929 "movl %%eax,%%ebx\n" 930 "movl %2,%%eax\n" 931 "int $0x80\n" 932 933 /* In the parent: restore %ebx 934 * In the child: move "fn" into %ebx 935 */ 936 "popl %%ebx\n" 937 938 /* if (%eax != 0) 939 * return %eax; 940 */ 941 "test %%eax,%%eax\n" 942 "jnz 1f\n" 943 944 /* In the child, now. Terminate frame pointer chain. 945 */ 946 "movl $0,%%ebp\n" 947 948 /* Call "fn". "arg" is already on the stack. 949 */ 950 "call *%%ebx\n" 951 952 /* Call _exit(%ebx). Unfortunately older versions 953 * of gcc restrict the number of arguments that can 954 * be passed to asm(). So, we need to hard-code the 955 * system call number. 956 */ 957 "movl %%eax,%%ebx\n" 958 "movl $1,%%eax\n" 959 "int $0x80\n" 960 961 /* Return to parent. 962 */ 963 "1:\n" 964 : "=a" (__res) 965 : "0"(-EINVAL), "i"(__NR_clone), 966 "m"(fn), "m"(child_stack), "m"(flags), "m"(arg), 967 "m"(parent_tidptr), "m"(newtls), "m"(child_tidptr) 968 : "esp", "memory", "ecx", "edx", "esi", "edi"); 969 LSS_RETURN(int, __res); 970 } 971 972 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) { 973 /* On i386, the kernel does not know how to return from a signal 974 * handler. Instead, it relies on user space to provide a 975 * restorer function that calls the {rt_,}sigreturn() system call. 976 * Unfortunately, we cannot just reference the glibc version of this 977 * function, as glibc goes out of its way to make it inaccessible. 978 */ 979 void (*res)(void); 980 __asm__ __volatile__("call 2f\n" 981 "0:.align 16\n" 982 "1:movl %1,%%eax\n" 983 "int $0x80\n" 984 "2:popl %0\n" 985 "addl $(1b-0b),%0\n" 986 : "=a" (res) 987 : "i" (__NR_rt_sigreturn)); 988 return res; 989 } 990 LSS_INLINE void (*LSS_NAME(restore)(void))(void) { 991 /* On i386, the kernel does not know how to return from a signal 992 * handler. Instead, it relies on user space to provide a 993 * restorer function that calls the {rt_,}sigreturn() system call. 994 * Unfortunately, we cannot just reference the glibc version of this 995 * function, as glibc goes out of its way to make it inaccessible. 996 */ 997 void (*res)(void); 998 __asm__ __volatile__("call 2f\n" 999 "0:.align 16\n" 1000 "1:pop %%eax\n" 1001 "movl %1,%%eax\n" 1002 "int $0x80\n" 1003 "2:popl %0\n" 1004 "addl $(1b-0b),%0\n" 1005 : "=a" (res) 1006 : "i" (__NR_sigreturn)); 1007 return res; 1008 } 1009 #elif defined(__x86_64__) 1010 /* There are no known problems with any of the _syscallX() macros 1011 * currently shipping for x86_64, but we still need to be able to define 1012 * our own version so that we can override the location of the errno 1013 * location (e.g. when using the clone() system call with the CLONE_VM 1014 * option). 1015 */ 1016 #undef LSS_BODY 1017 #define LSS_BODY(type,name, ...) \ 1018 long __res; \ 1019 __asm__ __volatile__("syscall" : "=a" (__res) : "0" (__NR_##name), \ 1020 ##__VA_ARGS__ : "r11", "rcx", "memory"); \ 1021 LSS_RETURN(type, __res) 1022 #undef _syscall0 1023 #define _syscall0(type,name) \ 1024 type LSS_NAME(name)() { \ 1025 LSS_BODY(type, name); \ 1026 } 1027 #undef _syscall1 1028 #define _syscall1(type,name,type1,arg1) \ 1029 type LSS_NAME(name)(type1 arg1) { \ 1030 LSS_BODY(type, name, "D" ((long)(arg1))); \ 1031 } 1032 #undef _syscall2 1033 #define _syscall2(type,name,type1,arg1,type2,arg2) \ 1034 type LSS_NAME(name)(type1 arg1, type2 arg2) { \ 1035 LSS_BODY(type, name, "D" ((long)(arg1)), "S" ((long)(arg2))); \ 1036 } 1037 #undef _syscall3 1038 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \ 1039 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \ 1040 LSS_BODY(type, name, "D" ((long)(arg1)), "S" ((long)(arg2)), \ 1041 "d" ((long)(arg3))); \ 1042 } 1043 #undef _syscall4 1044 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \ 1045 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \ 1046 long __res; \ 1047 __asm__ __volatile__("movq %5,%%r10; syscall" : \ 1048 "=a" (__res) : "0" (__NR_##name), \ 1049 "D" ((long)(arg1)), "S" ((long)(arg2)), "d" ((long)(arg3)), \ 1050 "r" ((long)(arg4)) : "r10", "r11", "rcx", "memory"); \ 1051 LSS_RETURN(type, __res); \ 1052 } 1053 #undef _syscall5 1054 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ 1055 type5,arg5) \ 1056 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ 1057 type5 arg5) { \ 1058 long __res; \ 1059 __asm__ __volatile__("movq %5,%%r10; movq %6,%%r8; syscall" : \ 1060 "=a" (__res) : "0" (__NR_##name), \ 1061 "D" ((long)(arg1)), "S" ((long)(arg2)), "d" ((long)(arg3)), \ 1062 "r" ((long)(arg4)), "r" ((long)(arg5)) : \ 1063 "r8", "r10", "r11", "rcx", "memory"); \ 1064 LSS_RETURN(type, __res); \ 1065 } 1066 #undef _syscall6 1067 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ 1068 type5,arg5,type6,arg6) \ 1069 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ 1070 type5 arg5, type6 arg6) { \ 1071 long __res; \ 1072 __asm__ __volatile__("movq %5,%%r10; movq %6,%%r8; movq %7,%%r9;" \ 1073 "syscall" : \ 1074 "=a" (__res) : "0" (__NR_##name), \ 1075 "D" ((long)(arg1)), "S" ((long)(arg2)), "d" ((long)(arg3)), \ 1076 "r" ((long)(arg4)), "r" ((long)(arg5)), "r" ((long)(arg6)) : \ 1077 "r8", "r9", "r10", "r11", "rcx", "memory"); \ 1078 LSS_RETURN(type, __res); \ 1079 } 1080 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack, 1081 int flags, void *arg, int *parent_tidptr, 1082 void *newtls, int *child_tidptr) { 1083 long __res; 1084 { 1085 __asm__ __volatile__(/* if (fn == NULL) 1086 * return -EINVAL; 1087 */ 1088 "testq %4,%4\n" 1089 "jz 1f\n" 1090 1091 /* if (child_stack == NULL) 1092 * return -EINVAL; 1093 */ 1094 "testq %5,%5\n" 1095 "jz 1f\n" 1096 1097 /* Set up alignment of the child stack: 1098 * child_stack = (child_stack & ~0xF) - 16; 1099 */ 1100 "andq $-16,%5\n" 1101 "subq $16,%5\n" 1102 1103 /* Push "arg" and "fn" onto the stack that will be 1104 * used by the child. 1105 */ 1106 "movq %7,8(%5)\n" 1107 "movq %4,0(%5)\n" 1108 1109 /* %rax = syscall(%rax = __NR_clone, 1110 * %rdi = flags, 1111 * %rsi = child_stack, 1112 * %rdx = parent_tidptr, 1113 * %r8 = new_tls, 1114 * %r10 = child_tidptr) 1115 */ 1116 "movq %2,%%rax\n" 1117 "movq %9,%%r8\n" 1118 "movq %10,%%r10\n" 1119 "syscall\n" 1120 1121 /* if (%rax != 0) 1122 * return; 1123 */ 1124 "testq %%rax,%%rax\n" 1125 "jnz 1f\n" 1126 1127 /* In the child. Terminate frame pointer chain. 1128 */ 1129 "xorq %%rbp,%%rbp\n" 1130 1131 /* Call "fn(arg)". 1132 */ 1133 "popq %%rax\n" 1134 "popq %%rdi\n" 1135 "call *%%rax\n" 1136 1137 /* Call _exit(%ebx). 1138 */ 1139 "movq %%rax,%%rdi\n" 1140 "movq %3,%%rax\n" 1141 "syscall\n" 1142 1143 /* Return to parent. 1144 */ 1145 "1:\n" 1146 : "=a" (__res) 1147 : "0"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit), 1148 "r"(fn), "S"(child_stack), "D"(flags), "r"(arg), 1149 "d"(parent_tidptr), "g"(newtls), "g"(child_tidptr) 1150 : "rsp", "memory", "r8", "r10", "r11", "rcx"); 1151 } 1152 LSS_RETURN(int, __res); 1153 } 1154 1155 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) { 1156 /* On x86-64, the kernel does not know how to return from 1157 * a signal handler. Instead, it relies on user space to provide a 1158 * restorer function that calls the rt_sigreturn() system call. 1159 * Unfortunately, we cannot just reference the glibc version of this 1160 * function, as glibc goes out of its way to make it inaccessible. 1161 */ 1162 void (*res)(void); 1163 __asm__ __volatile__("call 2f\n" 1164 "0:.align 16\n" 1165 "1:movq %1,%%rax\n" 1166 "syscall\n" 1167 "2:popq %0\n" 1168 "addq $(1b-0b),%0\n" 1169 : "=a" (res) 1170 : "i" (__NR_rt_sigreturn)); 1171 return res; 1172 } 1173 #elif defined(__arm__) 1174 /* Most definitions of _syscallX() neglect to mark "memory" as being 1175 * clobbered. This causes problems with compilers, that do a better job 1176 * at optimizing across __asm__ calls. 1177 * So, we just have to redefine all fo the _syscallX() macros. 1178 */ 1179 #undef LSS_REG 1180 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a 1181 1182 /* r0..r3 are scratch registers and not preserved across function 1183 * calls. We need to first evaluate the first 4 syscall arguments 1184 * and store them on stack. They must be loaded into r0..r3 after 1185 * all function calls to avoid r0..r3 being clobbered. 1186 */ 1187 #undef LSS_SAVE_ARG 1188 #define LSS_SAVE_ARG(r,a) long __tmp##r = (long)a 1189 #undef LSS_LOAD_ARG 1190 #define LSS_LOAD_ARG(r) register long __r##r __asm__("r"#r) = __tmp##r 1191 1192 #undef LSS_BODY 1193 #define LSS_BODY(type, name, args...) \ 1194 register long __res_r0 __asm__("r0"); \ 1195 long __res; \ 1196 __SYS_REG(name) \ 1197 __asm__ __volatile__ (__syscall_safe(name) \ 1198 : "=r"(__res_r0) \ 1199 : __SYS_REG_LIST(args) \ 1200 : "lr", "memory"); \ 1201 __res = __res_r0; \ 1202 LSS_RETURN(type, __res) 1203 #undef _syscall0 1204 #define _syscall0(type, name) \ 1205 type LSS_NAME(name)() { \ 1206 LSS_BODY(type, name); \ 1207 } 1208 #undef _syscall1 1209 #define _syscall1(type, name, type1, arg1) \ 1210 type LSS_NAME(name)(type1 arg1) { \ 1211 /* There is no need for using a volatile temp. */ \ 1212 LSS_REG(0, arg1); \ 1213 LSS_BODY(type, name, "r"(__r0)); \ 1214 } 1215 #undef _syscall2 1216 #define _syscall2(type, name, type1, arg1, type2, arg2) \ 1217 type LSS_NAME(name)(type1 arg1, type2 arg2) { \ 1218 LSS_SAVE_ARG(0, arg1); \ 1219 LSS_SAVE_ARG(1, arg2); \ 1220 LSS_LOAD_ARG(0); \ 1221 LSS_LOAD_ARG(1); \ 1222 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \ 1223 } 1224 #undef _syscall3 1225 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \ 1226 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \ 1227 LSS_SAVE_ARG(0, arg1); \ 1228 LSS_SAVE_ARG(1, arg2); \ 1229 LSS_SAVE_ARG(2, arg3); \ 1230 LSS_LOAD_ARG(0); \ 1231 LSS_LOAD_ARG(1); \ 1232 LSS_LOAD_ARG(2); \ 1233 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \ 1234 } 1235 #undef _syscall4 1236 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \ 1237 type4, arg4) \ 1238 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \ 1239 LSS_SAVE_ARG(0, arg1); \ 1240 LSS_SAVE_ARG(1, arg2); \ 1241 LSS_SAVE_ARG(2, arg3); \ 1242 LSS_SAVE_ARG(3, arg4); \ 1243 LSS_LOAD_ARG(0); \ 1244 LSS_LOAD_ARG(1); \ 1245 LSS_LOAD_ARG(2); \ 1246 LSS_LOAD_ARG(3); \ 1247 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \ 1248 } 1249 #undef _syscall5 1250 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \ 1251 type4, arg4, type5, arg5) \ 1252 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ 1253 type5 arg5) { \ 1254 LSS_SAVE_ARG(0, arg1); \ 1255 LSS_SAVE_ARG(1, arg2); \ 1256 LSS_SAVE_ARG(2, arg3); \ 1257 LSS_SAVE_ARG(3, arg4); \ 1258 LSS_REG(4, arg5); \ 1259 LSS_LOAD_ARG(0); \ 1260 LSS_LOAD_ARG(1); \ 1261 LSS_LOAD_ARG(2); \ 1262 LSS_LOAD_ARG(3); \ 1263 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \ 1264 "r"(__r4)); \ 1265 } 1266 #undef _syscall6 1267 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \ 1268 type4, arg4, type5, arg5, type6, arg6) \ 1269 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ 1270 type5 arg5, type6 arg6) { \ 1271 LSS_SAVE_ARG(0, arg1); \ 1272 LSS_SAVE_ARG(1, arg2); \ 1273 LSS_SAVE_ARG(2, arg3); \ 1274 LSS_SAVE_ARG(3, arg4); \ 1275 LSS_REG(4, arg5); \ 1276 LSS_REG(5, arg6); \ 1277 LSS_LOAD_ARG(0); \ 1278 LSS_LOAD_ARG(1); \ 1279 LSS_LOAD_ARG(2); \ 1280 LSS_LOAD_ARG(3); \ 1281 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \ 1282 "r"(__r4), "r"(__r5)); \ 1283 } 1284 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack, 1285 int flags, void *arg, int *parent_tidptr, 1286 void *newtls, int *child_tidptr) { 1287 register long __res __asm__("r5"); 1288 { 1289 if (fn == NULL || child_stack == NULL) { 1290 __res = -EINVAL; 1291 goto clone_exit; 1292 } 1293 1294 /* stash first 4 arguments on stack first because we can only load 1295 * them after all function calls. 1296 */ 1297 int tmp_flags = flags; 1298 int * tmp_stack = (int*) child_stack; 1299 void * tmp_ptid = parent_tidptr; 1300 void * tmp_tls = newtls; 1301 1302 register int *__ctid __asm__("r4") = child_tidptr; 1303 1304 /* Push "arg" and "fn" onto the stack that will be 1305 * used by the child. 1306 */ 1307 *(--tmp_stack) = (int) arg; 1308 *(--tmp_stack) = (int) fn; 1309 1310 /* We must load r0..r3 last after all possible function calls. */ 1311 register int __flags __asm__("r0") = tmp_flags; 1312 register void *__stack __asm__("r1") = tmp_stack; 1313 register void *__ptid __asm__("r2") = tmp_ptid; 1314 register void *__tls __asm__("r3") = tmp_tls; 1315 1316 /* %r0 = syscall(%r0 = flags, 1317 * %r1 = child_stack, 1318 * %r2 = parent_tidptr, 1319 * %r3 = newtls, 1320 * %r4 = child_tidptr) 1321 */ 1322 __SYS_REG(clone) 1323 __asm__ __volatile__(/* %r0 = syscall(%r0 = flags, 1324 * %r1 = child_stack, 1325 * %r2 = parent_tidptr, 1326 * %r3 = newtls, 1327 * %r4 = child_tidptr) 1328 */ 1329 "push {r7}\n" 1330 "mov r7,%1\n" 1331 __syscall(clone)"\n" 1332 1333 /* if (%r0 != 0) 1334 * return %r0; 1335 */ 1336 "movs %0,r0\n" 1337 "bne 1f\n" 1338 1339 /* In the child, now. Call "fn(arg)". 1340 */ 1341 "ldr r0,[sp, #4]\n" 1342 "mov lr,pc\n" 1343 "ldr pc,[sp]\n" 1344 1345 /* Call _exit(%r0), which never returns. We only 1346 * need to set r7 for EABI syscall ABI but we do 1347 * this always to simplify code sharing between 1348 * old and new syscall ABIs. 1349 */ 1350 "mov r7,%2\n" 1351 __syscall(exit)"\n" 1352 1353 /* Pop r7 from the stack only in the parent. 1354 */ 1355 "1: pop {r7}\n" 1356 : "=r" (__res) 1357 : "r"(__sysreg), 1358 "i"(__NR_exit), "r"(__stack), "r"(__flags), 1359 "r"(__ptid), "r"(__tls), "r"(__ctid) 1360 : "cc", "lr", "memory"); 1361 } 1362 clone_exit: 1363 LSS_RETURN(int, __res); 1364 } 1365 #elif defined(__mips__) 1366 #undef LSS_REG 1367 #define LSS_REG(r,a) register unsigned long __r##r __asm__("$"#r) = \ 1368 (unsigned long)(a) 1369 1370 #if _MIPS_SIM == _MIPS_SIM_ABI32 1371 // See http://sources.redhat.com/ml/libc-alpha/2004-10/msg00050.html 1372 // or http://www.linux-mips.org/archives/linux-mips/2004-10/msg00142.html 1373 #define MIPS_SYSCALL_CLOBBERS "$1", "$3", "$8", "$9", "$10", "$11", "$12",\ 1374 "$13", "$14", "$15", "$24", "$25", "memory" 1375 #else 1376 #define MIPS_SYSCALL_CLOBBERS "$1", "$3", "$10", "$11", "$12", "$13", \ 1377 "$14", "$15", "$24", "$25", "memory" 1378 #endif 1379 1380 #undef LSS_BODY 1381 #define LSS_BODY(type,name,r7,...) \ 1382 register unsigned long __v0 __asm__("$2") = __NR_##name; \ 1383 __asm__ __volatile__ ("syscall\n" \ 1384 : "=&r"(__v0), r7 (__r7) \ 1385 : "0"(__v0), ##__VA_ARGS__ \ 1386 : MIPS_SYSCALL_CLOBBERS); \ 1387 LSS_RETURN(type, __v0, __r7) 1388 #undef _syscall0 1389 #define _syscall0(type, name) \ 1390 type LSS_NAME(name)() { \ 1391 register unsigned long __r7 __asm__("$7"); \ 1392 LSS_BODY(type, name, "=r"); \ 1393 } 1394 #undef _syscall1 1395 #define _syscall1(type, name, type1, arg1) \ 1396 type LSS_NAME(name)(type1 arg1) { \ 1397 register unsigned long __r7 __asm__("$7"); \ 1398 LSS_REG(4, arg1); LSS_BODY(type, name, "=r", "r"(__r4)); \ 1399 } 1400 #undef _syscall2 1401 #define _syscall2(type, name, type1, arg1, type2, arg2) \ 1402 type LSS_NAME(name)(type1 arg1, type2 arg2) { \ 1403 register unsigned long __r7 __asm__("$7"); \ 1404 LSS_REG(4, arg1); LSS_REG(5, arg2); \ 1405 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5)); \ 1406 } 1407 #undef _syscall3 1408 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \ 1409 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \ 1410 register unsigned long __r7 __asm__("$7"); \ 1411 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \ 1412 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5), "r"(__r6)); \ 1413 } 1414 #undef _syscall4 1415 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \ 1416 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \ 1417 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \ 1418 LSS_REG(7, arg4); \ 1419 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6)); \ 1420 } 1421 #undef _syscall5 1422 #if _MIPS_SIM == _MIPS_SIM_ABI32 1423 /* The old 32bit MIPS system call API passes the fifth and sixth argument 1424 * on the stack, whereas the new APIs use registers "r8" and "r9". 1425 */ 1426 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ 1427 type5,arg5) \ 1428 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ 1429 type5 arg5) { \ 1430 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \ 1431 LSS_REG(7, arg4); \ 1432 register unsigned long __v0 __asm__("$2"); \ 1433 __asm__ __volatile__ (".set noreorder\n" \ 1434 "lw $2, %6\n" \ 1435 "subu $29, 32\n" \ 1436 "sw $2, 16($29)\n" \ 1437 "li $2, %2\n" \ 1438 "syscall\n" \ 1439 "addiu $29, 32\n" \ 1440 ".set reorder\n" \ 1441 : "=&r"(__v0), "+r" (__r7) \ 1442 : "i" (__NR_##name), "r"(__r4), "r"(__r5), \ 1443 "r"(__r6), "m" ((unsigned long)arg5) \ 1444 : MIPS_SYSCALL_CLOBBERS); \ 1445 LSS_RETURN(type, __v0, __r7); \ 1446 } 1447 #else 1448 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ 1449 type5,arg5) \ 1450 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ 1451 type5 arg5) { \ 1452 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \ 1453 LSS_REG(7, arg4); LSS_REG(8, arg5); \ 1454 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \ 1455 "r"(__r8)); \ 1456 } 1457 #endif 1458 #undef _syscall6 1459 #if _MIPS_SIM == _MIPS_SIM_ABI32 1460 /* The old 32bit MIPS system call API passes the fifth and sixth argument 1461 * on the stack, whereas the new APIs use registers "r8" and "r9". 1462 */ 1463 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ 1464 type5,arg5,type6,arg6) \ 1465 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ 1466 type5 arg5, type6 arg6) { \ 1467 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \ 1468 LSS_REG(7, arg4); \ 1469 register unsigned long __v0 __asm__("$2"); \ 1470 __asm__ __volatile__ (".set noreorder\n" \ 1471 "lw $2, %6\n" \ 1472 "lw $8, %7\n" \ 1473 "subu $29, 32\n" \ 1474 "sw $2, 16($29)\n" \ 1475 "sw $8, 20($29)\n" \ 1476 "li $2, %2\n" \ 1477 "syscall\n" \ 1478 "addiu $29, 32\n" \ 1479 ".set reorder\n" \ 1480 : "=&r"(__v0), "+r" (__r7) \ 1481 : "i" (__NR_##name), "r"(__r4), "r"(__r5), \ 1482 "r"(__r6), "r" ((unsigned long)arg5), \ 1483 "r" ((unsigned long)arg6) \ 1484 : MIPS_SYSCALL_CLOBBERS); \ 1485 LSS_RETURN(type, __v0, __r7); \ 1486 } 1487 #else 1488 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ 1489 type5,arg5,type6,arg6) \ 1490 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ 1491 type5 arg5,type6 arg6) { \ 1492 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \ 1493 LSS_REG(7, arg4); LSS_REG(8, arg5); LSS_REG(9, arg6); \ 1494 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \ 1495 "r"(__r8), "r"(__r9)); \ 1496 } 1497 #endif 1498 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack, 1499 int flags, void *arg, int *parent_tidptr, 1500 void *newtls, int *child_tidptr) { 1501 register unsigned long __v0 __asm__("$2"); 1502 register unsigned long __r7 __asm__("$7") = (unsigned long)newtls; 1503 { 1504 register int __flags __asm__("$4") = flags; 1505 register void *__stack __asm__("$5") = child_stack; 1506 register void *__ptid __asm__("$6") = parent_tidptr; 1507 register int *__ctid __asm__("$8") = child_tidptr; 1508 __asm__ __volatile__( 1509 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32 1510 "subu $29,24\n" 1511 #elif _MIPS_SIM == _MIPS_SIM_NABI32 1512 "sub $29,16\n" 1513 #else 1514 "dsubu $29,16\n" 1515 #endif 1516 1517 /* if (fn == NULL || child_stack == NULL) 1518 * return -EINVAL; 1519 */ 1520 "li %0,%2\n" 1521 "beqz %5,1f\n" 1522 "beqz %6,1f\n" 1523 1524 /* Push "arg" and "fn" onto the stack that will be 1525 * used by the child. 1526 */ 1527 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32 1528 "subu %6,32\n" 1529 "sw %5,0(%6)\n" 1530 "sw %8,4(%6)\n" 1531 #elif _MIPS_SIM == _MIPS_SIM_NABI32 1532 "sub %6,32\n" 1533 "sw %5,0(%6)\n" 1534 "sw %8,8(%6)\n" 1535 #else 1536 "dsubu %6,32\n" 1537 "sd %5,0(%6)\n" 1538 "sd %8,8(%6)\n" 1539 #endif 1540 1541 /* $7 = syscall($4 = flags, 1542 * $5 = child_stack, 1543 * $6 = parent_tidptr, 1544 * $7 = newtls, 1545 * $8 = child_tidptr) 1546 */ 1547 "li $2,%3\n" 1548 "syscall\n" 1549 1550 /* if ($7 != 0) 1551 * return $2; 1552 */ 1553 "bnez $7,1f\n" 1554 "bnez $2,1f\n" 1555 1556 /* In the child, now. Call "fn(arg)". 1557 */ 1558 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32 1559 "lw $25,0($29)\n" 1560 "lw $4,4($29)\n" 1561 #elif _MIPS_SIM == _MIPS_SIM_NABI32 1562 "lw $25,0($29)\n" 1563 "lw $4,8($29)\n" 1564 #else 1565 "ld $25,0($29)\n" 1566 "ld $4,8($29)\n" 1567 #endif 1568 "jalr $25\n" 1569 1570 /* Call _exit($2) 1571 */ 1572 "move $4,$2\n" 1573 "li $2,%4\n" 1574 "syscall\n" 1575 1576 "1:\n" 1577 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32 1578 "addu $29, 24\n" 1579 #elif _MIPS_SIM == _MIPS_SIM_NABI32 1580 "add $29, 16\n" 1581 #else 1582 "daddu $29,16\n" 1583 #endif 1584 : "=&r" (__v0), "=r" (__r7) 1585 : "i"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit), 1586 "r"(fn), "r"(__stack), "r"(__flags), "r"(arg), 1587 "r"(__ptid), "r"(__r7), "r"(__ctid) 1588 : "$9", "$10", "$11", "$12", "$13", "$14", "$15", 1589 "$24", "memory"); 1590 } 1591 LSS_RETURN(int, __v0, __r7); 1592 } 1593 #elif defined (__PPC__) 1594 #undef LSS_LOADARGS_0 1595 #define LSS_LOADARGS_0(name, dummy...) \ 1596 __sc_0 = __NR_##name 1597 #undef LSS_LOADARGS_1 1598 #define LSS_LOADARGS_1(name, arg1) \ 1599 LSS_LOADARGS_0(name); \ 1600 __sc_3 = (unsigned long) (arg1) 1601 #undef LSS_LOADARGS_2 1602 #define LSS_LOADARGS_2(name, arg1, arg2) \ 1603 LSS_LOADARGS_1(name, arg1); \ 1604 __sc_4 = (unsigned long) (arg2) 1605 #undef LSS_LOADARGS_3 1606 #define LSS_LOADARGS_3(name, arg1, arg2, arg3) \ 1607 LSS_LOADARGS_2(name, arg1, arg2); \ 1608 __sc_5 = (unsigned long) (arg3) 1609 #undef LSS_LOADARGS_4 1610 #define LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4) \ 1611 LSS_LOADARGS_3(name, arg1, arg2, arg3); \ 1612 __sc_6 = (unsigned long) (arg4) 1613 #undef LSS_LOADARGS_5 1614 #define LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5) \ 1615 LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4); \ 1616 __sc_7 = (unsigned long) (arg5) 1617 #undef LSS_LOADARGS_6 1618 #define LSS_LOADARGS_6(name, arg1, arg2, arg3, arg4, arg5, arg6) \ 1619 LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5); \ 1620 __sc_8 = (unsigned long) (arg6) 1621 #undef LSS_ASMINPUT_0 1622 #define LSS_ASMINPUT_0 "0" (__sc_0) 1623 #undef LSS_ASMINPUT_1 1624 #define LSS_ASMINPUT_1 LSS_ASMINPUT_0, "1" (__sc_3) 1625 #undef LSS_ASMINPUT_2 1626 #define LSS_ASMINPUT_2 LSS_ASMINPUT_1, "2" (__sc_4) 1627 #undef LSS_ASMINPUT_3 1628 #define LSS_ASMINPUT_3 LSS_ASMINPUT_2, "3" (__sc_5) 1629 #undef LSS_ASMINPUT_4 1630 #define LSS_ASMINPUT_4 LSS_ASMINPUT_3, "4" (__sc_6) 1631 #undef LSS_ASMINPUT_5 1632 #define LSS_ASMINPUT_5 LSS_ASMINPUT_4, "5" (__sc_7) 1633 #undef LSS_ASMINPUT_6 1634 #define LSS_ASMINPUT_6 LSS_ASMINPUT_5, "6" (__sc_8) 1635 #undef LSS_BODY 1636 #define LSS_BODY(nr, type, name, args...) \ 1637 long __sc_ret, __sc_err; \ 1638 { \ 1639 register unsigned long __sc_0 __asm__ ("r0"); \ 1640 register unsigned long __sc_3 __asm__ ("r3"); \ 1641 register unsigned long __sc_4 __asm__ ("r4"); \ 1642 register unsigned long __sc_5 __asm__ ("r5"); \ 1643 register unsigned long __sc_6 __asm__ ("r6"); \ 1644 register unsigned long __sc_7 __asm__ ("r7"); \ 1645 register unsigned long __sc_8 __asm__ ("r8"); \ 1646 \ 1647 LSS_LOADARGS_##nr(name, args); \ 1648 __asm__ __volatile__ \ 1649 ("sc\n\t" \ 1650 "mfcr %0" \ 1651 : "=&r" (__sc_0), \ 1652 "=&r" (__sc_3), "=&r" (__sc_4), \ 1653 "=&r" (__sc_5), "=&r" (__sc_6), \ 1654 "=&r" (__sc_7), "=&r" (__sc_8) \ 1655 : LSS_ASMINPUT_##nr \ 1656 : "cr0", "ctr", "memory", \ 1657 "r9", "r10", "r11", "r12"); \ 1658 __sc_ret = __sc_3; \ 1659 __sc_err = __sc_0; \ 1660 } \ 1661 LSS_RETURN(type, __sc_ret, __sc_err) 1662 #undef _syscall0 1663 #define _syscall0(type, name) \ 1664 type LSS_NAME(name)(void) { \ 1665 LSS_BODY(0, type, name); \ 1666 } 1667 #undef _syscall1 1668 #define _syscall1(type, name, type1, arg1) \ 1669 type LSS_NAME(name)(type1 arg1) { \ 1670 LSS_BODY(1, type, name, arg1); \ 1671 } 1672 #undef _syscall2 1673 #define _syscall2(type, name, type1, arg1, type2, arg2) \ 1674 type LSS_NAME(name)(type1 arg1, type2 arg2) { \ 1675 LSS_BODY(2, type, name, arg1, arg2); \ 1676 } 1677 #undef _syscall3 1678 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \ 1679 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \ 1680 LSS_BODY(3, type, name, arg1, arg2, arg3); \ 1681 } 1682 #undef _syscall4 1683 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \ 1684 type4, arg4) \ 1685 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \ 1686 LSS_BODY(4, type, name, arg1, arg2, arg3, arg4); \ 1687 } 1688 #undef _syscall5 1689 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \ 1690 type4, arg4, type5, arg5) \ 1691 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ 1692 type5 arg5) { \ 1693 LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5); \ 1694 } 1695 #undef _syscall6 1696 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \ 1697 type4, arg4, type5, arg5, type6, arg6) \ 1698 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \ 1699 type5 arg5, type6 arg6) { \ 1700 LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6); \ 1701 } 1702 /* clone function adapted from glibc 2.3.6 clone.S */ 1703 /* TODO(csilvers): consider wrapping some args up in a struct, like we 1704 * do for i386's _syscall6, so we can compile successfully on gcc 2.95 1705 */ 1706 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack, 1707 int flags, void *arg, int *parent_tidptr, 1708 void *newtls, int *child_tidptr) { 1709 long __ret, __err; 1710 { 1711 register int (*__fn)(void *) __asm__ ("r8") = fn; 1712 register void *__cstack __asm__ ("r4") = child_stack; 1713 register int __flags __asm__ ("r3") = flags; 1714 register void * __arg __asm__ ("r9") = arg; 1715 register int * __ptidptr __asm__ ("r5") = parent_tidptr; 1716 register void * __newtls __asm__ ("r6") = newtls; 1717 register int * __ctidptr __asm__ ("r7") = child_tidptr; 1718 __asm__ __volatile__( 1719 /* check for fn == NULL 1720 * and child_stack == NULL 1721 */ 1722 "cmpwi cr0, %6, 0\n\t" 1723 "cmpwi cr1, %7, 0\n\t" 1724 "cror cr0*4+eq, cr1*4+eq, cr0*4+eq\n\t" 1725 "beq- cr0, 1f\n\t" 1726 1727 /* set up stack frame for child */ 1728 "clrrwi %7, %7, 4\n\t" 1729 "li 0, 0\n\t" 1730 "stwu 0, -16(%7)\n\t" 1731 1732 /* fn, arg, child_stack are saved across the syscall: r28-30 */ 1733 "mr 28, %6\n\t" 1734 "mr 29, %7\n\t" 1735 "mr 27, %9\n\t" 1736 1737 /* syscall */ 1738 "li 0, %4\n\t" 1739 /* flags already in r3 1740 * child_stack already in r4 1741 * ptidptr already in r5 1742 * newtls already in r6 1743 * ctidptr already in r7 1744 */ 1745 "sc\n\t" 1746 1747 /* Test if syscall was successful */ 1748 "cmpwi cr1, 3, 0\n\t" 1749 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t" 1750 "bne- cr1, 1f\n\t" 1751 1752 /* Do the function call */ 1753 "mtctr 28\n\t" 1754 "mr 3, 27\n\t" 1755 "bctrl\n\t" 1756 1757 /* Call _exit(r3) */ 1758 "li 0, %5\n\t" 1759 "sc\n\t" 1760 1761 /* Return to parent */ 1762 "1:\n" 1763 "mfcr %1\n\t" 1764 "mr %0, 3\n\t" 1765 : "=r" (__ret), "=r" (__err) 1766 : "0" (-1), "1" (EINVAL), 1767 "i" (__NR_clone), "i" (__NR_exit), 1768 "r" (__fn), "r" (__cstack), "r" (__flags), 1769 "r" (__arg), "r" (__ptidptr), "r" (__newtls), 1770 "r" (__ctidptr) 1771 : "cr0", "cr1", "memory", "ctr", 1772 "r0", "r29", "r27", "r28"); 1773 } 1774 LSS_RETURN(int, __ret, __err); 1775 } 1776 #endif 1777 #define __NR__exit __NR_exit 1778 #define __NR__gettid __NR_gettid 1779 #define __NR__mremap __NR_mremap 1780 LSS_INLINE _syscall1(int, close, int, f) 1781 LSS_INLINE _syscall1(int, _exit, int, e) 1782 LSS_INLINE _syscall3(int, fcntl, int, f, 1783 int, c, long, a) 1784 LSS_INLINE _syscall2(int, fstat, int, f, 1785 struct kernel_stat*, b) 1786 LSS_INLINE _syscall4(int, futex, int*, a, 1787 int, o, int, v, 1788 struct kernel_timespec*, t) 1789 LSS_INLINE _syscall3(int, getdents, int, f, 1790 struct kernel_dirent*, d, int, c) 1791#ifdef __NR_getdents64 1792 LSS_INLINE _syscall3(int, getdents64, int, f, 1793 struct kernel_dirent64*, d, int, c) 1794#endif 1795 LSS_INLINE _syscall0(pid_t, getpid) 1796 LSS_INLINE _syscall0(pid_t, getppid) 1797 LSS_INLINE _syscall0(pid_t, _gettid) 1798 LSS_INLINE _syscall2(int, kill, pid_t, p, 1799 int, s) 1800 LSS_INLINE _syscall3(off_t, lseek, int, f, 1801 off_t, o, int, w) 1802 LSS_INLINE _syscall2(int, munmap, void*, s, 1803 size_t, l) 1804 LSS_INLINE _syscall5(void*, _mremap, void*, o, 1805 size_t, os, size_t, ns, 1806 unsigned long, f, void *, a) 1807 LSS_INLINE _syscall3(int, open, const char*, p, 1808 int, f, int, m) 1809 LSS_INLINE _syscall2(int, prctl, int, o, 1810 long, a) 1811 LSS_INLINE _syscall4(long, ptrace, int, r, 1812 pid_t, p, void *, a, void *, d) 1813 LSS_INLINE _syscall3(ssize_t, read, int, f, 1814 void *, b, size_t, c) 1815 LSS_INLINE _syscall4(int, rt_sigaction, int, s, 1816 const struct kernel_sigaction*, a, 1817 struct kernel_sigaction*, o, size_t, c) 1818 LSS_INLINE _syscall4(int, rt_sigprocmask, int, h, 1819 const struct kernel_sigset_t*, s, 1820 struct kernel_sigset_t*, o, size_t, c); 1821 LSS_INLINE _syscall0(int, sched_yield) 1822 LSS_INLINE _syscall2(int, sigaltstack, const stack_t*, s, 1823 const stack_t*, o) 1824 LSS_INLINE _syscall2(int, stat, const char*, f, 1825 struct kernel_stat*, b) 1826 LSS_INLINE _syscall3(ssize_t, write, int, f, 1827 const void *, b, size_t, c) 1828 #if defined(__NR_getcpu) 1829 LSS_INLINE _syscall3(long, getcpu, unsigned *, cpu, 1830 unsigned *, node, void *, unused); 1831 #endif 1832 #if defined(__x86_64__) || \ 1833 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32) 1834 LSS_INLINE _syscall3(int, socket, int, d, 1835 int, t, int, p) 1836 #endif 1837 #if defined(__x86_64__) 1838 LSS_INLINE _syscall6(void*, mmap, void*, s, 1839 size_t, l, int, p, 1840 int, f, int, d, 1841 __off64_t, o) 1842 1843 LSS_INLINE int LSS_NAME(sigaction)(int signum, 1844 const struct kernel_sigaction *act, 1845 struct kernel_sigaction *oldact) { 1846 /* On x86_64, the kernel requires us to always set our own 1847 * SA_RESTORER in order to be able to return from a signal handler. 1848 * This function must have a "magic" signature that the "gdb" 1849 * (and maybe the kernel?) can recognize. 1850 */ 1851 if (act != NULL && !(act->sa_flags & SA_RESTORER)) { 1852 struct kernel_sigaction a = *act; 1853 a.sa_flags |= SA_RESTORER; 1854 a.sa_restorer = LSS_NAME(restore_rt)(); 1855 return LSS_NAME(rt_sigaction)(signum, &a, oldact, 1856 (KERNEL_NSIG+7)/8); 1857 } else { 1858 return LSS_NAME(rt_sigaction)(signum, act, oldact, 1859 (KERNEL_NSIG+7)/8); 1860 } 1861 } 1862 1863 LSS_INLINE int LSS_NAME(sigprocmask)(int how, 1864 const struct kernel_sigset_t *set, 1865 struct kernel_sigset_t *oldset) { 1866 return LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8); 1867 } 1868 #endif 1869 #if defined(__x86_64__) || \ 1870 defined(__arm__) || \ 1871 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32) 1872 LSS_INLINE _syscall4(pid_t, wait4, pid_t, p, 1873 int*, s, int, o, 1874 struct kernel_rusage*, r) 1875 LSS_INLINE pid_t LSS_NAME(waitpid)(pid_t pid, int *status, int options){ 1876 return LSS_NAME(wait4)(pid, status, options, 0); 1877 } 1878 #endif 1879 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) 1880 LSS_INLINE _syscall4(int, openat, int, d, const char *, p, int, f, int, m) 1881 #endif 1882 LSS_INLINE int LSS_NAME(sigemptyset)(struct kernel_sigset_t *set) { 1883 memset(&set->sig, 0, sizeof(set->sig)); 1884 return 0; 1885 } 1886 1887 LSS_INLINE int LSS_NAME(sigfillset)(struct kernel_sigset_t *set) { 1888 memset(&set->sig, -1, sizeof(set->sig)); 1889 return 0; 1890 } 1891 1892 LSS_INLINE int LSS_NAME(sigaddset)(struct kernel_sigset_t *set, 1893 int signum) { 1894 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) { 1895 LSS_ERRNO = EINVAL; 1896 return -1; 1897 } else { 1898 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))] 1899 |= 1UL << ((signum - 1) % (8*sizeof(set->sig[0]))); 1900 return 0; 1901 } 1902 } 1903 1904 LSS_INLINE int LSS_NAME(sigdelset)(struct kernel_sigset_t *set, 1905 int signum) { 1906 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) { 1907 LSS_ERRNO = EINVAL; 1908 return -1; 1909 } else { 1910 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))] 1911 &= ~(1UL << ((signum - 1) % (8*sizeof(set->sig[0])))); 1912 return 0; 1913 } 1914 } 1915 1916 #if defined(__i386__) || \ 1917 defined(__arm__) || \ 1918 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || defined(__PPC__) 1919 #define __NR__sigaction __NR_sigaction 1920 #define __NR__sigprocmask __NR_sigprocmask 1921 LSS_INLINE _syscall2(int, fstat64, int, f, 1922 struct kernel_stat64 *, b) 1923 LSS_INLINE _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo, 1924 loff_t *, res, uint, wh) 1925#ifdef __PPC64__ 1926 LSS_INLINE _syscall6(void*, mmap, void*, s, 1927 size_t, l, int, p, 1928 int, f, int, d, 1929 off_t, o) 1930#else 1931 #ifndef __ARM_EABI__ 1932 /* Not available on ARM EABI Linux. */ 1933 LSS_INLINE _syscall1(void*, mmap, void*, a) 1934 #endif 1935 LSS_INLINE _syscall6(void*, mmap2, void*, s, 1936 size_t, l, int, p, 1937 int, f, int, d, 1938 off_t, o) 1939#endif 1940 LSS_INLINE _syscall3(int, _sigaction, int, s, 1941 const struct kernel_old_sigaction*, a, 1942 struct kernel_old_sigaction*, o) 1943 LSS_INLINE _syscall3(int, _sigprocmask, int, h, 1944 const unsigned long*, s, 1945 unsigned long*, o) 1946 LSS_INLINE _syscall2(int, stat64, const char *, p, 1947 struct kernel_stat64 *, b) 1948 1949 LSS_INLINE int LSS_NAME(sigaction)(int signum, 1950 const struct kernel_sigaction *act, 1951 struct kernel_sigaction *oldact) { 1952 int old_errno = LSS_ERRNO; 1953 int rc; 1954 struct kernel_sigaction a; 1955 if (act != NULL) { 1956 a = *act; 1957 #ifdef __i386__ 1958 /* On i386, the kernel requires us to always set our own 1959 * SA_RESTORER when using realtime signals. Otherwise, it does not 1960 * know how to return from a signal handler. This function must have 1961 * a "magic" signature that the "gdb" (and maybe the kernel?) can 1962 * recognize. 1963 * Apparently, a SA_RESTORER is implicitly set by the kernel, when 1964 * using non-realtime signals. 1965 * 1966 * TODO: Test whether ARM needs a restorer 1967 */ 1968 if (!(a.sa_flags & SA_RESTORER)) { 1969 a.sa_flags |= SA_RESTORER; 1970 a.sa_restorer = (a.sa_flags & SA_SIGINFO) 1971 ? LSS_NAME(restore_rt)() : LSS_NAME(restore)(); 1972 } 1973 #endif 1974 } 1975 rc = LSS_NAME(rt_sigaction)(signum, act ? &a : act, oldact, 1976 (KERNEL_NSIG+7)/8); 1977 if (rc < 0 && LSS_ERRNO == ENOSYS) { 1978 struct kernel_old_sigaction oa, ooa, *ptr_a = &oa, *ptr_oa = &ooa; 1979 if (!act) { 1980 ptr_a = NULL; 1981 } else { 1982 oa.sa_handler_ = act->sa_handler_; 1983 memcpy(&oa.sa_mask, &act->sa_mask, sizeof(oa.sa_mask)); 1984 #ifndef __mips__ 1985 oa.sa_restorer = act->sa_restorer; 1986 #endif 1987 oa.sa_flags = act->sa_flags; 1988 } 1989 if (!oldact) { 1990 ptr_oa = NULL; 1991 } 1992 LSS_ERRNO = old_errno; 1993 rc = LSS_NAME(_sigaction)(signum, ptr_a, ptr_oa); 1994 if (rc == 0 && oldact) { 1995 if (act) { 1996 memcpy(oldact, act, sizeof(*act)); 1997 } else { 1998 memset(oldact, 0, sizeof(*oldact)); 1999 } 2000 oldact->sa_handler_ = ptr_oa->sa_handler_; 2001 oldact->sa_flags = ptr_oa->sa_flags; 2002 memcpy(&oldact->sa_mask, &ptr_oa->sa_mask, sizeof(ptr_oa->sa_mask)); 2003 #ifndef __mips__ 2004 oldact->sa_restorer = ptr_oa->sa_restorer; 2005 #endif 2006 } 2007 } 2008 return rc; 2009 } 2010 2011 LSS_INLINE int LSS_NAME(sigprocmask)(int how, 2012 const struct kernel_sigset_t *set, 2013 struct kernel_sigset_t *oldset) { 2014 int olderrno = LSS_ERRNO; 2015 int rc = LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8); 2016 if (rc < 0 && LSS_ERRNO == ENOSYS) { 2017 LSS_ERRNO = olderrno; 2018 if (oldset) { 2019 LSS_NAME(sigemptyset)(oldset); 2020 } 2021 rc = LSS_NAME(_sigprocmask)(how, 2022 set ? &set->sig[0] : NULL, 2023 oldset ? &oldset->sig[0] : NULL); 2024 } 2025 return rc; 2026 } 2027 #endif 2028 #if defined(__PPC__) 2029 #undef LSS_SC_LOADARGS_0 2030 #define LSS_SC_LOADARGS_0(dummy...) 2031 #undef LSS_SC_LOADARGS_1 2032 #define LSS_SC_LOADARGS_1(arg1) \ 2033 __sc_4 = (unsigned long) (arg1) 2034 #undef LSS_SC_LOADARGS_2 2035 #define LSS_SC_LOADARGS_2(arg1, arg2) \ 2036 LSS_SC_LOADARGS_1(arg1); \ 2037 __sc_5 = (unsigned long) (arg2) 2038 #undef LSS_SC_LOADARGS_3 2039 #define LSS_SC_LOADARGS_3(arg1, arg2, arg3) \ 2040 LSS_SC_LOADARGS_2(arg1, arg2); \ 2041 __sc_6 = (unsigned long) (arg3) 2042 #undef LSS_SC_LOADARGS_4 2043 #define LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4) \ 2044 LSS_SC_LOADARGS_3(arg1, arg2, arg3); \ 2045 __sc_7 = (unsigned long) (arg4) 2046 #undef LSS_SC_LOADARGS_5 2047 #define LSS_SC_LOADARGS_5(arg1, arg2, arg3, arg4, arg5) \ 2048 LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4); \ 2049 __sc_8 = (unsigned long) (arg5) 2050 #undef LSS_SC_BODY 2051 #define LSS_SC_BODY(nr, type, opt, args...) \ 2052 long __sc_ret, __sc_err; \ 2053 { \ 2054 register unsigned long __sc_0 __asm__ ("r0") = __NR_socketcall; \ 2055 register unsigned long __sc_3 __asm__ ("r3") = opt; \ 2056 register unsigned long __sc_4 __asm__ ("r4"); \ 2057 register unsigned long __sc_5 __asm__ ("r5"); \ 2058 register unsigned long __sc_6 __asm__ ("r6"); \ 2059 register unsigned long __sc_7 __asm__ ("r7"); \ 2060 register unsigned long __sc_8 __asm__ ("r8"); \ 2061 LSS_SC_LOADARGS_##nr(args); \ 2062 __asm__ __volatile__ \ 2063 ("stwu 1, -48(1)\n\t" \ 2064 "stw 4, 20(1)\n\t" \ 2065 "stw 5, 24(1)\n\t" \ 2066 "stw 6, 28(1)\n\t" \ 2067 "stw 7, 32(1)\n\t" \ 2068 "stw 8, 36(1)\n\t" \ 2069 "addi 4, 1, 20\n\t" \ 2070 "sc\n\t" \ 2071 "mfcr %0" \ 2072 : "=&r" (__sc_0), \ 2073 "=&r" (__sc_3), "=&r" (__sc_4), \ 2074 "=&r" (__sc_5), "=&r" (__sc_6), \ 2075 "=&r" (__sc_7), "=&r" (__sc_8) \ 2076 : LSS_ASMINPUT_##nr \ 2077 : "cr0", "ctr", "memory"); \ 2078 __sc_ret = __sc_3; \ 2079 __sc_err = __sc_0; \ 2080 } \ 2081 LSS_RETURN(type, __sc_ret, __sc_err) 2082 2083 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) { 2084 LSS_SC_BODY(3, int, 1, domain, type, protocol); 2085 } 2086 #endif 2087 #if defined(__i386__) || \ 2088 (defined(__arm__) && !defined(__ARM_EABI__)) || \ 2089 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) 2090 2091 /* See sys_socketcall in net/socket.c in kernel source. 2092 * It de-multiplexes on its first arg and unpacks the arglist 2093 * array in its second arg. 2094 */ 2095 LSS_INLINE _syscall2(long, socketcall, int, c, unsigned long*, a) 2096 2097 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) { 2098 unsigned long args[3] = { 2099 (unsigned long) domain, 2100 (unsigned long) type, 2101 (unsigned long) protocol 2102 }; 2103 return LSS_NAME(socketcall)(1, args); 2104 } 2105 #elif defined(__ARM_EABI__) 2106 LSS_INLINE _syscall3(int, socket, int, d, 2107 int, t, int, p) 2108 #endif 2109 #if defined(__i386__) || defined(__PPC__) || \ 2110 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) 2111 LSS_INLINE _syscall3(pid_t, waitpid, pid_t, p, 2112 int*, s, int, o) 2113 #endif 2114 #if defined(__mips__) 2115 /* sys_pipe() on MIPS has non-standard calling conventions, as it returns 2116 * both file handles through CPU registers. 2117 */ 2118 LSS_INLINE int LSS_NAME(pipe)(int *p) { 2119 register unsigned long __v0 __asm__("$2") = __NR_pipe; 2120 register unsigned long __v1 __asm__("$3"); 2121 register unsigned long __r7 __asm__("$7"); 2122 __asm__ __volatile__ ("syscall\n" 2123 : "=&r"(__v0), "=&r"(__v1), "+r" (__r7) 2124 : "0"(__v0) 2125 : "$8", "$9", "$10", "$11", "$12", 2126 "$13", "$14", "$15", "$24", "memory"); 2127 if (__r7) { 2128 LSS_ERRNO = __v0; 2129 return -1; 2130 } else { 2131 p[0] = __v0; 2132 p[1] = __v1; 2133 return 0; 2134 } 2135 } 2136 #else 2137 LSS_INLINE _syscall1(int, pipe, int *, p) 2138 #endif 2139 2140 LSS_INLINE pid_t LSS_NAME(gettid)() { 2141 pid_t tid = LSS_NAME(_gettid)(); 2142 if (tid != -1) { 2143 return tid; 2144 } 2145 return LSS_NAME(getpid)(); 2146 } 2147 2148 LSS_INLINE void *LSS_NAME(mremap)(void *old_address, size_t old_size, 2149 size_t new_size, int flags, ...) { 2150 va_list ap; 2151 void *new_address, *rc; 2152 va_start(ap, flags); 2153 new_address = va_arg(ap, void *); 2154 rc = LSS_NAME(_mremap)(old_address, old_size, new_size, 2155 flags, new_address); 2156 va_end(ap); 2157 return rc; 2158 } 2159 2160 LSS_INLINE int LSS_NAME(ptrace_detach)(pid_t pid) { 2161 /* PTRACE_DETACH can sometimes forget to wake up the tracee and it 2162 * then sends job control signals to the real parent, rather than to 2163 * the tracer. We reduce the risk of this happening by starting a 2164 * whole new time slice, and then quickly sending a SIGCONT signal 2165 * right after detaching from the tracee. 2166 */ 2167 int rc, err; 2168 LSS_NAME(sched_yield)(); 2169 rc = LSS_NAME(ptrace)(PTRACE_DETACH, pid, (void *)0, (void *)0); 2170 err = LSS_ERRNO; 2171 LSS_NAME(kill)(pid, SIGCONT); 2172 LSS_ERRNO = err; 2173 return rc; 2174 } 2175#endif 2176 2177#if defined(__cplusplus) && !defined(SYS_CPLUSPLUS) 2178} 2179#endif 2180 2181#endif 2182#endif 2183