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