blob: bfa95f46045f64888831f83ad62cd28bcc765aea [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 unsigned long long kernel_blkcnt_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000369typedef unsigned kernel_dev_t;
370typedef unsigned kernel_gid_t;
371typedef unsigned long long kernel_ino_t;
372typedef unsigned kernel_nlink_t;
373typedef long long kernel_off_t;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000374typedef unsigned kernel_time_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000375typedef unsigned kernel_uid_t;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000376struct kernel_stat {
377#else
378struct kernel_stat64 {
379#endif
380 unsigned st_dev;
381 unsigned __pad0[3];
382 unsigned long long st_ino;
383 unsigned st_mode;
384 unsigned st_nlink;
385 unsigned st_uid;
386 unsigned st_gid;
387 unsigned st_rdev;
388 unsigned __pad1[3];
389 long long st_size;
390 unsigned st_atime_;
391 unsigned st_atime_nsec_;
392 unsigned st_mtime_;
393 unsigned st_mtime_nsec_;
394 unsigned st_ctime_;
395 unsigned st_ctime_nsec_;
396 unsigned st_blksize;
397 unsigned __pad2;
398 unsigned long long st_blocks;
399};
400#elif defined __PPC__
401struct kernel_stat64 {
402 unsigned long long st_dev;
403 unsigned long long st_ino;
404 unsigned st_mode;
405 unsigned st_nlink;
406 unsigned st_uid;
407 unsigned st_gid;
408 unsigned long long st_rdev;
409 unsigned short int __pad2;
410 long long st_size;
411 long st_blksize;
412 long long st_blocks;
413 long st_atime_;
414 unsigned long st_atime_nsec_;
415 long st_mtime_;
416 unsigned long st_mtime_nsec_;
417 long st_ctime_;
418 unsigned long st_ctime_nsec_;
419 unsigned long __unused4;
420 unsigned long __unused5;
421};
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300422#elif defined(__e2k__)
423struct kernel_stat64 {
424 unsigned long long st_dev;
425 unsigned long long st_ino;
426 unsigned int st_mode;
427 unsigned int st_nlink;
428 unsigned int st_uid;
429 unsigned int st_gid;
430 unsigned long long st_rdev;
431 long long st_size;
432 int st_blksize;
433 int __pad2;
434 unsigned long long st_blocks;
435 int st_atime_;
436 unsigned int st_atime_nsec_;
437 int st_mtime_;
438 unsigned int st_mtime_nsec_;
439 int st_ctime_;
440 unsigned int st_ctime_nsec_;
441 unsigned int __unused4;
442 unsigned int __unused5;
443};
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000444#else
445struct kernel_stat64 {
446 unsigned long long st_dev;
447 unsigned char __pad0[4];
448 unsigned __st_ino;
449 unsigned st_mode;
450 unsigned st_nlink;
451 unsigned st_uid;
452 unsigned st_gid;
453 unsigned long long st_rdev;
454 unsigned char __pad3[4];
455 long long st_size;
456 unsigned st_blksize;
457 unsigned long long st_blocks;
458 unsigned st_atime_;
459 unsigned st_atime_nsec_;
460 unsigned st_mtime_;
461 unsigned st_mtime_nsec_;
462 unsigned st_ctime_;
463 unsigned st_ctime_nsec_;
464 unsigned long long st_ino;
465};
466#endif
467
Bryan Chan3f6478a2016-06-14 08:38:17 -0400468/* include/asm-{arm,aarch64,i386,mips,x86_64,ppc,s390}/stat.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000469#if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__)
Peter Kasting99121a32022-08-22 16:43:44 +0000470typedef unsigned kernel_blkcnt_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000471typedef unsigned short kernel_dev_t;
472typedef unsigned short kernel_gid_t;
473typedef unsigned kernel_ino_t;
474typedef unsigned short kernel_nlink_t;
475typedef unsigned kernel_off_t;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000476typedef unsigned kernel_time_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000477typedef unsigned short kernel_uid_t;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000478struct kernel_stat {
479 /* The kernel headers suggest that st_dev and st_rdev should be 32bit
480 * quantities encoding 12bit major and 20bit minor numbers in an interleaved
481 * format. In reality, we do not see useful data in the top bits. So,
482 * we'll leave the padding in here, until we find a better solution.
483 */
Peter Kasting092a9212022-08-22 20:02:11 +0000484 kernel_dev_t st_dev;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000485 short pad1;
Peter Kasting092a9212022-08-22 20:02:11 +0000486 kernel_ino_t st_ino;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000487 unsigned short st_mode;
Peter Kasting092a9212022-08-22 20:02:11 +0000488 kernel_nlink_t st_nlink;
489 kernel_uid_t st_uid;
490 kernel_gid_t st_gid;
491 kernel_dev_t st_rdev;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000492 short pad2;
Peter Kasting99121a32022-08-22 16:43:44 +0000493 kernel_off_t st_size;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000494 unsigned st_blksize;
Peter Kasting99121a32022-08-22 16:43:44 +0000495 kernel_blkcnt_t st_blocks;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000496 kernel_time_t st_atime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000497 unsigned st_atime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000498 kernel_time_t st_mtime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000499 unsigned st_mtime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000500 kernel_time_t st_ctime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000501 unsigned st_ctime_nsec_;
502 unsigned __unused4;
503 unsigned __unused5;
504};
505#elif defined(__x86_64__)
Peter Kasting99121a32022-08-22 16:43:44 +0000506typedef int64_t kernel_blkcnt_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000507typedef uint64_t kernel_dev_t;
508typedef unsigned kernel_gid_t;
509typedef uint64_t kernel_ino_t;
510typedef uint64_t kernel_nlink_t;
511typedef int64_t kernel_off_t;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000512typedef uint64_t kernel_time_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000513typedef unsigned kernel_uid_t;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000514struct kernel_stat {
Peter Kasting092a9212022-08-22 20:02:11 +0000515 kernel_dev_t st_dev;
516 kernel_ino_t st_ino;
517 kernel_nlink_t st_nlink;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000518 unsigned st_mode;
Peter Kasting092a9212022-08-22 20:02:11 +0000519 kernel_uid_t st_uid;
520 kernel_gid_t st_gid;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000521 unsigned __pad0;
Peter Kasting092a9212022-08-22 20:02:11 +0000522 kernel_dev_t st_rdev;
Peter Kasting99121a32022-08-22 16:43:44 +0000523 kernel_off_t st_size;
vapier@chromium.org2273e812013-04-01 17:52:44 +0000524 int64_t st_blksize;
Peter Kasting99121a32022-08-22 16:43:44 +0000525 kernel_blkcnt_t st_blocks;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000526 kernel_time_t st_atime_;
vapier@chromium.org2273e812013-04-01 17:52:44 +0000527 uint64_t st_atime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000528 kernel_time_t st_mtime_;
vapier@chromium.org2273e812013-04-01 17:52:44 +0000529 uint64_t st_mtime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000530 kernel_time_t st_ctime_;
vapier@chromium.org2273e812013-04-01 17:52:44 +0000531 uint64_t st_ctime_nsec_;
anton@chromium.org43de0522014-04-04 11:20:46 +0000532 int64_t __unused4[3];
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000533};
534#elif defined(__PPC__)
Peter Kasting99121a32022-08-22 16:43:44 +0000535typedef unsigned long kernel_blkcnt_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000536typedef unsigned kernel_dev_t;
537typedef unsigned kernel_gid_t;
538typedef unsigned long kernel_ino_t;
539typedef unsigned short kernel_nlink_t;
540typedef long kernel_off_t;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000541typedef unsigned long kernel_time_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000542typedef unsigned kernel_uid_t;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000543struct kernel_stat {
Peter Kasting092a9212022-08-22 20:02:11 +0000544 kernel_dev_t st_dev;
545 kernel_ino_t st_ino;
546 unsigned long st_mode;
547 kernel_nlink_t st_nlink;
548 kernel_gid_t st_uid;
549 kernel_uid_t st_gid;
550 kernel_dev_t st_rdev;
551 kernel_off_t st_size;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000552 unsigned long st_blksize;
Peter Kasting99121a32022-08-22 16:43:44 +0000553 kernel_blkcnt_t st_blocks;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000554 kernel_time_t st_atime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000555 unsigned long st_atime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000556 kernel_time_t st_mtime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000557 unsigned long st_mtime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000558 kernel_time_t st_ctime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000559 unsigned long st_ctime_nsec_;
560 unsigned long __unused4;
561 unsigned long __unused5;
562};
563#elif (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64)
Peter Kasting99121a32022-08-22 16:43:44 +0000564typedef int kernel_blkcnt_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000565typedef unsigned kernel_dev_t;
566typedef unsigned kernel_gid_t;
567typedef unsigned kernel_ino_t;
568typedef unsigned kernel_nlink_t;
569typedef long kernel_off_t;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000570typedef long kernel_time_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000571typedef unsigned kernel_uid_t;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000572struct kernel_stat {
Peter Kasting092a9212022-08-22 20:02:11 +0000573 kernel_dev_t st_dev;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000574 int st_pad1[3];
Peter Kasting092a9212022-08-22 20:02:11 +0000575 kernel_ino_t st_ino;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000576 unsigned st_mode;
Peter Kasting092a9212022-08-22 20:02:11 +0000577 kernel_nlink_t st_nlink;
578 kernel_uid_t st_uid;
579 kernel_gid_t st_gid;
580 kernel_dev_t st_rdev;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000581 int st_pad2[2];
Peter Kasting99121a32022-08-22 16:43:44 +0000582 kernel_off_t st_size;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000583 int st_pad3;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000584 kernel_time_t st_atime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000585 long st_atime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000586 kernel_time_t st_mtime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000587 long st_mtime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000588 kernel_time_t st_ctime_;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000589 long st_ctime_nsec_;
590 int st_blksize;
Peter Kasting99121a32022-08-22 16:43:44 +0000591 kernel_blkcnt_t st_blocks;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000592 int st_pad4[14];
593};
mingtaoxt xtc0c96892022-08-11 16:53:21 +0800594#elif defined(__aarch64__) || defined(__riscv) || defined(__loongarch_lp64)
Peter Kasting99121a32022-08-22 16:43:44 +0000595typedef long kernel_blkcnt_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000596typedef unsigned long kernel_dev_t;
597typedef unsigned int kernel_gid_t;
598typedef unsigned long kernel_ino_t;
599typedef unsigned int kernel_nlink_t;
600typedef long kernel_off_t;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000601typedef long kernel_time_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000602typedef unsigned int kernel_uid_t;
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000603struct kernel_stat {
Peter Kasting092a9212022-08-22 20:02:11 +0000604 kernel_dev_t st_dev;
605 kernel_ino_t st_ino;
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000606 unsigned int st_mode;
Peter Kasting092a9212022-08-22 20:02:11 +0000607 kernel_nlink_t st_nlink;
608 kernel_uid_t st_uid;
609 kernel_gid_t st_gid;
610 kernel_dev_t st_rdev;
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000611 unsigned long __pad1;
Peter Kasting99121a32022-08-22 16:43:44 +0000612 kernel_off_t st_size;
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000613 int st_blksize;
614 int __pad2;
Peter Kasting99121a32022-08-22 16:43:44 +0000615 kernel_blkcnt_t st_blocks;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000616 kernel_time_t st_atime_;
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000617 unsigned long st_atime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000618 kernel_time_t st_mtime_;
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000619 unsigned long st_mtime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000620 kernel_time_t st_ctime_;
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000621 unsigned long st_ctime_nsec_;
622 unsigned int __unused4;
623 unsigned int __unused5;
624};
Bryan Chan3f6478a2016-06-14 08:38:17 -0400625#elif defined(__s390x__)
Peter Kasting99121a32022-08-22 16:43:44 +0000626typedef long kernel_blkcnt_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000627typedef unsigned long kernel_dev_t;
628typedef unsigned int kernel_gid_t;
629typedef unsigned long kernel_ino_t;
630typedef unsigned long kernel_nlink_t;
631typedef unsigned long kernel_off_t;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000632typedef unsigned long kernel_time_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000633typedef unsigned int kernel_uid_t;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400634struct kernel_stat {
Peter Kasting092a9212022-08-22 20:02:11 +0000635 kernel_dev_t st_dev;
636 kernel_ino_t st_ino;
637 kernel_nlink_t st_nlink;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400638 unsigned int st_mode;
Peter Kasting092a9212022-08-22 20:02:11 +0000639 kernel_uid_t st_uid;
640 kernel_gid_t st_gid;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400641 unsigned int __pad1;
Peter Kasting092a9212022-08-22 20:02:11 +0000642 kernel_dev_t st_rdev;
Peter Kasting99121a32022-08-22 16:43:44 +0000643 kernel_off_t st_size;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000644 kernel_time_t st_atime_;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400645 unsigned long st_atime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000646 kernel_time_t st_mtime_;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400647 unsigned long st_mtime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000648 kernel_time_t st_ctime_;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400649 unsigned long st_ctime_nsec_;
650 unsigned long st_blksize;
Peter Kasting99121a32022-08-22 16:43:44 +0000651 kernel_blkcnt_t st_blocks;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400652 unsigned long __unused[3];
653};
654#elif defined(__s390__)
Peter Kasting99121a32022-08-22 16:43:44 +0000655typedef unsigned long kernel_blkcnt_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000656typedef unsigned short kernel_dev_t;
657typedef unsigned short kernel_gid_t;
658typedef unsigned long kernel_ino_t;
659typedef unsigned short kernel_nlink_t;
660typedef unsigned long kernel_off_t;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000661typedef unsigned long kernel_time_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000662typedef unsigned short kernel_uid_t;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400663struct kernel_stat {
Peter Kasting092a9212022-08-22 20:02:11 +0000664 kernel_dev_t st_dev;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400665 unsigned short __pad1;
Peter Kasting092a9212022-08-22 20:02:11 +0000666 kernel_ino_t st_ino;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400667 unsigned short st_mode;
Peter Kasting092a9212022-08-22 20:02:11 +0000668 kernel_nlink_t st_nlink;
669 kernel_uid_t st_uid;
670 kernel_gid_t st_gid;
671 kernel_dev_t st_rdev;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400672 unsigned short __pad2;
Peter Kasting99121a32022-08-22 16:43:44 +0000673 kernel_off_t st_size;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400674 unsigned long st_blksize;
Peter Kasting99121a32022-08-22 16:43:44 +0000675 kernel_blkcnt_t st_blocks;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000676 kernel_time_t st_atime_;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400677 unsigned long st_atime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000678 kernel_time_t st_mtime_;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400679 unsigned long st_mtime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000680 kernel_time_t st_ctime_;
Bryan Chan3f6478a2016-06-14 08:38:17 -0400681 unsigned long st_ctime_nsec_;
682 unsigned long __unused4;
683 unsigned long __unused5;
684};
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300685#elif defined(__e2k__)
Peter Kasting99121a32022-08-22 16:43:44 +0000686typedef unsigned long kernel_blkcnt_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000687typedef unsigned long kernel_dev_t;
688typedef unsigned int kernel_gid_t;
689typedef unsigned long kernel_ino_t;
690typedef unsigned long kernel_nlink_t;
691typedef unsigned long kernel_off_t;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000692typedef unsigned long kernel_time_t;
Peter Kasting092a9212022-08-22 20:02:11 +0000693typedef unsigned int kernel_uid_t;
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300694struct kernel_stat {
Peter Kasting092a9212022-08-22 20:02:11 +0000695 kernel_dev_t st_dev;
696 kernel_ino_t st_ino;
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300697 unsigned int st_mode;
Peter Kasting092a9212022-08-22 20:02:11 +0000698 kernel_nlink_t st_nlink;
699 kernel_uid_t st_uid;
700 kernel_gid_t st_gid;
701 kernel_dev_t st_rdev;
Peter Kasting99121a32022-08-22 16:43:44 +0000702 kernel_off_t st_size;
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300703 unsigned long st_blksize;
Peter Kasting99121a32022-08-22 16:43:44 +0000704 kernel_blkcnt_t st_blocks;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000705 kernel_time_t st_atime_;
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300706 unsigned long st_atime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000707 kernel_time_t st_mtime_;
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300708 unsigned long st_mtime_nsec_;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +0000709 kernel_time_t st_ctime_;
Konstantin Ivlev8007b272021-01-27 18:27:42 +0300710 unsigned long st_ctime_nsec_;
711};
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000712#endif
713
Bryan Chan3f6478a2016-06-14 08:38:17 -0400714/* include/asm-{arm,aarch64,i386,mips,x86_64,ppc,s390}/statfs.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000715#ifdef __mips__
716#if _MIPS_SIM != _MIPS_SIM_ABI64
717struct kernel_statfs64 {
718 unsigned long f_type;
719 unsigned long f_bsize;
720 unsigned long f_frsize;
721 unsigned long __pad;
722 unsigned long long f_blocks;
723 unsigned long long f_bfree;
724 unsigned long long f_files;
725 unsigned long long f_ffree;
726 unsigned long long f_bavail;
727 struct { int val[2]; } f_fsid;
728 unsigned long f_namelen;
729 unsigned long f_spare[6];
730};
731#endif
Bryan Chan3f6478a2016-06-14 08:38:17 -0400732#elif defined(__s390__)
733/* See also arch/s390/include/asm/compat.h */
734struct kernel_statfs64 {
735 unsigned int f_type;
736 unsigned int f_bsize;
737 unsigned long long f_blocks;
738 unsigned long long f_bfree;
739 unsigned long long f_bavail;
740 unsigned long long f_files;
741 unsigned long long f_ffree;
742 struct { int val[2]; } f_fsid;
743 unsigned int f_namelen;
744 unsigned int f_frsize;
745 unsigned int f_flags;
746 unsigned int f_spare[4];
747};
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000748#elif !defined(__x86_64__)
749struct kernel_statfs64 {
750 unsigned long f_type;
751 unsigned long f_bsize;
752 unsigned long long f_blocks;
753 unsigned long long f_bfree;
754 unsigned long long f_bavail;
755 unsigned long long f_files;
756 unsigned long long f_ffree;
757 struct { int val[2]; } f_fsid;
758 unsigned long f_namelen;
759 unsigned long f_frsize;
760 unsigned long f_spare[5];
761};
762#endif
763
Bryan Chan3f6478a2016-06-14 08:38:17 -0400764/* include/asm-{arm,i386,mips,x86_64,ppc,generic,s390}/statfs.h */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000765#ifdef __mips__
766struct kernel_statfs {
767 long f_type;
768 long f_bsize;
769 long f_frsize;
770 long f_blocks;
771 long f_bfree;
772 long f_files;
773 long f_ffree;
774 long f_bavail;
775 struct { int val[2]; } f_fsid;
776 long f_namelen;
777 long f_spare[6];
778};
vapier@chromium.org2273e812013-04-01 17:52:44 +0000779#elif defined(__x86_64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000780struct kernel_statfs {
781 /* x86_64 actually defines all these fields as signed, whereas all other */
782 /* platforms define them as unsigned. Leaving them at unsigned should not */
vapier@chromium.org2273e812013-04-01 17:52:44 +0000783 /* cause any problems. Make sure these are 64-bit even on x32. */
784 uint64_t f_type;
785 uint64_t f_bsize;
786 uint64_t f_blocks;
787 uint64_t f_bfree;
788 uint64_t f_bavail;
789 uint64_t f_files;
790 uint64_t f_ffree;
791 struct { int val[2]; } f_fsid;
792 uint64_t f_namelen;
793 uint64_t f_frsize;
794 uint64_t f_spare[5];
795};
Bryan Chan3f6478a2016-06-14 08:38:17 -0400796#elif defined(__s390__)
797struct kernel_statfs {
798 unsigned int f_type;
799 unsigned int f_bsize;
800 unsigned long f_blocks;
801 unsigned long f_bfree;
802 unsigned long f_bavail;
803 unsigned long f_files;
804 unsigned long f_ffree;
805 struct { int val[2]; } f_fsid;
806 unsigned int f_namelen;
807 unsigned int f_frsize;
808 unsigned int f_flags;
809 unsigned int f_spare[4];
810};
vapier@chromium.org2273e812013-04-01 17:52:44 +0000811#else
812struct kernel_statfs {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000813 unsigned long f_type;
814 unsigned long f_bsize;
815 unsigned long f_blocks;
816 unsigned long f_bfree;
817 unsigned long f_bavail;
818 unsigned long f_files;
819 unsigned long f_ffree;
820 struct { int val[2]; } f_fsid;
821 unsigned long f_namelen;
822 unsigned long f_frsize;
823 unsigned long f_spare[5];
824};
825#endif
826
mingtaoxt xtc0c96892022-08-11 16:53:21 +0800827struct kernel_statx_timestamp {
828 int64_t tv_sec;
829 uint32_t tv_nsec;
830 int32_t __reserved;
831};
832
833struct kernel_statx {
834 uint32_t stx_mask;
835 uint32_t stx_blksize;
836 uint64_t stx_attributes;
837 uint32_t stx_nlink;
838 uint32_t stx_uid;
839 uint32_t stx_gid;
840 uint16_t stx_mode;
841 uint16_t __spare0[1];
842 uint64_t stx_ino;
843 uint64_t stx_size;
844 uint64_t stx_blocks;
845 uint64_t stx_attributes_mask;
846 struct kernel_statx_timestamp stx_atime;
847 struct kernel_statx_timestamp stx_btime;
848 struct kernel_statx_timestamp stx_ctime;
849 struct kernel_statx_timestamp stx_mtime;
850 uint32_t stx_rdev_major;
851 uint32_t stx_rdev_minor;
852 uint32_t stx_dev_major;
853 uint32_t stx_dev_minor;
854 uint64_t stx_mnt_id;
855 uint64_t __spare2;
856 uint64_t __spare3[12];
857};
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000858
859/* Definitions missing from the standard header files */
860#ifndef O_DIRECTORY
anton@chromium.org2f724fc2014-04-15 13:05:20 +0000861#if defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || defined(__aarch64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000862#define O_DIRECTORY 0040000
863#else
864#define O_DIRECTORY 0200000
865#endif
866#endif
867#ifndef NT_PRXFPREG
868#define NT_PRXFPREG 0x46e62b7f
869#endif
870#ifndef PTRACE_GETFPXREGS
871#define PTRACE_GETFPXREGS ((enum __ptrace_request)18)
872#endif
873#ifndef PR_GET_DUMPABLE
874#define PR_GET_DUMPABLE 3
875#endif
876#ifndef PR_SET_DUMPABLE
877#define PR_SET_DUMPABLE 4
878#endif
879#ifndef PR_GET_SECCOMP
880#define PR_GET_SECCOMP 21
881#endif
882#ifndef PR_SET_SECCOMP
883#define PR_SET_SECCOMP 22
884#endif
885#ifndef AT_FDCWD
886#define AT_FDCWD (-100)
887#endif
888#ifndef AT_SYMLINK_NOFOLLOW
889#define AT_SYMLINK_NOFOLLOW 0x100
890#endif
891#ifndef AT_REMOVEDIR
892#define AT_REMOVEDIR 0x200
893#endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +0800894#ifndef AT_NO_AUTOMOUNT
895#define AT_NO_AUTOMOUNT 0x800
896#endif
897#ifndef AT_EMPTY_PATH
898#define AT_EMPTY_PATH 0x1000
899#endif
900#ifndef STATX_BASIC_STATS
901#define STATX_BASIC_STATS 0x000007ffU
902#endif
903#ifndef AT_STATX_SYNC_AS_STAT
904#define AT_STATX_SYNC_AS_STAT 0x0000
905#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000906#ifndef MREMAP_FIXED
907#define MREMAP_FIXED 2
908#endif
909#ifndef SA_RESTORER
910#define SA_RESTORER 0x04000000
911#endif
912#ifndef CPUCLOCK_PROF
913#define CPUCLOCK_PROF 0
914#endif
915#ifndef CPUCLOCK_VIRT
916#define CPUCLOCK_VIRT 1
917#endif
918#ifndef CPUCLOCK_SCHED
919#define CPUCLOCK_SCHED 2
920#endif
921#ifndef CPUCLOCK_PERTHREAD_MASK
922#define CPUCLOCK_PERTHREAD_MASK 4
923#endif
924#ifndef MAKE_PROCESS_CPUCLOCK
925#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
Nico Webera2b70922017-03-30 11:03:37 -0400926 ((int)(~(unsigned)(pid) << 3) | (int)(clock))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000927#endif
928#ifndef MAKE_THREAD_CPUCLOCK
929#define MAKE_THREAD_CPUCLOCK(tid, clock) \
Nico Webera2b70922017-03-30 11:03:37 -0400930 ((int)(~(unsigned)(tid) << 3) | \
931 (int)((clock) | CPUCLOCK_PERTHREAD_MASK))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +0000932#endif
933
934#ifndef FUTEX_WAIT
935#define FUTEX_WAIT 0
936#endif
937#ifndef FUTEX_WAKE
938#define FUTEX_WAKE 1
939#endif
940#ifndef FUTEX_FD
941#define FUTEX_FD 2
942#endif
943#ifndef FUTEX_REQUEUE
944#define FUTEX_REQUEUE 3
945#endif
946#ifndef FUTEX_CMP_REQUEUE
947#define FUTEX_CMP_REQUEUE 4
948#endif
949#ifndef FUTEX_WAKE_OP
950#define FUTEX_WAKE_OP 5
951#endif
952#ifndef FUTEX_LOCK_PI
953#define FUTEX_LOCK_PI 6
954#endif
955#ifndef FUTEX_UNLOCK_PI
956#define FUTEX_UNLOCK_PI 7
957#endif
958#ifndef FUTEX_TRYLOCK_PI
959#define FUTEX_TRYLOCK_PI 8
960#endif
961#ifndef FUTEX_PRIVATE_FLAG
962#define FUTEX_PRIVATE_FLAG 128
963#endif
964#ifndef FUTEX_CMD_MASK
965#define FUTEX_CMD_MASK ~FUTEX_PRIVATE_FLAG
966#endif
967#ifndef FUTEX_WAIT_PRIVATE
968#define FUTEX_WAIT_PRIVATE (FUTEX_WAIT | FUTEX_PRIVATE_FLAG)
969#endif
970#ifndef FUTEX_WAKE_PRIVATE
971#define FUTEX_WAKE_PRIVATE (FUTEX_WAKE | FUTEX_PRIVATE_FLAG)
972#endif
973#ifndef FUTEX_REQUEUE_PRIVATE
974#define FUTEX_REQUEUE_PRIVATE (FUTEX_REQUEUE | FUTEX_PRIVATE_FLAG)
975#endif
976#ifndef FUTEX_CMP_REQUEUE_PRIVATE
977#define FUTEX_CMP_REQUEUE_PRIVATE (FUTEX_CMP_REQUEUE | FUTEX_PRIVATE_FLAG)
978#endif
979#ifndef FUTEX_WAKE_OP_PRIVATE
980#define FUTEX_WAKE_OP_PRIVATE (FUTEX_WAKE_OP | FUTEX_PRIVATE_FLAG)
981#endif
982#ifndef FUTEX_LOCK_PI_PRIVATE
983#define FUTEX_LOCK_PI_PRIVATE (FUTEX_LOCK_PI | FUTEX_PRIVATE_FLAG)
984#endif
985#ifndef FUTEX_UNLOCK_PI_PRIVATE
986#define FUTEX_UNLOCK_PI_PRIVATE (FUTEX_UNLOCK_PI | FUTEX_PRIVATE_FLAG)
987#endif
988#ifndef FUTEX_TRYLOCK_PI_PRIVATE
989#define FUTEX_TRYLOCK_PI_PRIVATE (FUTEX_TRYLOCK_PI | FUTEX_PRIVATE_FLAG)
990#endif
991
992
993#if defined(__x86_64__)
994#ifndef ARCH_SET_GS
995#define ARCH_SET_GS 0x1001
996#endif
997#ifndef ARCH_GET_GS
998#define ARCH_GET_GS 0x1004
999#endif
1000#endif
1001
1002#if defined(__i386__)
1003#ifndef __NR_quotactl
1004#define __NR_quotactl 131
1005#endif
1006#ifndef __NR_setresuid
1007#define __NR_setresuid 164
1008#define __NR_getresuid 165
1009#define __NR_setresgid 170
1010#define __NR_getresgid 171
1011#endif
1012#ifndef __NR_rt_sigaction
1013#define __NR_rt_sigreturn 173
1014#define __NR_rt_sigaction 174
1015#define __NR_rt_sigprocmask 175
1016#define __NR_rt_sigpending 176
1017#define __NR_rt_sigsuspend 179
1018#endif
1019#ifndef __NR_pread64
1020#define __NR_pread64 180
1021#endif
1022#ifndef __NR_pwrite64
1023#define __NR_pwrite64 181
1024#endif
1025#ifndef __NR_ugetrlimit
1026#define __NR_ugetrlimit 191
1027#endif
1028#ifndef __NR_stat64
1029#define __NR_stat64 195
1030#endif
1031#ifndef __NR_fstat64
1032#define __NR_fstat64 197
1033#endif
1034#ifndef __NR_setresuid32
1035#define __NR_setresuid32 208
1036#define __NR_getresuid32 209
1037#define __NR_setresgid32 210
1038#define __NR_getresgid32 211
1039#endif
1040#ifndef __NR_setfsuid32
1041#define __NR_setfsuid32 215
1042#define __NR_setfsgid32 216
1043#endif
1044#ifndef __NR_getdents64
1045#define __NR_getdents64 220
1046#endif
1047#ifndef __NR_gettid
1048#define __NR_gettid 224
1049#endif
1050#ifndef __NR_readahead
1051#define __NR_readahead 225
1052#endif
1053#ifndef __NR_setxattr
1054#define __NR_setxattr 226
1055#endif
1056#ifndef __NR_lsetxattr
1057#define __NR_lsetxattr 227
1058#endif
1059#ifndef __NR_getxattr
1060#define __NR_getxattr 229
1061#endif
1062#ifndef __NR_lgetxattr
1063#define __NR_lgetxattr 230
1064#endif
1065#ifndef __NR_listxattr
1066#define __NR_listxattr 232
1067#endif
1068#ifndef __NR_llistxattr
1069#define __NR_llistxattr 233
1070#endif
1071#ifndef __NR_tkill
1072#define __NR_tkill 238
1073#endif
1074#ifndef __NR_futex
1075#define __NR_futex 240
1076#endif
1077#ifndef __NR_sched_setaffinity
1078#define __NR_sched_setaffinity 241
1079#define __NR_sched_getaffinity 242
1080#endif
1081#ifndef __NR_set_tid_address
1082#define __NR_set_tid_address 258
1083#endif
1084#ifndef __NR_clock_gettime
1085#define __NR_clock_gettime 265
1086#endif
1087#ifndef __NR_clock_getres
1088#define __NR_clock_getres 266
1089#endif
1090#ifndef __NR_statfs64
1091#define __NR_statfs64 268
1092#endif
1093#ifndef __NR_fstatfs64
1094#define __NR_fstatfs64 269
1095#endif
1096#ifndef __NR_fadvise64_64
1097#define __NR_fadvise64_64 272
1098#endif
1099#ifndef __NR_ioprio_set
1100#define __NR_ioprio_set 289
1101#endif
1102#ifndef __NR_ioprio_get
1103#define __NR_ioprio_get 290
1104#endif
1105#ifndef __NR_openat
1106#define __NR_openat 295
1107#endif
1108#ifndef __NR_fstatat64
1109#define __NR_fstatat64 300
1110#endif
1111#ifndef __NR_unlinkat
1112#define __NR_unlinkat 301
1113#endif
1114#ifndef __NR_move_pages
1115#define __NR_move_pages 317
1116#endif
1117#ifndef __NR_getcpu
1118#define __NR_getcpu 318
1119#endif
1120#ifndef __NR_fallocate
1121#define __NR_fallocate 324
1122#endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001123#ifndef __NR_getrandom
1124#define __NR_getrandom 355
1125#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001126/* End of i386 definitions */
1127#elif defined(__ARM_ARCH_3__) || defined(__ARM_EABI__)
1128#ifndef __NR_setresuid
1129#define __NR_setresuid (__NR_SYSCALL_BASE + 164)
1130#define __NR_getresuid (__NR_SYSCALL_BASE + 165)
1131#define __NR_setresgid (__NR_SYSCALL_BASE + 170)
1132#define __NR_getresgid (__NR_SYSCALL_BASE + 171)
1133#endif
1134#ifndef __NR_rt_sigaction
1135#define __NR_rt_sigreturn (__NR_SYSCALL_BASE + 173)
1136#define __NR_rt_sigaction (__NR_SYSCALL_BASE + 174)
1137#define __NR_rt_sigprocmask (__NR_SYSCALL_BASE + 175)
1138#define __NR_rt_sigpending (__NR_SYSCALL_BASE + 176)
1139#define __NR_rt_sigsuspend (__NR_SYSCALL_BASE + 179)
1140#endif
1141#ifndef __NR_pread64
1142#define __NR_pread64 (__NR_SYSCALL_BASE + 180)
1143#endif
1144#ifndef __NR_pwrite64
1145#define __NR_pwrite64 (__NR_SYSCALL_BASE + 181)
1146#endif
1147#ifndef __NR_ugetrlimit
1148#define __NR_ugetrlimit (__NR_SYSCALL_BASE + 191)
1149#endif
1150#ifndef __NR_stat64
1151#define __NR_stat64 (__NR_SYSCALL_BASE + 195)
1152#endif
1153#ifndef __NR_fstat64
1154#define __NR_fstat64 (__NR_SYSCALL_BASE + 197)
1155#endif
1156#ifndef __NR_setresuid32
1157#define __NR_setresuid32 (__NR_SYSCALL_BASE + 208)
1158#define __NR_getresuid32 (__NR_SYSCALL_BASE + 209)
1159#define __NR_setresgid32 (__NR_SYSCALL_BASE + 210)
1160#define __NR_getresgid32 (__NR_SYSCALL_BASE + 211)
1161#endif
1162#ifndef __NR_setfsuid32
1163#define __NR_setfsuid32 (__NR_SYSCALL_BASE + 215)
1164#define __NR_setfsgid32 (__NR_SYSCALL_BASE + 216)
1165#endif
1166#ifndef __NR_getdents64
1167#define __NR_getdents64 (__NR_SYSCALL_BASE + 217)
1168#endif
1169#ifndef __NR_gettid
1170#define __NR_gettid (__NR_SYSCALL_BASE + 224)
1171#endif
1172#ifndef __NR_readahead
1173#define __NR_readahead (__NR_SYSCALL_BASE + 225)
1174#endif
1175#ifndef __NR_setxattr
1176#define __NR_setxattr (__NR_SYSCALL_BASE + 226)
1177#endif
1178#ifndef __NR_lsetxattr
1179#define __NR_lsetxattr (__NR_SYSCALL_BASE + 227)
1180#endif
1181#ifndef __NR_getxattr
1182#define __NR_getxattr (__NR_SYSCALL_BASE + 229)
1183#endif
1184#ifndef __NR_lgetxattr
1185#define __NR_lgetxattr (__NR_SYSCALL_BASE + 230)
1186#endif
1187#ifndef __NR_listxattr
1188#define __NR_listxattr (__NR_SYSCALL_BASE + 232)
1189#endif
1190#ifndef __NR_llistxattr
1191#define __NR_llistxattr (__NR_SYSCALL_BASE + 233)
1192#endif
1193#ifndef __NR_tkill
1194#define __NR_tkill (__NR_SYSCALL_BASE + 238)
1195#endif
1196#ifndef __NR_futex
1197#define __NR_futex (__NR_SYSCALL_BASE + 240)
1198#endif
1199#ifndef __NR_sched_setaffinity
1200#define __NR_sched_setaffinity (__NR_SYSCALL_BASE + 241)
1201#define __NR_sched_getaffinity (__NR_SYSCALL_BASE + 242)
1202#endif
1203#ifndef __NR_set_tid_address
1204#define __NR_set_tid_address (__NR_SYSCALL_BASE + 256)
1205#endif
1206#ifndef __NR_clock_gettime
1207#define __NR_clock_gettime (__NR_SYSCALL_BASE + 263)
1208#endif
1209#ifndef __NR_clock_getres
1210#define __NR_clock_getres (__NR_SYSCALL_BASE + 264)
1211#endif
1212#ifndef __NR_statfs64
1213#define __NR_statfs64 (__NR_SYSCALL_BASE + 266)
1214#endif
1215#ifndef __NR_fstatfs64
1216#define __NR_fstatfs64 (__NR_SYSCALL_BASE + 267)
1217#endif
1218#ifndef __NR_ioprio_set
1219#define __NR_ioprio_set (__NR_SYSCALL_BASE + 314)
1220#endif
1221#ifndef __NR_ioprio_get
1222#define __NR_ioprio_get (__NR_SYSCALL_BASE + 315)
1223#endif
Matthew Denton92a65a82021-04-01 13:00:07 -07001224#ifndef __NR_fstatat64
1225#define __NR_fstatat64 (__NR_SYSCALL_BASE + 327)
1226#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001227#ifndef __NR_move_pages
1228#define __NR_move_pages (__NR_SYSCALL_BASE + 344)
1229#endif
1230#ifndef __NR_getcpu
1231#define __NR_getcpu (__NR_SYSCALL_BASE + 345)
1232#endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001233#ifndef __NR_getrandom
1234#define __NR_getrandom (__NR_SYSCALL_BASE + 384)
1235#endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04001236/* End of ARM 3/EABI definitions */
mingtaoxt xtc0c96892022-08-11 16:53:21 +08001237#elif defined(__aarch64__) || defined(__riscv) || defined(__loongarch_lp64)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00001238#ifndef __NR_setxattr
1239#define __NR_setxattr 5
1240#endif
1241#ifndef __NR_lsetxattr
1242#define __NR_lsetxattr 6
1243#endif
1244#ifndef __NR_getxattr
1245#define __NR_getxattr 8
1246#endif
1247#ifndef __NR_lgetxattr
1248#define __NR_lgetxattr 9
1249#endif
1250#ifndef __NR_listxattr
1251#define __NR_listxattr 11
1252#endif
1253#ifndef __NR_llistxattr
1254#define __NR_llistxattr 12
1255#endif
1256#ifndef __NR_ioprio_set
1257#define __NR_ioprio_set 30
1258#endif
1259#ifndef __NR_ioprio_get
1260#define __NR_ioprio_get 31
1261#endif
1262#ifndef __NR_unlinkat
1263#define __NR_unlinkat 35
1264#endif
1265#ifndef __NR_fallocate
1266#define __NR_fallocate 47
1267#endif
1268#ifndef __NR_openat
1269#define __NR_openat 56
1270#endif
1271#ifndef __NR_quotactl
1272#define __NR_quotactl 60
1273#endif
1274#ifndef __NR_getdents64
1275#define __NR_getdents64 61
1276#endif
1277#ifndef __NR_getdents
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04001278// when getdents is not available, getdents64 is used for both.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00001279#define __NR_getdents __NR_getdents64
1280#endif
1281#ifndef __NR_pread64
1282#define __NR_pread64 67
1283#endif
1284#ifndef __NR_pwrite64
1285#define __NR_pwrite64 68
1286#endif
1287#ifndef __NR_ppoll
1288#define __NR_ppoll 73
1289#endif
1290#ifndef __NR_readlinkat
1291#define __NR_readlinkat 78
1292#endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08001293#if !defined(__loongarch_lp64)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00001294#ifndef __NR_newfstatat
1295#define __NR_newfstatat 79
1296#endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08001297#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00001298#ifndef __NR_set_tid_address
1299#define __NR_set_tid_address 96
1300#endif
1301#ifndef __NR_futex
1302#define __NR_futex 98
1303#endif
1304#ifndef __NR_clock_gettime
1305#define __NR_clock_gettime 113
1306#endif
1307#ifndef __NR_clock_getres
1308#define __NR_clock_getres 114
1309#endif
1310#ifndef __NR_sched_setaffinity
1311#define __NR_sched_setaffinity 122
1312#define __NR_sched_getaffinity 123
1313#endif
1314#ifndef __NR_tkill
1315#define __NR_tkill 130
1316#endif
1317#ifndef __NR_setresuid
1318#define __NR_setresuid 147
1319#define __NR_getresuid 148
1320#define __NR_setresgid 149
1321#define __NR_getresgid 150
1322#endif
1323#ifndef __NR_gettid
1324#define __NR_gettid 178
1325#endif
1326#ifndef __NR_readahead
1327#define __NR_readahead 213
1328#endif
1329#ifndef __NR_fadvise64
1330#define __NR_fadvise64 223
1331#endif
1332#ifndef __NR_move_pages
1333#define __NR_move_pages 239
1334#endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001335#ifndef __NR_getrandom
1336#define __NR_getrandom 278
1337#endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08001338#ifndef __NR_statx
1339#define __NR_statx 291
1340#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001341#elif defined(__x86_64__)
1342#ifndef __NR_pread64
1343#define __NR_pread64 17
1344#endif
1345#ifndef __NR_pwrite64
1346#define __NR_pwrite64 18
1347#endif
1348#ifndef __NR_setresuid
1349#define __NR_setresuid 117
1350#define __NR_getresuid 118
1351#define __NR_setresgid 119
1352#define __NR_getresgid 120
1353#endif
1354#ifndef __NR_quotactl
1355#define __NR_quotactl 179
1356#endif
1357#ifndef __NR_gettid
1358#define __NR_gettid 186
1359#endif
1360#ifndef __NR_readahead
1361#define __NR_readahead 187
1362#endif
1363#ifndef __NR_setxattr
1364#define __NR_setxattr 188
1365#endif
1366#ifndef __NR_lsetxattr
1367#define __NR_lsetxattr 189
1368#endif
1369#ifndef __NR_getxattr
1370#define __NR_getxattr 191
1371#endif
1372#ifndef __NR_lgetxattr
1373#define __NR_lgetxattr 192
1374#endif
1375#ifndef __NR_listxattr
1376#define __NR_listxattr 194
1377#endif
1378#ifndef __NR_llistxattr
1379#define __NR_llistxattr 195
1380#endif
1381#ifndef __NR_tkill
1382#define __NR_tkill 200
1383#endif
1384#ifndef __NR_futex
1385#define __NR_futex 202
1386#endif
1387#ifndef __NR_sched_setaffinity
1388#define __NR_sched_setaffinity 203
1389#define __NR_sched_getaffinity 204
1390#endif
1391#ifndef __NR_getdents64
1392#define __NR_getdents64 217
1393#endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04001394#ifndef __NR_getdents
1395// when getdents is not available, getdents64 is used for both.
1396#define __NR_getdents __NR_getdents64
1397#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001398#ifndef __NR_set_tid_address
1399#define __NR_set_tid_address 218
1400#endif
1401#ifndef __NR_fadvise64
1402#define __NR_fadvise64 221
1403#endif
1404#ifndef __NR_clock_gettime
1405#define __NR_clock_gettime 228
1406#endif
1407#ifndef __NR_clock_getres
1408#define __NR_clock_getres 229
1409#endif
1410#ifndef __NR_ioprio_set
1411#define __NR_ioprio_set 251
1412#endif
1413#ifndef __NR_ioprio_get
1414#define __NR_ioprio_get 252
1415#endif
1416#ifndef __NR_openat
1417#define __NR_openat 257
1418#endif
1419#ifndef __NR_newfstatat
1420#define __NR_newfstatat 262
1421#endif
1422#ifndef __NR_unlinkat
1423#define __NR_unlinkat 263
1424#endif
1425#ifndef __NR_move_pages
1426#define __NR_move_pages 279
1427#endif
1428#ifndef __NR_fallocate
1429#define __NR_fallocate 285
1430#endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001431#ifndef __NR_getrandom
1432#define __NR_getrandom 318
1433#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001434/* End of x86-64 definitions */
1435#elif defined(__mips__)
1436#if _MIPS_SIM == _MIPS_SIM_ABI32
1437#ifndef __NR_setresuid
1438#define __NR_setresuid (__NR_Linux + 185)
1439#define __NR_getresuid (__NR_Linux + 186)
1440#define __NR_setresgid (__NR_Linux + 190)
1441#define __NR_getresgid (__NR_Linux + 191)
1442#endif
1443#ifndef __NR_rt_sigaction
1444#define __NR_rt_sigreturn (__NR_Linux + 193)
1445#define __NR_rt_sigaction (__NR_Linux + 194)
1446#define __NR_rt_sigprocmask (__NR_Linux + 195)
1447#define __NR_rt_sigpending (__NR_Linux + 196)
1448#define __NR_rt_sigsuspend (__NR_Linux + 199)
1449#endif
1450#ifndef __NR_pread64
1451#define __NR_pread64 (__NR_Linux + 200)
1452#endif
1453#ifndef __NR_pwrite64
1454#define __NR_pwrite64 (__NR_Linux + 201)
1455#endif
1456#ifndef __NR_stat64
1457#define __NR_stat64 (__NR_Linux + 213)
1458#endif
1459#ifndef __NR_fstat64
1460#define __NR_fstat64 (__NR_Linux + 215)
1461#endif
1462#ifndef __NR_getdents64
1463#define __NR_getdents64 (__NR_Linux + 219)
1464#endif
1465#ifndef __NR_gettid
1466#define __NR_gettid (__NR_Linux + 222)
1467#endif
1468#ifndef __NR_readahead
1469#define __NR_readahead (__NR_Linux + 223)
1470#endif
1471#ifndef __NR_setxattr
1472#define __NR_setxattr (__NR_Linux + 224)
1473#endif
1474#ifndef __NR_lsetxattr
1475#define __NR_lsetxattr (__NR_Linux + 225)
1476#endif
1477#ifndef __NR_getxattr
1478#define __NR_getxattr (__NR_Linux + 227)
1479#endif
1480#ifndef __NR_lgetxattr
1481#define __NR_lgetxattr (__NR_Linux + 228)
1482#endif
1483#ifndef __NR_listxattr
1484#define __NR_listxattr (__NR_Linux + 230)
1485#endif
1486#ifndef __NR_llistxattr
1487#define __NR_llistxattr (__NR_Linux + 231)
1488#endif
1489#ifndef __NR_tkill
1490#define __NR_tkill (__NR_Linux + 236)
1491#endif
1492#ifndef __NR_futex
1493#define __NR_futex (__NR_Linux + 238)
1494#endif
1495#ifndef __NR_sched_setaffinity
1496#define __NR_sched_setaffinity (__NR_Linux + 239)
1497#define __NR_sched_getaffinity (__NR_Linux + 240)
1498#endif
1499#ifndef __NR_set_tid_address
1500#define __NR_set_tid_address (__NR_Linux + 252)
1501#endif
1502#ifndef __NR_statfs64
1503#define __NR_statfs64 (__NR_Linux + 255)
1504#endif
1505#ifndef __NR_fstatfs64
1506#define __NR_fstatfs64 (__NR_Linux + 256)
1507#endif
1508#ifndef __NR_clock_gettime
1509#define __NR_clock_gettime (__NR_Linux + 263)
1510#endif
1511#ifndef __NR_clock_getres
1512#define __NR_clock_getres (__NR_Linux + 264)
1513#endif
1514#ifndef __NR_openat
1515#define __NR_openat (__NR_Linux + 288)
1516#endif
1517#ifndef __NR_fstatat
1518#define __NR_fstatat (__NR_Linux + 293)
1519#endif
1520#ifndef __NR_unlinkat
1521#define __NR_unlinkat (__NR_Linux + 294)
1522#endif
1523#ifndef __NR_move_pages
1524#define __NR_move_pages (__NR_Linux + 308)
1525#endif
1526#ifndef __NR_getcpu
1527#define __NR_getcpu (__NR_Linux + 312)
1528#endif
1529#ifndef __NR_ioprio_set
1530#define __NR_ioprio_set (__NR_Linux + 314)
1531#endif
1532#ifndef __NR_ioprio_get
1533#define __NR_ioprio_get (__NR_Linux + 315)
1534#endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001535#ifndef __NR_getrandom
1536#define __NR_getrandom (__NR_Linux + 353)
1537#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001538/* End of MIPS (old 32bit API) definitions */
1539#elif _MIPS_SIM == _MIPS_SIM_ABI64
1540#ifndef __NR_pread64
1541#define __NR_pread64 (__NR_Linux + 16)
1542#endif
1543#ifndef __NR_pwrite64
1544#define __NR_pwrite64 (__NR_Linux + 17)
1545#endif
1546#ifndef __NR_setresuid
1547#define __NR_setresuid (__NR_Linux + 115)
1548#define __NR_getresuid (__NR_Linux + 116)
1549#define __NR_setresgid (__NR_Linux + 117)
1550#define __NR_getresgid (__NR_Linux + 118)
1551#endif
1552#ifndef __NR_gettid
1553#define __NR_gettid (__NR_Linux + 178)
1554#endif
1555#ifndef __NR_readahead
1556#define __NR_readahead (__NR_Linux + 179)
1557#endif
1558#ifndef __NR_setxattr
1559#define __NR_setxattr (__NR_Linux + 180)
1560#endif
1561#ifndef __NR_lsetxattr
1562#define __NR_lsetxattr (__NR_Linux + 181)
1563#endif
1564#ifndef __NR_getxattr
1565#define __NR_getxattr (__NR_Linux + 183)
1566#endif
1567#ifndef __NR_lgetxattr
1568#define __NR_lgetxattr (__NR_Linux + 184)
1569#endif
1570#ifndef __NR_listxattr
1571#define __NR_listxattr (__NR_Linux + 186)
1572#endif
1573#ifndef __NR_llistxattr
1574#define __NR_llistxattr (__NR_Linux + 187)
1575#endif
1576#ifndef __NR_tkill
1577#define __NR_tkill (__NR_Linux + 192)
1578#endif
1579#ifndef __NR_futex
1580#define __NR_futex (__NR_Linux + 194)
1581#endif
1582#ifndef __NR_sched_setaffinity
1583#define __NR_sched_setaffinity (__NR_Linux + 195)
1584#define __NR_sched_getaffinity (__NR_Linux + 196)
1585#endif
1586#ifndef __NR_set_tid_address
1587#define __NR_set_tid_address (__NR_Linux + 212)
1588#endif
1589#ifndef __NR_clock_gettime
1590#define __NR_clock_gettime (__NR_Linux + 222)
1591#endif
1592#ifndef __NR_clock_getres
1593#define __NR_clock_getres (__NR_Linux + 223)
1594#endif
1595#ifndef __NR_openat
1596#define __NR_openat (__NR_Linux + 247)
1597#endif
1598#ifndef __NR_fstatat
1599#define __NR_fstatat (__NR_Linux + 252)
1600#endif
1601#ifndef __NR_unlinkat
1602#define __NR_unlinkat (__NR_Linux + 253)
1603#endif
1604#ifndef __NR_move_pages
1605#define __NR_move_pages (__NR_Linux + 267)
1606#endif
1607#ifndef __NR_getcpu
1608#define __NR_getcpu (__NR_Linux + 271)
1609#endif
1610#ifndef __NR_ioprio_set
1611#define __NR_ioprio_set (__NR_Linux + 273)
1612#endif
1613#ifndef __NR_ioprio_get
1614#define __NR_ioprio_get (__NR_Linux + 274)
1615#endif
Yu Yind9ad2962020-11-24 16:49:22 +08001616#ifndef __NR_getrandom
1617#define __NR_getrandom (__NR_Linux + 313)
Chris Palmer29f7c7e2020-08-12 17:10:59 -07001618#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001619/* End of MIPS (64bit API) definitions */
1620#else
1621#ifndef __NR_setresuid
1622#define __NR_setresuid (__NR_Linux + 115)
1623#define __NR_getresuid (__NR_Linux + 116)
1624#define __NR_setresgid (__NR_Linux + 117)
1625#define __NR_getresgid (__NR_Linux + 118)
1626#endif
1627#ifndef __NR_gettid
1628#define __NR_gettid (__NR_Linux + 178)
1629#endif
1630#ifndef __NR_readahead
1631#define __NR_readahead (__NR_Linux + 179)
1632#endif
1633#ifndef __NR_setxattr
1634#define __NR_setxattr (__NR_Linux + 180)
1635#endif
1636#ifndef __NR_lsetxattr
1637#define __NR_lsetxattr (__NR_Linux + 181)
1638#endif
1639#ifndef __NR_getxattr
1640#define __NR_getxattr (__NR_Linux + 183)
1641#endif
1642#ifndef __NR_lgetxattr
1643#define __NR_lgetxattr (__NR_Linux + 184)
1644#endif
1645#ifndef __NR_listxattr
1646#define __NR_listxattr (__NR_Linux + 186)
1647#endif
1648#ifndef __NR_llistxattr
1649#define __NR_llistxattr (__NR_Linux + 187)
1650#endif
1651#ifndef __NR_tkill
1652#define __NR_tkill (__NR_Linux + 192)
1653#endif
1654#ifndef __NR_futex
1655#define __NR_futex (__NR_Linux + 194)
1656#endif
1657#ifndef __NR_sched_setaffinity
1658#define __NR_sched_setaffinity (__NR_Linux + 195)
1659#define __NR_sched_getaffinity (__NR_Linux + 196)
1660#endif
1661#ifndef __NR_set_tid_address
1662#define __NR_set_tid_address (__NR_Linux + 213)
1663#endif
1664#ifndef __NR_statfs64
1665#define __NR_statfs64 (__NR_Linux + 217)
1666#endif
1667#ifndef __NR_fstatfs64
1668#define __NR_fstatfs64 (__NR_Linux + 218)
1669#endif
1670#ifndef __NR_clock_gettime
1671#define __NR_clock_gettime (__NR_Linux + 226)
1672#endif
1673#ifndef __NR_clock_getres
1674#define __NR_clock_getres (__NR_Linux + 227)
1675#endif
1676#ifndef __NR_openat
1677#define __NR_openat (__NR_Linux + 251)
1678#endif
1679#ifndef __NR_fstatat
1680#define __NR_fstatat (__NR_Linux + 256)
1681#endif
1682#ifndef __NR_unlinkat
1683#define __NR_unlinkat (__NR_Linux + 257)
1684#endif
1685#ifndef __NR_move_pages
1686#define __NR_move_pages (__NR_Linux + 271)
1687#endif
1688#ifndef __NR_getcpu
1689#define __NR_getcpu (__NR_Linux + 275)
1690#endif
1691#ifndef __NR_ioprio_set
1692#define __NR_ioprio_set (__NR_Linux + 277)
1693#endif
1694#ifndef __NR_ioprio_get
1695#define __NR_ioprio_get (__NR_Linux + 278)
1696#endif
1697/* End of MIPS (new 32bit API) definitions */
1698#endif
1699/* End of MIPS definitions */
1700#elif defined(__PPC__)
1701#ifndef __NR_setfsuid
1702#define __NR_setfsuid 138
1703#define __NR_setfsgid 139
1704#endif
1705#ifndef __NR_setresuid
1706#define __NR_setresuid 164
1707#define __NR_getresuid 165
1708#define __NR_setresgid 169
1709#define __NR_getresgid 170
1710#endif
1711#ifndef __NR_rt_sigaction
1712#define __NR_rt_sigreturn 172
1713#define __NR_rt_sigaction 173
1714#define __NR_rt_sigprocmask 174
1715#define __NR_rt_sigpending 175
1716#define __NR_rt_sigsuspend 178
1717#endif
1718#ifndef __NR_pread64
1719#define __NR_pread64 179
1720#endif
1721#ifndef __NR_pwrite64
1722#define __NR_pwrite64 180
1723#endif
1724#ifndef __NR_ugetrlimit
1725#define __NR_ugetrlimit 190
1726#endif
1727#ifndef __NR_readahead
1728#define __NR_readahead 191
1729#endif
1730#ifndef __NR_stat64
1731#define __NR_stat64 195
1732#endif
1733#ifndef __NR_fstat64
1734#define __NR_fstat64 197
1735#endif
1736#ifndef __NR_getdents64
1737#define __NR_getdents64 202
1738#endif
1739#ifndef __NR_gettid
1740#define __NR_gettid 207
1741#endif
1742#ifndef __NR_tkill
1743#define __NR_tkill 208
1744#endif
1745#ifndef __NR_setxattr
1746#define __NR_setxattr 209
1747#endif
1748#ifndef __NR_lsetxattr
1749#define __NR_lsetxattr 210
1750#endif
1751#ifndef __NR_getxattr
1752#define __NR_getxattr 212
1753#endif
1754#ifndef __NR_lgetxattr
1755#define __NR_lgetxattr 213
1756#endif
1757#ifndef __NR_listxattr
1758#define __NR_listxattr 215
1759#endif
1760#ifndef __NR_llistxattr
1761#define __NR_llistxattr 216
1762#endif
1763#ifndef __NR_futex
1764#define __NR_futex 221
1765#endif
1766#ifndef __NR_sched_setaffinity
1767#define __NR_sched_setaffinity 222
1768#define __NR_sched_getaffinity 223
1769#endif
1770#ifndef __NR_set_tid_address
1771#define __NR_set_tid_address 232
1772#endif
1773#ifndef __NR_clock_gettime
1774#define __NR_clock_gettime 246
1775#endif
1776#ifndef __NR_clock_getres
1777#define __NR_clock_getres 247
1778#endif
1779#ifndef __NR_statfs64
1780#define __NR_statfs64 252
1781#endif
1782#ifndef __NR_fstatfs64
1783#define __NR_fstatfs64 253
1784#endif
1785#ifndef __NR_fadvise64_64
1786#define __NR_fadvise64_64 254
1787#endif
1788#ifndef __NR_ioprio_set
1789#define __NR_ioprio_set 273
1790#endif
1791#ifndef __NR_ioprio_get
1792#define __NR_ioprio_get 274
1793#endif
1794#ifndef __NR_openat
1795#define __NR_openat 286
1796#endif
1797#ifndef __NR_fstatat64
1798#define __NR_fstatat64 291
1799#endif
1800#ifndef __NR_unlinkat
1801#define __NR_unlinkat 292
1802#endif
1803#ifndef __NR_move_pages
1804#define __NR_move_pages 301
1805#endif
1806#ifndef __NR_getcpu
1807#define __NR_getcpu 302
1808#endif
1809/* End of powerpc defininitions */
Bryan Chan3f6478a2016-06-14 08:38:17 -04001810#elif defined(__s390__)
1811#ifndef __NR_quotactl
1812#define __NR_quotactl 131
1813#endif
1814#ifndef __NR_rt_sigreturn
1815#define __NR_rt_sigreturn 173
1816#endif
1817#ifndef __NR_rt_sigaction
1818#define __NR_rt_sigaction 174
1819#endif
1820#ifndef __NR_rt_sigprocmask
1821#define __NR_rt_sigprocmask 175
1822#endif
1823#ifndef __NR_rt_sigpending
1824#define __NR_rt_sigpending 176
1825#endif
1826#ifndef __NR_rt_sigsuspend
1827#define __NR_rt_sigsuspend 179
1828#endif
1829#ifndef __NR_pread64
1830#define __NR_pread64 180
1831#endif
1832#ifndef __NR_pwrite64
1833#define __NR_pwrite64 181
1834#endif
1835#ifndef __NR_getdents64
1836#define __NR_getdents64 220
1837#endif
1838#ifndef __NR_readahead
1839#define __NR_readahead 222
1840#endif
1841#ifndef __NR_setxattr
1842#define __NR_setxattr 224
1843#endif
1844#ifndef __NR_lsetxattr
1845#define __NR_lsetxattr 225
1846#endif
1847#ifndef __NR_getxattr
1848#define __NR_getxattr 227
1849#endif
1850#ifndef __NR_lgetxattr
1851#define __NR_lgetxattr 228
1852#endif
1853#ifndef __NR_listxattr
1854#define __NR_listxattr 230
1855#endif
1856#ifndef __NR_llistxattr
1857#define __NR_llistxattr 231
1858#endif
1859#ifndef __NR_gettid
1860#define __NR_gettid 236
1861#endif
1862#ifndef __NR_tkill
1863#define __NR_tkill 237
1864#endif
1865#ifndef __NR_futex
1866#define __NR_futex 238
1867#endif
1868#ifndef __NR_sched_setaffinity
1869#define __NR_sched_setaffinity 239
1870#endif
1871#ifndef __NR_sched_getaffinity
1872#define __NR_sched_getaffinity 240
1873#endif
1874#ifndef __NR_set_tid_address
1875#define __NR_set_tid_address 252
1876#endif
1877#ifndef __NR_clock_gettime
1878#define __NR_clock_gettime 260
1879#endif
1880#ifndef __NR_clock_getres
1881#define __NR_clock_getres 261
1882#endif
1883#ifndef __NR_statfs64
1884#define __NR_statfs64 265
1885#endif
1886#ifndef __NR_fstatfs64
1887#define __NR_fstatfs64 266
1888#endif
1889#ifndef __NR_ioprio_set
1890#define __NR_ioprio_set 282
1891#endif
1892#ifndef __NR_ioprio_get
1893#define __NR_ioprio_get 283
1894#endif
1895#ifndef __NR_openat
1896#define __NR_openat 288
1897#endif
1898#ifndef __NR_unlinkat
1899#define __NR_unlinkat 294
1900#endif
1901#ifndef __NR_move_pages
1902#define __NR_move_pages 310
1903#endif
1904#ifndef __NR_getcpu
1905#define __NR_getcpu 311
1906#endif
1907#ifndef __NR_fallocate
1908#define __NR_fallocate 314
1909#endif
1910/* Some syscalls are named/numbered differently between s390 and s390x. */
1911#ifdef __s390x__
1912# ifndef __NR_getrlimit
1913# define __NR_getrlimit 191
1914# endif
1915# ifndef __NR_setresuid
1916# define __NR_setresuid 208
1917# endif
1918# ifndef __NR_getresuid
1919# define __NR_getresuid 209
1920# endif
1921# ifndef __NR_setresgid
1922# define __NR_setresgid 210
1923# endif
1924# ifndef __NR_getresgid
1925# define __NR_getresgid 211
1926# endif
1927# ifndef __NR_setfsuid
1928# define __NR_setfsuid 215
1929# endif
1930# ifndef __NR_setfsgid
1931# define __NR_setfsgid 216
1932# endif
1933# ifndef __NR_fadvise64
1934# define __NR_fadvise64 253
1935# endif
1936# ifndef __NR_newfstatat
1937# define __NR_newfstatat 293
1938# endif
1939#else /* __s390x__ */
1940# ifndef __NR_getrlimit
1941# define __NR_getrlimit 76
1942# endif
1943# ifndef __NR_setfsuid
1944# define __NR_setfsuid 138
1945# endif
1946# ifndef __NR_setfsgid
1947# define __NR_setfsgid 139
1948# endif
1949# ifndef __NR_setresuid
1950# define __NR_setresuid 164
1951# endif
1952# ifndef __NR_getresuid
1953# define __NR_getresuid 165
1954# endif
1955# ifndef __NR_setresgid
1956# define __NR_setresgid 170
1957# endif
1958# ifndef __NR_getresgid
1959# define __NR_getresgid 171
1960# endif
1961# ifndef __NR_ugetrlimit
1962# define __NR_ugetrlimit 191
1963# endif
1964# ifndef __NR_mmap2
1965# define __NR_mmap2 192
1966# endif
1967# ifndef __NR_setresuid32
1968# define __NR_setresuid32 208
1969# endif
1970# ifndef __NR_getresuid32
1971# define __NR_getresuid32 209
1972# endif
1973# ifndef __NR_setresgid32
1974# define __NR_setresgid32 210
1975# endif
1976# ifndef __NR_getresgid32
1977# define __NR_getresgid32 211
1978# endif
1979# ifndef __NR_setfsuid32
1980# define __NR_setfsuid32 215
1981# endif
1982# ifndef __NR_setfsgid32
1983# define __NR_setfsgid32 216
1984# endif
1985# ifndef __NR_fadvise64_64
1986# define __NR_fadvise64_64 264
1987# endif
1988# ifndef __NR_fstatat64
1989# define __NR_fstatat64 293
1990# endif
1991#endif /* __s390__ */
1992/* End of s390/s390x definitions */
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001993#endif
1994
1995
1996/* After forking, we must make sure to only call system calls. */
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00001997#if defined(__BOUNDED_POINTERS__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00001998 #error "Need to port invocations of syscalls for bounded ptrs"
1999#else
2000 /* The core dumper and the thread lister get executed after threads
2001 * have been suspended. As a consequence, we cannot call any functions
2002 * that acquire locks. Unfortunately, libc wraps most system calls
2003 * (e.g. in order to implement pthread_atfork, and to make calls
2004 * cancellable), which means we cannot call these functions. Instead,
2005 * we have to call syscall() directly.
2006 */
2007 #undef LSS_ERRNO
2008 #ifdef SYS_ERRNO
2009 /* Allow the including file to override the location of errno. This can
2010 * be useful when using clone() with the CLONE_VM option.
2011 */
2012 #define LSS_ERRNO SYS_ERRNO
2013 #else
2014 #define LSS_ERRNO errno
2015 #endif
2016
2017 #undef LSS_INLINE
2018 #ifdef SYS_INLINE
2019 #define LSS_INLINE SYS_INLINE
2020 #else
2021 #define LSS_INLINE static inline
2022 #endif
2023
2024 /* Allow the including file to override the prefix used for all new
2025 * system calls. By default, it will be set to "sys_".
2026 */
2027 #undef LSS_NAME
2028 #ifndef SYS_PREFIX
2029 #define LSS_NAME(name) sys_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00002030 #elif defined(SYS_PREFIX) && SYS_PREFIX < 0
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002031 #define LSS_NAME(name) name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00002032 #elif defined(SYS_PREFIX) && SYS_PREFIX == 0
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002033 #define LSS_NAME(name) sys0_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00002034 #elif defined(SYS_PREFIX) && SYS_PREFIX == 1
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002035 #define LSS_NAME(name) sys1_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00002036 #elif defined(SYS_PREFIX) && SYS_PREFIX == 2
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002037 #define LSS_NAME(name) sys2_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00002038 #elif defined(SYS_PREFIX) && SYS_PREFIX == 3
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002039 #define LSS_NAME(name) sys3_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00002040 #elif defined(SYS_PREFIX) && SYS_PREFIX == 4
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002041 #define LSS_NAME(name) sys4_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00002042 #elif defined(SYS_PREFIX) && SYS_PREFIX == 5
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002043 #define LSS_NAME(name) sys5_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00002044 #elif defined(SYS_PREFIX) && SYS_PREFIX == 6
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002045 #define LSS_NAME(name) sys6_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00002046 #elif defined(SYS_PREFIX) && SYS_PREFIX == 7
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002047 #define LSS_NAME(name) sys7_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00002048 #elif defined(SYS_PREFIX) && SYS_PREFIX == 8
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002049 #define LSS_NAME(name) sys8_##name
mseaborn@chromium.org88a55e02012-06-14 19:43:32 +00002050 #elif defined(SYS_PREFIX) && SYS_PREFIX == 9
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002051 #define LSS_NAME(name) sys9_##name
2052 #endif
2053
2054 #undef LSS_RETURN
Askar Safine1e7b0a2021-04-12 14:03:02 +03002055 #if defined(__i386__) || defined(__x86_64__) || defined(__ARM_ARCH_3__) \
2056 || defined(__ARM_EABI__) || defined(__aarch64__) || defined(__s390__) \
mingtaoxt xtc0c96892022-08-11 16:53:21 +08002057 || defined(__e2k__) || defined(__riscv) || defined(__loongarch_lp64)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002058 /* Failing system calls return a negative result in the range of
2059 * -1..-4095. These are "errno" values with the sign inverted.
2060 */
2061 #define LSS_RETURN(type, res) \
2062 do { \
2063 if ((unsigned long)(res) >= (unsigned long)(-4095)) { \
Peter Kasting0d6435b2022-07-20 20:21:35 +00002064 LSS_ERRNO = (int)(-(res)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002065 res = -1; \
2066 } \
2067 return (type) (res); \
2068 } while (0)
2069 #elif defined(__mips__)
2070 /* On MIPS, failing system calls return -1, and set errno in a
2071 * separate CPU register.
2072 */
2073 #define LSS_RETURN(type, res, err) \
2074 do { \
2075 if (err) { \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00002076 unsigned long __errnovalue = (res); \
2077 LSS_ERRNO = __errnovalue; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002078 res = -1; \
2079 } \
2080 return (type) (res); \
2081 } while (0)
2082 #elif defined(__PPC__)
2083 /* On PPC, failing system calls return -1, and set errno in a
2084 * separate CPU register. See linux/unistd.h.
2085 */
2086 #define LSS_RETURN(type, res, err) \
2087 do { \
2088 if (err & 0x10000000 ) { \
2089 LSS_ERRNO = (res); \
2090 res = -1; \
2091 } \
2092 return (type) (res); \
2093 } while (0)
2094 #endif
2095 #if defined(__i386__)
2096 /* In PIC mode (e.g. when building shared libraries), gcc for i386
2097 * reserves ebx. Unfortunately, most distribution ship with implementations
2098 * of _syscallX() which clobber ebx.
2099 * Also, most definitions of _syscallX() neglect to mark "memory" as being
2100 * clobbered. This causes problems with compilers, that do a better job
2101 * at optimizing across __asm__ calls.
2102 * So, we just have to redefine all of the _syscallX() macros.
2103 */
2104 #undef LSS_ENTRYPOINT
2105 #ifdef SYS_SYSCALL_ENTRYPOINT
2106 static inline void (**LSS_NAME(get_syscall_entrypoint)(void))(void) {
2107 void (**entrypoint)(void);
2108 asm volatile(".bss\n"
2109 ".align 8\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002110 ".globl " SYS_SYSCALL_ENTRYPOINT "\n"
2111 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002112 ".previous\n"
2113 /* This logically does 'lea "SYS_SYSCALL_ENTRYPOINT", %0' */
2114 "call 0f\n"
2115 "0:pop %0\n"
2116 "add $_GLOBAL_OFFSET_TABLE_+[.-0b], %0\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002117 "mov " SYS_SYSCALL_ENTRYPOINT "@GOT(%0), %0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002118 : "=r"(entrypoint));
2119 return entrypoint;
2120 }
2121
2122 #define LSS_ENTRYPOINT ".bss\n" \
2123 ".align 8\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002124 ".globl " SYS_SYSCALL_ENTRYPOINT "\n" \
2125 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002126 ".previous\n" \
2127 /* Check the SYS_SYSCALL_ENTRYPOINT vector */ \
2128 "push %%eax\n" \
2129 "call 10000f\n" \
2130 "10000:pop %%eax\n" \
2131 "add $_GLOBAL_OFFSET_TABLE_+[.-10000b], %%eax\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002132 "mov " SYS_SYSCALL_ENTRYPOINT \
2133 "@GOT(%%eax), %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002134 "mov 0(%%eax), %%eax\n" \
2135 "test %%eax, %%eax\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00002136 "jz 10002f\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002137 "push %%eax\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00002138 "call 10001f\n" \
2139 "10001:pop %%eax\n" \
2140 "add $(10003f-10001b), %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002141 "xchg 4(%%esp), %%eax\n" \
2142 "ret\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00002143 "10002:pop %%eax\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002144 "int $0x80\n" \
agl@chromium.org92bafa42011-10-12 14:43:04 +00002145 "10003:\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002146 #else
2147 #define LSS_ENTRYPOINT "int $0x80\n"
2148 #endif
2149 #undef LSS_BODY
2150 #define LSS_BODY(type,args...) \
2151 long __res; \
2152 __asm__ __volatile__("push %%ebx\n" \
2153 "movl %2,%%ebx\n" \
2154 LSS_ENTRYPOINT \
2155 "pop %%ebx" \
2156 args \
Joshua Perazabe2d5a82020-04-15 14:36:21 -07002157 : "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002158 LSS_RETURN(type,__res)
2159 #undef _syscall0
2160 #define _syscall0(type,name) \
2161 type LSS_NAME(name)(void) { \
2162 long __res; \
2163 __asm__ volatile(LSS_ENTRYPOINT \
2164 : "=a" (__res) \
2165 : "0" (__NR_##name) \
Khem Raj8048ece2018-12-22 16:07:39 -08002166 : "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002167 LSS_RETURN(type,__res); \
2168 }
2169 #undef _syscall1
2170 #define _syscall1(type,name,type1,arg1) \
2171 type LSS_NAME(name)(type1 arg1) { \
2172 LSS_BODY(type, \
2173 : "=a" (__res) \
2174 : "0" (__NR_##name), "ri" ((long)(arg1))); \
2175 }
2176 #undef _syscall2
2177 #define _syscall2(type,name,type1,arg1,type2,arg2) \
2178 type LSS_NAME(name)(type1 arg1,type2 arg2) { \
2179 LSS_BODY(type, \
2180 : "=a" (__res) \
2181 : "0" (__NR_##name),"ri" ((long)(arg1)), "c" ((long)(arg2))); \
2182 }
2183 #undef _syscall3
2184 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
2185 type LSS_NAME(name)(type1 arg1,type2 arg2,type3 arg3) { \
2186 LSS_BODY(type, \
2187 : "=a" (__res) \
2188 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
2189 "d" ((long)(arg3))); \
2190 }
2191 #undef _syscall4
2192 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2193 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2194 LSS_BODY(type, \
2195 : "=a" (__res) \
2196 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
2197 "d" ((long)(arg3)),"S" ((long)(arg4))); \
2198 }
2199 #undef _syscall5
2200 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2201 type5,arg5) \
2202 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2203 type5 arg5) { \
2204 long __res; \
2205 __asm__ __volatile__("push %%ebx\n" \
2206 "movl %2,%%ebx\n" \
2207 "movl %1,%%eax\n" \
2208 LSS_ENTRYPOINT \
2209 "pop %%ebx" \
2210 : "=a" (__res) \
2211 : "i" (__NR_##name), "ri" ((long)(arg1)), \
2212 "c" ((long)(arg2)), "d" ((long)(arg3)), \
2213 "S" ((long)(arg4)), "D" ((long)(arg5)) \
Joshua Perazabe2d5a82020-04-15 14:36:21 -07002214 : "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002215 LSS_RETURN(type,__res); \
2216 }
2217 #undef _syscall6
2218 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2219 type5,arg5,type6,arg6) \
2220 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2221 type5 arg5, type6 arg6) { \
2222 long __res; \
2223 struct { long __a1; long __a6; } __s = { (long)arg1, (long) arg6 }; \
2224 __asm__ __volatile__("push %%ebp\n" \
2225 "push %%ebx\n" \
mseaborn@chromium.orge96ade32012-10-27 17:47:38 +00002226 "movl 4(%2),%%ebp\n" \
2227 "movl 0(%2), %%ebx\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002228 "movl %1,%%eax\n" \
2229 LSS_ENTRYPOINT \
2230 "pop %%ebx\n" \
2231 "pop %%ebp" \
2232 : "=a" (__res) \
2233 : "i" (__NR_##name), "0" ((long)(&__s)), \
2234 "c" ((long)(arg2)), "d" ((long)(arg3)), \
2235 "S" ((long)(arg4)), "D" ((long)(arg5)) \
Joshua Perazabe2d5a82020-04-15 14:36:21 -07002236 : "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002237 LSS_RETURN(type,__res); \
2238 }
2239 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2240 int flags, void *arg, int *parent_tidptr,
2241 void *newtls, int *child_tidptr) {
2242 long __res;
2243 __asm__ __volatile__(/* if (fn == NULL)
2244 * return -EINVAL;
2245 */
2246 "movl %3,%%ecx\n"
2247 "jecxz 1f\n"
2248
2249 /* if (child_stack == NULL)
2250 * return -EINVAL;
2251 */
2252 "movl %4,%%ecx\n"
2253 "jecxz 1f\n"
2254
2255 /* Set up alignment of the child stack:
2256 * child_stack = (child_stack & ~0xF) - 20;
2257 */
2258 "andl $-16,%%ecx\n"
2259 "subl $20,%%ecx\n"
2260
2261 /* Push "arg" and "fn" onto the stack that will be
2262 * used by the child.
2263 */
2264 "movl %6,%%eax\n"
2265 "movl %%eax,4(%%ecx)\n"
2266 "movl %3,%%eax\n"
2267 "movl %%eax,(%%ecx)\n"
2268
2269 /* %eax = syscall(%eax = __NR_clone,
2270 * %ebx = flags,
2271 * %ecx = child_stack,
2272 * %edx = parent_tidptr,
2273 * %esi = newtls,
2274 * %edi = child_tidptr)
2275 * Also, make sure that %ebx gets preserved as it is
2276 * used in PIC mode.
2277 */
2278 "movl %8,%%esi\n"
2279 "movl %7,%%edx\n"
2280 "movl %5,%%eax\n"
2281 "movl %9,%%edi\n"
2282 "pushl %%ebx\n"
2283 "movl %%eax,%%ebx\n"
2284 "movl %2,%%eax\n"
2285 LSS_ENTRYPOINT
2286
2287 /* In the parent: restore %ebx
2288 * In the child: move "fn" into %ebx
2289 */
2290 "popl %%ebx\n"
2291
2292 /* if (%eax != 0)
2293 * return %eax;
2294 */
2295 "test %%eax,%%eax\n"
2296 "jnz 1f\n"
2297
2298 /* In the child, now. Terminate frame pointer chain.
2299 */
2300 "movl $0,%%ebp\n"
2301
2302 /* Call "fn". "arg" is already on the stack.
2303 */
2304 "call *%%ebx\n"
2305
2306 /* Call _exit(%ebx). Unfortunately older versions
2307 * of gcc restrict the number of arguments that can
2308 * be passed to asm(). So, we need to hard-code the
2309 * system call number.
2310 */
2311 "movl %%eax,%%ebx\n"
2312 "movl $1,%%eax\n"
2313 LSS_ENTRYPOINT
2314
2315 /* Return to parent.
2316 */
2317 "1:\n"
2318 : "=a" (__res)
2319 : "0"(-EINVAL), "i"(__NR_clone),
2320 "m"(fn), "m"(child_stack), "m"(flags), "m"(arg),
2321 "m"(parent_tidptr), "m"(newtls), "m"(child_tidptr)
Joshua Perazabe2d5a82020-04-15 14:36:21 -07002322 : "memory", "ecx", "edx", "esi", "edi");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002323 LSS_RETURN(int, __res);
2324 }
2325
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002326 LSS_INLINE _syscall1(int, set_thread_area, void *, u)
2327 LSS_INLINE _syscall1(int, get_thread_area, void *, u)
2328
2329 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
2330 /* On i386, the kernel does not know how to return from a signal
2331 * handler. Instead, it relies on user space to provide a
2332 * restorer function that calls the {rt_,}sigreturn() system call.
2333 * Unfortunately, we cannot just reference the glibc version of this
2334 * function, as glibc goes out of its way to make it inaccessible.
2335 */
2336 void (*res)(void);
2337 __asm__ __volatile__("call 2f\n"
2338 "0:.align 16\n"
2339 "1:movl %1,%%eax\n"
2340 LSS_ENTRYPOINT
2341 "2:popl %0\n"
2342 "addl $(1b-0b),%0\n"
2343 : "=a" (res)
2344 : "i" (__NR_rt_sigreturn));
2345 return res;
2346 }
2347 LSS_INLINE void (*LSS_NAME(restore)(void))(void) {
2348 /* On i386, the kernel does not know how to return from a signal
2349 * handler. Instead, it relies on user space to provide a
2350 * restorer function that calls the {rt_,}sigreturn() system call.
2351 * Unfortunately, we cannot just reference the glibc version of this
2352 * function, as glibc goes out of its way to make it inaccessible.
2353 */
2354 void (*res)(void);
2355 __asm__ __volatile__("call 2f\n"
2356 "0:.align 16\n"
2357 "1:pop %%eax\n"
2358 "movl %1,%%eax\n"
2359 LSS_ENTRYPOINT
2360 "2:popl %0\n"
2361 "addl $(1b-0b),%0\n"
2362 : "=a" (res)
2363 : "i" (__NR_sigreturn));
2364 return res;
2365 }
2366 #elif defined(__x86_64__)
2367 /* There are no known problems with any of the _syscallX() macros
2368 * currently shipping for x86_64, but we still need to be able to define
2369 * our own version so that we can override the location of the errno
2370 * location (e.g. when using the clone() system call with the CLONE_VM
2371 * option).
2372 */
2373 #undef LSS_ENTRYPOINT
2374 #ifdef SYS_SYSCALL_ENTRYPOINT
2375 static inline void (**LSS_NAME(get_syscall_entrypoint)(void))(void) {
2376 void (**entrypoint)(void);
2377 asm volatile(".bss\n"
2378 ".align 8\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002379 ".globl " SYS_SYSCALL_ENTRYPOINT "\n"
2380 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002381 ".previous\n"
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002382 "mov " SYS_SYSCALL_ENTRYPOINT "@GOTPCREL(%%rip), %0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002383 : "=r"(entrypoint));
2384 return entrypoint;
2385 }
2386
2387 #define LSS_ENTRYPOINT \
2388 ".bss\n" \
2389 ".align 8\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002390 ".globl " SYS_SYSCALL_ENTRYPOINT "\n" \
2391 ".common " SYS_SYSCALL_ENTRYPOINT ",8,8\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002392 ".previous\n" \
mseaborn@chromium.orgc0e5b382014-05-28 17:59:51 +00002393 "mov " SYS_SYSCALL_ENTRYPOINT "@GOTPCREL(%%rip), %%rcx\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002394 "mov 0(%%rcx), %%rcx\n" \
2395 "test %%rcx, %%rcx\n" \
2396 "jz 10001f\n" \
2397 "call *%%rcx\n" \
2398 "jmp 10002f\n" \
2399 "10001:syscall\n" \
2400 "10002:\n"
2401
2402 #else
2403 #define LSS_ENTRYPOINT "syscall\n"
2404 #endif
vapier@chromium.org2273e812013-04-01 17:52:44 +00002405
2406 /* The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
2407 * We need to explicitly cast to an unsigned 64 bit type to avoid implicit
2408 * sign extension. We can't cast pointers directly because those are
2409 * 32 bits, and gcc will dump ugly warnings about casting from a pointer
2410 * to an integer of a different size.
2411 */
2412 #undef LSS_SYSCALL_ARG
2413 #define LSS_SYSCALL_ARG(a) ((uint64_t)(uintptr_t)(a))
2414 #undef _LSS_RETURN
2415 #define _LSS_RETURN(type, res, cast) \
2416 do { \
2417 if ((uint64_t)(res) >= (uint64_t)(-4095)) { \
Peter Kasting880985f2022-06-29 21:17:55 +00002418 LSS_ERRNO = (int)(-(res)); \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002419 res = -1; \
2420 } \
2421 return (type)(cast)(res); \
2422 } while (0)
2423 #undef LSS_RETURN
2424 #define LSS_RETURN(type, res) _LSS_RETURN(type, res, uintptr_t)
2425
2426 #undef _LSS_BODY
2427 #define _LSS_BODY(nr, type, name, cast, ...) \
2428 long long __res; \
2429 __asm__ __volatile__(LSS_BODY_ASM##nr LSS_ENTRYPOINT \
2430 : "=a" (__res) \
2431 : "0" (__NR_##name) LSS_BODY_ARG##nr(__VA_ARGS__) \
2432 : LSS_BODY_CLOBBER##nr "r11", "rcx", "memory"); \
2433 _LSS_RETURN(type, __res, cast)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002434 #undef LSS_BODY
vapier@chromium.org2273e812013-04-01 17:52:44 +00002435 #define LSS_BODY(nr, type, name, args...) \
2436 _LSS_BODY(nr, type, name, uintptr_t, ## args)
2437
2438 #undef LSS_BODY_ASM0
2439 #undef LSS_BODY_ASM1
2440 #undef LSS_BODY_ASM2
2441 #undef LSS_BODY_ASM3
2442 #undef LSS_BODY_ASM4
2443 #undef LSS_BODY_ASM5
2444 #undef LSS_BODY_ASM6
2445 #define LSS_BODY_ASM0
2446 #define LSS_BODY_ASM1 LSS_BODY_ASM0
2447 #define LSS_BODY_ASM2 LSS_BODY_ASM1
2448 #define LSS_BODY_ASM3 LSS_BODY_ASM2
2449 #define LSS_BODY_ASM4 LSS_BODY_ASM3 "movq %5,%%r10;"
2450 #define LSS_BODY_ASM5 LSS_BODY_ASM4 "movq %6,%%r8;"
2451 #define LSS_BODY_ASM6 LSS_BODY_ASM5 "movq %7,%%r9;"
2452
2453 #undef LSS_BODY_CLOBBER0
2454 #undef LSS_BODY_CLOBBER1
2455 #undef LSS_BODY_CLOBBER2
2456 #undef LSS_BODY_CLOBBER3
2457 #undef LSS_BODY_CLOBBER4
2458 #undef LSS_BODY_CLOBBER5
2459 #undef LSS_BODY_CLOBBER6
2460 #define LSS_BODY_CLOBBER0
2461 #define LSS_BODY_CLOBBER1 LSS_BODY_CLOBBER0
2462 #define LSS_BODY_CLOBBER2 LSS_BODY_CLOBBER1
2463 #define LSS_BODY_CLOBBER3 LSS_BODY_CLOBBER2
2464 #define LSS_BODY_CLOBBER4 LSS_BODY_CLOBBER3 "r10",
2465 #define LSS_BODY_CLOBBER5 LSS_BODY_CLOBBER4 "r8",
2466 #define LSS_BODY_CLOBBER6 LSS_BODY_CLOBBER5 "r9",
2467
2468 #undef LSS_BODY_ARG0
2469 #undef LSS_BODY_ARG1
2470 #undef LSS_BODY_ARG2
2471 #undef LSS_BODY_ARG3
2472 #undef LSS_BODY_ARG4
2473 #undef LSS_BODY_ARG5
2474 #undef LSS_BODY_ARG6
2475 #define LSS_BODY_ARG0()
2476 #define LSS_BODY_ARG1(arg1) \
2477 LSS_BODY_ARG0(), "D" (arg1)
2478 #define LSS_BODY_ARG2(arg1, arg2) \
2479 LSS_BODY_ARG1(arg1), "S" (arg2)
2480 #define LSS_BODY_ARG3(arg1, arg2, arg3) \
2481 LSS_BODY_ARG2(arg1, arg2), "d" (arg3)
2482 #define LSS_BODY_ARG4(arg1, arg2, arg3, arg4) \
2483 LSS_BODY_ARG3(arg1, arg2, arg3), "r" (arg4)
2484 #define LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5) \
2485 LSS_BODY_ARG4(arg1, arg2, arg3, arg4), "r" (arg5)
2486 #define LSS_BODY_ARG6(arg1, arg2, arg3, arg4, arg5, arg6) \
2487 LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5), "r" (arg6)
2488
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002489 #undef _syscall0
2490 #define _syscall0(type,name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002491 type LSS_NAME(name)(void) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002492 LSS_BODY(0, type, name); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002493 }
2494 #undef _syscall1
2495 #define _syscall1(type,name,type1,arg1) \
2496 type LSS_NAME(name)(type1 arg1) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002497 LSS_BODY(1, type, name, LSS_SYSCALL_ARG(arg1)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002498 }
2499 #undef _syscall2
2500 #define _syscall2(type,name,type1,arg1,type2,arg2) \
2501 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002502 LSS_BODY(2, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002503 }
2504 #undef _syscall3
2505 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
2506 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002507 LSS_BODY(3, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2508 LSS_SYSCALL_ARG(arg3)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002509 }
2510 #undef _syscall4
2511 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2512 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002513 LSS_BODY(4, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2514 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002515 }
2516 #undef _syscall5
2517 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2518 type5,arg5) \
2519 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2520 type5 arg5) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002521 LSS_BODY(5, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2522 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
2523 LSS_SYSCALL_ARG(arg5)); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002524 }
2525 #undef _syscall6
2526 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2527 type5,arg5,type6,arg6) \
2528 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2529 type5 arg5, type6 arg6) { \
vapier@chromium.org2273e812013-04-01 17:52:44 +00002530 LSS_BODY(6, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
2531 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
2532 LSS_SYSCALL_ARG(arg5), LSS_SYSCALL_ARG(arg6));\
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002533 }
2534 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2535 int flags, void *arg, int *parent_tidptr,
2536 void *newtls, int *child_tidptr) {
vapier@chromium.org2273e812013-04-01 17:52:44 +00002537 long long __res;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002538 {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002539 __asm__ __volatile__(/* if (fn == NULL)
2540 * return -EINVAL;
2541 */
2542 "testq %4,%4\n"
2543 "jz 1f\n"
2544
2545 /* if (child_stack == NULL)
2546 * return -EINVAL;
2547 */
2548 "testq %5,%5\n"
2549 "jz 1f\n"
2550
2551 /* childstack -= 2*sizeof(void *);
2552 */
2553 "subq $16,%5\n"
2554
2555 /* Push "arg" and "fn" onto the stack that will be
2556 * used by the child.
2557 */
2558 "movq %7,8(%5)\n"
2559 "movq %4,0(%5)\n"
2560
2561 /* %rax = syscall(%rax = __NR_clone,
2562 * %rdi = flags,
2563 * %rsi = child_stack,
2564 * %rdx = parent_tidptr,
2565 * %r8 = new_tls,
2566 * %r10 = child_tidptr)
2567 */
2568 "movq %2,%%rax\n"
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00002569 "movq %9,%%r8\n"
2570 "movq %10,%%r10\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002571 LSS_ENTRYPOINT
2572
2573 /* if (%rax != 0)
2574 * return;
2575 */
2576 "testq %%rax,%%rax\n"
2577 "jnz 1f\n"
2578
2579 /* In the child. Terminate frame pointer chain.
2580 */
2581 "xorq %%rbp,%%rbp\n"
2582
2583 /* Call "fn(arg)".
2584 */
2585 "popq %%rax\n"
2586 "popq %%rdi\n"
2587 "call *%%rax\n"
2588
2589 /* Call _exit(%ebx).
2590 */
2591 "movq %%rax,%%rdi\n"
2592 "movq %3,%%rax\n"
2593 LSS_ENTRYPOINT
2594
2595 /* Return to parent.
2596 */
2597 "1:\n"
2598 : "=a" (__res)
2599 : "0"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit),
vapier@chromium.org2273e812013-04-01 17:52:44 +00002600 "r"(LSS_SYSCALL_ARG(fn)),
2601 "S"(LSS_SYSCALL_ARG(child_stack)),
2602 "D"(LSS_SYSCALL_ARG(flags)),
2603 "r"(LSS_SYSCALL_ARG(arg)),
2604 "d"(LSS_SYSCALL_ARG(parent_tidptr)),
2605 "r"(LSS_SYSCALL_ARG(newtls)),
2606 "r"(LSS_SYSCALL_ARG(child_tidptr))
Khem Raj8048ece2018-12-22 16:07:39 -08002607 : "memory", "r8", "r10", "r11", "rcx");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002608 }
2609 LSS_RETURN(int, __res);
2610 }
2611 LSS_INLINE _syscall2(int, arch_prctl, int, c, void *, a)
vapier@chromium.org2273e812013-04-01 17:52:44 +00002612
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002613 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
2614 /* On x86-64, the kernel does not know how to return from
2615 * a signal handler. Instead, it relies on user space to provide a
2616 * restorer function that calls the rt_sigreturn() system call.
2617 * Unfortunately, we cannot just reference the glibc version of this
2618 * function, as glibc goes out of its way to make it inaccessible.
2619 */
vapier@chromium.org2273e812013-04-01 17:52:44 +00002620 long long res;
mseaborn@chromium.org798c2f72013-08-31 00:04:49 +00002621 __asm__ __volatile__("jmp 2f\n"
2622 ".align 16\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002623 "1:movq %1,%%rax\n"
2624 LSS_ENTRYPOINT
mseaborn@chromium.org798c2f72013-08-31 00:04:49 +00002625 "2:leaq 1b(%%rip),%0\n"
2626 : "=r" (res)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002627 : "i" (__NR_rt_sigreturn));
vapier@chromium.org833a10e2013-04-02 19:34:26 +00002628 return (void (*)(void))(uintptr_t)res;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002629 }
2630 #elif defined(__ARM_ARCH_3__)
2631 /* Most definitions of _syscallX() neglect to mark "memory" as being
2632 * clobbered. This causes problems with compilers, that do a better job
2633 * at optimizing across __asm__ calls.
2634 * So, we just have to redefine all of the _syscallX() macros.
2635 */
2636 #undef LSS_REG
2637 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
2638 #undef LSS_BODY
2639 #define LSS_BODY(type,name,args...) \
2640 register long __res_r0 __asm__("r0"); \
2641 long __res; \
2642 __asm__ __volatile__ (__syscall(name) \
2643 : "=r"(__res_r0) : args : "lr", "memory"); \
2644 __res = __res_r0; \
2645 LSS_RETURN(type, __res)
2646 #undef _syscall0
2647 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002648 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002649 LSS_BODY(type, name); \
2650 }
2651 #undef _syscall1
2652 #define _syscall1(type, name, type1, arg1) \
2653 type LSS_NAME(name)(type1 arg1) { \
2654 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2655 }
2656 #undef _syscall2
2657 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2658 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2659 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2660 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2661 }
2662 #undef _syscall3
2663 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2664 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2665 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2666 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2667 }
2668 #undef _syscall4
2669 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2670 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2671 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2672 LSS_REG(3, arg4); \
2673 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2674 }
2675 #undef _syscall5
2676 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2677 type5,arg5) \
2678 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2679 type5 arg5) { \
2680 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2681 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2682 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2683 "r"(__r4)); \
2684 }
2685 #undef _syscall6
2686 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2687 type5,arg5,type6,arg6) \
2688 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2689 type5 arg5, type6 arg6) { \
2690 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2691 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2692 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2693 "r"(__r4), "r"(__r5)); \
2694 }
2695 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2696 int flags, void *arg, int *parent_tidptr,
2697 void *newtls, int *child_tidptr) {
2698 long __res;
2699 {
2700 register int __flags __asm__("r0") = flags;
2701 register void *__stack __asm__("r1") = child_stack;
2702 register void *__ptid __asm__("r2") = parent_tidptr;
2703 register void *__tls __asm__("r3") = newtls;
2704 register int *__ctid __asm__("r4") = child_tidptr;
2705 __asm__ __volatile__(/* if (fn == NULL || child_stack == NULL)
2706 * return -EINVAL;
2707 */
2708 "cmp %2,#0\n"
2709 "cmpne %3,#0\n"
2710 "moveq %0,%1\n"
2711 "beq 1f\n"
2712
2713 /* Push "arg" and "fn" onto the stack that will be
2714 * used by the child.
2715 */
2716 "str %5,[%3,#-4]!\n"
2717 "str %2,[%3,#-4]!\n"
2718
2719 /* %r0 = syscall(%r0 = flags,
2720 * %r1 = child_stack,
2721 * %r2 = parent_tidptr,
2722 * %r3 = newtls,
2723 * %r4 = child_tidptr)
2724 */
2725 __syscall(clone)"\n"
2726
2727 /* if (%r0 != 0)
2728 * return %r0;
2729 */
2730 "movs %0,r0\n"
2731 "bne 1f\n"
2732
2733 /* In the child, now. Call "fn(arg)".
2734 */
2735 "ldr r0,[sp, #4]\n"
2736 "mov lr,pc\n"
2737 "ldr pc,[sp]\n"
2738
2739 /* Call _exit(%r0).
2740 */
2741 __syscall(exit)"\n"
2742 "1:\n"
2743 : "=r" (__res)
2744 : "i"(-EINVAL),
2745 "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
2746 "r"(__ptid), "r"(__tls), "r"(__ctid)
2747 : "cc", "lr", "memory");
2748 }
2749 LSS_RETURN(int, __res);
2750 }
2751 #elif defined(__ARM_EABI__)
2752 /* Most definitions of _syscallX() neglect to mark "memory" as being
2753 * clobbered. This causes problems with compilers, that do a better job
2754 * at optimizing across __asm__ calls.
2755 * So, we just have to redefine all fo the _syscallX() macros.
2756 */
2757 #undef LSS_REG
2758 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
2759 #undef LSS_BODY
2760 #define LSS_BODY(type,name,args...) \
2761 register long __res_r0 __asm__("r0"); \
2762 long __res; \
2763 __asm__ __volatile__ ("push {r7}\n" \
2764 "mov r7, %1\n" \
2765 "swi 0x0\n" \
2766 "pop {r7}\n" \
2767 : "=r"(__res_r0) \
2768 : "i"(__NR_##name) , ## args \
2769 : "lr", "memory"); \
2770 __res = __res_r0; \
2771 LSS_RETURN(type, __res)
2772 #undef _syscall0
2773 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00002774 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002775 LSS_BODY(type, name); \
2776 }
2777 #undef _syscall1
2778 #define _syscall1(type, name, type1, arg1) \
2779 type LSS_NAME(name)(type1 arg1) { \
2780 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2781 }
2782 #undef _syscall2
2783 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2784 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2785 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2786 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2787 }
2788 #undef _syscall3
2789 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2790 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2791 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2792 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2793 }
2794 #undef _syscall4
2795 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2796 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2797 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2798 LSS_REG(3, arg4); \
2799 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2800 }
2801 #undef _syscall5
2802 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2803 type5,arg5) \
2804 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2805 type5 arg5) { \
2806 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2807 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2808 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2809 "r"(__r4)); \
2810 }
2811 #undef _syscall6
2812 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2813 type5,arg5,type6,arg6) \
2814 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2815 type5 arg5, type6 arg6) { \
2816 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2817 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2818 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2819 "r"(__r4), "r"(__r5)); \
2820 }
2821 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2822 int flags, void *arg, int *parent_tidptr,
2823 void *newtls, int *child_tidptr) {
2824 long __res;
Amaury Le Leyzourc555f532017-02-23 12:33:02 -08002825 if (fn == NULL || child_stack == NULL) {
2826 __res = -EINVAL;
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002827 LSS_RETURN(int, __res);
2828 }
2829
2830 /* Push "arg" and "fn" onto the stack that will be
2831 * used by the child.
2832 */
2833 {
2834 uintptr_t* cstack = (uintptr_t*)child_stack - 2;
2835 cstack[0] = (uintptr_t)fn;
2836 cstack[1] = (uintptr_t)arg;
2837 child_stack = cstack;
2838 }
2839 {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002840 register int __flags __asm__("r0") = flags;
2841 register void *__stack __asm__("r1") = child_stack;
2842 register void *__ptid __asm__("r2") = parent_tidptr;
2843 register void *__tls __asm__("r3") = newtls;
2844 register int *__ctid __asm__("r4") = child_tidptr;
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002845 __asm__ __volatile__(
Nico Weber63f24c82017-03-30 13:37:06 -04002846#ifdef __thumb2__
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002847 "push {r7}\n"
Nico Weber63f24c82017-03-30 13:37:06 -04002848#endif
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002849 /* %r0 = syscall(%r0 = flags,
2850 * %r1 = child_stack,
2851 * %r2 = parent_tidptr,
2852 * %r3 = newtls,
2853 * %r4 = child_tidptr)
2854 */
2855 "mov r7, %6\n"
2856 "swi 0x0\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002857
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002858 /* if (%r0 != 0)
2859 * return %r0;
2860 */
2861 "cmp r0, #0\n"
2862 "bne 1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002863
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002864 /* In the child, now. Call "fn(arg)".
2865 */
2866 "ldr r0,[sp, #4]\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002867
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002868 "ldr lr,[sp]\n"
2869 "blx lr\n"
zodiac@gmail.com68c659b2011-10-06 05:34:19 +00002870
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002871 /* Call _exit(%r0).
2872 */
2873 "mov r7, %7\n"
2874 "swi 0x0\n"
2875 /* Unreachable */
2876 "bkpt #0\n"
2877 "1:\n"
Nico Weber63f24c82017-03-30 13:37:06 -04002878#ifdef __thumb2__
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002879 "pop {r7}\n"
Nico Weber63f24c82017-03-30 13:37:06 -04002880#endif
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002881 "movs %0,r0\n"
2882 : "=r"(__res)
2883 : "r"(__stack), "r"(__flags), "r"(__ptid), "r"(__tls), "r"(__ctid),
2884 "i"(__NR_clone), "i"(__NR_exit)
2885 : "cc", "lr", "memory"
2886#ifndef __thumb2__
2887 , "r7"
Nico Weber63f24c82017-03-30 13:37:06 -04002888#endif
Amaury Le Leyzoura91633d2017-06-01 10:44:09 -07002889 );
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00002890 }
2891 LSS_RETURN(int, __res);
2892 }
anton@chromium.org2f724fc2014-04-15 13:05:20 +00002893 #elif defined(__aarch64__)
2894 /* Most definitions of _syscallX() neglect to mark "memory" as being
2895 * clobbered. This causes problems with compilers, that do a better job
2896 * at optimizing across __asm__ calls.
2897 * So, we just have to redefine all of the _syscallX() macros.
2898 */
2899 #undef LSS_REG
2900 #define LSS_REG(r,a) register int64_t __r##r __asm__("x"#r) = (int64_t)a
2901 #undef LSS_BODY
2902 #define LSS_BODY(type,name,args...) \
2903 register int64_t __res_x0 __asm__("x0"); \
2904 int64_t __res; \
2905 __asm__ __volatile__ ("mov x8, %1\n" \
2906 "svc 0x0\n" \
2907 : "=r"(__res_x0) \
2908 : "i"(__NR_##name) , ## args \
2909 : "x8", "memory"); \
2910 __res = __res_x0; \
2911 LSS_RETURN(type, __res)
2912 #undef _syscall0
2913 #define _syscall0(type, name) \
2914 type LSS_NAME(name)(void) { \
2915 LSS_BODY(type, name); \
2916 }
2917 #undef _syscall1
2918 #define _syscall1(type, name, type1, arg1) \
2919 type LSS_NAME(name)(type1 arg1) { \
2920 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
2921 }
2922 #undef _syscall2
2923 #define _syscall2(type, name, type1, arg1, type2, arg2) \
2924 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
2925 LSS_REG(0, arg1); LSS_REG(1, arg2); \
2926 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
2927 }
2928 #undef _syscall3
2929 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
2930 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
2931 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2932 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
2933 }
2934 #undef _syscall4
2935 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
2936 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
2937 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2938 LSS_REG(3, arg4); \
2939 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
2940 }
2941 #undef _syscall5
2942 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2943 type5,arg5) \
2944 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2945 type5 arg5) { \
2946 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2947 LSS_REG(3, arg4); LSS_REG(4, arg5); \
2948 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2949 "r"(__r4)); \
2950 }
2951 #undef _syscall6
2952 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
2953 type5,arg5,type6,arg6) \
2954 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
2955 type5 arg5, type6 arg6) { \
2956 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
2957 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
2958 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
2959 "r"(__r4), "r"(__r5)); \
2960 }
2961
2962 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
2963 int flags, void *arg, int *parent_tidptr,
2964 void *newtls, int *child_tidptr) {
2965 int64_t __res;
2966 {
Peter Kasting0d6435b2022-07-20 20:21:35 +00002967 register uint64_t __flags __asm__("x0") = (uint64_t)flags;
anton@chromium.org2f724fc2014-04-15 13:05:20 +00002968 register void *__stack __asm__("x1") = child_stack;
2969 register void *__ptid __asm__("x2") = parent_tidptr;
2970 register void *__tls __asm__("x3") = newtls;
2971 register int *__ctid __asm__("x4") = child_tidptr;
2972 __asm__ __volatile__(/* Push "arg" and "fn" onto the stack that will be
2973 * used by the child.
2974 */
2975 "stp %1, %4, [%2, #-16]!\n"
2976
2977 /* %x0 = syscall(%x0 = flags,
2978 * %x1 = child_stack,
2979 * %x2 = parent_tidptr,
2980 * %x3 = newtls,
2981 * %x4 = child_tidptr)
2982 */
2983 "mov x8, %8\n"
2984 "svc 0x0\n"
2985
2986 /* if (%r0 != 0)
2987 * return %r0;
2988 */
2989 "mov %0, x0\n"
2990 "cbnz x0, 1f\n"
2991
2992 /* In the child, now. Call "fn(arg)".
2993 */
2994 "ldp x1, x0, [sp], #16\n"
2995 "blr x1\n"
2996
2997 /* Call _exit(%r0).
2998 */
2999 "mov x8, %9\n"
3000 "svc 0x0\n"
3001 "1:\n"
3002 : "=r" (__res)
3003 : "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
3004 "r"(__ptid), "r"(__tls), "r"(__ctid),
3005 "i"(__NR_clone), "i"(__NR_exit)
3006 : "cc", "x8", "memory");
3007 }
3008 LSS_RETURN(int, __res);
3009 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003010 #elif defined(__mips__)
3011 #undef LSS_REG
3012 #define LSS_REG(r,a) register unsigned long __r##r __asm__("$"#r) = \
3013 (unsigned long)(a)
3014 #undef LSS_BODY
thestig@chromium.org952107f2014-08-01 02:22:56 +00003015 #undef LSS_SYSCALL_CLOBBERS
3016 #if _MIPS_SIM == _MIPS_SIM_ABI32
3017 #define LSS_SYSCALL_CLOBBERS "$1", "$3", "$8", "$9", "$10", \
3018 "$11", "$12", "$13", "$14", "$15", \
3019 "$24", "$25", "hi", "lo", "memory"
3020 #else
3021 #define LSS_SYSCALL_CLOBBERS "$1", "$3", "$10", "$11", "$12", \
3022 "$13", "$14", "$15", "$24", "$25", \
3023 "hi", "lo", "memory"
3024 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003025 #define LSS_BODY(type,name,r7,...) \
3026 register unsigned long __v0 __asm__("$2") = __NR_##name; \
3027 __asm__ __volatile__ ("syscall\n" \
vapier@chromium.orgda4a4892015-01-22 16:46:39 +00003028 : "=r"(__v0), r7 (__r7) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003029 : "0"(__v0), ##__VA_ARGS__ \
thestig@chromium.org952107f2014-08-01 02:22:56 +00003030 : LSS_SYSCALL_CLOBBERS); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003031 LSS_RETURN(type, __v0, __r7)
3032 #undef _syscall0
3033 #define _syscall0(type, name) \
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00003034 type LSS_NAME(name)(void) { \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003035 register unsigned long __r7 __asm__("$7"); \
3036 LSS_BODY(type, name, "=r"); \
3037 }
3038 #undef _syscall1
3039 #define _syscall1(type, name, type1, arg1) \
3040 type LSS_NAME(name)(type1 arg1) { \
3041 register unsigned long __r7 __asm__("$7"); \
3042 LSS_REG(4, arg1); LSS_BODY(type, name, "=r", "r"(__r4)); \
3043 }
3044 #undef _syscall2
3045 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3046 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3047 register unsigned long __r7 __asm__("$7"); \
3048 LSS_REG(4, arg1); LSS_REG(5, arg2); \
3049 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5)); \
3050 }
3051 #undef _syscall3
3052 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3053 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3054 register unsigned long __r7 __asm__("$7"); \
3055 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
3056 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5), "r"(__r6)); \
3057 }
3058 #undef _syscall4
3059 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
3060 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3061 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
3062 LSS_REG(7, arg4); \
3063 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6)); \
3064 }
3065 #undef _syscall5
3066 #if _MIPS_SIM == _MIPS_SIM_ABI32
3067 /* The old 32bit MIPS system call API passes the fifth and sixth argument
3068 * on the stack, whereas the new APIs use registers "r8" and "r9".
3069 */
3070 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3071 type5,arg5) \
3072 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3073 type5 arg5) { \
3074 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
3075 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003076 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003077 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003078 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003079 "sw %5, 16($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003080 "syscall\n" \
3081 "addiu $29, 32\n" \
3082 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003083 : "+r"(__v0), "+r" (__r7) \
3084 : "r"(__r4), "r"(__r5), \
3085 "r"(__r6), "r" ((unsigned long)arg5) \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003086 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003087 "$13", "$14", "$15", "$24", "$25", \
3088 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003089 LSS_RETURN(type, __v0, __r7); \
3090 }
3091 #else
3092 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3093 type5,arg5) \
3094 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3095 type5 arg5) { \
3096 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
3097 LSS_REG(7, arg4); LSS_REG(8, arg5); \
3098 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
3099 "r"(__r8)); \
3100 }
3101 #endif
3102 #undef _syscall6
3103 #if _MIPS_SIM == _MIPS_SIM_ABI32
3104 /* The old 32bit MIPS system call API passes the fifth and sixth argument
3105 * on the stack, whereas the new APIs use registers "r8" and "r9".
3106 */
3107 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3108 type5,arg5,type6,arg6) \
3109 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3110 type5 arg5, type6 arg6) { \
3111 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
3112 LSS_REG(7, arg4); \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003113 register unsigned long __v0 __asm__("$2") = __NR_##name; \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003114 __asm__ __volatile__ (".set noreorder\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003115 "subu $29, 32\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003116 "sw %5, 16($29)\n" \
3117 "sw %6, 20($29)\n" \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003118 "syscall\n" \
3119 "addiu $29, 32\n" \
3120 ".set reorder\n" \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003121 : "+r"(__v0), "+r" (__r7) \
3122 : "r"(__r4), "r"(__r5), \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003123 "r"(__r6), "r" ((unsigned long)arg5), \
3124 "r" ((unsigned long)arg6) \
3125 : "$8", "$9", "$10", "$11", "$12", \
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003126 "$13", "$14", "$15", "$24", "$25", \
3127 "memory"); \
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003128 LSS_RETURN(type, __v0, __r7); \
3129 }
3130 #else
3131 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3132 type5,arg5,type6,arg6) \
3133 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3134 type5 arg5,type6 arg6) { \
3135 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
3136 LSS_REG(7, arg4); LSS_REG(8, arg5); LSS_REG(9, arg6); \
3137 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
3138 "r"(__r8), "r"(__r9)); \
3139 }
3140 #endif
3141 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3142 int flags, void *arg, int *parent_tidptr,
3143 void *newtls, int *child_tidptr) {
vapier@chromium.orge0797682015-02-20 20:45:56 +00003144 register unsigned long __v0 __asm__("$2") = -EINVAL;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003145 register unsigned long __r7 __asm__("$7") = (unsigned long)newtls;
3146 {
3147 register int __flags __asm__("$4") = flags;
3148 register void *__stack __asm__("$5") = child_stack;
3149 register void *__ptid __asm__("$6") = parent_tidptr;
3150 register int *__ctid __asm__("$8") = child_tidptr;
3151 __asm__ __volatile__(
3152 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
3153 "subu $29,24\n"
3154 #elif _MIPS_SIM == _MIPS_SIM_NABI32
3155 "sub $29,16\n"
3156 #else
3157 "dsubu $29,16\n"
3158 #endif
3159
3160 /* if (fn == NULL || child_stack == NULL)
3161 * return -EINVAL;
3162 */
vapier@chromium.orge0797682015-02-20 20:45:56 +00003163 "beqz %4,1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003164 "beqz %5,1f\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003165
3166 /* Push "arg" and "fn" onto the stack that will be
3167 * used by the child.
3168 */
3169 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
vapier@chromium.orge0797682015-02-20 20:45:56 +00003170 "subu %5,32\n"
3171 "sw %4,0(%5)\n"
3172 "sw %7,4(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003173 #elif _MIPS_SIM == _MIPS_SIM_NABI32
vapier@chromium.orge0797682015-02-20 20:45:56 +00003174 "sub %5,32\n"
3175 "sw %4,0(%5)\n"
3176 "sw %7,8(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003177 #else
vapier@chromium.orge0797682015-02-20 20:45:56 +00003178 "dsubu %5,32\n"
3179 "sd %4,0(%5)\n"
3180 "sd %7,8(%5)\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003181 #endif
3182
3183 /* $7 = syscall($4 = flags,
3184 * $5 = child_stack,
3185 * $6 = parent_tidptr,
3186 * $7 = newtls,
3187 * $8 = child_tidptr)
3188 */
vapier@chromium.orge0797682015-02-20 20:45:56 +00003189 "li $2,%2\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003190 "syscall\n"
3191
3192 /* if ($7 != 0)
3193 * return $2;
3194 */
3195 "bnez $7,1f\n"
3196 "bnez $2,1f\n"
3197
3198 /* In the child, now. Call "fn(arg)".
3199 */
3200 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
3201 "lw $25,0($29)\n"
3202 "lw $4,4($29)\n"
3203 #elif _MIPS_SIM == _MIPS_SIM_NABI32
3204 "lw $25,0($29)\n"
3205 "lw $4,8($29)\n"
3206 #else
3207 "ld $25,0($29)\n"
3208 "ld $4,8($29)\n"
3209 #endif
3210 "jalr $25\n"
3211
3212 /* Call _exit($2)
3213 */
3214 "move $4,$2\n"
vapier@chromium.orge0797682015-02-20 20:45:56 +00003215 "li $2,%3\n"
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003216 "syscall\n"
3217
3218 "1:\n"
3219 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
3220 "addu $29, 24\n"
3221 #elif _MIPS_SIM == _MIPS_SIM_NABI32
3222 "add $29, 16\n"
3223 #else
3224 "daddu $29,16\n"
3225 #endif
petarj@mips.com0ece1c62013-04-10 00:28:04 +00003226 : "+r" (__v0), "+r" (__r7)
vapier@chromium.orge0797682015-02-20 20:45:56 +00003227 : "i"(__NR_clone), "i"(__NR_exit), "r"(fn),
3228 "r"(__stack), "r"(__flags), "r"(arg),
3229 "r"(__ptid), "r"(__ctid)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003230 : "$9", "$10", "$11", "$12", "$13", "$14", "$15",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00003231 "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00003232 }
3233 LSS_RETURN(int, __v0, __r7);
3234 }
3235 #elif defined (__PPC__)
3236 #undef LSS_LOADARGS_0
3237 #define LSS_LOADARGS_0(name, dummy...) \
3238 __sc_0 = __NR_##name
3239 #undef LSS_LOADARGS_1
3240 #define LSS_LOADARGS_1(name, arg1) \
3241 LSS_LOADARGS_0(name); \
3242 __sc_3 = (unsigned long) (arg1)
3243 #undef LSS_LOADARGS_2
3244 #define LSS_LOADARGS_2(name, arg1, arg2) \
3245 LSS_LOADARGS_1(name, arg1); \
3246 __sc_4 = (unsigned long) (arg2)
3247 #undef LSS_LOADARGS_3
3248 #define LSS_LOADARGS_3(name, arg1, arg2, arg3) \
3249 LSS_LOADARGS_2(name, arg1, arg2); \
3250 __sc_5 = (unsigned long) (arg3)
3251 #undef LSS_LOADARGS_4
3252 #define LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4) \
3253 LSS_LOADARGS_3(name, arg1, arg2, arg3); \
3254 __sc_6 = (unsigned long) (arg4)
3255 #undef LSS_LOADARGS_5
3256 #define LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5) \
3257 LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4); \
3258 __sc_7 = (unsigned long) (arg5)
3259 #undef LSS_LOADARGS_6
3260 #define LSS_LOADARGS_6(name, arg1, arg2, arg3, arg4, arg5, arg6) \
3261 LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5); \
3262 __sc_8 = (unsigned long) (arg6)
3263 #undef LSS_ASMINPUT_0
3264 #define LSS_ASMINPUT_0 "0" (__sc_0)
3265 #undef LSS_ASMINPUT_1
3266 #define LSS_ASMINPUT_1 LSS_ASMINPUT_0, "1" (__sc_3)
3267 #undef LSS_ASMINPUT_2
3268 #define LSS_ASMINPUT_2 LSS_ASMINPUT_1, "2" (__sc_4)
3269 #undef LSS_ASMINPUT_3
3270 #define LSS_ASMINPUT_3 LSS_ASMINPUT_2, "3" (__sc_5)
3271 #undef LSS_ASMINPUT_4
3272 #define LSS_ASMINPUT_4 LSS_ASMINPUT_3, "4" (__sc_6)
3273 #undef LSS_ASMINPUT_5
3274 #define LSS_ASMINPUT_5 LSS_ASMINPUT_4, "5" (__sc_7)
3275 #undef LSS_ASMINPUT_6
3276 #define LSS_ASMINPUT_6 LSS_ASMINPUT_5, "6" (__sc_8)
3277 #undef LSS_BODY
3278 #define LSS_BODY(nr, type, name, args...) \
3279 long __sc_ret, __sc_err; \
3280 { \
3281 register unsigned long __sc_0 __asm__ ("r0"); \
3282 register unsigned long __sc_3 __asm__ ("r3"); \
3283 register unsigned long __sc_4 __asm__ ("r4"); \
3284 register unsigned long __sc_5 __asm__ ("r5"); \
3285 register unsigned long __sc_6 __asm__ ("r6"); \
3286 register unsigned long __sc_7 __asm__ ("r7"); \
3287 register unsigned long __sc_8 __asm__ ("r8"); \
3288 \
3289 LSS_LOADARGS_##nr(name, args); \
3290 __asm__ __volatile__ \
3291 ("sc\n\t" \
3292 "mfcr %0" \
3293 : "=&r" (__sc_0), \
3294 "=&r" (__sc_3), "=&r" (__sc_4), \
3295 "=&r" (__sc_5), "=&r" (__sc_6), \
3296 "=&r" (__sc_7), "=&r" (__sc_8) \
3297 : LSS_ASMINPUT_##nr \
3298 : "cr0", "ctr", "memory", \
3299 "r9", "r10", "r11", "r12"); \
3300 __sc_ret = __sc_3; \
3301 __sc_err = __sc_0; \
3302 } \
3303 LSS_RETURN(type, __sc_ret, __sc_err)
3304 #undef _syscall0
3305 #define _syscall0(type, name) \
3306 type LSS_NAME(name)(void) { \
3307 LSS_BODY(0, type, name); \
3308 }
3309 #undef _syscall1
3310 #define _syscall1(type, name, type1, arg1) \
3311 type LSS_NAME(name)(type1 arg1) { \
3312 LSS_BODY(1, type, name, arg1); \
3313 }
3314 #undef _syscall2
3315 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3316 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3317 LSS_BODY(2, type, name, arg1, arg2); \
3318 }
3319 #undef _syscall3
3320 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3321 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3322 LSS_BODY(3, type, name, arg1, arg2, arg3); \
3323 }
3324 #undef _syscall4
3325 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3326 type4, arg4) \
3327 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3328 LSS_BODY(4, type, name, arg1, arg2, arg3, arg4); \
3329 }
3330 #undef _syscall5
3331 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3332 type4, arg4, type5, arg5) \
3333 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3334 type5 arg5) { \
3335 LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5); \
3336 }
3337 #undef _syscall6
3338 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3339 type4, arg4, type5, arg5, type6, arg6) \
3340 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3341 type5 arg5, type6 arg6) { \
3342 LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6); \
3343 }
3344 /* clone function adapted from glibc 2.3.6 clone.S */
3345 /* TODO(csilvers): consider wrapping some args up in a struct, like we
3346 * do for i386's _syscall6, so we can compile successfully on gcc 2.95
3347 */
3348 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3349 int flags, void *arg, int *parent_tidptr,
3350 void *newtls, int *child_tidptr) {
3351 long __ret, __err;
3352 {
3353 register int (*__fn)(void *) __asm__ ("r8") = fn;
3354 register void *__cstack __asm__ ("r4") = child_stack;
3355 register int __flags __asm__ ("r3") = flags;
3356 register void * __arg __asm__ ("r9") = arg;
3357 register int * __ptidptr __asm__ ("r5") = parent_tidptr;
3358 register void * __newtls __asm__ ("r6") = newtls;
3359 register int * __ctidptr __asm__ ("r7") = child_tidptr;
3360 __asm__ __volatile__(
3361 /* check for fn == NULL
3362 * and child_stack == NULL
3363 */
3364 "cmpwi cr0, %6, 0\n\t"
3365 "cmpwi cr1, %7, 0\n\t"
3366 "cror cr0*4+eq, cr1*4+eq, cr0*4+eq\n\t"
3367 "beq- cr0, 1f\n\t"
3368
3369 /* set up stack frame for child */
3370 "clrrwi %7, %7, 4\n\t"
3371 "li 0, 0\n\t"
3372 "stwu 0, -16(%7)\n\t"
3373
3374 /* fn, arg, child_stack are saved across the syscall: r28-30 */
3375 "mr 28, %6\n\t"
3376 "mr 29, %7\n\t"
3377 "mr 27, %9\n\t"
3378
3379 /* syscall */
3380 "li 0, %4\n\t"
3381 /* flags already in r3
3382 * child_stack already in r4
3383 * ptidptr already in r5
3384 * newtls already in r6
3385 * ctidptr already in r7
3386 */
3387 "sc\n\t"
3388
3389 /* Test if syscall was successful */
3390 "cmpwi cr1, 3, 0\n\t"
3391 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
3392 "bne- cr1, 1f\n\t"
3393
3394 /* Do the function call */
3395 "mtctr 28\n\t"
3396 "mr 3, 27\n\t"
3397 "bctrl\n\t"
3398
3399 /* Call _exit(r3) */
3400 "li 0, %5\n\t"
3401 "sc\n\t"
3402
3403 /* Return to parent */
3404 "1:\n"
3405 "mfcr %1\n\t"
3406 "mr %0, 3\n\t"
3407 : "=r" (__ret), "=r" (__err)
3408 : "0" (-1), "1" (EINVAL),
3409 "i" (__NR_clone), "i" (__NR_exit),
3410 "r" (__fn), "r" (__cstack), "r" (__flags),
3411 "r" (__arg), "r" (__ptidptr), "r" (__newtls),
3412 "r" (__ctidptr)
3413 : "cr0", "cr1", "memory", "ctr",
3414 "r0", "r29", "r27", "r28");
3415 }
3416 LSS_RETURN(int, __ret, __err);
3417 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04003418 #elif defined(__s390__)
3419 #undef LSS_REG
3420 #define LSS_REG(r, a) register unsigned long __r##r __asm__("r"#r) = (unsigned long) a
3421 #undef LSS_BODY
3422 #define LSS_BODY(type, name, args...) \
3423 register unsigned long __nr __asm__("r1") \
3424 = (unsigned long)(__NR_##name); \
3425 register long __res_r2 __asm__("r2"); \
3426 long __res; \
3427 __asm__ __volatile__ \
3428 ("svc 0\n\t" \
3429 : "=d"(__res_r2) \
3430 : "d"(__nr), ## args \
3431 : "memory"); \
3432 __res = __res_r2; \
3433 LSS_RETURN(type, __res)
3434 #undef _syscall0
3435 #define _syscall0(type, name) \
3436 type LSS_NAME(name)(void) { \
3437 LSS_BODY(type, name); \
3438 }
3439 #undef _syscall1
3440 #define _syscall1(type, name, type1, arg1) \
3441 type LSS_NAME(name)(type1 arg1) { \
3442 LSS_REG(2, arg1); \
3443 LSS_BODY(type, name, "0"(__r2)); \
3444 }
3445 #undef _syscall2
3446 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3447 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3448 LSS_REG(2, arg1); LSS_REG(3, arg2); \
3449 LSS_BODY(type, name, "0"(__r2), "d"(__r3)); \
3450 }
3451 #undef _syscall3
3452 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3453 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3454 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3455 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4)); \
3456 }
3457 #undef _syscall4
3458 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3459 type4, arg4) \
3460 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3461 type4 arg4) { \
3462 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3463 LSS_REG(5, arg4); \
3464 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3465 "d"(__r5)); \
3466 }
3467 #undef _syscall5
3468 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3469 type4, arg4, type5, arg5) \
3470 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3471 type4 arg4, type5 arg5) { \
3472 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3473 LSS_REG(5, arg4); LSS_REG(6, arg5); \
3474 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3475 "d"(__r5), "d"(__r6)); \
3476 }
3477 #undef _syscall6
3478 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3479 type4, arg4, type5, arg5, type6, arg6) \
3480 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, \
3481 type4 arg4, type5 arg5, type6 arg6) { \
3482 LSS_REG(2, arg1); LSS_REG(3, arg2); LSS_REG(4, arg3); \
3483 LSS_REG(5, arg4); LSS_REG(6, arg5); LSS_REG(7, arg6); \
3484 LSS_BODY(type, name, "0"(__r2), "d"(__r3), "d"(__r4), \
3485 "d"(__r5), "d"(__r6), "d"(__r7)); \
3486 }
3487 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3488 int flags, void *arg, int *parent_tidptr,
3489 void *newtls, int *child_tidptr) {
3490 long __ret;
3491 {
3492 register int (*__fn)(void *) __asm__ ("r1") = fn;
3493 register void *__cstack __asm__ ("r2") = child_stack;
3494 register int __flags __asm__ ("r3") = flags;
3495 register void *__arg __asm__ ("r0") = arg;
3496 register int *__ptidptr __asm__ ("r4") = parent_tidptr;
3497 register void *__newtls __asm__ ("r6") = newtls;
3498 register int *__ctidptr __asm__ ("r5") = child_tidptr;
3499 __asm__ __volatile__ (
3500 #ifndef __s390x__
3501 /* arg already in r0 */
3502 "ltr %4, %4\n\t" /* check fn, which is already in r1 */
3503 "jz 1f\n\t" /* NULL function pointer, return -EINVAL */
3504 "ltr %5, %5\n\t" /* check child_stack, which is already in r2 */
3505 "jz 1f\n\t" /* NULL stack pointer, return -EINVAL */
3506 /* flags already in r3 */
3507 /* parent_tidptr already in r4 */
3508 /* child_tidptr already in r5 */
3509 /* newtls already in r6 */
3510 "svc %2\n\t" /* invoke clone syscall */
3511 "ltr %0,%%r2\n\t" /* load return code into __ret and test */
3512 "jnz 1f\n\t" /* return to parent if non-zero */
3513 /* start child thread */
3514 "lr %%r2, %7\n\t" /* set first parameter to void *arg */
3515 "ahi %%r15, -96\n\t" /* make room on the stack for the save area */
3516 "xc 0(4,%%r15), 0(%%r15)\n\t"
3517 "basr %%r14, %4\n\t" /* jump to fn */
3518 "svc %3\n" /* invoke exit syscall */
3519 "1:\n"
3520 #else
3521 /* arg already in r0 */
3522 "ltgr %4, %4\n\t" /* check fn, which is already in r1 */
3523 "jz 1f\n\t" /* NULL function pointer, return -EINVAL */
3524 "ltgr %5, %5\n\t" /* check child_stack, which is already in r2 */
3525 "jz 1f\n\t" /* NULL stack pointer, return -EINVAL */
3526 /* flags already in r3 */
3527 /* parent_tidptr already in r4 */
3528 /* child_tidptr already in r5 */
3529 /* newtls already in r6 */
3530 "svc %2\n\t" /* invoke clone syscall */
3531 "ltgr %0, %%r2\n\t" /* load return code into __ret and test */
3532 "jnz 1f\n\t" /* return to parent if non-zero */
3533 /* start child thread */
3534 "lgr %%r2, %7\n\t" /* set first parameter to void *arg */
3535 "aghi %%r15, -160\n\t" /* make room on the stack for the save area */
3536 "xc 0(8,%%r15), 0(%%r15)\n\t"
3537 "basr %%r14, %4\n\t" /* jump to fn */
3538 "svc %3\n" /* invoke exit syscall */
3539 "1:\n"
3540 #endif
3541 : "=r" (__ret)
3542 : "0" (-EINVAL), "i" (__NR_clone), "i" (__NR_exit),
3543 "d" (__fn), "d" (__cstack), "d" (__flags), "d" (__arg),
3544 "d" (__ptidptr), "d" (__newtls), "d" (__ctidptr)
3545 : "cc", "r14", "memory"
3546 );
3547 }
3548 LSS_RETURN(int, __ret);
3549 }
Andreas Schwab1d387f42022-02-15 16:21:13 +01003550 #elif defined(__riscv) && __riscv_xlen == 64
3551 #undef LSS_REG
3552 #define LSS_REG(r,a) register int64_t __r##r __asm__("a"#r) = (int64_t)a
3553 #undef LSS_BODY
3554 #define LSS_BODY(type,name,args...) \
3555 register int64_t __res_a0 __asm__("a0"); \
3556 register int64_t __a7 __asm__("a7") = __NR_##name; \
3557 int64_t __res; \
3558 __asm__ __volatile__ ("scall\n" \
3559 : "=r"(__res_a0) \
3560 : "r"(__a7) , ## args \
3561 : "memory"); \
3562 __res = __res_a0; \
3563 LSS_RETURN(type, __res)
3564 #undef _syscall0
3565 #define _syscall0(type, name) \
3566 type LSS_NAME(name)(void) { \
3567 LSS_BODY(type, name); \
3568 }
3569 #undef _syscall1
3570 #define _syscall1(type, name, type1, arg1) \
3571 type LSS_NAME(name)(type1 arg1) { \
3572 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
3573 }
3574 #undef _syscall2
3575 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3576 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3577 LSS_REG(0, arg1); LSS_REG(1, arg2); \
3578 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
3579 }
3580 #undef _syscall3
3581 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3582 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3583 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3584 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
3585 }
3586 #undef _syscall4
3587 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
3588 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3589 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3590 LSS_REG(3, arg4); \
3591 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
3592 }
3593 #undef _syscall5
3594 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3595 type5,arg5) \
3596 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3597 type5 arg5) { \
3598 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3599 LSS_REG(3, arg4); LSS_REG(4, arg5); \
3600 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
3601 "r"(__r4)); \
3602 }
3603 #undef _syscall6
3604 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3605 type5,arg5,type6,arg6) \
3606 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3607 type5 arg5, type6 arg6) { \
3608 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3609 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
3610 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
3611 "r"(__r4), "r"(__r5)); \
3612 }
3613
3614 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3615 int flags, void *arg, int *parent_tidptr,
3616 void *newtls, int *child_tidptr) {
3617 int64_t __res;
3618 {
3619 register int64_t __res_a0 __asm__("a0");
3620 register uint64_t __flags __asm__("a0") = flags;
3621 register void *__stack __asm__("a1") = child_stack;
3622 register void *__ptid __asm__("a2") = parent_tidptr;
3623 register void *__tls __asm__("a3") = newtls;
3624 register int *__ctid __asm__("a4") = child_tidptr;
3625 __asm__ __volatile__(/* Push "arg" and "fn" onto the stack that will be
3626 * used by the child.
3627 */
3628 "addi %2,%2,-16\n"
3629 "sd %1, 0(%2)\n"
3630 "sd %4, 8(%2)\n"
3631
3632 /* %a0 = syscall(%a0 = flags,
3633 * %a1 = child_stack,
3634 * %a2 = parent_tidptr,
3635 * %a3 = newtls,
3636 * %a4 = child_tidptr)
3637 */
3638 "li a7, %8\n"
3639 "scall\n"
3640
3641 /* if (%a0 != 0)
3642 * return %a0;
3643 */
3644 "bnez %0, 1f\n"
3645
3646 /* In the child, now. Call "fn(arg)".
3647 */
3648 "ld a1, 0(sp)\n"
3649 "ld a0, 8(sp)\n"
3650 "jalr a1\n"
3651
3652 /* Call _exit(%a0).
3653 */
3654 "li a7, %9\n"
3655 "scall\n"
3656 "1:\n"
3657 : "=r" (__res_a0)
3658 : "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
3659 "r"(__ptid), "r"(__tls), "r"(__ctid),
3660 "i"(__NR_clone), "i"(__NR_exit)
3661 : "cc", "memory");
3662 __res = __res_a0;
3663 }
3664 LSS_RETURN(int, __res);
3665 }
Konstantin Ivlev8007b272021-01-27 18:27:42 +03003666 #elif defined(__e2k__)
3667
3668 #undef _LSS_BODY
3669 #define _LSS_BODY(nr, type, name, ...) \
3670 register unsigned long long __res; \
3671 __asm__ __volatile__ \
3672 ( \
3673 "{\n\t" \
3674 " sdisp %%ctpr1, 0x3\n\t" \
3675 " addd, s 0x0, %[sys_num], %%b[0]\n\t" \
3676 LSS_BODY_ASM##nr \
3677 "}\n\t" \
3678 "{\n\t" \
3679 " call %%ctpr1, wbs = %#\n\t" \
3680 "}\n\t" \
3681 "{\n\t" \
3682 " addd, s 0x0, %%b[0], %[res]\n\t" \
3683 "}\n\t" \
3684 : [res] "=r" (__res) \
3685 : \
3686 LSS_BODY_ARG##nr(__VA_ARGS__) \
3687 [sys_num] "ri" (__NR_##name) \
3688 : "ctpr1", "ctpr2", "ctpr3", \
3689 "b[0]", "b[1]", "b[2]", "b[3]", \
3690 "b[4]", "b[5]", "b[6]", "b[7]" \
3691 ); \
3692 LSS_RETURN(type, __res);
3693
3694 #undef LSS_BODY
3695 #define LSS_BODY(nr, type, name, args...) \
3696 _LSS_BODY(nr, type, name, ## args)
3697
3698 #undef LSS_BODY_ASM0
3699 #undef LSS_BODY_ASM1
3700 #undef LSS_BODY_ASM2
3701 #undef LSS_BODY_ASM3
3702 #undef LSS_BODY_ASM4
3703 #undef LSS_BODY_ASM5
3704 #undef LSS_BODY_ASM6
3705
3706 #define LSS_BODY_ASM0
3707 #define LSS_BODY_ASM1 LSS_BODY_ASM0 \
3708 " addd, s 0x0, %[arg1], %%b[1]\n\t"
3709 #define LSS_BODY_ASM2 LSS_BODY_ASM1 \
3710 " addd, s 0x0, %[arg2], %%b[2]\n\t"
3711 #define LSS_BODY_ASM3 LSS_BODY_ASM2 \
3712 " addd, s 0x0, %[arg3], %%b[3]\n\t"
3713 #define LSS_BODY_ASM4 LSS_BODY_ASM3 \
3714 " addd, s 0x0, %[arg4], %%b[4]\n\t"
3715 #define LSS_BODY_ASM5 LSS_BODY_ASM4 \
3716 " addd, s 0x0, %[arg5], %%b[5]\n\t"
3717 #define LSS_BODY_ASM6 LSS_BODY_ASM5 \
3718 "}\n\t" \
3719 "{\n\t" \
3720 " addd, s 0x0, %[arg6], %%b[6]\n\t"
3721
3722 #undef LSS_SYSCALL_ARG
3723 #define LSS_SYSCALL_ARG(a) ((unsigned long long)(uintptr_t)(a))
3724
3725 #undef LSS_BODY_ARG0
3726 #undef LSS_BODY_ARG1
3727 #undef LSS_BODY_ARG2
3728 #undef LSS_BODY_ARG3
3729 #undef LSS_BODY_ARG4
3730 #undef LSS_BODY_ARG5
3731 #undef LSS_BODY_ARG6
3732
3733 #define LSS_BODY_ARG0()
3734 #define LSS_BODY_ARG1(_arg1) \
3735 [arg1] "ri" LSS_SYSCALL_ARG(_arg1),
3736 #define LSS_BODY_ARG2(_arg1, _arg2) \
3737 LSS_BODY_ARG1(_arg1) \
3738 [arg2] "ri" LSS_SYSCALL_ARG(_arg2),
3739 #define LSS_BODY_ARG3(_arg1, _arg2, _arg3) \
3740 LSS_BODY_ARG2(_arg1, _arg2) \
3741 [arg3] "ri" LSS_SYSCALL_ARG(_arg3),
3742 #define LSS_BODY_ARG4(_arg1, _arg2, _arg3, _arg4) \
3743 LSS_BODY_ARG3(_arg1, _arg2, _arg3) \
3744 [arg4] "ri" LSS_SYSCALL_ARG(_arg4),
3745 #define LSS_BODY_ARG5(_arg1, _arg2, _arg3, _arg4, _arg5) \
3746 LSS_BODY_ARG4(_arg1, _arg2, _arg3, _arg4) \
3747 [arg5] "ri" LSS_SYSCALL_ARG(_arg5),
3748 #define LSS_BODY_ARG6(_arg1, _arg2, _arg3, _arg4, _arg5, _arg6) \
3749 LSS_BODY_ARG5(_arg1, _arg2, _arg3, _arg4, _arg5) \
3750 [arg6] "ri" LSS_SYSCALL_ARG(_arg6),
3751
3752 #undef _syscall0
3753 #define _syscall0(type, name) \
3754 type LSS_NAME(name)(void) { \
3755 LSS_BODY(0, type, name); \
3756 }
3757
3758 #undef _syscall1
3759 #define _syscall1(type, name, type1, arg1) \
3760 type LSS_NAME(name)(type1 arg1) { \
3761 LSS_BODY(1, type, name, arg1) \
3762 }
3763
3764 #undef _syscall2
3765 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3766 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3767 LSS_BODY(2, type, name, arg1, arg2) \
3768 }
3769
3770 #undef _syscall3
3771 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3772 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3773 LSS_BODY(3, type, name, arg1, arg2, arg3) \
3774 }
3775
3776 #undef _syscall4
3777 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
3778 type4, arg4) \
3779 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3780 LSS_BODY(4, type, name, arg1, arg2, arg3, arg4) \
3781 }
3782
3783 #undef _syscall5
3784 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
3785 type4, arg4, type5, arg5) \
3786 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3787 type5 arg5) { \
3788 LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5) \
3789 }
3790
3791 #undef _syscall6
3792 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
3793 type4, arg4, type5, arg5, type6, arg6) \
3794 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3795 type5 arg5, type6 arg6) { \
3796 LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6) \
3797 }
3798
3799 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3800 int flags, void *arg, int *parent_tidptr,
3801 void *newtls, int *child_tidptr) {
3802 unsigned long long __res;
3803
3804 __asm__ __volatile__ (
3805 "{\n\t"
3806 " addd,s 0x0, %[nr_clone], %%b[0]\n\t"
3807 " addd,s 0x0, %[flags], %%db[1]\n\t"
3808 " addd,s 0x0, %[child_stack], %%db[2]\n\t"
3809 " addd,s 0x0, %[parent_tidptr], %%db[3]\n\t"
3810 " addd,s 0x0, %[child_tidptr], %%db[4]\n\t"
3811 " addd,s 0x0, %[newtls], %%db[5]\n\t"
3812 "}\n\t"
3813 /* if (fn == NULL)
3814 * return -EINVAL;
3815 */
3816
3817 "{\n\t"
3818 " disp %%ctpr1, .L1\n\t"
3819 "}\n\t"
3820 "{\n\t"
3821 " cmpesb,s 0x0, %[fn], %%pred0\n\t"
3822 "}\n\t"
3823 "{\n\t"
3824 " ct %%ctpr1 ? %%pred0\n\t"
3825 "}\n\t"
3826
3827 /* if (child_stack == NULL)
3828 * return -EINVAL;
3829 */
3830 "{\n\t"
3831 " cmpesb,s 0x0, %%db[2], %%pred0\n\t"
3832 "}\n\t"
3833 "{\n\t"
3834 " ct %%ctpr1 ? %%pred0\n\t"
3835 "}\n\t"
3836
3837 /* b[0] = syscall(%b[0] = __NR_clone,
3838 * %db[1] = flags,
3839 * %db[2] = child_stack,
3840 * %db[3] = parent_tidptr,
3841 * %db[4] = child_tidptr,
3842 * %db[5] = newtls)
3843 */
3844 "{\n\t"
3845 " sdisp %%ctpr1, 0x3\n\t"
3846 "}\n\t"
3847 "{\n\t"
3848 " call %%ctpr1, wbs = %#\n\t"
3849 "}\n\t"
3850
3851 /* if (%[b0] != 0)
3852 * return %b[0];
3853 */
3854 "{\n\t"
3855 " disp %%ctpr1, .L2\n\t"
3856 " cmpesb,s 0x0, %%b[0], %%pred0\n\t"
3857 "}\n\t"
3858 "{\n\t"
3859 " ct %%ctpr1 ? ~%%pred0\n\t"
3860 "}\n\t"
3861 /* In the child, now. Call "fn(arg)".
3862 */
3863
3864 "{\n\t"
3865 " movtd,s %[fn], %%ctpr1\n\t"
3866 "}\n\t"
3867 "{\n\t"
3868 " addd,s 0x0, %[arg], %%db[0]\n\t"
3869 "}\n\t"
3870 "{\n\t"
3871 " call %%ctpr1, wbs = %#\n\t"
3872 "}\n\t"
3873 /* Call _exit(%b[0]).
3874 */
3875
3876 "{\n\t"
3877 " sdisp %%ctpr1, 0x3\n\t"
3878 " addd,s 0x0, %%b[0], %%b[1]\n\t"
3879 "}\n\t"
3880 "{\n\t"
3881 " addd,s 0x0, %[nr_exit], %%b[0]\n\t"
3882 "}\n\t"
3883 "{\n\t"
3884 " call %%ctpr1, wbs = %#\n\t"
3885 "}\n\t"
3886 "{\n\t"
3887 " disp %%ctpr1, .L2\n\t"
3888 " adds,s 0x0, 0x0, %%b[0]\n\t"
3889 "}\n\t"
3890 "{\n\t"
3891 " ct %%ctpr1\n\t"
3892 "}\n\t"
3893 ".L1:\n\t"
3894 "{\n\t"
3895 " addd,s 0x0, %[einval], %%b[0]\n\t"
3896 "}\n\t"
3897 ".L2:\n\t"
3898 "{\n\t"
3899 " addd,s 0x0, %%b[0], %[res]\n\t"
3900 "}\n\t"
3901 : [res] "=r" LSS_SYSCALL_ARG(__res)
3902 : [nr_clone] "ri" LSS_SYSCALL_ARG(__NR_clone)
3903 [arg] "ri" LSS_SYSCALL_ARG(arg)
3904 [nr_exit] "ri" LSS_SYSCALL_ARG(__NR_exit)
3905 [flags] "ri" LSS_SYSCALL_ARG(flags)
3906 [child_stack] "ri" LSS_SYSCALL_ARG(child_stack)
3907 [parent_tidptr] "ri"
3908 LSS_SYSCALL_ARG(parent_tidptr)
3909 [newtls] "ri" LSS_SYSCALL_ARG(newtls)
3910 [child_tidptr] "ri"
3911 LSS_SYSCALL_ARG(child_tidptr)
3912 [fn] "ri" LSS_SYSCALL_ARG(fn)
3913 [einval] "ri" LSS_SYSCALL_ARG(-EINVAL)
3914 : "ctpr1", "b[0]", "b[1]", "b[2]", "b[3]",
3915 "b[4]", "b[5]", "pred0");
3916 LSS_RETURN(int, __res);
3917 }
mingtaoxt xtc0c96892022-08-11 16:53:21 +08003918 #elif defined(__loongarch_lp64)
3919 /* Most definitions of _syscallX() neglect to mark "memory" as being
3920 * clobbered. This causes problems with compilers, that do a better job
3921 * at optimizing across __asm__ calls.
3922 * So, we just have to redefine all of the _syscallX() macros.
3923 */
3924 #undef LSS_REG
3925 #define LSS_REG(ar,a) register int64_t __r##ar __asm__("a"#ar) = (int64_t)a
3926 /* syscall is like subroutine calls, all caller-saved registers may be
3927 * clobbered, we should add them to the |Clobbers| list.
3928 * a0 is not included because it's in the output list.
3929 */
3930 #define LSS_SYSCALL_CLOBBERS "t0", "t1", "t2", "t3", "t4", "t5", "t6", \
3931 "t7", "t8", "memory"
3932 #undef LSS_BODY
3933 #define LSS_BODY(type,name,args...) \
3934 register int64_t __res_a0 __asm__("a0"); \
3935 int64_t __res; \
3936 __asm__ __volatile__ ("li.d $a7, %1\n" \
3937 "syscall 0x0\n" \
3938 : "=r"(__res_a0) \
3939 : "i"(__NR_##name) , ## args \
3940 : LSS_SYSCALL_CLOBBERS); \
3941 __res = __res_a0; \
3942 LSS_RETURN(type, __res)
3943 #undef _syscall0
3944 #define _syscall0(type, name) \
3945 type LSS_NAME(name)(void) { \
3946 LSS_BODY(type, name); \
3947 }
3948 #undef _syscall1
3949 #define _syscall1(type, name, type1, arg1) \
3950 type LSS_NAME(name)(type1 arg1) { \
3951 LSS_REG(0, arg1); LSS_BODY(type, name, "r"(__r0)); \
3952 }
3953 #undef _syscall2
3954 #define _syscall2(type, name, type1, arg1, type2, arg2) \
3955 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
3956 LSS_REG(0, arg1); LSS_REG(1, arg2); \
3957 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
3958 }
3959 #undef _syscall3
3960 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
3961 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
3962 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3963 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
3964 }
3965 #undef _syscall4
3966 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
3967 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
3968 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3969 LSS_REG(3, arg4); \
3970 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
3971 }
3972 #undef _syscall5
3973 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3974 type5,arg5) \
3975 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3976 type5 arg5) { \
3977 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3978 LSS_REG(3, arg4); LSS_REG(4, arg5); \
3979 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
3980 "r"(__r4)); \
3981 }
3982 #undef _syscall6
3983 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
3984 type5,arg5,type6,arg6) \
3985 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
3986 type5 arg5, type6 arg6) { \
3987 LSS_REG(0, arg1); LSS_REG(1, arg2); LSS_REG(2, arg3); \
3988 LSS_REG(3, arg4); LSS_REG(4, arg5); LSS_REG(5, arg6); \
3989 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
3990 "r"(__r4), "r"(__r5)); \
3991 }
3992
3993 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
3994 int flags, void *arg, int *parent_tidptr,
3995 void *newtls, int *child_tidptr) {
3996 int64_t __res;
3997 {
3998 register int64_t __res_a0 __asm__("a0");
3999 register uint64_t __flags __asm__("a0") = flags;
4000 register void *__stack __asm__("a1") = child_stack;
4001 register void *__ptid __asm__("a2") = parent_tidptr;
4002 register void *__tls __asm__("a3") = newtls;
4003 register int *__ctid __asm__("a4") = child_tidptr;
4004 __asm__ __volatile__(/* Push "arg" and "fn" onto the stack that will be
4005 * used by the child.
4006 */
4007 "addi.d %2, %2, -16\n"
4008 "st.d %1, %2, 8\n"
4009 "st.d %4, %2, 0\n"
4010
4011 /* %a0 = syscall(%a0 = flags,
4012 * %a1 = child_stack,
4013 * %a2 = parent_tidptr,
4014 * %a3 = newtls,
4015 * %a4 = child_tidptr)
4016 */
4017 "li.d $a7, %8\n"
4018 "syscall 0x0\n"
4019
4020 /* if (%a0 != 0)
4021 * return %a0;
4022 */
4023 "bnez $a0, 1f\n"
4024
4025 /* In the child, now. Call "fn(arg)".
4026 */
4027 "ld.d $a0, $sp, 0\n"
4028 "ld.d $a1, $sp, 8\n"
4029 "addi.d $sp, $sp, 16\n"
4030 "jirl $ra, $a1, 0\n"
4031
4032 /* Call _exit(%a0).
4033 */
4034 "li.d $a7, %9\n"
4035 "syscall 0x0\n"
4036 "1:\n"
4037 : "=r" (__res_a0)
4038 : "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
4039 "r"(__ptid), "r"(__tls), "r"(__ctid),
4040 "i"(__NR_clone), "i"(__NR_exit)
4041 : LSS_SYSCALL_CLOBBERS);
4042 __res = __res_a0;
4043 }
4044 LSS_RETURN(int, __res);
4045 }
Konstantin Ivlev8007b272021-01-27 18:27:42 +03004046
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004047 #endif
4048 #define __NR__exit __NR_exit
4049 #define __NR__gettid __NR_gettid
4050 #define __NR__mremap __NR_mremap
phosek@chromium.orga9c02722013-08-16 17:31:42 +00004051 LSS_INLINE _syscall1(void *, brk, void *, e)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004052 LSS_INLINE _syscall1(int, chdir, const char *,p)
4053 LSS_INLINE _syscall1(int, close, int, f)
4054 LSS_INLINE _syscall2(int, clock_getres, int, c,
4055 struct kernel_timespec*, t)
4056 LSS_INLINE _syscall2(int, clock_gettime, int, c,
4057 struct kernel_timespec*, t)
4058 LSS_INLINE _syscall1(int, dup, int, f)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004059 #if defined(__NR_dup2)
4060 // dup2 is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004061 LSS_INLINE _syscall2(int, dup2, int, s,
4062 int, d)
4063 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004064 #if defined(__NR_dup3)
4065 LSS_INLINE _syscall3(int, dup3, int, s, int, d, int, f)
4066 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004067 LSS_INLINE _syscall3(int, execve, const char*, f,
4068 const char*const*,a,const char*const*, e)
4069 LSS_INLINE _syscall1(int, _exit, int, e)
4070 LSS_INLINE _syscall1(int, exit_group, int, e)
4071 LSS_INLINE _syscall3(int, fcntl, int, f,
4072 int, c, long, a)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004073 #if defined(__NR_fork)
4074 // fork is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004075 LSS_INLINE _syscall0(pid_t, fork)
4076 #endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004077 #if defined(__NR_fstat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004078 LSS_INLINE _syscall2(int, fstat, int, f,
4079 struct kernel_stat*, b)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004080 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004081 LSS_INLINE _syscall2(int, fstatfs, int, f,
4082 struct kernel_statfs*, b)
vapier@chromium.org2273e812013-04-01 17:52:44 +00004083 #if defined(__x86_64__)
4084 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
4085 LSS_INLINE int LSS_NAME(ftruncate)(int f, off_t l) {
4086 LSS_BODY(2, int, ftruncate, LSS_SYSCALL_ARG(f), (uint64_t)(l));
4087 }
4088 #else
4089 LSS_INLINE _syscall2(int, ftruncate, int, f,
4090 off_t, l)
4091 #endif
Mike Frysinger171a36a2019-01-26 23:05:43 -05004092 LSS_INLINE _syscall6(int, futex, int*, u,
4093 int, o, int, v,
4094 struct kernel_timespec*, t,
4095 int*, u2, int, v2)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004096 LSS_INLINE _syscall3(int, getdents, int, f,
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004097 struct kernel_dirent*, d, int, c)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004098 LSS_INLINE _syscall3(int, getdents64, int, f,
4099 struct kernel_dirent64*, d, int, c)
4100 LSS_INLINE _syscall0(gid_t, getegid)
4101 LSS_INLINE _syscall0(uid_t, geteuid)
Doug Kwan32a80cd2022-07-01 17:52:39 +00004102 LSS_INLINE _syscall2(int, getitimer, int, w,
4103 struct kernel_itimerval*, c)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004104 #if defined(__NR_getpgrp)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004105 LSS_INLINE _syscall0(pid_t, getpgrp)
4106 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004107 LSS_INLINE _syscall0(pid_t, getpid)
4108 LSS_INLINE _syscall0(pid_t, getppid)
4109 LSS_INLINE _syscall2(int, getpriority, int, a,
4110 int, b)
4111 LSS_INLINE _syscall3(int, getresgid, gid_t *, r,
4112 gid_t *, e, gid_t *, s)
4113 LSS_INLINE _syscall3(int, getresuid, uid_t *, r,
4114 uid_t *, e, uid_t *, s)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004115 #if defined(__NR_getrlimit)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004116 LSS_INLINE _syscall2(int, getrlimit, int, r,
4117 struct kernel_rlimit*, l)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004118 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004119 LSS_INLINE _syscall1(pid_t, getsid, pid_t, p)
4120 LSS_INLINE _syscall0(pid_t, _gettid)
4121 LSS_INLINE _syscall2(pid_t, gettimeofday, struct kernel_timeval*, t,
4122 void*, tz)
4123 LSS_INLINE _syscall5(int, setxattr, const char *,p,
4124 const char *, n, const void *,v,
4125 size_t, s, int, f)
4126 LSS_INLINE _syscall5(int, lsetxattr, const char *,p,
4127 const char *, n, const void *,v,
4128 size_t, s, int, f)
4129 LSS_INLINE _syscall4(ssize_t, getxattr, const char *,p,
4130 const char *, n, void *, v, size_t, s)
4131 LSS_INLINE _syscall4(ssize_t, lgetxattr, const char *,p,
4132 const char *, n, void *, v, size_t, s)
4133 LSS_INLINE _syscall3(ssize_t, listxattr, const char *,p,
4134 char *, l, size_t, s)
4135 LSS_INLINE _syscall3(ssize_t, llistxattr, const char *,p,
4136 char *, l, size_t, s)
4137 LSS_INLINE _syscall3(int, ioctl, int, d,
4138 int, r, void *, a)
4139 LSS_INLINE _syscall2(int, ioprio_get, int, which,
4140 int, who)
4141 LSS_INLINE _syscall3(int, ioprio_set, int, which,
4142 int, who, int, ioprio)
4143 LSS_INLINE _syscall2(int, kill, pid_t, p,
4144 int, s)
vapier@chromium.org2273e812013-04-01 17:52:44 +00004145 #if defined(__x86_64__)
4146 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
4147 LSS_INLINE off_t LSS_NAME(lseek)(int f, off_t o, int w) {
4148 _LSS_BODY(3, off_t, lseek, off_t, LSS_SYSCALL_ARG(f), (uint64_t)(o),
4149 LSS_SYSCALL_ARG(w));
4150 }
4151 #else
4152 LSS_INLINE _syscall3(off_t, lseek, int, f,
4153 off_t, o, int, w)
4154 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004155 LSS_INLINE _syscall2(int, munmap, void*, s,
4156 size_t, l)
4157 LSS_INLINE _syscall6(long, move_pages, pid_t, p,
4158 unsigned long, n, void **,g, int *, d,
4159 int *, s, int, f)
4160 LSS_INLINE _syscall3(int, mprotect, const void *,a,
4161 size_t, l, int, p)
4162 LSS_INLINE _syscall5(void*, _mremap, void*, o,
4163 size_t, os, size_t, ns,
4164 unsigned long, f, void *, a)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004165 #if defined(__NR_open)
4166 // open is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004167 LSS_INLINE _syscall3(int, open, const char*, p,
4168 int, f, int, m)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004169 #endif
4170 #if defined(__NR_poll)
4171 // poll is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004172 LSS_INLINE _syscall3(int, poll, struct kernel_pollfd*, u,
4173 unsigned int, n, int, t)
4174 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004175 #if defined(__NR_ppoll)
4176 LSS_INLINE _syscall5(int, ppoll, struct kernel_pollfd *, u,
4177 unsigned int, n, const struct kernel_timespec *, t,
4178 const struct kernel_sigset_t *, sigmask, size_t, s)
4179 #endif
mseaborn@chromium.orge6c76822013-08-31 00:08:44 +00004180 LSS_INLINE _syscall5(int, prctl, int, option,
4181 unsigned long, arg2,
4182 unsigned long, arg3,
4183 unsigned long, arg4,
4184 unsigned long, arg5)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004185 LSS_INLINE _syscall4(long, ptrace, int, r,
4186 pid_t, p, void *, a, void *, d)
4187 #if defined(__NR_quotactl)
4188 // Defined on x86_64 / i386 only
4189 LSS_INLINE _syscall4(int, quotactl, int, cmd, const char *, special,
4190 int, id, caddr_t, addr)
4191 #endif
4192 LSS_INLINE _syscall3(ssize_t, read, int, f,
4193 void *, b, size_t, c)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004194 #if defined(__NR_readlink)
4195 // readlink is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004196 LSS_INLINE _syscall3(int, readlink, const char*, p,
4197 char*, b, size_t, s)
4198 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004199 #if defined(__NR_readlinkat)
4200 LSS_INLINE _syscall4(int, readlinkat, int, d, const char *, p, char *, b,
4201 size_t, s)
4202 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004203 LSS_INLINE _syscall4(int, rt_sigaction, int, s,
4204 const struct kernel_sigaction*, a,
4205 struct kernel_sigaction*, o, size_t, c)
4206 LSS_INLINE _syscall2(int, rt_sigpending, struct kernel_sigset_t *, s,
4207 size_t, c)
4208 LSS_INLINE _syscall4(int, rt_sigprocmask, int, h,
4209 const struct kernel_sigset_t*, s,
4210 struct kernel_sigset_t*, o, size_t, c)
4211 LSS_INLINE _syscall2(int, rt_sigsuspend,
4212 const struct kernel_sigset_t*, s, size_t, c)
Joshua Peraza726d71e2019-11-13 12:21:13 -08004213 LSS_INLINE _syscall4(int, rt_sigtimedwait, const struct kernel_sigset_t*, s,
4214 siginfo_t*, i, const struct timespec*, t, size_t, c)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004215 LSS_INLINE _syscall3(int, sched_getaffinity,pid_t, p,
4216 unsigned int, l, unsigned long *, m)
4217 LSS_INLINE _syscall3(int, sched_setaffinity,pid_t, p,
4218 unsigned int, l, unsigned long *, m)
4219 LSS_INLINE _syscall0(int, sched_yield)
4220 LSS_INLINE _syscall1(long, set_tid_address, int *, t)
4221 LSS_INLINE _syscall1(int, setfsgid, gid_t, g)
4222 LSS_INLINE _syscall1(int, setfsuid, uid_t, u)
4223 LSS_INLINE _syscall1(int, setuid, uid_t, u)
4224 LSS_INLINE _syscall1(int, setgid, gid_t, g)
Doug Kwan32a80cd2022-07-01 17:52:39 +00004225 LSS_INLINE _syscall3(int, setitimer, int, w,
4226 const struct kernel_itimerval*, n,
4227 struct kernel_itimerval*, o)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004228 LSS_INLINE _syscall2(int, setpgid, pid_t, p,
4229 pid_t, g)
4230 LSS_INLINE _syscall3(int, setpriority, int, a,
4231 int, b, int, p)
4232 LSS_INLINE _syscall3(int, setresgid, gid_t, r,
4233 gid_t, e, gid_t, s)
4234 LSS_INLINE _syscall3(int, setresuid, uid_t, r,
4235 uid_t, e, uid_t, s)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004236 #if defined(__NR_setrlimit)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004237 LSS_INLINE _syscall2(int, setrlimit, int, r,
4238 const struct kernel_rlimit*, l)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004239 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004240 LSS_INLINE _syscall0(pid_t, setsid)
4241 LSS_INLINE _syscall2(int, sigaltstack, const stack_t*, s,
4242 const stack_t*, o)
4243 #if defined(__NR_sigreturn)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004244 LSS_INLINE _syscall1(int, sigreturn, unsigned long, u)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004245 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004246 #if defined(__NR_stat)
Matthew Denton92a65a82021-04-01 13:00:07 -07004247 // stat and lstat are polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004248 LSS_INLINE _syscall2(int, stat, const char*, f,
4249 struct kernel_stat*, b)
4250 #endif
Matthew Denton92a65a82021-04-01 13:00:07 -07004251 #if defined(__NR_lstat)
4252 LSS_INLINE _syscall2(int, lstat, const char*, f,
4253 struct kernel_stat*, b)
4254 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004255 LSS_INLINE _syscall2(int, statfs, const char*, f,
4256 struct kernel_statfs*, b)
4257 LSS_INLINE _syscall3(int, tgkill, pid_t, p,
4258 pid_t, t, int, s)
4259 LSS_INLINE _syscall2(int, tkill, pid_t, p,
4260 int, s)
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004261 #if defined(__NR_unlink)
4262 // unlink is polyfilled below when not available.
anton@chromium.org2f724fc2014-04-15 13:05:20 +00004263 LSS_INLINE _syscall1(int, unlink, const char*, f)
4264 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004265 LSS_INLINE _syscall3(ssize_t, write, int, f,
4266 const void *, b, size_t, c)
4267 LSS_INLINE _syscall3(ssize_t, writev, int, f,
4268 const struct kernel_iovec*, v, size_t, c)
4269 #if defined(__NR_getcpu)
4270 LSS_INLINE _syscall3(long, getcpu, unsigned *, cpu,
zodiac@gmail.comdb39de92010-12-10 00:22:03 +00004271 unsigned *, node, void *, unused)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004272 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004273 #if defined(__NR_fadvise64)
4274 #if defined(__x86_64__)
4275 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
4276 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset, loff_t len,
4277 int advice) {
4278 LSS_BODY(4, int, fadvise64, LSS_SYSCALL_ARG(fd), (uint64_t)(offset),
4279 (uint64_t)(len), LSS_SYSCALL_ARG(advice));
4280 }
4281 #else
4282 LSS_INLINE _syscall4(int, fadvise64,
4283 int, fd, loff_t, offset, loff_t, len, int, advice)
4284 #endif
4285 #elif defined(__i386__)
4286 #define __NR__fadvise64_64 __NR_fadvise64_64
4287 LSS_INLINE _syscall6(int, _fadvise64_64, int, fd,
4288 unsigned, offset_lo, unsigned, offset_hi,
4289 unsigned, len_lo, unsigned, len_hi,
4290 int, advice)
4291
4292 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
4293 loff_t len, int advice) {
4294 return LSS_NAME(_fadvise64_64)(fd,
4295 (unsigned)offset, (unsigned)(offset >>32),
4296 (unsigned)len, (unsigned)(len >> 32),
4297 advice);
4298 }
4299
4300 #elif defined(__s390__) && !defined(__s390x__)
4301 #define __NR__fadvise64_64 __NR_fadvise64_64
4302 struct kernel_fadvise64_64_args {
4303 int fd;
4304 long long offset;
4305 long long len;
4306 int advice;
4307 };
4308
4309 LSS_INLINE _syscall1(int, _fadvise64_64,
4310 struct kernel_fadvise64_64_args *args)
4311
4312 LSS_INLINE int LSS_NAME(fadvise64)(int fd, loff_t offset,
4313 loff_t len, int advice) {
4314 struct kernel_fadvise64_64_args args = { fd, offset, len, advice };
4315 return LSS_NAME(_fadvise64_64)(&args);
4316 }
4317 #endif
4318 #if defined(__NR_fallocate)
4319 #if defined(__x86_64__)
vapier@chromium.org2273e812013-04-01 17:52:44 +00004320 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
4321 LSS_INLINE int LSS_NAME(fallocate)(int f, int mode, loff_t offset,
4322 loff_t len) {
4323 LSS_BODY(4, int, fallocate, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(mode),
4324 (uint64_t)(offset), (uint64_t)(len));
4325 }
Joshua Peraza7bde79c2019-12-05 11:36:48 -08004326 #elif (defined(__i386__) || (defined(__s390__) && !defined(__s390x__)) \
4327 || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) \
4328 || (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) \
4329 || defined(__PPC__))
Bryan Chan3f6478a2016-06-14 08:38:17 -04004330 #define __NR__fallocate __NR_fallocate
4331 LSS_INLINE _syscall6(int, _fallocate, int, fd,
4332 int, mode,
4333 unsigned, offset_lo, unsigned, offset_hi,
4334 unsigned, len_lo, unsigned, len_hi)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004335
Bryan Chan3f6478a2016-06-14 08:38:17 -04004336 LSS_INLINE int LSS_NAME(fallocate)(int fd, int mode,
4337 loff_t offset, loff_t len) {
4338 union { loff_t off; unsigned w[2]; } o = { offset }, l = { len };
4339 return LSS_NAME(_fallocate)(fd, mode, o.w[0], o.w[1], l.w[0], l.w[1]);
4340 }
4341 #else
4342 LSS_INLINE _syscall4(int, fallocate,
4343 int, f, int, mode, loff_t, offset, loff_t, len)
4344 #endif
4345 #endif
Chris Palmer29f7c7e2020-08-12 17:10:59 -07004346 #if defined(__NR_getrandom)
4347 LSS_INLINE _syscall3(ssize_t, getrandom, void*, buffer, size_t, length,
4348 unsigned int, flags)
4349 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004350 #if defined(__NR_newfstatat)
4351 LSS_INLINE _syscall4(int, newfstatat, int, d,
4352 const char *, p,
4353 struct kernel_stat*, b, int, f)
4354 #endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004355 #if defined(__NR_statx)
4356 LSS_INLINE _syscall5(int, statx, int, d,
4357 const char *, p,
4358 int, f, int, m,
4359 struct kernel_statx*, b)
4360 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004361 #if defined(__x86_64__) || defined(__s390x__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004362 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
4363 gid_t *egid,
4364 gid_t *sgid) {
4365 return LSS_NAME(getresgid)(rgid, egid, sgid);
4366 }
4367
4368 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
4369 uid_t *euid,
4370 uid_t *suid) {
4371 return LSS_NAME(getresuid)(ruid, euid, suid);
4372 }
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004373
4374 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
4375 return LSS_NAME(setfsgid)(gid);
4376 }
4377
4378 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
4379 return LSS_NAME(setfsuid)(uid);
4380 }
4381
4382 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
4383 return LSS_NAME(setresgid)(rgid, egid, sgid);
4384 }
4385
4386 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
4387 return LSS_NAME(setresuid)(ruid, euid, suid);
4388 }
4389
4390 LSS_INLINE int LSS_NAME(sigaction)(int signum,
4391 const struct kernel_sigaction *act,
4392 struct kernel_sigaction *oldact) {
Bryan Chan3f6478a2016-06-14 08:38:17 -04004393 #if defined(__x86_64__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004394 /* On x86_64, the kernel requires us to always set our own
4395 * SA_RESTORER in order to be able to return from a signal handler.
4396 * This function must have a "magic" signature that the "gdb"
4397 * (and maybe the kernel?) can recognize.
4398 */
4399 if (act != NULL && !(act->sa_flags & SA_RESTORER)) {
4400 struct kernel_sigaction a = *act;
4401 a.sa_flags |= SA_RESTORER;
4402 a.sa_restorer = LSS_NAME(restore_rt)();
4403 return LSS_NAME(rt_sigaction)(signum, &a, oldact,
4404 (KERNEL_NSIG+7)/8);
Bryan Chan3f6478a2016-06-14 08:38:17 -04004405 } else
4406 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004407 return LSS_NAME(rt_sigaction)(signum, act, oldact,
4408 (KERNEL_NSIG+7)/8);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004409 }
4410
4411 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
4412 return LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
4413 }
4414
Joshua Peraza726d71e2019-11-13 12:21:13 -08004415 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
4416 return LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
4417 }
4418 #endif
4419 #if defined(__NR_rt_sigprocmask)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004420 LSS_INLINE int LSS_NAME(sigprocmask)(int how,
4421 const struct kernel_sigset_t *set,
4422 struct kernel_sigset_t *oldset) {
4423 return LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
4424 }
Joshua Peraza726d71e2019-11-13 12:21:13 -08004425 #endif
4426 #if defined(__NR_rt_sigtimedwait)
4427 LSS_INLINE int LSS_NAME(sigtimedwait)(const struct kernel_sigset_t *set,
4428 siginfo_t *info,
4429 const struct timespec *timeout) {
4430 return LSS_NAME(rt_sigtimedwait)(set, info, timeout, (KERNEL_NSIG+7)/8);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004431 }
4432 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004433 #if defined(__NR_wait4)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004434 LSS_INLINE _syscall4(pid_t, wait4, pid_t, p,
4435 int*, s, int, o,
4436 struct kernel_rusage*, r)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004437 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004438 #if defined(__NR_openat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004439 LSS_INLINE _syscall4(int, openat, int, d, const char *, p, int, f, int, m)
Bryan Chan3f6478a2016-06-14 08:38:17 -04004440 #endif
4441 #if defined(__NR_unlinkat)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004442 LSS_INLINE _syscall3(int, unlinkat, int, d, const char *, p, int, f)
4443 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004444 #if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
4445 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004446 #define __NR__getresgid32 __NR_getresgid32
4447 #define __NR__getresuid32 __NR_getresuid32
4448 #define __NR__setfsgid32 __NR_setfsgid32
4449 #define __NR__setfsuid32 __NR_setfsuid32
4450 #define __NR__setresgid32 __NR_setresgid32
4451 #define __NR__setresuid32 __NR_setresuid32
4452#if defined(__ARM_EABI__)
4453 LSS_INLINE _syscall2(int, ugetrlimit, int, r,
4454 struct kernel_rlimit*, l)
4455#endif
4456 LSS_INLINE _syscall3(int, _getresgid32, gid_t *, r,
4457 gid_t *, e, gid_t *, s)
4458 LSS_INLINE _syscall3(int, _getresuid32, uid_t *, r,
4459 uid_t *, e, uid_t *, s)
4460 LSS_INLINE _syscall1(int, _setfsgid32, gid_t, f)
4461 LSS_INLINE _syscall1(int, _setfsuid32, uid_t, f)
4462 LSS_INLINE _syscall3(int, _setresgid32, gid_t, r,
4463 gid_t, e, gid_t, s)
4464 LSS_INLINE _syscall3(int, _setresuid32, uid_t, r,
4465 uid_t, e, uid_t, s)
4466
4467 LSS_INLINE int LSS_NAME(getresgid32)(gid_t *rgid,
4468 gid_t *egid,
4469 gid_t *sgid) {
4470 int rc;
4471 if ((rc = LSS_NAME(_getresgid32)(rgid, egid, sgid)) < 0 &&
4472 LSS_ERRNO == ENOSYS) {
4473 if ((rgid == NULL) || (egid == NULL) || (sgid == NULL)) {
4474 return EFAULT;
4475 }
4476 // Clear the high bits first, since getresgid only sets 16 bits
4477 *rgid = *egid = *sgid = 0;
4478 rc = LSS_NAME(getresgid)(rgid, egid, sgid);
4479 }
4480 return rc;
4481 }
4482
4483 LSS_INLINE int LSS_NAME(getresuid32)(uid_t *ruid,
4484 uid_t *euid,
4485 uid_t *suid) {
4486 int rc;
4487 if ((rc = LSS_NAME(_getresuid32)(ruid, euid, suid)) < 0 &&
4488 LSS_ERRNO == ENOSYS) {
4489 if ((ruid == NULL) || (euid == NULL) || (suid == NULL)) {
4490 return EFAULT;
4491 }
4492 // Clear the high bits first, since getresuid only sets 16 bits
4493 *ruid = *euid = *suid = 0;
4494 rc = LSS_NAME(getresuid)(ruid, euid, suid);
4495 }
4496 return rc;
4497 }
4498
4499 LSS_INLINE int LSS_NAME(setfsgid32)(gid_t gid) {
4500 int rc;
4501 if ((rc = LSS_NAME(_setfsgid32)(gid)) < 0 &&
4502 LSS_ERRNO == ENOSYS) {
4503 if ((unsigned int)gid & ~0xFFFFu) {
4504 rc = EINVAL;
4505 } else {
4506 rc = LSS_NAME(setfsgid)(gid);
4507 }
4508 }
4509 return rc;
4510 }
4511
4512 LSS_INLINE int LSS_NAME(setfsuid32)(uid_t uid) {
4513 int rc;
4514 if ((rc = LSS_NAME(_setfsuid32)(uid)) < 0 &&
4515 LSS_ERRNO == ENOSYS) {
4516 if ((unsigned int)uid & ~0xFFFFu) {
4517 rc = EINVAL;
4518 } else {
4519 rc = LSS_NAME(setfsuid)(uid);
4520 }
4521 }
4522 return rc;
4523 }
4524
4525 LSS_INLINE int LSS_NAME(setresgid32)(gid_t rgid, gid_t egid, gid_t sgid) {
4526 int rc;
4527 if ((rc = LSS_NAME(_setresgid32)(rgid, egid, sgid)) < 0 &&
4528 LSS_ERRNO == ENOSYS) {
4529 if ((unsigned int)rgid & ~0xFFFFu ||
4530 (unsigned int)egid & ~0xFFFFu ||
4531 (unsigned int)sgid & ~0xFFFFu) {
4532 rc = EINVAL;
4533 } else {
4534 rc = LSS_NAME(setresgid)(rgid, egid, sgid);
4535 }
4536 }
4537 return rc;
4538 }
4539
4540 LSS_INLINE int LSS_NAME(setresuid32)(uid_t ruid, uid_t euid, uid_t suid) {
4541 int rc;
4542 if ((rc = LSS_NAME(_setresuid32)(ruid, euid, suid)) < 0 &&
4543 LSS_ERRNO == ENOSYS) {
4544 if ((unsigned int)ruid & ~0xFFFFu ||
4545 (unsigned int)euid & ~0xFFFFu ||
4546 (unsigned int)suid & ~0xFFFFu) {
4547 rc = EINVAL;
4548 } else {
4549 rc = LSS_NAME(setresuid)(ruid, euid, suid);
4550 }
4551 }
4552 return rc;
4553 }
4554 #endif
4555 LSS_INLINE int LSS_NAME(sigemptyset)(struct kernel_sigset_t *set) {
4556 memset(&set->sig, 0, sizeof(set->sig));
4557 return 0;
4558 }
4559
4560 LSS_INLINE int LSS_NAME(sigfillset)(struct kernel_sigset_t *set) {
4561 memset(&set->sig, -1, sizeof(set->sig));
4562 return 0;
4563 }
4564
4565 LSS_INLINE int LSS_NAME(sigaddset)(struct kernel_sigset_t *set,
4566 int signum) {
Peter Kasting880985f2022-06-29 21:17:55 +00004567 if (signum < 1 || (size_t)signum > (8*sizeof(set->sig))) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004568 LSS_ERRNO = EINVAL;
4569 return -1;
4570 } else {
Peter Kasting880985f2022-06-29 21:17:55 +00004571 set->sig[(size_t)(signum - 1)/(8*sizeof(set->sig[0]))]
4572 |= 1UL << ((size_t)(signum - 1) % (8*sizeof(set->sig[0])));
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004573 return 0;
4574 }
4575 }
4576
4577 LSS_INLINE int LSS_NAME(sigdelset)(struct kernel_sigset_t *set,
4578 int signum) {
Peter Kasting880985f2022-06-29 21:17:55 +00004579 if (signum < 1 || (size_t)signum > (8*sizeof(set->sig))) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004580 LSS_ERRNO = EINVAL;
4581 return -1;
4582 } else {
Peter Kasting880985f2022-06-29 21:17:55 +00004583 set->sig[(size_t)(signum - 1)/(8*sizeof(set->sig[0]))]
4584 &= ~(1UL << ((size_t)(signum - 1) % (8*sizeof(set->sig[0]))));
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004585 return 0;
4586 }
4587 }
mcgrathr@google.coma7999932011-11-21 22:26:20 +00004588
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004589 LSS_INLINE int LSS_NAME(sigismember)(struct kernel_sigset_t *set,
4590 int signum) {
Peter Kasting880985f2022-06-29 21:17:55 +00004591 if (signum < 1 || (size_t)signum > (8*sizeof(set->sig))) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004592 LSS_ERRNO = EINVAL;
4593 return -1;
4594 } else {
Peter Kasting880985f2022-06-29 21:17:55 +00004595 return !!(set->sig[(size_t)(signum - 1)/(8*sizeof(set->sig[0]))] &
4596 (1UL << ((size_t)(signum - 1) % (8*sizeof(set->sig[0])))));
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004597 }
4598 }
Bryan Chan3f6478a2016-06-14 08:38:17 -04004599 #if defined(__i386__) || \
4600 defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
4601 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
4602 defined(__PPC__) || \
Konstantin Ivlev8007b272021-01-27 18:27:42 +03004603 (defined(__s390__) && !defined(__s390x__)) || defined(__e2k__)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004604 #define __NR__sigaction __NR_sigaction
4605 #define __NR__sigpending __NR_sigpending
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004606 #define __NR__sigsuspend __NR_sigsuspend
4607 #define __NR__socketcall __NR_socketcall
4608 LSS_INLINE _syscall2(int, fstat64, int, f,
4609 struct kernel_stat64 *, b)
zodiac@gmail.com4f470182010-10-13 03:47:54 +00004610 LSS_INLINE _syscall5(int, _llseek, uint, fd,
4611 unsigned long, hi, unsigned long, lo,
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004612 loff_t *, res, uint, wh)
Bryan Chan3f6478a2016-06-14 08:38:17 -04004613#if defined(__s390__) && !defined(__s390x__)
4614 /* On s390, mmap2() arguments are passed in memory. */
4615 LSS_INLINE void* LSS_NAME(_mmap2)(void *s, size_t l, int p, int f, int d,
4616 off_t o) {
4617 unsigned long buf[6] = { (unsigned long) s, (unsigned long) l,
4618 (unsigned long) p, (unsigned long) f,
4619 (unsigned long) d, (unsigned long) o };
4620 LSS_REG(2, buf);
4621 LSS_BODY(void*, mmap2, "0"(__r2));
4622 }
4623#else
4624 #define __NR__mmap2 __NR_mmap2
4625 LSS_INLINE _syscall6(void*, _mmap2, void*, s,
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004626 size_t, l, int, p,
4627 int, f, int, d,
Bryan Chan3f6478a2016-06-14 08:38:17 -04004628 off_t, o)
4629#endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004630 LSS_INLINE _syscall3(int, _sigaction, int, s,
4631 const struct kernel_old_sigaction*, a,
4632 struct kernel_old_sigaction*, o)
4633 LSS_INLINE _syscall1(int, _sigpending, unsigned long*, s)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004634 #ifdef __PPC__
4635 LSS_INLINE _syscall1(int, _sigsuspend, unsigned long, s)
4636 #else
4637 LSS_INLINE _syscall3(int, _sigsuspend, const void*, a,
4638 int, b,
4639 unsigned long, s)
4640 #endif
4641 LSS_INLINE _syscall2(int, stat64, const char *, p,
4642 struct kernel_stat64 *, b)
4643
4644 LSS_INLINE int LSS_NAME(sigaction)(int signum,
4645 const struct kernel_sigaction *act,
4646 struct kernel_sigaction *oldact) {
4647 int old_errno = LSS_ERRNO;
4648 int rc;
4649 struct kernel_sigaction a;
4650 if (act != NULL) {
4651 a = *act;
4652 #ifdef __i386__
4653 /* On i386, the kernel requires us to always set our own
4654 * SA_RESTORER when using realtime signals. Otherwise, it does not
4655 * know how to return from a signal handler. This function must have
4656 * a "magic" signature that the "gdb" (and maybe the kernel?) can
4657 * recognize.
4658 * Apparently, a SA_RESTORER is implicitly set by the kernel, when
4659 * using non-realtime signals.
4660 *
4661 * TODO: Test whether ARM needs a restorer
4662 */
4663 if (!(a.sa_flags & SA_RESTORER)) {
4664 a.sa_flags |= SA_RESTORER;
4665 a.sa_restorer = (a.sa_flags & SA_SIGINFO)
4666 ? LSS_NAME(restore_rt)() : LSS_NAME(restore)();
4667 }
4668 #endif
4669 }
4670 rc = LSS_NAME(rt_sigaction)(signum, act ? &a : act, oldact,
4671 (KERNEL_NSIG+7)/8);
4672 if (rc < 0 && LSS_ERRNO == ENOSYS) {
4673 struct kernel_old_sigaction oa, ooa, *ptr_a = &oa, *ptr_oa = &ooa;
4674 if (!act) {
4675 ptr_a = NULL;
4676 } else {
4677 oa.sa_handler_ = act->sa_handler_;
4678 memcpy(&oa.sa_mask, &act->sa_mask, sizeof(oa.sa_mask));
4679 #ifndef __mips__
4680 oa.sa_restorer = act->sa_restorer;
4681 #endif
4682 oa.sa_flags = act->sa_flags;
4683 }
4684 if (!oldact) {
4685 ptr_oa = NULL;
4686 }
4687 LSS_ERRNO = old_errno;
4688 rc = LSS_NAME(_sigaction)(signum, ptr_a, ptr_oa);
4689 if (rc == 0 && oldact) {
4690 if (act) {
4691 memcpy(oldact, act, sizeof(*act));
4692 } else {
4693 memset(oldact, 0, sizeof(*oldact));
4694 }
4695 oldact->sa_handler_ = ptr_oa->sa_handler_;
4696 oldact->sa_flags = ptr_oa->sa_flags;
4697 memcpy(&oldact->sa_mask, &ptr_oa->sa_mask, sizeof(ptr_oa->sa_mask));
4698 #ifndef __mips__
4699 oldact->sa_restorer = ptr_oa->sa_restorer;
4700 #endif
4701 }
4702 }
4703 return rc;
4704 }
4705
4706 LSS_INLINE int LSS_NAME(sigpending)(struct kernel_sigset_t *set) {
4707 int old_errno = LSS_ERRNO;
4708 int rc = LSS_NAME(rt_sigpending)(set, (KERNEL_NSIG+7)/8);
4709 if (rc < 0 && LSS_ERRNO == ENOSYS) {
4710 LSS_ERRNO = old_errno;
4711 LSS_NAME(sigemptyset)(set);
4712 rc = LSS_NAME(_sigpending)(&set->sig[0]);
4713 }
4714 return rc;
4715 }
4716
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004717 LSS_INLINE int LSS_NAME(sigsuspend)(const struct kernel_sigset_t *set) {
4718 int olderrno = LSS_ERRNO;
4719 int rc = LSS_NAME(rt_sigsuspend)(set, (KERNEL_NSIG+7)/8);
4720 if (rc < 0 && LSS_ERRNO == ENOSYS) {
4721 LSS_ERRNO = olderrno;
4722 rc = LSS_NAME(_sigsuspend)(
4723 #ifndef __PPC__
4724 set, 0,
4725 #endif
4726 set->sig[0]);
4727 }
4728 return rc;
4729 }
4730 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004731 #if defined(__i386__) || \
4732 defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
4733 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
4734 defined(__PPC__) || \
4735 (defined(__s390__) && !defined(__s390x__))
4736 /* On these architectures, implement mmap() with mmap2(). */
4737 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4738 int64_t o) {
4739 if (o % 4096) {
4740 LSS_ERRNO = EINVAL;
4741 return (void *) -1;
4742 }
4743 return LSS_NAME(_mmap2)(s, l, p, f, d, (o / 4096));
4744 }
4745 #elif defined(__s390x__)
4746 /* On s390x, mmap() arguments are passed in memory. */
4747 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4748 int64_t o) {
4749 unsigned long buf[6] = { (unsigned long) s, (unsigned long) l,
4750 (unsigned long) p, (unsigned long) f,
4751 (unsigned long) d, (unsigned long) o };
4752 LSS_REG(2, buf);
4753 LSS_BODY(void*, mmap, "0"(__r2));
4754 }
4755 #elif defined(__x86_64__)
4756 /* Need to make sure __off64_t isn't truncated to 32-bits under x32. */
4757 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
4758 int64_t o) {
4759 LSS_BODY(6, void*, mmap, LSS_SYSCALL_ARG(s), LSS_SYSCALL_ARG(l),
4760 LSS_SYSCALL_ARG(p), LSS_SYSCALL_ARG(f),
4761 LSS_SYSCALL_ARG(d), (uint64_t)(o));
4762 }
4763 #else
4764 /* Remaining 64-bit architectures. */
4765 LSS_INLINE _syscall6(void*, mmap, void*, addr, size_t, length, int, prot,
4766 int, flags, int, fd, int64_t, offset)
4767 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004768 #if defined(__PPC__)
4769 #undef LSS_SC_LOADARGS_0
4770 #define LSS_SC_LOADARGS_0(dummy...)
4771 #undef LSS_SC_LOADARGS_1
4772 #define LSS_SC_LOADARGS_1(arg1) \
4773 __sc_4 = (unsigned long) (arg1)
4774 #undef LSS_SC_LOADARGS_2
4775 #define LSS_SC_LOADARGS_2(arg1, arg2) \
4776 LSS_SC_LOADARGS_1(arg1); \
4777 __sc_5 = (unsigned long) (arg2)
4778 #undef LSS_SC_LOADARGS_3
4779 #define LSS_SC_LOADARGS_3(arg1, arg2, arg3) \
4780 LSS_SC_LOADARGS_2(arg1, arg2); \
4781 __sc_6 = (unsigned long) (arg3)
4782 #undef LSS_SC_LOADARGS_4
4783 #define LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4) \
4784 LSS_SC_LOADARGS_3(arg1, arg2, arg3); \
4785 __sc_7 = (unsigned long) (arg4)
4786 #undef LSS_SC_LOADARGS_5
4787 #define LSS_SC_LOADARGS_5(arg1, arg2, arg3, arg4, arg5) \
4788 LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4); \
4789 __sc_8 = (unsigned long) (arg5)
4790 #undef LSS_SC_BODY
4791 #define LSS_SC_BODY(nr, type, opt, args...) \
4792 long __sc_ret, __sc_err; \
4793 { \
4794 register unsigned long __sc_0 __asm__ ("r0") = __NR_socketcall; \
4795 register unsigned long __sc_3 __asm__ ("r3") = opt; \
4796 register unsigned long __sc_4 __asm__ ("r4"); \
4797 register unsigned long __sc_5 __asm__ ("r5"); \
4798 register unsigned long __sc_6 __asm__ ("r6"); \
4799 register unsigned long __sc_7 __asm__ ("r7"); \
4800 register unsigned long __sc_8 __asm__ ("r8"); \
4801 LSS_SC_LOADARGS_##nr(args); \
4802 __asm__ __volatile__ \
4803 ("stwu 1, -48(1)\n\t" \
4804 "stw 4, 20(1)\n\t" \
4805 "stw 5, 24(1)\n\t" \
4806 "stw 6, 28(1)\n\t" \
4807 "stw 7, 32(1)\n\t" \
4808 "stw 8, 36(1)\n\t" \
4809 "addi 4, 1, 20\n\t" \
4810 "sc\n\t" \
4811 "mfcr %0" \
4812 : "=&r" (__sc_0), \
4813 "=&r" (__sc_3), "=&r" (__sc_4), \
4814 "=&r" (__sc_5), "=&r" (__sc_6), \
4815 "=&r" (__sc_7), "=&r" (__sc_8) \
4816 : LSS_ASMINPUT_##nr \
4817 : "cr0", "ctr", "memory"); \
4818 __sc_ret = __sc_3; \
4819 __sc_err = __sc_0; \
4820 } \
4821 LSS_RETURN(type, __sc_ret, __sc_err)
4822
4823 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
4824 int flags){
4825 LSS_SC_BODY(3, ssize_t, 17, s, msg, flags);
4826 }
4827
4828 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
4829 const struct kernel_msghdr *msg,
4830 int flags) {
4831 LSS_SC_BODY(3, ssize_t, 16, s, msg, flags);
4832 }
4833
4834 // TODO(csilvers): why is this ifdef'ed out?
4835#if 0
4836 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
4837 int flags,
4838 const struct kernel_sockaddr *to,
4839 unsigned int tolen) {
4840 LSS_BODY(6, ssize_t, 11, s, buf, len, flags, to, tolen);
4841 }
4842#endif
4843
4844 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
4845 LSS_SC_BODY(2, int, 13, s, how);
4846 }
4847
4848 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
4849 LSS_SC_BODY(3, int, 1, domain, type, protocol);
4850 }
4851
4852 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
4853 int sv[2]) {
4854 LSS_SC_BODY(4, int, 8, d, type, protocol, sv);
4855 }
4856 #endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004857 #if defined(__NR_recvmsg)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004858 LSS_INLINE _syscall3(ssize_t, recvmsg, int, s, struct kernel_msghdr*, msg,
4859 int, flags)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004860 #endif
4861 #if defined(__NR_sendmsg)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004862 LSS_INLINE _syscall3(ssize_t, sendmsg, int, s, const struct kernel_msghdr*,
4863 msg, int, flags)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004864 #endif
4865 #if defined(__NR_sendto)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004866 LSS_INLINE _syscall6(ssize_t, sendto, int, s, const void*, buf, size_t,len,
4867 int, flags, const struct kernel_sockaddr*, to,
4868 unsigned int, tolen)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004869 #endif
4870 #if defined(__NR_shutdown)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004871 LSS_INLINE _syscall2(int, shutdown, int, s, int, how)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004872 #endif
4873 #if defined(__NR_socket)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004874 LSS_INLINE _syscall3(int, socket, int, domain, int, type, int, protocol)
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004875 #endif
4876 #if defined(__NR_socketpair)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004877 LSS_INLINE _syscall4(int, socketpair, int, d, int, type, int, protocol,
4878 int*, sv)
4879 #endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004880
4881 #if defined(__NR_socketcall)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004882 LSS_INLINE _syscall2(int, _socketcall, int, c,
4883 va_list, a)
4884 LSS_INLINE int LSS_NAME(socketcall)(int op, ...) {
4885 int rc;
4886 va_list ap;
4887 va_start(ap, op);
4888 rc = LSS_NAME(_socketcall)(op, ap);
4889 va_end(ap);
4890 return rc;
4891 }
4892
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004893 # if !defined(__NR_recvmsg)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004894 LSS_INLINE ssize_t LSS_NAME(recvmsg)(int s,struct kernel_msghdr *msg,
4895 int flags){
4896 return (ssize_t)LSS_NAME(socketcall)(17, s, msg, flags);
4897 }
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004898 # endif
4899 # if !defined(__NR_sendmsg)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004900 LSS_INLINE ssize_t LSS_NAME(sendmsg)(int s,
4901 const struct kernel_msghdr *msg,
4902 int flags) {
4903 return (ssize_t)LSS_NAME(socketcall)(16, s, msg, flags);
4904 }
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004905 # endif
4906 # if !defined(__NR_sendto)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004907 LSS_INLINE ssize_t LSS_NAME(sendto)(int s, const void *buf, size_t len,
4908 int flags,
4909 const struct kernel_sockaddr *to,
4910 unsigned int tolen) {
4911 return (ssize_t)LSS_NAME(socketcall)(11, s, buf, len, flags, to, tolen);
4912 }
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004913 # endif
4914 # if !defined(__NR_shutdown)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004915 LSS_INLINE int LSS_NAME(shutdown)(int s, int how) {
4916 return LSS_NAME(socketcall)(13, s, how);
4917 }
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004918 # endif
4919 # if !defined(__NR_socket)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004920 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
4921 return LSS_NAME(socketcall)(1, domain, type, protocol);
4922 }
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004923 # endif
4924 # if !defined(__NR_socketpair)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004925 LSS_INLINE int LSS_NAME(socketpair)(int d, int type, int protocol,
4926 int sv[2]) {
4927 return LSS_NAME(socketcall)(8, d, type, protocol, sv);
4928 }
mingtaoxt xtc0c96892022-08-11 16:53:21 +08004929 # endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004930 #endif
Bryan Chan3f6478a2016-06-14 08:38:17 -04004931 #if defined(__NR_fstatat64)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004932 LSS_INLINE _syscall4(int, fstatat64, int, d,
4933 const char *, p,
4934 struct kernel_stat64 *, b, int, f)
4935 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004936 #if defined(__NR_waitpid)
4937 // waitpid is polyfilled below when not available.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004938 LSS_INLINE _syscall3(pid_t, waitpid, pid_t, p,
4939 int*, s, int, o)
4940 #endif
4941 #if defined(__mips__)
4942 /* sys_pipe() on MIPS has non-standard calling conventions, as it returns
4943 * both file handles through CPU registers.
4944 */
4945 LSS_INLINE int LSS_NAME(pipe)(int *p) {
4946 register unsigned long __v0 __asm__("$2") = __NR_pipe;
4947 register unsigned long __v1 __asm__("$3");
4948 register unsigned long __r7 __asm__("$7");
4949 __asm__ __volatile__ ("syscall\n"
vapier@chromium.orgda4a4892015-01-22 16:46:39 +00004950 : "=r"(__v0), "=r"(__v1), "=r" (__r7)
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004951 : "0"(__v0)
4952 : "$8", "$9", "$10", "$11", "$12",
zodiac@gmail.coma6591482012-04-13 01:29:30 +00004953 "$13", "$14", "$15", "$24", "$25", "memory");
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004954 if (__r7) {
zodiac@gmail.coma6591482012-04-13 01:29:30 +00004955 unsigned long __errnovalue = __v0;
4956 LSS_ERRNO = __errnovalue;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004957 return -1;
4958 } else {
4959 p[0] = __v0;
4960 p[1] = __v1;
4961 return 0;
4962 }
4963 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004964 #elif defined(__NR_pipe)
4965 // pipe is polyfilled below when not available.
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004966 LSS_INLINE _syscall1(int, pipe, int *, p)
4967 #endif
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04004968 #if defined(__NR_pipe2)
4969 LSS_INLINE _syscall2(int, pipe2, int *, pipefd, int, flags)
4970 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004971 /* TODO(csilvers): see if ppc can/should support this as well */
4972 #if defined(__i386__) || defined(__ARM_ARCH_3__) || \
Bryan Chan3f6478a2016-06-14 08:38:17 -04004973 defined(__ARM_EABI__) || \
4974 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64) || \
4975 (defined(__s390__) && !defined(__s390x__))
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004976 #define __NR__statfs64 __NR_statfs64
4977 #define __NR__fstatfs64 __NR_fstatfs64
4978 LSS_INLINE _syscall3(int, _statfs64, const char*, p,
4979 size_t, s,struct kernel_statfs64*, b)
4980 LSS_INLINE _syscall3(int, _fstatfs64, int, f,
4981 size_t, s,struct kernel_statfs64*, b)
4982 LSS_INLINE int LSS_NAME(statfs64)(const char *p,
4983 struct kernel_statfs64 *b) {
4984 return LSS_NAME(_statfs64)(p, sizeof(*b), b);
4985 }
4986 LSS_INLINE int LSS_NAME(fstatfs64)(int f,struct kernel_statfs64 *b) {
4987 return LSS_NAME(_fstatfs64)(f, sizeof(*b), b);
4988 }
4989 #endif
4990
4991 LSS_INLINE int LSS_NAME(execv)(const char *path, const char *const argv[]) {
4992 extern char **environ;
4993 return LSS_NAME(execve)(path, argv, (const char *const *)environ);
4994 }
4995
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00004996 LSS_INLINE pid_t LSS_NAME(gettid)(void) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00004997 pid_t tid = LSS_NAME(_gettid)();
4998 if (tid != -1) {
4999 return tid;
5000 }
5001 return LSS_NAME(getpid)();
5002 }
5003
5004 LSS_INLINE void *LSS_NAME(mremap)(void *old_address, size_t old_size,
5005 size_t new_size, int flags, ...) {
5006 va_list ap;
5007 void *new_address, *rc;
5008 va_start(ap, flags);
5009 new_address = va_arg(ap, void *);
5010 rc = LSS_NAME(_mremap)(old_address, old_size, new_size,
Peter Kasting880985f2022-06-29 21:17:55 +00005011 (unsigned long)flags, new_address);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005012 va_end(ap);
5013 return rc;
5014 }
5015
Peter Kasting880985f2022-06-29 21:17:55 +00005016 LSS_INLINE long LSS_NAME(ptrace_detach)(pid_t pid) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005017 /* PTRACE_DETACH can sometimes forget to wake up the tracee and it
5018 * then sends job control signals to the real parent, rather than to
5019 * the tracer. We reduce the risk of this happening by starting a
5020 * whole new time slice, and then quickly sending a SIGCONT signal
5021 * right after detaching from the tracee.
5022 *
5023 * We use tkill to ensure that we only issue a wakeup for the thread being
5024 * detached. Large multi threaded apps can take a long time in the kernel
5025 * processing SIGCONT.
5026 */
Peter Kasting880985f2022-06-29 21:17:55 +00005027 long rc;
5028 int err;
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005029 LSS_NAME(sched_yield)();
5030 rc = LSS_NAME(ptrace)(PTRACE_DETACH, pid, (void *)0, (void *)0);
5031 err = LSS_ERRNO;
5032 LSS_NAME(tkill)(pid, SIGCONT);
5033 /* Old systems don't have tkill */
5034 if (LSS_ERRNO == ENOSYS)
5035 LSS_NAME(kill)(pid, SIGCONT);
5036 LSS_ERRNO = err;
5037 return rc;
5038 }
5039
5040 LSS_INLINE int LSS_NAME(raise)(int sig) {
5041 return LSS_NAME(kill)(LSS_NAME(getpid)(), sig);
5042 }
5043
mseaborn@chromium.org8dce3582012-10-30 05:32:46 +00005044 LSS_INLINE int LSS_NAME(setpgrp)(void) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005045 return LSS_NAME(setpgid)(0, 0);
5046 }
5047
vapier@chromium.org2273e812013-04-01 17:52:44 +00005048 #if defined(__x86_64__)
5049 /* Need to make sure loff_t isn't truncated to 32-bits under x32. */
5050 LSS_INLINE ssize_t LSS_NAME(pread64)(int f, void *b, size_t c, loff_t o) {
5051 LSS_BODY(4, ssize_t, pread64, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(b),
5052 LSS_SYSCALL_ARG(c), (uint64_t)(o));
5053 }
5054
5055 LSS_INLINE ssize_t LSS_NAME(pwrite64)(int f, const void *b, size_t c,
5056 loff_t o) {
5057 LSS_BODY(4, ssize_t, pwrite64, LSS_SYSCALL_ARG(f), LSS_SYSCALL_ARG(b),
5058 LSS_SYSCALL_ARG(c), (uint64_t)(o));
5059 }
5060
Peter Kasting3bb68592022-07-18 19:29:31 +00005061 LSS_INLINE int LSS_NAME(readahead)(int f, loff_t o, size_t c) {
vapier@chromium.org2273e812013-04-01 17:52:44 +00005062 LSS_BODY(3, int, readahead, LSS_SYSCALL_ARG(f), (uint64_t)(o),
5063 LSS_SYSCALL_ARG(c));
5064 }
5065 #elif defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI64
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005066 LSS_INLINE _syscall4(ssize_t, pread64, int, f,
5067 void *, b, size_t, c,
5068 loff_t, o)
5069 LSS_INLINE _syscall4(ssize_t, pwrite64, int, f,
5070 const void *, b, size_t, c,
5071 loff_t, o)
5072 LSS_INLINE _syscall3(int, readahead, int, f,
5073 loff_t, o, unsigned, c)
5074 #else
5075 #define __NR__pread64 __NR_pread64
5076 #define __NR__pwrite64 __NR_pwrite64
5077 #define __NR__readahead __NR_readahead
mseaborn@chromium.org2c73abf2012-09-15 03:46:48 +00005078 #if defined(__ARM_EABI__) || defined(__mips__)
5079 /* On ARM and MIPS, a 64-bit parameter has to be in an even-odd register
5080 * pair. Hence these calls ignore their fourth argument (r3) so that their
mcgrathr@google.coma7999932011-11-21 22:26:20 +00005081 * fifth and sixth make such a pair (r4,r5).
5082 */
5083 #define LSS_LLARG_PAD 0,
5084 LSS_INLINE _syscall6(ssize_t, _pread64, int, f,
5085 void *, b, size_t, c,
5086 unsigned, skip, unsigned, o1, unsigned, o2)
5087 LSS_INLINE _syscall6(ssize_t, _pwrite64, int, f,
5088 const void *, b, size_t, c,
5089 unsigned, skip, unsigned, o1, unsigned, o2)
5090 LSS_INLINE _syscall5(int, _readahead, int, f,
5091 unsigned, skip,
5092 unsigned, o1, unsigned, o2, size_t, c)
5093 #else
5094 #define LSS_LLARG_PAD
5095 LSS_INLINE _syscall5(ssize_t, _pread64, int, f,
5096 void *, b, size_t, c, unsigned, o1,
5097 unsigned, o2)
5098 LSS_INLINE _syscall5(ssize_t, _pwrite64, int, f,
5099 const void *, b, size_t, c, unsigned, o1,
Samuel Attardacbdd592022-07-26 16:02:01 +00005100 unsigned, o2)
mcgrathr@google.coma7999932011-11-21 22:26:20 +00005101 LSS_INLINE _syscall4(int, _readahead, int, f,
5102 unsigned, o1, unsigned, o2, size_t, c)
5103 #endif
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005104 /* We force 64bit-wide parameters onto the stack, then access each
5105 * 32-bit component individually. This guarantees that we build the
5106 * correct parameters independent of the native byte-order of the
5107 * underlying architecture.
5108 */
5109 LSS_INLINE ssize_t LSS_NAME(pread64)(int fd, void *buf, size_t count,
5110 loff_t off) {
5111 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00005112 return LSS_NAME(_pread64)(fd, buf, count,
5113 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005114 }
5115 LSS_INLINE ssize_t LSS_NAME(pwrite64)(int fd, const void *buf,
5116 size_t count, loff_t off) {
5117 union { loff_t off; unsigned arg[2]; } o = { off };
mcgrathr@google.coma7999932011-11-21 22:26:20 +00005118 return LSS_NAME(_pwrite64)(fd, buf, count,
5119 LSS_LLARG_PAD o.arg[0], o.arg[1]);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005120 }
Peter Kasting3bb68592022-07-18 19:29:31 +00005121 LSS_INLINE int LSS_NAME(readahead)(int fd, loff_t off, size_t count) {
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005122 union { loff_t off; unsigned arg[2]; } o = { off };
Peter Kasting3bb68592022-07-18 19:29:31 +00005123 return LSS_NAME(_readahead)(fd, LSS_LLARG_PAD o.arg[0], o.arg[1], count);
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005124 }
5125 #endif
5126#endif
5127
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005128/*
5129 * Polyfills for deprecated syscalls.
5130 */
5131
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005132#if !defined(__NR_dup2)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005133 LSS_INLINE int LSS_NAME(dup2)(int s, int d) {
5134 return LSS_NAME(dup3)(s, d, 0);
5135 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005136#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005137
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005138#if !defined(__NR_open)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005139 LSS_INLINE int LSS_NAME(open)(const char *pathname, int flags, int mode) {
5140 return LSS_NAME(openat)(AT_FDCWD, pathname, flags, mode);
5141 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005142#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005143
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005144#if !defined(__NR_unlink)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005145 LSS_INLINE int LSS_NAME(unlink)(const char *pathname) {
5146 return LSS_NAME(unlinkat)(AT_FDCWD, pathname, 0);
5147 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005148#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005149
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005150#if !defined(__NR_readlink)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005151 LSS_INLINE int LSS_NAME(readlink)(const char *pathname, char *buffer,
5152 size_t size) {
5153 return LSS_NAME(readlinkat)(AT_FDCWD, pathname, buffer, size);
5154 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005155#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005156
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005157#if !defined(__NR_pipe)
Mike Frysinger4ce4c482018-01-03 18:31:42 -05005158 LSS_INLINE int LSS_NAME(pipe)(int *pipefd) {
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005159 return LSS_NAME(pipe2)(pipefd, 0);
5160 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005161#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005162
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005163#if !defined(__NR_poll)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005164 LSS_INLINE int LSS_NAME(poll)(struct kernel_pollfd *fds, unsigned int nfds,
5165 int timeout) {
5166 struct kernel_timespec timeout_ts;
5167 struct kernel_timespec *timeout_ts_p = NULL;
5168
5169 if (timeout >= 0) {
5170 timeout_ts.tv_sec = timeout / 1000;
5171 timeout_ts.tv_nsec = (timeout % 1000) * 1000000;
5172 timeout_ts_p = &timeout_ts;
5173 }
5174 return LSS_NAME(ppoll)(fds, nfds, timeout_ts_p, NULL, 0);
5175 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005176#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005177
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005178#if defined(__NR_statx)
5179 /* copy the contents of kernel_statx to the kernel_stat structure. */
5180 LSS_INLINE void LSS_NAME(cp_stat_statx)(struct kernel_stat *to,
5181 struct kernel_statx *from) {
5182 memset(to, 0, sizeof(struct kernel_stat));
Peter Kasting092a9212022-08-22 20:02:11 +00005183 to->st_dev = (kernel_dev_t)((from->stx_dev_minor & 0xff) |
5184 ((from->stx_dev_major & 0xfff) << 8) |
5185 ((from->stx_dev_minor & ~0xffu) << 12));
5186 to->st_rdev = (kernel_dev_t)((from->stx_rdev_minor & 0xff) |
5187 ((from->stx_rdev_major & 0xfff) << 8) |
5188 ((from->stx_rdev_minor & ~0xffu) << 12));
5189 to->st_ino = (kernel_ino_t)from->stx_ino;
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005190 to->st_mode = from->stx_mode;
Peter Kasting092a9212022-08-22 20:02:11 +00005191 to->st_nlink = (kernel_nlink_t)from->stx_nlink;
5192 to->st_uid = (kernel_uid_t)from->stx_uid;
5193 to->st_gid = (kernel_gid_t)from->stx_gid;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +00005194 to->st_atime_ = (kernel_time_t)(from->stx_atime.tv_sec);
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005195 to->st_atime_nsec_ = from->stx_atime.tv_nsec;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +00005196 to->st_mtime_ = (kernel_time_t)(from->stx_mtime.tv_sec);
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005197 to->st_mtime_nsec_ = from->stx_mtime.tv_nsec;
Peter Kasting7a6e3fd2022-08-22 17:33:00 +00005198 to->st_ctime_ = (kernel_time_t)(from->stx_ctime.tv_sec);
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005199 to->st_ctime_nsec_ = from->stx_ctime.tv_nsec;
Peter Kasting99121a32022-08-22 16:43:44 +00005200 to->st_size = (kernel_off_t)(from->stx_size);
5201 to->st_blocks = (kernel_blkcnt_t)(from->stx_blocks);
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005202 to->st_blksize = from->stx_blksize;
5203 }
5204#endif
5205
5206#if !defined(__NR_fstat)
5207 LSS_INLINE int LSS_NAME(fstat)(int fd,
5208 struct kernel_stat *buf) {
5209 #if defined(__NR_newfstatat)
5210 return LSS_NAME(newfstatat)(fd, "", buf, AT_EMPTY_PATH);
5211 #elif defined(__NR_statx)
5212 struct kernel_statx stx;
5213 int flags = AT_NO_AUTOMOUNT | AT_EMPTY_PATH;
5214 int mask = STATX_BASIC_STATS;
5215 int res = LSS_NAME(statx)(fd, "", flags, mask, &stx);
5216 LSS_NAME(cp_stat_statx)(buf, &stx);
5217 return res;
5218 #endif
5219 }
5220#endif
5221
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005222#if !defined(__NR_stat)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005223 LSS_INLINE int LSS_NAME(stat)(const char *pathname,
5224 struct kernel_stat *buf) {
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005225 #if defined(__NR_newfstatat)
5226 return LSS_NAME(newfstatat)(AT_FDCWD, pathname, buf, 0);
5227 #elif defined(__NR_statx)
5228 struct kernel_statx stx;
5229 int flags = AT_NO_AUTOMOUNT | AT_STATX_SYNC_AS_STAT;
5230 int mask = STATX_BASIC_STATS;
5231 int res = LSS_NAME(statx)(AT_FDCWD, pathname, flags, mask, &stx);
5232 LSS_NAME(cp_stat_statx)(buf, &stx);
5233 return res;
5234 #endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005235 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005236#endif
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005237
Matthew Denton92a65a82021-04-01 13:00:07 -07005238#if !defined(__NR_lstat)
5239 LSS_INLINE int LSS_NAME(lstat)(const char *pathname,
5240 struct kernel_stat *buf) {
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005241 #if defined(__NR_newfstatat)
5242 return LSS_NAME(newfstatat)(AT_FDCWD, pathname, buf, AT_SYMLINK_NOFOLLOW);
5243 #elif defined(__NR_statx)
5244 struct kernel_statx stx;
5245 int flags = AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW;
5246 int mask = STATX_BASIC_STATS;
5247 int res = LSS_NAME(statx)(AT_FDCWD, pathname, flags, mask, &stx);
5248 LSS_NAME(cp_stat_statx)(buf, &stx);
5249 return res;
5250 #endif
Matthew Denton92a65a82021-04-01 13:00:07 -07005251 }
5252#endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005253
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005254#if !defined(__NR_waitpid)
5255 LSS_INLINE pid_t LSS_NAME(waitpid)(pid_t pid, int *status, int options) {
5256 return LSS_NAME(wait4)(pid, status, options, 0);
5257 }
5258#endif
5259
5260#if !defined(__NR_fork)
5261// TODO: define this in an arch-independant way instead of inlining the clone
5262// syscall body.
5263
mingtaoxt xtc0c96892022-08-11 16:53:21 +08005264# if defined(__aarch64__) || defined(__riscv) || defined(__loongarch_lp64)
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005265 LSS_INLINE pid_t LSS_NAME(fork)(void) {
5266 // No fork syscall on aarch64 - implement by means of the clone syscall.
5267 // Note that this does not reset glibc's cached view of the PID/TID, so
5268 // some glibc interfaces might go wrong in the forked subprocess.
5269 int flags = SIGCHLD;
5270 void *child_stack = NULL;
5271 void *parent_tidptr = NULL;
5272 void *newtls = NULL;
5273 void *child_tidptr = NULL;
5274
5275 LSS_REG(0, flags);
5276 LSS_REG(1, child_stack);
5277 LSS_REG(2, parent_tidptr);
5278 LSS_REG(3, newtls);
5279 LSS_REG(4, child_tidptr);
5280 LSS_BODY(pid_t, clone, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3),
5281 "r"(__r4));
5282 }
Torne (Richard Coles)e6527b02017-10-03 17:38:15 -04005283# elif defined(__x86_64__)
5284 LSS_INLINE pid_t LSS_NAME(fork)(void) {
5285 // Android disallows the fork syscall on x86_64 - implement by means of the
5286 // clone syscall as above for aarch64.
5287 int flags = SIGCHLD;
5288 void *child_stack = NULL;
5289 void *parent_tidptr = NULL;
5290 void *newtls = NULL;
5291 void *child_tidptr = NULL;
5292
5293 LSS_BODY(5, pid_t, clone, LSS_SYSCALL_ARG(flags),
5294 LSS_SYSCALL_ARG(child_stack), LSS_SYSCALL_ARG(parent_tidptr),
5295 LSS_SYSCALL_ARG(newtls), LSS_SYSCALL_ARG(child_tidptr));
5296 }
5297# else
5298# error missing fork polyfill for this architecture
5299# endif
anton@chromium.org2f724fc2014-04-15 13:05:20 +00005300#endif
5301
Michael Forneyf70e2f12020-01-22 19:19:38 -08005302/* These restore the original values of these macros saved by the
5303 * corresponding #pragma push_macro near the top of this file. */
5304#pragma pop_macro("stat64")
5305#pragma pop_macro("fstat64")
5306#pragma pop_macro("lstat64")
5307#pragma pop_macro("pread64")
5308#pragma pop_macro("pwrite64")
Michael Forneyfd00dbb2020-03-10 14:12:52 -07005309#pragma pop_macro("getdents64")
mseaborn@chromium.orgca749372012-09-05 18:26:20 +00005310
zodiac@gmail.com71d26df2010-09-15 01:31:22 +00005311#if defined(__cplusplus) && !defined(SYS_CPLUSPLUS)
5312}
5313#endif
5314
5315#endif
5316#endif