blob: 7b7e59c3a4a1e437b92b2a8b06ef3a6e369bb551 [file] [log] [blame]
mcgrathr@google.coma7999932011-11-21 22:26:20 +00001/* Copyright (c) 2005-2011, Google Inc.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * ---
31 * Author: Markus Gutschke
32 */
33
34/* This file includes Linux-specific support functions common to the
35 * coredumper and the thread lister; primarily, this is a collection
36 * of direct system calls, and a couple of symbols missing from
37 * standard header files.
38 * There are a few options that the including file can set to control
39 * the behavior of this file:
40 *
41 * SYS_CPLUSPLUS:
42 * The entire header file will normally be wrapped in 'extern "C" { }",
43 * making it suitable for compilation as both C and C++ source. If you
44 * do not want to do this, you can set the SYS_CPLUSPLUS macro to inhibit
45 * the wrapping. N.B. doing so will suppress inclusion of all prerequisite
46 * system header files, too. It is the caller's responsibility to provide
47 * the necessary definitions.
48 *
49 * SYS_ERRNO:
50 * All system calls will update "errno" unless overriden by setting the
51 * SYS_ERRNO macro prior to including this file. SYS_ERRNO should be
52 * an l-value.
53 *
54 * SYS_INLINE:
55 * New symbols will be defined "static inline", unless overridden by
56 * the SYS_INLINE macro.
57 *
58 * SYS_LINUX_SYSCALL_SUPPORT_H
59 * This macro is used to avoid multiple inclusions of this header file.
60 * If you need to include this file more than once, make sure to
61 * unset SYS_LINUX_SYSCALL_SUPPORT_H before each inclusion.
62 *
63 * SYS_PREFIX:
64 * New system calls will have a prefix of "sys_" unless overridden by
65 * the SYS_PREFIX macro. Valid values for this macro are [0..9] which
66 * results in prefixes "sys[0..9]_". It is also possible to set this
67 * macro to -1, which avoids all prefixes.
68 *
69 * SYS_SYSCALL_ENTRYPOINT:
70 * Some applications (such as sandboxes that filter system calls), need
71 * to be able to run custom-code each time a system call is made. If this
72 * macro is defined, it expands to the name of a "common" symbol. If
73 * this symbol is assigned a non-NULL pointer value, it is used as the
74 * address of the system call entrypoint.
75 * A pointer to this symbol can be obtained by calling
76 * get_syscall_entrypoint()
77 *
78 * This file defines a few internal symbols that all start with "LSS_".
79 * Do not access these symbols from outside this file. They are not part
80 * of the supported API.
81 */
82#ifndef SYS_LINUX_SYSCALL_SUPPORT_H
83#define SYS_LINUX_SYSCALL_SUPPORT_H
84
85/* We currently only support x86-32, x86-64, ARM, MIPS, and PPC on Linux.
86 * Porting to other related platforms should not be difficult.
87 */
88#if (defined(__i386__) || defined(__x86_64__) || defined(__ARM_ARCH_3__) || \
89 defined(__mips__) || defined(__PPC__) || defined(__ARM_EABI__)) \
zodiac@gmail.com4f470182010-10-13 03:47:54 +000090 && (defined(__linux) || defined(__ANDROID__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +000091
92#ifndef SYS_CPLUSPLUS
93#ifdef __cplusplus
94/* Some system header files in older versions of gcc neglect to properly
95 * handle being included from C++. As it appears to be harmless to have
96 * multiple nested 'extern "C"' blocks, just add another one here.
97 */
98extern "C" {
99#endif
100
101#include <errno.h>
zodiac@gmail.com4f470182010-10-13 03:47:54 +0000102#include <fcntl.h>
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000103#include <signal.h>
104#include <stdarg.h>
105#include <stddef.h>
106#include <string.h>
107#include <sys/ptrace.h>
108#include <sys/resource.h>
109#include <sys/time.h>
110#include <sys/types.h>
zodiac@gmail.com4f470182010-10-13 03:47:54 +0000111#include <sys/syscall.h>
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000112#include <unistd.h>
113#include <linux/unistd.h>
114#include <endian.h>
115
116#ifdef __mips__
117/* Include definitions of the ABI currently in use. */
118#include <sgidefs.h>
119#endif
120#endif
121
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) { \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00001427 unsigned long __errnovalue = (res); \
1428 LSS_ERRNO = __errnovalue; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001429 res = -1; \
1430 } \
1431 return (type) (res); \
1432 } while (0)
1433 #elif defined(__PPC__)
1434 /* On PPC, failing system calls return -1, and set errno in a
1435 * separate CPU register. See linux/unistd.h.
1436 */
1437 #define LSS_RETURN(type, res, err) \
1438 do { \
1439 if (err & 0x10000000 ) { \
1440 LSS_ERRNO = (res); \
1441 res = -1; \
1442 } \
1443 return (type) (res); \
1444 } while (0)
1445 #endif
1446 #if defined(__i386__)
1447 /* In PIC mode (e.g. when building shared libraries), gcc for i386
1448 * reserves ebx. Unfortunately, most distribution ship with implementations
1449 * of _syscallX() which clobber ebx.
1450 * Also, most definitions of _syscallX() neglect to mark "memory" as being
1451 * clobbered. This causes problems with compilers, that do a better job
1452 * at optimizing across __asm__ calls.
1453 * So, we just have to redefine all of the _syscallX() macros.
1454 */
1455 #undef LSS_ENTRYPOINT
1456 #ifdef SYS_SYSCALL_ENTRYPOINT
1457 static inline void (**LSS_NAME(get_syscall_entrypoint)(void))(void) {
1458 void (**entrypoint)(void);
1459 asm volatile(".bss\n"
1460 ".align 8\n"
1461 ".globl "SYS_SYSCALL_ENTRYPOINT"\n"
1462 ".common "SYS_SYSCALL_ENTRYPOINT",8,8\n"
1463 ".previous\n"
1464 /* This logically does 'lea "SYS_SYSCALL_ENTRYPOINT", %0' */
1465 "call 0f\n"
1466 "0:pop %0\n"
1467 "add $_GLOBAL_OFFSET_TABLE_+[.-0b], %0\n"
1468 "mov "SYS_SYSCALL_ENTRYPOINT"@GOT(%0), %0\n"
1469 : "=r"(entrypoint));
1470 return entrypoint;
1471 }
1472
1473 #define LSS_ENTRYPOINT ".bss\n" \
1474 ".align 8\n" \
1475 ".globl "SYS_SYSCALL_ENTRYPOINT"\n" \
1476 ".common "SYS_SYSCALL_ENTRYPOINT",8,8\n" \
1477 ".previous\n" \
1478 /* Check the SYS_SYSCALL_ENTRYPOINT vector */ \
1479 "push %%eax\n" \
1480 "call 10000f\n" \
1481 "10000:pop %%eax\n" \
1482 "add $_GLOBAL_OFFSET_TABLE_+[.-10000b], %%eax\n" \
1483 "mov "SYS_SYSCALL_ENTRYPOINT"@GOT(%%eax), %%eax\n"\
1484 "mov 0(%%eax), %%eax\n" \
1485 "test %%eax, %%eax\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001486 "jz 10002f\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001487 "push %%eax\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001488 "call 10001f\n" \
1489 "10001:pop %%eax\n" \
1490 "add $(10003f-10001b), %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001491 "xchg 4(%%esp), %%eax\n" \
1492 "ret\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001493 "10002:pop %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001494 "int $0x80\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001495 "10003:\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001496 #else
1497 #define LSS_ENTRYPOINT "int $0x80\n"
1498 #endif
1499 #undef LSS_BODY
1500 #define LSS_BODY(type,args...) \
1501 long __res; \
1502 __asm__ __volatile__("push %%ebx\n" \
1503 "movl %2,%%ebx\n" \
1504 LSS_ENTRYPOINT \
1505 "pop %%ebx" \
1506 args \
1507 : "esp", "memory"); \
1508 LSS_RETURN(type,__res)
1509 #undef _syscall0
1510 #define _syscall0(type,name) \
1511 type LSS_NAME(name)(void) { \
1512 long __res; \
1513 __asm__ volatile(LSS_ENTRYPOINT \
1514 : "=a" (__res) \
1515 : "0" (__NR_##name) \
1516 : "esp", "memory"); \
1517 LSS_RETURN(type,__res); \
1518 }
1519 #undef _syscall1
1520 #define _syscall1(type,name,type1,arg1) \
1521 type LSS_NAME(name)(type1 arg1) { \
1522 LSS_BODY(type, \
1523 : "=a" (__res) \
1524 : "0" (__NR_##name), "ri" ((long)(arg1))); \
1525 }
1526 #undef _syscall2
1527 #define _syscall2(type,name,type1,arg1,type2,arg2) \
1528 type LSS_NAME(name)(type1 arg1,type2 arg2) { \
1529 LSS_BODY(type, \
1530 : "=a" (__res) \
1531 : "0" (__NR_##name),"ri" ((long)(arg1)), "c" ((long)(arg2))); \
1532 }
1533 #undef _syscall3
1534 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
1535 type LSS_NAME(name)(type1 arg1,type2 arg2,type3 arg3) { \
1536 LSS_BODY(type, \
1537 : "=a" (__res) \
1538 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
1539 "d" ((long)(arg3))); \
1540 }
1541 #undef _syscall4
1542 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
1543 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
1544 LSS_BODY(type, \
1545 : "=a" (__res) \
1546 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
1547 "d" ((long)(arg3)),"S" ((long)(arg4))); \
1548 }
1549 #undef _syscall5
1550 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1551 type5,arg5) \
1552 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1553 type5 arg5) { \
1554 long __res; \
1555 __asm__ __volatile__("push %%ebx\n" \
1556 "movl %2,%%ebx\n" \
1557 "movl %1,%%eax\n" \
1558 LSS_ENTRYPOINT \
1559 "pop %%ebx" \
1560 : "=a" (__res) \
1561 : "i" (__NR_##name), "ri" ((long)(arg1)), \
1562 "c" ((long)(arg2)), "d" ((long)(arg3)), \
1563 "S" ((long)(arg4)), "D" ((long)(arg5)) \
1564 : "esp", "memory"); \
1565 LSS_RETURN(type,__res); \
1566 }
1567 #undef _syscall6
1568 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1569 type5,arg5,type6,arg6) \
1570 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1571 type5 arg5, type6 arg6) { \
1572 long __res; \
1573 struct { long __a1; long __a6; } __s = { (long)arg1, (long) arg6 }; \
1574 __asm__ __volatile__("push %%ebp\n" \
1575 "push %%ebx\n" \
1576 "movl 4(%2),%%ebp\n" \
1577 "movl 0(%2), %%ebx\n" \
1578 "movl %1,%%eax\n" \
1579 LSS_ENTRYPOINT \
1580 "pop %%ebx\n" \
1581 "pop %%ebp" \
1582 : "=a" (__res) \
1583 : "i" (__NR_##name), "0" ((long)(&__s)), \
1584 "c" ((long)(arg2)), "d" ((long)(arg3)), \
1585 "S" ((long)(arg4)), "D" ((long)(arg5)) \
1586 : "esp", "memory"); \
1587 LSS_RETURN(type,__res); \
1588 }
1589 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
1590 int flags, void *arg, int *parent_tidptr,
1591 void *newtls, int *child_tidptr) {
1592 long __res;
1593 __asm__ __volatile__(/* if (fn == NULL)
1594 * return -EINVAL;
1595 */
1596 "movl %3,%%ecx\n"
1597 "jecxz 1f\n"
1598
1599 /* if (child_stack == NULL)
1600 * return -EINVAL;
1601 */
1602 "movl %4,%%ecx\n"
1603 "jecxz 1f\n"
1604
1605 /* Set up alignment of the child stack:
1606 * child_stack = (child_stack & ~0xF) - 20;
1607 */
1608 "andl $-16,%%ecx\n"
1609 "subl $20,%%ecx\n"
1610
1611 /* Push "arg" and "fn" onto the stack that will be
1612 * used by the child.
1613 */
1614 "movl %6,%%eax\n"
1615 "movl %%eax,4(%%ecx)\n"
1616 "movl %3,%%eax\n"
1617 "movl %%eax,(%%ecx)\n"
1618
1619 /* %eax = syscall(%eax = __NR_clone,
1620 * %ebx = flags,
1621 * %ecx = child_stack,
1622 * %edx = parent_tidptr,
1623 * %esi = newtls,
1624 * %edi = child_tidptr)
1625 * Also, make sure that %ebx gets preserved as it is
1626 * used in PIC mode.
1627 */
1628 "movl %8,%%esi\n"
1629 "movl %7,%%edx\n"
1630 "movl %5,%%eax\n"
1631 "movl %9,%%edi\n"
1632 "pushl %%ebx\n"
1633 "movl %%eax,%%ebx\n"
1634 "movl %2,%%eax\n"
1635 LSS_ENTRYPOINT
1636
1637 /* In the parent: restore %ebx
1638 * In the child: move "fn" into %ebx
1639 */
1640 "popl %%ebx\n"
1641
1642 /* if (%eax != 0)
1643 * return %eax;
1644 */
1645 "test %%eax,%%eax\n"
1646 "jnz 1f\n"
1647
1648 /* In the child, now. Terminate frame pointer chain.
1649 */
1650 "movl $0,%%ebp\n"
1651
1652 /* Call "fn". "arg" is already on the stack.
1653 */
1654 "call *%%ebx\n"
1655
1656 /* Call _exit(%ebx). Unfortunately older versions
1657 * of gcc restrict the number of arguments that can
1658 * be passed to asm(). So, we need to hard-code the
1659 * system call number.
1660 */
1661 "movl %%eax,%%ebx\n"
1662 "movl $1,%%eax\n"
1663 LSS_ENTRYPOINT
1664
1665 /* Return to parent.
1666 */
1667 "1:\n"
1668 : "=a" (__res)
1669 : "0"(-EINVAL), "i"(__NR_clone),
1670 "m"(fn), "m"(child_stack), "m"(flags), "m"(arg),
1671 "m"(parent_tidptr), "m"(newtls), "m"(child_tidptr)
1672 : "esp", "memory", "ecx", "edx", "esi", "edi");
1673 LSS_RETURN(int, __res);
1674 }
1675
1676 #define __NR__fadvise64_64 __NR_fadvise64_64
1677 LSS_INLINE _syscall6(int, _fadvise64_64, int, fd,
1678 unsigned, offset_lo, unsigned, offset_hi,
1679 unsigned, len_lo, unsigned, len_hi,
1680 int, advice)
1681
1682 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
1683 loff_t len, int advice) {
1684 return LSS_NAME(_fadvise64_64)(fd,
1685 (unsigned)offset, (unsigned)(offset >>32),
1686 (unsigned)len, (unsigned)(len >> 32),
1687 advice);
1688 }
1689
1690 #define __NR__fallocate __NR_fallocate
1691 LSS_INLINE _syscall6(int, _fallocate, int, fd,
1692 int, mode,
1693 unsigned, offset_lo, unsigned, offset_hi,
1694 unsigned, len_lo, unsigned, len_hi)
1695
1696 LSS_INLINE int LSS_NAME(fallocate)(int fd, int mode,
1697 loff_t offset, loff_t len) {
1698 union { loff_t off; unsigned w[2]; } o = { offset }, l = { len };
1699 return LSS_NAME(_fallocate)(fd, mode, o.w[0], o.w[1], l.w[0], l.w[1]);
1700 }
1701
1702 LSS_INLINE _syscall1(int, set_thread_area, void *, u)
1703 LSS_INLINE _syscall1(int, get_thread_area, void *, u)
1704
1705 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
1706 /* On i386, the kernel does not know how to return from a signal
1707 * handler. Instead, it relies on user space to provide a
1708 * restorer function that calls the {rt_,}sigreturn() system call.
1709 * Unfortunately, we cannot just reference the glibc version of this
1710 * function, as glibc goes out of its way to make it inaccessible.
1711 */
1712 void (*res)(void);
1713 __asm__ __volatile__("call 2f\n"
1714 "0:.align 16\n"
1715 "1:movl %1,%%eax\n"
1716 LSS_ENTRYPOINT
1717 "2:popl %0\n"
1718 "addl $(1b-0b),%0\n"
1719 : "=a" (res)
1720 : "i" (__NR_rt_sigreturn));
1721 return res;
1722 }
1723 LSS_INLINE void (*LSS_NAME(restore)(void))(void) {
1724 /* On i386, the kernel does not know how to return from a signal
1725 * handler. Instead, it relies on user space to provide a
1726 * restorer function that calls the {rt_,}sigreturn() system call.
1727 * Unfortunately, we cannot just reference the glibc version of this
1728 * function, as glibc goes out of its way to make it inaccessible.
1729 */
1730 void (*res)(void);
1731 __asm__ __volatile__("call 2f\n"
1732 "0:.align 16\n"
1733 "1:pop %%eax\n"
1734 "movl %1,%%eax\n"
1735 LSS_ENTRYPOINT
1736 "2:popl %0\n"
1737 "addl $(1b-0b),%0\n"
1738 : "=a" (res)
1739 : "i" (__NR_sigreturn));
1740 return res;
1741 }
1742 #elif defined(__x86_64__)
1743 /* There are no known problems with any of the _syscallX() macros
1744 * currently shipping for x86_64, but we still need to be able to define
1745 * our own version so that we can override the location of the errno
1746 * location (e.g. when using the clone() system call with the CLONE_VM
1747 * option).
1748 */
1749 #undef LSS_ENTRYPOINT
1750 #ifdef SYS_SYSCALL_ENTRYPOINT
1751 static inline void (**LSS_NAME(get_syscall_entrypoint)(void))(void) {
1752 void (**entrypoint)(void);
1753 asm volatile(".bss\n"
1754 ".align 8\n"
1755 ".globl "SYS_SYSCALL_ENTRYPOINT"\n"
1756 ".common "SYS_SYSCALL_ENTRYPOINT",8,8\n"
1757 ".previous\n"
1758 "mov "SYS_SYSCALL_ENTRYPOINT"@GOTPCREL(%%rip), %0\n"
1759 : "=r"(entrypoint));
1760 return entrypoint;
1761 }
1762
1763 #define LSS_ENTRYPOINT \
1764 ".bss\n" \
1765 ".align 8\n" \
1766 ".globl "SYS_SYSCALL_ENTRYPOINT"\n" \
1767 ".common "SYS_SYSCALL_ENTRYPOINT",8,8\n" \
1768 ".previous\n" \
1769 "mov "SYS_SYSCALL_ENTRYPOINT"@GOTPCREL(%%rip), %%rcx\n" \
1770 "mov 0(%%rcx), %%rcx\n" \
1771 "test %%rcx, %%rcx\n" \
1772 "jz 10001f\n" \
1773 "call *%%rcx\n" \
1774 "jmp 10002f\n" \
1775 "10001:syscall\n" \
1776 "10002:\n"
1777
1778 #else
1779 #define LSS_ENTRYPOINT "syscall\n"
1780 #endif
1781 #undef LSS_BODY
1782 #define LSS_BODY(type,name, ...) \
1783 long __res; \
1784 __asm__ __volatile__(LSS_ENTRYPOINT \
1785 : "=a" (__res) : "0" (__NR_##name), \
1786 ##__VA_ARGS__ : "r11", "rcx", "memory"); \
1787 LSS_RETURN(type, __res)
1788 #undef _syscall0
1789 #define _syscall0(type,name) \
1790 type LSS_NAME(name)() { \
1791 LSS_BODY(type, name); \
1792 }
1793 #undef _syscall1
1794 #define _syscall1(type,name,type1,arg1) \
1795 type LSS_NAME(name)(type1 arg1) { \
1796 LSS_BODY(type, name, "D" ((long)(arg1))); \
1797 }
1798 #undef _syscall2
1799 #define _syscall2(type,name,type1,arg1,type2,arg2) \
1800 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
1801 LSS_BODY(type, name, "D" ((long)(arg1)), "S" ((long)(arg2))); \
1802 }
1803 #undef _syscall3
1804 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
1805 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
1806 LSS_BODY(type, name, "D" ((long)(arg1)), "S" ((long)(arg2)), \
1807 "d" ((long)(arg3))); \
1808 }
1809 #undef _syscall4
1810 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
1811 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
1812 long __res; \
1813 __asm__ __volatile__("movq %5,%%r10;" LSS_ENTRYPOINT : \
1814 "=a" (__res) : "0" (__NR_##name), \
1815 "D" ((long)(arg1)), "S" ((long)(arg2)), "d" ((long)(arg3)), \
1816 "r" ((long)(arg4)) : "r10", "r11", "rcx", "memory"); \
1817 LSS_RETURN(type, __res); \
1818 }
1819 #undef _syscall5
1820 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1821 type5,arg5) \
1822 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1823 type5 arg5) { \
1824 long __res; \
1825 __asm__ __volatile__("movq %5,%%r10; movq %6,%%r8;" LSS_ENTRYPOINT :\
1826 "=a" (__res) : "0" (__NR_##name), \
1827 "D" ((long)(arg1)), "S" ((long)(arg2)), "d" ((long)(arg3)), \
1828 "r" ((long)(arg4)), "r" ((long)(arg5)) : \
1829 "r8", "r10", "r11", "rcx", "memory"); \
1830 LSS_RETURN(type, __res); \
1831 }
1832 #undef _syscall6
1833 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1834 type5,arg5,type6,arg6) \
1835 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1836 type5 arg5, type6 arg6) { \
1837 long __res; \
1838 __asm__ __volatile__("movq %5,%%r10; movq %6,%%r8; movq %7,%%r9;" \
1839 LSS_ENTRYPOINT : \
1840 "=a" (__res) : "0" (__NR_##name), \
1841 "D" ((long)(arg1)), "S" ((long)(arg2)), "d" ((long)(arg3)), \
1842 "r" ((long)(arg4)), "r" ((long)(arg5)), "r" ((long)(arg6)) : \
1843 "r8", "r9", "r10", "r11", "rcx", "memory"); \
1844 LSS_RETURN(type, __res); \
1845 }
1846 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
1847 int flags, void *arg, int *parent_tidptr,
1848 void *newtls, int *child_tidptr) {
1849 long __res;
1850 {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001851 __asm__ __volatile__(/* if (fn == NULL)
1852 * return -EINVAL;
1853 */
1854 "testq %4,%4\n"
1855 "jz 1f\n"
1856
1857 /* if (child_stack == NULL)
1858 * return -EINVAL;
1859 */
1860 "testq %5,%5\n"
1861 "jz 1f\n"
1862
1863 /* childstack -= 2*sizeof(void *);
1864 */
1865 "subq $16,%5\n"
1866
1867 /* Push "arg" and "fn" onto the stack that will be
1868 * used by the child.
1869 */
1870 "movq %7,8(%5)\n"
1871 "movq %4,0(%5)\n"
1872
1873 /* %rax = syscall(%rax = __NR_clone,
1874 * %rdi = flags,
1875 * %rsi = child_stack,
1876 * %rdx = parent_tidptr,
1877 * %r8 = new_tls,
1878 * %r10 = child_tidptr)
1879 */
1880 "movq %2,%%rax\n"
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00001881 "movq %9,%%r8\n"
1882 "movq %10,%%r10\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001883 LSS_ENTRYPOINT
1884
1885 /* if (%rax != 0)
1886 * return;
1887 */
1888 "testq %%rax,%%rax\n"
1889 "jnz 1f\n"
1890
1891 /* In the child. Terminate frame pointer chain.
1892 */
1893 "xorq %%rbp,%%rbp\n"
1894
1895 /* Call "fn(arg)".
1896 */
1897 "popq %%rax\n"
1898 "popq %%rdi\n"
1899 "call *%%rax\n"
1900
1901 /* Call _exit(%ebx).
1902 */
1903 "movq %%rax,%%rdi\n"
1904 "movq %3,%%rax\n"
1905 LSS_ENTRYPOINT
1906
1907 /* Return to parent.
1908 */
1909 "1:\n"
1910 : "=a" (__res)
1911 : "0"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit),
1912 "r"(fn), "S"(child_stack), "D"(flags), "r"(arg),
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00001913 "d"(parent_tidptr), "r"(newtls),
1914 "r"(child_tidptr)
1915 : "rsp", "memory", "r8", "r10", "r11", "rcx");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001916 }
1917 LSS_RETURN(int, __res);
1918 }
1919 LSS_INLINE _syscall2(int, arch_prctl, int, c, void *, a)
1920 LSS_INLINE _syscall4(int, fadvise64, int, fd, loff_t, offset, loff_t, len,
1921 int, advice)
1922
1923 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
1924 /* On x86-64, the kernel does not know how to return from
1925 * a signal handler. Instead, it relies on user space to provide a
1926 * restorer function that calls the rt_sigreturn() system call.
1927 * Unfortunately, we cannot just reference the glibc version of this
1928 * function, as glibc goes out of its way to make it inaccessible.
1929 */
1930 void (*res)(void);
1931 __asm__ __volatile__("call 2f\n"
1932 "0:.align 16\n"
1933 "1:movq %1,%%rax\n"
1934 LSS_ENTRYPOINT
1935 "2:popq %0\n"
1936 "addq $(1b-0b),%0\n"
1937 : "=a" (res)
1938 : "i" (__NR_rt_sigreturn));
1939 return res;
1940 }
1941 #elif defined(__ARM_ARCH_3__)
1942 /* Most definitions of _syscallX() neglect to mark "memory" as being
1943 * clobbered. This causes problems with compilers, that do a better job
1944 * at optimizing across __asm__ calls.
1945 * So, we just have to redefine all of the _syscallX() macros.
1946 */
1947 #undef LSS_REG
1948 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
1949 #undef LSS_BODY
1950 #define LSS_BODY(type,name,args...) \
1951 register long __res_r0 __asm__("r0"); \
1952 long __res; \
1953 __asm__ __volatile__ (__syscall(name) \
1954 : "=r"(__res_r0) : args : "lr", "memory"); \
1955 __res = __res_r0; \
1956 LSS_RETURN(type, __res)
1957 #undef _syscall0
1958 #define _syscall0(type, name) \
1959 type LSS_NAME(name)() { \
1960 LSS_BODY(type, name); \
1961 }
1962 #undef _syscall1
1963 #define _syscall1(type, name, type1, arg1) \
1964 type LSS_NAME(name)(type1 arg1) { \
1965 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
1966 }
1967 #undef _syscall2
1968 #define _syscall2(type, name, type1, arg1, type2, arg2) \
1969 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
1970 LSS_REG(0, arg1); LSS_REG(1, arg2); \
1971 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
1972 }
1973 #undef _syscall3
1974 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
1975 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
1976 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
1977 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
1978 }
1979 #undef _syscall4
1980 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
1981 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
1982 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
1983 LSS_REG(3, arg4); \
1984 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
1985 }
1986 #undef _syscall5
1987 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1988 type5,arg5) \
1989 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1990 type5 arg5) { \
1991 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
1992 LSS_REG(3, arg4); LSS_REG(4, arg5); \
1993 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
1994 "r"(__r4)); \
1995 }
1996 #undef _syscall6
1997 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1998 type5,arg5,type6,arg6) \
1999 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2000 type5 arg5, type6 arg6) { \
2001 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2002 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2003 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2004 "r"(__r4), "r"(__r5)); \
2005 }
2006 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2007 int flags, void *arg, int *parent_tidptr,
2008 void *newtls, int *child_tidptr) {
2009 long __res;
2010 {
2011 register int __flags __asm__("r0") = flags;
2012 register void *__stack __asm__("r1") = child_stack;
2013 register void *__ptid __asm__("r2") = parent_tidptr;
2014 register void *__tls __asm__("r3") = newtls;
2015 register int *__ctid __asm__("r4") = child_tidptr;
2016 __asm__ __volatile__(/* if (fn == NULL || child_stack == NULL)
2017 * return -EINVAL;
2018 */
2019 "cmp %2,#0\n"
2020 "cmpne %3,#0\n"
2021 "moveq %0,%1\n"
2022 "beq 1f\n"
2023
2024 /* Push "arg" and "fn" onto the stack that will be
2025 * used by the child.
2026 */
2027 "str %5,[%3,#-4]!\n"
2028 "str %2,[%3,#-4]!\n"
2029
2030 /* %r0 = syscall(%r0 = flags,
2031 * %r1 = child_stack,
2032 * %r2 = parent_tidptr,
2033 * %r3 = newtls,
2034 * %r4 = child_tidptr)
2035 */
2036 __syscall(clone)"\n"
2037
2038 /* if (%r0 != 0)
2039 * return %r0;
2040 */
2041 "movs %0,r0\n"
2042 "bne 1f\n"
2043
2044 /* In the child, now. Call "fn(arg)".
2045 */
2046 "ldr r0,[sp, #4]\n"
2047 "mov lr,pc\n"
2048 "ldr pc,[sp]\n"
2049
2050 /* Call _exit(%r0).
2051 */
2052 __syscall(exit)"\n"
2053 "1:\n"
2054 : "=r" (__res)
2055 : "i"(-EINVAL),
2056 "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2057 "r"(__ptid), "r"(__tls), "r"(__ctid)
2058 : "cc", "lr", "memory");
2059 }
2060 LSS_RETURN(int, __res);
2061 }
2062 #elif defined(__ARM_EABI__)
2063 /* Most definitions of _syscallX() neglect to mark "memory" as being
2064 * clobbered. This causes problems with compilers, that do a better job
2065 * at optimizing across __asm__ calls.
2066 * So, we just have to redefine all fo the _syscallX() macros.
2067 */
2068 #undef LSS_REG
2069 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
2070 #undef LSS_BODY
2071 #define LSS_BODY(type,name,args...) \
2072 register long __res_r0 __asm__("r0"); \
2073 long __res; \
2074 __asm__ __volatile__ ("push {r7}\n" \
2075 "mov r7, %1\n" \
2076 "swi 0x0\n" \
2077 "pop {r7}\n" \
2078 : "=r"(__res_r0) \
2079 : "i"(__NR_##name) , ## args \
2080 : "lr", "memory"); \
2081 __res = __res_r0; \
2082 LSS_RETURN(type, __res)
2083 #undef _syscall0
2084 #define _syscall0(type, name) \
2085 type LSS_NAME(name)() { \
2086 LSS_BODY(type, name); \
2087 }
2088 #undef _syscall1
2089 #define _syscall1(type, name, type1, arg1) \
2090 type LSS_NAME(name)(type1 arg1) { \
2091 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2092 }
2093 #undef _syscall2
2094 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2095 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2096 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2097 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2098 }
2099 #undef _syscall3
2100 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2101 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2102 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2103 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2104 }
2105 #undef _syscall4
2106 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2107 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2108 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2109 LSS_REG(3, arg4); \
2110 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2111 }
2112 #undef _syscall5
2113 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2114 type5,arg5) \
2115 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2116 type5 arg5) { \
2117 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2118 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2119 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2120 "r"(__r4)); \
2121 }
2122 #undef _syscall6
2123 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2124 type5,arg5,type6,arg6) \
2125 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2126 type5 arg5, type6 arg6) { \
2127 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2128 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2129 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2130 "r"(__r4), "r"(__r5)); \
2131 }
2132 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2133 int flags, void *arg, int *parent_tidptr,
2134 void *newtls, int *child_tidptr) {
2135 long __res;
2136 {
2137 register int __flags __asm__("r0") = flags;
2138 register void *__stack __asm__("r1") = child_stack;
2139 register void *__ptid __asm__("r2") = parent_tidptr;
2140 register void *__tls __asm__("r3") = newtls;
2141 register int *__ctid __asm__("r4") = child_tidptr;
2142 __asm__ __volatile__(/* if (fn == NULL || child_stack == NULL)
2143 * return -EINVAL;
2144 */
2145 "cmp %2,#0\n"
zodiac@gmail.com4f470182010-10-13 03:47:54 +00002146 "it ne\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002147 "cmpne %3,#0\n"
zodiac@gmail.com4f470182010-10-13 03:47:54 +00002148 "it eq\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002149 "moveq %0,%1\n"
2150 "beq 1f\n"
2151
2152 /* Push "arg" and "fn" onto the stack that will be
2153 * used by the child.
2154 */
2155 "str %5,[%3,#-4]!\n"
2156 "str %2,[%3,#-4]!\n"
2157
2158 /* %r0 = syscall(%r0 = flags,
2159 * %r1 = child_stack,
2160 * %r2 = parent_tidptr,
2161 * %r3 = newtls,
2162 * %r4 = child_tidptr)
2163 */
2164 "mov r7, %9\n"
2165 "swi 0x0\n"
2166
2167 /* if (%r0 != 0)
2168 * return %r0;
2169 */
2170 "movs %0,r0\n"
2171 "bne 1f\n"
2172
2173 /* In the child, now. Call "fn(arg)".
2174 */
2175 "ldr r0,[sp, #4]\n"
zodiac@gmail.com68c659b2011-10-06 05:34:19 +00002176
2177 /* When compiling for Thumb-2 the "MOV LR,PC" here
2178 * won't work because it loads PC+4 into LR,
2179 * whereas the LDR is a 4-byte instruction.
2180 * This results in the child thread always
2181 * crashing with an "Illegal Instruction" when it
2182 * returned into the middle of the LDR instruction
2183 * The instruction sequence used instead was
2184 * recommended by
2185 * "https://wiki.edubuntu.org/ARM/Thumb2PortingHowto#Quick_Reference".
2186 */
2187 #ifdef __thumb2__
2188 "ldr r7,[sp]\n"
2189 "blx r7\n"
2190 #else
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002191 "mov lr,pc\n"
2192 "ldr pc,[sp]\n"
zodiac@gmail.com68c659b2011-10-06 05:34:19 +00002193 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002194
2195 /* Call _exit(%r0).
2196 */
2197 "mov r7, %10\n"
2198 "swi 0x0\n"
2199 "1:\n"
2200 : "=r" (__res)
2201 : "i"(-EINVAL),
2202 "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2203 "r"(__ptid), "r"(__tls), "r"(__ctid),
2204 "i"(__NR_clone), "i"(__NR_exit)
2205 : "cc", "r7", "lr", "memory");
2206 }
2207 LSS_RETURN(int, __res);
2208 }
2209 #elif defined(__mips__)
2210 #undef LSS_REG
2211 #define LSS_REG(r,a) register unsigned long __r##r __asm__("$"#r) = \
2212 (unsigned long)(a)
2213 #undef LSS_BODY
2214 #define LSS_BODY(type,name,r7,...) \
2215 register unsigned long __v0 __asm__("$2") = __NR_##name; \
2216 __asm__ __volatile__ ("syscall\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002217 : "+r"(__v0), r7 (__r7) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002218 : "0"(__v0), ##__VA_ARGS__ \
2219 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002220 "$13", "$14", "$15", "$24", "$25", \
2221 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002222 LSS_RETURN(type, __v0, __r7)
2223 #undef _syscall0
2224 #define _syscall0(type, name) \
2225 type LSS_NAME(name)() { \
2226 register unsigned long __r7 __asm__("$7"); \
2227 LSS_BODY(type, name, "=r"); \
2228 }
2229 #undef _syscall1
2230 #define _syscall1(type, name, type1, arg1) \
2231 type LSS_NAME(name)(type1 arg1) { \
2232 register unsigned long __r7 __asm__("$7"); \
2233 LSS_REG(4, arg1); LSS_BODY(type, name, "=r", "r"(__r4)); \
2234 }
2235 #undef _syscall2
2236 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2237 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2238 register unsigned long __r7 __asm__("$7"); \
2239 LSS_REG(4, arg1); LSS_REG(5, arg2); \
2240 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5)); \
2241 }
2242 #undef _syscall3
2243 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2244 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2245 register unsigned long __r7 __asm__("$7"); \
2246 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2247 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5), "r"(__r6)); \
2248 }
2249 #undef _syscall4
2250 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2251 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2252 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2253 LSS_REG(7, arg4); \
2254 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6)); \
2255 }
2256 #undef _syscall5
2257 #if _MIPS_SIM == _MIPS_SIM_ABI32
2258 /* The old 32bit MIPS system call API passes the fifth and sixth argument
2259 * on the stack, whereas the new APIs use registers "r8" and "r9".
2260 */
2261 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2262 type5,arg5) \
2263 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2264 type5 arg5) { \
2265 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2266 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002267 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002268 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002269 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002270 "sw %5, 16($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002271 "syscall\n" \
2272 "addiu $29, 32\n" \
2273 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002274 : "+r"(__v0), "+r" (__r7) \
2275 : "r"(__r4), "r"(__r5), \
2276 "r"(__r6), "r" ((unsigned long)arg5) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002277 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002278 "$13", "$14", "$15", "$24", "$25", \
2279 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002280 LSS_RETURN(type, __v0, __r7); \
2281 }
2282 #else
2283 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2284 type5,arg5) \
2285 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2286 type5 arg5) { \
2287 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2288 LSS_REG(7, arg4); LSS_REG(8, arg5); \
2289 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
2290 "r"(__r8)); \
2291 }
2292 #endif
2293 #undef _syscall6
2294 #if _MIPS_SIM == _MIPS_SIM_ABI32
2295 /* The old 32bit MIPS system call API passes the fifth and sixth argument
2296 * on the stack, whereas the new APIs use registers "r8" and "r9".
2297 */
2298 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2299 type5,arg5,type6,arg6) \
2300 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2301 type5 arg5, type6 arg6) { \
2302 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2303 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002304 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002305 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002306 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002307 "sw %5, 16($29)\n" \
2308 "sw %6, 20($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002309 "syscall\n" \
2310 "addiu $29, 32\n" \
2311 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002312 : "+r"(__v0), "+r" (__r7) \
2313 : "r"(__r4), "r"(__r5), \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002314 "r"(__r6), "r" ((unsigned long)arg5), \
2315 "r" ((unsigned long)arg6) \
2316 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002317 "$13", "$14", "$15", "$24", "$25", \
2318 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002319 LSS_RETURN(type, __v0, __r7); \
2320 }
2321 #else
2322 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2323 type5,arg5,type6,arg6) \
2324 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2325 type5 arg5,type6 arg6) { \
2326 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2327 LSS_REG(7, arg4); LSS_REG(8, arg5); LSS_REG(9, arg6); \
2328 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
2329 "r"(__r8), "r"(__r9)); \
2330 }
2331 #endif
2332 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2333 int flags, void *arg, int *parent_tidptr,
2334 void *newtls, int *child_tidptr) {
2335 register unsigned long __v0 __asm__("$2");
2336 register unsigned long __r7 __asm__("$7") = (unsigned long)newtls;
2337 {
2338 register int __flags __asm__("$4") = flags;
2339 register void *__stack __asm__("$5") = child_stack;
2340 register void *__ptid __asm__("$6") = parent_tidptr;
2341 register int *__ctid __asm__("$8") = child_tidptr;
2342 __asm__ __volatile__(
2343 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2344 "subu $29,24\n"
2345 #elif _MIPS_SIM == _MIPS_SIM_NABI32
2346 "sub $29,16\n"
2347 #else
2348 "dsubu $29,16\n"
2349 #endif
2350
2351 /* if (fn == NULL || child_stack == NULL)
2352 * return -EINVAL;
2353 */
2354 "li %0,%2\n"
2355 "beqz %5,1f\n"
2356 "beqz %6,1f\n"
2357
2358 /* Push "arg" and "fn" onto the stack that will be
2359 * used by the child.
2360 */
2361 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2362 "subu %6,32\n"
2363 "sw %5,0(%6)\n"
2364 "sw %8,4(%6)\n"
2365 #elif _MIPS_SIM == _MIPS_SIM_NABI32
2366 "sub %6,32\n"
2367 "sw %5,0(%6)\n"
2368 "sw %8,8(%6)\n"
2369 #else
2370 "dsubu %6,32\n"
2371 "sd %5,0(%6)\n"
2372 "sd %8,8(%6)\n"
2373 #endif
2374
2375 /* $7 = syscall($4 = flags,
2376 * $5 = child_stack,
2377 * $6 = parent_tidptr,
2378 * $7 = newtls,
2379 * $8 = child_tidptr)
2380 */
2381 "li $2,%3\n"
2382 "syscall\n"
2383
2384 /* if ($7 != 0)
2385 * return $2;
2386 */
2387 "bnez $7,1f\n"
2388 "bnez $2,1f\n"
2389
2390 /* In the child, now. Call "fn(arg)".
2391 */
2392 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2393 "lw $25,0($29)\n"
2394 "lw $4,4($29)\n"
2395 #elif _MIPS_SIM == _MIPS_SIM_NABI32
2396 "lw $25,0($29)\n"
2397 "lw $4,8($29)\n"
2398 #else
2399 "ld $25,0($29)\n"
2400 "ld $4,8($29)\n"
2401 #endif
2402 "jalr $25\n"
2403
2404 /* Call _exit($2)
2405 */
2406 "move $4,$2\n"
2407 "li $2,%4\n"
2408 "syscall\n"
2409
2410 "1:\n"
2411 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2412 "addu $29, 24\n"
2413 #elif _MIPS_SIM == _MIPS_SIM_NABI32
2414 "add $29, 16\n"
2415 #else
2416 "daddu $29,16\n"
2417 #endif
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002418 : "=&r" (__v0), "+r" (__r7)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002419 : "i"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit),
2420 "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2421 "r"(__ptid), "r"(__r7), "r"(__ctid)
2422 : "$9", "$10", "$11", "$12", "$13", "$14", "$15",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002423 "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002424 }
2425 LSS_RETURN(int, __v0, __r7);
2426 }
2427 #elif defined (__PPC__)
2428 #undef LSS_LOADARGS_0
2429 #define LSS_LOADARGS_0(name, dummy...) \
2430 __sc_0 = __NR_##name
2431 #undef LSS_LOADARGS_1
2432 #define LSS_LOADARGS_1(name, arg1) \
2433 LSS_LOADARGS_0(name); \
2434 __sc_3 = (unsigned long) (arg1)
2435 #undef LSS_LOADARGS_2
2436 #define LSS_LOADARGS_2(name, arg1, arg2) \
2437 LSS_LOADARGS_1(name, arg1); \
2438 __sc_4 = (unsigned long) (arg2)
2439 #undef LSS_LOADARGS_3
2440 #define LSS_LOADARGS_3(name, arg1, arg2, arg3) \
2441 LSS_LOADARGS_2(name, arg1, arg2); \
2442 __sc_5 = (unsigned long) (arg3)
2443 #undef LSS_LOADARGS_4
2444 #define LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4) \
2445 LSS_LOADARGS_3(name, arg1, arg2, arg3); \
2446 __sc_6 = (unsigned long) (arg4)
2447 #undef LSS_LOADARGS_5
2448 #define LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5) \
2449 LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4); \
2450 __sc_7 = (unsigned long) (arg5)
2451 #undef LSS_LOADARGS_6
2452 #define LSS_LOADARGS_6(name, arg1, arg2, arg3, arg4, arg5, arg6) \
2453 LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5); \
2454 __sc_8 = (unsigned long) (arg6)
2455 #undef LSS_ASMINPUT_0
2456 #define LSS_ASMINPUT_0 "0" (__sc_0)
2457 #undef LSS_ASMINPUT_1
2458 #define LSS_ASMINPUT_1 LSS_ASMINPUT_0, "1" (__sc_3)
2459 #undef LSS_ASMINPUT_2
2460 #define LSS_ASMINPUT_2 LSS_ASMINPUT_1, "2" (__sc_4)
2461 #undef LSS_ASMINPUT_3
2462 #define LSS_ASMINPUT_3 LSS_ASMINPUT_2, "3" (__sc_5)
2463 #undef LSS_ASMINPUT_4
2464 #define LSS_ASMINPUT_4 LSS_ASMINPUT_3, "4" (__sc_6)
2465 #undef LSS_ASMINPUT_5
2466 #define LSS_ASMINPUT_5 LSS_ASMINPUT_4, "5" (__sc_7)
2467 #undef LSS_ASMINPUT_6
2468 #define LSS_ASMINPUT_6 LSS_ASMINPUT_5, "6" (__sc_8)
2469 #undef LSS_BODY
2470 #define LSS_BODY(nr, type, name, args...) \
2471 long __sc_ret, __sc_err; \
2472 { \
2473 register unsigned long __sc_0 __asm__ ("r0"); \
2474 register unsigned long __sc_3 __asm__ ("r3"); \
2475 register unsigned long __sc_4 __asm__ ("r4"); \
2476 register unsigned long __sc_5 __asm__ ("r5"); \
2477 register unsigned long __sc_6 __asm__ ("r6"); \
2478 register unsigned long __sc_7 __asm__ ("r7"); \
2479 register unsigned long __sc_8 __asm__ ("r8"); \
2480 \
2481 LSS_LOADARGS_##nr(name, args); \
2482 __asm__ __volatile__ \
2483 ("sc\n\t" \
2484 "mfcr %0" \
2485 : "=&r" (__sc_0), \
2486 "=&r" (__sc_3), "=&r" (__sc_4), \
2487 "=&r" (__sc_5), "=&r" (__sc_6), \
2488 "=&r" (__sc_7), "=&r" (__sc_8) \
2489 : LSS_ASMINPUT_##nr \
2490 : "cr0", "ctr", "memory", \
2491 "r9", "r10", "r11", "r12"); \
2492 __sc_ret = __sc_3; \
2493 __sc_err = __sc_0; \
2494 } \
2495 LSS_RETURN(type, __sc_ret, __sc_err)
2496 #undef _syscall0
2497 #define _syscall0(type, name) \
2498 type LSS_NAME(name)(void) { \
2499 LSS_BODY(0, type, name); \
2500 }
2501 #undef _syscall1
2502 #define _syscall1(type, name, type1, arg1) \
2503 type LSS_NAME(name)(type1 arg1) { \
2504 LSS_BODY(1, type, name, arg1); \
2505 }
2506 #undef _syscall2
2507 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2508 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2509 LSS_BODY(2, type, name, arg1, arg2); \
2510 }
2511 #undef _syscall3
2512 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2513 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2514 LSS_BODY(3, type, name, arg1, arg2, arg3); \
2515 }
2516 #undef _syscall4
2517 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
2518 type4, arg4) \
2519 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2520 LSS_BODY(4, type, name, arg1, arg2, arg3, arg4); \
2521 }
2522 #undef _syscall5
2523 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
2524 type4, arg4, type5, arg5) \
2525 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2526 type5 arg5) { \
2527 LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5); \
2528 }
2529 #undef _syscall6
2530 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
2531 type4, arg4, type5, arg5, type6, arg6) \
2532 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2533 type5 arg5, type6 arg6) { \
2534 LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6); \
2535 }
2536 /* clone function adapted from glibc 2.3.6 clone.S */
2537 /* TODO(csilvers): consider wrapping some args up in a struct, like we
2538 * do for i386's _syscall6, so we can compile successfully on gcc 2.95
2539 */
2540 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2541 int flags, void *arg, int *parent_tidptr,
2542 void *newtls, int *child_tidptr) {
2543 long __ret, __err;
2544 {
2545 register int (*__fn)(void *) __asm__ ("r8") = fn;
2546 register void *__cstack __asm__ ("r4") = child_stack;
2547 register int __flags __asm__ ("r3") = flags;
2548 register void * __arg __asm__ ("r9") = arg;
2549 register int * __ptidptr __asm__ ("r5") = parent_tidptr;
2550 register void * __newtls __asm__ ("r6") = newtls;
2551 register int * __ctidptr __asm__ ("r7") = child_tidptr;
2552 __asm__ __volatile__(
2553 /* check for fn == NULL
2554 * and child_stack == NULL
2555 */
2556 "cmpwi cr0, %6, 0\n\t"
2557 "cmpwi cr1, %7, 0\n\t"
2558 "cror cr0*4+eq, cr1*4+eq, cr0*4+eq\n\t"
2559 "beq- cr0, 1f\n\t"
2560
2561 /* set up stack frame for child */
2562 "clrrwi %7, %7, 4\n\t"
2563 "li 0, 0\n\t"
2564 "stwu 0, -16(%7)\n\t"
2565
2566 /* fn, arg, child_stack are saved across the syscall: r28-30 */
2567 "mr 28, %6\n\t"
2568 "mr 29, %7\n\t"
2569 "mr 27, %9\n\t"
2570
2571 /* syscall */
2572 "li 0, %4\n\t"
2573 /* flags already in r3
2574 * child_stack already in r4
2575 * ptidptr already in r5
2576 * newtls already in r6
2577 * ctidptr already in r7
2578 */
2579 "sc\n\t"
2580
2581 /* Test if syscall was successful */
2582 "cmpwi cr1, 3, 0\n\t"
2583 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
2584 "bne- cr1, 1f\n\t"
2585
2586 /* Do the function call */
2587 "mtctr 28\n\t"
2588 "mr 3, 27\n\t"
2589 "bctrl\n\t"
2590
2591 /* Call _exit(r3) */
2592 "li 0, %5\n\t"
2593 "sc\n\t"
2594
2595 /* Return to parent */
2596 "1:\n"
2597 "mfcr %1\n\t"
2598 "mr %0, 3\n\t"
2599 : "=r" (__ret), "=r" (__err)
2600 : "0" (-1), "1" (EINVAL),
2601 "i" (__NR_clone), "i" (__NR_exit),
2602 "r" (__fn), "r" (__cstack), "r" (__flags),
2603 "r" (__arg), "r" (__ptidptr), "r" (__newtls),
2604 "r" (__ctidptr)
2605 : "cr0", "cr1", "memory", "ctr",
2606 "r0", "r29", "r27", "r28");
2607 }
2608 LSS_RETURN(int, __ret, __err);
2609 }
2610 #endif
2611 #define __NR__exit __NR_exit
2612 #define __NR__gettid __NR_gettid
2613 #define __NR__mremap __NR_mremap
2614 LSS_INLINE _syscall1(int, brk, void *, e)
2615 LSS_INLINE _syscall1(int, chdir, const char *,p)
2616 LSS_INLINE _syscall1(int, close, int, f)
2617 LSS_INLINE _syscall2(int, clock_getres, int, c,
2618 struct kernel_timespec*, t)
2619 LSS_INLINE _syscall2(int, clock_gettime, int, c,
2620 struct kernel_timespec*, t)
2621 LSS_INLINE _syscall1(int, dup, int, f)
2622 LSS_INLINE _syscall2(int, dup2, int, s,
2623 int, d)
2624 LSS_INLINE _syscall3(int, execve, const char*, f,
2625 const char*const*,a,const char*const*, e)
2626 LSS_INLINE _syscall1(int, _exit, int, e)
2627 LSS_INLINE _syscall1(int, exit_group, int, e)
2628 LSS_INLINE _syscall3(int, fcntl, int, f,
2629 int, c, long, a)
2630 LSS_INLINE _syscall0(pid_t, fork)
2631 LSS_INLINE _syscall2(int, fstat, int, f,
2632 struct kernel_stat*, b)
2633 LSS_INLINE _syscall2(int, fstatfs, int, f,
2634 struct kernel_statfs*, b)
2635 LSS_INLINE _syscall2(int, ftruncate, int, f,
2636 off_t, l)
2637 LSS_INLINE _syscall4(int, futex, int*, a,
2638 int, o, int, v,
2639 struct kernel_timespec*, t)
2640 LSS_INLINE _syscall3(int, getdents, int, f,
2641 struct kernel_dirent*, d, int, c)
2642 LSS_INLINE _syscall3(int, getdents64, int, f,
2643 struct kernel_dirent64*, d, int, c)
2644 LSS_INLINE _syscall0(gid_t, getegid)
2645 LSS_INLINE _syscall0(uid_t, geteuid)
2646 LSS_INLINE _syscall0(pid_t, getpgrp)
2647 LSS_INLINE _syscall0(pid_t, getpid)
2648 LSS_INLINE _syscall0(pid_t, getppid)
2649 LSS_INLINE _syscall2(int, getpriority, int, a,
2650 int, b)
2651 LSS_INLINE _syscall3(int, getresgid, gid_t *, r,
2652 gid_t *, e, gid_t *, s)
2653 LSS_INLINE _syscall3(int, getresuid, uid_t *, r,
2654 uid_t *, e, uid_t *, s)
2655#if !defined(__ARM_EABI__)
2656 LSS_INLINE _syscall2(int, getrlimit, int, r,
2657 struct kernel_rlimit*, l)
2658#endif
2659 LSS_INLINE _syscall1(pid_t, getsid, pid_t, p)
2660 LSS_INLINE _syscall0(pid_t, _gettid)
2661 LSS_INLINE _syscall2(pid_t, gettimeofday, struct kernel_timeval*, t,
2662 void*, tz)
2663 LSS_INLINE _syscall5(int, setxattr, const char *,p,
2664 const char *, n, const void *,v,
2665 size_t, s, int, f)
2666 LSS_INLINE _syscall5(int, lsetxattr, const char *,p,
2667 const char *, n, const void *,v,
2668 size_t, s, int, f)
2669 LSS_INLINE _syscall4(ssize_t, getxattr, const char *,p,
2670 const char *, n, void *, v, size_t, s)
2671 LSS_INLINE _syscall4(ssize_t, lgetxattr, const char *,p,
2672 const char *, n, void *, v, size_t, s)
2673 LSS_INLINE _syscall3(ssize_t, listxattr, const char *,p,
2674 char *, l, size_t, s)
2675 LSS_INLINE _syscall3(ssize_t, llistxattr, const char *,p,
2676 char *, l, size_t, s)
2677 LSS_INLINE _syscall3(int, ioctl, int, d,
2678 int, r, void *, a)
2679 LSS_INLINE _syscall2(int, ioprio_get, int, which,
2680 int, who)
2681 LSS_INLINE _syscall3(int, ioprio_set, int, which,
2682 int, who, int, ioprio)
2683 LSS_INLINE _syscall2(int, kill, pid_t, p,
2684 int, s)
2685 LSS_INLINE _syscall3(off_t, lseek, int, f,
2686 off_t, o, int, w)
2687 LSS_INLINE _syscall2(int, munmap, void*, s,
2688 size_t, l)
2689 LSS_INLINE _syscall6(long, move_pages, pid_t, p,
2690 unsigned long, n, void **,g, int *, d,
2691 int *, s, int, f)
2692 LSS_INLINE _syscall3(int, mprotect, const void *,a,
2693 size_t, l, int, p)
2694 LSS_INLINE _syscall5(void*, _mremap, void*, o,
2695 size_t, os, size_t, ns,
2696 unsigned long, f, void *, a)
2697 LSS_INLINE _syscall3(int, open, const char*, p,
2698 int, f, int, m)
2699 LSS_INLINE _syscall3(int, poll, struct kernel_pollfd*, u,
2700 unsigned int, n, int, t)
2701 LSS_INLINE _syscall2(int, prctl, int, o,
2702 long, a)
2703 LSS_INLINE _syscall4(long, ptrace, int, r,
2704 pid_t, p, void *, a, void *, d)
2705 #if defined(__NR_quotactl)
2706 // Defined on x86_64 / i386 only
2707 LSS_INLINE _syscall4(int, quotactl, int, cmd, const char *, special,
2708 int, id, caddr_t, addr)
2709 #endif
2710 LSS_INLINE _syscall3(ssize_t, read, int, f,
2711 void *, b, size_t, c)
2712 LSS_INLINE _syscall3(int, readlink, const char*, p,
2713 char*, b, size_t, s)
2714 LSS_INLINE _syscall4(int, rt_sigaction, int, s,
2715 const struct kernel_sigaction*, a,
2716 struct kernel_sigaction*, o, size_t, c)
2717 LSS_INLINE _syscall2(int, rt_sigpending, struct kernel_sigset_t *, s,
2718 size_t, c)
2719 LSS_INLINE _syscall4(int, rt_sigprocmask, int, h,
2720 const struct kernel_sigset_t*, s,
2721 struct kernel_sigset_t*, o, size_t, c)
2722 LSS_INLINE _syscall2(int, rt_sigsuspend,
2723 const struct kernel_sigset_t*, s, size_t, c)
2724 LSS_INLINE _syscall3(int, sched_getaffinity,pid_t, p,
2725 unsigned int, l, unsigned long *, m)
2726 LSS_INLINE _syscall3(int, sched_setaffinity,pid_t, p,
2727 unsigned int, l, unsigned long *, m)
2728 LSS_INLINE _syscall0(int, sched_yield)
2729 LSS_INLINE _syscall1(long, set_tid_address, int *, t)
2730 LSS_INLINE _syscall1(int, setfsgid, gid_t, g)
2731 LSS_INLINE _syscall1(int, setfsuid, uid_t, u)
2732 LSS_INLINE _syscall1(int, setuid, uid_t, u)
2733 LSS_INLINE _syscall1(int, setgid, gid_t, g)
2734 LSS_INLINE _syscall2(int, setpgid, pid_t, p,
2735 pid_t, g)
2736 LSS_INLINE _syscall3(int, setpriority, int, a,
2737 int, b, int, p)
2738 LSS_INLINE _syscall3(int, setresgid, gid_t, r,
2739 gid_t, e, gid_t, s)
2740 LSS_INLINE _syscall3(int, setresuid, uid_t, r,
2741 uid_t, e, uid_t, s)
2742 LSS_INLINE _syscall2(int, setrlimit, int, r,
2743 const struct kernel_rlimit*, l)
2744 LSS_INLINE _syscall0(pid_t, setsid)
2745 LSS_INLINE _syscall2(int, sigaltstack, const stack_t*, s,
2746 const stack_t*, o)
2747 #if defined(__NR_sigreturn)
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00002748 LSS_INLINE _syscall1(int, sigreturn, unsigned long, u)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002749 #endif
2750 LSS_INLINE _syscall2(int, stat, const char*, f,
2751 struct kernel_stat*, b)
2752 LSS_INLINE _syscall2(int, statfs, const char*, f,
2753 struct kernel_statfs*, b)
2754 LSS_INLINE _syscall3(int, tgkill, pid_t, p,
2755 pid_t, t, int, s)
2756 LSS_INLINE _syscall2(int, tkill, pid_t, p,
2757 int, s)
2758 LSS_INLINE _syscall1(int, unlink, const char*, f)
2759 LSS_INLINE _syscall3(ssize_t, write, int, f,
2760 const void *, b, size_t, c)
2761 LSS_INLINE _syscall3(ssize_t, writev, int, f,
2762 const struct kernel_iovec*, v, size_t, c)
2763 #if defined(__NR_getcpu)
2764 LSS_INLINE _syscall3(long, getcpu, unsigned *, cpu,
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00002765 unsigned *, node, void *, unused)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002766 #endif
2767 #if defined(__x86_64__) || \
2768 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32)
2769 LSS_INLINE _syscall3(int, recvmsg, int, s,
2770 struct kernel_msghdr*, m, int, f)
2771 LSS_INLINE _syscall3(int, sendmsg, int, s,
2772 const struct kernel_msghdr*, m, int, f)
2773 LSS_INLINE _syscall6(int, sendto, int, s,
2774 const void*, m, size_t, l,
2775 int, f,
2776 const struct kernel_sockaddr*, a, int, t)
2777 LSS_INLINE _syscall2(int, shutdown, int, s,
2778 int, h)
2779 LSS_INLINE _syscall3(int, socket, int, d,
2780 int, t, int, p)
2781 LSS_INLINE _syscall4(int, socketpair, int, d,
2782 int, t, int, p, int*, s)
2783 #endif
2784 #if defined(__x86_64__)
2785 LSS_INLINE _syscall4(int, fallocate, int, fd, int, mode,
2786 loff_t, offset, loff_t, len)
2787
2788 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
2789 gid_t *egid,
2790 gid_t *sgid) {
2791 return LSS_NAME(getresgid)(rgid, egid, sgid);
2792 }
2793
2794 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
2795 uid_t *euid,
2796 uid_t *suid) {
2797 return LSS_NAME(getresuid)(ruid, euid, suid);
2798 }
2799
2800 LSS_INLINE _syscall6(void*, mmap, void*, s,
2801 size_t, l, int, p,
2802 int, f, int, d,
2803 __off64_t, o)
2804
2805 LSS_INLINE _syscall4(int, newfstatat, int, d,
2806 const char *, p,
2807 struct kernel_stat*, b, int, f)
2808
2809 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
2810 return LSS_NAME(setfsgid)(gid);
2811 }
2812
2813 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
2814 return LSS_NAME(setfsuid)(uid);
2815 }
2816
2817 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
2818 return LSS_NAME(setresgid)(rgid, egid, sgid);
2819 }
2820
2821 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
2822 return LSS_NAME(setresuid)(ruid, euid, suid);
2823 }
2824
2825 LSS_INLINE int LSS_NAME(sigaction)(int signum,
2826 const struct kernel_sigaction *act,
2827 struct kernel_sigaction *oldact) {
2828 /* On x86_64, the kernel requires us to always set our own
2829 * SA_RESTORER in order to be able to return from a signal handler.
2830 * This function must have a "magic" signature that the "gdb"
2831 * (and maybe the kernel?) can recognize.
2832 */
2833 if (act != NULL && !(act->sa_flags & SA_RESTORER)) {
2834 struct kernel_sigaction a = *act;
2835 a.sa_flags |= SA_RESTORER;
2836 a.sa_restorer = LSS_NAME(restore_rt)();
2837 return LSS_NAME(rt_sigaction)(signum, &a, oldact,
2838 (KERNEL_NSIG+7)/8);
2839 } else {
2840 return LSS_NAME(rt_sigaction)(signum, act, oldact,
2841 (KERNEL_NSIG+7)/8);
2842 }
2843 }
2844
2845 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
2846 return LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
2847 }
2848
2849 LSS_INLINE int LSS_NAME(sigprocmask)(int how,
2850 const struct kernel_sigset_t *set,
2851 struct kernel_sigset_t *oldset) {
2852 return LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
2853 }
2854
2855 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
2856 return LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
2857 }
2858 #endif
2859 #if defined(__x86_64__) || defined(__ARM_ARCH_3__) || \
2860 defined(__ARM_EABI__) || \
2861 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32)
2862 LSS_INLINE _syscall4(pid_t, wait4, pid_t, p,
2863 int*, s, int, o,
2864 struct kernel_rusage*, r)
2865
2866 LSS_INLINE pid_t LSS_NAME(waitpid)(pid_t pid, int *status, int options){
2867 return LSS_NAME(wait4)(pid, status, options, 0);
2868 }
2869 #endif
2870 #if defined(__i386__) || defined(__x86_64__)
2871 LSS_INLINE _syscall4(int, openat, int, d, const char *, p, int, f, int, m)
2872 LSS_INLINE _syscall3(int, unlinkat, int, d, const char *, p, int, f)
2873 #endif
2874 #if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__)
2875 #define __NR__getresgid32 __NR_getresgid32
2876 #define __NR__getresuid32 __NR_getresuid32
2877 #define __NR__setfsgid32 __NR_setfsgid32
2878 #define __NR__setfsuid32 __NR_setfsuid32
2879 #define __NR__setresgid32 __NR_setresgid32
2880 #define __NR__setresuid32 __NR_setresuid32
2881#if defined(__ARM_EABI__)
2882 LSS_INLINE _syscall2(int, ugetrlimit, int, r,
2883 struct kernel_rlimit*, l)
2884#endif
2885 LSS_INLINE _syscall3(int, _getresgid32, gid_t *, r,
2886 gid_t *, e, gid_t *, s)
2887 LSS_INLINE _syscall3(int, _getresuid32, uid_t *, r,
2888 uid_t *, e, uid_t *, s)
2889 LSS_INLINE _syscall1(int, _setfsgid32, gid_t, f)
2890 LSS_INLINE _syscall1(int, _setfsuid32, uid_t, f)
2891 LSS_INLINE _syscall3(int, _setresgid32, gid_t, r,
2892 gid_t, e, gid_t, s)
2893 LSS_INLINE _syscall3(int, _setresuid32, uid_t, r,
2894 uid_t, e, uid_t, s)
2895
2896 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
2897 gid_t *egid,
2898 gid_t *sgid) {
2899 int rc;
2900 if ((rc = LSS_NAME(_getresgid32)(rgid, egid, sgid)) < 0 &&
2901 LSS_ERRNO == ENOSYS) {
2902 if ((rgid == NULL) || (egid == NULL) || (sgid == NULL)) {
2903 return EFAULT;
2904 }
2905 // Clear the high bits first, since getresgid only sets 16 bits
2906 *rgid = *egid = *sgid = 0;
2907 rc = LSS_NAME(getresgid)(rgid, egid, sgid);
2908 }
2909 return rc;
2910 }
2911
2912 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
2913 uid_t *euid,
2914 uid_t *suid) {
2915 int rc;
2916 if ((rc = LSS_NAME(_getresuid32)(ruid, euid, suid)) < 0 &&
2917 LSS_ERRNO == ENOSYS) {
2918 if ((ruid == NULL) || (euid == NULL) || (suid == NULL)) {
2919 return EFAULT;
2920 }
2921 // Clear the high bits first, since getresuid only sets 16 bits
2922 *ruid = *euid = *suid = 0;
2923 rc = LSS_NAME(getresuid)(ruid, euid, suid);
2924 }
2925 return rc;
2926 }
2927
2928 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
2929 int rc;
2930 if ((rc = LSS_NAME(_setfsgid32)(gid)) < 0 &&
2931 LSS_ERRNO == ENOSYS) {
2932 if ((unsigned int)gid & ~0xFFFFu) {
2933 rc = EINVAL;
2934 } else {
2935 rc = LSS_NAME(setfsgid)(gid);
2936 }
2937 }
2938 return rc;
2939 }
2940
2941 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
2942 int rc;
2943 if ((rc = LSS_NAME(_setfsuid32)(uid)) < 0 &&
2944 LSS_ERRNO == ENOSYS) {
2945 if ((unsigned int)uid & ~0xFFFFu) {
2946 rc = EINVAL;
2947 } else {
2948 rc = LSS_NAME(setfsuid)(uid);
2949 }
2950 }
2951 return rc;
2952 }
2953
2954 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
2955 int rc;
2956 if ((rc = LSS_NAME(_setresgid32)(rgid, egid, sgid)) < 0 &&
2957 LSS_ERRNO == ENOSYS) {
2958 if ((unsigned int)rgid & ~0xFFFFu ||
2959 (unsigned int)egid & ~0xFFFFu ||
2960 (unsigned int)sgid & ~0xFFFFu) {
2961 rc = EINVAL;
2962 } else {
2963 rc = LSS_NAME(setresgid)(rgid, egid, sgid);
2964 }
2965 }
2966 return rc;
2967 }
2968
2969 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
2970 int rc;
2971 if ((rc = LSS_NAME(_setresuid32)(ruid, euid, suid)) < 0 &&
2972 LSS_ERRNO == ENOSYS) {
2973 if ((unsigned int)ruid & ~0xFFFFu ||
2974 (unsigned int)euid & ~0xFFFFu ||
2975 (unsigned int)suid & ~0xFFFFu) {
2976 rc = EINVAL;
2977 } else {
2978 rc = LSS_NAME(setresuid)(ruid, euid, suid);
2979 }
2980 }
2981 return rc;
2982 }
2983 #endif
2984 LSS_INLINE int LSS_NAME(sigemptyset)(struct kernel_sigset_t *set) {
2985 memset(&set->sig, 0, sizeof(set->sig));
2986 return 0;
2987 }
2988
2989 LSS_INLINE int LSS_NAME(sigfillset)(struct kernel_sigset_t *set) {
2990 memset(&set->sig, -1, sizeof(set->sig));
2991 return 0;
2992 }
2993
2994 LSS_INLINE int LSS_NAME(sigaddset)(struct kernel_sigset_t *set,
2995 int signum) {
2996 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
2997 LSS_ERRNO = EINVAL;
2998 return -1;
2999 } else {
3000 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
3001 |= 1UL << ((signum - 1) % (8*sizeof(set->sig[0])));
3002 return 0;
3003 }
3004 }
3005
3006 LSS_INLINE int LSS_NAME(sigdelset)(struct kernel_sigset_t *set,
3007 int signum) {
3008 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
3009 LSS_ERRNO = EINVAL;
3010 return -1;
3011 } else {
3012 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
3013 &= ~(1UL << ((signum - 1) % (8*sizeof(set->sig[0]))));
3014 return 0;
3015 }
3016 }
mcgrathr@google.coma7999932011-11-21 22:26:20 +00003017
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003018 LSS_INLINE int LSS_NAME(sigismember)(struct kernel_sigset_t *set,
3019 int signum) {
3020 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
3021 LSS_ERRNO = EINVAL;
3022 return -1;
3023 } else {
3024 return !!(set->sig[(signum - 1)/(8*sizeof(set->sig[0]))] &
3025 (1UL << ((signum - 1) % (8*sizeof(set->sig[0])))));
3026 }
3027 }
3028 #if defined(__i386__) || defined(__ARM_ARCH_3__) || \
3029 defined(__ARM_EABI__) || \
3030 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || defined(__PPC__)
3031 #define __NR__sigaction __NR_sigaction
3032 #define __NR__sigpending __NR_sigpending
3033 #define __NR__sigprocmask __NR_sigprocmask
3034 #define __NR__sigsuspend __NR_sigsuspend
3035 #define __NR__socketcall __NR_socketcall
zodiac@gmail.com4f470182010-10-13 03:47:54 +00003036#if ! defined(__ANDROID__)
3037 /* The Android NDK #defines stat64 stat, so avoid multiple-definition */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003038 LSS_INLINE _syscall2(int, fstat64, int, f,
3039 struct kernel_stat64 *, b)
zodiac@gmail.com4f470182010-10-13 03:47:54 +00003040#endif
3041 LSS_INLINE _syscall5(int, _llseek, uint, fd,
3042 unsigned long, hi, unsigned long, lo,
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003043 loff_t *, res, uint, wh)
3044#if !defined(__ARM_EABI__)
3045 LSS_INLINE _syscall1(void*, mmap, void*, a)
3046#endif
3047 LSS_INLINE _syscall6(void*, mmap2, void*, s,
3048 size_t, l, int, p,
3049 int, f, int, d,
zodiac@gmail.com4f470182010-10-13 03:47:54 +00003050 off_t, o)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003051 LSS_INLINE _syscall3(int, _sigaction, int, s,
3052 const struct kernel_old_sigaction*, a,
3053 struct kernel_old_sigaction*, o)
3054 LSS_INLINE _syscall1(int, _sigpending, unsigned long*, s)
3055 LSS_INLINE _syscall3(int, _sigprocmask, int, h,
3056 const unsigned long*, s,
3057 unsigned long*, o)
3058 #ifdef __PPC__
3059 LSS_INLINE _syscall1(int, _sigsuspend, unsigned long, s)
3060 #else
3061 LSS_INLINE _syscall3(int, _sigsuspend, const void*, a,
3062 int, b,
3063 unsigned long, s)
3064 #endif
zodiac@gmail.com4f470182010-10-13 03:47:54 +00003065#if ! defined(__ANDROID__)
3066 /* The Android NDK #defines stat64 stat, so avoid multiple-definition */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003067 LSS_INLINE _syscall2(int, stat64, const char *, p,
3068 struct kernel_stat64 *, b)
zodiac@gmail.com4f470182010-10-13 03:47:54 +00003069#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003070
3071 LSS_INLINE int LSS_NAME(sigaction)(int signum,
3072 const struct kernel_sigaction *act,
3073 struct kernel_sigaction *oldact) {
3074 int old_errno = LSS_ERRNO;
3075 int rc;
3076 struct kernel_sigaction a;
3077 if (act != NULL) {
3078 a = *act;
3079 #ifdef __i386__
3080 /* On i386, the kernel requires us to always set our own
3081 * SA_RESTORER when using realtime signals. Otherwise, it does not
3082 * know how to return from a signal handler. This function must have
3083 * a "magic" signature that the "gdb" (and maybe the kernel?) can
3084 * recognize.
3085 * Apparently, a SA_RESTORER is implicitly set by the kernel, when
3086 * using non-realtime signals.
3087 *
3088 * TODO: Test whether ARM needs a restorer
3089 */
3090 if (!(a.sa_flags & SA_RESTORER)) {
3091 a.sa_flags |= SA_RESTORER;
3092 a.sa_restorer = (a.sa_flags & SA_SIGINFO)
3093 ? LSS_NAME(restore_rt)() : LSS_NAME(restore)();
3094 }
3095 #endif
3096 }
3097 rc = LSS_NAME(rt_sigaction)(signum, act ? &a : act, oldact,
3098 (KERNEL_NSIG+7)/8);
3099 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3100 struct kernel_old_sigaction oa, ooa, *ptr_a = &oa, *ptr_oa = &ooa;
3101 if (!act) {
3102 ptr_a = NULL;
3103 } else {
3104 oa.sa_handler_ = act->sa_handler_;
3105 memcpy(&oa.sa_mask, &act->sa_mask, sizeof(oa.sa_mask));
3106 #ifndef __mips__
3107 oa.sa_restorer = act->sa_restorer;
3108 #endif
3109 oa.sa_flags = act->sa_flags;
3110 }
3111 if (!oldact) {
3112 ptr_oa = NULL;
3113 }
3114 LSS_ERRNO = old_errno;
3115 rc = LSS_NAME(_sigaction)(signum, ptr_a, ptr_oa);
3116 if (rc == 0 && oldact) {
3117 if (act) {
3118 memcpy(oldact, act, sizeof(*act));
3119 } else {
3120 memset(oldact, 0, sizeof(*oldact));
3121 }
3122 oldact->sa_handler_ = ptr_oa->sa_handler_;
3123 oldact->sa_flags = ptr_oa->sa_flags;
3124 memcpy(&oldact->sa_mask, &ptr_oa->sa_mask, sizeof(ptr_oa->sa_mask));
3125 #ifndef __mips__
3126 oldact->sa_restorer = ptr_oa->sa_restorer;
3127 #endif
3128 }
3129 }
3130 return rc;
3131 }
3132
3133 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
3134 int old_errno = LSS_ERRNO;
3135 int rc = LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
3136 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3137 LSS_ERRNO = old_errno;
3138 LSS_NAME(sigemptyset)(set);
3139 rc = LSS_NAME(_sigpending)(&set->sig[0]);
3140 }
3141 return rc;
3142 }
3143
3144 LSS_INLINE int LSS_NAME(sigprocmask)(int how,
3145 const struct kernel_sigset_t *set,
3146 struct kernel_sigset_t *oldset) {
3147 int olderrno = LSS_ERRNO;
3148 int rc = LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
3149 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3150 LSS_ERRNO = olderrno;
3151 if (oldset) {
3152 LSS_NAME(sigemptyset)(oldset);
3153 }
3154 rc = LSS_NAME(_sigprocmask)(how,
3155 set ? &set->sig[0] : NULL,
3156 oldset ? &oldset->sig[0] : NULL);
3157 }
3158 return rc;
3159 }
3160
3161 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
3162 int olderrno = LSS_ERRNO;
3163 int rc = LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
3164 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3165 LSS_ERRNO = olderrno;
3166 rc = LSS_NAME(_sigsuspend)(
3167 #ifndef __PPC__
3168 set, 0,
3169 #endif
3170 set->sig[0]);
3171 }
3172 return rc;
3173 }
3174 #endif
3175 #if defined(__PPC__)
3176 #undef LSS_SC_LOADARGS_0
3177 #define LSS_SC_LOADARGS_0(dummy...)
3178 #undef LSS_SC_LOADARGS_1
3179 #define LSS_SC_LOADARGS_1(arg1) \
3180 __sc_4 = (unsigned long) (arg1)
3181 #undef LSS_SC_LOADARGS_2
3182 #define LSS_SC_LOADARGS_2(arg1, arg2) \
3183 LSS_SC_LOADARGS_1(arg1); \
3184 __sc_5 = (unsigned long) (arg2)
3185 #undef LSS_SC_LOADARGS_3
3186 #define LSS_SC_LOADARGS_3(arg1, arg2, arg3) \
3187 LSS_SC_LOADARGS_2(arg1, arg2); \
3188 __sc_6 = (unsigned long) (arg3)
3189 #undef LSS_SC_LOADARGS_4
3190 #define LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4) \
3191 LSS_SC_LOADARGS_3(arg1, arg2, arg3); \
3192 __sc_7 = (unsigned long) (arg4)
3193 #undef LSS_SC_LOADARGS_5
3194 #define LSS_SC_LOADARGS_5(arg1, arg2, arg3, arg4, arg5) \
3195 LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4); \
3196 __sc_8 = (unsigned long) (arg5)
3197 #undef LSS_SC_BODY
3198 #define LSS_SC_BODY(nr, type, opt, args...) \
3199 long __sc_ret, __sc_err; \
3200 { \
3201 register unsigned long __sc_0 __asm__ ("r0") = __NR_socketcall; \
3202 register unsigned long __sc_3 __asm__ ("r3") = opt; \
3203 register unsigned long __sc_4 __asm__ ("r4"); \
3204 register unsigned long __sc_5 __asm__ ("r5"); \
3205 register unsigned long __sc_6 __asm__ ("r6"); \
3206 register unsigned long __sc_7 __asm__ ("r7"); \
3207 register unsigned long __sc_8 __asm__ ("r8"); \
3208 LSS_SC_LOADARGS_##nr(args); \
3209 __asm__ __volatile__ \
3210 ("stwu 1, -48(1)\n\t" \
3211 "stw 4, 20(1)\n\t" \
3212 "stw 5, 24(1)\n\t" \
3213 "stw 6, 28(1)\n\t" \
3214 "stw 7, 32(1)\n\t" \
3215 "stw 8, 36(1)\n\t" \
3216 "addi 4, 1, 20\n\t" \
3217 "sc\n\t" \
3218 "mfcr %0" \
3219 : "=&r" (__sc_0), \
3220 "=&r" (__sc_3), "=&r" (__sc_4), \
3221 "=&r" (__sc_5), "=&r" (__sc_6), \
3222 "=&r" (__sc_7), "=&r" (__sc_8) \
3223 : LSS_ASMINPUT_##nr \
3224 : "cr0", "ctr", "memory"); \
3225 __sc_ret = __sc_3; \
3226 __sc_err = __sc_0; \
3227 } \
3228 LSS_RETURN(type, __sc_ret, __sc_err)
3229
3230 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
3231 int flags){
3232 LSS_SC_BODY(3, ssize_t, 17, s, msg, flags);
3233 }
3234
3235 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
3236 const struct kernel_msghdr *msg,
3237 int flags) {
3238 LSS_SC_BODY(3, ssize_t, 16, s, msg, flags);
3239 }
3240
3241 // TODO(csilvers): why is this ifdef'ed out?
3242#if 0
3243 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
3244 int flags,
3245 const struct kernel_sockaddr *to,
3246 unsigned int tolen) {
3247 LSS_BODY(6, ssize_t, 11, s, buf, len, flags, to, tolen);
3248 }
3249#endif
3250
3251 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
3252 LSS_SC_BODY(2, int, 13, s, how);
3253 }
3254
3255 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
3256 LSS_SC_BODY(3, int, 1, domain, type, protocol);
3257 }
3258
3259 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
3260 int sv[2]) {
3261 LSS_SC_BODY(4, int, 8, d, type, protocol, sv);
3262 }
3263 #endif
3264 #if defined(__ARM_EABI__)
3265 LSS_INLINE _syscall3(ssize_t, recvmsg, int, s, struct kernel_msghdr*, msg,
3266 int, flags)
3267 LSS_INLINE _syscall3(ssize_t, sendmsg, int, s, const struct kernel_msghdr*,
3268 msg, int, flags)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003269 LSS_INLINE _syscall6(ssize_t, sendto, int, s, const void*, buf, size_t,len,
3270 int, flags, const struct kernel_sockaddr*, to,
3271 unsigned int, tolen)
3272 LSS_INLINE _syscall2(int, shutdown, int, s, int, how)
3273 LSS_INLINE _syscall3(int, socket, int, domain, int, type, int, protocol)
3274 LSS_INLINE _syscall4(int, socketpair, int, d, int, type, int, protocol,
3275 int*, sv)
3276 #endif
3277 #if defined(__i386__) || defined(__ARM_ARCH_3__) || \
3278 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
3279 #define __NR__socketcall __NR_socketcall
3280 LSS_INLINE _syscall2(int, _socketcall, int, c,
3281 va_list, a)
3282 LSS_INLINE int LSS_NAME(socketcall)(int op, ...) {
3283 int rc;
3284 va_list ap;
3285 va_start(ap, op);
3286 rc = LSS_NAME(_socketcall)(op, ap);
3287 va_end(ap);
3288 return rc;
3289 }
3290
3291 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
3292 int flags){
3293 return (ssize_t)LSS_NAME(socketcall)(17, s, msg, flags);
3294 }
3295
3296 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
3297 const struct kernel_msghdr *msg,
3298 int flags) {
3299 return (ssize_t)LSS_NAME(socketcall)(16, s, msg, flags);
3300 }
3301
3302 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
3303 int flags,
3304 const struct kernel_sockaddr *to,
3305 unsigned int tolen) {
3306 return (ssize_t)LSS_NAME(socketcall)(11, s, buf, len, flags, to, tolen);
3307 }
3308
3309 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
3310 return LSS_NAME(socketcall)(13, s, how);
3311 }
3312
3313 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
3314 return LSS_NAME(socketcall)(1, domain, type, protocol);
3315 }
3316
3317 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
3318 int sv[2]) {
3319 return LSS_NAME(socketcall)(8, d, type, protocol, sv);
3320 }
3321 #endif
3322 #if defined(__i386__) || defined(__PPC__)
3323 LSS_INLINE _syscall4(int, fstatat64, int, d,
3324 const char *, p,
3325 struct kernel_stat64 *, b, int, f)
3326 #endif
3327 #if defined(__i386__) || defined(__PPC__) || \
3328 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
3329 LSS_INLINE _syscall3(pid_t, waitpid, pid_t, p,
3330 int*, s, int, o)
3331 #endif
3332 #if defined(__mips__)
3333 /* sys_pipe() on MIPS has non-standard calling conventions, as it returns
3334 * both file handles through CPU registers.
3335 */
3336 LSS_INLINE int LSS_NAME(pipe)(int *p) {
3337 register unsigned long __v0 __asm__("$2") = __NR_pipe;
3338 register unsigned long __v1 __asm__("$3");
3339 register unsigned long __r7 __asm__("$7");
3340 __asm__ __volatile__ ("syscall\n"
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003341 : "+r"(__v0), "=r"(__v1), "=r" (__r7)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003342 : "0"(__v0)
3343 : "$8", "$9", "$10", "$11", "$12",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003344 "$13", "$14", "$15", "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003345 if (__r7) {
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003346 unsigned long __errnovalue = __v0;
3347 LSS_ERRNO = __errnovalue;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003348 return -1;
3349 } else {
3350 p[0] = __v0;
3351 p[1] = __v1;
3352 return 0;
3353 }
3354 }
3355 #else
3356 LSS_INLINE _syscall1(int, pipe, int *, p)
3357 #endif
3358 /* TODO(csilvers): see if ppc can/should support this as well */
3359 #if defined(__i386__) || defined(__ARM_ARCH_3__) || \
3360 defined(__ARM_EABI__) || \
3361 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64)
3362 #define __NR__statfs64 __NR_statfs64
3363 #define __NR__fstatfs64 __NR_fstatfs64
3364 LSS_INLINE _syscall3(int, _statfs64, const char*, p,
3365 size_t, s,struct kernel_statfs64*, b)
3366 LSS_INLINE _syscall3(int, _fstatfs64, int, f,
3367 size_t, s,struct kernel_statfs64*, b)
3368 LSS_INLINE int LSS_NAME(statfs64)(const char *p,
3369 struct kernel_statfs64 *b) {
3370 return LSS_NAME(_statfs64)(p, sizeof(*b), b);
3371 }
3372 LSS_INLINE int LSS_NAME(fstatfs64)(int f,struct kernel_statfs64 *b) {
3373 return LSS_NAME(_fstatfs64)(f, sizeof(*b), b);
3374 }
3375 #endif
3376
3377 LSS_INLINE int LSS_NAME(execv)(const char *path, const char *const argv[]) {
3378 extern char **environ;
3379 return LSS_NAME(execve)(path, argv, (const char *const *)environ);
3380 }
3381
3382 LSS_INLINE pid_t LSS_NAME(gettid)() {
3383 pid_t tid = LSS_NAME(_gettid)();
3384 if (tid != -1) {
3385 return tid;
3386 }
3387 return LSS_NAME(getpid)();
3388 }
3389
3390 LSS_INLINE void *LSS_NAME(mremap)(void *old_address, size_t old_size,
3391 size_t new_size, int flags, ...) {
3392 va_list ap;
3393 void *new_address, *rc;
3394 va_start(ap, flags);
3395 new_address = va_arg(ap, void *);
3396 rc = LSS_NAME(_mremap)(old_address, old_size, new_size,
3397 flags, new_address);
3398 va_end(ap);
3399 return rc;
3400 }
3401
3402 LSS_INLINE int LSS_NAME(ptrace_detach)(pid_t pid) {
3403 /* PTRACE_DETACH can sometimes forget to wake up the tracee and it
3404 * then sends job control signals to the real parent, rather than to
3405 * the tracer. We reduce the risk of this happening by starting a
3406 * whole new time slice, and then quickly sending a SIGCONT signal
3407 * right after detaching from the tracee.
3408 *
3409 * We use tkill to ensure that we only issue a wakeup for the thread being
3410 * detached. Large multi threaded apps can take a long time in the kernel
3411 * processing SIGCONT.
3412 */
3413 int rc, err;
3414 LSS_NAME(sched_yield)();
3415 rc = LSS_NAME(ptrace)(PTRACE_DETACH, pid, (void *)0, (void *)0);
3416 err = LSS_ERRNO;
3417 LSS_NAME(tkill)(pid, SIGCONT);
3418 /* Old systems don't have tkill */
3419 if (LSS_ERRNO == ENOSYS)
3420 LSS_NAME(kill)(pid, SIGCONT);
3421 LSS_ERRNO = err;
3422 return rc;
3423 }
3424
3425 LSS_INLINE int LSS_NAME(raise)(int sig) {
3426 return LSS_NAME(kill)(LSS_NAME(getpid)(), sig);
3427 }
3428
3429 LSS_INLINE int LSS_NAME(setpgrp)() {
3430 return LSS_NAME(setpgid)(0, 0);
3431 }
3432
3433 LSS_INLINE int LSS_NAME(sysconf)(int name) {
3434 extern int __getpagesize(void);
3435 switch (name) {
3436 case _SC_OPEN_MAX: {
3437 struct kernel_rlimit limit;
3438#if defined(__ARM_EABI__)
3439 return LSS_NAME(ugetrlimit)(RLIMIT_NOFILE, &limit) < 0
3440 ? 8192 : limit.rlim_cur;
3441#else
3442 return LSS_NAME(getrlimit)(RLIMIT_NOFILE, &limit) < 0
3443 ? 8192 : limit.rlim_cur;
3444#endif
3445 }
3446 case _SC_PAGESIZE:
3447 return __getpagesize();
3448 default:
3449 LSS_ERRNO = ENOSYS;
3450 return -1;
3451 }
3452 }
3453 #if defined(__x86_64__) || \
3454 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI64)
3455 LSS_INLINE _syscall4(ssize_t, pread64, int, f,
3456 void *, b, size_t, c,
3457 loff_t, o)
3458 LSS_INLINE _syscall4(ssize_t, pwrite64, int, f,
3459 const void *, b, size_t, c,
3460 loff_t, o)
3461 LSS_INLINE _syscall3(int, readahead, int, f,
3462 loff_t, o, unsigned, c)
3463 #else
3464 #define __NR__pread64 __NR_pread64
3465 #define __NR__pwrite64 __NR_pwrite64
3466 #define __NR__readahead __NR_readahead
mcgrathr@google.coma7999932011-11-21 22:26:20 +00003467 #if defined(__ARM_EABI__)
3468 /* On ARM, a 64-bit parameter has to be in an even-odd register pair.
3469 * Hence these calls ignore their fourth argument (r3) so that their
3470 * fifth and sixth make such a pair (r4,r5).
3471 */
3472 #define LSS_LLARG_PAD 0,
3473 LSS_INLINE _syscall6(ssize_t, _pread64, int, f,
3474 void *, b, size_t, c,
3475 unsigned, skip, unsigned, o1, unsigned, o2)
3476 LSS_INLINE _syscall6(ssize_t, _pwrite64, int, f,
3477 const void *, b, size_t, c,
3478 unsigned, skip, unsigned, o1, unsigned, o2)
3479 LSS_INLINE _syscall5(int, _readahead, int, f,
3480 unsigned, skip,
3481 unsigned, o1, unsigned, o2, size_t, c)
3482 #else
3483 #define LSS_LLARG_PAD
3484 LSS_INLINE _syscall5(ssize_t, _pread64, int, f,
3485 void *, b, size_t, c, unsigned, o1,
3486 unsigned, o2)
3487 LSS_INLINE _syscall5(ssize_t, _pwrite64, int, f,
3488 const void *, b, size_t, c, unsigned, o1,
3489 long, o2)
3490 LSS_INLINE _syscall4(int, _readahead, int, f,
3491 unsigned, o1, unsigned, o2, size_t, c)
3492 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003493 /* We force 64bit-wide parameters onto the stack, then access each
3494 * 32-bit component individually. This guarantees that we build the
3495 * correct parameters independent of the native byte-order of the
3496 * underlying architecture.
3497 */
3498 LSS_INLINE ssize_t LSS_NAME(pread64)(int fd, void *buf, size_t count,
3499 loff_t off) {
3500 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00003501 return LSS_NAME(_pread64)(fd, buf, count,
3502 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003503 }
3504 LSS_INLINE ssize_t LSS_NAME(pwrite64)(int fd, const void *buf,
3505 size_t count, loff_t off) {
3506 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00003507 return LSS_NAME(_pwrite64)(fd, buf, count,
3508 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003509 }
3510 LSS_INLINE int LSS_NAME(readahead)(int fd, loff_t off, int len) {
3511 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00003512 return LSS_NAME(_readahead)(fd, LSS_LLARG_PAD o.arg[0], o.arg[1], len);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003513 }
3514 #endif
3515#endif
3516
3517#if defined(__cplusplus) && !defined(SYS_CPLUSPLUS)
3518}
3519#endif
3520
3521#endif
3522#endif