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