blob: 129aa75eeade9a3c4b868b662a9f7a3bfa853f05 [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
Bryan Chan3f6478a2016-06-14 08:38:17 -040085/* We currently only support x86-32, x86-64, ARM, MIPS, PPC, s390 and s390x
86 * on Linux.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +000087 * Porting to other related platforms should not be difficult.
88 */
89#if (defined(__i386__) || defined(__x86_64__) || defined(__ARM_ARCH_3__) || \
anton@chromium.org2f724fc2014-04-15 13:05:20 +000090 defined(__mips__) || defined(__PPC__) || defined(__ARM_EABI__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -040091 defined(__aarch64__) || defined(__s390__)) \
zodiac@gmail.com4f470182010-10-13 03:47:54 +000092 && (defined(__linux) || defined(__ANDROID__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +000093
94#ifndef SYS_CPLUSPLUS
95#ifdef __cplusplus
96/* Some system header files in older versions of gcc neglect to properly
97 * handle being included from C++. As it appears to be harmless to have
98 * multiple nested 'extern "C"' blocks, just add another one here.
99 */
100extern "C" {
101#endif
102
103#include <errno.h>
zodiac@gmail.com4f470182010-10-13 03:47:54 +0000104#include <fcntl.h>
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000105#include <sched.h>
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000106#include <signal.h>
107#include <stdarg.h>
108#include <stddef.h>
vapier@chromium.org2273e812013-04-01 17:52:44 +0000109#include <stdint.h>
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000110#include <string.h>
111#include <sys/ptrace.h>
112#include <sys/resource.h>
113#include <sys/time.h>
114#include <sys/types.h>
zodiac@gmail.com4f470182010-10-13 03:47:54 +0000115#include <sys/syscall.h>
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000116#include <unistd.h>
117#include <linux/unistd.h>
118#include <endian.h>
119
120#ifdef __mips__
121/* Include definitions of the ABI currently in use. */
mseaborn@chromium.org4fc94222015-08-11 21:15:24 +0000122#ifdef __ANDROID__
123/* Android doesn't have sgidefs.h, but does have asm/sgidefs.h,
124 * which has the definitions we need.
125 */
126#include <asm/sgidefs.h>
127#else
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000128#include <sgidefs.h>
129#endif
130#endif
mseaborn@chromium.org4fc94222015-08-11 21:15:24 +0000131#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000132
mseaborn@chromium.orgca749372012-09-05 18:26:20 +0000133/* The Android NDK's <sys/stat.h> #defines these macros as aliases
134 * to their non-64 counterparts. To avoid naming conflict, remove them. */
135#ifdef __ANDROID__
136 /* These are restored by the corresponding #pragma pop_macro near
137 * the end of this file. */
138# pragma push_macro("stat64")
139# pragma push_macro("fstat64")
140# pragma push_macro("lstat64")
141# undef stat64
142# undef fstat64
143# undef lstat64
144#endif
145
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -0400146#if defined(__ANDROID__) && defined(__x86_64__)
147// A number of x86_64 syscalls are blocked by seccomp on recent Android;
148// undefine them so that modern alternatives will be used instead where
149// possible.
150// The alternative syscalls have been sanity checked against linux-3.4+;
151// older versions might not work.
152# undef __NR_getdents
153# undef __NR_dup2
154# undef __NR_fork
155# undef __NR_getpgrp
156# undef __NR_open
157# undef __NR_poll
158# undef __NR_readlink
159# undef __NR_stat
160# undef __NR_unlink
161# undef __NR_pipe
162#endif
163
164#if defined(__ANDROID__)
165// waitpid is blocked by seccomp on all architectures on recent Android.
166# undef __NR_waitpid
167#endif
168
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000169/* As glibc often provides subtly incompatible data structures (and implicit
170 * wrapper functions that convert them), we provide our own kernel data
171 * structures for use by the system calls.
172 * These structures have been developed by using Linux 2.6.23 headers for
173 * reference. Note though, we do not care about exact API compatibility
174 * with the kernel, and in fact the kernel often does not have a single
175 * API that works across architectures. Instead, we try to mimic the glibc
176 * API where reasonable, and only guarantee ABI compatibility with the
177 * kernel headers.
178 * Most notably, here are a few changes that were made to the structures
179 * defined by kernel headers:
180 *
181 * - we only define structures, but not symbolic names for kernel data
182 * types. For the latter, we directly use the native C datatype
183 * (i.e. "unsigned" instead of "mode_t").
184 * - in a few cases, it is possible to define identical structures for
185 * both 32bit (e.g. i386) and 64bit (e.g. x86-64) platforms by
186 * standardizing on the 64bit version of the data types. In particular,
187 * this means that we use "unsigned" where the 32bit headers say
188 * "unsigned long".
189 * - overall, we try to minimize the number of cases where we need to
190 * conditionally define different structures.
191 * - the "struct kernel_sigaction" class of structures have been
192 * modified to more closely mimic glibc's API by introducing an
193 * anonymous union for the function pointer.
194 * - a small number of field names had to have an underscore appended to
195 * them, because glibc defines a global macro by the same name.
196 */
197
198/* include/linux/dirent.h */
199struct kernel_dirent64 {
200 unsigned long long d_ino;
201 long long d_off;
202 unsigned short d_reclen;
203 unsigned char d_type;
204 char d_name[256];
205};
206
207/* include/linux/dirent.h */
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -0400208#if !defined(__NR_getdents)
209// when getdents is not available, getdents64 is used for both.
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000210#define kernel_dirent kernel_dirent64
211#else
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000212struct kernel_dirent {
213 long d_ino;
214 long d_off;
215 unsigned short d_reclen;
216 char d_name[256];
217};
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000218#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000219
220/* include/linux/uio.h */
221struct kernel_iovec {
222 void *iov_base;
223 unsigned long iov_len;
224};
225
226/* include/linux/socket.h */
227struct kernel_msghdr {
228 void *msg_name;
229 int msg_namelen;
230 struct kernel_iovec*msg_iov;
231 unsigned long msg_iovlen;
232 void *msg_control;
233 unsigned long msg_controllen;
234 unsigned msg_flags;
235};
236
237/* include/asm-generic/poll.h */
238struct kernel_pollfd {
239 int fd;
240 short events;
241 short revents;
242};
243
244/* include/linux/resource.h */
245struct kernel_rlimit {
246 unsigned long rlim_cur;
247 unsigned long rlim_max;
248};
249
250/* include/linux/time.h */
251struct kernel_timespec {
252 long tv_sec;
253 long tv_nsec;
254};
255
256/* include/linux/time.h */
257struct kernel_timeval {
258 long tv_sec;
259 long tv_usec;
260};
261
262/* include/linux/resource.h */
263struct kernel_rusage {
264 struct kernel_timeval ru_utime;
265 struct kernel_timeval ru_stime;
266 long ru_maxrss;
267 long ru_ixrss;
268 long ru_idrss;
269 long ru_isrss;
270 long ru_minflt;
271 long ru_majflt;
272 long ru_nswap;
273 long ru_inblock;
274 long ru_oublock;
275 long ru_msgsnd;
276 long ru_msgrcv;
277 long ru_nsignals;
278 long ru_nvcsw;
279 long ru_nivcsw;
280};
281
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000282#if defined(__i386__) || defined(__ARM_EABI__) || defined(__ARM_ARCH_3__) \
Bryan Chan3f6478a2016-06-14 08:38:17 -0400283 || defined(__PPC__) || (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000284
285/* include/asm-{arm,i386,mips,ppc}/signal.h */
286struct kernel_old_sigaction {
287 union {
288 void (*sa_handler_)(int);
vapier@chromium.orgcdda4342013-03-06 04:26:28 +0000289 void (*sa_sigaction_)(int, siginfo_t *, void *);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000290 };
291 unsigned long sa_mask;
292 unsigned long sa_flags;
293 void (*sa_restorer)(void);
294} __attribute__((packed,aligned(4)));
295#elif (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
296 #define kernel_old_sigaction kernel_sigaction
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000297#elif defined(__aarch64__)
298 // No kernel_old_sigaction defined for arm64.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000299#endif
300
301/* Some kernel functions (e.g. sigaction() in 2.6.23) require that the
302 * exactly match the size of the signal set, even though the API was
303 * intended to be extensible. We define our own KERNEL_NSIG to deal with
304 * this.
305 * Please note that glibc provides signals [1.._NSIG-1], whereas the
306 * kernel (and this header) provides the range [1..KERNEL_NSIG]. The
307 * actual number of signals is obviously the same, but the constants
308 * differ by one.
309 */
310#ifdef __mips__
311#define KERNEL_NSIG 128
312#else
313#define KERNEL_NSIG 64
314#endif
315
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000316/* include/asm-{arm,aarch64,i386,mips,x86_64}/signal.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000317struct kernel_sigset_t {
318 unsigned long sig[(KERNEL_NSIG + 8*sizeof(unsigned long) - 1)/
319 (8*sizeof(unsigned long))];
320};
321
322/* include/asm-{arm,i386,mips,x86_64,ppc}/signal.h */
323struct kernel_sigaction {
324#ifdef __mips__
325 unsigned long sa_flags;
326 union {
327 void (*sa_handler_)(int);
vapier@chromium.orgcdda4342013-03-06 04:26:28 +0000328 void (*sa_sigaction_)(int, siginfo_t *, void *);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000329 };
330 struct kernel_sigset_t sa_mask;
331#else
332 union {
333 void (*sa_handler_)(int);
vapier@chromium.orgcdda4342013-03-06 04:26:28 +0000334 void (*sa_sigaction_)(int, siginfo_t *, void *);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000335 };
336 unsigned long sa_flags;
337 void (*sa_restorer)(void);
338 struct kernel_sigset_t sa_mask;
339#endif
340};
341
342/* include/linux/socket.h */
343struct kernel_sockaddr {
344 unsigned short sa_family;
345 char sa_data[14];
346};
347
Bryan Chan3f6478a2016-06-14 08:38:17 -0400348/* include/asm-{arm,aarch64,i386,mips,ppc,s390}/stat.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000349#ifdef __mips__
350#if _MIPS_SIM == _MIPS_SIM_ABI64
351struct kernel_stat {
352#else
353struct kernel_stat64 {
354#endif
355 unsigned st_dev;
356 unsigned __pad0[3];
357 unsigned long long st_ino;
358 unsigned st_mode;
359 unsigned st_nlink;
360 unsigned st_uid;
361 unsigned st_gid;
362 unsigned st_rdev;
363 unsigned __pad1[3];
364 long long st_size;
365 unsigned st_atime_;
366 unsigned st_atime_nsec_;
367 unsigned st_mtime_;
368 unsigned st_mtime_nsec_;
369 unsigned st_ctime_;
370 unsigned st_ctime_nsec_;
371 unsigned st_blksize;
372 unsigned __pad2;
373 unsigned long long st_blocks;
374};
375#elif defined __PPC__
376struct kernel_stat64 {
377 unsigned long long st_dev;
378 unsigned long long st_ino;
379 unsigned st_mode;
380 unsigned st_nlink;
381 unsigned st_uid;
382 unsigned st_gid;
383 unsigned long long st_rdev;
384 unsigned short int __pad2;
385 long long st_size;
386 long st_blksize;
387 long long st_blocks;
388 long st_atime_;
389 unsigned long st_atime_nsec_;
390 long st_mtime_;
391 unsigned long st_mtime_nsec_;
392 long st_ctime_;
393 unsigned long st_ctime_nsec_;
394 unsigned long __unused4;
395 unsigned long __unused5;
396};
397#else
398struct kernel_stat64 {
399 unsigned long long st_dev;
400 unsigned char __pad0[4];
401 unsigned __st_ino;
402 unsigned st_mode;
403 unsigned st_nlink;
404 unsigned st_uid;
405 unsigned st_gid;
406 unsigned long long st_rdev;
407 unsigned char __pad3[4];
408 long long st_size;
409 unsigned st_blksize;
410 unsigned long long st_blocks;
411 unsigned st_atime_;
412 unsigned st_atime_nsec_;
413 unsigned st_mtime_;
414 unsigned st_mtime_nsec_;
415 unsigned st_ctime_;
416 unsigned st_ctime_nsec_;
417 unsigned long long st_ino;
418};
419#endif
420
Bryan Chan3f6478a2016-06-14 08:38:17 -0400421/* include/asm-{arm,aarch64,i386,mips,x86_64,ppc,s390}/stat.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000422#if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__)
423struct kernel_stat {
424 /* The kernel headers suggest that st_dev and st_rdev should be 32bit
425 * quantities encoding 12bit major and 20bit minor numbers in an interleaved
426 * format. In reality, we do not see useful data in the top bits. So,
427 * we'll leave the padding in here, until we find a better solution.
428 */
429 unsigned short st_dev;
430 short pad1;
431 unsigned st_ino;
432 unsigned short st_mode;
433 unsigned short st_nlink;
434 unsigned short st_uid;
435 unsigned short st_gid;
436 unsigned short st_rdev;
437 short pad2;
438 unsigned st_size;
439 unsigned st_blksize;
440 unsigned st_blocks;
441 unsigned st_atime_;
442 unsigned st_atime_nsec_;
443 unsigned st_mtime_;
444 unsigned st_mtime_nsec_;
445 unsigned st_ctime_;
446 unsigned st_ctime_nsec_;
447 unsigned __unused4;
448 unsigned __unused5;
449};
450#elif defined(__x86_64__)
451struct kernel_stat {
vapier@chromium.org2273e812013-04-01 17:52:44 +0000452 uint64_t st_dev;
453 uint64_t st_ino;
454 uint64_t st_nlink;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000455 unsigned st_mode;
456 unsigned st_uid;
457 unsigned st_gid;
458 unsigned __pad0;
vapier@chromium.org2273e812013-04-01 17:52:44 +0000459 uint64_t st_rdev;
460 int64_t st_size;
461 int64_t st_blksize;
462 int64_t st_blocks;
463 uint64_t st_atime_;
464 uint64_t st_atime_nsec_;
465 uint64_t st_mtime_;
466 uint64_t st_mtime_nsec_;
467 uint64_t st_ctime_;
468 uint64_t st_ctime_nsec_;
anton@chromium.org43de0522014-04-04 11:20:46 +0000469 int64_t __unused4[3];
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000470};
471#elif defined(__PPC__)
472struct kernel_stat {
473 unsigned st_dev;
474 unsigned long st_ino; // ino_t
475 unsigned long st_mode; // mode_t
476 unsigned short st_nlink; // nlink_t
477 unsigned st_uid; // uid_t
478 unsigned st_gid; // gid_t
479 unsigned st_rdev;
480 long st_size; // off_t
481 unsigned long st_blksize;
482 unsigned long st_blocks;
483 unsigned long st_atime_;
484 unsigned long st_atime_nsec_;
485 unsigned long st_mtime_;
486 unsigned long st_mtime_nsec_;
487 unsigned long st_ctime_;
488 unsigned long st_ctime_nsec_;
489 unsigned long __unused4;
490 unsigned long __unused5;
491};
492#elif (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64)
493struct kernel_stat {
494 unsigned st_dev;
495 int st_pad1[3];
496 unsigned st_ino;
497 unsigned st_mode;
498 unsigned st_nlink;
499 unsigned st_uid;
500 unsigned st_gid;
501 unsigned st_rdev;
502 int st_pad2[2];
503 long st_size;
504 int st_pad3;
505 long st_atime_;
506 long st_atime_nsec_;
507 long st_mtime_;
508 long st_mtime_nsec_;
509 long st_ctime_;
510 long st_ctime_nsec_;
511 int st_blksize;
512 int st_blocks;
513 int st_pad4[14];
514};
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000515#elif defined(__aarch64__)
516struct kernel_stat {
517 unsigned long st_dev;
518 unsigned long st_ino;
519 unsigned int st_mode;
520 unsigned int st_nlink;
521 unsigned int st_uid;
522 unsigned int st_gid;
523 unsigned long st_rdev;
524 unsigned long __pad1;
525 long st_size;
526 int st_blksize;
527 int __pad2;
528 long st_blocks;
529 long st_atime_;
530 unsigned long st_atime_nsec_;
531 long st_mtime_;
532 unsigned long st_mtime_nsec_;
533 long st_ctime_;
534 unsigned long st_ctime_nsec_;
535 unsigned int __unused4;
536 unsigned int __unused5;
537};
Bryan Chan3f6478a2016-06-14 08:38:17 -0400538#elif defined(__s390x__)
539struct kernel_stat {
540 unsigned long st_dev;
541 unsigned long st_ino;
542 unsigned long st_nlink;
543 unsigned int st_mode;
544 unsigned int st_uid;
545 unsigned int st_gid;
546 unsigned int __pad1;
547 unsigned long st_rdev;
548 unsigned long st_size;
549 unsigned long st_atime_;
550 unsigned long st_atime_nsec_;
551 unsigned long st_mtime_;
552 unsigned long st_mtime_nsec_;
553 unsigned long st_ctime_;
554 unsigned long st_ctime_nsec_;
555 unsigned long st_blksize;
556 long st_blocks;
557 unsigned long __unused[3];
558};
559#elif defined(__s390__)
560struct kernel_stat {
561 unsigned short st_dev;
562 unsigned short __pad1;
563 unsigned long st_ino;
564 unsigned short st_mode;
565 unsigned short st_nlink;
566 unsigned short st_uid;
567 unsigned short st_gid;
568 unsigned short st_rdev;
569 unsigned short __pad2;
570 unsigned long st_size;
571 unsigned long st_blksize;
572 unsigned long st_blocks;
573 unsigned long st_atime_;
574 unsigned long st_atime_nsec_;
575 unsigned long st_mtime_;
576 unsigned long st_mtime_nsec_;
577 unsigned long st_ctime_;
578 unsigned long st_ctime_nsec_;
579 unsigned long __unused4;
580 unsigned long __unused5;
581};
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000582#endif
583
Bryan Chan3f6478a2016-06-14 08:38:17 -0400584/* include/asm-{arm,aarch64,i386,mips,x86_64,ppc,s390}/statfs.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000585#ifdef __mips__
586#if _MIPS_SIM != _MIPS_SIM_ABI64
587struct kernel_statfs64 {
588 unsigned long f_type;
589 unsigned long f_bsize;
590 unsigned long f_frsize;
591 unsigned long __pad;
592 unsigned long long f_blocks;
593 unsigned long long f_bfree;
594 unsigned long long f_files;
595 unsigned long long f_ffree;
596 unsigned long long f_bavail;
597 struct { int val[2]; } f_fsid;
598 unsigned long f_namelen;
599 unsigned long f_spare[6];
600};
601#endif
Bryan Chan3f6478a2016-06-14 08:38:17 -0400602#elif defined(__s390__)
603/* See also arch/s390/include/asm/compat.h */
604struct kernel_statfs64 {
605 unsigned int f_type;
606 unsigned int f_bsize;
607 unsigned long long f_blocks;
608 unsigned long long f_bfree;
609 unsigned long long f_bavail;
610 unsigned long long f_files;
611 unsigned long long f_ffree;
612 struct { int val[2]; } f_fsid;
613 unsigned int f_namelen;
614 unsigned int f_frsize;
615 unsigned int f_flags;
616 unsigned int f_spare[4];
617};
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000618#elif !defined(__x86_64__)
619struct kernel_statfs64 {
620 unsigned long f_type;
621 unsigned long f_bsize;
622 unsigned long long f_blocks;
623 unsigned long long f_bfree;
624 unsigned long long f_bavail;
625 unsigned long long f_files;
626 unsigned long long f_ffree;
627 struct { int val[2]; } f_fsid;
628 unsigned long f_namelen;
629 unsigned long f_frsize;
630 unsigned long f_spare[5];
631};
632#endif
633
Bryan Chan3f6478a2016-06-14 08:38:17 -0400634/* include/asm-{arm,i386,mips,x86_64,ppc,generic,s390}/statfs.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000635#ifdef __mips__
636struct kernel_statfs {
637 long f_type;
638 long f_bsize;
639 long f_frsize;
640 long f_blocks;
641 long f_bfree;
642 long f_files;
643 long f_ffree;
644 long f_bavail;
645 struct { int val[2]; } f_fsid;
646 long f_namelen;
647 long f_spare[6];
648};
vapier@chromium.org2273e812013-04-01 17:52:44 +0000649#elif defined(__x86_64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000650struct kernel_statfs {
651 /* x86_64 actually defines all these fields as signed, whereas all other */
652 /* platforms define them as unsigned. Leaving them at unsigned should not */
vapier@chromium.org2273e812013-04-01 17:52:44 +0000653 /* cause any problems. Make sure these are 64-bit even on x32. */
654 uint64_t f_type;
655 uint64_t f_bsize;
656 uint64_t f_blocks;
657 uint64_t f_bfree;
658 uint64_t f_bavail;
659 uint64_t f_files;
660 uint64_t f_ffree;
661 struct { int val[2]; } f_fsid;
662 uint64_t f_namelen;
663 uint64_t f_frsize;
664 uint64_t f_spare[5];
665};
Bryan Chan3f6478a2016-06-14 08:38:17 -0400666#elif defined(__s390__)
667struct kernel_statfs {
668 unsigned int f_type;
669 unsigned int f_bsize;
670 unsigned long f_blocks;
671 unsigned long f_bfree;
672 unsigned long f_bavail;
673 unsigned long f_files;
674 unsigned long f_ffree;
675 struct { int val[2]; } f_fsid;
676 unsigned int f_namelen;
677 unsigned int f_frsize;
678 unsigned int f_flags;
679 unsigned int f_spare[4];
680};
vapier@chromium.org2273e812013-04-01 17:52:44 +0000681#else
682struct kernel_statfs {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000683 unsigned long f_type;
684 unsigned long f_bsize;
685 unsigned long f_blocks;
686 unsigned long f_bfree;
687 unsigned long f_bavail;
688 unsigned long f_files;
689 unsigned long f_ffree;
690 struct { int val[2]; } f_fsid;
691 unsigned long f_namelen;
692 unsigned long f_frsize;
693 unsigned long f_spare[5];
694};
695#endif
696
697
698/* Definitions missing from the standard header files */
699#ifndef O_DIRECTORY
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000700#if defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || defined(__aarch64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000701#define O_DIRECTORY 0040000
702#else
703#define O_DIRECTORY 0200000
704#endif
705#endif
706#ifndef NT_PRXFPREG
707#define NT_PRXFPREG 0x46e62b7f
708#endif
709#ifndef PTRACE_GETFPXREGS
710#define PTRACE_GETFPXREGS ((enum __ptrace_request)18)
711#endif
712#ifndef PR_GET_DUMPABLE
713#define PR_GET_DUMPABLE 3
714#endif
715#ifndef PR_SET_DUMPABLE
716#define PR_SET_DUMPABLE 4
717#endif
718#ifndef PR_GET_SECCOMP
719#define PR_GET_SECCOMP 21
720#endif
721#ifndef PR_SET_SECCOMP
722#define PR_SET_SECCOMP 22
723#endif
724#ifndef AT_FDCWD
725#define AT_FDCWD (-100)
726#endif
727#ifndef AT_SYMLINK_NOFOLLOW
728#define AT_SYMLINK_NOFOLLOW 0x100
729#endif
730#ifndef AT_REMOVEDIR
731#define AT_REMOVEDIR 0x200
732#endif
733#ifndef MREMAP_FIXED
734#define MREMAP_FIXED 2
735#endif
736#ifndef SA_RESTORER
737#define SA_RESTORER 0x04000000
738#endif
739#ifndef CPUCLOCK_PROF
740#define CPUCLOCK_PROF 0
741#endif
742#ifndef CPUCLOCK_VIRT
743#define CPUCLOCK_VIRT 1
744#endif
745#ifndef CPUCLOCK_SCHED
746#define CPUCLOCK_SCHED 2
747#endif
748#ifndef CPUCLOCK_PERTHREAD_MASK
749#define CPUCLOCK_PERTHREAD_MASK 4
750#endif
751#ifndef MAKE_PROCESS_CPUCLOCK
752#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
Nico Webera2b70922017-03-30 11:03:37 -0400753 ((int)(~(unsigned)(pid) << 3) | (int)(clock))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000754#endif
755#ifndef MAKE_THREAD_CPUCLOCK
756#define MAKE_THREAD_CPUCLOCK(tid, clock) \
Nico Webera2b70922017-03-30 11:03:37 -0400757 ((int)(~(unsigned)(tid) << 3) | \
758 (int)((clock) | CPUCLOCK_PERTHREAD_MASK))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000759#endif
760
761#ifndef FUTEX_WAIT
762#define FUTEX_WAIT 0
763#endif
764#ifndef FUTEX_WAKE
765#define FUTEX_WAKE 1
766#endif
767#ifndef FUTEX_FD
768#define FUTEX_FD 2
769#endif
770#ifndef FUTEX_REQUEUE
771#define FUTEX_REQUEUE 3
772#endif
773#ifndef FUTEX_CMP_REQUEUE
774#define FUTEX_CMP_REQUEUE 4
775#endif
776#ifndef FUTEX_WAKE_OP
777#define FUTEX_WAKE_OP 5
778#endif
779#ifndef FUTEX_LOCK_PI
780#define FUTEX_LOCK_PI 6
781#endif
782#ifndef FUTEX_UNLOCK_PI
783#define FUTEX_UNLOCK_PI 7
784#endif
785#ifndef FUTEX_TRYLOCK_PI
786#define FUTEX_TRYLOCK_PI 8
787#endif
788#ifndef FUTEX_PRIVATE_FLAG
789#define FUTEX_PRIVATE_FLAG 128
790#endif
791#ifndef FUTEX_CMD_MASK
792#define FUTEX_CMD_MASK ~FUTEX_PRIVATE_FLAG
793#endif
794#ifndef FUTEX_WAIT_PRIVATE
795#define FUTEX_WAIT_PRIVATE (FUTEX_WAIT | FUTEX_PRIVATE_FLAG)
796#endif
797#ifndef FUTEX_WAKE_PRIVATE
798#define FUTEX_WAKE_PRIVATE (FUTEX_WAKE | FUTEX_PRIVATE_FLAG)
799#endif
800#ifndef FUTEX_REQUEUE_PRIVATE
801#define FUTEX_REQUEUE_PRIVATE (FUTEX_REQUEUE | FUTEX_PRIVATE_FLAG)
802#endif
803#ifndef FUTEX_CMP_REQUEUE_PRIVATE
804#define FUTEX_CMP_REQUEUE_PRIVATE (FUTEX_CMP_REQUEUE | FUTEX_PRIVATE_FLAG)
805#endif
806#ifndef FUTEX_WAKE_OP_PRIVATE
807#define FUTEX_WAKE_OP_PRIVATE (FUTEX_WAKE_OP | FUTEX_PRIVATE_FLAG)
808#endif
809#ifndef FUTEX_LOCK_PI_PRIVATE
810#define FUTEX_LOCK_PI_PRIVATE (FUTEX_LOCK_PI | FUTEX_PRIVATE_FLAG)
811#endif
812#ifndef FUTEX_UNLOCK_PI_PRIVATE
813#define FUTEX_UNLOCK_PI_PRIVATE (FUTEX_UNLOCK_PI | FUTEX_PRIVATE_FLAG)
814#endif
815#ifndef FUTEX_TRYLOCK_PI_PRIVATE
816#define FUTEX_TRYLOCK_PI_PRIVATE (FUTEX_TRYLOCK_PI | FUTEX_PRIVATE_FLAG)
817#endif
818
819
820#if defined(__x86_64__)
821#ifndef ARCH_SET_GS
822#define ARCH_SET_GS 0x1001
823#endif
824#ifndef ARCH_GET_GS
825#define ARCH_GET_GS 0x1004
826#endif
827#endif
828
829#if defined(__i386__)
830#ifndef __NR_quotactl
831#define __NR_quotactl 131
832#endif
833#ifndef __NR_setresuid
834#define __NR_setresuid 164
835#define __NR_getresuid 165
836#define __NR_setresgid 170
837#define __NR_getresgid 171
838#endif
839#ifndef __NR_rt_sigaction
840#define __NR_rt_sigreturn 173
841#define __NR_rt_sigaction 174
842#define __NR_rt_sigprocmask 175
843#define __NR_rt_sigpending 176
844#define __NR_rt_sigsuspend 179
845#endif
846#ifndef __NR_pread64
847#define __NR_pread64 180
848#endif
849#ifndef __NR_pwrite64
850#define __NR_pwrite64 181
851#endif
852#ifndef __NR_ugetrlimit
853#define __NR_ugetrlimit 191
854#endif
855#ifndef __NR_stat64
856#define __NR_stat64 195
857#endif
858#ifndef __NR_fstat64
859#define __NR_fstat64 197
860#endif
861#ifndef __NR_setresuid32
862#define __NR_setresuid32 208
863#define __NR_getresuid32 209
864#define __NR_setresgid32 210
865#define __NR_getresgid32 211
866#endif
867#ifndef __NR_setfsuid32
868#define __NR_setfsuid32 215
869#define __NR_setfsgid32 216
870#endif
871#ifndef __NR_getdents64
872#define __NR_getdents64 220
873#endif
874#ifndef __NR_gettid
875#define __NR_gettid 224
876#endif
877#ifndef __NR_readahead
878#define __NR_readahead 225
879#endif
880#ifndef __NR_setxattr
881#define __NR_setxattr 226
882#endif
883#ifndef __NR_lsetxattr
884#define __NR_lsetxattr 227
885#endif
886#ifndef __NR_getxattr
887#define __NR_getxattr 229
888#endif
889#ifndef __NR_lgetxattr
890#define __NR_lgetxattr 230
891#endif
892#ifndef __NR_listxattr
893#define __NR_listxattr 232
894#endif
895#ifndef __NR_llistxattr
896#define __NR_llistxattr 233
897#endif
898#ifndef __NR_tkill
899#define __NR_tkill 238
900#endif
901#ifndef __NR_futex
902#define __NR_futex 240
903#endif
904#ifndef __NR_sched_setaffinity
905#define __NR_sched_setaffinity 241
906#define __NR_sched_getaffinity 242
907#endif
908#ifndef __NR_set_tid_address
909#define __NR_set_tid_address 258
910#endif
911#ifndef __NR_clock_gettime
912#define __NR_clock_gettime 265
913#endif
914#ifndef __NR_clock_getres
915#define __NR_clock_getres 266
916#endif
917#ifndef __NR_statfs64
918#define __NR_statfs64 268
919#endif
920#ifndef __NR_fstatfs64
921#define __NR_fstatfs64 269
922#endif
923#ifndef __NR_fadvise64_64
924#define __NR_fadvise64_64 272
925#endif
926#ifndef __NR_ioprio_set
927#define __NR_ioprio_set 289
928#endif
929#ifndef __NR_ioprio_get
930#define __NR_ioprio_get 290
931#endif
932#ifndef __NR_openat
933#define __NR_openat 295
934#endif
935#ifndef __NR_fstatat64
936#define __NR_fstatat64 300
937#endif
938#ifndef __NR_unlinkat
939#define __NR_unlinkat 301
940#endif
941#ifndef __NR_move_pages
942#define __NR_move_pages 317
943#endif
944#ifndef __NR_getcpu
945#define __NR_getcpu 318
946#endif
947#ifndef __NR_fallocate
948#define __NR_fallocate 324
949#endif
950/* End of i386 definitions */
951#elif defined(__ARM_ARCH_3__) || defined(__ARM_EABI__)
952#ifndef __NR_setresuid
953#define __NR_setresuid (__NR_SYSCALL_BASE + 164)
954#define __NR_getresuid (__NR_SYSCALL_BASE + 165)
955#define __NR_setresgid (__NR_SYSCALL_BASE + 170)
956#define __NR_getresgid (__NR_SYSCALL_BASE + 171)
957#endif
958#ifndef __NR_rt_sigaction
959#define __NR_rt_sigreturn (__NR_SYSCALL_BASE + 173)
960#define __NR_rt_sigaction (__NR_SYSCALL_BASE + 174)
961#define __NR_rt_sigprocmask (__NR_SYSCALL_BASE + 175)
962#define __NR_rt_sigpending (__NR_SYSCALL_BASE + 176)
963#define __NR_rt_sigsuspend (__NR_SYSCALL_BASE + 179)
964#endif
965#ifndef __NR_pread64
966#define __NR_pread64 (__NR_SYSCALL_BASE + 180)
967#endif
968#ifndef __NR_pwrite64
969#define __NR_pwrite64 (__NR_SYSCALL_BASE + 181)
970#endif
971#ifndef __NR_ugetrlimit
972#define __NR_ugetrlimit (__NR_SYSCALL_BASE + 191)
973#endif
974#ifndef __NR_stat64
975#define __NR_stat64 (__NR_SYSCALL_BASE + 195)
976#endif
977#ifndef __NR_fstat64
978#define __NR_fstat64 (__NR_SYSCALL_BASE + 197)
979#endif
980#ifndef __NR_setresuid32
981#define __NR_setresuid32 (__NR_SYSCALL_BASE + 208)
982#define __NR_getresuid32 (__NR_SYSCALL_BASE + 209)
983#define __NR_setresgid32 (__NR_SYSCALL_BASE + 210)
984#define __NR_getresgid32 (__NR_SYSCALL_BASE + 211)
985#endif
986#ifndef __NR_setfsuid32
987#define __NR_setfsuid32 (__NR_SYSCALL_BASE + 215)
988#define __NR_setfsgid32 (__NR_SYSCALL_BASE + 216)
989#endif
990#ifndef __NR_getdents64
991#define __NR_getdents64 (__NR_SYSCALL_BASE + 217)
992#endif
993#ifndef __NR_gettid
994#define __NR_gettid (__NR_SYSCALL_BASE + 224)
995#endif
996#ifndef __NR_readahead
997#define __NR_readahead (__NR_SYSCALL_BASE + 225)
998#endif
999#ifndef __NR_setxattr
1000#define __NR_setxattr (__NR_SYSCALL_BASE + 226)
1001#endif
1002#ifndef __NR_lsetxattr
1003#define __NR_lsetxattr (__NR_SYSCALL_BASE + 227)
1004#endif
1005#ifndef __NR_getxattr
1006#define __NR_getxattr (__NR_SYSCALL_BASE + 229)
1007#endif
1008#ifndef __NR_lgetxattr
1009#define __NR_lgetxattr (__NR_SYSCALL_BASE + 230)
1010#endif
1011#ifndef __NR_listxattr
1012#define __NR_listxattr (__NR_SYSCALL_BASE + 232)
1013#endif
1014#ifndef __NR_llistxattr
1015#define __NR_llistxattr (__NR_SYSCALL_BASE + 233)
1016#endif
1017#ifndef __NR_tkill
1018#define __NR_tkill (__NR_SYSCALL_BASE + 238)
1019#endif
1020#ifndef __NR_futex
1021#define __NR_futex (__NR_SYSCALL_BASE + 240)
1022#endif
1023#ifndef __NR_sched_setaffinity
1024#define __NR_sched_setaffinity (__NR_SYSCALL_BASE + 241)
1025#define __NR_sched_getaffinity (__NR_SYSCALL_BASE + 242)
1026#endif
1027#ifndef __NR_set_tid_address
1028#define __NR_set_tid_address (__NR_SYSCALL_BASE + 256)
1029#endif
1030#ifndef __NR_clock_gettime
1031#define __NR_clock_gettime (__NR_SYSCALL_BASE + 263)
1032#endif
1033#ifndef __NR_clock_getres
1034#define __NR_clock_getres (__NR_SYSCALL_BASE + 264)
1035#endif
1036#ifndef __NR_statfs64
1037#define __NR_statfs64 (__NR_SYSCALL_BASE + 266)
1038#endif
1039#ifndef __NR_fstatfs64
1040#define __NR_fstatfs64 (__NR_SYSCALL_BASE + 267)
1041#endif
1042#ifndef __NR_ioprio_set
1043#define __NR_ioprio_set (__NR_SYSCALL_BASE + 314)
1044#endif
1045#ifndef __NR_ioprio_get
1046#define __NR_ioprio_get (__NR_SYSCALL_BASE + 315)
1047#endif
1048#ifndef __NR_move_pages
1049#define __NR_move_pages (__NR_SYSCALL_BASE + 344)
1050#endif
1051#ifndef __NR_getcpu
1052#define __NR_getcpu (__NR_SYSCALL_BASE + 345)
1053#endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04001054/* End of ARM 3/EABI definitions */
anton@chromium.org2f724fc2014-04-15 13:05:20 +00001055#elif defined(__aarch64__)
1056#ifndef __NR_setxattr
1057#define __NR_setxattr 5
1058#endif
1059#ifndef __NR_lsetxattr
1060#define __NR_lsetxattr 6
1061#endif
1062#ifndef __NR_getxattr
1063#define __NR_getxattr 8
1064#endif
1065#ifndef __NR_lgetxattr
1066#define __NR_lgetxattr 9
1067#endif
1068#ifndef __NR_listxattr
1069#define __NR_listxattr 11
1070#endif
1071#ifndef __NR_llistxattr
1072#define __NR_llistxattr 12
1073#endif
1074#ifndef __NR_ioprio_set
1075#define __NR_ioprio_set 30
1076#endif
1077#ifndef __NR_ioprio_get
1078#define __NR_ioprio_get 31
1079#endif
1080#ifndef __NR_unlinkat
1081#define __NR_unlinkat 35
1082#endif
1083#ifndef __NR_fallocate
1084#define __NR_fallocate 47
1085#endif
1086#ifndef __NR_openat
1087#define __NR_openat 56
1088#endif
1089#ifndef __NR_quotactl
1090#define __NR_quotactl 60
1091#endif
1092#ifndef __NR_getdents64
1093#define __NR_getdents64 61
1094#endif
1095#ifndef __NR_getdents
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04001096// when getdents is not available, getdents64 is used for both.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00001097#define __NR_getdents __NR_getdents64
1098#endif
1099#ifndef __NR_pread64
1100#define __NR_pread64 67
1101#endif
1102#ifndef __NR_pwrite64
1103#define __NR_pwrite64 68
1104#endif
1105#ifndef __NR_ppoll
1106#define __NR_ppoll 73
1107#endif
1108#ifndef __NR_readlinkat
1109#define __NR_readlinkat 78
1110#endif
1111#ifndef __NR_newfstatat
1112#define __NR_newfstatat 79
1113#endif
1114#ifndef __NR_set_tid_address
1115#define __NR_set_tid_address 96
1116#endif
1117#ifndef __NR_futex
1118#define __NR_futex 98
1119#endif
1120#ifndef __NR_clock_gettime
1121#define __NR_clock_gettime 113
1122#endif
1123#ifndef __NR_clock_getres
1124#define __NR_clock_getres 114
1125#endif
1126#ifndef __NR_sched_setaffinity
1127#define __NR_sched_setaffinity 122
1128#define __NR_sched_getaffinity 123
1129#endif
1130#ifndef __NR_tkill
1131#define __NR_tkill 130
1132#endif
1133#ifndef __NR_setresuid
1134#define __NR_setresuid 147
1135#define __NR_getresuid 148
1136#define __NR_setresgid 149
1137#define __NR_getresgid 150
1138#endif
1139#ifndef __NR_gettid
1140#define __NR_gettid 178
1141#endif
1142#ifndef __NR_readahead
1143#define __NR_readahead 213
1144#endif
1145#ifndef __NR_fadvise64
1146#define __NR_fadvise64 223
1147#endif
1148#ifndef __NR_move_pages
1149#define __NR_move_pages 239
1150#endif
1151/* End of aarch64 definitions */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001152#elif defined(__x86_64__)
1153#ifndef __NR_pread64
1154#define __NR_pread64 17
1155#endif
1156#ifndef __NR_pwrite64
1157#define __NR_pwrite64 18
1158#endif
1159#ifndef __NR_setresuid
1160#define __NR_setresuid 117
1161#define __NR_getresuid 118
1162#define __NR_setresgid 119
1163#define __NR_getresgid 120
1164#endif
1165#ifndef __NR_quotactl
1166#define __NR_quotactl 179
1167#endif
1168#ifndef __NR_gettid
1169#define __NR_gettid 186
1170#endif
1171#ifndef __NR_readahead
1172#define __NR_readahead 187
1173#endif
1174#ifndef __NR_setxattr
1175#define __NR_setxattr 188
1176#endif
1177#ifndef __NR_lsetxattr
1178#define __NR_lsetxattr 189
1179#endif
1180#ifndef __NR_getxattr
1181#define __NR_getxattr 191
1182#endif
1183#ifndef __NR_lgetxattr
1184#define __NR_lgetxattr 192
1185#endif
1186#ifndef __NR_listxattr
1187#define __NR_listxattr 194
1188#endif
1189#ifndef __NR_llistxattr
1190#define __NR_llistxattr 195
1191#endif
1192#ifndef __NR_tkill
1193#define __NR_tkill 200
1194#endif
1195#ifndef __NR_futex
1196#define __NR_futex 202
1197#endif
1198#ifndef __NR_sched_setaffinity
1199#define __NR_sched_setaffinity 203
1200#define __NR_sched_getaffinity 204
1201#endif
1202#ifndef __NR_getdents64
1203#define __NR_getdents64 217
1204#endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04001205#ifndef __NR_getdents
1206// when getdents is not available, getdents64 is used for both.
1207#define __NR_getdents __NR_getdents64
1208#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001209#ifndef __NR_set_tid_address
1210#define __NR_set_tid_address 218
1211#endif
1212#ifndef __NR_fadvise64
1213#define __NR_fadvise64 221
1214#endif
1215#ifndef __NR_clock_gettime
1216#define __NR_clock_gettime 228
1217#endif
1218#ifndef __NR_clock_getres
1219#define __NR_clock_getres 229
1220#endif
1221#ifndef __NR_ioprio_set
1222#define __NR_ioprio_set 251
1223#endif
1224#ifndef __NR_ioprio_get
1225#define __NR_ioprio_get 252
1226#endif
1227#ifndef __NR_openat
1228#define __NR_openat 257
1229#endif
1230#ifndef __NR_newfstatat
1231#define __NR_newfstatat 262
1232#endif
1233#ifndef __NR_unlinkat
1234#define __NR_unlinkat 263
1235#endif
1236#ifndef __NR_move_pages
1237#define __NR_move_pages 279
1238#endif
1239#ifndef __NR_fallocate
1240#define __NR_fallocate 285
1241#endif
1242/* End of x86-64 definitions */
1243#elif defined(__mips__)
1244#if _MIPS_SIM == _MIPS_SIM_ABI32
1245#ifndef __NR_setresuid
1246#define __NR_setresuid (__NR_Linux + 185)
1247#define __NR_getresuid (__NR_Linux + 186)
1248#define __NR_setresgid (__NR_Linux + 190)
1249#define __NR_getresgid (__NR_Linux + 191)
1250#endif
1251#ifndef __NR_rt_sigaction
1252#define __NR_rt_sigreturn (__NR_Linux + 193)
1253#define __NR_rt_sigaction (__NR_Linux + 194)
1254#define __NR_rt_sigprocmask (__NR_Linux + 195)
1255#define __NR_rt_sigpending (__NR_Linux + 196)
1256#define __NR_rt_sigsuspend (__NR_Linux + 199)
1257#endif
1258#ifndef __NR_pread64
1259#define __NR_pread64 (__NR_Linux + 200)
1260#endif
1261#ifndef __NR_pwrite64
1262#define __NR_pwrite64 (__NR_Linux + 201)
1263#endif
1264#ifndef __NR_stat64
1265#define __NR_stat64 (__NR_Linux + 213)
1266#endif
1267#ifndef __NR_fstat64
1268#define __NR_fstat64 (__NR_Linux + 215)
1269#endif
1270#ifndef __NR_getdents64
1271#define __NR_getdents64 (__NR_Linux + 219)
1272#endif
1273#ifndef __NR_gettid
1274#define __NR_gettid (__NR_Linux + 222)
1275#endif
1276#ifndef __NR_readahead
1277#define __NR_readahead (__NR_Linux + 223)
1278#endif
1279#ifndef __NR_setxattr
1280#define __NR_setxattr (__NR_Linux + 224)
1281#endif
1282#ifndef __NR_lsetxattr
1283#define __NR_lsetxattr (__NR_Linux + 225)
1284#endif
1285#ifndef __NR_getxattr
1286#define __NR_getxattr (__NR_Linux + 227)
1287#endif
1288#ifndef __NR_lgetxattr
1289#define __NR_lgetxattr (__NR_Linux + 228)
1290#endif
1291#ifndef __NR_listxattr
1292#define __NR_listxattr (__NR_Linux + 230)
1293#endif
1294#ifndef __NR_llistxattr
1295#define __NR_llistxattr (__NR_Linux + 231)
1296#endif
1297#ifndef __NR_tkill
1298#define __NR_tkill (__NR_Linux + 236)
1299#endif
1300#ifndef __NR_futex
1301#define __NR_futex (__NR_Linux + 238)
1302#endif
1303#ifndef __NR_sched_setaffinity
1304#define __NR_sched_setaffinity (__NR_Linux + 239)
1305#define __NR_sched_getaffinity (__NR_Linux + 240)
1306#endif
1307#ifndef __NR_set_tid_address
1308#define __NR_set_tid_address (__NR_Linux + 252)
1309#endif
1310#ifndef __NR_statfs64
1311#define __NR_statfs64 (__NR_Linux + 255)
1312#endif
1313#ifndef __NR_fstatfs64
1314#define __NR_fstatfs64 (__NR_Linux + 256)
1315#endif
1316#ifndef __NR_clock_gettime
1317#define __NR_clock_gettime (__NR_Linux + 263)
1318#endif
1319#ifndef __NR_clock_getres
1320#define __NR_clock_getres (__NR_Linux + 264)
1321#endif
1322#ifndef __NR_openat
1323#define __NR_openat (__NR_Linux + 288)
1324#endif
1325#ifndef __NR_fstatat
1326#define __NR_fstatat (__NR_Linux + 293)
1327#endif
1328#ifndef __NR_unlinkat
1329#define __NR_unlinkat (__NR_Linux + 294)
1330#endif
1331#ifndef __NR_move_pages
1332#define __NR_move_pages (__NR_Linux + 308)
1333#endif
1334#ifndef __NR_getcpu
1335#define __NR_getcpu (__NR_Linux + 312)
1336#endif
1337#ifndef __NR_ioprio_set
1338#define __NR_ioprio_set (__NR_Linux + 314)
1339#endif
1340#ifndef __NR_ioprio_get
1341#define __NR_ioprio_get (__NR_Linux + 315)
1342#endif
1343/* End of MIPS (old 32bit API) definitions */
1344#elif _MIPS_SIM == _MIPS_SIM_ABI64
1345#ifndef __NR_pread64
1346#define __NR_pread64 (__NR_Linux + 16)
1347#endif
1348#ifndef __NR_pwrite64
1349#define __NR_pwrite64 (__NR_Linux + 17)
1350#endif
1351#ifndef __NR_setresuid
1352#define __NR_setresuid (__NR_Linux + 115)
1353#define __NR_getresuid (__NR_Linux + 116)
1354#define __NR_setresgid (__NR_Linux + 117)
1355#define __NR_getresgid (__NR_Linux + 118)
1356#endif
1357#ifndef __NR_gettid
1358#define __NR_gettid (__NR_Linux + 178)
1359#endif
1360#ifndef __NR_readahead
1361#define __NR_readahead (__NR_Linux + 179)
1362#endif
1363#ifndef __NR_setxattr
1364#define __NR_setxattr (__NR_Linux + 180)
1365#endif
1366#ifndef __NR_lsetxattr
1367#define __NR_lsetxattr (__NR_Linux + 181)
1368#endif
1369#ifndef __NR_getxattr
1370#define __NR_getxattr (__NR_Linux + 183)
1371#endif
1372#ifndef __NR_lgetxattr
1373#define __NR_lgetxattr (__NR_Linux + 184)
1374#endif
1375#ifndef __NR_listxattr
1376#define __NR_listxattr (__NR_Linux + 186)
1377#endif
1378#ifndef __NR_llistxattr
1379#define __NR_llistxattr (__NR_Linux + 187)
1380#endif
1381#ifndef __NR_tkill
1382#define __NR_tkill (__NR_Linux + 192)
1383#endif
1384#ifndef __NR_futex
1385#define __NR_futex (__NR_Linux + 194)
1386#endif
1387#ifndef __NR_sched_setaffinity
1388#define __NR_sched_setaffinity (__NR_Linux + 195)
1389#define __NR_sched_getaffinity (__NR_Linux + 196)
1390#endif
1391#ifndef __NR_set_tid_address
1392#define __NR_set_tid_address (__NR_Linux + 212)
1393#endif
1394#ifndef __NR_clock_gettime
1395#define __NR_clock_gettime (__NR_Linux + 222)
1396#endif
1397#ifndef __NR_clock_getres
1398#define __NR_clock_getres (__NR_Linux + 223)
1399#endif
1400#ifndef __NR_openat
1401#define __NR_openat (__NR_Linux + 247)
1402#endif
1403#ifndef __NR_fstatat
1404#define __NR_fstatat (__NR_Linux + 252)
1405#endif
1406#ifndef __NR_unlinkat
1407#define __NR_unlinkat (__NR_Linux + 253)
1408#endif
1409#ifndef __NR_move_pages
1410#define __NR_move_pages (__NR_Linux + 267)
1411#endif
1412#ifndef __NR_getcpu
1413#define __NR_getcpu (__NR_Linux + 271)
1414#endif
1415#ifndef __NR_ioprio_set
1416#define __NR_ioprio_set (__NR_Linux + 273)
1417#endif
1418#ifndef __NR_ioprio_get
1419#define __NR_ioprio_get (__NR_Linux + 274)
1420#endif
1421/* End of MIPS (64bit API) definitions */
1422#else
1423#ifndef __NR_setresuid
1424#define __NR_setresuid (__NR_Linux + 115)
1425#define __NR_getresuid (__NR_Linux + 116)
1426#define __NR_setresgid (__NR_Linux + 117)
1427#define __NR_getresgid (__NR_Linux + 118)
1428#endif
1429#ifndef __NR_gettid
1430#define __NR_gettid (__NR_Linux + 178)
1431#endif
1432#ifndef __NR_readahead
1433#define __NR_readahead (__NR_Linux + 179)
1434#endif
1435#ifndef __NR_setxattr
1436#define __NR_setxattr (__NR_Linux + 180)
1437#endif
1438#ifndef __NR_lsetxattr
1439#define __NR_lsetxattr (__NR_Linux + 181)
1440#endif
1441#ifndef __NR_getxattr
1442#define __NR_getxattr (__NR_Linux + 183)
1443#endif
1444#ifndef __NR_lgetxattr
1445#define __NR_lgetxattr (__NR_Linux + 184)
1446#endif
1447#ifndef __NR_listxattr
1448#define __NR_listxattr (__NR_Linux + 186)
1449#endif
1450#ifndef __NR_llistxattr
1451#define __NR_llistxattr (__NR_Linux + 187)
1452#endif
1453#ifndef __NR_tkill
1454#define __NR_tkill (__NR_Linux + 192)
1455#endif
1456#ifndef __NR_futex
1457#define __NR_futex (__NR_Linux + 194)
1458#endif
1459#ifndef __NR_sched_setaffinity
1460#define __NR_sched_setaffinity (__NR_Linux + 195)
1461#define __NR_sched_getaffinity (__NR_Linux + 196)
1462#endif
1463#ifndef __NR_set_tid_address
1464#define __NR_set_tid_address (__NR_Linux + 213)
1465#endif
1466#ifndef __NR_statfs64
1467#define __NR_statfs64 (__NR_Linux + 217)
1468#endif
1469#ifndef __NR_fstatfs64
1470#define __NR_fstatfs64 (__NR_Linux + 218)
1471#endif
1472#ifndef __NR_clock_gettime
1473#define __NR_clock_gettime (__NR_Linux + 226)
1474#endif
1475#ifndef __NR_clock_getres
1476#define __NR_clock_getres (__NR_Linux + 227)
1477#endif
1478#ifndef __NR_openat
1479#define __NR_openat (__NR_Linux + 251)
1480#endif
1481#ifndef __NR_fstatat
1482#define __NR_fstatat (__NR_Linux + 256)
1483#endif
1484#ifndef __NR_unlinkat
1485#define __NR_unlinkat (__NR_Linux + 257)
1486#endif
1487#ifndef __NR_move_pages
1488#define __NR_move_pages (__NR_Linux + 271)
1489#endif
1490#ifndef __NR_getcpu
1491#define __NR_getcpu (__NR_Linux + 275)
1492#endif
1493#ifndef __NR_ioprio_set
1494#define __NR_ioprio_set (__NR_Linux + 277)
1495#endif
1496#ifndef __NR_ioprio_get
1497#define __NR_ioprio_get (__NR_Linux + 278)
1498#endif
1499/* End of MIPS (new 32bit API) definitions */
1500#endif
1501/* End of MIPS definitions */
1502#elif defined(__PPC__)
1503#ifndef __NR_setfsuid
1504#define __NR_setfsuid 138
1505#define __NR_setfsgid 139
1506#endif
1507#ifndef __NR_setresuid
1508#define __NR_setresuid 164
1509#define __NR_getresuid 165
1510#define __NR_setresgid 169
1511#define __NR_getresgid 170
1512#endif
1513#ifndef __NR_rt_sigaction
1514#define __NR_rt_sigreturn 172
1515#define __NR_rt_sigaction 173
1516#define __NR_rt_sigprocmask 174
1517#define __NR_rt_sigpending 175
1518#define __NR_rt_sigsuspend 178
1519#endif
1520#ifndef __NR_pread64
1521#define __NR_pread64 179
1522#endif
1523#ifndef __NR_pwrite64
1524#define __NR_pwrite64 180
1525#endif
1526#ifndef __NR_ugetrlimit
1527#define __NR_ugetrlimit 190
1528#endif
1529#ifndef __NR_readahead
1530#define __NR_readahead 191
1531#endif
1532#ifndef __NR_stat64
1533#define __NR_stat64 195
1534#endif
1535#ifndef __NR_fstat64
1536#define __NR_fstat64 197
1537#endif
1538#ifndef __NR_getdents64
1539#define __NR_getdents64 202
1540#endif
1541#ifndef __NR_gettid
1542#define __NR_gettid 207
1543#endif
1544#ifndef __NR_tkill
1545#define __NR_tkill 208
1546#endif
1547#ifndef __NR_setxattr
1548#define __NR_setxattr 209
1549#endif
1550#ifndef __NR_lsetxattr
1551#define __NR_lsetxattr 210
1552#endif
1553#ifndef __NR_getxattr
1554#define __NR_getxattr 212
1555#endif
1556#ifndef __NR_lgetxattr
1557#define __NR_lgetxattr 213
1558#endif
1559#ifndef __NR_listxattr
1560#define __NR_listxattr 215
1561#endif
1562#ifndef __NR_llistxattr
1563#define __NR_llistxattr 216
1564#endif
1565#ifndef __NR_futex
1566#define __NR_futex 221
1567#endif
1568#ifndef __NR_sched_setaffinity
1569#define __NR_sched_setaffinity 222
1570#define __NR_sched_getaffinity 223
1571#endif
1572#ifndef __NR_set_tid_address
1573#define __NR_set_tid_address 232
1574#endif
1575#ifndef __NR_clock_gettime
1576#define __NR_clock_gettime 246
1577#endif
1578#ifndef __NR_clock_getres
1579#define __NR_clock_getres 247
1580#endif
1581#ifndef __NR_statfs64
1582#define __NR_statfs64 252
1583#endif
1584#ifndef __NR_fstatfs64
1585#define __NR_fstatfs64 253
1586#endif
1587#ifndef __NR_fadvise64_64
1588#define __NR_fadvise64_64 254
1589#endif
1590#ifndef __NR_ioprio_set
1591#define __NR_ioprio_set 273
1592#endif
1593#ifndef __NR_ioprio_get
1594#define __NR_ioprio_get 274
1595#endif
1596#ifndef __NR_openat
1597#define __NR_openat 286
1598#endif
1599#ifndef __NR_fstatat64
1600#define __NR_fstatat64 291
1601#endif
1602#ifndef __NR_unlinkat
1603#define __NR_unlinkat 292
1604#endif
1605#ifndef __NR_move_pages
1606#define __NR_move_pages 301
1607#endif
1608#ifndef __NR_getcpu
1609#define __NR_getcpu 302
1610#endif
1611/* End of powerpc defininitions */
Bryan Chan3f6478a2016-06-14 08:38:17 -04001612#elif defined(__s390__)
1613#ifndef __NR_quotactl
1614#define __NR_quotactl 131
1615#endif
1616#ifndef __NR_rt_sigreturn
1617#define __NR_rt_sigreturn 173
1618#endif
1619#ifndef __NR_rt_sigaction
1620#define __NR_rt_sigaction 174
1621#endif
1622#ifndef __NR_rt_sigprocmask
1623#define __NR_rt_sigprocmask 175
1624#endif
1625#ifndef __NR_rt_sigpending
1626#define __NR_rt_sigpending 176
1627#endif
1628#ifndef __NR_rt_sigsuspend
1629#define __NR_rt_sigsuspend 179
1630#endif
1631#ifndef __NR_pread64
1632#define __NR_pread64 180
1633#endif
1634#ifndef __NR_pwrite64
1635#define __NR_pwrite64 181
1636#endif
1637#ifndef __NR_getdents64
1638#define __NR_getdents64 220
1639#endif
1640#ifndef __NR_readahead
1641#define __NR_readahead 222
1642#endif
1643#ifndef __NR_setxattr
1644#define __NR_setxattr 224
1645#endif
1646#ifndef __NR_lsetxattr
1647#define __NR_lsetxattr 225
1648#endif
1649#ifndef __NR_getxattr
1650#define __NR_getxattr 227
1651#endif
1652#ifndef __NR_lgetxattr
1653#define __NR_lgetxattr 228
1654#endif
1655#ifndef __NR_listxattr
1656#define __NR_listxattr 230
1657#endif
1658#ifndef __NR_llistxattr
1659#define __NR_llistxattr 231
1660#endif
1661#ifndef __NR_gettid
1662#define __NR_gettid 236
1663#endif
1664#ifndef __NR_tkill
1665#define __NR_tkill 237
1666#endif
1667#ifndef __NR_futex
1668#define __NR_futex 238
1669#endif
1670#ifndef __NR_sched_setaffinity
1671#define __NR_sched_setaffinity 239
1672#endif
1673#ifndef __NR_sched_getaffinity
1674#define __NR_sched_getaffinity 240
1675#endif
1676#ifndef __NR_set_tid_address
1677#define __NR_set_tid_address 252
1678#endif
1679#ifndef __NR_clock_gettime
1680#define __NR_clock_gettime 260
1681#endif
1682#ifndef __NR_clock_getres
1683#define __NR_clock_getres 261
1684#endif
1685#ifndef __NR_statfs64
1686#define __NR_statfs64 265
1687#endif
1688#ifndef __NR_fstatfs64
1689#define __NR_fstatfs64 266
1690#endif
1691#ifndef __NR_ioprio_set
1692#define __NR_ioprio_set 282
1693#endif
1694#ifndef __NR_ioprio_get
1695#define __NR_ioprio_get 283
1696#endif
1697#ifndef __NR_openat
1698#define __NR_openat 288
1699#endif
1700#ifndef __NR_unlinkat
1701#define __NR_unlinkat 294
1702#endif
1703#ifndef __NR_move_pages
1704#define __NR_move_pages 310
1705#endif
1706#ifndef __NR_getcpu
1707#define __NR_getcpu 311
1708#endif
1709#ifndef __NR_fallocate
1710#define __NR_fallocate 314
1711#endif
1712/* Some syscalls are named/numbered differently between s390 and s390x. */
1713#ifdef __s390x__
1714# ifndef __NR_getrlimit
1715# define __NR_getrlimit 191
1716# endif
1717# ifndef __NR_setresuid
1718# define __NR_setresuid 208
1719# endif
1720# ifndef __NR_getresuid
1721# define __NR_getresuid 209
1722# endif
1723# ifndef __NR_setresgid
1724# define __NR_setresgid 210
1725# endif
1726# ifndef __NR_getresgid
1727# define __NR_getresgid 211
1728# endif
1729# ifndef __NR_setfsuid
1730# define __NR_setfsuid 215
1731# endif
1732# ifndef __NR_setfsgid
1733# define __NR_setfsgid 216
1734# endif
1735# ifndef __NR_fadvise64
1736# define __NR_fadvise64 253
1737# endif
1738# ifndef __NR_newfstatat
1739# define __NR_newfstatat 293
1740# endif
1741#else /* __s390x__ */
1742# ifndef __NR_getrlimit
1743# define __NR_getrlimit 76
1744# endif
1745# ifndef __NR_setfsuid
1746# define __NR_setfsuid 138
1747# endif
1748# ifndef __NR_setfsgid
1749# define __NR_setfsgid 139
1750# endif
1751# ifndef __NR_setresuid
1752# define __NR_setresuid 164
1753# endif
1754# ifndef __NR_getresuid
1755# define __NR_getresuid 165
1756# endif
1757# ifndef __NR_setresgid
1758# define __NR_setresgid 170
1759# endif
1760# ifndef __NR_getresgid
1761# define __NR_getresgid 171
1762# endif
1763# ifndef __NR_ugetrlimit
1764# define __NR_ugetrlimit 191
1765# endif
1766# ifndef __NR_mmap2
1767# define __NR_mmap2 192
1768# endif
1769# ifndef __NR_setresuid32
1770# define __NR_setresuid32 208
1771# endif
1772# ifndef __NR_getresuid32
1773# define __NR_getresuid32 209
1774# endif
1775# ifndef __NR_setresgid32
1776# define __NR_setresgid32 210
1777# endif
1778# ifndef __NR_getresgid32
1779# define __NR_getresgid32 211
1780# endif
1781# ifndef __NR_setfsuid32
1782# define __NR_setfsuid32 215
1783# endif
1784# ifndef __NR_setfsgid32
1785# define __NR_setfsgid32 216
1786# endif
1787# ifndef __NR_fadvise64_64
1788# define __NR_fadvise64_64 264
1789# endif
1790# ifndef __NR_fstatat64
1791# define __NR_fstatat64 293
1792# endif
1793#endif /* __s390__ */
1794/* End of s390/s390x definitions */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001795#endif
1796
1797
1798/* After forking, we must make sure to only call system calls. */
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001799#if defined(__BOUNDED_POINTERS__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001800 #error "Need to port invocations of syscalls for bounded ptrs"
1801#else
1802 /* The core dumper and the thread lister get executed after threads
1803 * have been suspended. As a consequence, we cannot call any functions
1804 * that acquire locks. Unfortunately, libc wraps most system calls
1805 * (e.g. in order to implement pthread_atfork, and to make calls
1806 * cancellable), which means we cannot call these functions. Instead,
1807 * we have to call syscall() directly.
1808 */
1809 #undef LSS_ERRNO
1810 #ifdef SYS_ERRNO
1811 /* Allow the including file to override the location of errno. This can
1812 * be useful when using clone() with the CLONE_VM option.
1813 */
1814 #define LSS_ERRNO SYS_ERRNO
1815 #else
1816 #define LSS_ERRNO errno
1817 #endif
1818
1819 #undef LSS_INLINE
1820 #ifdef SYS_INLINE
1821 #define LSS_INLINE SYS_INLINE
1822 #else
1823 #define LSS_INLINE static inline
1824 #endif
1825
1826 /* Allow the including file to override the prefix used for all new
1827 * system calls. By default, it will be set to "sys_".
1828 */
1829 #undef LSS_NAME
1830 #ifndef SYS_PREFIX
1831 #define LSS_NAME(name) sys_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001832 #elif defined(SYS_PREFIX) && SYS_PREFIX < 0
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001833 #define LSS_NAME(name) name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001834 #elif defined(SYS_PREFIX) && SYS_PREFIX == 0
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001835 #define LSS_NAME(name) sys0_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001836 #elif defined(SYS_PREFIX) && SYS_PREFIX == 1
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001837 #define LSS_NAME(name) sys1_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001838 #elif defined(SYS_PREFIX) && SYS_PREFIX == 2
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001839 #define LSS_NAME(name) sys2_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001840 #elif defined(SYS_PREFIX) && SYS_PREFIX == 3
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001841 #define LSS_NAME(name) sys3_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001842 #elif defined(SYS_PREFIX) && SYS_PREFIX == 4
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001843 #define LSS_NAME(name) sys4_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001844 #elif defined(SYS_PREFIX) && SYS_PREFIX == 5
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001845 #define LSS_NAME(name) sys5_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001846 #elif defined(SYS_PREFIX) && SYS_PREFIX == 6
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001847 #define LSS_NAME(name) sys6_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001848 #elif defined(SYS_PREFIX) && SYS_PREFIX == 7
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001849 #define LSS_NAME(name) sys7_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001850 #elif defined(SYS_PREFIX) && SYS_PREFIX == 8
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001851 #define LSS_NAME(name) sys8_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001852 #elif defined(SYS_PREFIX) && SYS_PREFIX == 9
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001853 #define LSS_NAME(name) sys9_##name
1854 #endif
1855
1856 #undef LSS_RETURN
1857 #if (defined(__i386__) || defined(__x86_64__) || defined(__ARM_ARCH_3__) \
Bryan Chan3f6478a2016-06-14 08:38:17 -04001858 || defined(__ARM_EABI__) || defined(__aarch64__) || defined(__s390__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001859 /* Failing system calls return a negative result in the range of
1860 * -1..-4095. These are "errno" values with the sign inverted.
1861 */
1862 #define LSS_RETURN(type, res) \
1863 do { \
1864 if ((unsigned long)(res) >= (unsigned long)(-4095)) { \
1865 LSS_ERRNO = -(res); \
1866 res = -1; \
1867 } \
1868 return (type) (res); \
1869 } while (0)
1870 #elif defined(__mips__)
1871 /* On MIPS, failing system calls return -1, and set errno in a
1872 * separate CPU register.
1873 */
1874 #define LSS_RETURN(type, res, err) \
1875 do { \
1876 if (err) { \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00001877 unsigned long __errnovalue = (res); \
1878 LSS_ERRNO = __errnovalue; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001879 res = -1; \
1880 } \
1881 return (type) (res); \
1882 } while (0)
1883 #elif defined(__PPC__)
1884 /* On PPC, failing system calls return -1, and set errno in a
1885 * separate CPU register. See linux/unistd.h.
1886 */
1887 #define LSS_RETURN(type, res, err) \
1888 do { \
1889 if (err & 0x10000000 ) { \
1890 LSS_ERRNO = (res); \
1891 res = -1; \
1892 } \
1893 return (type) (res); \
1894 } while (0)
1895 #endif
1896 #if defined(__i386__)
1897 /* In PIC mode (e.g. when building shared libraries), gcc for i386
1898 * reserves ebx. Unfortunately, most distribution ship with implementations
1899 * of _syscallX() which clobber ebx.
1900 * Also, most definitions of _syscallX() neglect to mark "memory" as being
1901 * clobbered. This causes problems with compilers, that do a better job
1902 * at optimizing across __asm__ calls.
1903 * So, we just have to redefine all of the _syscallX() macros.
1904 */
1905 #undef LSS_ENTRYPOINT
1906 #ifdef SYS_SYSCALL_ENTRYPOINT
1907 static inline void (**LSS_NAME(get_syscall_entrypoint)(void))(void) {
1908 void (**entrypoint)(void);
1909 asm volatile(".bss\n"
1910 ".align 8\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00001911 ".globl " SYS_SYSCALL_ENTRYPOINT "\n"
1912 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001913 ".previous\n"
1914 /* This logically does 'lea "SYS_SYSCALL_ENTRYPOINT", %0' */
1915 "call 0f\n"
1916 "0:pop %0\n"
1917 "add $_GLOBAL_OFFSET_TABLE_+[.-0b], %0\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00001918 "mov " SYS_SYSCALL_ENTRYPOINT "@GOT(%0), %0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001919 : "=r"(entrypoint));
1920 return entrypoint;
1921 }
1922
1923 #define LSS_ENTRYPOINT ".bss\n" \
1924 ".align 8\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00001925 ".globl " SYS_SYSCALL_ENTRYPOINT "\n" \
1926 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001927 ".previous\n" \
1928 /* Check the SYS_SYSCALL_ENTRYPOINT vector */ \
1929 "push %%eax\n" \
1930 "call 10000f\n" \
1931 "10000:pop %%eax\n" \
1932 "add $_GLOBAL_OFFSET_TABLE_+[.-10000b], %%eax\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00001933 "mov " SYS_SYSCALL_ENTRYPOINT \
1934 "@GOT(%%eax), %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001935 "mov 0(%%eax), %%eax\n" \
1936 "test %%eax, %%eax\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001937 "jz 10002f\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001938 "push %%eax\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001939 "call 10001f\n" \
1940 "10001:pop %%eax\n" \
1941 "add $(10003f-10001b), %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001942 "xchg 4(%%esp), %%eax\n" \
1943 "ret\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001944 "10002:pop %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001945 "int $0x80\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00001946 "10003:\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001947 #else
1948 #define LSS_ENTRYPOINT "int $0x80\n"
1949 #endif
1950 #undef LSS_BODY
1951 #define LSS_BODY(type,args...) \
1952 long __res; \
1953 __asm__ __volatile__("push %%ebx\n" \
1954 "movl %2,%%ebx\n" \
1955 LSS_ENTRYPOINT \
1956 "pop %%ebx" \
1957 args \
1958 : "esp", "memory"); \
1959 LSS_RETURN(type,__res)
1960 #undef _syscall0
1961 #define _syscall0(type,name) \
1962 type LSS_NAME(name)(void) { \
1963 long __res; \
1964 __asm__ volatile(LSS_ENTRYPOINT \
1965 : "=a" (__res) \
1966 : "0" (__NR_##name) \
1967 : "esp", "memory"); \
1968 LSS_RETURN(type,__res); \
1969 }
1970 #undef _syscall1
1971 #define _syscall1(type,name,type1,arg1) \
1972 type LSS_NAME(name)(type1 arg1) { \
1973 LSS_BODY(type, \
1974 : "=a" (__res) \
1975 : "0" (__NR_##name), "ri" ((long)(arg1))); \
1976 }
1977 #undef _syscall2
1978 #define _syscall2(type,name,type1,arg1,type2,arg2) \
1979 type LSS_NAME(name)(type1 arg1,type2 arg2) { \
1980 LSS_BODY(type, \
1981 : "=a" (__res) \
1982 : "0" (__NR_##name),"ri" ((long)(arg1)), "c" ((long)(arg2))); \
1983 }
1984 #undef _syscall3
1985 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
1986 type LSS_NAME(name)(type1 arg1,type2 arg2,type3 arg3) { \
1987 LSS_BODY(type, \
1988 : "=a" (__res) \
1989 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
1990 "d" ((long)(arg3))); \
1991 }
1992 #undef _syscall4
1993 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
1994 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
1995 LSS_BODY(type, \
1996 : "=a" (__res) \
1997 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
1998 "d" ((long)(arg3)),"S" ((long)(arg4))); \
1999 }
2000 #undef _syscall5
2001 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2002 type5,arg5) \
2003 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2004 type5 arg5) { \
2005 long __res; \
2006 __asm__ __volatile__("push %%ebx\n" \
2007 "movl %2,%%ebx\n" \
2008 "movl %1,%%eax\n" \
2009 LSS_ENTRYPOINT \
2010 "pop %%ebx" \
2011 : "=a" (__res) \
2012 : "i" (__NR_##name), "ri" ((long)(arg1)), \
2013 "c" ((long)(arg2)), "d" ((long)(arg3)), \
2014 "S" ((long)(arg4)), "D" ((long)(arg5)) \
2015 : "esp", "memory"); \
2016 LSS_RETURN(type,__res); \
2017 }
2018 #undef _syscall6
2019 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2020 type5,arg5,type6,arg6) \
2021 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2022 type5 arg5, type6 arg6) { \
2023 long __res; \
2024 struct { long __a1; long __a6; } __s = { (long)arg1, (long) arg6 }; \
2025 __asm__ __volatile__("push %%ebp\n" \
2026 "push %%ebx\n" \
mseaborn@chromium.orge96ade32012-10-27 17:47:38 +00002027 "movl 4(%2),%%ebp\n" \
2028 "movl 0(%2), %%ebx\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002029 "movl %1,%%eax\n" \
2030 LSS_ENTRYPOINT \
2031 "pop %%ebx\n" \
2032 "pop %%ebp" \
2033 : "=a" (__res) \
2034 : "i" (__NR_##name), "0" ((long)(&__s)), \
2035 "c" ((long)(arg2)), "d" ((long)(arg3)), \
2036 "S" ((long)(arg4)), "D" ((long)(arg5)) \
2037 : "esp", "memory"); \
2038 LSS_RETURN(type,__res); \
2039 }
2040 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2041 int flags, void *arg, int *parent_tidptr,
2042 void *newtls, int *child_tidptr) {
2043 long __res;
2044 __asm__ __volatile__(/* if (fn == NULL)
2045 * return -EINVAL;
2046 */
2047 "movl %3,%%ecx\n"
2048 "jecxz 1f\n"
2049
2050 /* if (child_stack == NULL)
2051 * return -EINVAL;
2052 */
2053 "movl %4,%%ecx\n"
2054 "jecxz 1f\n"
2055
2056 /* Set up alignment of the child stack:
2057 * child_stack = (child_stack & ~0xF) - 20;
2058 */
2059 "andl $-16,%%ecx\n"
2060 "subl $20,%%ecx\n"
2061
2062 /* Push "arg" and "fn" onto the stack that will be
2063 * used by the child.
2064 */
2065 "movl %6,%%eax\n"
2066 "movl %%eax,4(%%ecx)\n"
2067 "movl %3,%%eax\n"
2068 "movl %%eax,(%%ecx)\n"
2069
2070 /* %eax = syscall(%eax = __NR_clone,
2071 * %ebx = flags,
2072 * %ecx = child_stack,
2073 * %edx = parent_tidptr,
2074 * %esi = newtls,
2075 * %edi = child_tidptr)
2076 * Also, make sure that %ebx gets preserved as it is
2077 * used in PIC mode.
2078 */
2079 "movl %8,%%esi\n"
2080 "movl %7,%%edx\n"
2081 "movl %5,%%eax\n"
2082 "movl %9,%%edi\n"
2083 "pushl %%ebx\n"
2084 "movl %%eax,%%ebx\n"
2085 "movl %2,%%eax\n"
2086 LSS_ENTRYPOINT
2087
2088 /* In the parent: restore %ebx
2089 * In the child: move "fn" into %ebx
2090 */
2091 "popl %%ebx\n"
2092
2093 /* if (%eax != 0)
2094 * return %eax;
2095 */
2096 "test %%eax,%%eax\n"
2097 "jnz 1f\n"
2098
2099 /* In the child, now. Terminate frame pointer chain.
2100 */
2101 "movl $0,%%ebp\n"
2102
2103 /* Call "fn". "arg" is already on the stack.
2104 */
2105 "call *%%ebx\n"
2106
2107 /* Call _exit(%ebx). Unfortunately older versions
2108 * of gcc restrict the number of arguments that can
2109 * be passed to asm(). So, we need to hard-code the
2110 * system call number.
2111 */
2112 "movl %%eax,%%ebx\n"
2113 "movl $1,%%eax\n"
2114 LSS_ENTRYPOINT
2115
2116 /* Return to parent.
2117 */
2118 "1:\n"
2119 : "=a" (__res)
2120 : "0"(-EINVAL), "i"(__NR_clone),
2121 "m"(fn), "m"(child_stack), "m"(flags), "m"(arg),
2122 "m"(parent_tidptr), "m"(newtls), "m"(child_tidptr)
2123 : "esp", "memory", "ecx", "edx", "esi", "edi");
2124 LSS_RETURN(int, __res);
2125 }
2126
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002127 LSS_INLINE _syscall1(int, set_thread_area, void *, u)
2128 LSS_INLINE _syscall1(int, get_thread_area, void *, u)
2129
2130 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
2131 /* On i386, the kernel does not know how to return from a signal
2132 * handler. Instead, it relies on user space to provide a
2133 * restorer function that calls the {rt_,}sigreturn() system call.
2134 * Unfortunately, we cannot just reference the glibc version of this
2135 * function, as glibc goes out of its way to make it inaccessible.
2136 */
2137 void (*res)(void);
2138 __asm__ __volatile__("call 2f\n"
2139 "0:.align 16\n"
2140 "1:movl %1,%%eax\n"
2141 LSS_ENTRYPOINT
2142 "2:popl %0\n"
2143 "addl $(1b-0b),%0\n"
2144 : "=a" (res)
2145 : "i" (__NR_rt_sigreturn));
2146 return res;
2147 }
2148 LSS_INLINE void (*LSS_NAME(restore)(void))(void) {
2149 /* On i386, the kernel does not know how to return from a signal
2150 * handler. Instead, it relies on user space to provide a
2151 * restorer function that calls the {rt_,}sigreturn() system call.
2152 * Unfortunately, we cannot just reference the glibc version of this
2153 * function, as glibc goes out of its way to make it inaccessible.
2154 */
2155 void (*res)(void);
2156 __asm__ __volatile__("call 2f\n"
2157 "0:.align 16\n"
2158 "1:pop %%eax\n"
2159 "movl %1,%%eax\n"
2160 LSS_ENTRYPOINT
2161 "2:popl %0\n"
2162 "addl $(1b-0b),%0\n"
2163 : "=a" (res)
2164 : "i" (__NR_sigreturn));
2165 return res;
2166 }
2167 #elif defined(__x86_64__)
2168 /* There are no known problems with any of the _syscallX() macros
2169 * currently shipping for x86_64, but we still need to be able to define
2170 * our own version so that we can override the location of the errno
2171 * location (e.g. when using the clone() system call with the CLONE_VM
2172 * option).
2173 */
2174 #undef LSS_ENTRYPOINT
2175 #ifdef SYS_SYSCALL_ENTRYPOINT
2176 static inline void (**LSS_NAME(get_syscall_entrypoint)(void))(void) {
2177 void (**entrypoint)(void);
2178 asm volatile(".bss\n"
2179 ".align 8\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002180 ".globl " SYS_SYSCALL_ENTRYPOINT "\n"
2181 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002182 ".previous\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002183 "mov " SYS_SYSCALL_ENTRYPOINT "@GOTPCREL(%%rip), %0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002184 : "=r"(entrypoint));
2185 return entrypoint;
2186 }
2187
2188 #define LSS_ENTRYPOINT \
2189 ".bss\n" \
2190 ".align 8\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002191 ".globl " SYS_SYSCALL_ENTRYPOINT "\n" \
2192 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002193 ".previous\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002194 "mov " SYS_SYSCALL_ENTRYPOINT "@GOTPCREL(%%rip), %%rcx\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002195 "mov 0(%%rcx), %%rcx\n" \
2196 "test %%rcx, %%rcx\n" \
2197 "jz 10001f\n" \
2198 "call *%%rcx\n" \
2199 "jmp 10002f\n" \
2200 "10001:syscall\n" \
2201 "10002:\n"
2202
2203 #else
2204 #define LSS_ENTRYPOINT "syscall\n"
2205 #endif
vapier@chromium.org2273e812013-04-01 17:52:44 +00002206
2207 /* The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
2208 * We need to explicitly cast to an unsigned 64 bit type to avoid implicit
2209 * sign extension. We can't cast pointers directly because those are
2210 * 32 bits, and gcc will dump ugly warnings about casting from a pointer
2211 * to an integer of a different size.
2212 */
2213 #undef LSS_SYSCALL_ARG
2214 #define LSS_SYSCALL_ARG(a) ((uint64_t)(uintptr_t)(a))
2215 #undef _LSS_RETURN
2216 #define _LSS_RETURN(type, res, cast) \
2217 do { \
2218 if ((uint64_t)(res) >= (uint64_t)(-4095)) { \
2219 LSS_ERRNO = -(res); \
2220 res = -1; \
2221 } \
2222 return (type)(cast)(res); \
2223 } while (0)
2224 #undef LSS_RETURN
2225 #define LSS_RETURN(type, res) _LSS_RETURN(type, res, uintptr_t)
2226
2227 #undef _LSS_BODY
2228 #define _LSS_BODY(nr, type, name, cast, ...) \
2229 long long __res; \
2230 __asm__ __volatile__(LSS_BODY_ASM##nr LSS_ENTRYPOINT \
2231 : "=a" (__res) \
2232 : "0" (__NR_##name) LSS_BODY_ARG##nr(__VA_ARGS__) \
2233 : LSS_BODY_CLOBBER##nr "r11", "rcx", "memory"); \
2234 _LSS_RETURN(type, __res, cast)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002235 #undef LSS_BODY
vapier@chromium.org2273e812013-04-01 17:52:44 +00002236 #define LSS_BODY(nr, type, name, args...) \
2237 _LSS_BODY(nr, type, name, uintptr_t, ## args)
2238
2239 #undef LSS_BODY_ASM0
2240 #undef LSS_BODY_ASM1
2241 #undef LSS_BODY_ASM2
2242 #undef LSS_BODY_ASM3
2243 #undef LSS_BODY_ASM4
2244 #undef LSS_BODY_ASM5
2245 #undef LSS_BODY_ASM6
2246 #define LSS_BODY_ASM0
2247 #define LSS_BODY_ASM1 LSS_BODY_ASM0
2248 #define LSS_BODY_ASM2 LSS_BODY_ASM1
2249 #define LSS_BODY_ASM3 LSS_BODY_ASM2
2250 #define LSS_BODY_ASM4 LSS_BODY_ASM3 "movq %5,%%r10;"
2251 #define LSS_BODY_ASM5 LSS_BODY_ASM4 "movq %6,%%r8;"
2252 #define LSS_BODY_ASM6 LSS_BODY_ASM5 "movq %7,%%r9;"
2253
2254 #undef LSS_BODY_CLOBBER0
2255 #undef LSS_BODY_CLOBBER1
2256 #undef LSS_BODY_CLOBBER2
2257 #undef LSS_BODY_CLOBBER3
2258 #undef LSS_BODY_CLOBBER4
2259 #undef LSS_BODY_CLOBBER5
2260 #undef LSS_BODY_CLOBBER6
2261 #define LSS_BODY_CLOBBER0
2262 #define LSS_BODY_CLOBBER1 LSS_BODY_CLOBBER0
2263 #define LSS_BODY_CLOBBER2 LSS_BODY_CLOBBER1
2264 #define LSS_BODY_CLOBBER3 LSS_BODY_CLOBBER2
2265 #define LSS_BODY_CLOBBER4 LSS_BODY_CLOBBER3 "r10",
2266 #define LSS_BODY_CLOBBER5 LSS_BODY_CLOBBER4 "r8",
2267 #define LSS_BODY_CLOBBER6 LSS_BODY_CLOBBER5 "r9",
2268
2269 #undef LSS_BODY_ARG0
2270 #undef LSS_BODY_ARG1
2271 #undef LSS_BODY_ARG2
2272 #undef LSS_BODY_ARG3
2273 #undef LSS_BODY_ARG4
2274 #undef LSS_BODY_ARG5
2275 #undef LSS_BODY_ARG6
2276 #define LSS_BODY_ARG0()
2277 #define LSS_BODY_ARG1(arg1) \
2278 LSS_BODY_ARG0(), "D" (arg1)
2279 #define LSS_BODY_ARG2(arg1, arg2) \
2280 LSS_BODY_ARG1(arg1), "S" (arg2)
2281 #define LSS_BODY_ARG3(arg1, arg2, arg3) \
2282 LSS_BODY_ARG2(arg1, arg2), "d" (arg3)
2283 #define LSS_BODY_ARG4(arg1, arg2, arg3, arg4) \
2284 LSS_BODY_ARG3(arg1, arg2, arg3), "r" (arg4)
2285 #define LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5) \
2286 LSS_BODY_ARG4(arg1, arg2, arg3, arg4), "r" (arg5)
2287 #define LSS_BODY_ARG6(arg1, arg2, arg3, arg4, arg5, arg6) \
2288 LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5), "r" (arg6)
2289
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002290 #undef _syscall0
2291 #define _syscall0(type,name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002292 type LSS_NAME(name)(void) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002293 LSS_BODY(0, type, name); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002294 }
2295 #undef _syscall1
2296 #define _syscall1(type,name,type1,arg1) \
2297 type LSS_NAME(name)(type1 arg1) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002298 LSS_BODY(1, type, name, LSS_SYSCALL_ARG(arg1)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002299 }
2300 #undef _syscall2
2301 #define _syscall2(type,name,type1,arg1,type2,arg2) \
2302 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002303 LSS_BODY(2, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002304 }
2305 #undef _syscall3
2306 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
2307 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002308 LSS_BODY(3, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2309 LSS_SYSCALL_ARG(arg3)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002310 }
2311 #undef _syscall4
2312 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2313 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002314 LSS_BODY(4, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2315 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002316 }
2317 #undef _syscall5
2318 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2319 type5,arg5) \
2320 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2321 type5 arg5) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002322 LSS_BODY(5, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2323 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
2324 LSS_SYSCALL_ARG(arg5)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002325 }
2326 #undef _syscall6
2327 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2328 type5,arg5,type6,arg6) \
2329 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2330 type5 arg5, type6 arg6) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002331 LSS_BODY(6, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2332 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
2333 LSS_SYSCALL_ARG(arg5), LSS_SYSCALL_ARG(arg6));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002334 }
2335 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2336 int flags, void *arg, int *parent_tidptr,
2337 void *newtls, int *child_tidptr) {
vapier@chromium.org2273e812013-04-01 17:52:44 +00002338 long long __res;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002339 {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002340 __asm__ __volatile__(/* if (fn == NULL)
2341 * return -EINVAL;
2342 */
2343 "testq %4,%4\n"
2344 "jz 1f\n"
2345
2346 /* if (child_stack == NULL)
2347 * return -EINVAL;
2348 */
2349 "testq %5,%5\n"
2350 "jz 1f\n"
2351
2352 /* childstack -= 2*sizeof(void *);
2353 */
2354 "subq $16,%5\n"
2355
2356 /* Push "arg" and "fn" onto the stack that will be
2357 * used by the child.
2358 */
2359 "movq %7,8(%5)\n"
2360 "movq %4,0(%5)\n"
2361
2362 /* %rax = syscall(%rax = __NR_clone,
2363 * %rdi = flags,
2364 * %rsi = child_stack,
2365 * %rdx = parent_tidptr,
2366 * %r8 = new_tls,
2367 * %r10 = child_tidptr)
2368 */
2369 "movq %2,%%rax\n"
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00002370 "movq %9,%%r8\n"
2371 "movq %10,%%r10\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002372 LSS_ENTRYPOINT
2373
2374 /* if (%rax != 0)
2375 * return;
2376 */
2377 "testq %%rax,%%rax\n"
2378 "jnz 1f\n"
2379
2380 /* In the child. Terminate frame pointer chain.
2381 */
2382 "xorq %%rbp,%%rbp\n"
2383
2384 /* Call "fn(arg)".
2385 */
2386 "popq %%rax\n"
2387 "popq %%rdi\n"
2388 "call *%%rax\n"
2389
2390 /* Call _exit(%ebx).
2391 */
2392 "movq %%rax,%%rdi\n"
2393 "movq %3,%%rax\n"
2394 LSS_ENTRYPOINT
2395
2396 /* Return to parent.
2397 */
2398 "1:\n"
2399 : "=a" (__res)
2400 : "0"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit),
vapier@chromium.org2273e812013-04-01 17:52:44 +00002401 "r"(LSS_SYSCALL_ARG(fn)),
2402 "S"(LSS_SYSCALL_ARG(child_stack)),
2403 "D"(LSS_SYSCALL_ARG(flags)),
2404 "r"(LSS_SYSCALL_ARG(arg)),
2405 "d"(LSS_SYSCALL_ARG(parent_tidptr)),
2406 "r"(LSS_SYSCALL_ARG(newtls)),
2407 "r"(LSS_SYSCALL_ARG(child_tidptr))
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00002408 : "rsp", "memory", "r8", "r10", "r11", "rcx");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002409 }
2410 LSS_RETURN(int, __res);
2411 }
2412 LSS_INLINE _syscall2(int, arch_prctl, int, c, void *, a)
vapier@chromium.org2273e812013-04-01 17:52:44 +00002413
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002414 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
2415 /* On x86-64, the kernel does not know how to return from
2416 * a signal handler. Instead, it relies on user space to provide a
2417 * restorer function that calls the rt_sigreturn() system call.
2418 * Unfortunately, we cannot just reference the glibc version of this
2419 * function, as glibc goes out of its way to make it inaccessible.
2420 */
vapier@chromium.org2273e812013-04-01 17:52:44 +00002421 long long res;
mseaborn@chromium.org798c2f72013-08-31 00:04:49 +00002422 __asm__ __volatile__("jmp 2f\n"
2423 ".align 16\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002424 "1:movq %1,%%rax\n"
2425 LSS_ENTRYPOINT
mseaborn@chromium.org798c2f72013-08-31 00:04:49 +00002426 "2:leaq 1b(%%rip),%0\n"
2427 : "=r" (res)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002428 : "i" (__NR_rt_sigreturn));
vapier@chromium.org833a10e2013-04-02 19:34:26 +00002429 return (void (*)(void))(uintptr_t)res;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002430 }
2431 #elif defined(__ARM_ARCH_3__)
2432 /* Most definitions of _syscallX() neglect to mark "memory" as being
2433 * clobbered. This causes problems with compilers, that do a better job
2434 * at optimizing across __asm__ calls.
2435 * So, we just have to redefine all of the _syscallX() macros.
2436 */
2437 #undef LSS_REG
2438 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
2439 #undef LSS_BODY
2440 #define LSS_BODY(type,name,args...) \
2441 register long __res_r0 __asm__("r0"); \
2442 long __res; \
2443 __asm__ __volatile__ (__syscall(name) \
2444 : "=r"(__res_r0) : args : "lr", "memory"); \
2445 __res = __res_r0; \
2446 LSS_RETURN(type, __res)
2447 #undef _syscall0
2448 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002449 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002450 LSS_BODY(type, name); \
2451 }
2452 #undef _syscall1
2453 #define _syscall1(type, name, type1, arg1) \
2454 type LSS_NAME(name)(type1 arg1) { \
2455 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2456 }
2457 #undef _syscall2
2458 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2459 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2460 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2461 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2462 }
2463 #undef _syscall3
2464 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2465 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2466 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2467 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2468 }
2469 #undef _syscall4
2470 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2471 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2472 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2473 LSS_REG(3, arg4); \
2474 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2475 }
2476 #undef _syscall5
2477 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2478 type5,arg5) \
2479 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2480 type5 arg5) { \
2481 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2482 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2483 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2484 "r"(__r4)); \
2485 }
2486 #undef _syscall6
2487 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2488 type5,arg5,type6,arg6) \
2489 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2490 type5 arg5, type6 arg6) { \
2491 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2492 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2493 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2494 "r"(__r4), "r"(__r5)); \
2495 }
2496 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2497 int flags, void *arg, int *parent_tidptr,
2498 void *newtls, int *child_tidptr) {
2499 long __res;
2500 {
2501 register int __flags __asm__("r0") = flags;
2502 register void *__stack __asm__("r1") = child_stack;
2503 register void *__ptid __asm__("r2") = parent_tidptr;
2504 register void *__tls __asm__("r3") = newtls;
2505 register int *__ctid __asm__("r4") = child_tidptr;
2506 __asm__ __volatile__(/* if (fn == NULL || child_stack == NULL)
2507 * return -EINVAL;
2508 */
2509 "cmp %2,#0\n"
2510 "cmpne %3,#0\n"
2511 "moveq %0,%1\n"
2512 "beq 1f\n"
2513
2514 /* Push "arg" and "fn" onto the stack that will be
2515 * used by the child.
2516 */
2517 "str %5,[%3,#-4]!\n"
2518 "str %2,[%3,#-4]!\n"
2519
2520 /* %r0 = syscall(%r0 = flags,
2521 * %r1 = child_stack,
2522 * %r2 = parent_tidptr,
2523 * %r3 = newtls,
2524 * %r4 = child_tidptr)
2525 */
2526 __syscall(clone)"\n"
2527
2528 /* if (%r0 != 0)
2529 * return %r0;
2530 */
2531 "movs %0,r0\n"
2532 "bne 1f\n"
2533
2534 /* In the child, now. Call "fn(arg)".
2535 */
2536 "ldr r0,[sp, #4]\n"
2537 "mov lr,pc\n"
2538 "ldr pc,[sp]\n"
2539
2540 /* Call _exit(%r0).
2541 */
2542 __syscall(exit)"\n"
2543 "1:\n"
2544 : "=r" (__res)
2545 : "i"(-EINVAL),
2546 "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2547 "r"(__ptid), "r"(__tls), "r"(__ctid)
2548 : "cc", "lr", "memory");
2549 }
2550 LSS_RETURN(int, __res);
2551 }
2552 #elif defined(__ARM_EABI__)
2553 /* Most definitions of _syscallX() neglect to mark "memory" as being
2554 * clobbered. This causes problems with compilers, that do a better job
2555 * at optimizing across __asm__ calls.
2556 * So, we just have to redefine all fo the _syscallX() macros.
2557 */
2558 #undef LSS_REG
2559 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
2560 #undef LSS_BODY
2561 #define LSS_BODY(type,name,args...) \
2562 register long __res_r0 __asm__("r0"); \
2563 long __res; \
2564 __asm__ __volatile__ ("push {r7}\n" \
2565 "mov r7, %1\n" \
2566 "swi 0x0\n" \
2567 "pop {r7}\n" \
2568 : "=r"(__res_r0) \
2569 : "i"(__NR_##name) , ## args \
2570 : "lr", "memory"); \
2571 __res = __res_r0; \
2572 LSS_RETURN(type, __res)
2573 #undef _syscall0
2574 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002575 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002576 LSS_BODY(type, name); \
2577 }
2578 #undef _syscall1
2579 #define _syscall1(type, name, type1, arg1) \
2580 type LSS_NAME(name)(type1 arg1) { \
2581 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2582 }
2583 #undef _syscall2
2584 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2585 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2586 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2587 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2588 }
2589 #undef _syscall3
2590 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2591 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2592 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2593 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2594 }
2595 #undef _syscall4
2596 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2597 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2598 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2599 LSS_REG(3, arg4); \
2600 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2601 }
2602 #undef _syscall5
2603 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2604 type5,arg5) \
2605 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2606 type5 arg5) { \
2607 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2608 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2609 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2610 "r"(__r4)); \
2611 }
2612 #undef _syscall6
2613 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2614 type5,arg5,type6,arg6) \
2615 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2616 type5 arg5, type6 arg6) { \
2617 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2618 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2619 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2620 "r"(__r4), "r"(__r5)); \
2621 }
2622 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2623 int flags, void *arg, int *parent_tidptr,
2624 void *newtls, int *child_tidptr) {
2625 long __res;
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002626 if (fn == NULL || child_stack == NULL) {
2627 __res = -EINVAL;
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002628 LSS_RETURN(int, __res);
2629 }
2630
2631 /* Push "arg" and "fn" onto the stack that will be
2632 * used by the child.
2633 */
2634 {
2635 uintptr_t* cstack = (uintptr_t*)child_stack - 2;
2636 cstack[0] = (uintptr_t)fn;
2637 cstack[1] = (uintptr_t)arg;
2638 child_stack = cstack;
2639 }
2640 {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002641 register int __flags __asm__("r0") = flags;
2642 register void *__stack __asm__("r1") = child_stack;
2643 register void *__ptid __asm__("r2") = parent_tidptr;
2644 register void *__tls __asm__("r3") = newtls;
2645 register int *__ctid __asm__("r4") = child_tidptr;
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002646 __asm__ __volatile__(
Nico Weber63f24c82017-03-30 13:37:06 -04002647#ifdef __thumb2__
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002648 "push {r7}\n"
Nico Weber63f24c82017-03-30 13:37:06 -04002649#endif
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002650 /* %r0 = syscall(%r0 = flags,
2651 * %r1 = child_stack,
2652 * %r2 = parent_tidptr,
2653 * %r3 = newtls,
2654 * %r4 = child_tidptr)
2655 */
2656 "mov r7, %6\n"
2657 "swi 0x0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002658
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002659 /* if (%r0 != 0)
2660 * return %r0;
2661 */
2662 "cmp r0, #0\n"
2663 "bne 1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002664
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002665 /* In the child, now. Call "fn(arg)".
2666 */
2667 "ldr r0,[sp, #4]\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002668
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002669 "ldr lr,[sp]\n"
2670 "blx lr\n"
zodiac@gmail.com68c659b2011-10-06 05:34:19 +00002671
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002672 /* Call _exit(%r0).
2673 */
2674 "mov r7, %7\n"
2675 "swi 0x0\n"
2676 /* Unreachable */
2677 "bkpt #0\n"
2678 "1:\n"
Nico Weber63f24c82017-03-30 13:37:06 -04002679#ifdef __thumb2__
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002680 "pop {r7}\n"
Nico Weber63f24c82017-03-30 13:37:06 -04002681#endif
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002682 "movs %0,r0\n"
2683 : "=r"(__res)
2684 : "r"(__stack), "r"(__flags), "r"(__ptid), "r"(__tls), "r"(__ctid),
2685 "i"(__NR_clone), "i"(__NR_exit)
2686 : "cc", "lr", "memory"
2687#ifndef __thumb2__
2688 , "r7"
Nico Weber63f24c82017-03-30 13:37:06 -04002689#endif
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002690 );
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002691 }
2692 LSS_RETURN(int, __res);
2693 }
anton@chromium.org2f724fc2014-04-15 13:05:20 +00002694 #elif defined(__aarch64__)
2695 /* Most definitions of _syscallX() neglect to mark "memory" as being
2696 * clobbered. This causes problems with compilers, that do a better job
2697 * at optimizing across __asm__ calls.
2698 * So, we just have to redefine all of the _syscallX() macros.
2699 */
2700 #undef LSS_REG
2701 #define LSS_REG(r,a) register int64_t __r##r __asm__("x"#r) = (int64_t)a
2702 #undef LSS_BODY
2703 #define LSS_BODY(type,name,args...) \
2704 register int64_t __res_x0 __asm__("x0"); \
2705 int64_t __res; \
2706 __asm__ __volatile__ ("mov x8, %1\n" \
2707 "svc 0x0\n" \
2708 : "=r"(__res_x0) \
2709 : "i"(__NR_##name) , ## args \
2710 : "x8", "memory"); \
2711 __res = __res_x0; \
2712 LSS_RETURN(type, __res)
2713 #undef _syscall0
2714 #define _syscall0(type, name) \
2715 type LSS_NAME(name)(void) { \
2716 LSS_BODY(type, name); \
2717 }
2718 #undef _syscall1
2719 #define _syscall1(type, name, type1, arg1) \
2720 type LSS_NAME(name)(type1 arg1) { \
2721 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2722 }
2723 #undef _syscall2
2724 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2725 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2726 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2727 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2728 }
2729 #undef _syscall3
2730 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2731 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2732 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2733 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2734 }
2735 #undef _syscall4
2736 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2737 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2738 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2739 LSS_REG(3, arg4); \
2740 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2741 }
2742 #undef _syscall5
2743 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2744 type5,arg5) \
2745 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2746 type5 arg5) { \
2747 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2748 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2749 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2750 "r"(__r4)); \
2751 }
2752 #undef _syscall6
2753 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2754 type5,arg5,type6,arg6) \
2755 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2756 type5 arg5, type6 arg6) { \
2757 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2758 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2759 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2760 "r"(__r4), "r"(__r5)); \
2761 }
2762
2763 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2764 int flags, void *arg, int *parent_tidptr,
2765 void *newtls, int *child_tidptr) {
2766 int64_t __res;
2767 {
2768 register uint64_t __flags __asm__("x0") = flags;
2769 register void *__stack __asm__("x1") = child_stack;
2770 register void *__ptid __asm__("x2") = parent_tidptr;
2771 register void *__tls __asm__("x3") = newtls;
2772 register int *__ctid __asm__("x4") = child_tidptr;
2773 __asm__ __volatile__(/* Push "arg" and "fn" onto the stack that will be
2774 * used by the child.
2775 */
2776 "stp %1, %4, [%2, #-16]!\n"
2777
2778 /* %x0 = syscall(%x0 = flags,
2779 * %x1 = child_stack,
2780 * %x2 = parent_tidptr,
2781 * %x3 = newtls,
2782 * %x4 = child_tidptr)
2783 */
2784 "mov x8, %8\n"
2785 "svc 0x0\n"
2786
2787 /* if (%r0 != 0)
2788 * return %r0;
2789 */
2790 "mov %0, x0\n"
2791 "cbnz x0, 1f\n"
2792
2793 /* In the child, now. Call "fn(arg)".
2794 */
2795 "ldp x1, x0, [sp], #16\n"
2796 "blr x1\n"
2797
2798 /* Call _exit(%r0).
2799 */
2800 "mov x8, %9\n"
2801 "svc 0x0\n"
2802 "1:\n"
2803 : "=r" (__res)
2804 : "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2805 "r"(__ptid), "r"(__tls), "r"(__ctid),
2806 "i"(__NR_clone), "i"(__NR_exit)
2807 : "cc", "x8", "memory");
2808 }
2809 LSS_RETURN(int, __res);
2810 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002811 #elif defined(__mips__)
2812 #undef LSS_REG
2813 #define LSS_REG(r,a) register unsigned long __r##r __asm__("$"#r) = \
2814 (unsigned long)(a)
2815 #undef LSS_BODY
thestig@chromium.org952107f2014-08-01 02:22:56 +00002816 #undef LSS_SYSCALL_CLOBBERS
2817 #if _MIPS_SIM == _MIPS_SIM_ABI32
2818 #define LSS_SYSCALL_CLOBBERS "$1", "$3", "$8", "$9", "$10", \
2819 "$11", "$12", "$13", "$14", "$15", \
2820 "$24", "$25", "hi", "lo", "memory"
2821 #else
2822 #define LSS_SYSCALL_CLOBBERS "$1", "$3", "$10", "$11", "$12", \
2823 "$13", "$14", "$15", "$24", "$25", \
2824 "hi", "lo", "memory"
2825 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002826 #define LSS_BODY(type,name,r7,...) \
2827 register unsigned long __v0 __asm__("$2") = __NR_##name; \
2828 __asm__ __volatile__ ("syscall\n" \
vapier@chromium.orgda4a4892015-01-22 16:46:39 +00002829 : "=r"(__v0), r7 (__r7) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002830 : "0"(__v0), ##__VA_ARGS__ \
thestig@chromium.org952107f2014-08-01 02:22:56 +00002831 : LSS_SYSCALL_CLOBBERS); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002832 LSS_RETURN(type, __v0, __r7)
2833 #undef _syscall0
2834 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002835 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002836 register unsigned long __r7 __asm__("$7"); \
2837 LSS_BODY(type, name, "=r"); \
2838 }
2839 #undef _syscall1
2840 #define _syscall1(type, name, type1, arg1) \
2841 type LSS_NAME(name)(type1 arg1) { \
2842 register unsigned long __r7 __asm__("$7"); \
2843 LSS_REG(4, arg1); LSS_BODY(type, name, "=r", "r"(__r4)); \
2844 }
2845 #undef _syscall2
2846 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2847 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2848 register unsigned long __r7 __asm__("$7"); \
2849 LSS_REG(4, arg1); LSS_REG(5, arg2); \
2850 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5)); \
2851 }
2852 #undef _syscall3
2853 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2854 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2855 register unsigned long __r7 __asm__("$7"); \
2856 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2857 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5), "r"(__r6)); \
2858 }
2859 #undef _syscall4
2860 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2861 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2862 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2863 LSS_REG(7, arg4); \
2864 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6)); \
2865 }
2866 #undef _syscall5
2867 #if _MIPS_SIM == _MIPS_SIM_ABI32
2868 /* The old 32bit MIPS system call API passes the fifth and sixth argument
2869 * on the stack, whereas the new APIs use registers "r8" and "r9".
2870 */
2871 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2872 type5,arg5) \
2873 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2874 type5 arg5) { \
2875 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2876 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002877 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002878 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002879 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002880 "sw %5, 16($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002881 "syscall\n" \
2882 "addiu $29, 32\n" \
2883 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002884 : "+r"(__v0), "+r" (__r7) \
2885 : "r"(__r4), "r"(__r5), \
2886 "r"(__r6), "r" ((unsigned long)arg5) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002887 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002888 "$13", "$14", "$15", "$24", "$25", \
2889 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002890 LSS_RETURN(type, __v0, __r7); \
2891 }
2892 #else
2893 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2894 type5,arg5) \
2895 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2896 type5 arg5) { \
2897 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2898 LSS_REG(7, arg4); LSS_REG(8, arg5); \
2899 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
2900 "r"(__r8)); \
2901 }
2902 #endif
2903 #undef _syscall6
2904 #if _MIPS_SIM == _MIPS_SIM_ABI32
2905 /* The old 32bit MIPS system call API passes the fifth and sixth argument
2906 * on the stack, whereas the new APIs use registers "r8" and "r9".
2907 */
2908 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2909 type5,arg5,type6,arg6) \
2910 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2911 type5 arg5, type6 arg6) { \
2912 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2913 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002914 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002915 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002916 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002917 "sw %5, 16($29)\n" \
2918 "sw %6, 20($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002919 "syscall\n" \
2920 "addiu $29, 32\n" \
2921 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002922 : "+r"(__v0), "+r" (__r7) \
2923 : "r"(__r4), "r"(__r5), \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002924 "r"(__r6), "r" ((unsigned long)arg5), \
2925 "r" ((unsigned long)arg6) \
2926 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002927 "$13", "$14", "$15", "$24", "$25", \
2928 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002929 LSS_RETURN(type, __v0, __r7); \
2930 }
2931 #else
2932 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2933 type5,arg5,type6,arg6) \
2934 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2935 type5 arg5,type6 arg6) { \
2936 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2937 LSS_REG(7, arg4); LSS_REG(8, arg5); LSS_REG(9, arg6); \
2938 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
2939 "r"(__r8), "r"(__r9)); \
2940 }
2941 #endif
2942 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2943 int flags, void *arg, int *parent_tidptr,
2944 void *newtls, int *child_tidptr) {
vapier@chromium.orge0797682015-02-20 20:45:56 +00002945 register unsigned long __v0 __asm__("$2") = -EINVAL;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002946 register unsigned long __r7 __asm__("$7") = (unsigned long)newtls;
2947 {
2948 register int __flags __asm__("$4") = flags;
2949 register void *__stack __asm__("$5") = child_stack;
2950 register void *__ptid __asm__("$6") = parent_tidptr;
2951 register int *__ctid __asm__("$8") = child_tidptr;
2952 __asm__ __volatile__(
2953 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
2954 "subu $29,24\n"
2955 #elif _MIPS_SIM == _MIPS_SIM_NABI32
2956 "sub $29,16\n"
2957 #else
2958 "dsubu $29,16\n"
2959 #endif
2960
2961 /* if (fn == NULL || child_stack == NULL)
2962 * return -EINVAL;
2963 */
vapier@chromium.orge0797682015-02-20 20:45:56 +00002964 "beqz %4,1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002965 "beqz %5,1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002966
2967 /* Push "arg" and "fn" onto the stack that will be
2968 * used by the child.
2969 */
2970 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
vapier@chromium.orge0797682015-02-20 20:45:56 +00002971 "subu %5,32\n"
2972 "sw %4,0(%5)\n"
2973 "sw %7,4(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002974 #elif _MIPS_SIM == _MIPS_SIM_NABI32
vapier@chromium.orge0797682015-02-20 20:45:56 +00002975 "sub %5,32\n"
2976 "sw %4,0(%5)\n"
2977 "sw %7,8(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002978 #else
vapier@chromium.orge0797682015-02-20 20:45:56 +00002979 "dsubu %5,32\n"
2980 "sd %4,0(%5)\n"
2981 "sd %7,8(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002982 #endif
2983
2984 /* $7 = syscall($4 = flags,
2985 * $5 = child_stack,
2986 * $6 = parent_tidptr,
2987 * $7 = newtls,
2988 * $8 = child_tidptr)
2989 */
vapier@chromium.orge0797682015-02-20 20:45:56 +00002990 "li $2,%2\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002991 "syscall\n"
2992
2993 /* if ($7 != 0)
2994 * return $2;
2995 */
2996 "bnez $7,1f\n"
2997 "bnez $2,1f\n"
2998
2999 /* In the child, now. Call "fn(arg)".
3000 */
3001 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
3002 "lw $25,0($29)\n"
3003 "lw $4,4($29)\n"
3004 #elif _MIPS_SIM == _MIPS_SIM_NABI32
3005 "lw $25,0($29)\n"
3006 "lw $4,8($29)\n"
3007 #else
3008 "ld $25,0($29)\n"
3009 "ld $4,8($29)\n"
3010 #endif
3011 "jalr $25\n"
3012
3013 /* Call _exit($2)
3014 */
3015 "move $4,$2\n"
vapier@chromium.orge0797682015-02-20 20:45:56 +00003016 "li $2,%3\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003017 "syscall\n"
3018
3019 "1:\n"
3020 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
3021 "addu $29, 24\n"
3022 #elif _MIPS_SIM == _MIPS_SIM_NABI32
3023 "add $29, 16\n"
3024 #else
3025 "daddu $29,16\n"
3026 #endif
petarj@mips.com0ece1c62013-04-10 00:28:04 +00003027 : "+r" (__v0), "+r" (__r7)
vapier@chromium.orge0797682015-02-20 20:45:56 +00003028 : "i"(__NR_clone), "i"(__NR_exit), "r"(fn),
3029 "r"(__stack), "r"(__flags), "r"(arg),
3030 "r"(__ptid), "r"(__ctid)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003031 : "$9", "$10", "$11", "$12", "$13", "$14", "$15",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003032 "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003033 }
3034 LSS_RETURN(int, __v0, __r7);
3035 }
3036 #elif defined (__PPC__)
3037 #undef LSS_LOADARGS_0
3038 #define LSS_LOADARGS_0(name, dummy...) \
3039 __sc_0 = __NR_##name
3040 #undef LSS_LOADARGS_1
3041 #define LSS_LOADARGS_1(name, arg1) \
3042 LSS_LOADARGS_0(name); \
3043 __sc_3 = (unsigned long) (arg1)
3044 #undef LSS_LOADARGS_2
3045 #define LSS_LOADARGS_2(name, arg1, arg2) \
3046 LSS_LOADARGS_1(name, arg1); \
3047 __sc_4 = (unsigned long) (arg2)
3048 #undef LSS_LOADARGS_3
3049 #define LSS_LOADARGS_3(name, arg1, arg2, arg3) \
3050 LSS_LOADARGS_2(name, arg1, arg2); \
3051 __sc_5 = (unsigned long) (arg3)
3052 #undef LSS_LOADARGS_4
3053 #define LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4) \
3054 LSS_LOADARGS_3(name, arg1, arg2, arg3); \
3055 __sc_6 = (unsigned long) (arg4)
3056 #undef LSS_LOADARGS_5
3057 #define LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5) \
3058 LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4); \
3059 __sc_7 = (unsigned long) (arg5)
3060 #undef LSS_LOADARGS_6
3061 #define LSS_LOADARGS_6(name, arg1, arg2, arg3, arg4, arg5, arg6) \
3062 LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5); \
3063 __sc_8 = (unsigned long) (arg6)
3064 #undef LSS_ASMINPUT_0
3065 #define LSS_ASMINPUT_0 "0" (__sc_0)
3066 #undef LSS_ASMINPUT_1
3067 #define LSS_ASMINPUT_1 LSS_ASMINPUT_0, "1" (__sc_3)
3068 #undef LSS_ASMINPUT_2
3069 #define LSS_ASMINPUT_2 LSS_ASMINPUT_1, "2" (__sc_4)
3070 #undef LSS_ASMINPUT_3
3071 #define LSS_ASMINPUT_3 LSS_ASMINPUT_2, "3" (__sc_5)
3072 #undef LSS_ASMINPUT_4
3073 #define LSS_ASMINPUT_4 LSS_ASMINPUT_3, "4" (__sc_6)
3074 #undef LSS_ASMINPUT_5
3075 #define LSS_ASMINPUT_5 LSS_ASMINPUT_4, "5" (__sc_7)
3076 #undef LSS_ASMINPUT_6
3077 #define LSS_ASMINPUT_6 LSS_ASMINPUT_5, "6" (__sc_8)
3078 #undef LSS_BODY
3079 #define LSS_BODY(nr, type, name, args...) \
3080 long __sc_ret, __sc_err; \
3081 { \
3082 register unsigned long __sc_0 __asm__ ("r0"); \
3083 register unsigned long __sc_3 __asm__ ("r3"); \
3084 register unsigned long __sc_4 __asm__ ("r4"); \
3085 register unsigned long __sc_5 __asm__ ("r5"); \
3086 register unsigned long __sc_6 __asm__ ("r6"); \
3087 register unsigned long __sc_7 __asm__ ("r7"); \
3088 register unsigned long __sc_8 __asm__ ("r8"); \
3089 \
3090 LSS_LOADARGS_##nr(name, args); \
3091 __asm__ __volatile__ \
3092 ("sc\n\t" \
3093 "mfcr %0" \
3094 : "=&r" (__sc_0), \
3095 "=&r" (__sc_3), "=&r" (__sc_4), \
3096 "=&r" (__sc_5), "=&r" (__sc_6), \
3097 "=&r" (__sc_7), "=&r" (__sc_8) \
3098 : LSS_ASMINPUT_##nr \
3099 : "cr0", "ctr", "memory", \
3100 "r9", "r10", "r11", "r12"); \
3101 __sc_ret = __sc_3; \
3102 __sc_err = __sc_0; \
3103 } \
3104 LSS_RETURN(type, __sc_ret, __sc_err)
3105 #undef _syscall0
3106 #define _syscall0(type, name) \
3107 type LSS_NAME(name)(void) { \
3108 LSS_BODY(0, type, name); \
3109 }
3110 #undef _syscall1
3111 #define _syscall1(type, name, type1, arg1) \
3112 type LSS_NAME(name)(type1 arg1) { \
3113 LSS_BODY(1, type, name, arg1); \
3114 }
3115 #undef _syscall2
3116 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3117 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3118 LSS_BODY(2, type, name, arg1, arg2); \
3119 }
3120 #undef _syscall3
3121 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3122 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3123 LSS_BODY(3, type, name, arg1, arg2, arg3); \
3124 }
3125 #undef _syscall4
3126 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3127 type4, arg4) \
3128 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3129 LSS_BODY(4, type, name, arg1, arg2, arg3, arg4); \
3130 }
3131 #undef _syscall5
3132 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3133 type4, arg4, type5, arg5) \
3134 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3135 type5 arg5) { \
3136 LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5); \
3137 }
3138 #undef _syscall6
3139 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3140 type4, arg4, type5, arg5, type6, arg6) \
3141 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3142 type5 arg5, type6 arg6) { \
3143 LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6); \
3144 }
3145 /* clone function adapted from glibc 2.3.6 clone.S */
3146 /* TODO(csilvers): consider wrapping some args up in a struct, like we
3147 * do for i386's _syscall6, so we can compile successfully on gcc 2.95
3148 */
3149 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3150 int flags, void *arg, int *parent_tidptr,
3151 void *newtls, int *child_tidptr) {
3152 long __ret, __err;
3153 {
3154 register int (*__fn)(void *) __asm__ ("r8") = fn;
3155 register void *__cstack __asm__ ("r4") = child_stack;
3156 register int __flags __asm__ ("r3") = flags;
3157 register void * __arg __asm__ ("r9") = arg;
3158 register int * __ptidptr __asm__ ("r5") = parent_tidptr;
3159 register void * __newtls __asm__ ("r6") = newtls;
3160 register int * __ctidptr __asm__ ("r7") = child_tidptr;
3161 __asm__ __volatile__(
3162 /* check for fn == NULL
3163 * and child_stack == NULL
3164 */
3165 "cmpwi cr0, %6, 0\n\t"
3166 "cmpwi cr1, %7, 0\n\t"
3167 "cror cr0*4+eq, cr1*4+eq, cr0*4+eq\n\t"
3168 "beq- cr0, 1f\n\t"
3169
3170 /* set up stack frame for child */
3171 "clrrwi %7, %7, 4\n\t"
3172 "li 0, 0\n\t"
3173 "stwu 0, -16(%7)\n\t"
3174
3175 /* fn, arg, child_stack are saved across the syscall: r28-30 */
3176 "mr 28, %6\n\t"
3177 "mr 29, %7\n\t"
3178 "mr 27, %9\n\t"
3179
3180 /* syscall */
3181 "li 0, %4\n\t"
3182 /* flags already in r3
3183 * child_stack already in r4
3184 * ptidptr already in r5
3185 * newtls already in r6
3186 * ctidptr already in r7
3187 */
3188 "sc\n\t"
3189
3190 /* Test if syscall was successful */
3191 "cmpwi cr1, 3, 0\n\t"
3192 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
3193 "bne- cr1, 1f\n\t"
3194
3195 /* Do the function call */
3196 "mtctr 28\n\t"
3197 "mr 3, 27\n\t"
3198 "bctrl\n\t"
3199
3200 /* Call _exit(r3) */
3201 "li 0, %5\n\t"
3202 "sc\n\t"
3203
3204 /* Return to parent */
3205 "1:\n"
3206 "mfcr %1\n\t"
3207 "mr %0, 3\n\t"
3208 : "=r" (__ret), "=r" (__err)
3209 : "0" (-1), "1" (EINVAL),
3210 "i" (__NR_clone), "i" (__NR_exit),
3211 "r" (__fn), "r" (__cstack), "r" (__flags),
3212 "r" (__arg), "r" (__ptidptr), "r" (__newtls),
3213 "r" (__ctidptr)
3214 : "cr0", "cr1", "memory", "ctr",
3215 "r0", "r29", "r27", "r28");
3216 }
3217 LSS_RETURN(int, __ret, __err);
3218 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04003219 #elif defined(__s390__)
3220 #undef LSS_REG
3221 #define LSS_REG(r, a) register unsigned long __r##r __asm__("r"#r) = (unsigned long) a
3222 #undef LSS_BODY
3223 #define LSS_BODY(type, name, args...) \
3224 register unsigned long __nr __asm__("r1") \
3225 = (unsigned long)(__NR_##name); \
3226 register long __res_r2 __asm__("r2"); \
3227 long __res; \
3228 __asm__ __volatile__ \
3229 ("svc 0\n\t" \
3230 : "=d"(__res_r2) \
3231 : "d"(__nr), ## args \
3232 : "memory"); \
3233 __res = __res_r2; \
3234 LSS_RETURN(type, __res)
3235 #undef _syscall0
3236 #define _syscall0(type, name) \
3237 type LSS_NAME(name)(void) { \
3238 LSS_BODY(type, name); \
3239 }
3240 #undef _syscall1
3241 #define _syscall1(type, name, type1, arg1) \
3242 type LSS_NAME(name)(type1 arg1) { \
3243 LSS_REG(2, arg1); \
3244 LSS_BODY(type, name, "0"(__r2)); \
3245 }
3246 #undef _syscall2
3247 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3248 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3249 LSS_REG(2, arg1); LSS_REG(3, arg2); \
3250 LSS_BODY(type, name, "0"(__r2), "d"(__r3)); \
3251 }
3252 #undef _syscall3
3253 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3254 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3255 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3256 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4)); \
3257 }
3258 #undef _syscall4
3259 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3260 type4, arg4) \
3261 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3262 type4 arg4) { \
3263 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3264 LSS_REG(5, arg4); \
3265 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3266 "d"(__r5)); \
3267 }
3268 #undef _syscall5
3269 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3270 type4, arg4, type5, arg5) \
3271 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3272 type4 arg4, type5 arg5) { \
3273 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3274 LSS_REG(5, arg4); LSS_REG(6, arg5); \
3275 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3276 "d"(__r5), "d"(__r6)); \
3277 }
3278 #undef _syscall6
3279 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3280 type4, arg4, type5, arg5, type6, arg6) \
3281 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3282 type4 arg4, type5 arg5, type6 arg6) { \
3283 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3284 LSS_REG(5, arg4); LSS_REG(6, arg5); LSS_REG(7, arg6); \
3285 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3286 "d"(__r5), "d"(__r6), "d"(__r7)); \
3287 }
3288 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3289 int flags, void *arg, int *parent_tidptr,
3290 void *newtls, int *child_tidptr) {
3291 long __ret;
3292 {
3293 register int (*__fn)(void *) __asm__ ("r1") = fn;
3294 register void *__cstack __asm__ ("r2") = child_stack;
3295 register int __flags __asm__ ("r3") = flags;
3296 register void *__arg __asm__ ("r0") = arg;
3297 register int *__ptidptr __asm__ ("r4") = parent_tidptr;
3298 register void *__newtls __asm__ ("r6") = newtls;
3299 register int *__ctidptr __asm__ ("r5") = child_tidptr;
3300 __asm__ __volatile__ (
3301 #ifndef __s390x__
3302 /* arg already in r0 */
3303 "ltr %4, %4\n\t" /* check fn, which is already in r1 */
3304 "jz 1f\n\t" /* NULL function pointer, return -EINVAL */
3305 "ltr %5, %5\n\t" /* check child_stack, which is already in r2 */
3306 "jz 1f\n\t" /* NULL stack pointer, return -EINVAL */
3307 /* flags already in r3 */
3308 /* parent_tidptr already in r4 */
3309 /* child_tidptr already in r5 */
3310 /* newtls already in r6 */
3311 "svc %2\n\t" /* invoke clone syscall */
3312 "ltr %0,%%r2\n\t" /* load return code into __ret and test */
3313 "jnz 1f\n\t" /* return to parent if non-zero */
3314 /* start child thread */
3315 "lr %%r2, %7\n\t" /* set first parameter to void *arg */
3316 "ahi %%r15, -96\n\t" /* make room on the stack for the save area */
3317 "xc 0(4,%%r15), 0(%%r15)\n\t"
3318 "basr %%r14, %4\n\t" /* jump to fn */
3319 "svc %3\n" /* invoke exit syscall */
3320 "1:\n"
3321 #else
3322 /* arg already in r0 */
3323 "ltgr %4, %4\n\t" /* check fn, which is already in r1 */
3324 "jz 1f\n\t" /* NULL function pointer, return -EINVAL */
3325 "ltgr %5, %5\n\t" /* check child_stack, which is already in r2 */
3326 "jz 1f\n\t" /* NULL stack pointer, return -EINVAL */
3327 /* flags already in r3 */
3328 /* parent_tidptr already in r4 */
3329 /* child_tidptr already in r5 */
3330 /* newtls already in r6 */
3331 "svc %2\n\t" /* invoke clone syscall */
3332 "ltgr %0, %%r2\n\t" /* load return code into __ret and test */
3333 "jnz 1f\n\t" /* return to parent if non-zero */
3334 /* start child thread */
3335 "lgr %%r2, %7\n\t" /* set first parameter to void *arg */
3336 "aghi %%r15, -160\n\t" /* make room on the stack for the save area */
3337 "xc 0(8,%%r15), 0(%%r15)\n\t"
3338 "basr %%r14, %4\n\t" /* jump to fn */
3339 "svc %3\n" /* invoke exit syscall */
3340 "1:\n"
3341 #endif
3342 : "=r" (__ret)
3343 : "0" (-EINVAL), "i" (__NR_clone), "i" (__NR_exit),
3344 "d" (__fn), "d" (__cstack), "d" (__flags), "d" (__arg),
3345 "d" (__ptidptr), "d" (__newtls), "d" (__ctidptr)
3346 : "cc", "r14", "memory"
3347 );
3348 }
3349 LSS_RETURN(int, __ret);
3350 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003351 #endif
3352 #define __NR__exit __NR_exit
3353 #define __NR__gettid __NR_gettid
3354 #define __NR__mremap __NR_mremap
phosek@chromium.orga9c02722013-08-16 17:31:42 +00003355 LSS_INLINE _syscall1(void *, brk, void *, e)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003356 LSS_INLINE _syscall1(int, chdir, const char *,p)
3357 LSS_INLINE _syscall1(int, close, int, f)
3358 LSS_INLINE _syscall2(int, clock_getres, int, c,
3359 struct kernel_timespec*, t)
3360 LSS_INLINE _syscall2(int, clock_gettime, int, c,
3361 struct kernel_timespec*, t)
3362 LSS_INLINE _syscall1(int, dup, int, f)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003363 #if defined(__NR_dup2)
3364 // dup2 is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003365 LSS_INLINE _syscall2(int, dup2, int, s,
3366 int, d)
3367 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003368 #if defined(__NR_dup3)
3369 LSS_INLINE _syscall3(int, dup3, int, s, int, d, int, f)
3370 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003371 LSS_INLINE _syscall3(int, execve, const char*, f,
3372 const char*const*,a,const char*const*, e)
3373 LSS_INLINE _syscall1(int, _exit, int, e)
3374 LSS_INLINE _syscall1(int, exit_group, int, e)
3375 LSS_INLINE _syscall3(int, fcntl, int, f,
3376 int, c, long, a)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003377 #if defined(__NR_fork)
3378 // fork is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003379 LSS_INLINE _syscall0(pid_t, fork)
3380 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003381 LSS_INLINE _syscall2(int, fstat, int, f,
3382 struct kernel_stat*, b)
3383 LSS_INLINE _syscall2(int, fstatfs, int, f,
3384 struct kernel_statfs*, b)
vapier@chromium.org2273e812013-04-01 17:52:44 +00003385 #if defined(__x86_64__)
3386 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
3387 LSS_INLINE int LSS_NAME(ftruncate)(int f, off_t l) {
3388 LSS_BODY(2, int, ftruncate, LSS_SYSCALL_ARG(f), (uint64_t)(l));
3389 }
3390 #else
3391 LSS_INLINE _syscall2(int, ftruncate, int, f,
3392 off_t, l)
3393 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003394 LSS_INLINE _syscall4(int, futex, int*, a,
3395 int, o, int, v,
3396 struct kernel_timespec*, t)
3397 LSS_INLINE _syscall3(int, getdents, int, f,
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003398 struct kernel_dirent*, d, int, c)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003399 LSS_INLINE _syscall3(int, getdents64, int, f,
3400 struct kernel_dirent64*, d, int, c)
3401 LSS_INLINE _syscall0(gid_t, getegid)
3402 LSS_INLINE _syscall0(uid_t, geteuid)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003403 #if defined(__NR_getpgrp)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003404 LSS_INLINE _syscall0(pid_t, getpgrp)
3405 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003406 LSS_INLINE _syscall0(pid_t, getpid)
3407 LSS_INLINE _syscall0(pid_t, getppid)
3408 LSS_INLINE _syscall2(int, getpriority, int, a,
3409 int, b)
3410 LSS_INLINE _syscall3(int, getresgid, gid_t *, r,
3411 gid_t *, e, gid_t *, s)
3412 LSS_INLINE _syscall3(int, getresuid, uid_t *, r,
3413 uid_t *, e, uid_t *, s)
3414#if !defined(__ARM_EABI__)
3415 LSS_INLINE _syscall2(int, getrlimit, int, r,
3416 struct kernel_rlimit*, l)
3417#endif
3418 LSS_INLINE _syscall1(pid_t, getsid, pid_t, p)
3419 LSS_INLINE _syscall0(pid_t, _gettid)
3420 LSS_INLINE _syscall2(pid_t, gettimeofday, struct kernel_timeval*, t,
3421 void*, tz)
3422 LSS_INLINE _syscall5(int, setxattr, const char *,p,
3423 const char *, n, const void *,v,
3424 size_t, s, int, f)
3425 LSS_INLINE _syscall5(int, lsetxattr, const char *,p,
3426 const char *, n, const void *,v,
3427 size_t, s, int, f)
3428 LSS_INLINE _syscall4(ssize_t, getxattr, const char *,p,
3429 const char *, n, void *, v, size_t, s)
3430 LSS_INLINE _syscall4(ssize_t, lgetxattr, const char *,p,
3431 const char *, n, void *, v, size_t, s)
3432 LSS_INLINE _syscall3(ssize_t, listxattr, const char *,p,
3433 char *, l, size_t, s)
3434 LSS_INLINE _syscall3(ssize_t, llistxattr, const char *,p,
3435 char *, l, size_t, s)
3436 LSS_INLINE _syscall3(int, ioctl, int, d,
3437 int, r, void *, a)
3438 LSS_INLINE _syscall2(int, ioprio_get, int, which,
3439 int, who)
3440 LSS_INLINE _syscall3(int, ioprio_set, int, which,
3441 int, who, int, ioprio)
3442 LSS_INLINE _syscall2(int, kill, pid_t, p,
3443 int, s)
vapier@chromium.org2273e812013-04-01 17:52:44 +00003444 #if defined(__x86_64__)
3445 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
3446 LSS_INLINE off_t LSS_NAME(lseek)(int f, off_t o, int w) {
3447 _LSS_BODY(3, off_t, lseek, off_t, LSS_SYSCALL_ARG(f), (uint64_t)(o),
3448 LSS_SYSCALL_ARG(w));
3449 }
3450 #else
3451 LSS_INLINE _syscall3(off_t, lseek, int, f,
3452 off_t, o, int, w)
3453 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003454 LSS_INLINE _syscall2(int, munmap, void*, s,
3455 size_t, l)
3456 LSS_INLINE _syscall6(long, move_pages, pid_t, p,
3457 unsigned long, n, void **,g, int *, d,
3458 int *, s, int, f)
3459 LSS_INLINE _syscall3(int, mprotect, const void *,a,
3460 size_t, l, int, p)
3461 LSS_INLINE _syscall5(void*, _mremap, void*, o,
3462 size_t, os, size_t, ns,
3463 unsigned long, f, void *, a)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003464 #if defined(__NR_open)
3465 // open is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003466 LSS_INLINE _syscall3(int, open, const char*, p,
3467 int, f, int, m)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003468 #endif
3469 #if defined(__NR_poll)
3470 // poll is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003471 LSS_INLINE _syscall3(int, poll, struct kernel_pollfd*, u,
3472 unsigned int, n, int, t)
3473 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003474 #if defined(__NR_ppoll)
3475 LSS_INLINE _syscall5(int, ppoll, struct kernel_pollfd *, u,
3476 unsigned int, n, const struct kernel_timespec *, t,
3477 const struct kernel_sigset_t *, sigmask, size_t, s)
3478 #endif
mseaborn@chromium.orge6c76822013-08-31 00:08:44 +00003479 LSS_INLINE _syscall5(int, prctl, int, option,
3480 unsigned long, arg2,
3481 unsigned long, arg3,
3482 unsigned long, arg4,
3483 unsigned long, arg5)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003484 LSS_INLINE _syscall4(long, ptrace, int, r,
3485 pid_t, p, void *, a, void *, d)
3486 #if defined(__NR_quotactl)
3487 // Defined on x86_64 / i386 only
3488 LSS_INLINE _syscall4(int, quotactl, int, cmd, const char *, special,
3489 int, id, caddr_t, addr)
3490 #endif
3491 LSS_INLINE _syscall3(ssize_t, read, int, f,
3492 void *, b, size_t, c)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003493 #if defined(__NR_readlink)
3494 // readlink is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003495 LSS_INLINE _syscall3(int, readlink, const char*, p,
3496 char*, b, size_t, s)
3497 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003498 #if defined(__NR_readlinkat)
3499 LSS_INLINE _syscall4(int, readlinkat, int, d, const char *, p, char *, b,
3500 size_t, s)
3501 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003502 LSS_INLINE _syscall4(int, rt_sigaction, int, s,
3503 const struct kernel_sigaction*, a,
3504 struct kernel_sigaction*, o, size_t, c)
3505 LSS_INLINE _syscall2(int, rt_sigpending, struct kernel_sigset_t *, s,
3506 size_t, c)
3507 LSS_INLINE _syscall4(int, rt_sigprocmask, int, h,
3508 const struct kernel_sigset_t*, s,
3509 struct kernel_sigset_t*, o, size_t, c)
3510 LSS_INLINE _syscall2(int, rt_sigsuspend,
3511 const struct kernel_sigset_t*, s, size_t, c)
3512 LSS_INLINE _syscall3(int, sched_getaffinity,pid_t, p,
3513 unsigned int, l, unsigned long *, m)
3514 LSS_INLINE _syscall3(int, sched_setaffinity,pid_t, p,
3515 unsigned int, l, unsigned long *, m)
3516 LSS_INLINE _syscall0(int, sched_yield)
3517 LSS_INLINE _syscall1(long, set_tid_address, int *, t)
3518 LSS_INLINE _syscall1(int, setfsgid, gid_t, g)
3519 LSS_INLINE _syscall1(int, setfsuid, uid_t, u)
3520 LSS_INLINE _syscall1(int, setuid, uid_t, u)
3521 LSS_INLINE _syscall1(int, setgid, gid_t, g)
3522 LSS_INLINE _syscall2(int, setpgid, pid_t, p,
3523 pid_t, g)
3524 LSS_INLINE _syscall3(int, setpriority, int, a,
3525 int, b, int, p)
3526 LSS_INLINE _syscall3(int, setresgid, gid_t, r,
3527 gid_t, e, gid_t, s)
3528 LSS_INLINE _syscall3(int, setresuid, uid_t, r,
3529 uid_t, e, uid_t, s)
3530 LSS_INLINE _syscall2(int, setrlimit, int, r,
3531 const struct kernel_rlimit*, l)
3532 LSS_INLINE _syscall0(pid_t, setsid)
3533 LSS_INLINE _syscall2(int, sigaltstack, const stack_t*, s,
3534 const stack_t*, o)
3535 #if defined(__NR_sigreturn)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003536 LSS_INLINE _syscall1(int, sigreturn, unsigned long, u)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003537 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003538 #if defined(__NR_stat)
3539 // stat is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003540 LSS_INLINE _syscall2(int, stat, const char*, f,
3541 struct kernel_stat*, b)
3542 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003543 LSS_INLINE _syscall2(int, statfs, const char*, f,
3544 struct kernel_statfs*, b)
3545 LSS_INLINE _syscall3(int, tgkill, pid_t, p,
3546 pid_t, t, int, s)
3547 LSS_INLINE _syscall2(int, tkill, pid_t, p,
3548 int, s)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003549 #if defined(__NR_unlink)
3550 // unlink is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003551 LSS_INLINE _syscall1(int, unlink, const char*, f)
3552 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003553 LSS_INLINE _syscall3(ssize_t, write, int, f,
3554 const void *, b, size_t, c)
3555 LSS_INLINE _syscall3(ssize_t, writev, int, f,
3556 const struct kernel_iovec*, v, size_t, c)
3557 #if defined(__NR_getcpu)
3558 LSS_INLINE _syscall3(long, getcpu, unsigned *, cpu,
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00003559 unsigned *, node, void *, unused)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003560 #endif
3561 #if defined(__x86_64__) || \
3562 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32)
3563 LSS_INLINE _syscall3(int, recvmsg, int, s,
3564 struct kernel_msghdr*, m, int, f)
3565 LSS_INLINE _syscall3(int, sendmsg, int, s,
3566 const struct kernel_msghdr*, m, int, f)
3567 LSS_INLINE _syscall6(int, sendto, int, s,
3568 const void*, m, size_t, l,
3569 int, f,
3570 const struct kernel_sockaddr*, a, int, t)
3571 LSS_INLINE _syscall2(int, shutdown, int, s,
3572 int, h)
3573 LSS_INLINE _syscall3(int, socket, int, d,
3574 int, t, int, p)
3575 LSS_INLINE _syscall4(int, socketpair, int, d,
3576 int, t, int, p, int*, s)
3577 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003578 #if defined(__NR_fadvise64)
3579 #if defined(__x86_64__)
3580 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
3581 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset, loff_t len,
3582 int advice) {
3583 LSS_BODY(4, int, fadvise64, LSS_SYSCALL_ARG(fd), (uint64_t)(offset),
3584 (uint64_t)(len), LSS_SYSCALL_ARG(advice));
3585 }
3586 #else
3587 LSS_INLINE _syscall4(int, fadvise64,
3588 int, fd, loff_t, offset, loff_t, len, int, advice)
3589 #endif
3590 #elif defined(__i386__)
3591 #define __NR__fadvise64_64 __NR_fadvise64_64
3592 LSS_INLINE _syscall6(int, _fadvise64_64, int, fd,
3593 unsigned, offset_lo, unsigned, offset_hi,
3594 unsigned, len_lo, unsigned, len_hi,
3595 int, advice)
3596
3597 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
3598 loff_t len, int advice) {
3599 return LSS_NAME(_fadvise64_64)(fd,
3600 (unsigned)offset, (unsigned)(offset >>32),
3601 (unsigned)len, (unsigned)(len >> 32),
3602 advice);
3603 }
3604
3605 #elif defined(__s390__) && !defined(__s390x__)
3606 #define __NR__fadvise64_64 __NR_fadvise64_64
3607 struct kernel_fadvise64_64_args {
3608 int fd;
3609 long long offset;
3610 long long len;
3611 int advice;
3612 };
3613
3614 LSS_INLINE _syscall1(int, _fadvise64_64,
3615 struct kernel_fadvise64_64_args *args)
3616
3617 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
3618 loff_t len, int advice) {
3619 struct kernel_fadvise64_64_args args = { fd, offset, len, advice };
3620 return LSS_NAME(_fadvise64_64)(&args);
3621 }
3622 #endif
3623 #if defined(__NR_fallocate)
3624 #if defined(__x86_64__)
vapier@chromium.org2273e812013-04-01 17:52:44 +00003625 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
3626 LSS_INLINE int LSS_NAME(fallocate)(int f, int mode, loff_t offset,
3627 loff_t len) {
3628 LSS_BODY(4, int, fallocate, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(mode),
3629 (uint64_t)(offset), (uint64_t)(len));
3630 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04003631 #elif defined(__i386__) || (defined(__s390__) && !defined(__s390x__))
3632 #define __NR__fallocate __NR_fallocate
3633 LSS_INLINE _syscall6(int, _fallocate, int, fd,
3634 int, mode,
3635 unsigned, offset_lo, unsigned, offset_hi,
3636 unsigned, len_lo, unsigned, len_hi)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003637
Bryan Chan3f6478a2016-06-14 08:38:17 -04003638 LSS_INLINE int LSS_NAME(fallocate)(int fd, int mode,
3639 loff_t offset, loff_t len) {
3640 union { loff_t off; unsigned w[2]; } o = { offset }, l = { len };
3641 return LSS_NAME(_fallocate)(fd, mode, o.w[0], o.w[1], l.w[0], l.w[1]);
3642 }
3643 #else
3644 LSS_INLINE _syscall4(int, fallocate,
3645 int, f, int, mode, loff_t, offset, loff_t, len)
3646 #endif
3647 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003648 #if defined(__NR_newfstatat)
3649 LSS_INLINE _syscall4(int, newfstatat, int, d,
3650 const char *, p,
3651 struct kernel_stat*, b, int, f)
3652 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003653 #if defined(__x86_64__) || defined(__s390x__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003654 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
3655 gid_t *egid,
3656 gid_t *sgid) {
3657 return LSS_NAME(getresgid)(rgid, egid, sgid);
3658 }
3659
3660 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
3661 uid_t *euid,
3662 uid_t *suid) {
3663 return LSS_NAME(getresuid)(ruid, euid, suid);
3664 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003665
3666 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
3667 return LSS_NAME(setfsgid)(gid);
3668 }
3669
3670 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
3671 return LSS_NAME(setfsuid)(uid);
3672 }
3673
3674 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
3675 return LSS_NAME(setresgid)(rgid, egid, sgid);
3676 }
3677
3678 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
3679 return LSS_NAME(setresuid)(ruid, euid, suid);
3680 }
3681
3682 LSS_INLINE int LSS_NAME(sigaction)(int signum,
3683 const struct kernel_sigaction *act,
3684 struct kernel_sigaction *oldact) {
Bryan Chan3f6478a2016-06-14 08:38:17 -04003685 #if defined(__x86_64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003686 /* On x86_64, the kernel requires us to always set our own
3687 * SA_RESTORER in order to be able to return from a signal handler.
3688 * This function must have a "magic" signature that the "gdb"
3689 * (and maybe the kernel?) can recognize.
3690 */
3691 if (act != NULL && !(act->sa_flags & SA_RESTORER)) {
3692 struct kernel_sigaction a = *act;
3693 a.sa_flags |= SA_RESTORER;
3694 a.sa_restorer = LSS_NAME(restore_rt)();
3695 return LSS_NAME(rt_sigaction)(signum, &a, oldact,
3696 (KERNEL_NSIG+7)/8);
Bryan Chan3f6478a2016-06-14 08:38:17 -04003697 } else
3698 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003699 return LSS_NAME(rt_sigaction)(signum, act, oldact,
3700 (KERNEL_NSIG+7)/8);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003701 }
3702
3703 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
3704 return LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
3705 }
3706
3707 LSS_INLINE int LSS_NAME(sigprocmask)(int how,
3708 const struct kernel_sigset_t *set,
3709 struct kernel_sigset_t *oldset) {
3710 return LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
3711 }
3712
3713 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
3714 return LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
3715 }
3716 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003717 #if defined(__NR_wait4)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003718 LSS_INLINE _syscall4(pid_t, wait4, pid_t, p,
3719 int*, s, int, o,
3720 struct kernel_rusage*, r)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003721 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003722 #if defined(__NR_openat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003723 LSS_INLINE _syscall4(int, openat, int, d, const char *, p, int, f, int, m)
Bryan Chan3f6478a2016-06-14 08:38:17 -04003724 #endif
3725 #if defined(__NR_unlinkat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003726 LSS_INLINE _syscall3(int, unlinkat, int, d, const char *, p, int, f)
3727 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003728 #if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
3729 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003730 #define __NR__getresgid32 __NR_getresgid32
3731 #define __NR__getresuid32 __NR_getresuid32
3732 #define __NR__setfsgid32 __NR_setfsgid32
3733 #define __NR__setfsuid32 __NR_setfsuid32
3734 #define __NR__setresgid32 __NR_setresgid32
3735 #define __NR__setresuid32 __NR_setresuid32
3736#if defined(__ARM_EABI__)
3737 LSS_INLINE _syscall2(int, ugetrlimit, int, r,
3738 struct kernel_rlimit*, l)
3739#endif
3740 LSS_INLINE _syscall3(int, _getresgid32, gid_t *, r,
3741 gid_t *, e, gid_t *, s)
3742 LSS_INLINE _syscall3(int, _getresuid32, uid_t *, r,
3743 uid_t *, e, uid_t *, s)
3744 LSS_INLINE _syscall1(int, _setfsgid32, gid_t, f)
3745 LSS_INLINE _syscall1(int, _setfsuid32, uid_t, f)
3746 LSS_INLINE _syscall3(int, _setresgid32, gid_t, r,
3747 gid_t, e, gid_t, s)
3748 LSS_INLINE _syscall3(int, _setresuid32, uid_t, r,
3749 uid_t, e, uid_t, s)
3750
3751 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
3752 gid_t *egid,
3753 gid_t *sgid) {
3754 int rc;
3755 if ((rc = LSS_NAME(_getresgid32)(rgid, egid, sgid)) < 0 &&
3756 LSS_ERRNO == ENOSYS) {
3757 if ((rgid == NULL) || (egid == NULL) || (sgid == NULL)) {
3758 return EFAULT;
3759 }
3760 // Clear the high bits first, since getresgid only sets 16 bits
3761 *rgid = *egid = *sgid = 0;
3762 rc = LSS_NAME(getresgid)(rgid, egid, sgid);
3763 }
3764 return rc;
3765 }
3766
3767 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
3768 uid_t *euid,
3769 uid_t *suid) {
3770 int rc;
3771 if ((rc = LSS_NAME(_getresuid32)(ruid, euid, suid)) < 0 &&
3772 LSS_ERRNO == ENOSYS) {
3773 if ((ruid == NULL) || (euid == NULL) || (suid == NULL)) {
3774 return EFAULT;
3775 }
3776 // Clear the high bits first, since getresuid only sets 16 bits
3777 *ruid = *euid = *suid = 0;
3778 rc = LSS_NAME(getresuid)(ruid, euid, suid);
3779 }
3780 return rc;
3781 }
3782
3783 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
3784 int rc;
3785 if ((rc = LSS_NAME(_setfsgid32)(gid)) < 0 &&
3786 LSS_ERRNO == ENOSYS) {
3787 if ((unsigned int)gid & ~0xFFFFu) {
3788 rc = EINVAL;
3789 } else {
3790 rc = LSS_NAME(setfsgid)(gid);
3791 }
3792 }
3793 return rc;
3794 }
3795
3796 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
3797 int rc;
3798 if ((rc = LSS_NAME(_setfsuid32)(uid)) < 0 &&
3799 LSS_ERRNO == ENOSYS) {
3800 if ((unsigned int)uid & ~0xFFFFu) {
3801 rc = EINVAL;
3802 } else {
3803 rc = LSS_NAME(setfsuid)(uid);
3804 }
3805 }
3806 return rc;
3807 }
3808
3809 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
3810 int rc;
3811 if ((rc = LSS_NAME(_setresgid32)(rgid, egid, sgid)) < 0 &&
3812 LSS_ERRNO == ENOSYS) {
3813 if ((unsigned int)rgid & ~0xFFFFu ||
3814 (unsigned int)egid & ~0xFFFFu ||
3815 (unsigned int)sgid & ~0xFFFFu) {
3816 rc = EINVAL;
3817 } else {
3818 rc = LSS_NAME(setresgid)(rgid, egid, sgid);
3819 }
3820 }
3821 return rc;
3822 }
3823
3824 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
3825 int rc;
3826 if ((rc = LSS_NAME(_setresuid32)(ruid, euid, suid)) < 0 &&
3827 LSS_ERRNO == ENOSYS) {
3828 if ((unsigned int)ruid & ~0xFFFFu ||
3829 (unsigned int)euid & ~0xFFFFu ||
3830 (unsigned int)suid & ~0xFFFFu) {
3831 rc = EINVAL;
3832 } else {
3833 rc = LSS_NAME(setresuid)(ruid, euid, suid);
3834 }
3835 }
3836 return rc;
3837 }
3838 #endif
3839 LSS_INLINE int LSS_NAME(sigemptyset)(struct kernel_sigset_t *set) {
3840 memset(&set->sig, 0, sizeof(set->sig));
3841 return 0;
3842 }
3843
3844 LSS_INLINE int LSS_NAME(sigfillset)(struct kernel_sigset_t *set) {
3845 memset(&set->sig, -1, sizeof(set->sig));
3846 return 0;
3847 }
3848
3849 LSS_INLINE int LSS_NAME(sigaddset)(struct kernel_sigset_t *set,
3850 int signum) {
3851 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
3852 LSS_ERRNO = EINVAL;
3853 return -1;
3854 } else {
3855 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
3856 |= 1UL << ((signum - 1) % (8*sizeof(set->sig[0])));
3857 return 0;
3858 }
3859 }
3860
3861 LSS_INLINE int LSS_NAME(sigdelset)(struct kernel_sigset_t *set,
3862 int signum) {
3863 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
3864 LSS_ERRNO = EINVAL;
3865 return -1;
3866 } else {
3867 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
3868 &= ~(1UL << ((signum - 1) % (8*sizeof(set->sig[0]))));
3869 return 0;
3870 }
3871 }
mcgrathr@google.coma7999932011-11-21 22:26:20 +00003872
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003873 LSS_INLINE int LSS_NAME(sigismember)(struct kernel_sigset_t *set,
3874 int signum) {
3875 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
3876 LSS_ERRNO = EINVAL;
3877 return -1;
3878 } else {
3879 return !!(set->sig[(signum - 1)/(8*sizeof(set->sig[0]))] &
3880 (1UL << ((signum - 1) % (8*sizeof(set->sig[0])))));
3881 }
3882 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04003883 #if defined(__i386__) || \
3884 defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
3885 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
3886 defined(__PPC__) || \
3887 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003888 #define __NR__sigaction __NR_sigaction
3889 #define __NR__sigpending __NR_sigpending
3890 #define __NR__sigprocmask __NR_sigprocmask
3891 #define __NR__sigsuspend __NR_sigsuspend
3892 #define __NR__socketcall __NR_socketcall
3893 LSS_INLINE _syscall2(int, fstat64, int, f,
3894 struct kernel_stat64 *, b)
zodiac@gmail.com4f470182010-10-13 03:47:54 +00003895 LSS_INLINE _syscall5(int, _llseek, uint, fd,
3896 unsigned long, hi, unsigned long, lo,
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003897 loff_t *, res, uint, wh)
Bryan Chan3f6478a2016-06-14 08:38:17 -04003898#if defined(__s390__) && !defined(__s390x__)
3899 /* On s390, mmap2() arguments are passed in memory. */
3900 LSS_INLINE void* LSS_NAME(_mmap2)(void *s, size_t l, int p, int f, int d,
3901 off_t o) {
3902 unsigned long buf[6] = { (unsigned long) s, (unsigned long) l,
3903 (unsigned long) p, (unsigned long) f,
3904 (unsigned long) d, (unsigned long) o };
3905 LSS_REG(2, buf);
3906 LSS_BODY(void*, mmap2, "0"(__r2));
3907 }
3908#else
3909 #define __NR__mmap2 __NR_mmap2
3910 LSS_INLINE _syscall6(void*, _mmap2, void*, s,
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003911 size_t, l, int, p,
3912 int, f, int, d,
Bryan Chan3f6478a2016-06-14 08:38:17 -04003913 off_t, o)
3914#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003915 LSS_INLINE _syscall3(int, _sigaction, int, s,
3916 const struct kernel_old_sigaction*, a,
3917 struct kernel_old_sigaction*, o)
3918 LSS_INLINE _syscall1(int, _sigpending, unsigned long*, s)
3919 LSS_INLINE _syscall3(int, _sigprocmask, int, h,
3920 const unsigned long*, s,
3921 unsigned long*, o)
3922 #ifdef __PPC__
3923 LSS_INLINE _syscall1(int, _sigsuspend, unsigned long, s)
3924 #else
3925 LSS_INLINE _syscall3(int, _sigsuspend, const void*, a,
3926 int, b,
3927 unsigned long, s)
3928 #endif
3929 LSS_INLINE _syscall2(int, stat64, const char *, p,
3930 struct kernel_stat64 *, b)
3931
3932 LSS_INLINE int LSS_NAME(sigaction)(int signum,
3933 const struct kernel_sigaction *act,
3934 struct kernel_sigaction *oldact) {
3935 int old_errno = LSS_ERRNO;
3936 int rc;
3937 struct kernel_sigaction a;
3938 if (act != NULL) {
3939 a = *act;
3940 #ifdef __i386__
3941 /* On i386, the kernel requires us to always set our own
3942 * SA_RESTORER when using realtime signals. Otherwise, it does not
3943 * know how to return from a signal handler. This function must have
3944 * a "magic" signature that the "gdb" (and maybe the kernel?) can
3945 * recognize.
3946 * Apparently, a SA_RESTORER is implicitly set by the kernel, when
3947 * using non-realtime signals.
3948 *
3949 * TODO: Test whether ARM needs a restorer
3950 */
3951 if (!(a.sa_flags & SA_RESTORER)) {
3952 a.sa_flags |= SA_RESTORER;
3953 a.sa_restorer = (a.sa_flags & SA_SIGINFO)
3954 ? LSS_NAME(restore_rt)() : LSS_NAME(restore)();
3955 }
3956 #endif
3957 }
3958 rc = LSS_NAME(rt_sigaction)(signum, act ? &a : act, oldact,
3959 (KERNEL_NSIG+7)/8);
3960 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3961 struct kernel_old_sigaction oa, ooa, *ptr_a = &oa, *ptr_oa = &ooa;
3962 if (!act) {
3963 ptr_a = NULL;
3964 } else {
3965 oa.sa_handler_ = act->sa_handler_;
3966 memcpy(&oa.sa_mask, &act->sa_mask, sizeof(oa.sa_mask));
3967 #ifndef __mips__
3968 oa.sa_restorer = act->sa_restorer;
3969 #endif
3970 oa.sa_flags = act->sa_flags;
3971 }
3972 if (!oldact) {
3973 ptr_oa = NULL;
3974 }
3975 LSS_ERRNO = old_errno;
3976 rc = LSS_NAME(_sigaction)(signum, ptr_a, ptr_oa);
3977 if (rc == 0 && oldact) {
3978 if (act) {
3979 memcpy(oldact, act, sizeof(*act));
3980 } else {
3981 memset(oldact, 0, sizeof(*oldact));
3982 }
3983 oldact->sa_handler_ = ptr_oa->sa_handler_;
3984 oldact->sa_flags = ptr_oa->sa_flags;
3985 memcpy(&oldact->sa_mask, &ptr_oa->sa_mask, sizeof(ptr_oa->sa_mask));
3986 #ifndef __mips__
3987 oldact->sa_restorer = ptr_oa->sa_restorer;
3988 #endif
3989 }
3990 }
3991 return rc;
3992 }
3993
3994 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
3995 int old_errno = LSS_ERRNO;
3996 int rc = LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
3997 if (rc < 0 && LSS_ERRNO == ENOSYS) {
3998 LSS_ERRNO = old_errno;
3999 LSS_NAME(sigemptyset)(set);
4000 rc = LSS_NAME(_sigpending)(&set->sig[0]);
4001 }
4002 return rc;
4003 }
4004
4005 LSS_INLINE int LSS_NAME(sigprocmask)(int how,
4006 const struct kernel_sigset_t *set,
4007 struct kernel_sigset_t *oldset) {
4008 int olderrno = LSS_ERRNO;
4009 int rc = LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
4010 if (rc < 0 && LSS_ERRNO == ENOSYS) {
4011 LSS_ERRNO = olderrno;
4012 if (oldset) {
4013 LSS_NAME(sigemptyset)(oldset);
4014 }
4015 rc = LSS_NAME(_sigprocmask)(how,
4016 set ? &set->sig[0] : NULL,
4017 oldset ? &oldset->sig[0] : NULL);
4018 }
4019 return rc;
4020 }
4021
4022 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
4023 int olderrno = LSS_ERRNO;
4024 int rc = LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
4025 if (rc < 0 && LSS_ERRNO == ENOSYS) {
4026 LSS_ERRNO = olderrno;
4027 rc = LSS_NAME(_sigsuspend)(
4028 #ifndef __PPC__
4029 set, 0,
4030 #endif
4031 set->sig[0]);
4032 }
4033 return rc;
4034 }
4035 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004036 #if defined(__i386__) || \
4037 defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
4038 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
4039 defined(__PPC__) || \
4040 (defined(__s390__) && !defined(__s390x__))
4041 /* On these architectures, implement mmap() with mmap2(). */
4042 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4043 int64_t o) {
4044 if (o % 4096) {
4045 LSS_ERRNO = EINVAL;
4046 return (void *) -1;
4047 }
4048 return LSS_NAME(_mmap2)(s, l, p, f, d, (o / 4096));
4049 }
4050 #elif defined(__s390x__)
4051 /* On s390x, mmap() arguments are passed in memory. */
4052 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4053 int64_t o) {
4054 unsigned long buf[6] = { (unsigned long) s, (unsigned long) l,
4055 (unsigned long) p, (unsigned long) f,
4056 (unsigned long) d, (unsigned long) o };
4057 LSS_REG(2, buf);
4058 LSS_BODY(void*, mmap, "0"(__r2));
4059 }
4060 #elif defined(__x86_64__)
4061 /* Need to make sure __off64_t isn't truncated to 32-bits under x32. */
4062 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4063 int64_t o) {
4064 LSS_BODY(6, void*, mmap, LSS_SYSCALL_ARG(s), LSS_SYSCALL_ARG(l),
4065 LSS_SYSCALL_ARG(p), LSS_SYSCALL_ARG(f),
4066 LSS_SYSCALL_ARG(d), (uint64_t)(o));
4067 }
4068 #else
4069 /* Remaining 64-bit architectures. */
4070 LSS_INLINE _syscall6(void*, mmap, void*, addr, size_t, length, int, prot,
4071 int, flags, int, fd, int64_t, offset)
4072 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004073 #if defined(__PPC__)
4074 #undef LSS_SC_LOADARGS_0
4075 #define LSS_SC_LOADARGS_0(dummy...)
4076 #undef LSS_SC_LOADARGS_1
4077 #define LSS_SC_LOADARGS_1(arg1) \
4078 __sc_4 = (unsigned long) (arg1)
4079 #undef LSS_SC_LOADARGS_2
4080 #define LSS_SC_LOADARGS_2(arg1, arg2) \
4081 LSS_SC_LOADARGS_1(arg1); \
4082 __sc_5 = (unsigned long) (arg2)
4083 #undef LSS_SC_LOADARGS_3
4084 #define LSS_SC_LOADARGS_3(arg1, arg2, arg3) \
4085 LSS_SC_LOADARGS_2(arg1, arg2); \
4086 __sc_6 = (unsigned long) (arg3)
4087 #undef LSS_SC_LOADARGS_4
4088 #define LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4) \
4089 LSS_SC_LOADARGS_3(arg1, arg2, arg3); \
4090 __sc_7 = (unsigned long) (arg4)
4091 #undef LSS_SC_LOADARGS_5
4092 #define LSS_SC_LOADARGS_5(arg1, arg2, arg3, arg4, arg5) \
4093 LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4); \
4094 __sc_8 = (unsigned long) (arg5)
4095 #undef LSS_SC_BODY
4096 #define LSS_SC_BODY(nr, type, opt, args...) \
4097 long __sc_ret, __sc_err; \
4098 { \
4099 register unsigned long __sc_0 __asm__ ("r0") = __NR_socketcall; \
4100 register unsigned long __sc_3 __asm__ ("r3") = opt; \
4101 register unsigned long __sc_4 __asm__ ("r4"); \
4102 register unsigned long __sc_5 __asm__ ("r5"); \
4103 register unsigned long __sc_6 __asm__ ("r6"); \
4104 register unsigned long __sc_7 __asm__ ("r7"); \
4105 register unsigned long __sc_8 __asm__ ("r8"); \
4106 LSS_SC_LOADARGS_##nr(args); \
4107 __asm__ __volatile__ \
4108 ("stwu 1, -48(1)\n\t" \
4109 "stw 4, 20(1)\n\t" \
4110 "stw 5, 24(1)\n\t" \
4111 "stw 6, 28(1)\n\t" \
4112 "stw 7, 32(1)\n\t" \
4113 "stw 8, 36(1)\n\t" \
4114 "addi 4, 1, 20\n\t" \
4115 "sc\n\t" \
4116 "mfcr %0" \
4117 : "=&r" (__sc_0), \
4118 "=&r" (__sc_3), "=&r" (__sc_4), \
4119 "=&r" (__sc_5), "=&r" (__sc_6), \
4120 "=&r" (__sc_7), "=&r" (__sc_8) \
4121 : LSS_ASMINPUT_##nr \
4122 : "cr0", "ctr", "memory"); \
4123 __sc_ret = __sc_3; \
4124 __sc_err = __sc_0; \
4125 } \
4126 LSS_RETURN(type, __sc_ret, __sc_err)
4127
4128 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
4129 int flags){
4130 LSS_SC_BODY(3, ssize_t, 17, s, msg, flags);
4131 }
4132
4133 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
4134 const struct kernel_msghdr *msg,
4135 int flags) {
4136 LSS_SC_BODY(3, ssize_t, 16, s, msg, flags);
4137 }
4138
4139 // TODO(csilvers): why is this ifdef'ed out?
4140#if 0
4141 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
4142 int flags,
4143 const struct kernel_sockaddr *to,
4144 unsigned int tolen) {
4145 LSS_BODY(6, ssize_t, 11, s, buf, len, flags, to, tolen);
4146 }
4147#endif
4148
4149 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
4150 LSS_SC_BODY(2, int, 13, s, how);
4151 }
4152
4153 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
4154 LSS_SC_BODY(3, int, 1, domain, type, protocol);
4155 }
4156
4157 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
4158 int sv[2]) {
4159 LSS_SC_BODY(4, int, 8, d, type, protocol, sv);
4160 }
4161 #endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004162 #if defined(__ARM_EABI__) || defined (__aarch64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004163 LSS_INLINE _syscall3(ssize_t, recvmsg, int, s, struct kernel_msghdr*, msg,
4164 int, flags)
4165 LSS_INLINE _syscall3(ssize_t, sendmsg, int, s, const struct kernel_msghdr*,
4166 msg, int, flags)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004167 LSS_INLINE _syscall6(ssize_t, sendto, int, s, const void*, buf, size_t,len,
4168 int, flags, const struct kernel_sockaddr*, to,
4169 unsigned int, tolen)
4170 LSS_INLINE _syscall2(int, shutdown, int, s, int, how)
4171 LSS_INLINE _syscall3(int, socket, int, domain, int, type, int, protocol)
4172 LSS_INLINE _syscall4(int, socketpair, int, d, int, type, int, protocol,
4173 int*, sv)
4174 #endif
4175 #if defined(__i386__) || defined(__ARM_ARCH_3__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -04004176 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
4177 defined(__s390__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004178 #define __NR__socketcall __NR_socketcall
4179 LSS_INLINE _syscall2(int, _socketcall, int, c,
4180 va_list, a)
4181 LSS_INLINE int LSS_NAME(socketcall)(int op, ...) {
4182 int rc;
4183 va_list ap;
4184 va_start(ap, op);
4185 rc = LSS_NAME(_socketcall)(op, ap);
4186 va_end(ap);
4187 return rc;
4188 }
4189
4190 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
4191 int flags){
4192 return (ssize_t)LSS_NAME(socketcall)(17, s, msg, flags);
4193 }
4194
4195 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
4196 const struct kernel_msghdr *msg,
4197 int flags) {
4198 return (ssize_t)LSS_NAME(socketcall)(16, s, msg, flags);
4199 }
4200
4201 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
4202 int flags,
4203 const struct kernel_sockaddr *to,
4204 unsigned int tolen) {
4205 return (ssize_t)LSS_NAME(socketcall)(11, s, buf, len, flags, to, tolen);
4206 }
4207
4208 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
4209 return LSS_NAME(socketcall)(13, s, how);
4210 }
4211
4212 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
4213 return LSS_NAME(socketcall)(1, domain, type, protocol);
4214 }
4215
4216 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
4217 int sv[2]) {
4218 return LSS_NAME(socketcall)(8, d, type, protocol, sv);
4219 }
4220 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004221 #if defined(__NR_fstatat64)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004222 LSS_INLINE _syscall4(int, fstatat64, int, d,
4223 const char *, p,
4224 struct kernel_stat64 *, b, int, f)
4225 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004226 #if defined(__NR_waitpid)
4227 // waitpid is polyfilled below when not available.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004228 LSS_INLINE _syscall3(pid_t, waitpid, pid_t, p,
4229 int*, s, int, o)
4230 #endif
4231 #if defined(__mips__)
4232 /* sys_pipe() on MIPS has non-standard calling conventions, as it returns
4233 * both file handles through CPU registers.
4234 */
4235 LSS_INLINE int LSS_NAME(pipe)(int *p) {
4236 register unsigned long __v0 __asm__("$2") = __NR_pipe;
4237 register unsigned long __v1 __asm__("$3");
4238 register unsigned long __r7 __asm__("$7");
4239 __asm__ __volatile__ ("syscall\n"
vapier@chromium.orgda4a4892015-01-22 16:46:39 +00004240 : "=r"(__v0), "=r"(__v1), "=r" (__r7)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004241 : "0"(__v0)
4242 : "$8", "$9", "$10", "$11", "$12",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00004243 "$13", "$14", "$15", "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004244 if (__r7) {
zodiac@gmail.coma6591482012-04-13 01:29:30 +00004245 unsigned long __errnovalue = __v0;
4246 LSS_ERRNO = __errnovalue;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004247 return -1;
4248 } else {
4249 p[0] = __v0;
4250 p[1] = __v1;
4251 return 0;
4252 }
4253 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004254 #elif defined(__NR_pipe)
4255 // pipe is polyfilled below when not available.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004256 LSS_INLINE _syscall1(int, pipe, int *, p)
4257 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004258 #if defined(__NR_pipe2)
4259 LSS_INLINE _syscall2(int, pipe2, int *, pipefd, int, flags)
4260 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004261 /* TODO(csilvers): see if ppc can/should support this as well */
4262 #if defined(__i386__) || defined(__ARM_ARCH_3__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -04004263 defined(__ARM_EABI__) || \
4264 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64) || \
4265 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004266 #define __NR__statfs64 __NR_statfs64
4267 #define __NR__fstatfs64 __NR_fstatfs64
4268 LSS_INLINE _syscall3(int, _statfs64, const char*, p,
4269 size_t, s,struct kernel_statfs64*, b)
4270 LSS_INLINE _syscall3(int, _fstatfs64, int, f,
4271 size_t, s,struct kernel_statfs64*, b)
4272 LSS_INLINE int LSS_NAME(statfs64)(const char *p,
4273 struct kernel_statfs64 *b) {
4274 return LSS_NAME(_statfs64)(p, sizeof(*b), b);
4275 }
4276 LSS_INLINE int LSS_NAME(fstatfs64)(int f,struct kernel_statfs64 *b) {
4277 return LSS_NAME(_fstatfs64)(f, sizeof(*b), b);
4278 }
4279 #endif
4280
4281 LSS_INLINE int LSS_NAME(execv)(const char *path, const char *const argv[]) {
4282 extern char **environ;
4283 return LSS_NAME(execve)(path, argv, (const char *const *)environ);
4284 }
4285
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00004286 LSS_INLINE pid_t LSS_NAME(gettid)(void) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004287 pid_t tid = LSS_NAME(_gettid)();
4288 if (tid != -1) {
4289 return tid;
4290 }
4291 return LSS_NAME(getpid)();
4292 }
4293
4294 LSS_INLINE void *LSS_NAME(mremap)(void *old_address, size_t old_size,
4295 size_t new_size, int flags, ...) {
4296 va_list ap;
4297 void *new_address, *rc;
4298 va_start(ap, flags);
4299 new_address = va_arg(ap, void *);
4300 rc = LSS_NAME(_mremap)(old_address, old_size, new_size,
4301 flags, new_address);
4302 va_end(ap);
4303 return rc;
4304 }
4305
4306 LSS_INLINE int LSS_NAME(ptrace_detach)(pid_t pid) {
4307 /* PTRACE_DETACH can sometimes forget to wake up the tracee and it
4308 * then sends job control signals to the real parent, rather than to
4309 * the tracer. We reduce the risk of this happening by starting a
4310 * whole new time slice, and then quickly sending a SIGCONT signal
4311 * right after detaching from the tracee.
4312 *
4313 * We use tkill to ensure that we only issue a wakeup for the thread being
4314 * detached. Large multi threaded apps can take a long time in the kernel
4315 * processing SIGCONT.
4316 */
4317 int rc, err;
4318 LSS_NAME(sched_yield)();
4319 rc = LSS_NAME(ptrace)(PTRACE_DETACH, pid, (void *)0, (void *)0);
4320 err = LSS_ERRNO;
4321 LSS_NAME(tkill)(pid, SIGCONT);
4322 /* Old systems don't have tkill */
4323 if (LSS_ERRNO == ENOSYS)
4324 LSS_NAME(kill)(pid, SIGCONT);
4325 LSS_ERRNO = err;
4326 return rc;
4327 }
4328
4329 LSS_INLINE int LSS_NAME(raise)(int sig) {
4330 return LSS_NAME(kill)(LSS_NAME(getpid)(), sig);
4331 }
4332
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00004333 LSS_INLINE int LSS_NAME(setpgrp)(void) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004334 return LSS_NAME(setpgid)(0, 0);
4335 }
4336
vapier@chromium.org2273e812013-04-01 17:52:44 +00004337 #if defined(__x86_64__)
4338 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
4339 LSS_INLINE ssize_t LSS_NAME(pread64)(int f, void *b, size_t c, loff_t o) {
4340 LSS_BODY(4, ssize_t, pread64, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(b),
4341 LSS_SYSCALL_ARG(c), (uint64_t)(o));
4342 }
4343
4344 LSS_INLINE ssize_t LSS_NAME(pwrite64)(int f, const void *b, size_t c,
4345 loff_t o) {
4346 LSS_BODY(4, ssize_t, pwrite64, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(b),
4347 LSS_SYSCALL_ARG(c), (uint64_t)(o));
4348 }
4349
4350 LSS_INLINE int LSS_NAME(readahead)(int f, loff_t o, unsigned c) {
4351 LSS_BODY(3, int, readahead, LSS_SYSCALL_ARG(f), (uint64_t)(o),
4352 LSS_SYSCALL_ARG(c));
4353 }
4354 #elif defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI64
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004355 LSS_INLINE _syscall4(ssize_t, pread64, int, f,
4356 void *, b, size_t, c,
4357 loff_t, o)
4358 LSS_INLINE _syscall4(ssize_t, pwrite64, int, f,
4359 const void *, b, size_t, c,
4360 loff_t, o)
4361 LSS_INLINE _syscall3(int, readahead, int, f,
4362 loff_t, o, unsigned, c)
4363 #else
4364 #define __NR__pread64 __NR_pread64
4365 #define __NR__pwrite64 __NR_pwrite64
4366 #define __NR__readahead __NR_readahead
mseaborn@chromium.org2c73abf2012-09-15 03:46:48 +00004367 #if defined(__ARM_EABI__) || defined(__mips__)
4368 /* On ARM and MIPS, a 64-bit parameter has to be in an even-odd register
4369 * pair. Hence these calls ignore their fourth argument (r3) so that their
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004370 * fifth and sixth make such a pair (r4,r5).
4371 */
4372 #define LSS_LLARG_PAD 0,
4373 LSS_INLINE _syscall6(ssize_t, _pread64, int, f,
4374 void *, b, size_t, c,
4375 unsigned, skip, unsigned, o1, unsigned, o2)
4376 LSS_INLINE _syscall6(ssize_t, _pwrite64, int, f,
4377 const void *, b, size_t, c,
4378 unsigned, skip, unsigned, o1, unsigned, o2)
4379 LSS_INLINE _syscall5(int, _readahead, int, f,
4380 unsigned, skip,
4381 unsigned, o1, unsigned, o2, size_t, c)
4382 #else
4383 #define LSS_LLARG_PAD
4384 LSS_INLINE _syscall5(ssize_t, _pread64, int, f,
4385 void *, b, size_t, c, unsigned, o1,
4386 unsigned, o2)
4387 LSS_INLINE _syscall5(ssize_t, _pwrite64, int, f,
4388 const void *, b, size_t, c, unsigned, o1,
4389 long, o2)
4390 LSS_INLINE _syscall4(int, _readahead, int, f,
4391 unsigned, o1, unsigned, o2, size_t, c)
4392 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004393 /* We force 64bit-wide parameters onto the stack, then access each
4394 * 32-bit component individually. This guarantees that we build the
4395 * correct parameters independent of the native byte-order of the
4396 * underlying architecture.
4397 */
4398 LSS_INLINE ssize_t LSS_NAME(pread64)(int fd, void *buf, size_t count,
4399 loff_t off) {
4400 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004401 return LSS_NAME(_pread64)(fd, buf, count,
4402 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004403 }
4404 LSS_INLINE ssize_t LSS_NAME(pwrite64)(int fd, const void *buf,
4405 size_t count, loff_t off) {
4406 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004407 return LSS_NAME(_pwrite64)(fd, buf, count,
4408 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004409 }
4410 LSS_INLINE int LSS_NAME(readahead)(int fd, loff_t off, int len) {
4411 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004412 return LSS_NAME(_readahead)(fd, LSS_LLARG_PAD o.arg[0], o.arg[1], len);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004413 }
4414 #endif
4415#endif
4416
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004417/*
4418 * Polyfills for deprecated syscalls.
4419 */
4420
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004421#if !defined(__NR_dup2)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004422 LSS_INLINE int LSS_NAME(dup2)(int s, int d) {
4423 return LSS_NAME(dup3)(s, d, 0);
4424 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004425#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004426
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004427#if !defined(__NR_open)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004428 LSS_INLINE int LSS_NAME(open)(const char *pathname, int flags, int mode) {
4429 return LSS_NAME(openat)(AT_FDCWD, pathname, flags, mode);
4430 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004431#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004432
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004433#if !defined(__NR_unlink)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004434 LSS_INLINE int LSS_NAME(unlink)(const char *pathname) {
4435 return LSS_NAME(unlinkat)(AT_FDCWD, pathname, 0);
4436 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004437#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004438
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004439#if !defined(__NR_readlink)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004440 LSS_INLINE int LSS_NAME(readlink)(const char *pathname, char *buffer,
4441 size_t size) {
4442 return LSS_NAME(readlinkat)(AT_FDCWD, pathname, buffer, size);
4443 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004444#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004445
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004446#if !defined(__NR_pipe)
Mike Frysinger4ce4c482018-01-03 18:31:42 -05004447 LSS_INLINE int LSS_NAME(pipe)(int *pipefd) {
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004448 return LSS_NAME(pipe2)(pipefd, 0);
4449 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004450#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004451
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004452#if !defined(__NR_poll)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004453 LSS_INLINE int LSS_NAME(poll)(struct kernel_pollfd *fds, unsigned int nfds,
4454 int timeout) {
4455 struct kernel_timespec timeout_ts;
4456 struct kernel_timespec *timeout_ts_p = NULL;
4457
4458 if (timeout >= 0) {
4459 timeout_ts.tv_sec = timeout / 1000;
4460 timeout_ts.tv_nsec = (timeout % 1000) * 1000000;
4461 timeout_ts_p = &timeout_ts;
4462 }
4463 return LSS_NAME(ppoll)(fds, nfds, timeout_ts_p, NULL, 0);
4464 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004465#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004466
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004467#if !defined(__NR_stat)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004468 LSS_INLINE int LSS_NAME(stat)(const char *pathname,
4469 struct kernel_stat *buf) {
4470 return LSS_NAME(newfstatat)(AT_FDCWD, pathname, buf, 0);
4471 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004472#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004473
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004474#if !defined(__NR_waitpid)
4475 LSS_INLINE pid_t LSS_NAME(waitpid)(pid_t pid, int *status, int options) {
4476 return LSS_NAME(wait4)(pid, status, options, 0);
4477 }
4478#endif
4479
4480#if !defined(__NR_fork)
4481// TODO: define this in an arch-independant way instead of inlining the clone
4482// syscall body.
4483
4484# if defined(__aarch64__)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004485 LSS_INLINE pid_t LSS_NAME(fork)(void) {
4486 // No fork syscall on aarch64 - implement by means of the clone syscall.
4487 // Note that this does not reset glibc's cached view of the PID/TID, so
4488 // some glibc interfaces might go wrong in the forked subprocess.
4489 int flags = SIGCHLD;
4490 void *child_stack = NULL;
4491 void *parent_tidptr = NULL;
4492 void *newtls = NULL;
4493 void *child_tidptr = NULL;
4494
4495 LSS_REG(0, flags);
4496 LSS_REG(1, child_stack);
4497 LSS_REG(2, parent_tidptr);
4498 LSS_REG(3, newtls);
4499 LSS_REG(4, child_tidptr);
4500 LSS_BODY(pid_t, clone, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3),
4501 "r"(__r4));
4502 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004503# elif defined(__x86_64__)
4504 LSS_INLINE pid_t LSS_NAME(fork)(void) {
4505 // Android disallows the fork syscall on x86_64 - implement by means of the
4506 // clone syscall as above for aarch64.
4507 int flags = SIGCHLD;
4508 void *child_stack = NULL;
4509 void *parent_tidptr = NULL;
4510 void *newtls = NULL;
4511 void *child_tidptr = NULL;
4512
4513 LSS_BODY(5, pid_t, clone, LSS_SYSCALL_ARG(flags),
4514 LSS_SYSCALL_ARG(child_stack), LSS_SYSCALL_ARG(parent_tidptr),
4515 LSS_SYSCALL_ARG(newtls), LSS_SYSCALL_ARG(child_tidptr));
4516 }
4517# else
4518# error missing fork polyfill for this architecture
4519# endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004520#endif
4521
mseaborn@chromium.orgca749372012-09-05 18:26:20 +00004522#ifdef __ANDROID__
4523 /* These restore the original values of these macros saved by the
4524 * corresponding #pragma push_macro near the top of this file. */
4525# pragma pop_macro("stat64")
4526# pragma pop_macro("fstat64")
4527# pragma pop_macro("lstat64")
4528#endif
4529
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004530#if defined(__cplusplus) && !defined(SYS_CPLUSPLUS)
4531}
4532#endif
4533
4534#endif
4535#endif