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