blob: 4421e485d1b4e6ee63539f04c35c543919aeec22 [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__) || \
Konstantin Ivlev8007b272021-01-27 18:27:42 +030091 defined(__aarch64__) || defined(__s390__)) || defined(__e2k__) \
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
Michael Forneyf70e2f12020-01-22 19:19:38 -0800133/* Some libcs, for example Android NDK and musl, #define these
134 * macros as aliases to their non-64 counterparts. To avoid naming
135 * conflict, remove them.
136 *
137 * These are restored by the corresponding #pragma pop_macro near
138 * the end of this file.
139 */
140#pragma push_macro("stat64")
141#pragma push_macro("fstat64")
142#pragma push_macro("lstat64")
143#pragma push_macro("pread64")
144#pragma push_macro("pwrite64")
Michael Forneyfd00dbb2020-03-10 14:12:52 -0700145#pragma push_macro("getdents64")
Michael Forneyf70e2f12020-01-22 19:19:38 -0800146#undef stat64
147#undef fstat64
148#undef lstat64
149#undef pread64
150#undef pwrite64
Michael Forneyfd00dbb2020-03-10 14:12:52 -0700151#undef getdents64
mseaborn@chromium.orgca749372012-09-05 18:26:20 +0000152
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -0400153#if defined(__ANDROID__) && defined(__x86_64__)
154// A number of x86_64 syscalls are blocked by seccomp on recent Android;
155// undefine them so that modern alternatives will be used instead where
156// possible.
157// The alternative syscalls have been sanity checked against linux-3.4+;
158// older versions might not work.
159# undef __NR_getdents
160# undef __NR_dup2
161# undef __NR_fork
162# undef __NR_getpgrp
163# undef __NR_open
164# undef __NR_poll
165# undef __NR_readlink
166# undef __NR_stat
167# undef __NR_unlink
168# undef __NR_pipe
169#endif
170
171#if defined(__ANDROID__)
172// waitpid is blocked by seccomp on all architectures on recent Android.
173# undef __NR_waitpid
174#endif
175
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000176/* As glibc often provides subtly incompatible data structures (and implicit
177 * wrapper functions that convert them), we provide our own kernel data
178 * structures for use by the system calls.
179 * These structures have been developed by using Linux 2.6.23 headers for
180 * reference. Note though, we do not care about exact API compatibility
181 * with the kernel, and in fact the kernel often does not have a single
182 * API that works across architectures. Instead, we try to mimic the glibc
183 * API where reasonable, and only guarantee ABI compatibility with the
184 * kernel headers.
185 * Most notably, here are a few changes that were made to the structures
186 * defined by kernel headers:
187 *
188 * - we only define structures, but not symbolic names for kernel data
189 * types. For the latter, we directly use the native C datatype
190 * (i.e. "unsigned" instead of "mode_t").
191 * - in a few cases, it is possible to define identical structures for
192 * both 32bit (e.g. i386) and 64bit (e.g. x86-64) platforms by
193 * standardizing on the 64bit version of the data types. In particular,
194 * this means that we use "unsigned" where the 32bit headers say
195 * "unsigned long".
196 * - overall, we try to minimize the number of cases where we need to
197 * conditionally define different structures.
198 * - the "struct kernel_sigaction" class of structures have been
199 * modified to more closely mimic glibc's API by introducing an
200 * anonymous union for the function pointer.
201 * - a small number of field names had to have an underscore appended to
202 * them, because glibc defines a global macro by the same name.
203 */
204
205/* include/linux/dirent.h */
206struct kernel_dirent64 {
207 unsigned long long d_ino;
208 long long d_off;
209 unsigned short d_reclen;
210 unsigned char d_type;
211 char d_name[256];
212};
213
214/* include/linux/dirent.h */
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -0400215#if !defined(__NR_getdents)
216// when getdents is not available, getdents64 is used for both.
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000217#define kernel_dirent kernel_dirent64
218#else
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000219struct kernel_dirent {
220 long d_ino;
221 long d_off;
222 unsigned short d_reclen;
223 char d_name[256];
224};
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000225#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000226
227/* include/linux/uio.h */
228struct kernel_iovec {
229 void *iov_base;
230 unsigned long iov_len;
231};
232
233/* include/linux/socket.h */
234struct kernel_msghdr {
235 void *msg_name;
236 int msg_namelen;
237 struct kernel_iovec*msg_iov;
238 unsigned long msg_iovlen;
239 void *msg_control;
240 unsigned long msg_controllen;
241 unsigned msg_flags;
242};
243
244/* include/asm-generic/poll.h */
245struct kernel_pollfd {
246 int fd;
247 short events;
248 short revents;
249};
250
251/* include/linux/resource.h */
252struct kernel_rlimit {
253 unsigned long rlim_cur;
254 unsigned long rlim_max;
255};
256
257/* include/linux/time.h */
258struct kernel_timespec {
259 long tv_sec;
260 long tv_nsec;
261};
262
263/* include/linux/time.h */
264struct kernel_timeval {
265 long tv_sec;
266 long tv_usec;
267};
268
269/* include/linux/resource.h */
270struct kernel_rusage {
271 struct kernel_timeval ru_utime;
272 struct kernel_timeval ru_stime;
273 long ru_maxrss;
274 long ru_ixrss;
275 long ru_idrss;
276 long ru_isrss;
277 long ru_minflt;
278 long ru_majflt;
279 long ru_nswap;
280 long ru_inblock;
281 long ru_oublock;
282 long ru_msgsnd;
283 long ru_msgrcv;
284 long ru_nsignals;
285 long ru_nvcsw;
286 long ru_nivcsw;
287};
288
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000289#if defined(__i386__) || defined(__ARM_EABI__) || defined(__ARM_ARCH_3__) \
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300290 || defined(__PPC__) || (defined(__s390__) && !defined(__s390x__)) \
291 || defined(__e2k__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000292
293/* include/asm-{arm,i386,mips,ppc}/signal.h */
294struct kernel_old_sigaction {
295 union {
296 void (*sa_handler_)(int);
vapier@chromium.orgcdda4342013-03-06 04:26:28 +0000297 void (*sa_sigaction_)(int, siginfo_t *, void *);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000298 };
299 unsigned long sa_mask;
300 unsigned long sa_flags;
301 void (*sa_restorer)(void);
302} __attribute__((packed,aligned(4)));
303#elif (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
304 #define kernel_old_sigaction kernel_sigaction
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000305#elif defined(__aarch64__)
306 // No kernel_old_sigaction defined for arm64.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000307#endif
308
309/* Some kernel functions (e.g. sigaction() in 2.6.23) require that the
310 * exactly match the size of the signal set, even though the API was
311 * intended to be extensible. We define our own KERNEL_NSIG to deal with
312 * this.
313 * Please note that glibc provides signals [1.._NSIG-1], whereas the
314 * kernel (and this header) provides the range [1..KERNEL_NSIG]. The
315 * actual number of signals is obviously the same, but the constants
316 * differ by one.
317 */
318#ifdef __mips__
319#define KERNEL_NSIG 128
320#else
321#define KERNEL_NSIG 64
322#endif
323
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000324/* include/asm-{arm,aarch64,i386,mips,x86_64}/signal.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000325struct kernel_sigset_t {
326 unsigned long sig[(KERNEL_NSIG + 8*sizeof(unsigned long) - 1)/
327 (8*sizeof(unsigned long))];
328};
329
330/* include/asm-{arm,i386,mips,x86_64,ppc}/signal.h */
331struct kernel_sigaction {
332#ifdef __mips__
333 unsigned long sa_flags;
334 union {
335 void (*sa_handler_)(int);
vapier@chromium.orgcdda4342013-03-06 04:26:28 +0000336 void (*sa_sigaction_)(int, siginfo_t *, void *);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000337 };
338 struct kernel_sigset_t sa_mask;
339#else
340 union {
341 void (*sa_handler_)(int);
vapier@chromium.orgcdda4342013-03-06 04:26:28 +0000342 void (*sa_sigaction_)(int, siginfo_t *, void *);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000343 };
344 unsigned long sa_flags;
345 void (*sa_restorer)(void);
346 struct kernel_sigset_t sa_mask;
347#endif
348};
349
350/* include/linux/socket.h */
351struct kernel_sockaddr {
352 unsigned short sa_family;
353 char sa_data[14];
354};
355
Bryan Chan3f6478a2016-06-14 08:38:17 -0400356/* include/asm-{arm,aarch64,i386,mips,ppc,s390}/stat.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000357#ifdef __mips__
358#if _MIPS_SIM == _MIPS_SIM_ABI64
359struct kernel_stat {
360#else
361struct kernel_stat64 {
362#endif
363 unsigned st_dev;
364 unsigned __pad0[3];
365 unsigned long long st_ino;
366 unsigned st_mode;
367 unsigned st_nlink;
368 unsigned st_uid;
369 unsigned st_gid;
370 unsigned st_rdev;
371 unsigned __pad1[3];
372 long long st_size;
373 unsigned st_atime_;
374 unsigned st_atime_nsec_;
375 unsigned st_mtime_;
376 unsigned st_mtime_nsec_;
377 unsigned st_ctime_;
378 unsigned st_ctime_nsec_;
379 unsigned st_blksize;
380 unsigned __pad2;
381 unsigned long long st_blocks;
382};
383#elif defined __PPC__
384struct kernel_stat64 {
385 unsigned long long st_dev;
386 unsigned long long st_ino;
387 unsigned st_mode;
388 unsigned st_nlink;
389 unsigned st_uid;
390 unsigned st_gid;
391 unsigned long long st_rdev;
392 unsigned short int __pad2;
393 long long st_size;
394 long st_blksize;
395 long long st_blocks;
396 long st_atime_;
397 unsigned long st_atime_nsec_;
398 long st_mtime_;
399 unsigned long st_mtime_nsec_;
400 long st_ctime_;
401 unsigned long st_ctime_nsec_;
402 unsigned long __unused4;
403 unsigned long __unused5;
404};
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300405#elif defined(__e2k__)
406struct kernel_stat64 {
407 unsigned long long st_dev;
408 unsigned long long st_ino;
409 unsigned int st_mode;
410 unsigned int st_nlink;
411 unsigned int st_uid;
412 unsigned int st_gid;
413 unsigned long long st_rdev;
414 long long st_size;
415 int st_blksize;
416 int __pad2;
417 unsigned long long st_blocks;
418 int st_atime_;
419 unsigned int st_atime_nsec_;
420 int st_mtime_;
421 unsigned int st_mtime_nsec_;
422 int st_ctime_;
423 unsigned int st_ctime_nsec_;
424 unsigned int __unused4;
425 unsigned int __unused5;
426};
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000427#else
428struct kernel_stat64 {
429 unsigned long long st_dev;
430 unsigned char __pad0[4];
431 unsigned __st_ino;
432 unsigned st_mode;
433 unsigned st_nlink;
434 unsigned st_uid;
435 unsigned st_gid;
436 unsigned long long st_rdev;
437 unsigned char __pad3[4];
438 long long st_size;
439 unsigned st_blksize;
440 unsigned long long 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 long long st_ino;
448};
449#endif
450
Bryan Chan3f6478a2016-06-14 08:38:17 -0400451/* include/asm-{arm,aarch64,i386,mips,x86_64,ppc,s390}/stat.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000452#if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__)
453struct kernel_stat {
454 /* The kernel headers suggest that st_dev and st_rdev should be 32bit
455 * quantities encoding 12bit major and 20bit minor numbers in an interleaved
456 * format. In reality, we do not see useful data in the top bits. So,
457 * we'll leave the padding in here, until we find a better solution.
458 */
459 unsigned short st_dev;
460 short pad1;
461 unsigned st_ino;
462 unsigned short st_mode;
463 unsigned short st_nlink;
464 unsigned short st_uid;
465 unsigned short st_gid;
466 unsigned short st_rdev;
467 short pad2;
468 unsigned st_size;
469 unsigned st_blksize;
470 unsigned st_blocks;
471 unsigned st_atime_;
472 unsigned st_atime_nsec_;
473 unsigned st_mtime_;
474 unsigned st_mtime_nsec_;
475 unsigned st_ctime_;
476 unsigned st_ctime_nsec_;
477 unsigned __unused4;
478 unsigned __unused5;
479};
480#elif defined(__x86_64__)
481struct kernel_stat {
vapier@chromium.org2273e812013-04-01 17:52:44 +0000482 uint64_t st_dev;
483 uint64_t st_ino;
484 uint64_t st_nlink;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000485 unsigned st_mode;
486 unsigned st_uid;
487 unsigned st_gid;
488 unsigned __pad0;
vapier@chromium.org2273e812013-04-01 17:52:44 +0000489 uint64_t st_rdev;
490 int64_t st_size;
491 int64_t st_blksize;
492 int64_t st_blocks;
493 uint64_t st_atime_;
494 uint64_t st_atime_nsec_;
495 uint64_t st_mtime_;
496 uint64_t st_mtime_nsec_;
497 uint64_t st_ctime_;
498 uint64_t st_ctime_nsec_;
anton@chromium.org43de0522014-04-04 11:20:46 +0000499 int64_t __unused4[3];
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000500};
501#elif defined(__PPC__)
502struct kernel_stat {
503 unsigned st_dev;
504 unsigned long st_ino; // ino_t
505 unsigned long st_mode; // mode_t
506 unsigned short st_nlink; // nlink_t
507 unsigned st_uid; // uid_t
508 unsigned st_gid; // gid_t
509 unsigned st_rdev;
510 long st_size; // off_t
511 unsigned long st_blksize;
512 unsigned long st_blocks;
513 unsigned long st_atime_;
514 unsigned long st_atime_nsec_;
515 unsigned long st_mtime_;
516 unsigned long st_mtime_nsec_;
517 unsigned long st_ctime_;
518 unsigned long st_ctime_nsec_;
519 unsigned long __unused4;
520 unsigned long __unused5;
521};
522#elif (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64)
523struct kernel_stat {
524 unsigned st_dev;
525 int st_pad1[3];
526 unsigned st_ino;
527 unsigned st_mode;
528 unsigned st_nlink;
529 unsigned st_uid;
530 unsigned st_gid;
531 unsigned st_rdev;
532 int st_pad2[2];
533 long st_size;
534 int st_pad3;
535 long st_atime_;
536 long st_atime_nsec_;
537 long st_mtime_;
538 long st_mtime_nsec_;
539 long st_ctime_;
540 long st_ctime_nsec_;
541 int st_blksize;
542 int st_blocks;
543 int st_pad4[14];
544};
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000545#elif defined(__aarch64__)
546struct kernel_stat {
547 unsigned long st_dev;
548 unsigned long st_ino;
549 unsigned int st_mode;
550 unsigned int st_nlink;
551 unsigned int st_uid;
552 unsigned int st_gid;
553 unsigned long st_rdev;
554 unsigned long __pad1;
555 long st_size;
556 int st_blksize;
557 int __pad2;
558 long st_blocks;
559 long st_atime_;
560 unsigned long st_atime_nsec_;
561 long st_mtime_;
562 unsigned long st_mtime_nsec_;
563 long st_ctime_;
564 unsigned long st_ctime_nsec_;
565 unsigned int __unused4;
566 unsigned int __unused5;
567};
Bryan Chan3f6478a2016-06-14 08:38:17 -0400568#elif defined(__s390x__)
569struct kernel_stat {
570 unsigned long st_dev;
571 unsigned long st_ino;
572 unsigned long st_nlink;
573 unsigned int st_mode;
574 unsigned int st_uid;
575 unsigned int st_gid;
576 unsigned int __pad1;
577 unsigned long st_rdev;
578 unsigned long st_size;
579 unsigned long st_atime_;
580 unsigned long st_atime_nsec_;
581 unsigned long st_mtime_;
582 unsigned long st_mtime_nsec_;
583 unsigned long st_ctime_;
584 unsigned long st_ctime_nsec_;
585 unsigned long st_blksize;
586 long st_blocks;
587 unsigned long __unused[3];
588};
589#elif defined(__s390__)
590struct kernel_stat {
591 unsigned short st_dev;
592 unsigned short __pad1;
593 unsigned long st_ino;
594 unsigned short st_mode;
595 unsigned short st_nlink;
596 unsigned short st_uid;
597 unsigned short st_gid;
598 unsigned short st_rdev;
599 unsigned short __pad2;
600 unsigned long st_size;
601 unsigned long st_blksize;
602 unsigned long st_blocks;
603 unsigned long st_atime_;
604 unsigned long st_atime_nsec_;
605 unsigned long st_mtime_;
606 unsigned long st_mtime_nsec_;
607 unsigned long st_ctime_;
608 unsigned long st_ctime_nsec_;
609 unsigned long __unused4;
610 unsigned long __unused5;
611};
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300612#elif defined(__e2k__)
613struct kernel_stat {
614 unsigned long st_dev;
615 unsigned long st_ino;
616 unsigned int st_mode;
617 unsigned long st_nlink;
618 unsigned int st_uid;
619 unsigned int st_gid;
620 unsigned long st_rdev;
621 unsigned long st_size;
622 unsigned long st_blksize;
623 unsigned long st_blocks;
624 unsigned long st_atime_;
625 unsigned long st_atime_nsec_;
626 unsigned long st_mtime_;
627 unsigned long st_mtime_nsec_;
628 unsigned long st_ctime_;
629 unsigned long st_ctime_nsec_;
630};
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000631#endif
632
Bryan Chan3f6478a2016-06-14 08:38:17 -0400633/* include/asm-{arm,aarch64,i386,mips,x86_64,ppc,s390}/statfs.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000634#ifdef __mips__
635#if _MIPS_SIM != _MIPS_SIM_ABI64
636struct kernel_statfs64 {
637 unsigned long f_type;
638 unsigned long f_bsize;
639 unsigned long f_frsize;
640 unsigned long __pad;
641 unsigned long long f_blocks;
642 unsigned long long f_bfree;
643 unsigned long long f_files;
644 unsigned long long f_ffree;
645 unsigned long long f_bavail;
646 struct { int val[2]; } f_fsid;
647 unsigned long f_namelen;
648 unsigned long f_spare[6];
649};
650#endif
Bryan Chan3f6478a2016-06-14 08:38:17 -0400651#elif defined(__s390__)
652/* See also arch/s390/include/asm/compat.h */
653struct kernel_statfs64 {
654 unsigned int f_type;
655 unsigned int f_bsize;
656 unsigned long long f_blocks;
657 unsigned long long f_bfree;
658 unsigned long long f_bavail;
659 unsigned long long f_files;
660 unsigned long long f_ffree;
661 struct { int val[2]; } f_fsid;
662 unsigned int f_namelen;
663 unsigned int f_frsize;
664 unsigned int f_flags;
665 unsigned int f_spare[4];
666};
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000667#elif !defined(__x86_64__)
668struct kernel_statfs64 {
669 unsigned long f_type;
670 unsigned long f_bsize;
671 unsigned long long f_blocks;
672 unsigned long long f_bfree;
673 unsigned long long f_bavail;
674 unsigned long long f_files;
675 unsigned long long f_ffree;
676 struct { int val[2]; } f_fsid;
677 unsigned long f_namelen;
678 unsigned long f_frsize;
679 unsigned long f_spare[5];
680};
681#endif
682
Bryan Chan3f6478a2016-06-14 08:38:17 -0400683/* include/asm-{arm,i386,mips,x86_64,ppc,generic,s390}/statfs.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000684#ifdef __mips__
685struct kernel_statfs {
686 long f_type;
687 long f_bsize;
688 long f_frsize;
689 long f_blocks;
690 long f_bfree;
691 long f_files;
692 long f_ffree;
693 long f_bavail;
694 struct { int val[2]; } f_fsid;
695 long f_namelen;
696 long f_spare[6];
697};
vapier@chromium.org2273e812013-04-01 17:52:44 +0000698#elif defined(__x86_64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000699struct kernel_statfs {
700 /* x86_64 actually defines all these fields as signed, whereas all other */
701 /* platforms define them as unsigned. Leaving them at unsigned should not */
vapier@chromium.org2273e812013-04-01 17:52:44 +0000702 /* cause any problems. Make sure these are 64-bit even on x32. */
703 uint64_t f_type;
704 uint64_t f_bsize;
705 uint64_t f_blocks;
706 uint64_t f_bfree;
707 uint64_t f_bavail;
708 uint64_t f_files;
709 uint64_t f_ffree;
710 struct { int val[2]; } f_fsid;
711 uint64_t f_namelen;
712 uint64_t f_frsize;
713 uint64_t f_spare[5];
714};
Bryan Chan3f6478a2016-06-14 08:38:17 -0400715#elif defined(__s390__)
716struct kernel_statfs {
717 unsigned int f_type;
718 unsigned int f_bsize;
719 unsigned long f_blocks;
720 unsigned long f_bfree;
721 unsigned long f_bavail;
722 unsigned long f_files;
723 unsigned long f_ffree;
724 struct { int val[2]; } f_fsid;
725 unsigned int f_namelen;
726 unsigned int f_frsize;
727 unsigned int f_flags;
728 unsigned int f_spare[4];
729};
vapier@chromium.org2273e812013-04-01 17:52:44 +0000730#else
731struct kernel_statfs {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000732 unsigned long f_type;
733 unsigned long f_bsize;
734 unsigned long f_blocks;
735 unsigned long f_bfree;
736 unsigned long f_bavail;
737 unsigned long f_files;
738 unsigned long f_ffree;
739 struct { int val[2]; } f_fsid;
740 unsigned long f_namelen;
741 unsigned long f_frsize;
742 unsigned long f_spare[5];
743};
744#endif
745
746
747/* Definitions missing from the standard header files */
748#ifndef O_DIRECTORY
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000749#if defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || defined(__aarch64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000750#define O_DIRECTORY 0040000
751#else
752#define O_DIRECTORY 0200000
753#endif
754#endif
755#ifndef NT_PRXFPREG
756#define NT_PRXFPREG 0x46e62b7f
757#endif
758#ifndef PTRACE_GETFPXREGS
759#define PTRACE_GETFPXREGS ((enum __ptrace_request)18)
760#endif
761#ifndef PR_GET_DUMPABLE
762#define PR_GET_DUMPABLE 3
763#endif
764#ifndef PR_SET_DUMPABLE
765#define PR_SET_DUMPABLE 4
766#endif
767#ifndef PR_GET_SECCOMP
768#define PR_GET_SECCOMP 21
769#endif
770#ifndef PR_SET_SECCOMP
771#define PR_SET_SECCOMP 22
772#endif
773#ifndef AT_FDCWD
774#define AT_FDCWD (-100)
775#endif
776#ifndef AT_SYMLINK_NOFOLLOW
777#define AT_SYMLINK_NOFOLLOW 0x100
778#endif
779#ifndef AT_REMOVEDIR
780#define AT_REMOVEDIR 0x200
781#endif
782#ifndef MREMAP_FIXED
783#define MREMAP_FIXED 2
784#endif
785#ifndef SA_RESTORER
786#define SA_RESTORER 0x04000000
787#endif
788#ifndef CPUCLOCK_PROF
789#define CPUCLOCK_PROF 0
790#endif
791#ifndef CPUCLOCK_VIRT
792#define CPUCLOCK_VIRT 1
793#endif
794#ifndef CPUCLOCK_SCHED
795#define CPUCLOCK_SCHED 2
796#endif
797#ifndef CPUCLOCK_PERTHREAD_MASK
798#define CPUCLOCK_PERTHREAD_MASK 4
799#endif
800#ifndef MAKE_PROCESS_CPUCLOCK
801#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
Nico Webera2b70922017-03-30 11:03:37 -0400802 ((int)(~(unsigned)(pid) << 3) | (int)(clock))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000803#endif
804#ifndef MAKE_THREAD_CPUCLOCK
805#define MAKE_THREAD_CPUCLOCK(tid, clock) \
Nico Webera2b70922017-03-30 11:03:37 -0400806 ((int)(~(unsigned)(tid) << 3) | \
807 (int)((clock) | CPUCLOCK_PERTHREAD_MASK))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000808#endif
809
810#ifndef FUTEX_WAIT
811#define FUTEX_WAIT 0
812#endif
813#ifndef FUTEX_WAKE
814#define FUTEX_WAKE 1
815#endif
816#ifndef FUTEX_FD
817#define FUTEX_FD 2
818#endif
819#ifndef FUTEX_REQUEUE
820#define FUTEX_REQUEUE 3
821#endif
822#ifndef FUTEX_CMP_REQUEUE
823#define FUTEX_CMP_REQUEUE 4
824#endif
825#ifndef FUTEX_WAKE_OP
826#define FUTEX_WAKE_OP 5
827#endif
828#ifndef FUTEX_LOCK_PI
829#define FUTEX_LOCK_PI 6
830#endif
831#ifndef FUTEX_UNLOCK_PI
832#define FUTEX_UNLOCK_PI 7
833#endif
834#ifndef FUTEX_TRYLOCK_PI
835#define FUTEX_TRYLOCK_PI 8
836#endif
837#ifndef FUTEX_PRIVATE_FLAG
838#define FUTEX_PRIVATE_FLAG 128
839#endif
840#ifndef FUTEX_CMD_MASK
841#define FUTEX_CMD_MASK ~FUTEX_PRIVATE_FLAG
842#endif
843#ifndef FUTEX_WAIT_PRIVATE
844#define FUTEX_WAIT_PRIVATE (FUTEX_WAIT | FUTEX_PRIVATE_FLAG)
845#endif
846#ifndef FUTEX_WAKE_PRIVATE
847#define FUTEX_WAKE_PRIVATE (FUTEX_WAKE | FUTEX_PRIVATE_FLAG)
848#endif
849#ifndef FUTEX_REQUEUE_PRIVATE
850#define FUTEX_REQUEUE_PRIVATE (FUTEX_REQUEUE | FUTEX_PRIVATE_FLAG)
851#endif
852#ifndef FUTEX_CMP_REQUEUE_PRIVATE
853#define FUTEX_CMP_REQUEUE_PRIVATE (FUTEX_CMP_REQUEUE | FUTEX_PRIVATE_FLAG)
854#endif
855#ifndef FUTEX_WAKE_OP_PRIVATE
856#define FUTEX_WAKE_OP_PRIVATE (FUTEX_WAKE_OP | FUTEX_PRIVATE_FLAG)
857#endif
858#ifndef FUTEX_LOCK_PI_PRIVATE
859#define FUTEX_LOCK_PI_PRIVATE (FUTEX_LOCK_PI | FUTEX_PRIVATE_FLAG)
860#endif
861#ifndef FUTEX_UNLOCK_PI_PRIVATE
862#define FUTEX_UNLOCK_PI_PRIVATE (FUTEX_UNLOCK_PI | FUTEX_PRIVATE_FLAG)
863#endif
864#ifndef FUTEX_TRYLOCK_PI_PRIVATE
865#define FUTEX_TRYLOCK_PI_PRIVATE (FUTEX_TRYLOCK_PI | FUTEX_PRIVATE_FLAG)
866#endif
867
868
869#if defined(__x86_64__)
870#ifndef ARCH_SET_GS
871#define ARCH_SET_GS 0x1001
872#endif
873#ifndef ARCH_GET_GS
874#define ARCH_GET_GS 0x1004
875#endif
876#endif
877
878#if defined(__i386__)
879#ifndef __NR_quotactl
880#define __NR_quotactl 131
881#endif
882#ifndef __NR_setresuid
883#define __NR_setresuid 164
884#define __NR_getresuid 165
885#define __NR_setresgid 170
886#define __NR_getresgid 171
887#endif
888#ifndef __NR_rt_sigaction
889#define __NR_rt_sigreturn 173
890#define __NR_rt_sigaction 174
891#define __NR_rt_sigprocmask 175
892#define __NR_rt_sigpending 176
893#define __NR_rt_sigsuspend 179
894#endif
895#ifndef __NR_pread64
896#define __NR_pread64 180
897#endif
898#ifndef __NR_pwrite64
899#define __NR_pwrite64 181
900#endif
901#ifndef __NR_ugetrlimit
902#define __NR_ugetrlimit 191
903#endif
904#ifndef __NR_stat64
905#define __NR_stat64 195
906#endif
907#ifndef __NR_fstat64
908#define __NR_fstat64 197
909#endif
910#ifndef __NR_setresuid32
911#define __NR_setresuid32 208
912#define __NR_getresuid32 209
913#define __NR_setresgid32 210
914#define __NR_getresgid32 211
915#endif
916#ifndef __NR_setfsuid32
917#define __NR_setfsuid32 215
918#define __NR_setfsgid32 216
919#endif
920#ifndef __NR_getdents64
921#define __NR_getdents64 220
922#endif
923#ifndef __NR_gettid
924#define __NR_gettid 224
925#endif
926#ifndef __NR_readahead
927#define __NR_readahead 225
928#endif
929#ifndef __NR_setxattr
930#define __NR_setxattr 226
931#endif
932#ifndef __NR_lsetxattr
933#define __NR_lsetxattr 227
934#endif
935#ifndef __NR_getxattr
936#define __NR_getxattr 229
937#endif
938#ifndef __NR_lgetxattr
939#define __NR_lgetxattr 230
940#endif
941#ifndef __NR_listxattr
942#define __NR_listxattr 232
943#endif
944#ifndef __NR_llistxattr
945#define __NR_llistxattr 233
946#endif
947#ifndef __NR_tkill
948#define __NR_tkill 238
949#endif
950#ifndef __NR_futex
951#define __NR_futex 240
952#endif
953#ifndef __NR_sched_setaffinity
954#define __NR_sched_setaffinity 241
955#define __NR_sched_getaffinity 242
956#endif
957#ifndef __NR_set_tid_address
958#define __NR_set_tid_address 258
959#endif
960#ifndef __NR_clock_gettime
961#define __NR_clock_gettime 265
962#endif
963#ifndef __NR_clock_getres
964#define __NR_clock_getres 266
965#endif
966#ifndef __NR_statfs64
967#define __NR_statfs64 268
968#endif
969#ifndef __NR_fstatfs64
970#define __NR_fstatfs64 269
971#endif
972#ifndef __NR_fadvise64_64
973#define __NR_fadvise64_64 272
974#endif
975#ifndef __NR_ioprio_set
976#define __NR_ioprio_set 289
977#endif
978#ifndef __NR_ioprio_get
979#define __NR_ioprio_get 290
980#endif
981#ifndef __NR_openat
982#define __NR_openat 295
983#endif
984#ifndef __NR_fstatat64
985#define __NR_fstatat64 300
986#endif
987#ifndef __NR_unlinkat
988#define __NR_unlinkat 301
989#endif
990#ifndef __NR_move_pages
991#define __NR_move_pages 317
992#endif
993#ifndef __NR_getcpu
994#define __NR_getcpu 318
995#endif
996#ifndef __NR_fallocate
997#define __NR_fallocate 324
998#endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -0700999#ifndef __NR_getrandom
1000#define __NR_getrandom 355
1001#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001002/* End of i386 definitions */
1003#elif defined(__ARM_ARCH_3__) || defined(__ARM_EABI__)
1004#ifndef __NR_setresuid
1005#define __NR_setresuid (__NR_SYSCALL_BASE + 164)
1006#define __NR_getresuid (__NR_SYSCALL_BASE + 165)
1007#define __NR_setresgid (__NR_SYSCALL_BASE + 170)
1008#define __NR_getresgid (__NR_SYSCALL_BASE + 171)
1009#endif
1010#ifndef __NR_rt_sigaction
1011#define __NR_rt_sigreturn (__NR_SYSCALL_BASE + 173)
1012#define __NR_rt_sigaction (__NR_SYSCALL_BASE + 174)
1013#define __NR_rt_sigprocmask (__NR_SYSCALL_BASE + 175)
1014#define __NR_rt_sigpending (__NR_SYSCALL_BASE + 176)
1015#define __NR_rt_sigsuspend (__NR_SYSCALL_BASE + 179)
1016#endif
1017#ifndef __NR_pread64
1018#define __NR_pread64 (__NR_SYSCALL_BASE + 180)
1019#endif
1020#ifndef __NR_pwrite64
1021#define __NR_pwrite64 (__NR_SYSCALL_BASE + 181)
1022#endif
1023#ifndef __NR_ugetrlimit
1024#define __NR_ugetrlimit (__NR_SYSCALL_BASE + 191)
1025#endif
1026#ifndef __NR_stat64
1027#define __NR_stat64 (__NR_SYSCALL_BASE + 195)
1028#endif
1029#ifndef __NR_fstat64
1030#define __NR_fstat64 (__NR_SYSCALL_BASE + 197)
1031#endif
1032#ifndef __NR_setresuid32
1033#define __NR_setresuid32 (__NR_SYSCALL_BASE + 208)
1034#define __NR_getresuid32 (__NR_SYSCALL_BASE + 209)
1035#define __NR_setresgid32 (__NR_SYSCALL_BASE + 210)
1036#define __NR_getresgid32 (__NR_SYSCALL_BASE + 211)
1037#endif
1038#ifndef __NR_setfsuid32
1039#define __NR_setfsuid32 (__NR_SYSCALL_BASE + 215)
1040#define __NR_setfsgid32 (__NR_SYSCALL_BASE + 216)
1041#endif
1042#ifndef __NR_getdents64
1043#define __NR_getdents64 (__NR_SYSCALL_BASE + 217)
1044#endif
1045#ifndef __NR_gettid
1046#define __NR_gettid (__NR_SYSCALL_BASE + 224)
1047#endif
1048#ifndef __NR_readahead
1049#define __NR_readahead (__NR_SYSCALL_BASE + 225)
1050#endif
1051#ifndef __NR_setxattr
1052#define __NR_setxattr (__NR_SYSCALL_BASE + 226)
1053#endif
1054#ifndef __NR_lsetxattr
1055#define __NR_lsetxattr (__NR_SYSCALL_BASE + 227)
1056#endif
1057#ifndef __NR_getxattr
1058#define __NR_getxattr (__NR_SYSCALL_BASE + 229)
1059#endif
1060#ifndef __NR_lgetxattr
1061#define __NR_lgetxattr (__NR_SYSCALL_BASE + 230)
1062#endif
1063#ifndef __NR_listxattr
1064#define __NR_listxattr (__NR_SYSCALL_BASE + 232)
1065#endif
1066#ifndef __NR_llistxattr
1067#define __NR_llistxattr (__NR_SYSCALL_BASE + 233)
1068#endif
1069#ifndef __NR_tkill
1070#define __NR_tkill (__NR_SYSCALL_BASE + 238)
1071#endif
1072#ifndef __NR_futex
1073#define __NR_futex (__NR_SYSCALL_BASE + 240)
1074#endif
1075#ifndef __NR_sched_setaffinity
1076#define __NR_sched_setaffinity (__NR_SYSCALL_BASE + 241)
1077#define __NR_sched_getaffinity (__NR_SYSCALL_BASE + 242)
1078#endif
1079#ifndef __NR_set_tid_address
1080#define __NR_set_tid_address (__NR_SYSCALL_BASE + 256)
1081#endif
1082#ifndef __NR_clock_gettime
1083#define __NR_clock_gettime (__NR_SYSCALL_BASE + 263)
1084#endif
1085#ifndef __NR_clock_getres
1086#define __NR_clock_getres (__NR_SYSCALL_BASE + 264)
1087#endif
1088#ifndef __NR_statfs64
1089#define __NR_statfs64 (__NR_SYSCALL_BASE + 266)
1090#endif
1091#ifndef __NR_fstatfs64
1092#define __NR_fstatfs64 (__NR_SYSCALL_BASE + 267)
1093#endif
1094#ifndef __NR_ioprio_set
1095#define __NR_ioprio_set (__NR_SYSCALL_BASE + 314)
1096#endif
1097#ifndef __NR_ioprio_get
1098#define __NR_ioprio_get (__NR_SYSCALL_BASE + 315)
1099#endif
1100#ifndef __NR_move_pages
1101#define __NR_move_pages (__NR_SYSCALL_BASE + 344)
1102#endif
1103#ifndef __NR_getcpu
1104#define __NR_getcpu (__NR_SYSCALL_BASE + 345)
1105#endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001106#ifndef __NR_getrandom
1107#define __NR_getrandom (__NR_SYSCALL_BASE + 384)
1108#endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04001109/* End of ARM 3/EABI definitions */
anton@chromium.org2f724fc2014-04-15 13:05:20 +00001110#elif defined(__aarch64__)
1111#ifndef __NR_setxattr
1112#define __NR_setxattr 5
1113#endif
1114#ifndef __NR_lsetxattr
1115#define __NR_lsetxattr 6
1116#endif
1117#ifndef __NR_getxattr
1118#define __NR_getxattr 8
1119#endif
1120#ifndef __NR_lgetxattr
1121#define __NR_lgetxattr 9
1122#endif
1123#ifndef __NR_listxattr
1124#define __NR_listxattr 11
1125#endif
1126#ifndef __NR_llistxattr
1127#define __NR_llistxattr 12
1128#endif
1129#ifndef __NR_ioprio_set
1130#define __NR_ioprio_set 30
1131#endif
1132#ifndef __NR_ioprio_get
1133#define __NR_ioprio_get 31
1134#endif
1135#ifndef __NR_unlinkat
1136#define __NR_unlinkat 35
1137#endif
1138#ifndef __NR_fallocate
1139#define __NR_fallocate 47
1140#endif
1141#ifndef __NR_openat
1142#define __NR_openat 56
1143#endif
1144#ifndef __NR_quotactl
1145#define __NR_quotactl 60
1146#endif
1147#ifndef __NR_getdents64
1148#define __NR_getdents64 61
1149#endif
1150#ifndef __NR_getdents
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04001151// when getdents is not available, getdents64 is used for both.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00001152#define __NR_getdents __NR_getdents64
1153#endif
1154#ifndef __NR_pread64
1155#define __NR_pread64 67
1156#endif
1157#ifndef __NR_pwrite64
1158#define __NR_pwrite64 68
1159#endif
1160#ifndef __NR_ppoll
1161#define __NR_ppoll 73
1162#endif
1163#ifndef __NR_readlinkat
1164#define __NR_readlinkat 78
1165#endif
1166#ifndef __NR_newfstatat
1167#define __NR_newfstatat 79
1168#endif
1169#ifndef __NR_set_tid_address
1170#define __NR_set_tid_address 96
1171#endif
1172#ifndef __NR_futex
1173#define __NR_futex 98
1174#endif
1175#ifndef __NR_clock_gettime
1176#define __NR_clock_gettime 113
1177#endif
1178#ifndef __NR_clock_getres
1179#define __NR_clock_getres 114
1180#endif
1181#ifndef __NR_sched_setaffinity
1182#define __NR_sched_setaffinity 122
1183#define __NR_sched_getaffinity 123
1184#endif
1185#ifndef __NR_tkill
1186#define __NR_tkill 130
1187#endif
1188#ifndef __NR_setresuid
1189#define __NR_setresuid 147
1190#define __NR_getresuid 148
1191#define __NR_setresgid 149
1192#define __NR_getresgid 150
1193#endif
1194#ifndef __NR_gettid
1195#define __NR_gettid 178
1196#endif
1197#ifndef __NR_readahead
1198#define __NR_readahead 213
1199#endif
1200#ifndef __NR_fadvise64
1201#define __NR_fadvise64 223
1202#endif
1203#ifndef __NR_move_pages
1204#define __NR_move_pages 239
1205#endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001206#ifndef __NR_getrandom
1207#define __NR_getrandom 278
1208#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00001209/* End of aarch64 definitions */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001210#elif defined(__x86_64__)
1211#ifndef __NR_pread64
1212#define __NR_pread64 17
1213#endif
1214#ifndef __NR_pwrite64
1215#define __NR_pwrite64 18
1216#endif
1217#ifndef __NR_setresuid
1218#define __NR_setresuid 117
1219#define __NR_getresuid 118
1220#define __NR_setresgid 119
1221#define __NR_getresgid 120
1222#endif
1223#ifndef __NR_quotactl
1224#define __NR_quotactl 179
1225#endif
1226#ifndef __NR_gettid
1227#define __NR_gettid 186
1228#endif
1229#ifndef __NR_readahead
1230#define __NR_readahead 187
1231#endif
1232#ifndef __NR_setxattr
1233#define __NR_setxattr 188
1234#endif
1235#ifndef __NR_lsetxattr
1236#define __NR_lsetxattr 189
1237#endif
1238#ifndef __NR_getxattr
1239#define __NR_getxattr 191
1240#endif
1241#ifndef __NR_lgetxattr
1242#define __NR_lgetxattr 192
1243#endif
1244#ifndef __NR_listxattr
1245#define __NR_listxattr 194
1246#endif
1247#ifndef __NR_llistxattr
1248#define __NR_llistxattr 195
1249#endif
1250#ifndef __NR_tkill
1251#define __NR_tkill 200
1252#endif
1253#ifndef __NR_futex
1254#define __NR_futex 202
1255#endif
1256#ifndef __NR_sched_setaffinity
1257#define __NR_sched_setaffinity 203
1258#define __NR_sched_getaffinity 204
1259#endif
1260#ifndef __NR_getdents64
1261#define __NR_getdents64 217
1262#endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04001263#ifndef __NR_getdents
1264// when getdents is not available, getdents64 is used for both.
1265#define __NR_getdents __NR_getdents64
1266#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001267#ifndef __NR_set_tid_address
1268#define __NR_set_tid_address 218
1269#endif
1270#ifndef __NR_fadvise64
1271#define __NR_fadvise64 221
1272#endif
1273#ifndef __NR_clock_gettime
1274#define __NR_clock_gettime 228
1275#endif
1276#ifndef __NR_clock_getres
1277#define __NR_clock_getres 229
1278#endif
1279#ifndef __NR_ioprio_set
1280#define __NR_ioprio_set 251
1281#endif
1282#ifndef __NR_ioprio_get
1283#define __NR_ioprio_get 252
1284#endif
1285#ifndef __NR_openat
1286#define __NR_openat 257
1287#endif
1288#ifndef __NR_newfstatat
1289#define __NR_newfstatat 262
1290#endif
1291#ifndef __NR_unlinkat
1292#define __NR_unlinkat 263
1293#endif
1294#ifndef __NR_move_pages
1295#define __NR_move_pages 279
1296#endif
1297#ifndef __NR_fallocate
1298#define __NR_fallocate 285
1299#endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001300#ifndef __NR_getrandom
1301#define __NR_getrandom 318
1302#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001303/* End of x86-64 definitions */
1304#elif defined(__mips__)
1305#if _MIPS_SIM == _MIPS_SIM_ABI32
1306#ifndef __NR_setresuid
1307#define __NR_setresuid (__NR_Linux + 185)
1308#define __NR_getresuid (__NR_Linux + 186)
1309#define __NR_setresgid (__NR_Linux + 190)
1310#define __NR_getresgid (__NR_Linux + 191)
1311#endif
1312#ifndef __NR_rt_sigaction
1313#define __NR_rt_sigreturn (__NR_Linux + 193)
1314#define __NR_rt_sigaction (__NR_Linux + 194)
1315#define __NR_rt_sigprocmask (__NR_Linux + 195)
1316#define __NR_rt_sigpending (__NR_Linux + 196)
1317#define __NR_rt_sigsuspend (__NR_Linux + 199)
1318#endif
1319#ifndef __NR_pread64
1320#define __NR_pread64 (__NR_Linux + 200)
1321#endif
1322#ifndef __NR_pwrite64
1323#define __NR_pwrite64 (__NR_Linux + 201)
1324#endif
1325#ifndef __NR_stat64
1326#define __NR_stat64 (__NR_Linux + 213)
1327#endif
1328#ifndef __NR_fstat64
1329#define __NR_fstat64 (__NR_Linux + 215)
1330#endif
1331#ifndef __NR_getdents64
1332#define __NR_getdents64 (__NR_Linux + 219)
1333#endif
1334#ifndef __NR_gettid
1335#define __NR_gettid (__NR_Linux + 222)
1336#endif
1337#ifndef __NR_readahead
1338#define __NR_readahead (__NR_Linux + 223)
1339#endif
1340#ifndef __NR_setxattr
1341#define __NR_setxattr (__NR_Linux + 224)
1342#endif
1343#ifndef __NR_lsetxattr
1344#define __NR_lsetxattr (__NR_Linux + 225)
1345#endif
1346#ifndef __NR_getxattr
1347#define __NR_getxattr (__NR_Linux + 227)
1348#endif
1349#ifndef __NR_lgetxattr
1350#define __NR_lgetxattr (__NR_Linux + 228)
1351#endif
1352#ifndef __NR_listxattr
1353#define __NR_listxattr (__NR_Linux + 230)
1354#endif
1355#ifndef __NR_llistxattr
1356#define __NR_llistxattr (__NR_Linux + 231)
1357#endif
1358#ifndef __NR_tkill
1359#define __NR_tkill (__NR_Linux + 236)
1360#endif
1361#ifndef __NR_futex
1362#define __NR_futex (__NR_Linux + 238)
1363#endif
1364#ifndef __NR_sched_setaffinity
1365#define __NR_sched_setaffinity (__NR_Linux + 239)
1366#define __NR_sched_getaffinity (__NR_Linux + 240)
1367#endif
1368#ifndef __NR_set_tid_address
1369#define __NR_set_tid_address (__NR_Linux + 252)
1370#endif
1371#ifndef __NR_statfs64
1372#define __NR_statfs64 (__NR_Linux + 255)
1373#endif
1374#ifndef __NR_fstatfs64
1375#define __NR_fstatfs64 (__NR_Linux + 256)
1376#endif
1377#ifndef __NR_clock_gettime
1378#define __NR_clock_gettime (__NR_Linux + 263)
1379#endif
1380#ifndef __NR_clock_getres
1381#define __NR_clock_getres (__NR_Linux + 264)
1382#endif
1383#ifndef __NR_openat
1384#define __NR_openat (__NR_Linux + 288)
1385#endif
1386#ifndef __NR_fstatat
1387#define __NR_fstatat (__NR_Linux + 293)
1388#endif
1389#ifndef __NR_unlinkat
1390#define __NR_unlinkat (__NR_Linux + 294)
1391#endif
1392#ifndef __NR_move_pages
1393#define __NR_move_pages (__NR_Linux + 308)
1394#endif
1395#ifndef __NR_getcpu
1396#define __NR_getcpu (__NR_Linux + 312)
1397#endif
1398#ifndef __NR_ioprio_set
1399#define __NR_ioprio_set (__NR_Linux + 314)
1400#endif
1401#ifndef __NR_ioprio_get
1402#define __NR_ioprio_get (__NR_Linux + 315)
1403#endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001404#ifndef __NR_getrandom
1405#define __NR_getrandom (__NR_Linux + 353)
1406#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001407/* End of MIPS (old 32bit API) definitions */
1408#elif _MIPS_SIM == _MIPS_SIM_ABI64
1409#ifndef __NR_pread64
1410#define __NR_pread64 (__NR_Linux + 16)
1411#endif
1412#ifndef __NR_pwrite64
1413#define __NR_pwrite64 (__NR_Linux + 17)
1414#endif
1415#ifndef __NR_setresuid
1416#define __NR_setresuid (__NR_Linux + 115)
1417#define __NR_getresuid (__NR_Linux + 116)
1418#define __NR_setresgid (__NR_Linux + 117)
1419#define __NR_getresgid (__NR_Linux + 118)
1420#endif
1421#ifndef __NR_gettid
1422#define __NR_gettid (__NR_Linux + 178)
1423#endif
1424#ifndef __NR_readahead
1425#define __NR_readahead (__NR_Linux + 179)
1426#endif
1427#ifndef __NR_setxattr
1428#define __NR_setxattr (__NR_Linux + 180)
1429#endif
1430#ifndef __NR_lsetxattr
1431#define __NR_lsetxattr (__NR_Linux + 181)
1432#endif
1433#ifndef __NR_getxattr
1434#define __NR_getxattr (__NR_Linux + 183)
1435#endif
1436#ifndef __NR_lgetxattr
1437#define __NR_lgetxattr (__NR_Linux + 184)
1438#endif
1439#ifndef __NR_listxattr
1440#define __NR_listxattr (__NR_Linux + 186)
1441#endif
1442#ifndef __NR_llistxattr
1443#define __NR_llistxattr (__NR_Linux + 187)
1444#endif
1445#ifndef __NR_tkill
1446#define __NR_tkill (__NR_Linux + 192)
1447#endif
1448#ifndef __NR_futex
1449#define __NR_futex (__NR_Linux + 194)
1450#endif
1451#ifndef __NR_sched_setaffinity
1452#define __NR_sched_setaffinity (__NR_Linux + 195)
1453#define __NR_sched_getaffinity (__NR_Linux + 196)
1454#endif
1455#ifndef __NR_set_tid_address
1456#define __NR_set_tid_address (__NR_Linux + 212)
1457#endif
1458#ifndef __NR_clock_gettime
1459#define __NR_clock_gettime (__NR_Linux + 222)
1460#endif
1461#ifndef __NR_clock_getres
1462#define __NR_clock_getres (__NR_Linux + 223)
1463#endif
1464#ifndef __NR_openat
1465#define __NR_openat (__NR_Linux + 247)
1466#endif
1467#ifndef __NR_fstatat
1468#define __NR_fstatat (__NR_Linux + 252)
1469#endif
1470#ifndef __NR_unlinkat
1471#define __NR_unlinkat (__NR_Linux + 253)
1472#endif
1473#ifndef __NR_move_pages
1474#define __NR_move_pages (__NR_Linux + 267)
1475#endif
1476#ifndef __NR_getcpu
1477#define __NR_getcpu (__NR_Linux + 271)
1478#endif
1479#ifndef __NR_ioprio_set
1480#define __NR_ioprio_set (__NR_Linux + 273)
1481#endif
1482#ifndef __NR_ioprio_get
1483#define __NR_ioprio_get (__NR_Linux + 274)
1484#endif
Yu Yind9ad2962020-11-24 16:49:22 +08001485#ifndef __NR_getrandom
1486#define __NR_getrandom (__NR_Linux + 313)
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001487#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001488/* End of MIPS (64bit API) definitions */
1489#else
1490#ifndef __NR_setresuid
1491#define __NR_setresuid (__NR_Linux + 115)
1492#define __NR_getresuid (__NR_Linux + 116)
1493#define __NR_setresgid (__NR_Linux + 117)
1494#define __NR_getresgid (__NR_Linux + 118)
1495#endif
1496#ifndef __NR_gettid
1497#define __NR_gettid (__NR_Linux + 178)
1498#endif
1499#ifndef __NR_readahead
1500#define __NR_readahead (__NR_Linux + 179)
1501#endif
1502#ifndef __NR_setxattr
1503#define __NR_setxattr (__NR_Linux + 180)
1504#endif
1505#ifndef __NR_lsetxattr
1506#define __NR_lsetxattr (__NR_Linux + 181)
1507#endif
1508#ifndef __NR_getxattr
1509#define __NR_getxattr (__NR_Linux + 183)
1510#endif
1511#ifndef __NR_lgetxattr
1512#define __NR_lgetxattr (__NR_Linux + 184)
1513#endif
1514#ifndef __NR_listxattr
1515#define __NR_listxattr (__NR_Linux + 186)
1516#endif
1517#ifndef __NR_llistxattr
1518#define __NR_llistxattr (__NR_Linux + 187)
1519#endif
1520#ifndef __NR_tkill
1521#define __NR_tkill (__NR_Linux + 192)
1522#endif
1523#ifndef __NR_futex
1524#define __NR_futex (__NR_Linux + 194)
1525#endif
1526#ifndef __NR_sched_setaffinity
1527#define __NR_sched_setaffinity (__NR_Linux + 195)
1528#define __NR_sched_getaffinity (__NR_Linux + 196)
1529#endif
1530#ifndef __NR_set_tid_address
1531#define __NR_set_tid_address (__NR_Linux + 213)
1532#endif
1533#ifndef __NR_statfs64
1534#define __NR_statfs64 (__NR_Linux + 217)
1535#endif
1536#ifndef __NR_fstatfs64
1537#define __NR_fstatfs64 (__NR_Linux + 218)
1538#endif
1539#ifndef __NR_clock_gettime
1540#define __NR_clock_gettime (__NR_Linux + 226)
1541#endif
1542#ifndef __NR_clock_getres
1543#define __NR_clock_getres (__NR_Linux + 227)
1544#endif
1545#ifndef __NR_openat
1546#define __NR_openat (__NR_Linux + 251)
1547#endif
1548#ifndef __NR_fstatat
1549#define __NR_fstatat (__NR_Linux + 256)
1550#endif
1551#ifndef __NR_unlinkat
1552#define __NR_unlinkat (__NR_Linux + 257)
1553#endif
1554#ifndef __NR_move_pages
1555#define __NR_move_pages (__NR_Linux + 271)
1556#endif
1557#ifndef __NR_getcpu
1558#define __NR_getcpu (__NR_Linux + 275)
1559#endif
1560#ifndef __NR_ioprio_set
1561#define __NR_ioprio_set (__NR_Linux + 277)
1562#endif
1563#ifndef __NR_ioprio_get
1564#define __NR_ioprio_get (__NR_Linux + 278)
1565#endif
1566/* End of MIPS (new 32bit API) definitions */
1567#endif
1568/* End of MIPS definitions */
1569#elif defined(__PPC__)
1570#ifndef __NR_setfsuid
1571#define __NR_setfsuid 138
1572#define __NR_setfsgid 139
1573#endif
1574#ifndef __NR_setresuid
1575#define __NR_setresuid 164
1576#define __NR_getresuid 165
1577#define __NR_setresgid 169
1578#define __NR_getresgid 170
1579#endif
1580#ifndef __NR_rt_sigaction
1581#define __NR_rt_sigreturn 172
1582#define __NR_rt_sigaction 173
1583#define __NR_rt_sigprocmask 174
1584#define __NR_rt_sigpending 175
1585#define __NR_rt_sigsuspend 178
1586#endif
1587#ifndef __NR_pread64
1588#define __NR_pread64 179
1589#endif
1590#ifndef __NR_pwrite64
1591#define __NR_pwrite64 180
1592#endif
1593#ifndef __NR_ugetrlimit
1594#define __NR_ugetrlimit 190
1595#endif
1596#ifndef __NR_readahead
1597#define __NR_readahead 191
1598#endif
1599#ifndef __NR_stat64
1600#define __NR_stat64 195
1601#endif
1602#ifndef __NR_fstat64
1603#define __NR_fstat64 197
1604#endif
1605#ifndef __NR_getdents64
1606#define __NR_getdents64 202
1607#endif
1608#ifndef __NR_gettid
1609#define __NR_gettid 207
1610#endif
1611#ifndef __NR_tkill
1612#define __NR_tkill 208
1613#endif
1614#ifndef __NR_setxattr
1615#define __NR_setxattr 209
1616#endif
1617#ifndef __NR_lsetxattr
1618#define __NR_lsetxattr 210
1619#endif
1620#ifndef __NR_getxattr
1621#define __NR_getxattr 212
1622#endif
1623#ifndef __NR_lgetxattr
1624#define __NR_lgetxattr 213
1625#endif
1626#ifndef __NR_listxattr
1627#define __NR_listxattr 215
1628#endif
1629#ifndef __NR_llistxattr
1630#define __NR_llistxattr 216
1631#endif
1632#ifndef __NR_futex
1633#define __NR_futex 221
1634#endif
1635#ifndef __NR_sched_setaffinity
1636#define __NR_sched_setaffinity 222
1637#define __NR_sched_getaffinity 223
1638#endif
1639#ifndef __NR_set_tid_address
1640#define __NR_set_tid_address 232
1641#endif
1642#ifndef __NR_clock_gettime
1643#define __NR_clock_gettime 246
1644#endif
1645#ifndef __NR_clock_getres
1646#define __NR_clock_getres 247
1647#endif
1648#ifndef __NR_statfs64
1649#define __NR_statfs64 252
1650#endif
1651#ifndef __NR_fstatfs64
1652#define __NR_fstatfs64 253
1653#endif
1654#ifndef __NR_fadvise64_64
1655#define __NR_fadvise64_64 254
1656#endif
1657#ifndef __NR_ioprio_set
1658#define __NR_ioprio_set 273
1659#endif
1660#ifndef __NR_ioprio_get
1661#define __NR_ioprio_get 274
1662#endif
1663#ifndef __NR_openat
1664#define __NR_openat 286
1665#endif
1666#ifndef __NR_fstatat64
1667#define __NR_fstatat64 291
1668#endif
1669#ifndef __NR_unlinkat
1670#define __NR_unlinkat 292
1671#endif
1672#ifndef __NR_move_pages
1673#define __NR_move_pages 301
1674#endif
1675#ifndef __NR_getcpu
1676#define __NR_getcpu 302
1677#endif
1678/* End of powerpc defininitions */
Bryan Chan3f6478a2016-06-14 08:38:17 -04001679#elif defined(__s390__)
1680#ifndef __NR_quotactl
1681#define __NR_quotactl 131
1682#endif
1683#ifndef __NR_rt_sigreturn
1684#define __NR_rt_sigreturn 173
1685#endif
1686#ifndef __NR_rt_sigaction
1687#define __NR_rt_sigaction 174
1688#endif
1689#ifndef __NR_rt_sigprocmask
1690#define __NR_rt_sigprocmask 175
1691#endif
1692#ifndef __NR_rt_sigpending
1693#define __NR_rt_sigpending 176
1694#endif
1695#ifndef __NR_rt_sigsuspend
1696#define __NR_rt_sigsuspend 179
1697#endif
1698#ifndef __NR_pread64
1699#define __NR_pread64 180
1700#endif
1701#ifndef __NR_pwrite64
1702#define __NR_pwrite64 181
1703#endif
1704#ifndef __NR_getdents64
1705#define __NR_getdents64 220
1706#endif
1707#ifndef __NR_readahead
1708#define __NR_readahead 222
1709#endif
1710#ifndef __NR_setxattr
1711#define __NR_setxattr 224
1712#endif
1713#ifndef __NR_lsetxattr
1714#define __NR_lsetxattr 225
1715#endif
1716#ifndef __NR_getxattr
1717#define __NR_getxattr 227
1718#endif
1719#ifndef __NR_lgetxattr
1720#define __NR_lgetxattr 228
1721#endif
1722#ifndef __NR_listxattr
1723#define __NR_listxattr 230
1724#endif
1725#ifndef __NR_llistxattr
1726#define __NR_llistxattr 231
1727#endif
1728#ifndef __NR_gettid
1729#define __NR_gettid 236
1730#endif
1731#ifndef __NR_tkill
1732#define __NR_tkill 237
1733#endif
1734#ifndef __NR_futex
1735#define __NR_futex 238
1736#endif
1737#ifndef __NR_sched_setaffinity
1738#define __NR_sched_setaffinity 239
1739#endif
1740#ifndef __NR_sched_getaffinity
1741#define __NR_sched_getaffinity 240
1742#endif
1743#ifndef __NR_set_tid_address
1744#define __NR_set_tid_address 252
1745#endif
1746#ifndef __NR_clock_gettime
1747#define __NR_clock_gettime 260
1748#endif
1749#ifndef __NR_clock_getres
1750#define __NR_clock_getres 261
1751#endif
1752#ifndef __NR_statfs64
1753#define __NR_statfs64 265
1754#endif
1755#ifndef __NR_fstatfs64
1756#define __NR_fstatfs64 266
1757#endif
1758#ifndef __NR_ioprio_set
1759#define __NR_ioprio_set 282
1760#endif
1761#ifndef __NR_ioprio_get
1762#define __NR_ioprio_get 283
1763#endif
1764#ifndef __NR_openat
1765#define __NR_openat 288
1766#endif
1767#ifndef __NR_unlinkat
1768#define __NR_unlinkat 294
1769#endif
1770#ifndef __NR_move_pages
1771#define __NR_move_pages 310
1772#endif
1773#ifndef __NR_getcpu
1774#define __NR_getcpu 311
1775#endif
1776#ifndef __NR_fallocate
1777#define __NR_fallocate 314
1778#endif
1779/* Some syscalls are named/numbered differently between s390 and s390x. */
1780#ifdef __s390x__
1781# ifndef __NR_getrlimit
1782# define __NR_getrlimit 191
1783# endif
1784# ifndef __NR_setresuid
1785# define __NR_setresuid 208
1786# endif
1787# ifndef __NR_getresuid
1788# define __NR_getresuid 209
1789# endif
1790# ifndef __NR_setresgid
1791# define __NR_setresgid 210
1792# endif
1793# ifndef __NR_getresgid
1794# define __NR_getresgid 211
1795# endif
1796# ifndef __NR_setfsuid
1797# define __NR_setfsuid 215
1798# endif
1799# ifndef __NR_setfsgid
1800# define __NR_setfsgid 216
1801# endif
1802# ifndef __NR_fadvise64
1803# define __NR_fadvise64 253
1804# endif
1805# ifndef __NR_newfstatat
1806# define __NR_newfstatat 293
1807# endif
1808#else /* __s390x__ */
1809# ifndef __NR_getrlimit
1810# define __NR_getrlimit 76
1811# endif
1812# ifndef __NR_setfsuid
1813# define __NR_setfsuid 138
1814# endif
1815# ifndef __NR_setfsgid
1816# define __NR_setfsgid 139
1817# endif
1818# ifndef __NR_setresuid
1819# define __NR_setresuid 164
1820# endif
1821# ifndef __NR_getresuid
1822# define __NR_getresuid 165
1823# endif
1824# ifndef __NR_setresgid
1825# define __NR_setresgid 170
1826# endif
1827# ifndef __NR_getresgid
1828# define __NR_getresgid 171
1829# endif
1830# ifndef __NR_ugetrlimit
1831# define __NR_ugetrlimit 191
1832# endif
1833# ifndef __NR_mmap2
1834# define __NR_mmap2 192
1835# endif
1836# ifndef __NR_setresuid32
1837# define __NR_setresuid32 208
1838# endif
1839# ifndef __NR_getresuid32
1840# define __NR_getresuid32 209
1841# endif
1842# ifndef __NR_setresgid32
1843# define __NR_setresgid32 210
1844# endif
1845# ifndef __NR_getresgid32
1846# define __NR_getresgid32 211
1847# endif
1848# ifndef __NR_setfsuid32
1849# define __NR_setfsuid32 215
1850# endif
1851# ifndef __NR_setfsgid32
1852# define __NR_setfsgid32 216
1853# endif
1854# ifndef __NR_fadvise64_64
1855# define __NR_fadvise64_64 264
1856# endif
1857# ifndef __NR_fstatat64
1858# define __NR_fstatat64 293
1859# endif
1860#endif /* __s390__ */
1861/* End of s390/s390x definitions */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001862#endif
1863
1864
1865/* After forking, we must make sure to only call system calls. */
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001866#if defined(__BOUNDED_POINTERS__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001867 #error "Need to port invocations of syscalls for bounded ptrs"
1868#else
1869 /* The core dumper and the thread lister get executed after threads
1870 * have been suspended. As a consequence, we cannot call any functions
1871 * that acquire locks. Unfortunately, libc wraps most system calls
1872 * (e.g. in order to implement pthread_atfork, and to make calls
1873 * cancellable), which means we cannot call these functions. Instead,
1874 * we have to call syscall() directly.
1875 */
1876 #undef LSS_ERRNO
1877 #ifdef SYS_ERRNO
1878 /* Allow the including file to override the location of errno. This can
1879 * be useful when using clone() with the CLONE_VM option.
1880 */
1881 #define LSS_ERRNO SYS_ERRNO
1882 #else
1883 #define LSS_ERRNO errno
1884 #endif
1885
1886 #undef LSS_INLINE
1887 #ifdef SYS_INLINE
1888 #define LSS_INLINE SYS_INLINE
1889 #else
1890 #define LSS_INLINE static inline
1891 #endif
1892
1893 /* Allow the including file to override the prefix used for all new
1894 * system calls. By default, it will be set to "sys_".
1895 */
1896 #undef LSS_NAME
1897 #ifndef SYS_PREFIX
1898 #define LSS_NAME(name) sys_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001899 #elif defined(SYS_PREFIX) && SYS_PREFIX < 0
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001900 #define LSS_NAME(name) name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001901 #elif defined(SYS_PREFIX) && SYS_PREFIX == 0
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001902 #define LSS_NAME(name) sys0_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001903 #elif defined(SYS_PREFIX) && SYS_PREFIX == 1
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001904 #define LSS_NAME(name) sys1_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001905 #elif defined(SYS_PREFIX) && SYS_PREFIX == 2
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001906 #define LSS_NAME(name) sys2_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001907 #elif defined(SYS_PREFIX) && SYS_PREFIX == 3
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001908 #define LSS_NAME(name) sys3_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001909 #elif defined(SYS_PREFIX) && SYS_PREFIX == 4
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001910 #define LSS_NAME(name) sys4_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001911 #elif defined(SYS_PREFIX) && SYS_PREFIX == 5
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001912 #define LSS_NAME(name) sys5_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001913 #elif defined(SYS_PREFIX) && SYS_PREFIX == 6
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001914 #define LSS_NAME(name) sys6_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001915 #elif defined(SYS_PREFIX) && SYS_PREFIX == 7
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001916 #define LSS_NAME(name) sys7_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001917 #elif defined(SYS_PREFIX) && SYS_PREFIX == 8
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001918 #define LSS_NAME(name) sys8_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001919 #elif defined(SYS_PREFIX) && SYS_PREFIX == 9
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001920 #define LSS_NAME(name) sys9_##name
1921 #endif
1922
1923 #undef LSS_RETURN
1924 #if (defined(__i386__) || defined(__x86_64__) || defined(__ARM_ARCH_3__) \
Konstantin Ivlev8007b272021-01-27 18:27:42 +03001925 || defined(__ARM_EABI__) || defined(__aarch64__) || defined(__s390__)) \
1926 || defined(__e2k__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001927 /* Failing system calls return a negative result in the range of
1928 * -1..-4095. These are "errno" values with the sign inverted.
1929 */
1930 #define LSS_RETURN(type, res) \
1931 do { \
1932 if ((unsigned long)(res) >= (unsigned long)(-4095)) { \
1933 LSS_ERRNO = -(res); \
1934 res = -1; \
1935 } \
1936 return (type) (res); \
1937 } while (0)
1938 #elif defined(__mips__)
1939 /* On MIPS, failing system calls return -1, and set errno in a
1940 * separate CPU register.
1941 */
1942 #define LSS_RETURN(type, res, err) \
1943 do { \
1944 if (err) { \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00001945 unsigned long __errnovalue = (res); \
1946 LSS_ERRNO = __errnovalue; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001947 res = -1; \
1948 } \
1949 return (type) (res); \
1950 } while (0)
1951 #elif defined(__PPC__)
1952 /* On PPC, failing system calls return -1, and set errno in a
1953 * separate CPU register. See linux/unistd.h.
1954 */
1955 #define LSS_RETURN(type, res, err) \
1956 do { \
1957 if (err & 0x10000000 ) { \
1958 LSS_ERRNO = (res); \
1959 res = -1; \
1960 } \
1961 return (type) (res); \
1962 } while (0)
1963 #endif
1964 #if defined(__i386__)
1965 /* In PIC mode (e.g. when building shared libraries), gcc for i386
1966 * reserves ebx. Unfortunately, most distribution ship with implementations
1967 * of _syscallX() which clobber ebx.
1968 * Also, most definitions of _syscallX() neglect to mark "memory" as being
1969 * clobbered. This causes problems with compilers, that do a better job
1970 * at optimizing across __asm__ calls.
1971 * So, we just have to redefine all of the _syscallX() macros.
1972 */
1973 #undef LSS_ENTRYPOINT
1974 #ifdef SYS_SYSCALL_ENTRYPOINT
1975 static inline void (**LSS_NAME(get_syscall_entrypoint)(void))(void) {
1976 void (**entrypoint)(void);
1977 asm volatile(".bss\n"
1978 ".align 8\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00001979 ".globl " SYS_SYSCALL_ENTRYPOINT "\n"
1980 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001981 ".previous\n"
1982 /* This logically does 'lea "SYS_SYSCALL_ENTRYPOINT", %0' */
1983 "call 0f\n"
1984 "0:pop %0\n"
1985 "add $_GLOBAL_OFFSET_TABLE_+[.-0b], %0\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00001986 "mov " SYS_SYSCALL_ENTRYPOINT "@GOT(%0), %0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001987 : "=r"(entrypoint));
1988 return entrypoint;
1989 }
1990
1991 #define LSS_ENTRYPOINT ".bss\n" \
1992 ".align 8\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00001993 ".globl " SYS_SYSCALL_ENTRYPOINT "\n" \
1994 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001995 ".previous\n" \
1996 /* Check the SYS_SYSCALL_ENTRYPOINT vector */ \
1997 "push %%eax\n" \
1998 "call 10000f\n" \
1999 "10000:pop %%eax\n" \
2000 "add $_GLOBAL_OFFSET_TABLE_+[.-10000b], %%eax\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002001 "mov " SYS_SYSCALL_ENTRYPOINT \
2002 "@GOT(%%eax), %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002003 "mov 0(%%eax), %%eax\n" \
2004 "test %%eax, %%eax\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00002005 "jz 10002f\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002006 "push %%eax\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00002007 "call 10001f\n" \
2008 "10001:pop %%eax\n" \
2009 "add $(10003f-10001b), %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002010 "xchg 4(%%esp), %%eax\n" \
2011 "ret\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00002012 "10002:pop %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002013 "int $0x80\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00002014 "10003:\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002015 #else
2016 #define LSS_ENTRYPOINT "int $0x80\n"
2017 #endif
2018 #undef LSS_BODY
2019 #define LSS_BODY(type,args...) \
2020 long __res; \
2021 __asm__ __volatile__("push %%ebx\n" \
2022 "movl %2,%%ebx\n" \
2023 LSS_ENTRYPOINT \
2024 "pop %%ebx" \
2025 args \
Joshua Perazabe2d5a82020-04-15 14:36:21 -07002026 : "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002027 LSS_RETURN(type,__res)
2028 #undef _syscall0
2029 #define _syscall0(type,name) \
2030 type LSS_NAME(name)(void) { \
2031 long __res; \
2032 __asm__ volatile(LSS_ENTRYPOINT \
2033 : "=a" (__res) \
2034 : "0" (__NR_##name) \
Khem Raj8048ece2018-12-22 16:07:39 -08002035 : "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002036 LSS_RETURN(type,__res); \
2037 }
2038 #undef _syscall1
2039 #define _syscall1(type,name,type1,arg1) \
2040 type LSS_NAME(name)(type1 arg1) { \
2041 LSS_BODY(type, \
2042 : "=a" (__res) \
2043 : "0" (__NR_##name), "ri" ((long)(arg1))); \
2044 }
2045 #undef _syscall2
2046 #define _syscall2(type,name,type1,arg1,type2,arg2) \
2047 type LSS_NAME(name)(type1 arg1,type2 arg2) { \
2048 LSS_BODY(type, \
2049 : "=a" (__res) \
2050 : "0" (__NR_##name),"ri" ((long)(arg1)), "c" ((long)(arg2))); \
2051 }
2052 #undef _syscall3
2053 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
2054 type LSS_NAME(name)(type1 arg1,type2 arg2,type3 arg3) { \
2055 LSS_BODY(type, \
2056 : "=a" (__res) \
2057 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
2058 "d" ((long)(arg3))); \
2059 }
2060 #undef _syscall4
2061 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2062 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2063 LSS_BODY(type, \
2064 : "=a" (__res) \
2065 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
2066 "d" ((long)(arg3)),"S" ((long)(arg4))); \
2067 }
2068 #undef _syscall5
2069 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2070 type5,arg5) \
2071 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2072 type5 arg5) { \
2073 long __res; \
2074 __asm__ __volatile__("push %%ebx\n" \
2075 "movl %2,%%ebx\n" \
2076 "movl %1,%%eax\n" \
2077 LSS_ENTRYPOINT \
2078 "pop %%ebx" \
2079 : "=a" (__res) \
2080 : "i" (__NR_##name), "ri" ((long)(arg1)), \
2081 "c" ((long)(arg2)), "d" ((long)(arg3)), \
2082 "S" ((long)(arg4)), "D" ((long)(arg5)) \
Joshua Perazabe2d5a82020-04-15 14:36:21 -07002083 : "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002084 LSS_RETURN(type,__res); \
2085 }
2086 #undef _syscall6
2087 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2088 type5,arg5,type6,arg6) \
2089 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2090 type5 arg5, type6 arg6) { \
2091 long __res; \
2092 struct { long __a1; long __a6; } __s = { (long)arg1, (long) arg6 }; \
2093 __asm__ __volatile__("push %%ebp\n" \
2094 "push %%ebx\n" \
mseaborn@chromium.orge96ade32012-10-27 17:47:38 +00002095 "movl 4(%2),%%ebp\n" \
2096 "movl 0(%2), %%ebx\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002097 "movl %1,%%eax\n" \
2098 LSS_ENTRYPOINT \
2099 "pop %%ebx\n" \
2100 "pop %%ebp" \
2101 : "=a" (__res) \
2102 : "i" (__NR_##name), "0" ((long)(&__s)), \
2103 "c" ((long)(arg2)), "d" ((long)(arg3)), \
2104 "S" ((long)(arg4)), "D" ((long)(arg5)) \
Joshua Perazabe2d5a82020-04-15 14:36:21 -07002105 : "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002106 LSS_RETURN(type,__res); \
2107 }
2108 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2109 int flags, void *arg, int *parent_tidptr,
2110 void *newtls, int *child_tidptr) {
2111 long __res;
2112 __asm__ __volatile__(/* if (fn == NULL)
2113 * return -EINVAL;
2114 */
2115 "movl %3,%%ecx\n"
2116 "jecxz 1f\n"
2117
2118 /* if (child_stack == NULL)
2119 * return -EINVAL;
2120 */
2121 "movl %4,%%ecx\n"
2122 "jecxz 1f\n"
2123
2124 /* Set up alignment of the child stack:
2125 * child_stack = (child_stack & ~0xF) - 20;
2126 */
2127 "andl $-16,%%ecx\n"
2128 "subl $20,%%ecx\n"
2129
2130 /* Push "arg" and "fn" onto the stack that will be
2131 * used by the child.
2132 */
2133 "movl %6,%%eax\n"
2134 "movl %%eax,4(%%ecx)\n"
2135 "movl %3,%%eax\n"
2136 "movl %%eax,(%%ecx)\n"
2137
2138 /* %eax = syscall(%eax = __NR_clone,
2139 * %ebx = flags,
2140 * %ecx = child_stack,
2141 * %edx = parent_tidptr,
2142 * %esi = newtls,
2143 * %edi = child_tidptr)
2144 * Also, make sure that %ebx gets preserved as it is
2145 * used in PIC mode.
2146 */
2147 "movl %8,%%esi\n"
2148 "movl %7,%%edx\n"
2149 "movl %5,%%eax\n"
2150 "movl %9,%%edi\n"
2151 "pushl %%ebx\n"
2152 "movl %%eax,%%ebx\n"
2153 "movl %2,%%eax\n"
2154 LSS_ENTRYPOINT
2155
2156 /* In the parent: restore %ebx
2157 * In the child: move "fn" into %ebx
2158 */
2159 "popl %%ebx\n"
2160
2161 /* if (%eax != 0)
2162 * return %eax;
2163 */
2164 "test %%eax,%%eax\n"
2165 "jnz 1f\n"
2166
2167 /* In the child, now. Terminate frame pointer chain.
2168 */
2169 "movl $0,%%ebp\n"
2170
2171 /* Call "fn". "arg" is already on the stack.
2172 */
2173 "call *%%ebx\n"
2174
2175 /* Call _exit(%ebx). Unfortunately older versions
2176 * of gcc restrict the number of arguments that can
2177 * be passed to asm(). So, we need to hard-code the
2178 * system call number.
2179 */
2180 "movl %%eax,%%ebx\n"
2181 "movl $1,%%eax\n"
2182 LSS_ENTRYPOINT
2183
2184 /* Return to parent.
2185 */
2186 "1:\n"
2187 : "=a" (__res)
2188 : "0"(-EINVAL), "i"(__NR_clone),
2189 "m"(fn), "m"(child_stack), "m"(flags), "m"(arg),
2190 "m"(parent_tidptr), "m"(newtls), "m"(child_tidptr)
Joshua Perazabe2d5a82020-04-15 14:36:21 -07002191 : "memory", "ecx", "edx", "esi", "edi");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002192 LSS_RETURN(int, __res);
2193 }
2194
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002195 LSS_INLINE _syscall1(int, set_thread_area, void *, u)
2196 LSS_INLINE _syscall1(int, get_thread_area, void *, u)
2197
2198 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
2199 /* On i386, the kernel does not know how to return from a signal
2200 * handler. Instead, it relies on user space to provide a
2201 * restorer function that calls the {rt_,}sigreturn() system call.
2202 * Unfortunately, we cannot just reference the glibc version of this
2203 * function, as glibc goes out of its way to make it inaccessible.
2204 */
2205 void (*res)(void);
2206 __asm__ __volatile__("call 2f\n"
2207 "0:.align 16\n"
2208 "1:movl %1,%%eax\n"
2209 LSS_ENTRYPOINT
2210 "2:popl %0\n"
2211 "addl $(1b-0b),%0\n"
2212 : "=a" (res)
2213 : "i" (__NR_rt_sigreturn));
2214 return res;
2215 }
2216 LSS_INLINE void (*LSS_NAME(restore)(void))(void) {
2217 /* On i386, the kernel does not know how to return from a signal
2218 * handler. Instead, it relies on user space to provide a
2219 * restorer function that calls the {rt_,}sigreturn() system call.
2220 * Unfortunately, we cannot just reference the glibc version of this
2221 * function, as glibc goes out of its way to make it inaccessible.
2222 */
2223 void (*res)(void);
2224 __asm__ __volatile__("call 2f\n"
2225 "0:.align 16\n"
2226 "1:pop %%eax\n"
2227 "movl %1,%%eax\n"
2228 LSS_ENTRYPOINT
2229 "2:popl %0\n"
2230 "addl $(1b-0b),%0\n"
2231 : "=a" (res)
2232 : "i" (__NR_sigreturn));
2233 return res;
2234 }
2235 #elif defined(__x86_64__)
2236 /* There are no known problems with any of the _syscallX() macros
2237 * currently shipping for x86_64, but we still need to be able to define
2238 * our own version so that we can override the location of the errno
2239 * location (e.g. when using the clone() system call with the CLONE_VM
2240 * option).
2241 */
2242 #undef LSS_ENTRYPOINT
2243 #ifdef SYS_SYSCALL_ENTRYPOINT
2244 static inline void (**LSS_NAME(get_syscall_entrypoint)(void))(void) {
2245 void (**entrypoint)(void);
2246 asm volatile(".bss\n"
2247 ".align 8\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002248 ".globl " SYS_SYSCALL_ENTRYPOINT "\n"
2249 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002250 ".previous\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002251 "mov " SYS_SYSCALL_ENTRYPOINT "@GOTPCREL(%%rip), %0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002252 : "=r"(entrypoint));
2253 return entrypoint;
2254 }
2255
2256 #define LSS_ENTRYPOINT \
2257 ".bss\n" \
2258 ".align 8\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002259 ".globl " SYS_SYSCALL_ENTRYPOINT "\n" \
2260 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002261 ".previous\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002262 "mov " SYS_SYSCALL_ENTRYPOINT "@GOTPCREL(%%rip), %%rcx\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002263 "mov 0(%%rcx), %%rcx\n" \
2264 "test %%rcx, %%rcx\n" \
2265 "jz 10001f\n" \
2266 "call *%%rcx\n" \
2267 "jmp 10002f\n" \
2268 "10001:syscall\n" \
2269 "10002:\n"
2270
2271 #else
2272 #define LSS_ENTRYPOINT "syscall\n"
2273 #endif
vapier@chromium.org2273e812013-04-01 17:52:44 +00002274
2275 /* The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
2276 * We need to explicitly cast to an unsigned 64 bit type to avoid implicit
2277 * sign extension. We can't cast pointers directly because those are
2278 * 32 bits, and gcc will dump ugly warnings about casting from a pointer
2279 * to an integer of a different size.
2280 */
2281 #undef LSS_SYSCALL_ARG
2282 #define LSS_SYSCALL_ARG(a) ((uint64_t)(uintptr_t)(a))
2283 #undef _LSS_RETURN
2284 #define _LSS_RETURN(type, res, cast) \
2285 do { \
2286 if ((uint64_t)(res) >= (uint64_t)(-4095)) { \
2287 LSS_ERRNO = -(res); \
2288 res = -1; \
2289 } \
2290 return (type)(cast)(res); \
2291 } while (0)
2292 #undef LSS_RETURN
2293 #define LSS_RETURN(type, res) _LSS_RETURN(type, res, uintptr_t)
2294
2295 #undef _LSS_BODY
2296 #define _LSS_BODY(nr, type, name, cast, ...) \
2297 long long __res; \
2298 __asm__ __volatile__(LSS_BODY_ASM##nr LSS_ENTRYPOINT \
2299 : "=a" (__res) \
2300 : "0" (__NR_##name) LSS_BODY_ARG##nr(__VA_ARGS__) \
2301 : LSS_BODY_CLOBBER##nr "r11", "rcx", "memory"); \
2302 _LSS_RETURN(type, __res, cast)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002303 #undef LSS_BODY
vapier@chromium.org2273e812013-04-01 17:52:44 +00002304 #define LSS_BODY(nr, type, name, args...) \
2305 _LSS_BODY(nr, type, name, uintptr_t, ## args)
2306
2307 #undef LSS_BODY_ASM0
2308 #undef LSS_BODY_ASM1
2309 #undef LSS_BODY_ASM2
2310 #undef LSS_BODY_ASM3
2311 #undef LSS_BODY_ASM4
2312 #undef LSS_BODY_ASM5
2313 #undef LSS_BODY_ASM6
2314 #define LSS_BODY_ASM0
2315 #define LSS_BODY_ASM1 LSS_BODY_ASM0
2316 #define LSS_BODY_ASM2 LSS_BODY_ASM1
2317 #define LSS_BODY_ASM3 LSS_BODY_ASM2
2318 #define LSS_BODY_ASM4 LSS_BODY_ASM3 "movq %5,%%r10;"
2319 #define LSS_BODY_ASM5 LSS_BODY_ASM4 "movq %6,%%r8;"
2320 #define LSS_BODY_ASM6 LSS_BODY_ASM5 "movq %7,%%r9;"
2321
2322 #undef LSS_BODY_CLOBBER0
2323 #undef LSS_BODY_CLOBBER1
2324 #undef LSS_BODY_CLOBBER2
2325 #undef LSS_BODY_CLOBBER3
2326 #undef LSS_BODY_CLOBBER4
2327 #undef LSS_BODY_CLOBBER5
2328 #undef LSS_BODY_CLOBBER6
2329 #define LSS_BODY_CLOBBER0
2330 #define LSS_BODY_CLOBBER1 LSS_BODY_CLOBBER0
2331 #define LSS_BODY_CLOBBER2 LSS_BODY_CLOBBER1
2332 #define LSS_BODY_CLOBBER3 LSS_BODY_CLOBBER2
2333 #define LSS_BODY_CLOBBER4 LSS_BODY_CLOBBER3 "r10",
2334 #define LSS_BODY_CLOBBER5 LSS_BODY_CLOBBER4 "r8",
2335 #define LSS_BODY_CLOBBER6 LSS_BODY_CLOBBER5 "r9",
2336
2337 #undef LSS_BODY_ARG0
2338 #undef LSS_BODY_ARG1
2339 #undef LSS_BODY_ARG2
2340 #undef LSS_BODY_ARG3
2341 #undef LSS_BODY_ARG4
2342 #undef LSS_BODY_ARG5
2343 #undef LSS_BODY_ARG6
2344 #define LSS_BODY_ARG0()
2345 #define LSS_BODY_ARG1(arg1) \
2346 LSS_BODY_ARG0(), "D" (arg1)
2347 #define LSS_BODY_ARG2(arg1, arg2) \
2348 LSS_BODY_ARG1(arg1), "S" (arg2)
2349 #define LSS_BODY_ARG3(arg1, arg2, arg3) \
2350 LSS_BODY_ARG2(arg1, arg2), "d" (arg3)
2351 #define LSS_BODY_ARG4(arg1, arg2, arg3, arg4) \
2352 LSS_BODY_ARG3(arg1, arg2, arg3), "r" (arg4)
2353 #define LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5) \
2354 LSS_BODY_ARG4(arg1, arg2, arg3, arg4), "r" (arg5)
2355 #define LSS_BODY_ARG6(arg1, arg2, arg3, arg4, arg5, arg6) \
2356 LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5), "r" (arg6)
2357
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002358 #undef _syscall0
2359 #define _syscall0(type,name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002360 type LSS_NAME(name)(void) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002361 LSS_BODY(0, type, name); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002362 }
2363 #undef _syscall1
2364 #define _syscall1(type,name,type1,arg1) \
2365 type LSS_NAME(name)(type1 arg1) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002366 LSS_BODY(1, type, name, LSS_SYSCALL_ARG(arg1)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002367 }
2368 #undef _syscall2
2369 #define _syscall2(type,name,type1,arg1,type2,arg2) \
2370 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002371 LSS_BODY(2, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002372 }
2373 #undef _syscall3
2374 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
2375 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002376 LSS_BODY(3, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2377 LSS_SYSCALL_ARG(arg3)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002378 }
2379 #undef _syscall4
2380 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2381 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002382 LSS_BODY(4, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2383 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002384 }
2385 #undef _syscall5
2386 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2387 type5,arg5) \
2388 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2389 type5 arg5) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002390 LSS_BODY(5, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2391 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
2392 LSS_SYSCALL_ARG(arg5)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002393 }
2394 #undef _syscall6
2395 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2396 type5,arg5,type6,arg6) \
2397 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2398 type5 arg5, type6 arg6) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002399 LSS_BODY(6, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2400 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
2401 LSS_SYSCALL_ARG(arg5), LSS_SYSCALL_ARG(arg6));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002402 }
2403 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2404 int flags, void *arg, int *parent_tidptr,
2405 void *newtls, int *child_tidptr) {
vapier@chromium.org2273e812013-04-01 17:52:44 +00002406 long long __res;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002407 {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002408 __asm__ __volatile__(/* if (fn == NULL)
2409 * return -EINVAL;
2410 */
2411 "testq %4,%4\n"
2412 "jz 1f\n"
2413
2414 /* if (child_stack == NULL)
2415 * return -EINVAL;
2416 */
2417 "testq %5,%5\n"
2418 "jz 1f\n"
2419
2420 /* childstack -= 2*sizeof(void *);
2421 */
2422 "subq $16,%5\n"
2423
2424 /* Push "arg" and "fn" onto the stack that will be
2425 * used by the child.
2426 */
2427 "movq %7,8(%5)\n"
2428 "movq %4,0(%5)\n"
2429
2430 /* %rax = syscall(%rax = __NR_clone,
2431 * %rdi = flags,
2432 * %rsi = child_stack,
2433 * %rdx = parent_tidptr,
2434 * %r8 = new_tls,
2435 * %r10 = child_tidptr)
2436 */
2437 "movq %2,%%rax\n"
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00002438 "movq %9,%%r8\n"
2439 "movq %10,%%r10\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002440 LSS_ENTRYPOINT
2441
2442 /* if (%rax != 0)
2443 * return;
2444 */
2445 "testq %%rax,%%rax\n"
2446 "jnz 1f\n"
2447
2448 /* In the child. Terminate frame pointer chain.
2449 */
2450 "xorq %%rbp,%%rbp\n"
2451
2452 /* Call "fn(arg)".
2453 */
2454 "popq %%rax\n"
2455 "popq %%rdi\n"
2456 "call *%%rax\n"
2457
2458 /* Call _exit(%ebx).
2459 */
2460 "movq %%rax,%%rdi\n"
2461 "movq %3,%%rax\n"
2462 LSS_ENTRYPOINT
2463
2464 /* Return to parent.
2465 */
2466 "1:\n"
2467 : "=a" (__res)
2468 : "0"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit),
vapier@chromium.org2273e812013-04-01 17:52:44 +00002469 "r"(LSS_SYSCALL_ARG(fn)),
2470 "S"(LSS_SYSCALL_ARG(child_stack)),
2471 "D"(LSS_SYSCALL_ARG(flags)),
2472 "r"(LSS_SYSCALL_ARG(arg)),
2473 "d"(LSS_SYSCALL_ARG(parent_tidptr)),
2474 "r"(LSS_SYSCALL_ARG(newtls)),
2475 "r"(LSS_SYSCALL_ARG(child_tidptr))
Khem Raj8048ece2018-12-22 16:07:39 -08002476 : "memory", "r8", "r10", "r11", "rcx");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002477 }
2478 LSS_RETURN(int, __res);
2479 }
2480 LSS_INLINE _syscall2(int, arch_prctl, int, c, void *, a)
vapier@chromium.org2273e812013-04-01 17:52:44 +00002481
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002482 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
2483 /* On x86-64, the kernel does not know how to return from
2484 * a signal handler. Instead, it relies on user space to provide a
2485 * restorer function that calls the rt_sigreturn() system call.
2486 * Unfortunately, we cannot just reference the glibc version of this
2487 * function, as glibc goes out of its way to make it inaccessible.
2488 */
vapier@chromium.org2273e812013-04-01 17:52:44 +00002489 long long res;
mseaborn@chromium.org798c2f72013-08-31 00:04:49 +00002490 __asm__ __volatile__("jmp 2f\n"
2491 ".align 16\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002492 "1:movq %1,%%rax\n"
2493 LSS_ENTRYPOINT
mseaborn@chromium.org798c2f72013-08-31 00:04:49 +00002494 "2:leaq 1b(%%rip),%0\n"
2495 : "=r" (res)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002496 : "i" (__NR_rt_sigreturn));
vapier@chromium.org833a10e2013-04-02 19:34:26 +00002497 return (void (*)(void))(uintptr_t)res;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002498 }
2499 #elif defined(__ARM_ARCH_3__)
2500 /* Most definitions of _syscallX() neglect to mark "memory" as being
2501 * clobbered. This causes problems with compilers, that do a better job
2502 * at optimizing across __asm__ calls.
2503 * So, we just have to redefine all of the _syscallX() macros.
2504 */
2505 #undef LSS_REG
2506 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
2507 #undef LSS_BODY
2508 #define LSS_BODY(type,name,args...) \
2509 register long __res_r0 __asm__("r0"); \
2510 long __res; \
2511 __asm__ __volatile__ (__syscall(name) \
2512 : "=r"(__res_r0) : args : "lr", "memory"); \
2513 __res = __res_r0; \
2514 LSS_RETURN(type, __res)
2515 #undef _syscall0
2516 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002517 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002518 LSS_BODY(type, name); \
2519 }
2520 #undef _syscall1
2521 #define _syscall1(type, name, type1, arg1) \
2522 type LSS_NAME(name)(type1 arg1) { \
2523 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2524 }
2525 #undef _syscall2
2526 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2527 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2528 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2529 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2530 }
2531 #undef _syscall3
2532 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2533 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2534 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2535 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2536 }
2537 #undef _syscall4
2538 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2539 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2540 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2541 LSS_REG(3, arg4); \
2542 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2543 }
2544 #undef _syscall5
2545 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2546 type5,arg5) \
2547 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2548 type5 arg5) { \
2549 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2550 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2551 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2552 "r"(__r4)); \
2553 }
2554 #undef _syscall6
2555 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2556 type5,arg5,type6,arg6) \
2557 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2558 type5 arg5, type6 arg6) { \
2559 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2560 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2561 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2562 "r"(__r4), "r"(__r5)); \
2563 }
2564 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2565 int flags, void *arg, int *parent_tidptr,
2566 void *newtls, int *child_tidptr) {
2567 long __res;
2568 {
2569 register int __flags __asm__("r0") = flags;
2570 register void *__stack __asm__("r1") = child_stack;
2571 register void *__ptid __asm__("r2") = parent_tidptr;
2572 register void *__tls __asm__("r3") = newtls;
2573 register int *__ctid __asm__("r4") = child_tidptr;
2574 __asm__ __volatile__(/* if (fn == NULL || child_stack == NULL)
2575 * return -EINVAL;
2576 */
2577 "cmp %2,#0\n"
2578 "cmpne %3,#0\n"
2579 "moveq %0,%1\n"
2580 "beq 1f\n"
2581
2582 /* Push "arg" and "fn" onto the stack that will be
2583 * used by the child.
2584 */
2585 "str %5,[%3,#-4]!\n"
2586 "str %2,[%3,#-4]!\n"
2587
2588 /* %r0 = syscall(%r0 = flags,
2589 * %r1 = child_stack,
2590 * %r2 = parent_tidptr,
2591 * %r3 = newtls,
2592 * %r4 = child_tidptr)
2593 */
2594 __syscall(clone)"\n"
2595
2596 /* if (%r0 != 0)
2597 * return %r0;
2598 */
2599 "movs %0,r0\n"
2600 "bne 1f\n"
2601
2602 /* In the child, now. Call "fn(arg)".
2603 */
2604 "ldr r0,[sp, #4]\n"
2605 "mov lr,pc\n"
2606 "ldr pc,[sp]\n"
2607
2608 /* Call _exit(%r0).
2609 */
2610 __syscall(exit)"\n"
2611 "1:\n"
2612 : "=r" (__res)
2613 : "i"(-EINVAL),
2614 "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2615 "r"(__ptid), "r"(__tls), "r"(__ctid)
2616 : "cc", "lr", "memory");
2617 }
2618 LSS_RETURN(int, __res);
2619 }
2620 #elif defined(__ARM_EABI__)
2621 /* Most definitions of _syscallX() neglect to mark "memory" as being
2622 * clobbered. This causes problems with compilers, that do a better job
2623 * at optimizing across __asm__ calls.
2624 * So, we just have to redefine all fo the _syscallX() macros.
2625 */
2626 #undef LSS_REG
2627 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
2628 #undef LSS_BODY
2629 #define LSS_BODY(type,name,args...) \
2630 register long __res_r0 __asm__("r0"); \
2631 long __res; \
2632 __asm__ __volatile__ ("push {r7}\n" \
2633 "mov r7, %1\n" \
2634 "swi 0x0\n" \
2635 "pop {r7}\n" \
2636 : "=r"(__res_r0) \
2637 : "i"(__NR_##name) , ## args \
2638 : "lr", "memory"); \
2639 __res = __res_r0; \
2640 LSS_RETURN(type, __res)
2641 #undef _syscall0
2642 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002643 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002644 LSS_BODY(type, name); \
2645 }
2646 #undef _syscall1
2647 #define _syscall1(type, name, type1, arg1) \
2648 type LSS_NAME(name)(type1 arg1) { \
2649 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2650 }
2651 #undef _syscall2
2652 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2653 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2654 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2655 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2656 }
2657 #undef _syscall3
2658 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2659 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2660 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2661 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2662 }
2663 #undef _syscall4
2664 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2665 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2666 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2667 LSS_REG(3, arg4); \
2668 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2669 }
2670 #undef _syscall5
2671 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2672 type5,arg5) \
2673 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2674 type5 arg5) { \
2675 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2676 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2677 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2678 "r"(__r4)); \
2679 }
2680 #undef _syscall6
2681 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2682 type5,arg5,type6,arg6) \
2683 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2684 type5 arg5, type6 arg6) { \
2685 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2686 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2687 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2688 "r"(__r4), "r"(__r5)); \
2689 }
2690 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2691 int flags, void *arg, int *parent_tidptr,
2692 void *newtls, int *child_tidptr) {
2693 long __res;
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002694 if (fn == NULL || child_stack == NULL) {
2695 __res = -EINVAL;
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002696 LSS_RETURN(int, __res);
2697 }
2698
2699 /* Push "arg" and "fn" onto the stack that will be
2700 * used by the child.
2701 */
2702 {
2703 uintptr_t* cstack = (uintptr_t*)child_stack - 2;
2704 cstack[0] = (uintptr_t)fn;
2705 cstack[1] = (uintptr_t)arg;
2706 child_stack = cstack;
2707 }
2708 {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002709 register int __flags __asm__("r0") = flags;
2710 register void *__stack __asm__("r1") = child_stack;
2711 register void *__ptid __asm__("r2") = parent_tidptr;
2712 register void *__tls __asm__("r3") = newtls;
2713 register int *__ctid __asm__("r4") = child_tidptr;
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002714 __asm__ __volatile__(
Nico Weber63f24c82017-03-30 13:37:06 -04002715#ifdef __thumb2__
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002716 "push {r7}\n"
Nico Weber63f24c82017-03-30 13:37:06 -04002717#endif
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002718 /* %r0 = syscall(%r0 = flags,
2719 * %r1 = child_stack,
2720 * %r2 = parent_tidptr,
2721 * %r3 = newtls,
2722 * %r4 = child_tidptr)
2723 */
2724 "mov r7, %6\n"
2725 "swi 0x0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002726
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002727 /* if (%r0 != 0)
2728 * return %r0;
2729 */
2730 "cmp r0, #0\n"
2731 "bne 1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002732
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002733 /* In the child, now. Call "fn(arg)".
2734 */
2735 "ldr r0,[sp, #4]\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002736
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002737 "ldr lr,[sp]\n"
2738 "blx lr\n"
zodiac@gmail.com68c659b2011-10-06 05:34:19 +00002739
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002740 /* Call _exit(%r0).
2741 */
2742 "mov r7, %7\n"
2743 "swi 0x0\n"
2744 /* Unreachable */
2745 "bkpt #0\n"
2746 "1:\n"
Nico Weber63f24c82017-03-30 13:37:06 -04002747#ifdef __thumb2__
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002748 "pop {r7}\n"
Nico Weber63f24c82017-03-30 13:37:06 -04002749#endif
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002750 "movs %0,r0\n"
2751 : "=r"(__res)
2752 : "r"(__stack), "r"(__flags), "r"(__ptid), "r"(__tls), "r"(__ctid),
2753 "i"(__NR_clone), "i"(__NR_exit)
2754 : "cc", "lr", "memory"
2755#ifndef __thumb2__
2756 , "r7"
Nico Weber63f24c82017-03-30 13:37:06 -04002757#endif
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002758 );
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002759 }
2760 LSS_RETURN(int, __res);
2761 }
anton@chromium.org2f724fc2014-04-15 13:05:20 +00002762 #elif defined(__aarch64__)
2763 /* Most definitions of _syscallX() neglect to mark "memory" as being
2764 * clobbered. This causes problems with compilers, that do a better job
2765 * at optimizing across __asm__ calls.
2766 * So, we just have to redefine all of the _syscallX() macros.
2767 */
2768 #undef LSS_REG
2769 #define LSS_REG(r,a) register int64_t __r##r __asm__("x"#r) = (int64_t)a
2770 #undef LSS_BODY
2771 #define LSS_BODY(type,name,args...) \
2772 register int64_t __res_x0 __asm__("x0"); \
2773 int64_t __res; \
2774 __asm__ __volatile__ ("mov x8, %1\n" \
2775 "svc 0x0\n" \
2776 : "=r"(__res_x0) \
2777 : "i"(__NR_##name) , ## args \
2778 : "x8", "memory"); \
2779 __res = __res_x0; \
2780 LSS_RETURN(type, __res)
2781 #undef _syscall0
2782 #define _syscall0(type, name) \
2783 type LSS_NAME(name)(void) { \
2784 LSS_BODY(type, name); \
2785 }
2786 #undef _syscall1
2787 #define _syscall1(type, name, type1, arg1) \
2788 type LSS_NAME(name)(type1 arg1) { \
2789 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2790 }
2791 #undef _syscall2
2792 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2793 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2794 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2795 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2796 }
2797 #undef _syscall3
2798 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2799 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2800 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2801 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2802 }
2803 #undef _syscall4
2804 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2805 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2806 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2807 LSS_REG(3, arg4); \
2808 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2809 }
2810 #undef _syscall5
2811 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2812 type5,arg5) \
2813 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2814 type5 arg5) { \
2815 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2816 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2817 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2818 "r"(__r4)); \
2819 }
2820 #undef _syscall6
2821 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2822 type5,arg5,type6,arg6) \
2823 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2824 type5 arg5, type6 arg6) { \
2825 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2826 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2827 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2828 "r"(__r4), "r"(__r5)); \
2829 }
2830
2831 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2832 int flags, void *arg, int *parent_tidptr,
2833 void *newtls, int *child_tidptr) {
2834 int64_t __res;
2835 {
2836 register uint64_t __flags __asm__("x0") = flags;
2837 register void *__stack __asm__("x1") = child_stack;
2838 register void *__ptid __asm__("x2") = parent_tidptr;
2839 register void *__tls __asm__("x3") = newtls;
2840 register int *__ctid __asm__("x4") = child_tidptr;
2841 __asm__ __volatile__(/* Push "arg" and "fn" onto the stack that will be
2842 * used by the child.
2843 */
2844 "stp %1, %4, [%2, #-16]!\n"
2845
2846 /* %x0 = syscall(%x0 = flags,
2847 * %x1 = child_stack,
2848 * %x2 = parent_tidptr,
2849 * %x3 = newtls,
2850 * %x4 = child_tidptr)
2851 */
2852 "mov x8, %8\n"
2853 "svc 0x0\n"
2854
2855 /* if (%r0 != 0)
2856 * return %r0;
2857 */
2858 "mov %0, x0\n"
2859 "cbnz x0, 1f\n"
2860
2861 /* In the child, now. Call "fn(arg)".
2862 */
2863 "ldp x1, x0, [sp], #16\n"
2864 "blr x1\n"
2865
2866 /* Call _exit(%r0).
2867 */
2868 "mov x8, %9\n"
2869 "svc 0x0\n"
2870 "1:\n"
2871 : "=r" (__res)
2872 : "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2873 "r"(__ptid), "r"(__tls), "r"(__ctid),
2874 "i"(__NR_clone), "i"(__NR_exit)
2875 : "cc", "x8", "memory");
2876 }
2877 LSS_RETURN(int, __res);
2878 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002879 #elif defined(__mips__)
2880 #undef LSS_REG
2881 #define LSS_REG(r,a) register unsigned long __r##r __asm__("$"#r) = \
2882 (unsigned long)(a)
2883 #undef LSS_BODY
thestig@chromium.org952107f2014-08-01 02:22:56 +00002884 #undef LSS_SYSCALL_CLOBBERS
2885 #if _MIPS_SIM == _MIPS_SIM_ABI32
2886 #define LSS_SYSCALL_CLOBBERS "$1", "$3", "$8", "$9", "$10", \
2887 "$11", "$12", "$13", "$14", "$15", \
2888 "$24", "$25", "hi", "lo", "memory"
2889 #else
2890 #define LSS_SYSCALL_CLOBBERS "$1", "$3", "$10", "$11", "$12", \
2891 "$13", "$14", "$15", "$24", "$25", \
2892 "hi", "lo", "memory"
2893 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002894 #define LSS_BODY(type,name,r7,...) \
2895 register unsigned long __v0 __asm__("$2") = __NR_##name; \
2896 __asm__ __volatile__ ("syscall\n" \
vapier@chromium.orgda4a4892015-01-22 16:46:39 +00002897 : "=r"(__v0), r7 (__r7) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002898 : "0"(__v0), ##__VA_ARGS__ \
thestig@chromium.org952107f2014-08-01 02:22:56 +00002899 : LSS_SYSCALL_CLOBBERS); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002900 LSS_RETURN(type, __v0, __r7)
2901 #undef _syscall0
2902 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002903 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002904 register unsigned long __r7 __asm__("$7"); \
2905 LSS_BODY(type, name, "=r"); \
2906 }
2907 #undef _syscall1
2908 #define _syscall1(type, name, type1, arg1) \
2909 type LSS_NAME(name)(type1 arg1) { \
2910 register unsigned long __r7 __asm__("$7"); \
2911 LSS_REG(4, arg1); LSS_BODY(type, name, "=r", "r"(__r4)); \
2912 }
2913 #undef _syscall2
2914 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2915 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2916 register unsigned long __r7 __asm__("$7"); \
2917 LSS_REG(4, arg1); LSS_REG(5, arg2); \
2918 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5)); \
2919 }
2920 #undef _syscall3
2921 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2922 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2923 register unsigned long __r7 __asm__("$7"); \
2924 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2925 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5), "r"(__r6)); \
2926 }
2927 #undef _syscall4
2928 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2929 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2930 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2931 LSS_REG(7, arg4); \
2932 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6)); \
2933 }
2934 #undef _syscall5
2935 #if _MIPS_SIM == _MIPS_SIM_ABI32
2936 /* The old 32bit MIPS system call API passes the fifth and sixth argument
2937 * on the stack, whereas the new APIs use registers "r8" and "r9".
2938 */
2939 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2940 type5,arg5) \
2941 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2942 type5 arg5) { \
2943 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2944 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002945 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002946 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002947 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002948 "sw %5, 16($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002949 "syscall\n" \
2950 "addiu $29, 32\n" \
2951 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002952 : "+r"(__v0), "+r" (__r7) \
2953 : "r"(__r4), "r"(__r5), \
2954 "r"(__r6), "r" ((unsigned long)arg5) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002955 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002956 "$13", "$14", "$15", "$24", "$25", \
2957 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002958 LSS_RETURN(type, __v0, __r7); \
2959 }
2960 #else
2961 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2962 type5,arg5) \
2963 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2964 type5 arg5) { \
2965 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2966 LSS_REG(7, arg4); LSS_REG(8, arg5); \
2967 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
2968 "r"(__r8)); \
2969 }
2970 #endif
2971 #undef _syscall6
2972 #if _MIPS_SIM == _MIPS_SIM_ABI32
2973 /* The old 32bit MIPS system call API passes the fifth and sixth argument
2974 * on the stack, whereas the new APIs use registers "r8" and "r9".
2975 */
2976 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2977 type5,arg5,type6,arg6) \
2978 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2979 type5 arg5, type6 arg6) { \
2980 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
2981 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002982 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002983 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002984 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002985 "sw %5, 16($29)\n" \
2986 "sw %6, 20($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002987 "syscall\n" \
2988 "addiu $29, 32\n" \
2989 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002990 : "+r"(__v0), "+r" (__r7) \
2991 : "r"(__r4), "r"(__r5), \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002992 "r"(__r6), "r" ((unsigned long)arg5), \
2993 "r" ((unsigned long)arg6) \
2994 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002995 "$13", "$14", "$15", "$24", "$25", \
2996 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002997 LSS_RETURN(type, __v0, __r7); \
2998 }
2999 #else
3000 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3001 type5,arg5,type6,arg6) \
3002 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3003 type5 arg5,type6 arg6) { \
3004 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
3005 LSS_REG(7, arg4); LSS_REG(8, arg5); LSS_REG(9, arg6); \
3006 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
3007 "r"(__r8), "r"(__r9)); \
3008 }
3009 #endif
3010 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3011 int flags, void *arg, int *parent_tidptr,
3012 void *newtls, int *child_tidptr) {
vapier@chromium.orge0797682015-02-20 20:45:56 +00003013 register unsigned long __v0 __asm__("$2") = -EINVAL;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003014 register unsigned long __r7 __asm__("$7") = (unsigned long)newtls;
3015 {
3016 register int __flags __asm__("$4") = flags;
3017 register void *__stack __asm__("$5") = child_stack;
3018 register void *__ptid __asm__("$6") = parent_tidptr;
3019 register int *__ctid __asm__("$8") = child_tidptr;
3020 __asm__ __volatile__(
3021 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
3022 "subu $29,24\n"
3023 #elif _MIPS_SIM == _MIPS_SIM_NABI32
3024 "sub $29,16\n"
3025 #else
3026 "dsubu $29,16\n"
3027 #endif
3028
3029 /* if (fn == NULL || child_stack == NULL)
3030 * return -EINVAL;
3031 */
vapier@chromium.orge0797682015-02-20 20:45:56 +00003032 "beqz %4,1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003033 "beqz %5,1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003034
3035 /* Push "arg" and "fn" onto the stack that will be
3036 * used by the child.
3037 */
3038 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
vapier@chromium.orge0797682015-02-20 20:45:56 +00003039 "subu %5,32\n"
3040 "sw %4,0(%5)\n"
3041 "sw %7,4(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003042 #elif _MIPS_SIM == _MIPS_SIM_NABI32
vapier@chromium.orge0797682015-02-20 20:45:56 +00003043 "sub %5,32\n"
3044 "sw %4,0(%5)\n"
3045 "sw %7,8(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003046 #else
vapier@chromium.orge0797682015-02-20 20:45:56 +00003047 "dsubu %5,32\n"
3048 "sd %4,0(%5)\n"
3049 "sd %7,8(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003050 #endif
3051
3052 /* $7 = syscall($4 = flags,
3053 * $5 = child_stack,
3054 * $6 = parent_tidptr,
3055 * $7 = newtls,
3056 * $8 = child_tidptr)
3057 */
vapier@chromium.orge0797682015-02-20 20:45:56 +00003058 "li $2,%2\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003059 "syscall\n"
3060
3061 /* if ($7 != 0)
3062 * return $2;
3063 */
3064 "bnez $7,1f\n"
3065 "bnez $2,1f\n"
3066
3067 /* In the child, now. Call "fn(arg)".
3068 */
3069 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
3070 "lw $25,0($29)\n"
3071 "lw $4,4($29)\n"
3072 #elif _MIPS_SIM == _MIPS_SIM_NABI32
3073 "lw $25,0($29)\n"
3074 "lw $4,8($29)\n"
3075 #else
3076 "ld $25,0($29)\n"
3077 "ld $4,8($29)\n"
3078 #endif
3079 "jalr $25\n"
3080
3081 /* Call _exit($2)
3082 */
3083 "move $4,$2\n"
vapier@chromium.orge0797682015-02-20 20:45:56 +00003084 "li $2,%3\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003085 "syscall\n"
3086
3087 "1:\n"
3088 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
3089 "addu $29, 24\n"
3090 #elif _MIPS_SIM == _MIPS_SIM_NABI32
3091 "add $29, 16\n"
3092 #else
3093 "daddu $29,16\n"
3094 #endif
petarj@mips.com0ece1c62013-04-10 00:28:04 +00003095 : "+r" (__v0), "+r" (__r7)
vapier@chromium.orge0797682015-02-20 20:45:56 +00003096 : "i"(__NR_clone), "i"(__NR_exit), "r"(fn),
3097 "r"(__stack), "r"(__flags), "r"(arg),
3098 "r"(__ptid), "r"(__ctid)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003099 : "$9", "$10", "$11", "$12", "$13", "$14", "$15",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003100 "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003101 }
3102 LSS_RETURN(int, __v0, __r7);
3103 }
3104 #elif defined (__PPC__)
3105 #undef LSS_LOADARGS_0
3106 #define LSS_LOADARGS_0(name, dummy...) \
3107 __sc_0 = __NR_##name
3108 #undef LSS_LOADARGS_1
3109 #define LSS_LOADARGS_1(name, arg1) \
3110 LSS_LOADARGS_0(name); \
3111 __sc_3 = (unsigned long) (arg1)
3112 #undef LSS_LOADARGS_2
3113 #define LSS_LOADARGS_2(name, arg1, arg2) \
3114 LSS_LOADARGS_1(name, arg1); \
3115 __sc_4 = (unsigned long) (arg2)
3116 #undef LSS_LOADARGS_3
3117 #define LSS_LOADARGS_3(name, arg1, arg2, arg3) \
3118 LSS_LOADARGS_2(name, arg1, arg2); \
3119 __sc_5 = (unsigned long) (arg3)
3120 #undef LSS_LOADARGS_4
3121 #define LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4) \
3122 LSS_LOADARGS_3(name, arg1, arg2, arg3); \
3123 __sc_6 = (unsigned long) (arg4)
3124 #undef LSS_LOADARGS_5
3125 #define LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5) \
3126 LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4); \
3127 __sc_7 = (unsigned long) (arg5)
3128 #undef LSS_LOADARGS_6
3129 #define LSS_LOADARGS_6(name, arg1, arg2, arg3, arg4, arg5, arg6) \
3130 LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5); \
3131 __sc_8 = (unsigned long) (arg6)
3132 #undef LSS_ASMINPUT_0
3133 #define LSS_ASMINPUT_0 "0" (__sc_0)
3134 #undef LSS_ASMINPUT_1
3135 #define LSS_ASMINPUT_1 LSS_ASMINPUT_0, "1" (__sc_3)
3136 #undef LSS_ASMINPUT_2
3137 #define LSS_ASMINPUT_2 LSS_ASMINPUT_1, "2" (__sc_4)
3138 #undef LSS_ASMINPUT_3
3139 #define LSS_ASMINPUT_3 LSS_ASMINPUT_2, "3" (__sc_5)
3140 #undef LSS_ASMINPUT_4
3141 #define LSS_ASMINPUT_4 LSS_ASMINPUT_3, "4" (__sc_6)
3142 #undef LSS_ASMINPUT_5
3143 #define LSS_ASMINPUT_5 LSS_ASMINPUT_4, "5" (__sc_7)
3144 #undef LSS_ASMINPUT_6
3145 #define LSS_ASMINPUT_6 LSS_ASMINPUT_5, "6" (__sc_8)
3146 #undef LSS_BODY
3147 #define LSS_BODY(nr, type, name, args...) \
3148 long __sc_ret, __sc_err; \
3149 { \
3150 register unsigned long __sc_0 __asm__ ("r0"); \
3151 register unsigned long __sc_3 __asm__ ("r3"); \
3152 register unsigned long __sc_4 __asm__ ("r4"); \
3153 register unsigned long __sc_5 __asm__ ("r5"); \
3154 register unsigned long __sc_6 __asm__ ("r6"); \
3155 register unsigned long __sc_7 __asm__ ("r7"); \
3156 register unsigned long __sc_8 __asm__ ("r8"); \
3157 \
3158 LSS_LOADARGS_##nr(name, args); \
3159 __asm__ __volatile__ \
3160 ("sc\n\t" \
3161 "mfcr %0" \
3162 : "=&r" (__sc_0), \
3163 "=&r" (__sc_3), "=&r" (__sc_4), \
3164 "=&r" (__sc_5), "=&r" (__sc_6), \
3165 "=&r" (__sc_7), "=&r" (__sc_8) \
3166 : LSS_ASMINPUT_##nr \
3167 : "cr0", "ctr", "memory", \
3168 "r9", "r10", "r11", "r12"); \
3169 __sc_ret = __sc_3; \
3170 __sc_err = __sc_0; \
3171 } \
3172 LSS_RETURN(type, __sc_ret, __sc_err)
3173 #undef _syscall0
3174 #define _syscall0(type, name) \
3175 type LSS_NAME(name)(void) { \
3176 LSS_BODY(0, type, name); \
3177 }
3178 #undef _syscall1
3179 #define _syscall1(type, name, type1, arg1) \
3180 type LSS_NAME(name)(type1 arg1) { \
3181 LSS_BODY(1, type, name, arg1); \
3182 }
3183 #undef _syscall2
3184 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3185 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3186 LSS_BODY(2, type, name, arg1, arg2); \
3187 }
3188 #undef _syscall3
3189 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3190 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3191 LSS_BODY(3, type, name, arg1, arg2, arg3); \
3192 }
3193 #undef _syscall4
3194 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3195 type4, arg4) \
3196 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3197 LSS_BODY(4, type, name, arg1, arg2, arg3, arg4); \
3198 }
3199 #undef _syscall5
3200 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3201 type4, arg4, type5, arg5) \
3202 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3203 type5 arg5) { \
3204 LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5); \
3205 }
3206 #undef _syscall6
3207 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3208 type4, arg4, type5, arg5, type6, arg6) \
3209 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3210 type5 arg5, type6 arg6) { \
3211 LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6); \
3212 }
3213 /* clone function adapted from glibc 2.3.6 clone.S */
3214 /* TODO(csilvers): consider wrapping some args up in a struct, like we
3215 * do for i386's _syscall6, so we can compile successfully on gcc 2.95
3216 */
3217 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3218 int flags, void *arg, int *parent_tidptr,
3219 void *newtls, int *child_tidptr) {
3220 long __ret, __err;
3221 {
3222 register int (*__fn)(void *) __asm__ ("r8") = fn;
3223 register void *__cstack __asm__ ("r4") = child_stack;
3224 register int __flags __asm__ ("r3") = flags;
3225 register void * __arg __asm__ ("r9") = arg;
3226 register int * __ptidptr __asm__ ("r5") = parent_tidptr;
3227 register void * __newtls __asm__ ("r6") = newtls;
3228 register int * __ctidptr __asm__ ("r7") = child_tidptr;
3229 __asm__ __volatile__(
3230 /* check for fn == NULL
3231 * and child_stack == NULL
3232 */
3233 "cmpwi cr0, %6, 0\n\t"
3234 "cmpwi cr1, %7, 0\n\t"
3235 "cror cr0*4+eq, cr1*4+eq, cr0*4+eq\n\t"
3236 "beq- cr0, 1f\n\t"
3237
3238 /* set up stack frame for child */
3239 "clrrwi %7, %7, 4\n\t"
3240 "li 0, 0\n\t"
3241 "stwu 0, -16(%7)\n\t"
3242
3243 /* fn, arg, child_stack are saved across the syscall: r28-30 */
3244 "mr 28, %6\n\t"
3245 "mr 29, %7\n\t"
3246 "mr 27, %9\n\t"
3247
3248 /* syscall */
3249 "li 0, %4\n\t"
3250 /* flags already in r3
3251 * child_stack already in r4
3252 * ptidptr already in r5
3253 * newtls already in r6
3254 * ctidptr already in r7
3255 */
3256 "sc\n\t"
3257
3258 /* Test if syscall was successful */
3259 "cmpwi cr1, 3, 0\n\t"
3260 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
3261 "bne- cr1, 1f\n\t"
3262
3263 /* Do the function call */
3264 "mtctr 28\n\t"
3265 "mr 3, 27\n\t"
3266 "bctrl\n\t"
3267
3268 /* Call _exit(r3) */
3269 "li 0, %5\n\t"
3270 "sc\n\t"
3271
3272 /* Return to parent */
3273 "1:\n"
3274 "mfcr %1\n\t"
3275 "mr %0, 3\n\t"
3276 : "=r" (__ret), "=r" (__err)
3277 : "0" (-1), "1" (EINVAL),
3278 "i" (__NR_clone), "i" (__NR_exit),
3279 "r" (__fn), "r" (__cstack), "r" (__flags),
3280 "r" (__arg), "r" (__ptidptr), "r" (__newtls),
3281 "r" (__ctidptr)
3282 : "cr0", "cr1", "memory", "ctr",
3283 "r0", "r29", "r27", "r28");
3284 }
3285 LSS_RETURN(int, __ret, __err);
3286 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04003287 #elif defined(__s390__)
3288 #undef LSS_REG
3289 #define LSS_REG(r, a) register unsigned long __r##r __asm__("r"#r) = (unsigned long) a
3290 #undef LSS_BODY
3291 #define LSS_BODY(type, name, args...) \
3292 register unsigned long __nr __asm__("r1") \
3293 = (unsigned long)(__NR_##name); \
3294 register long __res_r2 __asm__("r2"); \
3295 long __res; \
3296 __asm__ __volatile__ \
3297 ("svc 0\n\t" \
3298 : "=d"(__res_r2) \
3299 : "d"(__nr), ## args \
3300 : "memory"); \
3301 __res = __res_r2; \
3302 LSS_RETURN(type, __res)
3303 #undef _syscall0
3304 #define _syscall0(type, name) \
3305 type LSS_NAME(name)(void) { \
3306 LSS_BODY(type, name); \
3307 }
3308 #undef _syscall1
3309 #define _syscall1(type, name, type1, arg1) \
3310 type LSS_NAME(name)(type1 arg1) { \
3311 LSS_REG(2, arg1); \
3312 LSS_BODY(type, name, "0"(__r2)); \
3313 }
3314 #undef _syscall2
3315 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3316 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3317 LSS_REG(2, arg1); LSS_REG(3, arg2); \
3318 LSS_BODY(type, name, "0"(__r2), "d"(__r3)); \
3319 }
3320 #undef _syscall3
3321 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3322 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3323 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3324 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4)); \
3325 }
3326 #undef _syscall4
3327 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3328 type4, arg4) \
3329 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3330 type4 arg4) { \
3331 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3332 LSS_REG(5, arg4); \
3333 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3334 "d"(__r5)); \
3335 }
3336 #undef _syscall5
3337 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3338 type4, arg4, type5, arg5) \
3339 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3340 type4 arg4, type5 arg5) { \
3341 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3342 LSS_REG(5, arg4); LSS_REG(6, arg5); \
3343 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3344 "d"(__r5), "d"(__r6)); \
3345 }
3346 #undef _syscall6
3347 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3348 type4, arg4, type5, arg5, type6, arg6) \
3349 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3350 type4 arg4, type5 arg5, type6 arg6) { \
3351 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3352 LSS_REG(5, arg4); LSS_REG(6, arg5); LSS_REG(7, arg6); \
3353 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3354 "d"(__r5), "d"(__r6), "d"(__r7)); \
3355 }
3356 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3357 int flags, void *arg, int *parent_tidptr,
3358 void *newtls, int *child_tidptr) {
3359 long __ret;
3360 {
3361 register int (*__fn)(void *) __asm__ ("r1") = fn;
3362 register void *__cstack __asm__ ("r2") = child_stack;
3363 register int __flags __asm__ ("r3") = flags;
3364 register void *__arg __asm__ ("r0") = arg;
3365 register int *__ptidptr __asm__ ("r4") = parent_tidptr;
3366 register void *__newtls __asm__ ("r6") = newtls;
3367 register int *__ctidptr __asm__ ("r5") = child_tidptr;
3368 __asm__ __volatile__ (
3369 #ifndef __s390x__
3370 /* arg already in r0 */
3371 "ltr %4, %4\n\t" /* check fn, which is already in r1 */
3372 "jz 1f\n\t" /* NULL function pointer, return -EINVAL */
3373 "ltr %5, %5\n\t" /* check child_stack, which is already in r2 */
3374 "jz 1f\n\t" /* NULL stack pointer, return -EINVAL */
3375 /* flags already in r3 */
3376 /* parent_tidptr already in r4 */
3377 /* child_tidptr already in r5 */
3378 /* newtls already in r6 */
3379 "svc %2\n\t" /* invoke clone syscall */
3380 "ltr %0,%%r2\n\t" /* load return code into __ret and test */
3381 "jnz 1f\n\t" /* return to parent if non-zero */
3382 /* start child thread */
3383 "lr %%r2, %7\n\t" /* set first parameter to void *arg */
3384 "ahi %%r15, -96\n\t" /* make room on the stack for the save area */
3385 "xc 0(4,%%r15), 0(%%r15)\n\t"
3386 "basr %%r14, %4\n\t" /* jump to fn */
3387 "svc %3\n" /* invoke exit syscall */
3388 "1:\n"
3389 #else
3390 /* arg already in r0 */
3391 "ltgr %4, %4\n\t" /* check fn, which is already in r1 */
3392 "jz 1f\n\t" /* NULL function pointer, return -EINVAL */
3393 "ltgr %5, %5\n\t" /* check child_stack, which is already in r2 */
3394 "jz 1f\n\t" /* NULL stack pointer, return -EINVAL */
3395 /* flags already in r3 */
3396 /* parent_tidptr already in r4 */
3397 /* child_tidptr already in r5 */
3398 /* newtls already in r6 */
3399 "svc %2\n\t" /* invoke clone syscall */
3400 "ltgr %0, %%r2\n\t" /* load return code into __ret and test */
3401 "jnz 1f\n\t" /* return to parent if non-zero */
3402 /* start child thread */
3403 "lgr %%r2, %7\n\t" /* set first parameter to void *arg */
3404 "aghi %%r15, -160\n\t" /* make room on the stack for the save area */
3405 "xc 0(8,%%r15), 0(%%r15)\n\t"
3406 "basr %%r14, %4\n\t" /* jump to fn */
3407 "svc %3\n" /* invoke exit syscall */
3408 "1:\n"
3409 #endif
3410 : "=r" (__ret)
3411 : "0" (-EINVAL), "i" (__NR_clone), "i" (__NR_exit),
3412 "d" (__fn), "d" (__cstack), "d" (__flags), "d" (__arg),
3413 "d" (__ptidptr), "d" (__newtls), "d" (__ctidptr)
3414 : "cc", "r14", "memory"
3415 );
3416 }
3417 LSS_RETURN(int, __ret);
3418 }
Konstantin Ivlev8007b272021-01-27 18:27:42 +03003419 #elif defined(__e2k__)
3420
3421 #undef _LSS_BODY
3422 #define _LSS_BODY(nr, type, name, ...) \
3423 register unsigned long long __res; \
3424 __asm__ __volatile__ \
3425 ( \
3426 "{\n\t" \
3427 " sdisp %%ctpr1, 0x3\n\t" \
3428 " addd, s 0x0, %[sys_num], %%b[0]\n\t" \
3429 LSS_BODY_ASM##nr \
3430 "}\n\t" \
3431 "{\n\t" \
3432 " call %%ctpr1, wbs = %#\n\t" \
3433 "}\n\t" \
3434 "{\n\t" \
3435 " addd, s 0x0, %%b[0], %[res]\n\t" \
3436 "}\n\t" \
3437 : [res] "=r" (__res) \
3438 : \
3439 LSS_BODY_ARG##nr(__VA_ARGS__) \
3440 [sys_num] "ri" (__NR_##name) \
3441 : "ctpr1", "ctpr2", "ctpr3", \
3442 "b[0]", "b[1]", "b[2]", "b[3]", \
3443 "b[4]", "b[5]", "b[6]", "b[7]" \
3444 ); \
3445 LSS_RETURN(type, __res);
3446
3447 #undef LSS_BODY
3448 #define LSS_BODY(nr, type, name, args...) \
3449 _LSS_BODY(nr, type, name, ## args)
3450
3451 #undef LSS_BODY_ASM0
3452 #undef LSS_BODY_ASM1
3453 #undef LSS_BODY_ASM2
3454 #undef LSS_BODY_ASM3
3455 #undef LSS_BODY_ASM4
3456 #undef LSS_BODY_ASM5
3457 #undef LSS_BODY_ASM6
3458
3459 #define LSS_BODY_ASM0
3460 #define LSS_BODY_ASM1 LSS_BODY_ASM0 \
3461 " addd, s 0x0, %[arg1], %%b[1]\n\t"
3462 #define LSS_BODY_ASM2 LSS_BODY_ASM1 \
3463 " addd, s 0x0, %[arg2], %%b[2]\n\t"
3464 #define LSS_BODY_ASM3 LSS_BODY_ASM2 \
3465 " addd, s 0x0, %[arg3], %%b[3]\n\t"
3466 #define LSS_BODY_ASM4 LSS_BODY_ASM3 \
3467 " addd, s 0x0, %[arg4], %%b[4]\n\t"
3468 #define LSS_BODY_ASM5 LSS_BODY_ASM4 \
3469 " addd, s 0x0, %[arg5], %%b[5]\n\t"
3470 #define LSS_BODY_ASM6 LSS_BODY_ASM5 \
3471 "}\n\t" \
3472 "{\n\t" \
3473 " addd, s 0x0, %[arg6], %%b[6]\n\t"
3474
3475 #undef LSS_SYSCALL_ARG
3476 #define LSS_SYSCALL_ARG(a) ((unsigned long long)(uintptr_t)(a))
3477
3478 #undef LSS_BODY_ARG0
3479 #undef LSS_BODY_ARG1
3480 #undef LSS_BODY_ARG2
3481 #undef LSS_BODY_ARG3
3482 #undef LSS_BODY_ARG4
3483 #undef LSS_BODY_ARG5
3484 #undef LSS_BODY_ARG6
3485
3486 #define LSS_BODY_ARG0()
3487 #define LSS_BODY_ARG1(_arg1) \
3488 [arg1] "ri" LSS_SYSCALL_ARG(_arg1),
3489 #define LSS_BODY_ARG2(_arg1, _arg2) \
3490 LSS_BODY_ARG1(_arg1) \
3491 [arg2] "ri" LSS_SYSCALL_ARG(_arg2),
3492 #define LSS_BODY_ARG3(_arg1, _arg2, _arg3) \
3493 LSS_BODY_ARG2(_arg1, _arg2) \
3494 [arg3] "ri" LSS_SYSCALL_ARG(_arg3),
3495 #define LSS_BODY_ARG4(_arg1, _arg2, _arg3, _arg4) \
3496 LSS_BODY_ARG3(_arg1, _arg2, _arg3) \
3497 [arg4] "ri" LSS_SYSCALL_ARG(_arg4),
3498 #define LSS_BODY_ARG5(_arg1, _arg2, _arg3, _arg4, _arg5) \
3499 LSS_BODY_ARG4(_arg1, _arg2, _arg3, _arg4) \
3500 [arg5] "ri" LSS_SYSCALL_ARG(_arg5),
3501 #define LSS_BODY_ARG6(_arg1, _arg2, _arg3, _arg4, _arg5, _arg6) \
3502 LSS_BODY_ARG5(_arg1, _arg2, _arg3, _arg4, _arg5) \
3503 [arg6] "ri" LSS_SYSCALL_ARG(_arg6),
3504
3505 #undef _syscall0
3506 #define _syscall0(type, name) \
3507 type LSS_NAME(name)(void) { \
3508 LSS_BODY(0, type, name); \
3509 }
3510
3511 #undef _syscall1
3512 #define _syscall1(type, name, type1, arg1) \
3513 type LSS_NAME(name)(type1 arg1) { \
3514 LSS_BODY(1, type, name, arg1) \
3515 }
3516
3517 #undef _syscall2
3518 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3519 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3520 LSS_BODY(2, type, name, arg1, arg2) \
3521 }
3522
3523 #undef _syscall3
3524 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3525 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3526 LSS_BODY(3, type, name, arg1, arg2, arg3) \
3527 }
3528
3529 #undef _syscall4
3530 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3531 type4, arg4) \
3532 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3533 LSS_BODY(4, type, name, arg1, arg2, arg3, arg4) \
3534 }
3535
3536 #undef _syscall5
3537 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3538 type4, arg4, type5, arg5) \
3539 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3540 type5 arg5) { \
3541 LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5) \
3542 }
3543
3544 #undef _syscall6
3545 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3546 type4, arg4, type5, arg5, type6, arg6) \
3547 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3548 type5 arg5, type6 arg6) { \
3549 LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6) \
3550 }
3551
3552 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3553 int flags, void *arg, int *parent_tidptr,
3554 void *newtls, int *child_tidptr) {
3555 unsigned long long __res;
3556
3557 __asm__ __volatile__ (
3558 "{\n\t"
3559 " addd,s 0x0, %[nr_clone], %%b[0]\n\t"
3560 " addd,s 0x0, %[flags], %%db[1]\n\t"
3561 " addd,s 0x0, %[child_stack], %%db[2]\n\t"
3562 " addd,s 0x0, %[parent_tidptr], %%db[3]\n\t"
3563 " addd,s 0x0, %[child_tidptr], %%db[4]\n\t"
3564 " addd,s 0x0, %[newtls], %%db[5]\n\t"
3565 "}\n\t"
3566 /* if (fn == NULL)
3567 * return -EINVAL;
3568 */
3569
3570 "{\n\t"
3571 " disp %%ctpr1, .L1\n\t"
3572 "}\n\t"
3573 "{\n\t"
3574 " cmpesb,s 0x0, %[fn], %%pred0\n\t"
3575 "}\n\t"
3576 "{\n\t"
3577 " ct %%ctpr1 ? %%pred0\n\t"
3578 "}\n\t"
3579
3580 /* if (child_stack == NULL)
3581 * return -EINVAL;
3582 */
3583 "{\n\t"
3584 " cmpesb,s 0x0, %%db[2], %%pred0\n\t"
3585 "}\n\t"
3586 "{\n\t"
3587 " ct %%ctpr1 ? %%pred0\n\t"
3588 "}\n\t"
3589
3590 /* b[0] = syscall(%b[0] = __NR_clone,
3591 * %db[1] = flags,
3592 * %db[2] = child_stack,
3593 * %db[3] = parent_tidptr,
3594 * %db[4] = child_tidptr,
3595 * %db[5] = newtls)
3596 */
3597 "{\n\t"
3598 " sdisp %%ctpr1, 0x3\n\t"
3599 "}\n\t"
3600 "{\n\t"
3601 " call %%ctpr1, wbs = %#\n\t"
3602 "}\n\t"
3603
3604 /* if (%[b0] != 0)
3605 * return %b[0];
3606 */
3607 "{\n\t"
3608 " disp %%ctpr1, .L2\n\t"
3609 " cmpesb,s 0x0, %%b[0], %%pred0\n\t"
3610 "}\n\t"
3611 "{\n\t"
3612 " ct %%ctpr1 ? ~%%pred0\n\t"
3613 "}\n\t"
3614 /* In the child, now. Call "fn(arg)".
3615 */
3616
3617 "{\n\t"
3618 " movtd,s %[fn], %%ctpr1\n\t"
3619 "}\n\t"
3620 "{\n\t"
3621 " addd,s 0x0, %[arg], %%db[0]\n\t"
3622 "}\n\t"
3623 "{\n\t"
3624 " call %%ctpr1, wbs = %#\n\t"
3625 "}\n\t"
3626 /* Call _exit(%b[0]).
3627 */
3628
3629 "{\n\t"
3630 " sdisp %%ctpr1, 0x3\n\t"
3631 " addd,s 0x0, %%b[0], %%b[1]\n\t"
3632 "}\n\t"
3633 "{\n\t"
3634 " addd,s 0x0, %[nr_exit], %%b[0]\n\t"
3635 "}\n\t"
3636 "{\n\t"
3637 " call %%ctpr1, wbs = %#\n\t"
3638 "}\n\t"
3639 "{\n\t"
3640 " disp %%ctpr1, .L2\n\t"
3641 " adds,s 0x0, 0x0, %%b[0]\n\t"
3642 "}\n\t"
3643 "{\n\t"
3644 " ct %%ctpr1\n\t"
3645 "}\n\t"
3646 ".L1:\n\t"
3647 "{\n\t"
3648 " addd,s 0x0, %[einval], %%b[0]\n\t"
3649 "}\n\t"
3650 ".L2:\n\t"
3651 "{\n\t"
3652 " addd,s 0x0, %%b[0], %[res]\n\t"
3653 "}\n\t"
3654 : [res] "=r" LSS_SYSCALL_ARG(__res)
3655 : [nr_clone] "ri" LSS_SYSCALL_ARG(__NR_clone)
3656 [arg] "ri" LSS_SYSCALL_ARG(arg)
3657 [nr_exit] "ri" LSS_SYSCALL_ARG(__NR_exit)
3658 [flags] "ri" LSS_SYSCALL_ARG(flags)
3659 [child_stack] "ri" LSS_SYSCALL_ARG(child_stack)
3660 [parent_tidptr] "ri"
3661 LSS_SYSCALL_ARG(parent_tidptr)
3662 [newtls] "ri" LSS_SYSCALL_ARG(newtls)
3663 [child_tidptr] "ri"
3664 LSS_SYSCALL_ARG(child_tidptr)
3665 [fn] "ri" LSS_SYSCALL_ARG(fn)
3666 [einval] "ri" LSS_SYSCALL_ARG(-EINVAL)
3667 : "ctpr1", "b[0]", "b[1]", "b[2]", "b[3]",
3668 "b[4]", "b[5]", "pred0");
3669 LSS_RETURN(int, __res);
3670 }
3671
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003672 #endif
3673 #define __NR__exit __NR_exit
3674 #define __NR__gettid __NR_gettid
3675 #define __NR__mremap __NR_mremap
phosek@chromium.orga9c02722013-08-16 17:31:42 +00003676 LSS_INLINE _syscall1(void *, brk, void *, e)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003677 LSS_INLINE _syscall1(int, chdir, const char *,p)
3678 LSS_INLINE _syscall1(int, close, int, f)
3679 LSS_INLINE _syscall2(int, clock_getres, int, c,
3680 struct kernel_timespec*, t)
3681 LSS_INLINE _syscall2(int, clock_gettime, int, c,
3682 struct kernel_timespec*, t)
3683 LSS_INLINE _syscall1(int, dup, int, f)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003684 #if defined(__NR_dup2)
3685 // dup2 is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003686 LSS_INLINE _syscall2(int, dup2, int, s,
3687 int, d)
3688 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003689 #if defined(__NR_dup3)
3690 LSS_INLINE _syscall3(int, dup3, int, s, int, d, int, f)
3691 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003692 LSS_INLINE _syscall3(int, execve, const char*, f,
3693 const char*const*,a,const char*const*, e)
3694 LSS_INLINE _syscall1(int, _exit, int, e)
3695 LSS_INLINE _syscall1(int, exit_group, int, e)
3696 LSS_INLINE _syscall3(int, fcntl, int, f,
3697 int, c, long, a)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003698 #if defined(__NR_fork)
3699 // fork is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003700 LSS_INLINE _syscall0(pid_t, fork)
3701 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003702 LSS_INLINE _syscall2(int, fstat, int, f,
3703 struct kernel_stat*, b)
3704 LSS_INLINE _syscall2(int, fstatfs, int, f,
3705 struct kernel_statfs*, b)
vapier@chromium.org2273e812013-04-01 17:52:44 +00003706 #if defined(__x86_64__)
3707 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
3708 LSS_INLINE int LSS_NAME(ftruncate)(int f, off_t l) {
3709 LSS_BODY(2, int, ftruncate, LSS_SYSCALL_ARG(f), (uint64_t)(l));
3710 }
3711 #else
3712 LSS_INLINE _syscall2(int, ftruncate, int, f,
3713 off_t, l)
3714 #endif
Mike Frysinger171a36a2019-01-26 23:05:43 -05003715 LSS_INLINE _syscall6(int, futex, int*, u,
3716 int, o, int, v,
3717 struct kernel_timespec*, t,
3718 int*, u2, int, v2)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003719 LSS_INLINE _syscall3(int, getdents, int, f,
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003720 struct kernel_dirent*, d, int, c)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003721 LSS_INLINE _syscall3(int, getdents64, int, f,
3722 struct kernel_dirent64*, d, int, c)
3723 LSS_INLINE _syscall0(gid_t, getegid)
3724 LSS_INLINE _syscall0(uid_t, geteuid)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003725 #if defined(__NR_getpgrp)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003726 LSS_INLINE _syscall0(pid_t, getpgrp)
3727 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003728 LSS_INLINE _syscall0(pid_t, getpid)
3729 LSS_INLINE _syscall0(pid_t, getppid)
3730 LSS_INLINE _syscall2(int, getpriority, int, a,
3731 int, b)
3732 LSS_INLINE _syscall3(int, getresgid, gid_t *, r,
3733 gid_t *, e, gid_t *, s)
3734 LSS_INLINE _syscall3(int, getresuid, uid_t *, r,
3735 uid_t *, e, uid_t *, s)
3736#if !defined(__ARM_EABI__)
3737 LSS_INLINE _syscall2(int, getrlimit, int, r,
3738 struct kernel_rlimit*, l)
3739#endif
3740 LSS_INLINE _syscall1(pid_t, getsid, pid_t, p)
3741 LSS_INLINE _syscall0(pid_t, _gettid)
3742 LSS_INLINE _syscall2(pid_t, gettimeofday, struct kernel_timeval*, t,
3743 void*, tz)
3744 LSS_INLINE _syscall5(int, setxattr, const char *,p,
3745 const char *, n, const void *,v,
3746 size_t, s, int, f)
3747 LSS_INLINE _syscall5(int, lsetxattr, const char *,p,
3748 const char *, n, const void *,v,
3749 size_t, s, int, f)
3750 LSS_INLINE _syscall4(ssize_t, getxattr, const char *,p,
3751 const char *, n, void *, v, size_t, s)
3752 LSS_INLINE _syscall4(ssize_t, lgetxattr, const char *,p,
3753 const char *, n, void *, v, size_t, s)
3754 LSS_INLINE _syscall3(ssize_t, listxattr, const char *,p,
3755 char *, l, size_t, s)
3756 LSS_INLINE _syscall3(ssize_t, llistxattr, const char *,p,
3757 char *, l, size_t, s)
3758 LSS_INLINE _syscall3(int, ioctl, int, d,
3759 int, r, void *, a)
3760 LSS_INLINE _syscall2(int, ioprio_get, int, which,
3761 int, who)
3762 LSS_INLINE _syscall3(int, ioprio_set, int, which,
3763 int, who, int, ioprio)
3764 LSS_INLINE _syscall2(int, kill, pid_t, p,
3765 int, s)
vapier@chromium.org2273e812013-04-01 17:52:44 +00003766 #if defined(__x86_64__)
3767 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
3768 LSS_INLINE off_t LSS_NAME(lseek)(int f, off_t o, int w) {
3769 _LSS_BODY(3, off_t, lseek, off_t, LSS_SYSCALL_ARG(f), (uint64_t)(o),
3770 LSS_SYSCALL_ARG(w));
3771 }
3772 #else
3773 LSS_INLINE _syscall3(off_t, lseek, int, f,
3774 off_t, o, int, w)
3775 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003776 LSS_INLINE _syscall2(int, munmap, void*, s,
3777 size_t, l)
3778 LSS_INLINE _syscall6(long, move_pages, pid_t, p,
3779 unsigned long, n, void **,g, int *, d,
3780 int *, s, int, f)
3781 LSS_INLINE _syscall3(int, mprotect, const void *,a,
3782 size_t, l, int, p)
3783 LSS_INLINE _syscall5(void*, _mremap, void*, o,
3784 size_t, os, size_t, ns,
3785 unsigned long, f, void *, a)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003786 #if defined(__NR_open)
3787 // open is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003788 LSS_INLINE _syscall3(int, open, const char*, p,
3789 int, f, int, m)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003790 #endif
3791 #if defined(__NR_poll)
3792 // poll is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003793 LSS_INLINE _syscall3(int, poll, struct kernel_pollfd*, u,
3794 unsigned int, n, int, t)
3795 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003796 #if defined(__NR_ppoll)
3797 LSS_INLINE _syscall5(int, ppoll, struct kernel_pollfd *, u,
3798 unsigned int, n, const struct kernel_timespec *, t,
3799 const struct kernel_sigset_t *, sigmask, size_t, s)
3800 #endif
mseaborn@chromium.orge6c76822013-08-31 00:08:44 +00003801 LSS_INLINE _syscall5(int, prctl, int, option,
3802 unsigned long, arg2,
3803 unsigned long, arg3,
3804 unsigned long, arg4,
3805 unsigned long, arg5)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003806 LSS_INLINE _syscall4(long, ptrace, int, r,
3807 pid_t, p, void *, a, void *, d)
3808 #if defined(__NR_quotactl)
3809 // Defined on x86_64 / i386 only
3810 LSS_INLINE _syscall4(int, quotactl, int, cmd, const char *, special,
3811 int, id, caddr_t, addr)
3812 #endif
3813 LSS_INLINE _syscall3(ssize_t, read, int, f,
3814 void *, b, size_t, c)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003815 #if defined(__NR_readlink)
3816 // readlink is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003817 LSS_INLINE _syscall3(int, readlink, const char*, p,
3818 char*, b, size_t, s)
3819 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003820 #if defined(__NR_readlinkat)
3821 LSS_INLINE _syscall4(int, readlinkat, int, d, const char *, p, char *, b,
3822 size_t, s)
3823 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003824 LSS_INLINE _syscall4(int, rt_sigaction, int, s,
3825 const struct kernel_sigaction*, a,
3826 struct kernel_sigaction*, o, size_t, c)
3827 LSS_INLINE _syscall2(int, rt_sigpending, struct kernel_sigset_t *, s,
3828 size_t, c)
3829 LSS_INLINE _syscall4(int, rt_sigprocmask, int, h,
3830 const struct kernel_sigset_t*, s,
3831 struct kernel_sigset_t*, o, size_t, c)
3832 LSS_INLINE _syscall2(int, rt_sigsuspend,
3833 const struct kernel_sigset_t*, s, size_t, c)
Joshua Peraza726d71e2019-11-13 12:21:13 -08003834 LSS_INLINE _syscall4(int, rt_sigtimedwait, const struct kernel_sigset_t*, s,
3835 siginfo_t*, i, const struct timespec*, t, size_t, c)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003836 LSS_INLINE _syscall3(int, sched_getaffinity,pid_t, p,
3837 unsigned int, l, unsigned long *, m)
3838 LSS_INLINE _syscall3(int, sched_setaffinity,pid_t, p,
3839 unsigned int, l, unsigned long *, m)
3840 LSS_INLINE _syscall0(int, sched_yield)
3841 LSS_INLINE _syscall1(long, set_tid_address, int *, t)
3842 LSS_INLINE _syscall1(int, setfsgid, gid_t, g)
3843 LSS_INLINE _syscall1(int, setfsuid, uid_t, u)
3844 LSS_INLINE _syscall1(int, setuid, uid_t, u)
3845 LSS_INLINE _syscall1(int, setgid, gid_t, g)
3846 LSS_INLINE _syscall2(int, setpgid, pid_t, p,
3847 pid_t, g)
3848 LSS_INLINE _syscall3(int, setpriority, int, a,
3849 int, b, int, p)
3850 LSS_INLINE _syscall3(int, setresgid, gid_t, r,
3851 gid_t, e, gid_t, s)
3852 LSS_INLINE _syscall3(int, setresuid, uid_t, r,
3853 uid_t, e, uid_t, s)
3854 LSS_INLINE _syscall2(int, setrlimit, int, r,
3855 const struct kernel_rlimit*, l)
3856 LSS_INLINE _syscall0(pid_t, setsid)
3857 LSS_INLINE _syscall2(int, sigaltstack, const stack_t*, s,
3858 const stack_t*, o)
3859 #if defined(__NR_sigreturn)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003860 LSS_INLINE _syscall1(int, sigreturn, unsigned long, u)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003861 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003862 #if defined(__NR_stat)
3863 // stat is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003864 LSS_INLINE _syscall2(int, stat, const char*, f,
3865 struct kernel_stat*, b)
3866 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003867 LSS_INLINE _syscall2(int, statfs, const char*, f,
3868 struct kernel_statfs*, b)
3869 LSS_INLINE _syscall3(int, tgkill, pid_t, p,
3870 pid_t, t, int, s)
3871 LSS_INLINE _syscall2(int, tkill, pid_t, p,
3872 int, s)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003873 #if defined(__NR_unlink)
3874 // unlink is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00003875 LSS_INLINE _syscall1(int, unlink, const char*, f)
3876 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003877 LSS_INLINE _syscall3(ssize_t, write, int, f,
3878 const void *, b, size_t, c)
3879 LSS_INLINE _syscall3(ssize_t, writev, int, f,
3880 const struct kernel_iovec*, v, size_t, c)
3881 #if defined(__NR_getcpu)
3882 LSS_INLINE _syscall3(long, getcpu, unsigned *, cpu,
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00003883 unsigned *, node, void *, unused)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003884 #endif
Konstantin Ivlev8007b272021-01-27 18:27:42 +03003885 #if defined(__x86_64__) || defined(__e2k__) || \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003886 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32)
3887 LSS_INLINE _syscall3(int, recvmsg, int, s,
3888 struct kernel_msghdr*, m, int, f)
3889 LSS_INLINE _syscall3(int, sendmsg, int, s,
3890 const struct kernel_msghdr*, m, int, f)
3891 LSS_INLINE _syscall6(int, sendto, int, s,
3892 const void*, m, size_t, l,
3893 int, f,
3894 const struct kernel_sockaddr*, a, int, t)
3895 LSS_INLINE _syscall2(int, shutdown, int, s,
3896 int, h)
3897 LSS_INLINE _syscall3(int, socket, int, d,
3898 int, t, int, p)
3899 LSS_INLINE _syscall4(int, socketpair, int, d,
3900 int, t, int, p, int*, s)
3901 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003902 #if defined(__NR_fadvise64)
3903 #if defined(__x86_64__)
3904 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
3905 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset, loff_t len,
3906 int advice) {
3907 LSS_BODY(4, int, fadvise64, LSS_SYSCALL_ARG(fd), (uint64_t)(offset),
3908 (uint64_t)(len), LSS_SYSCALL_ARG(advice));
3909 }
3910 #else
3911 LSS_INLINE _syscall4(int, fadvise64,
3912 int, fd, loff_t, offset, loff_t, len, int, advice)
3913 #endif
3914 #elif defined(__i386__)
3915 #define __NR__fadvise64_64 __NR_fadvise64_64
3916 LSS_INLINE _syscall6(int, _fadvise64_64, int, fd,
3917 unsigned, offset_lo, unsigned, offset_hi,
3918 unsigned, len_lo, unsigned, len_hi,
3919 int, advice)
3920
3921 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
3922 loff_t len, int advice) {
3923 return LSS_NAME(_fadvise64_64)(fd,
3924 (unsigned)offset, (unsigned)(offset >>32),
3925 (unsigned)len, (unsigned)(len >> 32),
3926 advice);
3927 }
3928
3929 #elif defined(__s390__) && !defined(__s390x__)
3930 #define __NR__fadvise64_64 __NR_fadvise64_64
3931 struct kernel_fadvise64_64_args {
3932 int fd;
3933 long long offset;
3934 long long len;
3935 int advice;
3936 };
3937
3938 LSS_INLINE _syscall1(int, _fadvise64_64,
3939 struct kernel_fadvise64_64_args *args)
3940
3941 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
3942 loff_t len, int advice) {
3943 struct kernel_fadvise64_64_args args = { fd, offset, len, advice };
3944 return LSS_NAME(_fadvise64_64)(&args);
3945 }
3946 #endif
3947 #if defined(__NR_fallocate)
3948 #if defined(__x86_64__)
vapier@chromium.org2273e812013-04-01 17:52:44 +00003949 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
3950 LSS_INLINE int LSS_NAME(fallocate)(int f, int mode, loff_t offset,
3951 loff_t len) {
3952 LSS_BODY(4, int, fallocate, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(mode),
3953 (uint64_t)(offset), (uint64_t)(len));
3954 }
Joshua Peraza7bde79c2019-12-05 11:36:48 -08003955 #elif (defined(__i386__) || (defined(__s390__) && !defined(__s390x__)) \
3956 || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) \
3957 || (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) \
3958 || defined(__PPC__))
Bryan Chan3f6478a2016-06-14 08:38:17 -04003959 #define __NR__fallocate __NR_fallocate
3960 LSS_INLINE _syscall6(int, _fallocate, int, fd,
3961 int, mode,
3962 unsigned, offset_lo, unsigned, offset_hi,
3963 unsigned, len_lo, unsigned, len_hi)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003964
Bryan Chan3f6478a2016-06-14 08:38:17 -04003965 LSS_INLINE int LSS_NAME(fallocate)(int fd, int mode,
3966 loff_t offset, loff_t len) {
3967 union { loff_t off; unsigned w[2]; } o = { offset }, l = { len };
3968 return LSS_NAME(_fallocate)(fd, mode, o.w[0], o.w[1], l.w[0], l.w[1]);
3969 }
3970 #else
3971 LSS_INLINE _syscall4(int, fallocate,
3972 int, f, int, mode, loff_t, offset, loff_t, len)
3973 #endif
3974 #endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07003975 #if defined(__NR_getrandom)
3976 LSS_INLINE _syscall3(ssize_t, getrandom, void*, buffer, size_t, length,
3977 unsigned int, flags)
3978 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04003979 #if defined(__NR_newfstatat)
3980 LSS_INLINE _syscall4(int, newfstatat, int, d,
3981 const char *, p,
3982 struct kernel_stat*, b, int, f)
3983 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04003984 #if defined(__x86_64__) || defined(__s390x__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003985 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
3986 gid_t *egid,
3987 gid_t *sgid) {
3988 return LSS_NAME(getresgid)(rgid, egid, sgid);
3989 }
3990
3991 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
3992 uid_t *euid,
3993 uid_t *suid) {
3994 return LSS_NAME(getresuid)(ruid, euid, suid);
3995 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003996
3997 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
3998 return LSS_NAME(setfsgid)(gid);
3999 }
4000
4001 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
4002 return LSS_NAME(setfsuid)(uid);
4003 }
4004
4005 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
4006 return LSS_NAME(setresgid)(rgid, egid, sgid);
4007 }
4008
4009 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
4010 return LSS_NAME(setresuid)(ruid, euid, suid);
4011 }
4012
4013 LSS_INLINE int LSS_NAME(sigaction)(int signum,
4014 const struct kernel_sigaction *act,
4015 struct kernel_sigaction *oldact) {
Bryan Chan3f6478a2016-06-14 08:38:17 -04004016 #if defined(__x86_64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004017 /* On x86_64, the kernel requires us to always set our own
4018 * SA_RESTORER in order to be able to return from a signal handler.
4019 * This function must have a "magic" signature that the "gdb"
4020 * (and maybe the kernel?) can recognize.
4021 */
4022 if (act != NULL && !(act->sa_flags & SA_RESTORER)) {
4023 struct kernel_sigaction a = *act;
4024 a.sa_flags |= SA_RESTORER;
4025 a.sa_restorer = LSS_NAME(restore_rt)();
4026 return LSS_NAME(rt_sigaction)(signum, &a, oldact,
4027 (KERNEL_NSIG+7)/8);
Bryan Chan3f6478a2016-06-14 08:38:17 -04004028 } else
4029 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004030 return LSS_NAME(rt_sigaction)(signum, act, oldact,
4031 (KERNEL_NSIG+7)/8);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004032 }
4033
4034 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
4035 return LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
4036 }
4037
Joshua Peraza726d71e2019-11-13 12:21:13 -08004038 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
4039 return LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
4040 }
4041 #endif
4042 #if defined(__NR_rt_sigprocmask)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004043 LSS_INLINE int LSS_NAME(sigprocmask)(int how,
4044 const struct kernel_sigset_t *set,
4045 struct kernel_sigset_t *oldset) {
4046 return LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
4047 }
Joshua Peraza726d71e2019-11-13 12:21:13 -08004048 #endif
4049 #if defined(__NR_rt_sigtimedwait)
4050 LSS_INLINE int LSS_NAME(sigtimedwait)(const struct kernel_sigset_t *set,
4051 siginfo_t *info,
4052 const struct timespec *timeout) {
4053 return LSS_NAME(rt_sigtimedwait)(set, info, timeout, (KERNEL_NSIG+7)/8);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004054 }
4055 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004056 #if defined(__NR_wait4)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004057 LSS_INLINE _syscall4(pid_t, wait4, pid_t, p,
4058 int*, s, int, o,
4059 struct kernel_rusage*, r)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004060 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004061 #if defined(__NR_openat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004062 LSS_INLINE _syscall4(int, openat, int, d, const char *, p, int, f, int, m)
Bryan Chan3f6478a2016-06-14 08:38:17 -04004063 #endif
4064 #if defined(__NR_unlinkat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004065 LSS_INLINE _syscall3(int, unlinkat, int, d, const char *, p, int, f)
4066 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004067 #if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
4068 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004069 #define __NR__getresgid32 __NR_getresgid32
4070 #define __NR__getresuid32 __NR_getresuid32
4071 #define __NR__setfsgid32 __NR_setfsgid32
4072 #define __NR__setfsuid32 __NR_setfsuid32
4073 #define __NR__setresgid32 __NR_setresgid32
4074 #define __NR__setresuid32 __NR_setresuid32
4075#if defined(__ARM_EABI__)
4076 LSS_INLINE _syscall2(int, ugetrlimit, int, r,
4077 struct kernel_rlimit*, l)
4078#endif
4079 LSS_INLINE _syscall3(int, _getresgid32, gid_t *, r,
4080 gid_t *, e, gid_t *, s)
4081 LSS_INLINE _syscall3(int, _getresuid32, uid_t *, r,
4082 uid_t *, e, uid_t *, s)
4083 LSS_INLINE _syscall1(int, _setfsgid32, gid_t, f)
4084 LSS_INLINE _syscall1(int, _setfsuid32, uid_t, f)
4085 LSS_INLINE _syscall3(int, _setresgid32, gid_t, r,
4086 gid_t, e, gid_t, s)
4087 LSS_INLINE _syscall3(int, _setresuid32, uid_t, r,
4088 uid_t, e, uid_t, s)
4089
4090 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
4091 gid_t *egid,
4092 gid_t *sgid) {
4093 int rc;
4094 if ((rc = LSS_NAME(_getresgid32)(rgid, egid, sgid)) < 0 &&
4095 LSS_ERRNO == ENOSYS) {
4096 if ((rgid == NULL) || (egid == NULL) || (sgid == NULL)) {
4097 return EFAULT;
4098 }
4099 // Clear the high bits first, since getresgid only sets 16 bits
4100 *rgid = *egid = *sgid = 0;
4101 rc = LSS_NAME(getresgid)(rgid, egid, sgid);
4102 }
4103 return rc;
4104 }
4105
4106 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
4107 uid_t *euid,
4108 uid_t *suid) {
4109 int rc;
4110 if ((rc = LSS_NAME(_getresuid32)(ruid, euid, suid)) < 0 &&
4111 LSS_ERRNO == ENOSYS) {
4112 if ((ruid == NULL) || (euid == NULL) || (suid == NULL)) {
4113 return EFAULT;
4114 }
4115 // Clear the high bits first, since getresuid only sets 16 bits
4116 *ruid = *euid = *suid = 0;
4117 rc = LSS_NAME(getresuid)(ruid, euid, suid);
4118 }
4119 return rc;
4120 }
4121
4122 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
4123 int rc;
4124 if ((rc = LSS_NAME(_setfsgid32)(gid)) < 0 &&
4125 LSS_ERRNO == ENOSYS) {
4126 if ((unsigned int)gid & ~0xFFFFu) {
4127 rc = EINVAL;
4128 } else {
4129 rc = LSS_NAME(setfsgid)(gid);
4130 }
4131 }
4132 return rc;
4133 }
4134
4135 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
4136 int rc;
4137 if ((rc = LSS_NAME(_setfsuid32)(uid)) < 0 &&
4138 LSS_ERRNO == ENOSYS) {
4139 if ((unsigned int)uid & ~0xFFFFu) {
4140 rc = EINVAL;
4141 } else {
4142 rc = LSS_NAME(setfsuid)(uid);
4143 }
4144 }
4145 return rc;
4146 }
4147
4148 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
4149 int rc;
4150 if ((rc = LSS_NAME(_setresgid32)(rgid, egid, sgid)) < 0 &&
4151 LSS_ERRNO == ENOSYS) {
4152 if ((unsigned int)rgid & ~0xFFFFu ||
4153 (unsigned int)egid & ~0xFFFFu ||
4154 (unsigned int)sgid & ~0xFFFFu) {
4155 rc = EINVAL;
4156 } else {
4157 rc = LSS_NAME(setresgid)(rgid, egid, sgid);
4158 }
4159 }
4160 return rc;
4161 }
4162
4163 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
4164 int rc;
4165 if ((rc = LSS_NAME(_setresuid32)(ruid, euid, suid)) < 0 &&
4166 LSS_ERRNO == ENOSYS) {
4167 if ((unsigned int)ruid & ~0xFFFFu ||
4168 (unsigned int)euid & ~0xFFFFu ||
4169 (unsigned int)suid & ~0xFFFFu) {
4170 rc = EINVAL;
4171 } else {
4172 rc = LSS_NAME(setresuid)(ruid, euid, suid);
4173 }
4174 }
4175 return rc;
4176 }
4177 #endif
4178 LSS_INLINE int LSS_NAME(sigemptyset)(struct kernel_sigset_t *set) {
4179 memset(&set->sig, 0, sizeof(set->sig));
4180 return 0;
4181 }
4182
4183 LSS_INLINE int LSS_NAME(sigfillset)(struct kernel_sigset_t *set) {
4184 memset(&set->sig, -1, sizeof(set->sig));
4185 return 0;
4186 }
4187
4188 LSS_INLINE int LSS_NAME(sigaddset)(struct kernel_sigset_t *set,
4189 int signum) {
4190 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
4191 LSS_ERRNO = EINVAL;
4192 return -1;
4193 } else {
4194 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
4195 |= 1UL << ((signum - 1) % (8*sizeof(set->sig[0])));
4196 return 0;
4197 }
4198 }
4199
4200 LSS_INLINE int LSS_NAME(sigdelset)(struct kernel_sigset_t *set,
4201 int signum) {
4202 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
4203 LSS_ERRNO = EINVAL;
4204 return -1;
4205 } else {
4206 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
4207 &= ~(1UL << ((signum - 1) % (8*sizeof(set->sig[0]))));
4208 return 0;
4209 }
4210 }
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004211
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004212 LSS_INLINE int LSS_NAME(sigismember)(struct kernel_sigset_t *set,
4213 int signum) {
4214 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
4215 LSS_ERRNO = EINVAL;
4216 return -1;
4217 } else {
4218 return !!(set->sig[(signum - 1)/(8*sizeof(set->sig[0]))] &
4219 (1UL << ((signum - 1) % (8*sizeof(set->sig[0])))));
4220 }
4221 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04004222 #if defined(__i386__) || \
4223 defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
4224 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
4225 defined(__PPC__) || \
Konstantin Ivlev8007b272021-01-27 18:27:42 +03004226 (defined(__s390__) && !defined(__s390x__)) || defined(__e2k__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004227 #define __NR__sigaction __NR_sigaction
4228 #define __NR__sigpending __NR_sigpending
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004229 #define __NR__sigsuspend __NR_sigsuspend
4230 #define __NR__socketcall __NR_socketcall
4231 LSS_INLINE _syscall2(int, fstat64, int, f,
4232 struct kernel_stat64 *, b)
zodiac@gmail.com4f470182010-10-13 03:47:54 +00004233 LSS_INLINE _syscall5(int, _llseek, uint, fd,
4234 unsigned long, hi, unsigned long, lo,
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004235 loff_t *, res, uint, wh)
Bryan Chan3f6478a2016-06-14 08:38:17 -04004236#if defined(__s390__) && !defined(__s390x__)
4237 /* On s390, mmap2() arguments are passed in memory. */
4238 LSS_INLINE void* LSS_NAME(_mmap2)(void *s, size_t l, int p, int f, int d,
4239 off_t o) {
4240 unsigned long buf[6] = { (unsigned long) s, (unsigned long) l,
4241 (unsigned long) p, (unsigned long) f,
4242 (unsigned long) d, (unsigned long) o };
4243 LSS_REG(2, buf);
4244 LSS_BODY(void*, mmap2, "0"(__r2));
4245 }
4246#else
4247 #define __NR__mmap2 __NR_mmap2
4248 LSS_INLINE _syscall6(void*, _mmap2, void*, s,
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004249 size_t, l, int, p,
4250 int, f, int, d,
Bryan Chan3f6478a2016-06-14 08:38:17 -04004251 off_t, o)
4252#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004253 LSS_INLINE _syscall3(int, _sigaction, int, s,
4254 const struct kernel_old_sigaction*, a,
4255 struct kernel_old_sigaction*, o)
4256 LSS_INLINE _syscall1(int, _sigpending, unsigned long*, s)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004257 #ifdef __PPC__
4258 LSS_INLINE _syscall1(int, _sigsuspend, unsigned long, s)
4259 #else
4260 LSS_INLINE _syscall3(int, _sigsuspend, const void*, a,
4261 int, b,
4262 unsigned long, s)
4263 #endif
4264 LSS_INLINE _syscall2(int, stat64, const char *, p,
4265 struct kernel_stat64 *, b)
4266
4267 LSS_INLINE int LSS_NAME(sigaction)(int signum,
4268 const struct kernel_sigaction *act,
4269 struct kernel_sigaction *oldact) {
4270 int old_errno = LSS_ERRNO;
4271 int rc;
4272 struct kernel_sigaction a;
4273 if (act != NULL) {
4274 a = *act;
4275 #ifdef __i386__
4276 /* On i386, the kernel requires us to always set our own
4277 * SA_RESTORER when using realtime signals. Otherwise, it does not
4278 * know how to return from a signal handler. This function must have
4279 * a "magic" signature that the "gdb" (and maybe the kernel?) can
4280 * recognize.
4281 * Apparently, a SA_RESTORER is implicitly set by the kernel, when
4282 * using non-realtime signals.
4283 *
4284 * TODO: Test whether ARM needs a restorer
4285 */
4286 if (!(a.sa_flags & SA_RESTORER)) {
4287 a.sa_flags |= SA_RESTORER;
4288 a.sa_restorer = (a.sa_flags & SA_SIGINFO)
4289 ? LSS_NAME(restore_rt)() : LSS_NAME(restore)();
4290 }
4291 #endif
4292 }
4293 rc = LSS_NAME(rt_sigaction)(signum, act ? &a : act, oldact,
4294 (KERNEL_NSIG+7)/8);
4295 if (rc < 0 && LSS_ERRNO == ENOSYS) {
4296 struct kernel_old_sigaction oa, ooa, *ptr_a = &oa, *ptr_oa = &ooa;
4297 if (!act) {
4298 ptr_a = NULL;
4299 } else {
4300 oa.sa_handler_ = act->sa_handler_;
4301 memcpy(&oa.sa_mask, &act->sa_mask, sizeof(oa.sa_mask));
4302 #ifndef __mips__
4303 oa.sa_restorer = act->sa_restorer;
4304 #endif
4305 oa.sa_flags = act->sa_flags;
4306 }
4307 if (!oldact) {
4308 ptr_oa = NULL;
4309 }
4310 LSS_ERRNO = old_errno;
4311 rc = LSS_NAME(_sigaction)(signum, ptr_a, ptr_oa);
4312 if (rc == 0 && oldact) {
4313 if (act) {
4314 memcpy(oldact, act, sizeof(*act));
4315 } else {
4316 memset(oldact, 0, sizeof(*oldact));
4317 }
4318 oldact->sa_handler_ = ptr_oa->sa_handler_;
4319 oldact->sa_flags = ptr_oa->sa_flags;
4320 memcpy(&oldact->sa_mask, &ptr_oa->sa_mask, sizeof(ptr_oa->sa_mask));
4321 #ifndef __mips__
4322 oldact->sa_restorer = ptr_oa->sa_restorer;
4323 #endif
4324 }
4325 }
4326 return rc;
4327 }
4328
4329 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
4330 int old_errno = LSS_ERRNO;
4331 int rc = LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
4332 if (rc < 0 && LSS_ERRNO == ENOSYS) {
4333 LSS_ERRNO = old_errno;
4334 LSS_NAME(sigemptyset)(set);
4335 rc = LSS_NAME(_sigpending)(&set->sig[0]);
4336 }
4337 return rc;
4338 }
4339
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004340 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
4341 int olderrno = LSS_ERRNO;
4342 int rc = LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
4343 if (rc < 0 && LSS_ERRNO == ENOSYS) {
4344 LSS_ERRNO = olderrno;
4345 rc = LSS_NAME(_sigsuspend)(
4346 #ifndef __PPC__
4347 set, 0,
4348 #endif
4349 set->sig[0]);
4350 }
4351 return rc;
4352 }
4353 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004354 #if defined(__i386__) || \
4355 defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
4356 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
4357 defined(__PPC__) || \
4358 (defined(__s390__) && !defined(__s390x__))
4359 /* On these architectures, implement mmap() with mmap2(). */
4360 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4361 int64_t o) {
4362 if (o % 4096) {
4363 LSS_ERRNO = EINVAL;
4364 return (void *) -1;
4365 }
4366 return LSS_NAME(_mmap2)(s, l, p, f, d, (o / 4096));
4367 }
4368 #elif defined(__s390x__)
4369 /* On s390x, mmap() arguments are passed in memory. */
4370 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4371 int64_t o) {
4372 unsigned long buf[6] = { (unsigned long) s, (unsigned long) l,
4373 (unsigned long) p, (unsigned long) f,
4374 (unsigned long) d, (unsigned long) o };
4375 LSS_REG(2, buf);
4376 LSS_BODY(void*, mmap, "0"(__r2));
4377 }
4378 #elif defined(__x86_64__)
4379 /* Need to make sure __off64_t isn't truncated to 32-bits under x32. */
4380 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4381 int64_t o) {
4382 LSS_BODY(6, void*, mmap, LSS_SYSCALL_ARG(s), LSS_SYSCALL_ARG(l),
4383 LSS_SYSCALL_ARG(p), LSS_SYSCALL_ARG(f),
4384 LSS_SYSCALL_ARG(d), (uint64_t)(o));
4385 }
4386 #else
4387 /* Remaining 64-bit architectures. */
4388 LSS_INLINE _syscall6(void*, mmap, void*, addr, size_t, length, int, prot,
4389 int, flags, int, fd, int64_t, offset)
4390 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004391 #if defined(__PPC__)
4392 #undef LSS_SC_LOADARGS_0
4393 #define LSS_SC_LOADARGS_0(dummy...)
4394 #undef LSS_SC_LOADARGS_1
4395 #define LSS_SC_LOADARGS_1(arg1) \
4396 __sc_4 = (unsigned long) (arg1)
4397 #undef LSS_SC_LOADARGS_2
4398 #define LSS_SC_LOADARGS_2(arg1, arg2) \
4399 LSS_SC_LOADARGS_1(arg1); \
4400 __sc_5 = (unsigned long) (arg2)
4401 #undef LSS_SC_LOADARGS_3
4402 #define LSS_SC_LOADARGS_3(arg1, arg2, arg3) \
4403 LSS_SC_LOADARGS_2(arg1, arg2); \
4404 __sc_6 = (unsigned long) (arg3)
4405 #undef LSS_SC_LOADARGS_4
4406 #define LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4) \
4407 LSS_SC_LOADARGS_3(arg1, arg2, arg3); \
4408 __sc_7 = (unsigned long) (arg4)
4409 #undef LSS_SC_LOADARGS_5
4410 #define LSS_SC_LOADARGS_5(arg1, arg2, arg3, arg4, arg5) \
4411 LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4); \
4412 __sc_8 = (unsigned long) (arg5)
4413 #undef LSS_SC_BODY
4414 #define LSS_SC_BODY(nr, type, opt, args...) \
4415 long __sc_ret, __sc_err; \
4416 { \
4417 register unsigned long __sc_0 __asm__ ("r0") = __NR_socketcall; \
4418 register unsigned long __sc_3 __asm__ ("r3") = opt; \
4419 register unsigned long __sc_4 __asm__ ("r4"); \
4420 register unsigned long __sc_5 __asm__ ("r5"); \
4421 register unsigned long __sc_6 __asm__ ("r6"); \
4422 register unsigned long __sc_7 __asm__ ("r7"); \
4423 register unsigned long __sc_8 __asm__ ("r8"); \
4424 LSS_SC_LOADARGS_##nr(args); \
4425 __asm__ __volatile__ \
4426 ("stwu 1, -48(1)\n\t" \
4427 "stw 4, 20(1)\n\t" \
4428 "stw 5, 24(1)\n\t" \
4429 "stw 6, 28(1)\n\t" \
4430 "stw 7, 32(1)\n\t" \
4431 "stw 8, 36(1)\n\t" \
4432 "addi 4, 1, 20\n\t" \
4433 "sc\n\t" \
4434 "mfcr %0" \
4435 : "=&r" (__sc_0), \
4436 "=&r" (__sc_3), "=&r" (__sc_4), \
4437 "=&r" (__sc_5), "=&r" (__sc_6), \
4438 "=&r" (__sc_7), "=&r" (__sc_8) \
4439 : LSS_ASMINPUT_##nr \
4440 : "cr0", "ctr", "memory"); \
4441 __sc_ret = __sc_3; \
4442 __sc_err = __sc_0; \
4443 } \
4444 LSS_RETURN(type, __sc_ret, __sc_err)
4445
4446 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
4447 int flags){
4448 LSS_SC_BODY(3, ssize_t, 17, s, msg, flags);
4449 }
4450
4451 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
4452 const struct kernel_msghdr *msg,
4453 int flags) {
4454 LSS_SC_BODY(3, ssize_t, 16, s, msg, flags);
4455 }
4456
4457 // TODO(csilvers): why is this ifdef'ed out?
4458#if 0
4459 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
4460 int flags,
4461 const struct kernel_sockaddr *to,
4462 unsigned int tolen) {
4463 LSS_BODY(6, ssize_t, 11, s, buf, len, flags, to, tolen);
4464 }
4465#endif
4466
4467 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
4468 LSS_SC_BODY(2, int, 13, s, how);
4469 }
4470
4471 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
4472 LSS_SC_BODY(3, int, 1, domain, type, protocol);
4473 }
4474
4475 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
4476 int sv[2]) {
4477 LSS_SC_BODY(4, int, 8, d, type, protocol, sv);
4478 }
4479 #endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004480 #if defined(__ARM_EABI__) || defined (__aarch64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004481 LSS_INLINE _syscall3(ssize_t, recvmsg, int, s, struct kernel_msghdr*, msg,
4482 int, flags)
4483 LSS_INLINE _syscall3(ssize_t, sendmsg, int, s, const struct kernel_msghdr*,
4484 msg, int, flags)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004485 LSS_INLINE _syscall6(ssize_t, sendto, int, s, const void*, buf, size_t,len,
4486 int, flags, const struct kernel_sockaddr*, to,
4487 unsigned int, tolen)
4488 LSS_INLINE _syscall2(int, shutdown, int, s, int, how)
4489 LSS_INLINE _syscall3(int, socket, int, domain, int, type, int, protocol)
4490 LSS_INLINE _syscall4(int, socketpair, int, d, int, type, int, protocol,
4491 int*, sv)
4492 #endif
4493 #if defined(__i386__) || defined(__ARM_ARCH_3__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -04004494 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
4495 defined(__s390__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004496 #define __NR__socketcall __NR_socketcall
4497 LSS_INLINE _syscall2(int, _socketcall, int, c,
4498 va_list, a)
4499 LSS_INLINE int LSS_NAME(socketcall)(int op, ...) {
4500 int rc;
4501 va_list ap;
4502 va_start(ap, op);
4503 rc = LSS_NAME(_socketcall)(op, ap);
4504 va_end(ap);
4505 return rc;
4506 }
4507
4508 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
4509 int flags){
4510 return (ssize_t)LSS_NAME(socketcall)(17, s, msg, flags);
4511 }
4512
4513 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
4514 const struct kernel_msghdr *msg,
4515 int flags) {
4516 return (ssize_t)LSS_NAME(socketcall)(16, s, msg, flags);
4517 }
4518
4519 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
4520 int flags,
4521 const struct kernel_sockaddr *to,
4522 unsigned int tolen) {
4523 return (ssize_t)LSS_NAME(socketcall)(11, s, buf, len, flags, to, tolen);
4524 }
4525
4526 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
4527 return LSS_NAME(socketcall)(13, s, how);
4528 }
4529
4530 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
4531 return LSS_NAME(socketcall)(1, domain, type, protocol);
4532 }
4533
4534 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
4535 int sv[2]) {
4536 return LSS_NAME(socketcall)(8, d, type, protocol, sv);
4537 }
4538 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004539 #if defined(__NR_fstatat64)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004540 LSS_INLINE _syscall4(int, fstatat64, int, d,
4541 const char *, p,
4542 struct kernel_stat64 *, b, int, f)
4543 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004544 #if defined(__NR_waitpid)
4545 // waitpid is polyfilled below when not available.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004546 LSS_INLINE _syscall3(pid_t, waitpid, pid_t, p,
4547 int*, s, int, o)
4548 #endif
4549 #if defined(__mips__)
4550 /* sys_pipe() on MIPS has non-standard calling conventions, as it returns
4551 * both file handles through CPU registers.
4552 */
4553 LSS_INLINE int LSS_NAME(pipe)(int *p) {
4554 register unsigned long __v0 __asm__("$2") = __NR_pipe;
4555 register unsigned long __v1 __asm__("$3");
4556 register unsigned long __r7 __asm__("$7");
4557 __asm__ __volatile__ ("syscall\n"
vapier@chromium.orgda4a4892015-01-22 16:46:39 +00004558 : "=r"(__v0), "=r"(__v1), "=r" (__r7)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004559 : "0"(__v0)
4560 : "$8", "$9", "$10", "$11", "$12",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00004561 "$13", "$14", "$15", "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004562 if (__r7) {
zodiac@gmail.coma6591482012-04-13 01:29:30 +00004563 unsigned long __errnovalue = __v0;
4564 LSS_ERRNO = __errnovalue;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004565 return -1;
4566 } else {
4567 p[0] = __v0;
4568 p[1] = __v1;
4569 return 0;
4570 }
4571 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004572 #elif defined(__NR_pipe)
4573 // pipe is polyfilled below when not available.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004574 LSS_INLINE _syscall1(int, pipe, int *, p)
4575 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004576 #if defined(__NR_pipe2)
4577 LSS_INLINE _syscall2(int, pipe2, int *, pipefd, int, flags)
4578 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004579 /* TODO(csilvers): see if ppc can/should support this as well */
4580 #if defined(__i386__) || defined(__ARM_ARCH_3__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -04004581 defined(__ARM_EABI__) || \
4582 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64) || \
4583 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004584 #define __NR__statfs64 __NR_statfs64
4585 #define __NR__fstatfs64 __NR_fstatfs64
4586 LSS_INLINE _syscall3(int, _statfs64, const char*, p,
4587 size_t, s,struct kernel_statfs64*, b)
4588 LSS_INLINE _syscall3(int, _fstatfs64, int, f,
4589 size_t, s,struct kernel_statfs64*, b)
4590 LSS_INLINE int LSS_NAME(statfs64)(const char *p,
4591 struct kernel_statfs64 *b) {
4592 return LSS_NAME(_statfs64)(p, sizeof(*b), b);
4593 }
4594 LSS_INLINE int LSS_NAME(fstatfs64)(int f,struct kernel_statfs64 *b) {
4595 return LSS_NAME(_fstatfs64)(f, sizeof(*b), b);
4596 }
4597 #endif
4598
4599 LSS_INLINE int LSS_NAME(execv)(const char *path, const char *const argv[]) {
4600 extern char **environ;
4601 return LSS_NAME(execve)(path, argv, (const char *const *)environ);
4602 }
4603
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00004604 LSS_INLINE pid_t LSS_NAME(gettid)(void) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004605 pid_t tid = LSS_NAME(_gettid)();
4606 if (tid != -1) {
4607 return tid;
4608 }
4609 return LSS_NAME(getpid)();
4610 }
4611
4612 LSS_INLINE void *LSS_NAME(mremap)(void *old_address, size_t old_size,
4613 size_t new_size, int flags, ...) {
4614 va_list ap;
4615 void *new_address, *rc;
4616 va_start(ap, flags);
4617 new_address = va_arg(ap, void *);
4618 rc = LSS_NAME(_mremap)(old_address, old_size, new_size,
4619 flags, new_address);
4620 va_end(ap);
4621 return rc;
4622 }
4623
4624 LSS_INLINE int LSS_NAME(ptrace_detach)(pid_t pid) {
4625 /* PTRACE_DETACH can sometimes forget to wake up the tracee and it
4626 * then sends job control signals to the real parent, rather than to
4627 * the tracer. We reduce the risk of this happening by starting a
4628 * whole new time slice, and then quickly sending a SIGCONT signal
4629 * right after detaching from the tracee.
4630 *
4631 * We use tkill to ensure that we only issue a wakeup for the thread being
4632 * detached. Large multi threaded apps can take a long time in the kernel
4633 * processing SIGCONT.
4634 */
4635 int rc, err;
4636 LSS_NAME(sched_yield)();
4637 rc = LSS_NAME(ptrace)(PTRACE_DETACH, pid, (void *)0, (void *)0);
4638 err = LSS_ERRNO;
4639 LSS_NAME(tkill)(pid, SIGCONT);
4640 /* Old systems don't have tkill */
4641 if (LSS_ERRNO == ENOSYS)
4642 LSS_NAME(kill)(pid, SIGCONT);
4643 LSS_ERRNO = err;
4644 return rc;
4645 }
4646
4647 LSS_INLINE int LSS_NAME(raise)(int sig) {
4648 return LSS_NAME(kill)(LSS_NAME(getpid)(), sig);
4649 }
4650
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00004651 LSS_INLINE int LSS_NAME(setpgrp)(void) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004652 return LSS_NAME(setpgid)(0, 0);
4653 }
4654
vapier@chromium.org2273e812013-04-01 17:52:44 +00004655 #if defined(__x86_64__)
4656 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
4657 LSS_INLINE ssize_t LSS_NAME(pread64)(int f, void *b, size_t c, loff_t o) {
4658 LSS_BODY(4, ssize_t, pread64, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(b),
4659 LSS_SYSCALL_ARG(c), (uint64_t)(o));
4660 }
4661
4662 LSS_INLINE ssize_t LSS_NAME(pwrite64)(int f, const void *b, size_t c,
4663 loff_t o) {
4664 LSS_BODY(4, ssize_t, pwrite64, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(b),
4665 LSS_SYSCALL_ARG(c), (uint64_t)(o));
4666 }
4667
4668 LSS_INLINE int LSS_NAME(readahead)(int f, loff_t o, unsigned c) {
4669 LSS_BODY(3, int, readahead, LSS_SYSCALL_ARG(f), (uint64_t)(o),
4670 LSS_SYSCALL_ARG(c));
4671 }
4672 #elif defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI64
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004673 LSS_INLINE _syscall4(ssize_t, pread64, int, f,
4674 void *, b, size_t, c,
4675 loff_t, o)
4676 LSS_INLINE _syscall4(ssize_t, pwrite64, int, f,
4677 const void *, b, size_t, c,
4678 loff_t, o)
4679 LSS_INLINE _syscall3(int, readahead, int, f,
4680 loff_t, o, unsigned, c)
4681 #else
4682 #define __NR__pread64 __NR_pread64
4683 #define __NR__pwrite64 __NR_pwrite64
4684 #define __NR__readahead __NR_readahead
mseaborn@chromium.org2c73abf2012-09-15 03:46:48 +00004685 #if defined(__ARM_EABI__) || defined(__mips__)
4686 /* On ARM and MIPS, a 64-bit parameter has to be in an even-odd register
4687 * pair. Hence these calls ignore their fourth argument (r3) so that their
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004688 * fifth and sixth make such a pair (r4,r5).
4689 */
4690 #define LSS_LLARG_PAD 0,
4691 LSS_INLINE _syscall6(ssize_t, _pread64, int, f,
4692 void *, b, size_t, c,
4693 unsigned, skip, unsigned, o1, unsigned, o2)
4694 LSS_INLINE _syscall6(ssize_t, _pwrite64, int, f,
4695 const void *, b, size_t, c,
4696 unsigned, skip, unsigned, o1, unsigned, o2)
4697 LSS_INLINE _syscall5(int, _readahead, int, f,
4698 unsigned, skip,
4699 unsigned, o1, unsigned, o2, size_t, c)
4700 #else
4701 #define LSS_LLARG_PAD
4702 LSS_INLINE _syscall5(ssize_t, _pread64, int, f,
4703 void *, b, size_t, c, unsigned, o1,
4704 unsigned, o2)
4705 LSS_INLINE _syscall5(ssize_t, _pwrite64, int, f,
4706 const void *, b, size_t, c, unsigned, o1,
4707 long, o2)
4708 LSS_INLINE _syscall4(int, _readahead, int, f,
4709 unsigned, o1, unsigned, o2, size_t, c)
4710 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004711 /* We force 64bit-wide parameters onto the stack, then access each
4712 * 32-bit component individually. This guarantees that we build the
4713 * correct parameters independent of the native byte-order of the
4714 * underlying architecture.
4715 */
4716 LSS_INLINE ssize_t LSS_NAME(pread64)(int fd, void *buf, size_t count,
4717 loff_t off) {
4718 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004719 return LSS_NAME(_pread64)(fd, buf, count,
4720 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004721 }
4722 LSS_INLINE ssize_t LSS_NAME(pwrite64)(int fd, const void *buf,
4723 size_t count, loff_t off) {
4724 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004725 return LSS_NAME(_pwrite64)(fd, buf, count,
4726 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004727 }
4728 LSS_INLINE int LSS_NAME(readahead)(int fd, loff_t off, int len) {
4729 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004730 return LSS_NAME(_readahead)(fd, LSS_LLARG_PAD o.arg[0], o.arg[1], len);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004731 }
4732 #endif
4733#endif
4734
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004735/*
4736 * Polyfills for deprecated syscalls.
4737 */
4738
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004739#if !defined(__NR_dup2)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004740 LSS_INLINE int LSS_NAME(dup2)(int s, int d) {
4741 return LSS_NAME(dup3)(s, d, 0);
4742 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004743#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004744
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004745#if !defined(__NR_open)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004746 LSS_INLINE int LSS_NAME(open)(const char *pathname, int flags, int mode) {
4747 return LSS_NAME(openat)(AT_FDCWD, pathname, flags, mode);
4748 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004749#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004750
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004751#if !defined(__NR_unlink)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004752 LSS_INLINE int LSS_NAME(unlink)(const char *pathname) {
4753 return LSS_NAME(unlinkat)(AT_FDCWD, pathname, 0);
4754 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004755#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004756
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004757#if !defined(__NR_readlink)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004758 LSS_INLINE int LSS_NAME(readlink)(const char *pathname, char *buffer,
4759 size_t size) {
4760 return LSS_NAME(readlinkat)(AT_FDCWD, pathname, buffer, size);
4761 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004762#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004763
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004764#if !defined(__NR_pipe)
Mike Frysinger4ce4c482018-01-03 18:31:42 -05004765 LSS_INLINE int LSS_NAME(pipe)(int *pipefd) {
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004766 return LSS_NAME(pipe2)(pipefd, 0);
4767 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004768#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004769
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004770#if !defined(__NR_poll)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004771 LSS_INLINE int LSS_NAME(poll)(struct kernel_pollfd *fds, unsigned int nfds,
4772 int timeout) {
4773 struct kernel_timespec timeout_ts;
4774 struct kernel_timespec *timeout_ts_p = NULL;
4775
4776 if (timeout >= 0) {
4777 timeout_ts.tv_sec = timeout / 1000;
4778 timeout_ts.tv_nsec = (timeout % 1000) * 1000000;
4779 timeout_ts_p = &timeout_ts;
4780 }
4781 return LSS_NAME(ppoll)(fds, nfds, timeout_ts_p, NULL, 0);
4782 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004783#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004784
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004785#if !defined(__NR_stat)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004786 LSS_INLINE int LSS_NAME(stat)(const char *pathname,
4787 struct kernel_stat *buf) {
4788 return LSS_NAME(newfstatat)(AT_FDCWD, pathname, buf, 0);
4789 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004790#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004791
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004792#if !defined(__NR_waitpid)
4793 LSS_INLINE pid_t LSS_NAME(waitpid)(pid_t pid, int *status, int options) {
4794 return LSS_NAME(wait4)(pid, status, options, 0);
4795 }
4796#endif
4797
4798#if !defined(__NR_fork)
4799// TODO: define this in an arch-independant way instead of inlining the clone
4800// syscall body.
4801
4802# if defined(__aarch64__)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004803 LSS_INLINE pid_t LSS_NAME(fork)(void) {
4804 // No fork syscall on aarch64 - implement by means of the clone syscall.
4805 // Note that this does not reset glibc's cached view of the PID/TID, so
4806 // some glibc interfaces might go wrong in the forked subprocess.
4807 int flags = SIGCHLD;
4808 void *child_stack = NULL;
4809 void *parent_tidptr = NULL;
4810 void *newtls = NULL;
4811 void *child_tidptr = NULL;
4812
4813 LSS_REG(0, flags);
4814 LSS_REG(1, child_stack);
4815 LSS_REG(2, parent_tidptr);
4816 LSS_REG(3, newtls);
4817 LSS_REG(4, child_tidptr);
4818 LSS_BODY(pid_t, clone, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3),
4819 "r"(__r4));
4820 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004821# elif defined(__x86_64__)
4822 LSS_INLINE pid_t LSS_NAME(fork)(void) {
4823 // Android disallows the fork syscall on x86_64 - implement by means of the
4824 // clone syscall as above for aarch64.
4825 int flags = SIGCHLD;
4826 void *child_stack = NULL;
4827 void *parent_tidptr = NULL;
4828 void *newtls = NULL;
4829 void *child_tidptr = NULL;
4830
4831 LSS_BODY(5, pid_t, clone, LSS_SYSCALL_ARG(flags),
4832 LSS_SYSCALL_ARG(child_stack), LSS_SYSCALL_ARG(parent_tidptr),
4833 LSS_SYSCALL_ARG(newtls), LSS_SYSCALL_ARG(child_tidptr));
4834 }
4835# else
4836# error missing fork polyfill for this architecture
4837# endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004838#endif
4839
Michael Forneyf70e2f12020-01-22 19:19:38 -08004840/* These restore the original values of these macros saved by the
4841 * corresponding #pragma push_macro near the top of this file. */
4842#pragma pop_macro("stat64")
4843#pragma pop_macro("fstat64")
4844#pragma pop_macro("lstat64")
4845#pragma pop_macro("pread64")
4846#pragma pop_macro("pwrite64")
Michael Forneyfd00dbb2020-03-10 14:12:52 -07004847#pragma pop_macro("getdents64")
mseaborn@chromium.orgca749372012-09-05 18:26:20 +00004848
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004849#if defined(__cplusplus) && !defined(SYS_CPLUSPLUS)
4850}
4851#endif
4852
4853#endif
4854#endif