blob: dd19c5a8ef4a497faf47c90e1205788c649361e4 [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__) || \
Andreas Schwab1d387f42022-02-15 16:21:13 +010091 defined(__aarch64__) || defined(__s390__) || defined(__e2k__) || \
mingtaoxt xtc0c96892022-08-11 16:53:21 +080092 (defined(__riscv) && __riscv_xlen == 64) || defined(__loongarch_lp64)) \
zodiac@gmail.com4f470182010-10-13 03:47:54 +000093 && (defined(__linux) || defined(__ANDROID__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +000094
95#ifndef SYS_CPLUSPLUS
96#ifdef __cplusplus
97/* Some system header files in older versions of gcc neglect to properly
98 * handle being included from C++. As it appears to be harmless to have
99 * multiple nested 'extern "C"' blocks, just add another one here.
100 */
101extern "C" {
102#endif
103
104#include <errno.h>
zodiac@gmail.com4f470182010-10-13 03:47:54 +0000105#include <fcntl.h>
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000106#include <sched.h>
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000107#include <signal.h>
108#include <stdarg.h>
109#include <stddef.h>
vapier@chromium.org2273e812013-04-01 17:52:44 +0000110#include <stdint.h>
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000111#include <string.h>
112#include <sys/ptrace.h>
113#include <sys/resource.h>
114#include <sys/time.h>
115#include <sys/types.h>
zodiac@gmail.com4f470182010-10-13 03:47:54 +0000116#include <sys/syscall.h>
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000117#include <unistd.h>
118#include <linux/unistd.h>
119#include <endian.h>
120
121#ifdef __mips__
122/* Include definitions of the ABI currently in use. */
mseaborn@chromium.org4fc94222015-08-11 21:15:24 +0000123#ifdef __ANDROID__
124/* Android doesn't have sgidefs.h, but does have asm/sgidefs.h,
125 * which has the definitions we need.
126 */
127#include <asm/sgidefs.h>
128#else
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000129#include <sgidefs.h>
130#endif
131#endif
mseaborn@chromium.org4fc94222015-08-11 21:15:24 +0000132#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000133
Michael Forneyf70e2f12020-01-22 19:19:38 -0800134/* Some libcs, for example Android NDK and musl, #define these
135 * macros as aliases to their non-64 counterparts. To avoid naming
136 * conflict, remove them.
137 *
138 * These are restored by the corresponding #pragma pop_macro near
139 * the end of this file.
140 */
141#pragma push_macro("stat64")
142#pragma push_macro("fstat64")
143#pragma push_macro("lstat64")
144#pragma push_macro("pread64")
145#pragma push_macro("pwrite64")
Michael Forneyfd00dbb2020-03-10 14:12:52 -0700146#pragma push_macro("getdents64")
Michael Forneyf70e2f12020-01-22 19:19:38 -0800147#undef stat64
148#undef fstat64
149#undef lstat64
150#undef pread64
151#undef pwrite64
Michael Forneyfd00dbb2020-03-10 14:12:52 -0700152#undef getdents64
mseaborn@chromium.orgca749372012-09-05 18:26:20 +0000153
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -0400154#if defined(__ANDROID__) && defined(__x86_64__)
155// A number of x86_64 syscalls are blocked by seccomp on recent Android;
156// undefine them so that modern alternatives will be used instead where
157// possible.
158// The alternative syscalls have been sanity checked against linux-3.4+;
159// older versions might not work.
160# undef __NR_getdents
161# undef __NR_dup2
162# undef __NR_fork
163# undef __NR_getpgrp
164# undef __NR_open
165# undef __NR_poll
166# undef __NR_readlink
167# undef __NR_stat
168# undef __NR_unlink
169# undef __NR_pipe
170#endif
171
172#if defined(__ANDROID__)
173// waitpid is blocked by seccomp on all architectures on recent Android.
174# undef __NR_waitpid
175#endif
176
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000177/* As glibc often provides subtly incompatible data structures (and implicit
178 * wrapper functions that convert them), we provide our own kernel data
179 * structures for use by the system calls.
180 * These structures have been developed by using Linux 2.6.23 headers for
181 * reference. Note though, we do not care about exact API compatibility
182 * with the kernel, and in fact the kernel often does not have a single
183 * API that works across architectures. Instead, we try to mimic the glibc
184 * API where reasonable, and only guarantee ABI compatibility with the
185 * kernel headers.
186 * Most notably, here are a few changes that were made to the structures
187 * defined by kernel headers:
188 *
189 * - we only define structures, but not symbolic names for kernel data
190 * types. For the latter, we directly use the native C datatype
191 * (i.e. "unsigned" instead of "mode_t").
192 * - in a few cases, it is possible to define identical structures for
193 * both 32bit (e.g. i386) and 64bit (e.g. x86-64) platforms by
194 * standardizing on the 64bit version of the data types. In particular,
195 * this means that we use "unsigned" where the 32bit headers say
196 * "unsigned long".
197 * - overall, we try to minimize the number of cases where we need to
198 * conditionally define different structures.
199 * - the "struct kernel_sigaction" class of structures have been
200 * modified to more closely mimic glibc's API by introducing an
201 * anonymous union for the function pointer.
202 * - a small number of field names had to have an underscore appended to
203 * them, because glibc defines a global macro by the same name.
204 */
205
206/* include/linux/dirent.h */
207struct kernel_dirent64 {
208 unsigned long long d_ino;
209 long long d_off;
210 unsigned short d_reclen;
211 unsigned char d_type;
212 char d_name[256];
213};
214
215/* include/linux/dirent.h */
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -0400216#if !defined(__NR_getdents)
217// when getdents is not available, getdents64 is used for both.
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000218#define kernel_dirent kernel_dirent64
219#else
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000220struct kernel_dirent {
221 long d_ino;
222 long d_off;
223 unsigned short d_reclen;
224 char d_name[256];
225};
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000226#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000227
228/* include/linux/uio.h */
229struct kernel_iovec {
230 void *iov_base;
231 unsigned long iov_len;
232};
233
234/* include/linux/socket.h */
235struct kernel_msghdr {
236 void *msg_name;
237 int msg_namelen;
238 struct kernel_iovec*msg_iov;
239 unsigned long msg_iovlen;
240 void *msg_control;
241 unsigned long msg_controllen;
242 unsigned msg_flags;
243};
244
245/* include/asm-generic/poll.h */
246struct kernel_pollfd {
247 int fd;
248 short events;
249 short revents;
250};
251
252/* include/linux/resource.h */
253struct kernel_rlimit {
254 unsigned long rlim_cur;
255 unsigned long rlim_max;
256};
257
258/* include/linux/time.h */
259struct kernel_timespec {
260 long tv_sec;
261 long tv_nsec;
262};
263
264/* include/linux/time.h */
265struct kernel_timeval {
266 long tv_sec;
267 long tv_usec;
268};
269
Doug Kwan32a80cd2022-07-01 17:52:39 +0000270/* include/linux/time.h */
271struct kernel_itimerval {
272 struct kernel_timeval it_interval;
273 struct kernel_timeval it_value;
274};
275
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000276/* include/linux/resource.h */
277struct kernel_rusage {
278 struct kernel_timeval ru_utime;
279 struct kernel_timeval ru_stime;
280 long ru_maxrss;
281 long ru_ixrss;
282 long ru_idrss;
283 long ru_isrss;
284 long ru_minflt;
285 long ru_majflt;
286 long ru_nswap;
287 long ru_inblock;
288 long ru_oublock;
289 long ru_msgsnd;
290 long ru_msgrcv;
291 long ru_nsignals;
292 long ru_nvcsw;
293 long ru_nivcsw;
294};
295
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000296#if defined(__i386__) || defined(__ARM_EABI__) || defined(__ARM_ARCH_3__) \
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300297 || defined(__PPC__) || (defined(__s390__) && !defined(__s390x__)) \
298 || defined(__e2k__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000299
300/* include/asm-{arm,i386,mips,ppc}/signal.h */
301struct kernel_old_sigaction {
302 union {
303 void (*sa_handler_)(int);
vapier@chromium.orgcdda4342013-03-06 04:26:28 +0000304 void (*sa_sigaction_)(int, siginfo_t *, void *);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000305 };
306 unsigned long sa_mask;
307 unsigned long sa_flags;
308 void (*sa_restorer)(void);
309} __attribute__((packed,aligned(4)));
310#elif (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
311 #define kernel_old_sigaction kernel_sigaction
mingtaoxt xtc0c96892022-08-11 16:53:21 +0800312#elif defined(__aarch64__) || defined(__riscv) || defined(__loongarch_lp64)
313 // No kernel_old_sigaction defined for arm64 riscv and loongarch64.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000314#endif
315
316/* Some kernel functions (e.g. sigaction() in 2.6.23) require that the
317 * exactly match the size of the signal set, even though the API was
318 * intended to be extensible. We define our own KERNEL_NSIG to deal with
319 * this.
320 * Please note that glibc provides signals [1.._NSIG-1], whereas the
321 * kernel (and this header) provides the range [1..KERNEL_NSIG]. The
322 * actual number of signals is obviously the same, but the constants
323 * differ by one.
324 */
325#ifdef __mips__
326#define KERNEL_NSIG 128
327#else
328#define KERNEL_NSIG 64
329#endif
330
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000331/* include/asm-{arm,aarch64,i386,mips,x86_64}/signal.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000332struct kernel_sigset_t {
333 unsigned long sig[(KERNEL_NSIG + 8*sizeof(unsigned long) - 1)/
334 (8*sizeof(unsigned long))];
335};
336
337/* include/asm-{arm,i386,mips,x86_64,ppc}/signal.h */
338struct kernel_sigaction {
339#ifdef __mips__
340 unsigned long sa_flags;
341 union {
342 void (*sa_handler_)(int);
vapier@chromium.orgcdda4342013-03-06 04:26:28 +0000343 void (*sa_sigaction_)(int, siginfo_t *, void *);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000344 };
345 struct kernel_sigset_t sa_mask;
346#else
347 union {
348 void (*sa_handler_)(int);
vapier@chromium.orgcdda4342013-03-06 04:26:28 +0000349 void (*sa_sigaction_)(int, siginfo_t *, void *);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000350 };
351 unsigned long sa_flags;
mingtaoxt xtc0c96892022-08-11 16:53:21 +0800352#if !defined(__riscv) && !defined(__loongarch_lp64)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000353 void (*sa_restorer)(void);
Andreas Schwab1d387f42022-02-15 16:21:13 +0100354#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000355 struct kernel_sigset_t sa_mask;
356#endif
357};
358
359/* include/linux/socket.h */
360struct kernel_sockaddr {
361 unsigned short sa_family;
362 char sa_data[14];
363};
364
Bryan Chan3f6478a2016-06-14 08:38:17 -0400365/* include/asm-{arm,aarch64,i386,mips,ppc,s390}/stat.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000366#ifdef __mips__
367#if _MIPS_SIM == _MIPS_SIM_ABI64
Peter Kasting99121a32022-08-22 16:43:44 +0000368typedef long long kernel_off_t;
369typedef unsigned long long kernel_blkcnt_t;
370typedef unsigned kernel_timespec;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000371struct kernel_stat {
372#else
373struct kernel_stat64 {
374#endif
375 unsigned st_dev;
376 unsigned __pad0[3];
377 unsigned long long st_ino;
378 unsigned st_mode;
379 unsigned st_nlink;
380 unsigned st_uid;
381 unsigned st_gid;
382 unsigned st_rdev;
383 unsigned __pad1[3];
384 long long st_size;
385 unsigned st_atime_;
386 unsigned st_atime_nsec_;
387 unsigned st_mtime_;
388 unsigned st_mtime_nsec_;
389 unsigned st_ctime_;
390 unsigned st_ctime_nsec_;
391 unsigned st_blksize;
392 unsigned __pad2;
393 unsigned long long st_blocks;
394};
395#elif defined __PPC__
396struct kernel_stat64 {
397 unsigned long long st_dev;
398 unsigned long long st_ino;
399 unsigned st_mode;
400 unsigned st_nlink;
401 unsigned st_uid;
402 unsigned st_gid;
403 unsigned long long st_rdev;
404 unsigned short int __pad2;
405 long long st_size;
406 long st_blksize;
407 long long st_blocks;
408 long st_atime_;
409 unsigned long st_atime_nsec_;
410 long st_mtime_;
411 unsigned long st_mtime_nsec_;
412 long st_ctime_;
413 unsigned long st_ctime_nsec_;
414 unsigned long __unused4;
415 unsigned long __unused5;
416};
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300417#elif defined(__e2k__)
418struct kernel_stat64 {
419 unsigned long long st_dev;
420 unsigned long long st_ino;
421 unsigned int st_mode;
422 unsigned int st_nlink;
423 unsigned int st_uid;
424 unsigned int st_gid;
425 unsigned long long st_rdev;
426 long long st_size;
427 int st_blksize;
428 int __pad2;
429 unsigned long long st_blocks;
430 int st_atime_;
431 unsigned int st_atime_nsec_;
432 int st_mtime_;
433 unsigned int st_mtime_nsec_;
434 int st_ctime_;
435 unsigned int st_ctime_nsec_;
436 unsigned int __unused4;
437 unsigned int __unused5;
438};
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000439#else
440struct kernel_stat64 {
441 unsigned long long st_dev;
442 unsigned char __pad0[4];
443 unsigned __st_ino;
444 unsigned st_mode;
445 unsigned st_nlink;
446 unsigned st_uid;
447 unsigned st_gid;
448 unsigned long long st_rdev;
449 unsigned char __pad3[4];
450 long long st_size;
451 unsigned st_blksize;
452 unsigned long long st_blocks;
453 unsigned st_atime_;
454 unsigned st_atime_nsec_;
455 unsigned st_mtime_;
456 unsigned st_mtime_nsec_;
457 unsigned st_ctime_;
458 unsigned st_ctime_nsec_;
459 unsigned long long st_ino;
460};
461#endif
462
Bryan Chan3f6478a2016-06-14 08:38:17 -0400463/* include/asm-{arm,aarch64,i386,mips,x86_64,ppc,s390}/stat.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000464#if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__)
Peter Kasting99121a32022-08-22 16:43:44 +0000465typedef unsigned kernel_off_t;
466typedef unsigned kernel_blkcnt_t;
467typedef unsigned kernel_timespec;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000468struct kernel_stat {
469 /* The kernel headers suggest that st_dev and st_rdev should be 32bit
470 * quantities encoding 12bit major and 20bit minor numbers in an interleaved
471 * format. In reality, we do not see useful data in the top bits. So,
472 * we'll leave the padding in here, until we find a better solution.
473 */
474 unsigned short st_dev;
475 short pad1;
476 unsigned st_ino;
477 unsigned short st_mode;
478 unsigned short st_nlink;
479 unsigned short st_uid;
480 unsigned short st_gid;
481 unsigned short st_rdev;
482 short pad2;
Peter Kasting99121a32022-08-22 16:43:44 +0000483 kernel_off_t st_size;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000484 unsigned st_blksize;
Peter Kasting99121a32022-08-22 16:43:44 +0000485 kernel_blkcnt_t st_blocks;
486 kernel_timespec st_atime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000487 unsigned st_atime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000488 kernel_timespec st_mtime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000489 unsigned st_mtime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000490 kernel_timespec st_ctime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000491 unsigned st_ctime_nsec_;
492 unsigned __unused4;
493 unsigned __unused5;
494};
495#elif defined(__x86_64__)
Peter Kasting99121a32022-08-22 16:43:44 +0000496typedef int64_t kernel_off_t;
497typedef int64_t kernel_blkcnt_t;
498typedef uint64_t kernel_timespec;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000499struct kernel_stat {
vapier@chromium.org2273e812013-04-01 17:52:44 +0000500 uint64_t st_dev;
501 uint64_t st_ino;
502 uint64_t st_nlink;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000503 unsigned st_mode;
504 unsigned st_uid;
505 unsigned st_gid;
506 unsigned __pad0;
vapier@chromium.org2273e812013-04-01 17:52:44 +0000507 uint64_t st_rdev;
Peter Kasting99121a32022-08-22 16:43:44 +0000508 kernel_off_t st_size;
vapier@chromium.org2273e812013-04-01 17:52:44 +0000509 int64_t st_blksize;
Peter Kasting99121a32022-08-22 16:43:44 +0000510 kernel_blkcnt_t st_blocks;
511 kernel_timespec st_atime_;
vapier@chromium.org2273e812013-04-01 17:52:44 +0000512 uint64_t st_atime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000513 kernel_timespec st_mtime_;
vapier@chromium.org2273e812013-04-01 17:52:44 +0000514 uint64_t st_mtime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000515 kernel_timespec st_ctime_;
vapier@chromium.org2273e812013-04-01 17:52:44 +0000516 uint64_t st_ctime_nsec_;
anton@chromium.org43de0522014-04-04 11:20:46 +0000517 int64_t __unused4[3];
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000518};
519#elif defined(__PPC__)
Peter Kasting99121a32022-08-22 16:43:44 +0000520typedef long kernel_off_t;
521typedef unsigned long kernel_blkcnt_t;
522typedef unsigned long kernel_timespec;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000523struct kernel_stat {
524 unsigned st_dev;
525 unsigned long st_ino; // ino_t
526 unsigned long st_mode; // mode_t
527 unsigned short st_nlink; // nlink_t
528 unsigned st_uid; // uid_t
529 unsigned st_gid; // gid_t
530 unsigned st_rdev;
Peter Kasting99121a32022-08-22 16:43:44 +0000531 kernel_off_t st_size; // off_t
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000532 unsigned long st_blksize;
Peter Kasting99121a32022-08-22 16:43:44 +0000533 kernel_blkcnt_t st_blocks;
534 kernel_timespec st_atime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000535 unsigned long st_atime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000536 kernel_timespec st_mtime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000537 unsigned long st_mtime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000538 kernel_timespec st_ctime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000539 unsigned long st_ctime_nsec_;
540 unsigned long __unused4;
541 unsigned long __unused5;
542};
543#elif (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64)
Peter Kasting99121a32022-08-22 16:43:44 +0000544typedef long kernel_off_t;
545typedef int kernel_blkcnt_t;
546typedef long kernel_timespec;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000547struct kernel_stat {
548 unsigned st_dev;
549 int st_pad1[3];
550 unsigned st_ino;
551 unsigned st_mode;
552 unsigned st_nlink;
553 unsigned st_uid;
554 unsigned st_gid;
555 unsigned st_rdev;
556 int st_pad2[2];
Peter Kasting99121a32022-08-22 16:43:44 +0000557 kernel_off_t st_size;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000558 int st_pad3;
Peter Kasting99121a32022-08-22 16:43:44 +0000559 kernel_timespec st_atime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000560 long st_atime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000561 kernel_timespec st_mtime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000562 long st_mtime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000563 kernel_timespec st_ctime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000564 long st_ctime_nsec_;
565 int st_blksize;
Peter Kasting99121a32022-08-22 16:43:44 +0000566 kernel_blkcnt_t st_blocks;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000567 int st_pad4[14];
568};
mingtaoxt xtc0c96892022-08-11 16:53:21 +0800569#elif defined(__aarch64__) || defined(__riscv) || defined(__loongarch_lp64)
Peter Kasting99121a32022-08-22 16:43:44 +0000570typedef long kernel_off_t;
571typedef long kernel_blkcnt_t;
572typedef long kernel_timespec;
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000573struct kernel_stat {
574 unsigned long st_dev;
575 unsigned long st_ino;
576 unsigned int st_mode;
577 unsigned int st_nlink;
578 unsigned int st_uid;
579 unsigned int st_gid;
580 unsigned long st_rdev;
581 unsigned long __pad1;
Peter Kasting99121a32022-08-22 16:43:44 +0000582 kernel_off_t st_size;
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000583 int st_blksize;
584 int __pad2;
Peter Kasting99121a32022-08-22 16:43:44 +0000585 kernel_blkcnt_t st_blocks;
586 kernel_timespec st_atime_;
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000587 unsigned long st_atime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000588 kernel_timespec st_mtime_;
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000589 unsigned long st_mtime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000590 kernel_timespec st_ctime_;
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000591 unsigned long st_ctime_nsec_;
592 unsigned int __unused4;
593 unsigned int __unused5;
594};
Bryan Chan3f6478a2016-06-14 08:38:17 -0400595#elif defined(__s390x__)
Peter Kasting99121a32022-08-22 16:43:44 +0000596typedef unsigned long kernel_off_t;
597typedef long kernel_blkcnt_t;
598typedef unsigned long kernel_timespec;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400599struct kernel_stat {
600 unsigned long st_dev;
601 unsigned long st_ino;
602 unsigned long st_nlink;
603 unsigned int st_mode;
604 unsigned int st_uid;
605 unsigned int st_gid;
606 unsigned int __pad1;
607 unsigned long st_rdev;
Peter Kasting99121a32022-08-22 16:43:44 +0000608 kernel_off_t st_size;
609 kernel_timespec st_atime_;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400610 unsigned long st_atime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000611 kernel_timespec st_mtime_;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400612 unsigned long st_mtime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000613 kernel_timespec st_ctime_;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400614 unsigned long st_ctime_nsec_;
615 unsigned long st_blksize;
Peter Kasting99121a32022-08-22 16:43:44 +0000616 kernel_blkcnt_t st_blocks;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400617 unsigned long __unused[3];
618};
619#elif defined(__s390__)
Peter Kasting99121a32022-08-22 16:43:44 +0000620typedef unsigned long kernel_off_t;
621typedef unsigned long kernel_blkcnt_t;
622typedef unsigned long kernel_timespec;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400623struct kernel_stat {
624 unsigned short st_dev;
625 unsigned short __pad1;
626 unsigned long st_ino;
627 unsigned short st_mode;
628 unsigned short st_nlink;
629 unsigned short st_uid;
630 unsigned short st_gid;
631 unsigned short st_rdev;
632 unsigned short __pad2;
Peter Kasting99121a32022-08-22 16:43:44 +0000633 kernel_off_t st_size;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400634 unsigned long st_blksize;
Peter Kasting99121a32022-08-22 16:43:44 +0000635 kernel_blkcnt_t st_blocks;
636 kernel_timespec st_atime_;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400637 unsigned long st_atime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000638 kernel_timespec st_mtime_;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400639 unsigned long st_mtime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000640 kernel_timespec st_ctime_;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400641 unsigned long st_ctime_nsec_;
642 unsigned long __unused4;
643 unsigned long __unused5;
644};
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300645#elif defined(__e2k__)
Peter Kasting99121a32022-08-22 16:43:44 +0000646typedef unsigned long kernel_off_t;
647typedef unsigned long kernel_blkcnt_t;
648typedef unsigned long kernel_timespec;
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300649struct kernel_stat {
650 unsigned long st_dev;
651 unsigned long st_ino;
652 unsigned int st_mode;
653 unsigned long st_nlink;
654 unsigned int st_uid;
655 unsigned int st_gid;
656 unsigned long st_rdev;
Peter Kasting99121a32022-08-22 16:43:44 +0000657 kernel_off_t st_size;
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300658 unsigned long st_blksize;
Peter Kasting99121a32022-08-22 16:43:44 +0000659 kernel_blkcnt_t st_blocks;
660 kernel_timespec st_atime_;
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300661 unsigned long st_atime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000662 kernel_timespec st_mtime_;
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300663 unsigned long st_mtime_nsec_;
Peter Kasting99121a32022-08-22 16:43:44 +0000664 kernel_timespec st_ctime_;
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300665 unsigned long st_ctime_nsec_;
666};
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000667#endif
668
Bryan Chan3f6478a2016-06-14 08:38:17 -0400669/* include/asm-{arm,aarch64,i386,mips,x86_64,ppc,s390}/statfs.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000670#ifdef __mips__
671#if _MIPS_SIM != _MIPS_SIM_ABI64
672struct kernel_statfs64 {
673 unsigned long f_type;
674 unsigned long f_bsize;
675 unsigned long f_frsize;
676 unsigned long __pad;
677 unsigned long long f_blocks;
678 unsigned long long f_bfree;
679 unsigned long long f_files;
680 unsigned long long f_ffree;
681 unsigned long long f_bavail;
682 struct { int val[2]; } f_fsid;
683 unsigned long f_namelen;
684 unsigned long f_spare[6];
685};
686#endif
Bryan Chan3f6478a2016-06-14 08:38:17 -0400687#elif defined(__s390__)
688/* See also arch/s390/include/asm/compat.h */
689struct kernel_statfs64 {
690 unsigned int f_type;
691 unsigned int f_bsize;
692 unsigned long long f_blocks;
693 unsigned long long f_bfree;
694 unsigned long long f_bavail;
695 unsigned long long f_files;
696 unsigned long long f_ffree;
697 struct { int val[2]; } f_fsid;
698 unsigned int f_namelen;
699 unsigned int f_frsize;
700 unsigned int f_flags;
701 unsigned int f_spare[4];
702};
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000703#elif !defined(__x86_64__)
704struct kernel_statfs64 {
705 unsigned long f_type;
706 unsigned long f_bsize;
707 unsigned long long f_blocks;
708 unsigned long long f_bfree;
709 unsigned long long f_bavail;
710 unsigned long long f_files;
711 unsigned long long f_ffree;
712 struct { int val[2]; } f_fsid;
713 unsigned long f_namelen;
714 unsigned long f_frsize;
715 unsigned long f_spare[5];
716};
717#endif
718
Bryan Chan3f6478a2016-06-14 08:38:17 -0400719/* include/asm-{arm,i386,mips,x86_64,ppc,generic,s390}/statfs.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000720#ifdef __mips__
721struct kernel_statfs {
722 long f_type;
723 long f_bsize;
724 long f_frsize;
725 long f_blocks;
726 long f_bfree;
727 long f_files;
728 long f_ffree;
729 long f_bavail;
730 struct { int val[2]; } f_fsid;
731 long f_namelen;
732 long f_spare[6];
733};
vapier@chromium.org2273e812013-04-01 17:52:44 +0000734#elif defined(__x86_64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000735struct kernel_statfs {
736 /* x86_64 actually defines all these fields as signed, whereas all other */
737 /* platforms define them as unsigned. Leaving them at unsigned should not */
vapier@chromium.org2273e812013-04-01 17:52:44 +0000738 /* cause any problems. Make sure these are 64-bit even on x32. */
739 uint64_t f_type;
740 uint64_t f_bsize;
741 uint64_t f_blocks;
742 uint64_t f_bfree;
743 uint64_t f_bavail;
744 uint64_t f_files;
745 uint64_t f_ffree;
746 struct { int val[2]; } f_fsid;
747 uint64_t f_namelen;
748 uint64_t f_frsize;
749 uint64_t f_spare[5];
750};
Bryan Chan3f6478a2016-06-14 08:38:17 -0400751#elif defined(__s390__)
752struct kernel_statfs {
753 unsigned int f_type;
754 unsigned int f_bsize;
755 unsigned long f_blocks;
756 unsigned long f_bfree;
757 unsigned long f_bavail;
758 unsigned long f_files;
759 unsigned long f_ffree;
760 struct { int val[2]; } f_fsid;
761 unsigned int f_namelen;
762 unsigned int f_frsize;
763 unsigned int f_flags;
764 unsigned int f_spare[4];
765};
vapier@chromium.org2273e812013-04-01 17:52:44 +0000766#else
767struct kernel_statfs {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000768 unsigned long f_type;
769 unsigned long f_bsize;
770 unsigned long f_blocks;
771 unsigned long f_bfree;
772 unsigned long f_bavail;
773 unsigned long f_files;
774 unsigned long f_ffree;
775 struct { int val[2]; } f_fsid;
776 unsigned long f_namelen;
777 unsigned long f_frsize;
778 unsigned long f_spare[5];
779};
780#endif
781
mingtaoxt xtc0c96892022-08-11 16:53:21 +0800782struct kernel_statx_timestamp {
783 int64_t tv_sec;
784 uint32_t tv_nsec;
785 int32_t __reserved;
786};
787
788struct kernel_statx {
789 uint32_t stx_mask;
790 uint32_t stx_blksize;
791 uint64_t stx_attributes;
792 uint32_t stx_nlink;
793 uint32_t stx_uid;
794 uint32_t stx_gid;
795 uint16_t stx_mode;
796 uint16_t __spare0[1];
797 uint64_t stx_ino;
798 uint64_t stx_size;
799 uint64_t stx_blocks;
800 uint64_t stx_attributes_mask;
801 struct kernel_statx_timestamp stx_atime;
802 struct kernel_statx_timestamp stx_btime;
803 struct kernel_statx_timestamp stx_ctime;
804 struct kernel_statx_timestamp stx_mtime;
805 uint32_t stx_rdev_major;
806 uint32_t stx_rdev_minor;
807 uint32_t stx_dev_major;
808 uint32_t stx_dev_minor;
809 uint64_t stx_mnt_id;
810 uint64_t __spare2;
811 uint64_t __spare3[12];
812};
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000813
814/* Definitions missing from the standard header files */
815#ifndef O_DIRECTORY
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000816#if defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || defined(__aarch64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000817#define O_DIRECTORY 0040000
818#else
819#define O_DIRECTORY 0200000
820#endif
821#endif
822#ifndef NT_PRXFPREG
823#define NT_PRXFPREG 0x46e62b7f
824#endif
825#ifndef PTRACE_GETFPXREGS
826#define PTRACE_GETFPXREGS ((enum __ptrace_request)18)
827#endif
828#ifndef PR_GET_DUMPABLE
829#define PR_GET_DUMPABLE 3
830#endif
831#ifndef PR_SET_DUMPABLE
832#define PR_SET_DUMPABLE 4
833#endif
834#ifndef PR_GET_SECCOMP
835#define PR_GET_SECCOMP 21
836#endif
837#ifndef PR_SET_SECCOMP
838#define PR_SET_SECCOMP 22
839#endif
840#ifndef AT_FDCWD
841#define AT_FDCWD (-100)
842#endif
843#ifndef AT_SYMLINK_NOFOLLOW
844#define AT_SYMLINK_NOFOLLOW 0x100
845#endif
846#ifndef AT_REMOVEDIR
847#define AT_REMOVEDIR 0x200
848#endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +0800849#ifndef AT_NO_AUTOMOUNT
850#define AT_NO_AUTOMOUNT 0x800
851#endif
852#ifndef AT_EMPTY_PATH
853#define AT_EMPTY_PATH 0x1000
854#endif
855#ifndef STATX_BASIC_STATS
856#define STATX_BASIC_STATS 0x000007ffU
857#endif
858#ifndef AT_STATX_SYNC_AS_STAT
859#define AT_STATX_SYNC_AS_STAT 0x0000
860#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000861#ifndef MREMAP_FIXED
862#define MREMAP_FIXED 2
863#endif
864#ifndef SA_RESTORER
865#define SA_RESTORER 0x04000000
866#endif
867#ifndef CPUCLOCK_PROF
868#define CPUCLOCK_PROF 0
869#endif
870#ifndef CPUCLOCK_VIRT
871#define CPUCLOCK_VIRT 1
872#endif
873#ifndef CPUCLOCK_SCHED
874#define CPUCLOCK_SCHED 2
875#endif
876#ifndef CPUCLOCK_PERTHREAD_MASK
877#define CPUCLOCK_PERTHREAD_MASK 4
878#endif
879#ifndef MAKE_PROCESS_CPUCLOCK
880#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
Nico Webera2b70922017-03-30 11:03:37 -0400881 ((int)(~(unsigned)(pid) << 3) | (int)(clock))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000882#endif
883#ifndef MAKE_THREAD_CPUCLOCK
884#define MAKE_THREAD_CPUCLOCK(tid, clock) \
Nico Webera2b70922017-03-30 11:03:37 -0400885 ((int)(~(unsigned)(tid) << 3) | \
886 (int)((clock) | CPUCLOCK_PERTHREAD_MASK))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000887#endif
888
889#ifndef FUTEX_WAIT
890#define FUTEX_WAIT 0
891#endif
892#ifndef FUTEX_WAKE
893#define FUTEX_WAKE 1
894#endif
895#ifndef FUTEX_FD
896#define FUTEX_FD 2
897#endif
898#ifndef FUTEX_REQUEUE
899#define FUTEX_REQUEUE 3
900#endif
901#ifndef FUTEX_CMP_REQUEUE
902#define FUTEX_CMP_REQUEUE 4
903#endif
904#ifndef FUTEX_WAKE_OP
905#define FUTEX_WAKE_OP 5
906#endif
907#ifndef FUTEX_LOCK_PI
908#define FUTEX_LOCK_PI 6
909#endif
910#ifndef FUTEX_UNLOCK_PI
911#define FUTEX_UNLOCK_PI 7
912#endif
913#ifndef FUTEX_TRYLOCK_PI
914#define FUTEX_TRYLOCK_PI 8
915#endif
916#ifndef FUTEX_PRIVATE_FLAG
917#define FUTEX_PRIVATE_FLAG 128
918#endif
919#ifndef FUTEX_CMD_MASK
920#define FUTEX_CMD_MASK ~FUTEX_PRIVATE_FLAG
921#endif
922#ifndef FUTEX_WAIT_PRIVATE
923#define FUTEX_WAIT_PRIVATE (FUTEX_WAIT | FUTEX_PRIVATE_FLAG)
924#endif
925#ifndef FUTEX_WAKE_PRIVATE
926#define FUTEX_WAKE_PRIVATE (FUTEX_WAKE | FUTEX_PRIVATE_FLAG)
927#endif
928#ifndef FUTEX_REQUEUE_PRIVATE
929#define FUTEX_REQUEUE_PRIVATE (FUTEX_REQUEUE | FUTEX_PRIVATE_FLAG)
930#endif
931#ifndef FUTEX_CMP_REQUEUE_PRIVATE
932#define FUTEX_CMP_REQUEUE_PRIVATE (FUTEX_CMP_REQUEUE | FUTEX_PRIVATE_FLAG)
933#endif
934#ifndef FUTEX_WAKE_OP_PRIVATE
935#define FUTEX_WAKE_OP_PRIVATE (FUTEX_WAKE_OP | FUTEX_PRIVATE_FLAG)
936#endif
937#ifndef FUTEX_LOCK_PI_PRIVATE
938#define FUTEX_LOCK_PI_PRIVATE (FUTEX_LOCK_PI | FUTEX_PRIVATE_FLAG)
939#endif
940#ifndef FUTEX_UNLOCK_PI_PRIVATE
941#define FUTEX_UNLOCK_PI_PRIVATE (FUTEX_UNLOCK_PI | FUTEX_PRIVATE_FLAG)
942#endif
943#ifndef FUTEX_TRYLOCK_PI_PRIVATE
944#define FUTEX_TRYLOCK_PI_PRIVATE (FUTEX_TRYLOCK_PI | FUTEX_PRIVATE_FLAG)
945#endif
946
947
948#if defined(__x86_64__)
949#ifndef ARCH_SET_GS
950#define ARCH_SET_GS 0x1001
951#endif
952#ifndef ARCH_GET_GS
953#define ARCH_GET_GS 0x1004
954#endif
955#endif
956
957#if defined(__i386__)
958#ifndef __NR_quotactl
959#define __NR_quotactl 131
960#endif
961#ifndef __NR_setresuid
962#define __NR_setresuid 164
963#define __NR_getresuid 165
964#define __NR_setresgid 170
965#define __NR_getresgid 171
966#endif
967#ifndef __NR_rt_sigaction
968#define __NR_rt_sigreturn 173
969#define __NR_rt_sigaction 174
970#define __NR_rt_sigprocmask 175
971#define __NR_rt_sigpending 176
972#define __NR_rt_sigsuspend 179
973#endif
974#ifndef __NR_pread64
975#define __NR_pread64 180
976#endif
977#ifndef __NR_pwrite64
978#define __NR_pwrite64 181
979#endif
980#ifndef __NR_ugetrlimit
981#define __NR_ugetrlimit 191
982#endif
983#ifndef __NR_stat64
984#define __NR_stat64 195
985#endif
986#ifndef __NR_fstat64
987#define __NR_fstat64 197
988#endif
989#ifndef __NR_setresuid32
990#define __NR_setresuid32 208
991#define __NR_getresuid32 209
992#define __NR_setresgid32 210
993#define __NR_getresgid32 211
994#endif
995#ifndef __NR_setfsuid32
996#define __NR_setfsuid32 215
997#define __NR_setfsgid32 216
998#endif
999#ifndef __NR_getdents64
1000#define __NR_getdents64 220
1001#endif
1002#ifndef __NR_gettid
1003#define __NR_gettid 224
1004#endif
1005#ifndef __NR_readahead
1006#define __NR_readahead 225
1007#endif
1008#ifndef __NR_setxattr
1009#define __NR_setxattr 226
1010#endif
1011#ifndef __NR_lsetxattr
1012#define __NR_lsetxattr 227
1013#endif
1014#ifndef __NR_getxattr
1015#define __NR_getxattr 229
1016#endif
1017#ifndef __NR_lgetxattr
1018#define __NR_lgetxattr 230
1019#endif
1020#ifndef __NR_listxattr
1021#define __NR_listxattr 232
1022#endif
1023#ifndef __NR_llistxattr
1024#define __NR_llistxattr 233
1025#endif
1026#ifndef __NR_tkill
1027#define __NR_tkill 238
1028#endif
1029#ifndef __NR_futex
1030#define __NR_futex 240
1031#endif
1032#ifndef __NR_sched_setaffinity
1033#define __NR_sched_setaffinity 241
1034#define __NR_sched_getaffinity 242
1035#endif
1036#ifndef __NR_set_tid_address
1037#define __NR_set_tid_address 258
1038#endif
1039#ifndef __NR_clock_gettime
1040#define __NR_clock_gettime 265
1041#endif
1042#ifndef __NR_clock_getres
1043#define __NR_clock_getres 266
1044#endif
1045#ifndef __NR_statfs64
1046#define __NR_statfs64 268
1047#endif
1048#ifndef __NR_fstatfs64
1049#define __NR_fstatfs64 269
1050#endif
1051#ifndef __NR_fadvise64_64
1052#define __NR_fadvise64_64 272
1053#endif
1054#ifndef __NR_ioprio_set
1055#define __NR_ioprio_set 289
1056#endif
1057#ifndef __NR_ioprio_get
1058#define __NR_ioprio_get 290
1059#endif
1060#ifndef __NR_openat
1061#define __NR_openat 295
1062#endif
1063#ifndef __NR_fstatat64
1064#define __NR_fstatat64 300
1065#endif
1066#ifndef __NR_unlinkat
1067#define __NR_unlinkat 301
1068#endif
1069#ifndef __NR_move_pages
1070#define __NR_move_pages 317
1071#endif
1072#ifndef __NR_getcpu
1073#define __NR_getcpu 318
1074#endif
1075#ifndef __NR_fallocate
1076#define __NR_fallocate 324
1077#endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001078#ifndef __NR_getrandom
1079#define __NR_getrandom 355
1080#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001081/* End of i386 definitions */
1082#elif defined(__ARM_ARCH_3__) || defined(__ARM_EABI__)
1083#ifndef __NR_setresuid
1084#define __NR_setresuid (__NR_SYSCALL_BASE + 164)
1085#define __NR_getresuid (__NR_SYSCALL_BASE + 165)
1086#define __NR_setresgid (__NR_SYSCALL_BASE + 170)
1087#define __NR_getresgid (__NR_SYSCALL_BASE + 171)
1088#endif
1089#ifndef __NR_rt_sigaction
1090#define __NR_rt_sigreturn (__NR_SYSCALL_BASE + 173)
1091#define __NR_rt_sigaction (__NR_SYSCALL_BASE + 174)
1092#define __NR_rt_sigprocmask (__NR_SYSCALL_BASE + 175)
1093#define __NR_rt_sigpending (__NR_SYSCALL_BASE + 176)
1094#define __NR_rt_sigsuspend (__NR_SYSCALL_BASE + 179)
1095#endif
1096#ifndef __NR_pread64
1097#define __NR_pread64 (__NR_SYSCALL_BASE + 180)
1098#endif
1099#ifndef __NR_pwrite64
1100#define __NR_pwrite64 (__NR_SYSCALL_BASE + 181)
1101#endif
1102#ifndef __NR_ugetrlimit
1103#define __NR_ugetrlimit (__NR_SYSCALL_BASE + 191)
1104#endif
1105#ifndef __NR_stat64
1106#define __NR_stat64 (__NR_SYSCALL_BASE + 195)
1107#endif
1108#ifndef __NR_fstat64
1109#define __NR_fstat64 (__NR_SYSCALL_BASE + 197)
1110#endif
1111#ifndef __NR_setresuid32
1112#define __NR_setresuid32 (__NR_SYSCALL_BASE + 208)
1113#define __NR_getresuid32 (__NR_SYSCALL_BASE + 209)
1114#define __NR_setresgid32 (__NR_SYSCALL_BASE + 210)
1115#define __NR_getresgid32 (__NR_SYSCALL_BASE + 211)
1116#endif
1117#ifndef __NR_setfsuid32
1118#define __NR_setfsuid32 (__NR_SYSCALL_BASE + 215)
1119#define __NR_setfsgid32 (__NR_SYSCALL_BASE + 216)
1120#endif
1121#ifndef __NR_getdents64
1122#define __NR_getdents64 (__NR_SYSCALL_BASE + 217)
1123#endif
1124#ifndef __NR_gettid
1125#define __NR_gettid (__NR_SYSCALL_BASE + 224)
1126#endif
1127#ifndef __NR_readahead
1128#define __NR_readahead (__NR_SYSCALL_BASE + 225)
1129#endif
1130#ifndef __NR_setxattr
1131#define __NR_setxattr (__NR_SYSCALL_BASE + 226)
1132#endif
1133#ifndef __NR_lsetxattr
1134#define __NR_lsetxattr (__NR_SYSCALL_BASE + 227)
1135#endif
1136#ifndef __NR_getxattr
1137#define __NR_getxattr (__NR_SYSCALL_BASE + 229)
1138#endif
1139#ifndef __NR_lgetxattr
1140#define __NR_lgetxattr (__NR_SYSCALL_BASE + 230)
1141#endif
1142#ifndef __NR_listxattr
1143#define __NR_listxattr (__NR_SYSCALL_BASE + 232)
1144#endif
1145#ifndef __NR_llistxattr
1146#define __NR_llistxattr (__NR_SYSCALL_BASE + 233)
1147#endif
1148#ifndef __NR_tkill
1149#define __NR_tkill (__NR_SYSCALL_BASE + 238)
1150#endif
1151#ifndef __NR_futex
1152#define __NR_futex (__NR_SYSCALL_BASE + 240)
1153#endif
1154#ifndef __NR_sched_setaffinity
1155#define __NR_sched_setaffinity (__NR_SYSCALL_BASE + 241)
1156#define __NR_sched_getaffinity (__NR_SYSCALL_BASE + 242)
1157#endif
1158#ifndef __NR_set_tid_address
1159#define __NR_set_tid_address (__NR_SYSCALL_BASE + 256)
1160#endif
1161#ifndef __NR_clock_gettime
1162#define __NR_clock_gettime (__NR_SYSCALL_BASE + 263)
1163#endif
1164#ifndef __NR_clock_getres
1165#define __NR_clock_getres (__NR_SYSCALL_BASE + 264)
1166#endif
1167#ifndef __NR_statfs64
1168#define __NR_statfs64 (__NR_SYSCALL_BASE + 266)
1169#endif
1170#ifndef __NR_fstatfs64
1171#define __NR_fstatfs64 (__NR_SYSCALL_BASE + 267)
1172#endif
1173#ifndef __NR_ioprio_set
1174#define __NR_ioprio_set (__NR_SYSCALL_BASE + 314)
1175#endif
1176#ifndef __NR_ioprio_get
1177#define __NR_ioprio_get (__NR_SYSCALL_BASE + 315)
1178#endif
Matthew Denton92a65a82021-04-01 13:00:07 -07001179#ifndef __NR_fstatat64
1180#define __NR_fstatat64 (__NR_SYSCALL_BASE + 327)
1181#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001182#ifndef __NR_move_pages
1183#define __NR_move_pages (__NR_SYSCALL_BASE + 344)
1184#endif
1185#ifndef __NR_getcpu
1186#define __NR_getcpu (__NR_SYSCALL_BASE + 345)
1187#endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001188#ifndef __NR_getrandom
1189#define __NR_getrandom (__NR_SYSCALL_BASE + 384)
1190#endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04001191/* End of ARM 3/EABI definitions */
mingtaoxt xtc0c96892022-08-11 16:53:21 +08001192#elif defined(__aarch64__) || defined(__riscv) || defined(__loongarch_lp64)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00001193#ifndef __NR_setxattr
1194#define __NR_setxattr 5
1195#endif
1196#ifndef __NR_lsetxattr
1197#define __NR_lsetxattr 6
1198#endif
1199#ifndef __NR_getxattr
1200#define __NR_getxattr 8
1201#endif
1202#ifndef __NR_lgetxattr
1203#define __NR_lgetxattr 9
1204#endif
1205#ifndef __NR_listxattr
1206#define __NR_listxattr 11
1207#endif
1208#ifndef __NR_llistxattr
1209#define __NR_llistxattr 12
1210#endif
1211#ifndef __NR_ioprio_set
1212#define __NR_ioprio_set 30
1213#endif
1214#ifndef __NR_ioprio_get
1215#define __NR_ioprio_get 31
1216#endif
1217#ifndef __NR_unlinkat
1218#define __NR_unlinkat 35
1219#endif
1220#ifndef __NR_fallocate
1221#define __NR_fallocate 47
1222#endif
1223#ifndef __NR_openat
1224#define __NR_openat 56
1225#endif
1226#ifndef __NR_quotactl
1227#define __NR_quotactl 60
1228#endif
1229#ifndef __NR_getdents64
1230#define __NR_getdents64 61
1231#endif
1232#ifndef __NR_getdents
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04001233// when getdents is not available, getdents64 is used for both.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00001234#define __NR_getdents __NR_getdents64
1235#endif
1236#ifndef __NR_pread64
1237#define __NR_pread64 67
1238#endif
1239#ifndef __NR_pwrite64
1240#define __NR_pwrite64 68
1241#endif
1242#ifndef __NR_ppoll
1243#define __NR_ppoll 73
1244#endif
1245#ifndef __NR_readlinkat
1246#define __NR_readlinkat 78
1247#endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08001248#if !defined(__loongarch_lp64)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00001249#ifndef __NR_newfstatat
1250#define __NR_newfstatat 79
1251#endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08001252#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00001253#ifndef __NR_set_tid_address
1254#define __NR_set_tid_address 96
1255#endif
1256#ifndef __NR_futex
1257#define __NR_futex 98
1258#endif
1259#ifndef __NR_clock_gettime
1260#define __NR_clock_gettime 113
1261#endif
1262#ifndef __NR_clock_getres
1263#define __NR_clock_getres 114
1264#endif
1265#ifndef __NR_sched_setaffinity
1266#define __NR_sched_setaffinity 122
1267#define __NR_sched_getaffinity 123
1268#endif
1269#ifndef __NR_tkill
1270#define __NR_tkill 130
1271#endif
1272#ifndef __NR_setresuid
1273#define __NR_setresuid 147
1274#define __NR_getresuid 148
1275#define __NR_setresgid 149
1276#define __NR_getresgid 150
1277#endif
1278#ifndef __NR_gettid
1279#define __NR_gettid 178
1280#endif
1281#ifndef __NR_readahead
1282#define __NR_readahead 213
1283#endif
1284#ifndef __NR_fadvise64
1285#define __NR_fadvise64 223
1286#endif
1287#ifndef __NR_move_pages
1288#define __NR_move_pages 239
1289#endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001290#ifndef __NR_getrandom
1291#define __NR_getrandom 278
1292#endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08001293#ifndef __NR_statx
1294#define __NR_statx 291
1295#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001296#elif defined(__x86_64__)
1297#ifndef __NR_pread64
1298#define __NR_pread64 17
1299#endif
1300#ifndef __NR_pwrite64
1301#define __NR_pwrite64 18
1302#endif
1303#ifndef __NR_setresuid
1304#define __NR_setresuid 117
1305#define __NR_getresuid 118
1306#define __NR_setresgid 119
1307#define __NR_getresgid 120
1308#endif
1309#ifndef __NR_quotactl
1310#define __NR_quotactl 179
1311#endif
1312#ifndef __NR_gettid
1313#define __NR_gettid 186
1314#endif
1315#ifndef __NR_readahead
1316#define __NR_readahead 187
1317#endif
1318#ifndef __NR_setxattr
1319#define __NR_setxattr 188
1320#endif
1321#ifndef __NR_lsetxattr
1322#define __NR_lsetxattr 189
1323#endif
1324#ifndef __NR_getxattr
1325#define __NR_getxattr 191
1326#endif
1327#ifndef __NR_lgetxattr
1328#define __NR_lgetxattr 192
1329#endif
1330#ifndef __NR_listxattr
1331#define __NR_listxattr 194
1332#endif
1333#ifndef __NR_llistxattr
1334#define __NR_llistxattr 195
1335#endif
1336#ifndef __NR_tkill
1337#define __NR_tkill 200
1338#endif
1339#ifndef __NR_futex
1340#define __NR_futex 202
1341#endif
1342#ifndef __NR_sched_setaffinity
1343#define __NR_sched_setaffinity 203
1344#define __NR_sched_getaffinity 204
1345#endif
1346#ifndef __NR_getdents64
1347#define __NR_getdents64 217
1348#endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04001349#ifndef __NR_getdents
1350// when getdents is not available, getdents64 is used for both.
1351#define __NR_getdents __NR_getdents64
1352#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001353#ifndef __NR_set_tid_address
1354#define __NR_set_tid_address 218
1355#endif
1356#ifndef __NR_fadvise64
1357#define __NR_fadvise64 221
1358#endif
1359#ifndef __NR_clock_gettime
1360#define __NR_clock_gettime 228
1361#endif
1362#ifndef __NR_clock_getres
1363#define __NR_clock_getres 229
1364#endif
1365#ifndef __NR_ioprio_set
1366#define __NR_ioprio_set 251
1367#endif
1368#ifndef __NR_ioprio_get
1369#define __NR_ioprio_get 252
1370#endif
1371#ifndef __NR_openat
1372#define __NR_openat 257
1373#endif
1374#ifndef __NR_newfstatat
1375#define __NR_newfstatat 262
1376#endif
1377#ifndef __NR_unlinkat
1378#define __NR_unlinkat 263
1379#endif
1380#ifndef __NR_move_pages
1381#define __NR_move_pages 279
1382#endif
1383#ifndef __NR_fallocate
1384#define __NR_fallocate 285
1385#endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001386#ifndef __NR_getrandom
1387#define __NR_getrandom 318
1388#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001389/* End of x86-64 definitions */
1390#elif defined(__mips__)
1391#if _MIPS_SIM == _MIPS_SIM_ABI32
1392#ifndef __NR_setresuid
1393#define __NR_setresuid (__NR_Linux + 185)
1394#define __NR_getresuid (__NR_Linux + 186)
1395#define __NR_setresgid (__NR_Linux + 190)
1396#define __NR_getresgid (__NR_Linux + 191)
1397#endif
1398#ifndef __NR_rt_sigaction
1399#define __NR_rt_sigreturn (__NR_Linux + 193)
1400#define __NR_rt_sigaction (__NR_Linux + 194)
1401#define __NR_rt_sigprocmask (__NR_Linux + 195)
1402#define __NR_rt_sigpending (__NR_Linux + 196)
1403#define __NR_rt_sigsuspend (__NR_Linux + 199)
1404#endif
1405#ifndef __NR_pread64
1406#define __NR_pread64 (__NR_Linux + 200)
1407#endif
1408#ifndef __NR_pwrite64
1409#define __NR_pwrite64 (__NR_Linux + 201)
1410#endif
1411#ifndef __NR_stat64
1412#define __NR_stat64 (__NR_Linux + 213)
1413#endif
1414#ifndef __NR_fstat64
1415#define __NR_fstat64 (__NR_Linux + 215)
1416#endif
1417#ifndef __NR_getdents64
1418#define __NR_getdents64 (__NR_Linux + 219)
1419#endif
1420#ifndef __NR_gettid
1421#define __NR_gettid (__NR_Linux + 222)
1422#endif
1423#ifndef __NR_readahead
1424#define __NR_readahead (__NR_Linux + 223)
1425#endif
1426#ifndef __NR_setxattr
1427#define __NR_setxattr (__NR_Linux + 224)
1428#endif
1429#ifndef __NR_lsetxattr
1430#define __NR_lsetxattr (__NR_Linux + 225)
1431#endif
1432#ifndef __NR_getxattr
1433#define __NR_getxattr (__NR_Linux + 227)
1434#endif
1435#ifndef __NR_lgetxattr
1436#define __NR_lgetxattr (__NR_Linux + 228)
1437#endif
1438#ifndef __NR_listxattr
1439#define __NR_listxattr (__NR_Linux + 230)
1440#endif
1441#ifndef __NR_llistxattr
1442#define __NR_llistxattr (__NR_Linux + 231)
1443#endif
1444#ifndef __NR_tkill
1445#define __NR_tkill (__NR_Linux + 236)
1446#endif
1447#ifndef __NR_futex
1448#define __NR_futex (__NR_Linux + 238)
1449#endif
1450#ifndef __NR_sched_setaffinity
1451#define __NR_sched_setaffinity (__NR_Linux + 239)
1452#define __NR_sched_getaffinity (__NR_Linux + 240)
1453#endif
1454#ifndef __NR_set_tid_address
1455#define __NR_set_tid_address (__NR_Linux + 252)
1456#endif
1457#ifndef __NR_statfs64
1458#define __NR_statfs64 (__NR_Linux + 255)
1459#endif
1460#ifndef __NR_fstatfs64
1461#define __NR_fstatfs64 (__NR_Linux + 256)
1462#endif
1463#ifndef __NR_clock_gettime
1464#define __NR_clock_gettime (__NR_Linux + 263)
1465#endif
1466#ifndef __NR_clock_getres
1467#define __NR_clock_getres (__NR_Linux + 264)
1468#endif
1469#ifndef __NR_openat
1470#define __NR_openat (__NR_Linux + 288)
1471#endif
1472#ifndef __NR_fstatat
1473#define __NR_fstatat (__NR_Linux + 293)
1474#endif
1475#ifndef __NR_unlinkat
1476#define __NR_unlinkat (__NR_Linux + 294)
1477#endif
1478#ifndef __NR_move_pages
1479#define __NR_move_pages (__NR_Linux + 308)
1480#endif
1481#ifndef __NR_getcpu
1482#define __NR_getcpu (__NR_Linux + 312)
1483#endif
1484#ifndef __NR_ioprio_set
1485#define __NR_ioprio_set (__NR_Linux + 314)
1486#endif
1487#ifndef __NR_ioprio_get
1488#define __NR_ioprio_get (__NR_Linux + 315)
1489#endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001490#ifndef __NR_getrandom
1491#define __NR_getrandom (__NR_Linux + 353)
1492#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001493/* End of MIPS (old 32bit API) definitions */
1494#elif _MIPS_SIM == _MIPS_SIM_ABI64
1495#ifndef __NR_pread64
1496#define __NR_pread64 (__NR_Linux + 16)
1497#endif
1498#ifndef __NR_pwrite64
1499#define __NR_pwrite64 (__NR_Linux + 17)
1500#endif
1501#ifndef __NR_setresuid
1502#define __NR_setresuid (__NR_Linux + 115)
1503#define __NR_getresuid (__NR_Linux + 116)
1504#define __NR_setresgid (__NR_Linux + 117)
1505#define __NR_getresgid (__NR_Linux + 118)
1506#endif
1507#ifndef __NR_gettid
1508#define __NR_gettid (__NR_Linux + 178)
1509#endif
1510#ifndef __NR_readahead
1511#define __NR_readahead (__NR_Linux + 179)
1512#endif
1513#ifndef __NR_setxattr
1514#define __NR_setxattr (__NR_Linux + 180)
1515#endif
1516#ifndef __NR_lsetxattr
1517#define __NR_lsetxattr (__NR_Linux + 181)
1518#endif
1519#ifndef __NR_getxattr
1520#define __NR_getxattr (__NR_Linux + 183)
1521#endif
1522#ifndef __NR_lgetxattr
1523#define __NR_lgetxattr (__NR_Linux + 184)
1524#endif
1525#ifndef __NR_listxattr
1526#define __NR_listxattr (__NR_Linux + 186)
1527#endif
1528#ifndef __NR_llistxattr
1529#define __NR_llistxattr (__NR_Linux + 187)
1530#endif
1531#ifndef __NR_tkill
1532#define __NR_tkill (__NR_Linux + 192)
1533#endif
1534#ifndef __NR_futex
1535#define __NR_futex (__NR_Linux + 194)
1536#endif
1537#ifndef __NR_sched_setaffinity
1538#define __NR_sched_setaffinity (__NR_Linux + 195)
1539#define __NR_sched_getaffinity (__NR_Linux + 196)
1540#endif
1541#ifndef __NR_set_tid_address
1542#define __NR_set_tid_address (__NR_Linux + 212)
1543#endif
1544#ifndef __NR_clock_gettime
1545#define __NR_clock_gettime (__NR_Linux + 222)
1546#endif
1547#ifndef __NR_clock_getres
1548#define __NR_clock_getres (__NR_Linux + 223)
1549#endif
1550#ifndef __NR_openat
1551#define __NR_openat (__NR_Linux + 247)
1552#endif
1553#ifndef __NR_fstatat
1554#define __NR_fstatat (__NR_Linux + 252)
1555#endif
1556#ifndef __NR_unlinkat
1557#define __NR_unlinkat (__NR_Linux + 253)
1558#endif
1559#ifndef __NR_move_pages
1560#define __NR_move_pages (__NR_Linux + 267)
1561#endif
1562#ifndef __NR_getcpu
1563#define __NR_getcpu (__NR_Linux + 271)
1564#endif
1565#ifndef __NR_ioprio_set
1566#define __NR_ioprio_set (__NR_Linux + 273)
1567#endif
1568#ifndef __NR_ioprio_get
1569#define __NR_ioprio_get (__NR_Linux + 274)
1570#endif
Yu Yind9ad2962020-11-24 16:49:22 +08001571#ifndef __NR_getrandom
1572#define __NR_getrandom (__NR_Linux + 313)
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001573#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001574/* End of MIPS (64bit API) definitions */
1575#else
1576#ifndef __NR_setresuid
1577#define __NR_setresuid (__NR_Linux + 115)
1578#define __NR_getresuid (__NR_Linux + 116)
1579#define __NR_setresgid (__NR_Linux + 117)
1580#define __NR_getresgid (__NR_Linux + 118)
1581#endif
1582#ifndef __NR_gettid
1583#define __NR_gettid (__NR_Linux + 178)
1584#endif
1585#ifndef __NR_readahead
1586#define __NR_readahead (__NR_Linux + 179)
1587#endif
1588#ifndef __NR_setxattr
1589#define __NR_setxattr (__NR_Linux + 180)
1590#endif
1591#ifndef __NR_lsetxattr
1592#define __NR_lsetxattr (__NR_Linux + 181)
1593#endif
1594#ifndef __NR_getxattr
1595#define __NR_getxattr (__NR_Linux + 183)
1596#endif
1597#ifndef __NR_lgetxattr
1598#define __NR_lgetxattr (__NR_Linux + 184)
1599#endif
1600#ifndef __NR_listxattr
1601#define __NR_listxattr (__NR_Linux + 186)
1602#endif
1603#ifndef __NR_llistxattr
1604#define __NR_llistxattr (__NR_Linux + 187)
1605#endif
1606#ifndef __NR_tkill
1607#define __NR_tkill (__NR_Linux + 192)
1608#endif
1609#ifndef __NR_futex
1610#define __NR_futex (__NR_Linux + 194)
1611#endif
1612#ifndef __NR_sched_setaffinity
1613#define __NR_sched_setaffinity (__NR_Linux + 195)
1614#define __NR_sched_getaffinity (__NR_Linux + 196)
1615#endif
1616#ifndef __NR_set_tid_address
1617#define __NR_set_tid_address (__NR_Linux + 213)
1618#endif
1619#ifndef __NR_statfs64
1620#define __NR_statfs64 (__NR_Linux + 217)
1621#endif
1622#ifndef __NR_fstatfs64
1623#define __NR_fstatfs64 (__NR_Linux + 218)
1624#endif
1625#ifndef __NR_clock_gettime
1626#define __NR_clock_gettime (__NR_Linux + 226)
1627#endif
1628#ifndef __NR_clock_getres
1629#define __NR_clock_getres (__NR_Linux + 227)
1630#endif
1631#ifndef __NR_openat
1632#define __NR_openat (__NR_Linux + 251)
1633#endif
1634#ifndef __NR_fstatat
1635#define __NR_fstatat (__NR_Linux + 256)
1636#endif
1637#ifndef __NR_unlinkat
1638#define __NR_unlinkat (__NR_Linux + 257)
1639#endif
1640#ifndef __NR_move_pages
1641#define __NR_move_pages (__NR_Linux + 271)
1642#endif
1643#ifndef __NR_getcpu
1644#define __NR_getcpu (__NR_Linux + 275)
1645#endif
1646#ifndef __NR_ioprio_set
1647#define __NR_ioprio_set (__NR_Linux + 277)
1648#endif
1649#ifndef __NR_ioprio_get
1650#define __NR_ioprio_get (__NR_Linux + 278)
1651#endif
1652/* End of MIPS (new 32bit API) definitions */
1653#endif
1654/* End of MIPS definitions */
1655#elif defined(__PPC__)
1656#ifndef __NR_setfsuid
1657#define __NR_setfsuid 138
1658#define __NR_setfsgid 139
1659#endif
1660#ifndef __NR_setresuid
1661#define __NR_setresuid 164
1662#define __NR_getresuid 165
1663#define __NR_setresgid 169
1664#define __NR_getresgid 170
1665#endif
1666#ifndef __NR_rt_sigaction
1667#define __NR_rt_sigreturn 172
1668#define __NR_rt_sigaction 173
1669#define __NR_rt_sigprocmask 174
1670#define __NR_rt_sigpending 175
1671#define __NR_rt_sigsuspend 178
1672#endif
1673#ifndef __NR_pread64
1674#define __NR_pread64 179
1675#endif
1676#ifndef __NR_pwrite64
1677#define __NR_pwrite64 180
1678#endif
1679#ifndef __NR_ugetrlimit
1680#define __NR_ugetrlimit 190
1681#endif
1682#ifndef __NR_readahead
1683#define __NR_readahead 191
1684#endif
1685#ifndef __NR_stat64
1686#define __NR_stat64 195
1687#endif
1688#ifndef __NR_fstat64
1689#define __NR_fstat64 197
1690#endif
1691#ifndef __NR_getdents64
1692#define __NR_getdents64 202
1693#endif
1694#ifndef __NR_gettid
1695#define __NR_gettid 207
1696#endif
1697#ifndef __NR_tkill
1698#define __NR_tkill 208
1699#endif
1700#ifndef __NR_setxattr
1701#define __NR_setxattr 209
1702#endif
1703#ifndef __NR_lsetxattr
1704#define __NR_lsetxattr 210
1705#endif
1706#ifndef __NR_getxattr
1707#define __NR_getxattr 212
1708#endif
1709#ifndef __NR_lgetxattr
1710#define __NR_lgetxattr 213
1711#endif
1712#ifndef __NR_listxattr
1713#define __NR_listxattr 215
1714#endif
1715#ifndef __NR_llistxattr
1716#define __NR_llistxattr 216
1717#endif
1718#ifndef __NR_futex
1719#define __NR_futex 221
1720#endif
1721#ifndef __NR_sched_setaffinity
1722#define __NR_sched_setaffinity 222
1723#define __NR_sched_getaffinity 223
1724#endif
1725#ifndef __NR_set_tid_address
1726#define __NR_set_tid_address 232
1727#endif
1728#ifndef __NR_clock_gettime
1729#define __NR_clock_gettime 246
1730#endif
1731#ifndef __NR_clock_getres
1732#define __NR_clock_getres 247
1733#endif
1734#ifndef __NR_statfs64
1735#define __NR_statfs64 252
1736#endif
1737#ifndef __NR_fstatfs64
1738#define __NR_fstatfs64 253
1739#endif
1740#ifndef __NR_fadvise64_64
1741#define __NR_fadvise64_64 254
1742#endif
1743#ifndef __NR_ioprio_set
1744#define __NR_ioprio_set 273
1745#endif
1746#ifndef __NR_ioprio_get
1747#define __NR_ioprio_get 274
1748#endif
1749#ifndef __NR_openat
1750#define __NR_openat 286
1751#endif
1752#ifndef __NR_fstatat64
1753#define __NR_fstatat64 291
1754#endif
1755#ifndef __NR_unlinkat
1756#define __NR_unlinkat 292
1757#endif
1758#ifndef __NR_move_pages
1759#define __NR_move_pages 301
1760#endif
1761#ifndef __NR_getcpu
1762#define __NR_getcpu 302
1763#endif
1764/* End of powerpc defininitions */
Bryan Chan3f6478a2016-06-14 08:38:17 -04001765#elif defined(__s390__)
1766#ifndef __NR_quotactl
1767#define __NR_quotactl 131
1768#endif
1769#ifndef __NR_rt_sigreturn
1770#define __NR_rt_sigreturn 173
1771#endif
1772#ifndef __NR_rt_sigaction
1773#define __NR_rt_sigaction 174
1774#endif
1775#ifndef __NR_rt_sigprocmask
1776#define __NR_rt_sigprocmask 175
1777#endif
1778#ifndef __NR_rt_sigpending
1779#define __NR_rt_sigpending 176
1780#endif
1781#ifndef __NR_rt_sigsuspend
1782#define __NR_rt_sigsuspend 179
1783#endif
1784#ifndef __NR_pread64
1785#define __NR_pread64 180
1786#endif
1787#ifndef __NR_pwrite64
1788#define __NR_pwrite64 181
1789#endif
1790#ifndef __NR_getdents64
1791#define __NR_getdents64 220
1792#endif
1793#ifndef __NR_readahead
1794#define __NR_readahead 222
1795#endif
1796#ifndef __NR_setxattr
1797#define __NR_setxattr 224
1798#endif
1799#ifndef __NR_lsetxattr
1800#define __NR_lsetxattr 225
1801#endif
1802#ifndef __NR_getxattr
1803#define __NR_getxattr 227
1804#endif
1805#ifndef __NR_lgetxattr
1806#define __NR_lgetxattr 228
1807#endif
1808#ifndef __NR_listxattr
1809#define __NR_listxattr 230
1810#endif
1811#ifndef __NR_llistxattr
1812#define __NR_llistxattr 231
1813#endif
1814#ifndef __NR_gettid
1815#define __NR_gettid 236
1816#endif
1817#ifndef __NR_tkill
1818#define __NR_tkill 237
1819#endif
1820#ifndef __NR_futex
1821#define __NR_futex 238
1822#endif
1823#ifndef __NR_sched_setaffinity
1824#define __NR_sched_setaffinity 239
1825#endif
1826#ifndef __NR_sched_getaffinity
1827#define __NR_sched_getaffinity 240
1828#endif
1829#ifndef __NR_set_tid_address
1830#define __NR_set_tid_address 252
1831#endif
1832#ifndef __NR_clock_gettime
1833#define __NR_clock_gettime 260
1834#endif
1835#ifndef __NR_clock_getres
1836#define __NR_clock_getres 261
1837#endif
1838#ifndef __NR_statfs64
1839#define __NR_statfs64 265
1840#endif
1841#ifndef __NR_fstatfs64
1842#define __NR_fstatfs64 266
1843#endif
1844#ifndef __NR_ioprio_set
1845#define __NR_ioprio_set 282
1846#endif
1847#ifndef __NR_ioprio_get
1848#define __NR_ioprio_get 283
1849#endif
1850#ifndef __NR_openat
1851#define __NR_openat 288
1852#endif
1853#ifndef __NR_unlinkat
1854#define __NR_unlinkat 294
1855#endif
1856#ifndef __NR_move_pages
1857#define __NR_move_pages 310
1858#endif
1859#ifndef __NR_getcpu
1860#define __NR_getcpu 311
1861#endif
1862#ifndef __NR_fallocate
1863#define __NR_fallocate 314
1864#endif
1865/* Some syscalls are named/numbered differently between s390 and s390x. */
1866#ifdef __s390x__
1867# ifndef __NR_getrlimit
1868# define __NR_getrlimit 191
1869# endif
1870# ifndef __NR_setresuid
1871# define __NR_setresuid 208
1872# endif
1873# ifndef __NR_getresuid
1874# define __NR_getresuid 209
1875# endif
1876# ifndef __NR_setresgid
1877# define __NR_setresgid 210
1878# endif
1879# ifndef __NR_getresgid
1880# define __NR_getresgid 211
1881# endif
1882# ifndef __NR_setfsuid
1883# define __NR_setfsuid 215
1884# endif
1885# ifndef __NR_setfsgid
1886# define __NR_setfsgid 216
1887# endif
1888# ifndef __NR_fadvise64
1889# define __NR_fadvise64 253
1890# endif
1891# ifndef __NR_newfstatat
1892# define __NR_newfstatat 293
1893# endif
1894#else /* __s390x__ */
1895# ifndef __NR_getrlimit
1896# define __NR_getrlimit 76
1897# endif
1898# ifndef __NR_setfsuid
1899# define __NR_setfsuid 138
1900# endif
1901# ifndef __NR_setfsgid
1902# define __NR_setfsgid 139
1903# endif
1904# ifndef __NR_setresuid
1905# define __NR_setresuid 164
1906# endif
1907# ifndef __NR_getresuid
1908# define __NR_getresuid 165
1909# endif
1910# ifndef __NR_setresgid
1911# define __NR_setresgid 170
1912# endif
1913# ifndef __NR_getresgid
1914# define __NR_getresgid 171
1915# endif
1916# ifndef __NR_ugetrlimit
1917# define __NR_ugetrlimit 191
1918# endif
1919# ifndef __NR_mmap2
1920# define __NR_mmap2 192
1921# endif
1922# ifndef __NR_setresuid32
1923# define __NR_setresuid32 208
1924# endif
1925# ifndef __NR_getresuid32
1926# define __NR_getresuid32 209
1927# endif
1928# ifndef __NR_setresgid32
1929# define __NR_setresgid32 210
1930# endif
1931# ifndef __NR_getresgid32
1932# define __NR_getresgid32 211
1933# endif
1934# ifndef __NR_setfsuid32
1935# define __NR_setfsuid32 215
1936# endif
1937# ifndef __NR_setfsgid32
1938# define __NR_setfsgid32 216
1939# endif
1940# ifndef __NR_fadvise64_64
1941# define __NR_fadvise64_64 264
1942# endif
1943# ifndef __NR_fstatat64
1944# define __NR_fstatat64 293
1945# endif
1946#endif /* __s390__ */
1947/* End of s390/s390x definitions */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001948#endif
1949
1950
1951/* After forking, we must make sure to only call system calls. */
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001952#if defined(__BOUNDED_POINTERS__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001953 #error "Need to port invocations of syscalls for bounded ptrs"
1954#else
1955 /* The core dumper and the thread lister get executed after threads
1956 * have been suspended. As a consequence, we cannot call any functions
1957 * that acquire locks. Unfortunately, libc wraps most system calls
1958 * (e.g. in order to implement pthread_atfork, and to make calls
1959 * cancellable), which means we cannot call these functions. Instead,
1960 * we have to call syscall() directly.
1961 */
1962 #undef LSS_ERRNO
1963 #ifdef SYS_ERRNO
1964 /* Allow the including file to override the location of errno. This can
1965 * be useful when using clone() with the CLONE_VM option.
1966 */
1967 #define LSS_ERRNO SYS_ERRNO
1968 #else
1969 #define LSS_ERRNO errno
1970 #endif
1971
1972 #undef LSS_INLINE
1973 #ifdef SYS_INLINE
1974 #define LSS_INLINE SYS_INLINE
1975 #else
1976 #define LSS_INLINE static inline
1977 #endif
1978
1979 /* Allow the including file to override the prefix used for all new
1980 * system calls. By default, it will be set to "sys_".
1981 */
1982 #undef LSS_NAME
1983 #ifndef SYS_PREFIX
1984 #define LSS_NAME(name) sys_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001985 #elif defined(SYS_PREFIX) && SYS_PREFIX < 0
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001986 #define LSS_NAME(name) name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001987 #elif defined(SYS_PREFIX) && SYS_PREFIX == 0
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001988 #define LSS_NAME(name) sys0_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001989 #elif defined(SYS_PREFIX) && SYS_PREFIX == 1
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001990 #define LSS_NAME(name) sys1_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001991 #elif defined(SYS_PREFIX) && SYS_PREFIX == 2
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001992 #define LSS_NAME(name) sys2_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001993 #elif defined(SYS_PREFIX) && SYS_PREFIX == 3
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001994 #define LSS_NAME(name) sys3_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001995 #elif defined(SYS_PREFIX) && SYS_PREFIX == 4
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001996 #define LSS_NAME(name) sys4_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001997 #elif defined(SYS_PREFIX) && SYS_PREFIX == 5
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001998 #define LSS_NAME(name) sys5_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001999 #elif defined(SYS_PREFIX) && SYS_PREFIX == 6
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002000 #define LSS_NAME(name) sys6_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00002001 #elif defined(SYS_PREFIX) && SYS_PREFIX == 7
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002002 #define LSS_NAME(name) sys7_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00002003 #elif defined(SYS_PREFIX) && SYS_PREFIX == 8
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002004 #define LSS_NAME(name) sys8_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00002005 #elif defined(SYS_PREFIX) && SYS_PREFIX == 9
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002006 #define LSS_NAME(name) sys9_##name
2007 #endif
2008
2009 #undef LSS_RETURN
Askar Safine1e7b0a2021-04-12 14:03:02 +03002010 #if defined(__i386__) || defined(__x86_64__) || defined(__ARM_ARCH_3__) \
2011 || defined(__ARM_EABI__) || defined(__aarch64__) || defined(__s390__) \
mingtaoxt xtc0c96892022-08-11 16:53:21 +08002012 || defined(__e2k__) || defined(__riscv) || defined(__loongarch_lp64)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002013 /* Failing system calls return a negative result in the range of
2014 * -1..-4095. These are "errno" values with the sign inverted.
2015 */
2016 #define LSS_RETURN(type, res) \
2017 do { \
2018 if ((unsigned long)(res) >= (unsigned long)(-4095)) { \
Peter Kasting0d6435b2022-07-20 20:21:35 +00002019 LSS_ERRNO = (int)(-(res)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002020 res = -1; \
2021 } \
2022 return (type) (res); \
2023 } while (0)
2024 #elif defined(__mips__)
2025 /* On MIPS, failing system calls return -1, and set errno in a
2026 * separate CPU register.
2027 */
2028 #define LSS_RETURN(type, res, err) \
2029 do { \
2030 if (err) { \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002031 unsigned long __errnovalue = (res); \
2032 LSS_ERRNO = __errnovalue; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002033 res = -1; \
2034 } \
2035 return (type) (res); \
2036 } while (0)
2037 #elif defined(__PPC__)
2038 /* On PPC, failing system calls return -1, and set errno in a
2039 * separate CPU register. See linux/unistd.h.
2040 */
2041 #define LSS_RETURN(type, res, err) \
2042 do { \
2043 if (err & 0x10000000 ) { \
2044 LSS_ERRNO = (res); \
2045 res = -1; \
2046 } \
2047 return (type) (res); \
2048 } while (0)
2049 #endif
2050 #if defined(__i386__)
2051 /* In PIC mode (e.g. when building shared libraries), gcc for i386
2052 * reserves ebx. Unfortunately, most distribution ship with implementations
2053 * of _syscallX() which clobber ebx.
2054 * Also, most definitions of _syscallX() neglect to mark "memory" as being
2055 * clobbered. This causes problems with compilers, that do a better job
2056 * at optimizing across __asm__ calls.
2057 * So, we just have to redefine all of the _syscallX() macros.
2058 */
2059 #undef LSS_ENTRYPOINT
2060 #ifdef SYS_SYSCALL_ENTRYPOINT
2061 static inline void (**LSS_NAME(get_syscall_entrypoint)(void))(void) {
2062 void (**entrypoint)(void);
2063 asm volatile(".bss\n"
2064 ".align 8\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002065 ".globl " SYS_SYSCALL_ENTRYPOINT "\n"
2066 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002067 ".previous\n"
2068 /* This logically does 'lea "SYS_SYSCALL_ENTRYPOINT", %0' */
2069 "call 0f\n"
2070 "0:pop %0\n"
2071 "add $_GLOBAL_OFFSET_TABLE_+[.-0b], %0\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002072 "mov " SYS_SYSCALL_ENTRYPOINT "@GOT(%0), %0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002073 : "=r"(entrypoint));
2074 return entrypoint;
2075 }
2076
2077 #define LSS_ENTRYPOINT ".bss\n" \
2078 ".align 8\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002079 ".globl " SYS_SYSCALL_ENTRYPOINT "\n" \
2080 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002081 ".previous\n" \
2082 /* Check the SYS_SYSCALL_ENTRYPOINT vector */ \
2083 "push %%eax\n" \
2084 "call 10000f\n" \
2085 "10000:pop %%eax\n" \
2086 "add $_GLOBAL_OFFSET_TABLE_+[.-10000b], %%eax\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002087 "mov " SYS_SYSCALL_ENTRYPOINT \
2088 "@GOT(%%eax), %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002089 "mov 0(%%eax), %%eax\n" \
2090 "test %%eax, %%eax\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00002091 "jz 10002f\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002092 "push %%eax\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00002093 "call 10001f\n" \
2094 "10001:pop %%eax\n" \
2095 "add $(10003f-10001b), %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002096 "xchg 4(%%esp), %%eax\n" \
2097 "ret\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00002098 "10002:pop %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002099 "int $0x80\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00002100 "10003:\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002101 #else
2102 #define LSS_ENTRYPOINT "int $0x80\n"
2103 #endif
2104 #undef LSS_BODY
2105 #define LSS_BODY(type,args...) \
2106 long __res; \
2107 __asm__ __volatile__("push %%ebx\n" \
2108 "movl %2,%%ebx\n" \
2109 LSS_ENTRYPOINT \
2110 "pop %%ebx" \
2111 args \
Joshua Perazabe2d5a82020-04-15 14:36:21 -07002112 : "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002113 LSS_RETURN(type,__res)
2114 #undef _syscall0
2115 #define _syscall0(type,name) \
2116 type LSS_NAME(name)(void) { \
2117 long __res; \
2118 __asm__ volatile(LSS_ENTRYPOINT \
2119 : "=a" (__res) \
2120 : "0" (__NR_##name) \
Khem Raj8048ece2018-12-22 16:07:39 -08002121 : "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002122 LSS_RETURN(type,__res); \
2123 }
2124 #undef _syscall1
2125 #define _syscall1(type,name,type1,arg1) \
2126 type LSS_NAME(name)(type1 arg1) { \
2127 LSS_BODY(type, \
2128 : "=a" (__res) \
2129 : "0" (__NR_##name), "ri" ((long)(arg1))); \
2130 }
2131 #undef _syscall2
2132 #define _syscall2(type,name,type1,arg1,type2,arg2) \
2133 type LSS_NAME(name)(type1 arg1,type2 arg2) { \
2134 LSS_BODY(type, \
2135 : "=a" (__res) \
2136 : "0" (__NR_##name),"ri" ((long)(arg1)), "c" ((long)(arg2))); \
2137 }
2138 #undef _syscall3
2139 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
2140 type LSS_NAME(name)(type1 arg1,type2 arg2,type3 arg3) { \
2141 LSS_BODY(type, \
2142 : "=a" (__res) \
2143 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
2144 "d" ((long)(arg3))); \
2145 }
2146 #undef _syscall4
2147 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2148 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2149 LSS_BODY(type, \
2150 : "=a" (__res) \
2151 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
2152 "d" ((long)(arg3)),"S" ((long)(arg4))); \
2153 }
2154 #undef _syscall5
2155 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2156 type5,arg5) \
2157 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2158 type5 arg5) { \
2159 long __res; \
2160 __asm__ __volatile__("push %%ebx\n" \
2161 "movl %2,%%ebx\n" \
2162 "movl %1,%%eax\n" \
2163 LSS_ENTRYPOINT \
2164 "pop %%ebx" \
2165 : "=a" (__res) \
2166 : "i" (__NR_##name), "ri" ((long)(arg1)), \
2167 "c" ((long)(arg2)), "d" ((long)(arg3)), \
2168 "S" ((long)(arg4)), "D" ((long)(arg5)) \
Joshua Perazabe2d5a82020-04-15 14:36:21 -07002169 : "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002170 LSS_RETURN(type,__res); \
2171 }
2172 #undef _syscall6
2173 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2174 type5,arg5,type6,arg6) \
2175 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2176 type5 arg5, type6 arg6) { \
2177 long __res; \
2178 struct { long __a1; long __a6; } __s = { (long)arg1, (long) arg6 }; \
2179 __asm__ __volatile__("push %%ebp\n" \
2180 "push %%ebx\n" \
mseaborn@chromium.orge96ade32012-10-27 17:47:38 +00002181 "movl 4(%2),%%ebp\n" \
2182 "movl 0(%2), %%ebx\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002183 "movl %1,%%eax\n" \
2184 LSS_ENTRYPOINT \
2185 "pop %%ebx\n" \
2186 "pop %%ebp" \
2187 : "=a" (__res) \
2188 : "i" (__NR_##name), "0" ((long)(&__s)), \
2189 "c" ((long)(arg2)), "d" ((long)(arg3)), \
2190 "S" ((long)(arg4)), "D" ((long)(arg5)) \
Joshua Perazabe2d5a82020-04-15 14:36:21 -07002191 : "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002192 LSS_RETURN(type,__res); \
2193 }
2194 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2195 int flags, void *arg, int *parent_tidptr,
2196 void *newtls, int *child_tidptr) {
2197 long __res;
2198 __asm__ __volatile__(/* if (fn == NULL)
2199 * return -EINVAL;
2200 */
2201 "movl %3,%%ecx\n"
2202 "jecxz 1f\n"
2203
2204 /* if (child_stack == NULL)
2205 * return -EINVAL;
2206 */
2207 "movl %4,%%ecx\n"
2208 "jecxz 1f\n"
2209
2210 /* Set up alignment of the child stack:
2211 * child_stack = (child_stack & ~0xF) - 20;
2212 */
2213 "andl $-16,%%ecx\n"
2214 "subl $20,%%ecx\n"
2215
2216 /* Push "arg" and "fn" onto the stack that will be
2217 * used by the child.
2218 */
2219 "movl %6,%%eax\n"
2220 "movl %%eax,4(%%ecx)\n"
2221 "movl %3,%%eax\n"
2222 "movl %%eax,(%%ecx)\n"
2223
2224 /* %eax = syscall(%eax = __NR_clone,
2225 * %ebx = flags,
2226 * %ecx = child_stack,
2227 * %edx = parent_tidptr,
2228 * %esi = newtls,
2229 * %edi = child_tidptr)
2230 * Also, make sure that %ebx gets preserved as it is
2231 * used in PIC mode.
2232 */
2233 "movl %8,%%esi\n"
2234 "movl %7,%%edx\n"
2235 "movl %5,%%eax\n"
2236 "movl %9,%%edi\n"
2237 "pushl %%ebx\n"
2238 "movl %%eax,%%ebx\n"
2239 "movl %2,%%eax\n"
2240 LSS_ENTRYPOINT
2241
2242 /* In the parent: restore %ebx
2243 * In the child: move "fn" into %ebx
2244 */
2245 "popl %%ebx\n"
2246
2247 /* if (%eax != 0)
2248 * return %eax;
2249 */
2250 "test %%eax,%%eax\n"
2251 "jnz 1f\n"
2252
2253 /* In the child, now. Terminate frame pointer chain.
2254 */
2255 "movl $0,%%ebp\n"
2256
2257 /* Call "fn". "arg" is already on the stack.
2258 */
2259 "call *%%ebx\n"
2260
2261 /* Call _exit(%ebx). Unfortunately older versions
2262 * of gcc restrict the number of arguments that can
2263 * be passed to asm(). So, we need to hard-code the
2264 * system call number.
2265 */
2266 "movl %%eax,%%ebx\n"
2267 "movl $1,%%eax\n"
2268 LSS_ENTRYPOINT
2269
2270 /* Return to parent.
2271 */
2272 "1:\n"
2273 : "=a" (__res)
2274 : "0"(-EINVAL), "i"(__NR_clone),
2275 "m"(fn), "m"(child_stack), "m"(flags), "m"(arg),
2276 "m"(parent_tidptr), "m"(newtls), "m"(child_tidptr)
Joshua Perazabe2d5a82020-04-15 14:36:21 -07002277 : "memory", "ecx", "edx", "esi", "edi");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002278 LSS_RETURN(int, __res);
2279 }
2280
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002281 LSS_INLINE _syscall1(int, set_thread_area, void *, u)
2282 LSS_INLINE _syscall1(int, get_thread_area, void *, u)
2283
2284 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
2285 /* On i386, the kernel does not know how to return from a signal
2286 * handler. Instead, it relies on user space to provide a
2287 * restorer function that calls the {rt_,}sigreturn() system call.
2288 * Unfortunately, we cannot just reference the glibc version of this
2289 * function, as glibc goes out of its way to make it inaccessible.
2290 */
2291 void (*res)(void);
2292 __asm__ __volatile__("call 2f\n"
2293 "0:.align 16\n"
2294 "1:movl %1,%%eax\n"
2295 LSS_ENTRYPOINT
2296 "2:popl %0\n"
2297 "addl $(1b-0b),%0\n"
2298 : "=a" (res)
2299 : "i" (__NR_rt_sigreturn));
2300 return res;
2301 }
2302 LSS_INLINE void (*LSS_NAME(restore)(void))(void) {
2303 /* On i386, the kernel does not know how to return from a signal
2304 * handler. Instead, it relies on user space to provide a
2305 * restorer function that calls the {rt_,}sigreturn() system call.
2306 * Unfortunately, we cannot just reference the glibc version of this
2307 * function, as glibc goes out of its way to make it inaccessible.
2308 */
2309 void (*res)(void);
2310 __asm__ __volatile__("call 2f\n"
2311 "0:.align 16\n"
2312 "1:pop %%eax\n"
2313 "movl %1,%%eax\n"
2314 LSS_ENTRYPOINT
2315 "2:popl %0\n"
2316 "addl $(1b-0b),%0\n"
2317 : "=a" (res)
2318 : "i" (__NR_sigreturn));
2319 return res;
2320 }
2321 #elif defined(__x86_64__)
2322 /* There are no known problems with any of the _syscallX() macros
2323 * currently shipping for x86_64, but we still need to be able to define
2324 * our own version so that we can override the location of the errno
2325 * location (e.g. when using the clone() system call with the CLONE_VM
2326 * option).
2327 */
2328 #undef LSS_ENTRYPOINT
2329 #ifdef SYS_SYSCALL_ENTRYPOINT
2330 static inline void (**LSS_NAME(get_syscall_entrypoint)(void))(void) {
2331 void (**entrypoint)(void);
2332 asm volatile(".bss\n"
2333 ".align 8\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002334 ".globl " SYS_SYSCALL_ENTRYPOINT "\n"
2335 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002336 ".previous\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002337 "mov " SYS_SYSCALL_ENTRYPOINT "@GOTPCREL(%%rip), %0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002338 : "=r"(entrypoint));
2339 return entrypoint;
2340 }
2341
2342 #define LSS_ENTRYPOINT \
2343 ".bss\n" \
2344 ".align 8\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002345 ".globl " SYS_SYSCALL_ENTRYPOINT "\n" \
2346 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002347 ".previous\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002348 "mov " SYS_SYSCALL_ENTRYPOINT "@GOTPCREL(%%rip), %%rcx\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002349 "mov 0(%%rcx), %%rcx\n" \
2350 "test %%rcx, %%rcx\n" \
2351 "jz 10001f\n" \
2352 "call *%%rcx\n" \
2353 "jmp 10002f\n" \
2354 "10001:syscall\n" \
2355 "10002:\n"
2356
2357 #else
2358 #define LSS_ENTRYPOINT "syscall\n"
2359 #endif
vapier@chromium.org2273e812013-04-01 17:52:44 +00002360
2361 /* The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
2362 * We need to explicitly cast to an unsigned 64 bit type to avoid implicit
2363 * sign extension. We can't cast pointers directly because those are
2364 * 32 bits, and gcc will dump ugly warnings about casting from a pointer
2365 * to an integer of a different size.
2366 */
2367 #undef LSS_SYSCALL_ARG
2368 #define LSS_SYSCALL_ARG(a) ((uint64_t)(uintptr_t)(a))
2369 #undef _LSS_RETURN
2370 #define _LSS_RETURN(type, res, cast) \
2371 do { \
2372 if ((uint64_t)(res) >= (uint64_t)(-4095)) { \
Peter Kasting880985f2022-06-29 21:17:55 +00002373 LSS_ERRNO = (int)(-(res)); \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002374 res = -1; \
2375 } \
2376 return (type)(cast)(res); \
2377 } while (0)
2378 #undef LSS_RETURN
2379 #define LSS_RETURN(type, res) _LSS_RETURN(type, res, uintptr_t)
2380
2381 #undef _LSS_BODY
2382 #define _LSS_BODY(nr, type, name, cast, ...) \
2383 long long __res; \
2384 __asm__ __volatile__(LSS_BODY_ASM##nr LSS_ENTRYPOINT \
2385 : "=a" (__res) \
2386 : "0" (__NR_##name) LSS_BODY_ARG##nr(__VA_ARGS__) \
2387 : LSS_BODY_CLOBBER##nr "r11", "rcx", "memory"); \
2388 _LSS_RETURN(type, __res, cast)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002389 #undef LSS_BODY
vapier@chromium.org2273e812013-04-01 17:52:44 +00002390 #define LSS_BODY(nr, type, name, args...) \
2391 _LSS_BODY(nr, type, name, uintptr_t, ## args)
2392
2393 #undef LSS_BODY_ASM0
2394 #undef LSS_BODY_ASM1
2395 #undef LSS_BODY_ASM2
2396 #undef LSS_BODY_ASM3
2397 #undef LSS_BODY_ASM4
2398 #undef LSS_BODY_ASM5
2399 #undef LSS_BODY_ASM6
2400 #define LSS_BODY_ASM0
2401 #define LSS_BODY_ASM1 LSS_BODY_ASM0
2402 #define LSS_BODY_ASM2 LSS_BODY_ASM1
2403 #define LSS_BODY_ASM3 LSS_BODY_ASM2
2404 #define LSS_BODY_ASM4 LSS_BODY_ASM3 "movq %5,%%r10;"
2405 #define LSS_BODY_ASM5 LSS_BODY_ASM4 "movq %6,%%r8;"
2406 #define LSS_BODY_ASM6 LSS_BODY_ASM5 "movq %7,%%r9;"
2407
2408 #undef LSS_BODY_CLOBBER0
2409 #undef LSS_BODY_CLOBBER1
2410 #undef LSS_BODY_CLOBBER2
2411 #undef LSS_BODY_CLOBBER3
2412 #undef LSS_BODY_CLOBBER4
2413 #undef LSS_BODY_CLOBBER5
2414 #undef LSS_BODY_CLOBBER6
2415 #define LSS_BODY_CLOBBER0
2416 #define LSS_BODY_CLOBBER1 LSS_BODY_CLOBBER0
2417 #define LSS_BODY_CLOBBER2 LSS_BODY_CLOBBER1
2418 #define LSS_BODY_CLOBBER3 LSS_BODY_CLOBBER2
2419 #define LSS_BODY_CLOBBER4 LSS_BODY_CLOBBER3 "r10",
2420 #define LSS_BODY_CLOBBER5 LSS_BODY_CLOBBER4 "r8",
2421 #define LSS_BODY_CLOBBER6 LSS_BODY_CLOBBER5 "r9",
2422
2423 #undef LSS_BODY_ARG0
2424 #undef LSS_BODY_ARG1
2425 #undef LSS_BODY_ARG2
2426 #undef LSS_BODY_ARG3
2427 #undef LSS_BODY_ARG4
2428 #undef LSS_BODY_ARG5
2429 #undef LSS_BODY_ARG6
2430 #define LSS_BODY_ARG0()
2431 #define LSS_BODY_ARG1(arg1) \
2432 LSS_BODY_ARG0(), "D" (arg1)
2433 #define LSS_BODY_ARG2(arg1, arg2) \
2434 LSS_BODY_ARG1(arg1), "S" (arg2)
2435 #define LSS_BODY_ARG3(arg1, arg2, arg3) \
2436 LSS_BODY_ARG2(arg1, arg2), "d" (arg3)
2437 #define LSS_BODY_ARG4(arg1, arg2, arg3, arg4) \
2438 LSS_BODY_ARG3(arg1, arg2, arg3), "r" (arg4)
2439 #define LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5) \
2440 LSS_BODY_ARG4(arg1, arg2, arg3, arg4), "r" (arg5)
2441 #define LSS_BODY_ARG6(arg1, arg2, arg3, arg4, arg5, arg6) \
2442 LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5), "r" (arg6)
2443
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002444 #undef _syscall0
2445 #define _syscall0(type,name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002446 type LSS_NAME(name)(void) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002447 LSS_BODY(0, type, name); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002448 }
2449 #undef _syscall1
2450 #define _syscall1(type,name,type1,arg1) \
2451 type LSS_NAME(name)(type1 arg1) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002452 LSS_BODY(1, type, name, LSS_SYSCALL_ARG(arg1)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002453 }
2454 #undef _syscall2
2455 #define _syscall2(type,name,type1,arg1,type2,arg2) \
2456 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002457 LSS_BODY(2, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002458 }
2459 #undef _syscall3
2460 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
2461 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002462 LSS_BODY(3, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2463 LSS_SYSCALL_ARG(arg3)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002464 }
2465 #undef _syscall4
2466 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2467 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002468 LSS_BODY(4, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2469 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002470 }
2471 #undef _syscall5
2472 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2473 type5,arg5) \
2474 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2475 type5 arg5) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002476 LSS_BODY(5, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2477 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
2478 LSS_SYSCALL_ARG(arg5)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002479 }
2480 #undef _syscall6
2481 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2482 type5,arg5,type6,arg6) \
2483 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2484 type5 arg5, type6 arg6) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002485 LSS_BODY(6, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2486 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
2487 LSS_SYSCALL_ARG(arg5), LSS_SYSCALL_ARG(arg6));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002488 }
2489 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2490 int flags, void *arg, int *parent_tidptr,
2491 void *newtls, int *child_tidptr) {
vapier@chromium.org2273e812013-04-01 17:52:44 +00002492 long long __res;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002493 {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002494 __asm__ __volatile__(/* if (fn == NULL)
2495 * return -EINVAL;
2496 */
2497 "testq %4,%4\n"
2498 "jz 1f\n"
2499
2500 /* if (child_stack == NULL)
2501 * return -EINVAL;
2502 */
2503 "testq %5,%5\n"
2504 "jz 1f\n"
2505
2506 /* childstack -= 2*sizeof(void *);
2507 */
2508 "subq $16,%5\n"
2509
2510 /* Push "arg" and "fn" onto the stack that will be
2511 * used by the child.
2512 */
2513 "movq %7,8(%5)\n"
2514 "movq %4,0(%5)\n"
2515
2516 /* %rax = syscall(%rax = __NR_clone,
2517 * %rdi = flags,
2518 * %rsi = child_stack,
2519 * %rdx = parent_tidptr,
2520 * %r8 = new_tls,
2521 * %r10 = child_tidptr)
2522 */
2523 "movq %2,%%rax\n"
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00002524 "movq %9,%%r8\n"
2525 "movq %10,%%r10\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002526 LSS_ENTRYPOINT
2527
2528 /* if (%rax != 0)
2529 * return;
2530 */
2531 "testq %%rax,%%rax\n"
2532 "jnz 1f\n"
2533
2534 /* In the child. Terminate frame pointer chain.
2535 */
2536 "xorq %%rbp,%%rbp\n"
2537
2538 /* Call "fn(arg)".
2539 */
2540 "popq %%rax\n"
2541 "popq %%rdi\n"
2542 "call *%%rax\n"
2543
2544 /* Call _exit(%ebx).
2545 */
2546 "movq %%rax,%%rdi\n"
2547 "movq %3,%%rax\n"
2548 LSS_ENTRYPOINT
2549
2550 /* Return to parent.
2551 */
2552 "1:\n"
2553 : "=a" (__res)
2554 : "0"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit),
vapier@chromium.org2273e812013-04-01 17:52:44 +00002555 "r"(LSS_SYSCALL_ARG(fn)),
2556 "S"(LSS_SYSCALL_ARG(child_stack)),
2557 "D"(LSS_SYSCALL_ARG(flags)),
2558 "r"(LSS_SYSCALL_ARG(arg)),
2559 "d"(LSS_SYSCALL_ARG(parent_tidptr)),
2560 "r"(LSS_SYSCALL_ARG(newtls)),
2561 "r"(LSS_SYSCALL_ARG(child_tidptr))
Khem Raj8048ece2018-12-22 16:07:39 -08002562 : "memory", "r8", "r10", "r11", "rcx");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002563 }
2564 LSS_RETURN(int, __res);
2565 }
2566 LSS_INLINE _syscall2(int, arch_prctl, int, c, void *, a)
vapier@chromium.org2273e812013-04-01 17:52:44 +00002567
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002568 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
2569 /* On x86-64, the kernel does not know how to return from
2570 * a signal handler. Instead, it relies on user space to provide a
2571 * restorer function that calls the rt_sigreturn() system call.
2572 * Unfortunately, we cannot just reference the glibc version of this
2573 * function, as glibc goes out of its way to make it inaccessible.
2574 */
vapier@chromium.org2273e812013-04-01 17:52:44 +00002575 long long res;
mseaborn@chromium.org798c2f72013-08-31 00:04:49 +00002576 __asm__ __volatile__("jmp 2f\n"
2577 ".align 16\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002578 "1:movq %1,%%rax\n"
2579 LSS_ENTRYPOINT
mseaborn@chromium.org798c2f72013-08-31 00:04:49 +00002580 "2:leaq 1b(%%rip),%0\n"
2581 : "=r" (res)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002582 : "i" (__NR_rt_sigreturn));
vapier@chromium.org833a10e2013-04-02 19:34:26 +00002583 return (void (*)(void))(uintptr_t)res;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002584 }
2585 #elif defined(__ARM_ARCH_3__)
2586 /* Most definitions of _syscallX() neglect to mark "memory" as being
2587 * clobbered. This causes problems with compilers, that do a better job
2588 * at optimizing across __asm__ calls.
2589 * So, we just have to redefine all of the _syscallX() macros.
2590 */
2591 #undef LSS_REG
2592 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
2593 #undef LSS_BODY
2594 #define LSS_BODY(type,name,args...) \
2595 register long __res_r0 __asm__("r0"); \
2596 long __res; \
2597 __asm__ __volatile__ (__syscall(name) \
2598 : "=r"(__res_r0) : args : "lr", "memory"); \
2599 __res = __res_r0; \
2600 LSS_RETURN(type, __res)
2601 #undef _syscall0
2602 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002603 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002604 LSS_BODY(type, name); \
2605 }
2606 #undef _syscall1
2607 #define _syscall1(type, name, type1, arg1) \
2608 type LSS_NAME(name)(type1 arg1) { \
2609 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2610 }
2611 #undef _syscall2
2612 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2613 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2614 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2615 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2616 }
2617 #undef _syscall3
2618 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2619 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2620 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2621 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2622 }
2623 #undef _syscall4
2624 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2625 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2626 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2627 LSS_REG(3, arg4); \
2628 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2629 }
2630 #undef _syscall5
2631 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2632 type5,arg5) \
2633 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2634 type5 arg5) { \
2635 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2636 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2637 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2638 "r"(__r4)); \
2639 }
2640 #undef _syscall6
2641 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2642 type5,arg5,type6,arg6) \
2643 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2644 type5 arg5, type6 arg6) { \
2645 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2646 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2647 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2648 "r"(__r4), "r"(__r5)); \
2649 }
2650 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2651 int flags, void *arg, int *parent_tidptr,
2652 void *newtls, int *child_tidptr) {
2653 long __res;
2654 {
2655 register int __flags __asm__("r0") = flags;
2656 register void *__stack __asm__("r1") = child_stack;
2657 register void *__ptid __asm__("r2") = parent_tidptr;
2658 register void *__tls __asm__("r3") = newtls;
2659 register int *__ctid __asm__("r4") = child_tidptr;
2660 __asm__ __volatile__(/* if (fn == NULL || child_stack == NULL)
2661 * return -EINVAL;
2662 */
2663 "cmp %2,#0\n"
2664 "cmpne %3,#0\n"
2665 "moveq %0,%1\n"
2666 "beq 1f\n"
2667
2668 /* Push "arg" and "fn" onto the stack that will be
2669 * used by the child.
2670 */
2671 "str %5,[%3,#-4]!\n"
2672 "str %2,[%3,#-4]!\n"
2673
2674 /* %r0 = syscall(%r0 = flags,
2675 * %r1 = child_stack,
2676 * %r2 = parent_tidptr,
2677 * %r3 = newtls,
2678 * %r4 = child_tidptr)
2679 */
2680 __syscall(clone)"\n"
2681
2682 /* if (%r0 != 0)
2683 * return %r0;
2684 */
2685 "movs %0,r0\n"
2686 "bne 1f\n"
2687
2688 /* In the child, now. Call "fn(arg)".
2689 */
2690 "ldr r0,[sp, #4]\n"
2691 "mov lr,pc\n"
2692 "ldr pc,[sp]\n"
2693
2694 /* Call _exit(%r0).
2695 */
2696 __syscall(exit)"\n"
2697 "1:\n"
2698 : "=r" (__res)
2699 : "i"(-EINVAL),
2700 "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2701 "r"(__ptid), "r"(__tls), "r"(__ctid)
2702 : "cc", "lr", "memory");
2703 }
2704 LSS_RETURN(int, __res);
2705 }
2706 #elif defined(__ARM_EABI__)
2707 /* Most definitions of _syscallX() neglect to mark "memory" as being
2708 * clobbered. This causes problems with compilers, that do a better job
2709 * at optimizing across __asm__ calls.
2710 * So, we just have to redefine all fo the _syscallX() macros.
2711 */
2712 #undef LSS_REG
2713 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
2714 #undef LSS_BODY
2715 #define LSS_BODY(type,name,args...) \
2716 register long __res_r0 __asm__("r0"); \
2717 long __res; \
2718 __asm__ __volatile__ ("push {r7}\n" \
2719 "mov r7, %1\n" \
2720 "swi 0x0\n" \
2721 "pop {r7}\n" \
2722 : "=r"(__res_r0) \
2723 : "i"(__NR_##name) , ## args \
2724 : "lr", "memory"); \
2725 __res = __res_r0; \
2726 LSS_RETURN(type, __res)
2727 #undef _syscall0
2728 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002729 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002730 LSS_BODY(type, name); \
2731 }
2732 #undef _syscall1
2733 #define _syscall1(type, name, type1, arg1) \
2734 type LSS_NAME(name)(type1 arg1) { \
2735 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2736 }
2737 #undef _syscall2
2738 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2739 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2740 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2741 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2742 }
2743 #undef _syscall3
2744 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2745 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2746 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2747 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2748 }
2749 #undef _syscall4
2750 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2751 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2752 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2753 LSS_REG(3, arg4); \
2754 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2755 }
2756 #undef _syscall5
2757 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2758 type5,arg5) \
2759 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2760 type5 arg5) { \
2761 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2762 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2763 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2764 "r"(__r4)); \
2765 }
2766 #undef _syscall6
2767 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2768 type5,arg5,type6,arg6) \
2769 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2770 type5 arg5, type6 arg6) { \
2771 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2772 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2773 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2774 "r"(__r4), "r"(__r5)); \
2775 }
2776 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2777 int flags, void *arg, int *parent_tidptr,
2778 void *newtls, int *child_tidptr) {
2779 long __res;
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002780 if (fn == NULL || child_stack == NULL) {
2781 __res = -EINVAL;
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002782 LSS_RETURN(int, __res);
2783 }
2784
2785 /* Push "arg" and "fn" onto the stack that will be
2786 * used by the child.
2787 */
2788 {
2789 uintptr_t* cstack = (uintptr_t*)child_stack - 2;
2790 cstack[0] = (uintptr_t)fn;
2791 cstack[1] = (uintptr_t)arg;
2792 child_stack = cstack;
2793 }
2794 {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002795 register int __flags __asm__("r0") = flags;
2796 register void *__stack __asm__("r1") = child_stack;
2797 register void *__ptid __asm__("r2") = parent_tidptr;
2798 register void *__tls __asm__("r3") = newtls;
2799 register int *__ctid __asm__("r4") = child_tidptr;
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002800 __asm__ __volatile__(
Nico Weber63f24c82017-03-30 13:37:06 -04002801#ifdef __thumb2__
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002802 "push {r7}\n"
Nico Weber63f24c82017-03-30 13:37:06 -04002803#endif
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002804 /* %r0 = syscall(%r0 = flags,
2805 * %r1 = child_stack,
2806 * %r2 = parent_tidptr,
2807 * %r3 = newtls,
2808 * %r4 = child_tidptr)
2809 */
2810 "mov r7, %6\n"
2811 "swi 0x0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002812
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002813 /* if (%r0 != 0)
2814 * return %r0;
2815 */
2816 "cmp r0, #0\n"
2817 "bne 1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002818
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002819 /* In the child, now. Call "fn(arg)".
2820 */
2821 "ldr r0,[sp, #4]\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002822
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002823 "ldr lr,[sp]\n"
2824 "blx lr\n"
zodiac@gmail.com68c659b2011-10-06 05:34:19 +00002825
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002826 /* Call _exit(%r0).
2827 */
2828 "mov r7, %7\n"
2829 "swi 0x0\n"
2830 /* Unreachable */
2831 "bkpt #0\n"
2832 "1:\n"
Nico Weber63f24c82017-03-30 13:37:06 -04002833#ifdef __thumb2__
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002834 "pop {r7}\n"
Nico Weber63f24c82017-03-30 13:37:06 -04002835#endif
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002836 "movs %0,r0\n"
2837 : "=r"(__res)
2838 : "r"(__stack), "r"(__flags), "r"(__ptid), "r"(__tls), "r"(__ctid),
2839 "i"(__NR_clone), "i"(__NR_exit)
2840 : "cc", "lr", "memory"
2841#ifndef __thumb2__
2842 , "r7"
Nico Weber63f24c82017-03-30 13:37:06 -04002843#endif
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002844 );
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002845 }
2846 LSS_RETURN(int, __res);
2847 }
anton@chromium.org2f724fc2014-04-15 13:05:20 +00002848 #elif defined(__aarch64__)
2849 /* Most definitions of _syscallX() neglect to mark "memory" as being
2850 * clobbered. This causes problems with compilers, that do a better job
2851 * at optimizing across __asm__ calls.
2852 * So, we just have to redefine all of the _syscallX() macros.
2853 */
2854 #undef LSS_REG
2855 #define LSS_REG(r,a) register int64_t __r##r __asm__("x"#r) = (int64_t)a
2856 #undef LSS_BODY
2857 #define LSS_BODY(type,name,args...) \
2858 register int64_t __res_x0 __asm__("x0"); \
2859 int64_t __res; \
2860 __asm__ __volatile__ ("mov x8, %1\n" \
2861 "svc 0x0\n" \
2862 : "=r"(__res_x0) \
2863 : "i"(__NR_##name) , ## args \
2864 : "x8", "memory"); \
2865 __res = __res_x0; \
2866 LSS_RETURN(type, __res)
2867 #undef _syscall0
2868 #define _syscall0(type, name) \
2869 type LSS_NAME(name)(void) { \
2870 LSS_BODY(type, name); \
2871 }
2872 #undef _syscall1
2873 #define _syscall1(type, name, type1, arg1) \
2874 type LSS_NAME(name)(type1 arg1) { \
2875 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2876 }
2877 #undef _syscall2
2878 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2879 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2880 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2881 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2882 }
2883 #undef _syscall3
2884 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2885 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2886 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2887 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2888 }
2889 #undef _syscall4
2890 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2891 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2892 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2893 LSS_REG(3, arg4); \
2894 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2895 }
2896 #undef _syscall5
2897 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2898 type5,arg5) \
2899 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2900 type5 arg5) { \
2901 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2902 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2903 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2904 "r"(__r4)); \
2905 }
2906 #undef _syscall6
2907 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2908 type5,arg5,type6,arg6) \
2909 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2910 type5 arg5, type6 arg6) { \
2911 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2912 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2913 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2914 "r"(__r4), "r"(__r5)); \
2915 }
2916
2917 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2918 int flags, void *arg, int *parent_tidptr,
2919 void *newtls, int *child_tidptr) {
2920 int64_t __res;
2921 {
Peter Kasting0d6435b2022-07-20 20:21:35 +00002922 register uint64_t __flags __asm__("x0") = (uint64_t)flags;
anton@chromium.org2f724fc2014-04-15 13:05:20 +00002923 register void *__stack __asm__("x1") = child_stack;
2924 register void *__ptid __asm__("x2") = parent_tidptr;
2925 register void *__tls __asm__("x3") = newtls;
2926 register int *__ctid __asm__("x4") = child_tidptr;
2927 __asm__ __volatile__(/* Push "arg" and "fn" onto the stack that will be
2928 * used by the child.
2929 */
2930 "stp %1, %4, [%2, #-16]!\n"
2931
2932 /* %x0 = syscall(%x0 = flags,
2933 * %x1 = child_stack,
2934 * %x2 = parent_tidptr,
2935 * %x3 = newtls,
2936 * %x4 = child_tidptr)
2937 */
2938 "mov x8, %8\n"
2939 "svc 0x0\n"
2940
2941 /* if (%r0 != 0)
2942 * return %r0;
2943 */
2944 "mov %0, x0\n"
2945 "cbnz x0, 1f\n"
2946
2947 /* In the child, now. Call "fn(arg)".
2948 */
2949 "ldp x1, x0, [sp], #16\n"
2950 "blr x1\n"
2951
2952 /* Call _exit(%r0).
2953 */
2954 "mov x8, %9\n"
2955 "svc 0x0\n"
2956 "1:\n"
2957 : "=r" (__res)
2958 : "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2959 "r"(__ptid), "r"(__tls), "r"(__ctid),
2960 "i"(__NR_clone), "i"(__NR_exit)
2961 : "cc", "x8", "memory");
2962 }
2963 LSS_RETURN(int, __res);
2964 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002965 #elif defined(__mips__)
2966 #undef LSS_REG
2967 #define LSS_REG(r,a) register unsigned long __r##r __asm__("$"#r) = \
2968 (unsigned long)(a)
2969 #undef LSS_BODY
thestig@chromium.org952107f2014-08-01 02:22:56 +00002970 #undef LSS_SYSCALL_CLOBBERS
2971 #if _MIPS_SIM == _MIPS_SIM_ABI32
2972 #define LSS_SYSCALL_CLOBBERS "$1", "$3", "$8", "$9", "$10", \
2973 "$11", "$12", "$13", "$14", "$15", \
2974 "$24", "$25", "hi", "lo", "memory"
2975 #else
2976 #define LSS_SYSCALL_CLOBBERS "$1", "$3", "$10", "$11", "$12", \
2977 "$13", "$14", "$15", "$24", "$25", \
2978 "hi", "lo", "memory"
2979 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002980 #define LSS_BODY(type,name,r7,...) \
2981 register unsigned long __v0 __asm__("$2") = __NR_##name; \
2982 __asm__ __volatile__ ("syscall\n" \
vapier@chromium.orgda4a4892015-01-22 16:46:39 +00002983 : "=r"(__v0), r7 (__r7) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002984 : "0"(__v0), ##__VA_ARGS__ \
thestig@chromium.org952107f2014-08-01 02:22:56 +00002985 : LSS_SYSCALL_CLOBBERS); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002986 LSS_RETURN(type, __v0, __r7)
2987 #undef _syscall0
2988 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002989 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002990 register unsigned long __r7 __asm__("$7"); \
2991 LSS_BODY(type, name, "=r"); \
2992 }
2993 #undef _syscall1
2994 #define _syscall1(type, name, type1, arg1) \
2995 type LSS_NAME(name)(type1 arg1) { \
2996 register unsigned long __r7 __asm__("$7"); \
2997 LSS_REG(4, arg1); LSS_BODY(type, name, "=r", "r"(__r4)); \
2998 }
2999 #undef _syscall2
3000 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3001 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3002 register unsigned long __r7 __asm__("$7"); \
3003 LSS_REG(4, arg1); LSS_REG(5, arg2); \
3004 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5)); \
3005 }
3006 #undef _syscall3
3007 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3008 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3009 register unsigned long __r7 __asm__("$7"); \
3010 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
3011 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5), "r"(__r6)); \
3012 }
3013 #undef _syscall4
3014 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
3015 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3016 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
3017 LSS_REG(7, arg4); \
3018 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6)); \
3019 }
3020 #undef _syscall5
3021 #if _MIPS_SIM == _MIPS_SIM_ABI32
3022 /* The old 32bit MIPS system call API passes the fifth and sixth argument
3023 * on the stack, whereas the new APIs use registers "r8" and "r9".
3024 */
3025 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3026 type5,arg5) \
3027 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3028 type5 arg5) { \
3029 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
3030 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003031 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003032 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003033 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003034 "sw %5, 16($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003035 "syscall\n" \
3036 "addiu $29, 32\n" \
3037 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003038 : "+r"(__v0), "+r" (__r7) \
3039 : "r"(__r4), "r"(__r5), \
3040 "r"(__r6), "r" ((unsigned long)arg5) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003041 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003042 "$13", "$14", "$15", "$24", "$25", \
3043 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003044 LSS_RETURN(type, __v0, __r7); \
3045 }
3046 #else
3047 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3048 type5,arg5) \
3049 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3050 type5 arg5) { \
3051 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
3052 LSS_REG(7, arg4); LSS_REG(8, arg5); \
3053 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
3054 "r"(__r8)); \
3055 }
3056 #endif
3057 #undef _syscall6
3058 #if _MIPS_SIM == _MIPS_SIM_ABI32
3059 /* The old 32bit MIPS system call API passes the fifth and sixth argument
3060 * on the stack, whereas the new APIs use registers "r8" and "r9".
3061 */
3062 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3063 type5,arg5,type6,arg6) \
3064 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3065 type5 arg5, type6 arg6) { \
3066 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
3067 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003068 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003069 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003070 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003071 "sw %5, 16($29)\n" \
3072 "sw %6, 20($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003073 "syscall\n" \
3074 "addiu $29, 32\n" \
3075 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003076 : "+r"(__v0), "+r" (__r7) \
3077 : "r"(__r4), "r"(__r5), \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003078 "r"(__r6), "r" ((unsigned long)arg5), \
3079 "r" ((unsigned long)arg6) \
3080 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003081 "$13", "$14", "$15", "$24", "$25", \
3082 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003083 LSS_RETURN(type, __v0, __r7); \
3084 }
3085 #else
3086 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3087 type5,arg5,type6,arg6) \
3088 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3089 type5 arg5,type6 arg6) { \
3090 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
3091 LSS_REG(7, arg4); LSS_REG(8, arg5); LSS_REG(9, arg6); \
3092 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
3093 "r"(__r8), "r"(__r9)); \
3094 }
3095 #endif
3096 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3097 int flags, void *arg, int *parent_tidptr,
3098 void *newtls, int *child_tidptr) {
vapier@chromium.orge0797682015-02-20 20:45:56 +00003099 register unsigned long __v0 __asm__("$2") = -EINVAL;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003100 register unsigned long __r7 __asm__("$7") = (unsigned long)newtls;
3101 {
3102 register int __flags __asm__("$4") = flags;
3103 register void *__stack __asm__("$5") = child_stack;
3104 register void *__ptid __asm__("$6") = parent_tidptr;
3105 register int *__ctid __asm__("$8") = child_tidptr;
3106 __asm__ __volatile__(
3107 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
3108 "subu $29,24\n"
3109 #elif _MIPS_SIM == _MIPS_SIM_NABI32
3110 "sub $29,16\n"
3111 #else
3112 "dsubu $29,16\n"
3113 #endif
3114
3115 /* if (fn == NULL || child_stack == NULL)
3116 * return -EINVAL;
3117 */
vapier@chromium.orge0797682015-02-20 20:45:56 +00003118 "beqz %4,1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003119 "beqz %5,1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003120
3121 /* Push "arg" and "fn" onto the stack that will be
3122 * used by the child.
3123 */
3124 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
vapier@chromium.orge0797682015-02-20 20:45:56 +00003125 "subu %5,32\n"
3126 "sw %4,0(%5)\n"
3127 "sw %7,4(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003128 #elif _MIPS_SIM == _MIPS_SIM_NABI32
vapier@chromium.orge0797682015-02-20 20:45:56 +00003129 "sub %5,32\n"
3130 "sw %4,0(%5)\n"
3131 "sw %7,8(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003132 #else
vapier@chromium.orge0797682015-02-20 20:45:56 +00003133 "dsubu %5,32\n"
3134 "sd %4,0(%5)\n"
3135 "sd %7,8(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003136 #endif
3137
3138 /* $7 = syscall($4 = flags,
3139 * $5 = child_stack,
3140 * $6 = parent_tidptr,
3141 * $7 = newtls,
3142 * $8 = child_tidptr)
3143 */
vapier@chromium.orge0797682015-02-20 20:45:56 +00003144 "li $2,%2\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003145 "syscall\n"
3146
3147 /* if ($7 != 0)
3148 * return $2;
3149 */
3150 "bnez $7,1f\n"
3151 "bnez $2,1f\n"
3152
3153 /* In the child, now. Call "fn(arg)".
3154 */
3155 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
3156 "lw $25,0($29)\n"
3157 "lw $4,4($29)\n"
3158 #elif _MIPS_SIM == _MIPS_SIM_NABI32
3159 "lw $25,0($29)\n"
3160 "lw $4,8($29)\n"
3161 #else
3162 "ld $25,0($29)\n"
3163 "ld $4,8($29)\n"
3164 #endif
3165 "jalr $25\n"
3166
3167 /* Call _exit($2)
3168 */
3169 "move $4,$2\n"
vapier@chromium.orge0797682015-02-20 20:45:56 +00003170 "li $2,%3\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003171 "syscall\n"
3172
3173 "1:\n"
3174 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
3175 "addu $29, 24\n"
3176 #elif _MIPS_SIM == _MIPS_SIM_NABI32
3177 "add $29, 16\n"
3178 #else
3179 "daddu $29,16\n"
3180 #endif
petarj@mips.com0ece1c62013-04-10 00:28:04 +00003181 : "+r" (__v0), "+r" (__r7)
vapier@chromium.orge0797682015-02-20 20:45:56 +00003182 : "i"(__NR_clone), "i"(__NR_exit), "r"(fn),
3183 "r"(__stack), "r"(__flags), "r"(arg),
3184 "r"(__ptid), "r"(__ctid)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003185 : "$9", "$10", "$11", "$12", "$13", "$14", "$15",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003186 "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003187 }
3188 LSS_RETURN(int, __v0, __r7);
3189 }
3190 #elif defined (__PPC__)
3191 #undef LSS_LOADARGS_0
3192 #define LSS_LOADARGS_0(name, dummy...) \
3193 __sc_0 = __NR_##name
3194 #undef LSS_LOADARGS_1
3195 #define LSS_LOADARGS_1(name, arg1) \
3196 LSS_LOADARGS_0(name); \
3197 __sc_3 = (unsigned long) (arg1)
3198 #undef LSS_LOADARGS_2
3199 #define LSS_LOADARGS_2(name, arg1, arg2) \
3200 LSS_LOADARGS_1(name, arg1); \
3201 __sc_4 = (unsigned long) (arg2)
3202 #undef LSS_LOADARGS_3
3203 #define LSS_LOADARGS_3(name, arg1, arg2, arg3) \
3204 LSS_LOADARGS_2(name, arg1, arg2); \
3205 __sc_5 = (unsigned long) (arg3)
3206 #undef LSS_LOADARGS_4
3207 #define LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4) \
3208 LSS_LOADARGS_3(name, arg1, arg2, arg3); \
3209 __sc_6 = (unsigned long) (arg4)
3210 #undef LSS_LOADARGS_5
3211 #define LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5) \
3212 LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4); \
3213 __sc_7 = (unsigned long) (arg5)
3214 #undef LSS_LOADARGS_6
3215 #define LSS_LOADARGS_6(name, arg1, arg2, arg3, arg4, arg5, arg6) \
3216 LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5); \
3217 __sc_8 = (unsigned long) (arg6)
3218 #undef LSS_ASMINPUT_0
3219 #define LSS_ASMINPUT_0 "0" (__sc_0)
3220 #undef LSS_ASMINPUT_1
3221 #define LSS_ASMINPUT_1 LSS_ASMINPUT_0, "1" (__sc_3)
3222 #undef LSS_ASMINPUT_2
3223 #define LSS_ASMINPUT_2 LSS_ASMINPUT_1, "2" (__sc_4)
3224 #undef LSS_ASMINPUT_3
3225 #define LSS_ASMINPUT_3 LSS_ASMINPUT_2, "3" (__sc_5)
3226 #undef LSS_ASMINPUT_4
3227 #define LSS_ASMINPUT_4 LSS_ASMINPUT_3, "4" (__sc_6)
3228 #undef LSS_ASMINPUT_5
3229 #define LSS_ASMINPUT_5 LSS_ASMINPUT_4, "5" (__sc_7)
3230 #undef LSS_ASMINPUT_6
3231 #define LSS_ASMINPUT_6 LSS_ASMINPUT_5, "6" (__sc_8)
3232 #undef LSS_BODY
3233 #define LSS_BODY(nr, type, name, args...) \
3234 long __sc_ret, __sc_err; \
3235 { \
3236 register unsigned long __sc_0 __asm__ ("r0"); \
3237 register unsigned long __sc_3 __asm__ ("r3"); \
3238 register unsigned long __sc_4 __asm__ ("r4"); \
3239 register unsigned long __sc_5 __asm__ ("r5"); \
3240 register unsigned long __sc_6 __asm__ ("r6"); \
3241 register unsigned long __sc_7 __asm__ ("r7"); \
3242 register unsigned long __sc_8 __asm__ ("r8"); \
3243 \
3244 LSS_LOADARGS_##nr(name, args); \
3245 __asm__ __volatile__ \
3246 ("sc\n\t" \
3247 "mfcr %0" \
3248 : "=&r" (__sc_0), \
3249 "=&r" (__sc_3), "=&r" (__sc_4), \
3250 "=&r" (__sc_5), "=&r" (__sc_6), \
3251 "=&r" (__sc_7), "=&r" (__sc_8) \
3252 : LSS_ASMINPUT_##nr \
3253 : "cr0", "ctr", "memory", \
3254 "r9", "r10", "r11", "r12"); \
3255 __sc_ret = __sc_3; \
3256 __sc_err = __sc_0; \
3257 } \
3258 LSS_RETURN(type, __sc_ret, __sc_err)
3259 #undef _syscall0
3260 #define _syscall0(type, name) \
3261 type LSS_NAME(name)(void) { \
3262 LSS_BODY(0, type, name); \
3263 }
3264 #undef _syscall1
3265 #define _syscall1(type, name, type1, arg1) \
3266 type LSS_NAME(name)(type1 arg1) { \
3267 LSS_BODY(1, type, name, arg1); \
3268 }
3269 #undef _syscall2
3270 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3271 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3272 LSS_BODY(2, type, name, arg1, arg2); \
3273 }
3274 #undef _syscall3
3275 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3276 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3277 LSS_BODY(3, type, name, arg1, arg2, arg3); \
3278 }
3279 #undef _syscall4
3280 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3281 type4, arg4) \
3282 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3283 LSS_BODY(4, type, name, arg1, arg2, arg3, arg4); \
3284 }
3285 #undef _syscall5
3286 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3287 type4, arg4, type5, arg5) \
3288 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3289 type5 arg5) { \
3290 LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5); \
3291 }
3292 #undef _syscall6
3293 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3294 type4, arg4, type5, arg5, type6, arg6) \
3295 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3296 type5 arg5, type6 arg6) { \
3297 LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6); \
3298 }
3299 /* clone function adapted from glibc 2.3.6 clone.S */
3300 /* TODO(csilvers): consider wrapping some args up in a struct, like we
3301 * do for i386's _syscall6, so we can compile successfully on gcc 2.95
3302 */
3303 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3304 int flags, void *arg, int *parent_tidptr,
3305 void *newtls, int *child_tidptr) {
3306 long __ret, __err;
3307 {
3308 register int (*__fn)(void *) __asm__ ("r8") = fn;
3309 register void *__cstack __asm__ ("r4") = child_stack;
3310 register int __flags __asm__ ("r3") = flags;
3311 register void * __arg __asm__ ("r9") = arg;
3312 register int * __ptidptr __asm__ ("r5") = parent_tidptr;
3313 register void * __newtls __asm__ ("r6") = newtls;
3314 register int * __ctidptr __asm__ ("r7") = child_tidptr;
3315 __asm__ __volatile__(
3316 /* check for fn == NULL
3317 * and child_stack == NULL
3318 */
3319 "cmpwi cr0, %6, 0\n\t"
3320 "cmpwi cr1, %7, 0\n\t"
3321 "cror cr0*4+eq, cr1*4+eq, cr0*4+eq\n\t"
3322 "beq- cr0, 1f\n\t"
3323
3324 /* set up stack frame for child */
3325 "clrrwi %7, %7, 4\n\t"
3326 "li 0, 0\n\t"
3327 "stwu 0, -16(%7)\n\t"
3328
3329 /* fn, arg, child_stack are saved across the syscall: r28-30 */
3330 "mr 28, %6\n\t"
3331 "mr 29, %7\n\t"
3332 "mr 27, %9\n\t"
3333
3334 /* syscall */
3335 "li 0, %4\n\t"
3336 /* flags already in r3
3337 * child_stack already in r4
3338 * ptidptr already in r5
3339 * newtls already in r6
3340 * ctidptr already in r7
3341 */
3342 "sc\n\t"
3343
3344 /* Test if syscall was successful */
3345 "cmpwi cr1, 3, 0\n\t"
3346 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
3347 "bne- cr1, 1f\n\t"
3348
3349 /* Do the function call */
3350 "mtctr 28\n\t"
3351 "mr 3, 27\n\t"
3352 "bctrl\n\t"
3353
3354 /* Call _exit(r3) */
3355 "li 0, %5\n\t"
3356 "sc\n\t"
3357
3358 /* Return to parent */
3359 "1:\n"
3360 "mfcr %1\n\t"
3361 "mr %0, 3\n\t"
3362 : "=r" (__ret), "=r" (__err)
3363 : "0" (-1), "1" (EINVAL),
3364 "i" (__NR_clone), "i" (__NR_exit),
3365 "r" (__fn), "r" (__cstack), "r" (__flags),
3366 "r" (__arg), "r" (__ptidptr), "r" (__newtls),
3367 "r" (__ctidptr)
3368 : "cr0", "cr1", "memory", "ctr",
3369 "r0", "r29", "r27", "r28");
3370 }
3371 LSS_RETURN(int, __ret, __err);
3372 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04003373 #elif defined(__s390__)
3374 #undef LSS_REG
3375 #define LSS_REG(r, a) register unsigned long __r##r __asm__("r"#r) = (unsigned long) a
3376 #undef LSS_BODY
3377 #define LSS_BODY(type, name, args...) \
3378 register unsigned long __nr __asm__("r1") \
3379 = (unsigned long)(__NR_##name); \
3380 register long __res_r2 __asm__("r2"); \
3381 long __res; \
3382 __asm__ __volatile__ \
3383 ("svc 0\n\t" \
3384 : "=d"(__res_r2) \
3385 : "d"(__nr), ## args \
3386 : "memory"); \
3387 __res = __res_r2; \
3388 LSS_RETURN(type, __res)
3389 #undef _syscall0
3390 #define _syscall0(type, name) \
3391 type LSS_NAME(name)(void) { \
3392 LSS_BODY(type, name); \
3393 }
3394 #undef _syscall1
3395 #define _syscall1(type, name, type1, arg1) \
3396 type LSS_NAME(name)(type1 arg1) { \
3397 LSS_REG(2, arg1); \
3398 LSS_BODY(type, name, "0"(__r2)); \
3399 }
3400 #undef _syscall2
3401 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3402 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3403 LSS_REG(2, arg1); LSS_REG(3, arg2); \
3404 LSS_BODY(type, name, "0"(__r2), "d"(__r3)); \
3405 }
3406 #undef _syscall3
3407 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3408 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3409 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3410 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4)); \
3411 }
3412 #undef _syscall4
3413 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3414 type4, arg4) \
3415 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3416 type4 arg4) { \
3417 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3418 LSS_REG(5, arg4); \
3419 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3420 "d"(__r5)); \
3421 }
3422 #undef _syscall5
3423 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3424 type4, arg4, type5, arg5) \
3425 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3426 type4 arg4, type5 arg5) { \
3427 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3428 LSS_REG(5, arg4); LSS_REG(6, arg5); \
3429 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3430 "d"(__r5), "d"(__r6)); \
3431 }
3432 #undef _syscall6
3433 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3434 type4, arg4, type5, arg5, type6, arg6) \
3435 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3436 type4 arg4, type5 arg5, type6 arg6) { \
3437 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3438 LSS_REG(5, arg4); LSS_REG(6, arg5); LSS_REG(7, arg6); \
3439 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3440 "d"(__r5), "d"(__r6), "d"(__r7)); \
3441 }
3442 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3443 int flags, void *arg, int *parent_tidptr,
3444 void *newtls, int *child_tidptr) {
3445 long __ret;
3446 {
3447 register int (*__fn)(void *) __asm__ ("r1") = fn;
3448 register void *__cstack __asm__ ("r2") = child_stack;
3449 register int __flags __asm__ ("r3") = flags;
3450 register void *__arg __asm__ ("r0") = arg;
3451 register int *__ptidptr __asm__ ("r4") = parent_tidptr;
3452 register void *__newtls __asm__ ("r6") = newtls;
3453 register int *__ctidptr __asm__ ("r5") = child_tidptr;
3454 __asm__ __volatile__ (
3455 #ifndef __s390x__
3456 /* arg already in r0 */
3457 "ltr %4, %4\n\t" /* check fn, which is already in r1 */
3458 "jz 1f\n\t" /* NULL function pointer, return -EINVAL */
3459 "ltr %5, %5\n\t" /* check child_stack, which is already in r2 */
3460 "jz 1f\n\t" /* NULL stack pointer, return -EINVAL */
3461 /* flags already in r3 */
3462 /* parent_tidptr already in r4 */
3463 /* child_tidptr already in r5 */
3464 /* newtls already in r6 */
3465 "svc %2\n\t" /* invoke clone syscall */
3466 "ltr %0,%%r2\n\t" /* load return code into __ret and test */
3467 "jnz 1f\n\t" /* return to parent if non-zero */
3468 /* start child thread */
3469 "lr %%r2, %7\n\t" /* set first parameter to void *arg */
3470 "ahi %%r15, -96\n\t" /* make room on the stack for the save area */
3471 "xc 0(4,%%r15), 0(%%r15)\n\t"
3472 "basr %%r14, %4\n\t" /* jump to fn */
3473 "svc %3\n" /* invoke exit syscall */
3474 "1:\n"
3475 #else
3476 /* arg already in r0 */
3477 "ltgr %4, %4\n\t" /* check fn, which is already in r1 */
3478 "jz 1f\n\t" /* NULL function pointer, return -EINVAL */
3479 "ltgr %5, %5\n\t" /* check child_stack, which is already in r2 */
3480 "jz 1f\n\t" /* NULL stack pointer, return -EINVAL */
3481 /* flags already in r3 */
3482 /* parent_tidptr already in r4 */
3483 /* child_tidptr already in r5 */
3484 /* newtls already in r6 */
3485 "svc %2\n\t" /* invoke clone syscall */
3486 "ltgr %0, %%r2\n\t" /* load return code into __ret and test */
3487 "jnz 1f\n\t" /* return to parent if non-zero */
3488 /* start child thread */
3489 "lgr %%r2, %7\n\t" /* set first parameter to void *arg */
3490 "aghi %%r15, -160\n\t" /* make room on the stack for the save area */
3491 "xc 0(8,%%r15), 0(%%r15)\n\t"
3492 "basr %%r14, %4\n\t" /* jump to fn */
3493 "svc %3\n" /* invoke exit syscall */
3494 "1:\n"
3495 #endif
3496 : "=r" (__ret)
3497 : "0" (-EINVAL), "i" (__NR_clone), "i" (__NR_exit),
3498 "d" (__fn), "d" (__cstack), "d" (__flags), "d" (__arg),
3499 "d" (__ptidptr), "d" (__newtls), "d" (__ctidptr)
3500 : "cc", "r14", "memory"
3501 );
3502 }
3503 LSS_RETURN(int, __ret);
3504 }
Andreas Schwab1d387f42022-02-15 16:21:13 +01003505 #elif defined(__riscv) && __riscv_xlen == 64
3506 #undef LSS_REG
3507 #define LSS_REG(r,a) register int64_t __r##r __asm__("a"#r) = (int64_t)a
3508 #undef LSS_BODY
3509 #define LSS_BODY(type,name,args...) \
3510 register int64_t __res_a0 __asm__("a0"); \
3511 register int64_t __a7 __asm__("a7") = __NR_##name; \
3512 int64_t __res; \
3513 __asm__ __volatile__ ("scall\n" \
3514 : "=r"(__res_a0) \
3515 : "r"(__a7) , ## args \
3516 : "memory"); \
3517 __res = __res_a0; \
3518 LSS_RETURN(type, __res)
3519 #undef _syscall0
3520 #define _syscall0(type, name) \
3521 type LSS_NAME(name)(void) { \
3522 LSS_BODY(type, name); \
3523 }
3524 #undef _syscall1
3525 #define _syscall1(type, name, type1, arg1) \
3526 type LSS_NAME(name)(type1 arg1) { \
3527 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
3528 }
3529 #undef _syscall2
3530 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3531 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3532 LSS_REG(0, arg1); LSS_REG(1, arg2); \
3533 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
3534 }
3535 #undef _syscall3
3536 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3537 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3538 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3539 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
3540 }
3541 #undef _syscall4
3542 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
3543 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3544 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3545 LSS_REG(3, arg4); \
3546 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
3547 }
3548 #undef _syscall5
3549 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3550 type5,arg5) \
3551 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3552 type5 arg5) { \
3553 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3554 LSS_REG(3, arg4); LSS_REG(4, arg5); \
3555 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
3556 "r"(__r4)); \
3557 }
3558 #undef _syscall6
3559 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3560 type5,arg5,type6,arg6) \
3561 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3562 type5 arg5, type6 arg6) { \
3563 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3564 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
3565 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
3566 "r"(__r4), "r"(__r5)); \
3567 }
3568
3569 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3570 int flags, void *arg, int *parent_tidptr,
3571 void *newtls, int *child_tidptr) {
3572 int64_t __res;
3573 {
3574 register int64_t __res_a0 __asm__("a0");
3575 register uint64_t __flags __asm__("a0") = flags;
3576 register void *__stack __asm__("a1") = child_stack;
3577 register void *__ptid __asm__("a2") = parent_tidptr;
3578 register void *__tls __asm__("a3") = newtls;
3579 register int *__ctid __asm__("a4") = child_tidptr;
3580 __asm__ __volatile__(/* Push "arg" and "fn" onto the stack that will be
3581 * used by the child.
3582 */
3583 "addi %2,%2,-16\n"
3584 "sd %1, 0(%2)\n"
3585 "sd %4, 8(%2)\n"
3586
3587 /* %a0 = syscall(%a0 = flags,
3588 * %a1 = child_stack,
3589 * %a2 = parent_tidptr,
3590 * %a3 = newtls,
3591 * %a4 = child_tidptr)
3592 */
3593 "li a7, %8\n"
3594 "scall\n"
3595
3596 /* if (%a0 != 0)
3597 * return %a0;
3598 */
3599 "bnez %0, 1f\n"
3600
3601 /* In the child, now. Call "fn(arg)".
3602 */
3603 "ld a1, 0(sp)\n"
3604 "ld a0, 8(sp)\n"
3605 "jalr a1\n"
3606
3607 /* Call _exit(%a0).
3608 */
3609 "li a7, %9\n"
3610 "scall\n"
3611 "1:\n"
3612 : "=r" (__res_a0)
3613 : "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
3614 "r"(__ptid), "r"(__tls), "r"(__ctid),
3615 "i"(__NR_clone), "i"(__NR_exit)
3616 : "cc", "memory");
3617 __res = __res_a0;
3618 }
3619 LSS_RETURN(int, __res);
3620 }
Konstantin Ivlev8007b272021-01-27 18:27:42 +03003621 #elif defined(__e2k__)
3622
3623 #undef _LSS_BODY
3624 #define _LSS_BODY(nr, type, name, ...) \
3625 register unsigned long long __res; \
3626 __asm__ __volatile__ \
3627 ( \
3628 "{\n\t" \
3629 " sdisp %%ctpr1, 0x3\n\t" \
3630 " addd, s 0x0, %[sys_num], %%b[0]\n\t" \
3631 LSS_BODY_ASM##nr \
3632 "}\n\t" \
3633 "{\n\t" \
3634 " call %%ctpr1, wbs = %#\n\t" \
3635 "}\n\t" \
3636 "{\n\t" \
3637 " addd, s 0x0, %%b[0], %[res]\n\t" \
3638 "}\n\t" \
3639 : [res] "=r" (__res) \
3640 : \
3641 LSS_BODY_ARG##nr(__VA_ARGS__) \
3642 [sys_num] "ri" (__NR_##name) \
3643 : "ctpr1", "ctpr2", "ctpr3", \
3644 "b[0]", "b[1]", "b[2]", "b[3]", \
3645 "b[4]", "b[5]", "b[6]", "b[7]" \
3646 ); \
3647 LSS_RETURN(type, __res);
3648
3649 #undef LSS_BODY
3650 #define LSS_BODY(nr, type, name, args...) \
3651 _LSS_BODY(nr, type, name, ## args)
3652
3653 #undef LSS_BODY_ASM0
3654 #undef LSS_BODY_ASM1
3655 #undef LSS_BODY_ASM2
3656 #undef LSS_BODY_ASM3
3657 #undef LSS_BODY_ASM4
3658 #undef LSS_BODY_ASM5
3659 #undef LSS_BODY_ASM6
3660
3661 #define LSS_BODY_ASM0
3662 #define LSS_BODY_ASM1 LSS_BODY_ASM0 \
3663 " addd, s 0x0, %[arg1], %%b[1]\n\t"
3664 #define LSS_BODY_ASM2 LSS_BODY_ASM1 \
3665 " addd, s 0x0, %[arg2], %%b[2]\n\t"
3666 #define LSS_BODY_ASM3 LSS_BODY_ASM2 \
3667 " addd, s 0x0, %[arg3], %%b[3]\n\t"
3668 #define LSS_BODY_ASM4 LSS_BODY_ASM3 \
3669 " addd, s 0x0, %[arg4], %%b[4]\n\t"
3670 #define LSS_BODY_ASM5 LSS_BODY_ASM4 \
3671 " addd, s 0x0, %[arg5], %%b[5]\n\t"
3672 #define LSS_BODY_ASM6 LSS_BODY_ASM5 \
3673 "}\n\t" \
3674 "{\n\t" \
3675 " addd, s 0x0, %[arg6], %%b[6]\n\t"
3676
3677 #undef LSS_SYSCALL_ARG
3678 #define LSS_SYSCALL_ARG(a) ((unsigned long long)(uintptr_t)(a))
3679
3680 #undef LSS_BODY_ARG0
3681 #undef LSS_BODY_ARG1
3682 #undef LSS_BODY_ARG2
3683 #undef LSS_BODY_ARG3
3684 #undef LSS_BODY_ARG4
3685 #undef LSS_BODY_ARG5
3686 #undef LSS_BODY_ARG6
3687
3688 #define LSS_BODY_ARG0()
3689 #define LSS_BODY_ARG1(_arg1) \
3690 [arg1] "ri" LSS_SYSCALL_ARG(_arg1),
3691 #define LSS_BODY_ARG2(_arg1, _arg2) \
3692 LSS_BODY_ARG1(_arg1) \
3693 [arg2] "ri" LSS_SYSCALL_ARG(_arg2),
3694 #define LSS_BODY_ARG3(_arg1, _arg2, _arg3) \
3695 LSS_BODY_ARG2(_arg1, _arg2) \
3696 [arg3] "ri" LSS_SYSCALL_ARG(_arg3),
3697 #define LSS_BODY_ARG4(_arg1, _arg2, _arg3, _arg4) \
3698 LSS_BODY_ARG3(_arg1, _arg2, _arg3) \
3699 [arg4] "ri" LSS_SYSCALL_ARG(_arg4),
3700 #define LSS_BODY_ARG5(_arg1, _arg2, _arg3, _arg4, _arg5) \
3701 LSS_BODY_ARG4(_arg1, _arg2, _arg3, _arg4) \
3702 [arg5] "ri" LSS_SYSCALL_ARG(_arg5),
3703 #define LSS_BODY_ARG6(_arg1, _arg2, _arg3, _arg4, _arg5, _arg6) \
3704 LSS_BODY_ARG5(_arg1, _arg2, _arg3, _arg4, _arg5) \
3705 [arg6] "ri" LSS_SYSCALL_ARG(_arg6),
3706
3707 #undef _syscall0
3708 #define _syscall0(type, name) \
3709 type LSS_NAME(name)(void) { \
3710 LSS_BODY(0, type, name); \
3711 }
3712
3713 #undef _syscall1
3714 #define _syscall1(type, name, type1, arg1) \
3715 type LSS_NAME(name)(type1 arg1) { \
3716 LSS_BODY(1, type, name, arg1) \
3717 }
3718
3719 #undef _syscall2
3720 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3721 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3722 LSS_BODY(2, type, name, arg1, arg2) \
3723 }
3724
3725 #undef _syscall3
3726 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3727 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3728 LSS_BODY(3, type, name, arg1, arg2, arg3) \
3729 }
3730
3731 #undef _syscall4
3732 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3733 type4, arg4) \
3734 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3735 LSS_BODY(4, type, name, arg1, arg2, arg3, arg4) \
3736 }
3737
3738 #undef _syscall5
3739 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3740 type4, arg4, type5, arg5) \
3741 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3742 type5 arg5) { \
3743 LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5) \
3744 }
3745
3746 #undef _syscall6
3747 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3748 type4, arg4, type5, arg5, type6, arg6) \
3749 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3750 type5 arg5, type6 arg6) { \
3751 LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6) \
3752 }
3753
3754 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3755 int flags, void *arg, int *parent_tidptr,
3756 void *newtls, int *child_tidptr) {
3757 unsigned long long __res;
3758
3759 __asm__ __volatile__ (
3760 "{\n\t"
3761 " addd,s 0x0, %[nr_clone], %%b[0]\n\t"
3762 " addd,s 0x0, %[flags], %%db[1]\n\t"
3763 " addd,s 0x0, %[child_stack], %%db[2]\n\t"
3764 " addd,s 0x0, %[parent_tidptr], %%db[3]\n\t"
3765 " addd,s 0x0, %[child_tidptr], %%db[4]\n\t"
3766 " addd,s 0x0, %[newtls], %%db[5]\n\t"
3767 "}\n\t"
3768 /* if (fn == NULL)
3769 * return -EINVAL;
3770 */
3771
3772 "{\n\t"
3773 " disp %%ctpr1, .L1\n\t"
3774 "}\n\t"
3775 "{\n\t"
3776 " cmpesb,s 0x0, %[fn], %%pred0\n\t"
3777 "}\n\t"
3778 "{\n\t"
3779 " ct %%ctpr1 ? %%pred0\n\t"
3780 "}\n\t"
3781
3782 /* if (child_stack == NULL)
3783 * return -EINVAL;
3784 */
3785 "{\n\t"
3786 " cmpesb,s 0x0, %%db[2], %%pred0\n\t"
3787 "}\n\t"
3788 "{\n\t"
3789 " ct %%ctpr1 ? %%pred0\n\t"
3790 "}\n\t"
3791
3792 /* b[0] = syscall(%b[0] = __NR_clone,
3793 * %db[1] = flags,
3794 * %db[2] = child_stack,
3795 * %db[3] = parent_tidptr,
3796 * %db[4] = child_tidptr,
3797 * %db[5] = newtls)
3798 */
3799 "{\n\t"
3800 " sdisp %%ctpr1, 0x3\n\t"
3801 "}\n\t"
3802 "{\n\t"
3803 " call %%ctpr1, wbs = %#\n\t"
3804 "}\n\t"
3805
3806 /* if (%[b0] != 0)
3807 * return %b[0];
3808 */
3809 "{\n\t"
3810 " disp %%ctpr1, .L2\n\t"
3811 " cmpesb,s 0x0, %%b[0], %%pred0\n\t"
3812 "}\n\t"
3813 "{\n\t"
3814 " ct %%ctpr1 ? ~%%pred0\n\t"
3815 "}\n\t"
3816 /* In the child, now. Call "fn(arg)".
3817 */
3818
3819 "{\n\t"
3820 " movtd,s %[fn], %%ctpr1\n\t"
3821 "}\n\t"
3822 "{\n\t"
3823 " addd,s 0x0, %[arg], %%db[0]\n\t"
3824 "}\n\t"
3825 "{\n\t"
3826 " call %%ctpr1, wbs = %#\n\t"
3827 "}\n\t"
3828 /* Call _exit(%b[0]).
3829 */
3830
3831 "{\n\t"
3832 " sdisp %%ctpr1, 0x3\n\t"
3833 " addd,s 0x0, %%b[0], %%b[1]\n\t"
3834 "}\n\t"
3835 "{\n\t"
3836 " addd,s 0x0, %[nr_exit], %%b[0]\n\t"
3837 "}\n\t"
3838 "{\n\t"
3839 " call %%ctpr1, wbs = %#\n\t"
3840 "}\n\t"
3841 "{\n\t"
3842 " disp %%ctpr1, .L2\n\t"
3843 " adds,s 0x0, 0x0, %%b[0]\n\t"
3844 "}\n\t"
3845 "{\n\t"
3846 " ct %%ctpr1\n\t"
3847 "}\n\t"
3848 ".L1:\n\t"
3849 "{\n\t"
3850 " addd,s 0x0, %[einval], %%b[0]\n\t"
3851 "}\n\t"
3852 ".L2:\n\t"
3853 "{\n\t"
3854 " addd,s 0x0, %%b[0], %[res]\n\t"
3855 "}\n\t"
3856 : [res] "=r" LSS_SYSCALL_ARG(__res)
3857 : [nr_clone] "ri" LSS_SYSCALL_ARG(__NR_clone)
3858 [arg] "ri" LSS_SYSCALL_ARG(arg)
3859 [nr_exit] "ri" LSS_SYSCALL_ARG(__NR_exit)
3860 [flags] "ri" LSS_SYSCALL_ARG(flags)
3861 [child_stack] "ri" LSS_SYSCALL_ARG(child_stack)
3862 [parent_tidptr] "ri"
3863 LSS_SYSCALL_ARG(parent_tidptr)
3864 [newtls] "ri" LSS_SYSCALL_ARG(newtls)
3865 [child_tidptr] "ri"
3866 LSS_SYSCALL_ARG(child_tidptr)
3867 [fn] "ri" LSS_SYSCALL_ARG(fn)
3868 [einval] "ri" LSS_SYSCALL_ARG(-EINVAL)
3869 : "ctpr1", "b[0]", "b[1]", "b[2]", "b[3]",
3870 "b[4]", "b[5]", "pred0");
3871 LSS_RETURN(int, __res);
3872 }
mingtaoxt xtc0c96892022-08-11 16:53:21 +08003873 #elif defined(__loongarch_lp64)
3874 /* Most definitions of _syscallX() neglect to mark "memory" as being
3875 * clobbered. This causes problems with compilers, that do a better job
3876 * at optimizing across __asm__ calls.
3877 * So, we just have to redefine all of the _syscallX() macros.
3878 */
3879 #undef LSS_REG
3880 #define LSS_REG(ar,a) register int64_t __r##ar __asm__("a"#ar) = (int64_t)a
3881 /* syscall is like subroutine calls, all caller-saved registers may be
3882 * clobbered, we should add them to the |Clobbers| list.
3883 * a0 is not included because it's in the output list.
3884 */
3885 #define LSS_SYSCALL_CLOBBERS "t0", "t1", "t2", "t3", "t4", "t5", "t6", \
3886 "t7", "t8", "memory"
3887 #undef LSS_BODY
3888 #define LSS_BODY(type,name,args...) \
3889 register int64_t __res_a0 __asm__("a0"); \
3890 int64_t __res; \
3891 __asm__ __volatile__ ("li.d $a7, %1\n" \
3892 "syscall 0x0\n" \
3893 : "=r"(__res_a0) \
3894 : "i"(__NR_##name) , ## args \
3895 : LSS_SYSCALL_CLOBBERS); \
3896 __res = __res_a0; \
3897 LSS_RETURN(type, __res)
3898 #undef _syscall0
3899 #define _syscall0(type, name) \
3900 type LSS_NAME(name)(void) { \
3901 LSS_BODY(type, name); \
3902 }
3903 #undef _syscall1
3904 #define _syscall1(type, name, type1, arg1) \
3905 type LSS_NAME(name)(type1 arg1) { \
3906 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
3907 }
3908 #undef _syscall2
3909 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3910 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3911 LSS_REG(0, arg1); LSS_REG(1, arg2); \
3912 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
3913 }
3914 #undef _syscall3
3915 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3916 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3917 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3918 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
3919 }
3920 #undef _syscall4
3921 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
3922 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3923 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3924 LSS_REG(3, arg4); \
3925 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
3926 }
3927 #undef _syscall5
3928 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3929 type5,arg5) \
3930 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3931 type5 arg5) { \
3932 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3933 LSS_REG(3, arg4); LSS_REG(4, arg5); \
3934 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
3935 "r"(__r4)); \
3936 }
3937 #undef _syscall6
3938 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3939 type5,arg5,type6,arg6) \
3940 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3941 type5 arg5, type6 arg6) { \
3942 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3943 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
3944 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
3945 "r"(__r4), "r"(__r5)); \
3946 }
3947
3948 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3949 int flags, void *arg, int *parent_tidptr,
3950 void *newtls, int *child_tidptr) {
3951 int64_t __res;
3952 {
3953 register int64_t __res_a0 __asm__("a0");
3954 register uint64_t __flags __asm__("a0") = flags;
3955 register void *__stack __asm__("a1") = child_stack;
3956 register void *__ptid __asm__("a2") = parent_tidptr;
3957 register void *__tls __asm__("a3") = newtls;
3958 register int *__ctid __asm__("a4") = child_tidptr;
3959 __asm__ __volatile__(/* Push "arg" and "fn" onto the stack that will be
3960 * used by the child.
3961 */
3962 "addi.d %2, %2, -16\n"
3963 "st.d %1, %2, 8\n"
3964 "st.d %4, %2, 0\n"
3965
3966 /* %a0 = syscall(%a0 = flags,
3967 * %a1 = child_stack,
3968 * %a2 = parent_tidptr,
3969 * %a3 = newtls,
3970 * %a4 = child_tidptr)
3971 */
3972 "li.d $a7, %8\n"
3973 "syscall 0x0\n"
3974
3975 /* if (%a0 != 0)
3976 * return %a0;
3977 */
3978 "bnez $a0, 1f\n"
3979
3980 /* In the child, now. Call "fn(arg)".
3981 */
3982 "ld.d $a0, $sp, 0\n"
3983 "ld.d $a1, $sp, 8\n"
3984 "addi.d $sp, $sp, 16\n"
3985 "jirl $ra, $a1, 0\n"
3986
3987 /* Call _exit(%a0).
3988 */
3989 "li.d $a7, %9\n"
3990 "syscall 0x0\n"
3991 "1:\n"
3992 : "=r" (__res_a0)
3993 : "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
3994 "r"(__ptid), "r"(__tls), "r"(__ctid),
3995 "i"(__NR_clone), "i"(__NR_exit)
3996 : LSS_SYSCALL_CLOBBERS);
3997 __res = __res_a0;
3998 }
3999 LSS_RETURN(int, __res);
4000 }
Konstantin Ivlev8007b272021-01-27 18:27:42 +03004001
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004002 #endif
4003 #define __NR__exit __NR_exit
4004 #define __NR__gettid __NR_gettid
4005 #define __NR__mremap __NR_mremap
phosek@chromium.orga9c02722013-08-16 17:31:42 +00004006 LSS_INLINE _syscall1(void *, brk, void *, e)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004007 LSS_INLINE _syscall1(int, chdir, const char *,p)
4008 LSS_INLINE _syscall1(int, close, int, f)
4009 LSS_INLINE _syscall2(int, clock_getres, int, c,
4010 struct kernel_timespec*, t)
4011 LSS_INLINE _syscall2(int, clock_gettime, int, c,
4012 struct kernel_timespec*, t)
4013 LSS_INLINE _syscall1(int, dup, int, f)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004014 #if defined(__NR_dup2)
4015 // dup2 is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004016 LSS_INLINE _syscall2(int, dup2, int, s,
4017 int, d)
4018 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004019 #if defined(__NR_dup3)
4020 LSS_INLINE _syscall3(int, dup3, int, s, int, d, int, f)
4021 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004022 LSS_INLINE _syscall3(int, execve, const char*, f,
4023 const char*const*,a,const char*const*, e)
4024 LSS_INLINE _syscall1(int, _exit, int, e)
4025 LSS_INLINE _syscall1(int, exit_group, int, e)
4026 LSS_INLINE _syscall3(int, fcntl, int, f,
4027 int, c, long, a)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004028 #if defined(__NR_fork)
4029 // fork is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004030 LSS_INLINE _syscall0(pid_t, fork)
4031 #endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004032 #if defined(__NR_fstat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004033 LSS_INLINE _syscall2(int, fstat, int, f,
4034 struct kernel_stat*, b)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004035 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004036 LSS_INLINE _syscall2(int, fstatfs, int, f,
4037 struct kernel_statfs*, b)
vapier@chromium.org2273e812013-04-01 17:52:44 +00004038 #if defined(__x86_64__)
4039 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
4040 LSS_INLINE int LSS_NAME(ftruncate)(int f, off_t l) {
4041 LSS_BODY(2, int, ftruncate, LSS_SYSCALL_ARG(f), (uint64_t)(l));
4042 }
4043 #else
4044 LSS_INLINE _syscall2(int, ftruncate, int, f,
4045 off_t, l)
4046 #endif
Mike Frysinger171a36a2019-01-26 23:05:43 -05004047 LSS_INLINE _syscall6(int, futex, int*, u,
4048 int, o, int, v,
4049 struct kernel_timespec*, t,
4050 int*, u2, int, v2)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004051 LSS_INLINE _syscall3(int, getdents, int, f,
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004052 struct kernel_dirent*, d, int, c)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004053 LSS_INLINE _syscall3(int, getdents64, int, f,
4054 struct kernel_dirent64*, d, int, c)
4055 LSS_INLINE _syscall0(gid_t, getegid)
4056 LSS_INLINE _syscall0(uid_t, geteuid)
Doug Kwan32a80cd2022-07-01 17:52:39 +00004057 LSS_INLINE _syscall2(int, getitimer, int, w,
4058 struct kernel_itimerval*, c)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004059 #if defined(__NR_getpgrp)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004060 LSS_INLINE _syscall0(pid_t, getpgrp)
4061 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004062 LSS_INLINE _syscall0(pid_t, getpid)
4063 LSS_INLINE _syscall0(pid_t, getppid)
4064 LSS_INLINE _syscall2(int, getpriority, int, a,
4065 int, b)
4066 LSS_INLINE _syscall3(int, getresgid, gid_t *, r,
4067 gid_t *, e, gid_t *, s)
4068 LSS_INLINE _syscall3(int, getresuid, uid_t *, r,
4069 uid_t *, e, uid_t *, s)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004070 #if defined(__NR_getrlimit)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004071 LSS_INLINE _syscall2(int, getrlimit, int, r,
4072 struct kernel_rlimit*, l)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004073 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004074 LSS_INLINE _syscall1(pid_t, getsid, pid_t, p)
4075 LSS_INLINE _syscall0(pid_t, _gettid)
4076 LSS_INLINE _syscall2(pid_t, gettimeofday, struct kernel_timeval*, t,
4077 void*, tz)
4078 LSS_INLINE _syscall5(int, setxattr, const char *,p,
4079 const char *, n, const void *,v,
4080 size_t, s, int, f)
4081 LSS_INLINE _syscall5(int, lsetxattr, const char *,p,
4082 const char *, n, const void *,v,
4083 size_t, s, int, f)
4084 LSS_INLINE _syscall4(ssize_t, getxattr, const char *,p,
4085 const char *, n, void *, v, size_t, s)
4086 LSS_INLINE _syscall4(ssize_t, lgetxattr, const char *,p,
4087 const char *, n, void *, v, size_t, s)
4088 LSS_INLINE _syscall3(ssize_t, listxattr, const char *,p,
4089 char *, l, size_t, s)
4090 LSS_INLINE _syscall3(ssize_t, llistxattr, const char *,p,
4091 char *, l, size_t, s)
4092 LSS_INLINE _syscall3(int, ioctl, int, d,
4093 int, r, void *, a)
4094 LSS_INLINE _syscall2(int, ioprio_get, int, which,
4095 int, who)
4096 LSS_INLINE _syscall3(int, ioprio_set, int, which,
4097 int, who, int, ioprio)
4098 LSS_INLINE _syscall2(int, kill, pid_t, p,
4099 int, s)
vapier@chromium.org2273e812013-04-01 17:52:44 +00004100 #if defined(__x86_64__)
4101 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
4102 LSS_INLINE off_t LSS_NAME(lseek)(int f, off_t o, int w) {
4103 _LSS_BODY(3, off_t, lseek, off_t, LSS_SYSCALL_ARG(f), (uint64_t)(o),
4104 LSS_SYSCALL_ARG(w));
4105 }
4106 #else
4107 LSS_INLINE _syscall3(off_t, lseek, int, f,
4108 off_t, o, int, w)
4109 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004110 LSS_INLINE _syscall2(int, munmap, void*, s,
4111 size_t, l)
4112 LSS_INLINE _syscall6(long, move_pages, pid_t, p,
4113 unsigned long, n, void **,g, int *, d,
4114 int *, s, int, f)
4115 LSS_INLINE _syscall3(int, mprotect, const void *,a,
4116 size_t, l, int, p)
4117 LSS_INLINE _syscall5(void*, _mremap, void*, o,
4118 size_t, os, size_t, ns,
4119 unsigned long, f, void *, a)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004120 #if defined(__NR_open)
4121 // open is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004122 LSS_INLINE _syscall3(int, open, const char*, p,
4123 int, f, int, m)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004124 #endif
4125 #if defined(__NR_poll)
4126 // poll is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004127 LSS_INLINE _syscall3(int, poll, struct kernel_pollfd*, u,
4128 unsigned int, n, int, t)
4129 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004130 #if defined(__NR_ppoll)
4131 LSS_INLINE _syscall5(int, ppoll, struct kernel_pollfd *, u,
4132 unsigned int, n, const struct kernel_timespec *, t,
4133 const struct kernel_sigset_t *, sigmask, size_t, s)
4134 #endif
mseaborn@chromium.orge6c76822013-08-31 00:08:44 +00004135 LSS_INLINE _syscall5(int, prctl, int, option,
4136 unsigned long, arg2,
4137 unsigned long, arg3,
4138 unsigned long, arg4,
4139 unsigned long, arg5)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004140 LSS_INLINE _syscall4(long, ptrace, int, r,
4141 pid_t, p, void *, a, void *, d)
4142 #if defined(__NR_quotactl)
4143 // Defined on x86_64 / i386 only
4144 LSS_INLINE _syscall4(int, quotactl, int, cmd, const char *, special,
4145 int, id, caddr_t, addr)
4146 #endif
4147 LSS_INLINE _syscall3(ssize_t, read, int, f,
4148 void *, b, size_t, c)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004149 #if defined(__NR_readlink)
4150 // readlink is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004151 LSS_INLINE _syscall3(int, readlink, const char*, p,
4152 char*, b, size_t, s)
4153 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004154 #if defined(__NR_readlinkat)
4155 LSS_INLINE _syscall4(int, readlinkat, int, d, const char *, p, char *, b,
4156 size_t, s)
4157 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004158 LSS_INLINE _syscall4(int, rt_sigaction, int, s,
4159 const struct kernel_sigaction*, a,
4160 struct kernel_sigaction*, o, size_t, c)
4161 LSS_INLINE _syscall2(int, rt_sigpending, struct kernel_sigset_t *, s,
4162 size_t, c)
4163 LSS_INLINE _syscall4(int, rt_sigprocmask, int, h,
4164 const struct kernel_sigset_t*, s,
4165 struct kernel_sigset_t*, o, size_t, c)
4166 LSS_INLINE _syscall2(int, rt_sigsuspend,
4167 const struct kernel_sigset_t*, s, size_t, c)
Joshua Peraza726d71e2019-11-13 12:21:13 -08004168 LSS_INLINE _syscall4(int, rt_sigtimedwait, const struct kernel_sigset_t*, s,
4169 siginfo_t*, i, const struct timespec*, t, size_t, c)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004170 LSS_INLINE _syscall3(int, sched_getaffinity,pid_t, p,
4171 unsigned int, l, unsigned long *, m)
4172 LSS_INLINE _syscall3(int, sched_setaffinity,pid_t, p,
4173 unsigned int, l, unsigned long *, m)
4174 LSS_INLINE _syscall0(int, sched_yield)
4175 LSS_INLINE _syscall1(long, set_tid_address, int *, t)
4176 LSS_INLINE _syscall1(int, setfsgid, gid_t, g)
4177 LSS_INLINE _syscall1(int, setfsuid, uid_t, u)
4178 LSS_INLINE _syscall1(int, setuid, uid_t, u)
4179 LSS_INLINE _syscall1(int, setgid, gid_t, g)
Doug Kwan32a80cd2022-07-01 17:52:39 +00004180 LSS_INLINE _syscall3(int, setitimer, int, w,
4181 const struct kernel_itimerval*, n,
4182 struct kernel_itimerval*, o)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004183 LSS_INLINE _syscall2(int, setpgid, pid_t, p,
4184 pid_t, g)
4185 LSS_INLINE _syscall3(int, setpriority, int, a,
4186 int, b, int, p)
4187 LSS_INLINE _syscall3(int, setresgid, gid_t, r,
4188 gid_t, e, gid_t, s)
4189 LSS_INLINE _syscall3(int, setresuid, uid_t, r,
4190 uid_t, e, uid_t, s)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004191 #if defined(__NR_setrlimit)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004192 LSS_INLINE _syscall2(int, setrlimit, int, r,
4193 const struct kernel_rlimit*, l)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004194 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004195 LSS_INLINE _syscall0(pid_t, setsid)
4196 LSS_INLINE _syscall2(int, sigaltstack, const stack_t*, s,
4197 const stack_t*, o)
4198 #if defined(__NR_sigreturn)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004199 LSS_INLINE _syscall1(int, sigreturn, unsigned long, u)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004200 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004201 #if defined(__NR_stat)
Matthew Denton92a65a82021-04-01 13:00:07 -07004202 // stat and lstat are polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004203 LSS_INLINE _syscall2(int, stat, const char*, f,
4204 struct kernel_stat*, b)
4205 #endif
Matthew Denton92a65a82021-04-01 13:00:07 -07004206 #if defined(__NR_lstat)
4207 LSS_INLINE _syscall2(int, lstat, const char*, f,
4208 struct kernel_stat*, b)
4209 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004210 LSS_INLINE _syscall2(int, statfs, const char*, f,
4211 struct kernel_statfs*, b)
4212 LSS_INLINE _syscall3(int, tgkill, pid_t, p,
4213 pid_t, t, int, s)
4214 LSS_INLINE _syscall2(int, tkill, pid_t, p,
4215 int, s)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004216 #if defined(__NR_unlink)
4217 // unlink is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004218 LSS_INLINE _syscall1(int, unlink, const char*, f)
4219 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004220 LSS_INLINE _syscall3(ssize_t, write, int, f,
4221 const void *, b, size_t, c)
4222 LSS_INLINE _syscall3(ssize_t, writev, int, f,
4223 const struct kernel_iovec*, v, size_t, c)
4224 #if defined(__NR_getcpu)
4225 LSS_INLINE _syscall3(long, getcpu, unsigned *, cpu,
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00004226 unsigned *, node, void *, unused)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004227 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004228 #if defined(__NR_fadvise64)
4229 #if defined(__x86_64__)
4230 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
4231 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset, loff_t len,
4232 int advice) {
4233 LSS_BODY(4, int, fadvise64, LSS_SYSCALL_ARG(fd), (uint64_t)(offset),
4234 (uint64_t)(len), LSS_SYSCALL_ARG(advice));
4235 }
4236 #else
4237 LSS_INLINE _syscall4(int, fadvise64,
4238 int, fd, loff_t, offset, loff_t, len, int, advice)
4239 #endif
4240 #elif defined(__i386__)
4241 #define __NR__fadvise64_64 __NR_fadvise64_64
4242 LSS_INLINE _syscall6(int, _fadvise64_64, int, fd,
4243 unsigned, offset_lo, unsigned, offset_hi,
4244 unsigned, len_lo, unsigned, len_hi,
4245 int, advice)
4246
4247 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
4248 loff_t len, int advice) {
4249 return LSS_NAME(_fadvise64_64)(fd,
4250 (unsigned)offset, (unsigned)(offset >>32),
4251 (unsigned)len, (unsigned)(len >> 32),
4252 advice);
4253 }
4254
4255 #elif defined(__s390__) && !defined(__s390x__)
4256 #define __NR__fadvise64_64 __NR_fadvise64_64
4257 struct kernel_fadvise64_64_args {
4258 int fd;
4259 long long offset;
4260 long long len;
4261 int advice;
4262 };
4263
4264 LSS_INLINE _syscall1(int, _fadvise64_64,
4265 struct kernel_fadvise64_64_args *args)
4266
4267 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
4268 loff_t len, int advice) {
4269 struct kernel_fadvise64_64_args args = { fd, offset, len, advice };
4270 return LSS_NAME(_fadvise64_64)(&args);
4271 }
4272 #endif
4273 #if defined(__NR_fallocate)
4274 #if defined(__x86_64__)
vapier@chromium.org2273e812013-04-01 17:52:44 +00004275 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
4276 LSS_INLINE int LSS_NAME(fallocate)(int f, int mode, loff_t offset,
4277 loff_t len) {
4278 LSS_BODY(4, int, fallocate, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(mode),
4279 (uint64_t)(offset), (uint64_t)(len));
4280 }
Joshua Peraza7bde79c2019-12-05 11:36:48 -08004281 #elif (defined(__i386__) || (defined(__s390__) && !defined(__s390x__)) \
4282 || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) \
4283 || (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) \
4284 || defined(__PPC__))
Bryan Chan3f6478a2016-06-14 08:38:17 -04004285 #define __NR__fallocate __NR_fallocate
4286 LSS_INLINE _syscall6(int, _fallocate, int, fd,
4287 int, mode,
4288 unsigned, offset_lo, unsigned, offset_hi,
4289 unsigned, len_lo, unsigned, len_hi)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004290
Bryan Chan3f6478a2016-06-14 08:38:17 -04004291 LSS_INLINE int LSS_NAME(fallocate)(int fd, int mode,
4292 loff_t offset, loff_t len) {
4293 union { loff_t off; unsigned w[2]; } o = { offset }, l = { len };
4294 return LSS_NAME(_fallocate)(fd, mode, o.w[0], o.w[1], l.w[0], l.w[1]);
4295 }
4296 #else
4297 LSS_INLINE _syscall4(int, fallocate,
4298 int, f, int, mode, loff_t, offset, loff_t, len)
4299 #endif
4300 #endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07004301 #if defined(__NR_getrandom)
4302 LSS_INLINE _syscall3(ssize_t, getrandom, void*, buffer, size_t, length,
4303 unsigned int, flags)
4304 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004305 #if defined(__NR_newfstatat)
4306 LSS_INLINE _syscall4(int, newfstatat, int, d,
4307 const char *, p,
4308 struct kernel_stat*, b, int, f)
4309 #endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004310 #if defined(__NR_statx)
4311 LSS_INLINE _syscall5(int, statx, int, d,
4312 const char *, p,
4313 int, f, int, m,
4314 struct kernel_statx*, b)
4315 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004316 #if defined(__x86_64__) || defined(__s390x__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004317 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
4318 gid_t *egid,
4319 gid_t *sgid) {
4320 return LSS_NAME(getresgid)(rgid, egid, sgid);
4321 }
4322
4323 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
4324 uid_t *euid,
4325 uid_t *suid) {
4326 return LSS_NAME(getresuid)(ruid, euid, suid);
4327 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004328
4329 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
4330 return LSS_NAME(setfsgid)(gid);
4331 }
4332
4333 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
4334 return LSS_NAME(setfsuid)(uid);
4335 }
4336
4337 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
4338 return LSS_NAME(setresgid)(rgid, egid, sgid);
4339 }
4340
4341 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
4342 return LSS_NAME(setresuid)(ruid, euid, suid);
4343 }
4344
4345 LSS_INLINE int LSS_NAME(sigaction)(int signum,
4346 const struct kernel_sigaction *act,
4347 struct kernel_sigaction *oldact) {
Bryan Chan3f6478a2016-06-14 08:38:17 -04004348 #if defined(__x86_64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004349 /* On x86_64, the kernel requires us to always set our own
4350 * SA_RESTORER in order to be able to return from a signal handler.
4351 * This function must have a "magic" signature that the "gdb"
4352 * (and maybe the kernel?) can recognize.
4353 */
4354 if (act != NULL && !(act->sa_flags & SA_RESTORER)) {
4355 struct kernel_sigaction a = *act;
4356 a.sa_flags |= SA_RESTORER;
4357 a.sa_restorer = LSS_NAME(restore_rt)();
4358 return LSS_NAME(rt_sigaction)(signum, &a, oldact,
4359 (KERNEL_NSIG+7)/8);
Bryan Chan3f6478a2016-06-14 08:38:17 -04004360 } else
4361 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004362 return LSS_NAME(rt_sigaction)(signum, act, oldact,
4363 (KERNEL_NSIG+7)/8);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004364 }
4365
4366 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
4367 return LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
4368 }
4369
Joshua Peraza726d71e2019-11-13 12:21:13 -08004370 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
4371 return LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
4372 }
4373 #endif
4374 #if defined(__NR_rt_sigprocmask)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004375 LSS_INLINE int LSS_NAME(sigprocmask)(int how,
4376 const struct kernel_sigset_t *set,
4377 struct kernel_sigset_t *oldset) {
4378 return LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
4379 }
Joshua Peraza726d71e2019-11-13 12:21:13 -08004380 #endif
4381 #if defined(__NR_rt_sigtimedwait)
4382 LSS_INLINE int LSS_NAME(sigtimedwait)(const struct kernel_sigset_t *set,
4383 siginfo_t *info,
4384 const struct timespec *timeout) {
4385 return LSS_NAME(rt_sigtimedwait)(set, info, timeout, (KERNEL_NSIG+7)/8);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004386 }
4387 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004388 #if defined(__NR_wait4)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004389 LSS_INLINE _syscall4(pid_t, wait4, pid_t, p,
4390 int*, s, int, o,
4391 struct kernel_rusage*, r)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004392 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004393 #if defined(__NR_openat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004394 LSS_INLINE _syscall4(int, openat, int, d, const char *, p, int, f, int, m)
Bryan Chan3f6478a2016-06-14 08:38:17 -04004395 #endif
4396 #if defined(__NR_unlinkat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004397 LSS_INLINE _syscall3(int, unlinkat, int, d, const char *, p, int, f)
4398 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004399 #if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
4400 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004401 #define __NR__getresgid32 __NR_getresgid32
4402 #define __NR__getresuid32 __NR_getresuid32
4403 #define __NR__setfsgid32 __NR_setfsgid32
4404 #define __NR__setfsuid32 __NR_setfsuid32
4405 #define __NR__setresgid32 __NR_setresgid32
4406 #define __NR__setresuid32 __NR_setresuid32
4407#if defined(__ARM_EABI__)
4408 LSS_INLINE _syscall2(int, ugetrlimit, int, r,
4409 struct kernel_rlimit*, l)
4410#endif
4411 LSS_INLINE _syscall3(int, _getresgid32, gid_t *, r,
4412 gid_t *, e, gid_t *, s)
4413 LSS_INLINE _syscall3(int, _getresuid32, uid_t *, r,
4414 uid_t *, e, uid_t *, s)
4415 LSS_INLINE _syscall1(int, _setfsgid32, gid_t, f)
4416 LSS_INLINE _syscall1(int, _setfsuid32, uid_t, f)
4417 LSS_INLINE _syscall3(int, _setresgid32, gid_t, r,
4418 gid_t, e, gid_t, s)
4419 LSS_INLINE _syscall3(int, _setresuid32, uid_t, r,
4420 uid_t, e, uid_t, s)
4421
4422 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
4423 gid_t *egid,
4424 gid_t *sgid) {
4425 int rc;
4426 if ((rc = LSS_NAME(_getresgid32)(rgid, egid, sgid)) < 0 &&
4427 LSS_ERRNO == ENOSYS) {
4428 if ((rgid == NULL) || (egid == NULL) || (sgid == NULL)) {
4429 return EFAULT;
4430 }
4431 // Clear the high bits first, since getresgid only sets 16 bits
4432 *rgid = *egid = *sgid = 0;
4433 rc = LSS_NAME(getresgid)(rgid, egid, sgid);
4434 }
4435 return rc;
4436 }
4437
4438 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
4439 uid_t *euid,
4440 uid_t *suid) {
4441 int rc;
4442 if ((rc = LSS_NAME(_getresuid32)(ruid, euid, suid)) < 0 &&
4443 LSS_ERRNO == ENOSYS) {
4444 if ((ruid == NULL) || (euid == NULL) || (suid == NULL)) {
4445 return EFAULT;
4446 }
4447 // Clear the high bits first, since getresuid only sets 16 bits
4448 *ruid = *euid = *suid = 0;
4449 rc = LSS_NAME(getresuid)(ruid, euid, suid);
4450 }
4451 return rc;
4452 }
4453
4454 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
4455 int rc;
4456 if ((rc = LSS_NAME(_setfsgid32)(gid)) < 0 &&
4457 LSS_ERRNO == ENOSYS) {
4458 if ((unsigned int)gid & ~0xFFFFu) {
4459 rc = EINVAL;
4460 } else {
4461 rc = LSS_NAME(setfsgid)(gid);
4462 }
4463 }
4464 return rc;
4465 }
4466
4467 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
4468 int rc;
4469 if ((rc = LSS_NAME(_setfsuid32)(uid)) < 0 &&
4470 LSS_ERRNO == ENOSYS) {
4471 if ((unsigned int)uid & ~0xFFFFu) {
4472 rc = EINVAL;
4473 } else {
4474 rc = LSS_NAME(setfsuid)(uid);
4475 }
4476 }
4477 return rc;
4478 }
4479
4480 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
4481 int rc;
4482 if ((rc = LSS_NAME(_setresgid32)(rgid, egid, sgid)) < 0 &&
4483 LSS_ERRNO == ENOSYS) {
4484 if ((unsigned int)rgid & ~0xFFFFu ||
4485 (unsigned int)egid & ~0xFFFFu ||
4486 (unsigned int)sgid & ~0xFFFFu) {
4487 rc = EINVAL;
4488 } else {
4489 rc = LSS_NAME(setresgid)(rgid, egid, sgid);
4490 }
4491 }
4492 return rc;
4493 }
4494
4495 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
4496 int rc;
4497 if ((rc = LSS_NAME(_setresuid32)(ruid, euid, suid)) < 0 &&
4498 LSS_ERRNO == ENOSYS) {
4499 if ((unsigned int)ruid & ~0xFFFFu ||
4500 (unsigned int)euid & ~0xFFFFu ||
4501 (unsigned int)suid & ~0xFFFFu) {
4502 rc = EINVAL;
4503 } else {
4504 rc = LSS_NAME(setresuid)(ruid, euid, suid);
4505 }
4506 }
4507 return rc;
4508 }
4509 #endif
4510 LSS_INLINE int LSS_NAME(sigemptyset)(struct kernel_sigset_t *set) {
4511 memset(&set->sig, 0, sizeof(set->sig));
4512 return 0;
4513 }
4514
4515 LSS_INLINE int LSS_NAME(sigfillset)(struct kernel_sigset_t *set) {
4516 memset(&set->sig, -1, sizeof(set->sig));
4517 return 0;
4518 }
4519
4520 LSS_INLINE int LSS_NAME(sigaddset)(struct kernel_sigset_t *set,
4521 int signum) {
Peter Kasting880985f2022-06-29 21:17:55 +00004522 if (signum < 1 || (size_t)signum > (8*sizeof(set->sig))) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004523 LSS_ERRNO = EINVAL;
4524 return -1;
4525 } else {
Peter Kasting880985f2022-06-29 21:17:55 +00004526 set->sig[(size_t)(signum - 1)/(8*sizeof(set->sig[0]))]
4527 |= 1UL << ((size_t)(signum - 1) % (8*sizeof(set->sig[0])));
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004528 return 0;
4529 }
4530 }
4531
4532 LSS_INLINE int LSS_NAME(sigdelset)(struct kernel_sigset_t *set,
4533 int signum) {
Peter Kasting880985f2022-06-29 21:17:55 +00004534 if (signum < 1 || (size_t)signum > (8*sizeof(set->sig))) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004535 LSS_ERRNO = EINVAL;
4536 return -1;
4537 } else {
Peter Kasting880985f2022-06-29 21:17:55 +00004538 set->sig[(size_t)(signum - 1)/(8*sizeof(set->sig[0]))]
4539 &= ~(1UL << ((size_t)(signum - 1) % (8*sizeof(set->sig[0]))));
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004540 return 0;
4541 }
4542 }
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004543
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004544 LSS_INLINE int LSS_NAME(sigismember)(struct kernel_sigset_t *set,
4545 int signum) {
Peter Kasting880985f2022-06-29 21:17:55 +00004546 if (signum < 1 || (size_t)signum > (8*sizeof(set->sig))) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004547 LSS_ERRNO = EINVAL;
4548 return -1;
4549 } else {
Peter Kasting880985f2022-06-29 21:17:55 +00004550 return !!(set->sig[(size_t)(signum - 1)/(8*sizeof(set->sig[0]))] &
4551 (1UL << ((size_t)(signum - 1) % (8*sizeof(set->sig[0])))));
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004552 }
4553 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04004554 #if defined(__i386__) || \
4555 defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
4556 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
4557 defined(__PPC__) || \
Konstantin Ivlev8007b272021-01-27 18:27:42 +03004558 (defined(__s390__) && !defined(__s390x__)) || defined(__e2k__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004559 #define __NR__sigaction __NR_sigaction
4560 #define __NR__sigpending __NR_sigpending
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004561 #define __NR__sigsuspend __NR_sigsuspend
4562 #define __NR__socketcall __NR_socketcall
4563 LSS_INLINE _syscall2(int, fstat64, int, f,
4564 struct kernel_stat64 *, b)
zodiac@gmail.com4f470182010-10-13 03:47:54 +00004565 LSS_INLINE _syscall5(int, _llseek, uint, fd,
4566 unsigned long, hi, unsigned long, lo,
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004567 loff_t *, res, uint, wh)
Bryan Chan3f6478a2016-06-14 08:38:17 -04004568#if defined(__s390__) && !defined(__s390x__)
4569 /* On s390, mmap2() arguments are passed in memory. */
4570 LSS_INLINE void* LSS_NAME(_mmap2)(void *s, size_t l, int p, int f, int d,
4571 off_t o) {
4572 unsigned long buf[6] = { (unsigned long) s, (unsigned long) l,
4573 (unsigned long) p, (unsigned long) f,
4574 (unsigned long) d, (unsigned long) o };
4575 LSS_REG(2, buf);
4576 LSS_BODY(void*, mmap2, "0"(__r2));
4577 }
4578#else
4579 #define __NR__mmap2 __NR_mmap2
4580 LSS_INLINE _syscall6(void*, _mmap2, void*, s,
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004581 size_t, l, int, p,
4582 int, f, int, d,
Bryan Chan3f6478a2016-06-14 08:38:17 -04004583 off_t, o)
4584#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004585 LSS_INLINE _syscall3(int, _sigaction, int, s,
4586 const struct kernel_old_sigaction*, a,
4587 struct kernel_old_sigaction*, o)
4588 LSS_INLINE _syscall1(int, _sigpending, unsigned long*, s)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004589 #ifdef __PPC__
4590 LSS_INLINE _syscall1(int, _sigsuspend, unsigned long, s)
4591 #else
4592 LSS_INLINE _syscall3(int, _sigsuspend, const void*, a,
4593 int, b,
4594 unsigned long, s)
4595 #endif
4596 LSS_INLINE _syscall2(int, stat64, const char *, p,
4597 struct kernel_stat64 *, b)
4598
4599 LSS_INLINE int LSS_NAME(sigaction)(int signum,
4600 const struct kernel_sigaction *act,
4601 struct kernel_sigaction *oldact) {
4602 int old_errno = LSS_ERRNO;
4603 int rc;
4604 struct kernel_sigaction a;
4605 if (act != NULL) {
4606 a = *act;
4607 #ifdef __i386__
4608 /* On i386, the kernel requires us to always set our own
4609 * SA_RESTORER when using realtime signals. Otherwise, it does not
4610 * know how to return from a signal handler. This function must have
4611 * a "magic" signature that the "gdb" (and maybe the kernel?) can
4612 * recognize.
4613 * Apparently, a SA_RESTORER is implicitly set by the kernel, when
4614 * using non-realtime signals.
4615 *
4616 * TODO: Test whether ARM needs a restorer
4617 */
4618 if (!(a.sa_flags & SA_RESTORER)) {
4619 a.sa_flags |= SA_RESTORER;
4620 a.sa_restorer = (a.sa_flags & SA_SIGINFO)
4621 ? LSS_NAME(restore_rt)() : LSS_NAME(restore)();
4622 }
4623 #endif
4624 }
4625 rc = LSS_NAME(rt_sigaction)(signum, act ? &a : act, oldact,
4626 (KERNEL_NSIG+7)/8);
4627 if (rc < 0 && LSS_ERRNO == ENOSYS) {
4628 struct kernel_old_sigaction oa, ooa, *ptr_a = &oa, *ptr_oa = &ooa;
4629 if (!act) {
4630 ptr_a = NULL;
4631 } else {
4632 oa.sa_handler_ = act->sa_handler_;
4633 memcpy(&oa.sa_mask, &act->sa_mask, sizeof(oa.sa_mask));
4634 #ifndef __mips__
4635 oa.sa_restorer = act->sa_restorer;
4636 #endif
4637 oa.sa_flags = act->sa_flags;
4638 }
4639 if (!oldact) {
4640 ptr_oa = NULL;
4641 }
4642 LSS_ERRNO = old_errno;
4643 rc = LSS_NAME(_sigaction)(signum, ptr_a, ptr_oa);
4644 if (rc == 0 && oldact) {
4645 if (act) {
4646 memcpy(oldact, act, sizeof(*act));
4647 } else {
4648 memset(oldact, 0, sizeof(*oldact));
4649 }
4650 oldact->sa_handler_ = ptr_oa->sa_handler_;
4651 oldact->sa_flags = ptr_oa->sa_flags;
4652 memcpy(&oldact->sa_mask, &ptr_oa->sa_mask, sizeof(ptr_oa->sa_mask));
4653 #ifndef __mips__
4654 oldact->sa_restorer = ptr_oa->sa_restorer;
4655 #endif
4656 }
4657 }
4658 return rc;
4659 }
4660
4661 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
4662 int old_errno = LSS_ERRNO;
4663 int rc = LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
4664 if (rc < 0 && LSS_ERRNO == ENOSYS) {
4665 LSS_ERRNO = old_errno;
4666 LSS_NAME(sigemptyset)(set);
4667 rc = LSS_NAME(_sigpending)(&set->sig[0]);
4668 }
4669 return rc;
4670 }
4671
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004672 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
4673 int olderrno = LSS_ERRNO;
4674 int rc = LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
4675 if (rc < 0 && LSS_ERRNO == ENOSYS) {
4676 LSS_ERRNO = olderrno;
4677 rc = LSS_NAME(_sigsuspend)(
4678 #ifndef __PPC__
4679 set, 0,
4680 #endif
4681 set->sig[0]);
4682 }
4683 return rc;
4684 }
4685 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004686 #if defined(__i386__) || \
4687 defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
4688 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
4689 defined(__PPC__) || \
4690 (defined(__s390__) && !defined(__s390x__))
4691 /* On these architectures, implement mmap() with mmap2(). */
4692 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4693 int64_t o) {
4694 if (o % 4096) {
4695 LSS_ERRNO = EINVAL;
4696 return (void *) -1;
4697 }
4698 return LSS_NAME(_mmap2)(s, l, p, f, d, (o / 4096));
4699 }
4700 #elif defined(__s390x__)
4701 /* On s390x, mmap() arguments are passed in memory. */
4702 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4703 int64_t o) {
4704 unsigned long buf[6] = { (unsigned long) s, (unsigned long) l,
4705 (unsigned long) p, (unsigned long) f,
4706 (unsigned long) d, (unsigned long) o };
4707 LSS_REG(2, buf);
4708 LSS_BODY(void*, mmap, "0"(__r2));
4709 }
4710 #elif defined(__x86_64__)
4711 /* Need to make sure __off64_t isn't truncated to 32-bits under x32. */
4712 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4713 int64_t o) {
4714 LSS_BODY(6, void*, mmap, LSS_SYSCALL_ARG(s), LSS_SYSCALL_ARG(l),
4715 LSS_SYSCALL_ARG(p), LSS_SYSCALL_ARG(f),
4716 LSS_SYSCALL_ARG(d), (uint64_t)(o));
4717 }
4718 #else
4719 /* Remaining 64-bit architectures. */
4720 LSS_INLINE _syscall6(void*, mmap, void*, addr, size_t, length, int, prot,
4721 int, flags, int, fd, int64_t, offset)
4722 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004723 #if defined(__PPC__)
4724 #undef LSS_SC_LOADARGS_0
4725 #define LSS_SC_LOADARGS_0(dummy...)
4726 #undef LSS_SC_LOADARGS_1
4727 #define LSS_SC_LOADARGS_1(arg1) \
4728 __sc_4 = (unsigned long) (arg1)
4729 #undef LSS_SC_LOADARGS_2
4730 #define LSS_SC_LOADARGS_2(arg1, arg2) \
4731 LSS_SC_LOADARGS_1(arg1); \
4732 __sc_5 = (unsigned long) (arg2)
4733 #undef LSS_SC_LOADARGS_3
4734 #define LSS_SC_LOADARGS_3(arg1, arg2, arg3) \
4735 LSS_SC_LOADARGS_2(arg1, arg2); \
4736 __sc_6 = (unsigned long) (arg3)
4737 #undef LSS_SC_LOADARGS_4
4738 #define LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4) \
4739 LSS_SC_LOADARGS_3(arg1, arg2, arg3); \
4740 __sc_7 = (unsigned long) (arg4)
4741 #undef LSS_SC_LOADARGS_5
4742 #define LSS_SC_LOADARGS_5(arg1, arg2, arg3, arg4, arg5) \
4743 LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4); \
4744 __sc_8 = (unsigned long) (arg5)
4745 #undef LSS_SC_BODY
4746 #define LSS_SC_BODY(nr, type, opt, args...) \
4747 long __sc_ret, __sc_err; \
4748 { \
4749 register unsigned long __sc_0 __asm__ ("r0") = __NR_socketcall; \
4750 register unsigned long __sc_3 __asm__ ("r3") = opt; \
4751 register unsigned long __sc_4 __asm__ ("r4"); \
4752 register unsigned long __sc_5 __asm__ ("r5"); \
4753 register unsigned long __sc_6 __asm__ ("r6"); \
4754 register unsigned long __sc_7 __asm__ ("r7"); \
4755 register unsigned long __sc_8 __asm__ ("r8"); \
4756 LSS_SC_LOADARGS_##nr(args); \
4757 __asm__ __volatile__ \
4758 ("stwu 1, -48(1)\n\t" \
4759 "stw 4, 20(1)\n\t" \
4760 "stw 5, 24(1)\n\t" \
4761 "stw 6, 28(1)\n\t" \
4762 "stw 7, 32(1)\n\t" \
4763 "stw 8, 36(1)\n\t" \
4764 "addi 4, 1, 20\n\t" \
4765 "sc\n\t" \
4766 "mfcr %0" \
4767 : "=&r" (__sc_0), \
4768 "=&r" (__sc_3), "=&r" (__sc_4), \
4769 "=&r" (__sc_5), "=&r" (__sc_6), \
4770 "=&r" (__sc_7), "=&r" (__sc_8) \
4771 : LSS_ASMINPUT_##nr \
4772 : "cr0", "ctr", "memory"); \
4773 __sc_ret = __sc_3; \
4774 __sc_err = __sc_0; \
4775 } \
4776 LSS_RETURN(type, __sc_ret, __sc_err)
4777
4778 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
4779 int flags){
4780 LSS_SC_BODY(3, ssize_t, 17, s, msg, flags);
4781 }
4782
4783 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
4784 const struct kernel_msghdr *msg,
4785 int flags) {
4786 LSS_SC_BODY(3, ssize_t, 16, s, msg, flags);
4787 }
4788
4789 // TODO(csilvers): why is this ifdef'ed out?
4790#if 0
4791 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
4792 int flags,
4793 const struct kernel_sockaddr *to,
4794 unsigned int tolen) {
4795 LSS_BODY(6, ssize_t, 11, s, buf, len, flags, to, tolen);
4796 }
4797#endif
4798
4799 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
4800 LSS_SC_BODY(2, int, 13, s, how);
4801 }
4802
4803 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
4804 LSS_SC_BODY(3, int, 1, domain, type, protocol);
4805 }
4806
4807 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
4808 int sv[2]) {
4809 LSS_SC_BODY(4, int, 8, d, type, protocol, sv);
4810 }
4811 #endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004812 #if defined(__NR_recvmsg)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004813 LSS_INLINE _syscall3(ssize_t, recvmsg, int, s, struct kernel_msghdr*, msg,
4814 int, flags)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004815 #endif
4816 #if defined(__NR_sendmsg)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004817 LSS_INLINE _syscall3(ssize_t, sendmsg, int, s, const struct kernel_msghdr*,
4818 msg, int, flags)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004819 #endif
4820 #if defined(__NR_sendto)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004821 LSS_INLINE _syscall6(ssize_t, sendto, int, s, const void*, buf, size_t,len,
4822 int, flags, const struct kernel_sockaddr*, to,
4823 unsigned int, tolen)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004824 #endif
4825 #if defined(__NR_shutdown)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004826 LSS_INLINE _syscall2(int, shutdown, int, s, int, how)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004827 #endif
4828 #if defined(__NR_socket)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004829 LSS_INLINE _syscall3(int, socket, int, domain, int, type, int, protocol)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004830 #endif
4831 #if defined(__NR_socketpair)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004832 LSS_INLINE _syscall4(int, socketpair, int, d, int, type, int, protocol,
4833 int*, sv)
4834 #endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004835
4836 #if defined(__NR_socketcall)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004837 LSS_INLINE _syscall2(int, _socketcall, int, c,
4838 va_list, a)
4839 LSS_INLINE int LSS_NAME(socketcall)(int op, ...) {
4840 int rc;
4841 va_list ap;
4842 va_start(ap, op);
4843 rc = LSS_NAME(_socketcall)(op, ap);
4844 va_end(ap);
4845 return rc;
4846 }
4847
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004848 # if !defined(__NR_recvmsg)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004849 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
4850 int flags){
4851 return (ssize_t)LSS_NAME(socketcall)(17, s, msg, flags);
4852 }
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004853 # endif
4854 # if !defined(__NR_sendmsg)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004855 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
4856 const struct kernel_msghdr *msg,
4857 int flags) {
4858 return (ssize_t)LSS_NAME(socketcall)(16, s, msg, flags);
4859 }
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004860 # endif
4861 # if !defined(__NR_sendto)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004862 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
4863 int flags,
4864 const struct kernel_sockaddr *to,
4865 unsigned int tolen) {
4866 return (ssize_t)LSS_NAME(socketcall)(11, s, buf, len, flags, to, tolen);
4867 }
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004868 # endif
4869 # if !defined(__NR_shutdown)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004870 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
4871 return LSS_NAME(socketcall)(13, s, how);
4872 }
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004873 # endif
4874 # if !defined(__NR_socket)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004875 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
4876 return LSS_NAME(socketcall)(1, domain, type, protocol);
4877 }
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004878 # endif
4879 # if !defined(__NR_socketpair)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004880 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
4881 int sv[2]) {
4882 return LSS_NAME(socketcall)(8, d, type, protocol, sv);
4883 }
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004884 # endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004885 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004886 #if defined(__NR_fstatat64)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004887 LSS_INLINE _syscall4(int, fstatat64, int, d,
4888 const char *, p,
4889 struct kernel_stat64 *, b, int, f)
4890 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004891 #if defined(__NR_waitpid)
4892 // waitpid is polyfilled below when not available.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004893 LSS_INLINE _syscall3(pid_t, waitpid, pid_t, p,
4894 int*, s, int, o)
4895 #endif
4896 #if defined(__mips__)
4897 /* sys_pipe() on MIPS has non-standard calling conventions, as it returns
4898 * both file handles through CPU registers.
4899 */
4900 LSS_INLINE int LSS_NAME(pipe)(int *p) {
4901 register unsigned long __v0 __asm__("$2") = __NR_pipe;
4902 register unsigned long __v1 __asm__("$3");
4903 register unsigned long __r7 __asm__("$7");
4904 __asm__ __volatile__ ("syscall\n"
vapier@chromium.orgda4a4892015-01-22 16:46:39 +00004905 : "=r"(__v0), "=r"(__v1), "=r" (__r7)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004906 : "0"(__v0)
4907 : "$8", "$9", "$10", "$11", "$12",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00004908 "$13", "$14", "$15", "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004909 if (__r7) {
zodiac@gmail.coma6591482012-04-13 01:29:30 +00004910 unsigned long __errnovalue = __v0;
4911 LSS_ERRNO = __errnovalue;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004912 return -1;
4913 } else {
4914 p[0] = __v0;
4915 p[1] = __v1;
4916 return 0;
4917 }
4918 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004919 #elif defined(__NR_pipe)
4920 // pipe is polyfilled below when not available.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004921 LSS_INLINE _syscall1(int, pipe, int *, p)
4922 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004923 #if defined(__NR_pipe2)
4924 LSS_INLINE _syscall2(int, pipe2, int *, pipefd, int, flags)
4925 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004926 /* TODO(csilvers): see if ppc can/should support this as well */
4927 #if defined(__i386__) || defined(__ARM_ARCH_3__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -04004928 defined(__ARM_EABI__) || \
4929 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64) || \
4930 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004931 #define __NR__statfs64 __NR_statfs64
4932 #define __NR__fstatfs64 __NR_fstatfs64
4933 LSS_INLINE _syscall3(int, _statfs64, const char*, p,
4934 size_t, s,struct kernel_statfs64*, b)
4935 LSS_INLINE _syscall3(int, _fstatfs64, int, f,
4936 size_t, s,struct kernel_statfs64*, b)
4937 LSS_INLINE int LSS_NAME(statfs64)(const char *p,
4938 struct kernel_statfs64 *b) {
4939 return LSS_NAME(_statfs64)(p, sizeof(*b), b);
4940 }
4941 LSS_INLINE int LSS_NAME(fstatfs64)(int f,struct kernel_statfs64 *b) {
4942 return LSS_NAME(_fstatfs64)(f, sizeof(*b), b);
4943 }
4944 #endif
4945
4946 LSS_INLINE int LSS_NAME(execv)(const char *path, const char *const argv[]) {
4947 extern char **environ;
4948 return LSS_NAME(execve)(path, argv, (const char *const *)environ);
4949 }
4950
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00004951 LSS_INLINE pid_t LSS_NAME(gettid)(void) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004952 pid_t tid = LSS_NAME(_gettid)();
4953 if (tid != -1) {
4954 return tid;
4955 }
4956 return LSS_NAME(getpid)();
4957 }
4958
4959 LSS_INLINE void *LSS_NAME(mremap)(void *old_address, size_t old_size,
4960 size_t new_size, int flags, ...) {
4961 va_list ap;
4962 void *new_address, *rc;
4963 va_start(ap, flags);
4964 new_address = va_arg(ap, void *);
4965 rc = LSS_NAME(_mremap)(old_address, old_size, new_size,
Peter Kasting880985f2022-06-29 21:17:55 +00004966 (unsigned long)flags, new_address);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004967 va_end(ap);
4968 return rc;
4969 }
4970
Peter Kasting880985f2022-06-29 21:17:55 +00004971 LSS_INLINE long LSS_NAME(ptrace_detach)(pid_t pid) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004972 /* PTRACE_DETACH can sometimes forget to wake up the tracee and it
4973 * then sends job control signals to the real parent, rather than to
4974 * the tracer. We reduce the risk of this happening by starting a
4975 * whole new time slice, and then quickly sending a SIGCONT signal
4976 * right after detaching from the tracee.
4977 *
4978 * We use tkill to ensure that we only issue a wakeup for the thread being
4979 * detached. Large multi threaded apps can take a long time in the kernel
4980 * processing SIGCONT.
4981 */
Peter Kasting880985f2022-06-29 21:17:55 +00004982 long rc;
4983 int err;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004984 LSS_NAME(sched_yield)();
4985 rc = LSS_NAME(ptrace)(PTRACE_DETACH, pid, (void *)0, (void *)0);
4986 err = LSS_ERRNO;
4987 LSS_NAME(tkill)(pid, SIGCONT);
4988 /* Old systems don't have tkill */
4989 if (LSS_ERRNO == ENOSYS)
4990 LSS_NAME(kill)(pid, SIGCONT);
4991 LSS_ERRNO = err;
4992 return rc;
4993 }
4994
4995 LSS_INLINE int LSS_NAME(raise)(int sig) {
4996 return LSS_NAME(kill)(LSS_NAME(getpid)(), sig);
4997 }
4998
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00004999 LSS_INLINE int LSS_NAME(setpgrp)(void) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005000 return LSS_NAME(setpgid)(0, 0);
5001 }
5002
vapier@chromium.org2273e812013-04-01 17:52:44 +00005003 #if defined(__x86_64__)
5004 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
5005 LSS_INLINE ssize_t LSS_NAME(pread64)(int f, void *b, size_t c, loff_t o) {
5006 LSS_BODY(4, ssize_t, pread64, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(b),
5007 LSS_SYSCALL_ARG(c), (uint64_t)(o));
5008 }
5009
5010 LSS_INLINE ssize_t LSS_NAME(pwrite64)(int f, const void *b, size_t c,
5011 loff_t o) {
5012 LSS_BODY(4, ssize_t, pwrite64, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(b),
5013 LSS_SYSCALL_ARG(c), (uint64_t)(o));
5014 }
5015
Peter Kasting3bb68592022-07-18 19:29:31 +00005016 LSS_INLINE int LSS_NAME(readahead)(int f, loff_t o, size_t c) {
vapier@chromium.org2273e812013-04-01 17:52:44 +00005017 LSS_BODY(3, int, readahead, LSS_SYSCALL_ARG(f), (uint64_t)(o),
5018 LSS_SYSCALL_ARG(c));
5019 }
5020 #elif defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI64
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005021 LSS_INLINE _syscall4(ssize_t, pread64, int, f,
5022 void *, b, size_t, c,
5023 loff_t, o)
5024 LSS_INLINE _syscall4(ssize_t, pwrite64, int, f,
5025 const void *, b, size_t, c,
5026 loff_t, o)
5027 LSS_INLINE _syscall3(int, readahead, int, f,
5028 loff_t, o, unsigned, c)
5029 #else
5030 #define __NR__pread64 __NR_pread64
5031 #define __NR__pwrite64 __NR_pwrite64
5032 #define __NR__readahead __NR_readahead
mseaborn@chromium.org2c73abf2012-09-15 03:46:48 +00005033 #if defined(__ARM_EABI__) || defined(__mips__)
5034 /* On ARM and MIPS, a 64-bit parameter has to be in an even-odd register
5035 * pair. Hence these calls ignore their fourth argument (r3) so that their
mcgrathr@google.coma7999932011-11-21 22:26:20 +00005036 * fifth and sixth make such a pair (r4,r5).
5037 */
5038 #define LSS_LLARG_PAD 0,
5039 LSS_INLINE _syscall6(ssize_t, _pread64, int, f,
5040 void *, b, size_t, c,
5041 unsigned, skip, unsigned, o1, unsigned, o2)
5042 LSS_INLINE _syscall6(ssize_t, _pwrite64, int, f,
5043 const void *, b, size_t, c,
5044 unsigned, skip, unsigned, o1, unsigned, o2)
5045 LSS_INLINE _syscall5(int, _readahead, int, f,
5046 unsigned, skip,
5047 unsigned, o1, unsigned, o2, size_t, c)
5048 #else
5049 #define LSS_LLARG_PAD
5050 LSS_INLINE _syscall5(ssize_t, _pread64, int, f,
5051 void *, b, size_t, c, unsigned, o1,
5052 unsigned, o2)
5053 LSS_INLINE _syscall5(ssize_t, _pwrite64, int, f,
5054 const void *, b, size_t, c, unsigned, o1,
Samuel Attardacbdd592022-07-26 16:02:01 +00005055 unsigned, o2)
mcgrathr@google.coma7999932011-11-21 22:26:20 +00005056 LSS_INLINE _syscall4(int, _readahead, int, f,
5057 unsigned, o1, unsigned, o2, size_t, c)
5058 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005059 /* We force 64bit-wide parameters onto the stack, then access each
5060 * 32-bit component individually. This guarantees that we build the
5061 * correct parameters independent of the native byte-order of the
5062 * underlying architecture.
5063 */
5064 LSS_INLINE ssize_t LSS_NAME(pread64)(int fd, void *buf, size_t count,
5065 loff_t off) {
5066 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00005067 return LSS_NAME(_pread64)(fd, buf, count,
5068 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005069 }
5070 LSS_INLINE ssize_t LSS_NAME(pwrite64)(int fd, const void *buf,
5071 size_t count, loff_t off) {
5072 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00005073 return LSS_NAME(_pwrite64)(fd, buf, count,
5074 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005075 }
Peter Kasting3bb68592022-07-18 19:29:31 +00005076 LSS_INLINE int LSS_NAME(readahead)(int fd, loff_t off, size_t count) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005077 union { loff_t off; unsigned arg[2]; } o = { off };
Peter Kasting3bb68592022-07-18 19:29:31 +00005078 return LSS_NAME(_readahead)(fd, LSS_LLARG_PAD o.arg[0], o.arg[1], count);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005079 }
5080 #endif
5081#endif
5082
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005083/*
5084 * Polyfills for deprecated syscalls.
5085 */
5086
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005087#if !defined(__NR_dup2)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005088 LSS_INLINE int LSS_NAME(dup2)(int s, int d) {
5089 return LSS_NAME(dup3)(s, d, 0);
5090 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005091#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005092
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005093#if !defined(__NR_open)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005094 LSS_INLINE int LSS_NAME(open)(const char *pathname, int flags, int mode) {
5095 return LSS_NAME(openat)(AT_FDCWD, pathname, flags, mode);
5096 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005097#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005098
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005099#if !defined(__NR_unlink)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005100 LSS_INLINE int LSS_NAME(unlink)(const char *pathname) {
5101 return LSS_NAME(unlinkat)(AT_FDCWD, pathname, 0);
5102 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005103#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005104
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005105#if !defined(__NR_readlink)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005106 LSS_INLINE int LSS_NAME(readlink)(const char *pathname, char *buffer,
5107 size_t size) {
5108 return LSS_NAME(readlinkat)(AT_FDCWD, pathname, buffer, size);
5109 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005110#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005111
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005112#if !defined(__NR_pipe)
Mike Frysinger4ce4c482018-01-03 18:31:42 -05005113 LSS_INLINE int LSS_NAME(pipe)(int *pipefd) {
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005114 return LSS_NAME(pipe2)(pipefd, 0);
5115 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005116#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005117
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005118#if !defined(__NR_poll)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005119 LSS_INLINE int LSS_NAME(poll)(struct kernel_pollfd *fds, unsigned int nfds,
5120 int timeout) {
5121 struct kernel_timespec timeout_ts;
5122 struct kernel_timespec *timeout_ts_p = NULL;
5123
5124 if (timeout >= 0) {
5125 timeout_ts.tv_sec = timeout / 1000;
5126 timeout_ts.tv_nsec = (timeout % 1000) * 1000000;
5127 timeout_ts_p = &timeout_ts;
5128 }
5129 return LSS_NAME(ppoll)(fds, nfds, timeout_ts_p, NULL, 0);
5130 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005131#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005132
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005133#if defined(__NR_statx)
5134 /* copy the contents of kernel_statx to the kernel_stat structure. */
5135 LSS_INLINE void LSS_NAME(cp_stat_statx)(struct kernel_stat *to,
5136 struct kernel_statx *from) {
5137 memset(to, 0, sizeof(struct kernel_stat));
5138 to->st_dev = ((from->stx_dev_minor & 0xff) |
5139 ((from->stx_dev_major & 0xfff) << 8) |
Peter Kastingaec5a5b2022-08-21 18:16:05 +00005140 ((from->stx_dev_minor & ~0xffu) << 12));
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005141 to->st_rdev = ((from->stx_rdev_minor & 0xff) |
5142 ((from->stx_rdev_major & 0xfff) << 8) |
Peter Kastingaec5a5b2022-08-21 18:16:05 +00005143 ((from->stx_rdev_minor & ~0xffu) << 12));
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005144 to->st_ino = from->stx_ino;
5145 to->st_mode = from->stx_mode;
5146 to->st_nlink = from->stx_nlink;
5147 to->st_uid = from->stx_uid;
5148 to->st_gid = from->stx_gid;
Peter Kasting99121a32022-08-22 16:43:44 +00005149 to->st_atime_ = (kernel_timespec)(from->stx_atime.tv_sec);
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005150 to->st_atime_nsec_ = from->stx_atime.tv_nsec;
Peter Kasting99121a32022-08-22 16:43:44 +00005151 to->st_mtime_ = (kernel_timespec)(from->stx_mtime.tv_sec);
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005152 to->st_mtime_nsec_ = from->stx_mtime.tv_nsec;
Peter Kasting99121a32022-08-22 16:43:44 +00005153 to->st_ctime_ = (kernel_timespec)(from->stx_ctime.tv_sec);
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005154 to->st_ctime_nsec_ = from->stx_ctime.tv_nsec;
Peter Kasting99121a32022-08-22 16:43:44 +00005155 to->st_size = (kernel_off_t)(from->stx_size);
5156 to->st_blocks = (kernel_blkcnt_t)(from->stx_blocks);
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005157 to->st_blksize = from->stx_blksize;
5158 }
5159#endif
5160
5161#if !defined(__NR_fstat)
5162 LSS_INLINE int LSS_NAME(fstat)(int fd,
5163 struct kernel_stat *buf) {
5164 #if defined(__NR_newfstatat)
5165 return LSS_NAME(newfstatat)(fd, "", buf, AT_EMPTY_PATH);
5166 #elif defined(__NR_statx)
5167 struct kernel_statx stx;
5168 int flags = AT_NO_AUTOMOUNT | AT_EMPTY_PATH;
5169 int mask = STATX_BASIC_STATS;
5170 int res = LSS_NAME(statx)(fd, "", flags, mask, &stx);
5171 LSS_NAME(cp_stat_statx)(buf, &stx);
5172 return res;
5173 #endif
5174 }
5175#endif
5176
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005177#if !defined(__NR_stat)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005178 LSS_INLINE int LSS_NAME(stat)(const char *pathname,
5179 struct kernel_stat *buf) {
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005180 #if defined(__NR_newfstatat)
5181 return LSS_NAME(newfstatat)(AT_FDCWD, pathname, buf, 0);
5182 #elif defined(__NR_statx)
5183 struct kernel_statx stx;
5184 int flags = AT_NO_AUTOMOUNT | AT_STATX_SYNC_AS_STAT;
5185 int mask = STATX_BASIC_STATS;
5186 int res = LSS_NAME(statx)(AT_FDCWD, pathname, flags, mask, &stx);
5187 LSS_NAME(cp_stat_statx)(buf, &stx);
5188 return res;
5189 #endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005190 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005191#endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005192
Matthew Denton92a65a82021-04-01 13:00:07 -07005193#if !defined(__NR_lstat)
5194 LSS_INLINE int LSS_NAME(lstat)(const char *pathname,
5195 struct kernel_stat *buf) {
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005196 #if defined(__NR_newfstatat)
5197 return LSS_NAME(newfstatat)(AT_FDCWD, pathname, buf, AT_SYMLINK_NOFOLLOW);
5198 #elif defined(__NR_statx)
5199 struct kernel_statx stx;
5200 int flags = AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW;
5201 int mask = STATX_BASIC_STATS;
5202 int res = LSS_NAME(statx)(AT_FDCWD, pathname, flags, mask, &stx);
5203 LSS_NAME(cp_stat_statx)(buf, &stx);
5204 return res;
5205 #endif
Matthew Denton92a65a82021-04-01 13:00:07 -07005206 }
5207#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005208
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005209#if !defined(__NR_waitpid)
5210 LSS_INLINE pid_t LSS_NAME(waitpid)(pid_t pid, int *status, int options) {
5211 return LSS_NAME(wait4)(pid, status, options, 0);
5212 }
5213#endif
5214
5215#if !defined(__NR_fork)
5216// TODO: define this in an arch-independant way instead of inlining the clone
5217// syscall body.
5218
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005219# if defined(__aarch64__) || defined(__riscv) || defined(__loongarch_lp64)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005220 LSS_INLINE pid_t LSS_NAME(fork)(void) {
5221 // No fork syscall on aarch64 - implement by means of the clone syscall.
5222 // Note that this does not reset glibc's cached view of the PID/TID, so
5223 // some glibc interfaces might go wrong in the forked subprocess.
5224 int flags = SIGCHLD;
5225 void *child_stack = NULL;
5226 void *parent_tidptr = NULL;
5227 void *newtls = NULL;
5228 void *child_tidptr = NULL;
5229
5230 LSS_REG(0, flags);
5231 LSS_REG(1, child_stack);
5232 LSS_REG(2, parent_tidptr);
5233 LSS_REG(3, newtls);
5234 LSS_REG(4, child_tidptr);
5235 LSS_BODY(pid_t, clone, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3),
5236 "r"(__r4));
5237 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005238# elif defined(__x86_64__)
5239 LSS_INLINE pid_t LSS_NAME(fork)(void) {
5240 // Android disallows the fork syscall on x86_64 - implement by means of the
5241 // clone syscall as above for aarch64.
5242 int flags = SIGCHLD;
5243 void *child_stack = NULL;
5244 void *parent_tidptr = NULL;
5245 void *newtls = NULL;
5246 void *child_tidptr = NULL;
5247
5248 LSS_BODY(5, pid_t, clone, LSS_SYSCALL_ARG(flags),
5249 LSS_SYSCALL_ARG(child_stack), LSS_SYSCALL_ARG(parent_tidptr),
5250 LSS_SYSCALL_ARG(newtls), LSS_SYSCALL_ARG(child_tidptr));
5251 }
5252# else
5253# error missing fork polyfill for this architecture
5254# endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005255#endif
5256
Michael Forneyf70e2f12020-01-22 19:19:38 -08005257/* These restore the original values of these macros saved by the
5258 * corresponding #pragma push_macro near the top of this file. */
5259#pragma pop_macro("stat64")
5260#pragma pop_macro("fstat64")
5261#pragma pop_macro("lstat64")
5262#pragma pop_macro("pread64")
5263#pragma pop_macro("pwrite64")
Michael Forneyfd00dbb2020-03-10 14:12:52 -07005264#pragma pop_macro("getdents64")
mseaborn@chromium.orgca749372012-09-05 18:26:20 +00005265
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005266#if defined(__cplusplus) && !defined(SYS_CPLUSPLUS)
5267}
5268#endif
5269
5270#endif
5271#endif