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