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