1 /* Copyright (c) 2005-2008, Google Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 * Author: Markus Gutschke
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:
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.
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
55 * New symbols will be defined "static inline", unless overridden by
56 * the SYS_INLINE macro.
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.
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.
69 * This file defines a few internal symbols that all start with "LSS_".
70 * Do not access these symbols from outside this file. They are not part
71 * of the supported API.
73 * NOTE: This is a stripped down version of the official opensource
74 * version of linux_syscall_support.h, which lives at
75 * http://code.google.com/p/linux-syscall-support/
76 * It includes only the syscalls that are used in perftools, plus a
77 * few extra. Here's the breakdown:
78 * 1) Perftools uses these: grep -rho 'sys_[a-z0-9_A-Z]* *(' src | sort -u
109 * 2) These are used as subroutines of the above:
110 * sys_getpid -- gettid
111 * sys_kill -- ptrace_detach
112 * sys_restore -- sigaction
113 * sys_restore_rt -- sigaction
114 * sys_socketcall -- socket
115 * sys_wait4 -- waitpid
116 * 3) I left these in even though they're not used. They either
117 * complement the above (write vs read) or are variants (rt_sigaction):
130 #ifndef SYS_LINUX_SYSCALL_SUPPORT_H
131 #define SYS_LINUX_SYSCALL_SUPPORT_H
133 /* We currently only support x86-32, x86-64, ARM, MIPS, and PPC on Linux.
134 * Porting to other related platforms should not be difficult.
136 #if (defined(__i386__) || defined(__x86_64__) || defined(__arm__) || \
137 defined(__mips__) || defined(__PPC__)) && defined(__linux)
139 #ifndef SYS_CPLUSPLUS
141 /* Some system header files in older versions of gcc neglect to properly
142 * handle being included from C++. As it appears to be harmless to have
143 * multiple nested 'extern "C"' blocks, just add another one here.
154 #include <sys/ptrace.h>
155 #include <sys/resource.h>
156 #include <sys/time.h>
157 #include <sys/types.h>
158 #if defined(__ANDROID__)
159 #include <sys/syscall.h>
160 #ifndef ANDROID_NON_SDK_BUILD
161 #include <sys/linux-syscalls.h>
167 #include <linux/unistd.h>
171 /* Include definitions of the ABI currently in use. */
177 /* As glibc often provides subtly incompatible data structures (and implicit
178 * wrapper functions that convert them), we provide our own kernel data
179 * structures for use by the system calls.
180 * These structures have been developed by using Linux 2.6.23 headers for
181 * reference. Note though, we do not care about exact API compatibility
182 * with the kernel, and in fact the kernel often does not have a single
183 * API that works across architectures. Instead, we try to mimic the glibc
184 * API where reasonable, and only guarantee ABI compatibility with the
186 * Most notably, here are a few changes that were made to the structures
187 * defined by kernel headers:
189 * - we only define structures, but not symbolic names for kernel data
190 * types. For the latter, we directly use the native C datatype
191 * (i.e. "unsigned" instead of "mode_t").
192 * - in a few cases, it is possible to define identical structures for
193 * both 32bit (e.g. i386) and 64bit (e.g. x86-64) platforms by
194 * standardizing on the 64bit version of the data types. In particular,
195 * this means that we use "unsigned" where the 32bit headers say
197 * - overall, we try to minimize the number of cases where we need to
198 * conditionally define different structures.
199 * - the "struct kernel_sigaction" class of structures have been
200 * modified to more closely mimic glibc's API by introducing an
201 * anonymous union for the function pointer.
202 * - a small number of field names had to have an underscore appended to
203 * them, because glibc defines a global macro by the same name.
206 /* include/linux/dirent.h */
207 struct kernel_dirent64 {
208 unsigned long long d_ino;
210 unsigned short d_reclen;
211 unsigned char d_type;
215 /* include/linux/dirent.h */
216 struct kernel_dirent {
219 unsigned short d_reclen;
223 /* include/linux/time.h */
224 struct kernel_timespec {
229 /* include/linux/time.h */
230 struct kernel_timeval {
235 /* include/linux/resource.h */
236 struct kernel_rusage {
237 struct kernel_timeval ru_utime;
238 struct kernel_timeval ru_stime;
255 #if defined(__i386__) || defined(__arm__) || defined(__PPC__)
257 /* include/asm-{arm,i386,mips,ppc}/signal.h */
258 struct kernel_old_sigaction {
260 void (*sa_handler_)(int);
261 void (*sa_sigaction_)(int, siginfo_t *, void *);
263 unsigned long sa_mask;
264 unsigned long sa_flags;
265 void (*sa_restorer)(void);
266 } __attribute__((packed,aligned(4)));
267 #elif (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
268 #define kernel_old_sigaction kernel_sigaction
271 /* Some kernel functions (e.g. sigaction() in 2.6.23) require that the
272 * exactly match the size of the signal set, even though the API was
273 * intended to be extensible. We define our own KERNEL_NSIG to deal with
275 * Please note that glibc provides signals [1.._NSIG-1], whereas the
276 * kernel (and this header) provides the range [1..KERNEL_NSIG]. The
277 * actual number of signals is obviously the same, but the constants
281 #define KERNEL_NSIG 128
283 #define KERNEL_NSIG 64
286 /* include/asm-{arm,i386,mips,x86_64}/signal.h */
287 struct kernel_sigset_t {
288 unsigned long sig[(KERNEL_NSIG + 8*sizeof(unsigned long) - 1)/
289 (8*sizeof(unsigned long))];
292 /* include/asm-{arm,i386,mips,x86_64,ppc}/signal.h */
293 struct kernel_sigaction {
295 unsigned long sa_flags;
297 void (*sa_handler_)(int);
298 void (*sa_sigaction_)(int, siginfo_t *, void *);
300 struct kernel_sigset_t sa_mask;
303 void (*sa_handler_)(int);
304 void (*sa_sigaction_)(int, siginfo_t *, void *);
306 unsigned long sa_flags;
307 void (*sa_restorer)(void);
308 struct kernel_sigset_t sa_mask;
312 /* include/asm-{arm,i386,mips,ppc}/stat.h */
314 #if _MIPS_SIM == _MIPS_SIM_ABI64
317 struct kernel_stat64 {
321 unsigned long long st_ino;
330 unsigned st_atime_nsec_;
332 unsigned st_mtime_nsec_;
334 unsigned st_ctime_nsec_;
337 unsigned long long st_blocks;
339 #elif defined __PPC__
340 struct kernel_stat64 {
341 unsigned long long st_dev;
342 unsigned long long st_ino;
347 unsigned long long st_rdev;
348 unsigned short int __pad2;
353 unsigned long st_atime_nsec_;
355 unsigned long st_mtime_nsec_;
357 unsigned long st_ctime_nsec_;
358 unsigned long __unused4;
359 unsigned long __unused5;
362 struct kernel_stat64 {
363 unsigned long long st_dev;
364 unsigned char __pad0[4];
370 unsigned long long st_rdev;
371 unsigned char __pad3[4];
374 unsigned long long st_blocks;
376 unsigned st_atime_nsec_;
378 unsigned st_mtime_nsec_;
380 unsigned st_ctime_nsec_;
381 unsigned long long st_ino;
385 /* include/asm-{arm,i386,mips,x86_64,ppc}/stat.h */
386 #if defined(__i386__) || defined(__arm__)
388 /* The kernel headers suggest that st_dev and st_rdev should be 32bit
389 * quantities encoding 12bit major and 20bit minor numbers in an interleaved
390 * format. In reality, we do not see useful data in the top bits. So,
391 * we'll leave the padding in here, until we find a better solution.
393 unsigned short st_dev;
396 unsigned short st_mode;
397 unsigned short st_nlink;
398 unsigned short st_uid;
399 unsigned short st_gid;
400 unsigned short st_rdev;
406 unsigned st_atime_nsec_;
408 unsigned st_mtime_nsec_;
410 unsigned st_ctime_nsec_;
414 #elif defined(__x86_64__)
428 uint64_t st_atime_nsec_;
430 uint64_t st_mtime_nsec_;
432 uint64_t st_ctime_nsec_;
435 #elif defined(__PPC__)
438 unsigned long st_ino; // ino_t
439 unsigned long st_mode; // mode_t
440 unsigned short st_nlink; // nlink_t
441 unsigned st_uid; // uid_t
442 unsigned st_gid; // gid_t
444 long st_size; // off_t
445 unsigned long st_blksize;
446 unsigned long st_blocks;
447 unsigned long st_atime_;
448 unsigned long st_atime_nsec_;
449 unsigned long st_mtime_;
450 unsigned long st_mtime_nsec_;
451 unsigned long st_ctime_;
452 unsigned long st_ctime_nsec_;
453 unsigned long __unused4;
454 unsigned long __unused5;
456 #elif (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64)
481 // ulong is not defined in Android while used to define __llseek.
482 #if defined(__ANDROID__)
483 typedef unsigned long int ulong;
487 /* Definitions missing from the standard header files */
490 #define O_DIRECTORY 0040000
492 #define O_DIRECTORY 0200000
495 #ifndef PR_GET_DUMPABLE
496 #define PR_GET_DUMPABLE 3
498 #ifndef PR_SET_DUMPABLE
499 #define PR_SET_DUMPABLE 4
502 #define AT_FDCWD (-100)
504 #ifndef AT_SYMLINK_NOFOLLOW
505 #define AT_SYMLINK_NOFOLLOW 0x100
508 #define AT_REMOVEDIR 0x200
511 #define MREMAP_FIXED 2
514 #define SA_RESTORER 0x04000000
517 #if defined(__i386__)
518 #ifndef __NR_rt_sigaction
519 #define __NR_rt_sigaction 174
520 #define __NR_rt_sigprocmask 175
523 #define __NR_stat64 195
526 #define __NR_fstat64 197
528 #ifndef __NR_getdents64
529 #define __NR_getdents64 220
532 #define __NR_gettid 224
535 #define __NR_futex 240
538 #define __NR_openat 295
541 #define __NR_getcpu 318
543 /* End of i386 definitions */
544 #elif defined(__arm__)
546 #if defined(__thumb__) || defined(__ARM_EABI__)
547 #define __SYS_REG(name) register long __sysreg __asm__("r6") = __NR_##name;
548 #define __SYS_REG_LIST(regs...) [sysreg] "r" (__sysreg) , ##regs
549 #define __syscall(name) "swi\t0"
550 #define __syscall_safe(name) \
552 "mov r7,%[sysreg]\n" \
553 __syscall(name)"\n" \
556 #define __SYS_REG(name)
557 #define __SYS_REG_LIST(regs...) regs
558 #define __syscall(name) "swi\t" __sys1(__NR_##name) ""
559 #define __syscall_safe(name) __syscall(name)
562 #ifndef __NR_rt_sigaction
563 #define __NR_rt_sigaction (__NR_SYSCALL_BASE + 174)
564 #define __NR_rt_sigprocmask (__NR_SYSCALL_BASE + 175)
567 #define __NR_stat64 (__NR_SYSCALL_BASE + 195)
570 #define __NR_fstat64 (__NR_SYSCALL_BASE + 197)
572 #ifndef __NR_getdents64
573 #define __NR_getdents64 (__NR_SYSCALL_BASE + 217)
576 #define __NR_gettid (__NR_SYSCALL_BASE + 224)
579 #define __NR_futex (__NR_SYSCALL_BASE + 240)
581 /* End of ARM definitions */
582 #elif defined(__x86_64__)
584 #define __NR_gettid 186
587 #define __NR_futex 202
589 #ifndef __NR_getdents64
590 #define __NR_getdents64 217
593 #define __NR_openat 257
595 /* End of x86-64 definitions */
596 #elif defined(__mips__)
597 #if _MIPS_SIM == _MIPS_SIM_ABI32
598 #ifndef __NR_rt_sigaction
599 #define __NR_rt_sigaction (__NR_Linux + 194)
600 #define __NR_rt_sigprocmask (__NR_Linux + 195)
603 #define __NR_stat64 (__NR_Linux + 213)
606 #define __NR_fstat64 (__NR_Linux + 215)
608 #ifndef __NR_getdents64
609 #define __NR_getdents64 (__NR_Linux + 219)
612 #define __NR_gettid (__NR_Linux + 222)
615 #define __NR_futex (__NR_Linux + 238)
618 #define __NR_openat (__NR_Linux + 288)
621 #define __NR_fstatat (__NR_Linux + 293)
624 #define __NR_getcpu (__NR_Linux + 312)
626 /* End of MIPS (old 32bit API) definitions */
627 #elif _MIPS_SIM == _MIPS_SIM_ABI64
629 #define __NR_gettid (__NR_Linux + 178)
632 #define __NR_futex (__NR_Linux + 194)
635 #define __NR_openat (__NR_Linux + 247)
638 #define __NR_fstatat (__NR_Linux + 252)
641 #define __NR_getcpu (__NR_Linux + 271)
643 /* End of MIPS (64bit API) definitions */
646 #define __NR_gettid (__NR_Linux + 178)
649 #define __NR_futex (__NR_Linux + 194)
652 #define __NR_openat (__NR_Linux + 251)
655 #define __NR_fstatat (__NR_Linux + 256)
658 #define __NR_getcpu (__NR_Linux + 275)
660 /* End of MIPS (new 32bit API) definitions */
662 /* End of MIPS definitions */
663 #elif defined(__PPC__)
664 #ifndef __NR_rt_sigaction
665 #define __NR_rt_sigaction 173
666 #define __NR_rt_sigprocmask 174
669 #define __NR_stat64 195
672 #define __NR_fstat64 197
674 #ifndef __NR_getdents64
675 #define __NR_getdents64 202
678 #define __NR_gettid 207
681 #define __NR_futex 221
684 #define __NR_openat 286
687 #define __NR_getcpu 302
689 /* End of powerpc defininitions */
693 /* After forking, we must make sure to only call system calls. */
694 #if __BOUNDED_POINTERS__
695 #error "Need to port invocations of syscalls for bounded ptrs"
697 /* The core dumper and the thread lister get executed after threads
698 * have been suspended. As a consequence, we cannot call any functions
699 * that acquire locks. Unfortunately, libc wraps most system calls
700 * (e.g. in order to implement pthread_atfork, and to make calls
701 * cancellable), which means we cannot call these functions. Instead,
702 * we have to call syscall() directly.
706 /* Allow the including file to override the location of errno. This can
707 * be useful when using clone() with the CLONE_VM option.
709 #define LSS_ERRNO SYS_ERRNO
711 #define LSS_ERRNO errno
716 #define LSS_INLINE SYS_INLINE
718 #define LSS_INLINE static inline
721 /* Allow the including file to override the prefix used for all new
722 * system calls. By default, it will be set to "sys_".
726 #define LSS_NAME(name) sys_##name
728 #define LSS_NAME(name) name
729 #elif SYS_PREFIX == 0
730 #define LSS_NAME(name) sys0_##name
731 #elif SYS_PREFIX == 1
732 #define LSS_NAME(name) sys1_##name
733 #elif SYS_PREFIX == 2
734 #define LSS_NAME(name) sys2_##name
735 #elif SYS_PREFIX == 3
736 #define LSS_NAME(name) sys3_##name
737 #elif SYS_PREFIX == 4
738 #define LSS_NAME(name) sys4_##name
739 #elif SYS_PREFIX == 5
740 #define LSS_NAME(name) sys5_##name
741 #elif SYS_PREFIX == 6
742 #define LSS_NAME(name) sys6_##name
743 #elif SYS_PREFIX == 7
744 #define LSS_NAME(name) sys7_##name
745 #elif SYS_PREFIX == 8
746 #define LSS_NAME(name) sys8_##name
747 #elif SYS_PREFIX == 9
748 #define LSS_NAME(name) sys9_##name
752 #if (defined(__i386__) || defined(__x86_64__) || defined(__arm__))
753 /* Failing system calls return a negative result in the range of
754 * -1..-4095. These are "errno" values with the sign inverted.
756 #define LSS_RETURN(type, res) \
758 if ((unsigned long)(res) >= (unsigned long)(-4095)) { \
759 LSS_ERRNO = -(res); \
762 return (type) (res); \
764 #elif defined(__mips__)
765 /* On MIPS, failing system calls return -1, and set errno in a
766 * separate CPU register.
768 #define LSS_RETURN(type, res, err) \
774 return (type) (res); \
776 #elif defined(__PPC__)
777 /* On PPC, failing system calls return -1, and set errno in a
778 * separate CPU register. See linux/unistd.h.
780 #define LSS_RETURN(type, res, err) \
782 if (err & 0x10000000 ) { \
786 return (type) (res); \
789 #if defined(__i386__)
790 #if defined(NO_FRAME_POINTER) && (100 * __GNUC__ + __GNUC_MINOR__ >= 404)
791 /* This only works for GCC-4.4 and above -- the first version to use
792 .cfi directives for dwarf unwind info. */
793 #define CFI_ADJUST_CFA_OFFSET(adjust) \
794 ".cfi_adjust_cfa_offset " #adjust "\n"
796 #define CFI_ADJUST_CFA_OFFSET(adjust) /**/
799 /* In PIC mode (e.g. when building shared libraries), gcc for i386
800 * reserves ebx. Unfortunately, most distribution ship with implementations
801 * of _syscallX() which clobber ebx.
802 * Also, most definitions of _syscallX() neglect to mark "memory" as being
803 * clobbered. This causes problems with compilers, that do a better job
804 * at optimizing across __asm__ calls.
805 * So, we just have to redefine all of the _syscallX() macros.
808 #define LSS_BODY(type,args...) \
810 __asm__ __volatile__("push %%ebx\n" \
811 CFI_ADJUST_CFA_OFFSET(4) \
815 CFI_ADJUST_CFA_OFFSET(-4) \
817 : "esp", "memory"); \
818 LSS_RETURN(type,__res)
820 #define _syscall0(type,name) \
821 type LSS_NAME(name)(void) { \
823 __asm__ volatile("int $0x80" \
825 : "0" (__NR_##name) \
827 LSS_RETURN(type,__res); \
830 #define _syscall1(type,name,type1,arg1) \
831 type LSS_NAME(name)(type1 arg1) { \
834 : "0" (__NR_##name), "ri" ((long)(arg1))); \
837 #define _syscall2(type,name,type1,arg1,type2,arg2) \
838 type LSS_NAME(name)(type1 arg1,type2 arg2) { \
841 : "0" (__NR_##name),"ri" ((long)(arg1)), "c" ((long)(arg2))); \
844 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
845 type LSS_NAME(name)(type1 arg1,type2 arg2,type3 arg3) { \
848 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
849 "d" ((long)(arg3))); \
852 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
853 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
856 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
857 "d" ((long)(arg3)),"S" ((long)(arg4))); \
860 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
862 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
865 __asm__ __volatile__("push %%ebx\n" \
871 : "i" (__NR_##name), "ri" ((long)(arg1)), \
872 "c" ((long)(arg2)), "d" ((long)(arg3)), \
873 "S" ((long)(arg4)), "D" ((long)(arg5)) \
874 : "esp", "memory"); \
875 LSS_RETURN(type,__res); \
878 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
879 type5,arg5,type6,arg6) \
880 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
881 type5 arg5, type6 arg6) { \
883 struct { long __a1; long __a6; } __s = { (long)arg1, (long) arg6 }; \
884 __asm__ __volatile__("push %%ebp\n" \
886 "movl 4(%2),%%ebp\n" \
887 "movl 0(%2), %%ebx\n" \
893 : "i" (__NR_##name), "0" ((long)(&__s)), \
894 "c" ((long)(arg2)), "d" ((long)(arg3)), \
895 "S" ((long)(arg4)), "D" ((long)(arg5)) \
896 : "esp", "memory"); \
897 LSS_RETURN(type,__res); \
899 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
900 int flags, void *arg, int *parent_tidptr,
901 void *newtls, int *child_tidptr) {
903 __asm__ __volatile__(/* if (fn == NULL)
909 /* if (child_stack == NULL)
915 /* Set up alignment of the child stack:
916 * child_stack = (child_stack & ~0xF) - 20;
921 /* Push "arg" and "fn" onto the stack that will be
925 "movl %%eax,4(%%ecx)\n"
927 "movl %%eax,(%%ecx)\n"
929 /* %eax = syscall(%eax = __NR_clone,
931 * %ecx = child_stack,
932 * %edx = parent_tidptr,
934 * %edi = child_tidptr)
935 * Also, make sure that %ebx gets preserved as it is
947 /* In the parent: restore %ebx
948 * In the child: move "fn" into %ebx
958 /* In the child, now. Terminate frame pointer chain.
962 /* Call "fn". "arg" is already on the stack.
966 /* Call _exit(%ebx). Unfortunately older versions
967 * of gcc restrict the number of arguments that can
968 * be passed to asm(). So, we need to hard-code the
969 * system call number.
979 : "0"(-EINVAL), "i"(__NR_clone),
980 "m"(fn), "m"(child_stack), "m"(flags), "m"(arg),
981 "m"(parent_tidptr), "m"(newtls), "m"(child_tidptr)
982 : "esp", "memory", "ecx", "edx", "esi", "edi");
983 LSS_RETURN(int, __res);
986 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
987 /* On i386, the kernel does not know how to return from a signal
988 * handler. Instead, it relies on user space to provide a
989 * restorer function that calls the {rt_,}sigreturn() system call.
990 * Unfortunately, we cannot just reference the glibc version of this
991 * function, as glibc goes out of its way to make it inaccessible.
994 __asm__ __volatile__("call 2f\n"
1001 : "i" (__NR_rt_sigreturn));
1004 LSS_INLINE void (*LSS_NAME(restore)(void))(void) {
1005 /* On i386, the kernel does not know how to return from a signal
1006 * handler. Instead, it relies on user space to provide a
1007 * restorer function that calls the {rt_,}sigreturn() system call.
1008 * Unfortunately, we cannot just reference the glibc version of this
1009 * function, as glibc goes out of its way to make it inaccessible.
1012 __asm__ __volatile__("call 2f\n"
1018 "addl $(1b-0b),%0\n"
1020 : "i" (__NR_sigreturn));
1023 #elif defined(__x86_64__)
1024 /* There are no known problems with any of the _syscallX() macros
1025 * currently shipping for x86_64, but we still need to be able to define
1026 * our own version so that we can override the location of the errno
1027 * location (e.g. when using the clone() system call with the CLONE_VM
1030 #undef LSS_ENTRYPOINT
1031 #define LSS_ENTRYPOINT "syscall\n"
1033 /* The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
1034 * We need to explicitly cast to an unsigned 64 bit type to avoid implicit
1035 * sign extension. We can't cast pointers directly because those are
1036 * 32 bits, and gcc will dump ugly warnings about casting from a pointer
1037 * to an integer of a different size.
1039 #undef LSS_SYSCALL_ARG
1040 #define LSS_SYSCALL_ARG(a) ((uint64_t)(uintptr_t)(a))
1042 #define _LSS_RETURN(type, res, cast) \
1044 if ((uint64_t)(res) >= (uint64_t)(-4095)) { \
1045 LSS_ERRNO = -(res); \
1048 return (type)(cast)(res); \
1051 #define LSS_RETURN(type, res) _LSS_RETURN(type, res, uintptr_t)
1054 #define _LSS_BODY(nr, type, name, cast, ...) \
1056 __asm__ __volatile__(LSS_BODY_ASM##nr LSS_ENTRYPOINT \
1058 : "0" (__NR_##name) LSS_BODY_ARG##nr(__VA_ARGS__) \
1059 : LSS_BODY_CLOBBER##nr "r11", "rcx", "memory"); \
1060 _LSS_RETURN(type, __res, cast)
1062 #define LSS_BODY(nr, type, name, args...) \
1063 _LSS_BODY(nr, type, name, uintptr_t, ## args)
1065 #undef LSS_BODY_ASM0
1066 #undef LSS_BODY_ASM1
1067 #undef LSS_BODY_ASM2
1068 #undef LSS_BODY_ASM3
1069 #undef LSS_BODY_ASM4
1070 #undef LSS_BODY_ASM5
1071 #undef LSS_BODY_ASM6
1072 #define LSS_BODY_ASM0
1073 #define LSS_BODY_ASM1 LSS_BODY_ASM0
1074 #define LSS_BODY_ASM2 LSS_BODY_ASM1
1075 #define LSS_BODY_ASM3 LSS_BODY_ASM2
1076 #define LSS_BODY_ASM4 LSS_BODY_ASM3 "movq %5,%%r10;"
1077 #define LSS_BODY_ASM5 LSS_BODY_ASM4 "movq %6,%%r8;"
1078 #define LSS_BODY_ASM6 LSS_BODY_ASM5 "movq %7,%%r9;"
1080 #undef LSS_BODY_CLOBBER0
1081 #undef LSS_BODY_CLOBBER1
1082 #undef LSS_BODY_CLOBBER2
1083 #undef LSS_BODY_CLOBBER3
1084 #undef LSS_BODY_CLOBBER4
1085 #undef LSS_BODY_CLOBBER5
1086 #undef LSS_BODY_CLOBBER6
1087 #define LSS_BODY_CLOBBER0
1088 #define LSS_BODY_CLOBBER1 LSS_BODY_CLOBBER0
1089 #define LSS_BODY_CLOBBER2 LSS_BODY_CLOBBER1
1090 #define LSS_BODY_CLOBBER3 LSS_BODY_CLOBBER2
1091 #define LSS_BODY_CLOBBER4 LSS_BODY_CLOBBER3 "r10",
1092 #define LSS_BODY_CLOBBER5 LSS_BODY_CLOBBER4 "r8",
1093 #define LSS_BODY_CLOBBER6 LSS_BODY_CLOBBER5 "r9",
1095 #undef LSS_BODY_ARG0
1096 #undef LSS_BODY_ARG1
1097 #undef LSS_BODY_ARG2
1098 #undef LSS_BODY_ARG3
1099 #undef LSS_BODY_ARG4
1100 #undef LSS_BODY_ARG5
1101 #undef LSS_BODY_ARG6
1102 #define LSS_BODY_ARG0()
1103 #define LSS_BODY_ARG1(arg1) \
1104 LSS_BODY_ARG0(), "D" (arg1)
1105 #define LSS_BODY_ARG2(arg1, arg2) \
1106 LSS_BODY_ARG1(arg1), "S" (arg2)
1107 #define LSS_BODY_ARG3(arg1, arg2, arg3) \
1108 LSS_BODY_ARG2(arg1, arg2), "d" (arg3)
1109 #define LSS_BODY_ARG4(arg1, arg2, arg3, arg4) \
1110 LSS_BODY_ARG3(arg1, arg2, arg3), "r" (arg4)
1111 #define LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5) \
1112 LSS_BODY_ARG4(arg1, arg2, arg3, arg4), "r" (arg5)
1113 #define LSS_BODY_ARG6(arg1, arg2, arg3, arg4, arg5, arg6) \
1114 LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5), "r" (arg6)
1117 #define _syscall0(type,name) \
1118 type LSS_NAME(name)() { \
1119 LSS_BODY(0, type, name); \
1122 #define _syscall1(type,name,type1,arg1) \
1123 type LSS_NAME(name)(type1 arg1) { \
1124 LSS_BODY(1, type, name, LSS_SYSCALL_ARG(arg1)); \
1127 #define _syscall2(type,name,type1,arg1,type2,arg2) \
1128 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
1129 LSS_BODY(2, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2));\
1132 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
1133 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
1134 LSS_BODY(3, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
1135 LSS_SYSCALL_ARG(arg3)); \
1138 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
1139 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
1140 LSS_BODY(4, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
1141 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4));\
1144 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1146 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1148 LSS_BODY(5, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
1149 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
1150 LSS_SYSCALL_ARG(arg5)); \
1153 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1154 type5,arg5,type6,arg6) \
1155 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1156 type5 arg5, type6 arg6) { \
1157 LSS_BODY(6, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
1158 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
1159 LSS_SYSCALL_ARG(arg5), LSS_SYSCALL_ARG(arg6));\
1161 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
1162 int flags, void *arg, int *parent_tidptr,
1163 void *newtls, int *child_tidptr) {
1166 __asm__ __volatile__(/* if (fn == NULL)
1172 /* if (child_stack == NULL)
1178 /* Set up alignment of the child stack:
1179 * child_stack = (child_stack & ~0xF) - 16;
1184 /* Push "arg" and "fn" onto the stack that will be
1185 * used by the child.
1190 /* %rax = syscall(%rax = __NR_clone,
1192 * %rsi = child_stack,
1193 * %rdx = parent_tidptr,
1195 * %r10 = child_tidptr)
1205 "testq %%rax,%%rax\n"
1208 /* In the child. Terminate frame pointer chain.
1210 "xorq %%rbp,%%rbp\n"
1218 /* Call _exit(%ebx).
1220 "movq %%rax,%%rdi\n"
1224 /* Return to parent.
1228 : "0"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit),
1229 "r"(LSS_SYSCALL_ARG(fn)),
1230 "S"(LSS_SYSCALL_ARG(child_stack)),
1231 "D"(LSS_SYSCALL_ARG(flags)),
1232 "r"(LSS_SYSCALL_ARG(arg)),
1233 "d"(LSS_SYSCALL_ARG(parent_tidptr)),
1234 "r"(LSS_SYSCALL_ARG(newtls)),
1235 "r"(LSS_SYSCALL_ARG(child_tidptr))
1236 : "rsp", "memory", "r8", "r10", "r11", "rcx");
1238 LSS_RETURN(int, __res);
1241 LSS_INLINE void (*LSS_NAME(restore_rt)(void))(void) {
1242 /* On x86-64, the kernel does not know how to return from
1243 * a signal handler. Instead, it relies on user space to provide a
1244 * restorer function that calls the rt_sigreturn() system call.
1245 * Unfortunately, we cannot just reference the glibc version of this
1246 * function, as glibc goes out of its way to make it inaccessible.
1249 __asm__ __volatile__("call 2f\n"
1254 "addq $(1b-0b),%0\n"
1256 : "i" (__NR_rt_sigreturn));
1257 return (void (*)(void))(uintptr_t)res;
1259 #elif defined(__arm__)
1260 /* Most definitions of _syscallX() neglect to mark "memory" as being
1261 * clobbered. This causes problems with compilers, that do a better job
1262 * at optimizing across __asm__ calls.
1263 * So, we just have to redefine all fo the _syscallX() macros.
1266 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
1268 /* r0..r3 are scratch registers and not preserved across function
1269 * calls. We need to first evaluate the first 4 syscall arguments
1270 * and store them on stack. They must be loaded into r0..r3 after
1271 * all function calls to avoid r0..r3 being clobbered.
1274 #define LSS_SAVE_ARG(r,a) long __tmp##r = (long)a
1276 #define LSS_LOAD_ARG(r) register long __r##r __asm__("r"#r) = __tmp##r
1279 #define LSS_BODY(type, name, args...) \
1280 register long __res_r0 __asm__("r0"); \
1283 __asm__ __volatile__ (__syscall_safe(name) \
1285 : __SYS_REG_LIST(args) \
1286 : "lr", "memory"); \
1288 LSS_RETURN(type, __res)
1290 #define _syscall0(type, name) \
1291 type LSS_NAME(name)() { \
1292 LSS_BODY(type, name); \
1295 #define _syscall1(type, name, type1, arg1) \
1296 type LSS_NAME(name)(type1 arg1) { \
1297 /* There is no need for using a volatile temp. */ \
1299 LSS_BODY(type, name, "r"(__r0)); \
1302 #define _syscall2(type, name, type1, arg1, type2, arg2) \
1303 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
1304 LSS_SAVE_ARG(0, arg1); \
1305 LSS_SAVE_ARG(1, arg2); \
1308 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
1311 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
1312 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
1313 LSS_SAVE_ARG(0, arg1); \
1314 LSS_SAVE_ARG(1, arg2); \
1315 LSS_SAVE_ARG(2, arg3); \
1319 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
1322 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
1324 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
1325 LSS_SAVE_ARG(0, arg1); \
1326 LSS_SAVE_ARG(1, arg2); \
1327 LSS_SAVE_ARG(2, arg3); \
1328 LSS_SAVE_ARG(3, arg4); \
1333 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
1336 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
1337 type4, arg4, type5, arg5) \
1338 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1340 LSS_SAVE_ARG(0, arg1); \
1341 LSS_SAVE_ARG(1, arg2); \
1342 LSS_SAVE_ARG(2, arg3); \
1343 LSS_SAVE_ARG(3, arg4); \
1349 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
1353 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
1354 type4, arg4, type5, arg5, type6, arg6) \
1355 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1356 type5 arg5, type6 arg6) { \
1357 LSS_SAVE_ARG(0, arg1); \
1358 LSS_SAVE_ARG(1, arg2); \
1359 LSS_SAVE_ARG(2, arg3); \
1360 LSS_SAVE_ARG(3, arg4); \
1367 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
1368 "r"(__r4), "r"(__r5)); \
1370 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
1371 int flags, void *arg, int *parent_tidptr,
1372 void *newtls, int *child_tidptr) {
1373 register long __res __asm__("r5");
1375 if (fn == NULL || child_stack == NULL) {
1380 /* stash first 4 arguments on stack first because we can only load
1381 * them after all function calls.
1383 int tmp_flags = flags;
1384 int * tmp_stack = (int*) child_stack;
1385 void * tmp_ptid = parent_tidptr;
1386 void * tmp_tls = newtls;
1388 register int *__ctid __asm__("r4") = child_tidptr;
1390 /* Push "arg" and "fn" onto the stack that will be
1391 * used by the child.
1393 *(--tmp_stack) = (int) arg;
1394 *(--tmp_stack) = (int) fn;
1396 /* We must load r0..r3 last after all possible function calls. */
1397 register int __flags __asm__("r0") = tmp_flags;
1398 register void *__stack __asm__("r1") = tmp_stack;
1399 register void *__ptid __asm__("r2") = tmp_ptid;
1400 register void *__tls __asm__("r3") = tmp_tls;
1402 /* %r0 = syscall(%r0 = flags,
1403 * %r1 = child_stack,
1404 * %r2 = parent_tidptr,
1406 * %r4 = child_tidptr)
1409 __asm__ __volatile__(/* %r0 = syscall(%r0 = flags,
1410 * %r1 = child_stack,
1411 * %r2 = parent_tidptr,
1413 * %r4 = child_tidptr)
1417 __syscall(clone)"\n"
1425 /* In the child, now. Call "fn(arg)".
1431 /* Call _exit(%r0), which never returns. We only
1432 * need to set r7 for EABI syscall ABI but we do
1433 * this always to simplify code sharing between
1434 * old and new syscall ABIs.
1439 /* Pop r7 from the stack only in the parent.
1444 "i"(__NR_exit), "r"(__stack), "r"(__flags),
1445 "r"(__ptid), "r"(__tls), "r"(__ctid)
1446 : "cc", "lr", "memory");
1449 LSS_RETURN(int, __res);
1451 #elif defined(__mips__)
1453 #define LSS_REG(r,a) register unsigned long __r##r __asm__("$"#r) = \
1456 #if _MIPS_SIM == _MIPS_SIM_ABI32
1457 // See http://sources.redhat.com/ml/libc-alpha/2004-10/msg00050.html
1458 // or http://www.linux-mips.org/archives/linux-mips/2004-10/msg00142.html
1459 #define MIPS_SYSCALL_CLOBBERS "$1", "$3", "$8", "$9", "$10", "$11", "$12",\
1460 "$13", "$14", "$15", "$24", "$25", "memory"
1462 #define MIPS_SYSCALL_CLOBBERS "$1", "$3", "$10", "$11", "$12", "$13", \
1463 "$14", "$15", "$24", "$25", "memory"
1467 #define LSS_BODY(type,name,r7,...) \
1468 register unsigned long __v0 __asm__("$2") = __NR_##name; \
1469 __asm__ __volatile__ ("syscall\n" \
1470 : "=&r"(__v0), r7 (__r7) \
1471 : "0"(__v0), ##__VA_ARGS__ \
1472 : MIPS_SYSCALL_CLOBBERS); \
1473 LSS_RETURN(type, __v0, __r7)
1475 #define _syscall0(type, name) \
1476 type LSS_NAME(name)() { \
1477 register unsigned long __r7 __asm__("$7"); \
1478 LSS_BODY(type, name, "=r"); \
1481 #define _syscall1(type, name, type1, arg1) \
1482 type LSS_NAME(name)(type1 arg1) { \
1483 register unsigned long __r7 __asm__("$7"); \
1484 LSS_REG(4, arg1); LSS_BODY(type, name, "=r", "r"(__r4)); \
1487 #define _syscall2(type, name, type1, arg1, type2, arg2) \
1488 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
1489 register unsigned long __r7 __asm__("$7"); \
1490 LSS_REG(4, arg1); LSS_REG(5, arg2); \
1491 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5)); \
1494 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
1495 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
1496 register unsigned long __r7 __asm__("$7"); \
1497 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
1498 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5), "r"(__r6)); \
1501 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
1502 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
1503 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
1505 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6)); \
1508 #if _MIPS_SIM == _MIPS_SIM_ABI32
1509 /* The old 32bit MIPS system call API passes the fifth and sixth argument
1510 * on the stack, whereas the new APIs use registers "r8" and "r9".
1512 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1514 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1516 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
1518 register unsigned long __v0 __asm__("$2"); \
1519 __asm__ __volatile__ (".set noreorder\n" \
1522 "sw $2, 16($29)\n" \
1527 : "=&r"(__v0), "+r" (__r7) \
1528 : "i" (__NR_##name), "r"(__r4), "r"(__r5), \
1529 "r"(__r6), "m" ((unsigned long)arg5) \
1530 : MIPS_SYSCALL_CLOBBERS); \
1531 LSS_RETURN(type, __v0, __r7); \
1534 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1536 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1538 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
1539 LSS_REG(7, arg4); LSS_REG(8, arg5); \
1540 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
1545 #if _MIPS_SIM == _MIPS_SIM_ABI32
1546 /* The old 32bit MIPS system call API passes the fifth and sixth argument
1547 * on the stack, whereas the new APIs use registers "r8" and "r9".
1549 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1550 type5,arg5,type6,arg6) \
1551 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1552 type5 arg5, type6 arg6) { \
1553 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
1555 register unsigned long __v0 __asm__("$2"); \
1556 __asm__ __volatile__ (".set noreorder\n" \
1560 "sw $2, 16($29)\n" \
1561 "sw $8, 20($29)\n" \
1566 : "=&r"(__v0), "+r" (__r7) \
1567 : "i" (__NR_##name), "r"(__r4), "r"(__r5), \
1568 "r"(__r6), "r" ((unsigned long)arg5), \
1569 "r" ((unsigned long)arg6) \
1570 : MIPS_SYSCALL_CLOBBERS); \
1571 LSS_RETURN(type, __v0, __r7); \
1574 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1575 type5,arg5,type6,arg6) \
1576 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1577 type5 arg5,type6 arg6) { \
1578 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
1579 LSS_REG(7, arg4); LSS_REG(8, arg5); LSS_REG(9, arg6); \
1580 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
1581 "r"(__r8), "r"(__r9)); \
1584 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
1585 int flags, void *arg, int *parent_tidptr,
1586 void *newtls, int *child_tidptr) {
1587 register unsigned long __v0 __asm__("$2");
1588 register unsigned long __r7 __asm__("$7") = (unsigned long)newtls;
1590 register int __flags __asm__("$4") = flags;
1591 register void *__stack __asm__("$5") = child_stack;
1592 register void *__ptid __asm__("$6") = parent_tidptr;
1593 register int *__ctid __asm__("$8") = child_tidptr;
1594 __asm__ __volatile__(
1595 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
1597 #elif _MIPS_SIM == _MIPS_SIM_NABI32
1603 /* if (fn == NULL || child_stack == NULL)
1610 /* Push "arg" and "fn" onto the stack that will be
1611 * used by the child.
1613 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
1617 #elif _MIPS_SIM == _MIPS_SIM_NABI32
1627 /* $7 = syscall($4 = flags,
1629 * $6 = parent_tidptr,
1631 * $8 = child_tidptr)
1642 /* In the child, now. Call "fn(arg)".
1644 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
1647 #elif _MIPS_SIM == _MIPS_SIM_NABI32
1663 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
1665 #elif _MIPS_SIM == _MIPS_SIM_NABI32
1670 : "=&r" (__v0), "=r" (__r7)
1671 : "i"(-EINVAL), "i"(__NR_clone), "i"(__NR_exit),
1672 "r"(fn), "r"(__stack), "r"(__flags), "r"(arg),
1673 "r"(__ptid), "r"(__r7), "r"(__ctid)
1674 : "$9", "$10", "$11", "$12", "$13", "$14", "$15",
1677 LSS_RETURN(int, __v0, __r7);
1679 #elif defined (__PPC__)
1680 #undef LSS_LOADARGS_0
1681 #define LSS_LOADARGS_0(name, dummy...) \
1682 __sc_0 = __NR_##name
1683 #undef LSS_LOADARGS_1
1684 #define LSS_LOADARGS_1(name, arg1) \
1685 LSS_LOADARGS_0(name); \
1686 __sc_3 = (unsigned long) (arg1)
1687 #undef LSS_LOADARGS_2
1688 #define LSS_LOADARGS_2(name, arg1, arg2) \
1689 LSS_LOADARGS_1(name, arg1); \
1690 __sc_4 = (unsigned long) (arg2)
1691 #undef LSS_LOADARGS_3
1692 #define LSS_LOADARGS_3(name, arg1, arg2, arg3) \
1693 LSS_LOADARGS_2(name, arg1, arg2); \
1694 __sc_5 = (unsigned long) (arg3)
1695 #undef LSS_LOADARGS_4
1696 #define LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4) \
1697 LSS_LOADARGS_3(name, arg1, arg2, arg3); \
1698 __sc_6 = (unsigned long) (arg4)
1699 #undef LSS_LOADARGS_5
1700 #define LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5) \
1701 LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4); \
1702 __sc_7 = (unsigned long) (arg5)
1703 #undef LSS_LOADARGS_6
1704 #define LSS_LOADARGS_6(name, arg1, arg2, arg3, arg4, arg5, arg6) \
1705 LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5); \
1706 __sc_8 = (unsigned long) (arg6)
1707 #undef LSS_ASMINPUT_0
1708 #define LSS_ASMINPUT_0 "0" (__sc_0)
1709 #undef LSS_ASMINPUT_1
1710 #define LSS_ASMINPUT_1 LSS_ASMINPUT_0, "1" (__sc_3)
1711 #undef LSS_ASMINPUT_2
1712 #define LSS_ASMINPUT_2 LSS_ASMINPUT_1, "2" (__sc_4)
1713 #undef LSS_ASMINPUT_3
1714 #define LSS_ASMINPUT_3 LSS_ASMINPUT_2, "3" (__sc_5)
1715 #undef LSS_ASMINPUT_4
1716 #define LSS_ASMINPUT_4 LSS_ASMINPUT_3, "4" (__sc_6)
1717 #undef LSS_ASMINPUT_5
1718 #define LSS_ASMINPUT_5 LSS_ASMINPUT_4, "5" (__sc_7)
1719 #undef LSS_ASMINPUT_6
1720 #define LSS_ASMINPUT_6 LSS_ASMINPUT_5, "6" (__sc_8)
1722 #define LSS_BODY(nr, type, name, args...) \
1723 long __sc_ret, __sc_err; \
1725 register unsigned long __sc_0 __asm__ ("r0"); \
1726 register unsigned long __sc_3 __asm__ ("r3"); \
1727 register unsigned long __sc_4 __asm__ ("r4"); \
1728 register unsigned long __sc_5 __asm__ ("r5"); \
1729 register unsigned long __sc_6 __asm__ ("r6"); \
1730 register unsigned long __sc_7 __asm__ ("r7"); \
1731 register unsigned long __sc_8 __asm__ ("r8"); \
1733 LSS_LOADARGS_##nr(name, args); \
1734 __asm__ __volatile__ \
1738 "=&r" (__sc_3), "=&r" (__sc_4), \
1739 "=&r" (__sc_5), "=&r" (__sc_6), \
1740 "=&r" (__sc_7), "=&r" (__sc_8) \
1741 : LSS_ASMINPUT_##nr \
1742 : "cr0", "ctr", "memory", \
1743 "r9", "r10", "r11", "r12"); \
1744 __sc_ret = __sc_3; \
1745 __sc_err = __sc_0; \
1747 LSS_RETURN(type, __sc_ret, __sc_err)
1749 #define _syscall0(type, name) \
1750 type LSS_NAME(name)(void) { \
1751 LSS_BODY(0, type, name); \
1754 #define _syscall1(type, name, type1, arg1) \
1755 type LSS_NAME(name)(type1 arg1) { \
1756 LSS_BODY(1, type, name, arg1); \
1759 #define _syscall2(type, name, type1, arg1, type2, arg2) \
1760 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
1761 LSS_BODY(2, type, name, arg1, arg2); \
1764 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
1765 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
1766 LSS_BODY(3, type, name, arg1, arg2, arg3); \
1769 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
1771 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
1772 LSS_BODY(4, type, name, arg1, arg2, arg3, arg4); \
1775 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
1776 type4, arg4, type5, arg5) \
1777 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1779 LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5); \
1782 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
1783 type4, arg4, type5, arg5, type6, arg6) \
1784 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1785 type5 arg5, type6 arg6) { \
1786 LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6); \
1788 /* clone function adapted from glibc 2.3.6 clone.S */
1789 /* TODO(csilvers): consider wrapping some args up in a struct, like we
1790 * do for i386's _syscall6, so we can compile successfully on gcc 2.95
1792 LSS_INLINE int LSS_NAME(clone)(int (*fn)(void *), void *child_stack,
1793 int flags, void *arg, int *parent_tidptr,
1794 void *newtls, int *child_tidptr) {
1797 register int (*__fn)(void *) __asm__ ("r8") = fn;
1798 register void *__cstack __asm__ ("r4") = child_stack;
1799 register int __flags __asm__ ("r3") = flags;
1800 register void * __arg __asm__ ("r9") = arg;
1801 register int * __ptidptr __asm__ ("r5") = parent_tidptr;
1802 register void * __newtls __asm__ ("r6") = newtls;
1803 register int * __ctidptr __asm__ ("r7") = child_tidptr;
1804 __asm__ __volatile__(
1805 /* check for fn == NULL
1806 * and child_stack == NULL
1808 "cmpwi cr0, %6, 0\n\t"
1809 "cmpwi cr1, %7, 0\n\t"
1810 "cror cr0*4+eq, cr1*4+eq, cr0*4+eq\n\t"
1813 /* set up stack frame for child */
1814 "clrrwi %7, %7, 4\n\t"
1816 "stwu 0, -16(%7)\n\t"
1818 /* fn, arg, child_stack are saved across the syscall: r28-30 */
1825 /* flags already in r3
1826 * child_stack already in r4
1827 * ptidptr already in r5
1828 * newtls already in r6
1829 * ctidptr already in r7
1833 /* Test if syscall was successful */
1834 "cmpwi cr1, 3, 0\n\t"
1835 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
1838 /* Do the function call */
1843 /* Call _exit(r3) */
1847 /* Return to parent */
1851 : "=r" (__ret), "=r" (__err)
1852 : "0" (-1), "1" (EINVAL),
1853 "i" (__NR_clone), "i" (__NR_exit),
1854 "r" (__fn), "r" (__cstack), "r" (__flags),
1855 "r" (__arg), "r" (__ptidptr), "r" (__newtls),
1857 : "cr0", "cr1", "memory", "ctr",
1858 "r0", "r29", "r27", "r28");
1860 LSS_RETURN(int, __ret, __err);
1863 #define __NR__exit __NR_exit
1864 #define __NR__gettid __NR_gettid
1865 #define __NR__mremap __NR_mremap
1866 LSS_INLINE _syscall1(int, close, int, f)
1867 LSS_INLINE _syscall1(int, _exit, int, e)
1868 LSS_INLINE _syscall3(int, fcntl, int, f,
1870 LSS_INLINE _syscall2(int, fstat, int, f,
1871 struct kernel_stat*, b)
1872 LSS_INLINE _syscall4(int, futex, int*, a,
1874 struct kernel_timespec*, t)
1875 LSS_INLINE _syscall3(int, getdents, int, f,
1876 struct kernel_dirent*, d, int, c)
1877 #ifdef __NR_getdents64
1878 LSS_INLINE _syscall3(int, getdents64, int, f,
1879 struct kernel_dirent64*, d, int, c)
1881 LSS_INLINE _syscall0(pid_t, getpid)
1882 LSS_INLINE _syscall0(pid_t, getppid)
1883 LSS_INLINE _syscall0(pid_t, _gettid)
1884 LSS_INLINE _syscall2(int, kill, pid_t, p,
1886 #if defined(__x86_64__)
1887 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
1888 LSS_INLINE off_t LSS_NAME(lseek)(int f, off_t o, int w) {
1889 _LSS_BODY(3, off_t, lseek, off_t, LSS_SYSCALL_ARG(f), (uint64_t)(o),
1890 LSS_SYSCALL_ARG(w));
1893 LSS_INLINE _syscall3(off_t, lseek, int, f,
1896 LSS_INLINE _syscall2(int, munmap, void*, s,
1898 LSS_INLINE _syscall5(void*, _mremap, void*, o,
1899 size_t, os, size_t, ns,
1900 unsigned long, f, void *, a)
1901 LSS_INLINE _syscall3(int, open, const char*, p,
1903 LSS_INLINE _syscall2(int, prctl, int, o,
1905 LSS_INLINE _syscall4(long, ptrace, int, r,
1906 pid_t, p, void *, a, void *, d)
1907 LSS_INLINE _syscall3(ssize_t, read, int, f,
1908 void *, b, size_t, c)
1909 LSS_INLINE _syscall4(int, rt_sigaction, int, s,
1910 const struct kernel_sigaction*, a,
1911 struct kernel_sigaction*, o, size_t, c)
1912 LSS_INLINE _syscall4(int, rt_sigprocmask, int, h,
1913 const struct kernel_sigset_t*, s,
1914 struct kernel_sigset_t*, o, size_t, c);
1915 LSS_INLINE _syscall0(int, sched_yield)
1916 LSS_INLINE _syscall2(int, sigaltstack, const stack_t*, s,
1918 LSS_INLINE _syscall2(int, stat, const char*, f,
1919 struct kernel_stat*, b)
1920 LSS_INLINE _syscall3(ssize_t, write, int, f,
1921 const void *, b, size_t, c)
1922 #if defined(__NR_getcpu)
1923 LSS_INLINE _syscall3(long, getcpu, unsigned *, cpu,
1924 unsigned *, node, void *, unused);
1926 #if defined(__x86_64__) || \
1927 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32)
1928 LSS_INLINE _syscall3(int, socket, int, d,
1931 #if defined(__x86_64__)
1932 /* Need to make sure __off64_t isn't truncated to 32-bits under x32. */
1933 LSS_INLINE void* LSS_NAME(mmap)(void *s, size_t l, int p, int f, int d,
1935 LSS_BODY(6, void*, mmap, LSS_SYSCALL_ARG(s), LSS_SYSCALL_ARG(l),
1936 LSS_SYSCALL_ARG(p), LSS_SYSCALL_ARG(f),
1937 LSS_SYSCALL_ARG(d), (uint64_t)(o));
1940 LSS_INLINE int LSS_NAME(sigaction)(int signum,
1941 const struct kernel_sigaction *act,
1942 struct kernel_sigaction *oldact) {
1943 /* On x86_64, the kernel requires us to always set our own
1944 * SA_RESTORER in order to be able to return from a signal handler.
1945 * This function must have a "magic" signature that the "gdb"
1946 * (and maybe the kernel?) can recognize.
1948 if (act != NULL && !(act->sa_flags & SA_RESTORER)) {
1949 struct kernel_sigaction a = *act;
1950 a.sa_flags |= SA_RESTORER;
1951 a.sa_restorer = LSS_NAME(restore_rt)();
1952 return LSS_NAME(rt_sigaction)(signum, &a, oldact,
1955 return LSS_NAME(rt_sigaction)(signum, act, oldact,
1960 LSS_INLINE int LSS_NAME(sigprocmask)(int how,
1961 const struct kernel_sigset_t *set,
1962 struct kernel_sigset_t *oldset) {
1963 return LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
1966 #if defined(__x86_64__) || \
1967 defined(__arm__) || \
1968 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32)
1969 LSS_INLINE _syscall4(pid_t, wait4, pid_t, p,
1971 struct kernel_rusage*, r)
1972 LSS_INLINE pid_t LSS_NAME(waitpid)(pid_t pid, int *status, int options){
1973 return LSS_NAME(wait4)(pid, status, options, 0);
1976 #if (defined(__i386__) || defined(__x86_64__) || defined(__arm__)) && \
1977 !defined(__ANDROID__)
1978 LSS_INLINE _syscall4(int, openat, int, d, const char *, p, int, f, int, m)
1980 LSS_INLINE int LSS_NAME(sigemptyset)(struct kernel_sigset_t *set) {
1981 memset(&set->sig, 0, sizeof(set->sig));
1985 LSS_INLINE int LSS_NAME(sigfillset)(struct kernel_sigset_t *set) {
1986 memset(&set->sig, -1, sizeof(set->sig));
1990 LSS_INLINE int LSS_NAME(sigaddset)(struct kernel_sigset_t *set,
1992 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
1996 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
1997 |= 1UL << ((signum - 1) % (8*sizeof(set->sig[0])));
2002 LSS_INLINE int LSS_NAME(sigdelset)(struct kernel_sigset_t *set,
2004 if (signum < 1 || signum > (int)(8*sizeof(set->sig))) {
2008 set->sig[(signum - 1)/(8*sizeof(set->sig[0]))]
2009 &= ~(1UL << ((signum - 1) % (8*sizeof(set->sig[0]))));
2014 #if defined(__i386__) || \
2015 defined(__arm__) || \
2016 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || defined(__PPC__)
2017 #define __NR__sigaction __NR_sigaction
2018 #define __NR__sigprocmask __NR_sigprocmask
2019 LSS_INLINE _syscall2(int, fstat64, int, f,
2020 struct kernel_stat64 *, b)
2021 LSS_INLINE _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
2022 loff_t *, res, uint, wh)
2024 LSS_INLINE _syscall6(void*, mmap, void*, s,
2029 #ifndef __ARM_EABI__
2030 /* Not available on ARM EABI Linux. */
2031 LSS_INLINE _syscall1(void*, mmap, void*, a)
2033 LSS_INLINE _syscall6(void*, mmap2, void*, s,
2038 LSS_INLINE _syscall3(int, _sigaction, int, s,
2039 const struct kernel_old_sigaction*, a,
2040 struct kernel_old_sigaction*, o)
2041 LSS_INLINE _syscall3(int, _sigprocmask, int, h,
2042 const unsigned long*, s,
2044 LSS_INLINE _syscall2(int, stat64, const char *, p,
2045 struct kernel_stat64 *, b)
2047 LSS_INLINE int LSS_NAME(sigaction)(int signum,
2048 const struct kernel_sigaction *act,
2049 struct kernel_sigaction *oldact) {
2050 int old_errno = LSS_ERRNO;
2052 struct kernel_sigaction a;
2056 /* On i386, the kernel requires us to always set our own
2057 * SA_RESTORER when using realtime signals. Otherwise, it does not
2058 * know how to return from a signal handler. This function must have
2059 * a "magic" signature that the "gdb" (and maybe the kernel?) can
2061 * Apparently, a SA_RESTORER is implicitly set by the kernel, when
2062 * using non-realtime signals.
2064 * TODO: Test whether ARM needs a restorer
2066 if (!(a.sa_flags & SA_RESTORER)) {
2067 a.sa_flags |= SA_RESTORER;
2068 a.sa_restorer = (a.sa_flags & SA_SIGINFO)
2069 ? LSS_NAME(restore_rt)() : LSS_NAME(restore)();
2073 rc = LSS_NAME(rt_sigaction)(signum, act ? &a : act, oldact,
2075 if (rc < 0 && LSS_ERRNO == ENOSYS) {
2076 struct kernel_old_sigaction oa, ooa, *ptr_a = &oa, *ptr_oa = &ooa;
2080 oa.sa_handler_ = act->sa_handler_;
2081 memcpy(&oa.sa_mask, &act->sa_mask, sizeof(oa.sa_mask));
2083 oa.sa_restorer = act->sa_restorer;
2085 oa.sa_flags = act->sa_flags;
2090 LSS_ERRNO = old_errno;
2091 rc = LSS_NAME(_sigaction)(signum, ptr_a, ptr_oa);
2092 if (rc == 0 && oldact) {
2094 memcpy(oldact, act, sizeof(*act));
2096 memset(oldact, 0, sizeof(*oldact));
2098 oldact->sa_handler_ = ptr_oa->sa_handler_;
2099 oldact->sa_flags = ptr_oa->sa_flags;
2100 memcpy(&oldact->sa_mask, &ptr_oa->sa_mask, sizeof(ptr_oa->sa_mask));
2102 oldact->sa_restorer = ptr_oa->sa_restorer;
2109 LSS_INLINE int LSS_NAME(sigprocmask)(int how,
2110 const struct kernel_sigset_t *set,
2111 struct kernel_sigset_t *oldset) {
2112 int olderrno = LSS_ERRNO;
2113 int rc = LSS_NAME(rt_sigprocmask)(how, set, oldset, (KERNEL_NSIG+7)/8);
2114 if (rc < 0 && LSS_ERRNO == ENOSYS) {
2115 LSS_ERRNO = olderrno;
2117 LSS_NAME(sigemptyset)(oldset);
2119 rc = LSS_NAME(_sigprocmask)(how,
2120 set ? &set->sig[0] : NULL,
2121 oldset ? &oldset->sig[0] : NULL);
2126 #if defined(__PPC__)
2127 #undef LSS_SC_LOADARGS_0
2128 #define LSS_SC_LOADARGS_0(dummy...)
2129 #undef LSS_SC_LOADARGS_1
2130 #define LSS_SC_LOADARGS_1(arg1) \
2131 __sc_4 = (unsigned long) (arg1)
2132 #undef LSS_SC_LOADARGS_2
2133 #define LSS_SC_LOADARGS_2(arg1, arg2) \
2134 LSS_SC_LOADARGS_1(arg1); \
2135 __sc_5 = (unsigned long) (arg2)
2136 #undef LSS_SC_LOADARGS_3
2137 #define LSS_SC_LOADARGS_3(arg1, arg2, arg3) \
2138 LSS_SC_LOADARGS_2(arg1, arg2); \
2139 __sc_6 = (unsigned long) (arg3)
2140 #undef LSS_SC_LOADARGS_4
2141 #define LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4) \
2142 LSS_SC_LOADARGS_3(arg1, arg2, arg3); \
2143 __sc_7 = (unsigned long) (arg4)
2144 #undef LSS_SC_LOADARGS_5
2145 #define LSS_SC_LOADARGS_5(arg1, arg2, arg3, arg4, arg5) \
2146 LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4); \
2147 __sc_8 = (unsigned long) (arg5)
2149 #define LSS_SC_BODY(nr, type, opt, args...) \
2150 long __sc_ret, __sc_err; \
2152 register unsigned long __sc_0 __asm__ ("r0") = __NR_socketcall; \
2153 register unsigned long __sc_3 __asm__ ("r3") = opt; \
2154 register unsigned long __sc_4 __asm__ ("r4"); \
2155 register unsigned long __sc_5 __asm__ ("r5"); \
2156 register unsigned long __sc_6 __asm__ ("r6"); \
2157 register unsigned long __sc_7 __asm__ ("r7"); \
2158 register unsigned long __sc_8 __asm__ ("r8"); \
2159 LSS_SC_LOADARGS_##nr(args); \
2160 __asm__ __volatile__ \
2161 ("stwu 1, -48(1)\n\t" \
2162 "stw 4, 20(1)\n\t" \
2163 "stw 5, 24(1)\n\t" \
2164 "stw 6, 28(1)\n\t" \
2165 "stw 7, 32(1)\n\t" \
2166 "stw 8, 36(1)\n\t" \
2167 "addi 4, 1, 20\n\t" \
2171 "=&r" (__sc_3), "=&r" (__sc_4), \
2172 "=&r" (__sc_5), "=&r" (__sc_6), \
2173 "=&r" (__sc_7), "=&r" (__sc_8) \
2174 : LSS_ASMINPUT_##nr \
2175 : "cr0", "ctr", "memory"); \
2176 __sc_ret = __sc_3; \
2177 __sc_err = __sc_0; \
2179 LSS_RETURN(type, __sc_ret, __sc_err)
2181 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
2182 LSS_SC_BODY(3, int, 1, domain, type, protocol);
2185 #if defined(__i386__) || \
2186 (defined(__arm__) && !defined(__ARM_EABI__)) || \
2187 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
2189 /* See sys_socketcall in net/socket.c in kernel source.
2190 * It de-multiplexes on its first arg and unpacks the arglist
2191 * array in its second arg.
2193 LSS_INLINE _syscall2(long, socketcall, int, c, unsigned long*, a)
2195 LSS_INLINE int LSS_NAME(socket)(int domain, int type, int protocol) {
2196 unsigned long args[3] = {
2197 (unsigned long) domain,
2198 (unsigned long) type,
2199 (unsigned long) protocol
2201 return LSS_NAME(socketcall)(1, args);
2203 #elif defined(__ARM_EABI__)
2204 LSS_INLINE _syscall3(int, socket, int, d,
2207 #if defined(__i386__) || defined(__PPC__) || \
2208 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
2209 LSS_INLINE _syscall3(pid_t, waitpid, pid_t, p,
2212 #if defined(__mips__)
2213 /* sys_pipe() on MIPS has non-standard calling conventions, as it returns
2214 * both file handles through CPU registers.
2216 LSS_INLINE int LSS_NAME(pipe)(int *p) {
2217 register unsigned long __v0 __asm__("$2") = __NR_pipe;
2218 register unsigned long __v1 __asm__("$3");
2219 register unsigned long __r7 __asm__("$7");
2220 __asm__ __volatile__ ("syscall\n"
2221 : "=&r"(__v0), "=&r"(__v1), "+r" (__r7)
2223 : "$8", "$9", "$10", "$11", "$12",
2224 "$13", "$14", "$15", "$24", "memory");
2235 LSS_INLINE _syscall1(int, pipe, int *, p)
2238 LSS_INLINE pid_t LSS_NAME(gettid)() {
2239 pid_t tid = LSS_NAME(_gettid)();
2243 return LSS_NAME(getpid)();
2246 LSS_INLINE void *LSS_NAME(mremap)(void *old_address, size_t old_size,
2247 size_t new_size, int flags, ...) {
2249 void *new_address, *rc;
2250 va_start(ap, flags);
2251 new_address = va_arg(ap, void *);
2252 rc = LSS_NAME(_mremap)(old_address, old_size, new_size,
2253 flags, new_address);
2258 LSS_INLINE int LSS_NAME(ptrace_detach)(pid_t pid) {
2259 /* PTRACE_DETACH can sometimes forget to wake up the tracee and it
2260 * then sends job control signals to the real parent, rather than to
2261 * the tracer. We reduce the risk of this happening by starting a
2262 * whole new time slice, and then quickly sending a SIGCONT signal
2263 * right after detaching from the tracee.
2266 LSS_NAME(sched_yield)();
2267 rc = LSS_NAME(ptrace)(PTRACE_DETACH, pid, (void *)0, (void *)0);
2269 LSS_NAME(kill)(pid, SIGCONT);
2275 #if defined(__cplusplus) && !defined(SYS_CPLUSPLUS)