1 /*
2  *  linux_syscall_support.h
3  *  Avida
4  *
5  *  Added by David on 10/14/09.
6  *  Copyright 2009-2011 Michigan State University. All rights reserved.
7  *
8  *
9  *  This file is part of Avida.
10  *
11  *  Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
12  *  as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
13  *
14  *  Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License along with Avida.
18  *  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /* Copyright (c) 2005-2008, Google Inc.
23  * All rights reserved.
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions are
27  * met:
28  *
29  *     * Redistributions of source code must retain the above copyright
30  * notice, this list of conditions and the following disclaimer.
31  *     * Redistributions in binary form must reproduce the above
32  * copyright notice, this list of conditions and the following disclaimer
33  * in the documentation and/or other materials provided with the
34  * distribution.
35  *     * Neither the name of Google Inc. nor the names of its
36  * contributors may be used to endorse or promote products derived from
37  * this software without specific prior written permission.
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
40  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
41  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
42  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
43  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
46  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
47  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
48  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
49  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50  *
51  * ---
52  * Author: Markus Gutschke
53  */
54 
55 /* This file includes Linux-specific support functions common to the
56  * coredumper and the thread lister; primarily, this is a collection
57  * of direct system calls, and a couple of symbols missing from
58  * standard header files.
59  * There are a few options that the including file can set to control
60  * the behavior of this file:
61  *
62  * SYS_CPLUSPLUS:
63  *   The entire header file will normally be wrapped in 'extern "C" { }",
64  *   making it suitable for compilation as both C and C++ source. If you
65  *   do not want to do this, you can set the SYS_CPLUSPLUS macro to inhibit
66  *   the wrapping. N.B. doing so will suppress inclusion of all prerequisite
67  *   system header files, too. It is the caller's responsibility to provide
68  *   the necessary definitions.
69  *
70  * SYS_ERRNO:
71  *   All system calls will update "errno" unless overriden by setting the
72  *   SYS_ERRNO macro prior to including this file. SYS_ERRNO should be
73  *   an l-value.
74  *
75  * SYS_INLINE:
76  *   New symbols will be defined "static inline", unless overridden by
77  *   the SYS_INLINE macro.
78  *
79  * SYS_LINUX_SYSCALL_SUPPORT_H
80  *   This macro is used to avoid multiple inclusions of this header file.
81  *   If you need to include this file more than once, make sure to
82  *   unset SYS_LINUX_SYSCALL_SUPPORT_H before each inclusion.
83  *
84  * SYS_PREFIX:
85  *   New system calls will have a prefix of "sys_" unless overridden by
86  *   the SYS_PREFIX macro. Valid values for this macro are [0..9] which
87  *   results in prefixes "sys[0..9]_". It is also possible to set this
88  *   macro to -1, which avoids all prefixes.
89  *
90  * This file defines a few internal symbols that all start with "LSS_".
91  * Do not access these symbols from outside this file. They are not part
92  * of the supported API.
93  */
94 #ifndef SYS_LINUX_SYSCALL_SUPPORT_H
95 #define SYS_LINUX_SYSCALL_SUPPORT_H
96 
97 /* We currently only support x86-32, x86-64, ARM, MIPS, and PPC on Linux.
98  * Porting to other related platforms should not be difficult.
99  */
100 #if (defined(__i386__) || defined(__x86_64__) || defined(__ARM_ARCH_3__) ||   \
101      defined(__mips__) || defined(__PPC__)) && defined(__linux)
102 
103 #ifndef SYS_CPLUSPLUS
104 #ifdef __cplusplus
105 /* Some system header files in older versions of gcc neglect to properly
106  * handle being included from C++. As it appears to be harmless to have
107  * multiple nested 'extern "C"' blocks, just add another one here.
108  */
109 extern "C" {
110 #endif
111 
112 #include <errno.h>
113 #include <signal.h>
114 #include <stdarg.h>
115 #include <string.h>
116 #include <sys/ptrace.h>
117 #include <sys/resource.h>
118 #include <sys/time.h>
119 #include <sys/types.h>
120 #include <syscall.h>
121 #include <unistd.h>
122 #include <linux/unistd.h>
123 #include <endian.h>
124 
125 #ifdef __mips__
126 /* Include definitions of the ABI currently in use.                          */
127 #include <sgidefs.h>
128 #endif
129 
130 #endif
131 
132 /* As glibc often provides subtly incompatible data structures (and implicit
133  * wrapper functions that convert them), we provide our own kernel data
134  * structures for use by the system calls.
135  * These structures have been developed by using Linux 2.6.23 headers for
136  * reference. Note though, we do not care about exact API compatibility
137  * with the kernel, and in fact the kernel often does not have a single
138  * API that works across architectures. Instead, we try to mimic the glibc
139  * API where reasonable, and only guarantee ABI compatibility with the
140  * kernel headers.
141  * Most notably, here are a few changes that were made to the structures
142  * defined by kernel headers:
143  *
144  * - we only define structures, but not symbolic names for kernel data
145  *   types. For the latter, we directly use the native C datatype
146  *   (i.e. "unsigned" instead of "mode_t").
147  * - in a few cases, it is possible to define identical structures for
148  *   both 32bit (e.g. i386) and 64bit (e.g. x86-64) platforms by
149  *   standardizing on the 64bit version of the data types. In particular,
150  *   this means that we use "unsigned" where the 32bit headers say
151  *   "unsigned long".
152  * - overall, we try to minimize the number of cases where we need to
153  *   conditionally define different structures.
154  * - the "struct kernel_sigaction" class of structures have been
155  *   modified to more closely mimic glibc's API by introducing an
156  *   anonymous union for the function pointer.
157  * - a small number of field names had to have an underscore appended to
158  *   them, because glibc defines a global macro by the same name.
159  */
160 
161 /* include/linux/dirent.h                                                    */
162 struct kernel_dirent64 {
163   unsigned long long d_ino;
164   long long          d_off;
165   unsigned short     d_reclen;
166   unsigned char      d_type;
167   char               d_name[256];
168 };
169 
170 /* include/linux/dirent.h                                                    */
171 struct kernel_dirent {
172   long               d_ino;
173   long               d_off;
174   unsigned short     d_reclen;
175   char               d_name[256];
176 };
177 
178 /* include/linux/uio.h                                                       */
179 struct kernel_iovec {
180   void               *iov_base;
181   unsigned long      iov_len;
182 };
183 
184 /* include/linux/socket.h                                                    */
185 struct kernel_msghdr {
186   void               *msg_name;
187   int                msg_namelen;
188   struct kernel_iovec*msg_iov;
189   unsigned long      msg_iovlen;
190   void               *msg_control;
191   unsigned long      msg_controllen;
192   unsigned           msg_flags;
193 };
194 
195 /* include/asm-generic/poll.h                                                */
196 struct kernel_pollfd {
197   int                fd;
198   short              events;
199   short              revents;
200 };
201 
202 /* include/linux/resource.h                                                  */
203 struct kernel_rlimit {
204   unsigned long      rlim_cur;
205   unsigned long      rlim_max;
206 };
207 
208 /* include/linux/time.h                                                      */
209 struct kernel_timespec {
210   long               tv_sec;
211   long               tv_nsec;
212 };
213 
214 /* include/linux/time.h                                                      */
215 struct kernel_timeval {
216   long               tv_sec;
217   long               tv_usec;
218 };
219 
220 /* include/linux/resource.h                                                  */
221 struct kernel_rusage {
222   struct kernel_timeval ru_utime;
223   struct kernel_timeval ru_stime;
224   long               ru_maxrss;
225   long               ru_ixrss;
226   long               ru_idrss;
227   long               ru_isrss;
228   long               ru_minflt;
229   long               ru_majflt;
230   long               ru_nswap;
231   long               ru_inblock;
232   long               ru_oublock;
233   long               ru_msgsnd;
234   long               ru_msgrcv;
235   long               ru_nsignals;
236   long               ru_nvcsw;
237   long               ru_nivcsw;
238 };
239 
240 struct siginfo;
241 #if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__PPC__)
242 
243 /* include/asm-{arm,i386,mips,ppc}/signal.h                                  */
244 struct kernel_old_sigaction {
245   union {
246     void             (*sa_handler_)(int);
247     void             (*sa_sigaction_)(int, struct siginfo *, void *);
248   };
249   unsigned long      sa_mask;
250   unsigned long      sa_flags;
251   void               (*sa_restorer)(void);
252 } __attribute__((packed,aligned(4)));
253 #elif (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
254   #define kernel_old_sigaction kernel_sigaction
255 #endif
256 
257 /* Some kernel functions (e.g. sigaction() in 2.6.23) require that the
258  * exactly match the size of the signal set, even though the API was
259  * intended to be extensible. We define our own KERNEL_NSIG to deal with
260  * this.
261  * Please note that glibc provides signals [1.._NSIG-1], whereas the
262  * kernel (and this header) provides the range [1..KERNEL_NSIG]. The
263  * actual number of signals is obviously the same, but the constants
264  * differ by one.
265  */
266 #ifdef __mips__
267 #define KERNEL_NSIG 128
268 #else
269 #define KERNEL_NSIG  64
270 #endif
271 
272 /* include/asm-{arm,i386,mips,x86_64}/signal.h                               */
273 struct kernel_sigset_t {
274   unsigned long sig[(KERNEL_NSIG + 8*sizeof(unsigned long) - 1)/
275                     (8*sizeof(unsigned long))];
276 };
277 
278 /* include/asm-{arm,i386,mips,x86_64,ppc}/signal.h                           */
279 struct kernel_sigaction {
280 #ifdef __mips__
281   unsigned long      sa_flags;
282   union {
283     void             (*sa_handler_)(int);
284     void             (*sa_sigaction_)(int, struct siginfo *, void *);
285   };
286   struct kernel_sigset_t sa_mask;
287 #else
288   union {
289     void             (*sa_handler_)(int);
290     void             (*sa_sigaction_)(int, struct siginfo *, void *);
291   };
292   unsigned long      sa_flags;
293   void               (*sa_restorer)(void);
294   struct kernel_sigset_t sa_mask;
295 #endif
296 };
297 
298 /* include/linux/socket.h                                                    */
299 struct kernel_sockaddr {
300   unsigned short     sa_family;
301   char               sa_data[14];
302 };
303 
304 /* include/asm-{arm,i386,mips,ppc}/stat.h                                    */
305 #ifdef __mips__
306 #if _MIPS_SIM == _MIPS_SIM_ABI64
307 struct kernel_stat {
308 #else
309 struct kernel_stat64 {
310 #endif
311   unsigned           st_dev;
312   unsigned           __pad0[3];
313   unsigned long long st_ino;
314   unsigned           st_mode;
315   unsigned           st_nlink;
316   unsigned           st_uid;
317   unsigned           st_gid;
318   unsigned           st_rdev;
319   unsigned           __pad1[3];
320   long long          st_size;
321   unsigned           st_atime_;
322   unsigned           st_atime_nsec_;
323   unsigned           st_mtime_;
324   unsigned           st_mtime_nsec_;
325   unsigned           st_ctime_;
326   unsigned           st_ctime_nsec_;
327   unsigned           st_blksize;
328   unsigned           __pad2;
329   unsigned long long st_blocks;
330 };
331 #elif defined __PPC__
332 struct kernel_stat64 {
333   unsigned long long st_dev;
334   unsigned long long st_ino;
335   unsigned           st_mode;
336   unsigned           st_nlink;
337   unsigned           st_uid;
338   unsigned           st_gid;
339   unsigned long long st_rdev;
340   unsigned short int __pad2;
341   long long          st_size;
342   long               st_blksize;
343   long long          st_blocks;
344   long               st_atime_;
345   unsigned long      st_atime_nsec_;
346   long               st_mtime_;
347   unsigned long      st_mtime_nsec_;
348   long               st_ctime_;
349   unsigned long      st_ctime_nsec_;
350   unsigned long      __unused4;
351   unsigned long      __unused5;
352 };
353 #else
354 struct kernel_stat64 {
355   unsigned long long st_dev;
356   unsigned char      __pad0[4];
357   unsigned           __st_ino;
358   unsigned           st_mode;
359   unsigned           st_nlink;
360   unsigned           st_uid;
361   unsigned           st_gid;
362   unsigned long long st_rdev;
363   unsigned char      __pad3[4];
364   long long          st_size;
365   unsigned           st_blksize;
366   unsigned long long st_blocks;
367   unsigned           st_atime_;
368   unsigned           st_atime_nsec_;
369   unsigned           st_mtime_;
370   unsigned           st_mtime_nsec_;
371   unsigned           st_ctime_;
372   unsigned           st_ctime_nsec_;
373   unsigned long long st_ino;
374 };
375 #endif
376 
377 /* include/asm-{arm,i386,mips,x86_64,ppc}/stat.h                             */
378 #if defined(__i386__) || defined(__ARM_ARCH_3__)
379 struct kernel_stat {
380   /* The kernel headers suggest that st_dev and st_rdev should be 32bit
381    * quantities encoding 12bit major and 20bit minor numbers in an interleaved
382    * format. In reality, we do not see useful data in the top bits. So,
383    * we'll leave the padding in here, until we find a better solution.
384    */
385   unsigned short     st_dev;
386   short              pad1;
387   unsigned           st_ino;
388   unsigned short     st_mode;
389   unsigned short     st_nlink;
390   unsigned short     st_uid;
391   unsigned short     st_gid;
392   unsigned short     st_rdev;
393   short              pad2;
394   unsigned           st_size;
395   unsigned           st_blksize;
396   unsigned           st_blocks;
397   unsigned           st_atime_;
398   unsigned           st_atime_nsec_;
399   unsigned           st_mtime_;
400   unsigned           st_mtime_nsec_;
401   unsigned           st_ctime_;
402   unsigned           st_ctime_nsec_;
403   unsigned           __unused4;
404   unsigned           __unused5;
405 };
406 #elif defined(__x86_64__)
407 struct kernel_stat {
408   unsigned long      st_dev;
409   unsigned long      st_ino;
410   unsigned long      st_nlink;
411   unsigned           st_mode;
412   unsigned           st_uid;
413   unsigned           st_gid;
414   unsigned           __pad0;
415   unsigned long      st_rdev;
416   long               st_size;
417   long               st_blksize;
418   long               st_blocks;
419   unsigned long      st_atime_;
420   unsigned long      st_atime_nsec_;
421   unsigned long      st_mtime_;
422   unsigned long      st_mtime_nsec_;
423   unsigned long      st_ctime_;
424   unsigned long      st_ctime_nsec_;
425   long               __unused[3];
426 };
427 #elif defined(__PPC__)
428 struct kernel_stat {
429   unsigned           st_dev;
430   unsigned long      st_ino;      // ino_t
431   unsigned long      st_mode;     // mode_t
432   unsigned short     st_nlink;    // nlink_t
433   unsigned           st_uid;      // uid_t
434   unsigned           st_gid;      // gid_t
435   unsigned           st_rdev;
436   long               st_size;     // off_t
437   unsigned long      st_blksize;
438   unsigned long      st_blocks;
439   unsigned long      st_atime_;
440   unsigned long      st_atime_nsec_;
441   unsigned long      st_mtime_;
442   unsigned long      st_mtime_nsec_;
443   unsigned long      st_ctime_;
444   unsigned long      st_ctime_nsec_;
445   unsigned long      __unused4;
446   unsigned long      __unused5;
447 };
448 #elif (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64)
449 struct kernel_stat {
450   unsigned           st_dev;
451   int                st_pad1[3];
452   unsigned           st_ino;
453   unsigned           st_mode;
454   unsigned           st_nlink;
455   unsigned           st_uid;
456   unsigned           st_gid;
457   unsigned           st_rdev;
458   int                st_pad2[2];
459   long               st_size;
460   int                st_pad3;
461   long               st_atime_;
462   long               st_atime_nsec_;
463   long               st_mtime_;
464   long               st_mtime_nsec_;
465   long               st_ctime_;
466   long               st_ctime_nsec_;
467   int                st_blksize;
468   int                st_blocks;
469   int                st_pad4[14];
470 };
471 #endif
472 
473 /* include/asm-{arm,i386,mips,x86_64,ppc}/statfs.h                           */
474 #ifdef __mips__
475 #if _MIPS_SIM != _MIPS_SIM_ABI64
476 struct kernel_statfs64 {
477   unsigned long      f_type;
478   unsigned long      f_bsize;
479   unsigned long      f_frsize;
480   unsigned long      __pad;
481   unsigned long long f_blocks;
482   unsigned long long f_bfree;
483   unsigned long long f_files;
484   unsigned long long f_ffree;
485   unsigned long long f_bavail;
486   struct { int val[2]; } f_fsid;
487   unsigned long      f_namelen;
488   unsigned long      f_spare[6];
489 };
490 #endif
491 #elif !defined(__x86_64__)
492 struct kernel_statfs64 {
493   unsigned long      f_type;
494   unsigned long      f_bsize;
495   unsigned long long f_blocks;
496   unsigned long long f_bfree;
497   unsigned long long f_bavail;
498   unsigned long long f_files;
499   unsigned long long f_ffree;
500   struct { int val[2]; } f_fsid;
501   unsigned long      f_namelen;
502   unsigned long      f_frsize;
503   unsigned long      f_spare[5];
504 };
505 #endif
506 
507 /* include/asm-{arm,i386,mips,x86_64,ppc,generic}/statfs.h                   */
508 #ifdef __mips__
509 struct kernel_statfs {
510   long               f_type;
511   long               f_bsize;
512   long               f_frsize;
513   long               f_blocks;
514   long               f_bfree;
515   long               f_files;
516   long               f_ffree;
517   long               f_bavail;
518   struct { int val[2]; } f_fsid;
519   long               f_namelen;
520   long               f_spare[6];
521 };
522 #else
523 struct kernel_statfs {
524   /* x86_64 actually defines all these fields as signed, whereas all other  */
525   /* platforms define them as unsigned. Leaving them at unsigned should not */
526   /* cause any problems.                                                    */
527   unsigned long      f_type;
528   unsigned long      f_bsize;
529   unsigned long      f_blocks;
530   unsigned long      f_bfree;
531   unsigned long      f_bavail;
532   unsigned long      f_files;
533   unsigned long      f_ffree;
534   struct { int val[2]; } f_fsid;
535   unsigned long      f_namelen;
536   unsigned long      f_frsize;
537   unsigned long      f_spare[5];
538 };
539 #endif
540 
541 
542 /* Definitions missing from the standard header files                        */
543 #ifndef O_DIRECTORY
544 #if defined(__ARM_ARCH_3__)
545 #define O_DIRECTORY             0040000
546 #else
547 #define O_DIRECTORY             0200000
548 #endif
549 #endif
550 #ifndef NT_PRXFPREG
551 #define NT_PRXFPREG             0x46e62b7f
552 #endif
553 #ifndef PTRACE_GETFPXREGS
554 #define PTRACE_GETFPXREGS       ((enum __ptrace_request)18)
555 #endif
556 #ifndef PR_GET_DUMPABLE
557 #define PR_GET_DUMPABLE         3
558 #endif
559 #ifndef PR_SET_DUMPABLE
560 #define PR_SET_DUMPABLE         4
561 #endif
562 #ifndef AT_FDCWD
563 #define AT_FDCWD                (-100)
564 #endif
565 #ifndef AT_SYMLINK_NOFOLLOW
566 #define AT_SYMLINK_NOFOLLOW     0x100
567 #endif
568 #ifndef AT_REMOVEDIR
569 #define AT_REMOVEDIR            0x200
570 #endif
571 #ifndef MREMAP_FIXED
572 #define MREMAP_FIXED            2
573 #endif
574 #ifndef SA_RESTORER
575 #define SA_RESTORER             0x04000000
576 #endif
577 #ifndef CPUCLOCK_PROF
578 #define CPUCLOCK_PROF           0
579 #endif
580 #ifndef CPUCLOCK_VIRT
581 #define CPUCLOCK_VIRT           1
582 #endif
583 #ifndef CPUCLOCK_SCHED
584 #define CPUCLOCK_SCHED          2
585 #endif
586 #ifndef CPUCLOCK_PERTHREAD_MASK
587 #define CPUCLOCK_PERTHREAD_MASK 4
588 #endif
589 #ifndef MAKE_PROCESS_CPUCLOCK
590 #define MAKE_PROCESS_CPUCLOCK(pid, clock)                                     \
591         ((~(int)(pid) << 3) | (int)(clock))
592 #endif
593 #ifndef MAKE_THREAD_CPUCLOCK
594 #define MAKE_THREAD_CPUCLOCK(tid, clock)                                      \
595         ((~(int)(tid) << 3) | (int)((clock) | CPUCLOCK_PERTHREAD_MASK))
596 #endif
597 
598 #if defined(__i386__)
599 #ifndef __NR_setresuid
600 #define __NR_setresuid          164
601 #define __NR_setresgid          170
602 #endif
603 #ifndef __NR_rt_sigaction
604 #define __NR_rt_sigaction       174
605 #define __NR_rt_sigprocmask     175
606 #define __NR_rt_sigpending      176
607 #define __NR_rt_sigsuspend      179
608 #endif
609 #ifndef __NR_pread64
610 #define __NR_pread64            180
611 #endif
612 #ifndef __NR_pwrite64
613 #define __NR_pwrite64           181
614 #endif
615 #ifndef __NR_ugetrlimit
616 #define __NR_ugetrlimit         191
617 #endif
618 #ifndef __NR_stat64
619 #define __NR_stat64             195
620 #endif
621 #ifndef __NR_fstat64
622 #define __NR_fstat64            197
623 #endif
624 #ifndef __NR_setresuid32
625 #define __NR_setresuid32        208
626 #define __NR_setresgid32        210
627 #endif
628 #ifndef __NR_setfsuid32
629 #define __NR_setfsuid32         215
630 #define __NR_setfsgid32         216
631 #endif
632 #ifndef __NR_getdents64
633 #define __NR_getdents64         220
634 #endif
635 #ifndef __NR_gettid
636 #define __NR_gettid             224
637 #endif
638 #ifndef __NR_readahead
639 #define __NR_readahead          225
640 #endif
641 #ifndef __NR_setxattr
642 #define __NR_setxattr           226
643 #endif
644 #ifndef __NR_lsetxattr
645 #define __NR_lsetxattr          227
646 #endif
647 #ifndef __NR_getxattr
648 #define __NR_getxattr           229
649 #endif
650 #ifndef __NR_lgetxattr
651 #define __NR_lgetxattr          230
652 #endif
653 #ifndef __NR_listxattr
654 #define __NR_listxattr          232
655 #endif
656 #ifndef __NR_llistxattr
657 #define __NR_llistxattr         233
658 #endif
659 #ifndef __NR_futex
660 #define __NR_futex              240
661 #endif
662 #ifndef __NR_sched_setaffinity
663 #define __NR_sched_setaffinity  241
664 #define __NR_sched_getaffinity  242
665 #endif
666 #ifndef __NR_set_tid_address
667 #define __NR_set_tid_address    258
668 #endif
669 #ifndef __NR_clock_gettime
670 #define __NR_clock_gettime      265
671 #endif
672 #ifndef __NR_clock_getres
673 #define __NR_clock_getres       266
674 #endif
675 #ifndef __NR_statfs64
676 #define __NR_statfs64           268
677 #endif
678 #ifndef __NR_fstatfs64
679 #define __NR_fstatfs64          269
680 #endif
681 #ifndef __NR_fadvise64_64
682 #define __NR_fadvise64_64       272
683 #endif
684 #ifndef __NR_ioprio_set
685 #define __NR_ioprio_set         289
686 #endif
687 #ifndef __NR_ioprio_get
688 #define __NR_ioprio_get         290
689 #endif
690 #ifndef __NR_openat
691 #define __NR_openat             295
692 #endif
693 #ifndef __NR_fstatat64
694 #define __NR_fstatat64          300
695 #endif
696 #ifndef __NR_unlinkat
697 #define __NR_unlinkat           301
698 #endif
699 #ifndef __NR_move_pages
700 #define __NR_move_pages         317
701 #endif
702 #ifndef __NR_getcpu
703 #define __NR_getcpu             318
704 #endif
705 #ifndef __NR_fallocate
706 #define __NR_fallocate          324
707 #endif
708 /* End of i386 definitions                                                   */
709 #elif defined(__ARM_ARCH_3__)
710 #ifndef __NR_setresuid
711 #define __NR_setresuid          (__NR_SYSCALL_BASE + 164)
712 #define __NR_setresgid          (__NR_SYSCALL_BASE + 170)
713 #endif
714 #ifndef __NR_rt_sigaction
715 #define __NR_rt_sigaction       (__NR_SYSCALL_BASE + 174)
716 #define __NR_rt_sigprocmask     (__NR_SYSCALL_BASE + 175)
717 #define __NR_rt_sigpending      (__NR_SYSCALL_BASE + 176)
718 #define __NR_rt_sigsuspend      (__NR_SYSCALL_BASE + 179)
719 #endif
720 #ifndef __NR_pread64
721 #define __NR_pread64            (__NR_SYSCALL_BASE + 180)
722 #endif
723 #ifndef __NR_pwrite64
724 #define __NR_pwrite64           (__NR_SYSCALL_BASE + 181)
725 #endif
726 #ifndef __NR_ugetrlimit
727 #define __NR_ugetrlimit         (__NR_SYSCALL_BASE + 191)
728 #endif
729 #ifndef __NR_stat64
730 #define __NR_stat64             (__NR_SYSCALL_BASE + 195)
731 #endif
732 #ifndef __NR_fstat64
733 #define __NR_fstat64            (__NR_SYSCALL_BASE + 197)
734 #endif
735 #ifndef __NR_setresuid32
736 #define __NR_setresuid32        (__NR_SYSCALL_BASE + 208)
737 #define __NR_setresgid32        (__NR_SYSCALL_BASE + 210)
738 #endif
739 #ifndef __NR_setfsuid32
740 #define __NR_setfsuid32         (__NR_SYSCALL_BASE + 215)
741 #define __NR_setfsgid32         (__NR_SYSCALL_BASE + 216)
742 #endif
743 #ifndef __NR_getdents64
744 #define __NR_getdents64         (__NR_SYSCALL_BASE + 217)
745 #endif
746 #ifndef __NR_gettid
747 #define __NR_gettid             (__NR_SYSCALL_BASE + 224)
748 #endif
749 #ifndef __NR_readahead
750 #define __NR_readahead          (__NR_SYSCALL_BASE + 225)
751 #endif
752 #ifndef __NR_setxattr
753 #define __NR_setxattr           (__NR_SYSCALL_BASE + 226)
754 #endif
755 #ifndef __NR_lsetxattr
756 #define __NR_lsetxattr          (__NR_SYSCALL_BASE + 227)
757 #endif
758 #ifndef __NR_getxattr
759 #define __NR_getxattr           (__NR_SYSCALL_BASE + 229)
760 #endif
761 #ifndef __NR_lgetxattr
762 #define __NR_lgetxattr          (__NR_SYSCALL_BASE + 230)
763 #endif
764 #ifndef __NR_listxattr
765 #define __NR_listxattr          (__NR_SYSCALL_BASE + 232)
766 #endif
767 #ifndef __NR_llistxattr
768 #define __NR_llistxattr         (__NR_SYSCALL_BASE + 233)
769 #endif
770 #ifndef __NR_futex
771 #define __NR_futex              (__NR_SYSCALL_BASE + 240)
772 #endif
773 #ifndef __NR_sched_setaffinity
774 #define __NR_sched_setaffinity  (__NR_SYSCALL_BASE + 241)
775 #define __NR_sched_getaffinity  (__NR_SYSCALL_BASE + 242)
776 #endif
777 #ifndef __NR_set_tid_address
778 #define __NR_set_tid_address    (__NR_SYSCALL_BASE + 256)
779 #endif
780 #ifndef __NR_clock_gettime
781 #define __NR_clock_gettime      (__NR_SYSCALL_BASE + 263)
782 #endif
783 #ifndef __NR_clock_getres
784 #define __NR_clock_getres       (__NR_SYSCALL_BASE + 264)
785 #endif
786 #ifndef __NR_statfs64
787 #define __NR_statfs64           (__NR_SYSCALL_BASE + 266)
788 #endif
789 #ifndef __NR_fstatfs64
790 #define __NR_fstatfs64          (__NR_SYSCALL_BASE + 267)
791 #endif
792 #ifndef __NR_ioprio_set
793 #define __NR_ioprio_set         (__NR_SYSCALL_BASE + 314)
794 #endif
795 #ifndef __NR_ioprio_get
796 #define __NR_ioprio_get         (__NR_SYSCALL_BASE + 315)
797 #endif
798 #ifndef __NR_move_pages
799 #define __NR_move_pages         (__NR_SYSCALL_BASE + 344)
800 #endif
801 #ifndef __NR_getcpu
802 #define __NR_getcpu             (__NR_SYSCALL_BASE + 345)
803 #endif
804 /* End of ARM 3 definitions                                                  */
805 #elif defined(__x86_64__)
806 #ifndef __NR_pread64
807 #define __NR_pread64             17
808 #endif
809 #ifndef __NR_pwrite64
810 #define __NR_pwrite64            18
811 #endif
812 #ifndef __NR_setresuid
813 #define __NR_setresuid          117
814 #define __NR_setresgid          119
815 #endif
816 #ifndef __NR_gettid
817 #define __NR_gettid             186
818 #endif
819 #ifndef __NR_readahead
820 #define __NR_readahead          187
821 #endif
822 #ifndef __NR_setxattr
823 #define __NR_setxattr           188
824 #endif
825 #ifndef __NR_lsetxattr
826 #define __NR_lsetxattr          189
827 #endif
828 #ifndef __NR_getxattr
829 #define __NR_getxattr           191
830 #endif
831 #ifndef __NR_lgetxattr
832 #define __NR_lgetxattr          192
833 #endif
834 #ifndef __NR_listxattr
835 #define __NR_listxattr          194
836 #endif
837 #ifndef __NR_llistxattr
838 #define __NR_llistxattr         195
839 #endif
840 #ifndef __NR_futex
841 #define __NR_futex              202
842 #endif
843 #ifndef __NR_sched_setaffinity
844 #define __NR_sched_setaffinity  203
845 #define __NR_sched_getaffinity  204
846 #endif
847 #ifndef __NR_getdents64
848 #define __NR_getdents64         217
849 #endif
850 #ifndef __NR_set_tid_address
851 #define __NR_set_tid_address    218
852 #endif
853 #ifndef __NR_fadvise64
854 #define __NR_fadvise64          221
855 #endif
856 #ifndef __NR_clock_gettime
857 #define __NR_clock_gettime      228
858 #endif
859 #ifndef __NR_clock_getres
860 #define __NR_clock_getres       229
861 #endif
862 #ifndef __NR_ioprio_set
863 #define __NR_ioprio_set         251
864 #endif
865 #ifndef __NR_ioprio_get
866 #define __NR_ioprio_get         252
867 #endif
868 #ifndef __NR_openat
869 #define __NR_openat             257
870 #endif
871 #ifndef __NR_newfstatat
872 #define __NR_newfstatat         262
873 #endif
874 #ifndef __NR_unlinkat
875 #define __NR_unlinkat           263
876 #endif
877 #ifndef __NR_move_pages
878 #define __NR_move_pages         279
879 #endif
880 #ifndef __NR_fallocate
881 #define __NR_fallocate          285
882 #endif
883 /* End of x86-64 definitions                                                 */
884 #elif defined(__mips__)
885 #if _MIPS_SIM == _MIPS_SIM_ABI32
886 #ifndef __NR_setresuid
887 #define __NR_setresuid          (__NR_Linux + 185)
888 #define __NR_setresgid          (__NR_Linux + 190)
889 #endif
890 #ifndef __NR_rt_sigaction
891 #define __NR_rt_sigaction       (__NR_Linux + 194)
892 #define __NR_rt_sigprocmask     (__NR_Linux + 195)
893 #define __NR_rt_sigpending      (__NR_Linux + 196)
894 #define __NR_rt_sigsuspend      (__NR_Linux + 199)
895 #endif
896 #ifndef __NR_pread64
897 #define __NR_pread64            (__NR_Linux + 200)
898 #endif
899 #ifndef __NR_pwrite64
900 #define __NR_pwrite64           (__NR_Linux + 201)
901 #endif
902 #ifndef __NR_stat64
903 #define __NR_stat64             (__NR_Linux + 213)
904 #endif
905 #ifndef __NR_fstat64
906 #define __NR_fstat64            (__NR_Linux + 215)
907 #endif
908 #ifndef __NR_getdents64
909 #define __NR_getdents64         (__NR_Linux + 219)
910 #endif
911 #ifndef __NR_gettid
912 #define __NR_gettid             (__NR_Linux + 222)
913 #endif
914 #ifndef __NR_readahead
915 #define __NR_readahead          (__NR_Linux + 223)
916 #endif
917 #ifndef __NR_setxattr
918 #define __NR_setxattr           (__NR_Linux + 224)
919 #endif
920 #ifndef __NR_lsetxattr
921 #define __NR_lsetxattr          (__NR_Linux + 225)
922 #endif
923 #ifndef __NR_getxattr
924 #define __NR_getxattr           (__NR_Linux + 227)
925 #endif
926 #ifndef __NR_lgetxattr
927 #define __NR_lgetxattr          (__NR_Linux + 228)
928 #endif
929 #ifndef __NR_listxattr
930 #define __NR_listxattr          (__NR_Linux + 230)
931 #endif
932 #ifndef __NR_llistxattr
933 #define __NR_llistxattr         (__NR_Linux + 231)
934 #endif
935 #ifndef __NR_futex
936 #define __NR_futex              (__NR_Linux + 238)
937 #endif
938 #ifndef __NR_sched_setaffinity
939 #define __NR_sched_setaffinity  (__NR_Linux + 239)
940 #define __NR_sched_getaffinity  (__NR_Linux + 240)
941 #endif
942 #ifndef __NR_set_tid_address
943 #define __NR_set_tid_address    (__NR_Linux + 252)
944 #endif
945 #ifndef __NR_statfs64
946 #define __NR_statfs64           (__NR_Linux + 255)
947 #endif
948 #ifndef __NR_fstatfs64
949 #define __NR_fstatfs64          (__NR_Linux + 256)
950 #endif
951 #ifndef __NR_clock_gettime
952 #define __NR_clock_gettime      (__NR_Linux + 263)
953 #endif
954 #ifndef __NR_clock_getres
955 #define __NR_clock_getres       (__NR_Linux + 264)
956 #endif
957 #ifndef __NR_openat
958 #define __NR_openat             (__NR_Linux + 288)
959 #endif
960 #ifndef __NR_fstatat
961 #define __NR_fstatat            (__NR_Linux + 293)
962 #endif
963 #ifndef __NR_unlinkat
964 #define __NR_unlinkat           (__NR_Linux + 294)
965 #endif
966 #ifndef __NR_move_pages
967 #define __NR_move_pages         (__NR_Linux + 308)
968 #endif
969 #ifndef __NR_getcpu
970 #define __NR_getcpu             (__NR_Linux + 312)
971 #endif
972 #ifndef __NR_ioprio_set
973 #define __NR_ioprio_set         (__NR_Linux + 314)
974 #endif
975 #ifndef __NR_ioprio_get
976 #define __NR_ioprio_get         (__NR_Linux + 315)
977 #endif
978 /* End of MIPS (old 32bit API) definitions */
979 #elif  _MIPS_SIM == _MIPS_SIM_ABI64
980 #ifndef __NR_pread64
981 #define __NR_pread64            (__NR_Linux +  16)
982 #endif
983 #ifndef __NR_pwrite64
984 #define __NR_pwrite64           (__NR_Linux +  17)
985 #endif
986 #ifndef __NR_setresuid
987 #define __NR_setresuid          (__NR_Linux + 115)
988 #define __NR_setresgid          (__NR_Linux + 117)
989 #endif
990 #ifndef __NR_gettid
991 #define __NR_gettid             (__NR_Linux + 178)
992 #endif
993 #ifndef __NR_readahead
994 #define __NR_readahead          (__NR_Linux + 179)
995 #endif
996 #ifndef __NR_setxattr
997 #define __NR_setxattr           (__NR_Linux + 180)
998 #endif
999 #ifndef __NR_lsetxattr
1000 #define __NR_lsetxattr          (__NR_Linux + 181)
1001 #endif
1002 #ifndef __NR_getxattr
1003 #define __NR_getxattr           (__NR_Linux + 183)
1004 #endif
1005 #ifndef __NR_lgetxattr
1006 #define __NR_lgetxattr          (__NR_Linux + 184)
1007 #endif
1008 #ifndef __NR_listxattr
1009 #define __NR_listxattr          (__NR_Linux + 186)
1010 #endif
1011 #ifndef __NR_llistxattr
1012 #define __NR_llistxattr         (__NR_Linux + 187)
1013 #endif
1014 #ifndef __NR_futex
1015 #define __NR_futex              (__NR_Linux + 194)
1016 #endif
1017 #ifndef __NR_sched_setaffinity
1018 #define __NR_sched_setaffinity  (__NR_Linux + 195)
1019 #define __NR_sched_getaffinity  (__NR_Linux + 196)
1020 #endif
1021 #ifndef __NR_set_tid_address
1022 #define __NR_set_tid_address    (__NR_Linux + 212)
1023 #endif
1024 #ifndef __NR_clock_gettime
1025 #define __NR_clock_gettime      (__NR_Linux + 222)
1026 #endif
1027 #ifndef __NR_clock_getres
1028 #define __NR_clock_getres       (__NR_Linux + 223)
1029 #endif
1030 #ifndef __NR_openat
1031 #define __NR_openat             (__NR_Linux + 247)
1032 #endif
1033 #ifndef __NR_fstatat
1034 #define __NR_fstatat            (__NR_Linux + 252)
1035 #endif
1036 #ifndef __NR_unlinkat
1037 #define __NR_unlinkat           (__NR_Linux + 253)
1038 #endif
1039 #ifndef __NR_move_pages
1040 #define __NR_move_pages         (__NR_Linux + 267)
1041 #endif
1042 #ifndef __NR_getcpu
1043 #define __NR_getcpu             (__NR_Linux + 271)
1044 #endif
1045 #ifndef __NR_ioprio_set
1046 #define __NR_ioprio_set         (__NR_Linux + 273)
1047 #endif
1048 #ifndef __NR_ioprio_get
1049 #define __NR_ioprio_get         (__NR_Linux + 274)
1050 #endif
1051 /* End of MIPS (64bit API) definitions */
1052 #else
1053 #ifndef __NR_setresuid
1054 #define __NR_setresuid          (__NR_Linux + 115)
1055 #define __NR_setresgid          (__NR_Linux + 117)
1056 #endif
1057 #ifndef __NR_gettid
1058 #define __NR_gettid             (__NR_Linux + 178)
1059 #endif
1060 #ifndef __NR_readahead
1061 #define __NR_readahead          (__NR_Linux + 179)
1062 #endif
1063 #ifndef __NR_setxattr
1064 #define __NR_setxattr           (__NR_Linux + 180)
1065 #endif
1066 #ifndef __NR_lsetxattr
1067 #define __NR_lsetxattr          (__NR_Linux + 181)
1068 #endif
1069 #ifndef __NR_getxattr
1070 #define __NR_getxattr           (__NR_Linux + 183)
1071 #endif
1072 #ifndef __NR_lgetxattr
1073 #define __NR_lgetxattr          (__NR_Linux + 184)
1074 #endif
1075 #ifndef __NR_listxattr
1076 #define __NR_listxattr          (__NR_Linux + 186)
1077 #endif
1078 #ifndef __NR_llistxattr
1079 #define __NR_llistxattr         (__NR_Linux + 187)
1080 #endif
1081 #ifndef __NR_futex
1082 #define __NR_futex              (__NR_Linux + 194)
1083 #endif
1084 #ifndef __NR_sched_setaffinity
1085 #define __NR_sched_setaffinity  (__NR_Linux + 195)
1086 #define __NR_sched_getaffinity  (__NR_Linux + 196)
1087 #endif
1088 #ifndef __NR_set_tid_address
1089 #define __NR_set_tid_address    (__NR_Linux + 213)
1090 #endif
1091 #ifndef __NR_statfs64
1092 #define __NR_statfs64           (__NR_Linux + 217)
1093 #endif
1094 #ifndef __NR_fstatfs64
1095 #define __NR_fstatfs64          (__NR_Linux + 218)
1096 #endif
1097 #ifndef __NR_clock_gettime
1098 #define __NR_clock_gettime      (__NR_Linux + 226)
1099 #endif
1100 #ifndef __NR_clock_getres
1101 #define __NR_clock_getres       (__NR_Linux + 227)
1102 #endif
1103 #ifndef __NR_openat
1104 #define __NR_openat             (__NR_Linux + 251)
1105 #endif
1106 #ifndef __NR_fstatat
1107 #define __NR_fstatat            (__NR_Linux + 256)
1108 #endif
1109 #ifndef __NR_unlinkat
1110 #define __NR_unlinkat           (__NR_Linux + 257)
1111 #endif
1112 #ifndef __NR_move_pages
1113 #define __NR_move_pages         (__NR_Linux + 271)
1114 #endif
1115 #ifndef __NR_getcpu
1116 #define __NR_getcpu             (__NR_Linux + 275)
1117 #endif
1118 #ifndef __NR_ioprio_set
1119 #define __NR_ioprio_set         (__NR_Linux + 277)
1120 #endif
1121 #ifndef __NR_ioprio_get
1122 #define __NR_ioprio_get         (__NR_Linux + 278)
1123 #endif
1124 /* End of MIPS (new 32bit API) definitions                                   */
1125 #endif
1126 /* End of MIPS definitions                                                   */
1127 #elif defined(__PPC__)
1128 #ifndef __NR_setfsuid
1129 #define __NR_setfsuid           138
1130 #define __NR_setfsgid           139
1131 #endif
1132 #ifndef __NR_setresuid
1133 #define __NR_setresuid          164
1134 #define __NR_setresgid          169
1135 #endif
1136 #ifndef __NR_rt_sigaction
1137 #define __NR_rt_sigaction       173
1138 #define __NR_rt_sigprocmask     174
1139 #define __NR_rt_sigpending      175
1140 #define __NR_rt_sigsuspend      178
1141 #endif
1142 #ifndef __NR_pread64
1143 #define __NR_pread64            179
1144 #endif
1145 #ifndef __NR_pwrite64
1146 #define __NR_pwrite64           180
1147 #endif
1148 #ifndef __NR_ugetrlimit
1149 #define __NR_ugetrlimit         190
1150 #endif
1151 #ifndef __NR_readahead
1152 #define __NR_readahead          191
1153 #endif
1154 #ifndef __NR_stat64
1155 #define __NR_stat64             195
1156 #endif
1157 #ifndef __NR_fstat64
1158 #define __NR_fstat64            197
1159 #endif
1160 #ifndef __NR_getdents64
1161 #define __NR_getdents64         202
1162 #endif
1163 #ifndef __NR_gettid
1164 #define __NR_gettid             207
1165 #endif
1166 #ifndef __NR_setxattr
1167 #define __NR_setxattr           209
1168 #endif
1169 #ifndef __NR_lsetxattr
1170 #define __NR_lsetxattr          210
1171 #endif
1172 #ifndef __NR_getxattr
1173 #define __NR_getxattr           212
1174 #endif
1175 #ifndef __NR_lgetxattr
1176 #define __NR_lgetxattr          213
1177 #endif
1178 #ifndef __NR_listxattr
1179 #define __NR_listxattr          215
1180 #endif
1181 #ifndef __NR_llistxattr
1182 #define __NR_llistxattr         216
1183 #endif
1184 #ifndef __NR_futex
1185 #define __NR_futex              221
1186 #endif
1187 #ifndef __NR_sched_setaffinity
1188 #define __NR_sched_setaffinity  222
1189 #define __NR_sched_getaffinity  223
1190 #endif
1191 #ifndef __NR_set_tid_address
1192 #define __NR_set_tid_address    232
1193 #endif
1194 #ifndef __NR_clock_gettime
1195 #define __NR_clock_gettime      246
1196 #endif
1197 #ifndef __NR_clock_getres
1198 #define __NR_clock_getres       247
1199 #endif
1200 #ifndef __NR_statfs64
1201 #define __NR_statfs64           252
1202 #endif
1203 #ifndef __NR_fstatfs64
1204 #define __NR_fstatfs64          253
1205 #endif
1206 #ifndef __NR_fadvise64_64
1207 #define __NR_fadvise64_64       254
1208 #endif
1209 #ifndef __NR_ioprio_set
1210 #define __NR_ioprio_set         273
1211 #endif
1212 #ifndef __NR_ioprio_get
1213 #define __NR_ioprio_get         274
1214 #endif
1215 #ifndef __NR_openat
1216 #define __NR_openat             286
1217 #endif
1218 #ifndef __NR_fstatat64
1219 #define __NR_fstatat64          291
1220 #endif
1221 #ifndef __NR_unlinkat
1222 #define __NR_unlinkat           292
1223 #endif
1224 #ifndef __NR_move_pages
1225 #define __NR_move_pages         301
1226 #endif
1227 #ifndef __NR_getcpu
1228 #define __NR_getcpu             302
1229 #endif
1230 /* End of powerpc defininitions                                              */
1231 #endif
1232 
1233 
1234 /* After forking, we must make sure to only call system calls.               */
1235 #if __BOUNDED_POINTERS__
1236   #error "Need to port invocations of syscalls for bounded ptrs"
1237 #else
1238   /* The core dumper and the thread lister get executed after threads
1239    * have been suspended. As a consequence, we cannot call any functions
1240    * that acquire locks. Unfortunately, libc wraps most system calls
1241    * (e.g. in order to implement pthread_atfork, and to make calls
1242    * cancellable), which means we cannot call these functions. Instead,
1243    * we have to call syscall() directly.
1244    */
1245   #undef LSS_ERRNO
1246   #ifdef SYS_ERRNO
1247     /* Allow the including file to override the location of errno. This can
1248      * be useful when using clone() with the CLONE_VM option.
1249      */
1250     #define LSS_ERRNO SYS_ERRNO
1251   #else
1252     #define LSS_ERRNO errno
1253   #endif
1254 
1255   #undef LSS_INLINE
1256   #ifdef SYS_INLINE
1257     #define LSS_INLINE SYS_INLINE
1258   #else
1259     #define LSS_INLINE static inline
1260   #endif
1261 
1262   /* Allow the including file to override the prefix used for all new
1263    * system calls. By default, it will be set to "sys_".
1264    */
1265   #undef LSS_NAME
1266   #ifndef SYS_PREFIX
1267     #define LSS_NAME(name) sys_##name
1268   #elif SYS_PREFIX < 0
1269     #define LSS_NAME(name) name
1270   #elif SYS_PREFIX == 0
1271     #define LSS_NAME(name) sys0_##name
1272   #elif SYS_PREFIX == 1
1273     #define LSS_NAME(name) sys1_##name
1274   #elif SYS_PREFIX == 2
1275     #define LSS_NAME(name) sys2_##name
1276   #elif SYS_PREFIX == 3
1277     #define LSS_NAME(name) sys3_##name
1278   #elif SYS_PREFIX == 4
1279     #define LSS_NAME(name) sys4_##name
1280   #elif SYS_PREFIX == 5
1281     #define LSS_NAME(name) sys5_##name
1282   #elif SYS_PREFIX == 6
1283     #define LSS_NAME(name) sys6_##name
1284   #elif SYS_PREFIX == 7
1285     #define LSS_NAME(name) sys7_##name
1286   #elif SYS_PREFIX == 8
1287     #define LSS_NAME(name) sys8_##name
1288   #elif SYS_PREFIX == 9
1289     #define LSS_NAME(name) sys9_##name
1290   #endif
1291 
1292   #undef  LSS_RETURN
1293   #if (defined(__i386__) || defined(__x86_64__) || defined(__ARM_ARCH_3__))
1294   /* Failing system calls return a negative result in the range of
1295    * -1..-4095. These are "errno" values with the sign inverted.
1296    */
1297   #define LSS_RETURN(type, res)                                               \
1298     do {                                                                      \
1299       if ((unsigned long)(res) >= (unsigned long)(-4095)) {                   \
1300         LSS_ERRNO = -(res);                                                   \
1301         res = -1;                                                             \
1302       }                                                                       \
1303       return (type) (res);                                                    \
1304     } while (0)
1305   #elif defined(__mips__)
1306   /* On MIPS, failing system calls return -1, and set errno in a
1307    * separate CPU register.
1308    */
1309   #define LSS_RETURN(type, res, err)                                          \
1310     do {                                                                      \
1311       if (err) {                                                              \
1312         LSS_ERRNO = (res);                                                    \
1313         res = -1;                                                             \
1314       }                                                                       \
1315       return (type) (res);                                                    \
1316     } while (0)
1317   #elif defined(__PPC__)
1318   /* On PPC, failing system calls return -1, and set errno in a
1319    * separate CPU register. See linux/unistd.h.
1320    */
1321   #define LSS_RETURN(type, res, err)                                          \
1322    do {                                                                       \
1323      if (err & 0x10000000 ) {                                                 \
1324        LSS_ERRNO = (res);                                                     \
1325        res = -1;                                                              \
1326      }                                                                        \
1327      return (type) (res);                                                     \
1328    } while (0)
1329   #endif
1330   #if defined(__i386__)
1331     /* In PIC mode (e.g. when building shared libraries), gcc for i386
1332      * reserves ebx. Unfortunately, most distribution ship with implementations
1333      * of _syscallX() which clobber ebx.
1334      * Also, most definitions of _syscallX() neglect to mark "memory" as being
1335      * clobbered. This causes problems with compilers, that do a better job
1336      * at optimizing across __asm__ calls.
1337      * So, we just have to redefine all of the _syscallX() macros.
1338      */
1339     #undef  LSS_BODY
1340     #define LSS_BODY(type,args...)                                            \
1341       long __res;                                                             \
1342       __asm__ __volatile__("push %%ebx\n"                                     \
1343                            "movl %2,%%ebx\n"                                  \
1344                            "int $0x80\n"                                      \
1345                            "pop %%ebx"                                        \
1346                            args                                               \
1347                            : "memory");                                       \
1348       LSS_RETURN(type,__res)
1349     #undef  _syscall0
1350     #define _syscall0(type,name)                                              \
1351       type LSS_NAME(name)(void) {                                             \
1352         long __res;                                                           \
1353         __asm__ volatile("int $0x80"                                          \
1354                          : "=a" (__res)                                       \
1355                          : "0" (__NR_##name)                                  \
1356                          : "memory");                                         \
1357         LSS_RETURN(type,__res);                                               \
1358       }
1359     #undef  _syscall1
1360     #define _syscall1(type,name,type1,arg1)                                   \
1361       type LSS_NAME(name)(type1 arg1) {                                       \
1362         LSS_BODY(type,                                                        \
1363              : "=a" (__res)                                                   \
1364              : "0" (__NR_##name), "ri" ((long)(arg1)));                       \
1365       }
1366     #undef  _syscall2
1367     #define _syscall2(type,name,type1,arg1,type2,arg2)                        \
1368       type LSS_NAME(name)(type1 arg1,type2 arg2) {                            \
1369         LSS_BODY(type,                                                        \
1370              : "=a" (__res)                                                   \
1371              : "0" (__NR_##name),"ri" ((long)(arg1)), "c" ((long)(arg2)));    \
1372       }
1373     #undef  _syscall3
1374     #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3)             \
1375       type LSS_NAME(name)(type1 arg1,type2 arg2,type3 arg3) {                 \
1376         LSS_BODY(type,                                                        \
1377              : "=a" (__res)                                                   \
1378              : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)),    \
1379                "d" ((long)(arg3)));                                           \
1380       }
1381     #undef  _syscall4
1382     #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4)  \
1383       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) {   \
1384         LSS_BODY(type,                                                        \
1385              : "=a" (__res)                                                   \
1386              : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)),    \
1387                "d" ((long)(arg3)),"S" ((long)(arg4)));                        \
1388       }
1389     #undef  _syscall5
1390     #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,  \
1391                       type5,arg5)                                             \
1392       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4,     \
1393                           type5 arg5) {                                       \
1394         long __res;                                                           \
1395         __asm__ __volatile__("push %%ebx\n"                                   \
1396                              "movl %2,%%ebx\n"                                \
1397                              "movl %1,%%eax\n"                                \
1398                              "int  $0x80\n"                                   \
1399                              "pop  %%ebx"                                     \
1400                              : "=a" (__res)                                   \
1401                              : "i" (__NR_##name), "ri" ((long)(arg1)),        \
1402                                "c" ((long)(arg2)), "d" ((long)(arg3)),        \
1403                                "S" ((long)(arg4)), "D" ((long)(arg5))         \
1404                              : "memory");                                     \
1405         LSS_RETURN(type,__res);                                               \
1406       }
1407     #undef  _syscall6
1408     #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,  \
1409                       type5,arg5,type6,arg6)                                  \
1410       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4,     \
1411                           type5 arg5, type6 arg6) {                           \
1412         long __res;                                                           \
1413         struct { long __a1; long __a6; } __s = { (long)arg1, (long) arg6 };   \
1414         __asm__ __volatile__("push %%ebp\n"                                   \
1415                              "push %%ebx\n"                                   \
1416                              "movl 4(%2),%%ebp\n"                             \
1417                              "movl 0(%2), %%ebx\n"                            \
1418                              "movl %1,%%eax\n"                                \
1419                              "int  $0x80\n"                                   \
1420                              "pop  %%ebx\n"                                   \
1421                              "pop  %%ebp"                                     \
1422                              : "=a" (__res)                                   \
1423                              : "i" (__NR_##name),  "0" ((long)(&__s)),        \
1424                                "c" ((long)(arg2)), "d" ((long)(arg3)),        \
1425                                "S" ((long)(arg4)), "D" ((long)(arg5))         \
1426                              : "memory");                                     \
1427         LSS_RETURN(type,__res);                                               \
1428       }
LSS_NAME(clone)1429     LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
1430                                    int flags, void *arg, int *parent_tidptr,
1431                                    void *newtls, int *child_tidptr) {
1432       long __res;
1433       __asm__ __volatile__(/* if (fn == NULL)
1434                             *   return -EINVAL;
1435                             */
1436                            "movl   %3,%%ecx\n"
1437                            "jecxz  1f\n"
1438 
1439                            /* if (child_stack == NULL)
1440                             *   return -EINVAL;
1441                             */
1442                            "movl   %4,%%ecx\n"
1443                            "jecxz  1f\n"
1444 
1445                            /* Set up alignment of the child stack:
1446                             * child_stack = (child_stack & ~0xF) - 20;
1447                             */
1448                            "andl   $-16,%%ecx\n"
1449                            "subl   $20,%%ecx\n"
1450 
1451                            /* Push "arg" and "fn" onto the stack that will be
1452                             * used by the child.
1453                             */
1454                            "movl   %6,%%eax\n"
1455                            "movl   %%eax,4(%%ecx)\n"
1456                            "movl   %3,%%eax\n"
1457                            "movl   %%eax,(%%ecx)\n"
1458 
1459                            /* %eax = syscall(%eax = __NR_clone,
1460                             *                %ebx = flags,
1461                             *                %ecx = child_stack,
1462                             *                %edx = parent_tidptr,
1463                             *                %esi = newtls,
1464                             *                %edi = child_tidptr)
1465                             * Also, make sure that %ebx gets preserved as it is
1466                             * used in PIC mode.
1467                             */
1468                            "movl   %8,%%esi\n"
1469                            "movl   %7,%%edx\n"
1470                            "movl   %5,%%eax\n"
1471                            "movl   %9,%%edi\n"
1472                            "pushl  %%ebx\n"
1473                            "movl   %%eax,%%ebx\n"
1474                            "movl   %2,%%eax\n"
1475                            "int    $0x80\n"
1476 
1477                            /* In the parent: restore %ebx
1478                             * In the child:  move "fn" into %ebx
1479                             */
1480                            "popl   %%ebx\n"
1481 
1482                            /* if (%eax != 0)
1483                             *   return %eax;
1484                             */
1485                            "test   %%eax,%%eax\n"
1486                            "jnz    1f\n"
1487 
1488                            /* In the child, now. Terminate frame pointer chain.
1489                             */
1490                            "movl   $0,%%ebp\n"
1491 
1492                            /* Call "fn". "arg" is already on the stack.
1493                             */
1494                            "call   *%%ebx\n"
1495 
1496                            /* Call _exit(%ebx). Unfortunately older versions
1497                             * of gcc restrict the number of arguments that can
1498                             * be passed to asm(). So, we need to hard-code the
1499                             * system call number.
1500                             */
1501                            "movl   %%eax,%%ebx\n"
1502                            "movl   $1,%%eax\n"
1503                            "int    $0x80\n"
1504 
1505                            /* Return to parent.
1506                             */
1507                          "1:\n"
1508                            : "=a" (__res)
1509                            : "0"(-EINVAL), "i"(__NR_clone),
1510                              "m"(fn), "m"(child_stack), "m"(flags), "m"(arg),
1511                              "m"(parent_tidptr), "m"(newtls), "m"(child_tidptr)
1512                            : "memory", "ecx", "edx", "esi", "edi");
1513       LSS_RETURN(int, __res);
1514     }
1515 
1516     #define __NR__fadvise64_64 __NR_fadvise64_64
_syscall6(int,_fadvise64_64,int,fd,unsigned,offset_lo,unsigned,offset_hi,unsigned,len_lo,unsigned,len_hi,int,advice)1517     LSS_INLINE _syscall6(int, _fadvise64_64, int, fd,
1518                          unsigned, offset_lo, unsigned, offset_hi,
1519                          unsigned, len_lo, unsigned, len_hi,
1520                          int, advice)
1521 
1522     LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
1523                                        loff_t len, int advice) {
1524       return LSS_NAME(_fadvise64_64)(fd,
1525                                      (unsigned)offset, (unsigned)(offset >>32),
1526                                      (unsigned)len, (unsigned)(len >> 32),
1527                                      advice);
1528     }
1529 
1530     #define __NR__fallocate __NR_fallocate
_syscall6(int,_fallocate,int,fd,int,mode,unsigned,offset_lo,unsigned,offset_hi,unsigned,len_lo,unsigned,len_hi)1531     LSS_INLINE _syscall6(int, _fallocate, int, fd,
1532                          int, mode,
1533                          unsigned, offset_lo, unsigned, offset_hi,
1534                          unsigned, len_lo, unsigned, len_hi)
1535 
1536     LSS_INLINE int LSS_NAME(fallocate)(int fd, int mode,
1537                                        loff_t offset, loff_t len) {
1538       union { loff_t off; unsigned w[2]; } o = { offset }, l = { len };
1539       return LSS_NAME(_fallocate)(fd, mode, o.w[0], o.w[1], l.w[0], l.w[1]);
1540     }
1541 
LSS_NAME(restore_rt)1542     LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
1543       /* On i386, the kernel does not know how to return from a signal
1544        * handler. Instead, it relies on user space to provide a
1545        * restorer function that calls the {rt_,}sigreturn() system call.
1546        * Unfortunately, we cannot just reference the glibc version of this
1547        * function, as glibc goes out of its way to make it inaccessible.
1548        */
1549       void (*res)(void);
1550       __asm__ __volatile__("call   2f\n"
1551                          "0:.align 16\n"
1552                          "1:movl   %1,%%eax\n"
1553                            "int    $0x80\n"
1554                          "2:popl   %0\n"
1555                            "addl   $(1b-0b),%0\n"
1556                            : "=a" (res)
1557                            : "i"  (__NR_rt_sigreturn));
1558       return res;
1559     }
LSS_NAME(restore)1560     LSS_INLINE void (*LSS_NAME(restore)(void))(void) {
1561       /* On i386, the kernel does not know how to return from a signal
1562        * handler. Instead, it relies on user space to provide a
1563        * restorer function that calls the {rt_,}sigreturn() system call.
1564        * Unfortunately, we cannot just reference the glibc version of this
1565        * function, as glibc goes out of its way to make it inaccessible.
1566        */
1567       void (*res)(void);
1568       __asm__ __volatile__("call   2f\n"
1569                          "0:.align 16\n"
1570                          "1:pop    %%eax\n"
1571                            "movl   %1,%%eax\n"
1572                            "int    $0x80\n"
1573                          "2:popl   %0\n"
1574                            "addl   $(1b-0b),%0\n"
1575                            : "=a" (res)
1576                            : "i"  (__NR_sigreturn));
1577       return res;
1578     }
1579   #elif defined(__x86_64__)
1580     /* There are no known problems with any of the _syscallX() macros
1581      * currently shipping for x86_64, but we still need to be able to define
1582      * our own version so that we can override the location of the errno
1583      * location (e.g. when using the clone() system call with the CLONE_VM
1584      * option).
1585      */
1586     #undef  LSS_BODY
1587     #define LSS_BODY(type,name, ...)                                          \
1588           long __res;                                                         \
1589           __asm__ __volatile__("syscall" : "=a" (__res) : "0" (__NR_##name),  \
1590             ##__VA_ARGS__ : "r11", "rcx", "memory");                          \
1591           LSS_RETURN(type, __res)
1592     #undef _syscall0
1593     #define _syscall0(type,name)                                              \
1594       type LSS_NAME(name)() {                                                 \
1595         LSS_BODY(type, name);                                                 \
1596       }
1597     #undef _syscall1
1598     #define _syscall1(type,name,type1,arg1)                                   \
1599       type LSS_NAME(name)(type1 arg1) {                                       \
1600         LSS_BODY(type, name, "D" ((long)(arg1)));                             \
1601       }
1602     #undef _syscall2
1603     #define _syscall2(type,name,type1,arg1,type2,arg2)                        \
1604       type LSS_NAME(name)(type1 arg1, type2 arg2) {                           \
1605         LSS_BODY(type, name, "D" ((long)(arg1)), "S" ((long)(arg2)));         \
1606       }
1607     #undef _syscall3
1608     #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3)             \
1609       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) {               \
1610         LSS_BODY(type, name, "D" ((long)(arg1)), "S" ((long)(arg2)),          \
1611                              "d" ((long)(arg3)));                             \
1612       }
1613     #undef _syscall4
1614     #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4)  \
1615       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) {   \
1616           long __res;                                                         \
1617           __asm__ __volatile__("movq %5,%%r10; syscall" :                     \
1618             "=a" (__res) : "0" (__NR_##name),                                 \
1619             "D" ((long)(arg1)), "S" ((long)(arg2)), "d" ((long)(arg3)),       \
1620             "g" ((long)(arg4)) : "r10", "r11", "rcx", "memory");              \
1621           LSS_RETURN(type, __res);                                            \
1622       }
1623     #undef _syscall5
1624     #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,  \
1625                       type5,arg5)                                             \
1626       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4,     \
1627                           type5 arg5) {                                       \
1628           long __res;                                                         \
1629           __asm__ __volatile__("movq %5,%%r10; movq %6,%%r8; syscall" :       \
1630             "=a" (__res) : "0" (__NR_##name),                                 \
1631             "D" ((long)(arg1)), "S" ((long)(arg2)), "d" ((long)(arg3)),       \
1632             "g" ((long)(arg4)), "g" ((long)(arg5)) :                          \
1633             "r8", "r10", "r11", "rcx", "memory");                             \
1634           LSS_RETURN(type, __res);                                            \
1635       }
1636     #undef _syscall6
1637     #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,  \
1638                       type5,arg5,type6,arg6)                                  \
1639       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4,     \
1640                           type5 arg5, type6 arg6) {                           \
1641           long __res;                                                         \
1642           __asm__ __volatile__("movq %5,%%r10; movq %6,%%r8; movq %7,%%r9;"   \
1643                                "syscall" :                                    \
1644             "=a" (__res) : "0" (__NR_##name),                                 \
1645             "D" ((long)(arg1)), "S" ((long)(arg2)), "d" ((long)(arg3)),       \
1646             "g" ((long)(arg4)), "g" ((long)(arg5)), "g" ((long)(arg6)) :      \
1647             "r8", "r9", "r10", "r11", "rcx", "memory");                       \
1648           LSS_RETURN(type, __res);                                            \
1649       }
LSS_NAME(clone)1650     LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
1651                                    int flags, void *arg, int *parent_tidptr,
1652                                    void *newtls, int *child_tidptr) {
1653       long __res;
1654       {
1655         register void *__tls  __asm__("r8")  = newtls;
1656         register int  *__ctid __asm__("r10") = child_tidptr;
1657         __asm__ __volatile__(/* if (fn == NULL)
1658                               *   return -EINVAL;
1659                               */
1660                              "testq  %4,%4\n"
1661                              "jz     1f\n"
1662 
1663                              /* if (child_stack == NULL)
1664                               *   return -EINVAL;
1665                               */
1666                              "testq  %5,%5\n"
1667                              "jz     1f\n"
1668 
1669                              /* childstack -= 2*sizeof(void *);
1670                               */
1671                              "subq   $16,%5\n"
1672 
1673                              /* Push "arg" and "fn" onto the stack that will be
1674                               * used by the child.
1675                               */
1676                              "movq   %7,8(%5)\n"
1677                              "movq   %4,0(%5)\n"
1678 
1679                              /* %rax = syscall(%rax = __NR_clone,
1680                               *                %rdi = flags,
1681                               *                %rsi = child_stack,
1682                               *                %rdx = parent_tidptr,
1683                               *                %r8  = new_tls,
1684                               *                %r10 = child_tidptr)
1685                               */
1686                              "movq   %2,%%rax\n"
1687                              "syscall\n"
1688 
1689                              /* if (%rax != 0)
1690                               *   return;
1691                               */
1692                              "testq  %%rax,%%rax\n"
1693                              "jnz    1f\n"
1694 
1695                              /* In the child. Terminate frame pointer chain.
1696                               */
1697                              "xorq   %%rbp,%%rbp\n"
1698 
1699                              /* Call "fn(arg)".
1700                               */
1701                              "popq   %%rax\n"
1702                              "popq   %%rdi\n"
1703                              "call   *%%rax\n"
1704 
1705                              /* Call _exit(%ebx).
1706                               */
1707                              "movq   %%rax,%%rdi\n"
1708                              "movq   %3,%%rax\n"
1709                              "syscall\n"
1710 
1711                              /* Return to parent.
1712                               */
1713                            "1:\n"
1714                              : "=a" (__res)
1715                              : "0"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit),
1716                                "r"(fn), "S"(child_stack), "D"(flags), "r"(arg),
1717                                "d"(parent_tidptr), "r"(__tls), "r"(__ctid)
1718                              : "memory", "r11", "rcx");
1719       }
1720       LSS_RETURN(int, __res);
1721     }
_syscall4(int,fadvise64,int,fd,loff_t,offset,loff_t,len,int,advice)1722     LSS_INLINE _syscall4(int, fadvise64, int, fd, loff_t, offset, loff_t, len,
1723                          int,  advice)
1724 
1725     LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
1726       /* On x86-64, the kernel does not know how to return from
1727        * a signal handler. Instead, it relies on user space to provide a
1728        * restorer function that calls the rt_sigreturn() system call.
1729        * Unfortunately, we cannot just reference the glibc version of this
1730        * function, as glibc goes out of its way to make it inaccessible.
1731        */
1732       void (*res)(void);
1733       __asm__ __volatile__("call   2f\n"
1734                          "0:.align 16\n"
1735                          "1:movq   %1,%%rax\n"
1736                            "syscall\n"
1737                          "2:popq   %0\n"
1738                            "addq   $(1b-0b),%0\n"
1739                            : "=a" (res)
1740                            : "i"  (__NR_rt_sigreturn));
1741       return res;
1742     }
1743   #elif defined(__ARM_ARCH_3__)
1744     /* Most definitions of _syscallX() neglect to mark "memory" as being
1745      * clobbered. This causes problems with compilers, that do a better job
1746      * at optimizing across __asm__ calls.
1747      * So, we just have to redefine all fo the _syscallX() macros.
1748      */
1749     #undef LSS_REG
1750     #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
1751     #undef  LSS_BODY
1752     #define LSS_BODY(type,name,args...)                                       \
1753           register long __res_r0 __asm__("r0");                               \
1754           long __res;                                                         \
1755           __asm__ __volatile__ (__syscall(name)                               \
1756                                 : "=r"(__res_r0) : args : "lr", "memory");    \
1757           __res = __res_r0;                                                   \
1758           LSS_RETURN(type, __res)
1759     #undef _syscall0
1760     #define _syscall0(type, name)                                             \
1761       type LSS_NAME(name)() {                                                 \
1762         LSS_BODY(type, name);                                                 \
1763       }
1764     #undef _syscall1
1765     #define _syscall1(type, name, type1, arg1)                                \
1766       type LSS_NAME(name)(type1 arg1) {                                       \
1767         LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0));                    \
1768       }
1769     #undef _syscall2
1770     #define _syscall2(type, name, type1, arg1, type2, arg2)                   \
1771       type LSS_NAME(name)(type1 arg1, type2 arg2) {                           \
1772         LSS_REG(0, arg1); LSS_REG(1, arg2);                                   \
1773         LSS_BODY(type, name, "r"(__r0), "r"(__r1));                           \
1774       }
1775     #undef _syscall3
1776     #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3)      \
1777       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) {               \
1778         LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3);                 \
1779         LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2));                \
1780       }
1781     #undef _syscall4
1782     #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4)  \
1783       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) {   \
1784         LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3);                 \
1785         LSS_REG(3, arg4);                                                     \
1786         LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3));     \
1787       }
1788     #undef _syscall5
1789     #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,  \
1790                       type5,arg5)                                             \
1791       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4,     \
1792                           type5 arg5) {                                       \
1793         LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3);                 \
1794         LSS_REG(3, arg4); LSS_REG(4, arg5);                                   \
1795         LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3),      \
1796                              "r"(__r4));                                      \
1797       }
1798     #undef _syscall6
1799     #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,  \
1800                       type5,arg5,type6,arg6)                                  \
1801       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4,     \
1802                           type5 arg5, type6 arg6) {                           \
1803         LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3);                 \
1804         LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6);                 \
1805         LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3),      \
1806                              "r"(__r4), "r"(__r5));                           \
1807       }
LSS_NAME(clone)1808     LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
1809                                    int flags, void *arg, int *parent_tidptr,
1810                                    void *newtls, int *child_tidptr) {
1811       long __res;
1812       {
1813         register int   __flags __asm__("r0") = flags;
1814         register void *__stack __asm__("r1") = child_stack;
1815         register void *__ptid  __asm__("r2") = parent_tidptr;
1816         register void *__tls   __asm__("r3") = newtls;
1817         register int  *__ctid  __asm__("r4") = child_tidptr;
1818         __asm__ __volatile__(/* if (fn == NULL || child_stack == NULL)
1819                               *   return -EINVAL;
1820                               */
1821                              "cmp   %2,#0\n"
1822                              "cmpne %3,#0\n"
1823                              "moveq %0,%1\n"
1824                              "beq   1f\n"
1825 
1826                              /* Push "arg" and "fn" onto the stack that will be
1827                               * used by the child.
1828                               */
1829                              "str   %5,[%3,#-4]!\n"
1830                              "str   %2,[%3,#-4]!\n"
1831 
1832                              /* %r0 = syscall(%r0 = flags,
1833                               *               %r1 = child_stack,
1834                               *               %r2 = parent_tidptr,
1835                               *               %r3 = newtls,
1836                               *               %r4 = child_tidptr)
1837                               */
1838                              __syscall(clone)"\n"
1839 
1840                              /* if (%r0 != 0)
1841                               *   return %r0;
1842                               */
1843                              "movs  %0,r0\n"
1844                              "bne   1f\n"
1845 
1846                              /* In the child, now. Call "fn(arg)".
1847                               */
1848                              "ldr   r0,[sp, #4]\n"
1849                              "mov   lr,pc\n"
1850                              "ldr   pc,[sp]\n"
1851 
1852                              /* Call _exit(%r0).
1853                               */
1854                              __syscall(exit)"\n"
1855                            "1:\n"
1856                              : "=r" (__res)
1857                              : "i"(-EINVAL),
1858                                "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
1859                                "r"(__ptid), "r"(__tls), "r"(__ctid)
1860                              : "lr", "memory");
1861       }
1862       LSS_RETURN(int, __res);
1863     }
1864   #elif defined(__mips__)
1865     #undef LSS_REG
1866     #define LSS_REG(r,a) register unsigned long __r##r __asm__("$"#r) =       \
1867                                  (unsigned long)(a)
1868     #undef  LSS_BODY
1869     #define LSS_BODY(type,name,r7,...)                                        \
1870           register unsigned long __v0 __asm__("$2") = __NR_##name;            \
1871           __asm__ __volatile__ ("syscall\n"                                   \
1872                                 : "=&r"(__v0), r7 (__r7)                      \
1873                                 : "0"(__v0), ##__VA_ARGS__                    \
1874                                 : "$8", "$9", "$10", "$11", "$12",            \
1875                                   "$13", "$14", "$15", "$24", "memory");      \
1876           LSS_RETURN(type, __v0, __r7)
1877     #undef _syscall0
1878     #define _syscall0(type, name)                                             \
1879       type LSS_NAME(name)() {                                                 \
1880         register unsigned long __r7 __asm__("$7");                            \
1881         LSS_BODY(type, name, "=r");                                           \
1882       }
1883     #undef _syscall1
1884     #define _syscall1(type, name, type1, arg1)                                \
1885       type LSS_NAME(name)(type1 arg1) {                                       \
1886         register unsigned long __r7 __asm__("$7");                            \
1887         LSS_REG(4, arg1); LSS_BODY(type, name, "=r", "r"(__r4));              \
1888       }
1889     #undef _syscall2
1890     #define _syscall2(type, name, type1, arg1, type2, arg2)                   \
1891       type LSS_NAME(name)(type1 arg1, type2 arg2) {                           \
1892         register unsigned long __r7 __asm__("$7");                            \
1893         LSS_REG(4, arg1); LSS_REG(5, arg2);                                   \
1894         LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5));                     \
1895       }
1896     #undef _syscall3
1897     #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3)      \
1898       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) {               \
1899         register unsigned long __r7 __asm__("$7");                            \
1900         LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3);                 \
1901         LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5), "r"(__r6));          \
1902       }
1903     #undef _syscall4
1904     #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4)  \
1905       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) {   \
1906         LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3);                 \
1907         LSS_REG(7, arg4);                                                     \
1908         LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6));          \
1909       }
1910     #undef _syscall5
1911     #if _MIPS_SIM == _MIPS_SIM_ABI32
1912     /* The old 32bit MIPS system call API passes the fifth and sixth argument
1913      * on the stack, whereas the new APIs use registers "r8" and "r9".
1914      */
1915     #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,  \
1916                       type5,arg5)                                             \
1917       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4,     \
1918                           type5 arg5) {                                       \
1919         LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3);                 \
1920         LSS_REG(7, arg4);                                                     \
1921         register unsigned long __v0 __asm__("$2");                            \
1922         __asm__ __volatile__ (".set noreorder\n"                              \
1923                               "lw    $2, %6\n"                                \
1924                               "subu  $29, 32\n"                               \
1925                               "sw    $2, 16($29)\n"                           \
1926                               "li    $2, %2\n"                                \
1927                               "syscall\n"                                     \
1928                               "addiu $29, 32\n"                               \
1929                               ".set reorder\n"                                \
1930                               : "=&r"(__v0), "+r" (__r7)                      \
1931                               : "i" (__NR_##name), "r"(__r4), "r"(__r5),      \
1932                                 "r"(__r6), "m" ((unsigned long)arg5)          \
1933                               : "$8", "$9", "$10", "$11", "$12",              \
1934                                 "$13", "$14", "$15", "$24", "memory");        \
1935         LSS_RETURN(type, __v0, __r7);                                         \
1936       }
1937     #else
1938     #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,  \
1939                       type5,arg5)                                             \
1940       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4,     \
1941                           type5 arg5) {                                       \
1942         LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3);                 \
1943         LSS_REG(7, arg4); LSS_REG(8, arg5);                                   \
1944         LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6),           \
1945                  "r"(__r8));                                                  \
1946       }
1947     #endif
1948     #undef _syscall6
1949     #if _MIPS_SIM == _MIPS_SIM_ABI32
1950     /* The old 32bit MIPS system call API passes the fifth and sixth argument
1951      * on the stack, whereas the new APIs use registers "r8" and "r9".
1952      */
1953     #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,  \
1954                       type5,arg5,type6,arg6)                                  \
1955       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4,     \
1956                           type5 arg5, type6 arg6) {                           \
1957         LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3);                 \
1958         LSS_REG(7, arg4);                                                     \
1959         register unsigned long __v0 __asm__("$2");                            \
1960         __asm__ __volatile__ (".set noreorder\n"                              \
1961                               "lw    $2, %6\n"                                \
1962                               "lw    $8, %7\n"                                \
1963                               "subu  $29, 32\n"                               \
1964                               "sw    $2, 16($29)\n"                           \
1965                               "sw    $8, 20($29)\n"                           \
1966                               "li    $2, %2\n"                                \
1967                               "syscall\n"                                     \
1968                               "addiu $29, 32\n"                               \
1969                               ".set reorder\n"                                \
1970                               : "=&r"(__v0), "+r" (__r7)                      \
1971                               : "i" (__NR_##name), "r"(__r4), "r"(__r5),      \
1972                                 "r"(__r6), "r" ((unsigned long)arg5),         \
1973                                 "r" ((unsigned long)arg6)                     \
1974                               : "$8", "$9", "$10", "$11", "$12",              \
1975                                 "$13", "$14", "$15", "$24", "memory");        \
1976         LSS_RETURN(type, __v0, __r7);                                         \
1977       }
1978     #else
1979     #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,  \
1980                       type5,arg5,type6,arg6)                                  \
1981       type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4,     \
1982                           type5 arg5,type6 arg6) {                            \
1983         LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3);                 \
1984         LSS_REG(7, arg4); LSS_REG(8, arg5); LSS_REG(9, arg6);                 \
1985         LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6),           \
1986                  "r"(__r8), "r"(__r9));                                       \
1987       }
1988     #endif
LSS_NAME(clone)1989     LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
1990                                    int flags, void *arg, int *parent_tidptr,
1991                                    void *newtls, int *child_tidptr) {
1992       register unsigned long __v0 __asm__("$2");
1993       register unsigned long __r7 __asm__("$7") = (unsigned long)newtls;
1994       {
1995         register int   __flags __asm__("$4") = flags;
1996         register void *__stack __asm__("$5") = child_stack;
1997         register void *__ptid  __asm__("$6") = parent_tidptr;
1998         register int  *__ctid  __asm__("$8") = child_tidptr;
1999         __asm__ __volatile__(
2000           #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2001                              "subu  $29,24\n"
2002           #elif _MIPS_SIM == _MIPS_SIM_NABI32
2003                              "sub   $29,16\n"
2004           #else
2005                              "dsubu $29,16\n"
2006           #endif
2007 
2008                              /* if (fn == NULL || child_stack == NULL)
2009                               *   return -EINVAL;
2010                               */
2011                              "li    %0,%2\n"
2012                              "beqz  %5,1f\n"
2013                              "beqz  %6,1f\n"
2014 
2015                              /* Push "arg" and "fn" onto the stack that will be
2016                               * used by the child.
2017                               */
2018           #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2019                              "subu  %6,32\n"
2020                              "sw    %5,0(%6)\n"
2021                              "sw    %8,4(%6)\n"
2022           #elif _MIPS_SIM == _MIPS_SIM_NABI32
2023                              "sub   %6,32\n"
2024                              "sw    %5,0(%6)\n"
2025                              "sw    %8,8(%6)\n"
2026           #else
2027                              "dsubu %6,32\n"
2028                              "sd    %5,0(%6)\n"
2029                              "sd    %8,8(%6)\n"
2030           #endif
2031 
2032                              /* $7 = syscall($4 = flags,
2033                               *              $5 = child_stack,
2034                               *              $6 = parent_tidptr,
2035                               *              $7 = newtls,
2036                               *              $8 = child_tidptr)
2037                               */
2038                              "li    $2,%3\n"
2039                              "syscall\n"
2040 
2041                              /* if ($7 != 0)
2042                               *   return $2;
2043                               */
2044                              "bnez  $7,1f\n"
2045                              "bnez  $2,1f\n"
2046 
2047                              /* In the child, now. Call "fn(arg)".
2048                               */
2049           #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2050                             "lw    $25,0($29)\n"
2051                             "lw    $4,4($29)\n"
2052           #elif _MIPS_SIM == _MIPS_SIM_NABI32
2053                             "lw    $25,0($29)\n"
2054                             "lw    $4,8($29)\n"
2055           #else
2056                             "ld    $25,0($29)\n"
2057                             "ld    $4,8($29)\n"
2058           #endif
2059                             "jalr  $25\n"
2060 
2061                              /* Call _exit($2)
2062                               */
2063                             "move  $4,$2\n"
2064                             "li    $2,%4\n"
2065                             "syscall\n"
2066 
2067                            "1:\n"
2068           #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2069                              "addu  $29, 24\n"
2070           #elif _MIPS_SIM == _MIPS_SIM_NABI32
2071                              "add   $29, 16\n"
2072           #else
2073                              "daddu $29,16\n"
2074           #endif
2075                              : "=&r" (__v0), "=r" (__r7)
2076                              : "i"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit),
2077                                "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2078                                "r"(__ptid), "r"(__r7), "r"(__ctid)
2079                              : "$9", "$10", "$11", "$12", "$13", "$14", "$15",
2080                                "$24", "memory");
2081       }
2082       LSS_RETURN(int, __v0, __r7);
2083     }
2084   #elif defined (__PPC__)
2085     #undef  LSS_LOADARGS_0
2086     #define LSS_LOADARGS_0(name, dummy...)                                    \
2087         __sc_0 = __NR_##name
2088     #undef  LSS_LOADARGS_1
2089     #define LSS_LOADARGS_1(name, arg1)                                        \
2090             LSS_LOADARGS_0(name);                                             \
2091             __sc_3 = (unsigned long) (arg1)
2092     #undef  LSS_LOADARGS_2
2093     #define LSS_LOADARGS_2(name, arg1, arg2)                                  \
2094             LSS_LOADARGS_1(name, arg1);                                       \
2095             __sc_4 = (unsigned long) (arg2)
2096     #undef  LSS_LOADARGS_3
2097     #define LSS_LOADARGS_3(name, arg1, arg2, arg3)                            \
2098             LSS_LOADARGS_2(name, arg1, arg2);                                 \
2099             __sc_5 = (unsigned long) (arg3)
2100     #undef  LSS_LOADARGS_4
2101     #define LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4)                      \
2102             LSS_LOADARGS_3(name, arg1, arg2, arg3);                           \
2103             __sc_6 = (unsigned long) (arg4)
2104     #undef  LSS_LOADARGS_5
2105     #define LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5)                \
2106             LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4);                     \
2107             __sc_7 = (unsigned long) (arg5)
2108     #undef  LSS_LOADARGS_6
2109     #define LSS_LOADARGS_6(name, arg1, arg2, arg3, arg4, arg5, arg6)          \
2110             LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5);               \
2111             __sc_8 = (unsigned long) (arg6)
2112     #undef  LSS_ASMINPUT_0
2113     #define LSS_ASMINPUT_0 "0" (__sc_0)
2114     #undef  LSS_ASMINPUT_1
2115     #define LSS_ASMINPUT_1 LSS_ASMINPUT_0, "1" (__sc_3)
2116     #undef  LSS_ASMINPUT_2
2117     #define LSS_ASMINPUT_2 LSS_ASMINPUT_1, "2" (__sc_4)
2118     #undef  LSS_ASMINPUT_3
2119     #define LSS_ASMINPUT_3 LSS_ASMINPUT_2, "3" (__sc_5)
2120     #undef  LSS_ASMINPUT_4
2121     #define LSS_ASMINPUT_4 LSS_ASMINPUT_3, "4" (__sc_6)
2122     #undef  LSS_ASMINPUT_5
2123     #define LSS_ASMINPUT_5 LSS_ASMINPUT_4, "5" (__sc_7)
2124     #undef  LSS_ASMINPUT_6
2125     #define LSS_ASMINPUT_6 LSS_ASMINPUT_5, "6" (__sc_8)
2126     #undef  LSS_BODY
2127     #define LSS_BODY(nr, type, name, args...)                                 \
2128         long __sc_ret, __sc_err;                                              \
2129         {                                                                     \
2130                         register unsigned long __sc_0 __asm__ ("r0");         \
2131                         register unsigned long __sc_3 __asm__ ("r3");         \
2132                         register unsigned long __sc_4 __asm__ ("r4");         \
2133                         register unsigned long __sc_5 __asm__ ("r5");         \
2134                         register unsigned long __sc_6 __asm__ ("r6");         \
2135                         register unsigned long __sc_7 __asm__ ("r7");         \
2136                         register unsigned long __sc_8 __asm__ ("r8");         \
2137                                                                               \
2138             LSS_LOADARGS_##nr(name, args);                                    \
2139             __asm__ __volatile__                                              \
2140                 ("sc\n\t"                                                     \
2141                  "mfcr %0"                                                    \
2142                  : "=&r" (__sc_0),                                            \
2143                    "=&r" (__sc_3), "=&r" (__sc_4),                            \
2144                    "=&r" (__sc_5), "=&r" (__sc_6),                            \
2145                    "=&r" (__sc_7), "=&r" (__sc_8)                             \
2146                  : LSS_ASMINPUT_##nr                                          \
2147                  : "cr0", "ctr", "memory",                                    \
2148                    "r9", "r10", "r11", "r12");                                \
2149             __sc_ret = __sc_3;                                                \
2150             __sc_err = __sc_0;                                                \
2151         }                                                                     \
2152         LSS_RETURN(type, __sc_ret, __sc_err)
2153     #undef _syscall0
2154     #define _syscall0(type, name)                                             \
2155        type LSS_NAME(name)(void) {                                            \
2156           LSS_BODY(0, type, name);                                            \
2157        }
2158     #undef _syscall1
2159     #define _syscall1(type, name, type1, arg1)                                \
2160        type LSS_NAME(name)(type1 arg1) {                                      \
2161           LSS_BODY(1, type, name, arg1);                                      \
2162        }
2163     #undef _syscall2
2164     #define _syscall2(type, name, type1, arg1, type2, arg2)                   \
2165        type LSS_NAME(name)(type1 arg1, type2 arg2) {                          \
2166           LSS_BODY(2, type, name, arg1, arg2);                                \
2167        }
2168     #undef _syscall3
2169     #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3)      \
2170        type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) {              \
2171           LSS_BODY(3, type, name, arg1, arg2, arg3);                          \
2172        }
2173     #undef _syscall4
2174     #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3,      \
2175                                   type4, arg4)                                \
2176        type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) {  \
2177           LSS_BODY(4, type, name, arg1, arg2, arg3, arg4);                    \
2178        }
2179     #undef _syscall5
2180     #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3,      \
2181                                   type4, arg4, type5, arg5)                   \
2182        type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4,    \
2183                                                type5 arg5) {                  \
2184           LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5);              \
2185        }
2186     #undef _syscall6
2187     #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3,      \
2188                                   type4, arg4, type5, arg5, type6, arg6)      \
2189        type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4,    \
2190                                                type5 arg5, type6 arg6) {      \
2191           LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6);        \
2192        }
2193     /* clone function adapted from glibc 2.3.6 clone.S                       */
2194     /* TODO(csilvers): consider wrapping some args up in a struct, like we
2195      * do for i386's _syscall6, so we can compile successfully on gcc 2.95
2196      */
LSS_NAME(clone)2197     LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2198                                    int flags, void *arg, int *parent_tidptr,
2199                                    void *newtls, int *child_tidptr) {
2200       long __ret, __err;
2201       {
2202         register int (*__fn)(void *)    __asm__ ("r8")  = fn;
2203         register void *__cstack                 __asm__ ("r4")  = child_stack;
2204         register int __flags                    __asm__ ("r3")  = flags;
2205         register void * __arg                   __asm__ ("r9")  = arg;
2206         register int * __ptidptr                __asm__ ("r5")  = parent_tidptr;
2207         register void * __newtls                __asm__ ("r6")  = newtls;
2208         register int * __ctidptr                __asm__ ("r7")  = child_tidptr;
2209         __asm__ __volatile__(
2210             /* check for fn == NULL
2211              * and child_stack == NULL
2212              */
2213             "cmpwi cr0, %6, 0\n\t"
2214             "cmpwi cr1, %7, 0\n\t"
2215             "cror cr0*4+eq, cr1*4+eq, cr0*4+eq\n\t"
2216             "beq- cr0, 1f\n\t"
2217 
2218             /* set up stack frame for child                                  */
2219             "clrrwi %7, %7, 4\n\t"
2220             "li 0, 0\n\t"
2221             "stwu 0, -16(%7)\n\t"
2222 
2223             /* fn, arg, child_stack are saved across the syscall: r28-30     */
2224             "mr 28, %6\n\t"
2225             "mr 29, %7\n\t"
2226             "mr 27, %9\n\t"
2227 
2228             /* syscall                                                       */
2229             "li 0, %4\n\t"
2230             /* flags already in r3
2231              * child_stack already in r4
2232              * ptidptr already in r5
2233              * newtls already in r6
2234              * ctidptr already in r7
2235              */
2236             "sc\n\t"
2237 
2238             /* Test if syscall was successful                                */
2239             "cmpwi cr1, 3, 0\n\t"
2240             "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
2241             "bne- cr1, 1f\n\t"
2242 
2243             /* Do the function call                                          */
2244             "mtctr 28\n\t"
2245             "mr 3, 27\n\t"
2246             "bctrl\n\t"
2247 
2248             /* Call _exit(r3)                                                */
2249             "li 0, %5\n\t"
2250             "sc\n\t"
2251 
2252             /* Return to parent                                              */
2253             "1:\n"
2254             "mfcr %1\n\t"
2255             "mr %0, 3\n\t"
2256               : "=r" (__ret), "=r" (__err)
2257               : "0" (-1), "1" (EINVAL),
2258                 "i" (__NR_clone), "i" (__NR_exit),
2259                 "r" (__fn), "r" (__cstack), "r" (__flags),
2260                 "r" (__arg), "r" (__ptidptr), "r" (__newtls),
2261                 "r" (__ctidptr)
2262               : "cr0", "cr1", "memory", "ctr",
2263                 "r0", "r29", "r27", "r28");
2264       }
2265       LSS_RETURN(int, __ret, __err);
2266     }
2267   #endif
2268   #define __NR__exit   __NR_exit
2269   #define __NR__gettid __NR_gettid
2270   #define __NR__mremap __NR_mremap
2271   LSS_INLINE _syscall1(int,     chdir,           const char *,p)
2272   LSS_INLINE _syscall1(int,     close,           int,         f)
2273   LSS_INLINE _syscall2(int,     clock_getres,    int,         c,
2274                        struct kernel_timespec*, t)
2275   LSS_INLINE _syscall2(int,     clock_gettime,   int,         c,
2276                        struct kernel_timespec*, t)
2277   LSS_INLINE _syscall1(int,     dup,             int,         f)
2278   LSS_INLINE _syscall2(int,     dup2,            int,         s,
2279                        int,            d)
2280   LSS_INLINE _syscall3(int,     execve,          const char*, f,
2281                        const char*const*,a,const char*const*, e)
2282   LSS_INLINE _syscall1(int,     _exit,           int,         e)
2283   LSS_INLINE _syscall3(int,     fcntl,           int,         f,
2284                        int,            c, long,   a)
2285   LSS_INLINE _syscall0(pid_t,   fork)
2286   LSS_INLINE _syscall2(int,     fstat,           int,         f,
2287                       struct kernel_stat*,   b)
2288   LSS_INLINE _syscall2(int,     fstatfs,         int,         f,
2289                       struct kernel_statfs*, b)
2290   LSS_INLINE _syscall4(int,     futex,           int*,        a,
2291                        int,            o, int,    v,
2292                       struct kernel_timespec*, t)
2293   LSS_INLINE _syscall3(int,     getdents,        int,         f,
2294                       struct kernel_dirent*, d, int,    c)
2295   LSS_INLINE _syscall3(int,     getdents64,      int,         f,
2296                       struct kernel_dirent64*, d, int,    c)
2297   LSS_INLINE _syscall0(gid_t,   getegid)
2298   LSS_INLINE _syscall0(uid_t,   geteuid)
2299   LSS_INLINE _syscall0(pid_t,   getpgrp)
2300   LSS_INLINE _syscall0(pid_t,   getpid)
2301   LSS_INLINE _syscall0(pid_t,   getppid)
2302   LSS_INLINE _syscall2(int,     getpriority,     int,         a,
2303                        int,            b)
2304   LSS_INLINE _syscall2(int,     getrlimit,       int,         r,
2305                       struct kernel_rlimit*, l)
2306   LSS_INLINE _syscall1(pid_t,   getsid,          pid_t,       p)
2307   LSS_INLINE _syscall0(pid_t,   _gettid)
2308   LSS_INLINE _syscall5(int,     setxattr,        const char *,p,
2309                        const char *,   n,        const void *,v,
2310                        size_t,         s,        int,         f)
2311   LSS_INLINE _syscall5(int,     lsetxattr,       const char *,p,
2312                        const char *,   n,        const void *,v,
2313                        size_t,         s,        int,         f)
2314   LSS_INLINE _syscall4(ssize_t, getxattr,        const char *,p,
2315                        const char *,   n,        void *,      v, size_t, s)
2316   LSS_INLINE _syscall4(ssize_t, lgetxattr,       const char *,p,
2317                        const char *,   n,        void *,      v, size_t, s)
2318   LSS_INLINE _syscall3(ssize_t, listxattr,       const char *,p,
2319                        char *,   l,              size_t,      s)
2320   LSS_INLINE _syscall3(ssize_t, llistxattr,      const char *,p,
2321                        char *,   l,              size_t,      s)
2322   LSS_INLINE _syscall2(int,     ioprio_get,      int,         which,
2323                        int,     who)
2324   LSS_INLINE _syscall3(int,     ioprio_set,      int,         which,
2325                        int,     who,             int,         ioprio)
2326   LSS_INLINE _syscall2(int,     kill,            pid_t,       p,
2327                        int,            s)
2328   LSS_INLINE _syscall3(off_t,   lseek,           int,         f,
2329                        off_t,          o, int,    w)
2330   LSS_INLINE _syscall2(int,     munmap,          void*,       s,
2331                        size_t,         l)
2332   LSS_INLINE _syscall6(long,    move_pages,      pid_t,       p,
2333                        unsigned long,  n, void **,g, int *,   d,
2334                        int *,          s, int,    f)
2335   LSS_INLINE _syscall5(void*,   _mremap,         void*,       o,
2336                        size_t,         os,       size_t,      ns,
2337                        unsigned long,  f, void *, a)
2338   LSS_INLINE _syscall3(int,     open,            const char*, p,
2339                        int,            f, int,    m)
2340   LSS_INLINE _syscall3(int,     poll,           struct kernel_pollfd*, u,
2341                        unsigned int,   n, int,    t)
2342   LSS_INLINE _syscall2(int,     prctl,           int,         o,
2343                        long,           a)
2344   LSS_INLINE _syscall4(long,    ptrace,          int,         r,
2345                        pid_t,          p, void *, a, void *, d)
2346   LSS_INLINE _syscall3(ssize_t, read,            int,         f,
2347                        void *,         b, size_t, c)
2348   LSS_INLINE _syscall3(int,     readlink,        const char*, p,
2349                        char*,          b, size_t, s)
2350   LSS_INLINE _syscall4(int,     rt_sigaction,    int,         s,
2351                        const struct kernel_sigaction*, a,
2352                        struct kernel_sigaction*, o, size_t,   c)
2353   LSS_INLINE _syscall2(int, rt_sigpending, struct kernel_sigset_t *, s,
2354                        size_t,         c)
2355   LSS_INLINE _syscall4(int, rt_sigprocmask,      int,         h,
2356                        const struct kernel_sigset_t*,  s,
2357                        struct kernel_sigset_t*,        o, size_t, c);
2358   LSS_INLINE _syscall2(int, rt_sigsuspend,
2359                        const struct kernel_sigset_t*, s,  size_t, c);
2360   LSS_INLINE _syscall3(int,     sched_getaffinity,pid_t,      p,
2361                        unsigned int,   l, unsigned long *, m)
2362   LSS_INLINE _syscall3(int,     sched_setaffinity,pid_t,      p,
2363                        unsigned int,   l, unsigned long *, m)
2364   LSS_INLINE _syscall0(int,     sched_yield)
2365   LSS_INLINE _syscall1(long,    set_tid_address, int *,       t)
2366   LSS_INLINE _syscall1(int,     setfsgid,        gid_t,       g)
2367   LSS_INLINE _syscall1(int,     setfsuid,        uid_t,       u)
2368   LSS_INLINE _syscall1(int,     setuid,          uid_t,       u)
2369   LSS_INLINE _syscall1(int,     setgid,          gid_t,       g)
2370   LSS_INLINE _syscall2(int,     setpgid,         pid_t,       p,
2371                        pid_t,          g)
2372   LSS_INLINE _syscall3(int,     setpriority,     int,         a,
2373                        int,            b, int,    p)
2374   LSS_INLINE _syscall3(int,     setresgid,       gid_t,       r,
2375                        gid_t,          e, gid_t,  s)
2376   LSS_INLINE _syscall3(int,     setresuid,       uid_t,       r,
2377                        uid_t,          e, uid_t,  s)
2378   LSS_INLINE _syscall2(int,     setrlimit,       int,         r,
2379                        const struct kernel_rlimit*, l)
2380   LSS_INLINE _syscall0(pid_t,    setsid)
2381   LSS_INLINE _syscall2(int,     sigaltstack,     const stack_t*, s,
2382                        const stack_t*, o)
2383   LSS_INLINE _syscall2(int,     stat,            const char*, f,
2384                       struct kernel_stat*,   b)
2385   LSS_INLINE _syscall2(int,     statfs,          const char*, f,
2386                       struct kernel_statfs*, b)
2387   LSS_INLINE _syscall3(ssize_t, write,            int,        f,
2388                        const void *,   b, size_t, c)
2389   LSS_INLINE _syscall3(ssize_t, writev,           int,        f,
2390                        const struct kernel_iovec*, v, size_t, c)
2391   #if defined(__NR_getcpu)
2392     LSS_INLINE _syscall3(long, getcpu, unsigned *, cpu,
2393                          unsigned *, node, void *, unused);
2394   #endif
2395   #if defined(__x86_64__) ||                                                  \
2396      (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32)
_syscall3(int,recvmsg,int,s,struct kernel_msghdr *,m,int,f)2397     LSS_INLINE _syscall3(int, recvmsg,            int,   s,
2398                         struct kernel_msghdr*,     m, int, f)
2399     LSS_INLINE _syscall3(int, sendmsg,            int,   s,
2400                          const struct kernel_msghdr*, m, int, f)
2401     LSS_INLINE _syscall6(int, sendto,             int,   s,
2402                          const void*,             m, size_t, l,
2403                          int,                     f,
2404                          const struct kernel_sockaddr*, a, int, t)
2405     LSS_INLINE _syscall2(int, shutdown,           int,   s,
2406                          int,                     h)
2407     LSS_INLINE _syscall3(int, socket,             int,   d,
2408                          int,                     t, int,       p)
2409     LSS_INLINE _syscall4(int, socketpair,         int,   d,
2410                          int,                     t, int,       p, int*, s)
2411   #endif
2412   #if defined(__x86_64__)
2413     LSS_INLINE _syscall4(int, fallocate, int, fd, int, mode,
2414                          loff_t, offset, loff_t, len)
2415     LSS_INLINE _syscall6(void*, mmap,              void*, s,
2416                          size_t,                   l, int,               p,
2417                          int,                      f, int,               d,
2418                          __off64_t,                o)
2419     LSS_INLINE _syscall4(int, newfstatat,         int,   d,
2420                          const char *,            p,
2421                         struct kernel_stat*,       b, int, f)
2422 
2423     LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
2424       return LSS_NAME(setfsgid)(gid);
2425     }
2426 
LSS_NAME(setfsuid32)2427     LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
2428       return LSS_NAME(setfsuid)(uid);
2429     }
2430 
LSS_NAME(setresgid32)2431     LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
2432       return LSS_NAME(setresgid)(rgid, egid, sgid);
2433     }
2434 
LSS_NAME(setresuid32)2435     LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
2436       return LSS_NAME(setresuid)(ruid, euid, suid);
2437     }
2438 
LSS_NAME(sigaction)2439     LSS_INLINE int LSS_NAME(sigaction)(int signum,
2440                                        const struct kernel_sigaction *act,
2441                                        struct kernel_sigaction *oldact) {
2442       /* On x86_64, the kernel requires us to always set our own
2443        * SA_RESTORER in order to be able to return from a signal handler.
2444        * This function must have a "magic" signature that the "gdb"
2445        * (and maybe the kernel?) can recognize.
2446        */
2447       if (act != NULL && !(act->sa_flags & SA_RESTORER)) {
2448         struct kernel_sigaction a = *act;
2449         a.sa_flags   |= SA_RESTORER;
2450         a.sa_restorer = LSS_NAME(restore_rt)();
2451         return LSS_NAME(rt_sigaction)(signum, &a, oldact,
2452                                       (KERNEL_NSIG+7)/8);
2453       } else {
2454         return LSS_NAME(rt_sigaction)(signum, act, oldact,
2455                                       (KERNEL_NSIG+7)/8);
2456       }
2457     }
2458 
LSS_NAME(sigpending)2459     LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
2460       return LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
2461     }
2462 
LSS_NAME(sigprocmask)2463     LSS_INLINE int LSS_NAME(sigprocmask)(int how,
2464                                          const struct kernel_sigset_t *set,
2465                                          struct kernel_sigset_t *oldset) {
2466       return LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
2467     }
2468 
LSS_NAME(sigsuspend)2469     LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
2470       return LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
2471     }
2472   #endif
2473   #if defined(__x86_64__) || defined(__ARM_ARCH_3__) ||                       \
2474      (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32)
_syscall4(pid_t,wait4,pid_t,p,int *,s,int,o,struct kernel_rusage *,r)2475     LSS_INLINE _syscall4(pid_t, wait4,            pid_t, p,
2476                          int*,                    s, int,       o,
2477                         struct kernel_rusage*,     r)
2478 
2479     LSS_INLINE pid_t LSS_NAME(waitpid)(pid_t pid, int *status, int options){
2480       return LSS_NAME(wait4)(pid, status, options, 0);
2481     }
2482   #endif
2483   #if defined(__i386__) || defined(__x86_64__)
_syscall4(int,openat,int,d,const char *,p,int,f,int,m)2484     LSS_INLINE _syscall4(int, openat, int, d, const char *, p, int, f, int, m)
2485     LSS_INLINE _syscall3(int, unlinkat, int, d, const char *, p, int, f)
2486   #endif
2487   #if defined(__i386__) || defined(__ARM_ARCH_3__)
2488     #define __NR__setfsgid32  __NR_setfsgid32
2489     #define __NR__setfsuid32  __NR_setfsuid32
2490     #define __NR__setresgid32 __NR_setresgid32
2491     #define __NR__setresuid32 __NR_setresuid32
2492     LSS_INLINE _syscall2(int,   ugetrlimit,        int,          r,
2493                         struct kernel_rlimit*, l)
2494     LSS_INLINE _syscall1(int,     _setfsgid32,      gid_t,       f)
2495     LSS_INLINE _syscall1(int,     _setfsuid32,      uid_t,       f)
2496     LSS_INLINE _syscall3(int,     _setresgid32,    gid_t,       r,
2497                          gid_t,          e, gid_t,  s)
2498     LSS_INLINE _syscall3(int,     _setresuid32,    uid_t,       r,
2499                          uid_t,          e, uid_t,  s)
2500 
2501     LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
2502       int rc;
2503       if ((rc = LSS_NAME(_setfsgid32)(gid)) < 0 &&
2504           LSS_ERRNO == ENOSYS) {
2505         if ((unsigned int)gid & ~0xFFFFu) {
2506           rc = EINVAL;
2507         } else {
2508           rc = LSS_NAME(setfsgid)(gid);
2509         }
2510       }
2511       return rc;
2512     }
2513 
LSS_NAME(setfsuid32)2514     LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
2515       int rc;
2516       if ((rc = LSS_NAME(_setfsuid32)(uid)) < 0 &&
2517           LSS_ERRNO == ENOSYS) {
2518         if ((unsigned int)uid & ~0xFFFFu) {
2519           rc = EINVAL;
2520         } else {
2521           rc = LSS_NAME(setfsuid)(uid);
2522         }
2523       }
2524       return rc;
2525     }
2526 
LSS_NAME(setresgid32)2527     LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
2528       int rc;
2529       if ((rc = LSS_NAME(_setresgid32)(rgid, egid, sgid)) < 0 &&
2530           LSS_ERRNO == ENOSYS) {
2531         if ((unsigned int)rgid & ~0xFFFFu ||
2532             (unsigned int)egid & ~0xFFFFu ||
2533             (unsigned int)sgid & ~0xFFFFu) {
2534           rc = EINVAL;
2535         } else {
2536           rc = LSS_NAME(setresgid)(rgid, egid, sgid);
2537         }
2538       }
2539       return rc;
2540     }
2541 
LSS_NAME(setresuid32)2542     LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
2543       int rc;
2544       if ((rc = LSS_NAME(_setresuid32)(ruid, euid, suid)) < 0 &&
2545           LSS_ERRNO == ENOSYS) {
2546         if ((unsigned int)ruid & ~0xFFFFu ||
2547             (unsigned int)euid & ~0xFFFFu ||
2548             (unsigned int)suid & ~0xFFFFu) {
2549           rc = EINVAL;
2550         } else {
2551           rc = LSS_NAME(setresuid)(ruid, euid, suid);
2552         }
2553       }
2554       return rc;
2555     }
2556   #endif
LSS_NAME(sigemptyset)2557   LSS_INLINE int LSS_NAME(sigemptyset)(struct kernel_sigset_t *set) {
2558     memset(&set->sig, 0, sizeof(set->sig));
2559     return 0;
2560   }
2561 
LSS_NAME(sigfillset)2562   LSS_INLINE int LSS_NAME(sigfillset)(struct kernel_sigset_t *set) {
2563     memset(&set->sig, -1, sizeof(set->sig));
2564     return 0;
2565   }
2566 
LSS_NAME(sigaddset)2567   LSS_INLINE int LSS_NAME(sigaddset)(struct kernel_sigset_t *set,
2568                                      int signum) {
2569     if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
2570       LSS_ERRNO = EINVAL;
2571       return -1;
2572     } else {
2573       set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
2574           |= 1UL << ((signum - 1) % (8*sizeof(set->sig[0])));
2575       return 0;
2576     }
2577   }
2578 
LSS_NAME(sigdelset)2579   LSS_INLINE int LSS_NAME(sigdelset)(struct kernel_sigset_t *set,
2580                                         int signum) {
2581     if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
2582       LSS_ERRNO = EINVAL;
2583       return -1;
2584     } else {
2585       set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
2586           &= ~(1UL << ((signum - 1) % (8*sizeof(set->sig[0]))));
2587       return 0;
2588     }
2589   }
2590 
LSS_NAME(sigismember)2591   LSS_INLINE int LSS_NAME(sigismember)(struct kernel_sigset_t *set,
2592                                           int signum) {
2593     if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
2594       LSS_ERRNO = EINVAL;
2595       return -1;
2596     } else {
2597       return !!(set->sig[(signum - 1)/(8*sizeof(set->sig[0]))] &
2598                 (1UL << ((signum - 1) % (8*sizeof(set->sig[0])))));
2599     }
2600   }
2601   #if defined(__i386__) || defined(__ARM_ARCH_3__) ||                         \
2602      (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || defined(__PPC__)
2603     #define __NR__sigaction   __NR_sigaction
2604     #define __NR__sigpending  __NR_sigpending
2605     #define __NR__sigprocmask __NR_sigprocmask
2606     #define __NR__sigsuspend  __NR_sigsuspend
2607     #define __NR__socketcall  __NR_socketcall
_syscall2(int,fstat64,int,f,struct kernel_stat64 *,b)2608     LSS_INLINE _syscall2(int, fstat64,             int, f,
2609                          struct kernel_stat64 *, b)
2610     LSS_INLINE _syscall5(int, _llseek,     uint, fd, ulong, hi, ulong, lo,
2611                          loff_t *, res, uint, wh)
2612     LSS_INLINE _syscall1(void*, mmap,              void*, a)
2613 #ifndef __PPC64__
2614     LSS_INLINE _syscall6(void*, mmap2,             void*, s,
2615                          size_t,                   l, int,               p,
2616                          int,                      f, int,               d,
2617                          __off64_t,                o)
2618 #endif
2619     LSS_INLINE _syscall3(int,   _sigaction,        int,   s,
2620                          const struct kernel_old_sigaction*,  a,
2621                          struct kernel_old_sigaction*,        o)
2622     LSS_INLINE _syscall1(int,   _sigpending, unsigned long*, s)
2623     LSS_INLINE _syscall3(int,   _sigprocmask,      int,   h,
2624                          const unsigned long*,     s,
2625                          unsigned long*,           o)
2626     #ifdef __PPC__
2627     LSS_INLINE _syscall1(int, _sigsuspend,         unsigned long, s)
2628     #else
2629     LSS_INLINE _syscall3(int, _sigsuspend,         const void*, a,
2630                          int,                      b,
2631                          unsigned long,            s)
2632     #endif
2633     LSS_INLINE _syscall2(int, stat64,              const char *, p,
2634                          struct kernel_stat64 *, b)
2635 
2636     LSS_INLINE int LSS_NAME(sigaction)(int signum,
2637                                        const struct kernel_sigaction *act,
2638                                        struct kernel_sigaction *oldact) {
2639       int old_errno = LSS_ERRNO;
2640       int rc;
2641       struct kernel_sigaction a;
2642       if (act != NULL) {
2643         a             = *act;
2644         #ifdef __i386__
2645         /* On i386, the kernel requires us to always set our own
2646          * SA_RESTORER when using realtime signals. Otherwise, it does not
2647          * know how to return from a signal handler. This function must have
2648          * a "magic" signature that the "gdb" (and maybe the kernel?) can
2649          * recognize.
2650          * Apparently, a SA_RESTORER is implicitly set by the kernel, when
2651          * using non-realtime signals.
2652          *
2653          * TODO: Test whether ARM needs a restorer
2654          */
2655         if (!(a.sa_flags & SA_RESTORER)) {
2656           a.sa_flags   |= SA_RESTORER;
2657           a.sa_restorer = (a.sa_flags & SA_SIGINFO)
2658                           ? LSS_NAME(restore_rt)() : LSS_NAME(restore)();
2659         }
2660         #endif
2661       }
2662       rc = LSS_NAME(rt_sigaction)(signum, act ? &a : act, oldact,
2663                                   (KERNEL_NSIG+7)/8);
2664       if (rc < 0 && LSS_ERRNO == ENOSYS) {
2665         struct kernel_old_sigaction oa, ooa, *ptr_a = &oa, *ptr_oa = &ooa;
2666         if (!act) {
2667           ptr_a            = NULL;
2668         } else {
2669           oa.sa_handler_   = act->sa_handler_;
2670           memcpy(&oa.sa_mask, &act->sa_mask, sizeof(oa.sa_mask));
2671           #ifndef __mips__
2672           oa.sa_restorer   = act->sa_restorer;
2673           #endif
2674           oa.sa_flags      = act->sa_flags;
2675         }
2676         if (!oldact) {
2677           ptr_oa           = NULL;
2678         }
2679         LSS_ERRNO = old_errno;
2680         rc = LSS_NAME(_sigaction)(signum, ptr_a, ptr_oa);
2681         if (rc == 0 && oldact) {
2682           if (act) {
2683             memcpy(oldact, act, sizeof(*act));
2684           } else {
2685             memset(oldact, 0, sizeof(*oldact));
2686           }
2687           oldact->sa_handler_    = ptr_oa->sa_handler_;
2688           oldact->sa_flags       = ptr_oa->sa_flags;
2689           memcpy(&oldact->sa_mask, &ptr_oa->sa_mask, sizeof(ptr_oa->sa_mask));
2690           #ifndef __mips__
2691           oldact->sa_restorer    = ptr_oa->sa_restorer;
2692           #endif
2693         }
2694       }
2695       return rc;
2696     }
2697 
LSS_NAME(sigpending)2698     LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
2699       int old_errno = LSS_ERRNO;
2700       int rc = LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
2701       if (rc < 0 && LSS_ERRNO == ENOSYS) {
2702         LSS_ERRNO = old_errno;
2703         LSS_NAME(sigemptyset)(set);
2704         rc = LSS_NAME(_sigpending)(&set->sig[0]);
2705       }
2706       return rc;
2707     }
2708 
LSS_NAME(sigprocmask)2709     LSS_INLINE int LSS_NAME(sigprocmask)(int how,
2710                                          const struct kernel_sigset_t *set,
2711                                          struct kernel_sigset_t *oldset) {
2712       int olderrno = LSS_ERRNO;
2713       int rc = LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
2714       if (rc < 0 && LSS_ERRNO == ENOSYS) {
2715         LSS_ERRNO = olderrno;
2716         if (oldset) {
2717           LSS_NAME(sigemptyset)(oldset);
2718         }
2719         rc = LSS_NAME(_sigprocmask)(how,
2720                                     set ? &set->sig[0] : NULL,
2721                                     oldset ? &oldset->sig[0] : NULL);
2722       }
2723       return rc;
2724     }
2725 
LSS_NAME(sigsuspend)2726     LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
2727       int olderrno = LSS_ERRNO;
2728       int rc = LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
2729       if (rc < 0 && LSS_ERRNO == ENOSYS) {
2730         LSS_ERRNO = olderrno;
2731         rc = LSS_NAME(_sigsuspend)(
2732         #ifndef __PPC__
2733                                    set, 0,
2734         #endif
2735                                    set->sig[0]);
2736       }
2737       return rc;
2738     }
2739   #endif
2740   #if defined(__PPC__)
2741     #undef LSS_SC_LOADARGS_0
2742     #define LSS_SC_LOADARGS_0(dummy...)
2743     #undef LSS_SC_LOADARGS_1
2744     #define LSS_SC_LOADARGS_1(arg1)                                           \
2745         __sc_4  = (unsigned long) (arg1)
2746     #undef LSS_SC_LOADARGS_2
2747     #define LSS_SC_LOADARGS_2(arg1, arg2)                                     \
2748         LSS_SC_LOADARGS_1(arg1);                                              \
2749         __sc_5  = (unsigned long) (arg2)
2750     #undef LSS_SC_LOADARGS_3
2751     #define LSS_SC_LOADARGS_3(arg1, arg2, arg3)                               \
2752         LSS_SC_LOADARGS_2(arg1, arg2);                                        \
2753         __sc_6  = (unsigned long) (arg3)
2754     #undef LSS_SC_LOADARGS_4
2755     #define LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4)                         \
2756         LSS_SC_LOADARGS_3(arg1, arg2, arg3);                                  \
2757         __sc_7  = (unsigned long) (arg4)
2758     #undef LSS_SC_LOADARGS_5
2759     #define LSS_SC_LOADARGS_5(arg1, arg2, arg3, arg4, arg5)                   \
2760         LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4);                            \
2761         __sc_8  = (unsigned long) (arg5)
2762     #undef LSS_SC_BODY
2763     #define LSS_SC_BODY(nr, type, opt, args...)                               \
2764         long __sc_ret, __sc_err;                                              \
2765         {                                                                     \
2766           register unsigned long __sc_0 __asm__ ("r0") = __NR_socketcall;     \
2767           register unsigned long __sc_3 __asm__ ("r3") = opt;                 \
2768           register unsigned long __sc_4 __asm__ ("r4");                       \
2769           register unsigned long __sc_5 __asm__ ("r5");                       \
2770           register unsigned long __sc_6 __asm__ ("r6");                       \
2771           register unsigned long __sc_7 __asm__ ("r7");                       \
2772           register unsigned long __sc_8 __asm__ ("r8");                       \
2773           LSS_SC_LOADARGS_##nr(args);                                         \
2774           __asm__ __volatile__                                                \
2775               ("stwu 1, -48(1)\n\t"                                           \
2776                "stw 4, 20(1)\n\t"                                             \
2777                "stw 5, 24(1)\n\t"                                             \
2778                "stw 6, 28(1)\n\t"                                             \
2779                "stw 7, 32(1)\n\t"                                             \
2780                "stw 8, 36(1)\n\t"                                             \
2781                "addi 4, 1, 20\n\t"                                            \
2782                "sc\n\t"                                                       \
2783                "mfcr %0"                                                      \
2784                  : "=&r" (__sc_0),                                            \
2785                    "=&r" (__sc_3), "=&r" (__sc_4),                            \
2786                    "=&r" (__sc_5), "=&r" (__sc_6),                            \
2787                    "=&r" (__sc_7), "=&r" (__sc_8)                             \
2788                  : LSS_ASMINPUT_##nr                                          \
2789                  : "cr0", "ctr", "memory");                                   \
2790           __sc_ret = __sc_3;                                                  \
2791           __sc_err = __sc_0;                                                  \
2792         }                                                                     \
2793         LSS_RETURN(type, __sc_ret, __sc_err)
2794 
LSS_NAME(recvmsg)2795     LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
2796                                          int flags){
2797       LSS_SC_BODY(3, ssize_t, 17, s, msg, flags);
2798     }
2799 
LSS_NAME(sendmsg)2800     LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
2801                                          const struct kernel_msghdr *msg,
2802                                          int flags) {
2803       LSS_SC_BODY(3, ssize_t, 16, s, msg, flags);
2804     }
2805 
2806     // TODO(csilvers): why is this ifdef'ed out?
2807 #if 0
2808     LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
2809                                         int flags,
2810                                         const struct kernel_sockaddr *to,
2811                                         unsigned int tolen) {
2812       LSS_BODY(6, ssize_t, 11, s, buf, len, flags, to, tolen);
2813     }
2814 #endif
2815 
LSS_NAME(shutdown)2816     LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
2817       LSS_SC_BODY(2, int, 13, s, how);
2818     }
2819 
LSS_NAME(socket)2820     LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
2821       LSS_SC_BODY(3, int, 1, domain, type, protocol);
2822     }
2823 
LSS_NAME(socketpair)2824     LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
2825                                         int sv[2]) {
2826       LSS_SC_BODY(4, int, 8, d, type, protocol, sv);
2827     }
2828   #endif
2829   #if defined(__i386__) || defined(__ARM_ARCH_3__) ||                         \
2830       (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
2831     #define __NR__socketcall  __NR_socketcall
_syscall2(int,_socketcall,int,c,va_list,a)2832     LSS_INLINE _syscall2(int,      _socketcall,    int,   c,
2833                          va_list,                  a)
2834 
2835     LSS_INLINE int LSS_NAME(socketcall)(int op, ...) {
2836       int rc;
2837       va_list ap;
2838       va_start(ap, op);
2839       rc = LSS_NAME(_socketcall)(op, ap);
2840       va_end(ap);
2841       return rc;
2842     }
2843 
LSS_NAME(recvmsg)2844     LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
2845                                          int flags){
2846       return (ssize_t)LSS_NAME(socketcall)(17, s, msg, flags);
2847     }
2848 
LSS_NAME(sendmsg)2849     LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
2850                                          const struct kernel_msghdr *msg,
2851                                          int flags) {
2852       return (ssize_t)LSS_NAME(socketcall)(16, s, msg, flags);
2853     }
2854 
LSS_NAME(sendto)2855     LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
2856                                         int flags,
2857                                         const struct kernel_sockaddr *to,
2858                                         unsigned int tolen) {
2859       return (ssize_t)LSS_NAME(socketcall)(11, s, buf, len, flags, to, tolen);
2860     }
2861 
LSS_NAME(shutdown)2862     LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
2863       return LSS_NAME(socketcall)(13, s, how);
2864     }
2865 
LSS_NAME(socket)2866     LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
2867       return LSS_NAME(socketcall)(1, domain, type, protocol);
2868     }
2869 
LSS_NAME(socketpair)2870     LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
2871                                         int sv[2]) {
2872       return LSS_NAME(socketcall)(8, d, type, protocol, sv);
2873     }
2874   #endif
2875   #if defined(__i386__) || defined(__PPC__)
_syscall4(int,fstatat64,int,d,const char *,p,struct kernel_stat64 *,b,int,f)2876     LSS_INLINE _syscall4(int,   fstatat64,        int,   d,
2877                          const char *,      p,
2878                          struct kernel_stat64 *,   b,    int,   f)
2879   #endif
2880   #if defined(__i386__) || defined(__PPC__) ||                                \
2881      (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
2882     LSS_INLINE _syscall3(pid_t, waitpid,          pid_t, p,
2883                          int*,              s,    int,   o)
2884   #endif
2885   #if defined(__mips__)
2886     /* sys_pipe() on MIPS has non-standard calling conventions, as it returns
2887      * both file handles through CPU registers.
2888      */
2889     LSS_INLINE int LSS_NAME(pipe)(int *p) {
2890       register unsigned long __v0 __asm__("$2") = __NR_pipe;
2891       register unsigned long __v1 __asm__("$3");
2892       register unsigned long __r7 __asm__("$7");
2893       __asm__ __volatile__ ("syscall\n"
2894                             : "=&r"(__v0), "=&r"(__v1), "+r" (__r7)
2895                             : "0"(__v0)
2896                             : "$8", "$9", "$10", "$11", "$12",
2897                               "$13", "$14", "$15", "$24", "memory");
2898       if (__r7) {
2899         LSS_ERRNO = __v0;
2900         return -1;
2901       } else {
2902         p[0] = __v0;
2903         p[1] = __v1;
2904         return 0;
2905       }
2906     }
2907   #else
2908     LSS_INLINE _syscall1(int,     pipe,           int *, p)
2909   #endif
2910   /* TODO(csilvers): see if ppc can/should support this as well              */
2911   #if defined(__i386__) || defined(__ARM_ARCH_3__) ||                         \
2912      (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64)
2913     #define __NR__statfs64  __NR_statfs64
2914     #define __NR__fstatfs64 __NR_fstatfs64
_syscall3(int,_statfs64,const char *,p,size_t,s,struct kernel_statfs64 *,b)2915     LSS_INLINE _syscall3(int, _statfs64,     const char*, p,
2916                          size_t, s,struct kernel_statfs64*, b)
2917     LSS_INLINE _syscall3(int, _fstatfs64,          int,   f,
2918                          size_t, s,struct kernel_statfs64*, b)
2919     LSS_INLINE int LSS_NAME(statfs64)(const char *p,
2920                                      struct kernel_statfs64 *b) {
2921       return LSS_NAME(_statfs64)(p, sizeof(*b), b);
2922     }
LSS_NAME(fstatfs64)2923     LSS_INLINE int LSS_NAME(fstatfs64)(int f,struct kernel_statfs64 *b) {
2924       return LSS_NAME(_fstatfs64)(f, sizeof(*b), b);
2925     }
2926   #endif
2927 
LSS_NAME(execv)2928   LSS_INLINE int LSS_NAME(execv)(const char *path, const char *const argv[]) {
2929     extern char **environ;
2930     return LSS_NAME(execve)(path, argv, (const char *const *)environ);
2931   }
2932 
LSS_NAME(gettid)2933   LSS_INLINE pid_t LSS_NAME(gettid)() {
2934     pid_t tid = LSS_NAME(_gettid)();
2935     if (tid != -1) {
2936       return tid;
2937     }
2938     return LSS_NAME(getpid)();
2939   }
2940 
LSS_NAME(mremap)2941   LSS_INLINE void *LSS_NAME(mremap)(void *old_address, size_t old_size,
2942                                     size_t new_size, int flags, ...) {
2943     va_list ap;
2944     void *new_address, *rc;
2945     va_start(ap, flags);
2946     new_address = va_arg(ap, void *);
2947     rc = LSS_NAME(_mremap)(old_address, old_size, new_size,
2948                            flags, new_address);
2949     va_end(ap);
2950     return rc;
2951   }
2952 
LSS_NAME(ptrace_detach)2953   LSS_INLINE int LSS_NAME(ptrace_detach)(pid_t pid) {
2954     /* PTRACE_DETACH can sometimes forget to wake up the tracee and it
2955      * then sends job control signals to the real parent, rather than to
2956      * the tracer. We reduce the risk of this happening by starting a
2957      * whole new time slice, and then quickly sending a SIGCONT signal
2958      * right after detaching from the tracee.
2959      */
2960     int rc, err;
2961     LSS_NAME(sched_yield)();
2962     rc = LSS_NAME(ptrace)(PTRACE_DETACH, pid, (void *)0, (void *)0);
2963     err = LSS_ERRNO;
2964     LSS_NAME(kill)(pid, SIGCONT);
2965     LSS_ERRNO = err;
2966     return rc;
2967   }
2968 
LSS_NAME(raise)2969   LSS_INLINE int LSS_NAME(raise)(int sig) {
2970     return LSS_NAME(kill)(LSS_NAME(getpid)(), sig);
2971   }
2972 
LSS_NAME(setpgrp)2973   LSS_INLINE int LSS_NAME(setpgrp)() {
2974     return LSS_NAME(setpgid)(0, 0);
2975   }
2976 
LSS_NAME(sysconf)2977   LSS_INLINE int LSS_NAME(sysconf)(int name) {
2978     extern int __getpagesize(void);
2979     switch (name) {
2980       case _SC_OPEN_MAX: {
2981         struct kernel_rlimit limit;
2982         return LSS_NAME(getrlimit)(RLIMIT_NOFILE, &limit) < 0
2983                ? 8192 : limit.rlim_cur;
2984       }
2985       case _SC_PAGESIZE:
2986         return __getpagesize();
2987       default:
2988         errno = ENOSYS;
2989         return -1;
2990     }
2991   }
2992   #if defined(__x86_64__) ||                                                  \
2993      (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI64)
2994     LSS_INLINE _syscall4(ssize_t, pread64,        int,         f,
2995                          void *,         b, size_t,   c,
2996                          loff_t,         o)
2997     LSS_INLINE _syscall4(ssize_t, pwrite64,       int,         f,
2998                          const void *,   b, size_t,   c,
2999                          loff_t,         o)
3000     LSS_INLINE _syscall3(int,     readahead,      int,         f,
3001                          loff_t,         o, unsigned, c)
3002   #else
3003     #define __NR__pread64   __NR_pread64
3004     #define __NR__pwrite64  __NR_pwrite64
3005     #define __NR__readahead __NR_readahead
3006     LSS_INLINE _syscall5(ssize_t, _pread64,        int,         f,
3007                          void *,         b, size_t, c, unsigned, o1,
3008                          unsigned, o2)
3009     LSS_INLINE _syscall5(ssize_t, _pwrite64,       int,         f,
3010                          const void *,   b, size_t, c, unsigned, o1,
3011                          long, o2)
3012     LSS_INLINE _syscall4(int, _readahead,          int,         f,
3013                          unsigned,       o1, unsigned, o2, size_t, c);
3014     /* We force 64bit-wide parameters onto the stack, then access each
3015      * 32-bit component individually. This guarantees that we build the
3016      * correct parameters independent of the native byte-order of the
3017      * underlying architecture.
3018      */
3019     LSS_INLINE ssize_t LSS_NAME(pread64)(int fd, void *buf, size_t count,
3020                                          loff_t off) {
3021       union { loff_t off; unsigned arg[2]; } o = { off };
3022       return LSS_NAME(_pread64)(fd, buf, count, o.arg[0], o.arg[1]);
3023     }
3024     LSS_INLINE ssize_t LSS_NAME(pwrite64)(int fd, const void *buf,
3025                                           size_t count, loff_t off) {
3026       union { loff_t off; unsigned arg[2]; } o = { off };
3027       return LSS_NAME(_pwrite64)(fd, buf, count, o.arg[0], o.arg[1]);
3028     }
3029     LSS_INLINE int LSS_NAME(readahead)(int fd, loff_t off, int len) {
3030       union { loff_t off; unsigned arg[2]; } o = { off };
3031       return LSS_NAME(_readahead)(fd, o.arg[0], o.arg[1], len);
3032     }
3033   #endif
3034 #endif
3035 
3036 #if defined(__cplusplus) && !defined(SYS_CPLUSPLUS)
3037 }
3038 #endif
3039 
3040 #endif
3041 #endif
3042