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