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