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