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