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 #include <sys/linux-syscalls.h>
165 #include <linux/unistd.h>
169 /* Include definitions of the ABI currently in use. */
175 /* As glibc often provides subtly incompatible data structures (and implicit
176 * wrapper functions that convert them), we provide our own kernel data
177 * structures for use by the system calls.
178 * These structures have been developed by using Linux 2.6.23 headers for
179 * reference. Note though, we do not care about exact API compatibility
180 * with the kernel, and in fact the kernel often does not have a single
181 * API that works across architectures. Instead, we try to mimic the glibc
182 * API where reasonable, and only guarantee ABI compatibility with the
184 * Most notably, here are a few changes that were made to the structures
185 * defined by kernel headers:
187 * - we only define structures, but not symbolic names for kernel data
188 * types. For the latter, we directly use the native C datatype
189 * (i.e. "unsigned" instead of "mode_t").
190 * - in a few cases, it is possible to define identical structures for
191 * both 32bit (e.g. i386) and 64bit (e.g. x86-64) platforms by
192 * standardizing on the 64bit version of the data types. In particular,
193 * this means that we use "unsigned" where the 32bit headers say
195 * - overall, we try to minimize the number of cases where we need to
196 * conditionally define different structures.
197 * - the "struct kernel_sigaction" class of structures have been
198 * modified to more closely mimic glibc's API by introducing an
199 * anonymous union for the function pointer.
200 * - a small number of field names had to have an underscore appended to
201 * them, because glibc defines a global macro by the same name.
204 /* include/linux/dirent.h */
205 struct kernel_dirent64
{
206 unsigned long long d_ino
;
208 unsigned short d_reclen
;
209 unsigned char d_type
;
213 /* include/linux/dirent.h */
214 struct kernel_dirent
{
217 unsigned short d_reclen
;
221 /* include/linux/time.h */
222 struct kernel_timespec
{
227 /* include/linux/time.h */
228 struct kernel_timeval
{
233 /* include/linux/resource.h */
234 struct kernel_rusage
{
235 struct kernel_timeval ru_utime
;
236 struct kernel_timeval ru_stime
;
253 #if defined(__i386__) || defined(__arm__) || defined(__PPC__)
255 /* include/asm-{arm,i386,mips,ppc}/signal.h */
256 struct kernel_old_sigaction
{
258 void (*sa_handler_
)(int);
259 void (*sa_sigaction_
)(int, siginfo_t
*, void *);
261 unsigned long sa_mask
;
262 unsigned long sa_flags
;
263 void (*sa_restorer
)(void);
264 } __attribute__((packed
,aligned(4)));
265 #elif (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
266 #define kernel_old_sigaction kernel_sigaction
269 /* Some kernel functions (e.g. sigaction() in 2.6.23) require that the
270 * exactly match the size of the signal set, even though the API was
271 * intended to be extensible. We define our own KERNEL_NSIG to deal with
273 * Please note that glibc provides signals [1.._NSIG-1], whereas the
274 * kernel (and this header) provides the range [1..KERNEL_NSIG]. The
275 * actual number of signals is obviously the same, but the constants
279 #define KERNEL_NSIG 128
281 #define KERNEL_NSIG 64
284 /* include/asm-{arm,i386,mips,x86_64}/signal.h */
285 struct kernel_sigset_t
{
286 unsigned long sig
[(KERNEL_NSIG
+ 8*sizeof(unsigned long) - 1)/
287 (8*sizeof(unsigned long))];
290 /* include/asm-{arm,i386,mips,x86_64,ppc}/signal.h */
291 struct kernel_sigaction
{
293 unsigned long sa_flags
;
295 void (*sa_handler_
)(int);
296 void (*sa_sigaction_
)(int, siginfo_t
*, void *);
298 struct kernel_sigset_t sa_mask
;
301 void (*sa_handler_
)(int);
302 void (*sa_sigaction_
)(int, siginfo_t
*, void *);
304 unsigned long sa_flags
;
305 void (*sa_restorer
)(void);
306 struct kernel_sigset_t sa_mask
;
310 /* include/asm-{arm,i386,mips,ppc}/stat.h */
312 #if _MIPS_SIM == _MIPS_SIM_ABI64
315 struct kernel_stat64
{
319 unsigned long long st_ino
;
328 unsigned st_atime_nsec_
;
330 unsigned st_mtime_nsec_
;
332 unsigned st_ctime_nsec_
;
335 unsigned long long st_blocks
;
337 #elif defined __PPC__
338 struct kernel_stat64
{
339 unsigned long long st_dev
;
340 unsigned long long st_ino
;
345 unsigned long long st_rdev
;
346 unsigned short int __pad2
;
351 unsigned long st_atime_nsec_
;
353 unsigned long st_mtime_nsec_
;
355 unsigned long st_ctime_nsec_
;
356 unsigned long __unused4
;
357 unsigned long __unused5
;
360 struct kernel_stat64
{
361 unsigned long long st_dev
;
362 unsigned char __pad0
[4];
368 unsigned long long st_rdev
;
369 unsigned char __pad3
[4];
372 unsigned long long st_blocks
;
374 unsigned st_atime_nsec_
;
376 unsigned st_mtime_nsec_
;
378 unsigned st_ctime_nsec_
;
379 unsigned long long st_ino
;
383 /* include/asm-{arm,i386,mips,x86_64,ppc}/stat.h */
384 #if defined(__i386__) || defined(__arm__)
386 /* The kernel headers suggest that st_dev and st_rdev should be 32bit
387 * quantities encoding 12bit major and 20bit minor numbers in an interleaved
388 * format. In reality, we do not see useful data in the top bits. So,
389 * we'll leave the padding in here, until we find a better solution.
391 unsigned short st_dev
;
394 unsigned short st_mode
;
395 unsigned short st_nlink
;
396 unsigned short st_uid
;
397 unsigned short st_gid
;
398 unsigned short st_rdev
;
404 unsigned st_atime_nsec_
;
406 unsigned st_mtime_nsec_
;
408 unsigned st_ctime_nsec_
;
412 #elif defined(__x86_64__)
426 uint64_t st_atime_nsec_
;
428 uint64_t st_mtime_nsec_
;
430 uint64_t st_ctime_nsec_
;
433 #elif defined(__PPC__)
436 unsigned long st_ino
; // ino_t
437 unsigned long st_mode
; // mode_t
438 unsigned short st_nlink
; // nlink_t
439 unsigned st_uid
; // uid_t
440 unsigned st_gid
; // gid_t
442 long st_size
; // off_t
443 unsigned long st_blksize
;
444 unsigned long st_blocks
;
445 unsigned long st_atime_
;
446 unsigned long st_atime_nsec_
;
447 unsigned long st_mtime_
;
448 unsigned long st_mtime_nsec_
;
449 unsigned long st_ctime_
;
450 unsigned long st_ctime_nsec_
;
451 unsigned long __unused4
;
452 unsigned long __unused5
;
454 #elif (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI64)
479 // ulong is not defined in Android while used to define __llseek.
480 #if defined(__ANDROID__)
481 typedef unsigned long int ulong
;
485 /* Definitions missing from the standard header files */
488 #define O_DIRECTORY 0040000
490 #define O_DIRECTORY 0200000
493 #ifndef PR_GET_DUMPABLE
494 #define PR_GET_DUMPABLE 3
496 #ifndef PR_SET_DUMPABLE
497 #define PR_SET_DUMPABLE 4
500 #define AT_FDCWD (-100)
502 #ifndef AT_SYMLINK_NOFOLLOW
503 #define AT_SYMLINK_NOFOLLOW 0x100
506 #define AT_REMOVEDIR 0x200
509 #define MREMAP_FIXED 2
512 #define SA_RESTORER 0x04000000
515 #if defined(__i386__)
516 #ifndef __NR_rt_sigaction
517 #define __NR_rt_sigaction 174
518 #define __NR_rt_sigprocmask 175
521 #define __NR_stat64 195
524 #define __NR_fstat64 197
526 #ifndef __NR_getdents64
527 #define __NR_getdents64 220
530 #define __NR_gettid 224
533 #define __NR_futex 240
536 #define __NR_openat 295
539 #define __NR_getcpu 318
541 /* End of i386 definitions */
542 #elif defined(__arm__)
544 #if defined(__thumb__) || defined(__ARM_EABI__)
545 #define __SYS_REG(name) register long __sysreg __asm__("r6") = __NR_##name;
546 #define __SYS_REG_LIST(regs...) [sysreg] "r" (__sysreg) , ##regs
547 #define __syscall(name) "swi\t0"
548 #define __syscall_safe(name) \
550 "mov r7,%[sysreg]\n" \
551 __syscall(name)"\n" \
554 #define __SYS_REG(name)
555 #define __SYS_REG_LIST(regs...) regs
556 #define __syscall(name) "swi\t" __sys1(__NR_##name) ""
557 #define __syscall_safe(name) __syscall(name)
560 #ifndef __NR_rt_sigaction
561 #define __NR_rt_sigaction (__NR_SYSCALL_BASE + 174)
562 #define __NR_rt_sigprocmask (__NR_SYSCALL_BASE + 175)
565 #define __NR_stat64 (__NR_SYSCALL_BASE + 195)
568 #define __NR_fstat64 (__NR_SYSCALL_BASE + 197)
570 #ifndef __NR_getdents64
571 #define __NR_getdents64 (__NR_SYSCALL_BASE + 217)
574 #define __NR_gettid (__NR_SYSCALL_BASE + 224)
577 #define __NR_futex (__NR_SYSCALL_BASE + 240)
579 /* End of ARM definitions */
580 #elif defined(__x86_64__)
582 #define __NR_gettid 186
585 #define __NR_futex 202
587 #ifndef __NR_getdents64
588 #define __NR_getdents64 217
591 #define __NR_openat 257
593 /* End of x86-64 definitions */
594 #elif defined(__mips__)
595 #if _MIPS_SIM == _MIPS_SIM_ABI32
596 #ifndef __NR_rt_sigaction
597 #define __NR_rt_sigaction (__NR_Linux + 194)
598 #define __NR_rt_sigprocmask (__NR_Linux + 195)
601 #define __NR_stat64 (__NR_Linux + 213)
604 #define __NR_fstat64 (__NR_Linux + 215)
606 #ifndef __NR_getdents64
607 #define __NR_getdents64 (__NR_Linux + 219)
610 #define __NR_gettid (__NR_Linux + 222)
613 #define __NR_futex (__NR_Linux + 238)
616 #define __NR_openat (__NR_Linux + 288)
619 #define __NR_fstatat (__NR_Linux + 293)
622 #define __NR_getcpu (__NR_Linux + 312)
624 /* End of MIPS (old 32bit API) definitions */
625 #elif _MIPS_SIM == _MIPS_SIM_ABI64
627 #define __NR_gettid (__NR_Linux + 178)
630 #define __NR_futex (__NR_Linux + 194)
633 #define __NR_openat (__NR_Linux + 247)
636 #define __NR_fstatat (__NR_Linux + 252)
639 #define __NR_getcpu (__NR_Linux + 271)
641 /* End of MIPS (64bit API) definitions */
644 #define __NR_gettid (__NR_Linux + 178)
647 #define __NR_futex (__NR_Linux + 194)
650 #define __NR_openat (__NR_Linux + 251)
653 #define __NR_fstatat (__NR_Linux + 256)
656 #define __NR_getcpu (__NR_Linux + 275)
658 /* End of MIPS (new 32bit API) definitions */
660 /* End of MIPS definitions */
661 #elif defined(__PPC__)
662 #ifndef __NR_rt_sigaction
663 #define __NR_rt_sigaction 173
664 #define __NR_rt_sigprocmask 174
667 #define __NR_stat64 195
670 #define __NR_fstat64 197
672 #ifndef __NR_getdents64
673 #define __NR_getdents64 202
676 #define __NR_gettid 207
679 #define __NR_futex 221
682 #define __NR_openat 286
685 #define __NR_getcpu 302
687 /* End of powerpc defininitions */
691 /* After forking, we must make sure to only call system calls. */
692 #if __BOUNDED_POINTERS__
693 #error "Need to port invocations of syscalls for bounded ptrs"
695 /* The core dumper and the thread lister get executed after threads
696 * have been suspended. As a consequence, we cannot call any functions
697 * that acquire locks. Unfortunately, libc wraps most system calls
698 * (e.g. in order to implement pthread_atfork, and to make calls
699 * cancellable), which means we cannot call these functions. Instead,
700 * we have to call syscall() directly.
704 /* Allow the including file to override the location of errno. This can
705 * be useful when using clone() with the CLONE_VM option.
707 #define LSS_ERRNO SYS_ERRNO
709 #define LSS_ERRNO errno
714 #define LSS_INLINE SYS_INLINE
716 #define LSS_INLINE static inline
719 /* Allow the including file to override the prefix used for all new
720 * system calls. By default, it will be set to "sys_".
724 #define LSS_NAME(name) sys_##name
726 #define LSS_NAME(name) name
727 #elif SYS_PREFIX == 0
728 #define LSS_NAME(name) sys0_##name
729 #elif SYS_PREFIX == 1
730 #define LSS_NAME(name) sys1_##name
731 #elif SYS_PREFIX == 2
732 #define LSS_NAME(name) sys2_##name
733 #elif SYS_PREFIX == 3
734 #define LSS_NAME(name) sys3_##name
735 #elif SYS_PREFIX == 4
736 #define LSS_NAME(name) sys4_##name
737 #elif SYS_PREFIX == 5
738 #define LSS_NAME(name) sys5_##name
739 #elif SYS_PREFIX == 6
740 #define LSS_NAME(name) sys6_##name
741 #elif SYS_PREFIX == 7
742 #define LSS_NAME(name) sys7_##name
743 #elif SYS_PREFIX == 8
744 #define LSS_NAME(name) sys8_##name
745 #elif SYS_PREFIX == 9
746 #define LSS_NAME(name) sys9_##name
750 #if (defined(__i386__) || defined(__x86_64__) || defined(__arm__))
751 /* Failing system calls return a negative result in the range of
752 * -1..-4095. These are "errno" values with the sign inverted.
754 #define LSS_RETURN(type, res) \
756 if ((unsigned long)(res) >= (unsigned long)(-4095)) { \
757 LSS_ERRNO = -(res); \
760 return (type) (res); \
762 #elif defined(__mips__)
763 /* On MIPS, failing system calls return -1, and set errno in a
764 * separate CPU register.
766 #define LSS_RETURN(type, res, err) \
772 return (type) (res); \
774 #elif defined(__PPC__)
775 /* On PPC, failing system calls return -1, and set errno in a
776 * separate CPU register. See linux/unistd.h.
778 #define LSS_RETURN(type, res, err) \
780 if (err & 0x10000000 ) { \
784 return (type) (res); \
787 #if defined(__i386__)
788 #if defined(NO_FRAME_POINTER) && (100 * __GNUC__ + __GNUC_MINOR__ >= 404)
789 /* This only works for GCC-4.4 and above -- the first version to use
790 .cfi directives for dwarf unwind info. */
791 #define CFI_ADJUST_CFA_OFFSET(adjust) \
792 ".cfi_adjust_cfa_offset " #adjust "\n"
794 #define CFI_ADJUST_CFA_OFFSET(adjust) /**/
797 /* In PIC mode (e.g. when building shared libraries), gcc for i386
798 * reserves ebx. Unfortunately, most distribution ship with implementations
799 * of _syscallX() which clobber ebx.
800 * Also, most definitions of _syscallX() neglect to mark "memory" as being
801 * clobbered. This causes problems with compilers, that do a better job
802 * at optimizing across __asm__ calls.
803 * So, we just have to redefine all of the _syscallX() macros.
806 #define LSS_BODY(type,args...) \
808 __asm__ __volatile__("push %%ebx\n" \
809 CFI_ADJUST_CFA_OFFSET(4) \
813 CFI_ADJUST_CFA_OFFSET(-4) \
815 : "esp", "memory"); \
816 LSS_RETURN(type,__res)
818 #define _syscall0(type,name) \
819 type LSS_NAME(name)(void) { \
821 __asm__ volatile("int $0x80" \
823 : "0" (__NR_##name) \
825 LSS_RETURN(type,__res); \
828 #define _syscall1(type,name,type1,arg1) \
829 type LSS_NAME(name)(type1 arg1) { \
832 : "0" (__NR_##name), "ri" ((long)(arg1))); \
835 #define _syscall2(type,name,type1,arg1,type2,arg2) \
836 type LSS_NAME(name)(type1 arg1,type2 arg2) { \
839 : "0" (__NR_##name),"ri" ((long)(arg1)), "c" ((long)(arg2))); \
842 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
843 type LSS_NAME(name)(type1 arg1,type2 arg2,type3 arg3) { \
846 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
847 "d" ((long)(arg3))); \
850 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
851 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
854 : "0" (__NR_##name), "ri" ((long)(arg1)), "c" ((long)(arg2)), \
855 "d" ((long)(arg3)),"S" ((long)(arg4))); \
858 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
860 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
863 __asm__ __volatile__("push %%ebx\n" \
869 : "i" (__NR_##name), "ri" ((long)(arg1)), \
870 "c" ((long)(arg2)), "d" ((long)(arg3)), \
871 "S" ((long)(arg4)), "D" ((long)(arg5)) \
872 : "esp", "memory"); \
873 LSS_RETURN(type,__res); \
876 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
877 type5,arg5,type6,arg6) \
878 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
879 type5 arg5, type6 arg6) { \
881 struct { long __a1; long __a6; } __s = { (long)arg1, (long) arg6 }; \
882 __asm__ __volatile__("push %%ebp\n" \
884 "movl 4(%2),%%ebp\n" \
885 "movl 0(%2), %%ebx\n" \
891 : "i" (__NR_##name), "0" ((long)(&__s)), \
892 "c" ((long)(arg2)), "d" ((long)(arg3)), \
893 "S" ((long)(arg4)), "D" ((long)(arg5)) \
894 : "esp", "memory"); \
895 LSS_RETURN(type,__res); \
897 LSS_INLINE
int LSS_NAME(clone
)(int (*fn
)(void *), void *child_stack
,
898 int flags
, void *arg
, int *parent_tidptr
,
899 void *newtls
, int *child_tidptr
) {
901 __asm__
__volatile__(/* if (fn == NULL)
907 /* if (child_stack == NULL)
913 /* Set up alignment of the child stack:
914 * child_stack = (child_stack & ~0xF) - 20;
919 /* Push "arg" and "fn" onto the stack that will be
923 "movl %%eax,4(%%ecx)\n"
925 "movl %%eax,(%%ecx)\n"
927 /* %eax = syscall(%eax = __NR_clone,
929 * %ecx = child_stack,
930 * %edx = parent_tidptr,
932 * %edi = child_tidptr)
933 * Also, make sure that %ebx gets preserved as it is
945 /* In the parent: restore %ebx
946 * In the child: move "fn" into %ebx
956 /* In the child, now. Terminate frame pointer chain.
960 /* Call "fn". "arg" is already on the stack.
964 /* Call _exit(%ebx). Unfortunately older versions
965 * of gcc restrict the number of arguments that can
966 * be passed to asm(). So, we need to hard-code the
967 * system call number.
977 : "0"(-EINVAL
), "i"(__NR_clone
),
978 "m"(fn
), "m"(child_stack
), "m"(flags
), "m"(arg
),
979 "m"(parent_tidptr
), "m"(newtls
), "m"(child_tidptr
)
980 : "esp", "memory", "ecx", "edx", "esi", "edi");
981 LSS_RETURN(int, __res
);
984 LSS_INLINE
void (*LSS_NAME(restore_rt
)(void))(void) {
985 /* On i386, the kernel does not know how to return from a signal
986 * handler. Instead, it relies on user space to provide a
987 * restorer function that calls the {rt_,}sigreturn() system call.
988 * Unfortunately, we cannot just reference the glibc version of this
989 * function, as glibc goes out of its way to make it inaccessible.
992 __asm__
__volatile__("call 2f\n"
999 : "i" (__NR_rt_sigreturn
));
1002 LSS_INLINE
void (*LSS_NAME(restore
)(void))(void) {
1003 /* On i386, the kernel does not know how to return from a signal
1004 * handler. Instead, it relies on user space to provide a
1005 * restorer function that calls the {rt_,}sigreturn() system call.
1006 * Unfortunately, we cannot just reference the glibc version of this
1007 * function, as glibc goes out of its way to make it inaccessible.
1010 __asm__
__volatile__("call 2f\n"
1016 "addl $(1b-0b),%0\n"
1018 : "i" (__NR_sigreturn
));
1021 #elif defined(__x86_64__)
1022 /* There are no known problems with any of the _syscallX() macros
1023 * currently shipping for x86_64, but we still need to be able to define
1024 * our own version so that we can override the location of the errno
1025 * location (e.g. when using the clone() system call with the CLONE_VM
1028 #undef LSS_ENTRYPOINT
1029 #define LSS_ENTRYPOINT "syscall\n"
1031 /* The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
1032 * We need to explicitly cast to an unsigned 64 bit type to avoid implicit
1033 * sign extension. We can't cast pointers directly because those are
1034 * 32 bits, and gcc will dump ugly warnings about casting from a pointer
1035 * to an integer of a different size.
1037 #undef LSS_SYSCALL_ARG
1038 #define LSS_SYSCALL_ARG(a) ((uint64_t)(uintptr_t)(a))
1040 #define _LSS_RETURN(type, res, cast) \
1042 if ((uint64_t)(res) >= (uint64_t)(-4095)) { \
1043 LSS_ERRNO = -(res); \
1046 return (type)(cast)(res); \
1049 #define LSS_RETURN(type, res) _LSS_RETURN(type, res, uintptr_t)
1052 #define _LSS_BODY(nr, type, name, cast, ...) \
1054 __asm__ __volatile__(LSS_BODY_ASM##nr LSS_ENTRYPOINT \
1056 : "0" (__NR_##name) LSS_BODY_ARG##nr(__VA_ARGS__) \
1057 : LSS_BODY_CLOBBER##nr "r11", "rcx", "memory"); \
1058 _LSS_RETURN(type, __res, cast)
1060 #define LSS_BODY(nr, type, name, args...) \
1061 _LSS_BODY(nr, type, name, uintptr_t, ## args)
1063 #undef LSS_BODY_ASM0
1064 #undef LSS_BODY_ASM1
1065 #undef LSS_BODY_ASM2
1066 #undef LSS_BODY_ASM3
1067 #undef LSS_BODY_ASM4
1068 #undef LSS_BODY_ASM5
1069 #undef LSS_BODY_ASM6
1070 #define LSS_BODY_ASM0
1071 #define LSS_BODY_ASM1 LSS_BODY_ASM0
1072 #define LSS_BODY_ASM2 LSS_BODY_ASM1
1073 #define LSS_BODY_ASM3 LSS_BODY_ASM2
1074 #define LSS_BODY_ASM4 LSS_BODY_ASM3 "movq %5,%%r10;"
1075 #define LSS_BODY_ASM5 LSS_BODY_ASM4 "movq %6,%%r8;"
1076 #define LSS_BODY_ASM6 LSS_BODY_ASM5 "movq %7,%%r9;"
1078 #undef LSS_BODY_CLOBBER0
1079 #undef LSS_BODY_CLOBBER1
1080 #undef LSS_BODY_CLOBBER2
1081 #undef LSS_BODY_CLOBBER3
1082 #undef LSS_BODY_CLOBBER4
1083 #undef LSS_BODY_CLOBBER5
1084 #undef LSS_BODY_CLOBBER6
1085 #define LSS_BODY_CLOBBER0
1086 #define LSS_BODY_CLOBBER1 LSS_BODY_CLOBBER0
1087 #define LSS_BODY_CLOBBER2 LSS_BODY_CLOBBER1
1088 #define LSS_BODY_CLOBBER3 LSS_BODY_CLOBBER2
1089 #define LSS_BODY_CLOBBER4 LSS_BODY_CLOBBER3 "r10",
1090 #define LSS_BODY_CLOBBER5 LSS_BODY_CLOBBER4 "r8",
1091 #define LSS_BODY_CLOBBER6 LSS_BODY_CLOBBER5 "r9",
1093 #undef LSS_BODY_ARG0
1094 #undef LSS_BODY_ARG1
1095 #undef LSS_BODY_ARG2
1096 #undef LSS_BODY_ARG3
1097 #undef LSS_BODY_ARG4
1098 #undef LSS_BODY_ARG5
1099 #undef LSS_BODY_ARG6
1100 #define LSS_BODY_ARG0()
1101 #define LSS_BODY_ARG1(arg1) \
1102 LSS_BODY_ARG0(), "D" (arg1)
1103 #define LSS_BODY_ARG2(arg1, arg2) \
1104 LSS_BODY_ARG1(arg1), "S" (arg2)
1105 #define LSS_BODY_ARG3(arg1, arg2, arg3) \
1106 LSS_BODY_ARG2(arg1, arg2), "d" (arg3)
1107 #define LSS_BODY_ARG4(arg1, arg2, arg3, arg4) \
1108 LSS_BODY_ARG3(arg1, arg2, arg3), "r" (arg4)
1109 #define LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5) \
1110 LSS_BODY_ARG4(arg1, arg2, arg3, arg4), "r" (arg5)
1111 #define LSS_BODY_ARG6(arg1, arg2, arg3, arg4, arg5, arg6) \
1112 LSS_BODY_ARG5(arg1, arg2, arg3, arg4, arg5), "r" (arg6)
1115 #define _syscall0(type,name) \
1116 type LSS_NAME(name)() { \
1117 LSS_BODY(0, type, name); \
1120 #define _syscall1(type,name,type1,arg1) \
1121 type LSS_NAME(name)(type1 arg1) { \
1122 LSS_BODY(1, type, name, LSS_SYSCALL_ARG(arg1)); \
1125 #define _syscall2(type,name,type1,arg1,type2,arg2) \
1126 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
1127 LSS_BODY(2, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2));\
1130 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
1131 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
1132 LSS_BODY(3, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
1133 LSS_SYSCALL_ARG(arg3)); \
1136 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
1137 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
1138 LSS_BODY(4, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
1139 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4));\
1142 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1144 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1146 LSS_BODY(5, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
1147 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
1148 LSS_SYSCALL_ARG(arg5)); \
1151 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1152 type5,arg5,type6,arg6) \
1153 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1154 type5 arg5, type6 arg6) { \
1155 LSS_BODY(6, type, name, LSS_SYSCALL_ARG(arg1), LSS_SYSCALL_ARG(arg2), \
1156 LSS_SYSCALL_ARG(arg3), LSS_SYSCALL_ARG(arg4), \
1157 LSS_SYSCALL_ARG(arg5), LSS_SYSCALL_ARG(arg6));\
1159 LSS_INLINE
int LSS_NAME(clone
)(int (*fn
)(void *), void *child_stack
,
1160 int flags
, void *arg
, int *parent_tidptr
,
1161 void *newtls
, int *child_tidptr
) {
1164 __asm__
__volatile__(/* if (fn == NULL)
1170 /* if (child_stack == NULL)
1176 /* Set up alignment of the child stack:
1177 * child_stack = (child_stack & ~0xF) - 16;
1182 /* Push "arg" and "fn" onto the stack that will be
1183 * used by the child.
1188 /* %rax = syscall(%rax = __NR_clone,
1190 * %rsi = child_stack,
1191 * %rdx = parent_tidptr,
1193 * %r10 = child_tidptr)
1203 "testq %%rax,%%rax\n"
1206 /* In the child. Terminate frame pointer chain.
1208 "xorq %%rbp,%%rbp\n"
1216 /* Call _exit(%ebx).
1218 "movq %%rax,%%rdi\n"
1222 /* Return to parent.
1226 : "0"(-EINVAL
), "i"(__NR_clone
), "i"(__NR_exit
),
1227 "r"(LSS_SYSCALL_ARG(fn
)),
1228 "S"(LSS_SYSCALL_ARG(child_stack
)),
1229 "D"(LSS_SYSCALL_ARG(flags
)),
1230 "r"(LSS_SYSCALL_ARG(arg
)),
1231 "d"(LSS_SYSCALL_ARG(parent_tidptr
)),
1232 "r"(LSS_SYSCALL_ARG(newtls
)),
1233 "r"(LSS_SYSCALL_ARG(child_tidptr
))
1234 : "rsp", "memory", "r8", "r10", "r11", "rcx");
1236 LSS_RETURN(int, __res
);
1239 LSS_INLINE
void (*LSS_NAME(restore_rt
)(void))(void) {
1240 /* On x86-64, the kernel does not know how to return from
1241 * a signal handler. Instead, it relies on user space to provide a
1242 * restorer function that calls the rt_sigreturn() system call.
1243 * Unfortunately, we cannot just reference the glibc version of this
1244 * function, as glibc goes out of its way to make it inaccessible.
1247 __asm__
__volatile__("call 2f\n"
1252 "addq $(1b-0b),%0\n"
1254 : "i" (__NR_rt_sigreturn
));
1255 return (void (*)(void))(uintptr_t)res
;
1257 #elif defined(__arm__)
1258 /* Most definitions of _syscallX() neglect to mark "memory" as being
1259 * clobbered. This causes problems with compilers, that do a better job
1260 * at optimizing across __asm__ calls.
1261 * So, we just have to redefine all fo the _syscallX() macros.
1264 #define LSS_REG(r,a) register long __r##r __asm__("r"#r) = (long)a
1266 /* r0..r3 are scratch registers and not preserved across function
1267 * calls. We need to first evaluate the first 4 syscall arguments
1268 * and store them on stack. They must be loaded into r0..r3 after
1269 * all function calls to avoid r0..r3 being clobbered.
1272 #define LSS_SAVE_ARG(r,a) long __tmp##r = (long)a
1274 #define LSS_LOAD_ARG(r) register long __r##r __asm__("r"#r) = __tmp##r
1277 #define LSS_BODY(type, name, args...) \
1278 register long __res_r0 __asm__("r0"); \
1281 __asm__ __volatile__ (__syscall_safe(name) \
1283 : __SYS_REG_LIST(args) \
1284 : "lr", "memory"); \
1286 LSS_RETURN(type, __res)
1288 #define _syscall0(type, name) \
1289 type LSS_NAME(name)() { \
1290 LSS_BODY(type, name); \
1293 #define _syscall1(type, name, type1, arg1) \
1294 type LSS_NAME(name)(type1 arg1) { \
1295 /* There is no need for using a volatile temp. */ \
1297 LSS_BODY(type, name, "r"(__r0)); \
1300 #define _syscall2(type, name, type1, arg1, type2, arg2) \
1301 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
1302 LSS_SAVE_ARG(0, arg1); \
1303 LSS_SAVE_ARG(1, arg2); \
1306 LSS_BODY(type, name, "r"(__r0), "r"(__r1)); \
1309 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
1310 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
1311 LSS_SAVE_ARG(0, arg1); \
1312 LSS_SAVE_ARG(1, arg2); \
1313 LSS_SAVE_ARG(2, arg3); \
1317 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2)); \
1320 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
1322 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
1323 LSS_SAVE_ARG(0, arg1); \
1324 LSS_SAVE_ARG(1, arg2); \
1325 LSS_SAVE_ARG(2, arg3); \
1326 LSS_SAVE_ARG(3, arg4); \
1331 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3)); \
1334 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
1335 type4, arg4, type5, arg5) \
1336 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1338 LSS_SAVE_ARG(0, arg1); \
1339 LSS_SAVE_ARG(1, arg2); \
1340 LSS_SAVE_ARG(2, arg3); \
1341 LSS_SAVE_ARG(3, arg4); \
1347 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
1351 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
1352 type4, arg4, type5, arg5, type6, arg6) \
1353 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1354 type5 arg5, type6 arg6) { \
1355 LSS_SAVE_ARG(0, arg1); \
1356 LSS_SAVE_ARG(1, arg2); \
1357 LSS_SAVE_ARG(2, arg3); \
1358 LSS_SAVE_ARG(3, arg4); \
1365 LSS_BODY(type, name, "r"(__r0), "r"(__r1), "r"(__r2), "r"(__r3), \
1366 "r"(__r4), "r"(__r5)); \
1368 LSS_INLINE
int LSS_NAME(clone
)(int (*fn
)(void *), void *child_stack
,
1369 int flags
, void *arg
, int *parent_tidptr
,
1370 void *newtls
, int *child_tidptr
) {
1371 register long __res
__asm__("r5");
1373 if (fn
== NULL
|| child_stack
== NULL
) {
1378 /* stash first 4 arguments on stack first because we can only load
1379 * them after all function calls.
1381 int tmp_flags
= flags
;
1382 int * tmp_stack
= (int*) child_stack
;
1383 void * tmp_ptid
= parent_tidptr
;
1384 void * tmp_tls
= newtls
;
1386 register int *__ctid
__asm__("r4") = child_tidptr
;
1388 /* Push "arg" and "fn" onto the stack that will be
1389 * used by the child.
1391 *(--tmp_stack
) = (int) arg
;
1392 *(--tmp_stack
) = (int) fn
;
1394 /* We must load r0..r3 last after all possible function calls. */
1395 register int __flags
__asm__("r0") = tmp_flags
;
1396 register void *__stack
__asm__("r1") = tmp_stack
;
1397 register void *__ptid
__asm__("r2") = tmp_ptid
;
1398 register void *__tls
__asm__("r3") = tmp_tls
;
1400 /* %r0 = syscall(%r0 = flags,
1401 * %r1 = child_stack,
1402 * %r2 = parent_tidptr,
1404 * %r4 = child_tidptr)
1407 __asm__
__volatile__(/* %r0 = syscall(%r0 = flags,
1408 * %r1 = child_stack,
1409 * %r2 = parent_tidptr,
1411 * %r4 = child_tidptr)
1415 __syscall(clone
)"\n"
1423 /* In the child, now. Call "fn(arg)".
1429 /* Call _exit(%r0), which never returns. We only
1430 * need to set r7 for EABI syscall ABI but we do
1431 * this always to simplify code sharing between
1432 * old and new syscall ABIs.
1437 /* Pop r7 from the stack only in the parent.
1442 "i"(__NR_exit
), "r"(__stack
), "r"(__flags
),
1443 "r"(__ptid
), "r"(__tls
), "r"(__ctid
)
1444 : "cc", "lr", "memory");
1447 LSS_RETURN(int, __res
);
1449 #elif defined(__mips__)
1451 #define LSS_REG(r,a) register unsigned long __r##r __asm__("$"#r) = \
1454 #if _MIPS_SIM == _MIPS_SIM_ABI32
1455 // See http://sources.redhat.com/ml/libc-alpha/2004-10/msg00050.html
1456 // or http://www.linux-mips.org/archives/linux-mips/2004-10/msg00142.html
1457 #define MIPS_SYSCALL_CLOBBERS "$1", "$3", "$8", "$9", "$10", "$11", "$12",\
1458 "$13", "$14", "$15", "$24", "$25", "memory"
1460 #define MIPS_SYSCALL_CLOBBERS "$1", "$3", "$10", "$11", "$12", "$13", \
1461 "$14", "$15", "$24", "$25", "memory"
1465 #define LSS_BODY(type,name,r7,...) \
1466 register unsigned long __v0 __asm__("$2") = __NR_##name; \
1467 __asm__ __volatile__ ("syscall\n" \
1468 : "=&r"(__v0), r7 (__r7) \
1469 : "0"(__v0), ##__VA_ARGS__ \
1470 : MIPS_SYSCALL_CLOBBERS); \
1471 LSS_RETURN(type, __v0, __r7)
1473 #define _syscall0(type, name) \
1474 type LSS_NAME(name)() { \
1475 register unsigned long __r7 __asm__("$7"); \
1476 LSS_BODY(type, name, "=r"); \
1479 #define _syscall1(type, name, type1, arg1) \
1480 type LSS_NAME(name)(type1 arg1) { \
1481 register unsigned long __r7 __asm__("$7"); \
1482 LSS_REG(4, arg1); LSS_BODY(type, name, "=r", "r"(__r4)); \
1485 #define _syscall2(type, name, type1, arg1, type2, arg2) \
1486 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
1487 register unsigned long __r7 __asm__("$7"); \
1488 LSS_REG(4, arg1); LSS_REG(5, arg2); \
1489 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5)); \
1492 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
1493 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
1494 register unsigned long __r7 __asm__("$7"); \
1495 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
1496 LSS_BODY(type, name, "=r", "r"(__r4), "r"(__r5), "r"(__r6)); \
1499 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
1500 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
1501 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
1503 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6)); \
1506 #if _MIPS_SIM == _MIPS_SIM_ABI32
1507 /* The old 32bit MIPS system call API passes the fifth and sixth argument
1508 * on the stack, whereas the new APIs use registers "r8" and "r9".
1510 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1512 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1514 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
1516 register unsigned long __v0 __asm__("$2"); \
1517 __asm__ __volatile__ (".set noreorder\n" \
1520 "sw $2, 16($29)\n" \
1525 : "=&r"(__v0), "+r" (__r7) \
1526 : "i" (__NR_##name), "r"(__r4), "r"(__r5), \
1527 "r"(__r6), "m" ((unsigned long)arg5) \
1528 : MIPS_SYSCALL_CLOBBERS); \
1529 LSS_RETURN(type, __v0, __r7); \
1532 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1534 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1536 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
1537 LSS_REG(7, arg4); LSS_REG(8, arg5); \
1538 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
1543 #if _MIPS_SIM == _MIPS_SIM_ABI32
1544 /* The old 32bit MIPS system call API passes the fifth and sixth argument
1545 * on the stack, whereas the new APIs use registers "r8" and "r9".
1547 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1548 type5,arg5,type6,arg6) \
1549 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1550 type5 arg5, type6 arg6) { \
1551 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
1553 register unsigned long __v0 __asm__("$2"); \
1554 __asm__ __volatile__ (".set noreorder\n" \
1558 "sw $2, 16($29)\n" \
1559 "sw $8, 20($29)\n" \
1564 : "=&r"(__v0), "+r" (__r7) \
1565 : "i" (__NR_##name), "r"(__r4), "r"(__r5), \
1566 "r"(__r6), "r" ((unsigned long)arg5), \
1567 "r" ((unsigned long)arg6) \
1568 : MIPS_SYSCALL_CLOBBERS); \
1569 LSS_RETURN(type, __v0, __r7); \
1572 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
1573 type5,arg5,type6,arg6) \
1574 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1575 type5 arg5,type6 arg6) { \
1576 LSS_REG(4, arg1); LSS_REG(5, arg2); LSS_REG(6, arg3); \
1577 LSS_REG(7, arg4); LSS_REG(8, arg5); LSS_REG(9, arg6); \
1578 LSS_BODY(type, name, "+r", "r"(__r4), "r"(__r5), "r"(__r6), \
1579 "r"(__r8), "r"(__r9)); \
1582 LSS_INLINE
int LSS_NAME(clone
)(int (*fn
)(void *), void *child_stack
,
1583 int flags
, void *arg
, int *parent_tidptr
,
1584 void *newtls
, int *child_tidptr
) {
1585 register unsigned long __v0
__asm__("$2");
1586 register unsigned long __r7
__asm__("$7") = (unsigned long)newtls
;
1588 register int __flags
__asm__("$4") = flags
;
1589 register void *__stack
__asm__("$5") = child_stack
;
1590 register void *__ptid
__asm__("$6") = parent_tidptr
;
1591 register int *__ctid
__asm__("$8") = child_tidptr
;
1592 __asm__
__volatile__(
1593 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
1595 #elif _MIPS_SIM == _MIPS_SIM_NABI32
1601 /* if (fn == NULL || child_stack == NULL)
1608 /* Push "arg" and "fn" onto the stack that will be
1609 * used by the child.
1611 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
1615 #elif _MIPS_SIM == _MIPS_SIM_NABI32
1625 /* $7 = syscall($4 = flags,
1627 * $6 = parent_tidptr,
1629 * $8 = child_tidptr)
1640 /* In the child, now. Call "fn(arg)".
1642 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
1645 #elif _MIPS_SIM == _MIPS_SIM_NABI32
1661 #if _MIPS_SIM == _MIPS_SIM_ABI32 && _MIPS_SZPTR == 32
1663 #elif _MIPS_SIM == _MIPS_SIM_NABI32
1668 : "=&r" (__v0
), "=r" (__r7
)
1669 : "i"(-EINVAL
), "i"(__NR_clone
), "i"(__NR_exit
),
1670 "r"(fn
), "r"(__stack
), "r"(__flags
), "r"(arg
),
1671 "r"(__ptid
), "r"(__r7
), "r"(__ctid
)
1672 : "$9", "$10", "$11", "$12", "$13", "$14", "$15",
1675 LSS_RETURN(int, __v0
, __r7
);
1677 #elif defined (__PPC__)
1678 #undef LSS_LOADARGS_0
1679 #define LSS_LOADARGS_0(name, dummy...) \
1680 __sc_0 = __NR_##name
1681 #undef LSS_LOADARGS_1
1682 #define LSS_LOADARGS_1(name, arg1) \
1683 LSS_LOADARGS_0(name); \
1684 __sc_3 = (unsigned long) (arg1)
1685 #undef LSS_LOADARGS_2
1686 #define LSS_LOADARGS_2(name, arg1, arg2) \
1687 LSS_LOADARGS_1(name, arg1); \
1688 __sc_4 = (unsigned long) (arg2)
1689 #undef LSS_LOADARGS_3
1690 #define LSS_LOADARGS_3(name, arg1, arg2, arg3) \
1691 LSS_LOADARGS_2(name, arg1, arg2); \
1692 __sc_5 = (unsigned long) (arg3)
1693 #undef LSS_LOADARGS_4
1694 #define LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4) \
1695 LSS_LOADARGS_3(name, arg1, arg2, arg3); \
1696 __sc_6 = (unsigned long) (arg4)
1697 #undef LSS_LOADARGS_5
1698 #define LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5) \
1699 LSS_LOADARGS_4(name, arg1, arg2, arg3, arg4); \
1700 __sc_7 = (unsigned long) (arg5)
1701 #undef LSS_LOADARGS_6
1702 #define LSS_LOADARGS_6(name, arg1, arg2, arg3, arg4, arg5, arg6) \
1703 LSS_LOADARGS_5(name, arg1, arg2, arg3, arg4, arg5); \
1704 __sc_8 = (unsigned long) (arg6)
1705 #undef LSS_ASMINPUT_0
1706 #define LSS_ASMINPUT_0 "0" (__sc_0)
1707 #undef LSS_ASMINPUT_1
1708 #define LSS_ASMINPUT_1 LSS_ASMINPUT_0, "1" (__sc_3)
1709 #undef LSS_ASMINPUT_2
1710 #define LSS_ASMINPUT_2 LSS_ASMINPUT_1, "2" (__sc_4)
1711 #undef LSS_ASMINPUT_3
1712 #define LSS_ASMINPUT_3 LSS_ASMINPUT_2, "3" (__sc_5)
1713 #undef LSS_ASMINPUT_4
1714 #define LSS_ASMINPUT_4 LSS_ASMINPUT_3, "4" (__sc_6)
1715 #undef LSS_ASMINPUT_5
1716 #define LSS_ASMINPUT_5 LSS_ASMINPUT_4, "5" (__sc_7)
1717 #undef LSS_ASMINPUT_6
1718 #define LSS_ASMINPUT_6 LSS_ASMINPUT_5, "6" (__sc_8)
1720 #define LSS_BODY(nr, type, name, args...) \
1721 long __sc_ret, __sc_err; \
1723 register unsigned long __sc_0 __asm__ ("r0"); \
1724 register unsigned long __sc_3 __asm__ ("r3"); \
1725 register unsigned long __sc_4 __asm__ ("r4"); \
1726 register unsigned long __sc_5 __asm__ ("r5"); \
1727 register unsigned long __sc_6 __asm__ ("r6"); \
1728 register unsigned long __sc_7 __asm__ ("r7"); \
1729 register unsigned long __sc_8 __asm__ ("r8"); \
1731 LSS_LOADARGS_##nr(name, args); \
1732 __asm__ __volatile__ \
1736 "=&r" (__sc_3), "=&r" (__sc_4), \
1737 "=&r" (__sc_5), "=&r" (__sc_6), \
1738 "=&r" (__sc_7), "=&r" (__sc_8) \
1739 : LSS_ASMINPUT_##nr \
1740 : "cr0", "ctr", "memory", \
1741 "r9", "r10", "r11", "r12"); \
1742 __sc_ret = __sc_3; \
1743 __sc_err = __sc_0; \
1745 LSS_RETURN(type, __sc_ret, __sc_err)
1747 #define _syscall0(type, name) \
1748 type LSS_NAME(name)(void) { \
1749 LSS_BODY(0, type, name); \
1752 #define _syscall1(type, name, type1, arg1) \
1753 type LSS_NAME(name)(type1 arg1) { \
1754 LSS_BODY(1, type, name, arg1); \
1757 #define _syscall2(type, name, type1, arg1, type2, arg2) \
1758 type LSS_NAME(name)(type1 arg1, type2 arg2) { \
1759 LSS_BODY(2, type, name, arg1, arg2); \
1762 #define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
1763 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3) { \
1764 LSS_BODY(3, type, name, arg1, arg2, arg3); \
1767 #define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
1769 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
1770 LSS_BODY(4, type, name, arg1, arg2, arg3, arg4); \
1773 #define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
1774 type4, arg4, type5, arg5) \
1775 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1777 LSS_BODY(5, type, name, arg1, arg2, arg3, arg4, arg5); \
1780 #define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
1781 type4, arg4, type5, arg5, type6, arg6) \
1782 type LSS_NAME(name)(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
1783 type5 arg5, type6 arg6) { \
1784 LSS_BODY(6, type, name, arg1, arg2, arg3, arg4, arg5, arg6); \
1786 /* clone function adapted from glibc 2.3.6 clone.S */
1787 /* TODO(csilvers): consider wrapping some args up in a struct, like we
1788 * do for i386's _syscall6, so we can compile successfully on gcc 2.95
1790 LSS_INLINE
int LSS_NAME(clone
)(int (*fn
)(void *), void *child_stack
,
1791 int flags
, void *arg
, int *parent_tidptr
,
1792 void *newtls
, int *child_tidptr
) {
1795 register int (*__fn
)(void *) __asm__ ("r8") = fn
;
1796 register void *__cstack
__asm__ ("r4") = child_stack
;
1797 register int __flags
__asm__ ("r3") = flags
;
1798 register void * __arg
__asm__ ("r9") = arg
;
1799 register int * __ptidptr
__asm__ ("r5") = parent_tidptr
;
1800 register void * __newtls
__asm__ ("r6") = newtls
;
1801 register int * __ctidptr
__asm__ ("r7") = child_tidptr
;
1802 __asm__
__volatile__(
1803 /* check for fn == NULL
1804 * and child_stack == NULL
1806 "cmpwi cr0, %6, 0\n\t"
1807 "cmpwi cr1, %7, 0\n\t"
1808 "cror cr0*4+eq, cr1*4+eq, cr0*4+eq\n\t"
1811 /* set up stack frame for child */
1812 "clrrwi %7, %7, 4\n\t"
1814 "stwu 0, -16(%7)\n\t"
1816 /* fn, arg, child_stack are saved across the syscall: r28-30 */
1823 /* flags already in r3
1824 * child_stack already in r4
1825 * ptidptr already in r5
1826 * newtls already in r6
1827 * ctidptr already in r7
1831 /* Test if syscall was successful */
1832 "cmpwi cr1, 3, 0\n\t"
1833 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
1836 /* Do the function call */
1841 /* Call _exit(r3) */
1845 /* Return to parent */
1849 : "=r" (__ret
), "=r" (__err
)
1850 : "0" (-1), "1" (EINVAL
),
1851 "i" (__NR_clone
), "i" (__NR_exit
),
1852 "r" (__fn
), "r" (__cstack
), "r" (__flags
),
1853 "r" (__arg
), "r" (__ptidptr
), "r" (__newtls
),
1855 : "cr0", "cr1", "memory", "ctr",
1856 "r0", "r29", "r27", "r28");
1858 LSS_RETURN(int, __ret
, __err
);
1861 #define __NR__exit __NR_exit
1862 #define __NR__gettid __NR_gettid
1863 #define __NR__mremap __NR_mremap
1864 LSS_INLINE
_syscall1(int, close
, int, f
)
1865 LSS_INLINE
_syscall1(int, _exit
, int, e
)
1866 LSS_INLINE
_syscall3(int, fcntl
, int, f
,
1868 LSS_INLINE
_syscall2(int, fstat
, int, f
,
1869 struct kernel_stat
*, b
)
1870 LSS_INLINE
_syscall4(int, futex
, int*, a
,
1872 struct kernel_timespec
*, t
)
1873 LSS_INLINE
_syscall3(int, getdents
, int, f
,
1874 struct kernel_dirent
*, d
, int, c
)
1875 #ifdef __NR_getdents64
1876 LSS_INLINE
_syscall3(int, getdents64
, int, f
,
1877 struct kernel_dirent64
*, d
, int, c
)
1879 LSS_INLINE
_syscall0(pid_t
, getpid
)
1880 LSS_INLINE
_syscall0(pid_t
, getppid
)
1881 LSS_INLINE
_syscall0(pid_t
, _gettid
)
1882 LSS_INLINE
_syscall2(int, kill
, pid_t
, p
,
1884 #if defined(__x86_64__)
1885 /* Need to make sure off_t isn't truncated to 32-bits under x32. */
1886 LSS_INLINE off_t
LSS_NAME(lseek
)(int f
, off_t o
, int w
) {
1887 _LSS_BODY(3, off_t
, lseek
, off_t
, LSS_SYSCALL_ARG(f
), (uint64_t)(o
),
1888 LSS_SYSCALL_ARG(w
));
1891 LSS_INLINE
_syscall3(off_t
, lseek
, int, f
,
1894 LSS_INLINE
_syscall2(int, munmap
, void*, s
,
1896 LSS_INLINE
_syscall5(void*, _mremap
, void*, o
,
1897 size_t, os
, size_t, ns
,
1898 unsigned long, f
, void *, a
)
1899 LSS_INLINE
_syscall3(int, open
, const char*, p
,
1901 LSS_INLINE
_syscall2(int, prctl
, int, o
,
1903 LSS_INLINE
_syscall4(long, ptrace
, int, r
,
1904 pid_t
, p
, void *, a
, void *, d
)
1905 LSS_INLINE
_syscall3(ssize_t
, read
, int, f
,
1906 void *, b
, size_t, c
)
1907 LSS_INLINE
_syscall4(int, rt_sigaction
, int, s
,
1908 const struct kernel_sigaction
*, a
,
1909 struct kernel_sigaction
*, o
, size_t, c
)
1910 LSS_INLINE
_syscall4(int, rt_sigprocmask
, int, h
,
1911 const struct kernel_sigset_t
*, s
,
1912 struct kernel_sigset_t
*, o
, size_t, c
);
1913 LSS_INLINE
_syscall0(int, sched_yield
)
1914 LSS_INLINE
_syscall2(int, sigaltstack
, const stack_t
*, s
,
1916 LSS_INLINE
_syscall2(int, stat
, const char*, f
,
1917 struct kernel_stat
*, b
)
1918 LSS_INLINE
_syscall3(ssize_t
, write
, int, f
,
1919 const void *, b
, size_t, c
)
1920 #if defined(__NR_getcpu)
1921 LSS_INLINE
_syscall3(long, getcpu
, unsigned *, cpu
,
1922 unsigned *, node
, void *, unused
);
1924 #if defined(__x86_64__) || \
1925 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32)
1926 LSS_INLINE
_syscall3(int, socket
, int, d
,
1929 #if defined(__x86_64__)
1930 /* Need to make sure __off64_t isn't truncated to 32-bits under x32. */
1931 LSS_INLINE
void* LSS_NAME(mmap
)(void *s
, size_t l
, int p
, int f
, int d
,
1933 LSS_BODY(6, void*, mmap
, LSS_SYSCALL_ARG(s
), LSS_SYSCALL_ARG(l
),
1934 LSS_SYSCALL_ARG(p
), LSS_SYSCALL_ARG(f
),
1935 LSS_SYSCALL_ARG(d
), (uint64_t)(o
));
1938 LSS_INLINE
int LSS_NAME(sigaction
)(int signum
,
1939 const struct kernel_sigaction
*act
,
1940 struct kernel_sigaction
*oldact
) {
1941 /* On x86_64, the kernel requires us to always set our own
1942 * SA_RESTORER in order to be able to return from a signal handler.
1943 * This function must have a "magic" signature that the "gdb"
1944 * (and maybe the kernel?) can recognize.
1946 if (act
!= NULL
&& !(act
->sa_flags
& SA_RESTORER
)) {
1947 struct kernel_sigaction a
= *act
;
1948 a
.sa_flags
|= SA_RESTORER
;
1949 a
.sa_restorer
= LSS_NAME(restore_rt
)();
1950 return LSS_NAME(rt_sigaction
)(signum
, &a
, oldact
,
1953 return LSS_NAME(rt_sigaction
)(signum
, act
, oldact
,
1958 LSS_INLINE
int LSS_NAME(sigprocmask
)(int how
,
1959 const struct kernel_sigset_t
*set
,
1960 struct kernel_sigset_t
*oldset
) {
1961 return LSS_NAME(rt_sigprocmask
)(how
, set
, oldset
, (KERNEL_NSIG
+7)/8);
1964 #if defined(__x86_64__) || \
1965 defined(__arm__) || \
1966 (defined(__mips__) && _MIPS_SIM != _MIPS_SIM_ABI32)
1967 LSS_INLINE
_syscall4(pid_t
, wait4
, pid_t
, p
,
1969 struct kernel_rusage
*, r
)
1970 LSS_INLINE pid_t
LSS_NAME(waitpid
)(pid_t pid
, int *status
, int options
){
1971 return LSS_NAME(wait4
)(pid
, status
, options
, 0);
1974 #if (defined(__i386__) || defined(__x86_64__) || defined(__arm__)) && \
1975 !defined(__ANDROID__)
1976 LSS_INLINE
_syscall4(int, openat
, int, d
, const char *, p
, int, f
, int, m
)
1978 LSS_INLINE
int LSS_NAME(sigemptyset
)(struct kernel_sigset_t
*set
) {
1979 memset(&set
->sig
, 0, sizeof(set
->sig
));
1983 LSS_INLINE
int LSS_NAME(sigfillset
)(struct kernel_sigset_t
*set
) {
1984 memset(&set
->sig
, -1, sizeof(set
->sig
));
1988 LSS_INLINE
int LSS_NAME(sigaddset
)(struct kernel_sigset_t
*set
,
1990 if (signum
< 1 || signum
> (int)(8*sizeof(set
->sig
))) {
1994 set
->sig
[(signum
- 1)/(8*sizeof(set
->sig
[0]))]
1995 |= 1UL << ((signum
- 1) % (8*sizeof(set
->sig
[0])));
2000 LSS_INLINE
int LSS_NAME(sigdelset
)(struct kernel_sigset_t
*set
,
2002 if (signum
< 1 || signum
> (int)(8*sizeof(set
->sig
))) {
2006 set
->sig
[(signum
- 1)/(8*sizeof(set
->sig
[0]))]
2007 &= ~(1UL << ((signum
- 1) % (8*sizeof(set
->sig
[0]))));
2012 #if defined(__i386__) || \
2013 defined(__arm__) || \
2014 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || defined(__PPC__)
2015 #define __NR__sigaction __NR_sigaction
2016 #define __NR__sigprocmask __NR_sigprocmask
2017 LSS_INLINE
_syscall2(int, fstat64
, int, f
,
2018 struct kernel_stat64
*, b
)
2019 LSS_INLINE
_syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
2020 loff_t
*, res
, uint
, wh
)
2022 LSS_INLINE
_syscall6(void*, mmap
, void*, s
,
2027 #ifndef __ARM_EABI__
2028 /* Not available on ARM EABI Linux. */
2029 LSS_INLINE
_syscall1(void*, mmap
, void*, a
)
2031 LSS_INLINE
_syscall6(void*, mmap2
, void*, s
,
2036 LSS_INLINE
_syscall3(int, _sigaction
, int, s
,
2037 const struct kernel_old_sigaction
*, a
,
2038 struct kernel_old_sigaction
*, o
)
2039 LSS_INLINE
_syscall3(int, _sigprocmask
, int, h
,
2040 const unsigned long*, s
,
2042 LSS_INLINE
_syscall2(int, stat64
, const char *, p
,
2043 struct kernel_stat64
*, b
)
2045 LSS_INLINE
int LSS_NAME(sigaction
)(int signum
,
2046 const struct kernel_sigaction
*act
,
2047 struct kernel_sigaction
*oldact
) {
2048 int old_errno
= LSS_ERRNO
;
2050 struct kernel_sigaction a
;
2054 /* On i386, the kernel requires us to always set our own
2055 * SA_RESTORER when using realtime signals. Otherwise, it does not
2056 * know how to return from a signal handler. This function must have
2057 * a "magic" signature that the "gdb" (and maybe the kernel?) can
2059 * Apparently, a SA_RESTORER is implicitly set by the kernel, when
2060 * using non-realtime signals.
2062 * TODO: Test whether ARM needs a restorer
2064 if (!(a
.sa_flags
& SA_RESTORER
)) {
2065 a
.sa_flags
|= SA_RESTORER
;
2066 a
.sa_restorer
= (a
.sa_flags
& SA_SIGINFO
)
2067 ? LSS_NAME(restore_rt
)() : LSS_NAME(restore
)();
2071 rc
= LSS_NAME(rt_sigaction
)(signum
, act
? &a
: act
, oldact
,
2073 if (rc
< 0 && LSS_ERRNO
== ENOSYS
) {
2074 struct kernel_old_sigaction oa
, ooa
, *ptr_a
= &oa
, *ptr_oa
= &ooa
;
2078 oa
.sa_handler_
= act
->sa_handler_
;
2079 memcpy(&oa
.sa_mask
, &act
->sa_mask
, sizeof(oa
.sa_mask
));
2081 oa
.sa_restorer
= act
->sa_restorer
;
2083 oa
.sa_flags
= act
->sa_flags
;
2088 LSS_ERRNO
= old_errno
;
2089 rc
= LSS_NAME(_sigaction
)(signum
, ptr_a
, ptr_oa
);
2090 if (rc
== 0 && oldact
) {
2092 memcpy(oldact
, act
, sizeof(*act
));
2094 memset(oldact
, 0, sizeof(*oldact
));
2096 oldact
->sa_handler_
= ptr_oa
->sa_handler_
;
2097 oldact
->sa_flags
= ptr_oa
->sa_flags
;
2098 memcpy(&oldact
->sa_mask
, &ptr_oa
->sa_mask
, sizeof(ptr_oa
->sa_mask
));
2100 oldact
->sa_restorer
= ptr_oa
->sa_restorer
;
2107 LSS_INLINE
int LSS_NAME(sigprocmask
)(int how
,
2108 const struct kernel_sigset_t
*set
,
2109 struct kernel_sigset_t
*oldset
) {
2110 int olderrno
= LSS_ERRNO
;
2111 int rc
= LSS_NAME(rt_sigprocmask
)(how
, set
, oldset
, (KERNEL_NSIG
+7)/8);
2112 if (rc
< 0 && LSS_ERRNO
== ENOSYS
) {
2113 LSS_ERRNO
= olderrno
;
2115 LSS_NAME(sigemptyset
)(oldset
);
2117 rc
= LSS_NAME(_sigprocmask
)(how
,
2118 set
? &set
->sig
[0] : NULL
,
2119 oldset
? &oldset
->sig
[0] : NULL
);
2124 #if defined(__PPC__)
2125 #undef LSS_SC_LOADARGS_0
2126 #define LSS_SC_LOADARGS_0(dummy...)
2127 #undef LSS_SC_LOADARGS_1
2128 #define LSS_SC_LOADARGS_1(arg1) \
2129 __sc_4 = (unsigned long) (arg1)
2130 #undef LSS_SC_LOADARGS_2
2131 #define LSS_SC_LOADARGS_2(arg1, arg2) \
2132 LSS_SC_LOADARGS_1(arg1); \
2133 __sc_5 = (unsigned long) (arg2)
2134 #undef LSS_SC_LOADARGS_3
2135 #define LSS_SC_LOADARGS_3(arg1, arg2, arg3) \
2136 LSS_SC_LOADARGS_2(arg1, arg2); \
2137 __sc_6 = (unsigned long) (arg3)
2138 #undef LSS_SC_LOADARGS_4
2139 #define LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4) \
2140 LSS_SC_LOADARGS_3(arg1, arg2, arg3); \
2141 __sc_7 = (unsigned long) (arg4)
2142 #undef LSS_SC_LOADARGS_5
2143 #define LSS_SC_LOADARGS_5(arg1, arg2, arg3, arg4, arg5) \
2144 LSS_SC_LOADARGS_4(arg1, arg2, arg3, arg4); \
2145 __sc_8 = (unsigned long) (arg5)
2147 #define LSS_SC_BODY(nr, type, opt, args...) \
2148 long __sc_ret, __sc_err; \
2150 register unsigned long __sc_0 __asm__ ("r0") = __NR_socketcall; \
2151 register unsigned long __sc_3 __asm__ ("r3") = opt; \
2152 register unsigned long __sc_4 __asm__ ("r4"); \
2153 register unsigned long __sc_5 __asm__ ("r5"); \
2154 register unsigned long __sc_6 __asm__ ("r6"); \
2155 register unsigned long __sc_7 __asm__ ("r7"); \
2156 register unsigned long __sc_8 __asm__ ("r8"); \
2157 LSS_SC_LOADARGS_##nr(args); \
2158 __asm__ __volatile__ \
2159 ("stwu 1, -48(1)\n\t" \
2160 "stw 4, 20(1)\n\t" \
2161 "stw 5, 24(1)\n\t" \
2162 "stw 6, 28(1)\n\t" \
2163 "stw 7, 32(1)\n\t" \
2164 "stw 8, 36(1)\n\t" \
2165 "addi 4, 1, 20\n\t" \
2169 "=&r" (__sc_3), "=&r" (__sc_4), \
2170 "=&r" (__sc_5), "=&r" (__sc_6), \
2171 "=&r" (__sc_7), "=&r" (__sc_8) \
2172 : LSS_ASMINPUT_##nr \
2173 : "cr0", "ctr", "memory"); \
2174 __sc_ret = __sc_3; \
2175 __sc_err = __sc_0; \
2177 LSS_RETURN(type, __sc_ret, __sc_err)
2179 LSS_INLINE
int LSS_NAME(socket
)(int domain
, int type
, int protocol
) {
2180 LSS_SC_BODY(3, int, 1, domain
, type
, protocol
);
2183 #if defined(__i386__) || \
2184 (defined(__arm__) && !defined(__ARM_EABI__)) || \
2185 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
2187 /* See sys_socketcall in net/socket.c in kernel source.
2188 * It de-multiplexes on its first arg and unpacks the arglist
2189 * array in its second arg.
2191 LSS_INLINE
_syscall2(long, socketcall
, int, c
, unsigned long*, a
)
2193 LSS_INLINE
int LSS_NAME(socket
)(int domain
, int type
, int protocol
) {
2194 unsigned long args
[3] = {
2195 (unsigned long) domain
,
2196 (unsigned long) type
,
2197 (unsigned long) protocol
2199 return LSS_NAME(socketcall
)(1, args
);
2201 #elif defined(__ARM_EABI__)
2202 LSS_INLINE
_syscall3(int, socket
, int, d
,
2205 #if defined(__i386__) || defined(__PPC__) || \
2206 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32)
2207 LSS_INLINE
_syscall3(pid_t
, waitpid
, pid_t
, p
,
2210 #if defined(__mips__)
2211 /* sys_pipe() on MIPS has non-standard calling conventions, as it returns
2212 * both file handles through CPU registers.
2214 LSS_INLINE
int LSS_NAME(pipe
)(int *p
) {
2215 register unsigned long __v0
__asm__("$2") = __NR_pipe
;
2216 register unsigned long __v1
__asm__("$3");
2217 register unsigned long __r7
__asm__("$7");
2218 __asm__
__volatile__ ("syscall\n"
2219 : "=&r"(__v0
), "=&r"(__v1
), "+r" (__r7
)
2221 : "$8", "$9", "$10", "$11", "$12",
2222 "$13", "$14", "$15", "$24", "memory");
2233 LSS_INLINE
_syscall1(int, pipe
, int *, p
)
2236 LSS_INLINE pid_t
LSS_NAME(gettid
)() {
2237 pid_t tid
= LSS_NAME(_gettid
)();
2241 return LSS_NAME(getpid
)();
2244 LSS_INLINE
void *LSS_NAME(mremap
)(void *old_address
, size_t old_size
,
2245 size_t new_size
, int flags
, ...) {
2247 void *new_address
, *rc
;
2248 va_start(ap
, flags
);
2249 new_address
= va_arg(ap
, void *);
2250 rc
= LSS_NAME(_mremap
)(old_address
, old_size
, new_size
,
2251 flags
, new_address
);
2256 LSS_INLINE
int LSS_NAME(ptrace_detach
)(pid_t pid
) {
2257 /* PTRACE_DETACH can sometimes forget to wake up the tracee and it
2258 * then sends job control signals to the real parent, rather than to
2259 * the tracer. We reduce the risk of this happening by starting a
2260 * whole new time slice, and then quickly sending a SIGCONT signal
2261 * right after detaching from the tracee.
2264 LSS_NAME(sched_yield
)();
2265 rc
= LSS_NAME(ptrace
)(PTRACE_DETACH
, pid
, (void *)0, (void *)0);
2267 LSS_NAME(kill
)(pid
, SIGCONT
);
2273 #if defined(__cplusplus) && !defined(SYS_CPLUSPLUS)