1*4882a593Smuzhiyun /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2*4882a593Smuzhiyun /* nolibc.h
3*4882a593Smuzhiyun * Copyright (C) 2017-2018 Willy Tarreau <w@1wt.eu>
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun /*
7*4882a593Smuzhiyun * This file is designed to be used as a libc alternative for minimal programs
8*4882a593Smuzhiyun * with very limited requirements. It consists of a small number of syscall and
9*4882a593Smuzhiyun * type definitions, and the minimal startup code needed to call main().
10*4882a593Smuzhiyun * All syscalls are declared as static functions so that they can be optimized
11*4882a593Smuzhiyun * away by the compiler when not used.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * Syscalls are split into 3 levels:
14*4882a593Smuzhiyun * - The lower level is the arch-specific syscall() definition, consisting in
15*4882a593Smuzhiyun * assembly code in compound expressions. These are called my_syscall0() to
16*4882a593Smuzhiyun * my_syscall6() depending on the number of arguments. The MIPS
17*4882a593Smuzhiyun * implementation is limited to 5 arguments. All input arguments are cast
18*4882a593Smuzhiyun * to a long stored in a register. These expressions always return the
19*4882a593Smuzhiyun * syscall's return value as a signed long value which is often either a
20*4882a593Smuzhiyun * pointer or the negated errno value.
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * - The second level is mostly architecture-independent. It is made of
23*4882a593Smuzhiyun * static functions called sys_<name>() which rely on my_syscallN()
24*4882a593Smuzhiyun * depending on the syscall definition. These functions are responsible
25*4882a593Smuzhiyun * for exposing the appropriate types for the syscall arguments (int,
26*4882a593Smuzhiyun * pointers, etc) and for setting the appropriate return type (often int).
27*4882a593Smuzhiyun * A few of them are architecture-specific because the syscalls are not all
28*4882a593Smuzhiyun * mapped exactly the same among architectures. For example, some archs do
29*4882a593Smuzhiyun * not implement select() and need pselect6() instead, so the sys_select()
30*4882a593Smuzhiyun * function will have to abstract this.
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * - The third level is the libc call definition. It exposes the lower raw
33*4882a593Smuzhiyun * sys_<name>() calls in a way that looks like what a libc usually does,
34*4882a593Smuzhiyun * takes care of specific input values, and of setting errno upon error.
35*4882a593Smuzhiyun * There can be minor variations compared to standard libc calls. For
36*4882a593Smuzhiyun * example the open() call always takes 3 args here.
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * The errno variable is declared static and unused. This way it can be
39*4882a593Smuzhiyun * optimized away if not used. However this means that a program made of
40*4882a593Smuzhiyun * multiple C files may observe different errno values (one per C file). For
41*4882a593Smuzhiyun * the type of programs this project targets it usually is not a problem. The
42*4882a593Smuzhiyun * resulting program may even be reduced by defining the NOLIBC_IGNORE_ERRNO
43*4882a593Smuzhiyun * macro, in which case the errno value will never be assigned.
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun * Some stdint-like integer types are defined. These are valid on all currently
46*4882a593Smuzhiyun * supported architectures, because signs are enforced, ints are assumed to be
47*4882a593Smuzhiyun * 32 bits, longs the size of a pointer and long long 64 bits. If more
48*4882a593Smuzhiyun * architectures have to be supported, this may need to be adapted.
49*4882a593Smuzhiyun *
50*4882a593Smuzhiyun * Some macro definitions like the O_* values passed to open(), and some
51*4882a593Smuzhiyun * structures like the sys_stat struct depend on the architecture.
52*4882a593Smuzhiyun *
53*4882a593Smuzhiyun * The definitions start with the architecture-specific parts, which are picked
54*4882a593Smuzhiyun * based on what the compiler knows about the target architecture, and are
55*4882a593Smuzhiyun * completed with the generic code. Since it is the compiler which sets the
56*4882a593Smuzhiyun * target architecture, cross-compiling normally works out of the box without
57*4882a593Smuzhiyun * having to specify anything.
58*4882a593Smuzhiyun *
59*4882a593Smuzhiyun * Finally some very common libc-level functions are provided. It is the case
60*4882a593Smuzhiyun * for a few functions usually found in string.h, ctype.h, or stdlib.h. Nothing
61*4882a593Smuzhiyun * is currently provided regarding stdio emulation.
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * The macro NOLIBC is always defined, so that it is possible for a program to
64*4882a593Smuzhiyun * check this macro to know if it is being built against and decide to disable
65*4882a593Smuzhiyun * some features or simply not to include some standard libc files.
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * Ideally this file should be split in multiple files for easier long term
68*4882a593Smuzhiyun * maintenance, but provided as a single file as it is now, it's quite
69*4882a593Smuzhiyun * convenient to use. Maybe some variations involving a set of includes at the
70*4882a593Smuzhiyun * top could work.
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun * A simple static executable may be built this way :
73*4882a593Smuzhiyun * $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \
74*4882a593Smuzhiyun * -static -include nolibc.h -lgcc -o hello hello.c
75*4882a593Smuzhiyun *
76*4882a593Smuzhiyun * A very useful calling convention table may be found here :
77*4882a593Smuzhiyun * http://man7.org/linux/man-pages/man2/syscall.2.html
78*4882a593Smuzhiyun *
79*4882a593Smuzhiyun * This doc is quite convenient though not necessarily up to date :
80*4882a593Smuzhiyun * https://w3challs.com/syscalls/
81*4882a593Smuzhiyun *
82*4882a593Smuzhiyun */
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /* Some archs (at least aarch64) don't expose the regular syscalls anymore by
85*4882a593Smuzhiyun * default, either because they have an "_at" replacement, or because there are
86*4882a593Smuzhiyun * more modern alternatives. For now we'd rather still use them.
87*4882a593Smuzhiyun */
88*4882a593Smuzhiyun #define __ARCH_WANT_SYSCALL_NO_AT
89*4882a593Smuzhiyun #define __ARCH_WANT_SYSCALL_NO_FLAGS
90*4882a593Smuzhiyun #define __ARCH_WANT_SYSCALL_DEPRECATED
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun #include <asm/unistd.h>
93*4882a593Smuzhiyun #include <asm/ioctls.h>
94*4882a593Smuzhiyun #include <asm/errno.h>
95*4882a593Smuzhiyun #include <linux/fs.h>
96*4882a593Smuzhiyun #include <linux/loop.h>
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun #define NOLIBC
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /* this way it will be removed if unused */
101*4882a593Smuzhiyun static int errno;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun #ifndef NOLIBC_IGNORE_ERRNO
104*4882a593Smuzhiyun #define SET_ERRNO(v) do { errno = (v); } while (0)
105*4882a593Smuzhiyun #else
106*4882a593Smuzhiyun #define SET_ERRNO(v) do { } while (0)
107*4882a593Smuzhiyun #endif
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /* errno codes all ensure that they will not conflict with a valid pointer
110*4882a593Smuzhiyun * because they all correspond to the highest addressable memry page.
111*4882a593Smuzhiyun */
112*4882a593Smuzhiyun #define MAX_ERRNO 4095
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* Declare a few quite common macros and types that usually are in stdlib.h,
115*4882a593Smuzhiyun * stdint.h, ctype.h, unistd.h and a few other common locations.
116*4882a593Smuzhiyun */
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun #define NULL ((void *)0)
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun /* stdint types */
121*4882a593Smuzhiyun typedef unsigned char uint8_t;
122*4882a593Smuzhiyun typedef signed char int8_t;
123*4882a593Smuzhiyun typedef unsigned short uint16_t;
124*4882a593Smuzhiyun typedef signed short int16_t;
125*4882a593Smuzhiyun typedef unsigned int uint32_t;
126*4882a593Smuzhiyun typedef signed int int32_t;
127*4882a593Smuzhiyun typedef unsigned long long uint64_t;
128*4882a593Smuzhiyun typedef signed long long int64_t;
129*4882a593Smuzhiyun typedef unsigned long size_t;
130*4882a593Smuzhiyun typedef signed long ssize_t;
131*4882a593Smuzhiyun typedef unsigned long uintptr_t;
132*4882a593Smuzhiyun typedef signed long intptr_t;
133*4882a593Smuzhiyun typedef signed long ptrdiff_t;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun /* for stat() */
136*4882a593Smuzhiyun typedef unsigned int dev_t;
137*4882a593Smuzhiyun typedef unsigned long ino_t;
138*4882a593Smuzhiyun typedef unsigned int mode_t;
139*4882a593Smuzhiyun typedef signed int pid_t;
140*4882a593Smuzhiyun typedef unsigned int uid_t;
141*4882a593Smuzhiyun typedef unsigned int gid_t;
142*4882a593Smuzhiyun typedef unsigned long nlink_t;
143*4882a593Smuzhiyun typedef signed long off_t;
144*4882a593Smuzhiyun typedef signed long blksize_t;
145*4882a593Smuzhiyun typedef signed long blkcnt_t;
146*4882a593Smuzhiyun typedef signed long time_t;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /* for poll() */
149*4882a593Smuzhiyun struct pollfd {
150*4882a593Smuzhiyun int fd;
151*4882a593Smuzhiyun short int events;
152*4882a593Smuzhiyun short int revents;
153*4882a593Smuzhiyun };
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /* for select() */
156*4882a593Smuzhiyun struct timeval {
157*4882a593Smuzhiyun long tv_sec;
158*4882a593Smuzhiyun long tv_usec;
159*4882a593Smuzhiyun };
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /* for pselect() */
162*4882a593Smuzhiyun struct timespec {
163*4882a593Smuzhiyun long tv_sec;
164*4882a593Smuzhiyun long tv_nsec;
165*4882a593Smuzhiyun };
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /* for gettimeofday() */
168*4882a593Smuzhiyun struct timezone {
169*4882a593Smuzhiyun int tz_minuteswest;
170*4882a593Smuzhiyun int tz_dsttime;
171*4882a593Smuzhiyun };
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun /* for getdents64() */
174*4882a593Smuzhiyun struct linux_dirent64 {
175*4882a593Smuzhiyun uint64_t d_ino;
176*4882a593Smuzhiyun int64_t d_off;
177*4882a593Smuzhiyun unsigned short d_reclen;
178*4882a593Smuzhiyun unsigned char d_type;
179*4882a593Smuzhiyun char d_name[];
180*4882a593Smuzhiyun };
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /* commonly an fd_set represents 256 FDs */
183*4882a593Smuzhiyun #define FD_SETSIZE 256
184*4882a593Smuzhiyun typedef struct { uint32_t fd32[FD_SETSIZE/32]; } fd_set;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /* needed by wait4() */
187*4882a593Smuzhiyun struct rusage {
188*4882a593Smuzhiyun struct timeval ru_utime;
189*4882a593Smuzhiyun struct timeval ru_stime;
190*4882a593Smuzhiyun long ru_maxrss;
191*4882a593Smuzhiyun long ru_ixrss;
192*4882a593Smuzhiyun long ru_idrss;
193*4882a593Smuzhiyun long ru_isrss;
194*4882a593Smuzhiyun long ru_minflt;
195*4882a593Smuzhiyun long ru_majflt;
196*4882a593Smuzhiyun long ru_nswap;
197*4882a593Smuzhiyun long ru_inblock;
198*4882a593Smuzhiyun long ru_oublock;
199*4882a593Smuzhiyun long ru_msgsnd;
200*4882a593Smuzhiyun long ru_msgrcv;
201*4882a593Smuzhiyun long ru_nsignals;
202*4882a593Smuzhiyun long ru_nvcsw;
203*4882a593Smuzhiyun long ru_nivcsw;
204*4882a593Smuzhiyun };
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /* stat flags (WARNING, octal here) */
207*4882a593Smuzhiyun #define S_IFDIR 0040000
208*4882a593Smuzhiyun #define S_IFCHR 0020000
209*4882a593Smuzhiyun #define S_IFBLK 0060000
210*4882a593Smuzhiyun #define S_IFREG 0100000
211*4882a593Smuzhiyun #define S_IFIFO 0010000
212*4882a593Smuzhiyun #define S_IFLNK 0120000
213*4882a593Smuzhiyun #define S_IFSOCK 0140000
214*4882a593Smuzhiyun #define S_IFMT 0170000
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun #define S_ISDIR(mode) (((mode) & S_IFDIR) == S_IFDIR)
217*4882a593Smuzhiyun #define S_ISCHR(mode) (((mode) & S_IFCHR) == S_IFCHR)
218*4882a593Smuzhiyun #define S_ISBLK(mode) (((mode) & S_IFBLK) == S_IFBLK)
219*4882a593Smuzhiyun #define S_ISREG(mode) (((mode) & S_IFREG) == S_IFREG)
220*4882a593Smuzhiyun #define S_ISFIFO(mode) (((mode) & S_IFIFO) == S_IFIFO)
221*4882a593Smuzhiyun #define S_ISLNK(mode) (((mode) & S_IFLNK) == S_IFLNK)
222*4882a593Smuzhiyun #define S_ISSOCK(mode) (((mode) & S_IFSOCK) == S_IFSOCK)
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun #define DT_UNKNOWN 0
225*4882a593Smuzhiyun #define DT_FIFO 1
226*4882a593Smuzhiyun #define DT_CHR 2
227*4882a593Smuzhiyun #define DT_DIR 4
228*4882a593Smuzhiyun #define DT_BLK 6
229*4882a593Smuzhiyun #define DT_REG 8
230*4882a593Smuzhiyun #define DT_LNK 10
231*4882a593Smuzhiyun #define DT_SOCK 12
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun /* all the *at functions */
234*4882a593Smuzhiyun #ifndef AT_FDWCD
235*4882a593Smuzhiyun #define AT_FDCWD -100
236*4882a593Smuzhiyun #endif
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun /* lseek */
239*4882a593Smuzhiyun #define SEEK_SET 0
240*4882a593Smuzhiyun #define SEEK_CUR 1
241*4882a593Smuzhiyun #define SEEK_END 2
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /* reboot */
244*4882a593Smuzhiyun #define LINUX_REBOOT_MAGIC1 0xfee1dead
245*4882a593Smuzhiyun #define LINUX_REBOOT_MAGIC2 0x28121969
246*4882a593Smuzhiyun #define LINUX_REBOOT_CMD_HALT 0xcdef0123
247*4882a593Smuzhiyun #define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc
248*4882a593Smuzhiyun #define LINUX_REBOOT_CMD_RESTART 0x01234567
249*4882a593Smuzhiyun #define LINUX_REBOOT_CMD_SW_SUSPEND 0xd000fce2
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /* The format of the struct as returned by the libc to the application, which
253*4882a593Smuzhiyun * significantly differs from the format returned by the stat() syscall flavours.
254*4882a593Smuzhiyun */
255*4882a593Smuzhiyun struct stat {
256*4882a593Smuzhiyun dev_t st_dev; /* ID of device containing file */
257*4882a593Smuzhiyun ino_t st_ino; /* inode number */
258*4882a593Smuzhiyun mode_t st_mode; /* protection */
259*4882a593Smuzhiyun nlink_t st_nlink; /* number of hard links */
260*4882a593Smuzhiyun uid_t st_uid; /* user ID of owner */
261*4882a593Smuzhiyun gid_t st_gid; /* group ID of owner */
262*4882a593Smuzhiyun dev_t st_rdev; /* device ID (if special file) */
263*4882a593Smuzhiyun off_t st_size; /* total size, in bytes */
264*4882a593Smuzhiyun blksize_t st_blksize; /* blocksize for file system I/O */
265*4882a593Smuzhiyun blkcnt_t st_blocks; /* number of 512B blocks allocated */
266*4882a593Smuzhiyun time_t st_atime; /* time of last access */
267*4882a593Smuzhiyun time_t st_mtime; /* time of last modification */
268*4882a593Smuzhiyun time_t st_ctime; /* time of last status change */
269*4882a593Smuzhiyun };
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun #define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
272*4882a593Smuzhiyun #define WIFEXITED(status) (((status) & 0x7f) == 0)
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun /* Below comes the architecture-specific code. For each architecture, we have
276*4882a593Smuzhiyun * the syscall declarations and the _start code definition. This is the only
277*4882a593Smuzhiyun * global part. On all architectures the kernel puts everything in the stack
278*4882a593Smuzhiyun * before jumping to _start just above us, without any return address (_start
279*4882a593Smuzhiyun * is not a function but an entry pint). So at the stack pointer we find argc.
280*4882a593Smuzhiyun * Then argv[] begins, and ends at the first NULL. Then we have envp which
281*4882a593Smuzhiyun * starts and ends with a NULL as well. So envp=argv+argc+1.
282*4882a593Smuzhiyun */
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun #if defined(__x86_64__)
285*4882a593Smuzhiyun /* Syscalls for x86_64 :
286*4882a593Smuzhiyun * - registers are 64-bit
287*4882a593Smuzhiyun * - syscall number is passed in rax
288*4882a593Smuzhiyun * - arguments are in rdi, rsi, rdx, r10, r8, r9 respectively
289*4882a593Smuzhiyun * - the system call is performed by calling the syscall instruction
290*4882a593Smuzhiyun * - syscall return comes in rax
291*4882a593Smuzhiyun * - rcx and r8..r11 may be clobbered, others are preserved.
292*4882a593Smuzhiyun * - the arguments are cast to long and assigned into the target registers
293*4882a593Smuzhiyun * which are then simply passed as registers to the asm code, so that we
294*4882a593Smuzhiyun * don't have to experience issues with register constraints.
295*4882a593Smuzhiyun * - the syscall number is always specified last in order to allow to force
296*4882a593Smuzhiyun * some registers before (gcc refuses a %-register at the last position).
297*4882a593Smuzhiyun */
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun #define my_syscall0(num) \
300*4882a593Smuzhiyun ({ \
301*4882a593Smuzhiyun long _ret; \
302*4882a593Smuzhiyun register long _num asm("rax") = (num); \
303*4882a593Smuzhiyun \
304*4882a593Smuzhiyun asm volatile ( \
305*4882a593Smuzhiyun "syscall\n" \
306*4882a593Smuzhiyun : "=a" (_ret) \
307*4882a593Smuzhiyun : "0"(_num) \
308*4882a593Smuzhiyun : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \
309*4882a593Smuzhiyun ); \
310*4882a593Smuzhiyun _ret; \
311*4882a593Smuzhiyun })
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun #define my_syscall1(num, arg1) \
314*4882a593Smuzhiyun ({ \
315*4882a593Smuzhiyun long _ret; \
316*4882a593Smuzhiyun register long _num asm("rax") = (num); \
317*4882a593Smuzhiyun register long _arg1 asm("rdi") = (long)(arg1); \
318*4882a593Smuzhiyun \
319*4882a593Smuzhiyun asm volatile ( \
320*4882a593Smuzhiyun "syscall\n" \
321*4882a593Smuzhiyun : "=a" (_ret) \
322*4882a593Smuzhiyun : "r"(_arg1), \
323*4882a593Smuzhiyun "0"(_num) \
324*4882a593Smuzhiyun : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \
325*4882a593Smuzhiyun ); \
326*4882a593Smuzhiyun _ret; \
327*4882a593Smuzhiyun })
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun #define my_syscall2(num, arg1, arg2) \
330*4882a593Smuzhiyun ({ \
331*4882a593Smuzhiyun long _ret; \
332*4882a593Smuzhiyun register long _num asm("rax") = (num); \
333*4882a593Smuzhiyun register long _arg1 asm("rdi") = (long)(arg1); \
334*4882a593Smuzhiyun register long _arg2 asm("rsi") = (long)(arg2); \
335*4882a593Smuzhiyun \
336*4882a593Smuzhiyun asm volatile ( \
337*4882a593Smuzhiyun "syscall\n" \
338*4882a593Smuzhiyun : "=a" (_ret) \
339*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), \
340*4882a593Smuzhiyun "0"(_num) \
341*4882a593Smuzhiyun : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \
342*4882a593Smuzhiyun ); \
343*4882a593Smuzhiyun _ret; \
344*4882a593Smuzhiyun })
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun #define my_syscall3(num, arg1, arg2, arg3) \
347*4882a593Smuzhiyun ({ \
348*4882a593Smuzhiyun long _ret; \
349*4882a593Smuzhiyun register long _num asm("rax") = (num); \
350*4882a593Smuzhiyun register long _arg1 asm("rdi") = (long)(arg1); \
351*4882a593Smuzhiyun register long _arg2 asm("rsi") = (long)(arg2); \
352*4882a593Smuzhiyun register long _arg3 asm("rdx") = (long)(arg3); \
353*4882a593Smuzhiyun \
354*4882a593Smuzhiyun asm volatile ( \
355*4882a593Smuzhiyun "syscall\n" \
356*4882a593Smuzhiyun : "=a" (_ret) \
357*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), "r"(_arg3), \
358*4882a593Smuzhiyun "0"(_num) \
359*4882a593Smuzhiyun : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \
360*4882a593Smuzhiyun ); \
361*4882a593Smuzhiyun _ret; \
362*4882a593Smuzhiyun })
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun #define my_syscall4(num, arg1, arg2, arg3, arg4) \
365*4882a593Smuzhiyun ({ \
366*4882a593Smuzhiyun long _ret; \
367*4882a593Smuzhiyun register long _num asm("rax") = (num); \
368*4882a593Smuzhiyun register long _arg1 asm("rdi") = (long)(arg1); \
369*4882a593Smuzhiyun register long _arg2 asm("rsi") = (long)(arg2); \
370*4882a593Smuzhiyun register long _arg3 asm("rdx") = (long)(arg3); \
371*4882a593Smuzhiyun register long _arg4 asm("r10") = (long)(arg4); \
372*4882a593Smuzhiyun \
373*4882a593Smuzhiyun asm volatile ( \
374*4882a593Smuzhiyun "syscall\n" \
375*4882a593Smuzhiyun : "=a" (_ret), "=r"(_arg4) \
376*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \
377*4882a593Smuzhiyun "0"(_num) \
378*4882a593Smuzhiyun : "rcx", "r8", "r9", "r11", "memory", "cc" \
379*4882a593Smuzhiyun ); \
380*4882a593Smuzhiyun _ret; \
381*4882a593Smuzhiyun })
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
384*4882a593Smuzhiyun ({ \
385*4882a593Smuzhiyun long _ret; \
386*4882a593Smuzhiyun register long _num asm("rax") = (num); \
387*4882a593Smuzhiyun register long _arg1 asm("rdi") = (long)(arg1); \
388*4882a593Smuzhiyun register long _arg2 asm("rsi") = (long)(arg2); \
389*4882a593Smuzhiyun register long _arg3 asm("rdx") = (long)(arg3); \
390*4882a593Smuzhiyun register long _arg4 asm("r10") = (long)(arg4); \
391*4882a593Smuzhiyun register long _arg5 asm("r8") = (long)(arg5); \
392*4882a593Smuzhiyun \
393*4882a593Smuzhiyun asm volatile ( \
394*4882a593Smuzhiyun "syscall\n" \
395*4882a593Smuzhiyun : "=a" (_ret), "=r"(_arg4), "=r"(_arg5) \
396*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
397*4882a593Smuzhiyun "0"(_num) \
398*4882a593Smuzhiyun : "rcx", "r9", "r11", "memory", "cc" \
399*4882a593Smuzhiyun ); \
400*4882a593Smuzhiyun _ret; \
401*4882a593Smuzhiyun })
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
404*4882a593Smuzhiyun ({ \
405*4882a593Smuzhiyun long _ret; \
406*4882a593Smuzhiyun register long _num asm("rax") = (num); \
407*4882a593Smuzhiyun register long _arg1 asm("rdi") = (long)(arg1); \
408*4882a593Smuzhiyun register long _arg2 asm("rsi") = (long)(arg2); \
409*4882a593Smuzhiyun register long _arg3 asm("rdx") = (long)(arg3); \
410*4882a593Smuzhiyun register long _arg4 asm("r10") = (long)(arg4); \
411*4882a593Smuzhiyun register long _arg5 asm("r8") = (long)(arg5); \
412*4882a593Smuzhiyun register long _arg6 asm("r9") = (long)(arg6); \
413*4882a593Smuzhiyun \
414*4882a593Smuzhiyun asm volatile ( \
415*4882a593Smuzhiyun "syscall\n" \
416*4882a593Smuzhiyun : "=a" (_ret), "=r"(_arg4), "=r"(_arg5) \
417*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
418*4882a593Smuzhiyun "r"(_arg6), "0"(_num) \
419*4882a593Smuzhiyun : "rcx", "r11", "memory", "cc" \
420*4882a593Smuzhiyun ); \
421*4882a593Smuzhiyun _ret; \
422*4882a593Smuzhiyun })
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun /* startup code */
425*4882a593Smuzhiyun /*
426*4882a593Smuzhiyun * x86-64 System V ABI mandates:
427*4882a593Smuzhiyun * 1) %rsp must be 16-byte aligned right before the function call.
428*4882a593Smuzhiyun * 2) The deepest stack frame should be zero (the %rbp).
429*4882a593Smuzhiyun *
430*4882a593Smuzhiyun */
431*4882a593Smuzhiyun asm(".section .text\n"
432*4882a593Smuzhiyun ".global _start\n"
433*4882a593Smuzhiyun "_start:\n"
434*4882a593Smuzhiyun "pop %rdi\n" // argc (first arg, %rdi)
435*4882a593Smuzhiyun "mov %rsp, %rsi\n" // argv[] (second arg, %rsi)
436*4882a593Smuzhiyun "lea 8(%rsi,%rdi,8),%rdx\n" // then a NULL then envp (third arg, %rdx)
437*4882a593Smuzhiyun "xor %ebp, %ebp\n" // zero the stack frame
438*4882a593Smuzhiyun "and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned before call
439*4882a593Smuzhiyun "call main\n" // main() returns the status code, we'll exit with it.
440*4882a593Smuzhiyun "mov %eax, %edi\n" // retrieve exit code (32 bit)
441*4882a593Smuzhiyun "mov $60, %rax\n" // NR_exit == 60
442*4882a593Smuzhiyun "syscall\n" // really exit
443*4882a593Smuzhiyun "hlt\n" // ensure it does not return
444*4882a593Smuzhiyun "");
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun /* fcntl / open */
447*4882a593Smuzhiyun #define O_RDONLY 0
448*4882a593Smuzhiyun #define O_WRONLY 1
449*4882a593Smuzhiyun #define O_RDWR 2
450*4882a593Smuzhiyun #define O_CREAT 0x40
451*4882a593Smuzhiyun #define O_EXCL 0x80
452*4882a593Smuzhiyun #define O_NOCTTY 0x100
453*4882a593Smuzhiyun #define O_TRUNC 0x200
454*4882a593Smuzhiyun #define O_APPEND 0x400
455*4882a593Smuzhiyun #define O_NONBLOCK 0x800
456*4882a593Smuzhiyun #define O_DIRECTORY 0x10000
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun /* The struct returned by the stat() syscall, equivalent to stat64(). The
459*4882a593Smuzhiyun * syscall returns 116 bytes and stops in the middle of __unused.
460*4882a593Smuzhiyun */
461*4882a593Smuzhiyun struct sys_stat_struct {
462*4882a593Smuzhiyun unsigned long st_dev;
463*4882a593Smuzhiyun unsigned long st_ino;
464*4882a593Smuzhiyun unsigned long st_nlink;
465*4882a593Smuzhiyun unsigned int st_mode;
466*4882a593Smuzhiyun unsigned int st_uid;
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun unsigned int st_gid;
469*4882a593Smuzhiyun unsigned int __pad0;
470*4882a593Smuzhiyun unsigned long st_rdev;
471*4882a593Smuzhiyun long st_size;
472*4882a593Smuzhiyun long st_blksize;
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun long st_blocks;
475*4882a593Smuzhiyun unsigned long st_atime;
476*4882a593Smuzhiyun unsigned long st_atime_nsec;
477*4882a593Smuzhiyun unsigned long st_mtime;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun unsigned long st_mtime_nsec;
480*4882a593Smuzhiyun unsigned long st_ctime;
481*4882a593Smuzhiyun unsigned long st_ctime_nsec;
482*4882a593Smuzhiyun long __unused[3];
483*4882a593Smuzhiyun };
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun #elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__)
486*4882a593Smuzhiyun /* Syscalls for i386 :
487*4882a593Smuzhiyun * - mostly similar to x86_64
488*4882a593Smuzhiyun * - registers are 32-bit
489*4882a593Smuzhiyun * - syscall number is passed in eax
490*4882a593Smuzhiyun * - arguments are in ebx, ecx, edx, esi, edi, ebp respectively
491*4882a593Smuzhiyun * - all registers are preserved (except eax of course)
492*4882a593Smuzhiyun * - the system call is performed by calling int $0x80
493*4882a593Smuzhiyun * - syscall return comes in eax
494*4882a593Smuzhiyun * - the arguments are cast to long and assigned into the target registers
495*4882a593Smuzhiyun * which are then simply passed as registers to the asm code, so that we
496*4882a593Smuzhiyun * don't have to experience issues with register constraints.
497*4882a593Smuzhiyun * - the syscall number is always specified last in order to allow to force
498*4882a593Smuzhiyun * some registers before (gcc refuses a %-register at the last position).
499*4882a593Smuzhiyun *
500*4882a593Smuzhiyun * Also, i386 supports the old_select syscall if newselect is not available
501*4882a593Smuzhiyun */
502*4882a593Smuzhiyun #define __ARCH_WANT_SYS_OLD_SELECT
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun #define my_syscall0(num) \
505*4882a593Smuzhiyun ({ \
506*4882a593Smuzhiyun long _ret; \
507*4882a593Smuzhiyun register long _num asm("eax") = (num); \
508*4882a593Smuzhiyun \
509*4882a593Smuzhiyun asm volatile ( \
510*4882a593Smuzhiyun "int $0x80\n" \
511*4882a593Smuzhiyun : "=a" (_ret) \
512*4882a593Smuzhiyun : "0"(_num) \
513*4882a593Smuzhiyun : "memory", "cc" \
514*4882a593Smuzhiyun ); \
515*4882a593Smuzhiyun _ret; \
516*4882a593Smuzhiyun })
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun #define my_syscall1(num, arg1) \
519*4882a593Smuzhiyun ({ \
520*4882a593Smuzhiyun long _ret; \
521*4882a593Smuzhiyun register long _num asm("eax") = (num); \
522*4882a593Smuzhiyun register long _arg1 asm("ebx") = (long)(arg1); \
523*4882a593Smuzhiyun \
524*4882a593Smuzhiyun asm volatile ( \
525*4882a593Smuzhiyun "int $0x80\n" \
526*4882a593Smuzhiyun : "=a" (_ret) \
527*4882a593Smuzhiyun : "r"(_arg1), \
528*4882a593Smuzhiyun "0"(_num) \
529*4882a593Smuzhiyun : "memory", "cc" \
530*4882a593Smuzhiyun ); \
531*4882a593Smuzhiyun _ret; \
532*4882a593Smuzhiyun })
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun #define my_syscall2(num, arg1, arg2) \
535*4882a593Smuzhiyun ({ \
536*4882a593Smuzhiyun long _ret; \
537*4882a593Smuzhiyun register long _num asm("eax") = (num); \
538*4882a593Smuzhiyun register long _arg1 asm("ebx") = (long)(arg1); \
539*4882a593Smuzhiyun register long _arg2 asm("ecx") = (long)(arg2); \
540*4882a593Smuzhiyun \
541*4882a593Smuzhiyun asm volatile ( \
542*4882a593Smuzhiyun "int $0x80\n" \
543*4882a593Smuzhiyun : "=a" (_ret) \
544*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), \
545*4882a593Smuzhiyun "0"(_num) \
546*4882a593Smuzhiyun : "memory", "cc" \
547*4882a593Smuzhiyun ); \
548*4882a593Smuzhiyun _ret; \
549*4882a593Smuzhiyun })
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun #define my_syscall3(num, arg1, arg2, arg3) \
552*4882a593Smuzhiyun ({ \
553*4882a593Smuzhiyun long _ret; \
554*4882a593Smuzhiyun register long _num asm("eax") = (num); \
555*4882a593Smuzhiyun register long _arg1 asm("ebx") = (long)(arg1); \
556*4882a593Smuzhiyun register long _arg2 asm("ecx") = (long)(arg2); \
557*4882a593Smuzhiyun register long _arg3 asm("edx") = (long)(arg3); \
558*4882a593Smuzhiyun \
559*4882a593Smuzhiyun asm volatile ( \
560*4882a593Smuzhiyun "int $0x80\n" \
561*4882a593Smuzhiyun : "=a" (_ret) \
562*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), "r"(_arg3), \
563*4882a593Smuzhiyun "0"(_num) \
564*4882a593Smuzhiyun : "memory", "cc" \
565*4882a593Smuzhiyun ); \
566*4882a593Smuzhiyun _ret; \
567*4882a593Smuzhiyun })
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun #define my_syscall4(num, arg1, arg2, arg3, arg4) \
570*4882a593Smuzhiyun ({ \
571*4882a593Smuzhiyun long _ret; \
572*4882a593Smuzhiyun register long _num asm("eax") = (num); \
573*4882a593Smuzhiyun register long _arg1 asm("ebx") = (long)(arg1); \
574*4882a593Smuzhiyun register long _arg2 asm("ecx") = (long)(arg2); \
575*4882a593Smuzhiyun register long _arg3 asm("edx") = (long)(arg3); \
576*4882a593Smuzhiyun register long _arg4 asm("esi") = (long)(arg4); \
577*4882a593Smuzhiyun \
578*4882a593Smuzhiyun asm volatile ( \
579*4882a593Smuzhiyun "int $0x80\n" \
580*4882a593Smuzhiyun : "=a" (_ret) \
581*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \
582*4882a593Smuzhiyun "0"(_num) \
583*4882a593Smuzhiyun : "memory", "cc" \
584*4882a593Smuzhiyun ); \
585*4882a593Smuzhiyun _ret; \
586*4882a593Smuzhiyun })
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
589*4882a593Smuzhiyun ({ \
590*4882a593Smuzhiyun long _ret; \
591*4882a593Smuzhiyun register long _num asm("eax") = (num); \
592*4882a593Smuzhiyun register long _arg1 asm("ebx") = (long)(arg1); \
593*4882a593Smuzhiyun register long _arg2 asm("ecx") = (long)(arg2); \
594*4882a593Smuzhiyun register long _arg3 asm("edx") = (long)(arg3); \
595*4882a593Smuzhiyun register long _arg4 asm("esi") = (long)(arg4); \
596*4882a593Smuzhiyun register long _arg5 asm("edi") = (long)(arg5); \
597*4882a593Smuzhiyun \
598*4882a593Smuzhiyun asm volatile ( \
599*4882a593Smuzhiyun "int $0x80\n" \
600*4882a593Smuzhiyun : "=a" (_ret) \
601*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
602*4882a593Smuzhiyun "0"(_num) \
603*4882a593Smuzhiyun : "memory", "cc" \
604*4882a593Smuzhiyun ); \
605*4882a593Smuzhiyun _ret; \
606*4882a593Smuzhiyun })
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun /* startup code */
609*4882a593Smuzhiyun /*
610*4882a593Smuzhiyun * i386 System V ABI mandates:
611*4882a593Smuzhiyun * 1) last pushed argument must be 16-byte aligned.
612*4882a593Smuzhiyun * 2) The deepest stack frame should be set to zero
613*4882a593Smuzhiyun *
614*4882a593Smuzhiyun */
615*4882a593Smuzhiyun asm(".section .text\n"
616*4882a593Smuzhiyun ".global _start\n"
617*4882a593Smuzhiyun "_start:\n"
618*4882a593Smuzhiyun "pop %eax\n" // argc (first arg, %eax)
619*4882a593Smuzhiyun "mov %esp, %ebx\n" // argv[] (second arg, %ebx)
620*4882a593Smuzhiyun "lea 4(%ebx,%eax,4),%ecx\n" // then a NULL then envp (third arg, %ecx)
621*4882a593Smuzhiyun "xor %ebp, %ebp\n" // zero the stack frame
622*4882a593Smuzhiyun "and $-16, %esp\n" // x86 ABI : esp must be 16-byte aligned before
623*4882a593Smuzhiyun "sub $4, %esp\n" // the call instruction (args are aligned)
624*4882a593Smuzhiyun "push %ecx\n" // push all registers on the stack so that we
625*4882a593Smuzhiyun "push %ebx\n" // support both regparm and plain stack modes
626*4882a593Smuzhiyun "push %eax\n"
627*4882a593Smuzhiyun "call main\n" // main() returns the status code in %eax
628*4882a593Smuzhiyun "mov %eax, %ebx\n" // retrieve exit code (32-bit int)
629*4882a593Smuzhiyun "movl $1, %eax\n" // NR_exit == 1
630*4882a593Smuzhiyun "int $0x80\n" // exit now
631*4882a593Smuzhiyun "hlt\n" // ensure it does not
632*4882a593Smuzhiyun "");
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun /* fcntl / open */
635*4882a593Smuzhiyun #define O_RDONLY 0
636*4882a593Smuzhiyun #define O_WRONLY 1
637*4882a593Smuzhiyun #define O_RDWR 2
638*4882a593Smuzhiyun #define O_CREAT 0x40
639*4882a593Smuzhiyun #define O_EXCL 0x80
640*4882a593Smuzhiyun #define O_NOCTTY 0x100
641*4882a593Smuzhiyun #define O_TRUNC 0x200
642*4882a593Smuzhiyun #define O_APPEND 0x400
643*4882a593Smuzhiyun #define O_NONBLOCK 0x800
644*4882a593Smuzhiyun #define O_DIRECTORY 0x10000
645*4882a593Smuzhiyun
646*4882a593Smuzhiyun /* The struct returned by the stat() syscall, 32-bit only, the syscall returns
647*4882a593Smuzhiyun * exactly 56 bytes (stops before the unused array).
648*4882a593Smuzhiyun */
649*4882a593Smuzhiyun struct sys_stat_struct {
650*4882a593Smuzhiyun unsigned long st_dev;
651*4882a593Smuzhiyun unsigned long st_ino;
652*4882a593Smuzhiyun unsigned short st_mode;
653*4882a593Smuzhiyun unsigned short st_nlink;
654*4882a593Smuzhiyun unsigned short st_uid;
655*4882a593Smuzhiyun unsigned short st_gid;
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun unsigned long st_rdev;
658*4882a593Smuzhiyun unsigned long st_size;
659*4882a593Smuzhiyun unsigned long st_blksize;
660*4882a593Smuzhiyun unsigned long st_blocks;
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun unsigned long st_atime;
663*4882a593Smuzhiyun unsigned long st_atime_nsec;
664*4882a593Smuzhiyun unsigned long st_mtime;
665*4882a593Smuzhiyun unsigned long st_mtime_nsec;
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun unsigned long st_ctime;
668*4882a593Smuzhiyun unsigned long st_ctime_nsec;
669*4882a593Smuzhiyun unsigned long __unused[2];
670*4882a593Smuzhiyun };
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun #elif defined(__ARM_EABI__)
673*4882a593Smuzhiyun /* Syscalls for ARM in ARM or Thumb modes :
674*4882a593Smuzhiyun * - registers are 32-bit
675*4882a593Smuzhiyun * - stack is 8-byte aligned
676*4882a593Smuzhiyun * ( http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka4127.html)
677*4882a593Smuzhiyun * - syscall number is passed in r7
678*4882a593Smuzhiyun * - arguments are in r0, r1, r2, r3, r4, r5
679*4882a593Smuzhiyun * - the system call is performed by calling svc #0
680*4882a593Smuzhiyun * - syscall return comes in r0.
681*4882a593Smuzhiyun * - only lr is clobbered.
682*4882a593Smuzhiyun * - the arguments are cast to long and assigned into the target registers
683*4882a593Smuzhiyun * which are then simply passed as registers to the asm code, so that we
684*4882a593Smuzhiyun * don't have to experience issues with register constraints.
685*4882a593Smuzhiyun * - the syscall number is always specified last in order to allow to force
686*4882a593Smuzhiyun * some registers before (gcc refuses a %-register at the last position).
687*4882a593Smuzhiyun *
688*4882a593Smuzhiyun * Also, ARM supports the old_select syscall if newselect is not available
689*4882a593Smuzhiyun */
690*4882a593Smuzhiyun #define __ARCH_WANT_SYS_OLD_SELECT
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun #define my_syscall0(num) \
693*4882a593Smuzhiyun ({ \
694*4882a593Smuzhiyun register long _num asm("r7") = (num); \
695*4882a593Smuzhiyun register long _arg1 asm("r0"); \
696*4882a593Smuzhiyun \
697*4882a593Smuzhiyun asm volatile ( \
698*4882a593Smuzhiyun "svc #0\n" \
699*4882a593Smuzhiyun : "=r"(_arg1) \
700*4882a593Smuzhiyun : "r"(_num) \
701*4882a593Smuzhiyun : "memory", "cc", "lr" \
702*4882a593Smuzhiyun ); \
703*4882a593Smuzhiyun _arg1; \
704*4882a593Smuzhiyun })
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun #define my_syscall1(num, arg1) \
707*4882a593Smuzhiyun ({ \
708*4882a593Smuzhiyun register long _num asm("r7") = (num); \
709*4882a593Smuzhiyun register long _arg1 asm("r0") = (long)(arg1); \
710*4882a593Smuzhiyun \
711*4882a593Smuzhiyun asm volatile ( \
712*4882a593Smuzhiyun "svc #0\n" \
713*4882a593Smuzhiyun : "=r"(_arg1) \
714*4882a593Smuzhiyun : "r"(_arg1), \
715*4882a593Smuzhiyun "r"(_num) \
716*4882a593Smuzhiyun : "memory", "cc", "lr" \
717*4882a593Smuzhiyun ); \
718*4882a593Smuzhiyun _arg1; \
719*4882a593Smuzhiyun })
720*4882a593Smuzhiyun
721*4882a593Smuzhiyun #define my_syscall2(num, arg1, arg2) \
722*4882a593Smuzhiyun ({ \
723*4882a593Smuzhiyun register long _num asm("r7") = (num); \
724*4882a593Smuzhiyun register long _arg1 asm("r0") = (long)(arg1); \
725*4882a593Smuzhiyun register long _arg2 asm("r1") = (long)(arg2); \
726*4882a593Smuzhiyun \
727*4882a593Smuzhiyun asm volatile ( \
728*4882a593Smuzhiyun "svc #0\n" \
729*4882a593Smuzhiyun : "=r"(_arg1) \
730*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), \
731*4882a593Smuzhiyun "r"(_num) \
732*4882a593Smuzhiyun : "memory", "cc", "lr" \
733*4882a593Smuzhiyun ); \
734*4882a593Smuzhiyun _arg1; \
735*4882a593Smuzhiyun })
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun #define my_syscall3(num, arg1, arg2, arg3) \
738*4882a593Smuzhiyun ({ \
739*4882a593Smuzhiyun register long _num asm("r7") = (num); \
740*4882a593Smuzhiyun register long _arg1 asm("r0") = (long)(arg1); \
741*4882a593Smuzhiyun register long _arg2 asm("r1") = (long)(arg2); \
742*4882a593Smuzhiyun register long _arg3 asm("r2") = (long)(arg3); \
743*4882a593Smuzhiyun \
744*4882a593Smuzhiyun asm volatile ( \
745*4882a593Smuzhiyun "svc #0\n" \
746*4882a593Smuzhiyun : "=r"(_arg1) \
747*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), "r"(_arg3), \
748*4882a593Smuzhiyun "r"(_num) \
749*4882a593Smuzhiyun : "memory", "cc", "lr" \
750*4882a593Smuzhiyun ); \
751*4882a593Smuzhiyun _arg1; \
752*4882a593Smuzhiyun })
753*4882a593Smuzhiyun
754*4882a593Smuzhiyun #define my_syscall4(num, arg1, arg2, arg3, arg4) \
755*4882a593Smuzhiyun ({ \
756*4882a593Smuzhiyun register long _num asm("r7") = (num); \
757*4882a593Smuzhiyun register long _arg1 asm("r0") = (long)(arg1); \
758*4882a593Smuzhiyun register long _arg2 asm("r1") = (long)(arg2); \
759*4882a593Smuzhiyun register long _arg3 asm("r2") = (long)(arg3); \
760*4882a593Smuzhiyun register long _arg4 asm("r3") = (long)(arg4); \
761*4882a593Smuzhiyun \
762*4882a593Smuzhiyun asm volatile ( \
763*4882a593Smuzhiyun "svc #0\n" \
764*4882a593Smuzhiyun : "=r"(_arg1) \
765*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \
766*4882a593Smuzhiyun "r"(_num) \
767*4882a593Smuzhiyun : "memory", "cc", "lr" \
768*4882a593Smuzhiyun ); \
769*4882a593Smuzhiyun _arg1; \
770*4882a593Smuzhiyun })
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
773*4882a593Smuzhiyun ({ \
774*4882a593Smuzhiyun register long _num asm("r7") = (num); \
775*4882a593Smuzhiyun register long _arg1 asm("r0") = (long)(arg1); \
776*4882a593Smuzhiyun register long _arg2 asm("r1") = (long)(arg2); \
777*4882a593Smuzhiyun register long _arg3 asm("r2") = (long)(arg3); \
778*4882a593Smuzhiyun register long _arg4 asm("r3") = (long)(arg4); \
779*4882a593Smuzhiyun register long _arg5 asm("r4") = (long)(arg5); \
780*4882a593Smuzhiyun \
781*4882a593Smuzhiyun asm volatile ( \
782*4882a593Smuzhiyun "svc #0\n" \
783*4882a593Smuzhiyun : "=r" (_arg1) \
784*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
785*4882a593Smuzhiyun "r"(_num) \
786*4882a593Smuzhiyun : "memory", "cc", "lr" \
787*4882a593Smuzhiyun ); \
788*4882a593Smuzhiyun _arg1; \
789*4882a593Smuzhiyun })
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun /* startup code */
792*4882a593Smuzhiyun asm(".section .text\n"
793*4882a593Smuzhiyun ".global _start\n"
794*4882a593Smuzhiyun "_start:\n"
795*4882a593Smuzhiyun #if defined(__THUMBEB__) || defined(__THUMBEL__)
796*4882a593Smuzhiyun /* We enter here in 32-bit mode but if some previous functions were in
797*4882a593Smuzhiyun * 16-bit mode, the assembler cannot know, so we need to tell it we're in
798*4882a593Smuzhiyun * 32-bit now, then switch to 16-bit (is there a better way to do it than
799*4882a593Smuzhiyun * adding 1 by hand ?) and tell the asm we're now in 16-bit mode so that
800*4882a593Smuzhiyun * it generates correct instructions. Note that we do not support thumb1.
801*4882a593Smuzhiyun */
802*4882a593Smuzhiyun ".code 32\n"
803*4882a593Smuzhiyun "add r0, pc, #1\n"
804*4882a593Smuzhiyun "bx r0\n"
805*4882a593Smuzhiyun ".code 16\n"
806*4882a593Smuzhiyun #endif
807*4882a593Smuzhiyun "pop {%r0}\n" // argc was in the stack
808*4882a593Smuzhiyun "mov %r1, %sp\n" // argv = sp
809*4882a593Smuzhiyun "add %r2, %r1, %r0, lsl #2\n" // envp = argv + 4*argc ...
810*4882a593Smuzhiyun "add %r2, %r2, $4\n" // ... + 4
811*4882a593Smuzhiyun "and %r3, %r1, $-8\n" // AAPCS : sp must be 8-byte aligned in the
812*4882a593Smuzhiyun "mov %sp, %r3\n" // callee, an bl doesn't push (lr=pc)
813*4882a593Smuzhiyun "bl main\n" // main() returns the status code, we'll exit with it.
814*4882a593Smuzhiyun "movs r7, $1\n" // NR_exit == 1
815*4882a593Smuzhiyun "svc $0x00\n"
816*4882a593Smuzhiyun "");
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun /* fcntl / open */
819*4882a593Smuzhiyun #define O_RDONLY 0
820*4882a593Smuzhiyun #define O_WRONLY 1
821*4882a593Smuzhiyun #define O_RDWR 2
822*4882a593Smuzhiyun #define O_CREAT 0x40
823*4882a593Smuzhiyun #define O_EXCL 0x80
824*4882a593Smuzhiyun #define O_NOCTTY 0x100
825*4882a593Smuzhiyun #define O_TRUNC 0x200
826*4882a593Smuzhiyun #define O_APPEND 0x400
827*4882a593Smuzhiyun #define O_NONBLOCK 0x800
828*4882a593Smuzhiyun #define O_DIRECTORY 0x4000
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun /* The struct returned by the stat() syscall, 32-bit only, the syscall returns
831*4882a593Smuzhiyun * exactly 56 bytes (stops before the unused array). In big endian, the format
832*4882a593Smuzhiyun * differs as devices are returned as short only.
833*4882a593Smuzhiyun */
834*4882a593Smuzhiyun struct sys_stat_struct {
835*4882a593Smuzhiyun #if defined(__ARMEB__)
836*4882a593Smuzhiyun unsigned short st_dev;
837*4882a593Smuzhiyun unsigned short __pad1;
838*4882a593Smuzhiyun #else
839*4882a593Smuzhiyun unsigned long st_dev;
840*4882a593Smuzhiyun #endif
841*4882a593Smuzhiyun unsigned long st_ino;
842*4882a593Smuzhiyun unsigned short st_mode;
843*4882a593Smuzhiyun unsigned short st_nlink;
844*4882a593Smuzhiyun unsigned short st_uid;
845*4882a593Smuzhiyun unsigned short st_gid;
846*4882a593Smuzhiyun #if defined(__ARMEB__)
847*4882a593Smuzhiyun unsigned short st_rdev;
848*4882a593Smuzhiyun unsigned short __pad2;
849*4882a593Smuzhiyun #else
850*4882a593Smuzhiyun unsigned long st_rdev;
851*4882a593Smuzhiyun #endif
852*4882a593Smuzhiyun unsigned long st_size;
853*4882a593Smuzhiyun unsigned long st_blksize;
854*4882a593Smuzhiyun unsigned long st_blocks;
855*4882a593Smuzhiyun unsigned long st_atime;
856*4882a593Smuzhiyun unsigned long st_atime_nsec;
857*4882a593Smuzhiyun unsigned long st_mtime;
858*4882a593Smuzhiyun unsigned long st_mtime_nsec;
859*4882a593Smuzhiyun unsigned long st_ctime;
860*4882a593Smuzhiyun unsigned long st_ctime_nsec;
861*4882a593Smuzhiyun unsigned long __unused[2];
862*4882a593Smuzhiyun };
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun #elif defined(__aarch64__)
865*4882a593Smuzhiyun /* Syscalls for AARCH64 :
866*4882a593Smuzhiyun * - registers are 64-bit
867*4882a593Smuzhiyun * - stack is 16-byte aligned
868*4882a593Smuzhiyun * - syscall number is passed in x8
869*4882a593Smuzhiyun * - arguments are in x0, x1, x2, x3, x4, x5
870*4882a593Smuzhiyun * - the system call is performed by calling svc 0
871*4882a593Smuzhiyun * - syscall return comes in x0.
872*4882a593Smuzhiyun * - the arguments are cast to long and assigned into the target registers
873*4882a593Smuzhiyun * which are then simply passed as registers to the asm code, so that we
874*4882a593Smuzhiyun * don't have to experience issues with register constraints.
875*4882a593Smuzhiyun *
876*4882a593Smuzhiyun * On aarch64, select() is not implemented so we have to use pselect6().
877*4882a593Smuzhiyun */
878*4882a593Smuzhiyun #define __ARCH_WANT_SYS_PSELECT6
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun #define my_syscall0(num) \
881*4882a593Smuzhiyun ({ \
882*4882a593Smuzhiyun register long _num asm("x8") = (num); \
883*4882a593Smuzhiyun register long _arg1 asm("x0"); \
884*4882a593Smuzhiyun \
885*4882a593Smuzhiyun asm volatile ( \
886*4882a593Smuzhiyun "svc #0\n" \
887*4882a593Smuzhiyun : "=r"(_arg1) \
888*4882a593Smuzhiyun : "r"(_num) \
889*4882a593Smuzhiyun : "memory", "cc" \
890*4882a593Smuzhiyun ); \
891*4882a593Smuzhiyun _arg1; \
892*4882a593Smuzhiyun })
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun #define my_syscall1(num, arg1) \
895*4882a593Smuzhiyun ({ \
896*4882a593Smuzhiyun register long _num asm("x8") = (num); \
897*4882a593Smuzhiyun register long _arg1 asm("x0") = (long)(arg1); \
898*4882a593Smuzhiyun \
899*4882a593Smuzhiyun asm volatile ( \
900*4882a593Smuzhiyun "svc #0\n" \
901*4882a593Smuzhiyun : "=r"(_arg1) \
902*4882a593Smuzhiyun : "r"(_arg1), \
903*4882a593Smuzhiyun "r"(_num) \
904*4882a593Smuzhiyun : "memory", "cc" \
905*4882a593Smuzhiyun ); \
906*4882a593Smuzhiyun _arg1; \
907*4882a593Smuzhiyun })
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun #define my_syscall2(num, arg1, arg2) \
910*4882a593Smuzhiyun ({ \
911*4882a593Smuzhiyun register long _num asm("x8") = (num); \
912*4882a593Smuzhiyun register long _arg1 asm("x0") = (long)(arg1); \
913*4882a593Smuzhiyun register long _arg2 asm("x1") = (long)(arg2); \
914*4882a593Smuzhiyun \
915*4882a593Smuzhiyun asm volatile ( \
916*4882a593Smuzhiyun "svc #0\n" \
917*4882a593Smuzhiyun : "=r"(_arg1) \
918*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), \
919*4882a593Smuzhiyun "r"(_num) \
920*4882a593Smuzhiyun : "memory", "cc" \
921*4882a593Smuzhiyun ); \
922*4882a593Smuzhiyun _arg1; \
923*4882a593Smuzhiyun })
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun #define my_syscall3(num, arg1, arg2, arg3) \
926*4882a593Smuzhiyun ({ \
927*4882a593Smuzhiyun register long _num asm("x8") = (num); \
928*4882a593Smuzhiyun register long _arg1 asm("x0") = (long)(arg1); \
929*4882a593Smuzhiyun register long _arg2 asm("x1") = (long)(arg2); \
930*4882a593Smuzhiyun register long _arg3 asm("x2") = (long)(arg3); \
931*4882a593Smuzhiyun \
932*4882a593Smuzhiyun asm volatile ( \
933*4882a593Smuzhiyun "svc #0\n" \
934*4882a593Smuzhiyun : "=r"(_arg1) \
935*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), "r"(_arg3), \
936*4882a593Smuzhiyun "r"(_num) \
937*4882a593Smuzhiyun : "memory", "cc" \
938*4882a593Smuzhiyun ); \
939*4882a593Smuzhiyun _arg1; \
940*4882a593Smuzhiyun })
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun #define my_syscall4(num, arg1, arg2, arg3, arg4) \
943*4882a593Smuzhiyun ({ \
944*4882a593Smuzhiyun register long _num asm("x8") = (num); \
945*4882a593Smuzhiyun register long _arg1 asm("x0") = (long)(arg1); \
946*4882a593Smuzhiyun register long _arg2 asm("x1") = (long)(arg2); \
947*4882a593Smuzhiyun register long _arg3 asm("x2") = (long)(arg3); \
948*4882a593Smuzhiyun register long _arg4 asm("x3") = (long)(arg4); \
949*4882a593Smuzhiyun \
950*4882a593Smuzhiyun asm volatile ( \
951*4882a593Smuzhiyun "svc #0\n" \
952*4882a593Smuzhiyun : "=r"(_arg1) \
953*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \
954*4882a593Smuzhiyun "r"(_num) \
955*4882a593Smuzhiyun : "memory", "cc" \
956*4882a593Smuzhiyun ); \
957*4882a593Smuzhiyun _arg1; \
958*4882a593Smuzhiyun })
959*4882a593Smuzhiyun
960*4882a593Smuzhiyun #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
961*4882a593Smuzhiyun ({ \
962*4882a593Smuzhiyun register long _num asm("x8") = (num); \
963*4882a593Smuzhiyun register long _arg1 asm("x0") = (long)(arg1); \
964*4882a593Smuzhiyun register long _arg2 asm("x1") = (long)(arg2); \
965*4882a593Smuzhiyun register long _arg3 asm("x2") = (long)(arg3); \
966*4882a593Smuzhiyun register long _arg4 asm("x3") = (long)(arg4); \
967*4882a593Smuzhiyun register long _arg5 asm("x4") = (long)(arg5); \
968*4882a593Smuzhiyun \
969*4882a593Smuzhiyun asm volatile ( \
970*4882a593Smuzhiyun "svc #0\n" \
971*4882a593Smuzhiyun : "=r" (_arg1) \
972*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
973*4882a593Smuzhiyun "r"(_num) \
974*4882a593Smuzhiyun : "memory", "cc" \
975*4882a593Smuzhiyun ); \
976*4882a593Smuzhiyun _arg1; \
977*4882a593Smuzhiyun })
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
980*4882a593Smuzhiyun ({ \
981*4882a593Smuzhiyun register long _num asm("x8") = (num); \
982*4882a593Smuzhiyun register long _arg1 asm("x0") = (long)(arg1); \
983*4882a593Smuzhiyun register long _arg2 asm("x1") = (long)(arg2); \
984*4882a593Smuzhiyun register long _arg3 asm("x2") = (long)(arg3); \
985*4882a593Smuzhiyun register long _arg4 asm("x3") = (long)(arg4); \
986*4882a593Smuzhiyun register long _arg5 asm("x4") = (long)(arg5); \
987*4882a593Smuzhiyun register long _arg6 asm("x5") = (long)(arg6); \
988*4882a593Smuzhiyun \
989*4882a593Smuzhiyun asm volatile ( \
990*4882a593Smuzhiyun "svc #0\n" \
991*4882a593Smuzhiyun : "=r" (_arg1) \
992*4882a593Smuzhiyun : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
993*4882a593Smuzhiyun "r"(_arg6), "r"(_num) \
994*4882a593Smuzhiyun : "memory", "cc" \
995*4882a593Smuzhiyun ); \
996*4882a593Smuzhiyun _arg1; \
997*4882a593Smuzhiyun })
998*4882a593Smuzhiyun
999*4882a593Smuzhiyun /* startup code */
1000*4882a593Smuzhiyun asm(".section .text\n"
1001*4882a593Smuzhiyun ".global _start\n"
1002*4882a593Smuzhiyun "_start:\n"
1003*4882a593Smuzhiyun "ldr x0, [sp]\n" // argc (x0) was in the stack
1004*4882a593Smuzhiyun "add x1, sp, 8\n" // argv (x1) = sp
1005*4882a593Smuzhiyun "lsl x2, x0, 3\n" // envp (x2) = 8*argc ...
1006*4882a593Smuzhiyun "add x2, x2, 8\n" // + 8 (skip null)
1007*4882a593Smuzhiyun "add x2, x2, x1\n" // + argv
1008*4882a593Smuzhiyun "and sp, x1, -16\n" // sp must be 16-byte aligned in the callee
1009*4882a593Smuzhiyun "bl main\n" // main() returns the status code, we'll exit with it.
1010*4882a593Smuzhiyun "mov x8, 93\n" // NR_exit == 93
1011*4882a593Smuzhiyun "svc #0\n"
1012*4882a593Smuzhiyun "");
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun /* fcntl / open */
1015*4882a593Smuzhiyun #define O_RDONLY 0
1016*4882a593Smuzhiyun #define O_WRONLY 1
1017*4882a593Smuzhiyun #define O_RDWR 2
1018*4882a593Smuzhiyun #define O_CREAT 0x40
1019*4882a593Smuzhiyun #define O_EXCL 0x80
1020*4882a593Smuzhiyun #define O_NOCTTY 0x100
1021*4882a593Smuzhiyun #define O_TRUNC 0x200
1022*4882a593Smuzhiyun #define O_APPEND 0x400
1023*4882a593Smuzhiyun #define O_NONBLOCK 0x800
1024*4882a593Smuzhiyun #define O_DIRECTORY 0x4000
1025*4882a593Smuzhiyun
1026*4882a593Smuzhiyun /* The struct returned by the newfstatat() syscall. Differs slightly from the
1027*4882a593Smuzhiyun * x86_64's stat one by field ordering, so be careful.
1028*4882a593Smuzhiyun */
1029*4882a593Smuzhiyun struct sys_stat_struct {
1030*4882a593Smuzhiyun unsigned long st_dev;
1031*4882a593Smuzhiyun unsigned long st_ino;
1032*4882a593Smuzhiyun unsigned int st_mode;
1033*4882a593Smuzhiyun unsigned int st_nlink;
1034*4882a593Smuzhiyun unsigned int st_uid;
1035*4882a593Smuzhiyun unsigned int st_gid;
1036*4882a593Smuzhiyun
1037*4882a593Smuzhiyun unsigned long st_rdev;
1038*4882a593Smuzhiyun unsigned long __pad1;
1039*4882a593Smuzhiyun long st_size;
1040*4882a593Smuzhiyun int st_blksize;
1041*4882a593Smuzhiyun int __pad2;
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun long st_blocks;
1044*4882a593Smuzhiyun long st_atime;
1045*4882a593Smuzhiyun unsigned long st_atime_nsec;
1046*4882a593Smuzhiyun long st_mtime;
1047*4882a593Smuzhiyun
1048*4882a593Smuzhiyun unsigned long st_mtime_nsec;
1049*4882a593Smuzhiyun long st_ctime;
1050*4882a593Smuzhiyun unsigned long st_ctime_nsec;
1051*4882a593Smuzhiyun unsigned int __unused[2];
1052*4882a593Smuzhiyun };
1053*4882a593Smuzhiyun
1054*4882a593Smuzhiyun #elif defined(__mips__) && defined(_ABIO32)
1055*4882a593Smuzhiyun /* Syscalls for MIPS ABI O32 :
1056*4882a593Smuzhiyun * - WARNING! there's always a delayed slot!
1057*4882a593Smuzhiyun * - WARNING again, the syntax is different, registers take a '$' and numbers
1058*4882a593Smuzhiyun * do not.
1059*4882a593Smuzhiyun * - registers are 32-bit
1060*4882a593Smuzhiyun * - stack is 8-byte aligned
1061*4882a593Smuzhiyun * - syscall number is passed in v0 (starts at 0xfa0).
1062*4882a593Smuzhiyun * - arguments are in a0, a1, a2, a3, then the stack. The caller needs to
1063*4882a593Smuzhiyun * leave some room in the stack for the callee to save a0..a3 if needed.
1064*4882a593Smuzhiyun * - Many registers are clobbered, in fact only a0..a2 and s0..s8 are
1065*4882a593Smuzhiyun * preserved. See: https://www.linux-mips.org/wiki/Syscall as well as
1066*4882a593Smuzhiyun * scall32-o32.S in the kernel sources.
1067*4882a593Smuzhiyun * - the system call is performed by calling "syscall"
1068*4882a593Smuzhiyun * - syscall return comes in v0, and register a3 needs to be checked to know
1069*4882a593Smuzhiyun * if an error occured, in which case errno is in v0.
1070*4882a593Smuzhiyun * - the arguments are cast to long and assigned into the target registers
1071*4882a593Smuzhiyun * which are then simply passed as registers to the asm code, so that we
1072*4882a593Smuzhiyun * don't have to experience issues with register constraints.
1073*4882a593Smuzhiyun */
1074*4882a593Smuzhiyun
1075*4882a593Smuzhiyun #define my_syscall0(num) \
1076*4882a593Smuzhiyun ({ \
1077*4882a593Smuzhiyun register long _num asm("v0") = (num); \
1078*4882a593Smuzhiyun register long _arg4 asm("a3"); \
1079*4882a593Smuzhiyun \
1080*4882a593Smuzhiyun asm volatile ( \
1081*4882a593Smuzhiyun "addiu $sp, $sp, -32\n" \
1082*4882a593Smuzhiyun "syscall\n" \
1083*4882a593Smuzhiyun "addiu $sp, $sp, 32\n" \
1084*4882a593Smuzhiyun : "=r"(_num), "=r"(_arg4) \
1085*4882a593Smuzhiyun : "r"(_num) \
1086*4882a593Smuzhiyun : "memory", "cc", "at", "v1", "hi", "lo", \
1087*4882a593Smuzhiyun "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \
1088*4882a593Smuzhiyun ); \
1089*4882a593Smuzhiyun _arg4 ? -_num : _num; \
1090*4882a593Smuzhiyun })
1091*4882a593Smuzhiyun
1092*4882a593Smuzhiyun #define my_syscall1(num, arg1) \
1093*4882a593Smuzhiyun ({ \
1094*4882a593Smuzhiyun register long _num asm("v0") = (num); \
1095*4882a593Smuzhiyun register long _arg1 asm("a0") = (long)(arg1); \
1096*4882a593Smuzhiyun register long _arg4 asm("a3"); \
1097*4882a593Smuzhiyun \
1098*4882a593Smuzhiyun asm volatile ( \
1099*4882a593Smuzhiyun "addiu $sp, $sp, -32\n" \
1100*4882a593Smuzhiyun "syscall\n" \
1101*4882a593Smuzhiyun "addiu $sp, $sp, 32\n" \
1102*4882a593Smuzhiyun : "=r"(_num), "=r"(_arg4) \
1103*4882a593Smuzhiyun : "0"(_num), \
1104*4882a593Smuzhiyun "r"(_arg1) \
1105*4882a593Smuzhiyun : "memory", "cc", "at", "v1", "hi", "lo", \
1106*4882a593Smuzhiyun "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \
1107*4882a593Smuzhiyun ); \
1108*4882a593Smuzhiyun _arg4 ? -_num : _num; \
1109*4882a593Smuzhiyun })
1110*4882a593Smuzhiyun
1111*4882a593Smuzhiyun #define my_syscall2(num, arg1, arg2) \
1112*4882a593Smuzhiyun ({ \
1113*4882a593Smuzhiyun register long _num asm("v0") = (num); \
1114*4882a593Smuzhiyun register long _arg1 asm("a0") = (long)(arg1); \
1115*4882a593Smuzhiyun register long _arg2 asm("a1") = (long)(arg2); \
1116*4882a593Smuzhiyun register long _arg4 asm("a3"); \
1117*4882a593Smuzhiyun \
1118*4882a593Smuzhiyun asm volatile ( \
1119*4882a593Smuzhiyun "addiu $sp, $sp, -32\n" \
1120*4882a593Smuzhiyun "syscall\n" \
1121*4882a593Smuzhiyun "addiu $sp, $sp, 32\n" \
1122*4882a593Smuzhiyun : "=r"(_num), "=r"(_arg4) \
1123*4882a593Smuzhiyun : "0"(_num), \
1124*4882a593Smuzhiyun "r"(_arg1), "r"(_arg2) \
1125*4882a593Smuzhiyun : "memory", "cc", "at", "v1", "hi", "lo", \
1126*4882a593Smuzhiyun "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \
1127*4882a593Smuzhiyun ); \
1128*4882a593Smuzhiyun _arg4 ? -_num : _num; \
1129*4882a593Smuzhiyun })
1130*4882a593Smuzhiyun
1131*4882a593Smuzhiyun #define my_syscall3(num, arg1, arg2, arg3) \
1132*4882a593Smuzhiyun ({ \
1133*4882a593Smuzhiyun register long _num asm("v0") = (num); \
1134*4882a593Smuzhiyun register long _arg1 asm("a0") = (long)(arg1); \
1135*4882a593Smuzhiyun register long _arg2 asm("a1") = (long)(arg2); \
1136*4882a593Smuzhiyun register long _arg3 asm("a2") = (long)(arg3); \
1137*4882a593Smuzhiyun register long _arg4 asm("a3"); \
1138*4882a593Smuzhiyun \
1139*4882a593Smuzhiyun asm volatile ( \
1140*4882a593Smuzhiyun "addiu $sp, $sp, -32\n" \
1141*4882a593Smuzhiyun "syscall\n" \
1142*4882a593Smuzhiyun "addiu $sp, $sp, 32\n" \
1143*4882a593Smuzhiyun : "=r"(_num), "=r"(_arg4) \
1144*4882a593Smuzhiyun : "0"(_num), \
1145*4882a593Smuzhiyun "r"(_arg1), "r"(_arg2), "r"(_arg3) \
1146*4882a593Smuzhiyun : "memory", "cc", "at", "v1", "hi", "lo", \
1147*4882a593Smuzhiyun "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \
1148*4882a593Smuzhiyun ); \
1149*4882a593Smuzhiyun _arg4 ? -_num : _num; \
1150*4882a593Smuzhiyun })
1151*4882a593Smuzhiyun
1152*4882a593Smuzhiyun #define my_syscall4(num, arg1, arg2, arg3, arg4) \
1153*4882a593Smuzhiyun ({ \
1154*4882a593Smuzhiyun register long _num asm("v0") = (num); \
1155*4882a593Smuzhiyun register long _arg1 asm("a0") = (long)(arg1); \
1156*4882a593Smuzhiyun register long _arg2 asm("a1") = (long)(arg2); \
1157*4882a593Smuzhiyun register long _arg3 asm("a2") = (long)(arg3); \
1158*4882a593Smuzhiyun register long _arg4 asm("a3") = (long)(arg4); \
1159*4882a593Smuzhiyun \
1160*4882a593Smuzhiyun asm volatile ( \
1161*4882a593Smuzhiyun "addiu $sp, $sp, -32\n" \
1162*4882a593Smuzhiyun "syscall\n" \
1163*4882a593Smuzhiyun "addiu $sp, $sp, 32\n" \
1164*4882a593Smuzhiyun : "=r" (_num), "=r"(_arg4) \
1165*4882a593Smuzhiyun : "0"(_num), \
1166*4882a593Smuzhiyun "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4) \
1167*4882a593Smuzhiyun : "memory", "cc", "at", "v1", "hi", "lo", \
1168*4882a593Smuzhiyun "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \
1169*4882a593Smuzhiyun ); \
1170*4882a593Smuzhiyun _arg4 ? -_num : _num; \
1171*4882a593Smuzhiyun })
1172*4882a593Smuzhiyun
1173*4882a593Smuzhiyun #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
1174*4882a593Smuzhiyun ({ \
1175*4882a593Smuzhiyun register long _num asm("v0") = (num); \
1176*4882a593Smuzhiyun register long _arg1 asm("a0") = (long)(arg1); \
1177*4882a593Smuzhiyun register long _arg2 asm("a1") = (long)(arg2); \
1178*4882a593Smuzhiyun register long _arg3 asm("a2") = (long)(arg3); \
1179*4882a593Smuzhiyun register long _arg4 asm("a3") = (long)(arg4); \
1180*4882a593Smuzhiyun register long _arg5 = (long)(arg5); \
1181*4882a593Smuzhiyun \
1182*4882a593Smuzhiyun asm volatile ( \
1183*4882a593Smuzhiyun "addiu $sp, $sp, -32\n" \
1184*4882a593Smuzhiyun "sw %7, 16($sp)\n" \
1185*4882a593Smuzhiyun "syscall\n " \
1186*4882a593Smuzhiyun "addiu $sp, $sp, 32\n" \
1187*4882a593Smuzhiyun : "=r" (_num), "=r"(_arg4) \
1188*4882a593Smuzhiyun : "0"(_num), \
1189*4882a593Smuzhiyun "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5) \
1190*4882a593Smuzhiyun : "memory", "cc", "at", "v1", "hi", "lo", \
1191*4882a593Smuzhiyun "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \
1192*4882a593Smuzhiyun ); \
1193*4882a593Smuzhiyun _arg4 ? -_num : _num; \
1194*4882a593Smuzhiyun })
1195*4882a593Smuzhiyun
1196*4882a593Smuzhiyun /* startup code, note that it's called __start on MIPS */
1197*4882a593Smuzhiyun asm(".section .text\n"
1198*4882a593Smuzhiyun ".set nomips16\n"
1199*4882a593Smuzhiyun ".global __start\n"
1200*4882a593Smuzhiyun ".set noreorder\n"
1201*4882a593Smuzhiyun ".option pic0\n"
1202*4882a593Smuzhiyun ".ent __start\n"
1203*4882a593Smuzhiyun "__start:\n"
1204*4882a593Smuzhiyun "lw $a0,($sp)\n" // argc was in the stack
1205*4882a593Smuzhiyun "addiu $a1, $sp, 4\n" // argv = sp + 4
1206*4882a593Smuzhiyun "sll $a2, $a0, 2\n" // a2 = argc * 4
1207*4882a593Smuzhiyun "add $a2, $a2, $a1\n" // envp = argv + 4*argc ...
1208*4882a593Smuzhiyun "addiu $a2, $a2, 4\n" // ... + 4
1209*4882a593Smuzhiyun "li $t0, -8\n"
1210*4882a593Smuzhiyun "and $sp, $sp, $t0\n" // sp must be 8-byte aligned
1211*4882a593Smuzhiyun "addiu $sp,$sp,-16\n" // the callee expects to save a0..a3 there!
1212*4882a593Smuzhiyun "jal main\n" // main() returns the status code, we'll exit with it.
1213*4882a593Smuzhiyun "nop\n" // delayed slot
1214*4882a593Smuzhiyun "move $a0, $v0\n" // retrieve 32-bit exit code from v0
1215*4882a593Smuzhiyun "li $v0, 4001\n" // NR_exit == 4001
1216*4882a593Smuzhiyun "syscall\n"
1217*4882a593Smuzhiyun ".end __start\n"
1218*4882a593Smuzhiyun "");
1219*4882a593Smuzhiyun
1220*4882a593Smuzhiyun /* fcntl / open */
1221*4882a593Smuzhiyun #define O_RDONLY 0
1222*4882a593Smuzhiyun #define O_WRONLY 1
1223*4882a593Smuzhiyun #define O_RDWR 2
1224*4882a593Smuzhiyun #define O_APPEND 0x0008
1225*4882a593Smuzhiyun #define O_NONBLOCK 0x0080
1226*4882a593Smuzhiyun #define O_CREAT 0x0100
1227*4882a593Smuzhiyun #define O_TRUNC 0x0200
1228*4882a593Smuzhiyun #define O_EXCL 0x0400
1229*4882a593Smuzhiyun #define O_NOCTTY 0x0800
1230*4882a593Smuzhiyun #define O_DIRECTORY 0x10000
1231*4882a593Smuzhiyun
1232*4882a593Smuzhiyun /* The struct returned by the stat() syscall. 88 bytes are returned by the
1233*4882a593Smuzhiyun * syscall.
1234*4882a593Smuzhiyun */
1235*4882a593Smuzhiyun struct sys_stat_struct {
1236*4882a593Smuzhiyun unsigned int st_dev;
1237*4882a593Smuzhiyun long st_pad1[3];
1238*4882a593Smuzhiyun unsigned long st_ino;
1239*4882a593Smuzhiyun unsigned int st_mode;
1240*4882a593Smuzhiyun unsigned int st_nlink;
1241*4882a593Smuzhiyun unsigned int st_uid;
1242*4882a593Smuzhiyun unsigned int st_gid;
1243*4882a593Smuzhiyun unsigned int st_rdev;
1244*4882a593Smuzhiyun long st_pad2[2];
1245*4882a593Smuzhiyun long st_size;
1246*4882a593Smuzhiyun long st_pad3;
1247*4882a593Smuzhiyun long st_atime;
1248*4882a593Smuzhiyun long st_atime_nsec;
1249*4882a593Smuzhiyun long st_mtime;
1250*4882a593Smuzhiyun long st_mtime_nsec;
1251*4882a593Smuzhiyun long st_ctime;
1252*4882a593Smuzhiyun long st_ctime_nsec;
1253*4882a593Smuzhiyun long st_blksize;
1254*4882a593Smuzhiyun long st_blocks;
1255*4882a593Smuzhiyun long st_pad4[14];
1256*4882a593Smuzhiyun };
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun #elif defined(__riscv)
1259*4882a593Smuzhiyun
1260*4882a593Smuzhiyun #if __riscv_xlen == 64
1261*4882a593Smuzhiyun #define PTRLOG "3"
1262*4882a593Smuzhiyun #define SZREG "8"
1263*4882a593Smuzhiyun #elif __riscv_xlen == 32
1264*4882a593Smuzhiyun #define PTRLOG "2"
1265*4882a593Smuzhiyun #define SZREG "4"
1266*4882a593Smuzhiyun #endif
1267*4882a593Smuzhiyun
1268*4882a593Smuzhiyun /* Syscalls for RISCV :
1269*4882a593Smuzhiyun * - stack is 16-byte aligned
1270*4882a593Smuzhiyun * - syscall number is passed in a7
1271*4882a593Smuzhiyun * - arguments are in a0, a1, a2, a3, a4, a5
1272*4882a593Smuzhiyun * - the system call is performed by calling ecall
1273*4882a593Smuzhiyun * - syscall return comes in a0
1274*4882a593Smuzhiyun * - the arguments are cast to long and assigned into the target
1275*4882a593Smuzhiyun * registers which are then simply passed as registers to the asm code,
1276*4882a593Smuzhiyun * so that we don't have to experience issues with register constraints.
1277*4882a593Smuzhiyun */
1278*4882a593Smuzhiyun
1279*4882a593Smuzhiyun #define my_syscall0(num) \
1280*4882a593Smuzhiyun ({ \
1281*4882a593Smuzhiyun register long _num asm("a7") = (num); \
1282*4882a593Smuzhiyun register long _arg1 asm("a0"); \
1283*4882a593Smuzhiyun \
1284*4882a593Smuzhiyun asm volatile ( \
1285*4882a593Smuzhiyun "ecall\n\t" \
1286*4882a593Smuzhiyun : "=r"(_arg1) \
1287*4882a593Smuzhiyun : "r"(_num) \
1288*4882a593Smuzhiyun : "memory", "cc" \
1289*4882a593Smuzhiyun ); \
1290*4882a593Smuzhiyun _arg1; \
1291*4882a593Smuzhiyun })
1292*4882a593Smuzhiyun
1293*4882a593Smuzhiyun #define my_syscall1(num, arg1) \
1294*4882a593Smuzhiyun ({ \
1295*4882a593Smuzhiyun register long _num asm("a7") = (num); \
1296*4882a593Smuzhiyun register long _arg1 asm("a0") = (long)(arg1); \
1297*4882a593Smuzhiyun \
1298*4882a593Smuzhiyun asm volatile ( \
1299*4882a593Smuzhiyun "ecall\n" \
1300*4882a593Smuzhiyun : "+r"(_arg1) \
1301*4882a593Smuzhiyun : "r"(_num) \
1302*4882a593Smuzhiyun : "memory", "cc" \
1303*4882a593Smuzhiyun ); \
1304*4882a593Smuzhiyun _arg1; \
1305*4882a593Smuzhiyun })
1306*4882a593Smuzhiyun
1307*4882a593Smuzhiyun #define my_syscall2(num, arg1, arg2) \
1308*4882a593Smuzhiyun ({ \
1309*4882a593Smuzhiyun register long _num asm("a7") = (num); \
1310*4882a593Smuzhiyun register long _arg1 asm("a0") = (long)(arg1); \
1311*4882a593Smuzhiyun register long _arg2 asm("a1") = (long)(arg2); \
1312*4882a593Smuzhiyun \
1313*4882a593Smuzhiyun asm volatile ( \
1314*4882a593Smuzhiyun "ecall\n" \
1315*4882a593Smuzhiyun : "+r"(_arg1) \
1316*4882a593Smuzhiyun : "r"(_arg2), \
1317*4882a593Smuzhiyun "r"(_num) \
1318*4882a593Smuzhiyun : "memory", "cc" \
1319*4882a593Smuzhiyun ); \
1320*4882a593Smuzhiyun _arg1; \
1321*4882a593Smuzhiyun })
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun #define my_syscall3(num, arg1, arg2, arg3) \
1324*4882a593Smuzhiyun ({ \
1325*4882a593Smuzhiyun register long _num asm("a7") = (num); \
1326*4882a593Smuzhiyun register long _arg1 asm("a0") = (long)(arg1); \
1327*4882a593Smuzhiyun register long _arg2 asm("a1") = (long)(arg2); \
1328*4882a593Smuzhiyun register long _arg3 asm("a2") = (long)(arg3); \
1329*4882a593Smuzhiyun \
1330*4882a593Smuzhiyun asm volatile ( \
1331*4882a593Smuzhiyun "ecall\n\t" \
1332*4882a593Smuzhiyun : "+r"(_arg1) \
1333*4882a593Smuzhiyun : "r"(_arg2), "r"(_arg3), \
1334*4882a593Smuzhiyun "r"(_num) \
1335*4882a593Smuzhiyun : "memory", "cc" \
1336*4882a593Smuzhiyun ); \
1337*4882a593Smuzhiyun _arg1; \
1338*4882a593Smuzhiyun })
1339*4882a593Smuzhiyun
1340*4882a593Smuzhiyun #define my_syscall4(num, arg1, arg2, arg3, arg4) \
1341*4882a593Smuzhiyun ({ \
1342*4882a593Smuzhiyun register long _num asm("a7") = (num); \
1343*4882a593Smuzhiyun register long _arg1 asm("a0") = (long)(arg1); \
1344*4882a593Smuzhiyun register long _arg2 asm("a1") = (long)(arg2); \
1345*4882a593Smuzhiyun register long _arg3 asm("a2") = (long)(arg3); \
1346*4882a593Smuzhiyun register long _arg4 asm("a3") = (long)(arg4); \
1347*4882a593Smuzhiyun \
1348*4882a593Smuzhiyun asm volatile ( \
1349*4882a593Smuzhiyun "ecall\n" \
1350*4882a593Smuzhiyun : "+r"(_arg1) \
1351*4882a593Smuzhiyun : "r"(_arg2), "r"(_arg3), "r"(_arg4), \
1352*4882a593Smuzhiyun "r"(_num) \
1353*4882a593Smuzhiyun : "memory", "cc" \
1354*4882a593Smuzhiyun ); \
1355*4882a593Smuzhiyun _arg1; \
1356*4882a593Smuzhiyun })
1357*4882a593Smuzhiyun
1358*4882a593Smuzhiyun #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
1359*4882a593Smuzhiyun ({ \
1360*4882a593Smuzhiyun register long _num asm("a7") = (num); \
1361*4882a593Smuzhiyun register long _arg1 asm("a0") = (long)(arg1); \
1362*4882a593Smuzhiyun register long _arg2 asm("a1") = (long)(arg2); \
1363*4882a593Smuzhiyun register long _arg3 asm("a2") = (long)(arg3); \
1364*4882a593Smuzhiyun register long _arg4 asm("a3") = (long)(arg4); \
1365*4882a593Smuzhiyun register long _arg5 asm("a4") = (long)(arg5); \
1366*4882a593Smuzhiyun \
1367*4882a593Smuzhiyun asm volatile ( \
1368*4882a593Smuzhiyun "ecall\n" \
1369*4882a593Smuzhiyun : "+r"(_arg1) \
1370*4882a593Smuzhiyun : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
1371*4882a593Smuzhiyun "r"(_num) \
1372*4882a593Smuzhiyun : "memory", "cc" \
1373*4882a593Smuzhiyun ); \
1374*4882a593Smuzhiyun _arg1; \
1375*4882a593Smuzhiyun })
1376*4882a593Smuzhiyun
1377*4882a593Smuzhiyun #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
1378*4882a593Smuzhiyun ({ \
1379*4882a593Smuzhiyun register long _num asm("a7") = (num); \
1380*4882a593Smuzhiyun register long _arg1 asm("a0") = (long)(arg1); \
1381*4882a593Smuzhiyun register long _arg2 asm("a1") = (long)(arg2); \
1382*4882a593Smuzhiyun register long _arg3 asm("a2") = (long)(arg3); \
1383*4882a593Smuzhiyun register long _arg4 asm("a3") = (long)(arg4); \
1384*4882a593Smuzhiyun register long _arg5 asm("a4") = (long)(arg5); \
1385*4882a593Smuzhiyun register long _arg6 asm("a5") = (long)(arg6); \
1386*4882a593Smuzhiyun \
1387*4882a593Smuzhiyun asm volatile ( \
1388*4882a593Smuzhiyun "ecall\n" \
1389*4882a593Smuzhiyun : "+r"(_arg1) \
1390*4882a593Smuzhiyun : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), "r"(_arg6), \
1391*4882a593Smuzhiyun "r"(_num) \
1392*4882a593Smuzhiyun : "memory", "cc" \
1393*4882a593Smuzhiyun ); \
1394*4882a593Smuzhiyun _arg1; \
1395*4882a593Smuzhiyun })
1396*4882a593Smuzhiyun
1397*4882a593Smuzhiyun /* startup code */
1398*4882a593Smuzhiyun asm(".section .text\n"
1399*4882a593Smuzhiyun ".global _start\n"
1400*4882a593Smuzhiyun "_start:\n"
1401*4882a593Smuzhiyun ".option push\n"
1402*4882a593Smuzhiyun ".option norelax\n"
1403*4882a593Smuzhiyun "lla gp, __global_pointer$\n"
1404*4882a593Smuzhiyun ".option pop\n"
1405*4882a593Smuzhiyun "ld a0, 0(sp)\n" // argc (a0) was in the stack
1406*4882a593Smuzhiyun "add a1, sp, "SZREG"\n" // argv (a1) = sp
1407*4882a593Smuzhiyun "slli a2, a0, "PTRLOG"\n" // envp (a2) = SZREG*argc ...
1408*4882a593Smuzhiyun "add a2, a2, "SZREG"\n" // + SZREG (skip null)
1409*4882a593Smuzhiyun "add a2,a2,a1\n" // + argv
1410*4882a593Smuzhiyun "andi sp,a1,-16\n" // sp must be 16-byte aligned
1411*4882a593Smuzhiyun "call main\n" // main() returns the status code, we'll exit with it.
1412*4882a593Smuzhiyun "li a7, 93\n" // NR_exit == 93
1413*4882a593Smuzhiyun "ecall\n"
1414*4882a593Smuzhiyun "");
1415*4882a593Smuzhiyun
1416*4882a593Smuzhiyun /* fcntl / open */
1417*4882a593Smuzhiyun #define O_RDONLY 0
1418*4882a593Smuzhiyun #define O_WRONLY 1
1419*4882a593Smuzhiyun #define O_RDWR 2
1420*4882a593Smuzhiyun #define O_CREAT 0x100
1421*4882a593Smuzhiyun #define O_EXCL 0x200
1422*4882a593Smuzhiyun #define O_NOCTTY 0x400
1423*4882a593Smuzhiyun #define O_TRUNC 0x1000
1424*4882a593Smuzhiyun #define O_APPEND 0x2000
1425*4882a593Smuzhiyun #define O_NONBLOCK 0x4000
1426*4882a593Smuzhiyun #define O_DIRECTORY 0x200000
1427*4882a593Smuzhiyun
1428*4882a593Smuzhiyun struct sys_stat_struct {
1429*4882a593Smuzhiyun unsigned long st_dev; /* Device. */
1430*4882a593Smuzhiyun unsigned long st_ino; /* File serial number. */
1431*4882a593Smuzhiyun unsigned int st_mode; /* File mode. */
1432*4882a593Smuzhiyun unsigned int st_nlink; /* Link count. */
1433*4882a593Smuzhiyun unsigned int st_uid; /* User ID of the file's owner. */
1434*4882a593Smuzhiyun unsigned int st_gid; /* Group ID of the file's group. */
1435*4882a593Smuzhiyun unsigned long st_rdev; /* Device number, if device. */
1436*4882a593Smuzhiyun unsigned long __pad1;
1437*4882a593Smuzhiyun long st_size; /* Size of file, in bytes. */
1438*4882a593Smuzhiyun int st_blksize; /* Optimal block size for I/O. */
1439*4882a593Smuzhiyun int __pad2;
1440*4882a593Smuzhiyun long st_blocks; /* Number 512-byte blocks allocated. */
1441*4882a593Smuzhiyun long st_atime; /* Time of last access. */
1442*4882a593Smuzhiyun unsigned long st_atime_nsec;
1443*4882a593Smuzhiyun long st_mtime; /* Time of last modification. */
1444*4882a593Smuzhiyun unsigned long st_mtime_nsec;
1445*4882a593Smuzhiyun long st_ctime; /* Time of last status change. */
1446*4882a593Smuzhiyun unsigned long st_ctime_nsec;
1447*4882a593Smuzhiyun unsigned int __unused4;
1448*4882a593Smuzhiyun unsigned int __unused5;
1449*4882a593Smuzhiyun };
1450*4882a593Smuzhiyun
1451*4882a593Smuzhiyun #endif
1452*4882a593Smuzhiyun
1453*4882a593Smuzhiyun
1454*4882a593Smuzhiyun /* Below are the C functions used to declare the raw syscalls. They try to be
1455*4882a593Smuzhiyun * architecture-agnostic, and return either a success or -errno. Declaring them
1456*4882a593Smuzhiyun * static will lead to them being inlined in most cases, but it's still possible
1457*4882a593Smuzhiyun * to reference them by a pointer if needed.
1458*4882a593Smuzhiyun */
1459*4882a593Smuzhiyun static __attribute__((unused))
sys_brk(void * addr)1460*4882a593Smuzhiyun void *sys_brk(void *addr)
1461*4882a593Smuzhiyun {
1462*4882a593Smuzhiyun return (void *)my_syscall1(__NR_brk, addr);
1463*4882a593Smuzhiyun }
1464*4882a593Smuzhiyun
1465*4882a593Smuzhiyun static __attribute__((noreturn,unused))
sys_exit(int status)1466*4882a593Smuzhiyun void sys_exit(int status)
1467*4882a593Smuzhiyun {
1468*4882a593Smuzhiyun my_syscall1(__NR_exit, status & 255);
1469*4882a593Smuzhiyun while(1); // shut the "noreturn" warnings.
1470*4882a593Smuzhiyun }
1471*4882a593Smuzhiyun
1472*4882a593Smuzhiyun static __attribute__((unused))
sys_chdir(const char * path)1473*4882a593Smuzhiyun int sys_chdir(const char *path)
1474*4882a593Smuzhiyun {
1475*4882a593Smuzhiyun return my_syscall1(__NR_chdir, path);
1476*4882a593Smuzhiyun }
1477*4882a593Smuzhiyun
1478*4882a593Smuzhiyun static __attribute__((unused))
sys_chmod(const char * path,mode_t mode)1479*4882a593Smuzhiyun int sys_chmod(const char *path, mode_t mode)
1480*4882a593Smuzhiyun {
1481*4882a593Smuzhiyun #ifdef __NR_fchmodat
1482*4882a593Smuzhiyun return my_syscall4(__NR_fchmodat, AT_FDCWD, path, mode, 0);
1483*4882a593Smuzhiyun #else
1484*4882a593Smuzhiyun return my_syscall2(__NR_chmod, path, mode);
1485*4882a593Smuzhiyun #endif
1486*4882a593Smuzhiyun }
1487*4882a593Smuzhiyun
1488*4882a593Smuzhiyun static __attribute__((unused))
sys_chown(const char * path,uid_t owner,gid_t group)1489*4882a593Smuzhiyun int sys_chown(const char *path, uid_t owner, gid_t group)
1490*4882a593Smuzhiyun {
1491*4882a593Smuzhiyun #ifdef __NR_fchownat
1492*4882a593Smuzhiyun return my_syscall5(__NR_fchownat, AT_FDCWD, path, owner, group, 0);
1493*4882a593Smuzhiyun #else
1494*4882a593Smuzhiyun return my_syscall3(__NR_chown, path, owner, group);
1495*4882a593Smuzhiyun #endif
1496*4882a593Smuzhiyun }
1497*4882a593Smuzhiyun
1498*4882a593Smuzhiyun static __attribute__((unused))
sys_chroot(const char * path)1499*4882a593Smuzhiyun int sys_chroot(const char *path)
1500*4882a593Smuzhiyun {
1501*4882a593Smuzhiyun return my_syscall1(__NR_chroot, path);
1502*4882a593Smuzhiyun }
1503*4882a593Smuzhiyun
1504*4882a593Smuzhiyun static __attribute__((unused))
sys_close(int fd)1505*4882a593Smuzhiyun int sys_close(int fd)
1506*4882a593Smuzhiyun {
1507*4882a593Smuzhiyun return my_syscall1(__NR_close, fd);
1508*4882a593Smuzhiyun }
1509*4882a593Smuzhiyun
1510*4882a593Smuzhiyun static __attribute__((unused))
sys_dup(int fd)1511*4882a593Smuzhiyun int sys_dup(int fd)
1512*4882a593Smuzhiyun {
1513*4882a593Smuzhiyun return my_syscall1(__NR_dup, fd);
1514*4882a593Smuzhiyun }
1515*4882a593Smuzhiyun
1516*4882a593Smuzhiyun static __attribute__((unused))
sys_dup2(int old,int new)1517*4882a593Smuzhiyun int sys_dup2(int old, int new)
1518*4882a593Smuzhiyun {
1519*4882a593Smuzhiyun return my_syscall2(__NR_dup2, old, new);
1520*4882a593Smuzhiyun }
1521*4882a593Smuzhiyun
1522*4882a593Smuzhiyun static __attribute__((unused))
sys_execve(const char * filename,char * const argv[],char * const envp[])1523*4882a593Smuzhiyun int sys_execve(const char *filename, char *const argv[], char *const envp[])
1524*4882a593Smuzhiyun {
1525*4882a593Smuzhiyun return my_syscall3(__NR_execve, filename, argv, envp);
1526*4882a593Smuzhiyun }
1527*4882a593Smuzhiyun
1528*4882a593Smuzhiyun static __attribute__((unused))
sys_fork(void)1529*4882a593Smuzhiyun pid_t sys_fork(void)
1530*4882a593Smuzhiyun {
1531*4882a593Smuzhiyun return my_syscall0(__NR_fork);
1532*4882a593Smuzhiyun }
1533*4882a593Smuzhiyun
1534*4882a593Smuzhiyun static __attribute__((unused))
sys_fsync(int fd)1535*4882a593Smuzhiyun int sys_fsync(int fd)
1536*4882a593Smuzhiyun {
1537*4882a593Smuzhiyun return my_syscall1(__NR_fsync, fd);
1538*4882a593Smuzhiyun }
1539*4882a593Smuzhiyun
1540*4882a593Smuzhiyun static __attribute__((unused))
sys_getdents64(int fd,struct linux_dirent64 * dirp,int count)1541*4882a593Smuzhiyun int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count)
1542*4882a593Smuzhiyun {
1543*4882a593Smuzhiyun return my_syscall3(__NR_getdents64, fd, dirp, count);
1544*4882a593Smuzhiyun }
1545*4882a593Smuzhiyun
1546*4882a593Smuzhiyun static __attribute__((unused))
sys_getpgrp(void)1547*4882a593Smuzhiyun pid_t sys_getpgrp(void)
1548*4882a593Smuzhiyun {
1549*4882a593Smuzhiyun return my_syscall0(__NR_getpgrp);
1550*4882a593Smuzhiyun }
1551*4882a593Smuzhiyun
1552*4882a593Smuzhiyun static __attribute__((unused))
sys_getpid(void)1553*4882a593Smuzhiyun pid_t sys_getpid(void)
1554*4882a593Smuzhiyun {
1555*4882a593Smuzhiyun return my_syscall0(__NR_getpid);
1556*4882a593Smuzhiyun }
1557*4882a593Smuzhiyun
1558*4882a593Smuzhiyun static __attribute__((unused))
sys_gettimeofday(struct timeval * tv,struct timezone * tz)1559*4882a593Smuzhiyun int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
1560*4882a593Smuzhiyun {
1561*4882a593Smuzhiyun return my_syscall2(__NR_gettimeofday, tv, tz);
1562*4882a593Smuzhiyun }
1563*4882a593Smuzhiyun
1564*4882a593Smuzhiyun static __attribute__((unused))
sys_ioctl(int fd,unsigned long req,void * value)1565*4882a593Smuzhiyun int sys_ioctl(int fd, unsigned long req, void *value)
1566*4882a593Smuzhiyun {
1567*4882a593Smuzhiyun return my_syscall3(__NR_ioctl, fd, req, value);
1568*4882a593Smuzhiyun }
1569*4882a593Smuzhiyun
1570*4882a593Smuzhiyun static __attribute__((unused))
sys_kill(pid_t pid,int signal)1571*4882a593Smuzhiyun int sys_kill(pid_t pid, int signal)
1572*4882a593Smuzhiyun {
1573*4882a593Smuzhiyun return my_syscall2(__NR_kill, pid, signal);
1574*4882a593Smuzhiyun }
1575*4882a593Smuzhiyun
1576*4882a593Smuzhiyun static __attribute__((unused))
sys_link(const char * old,const char * new)1577*4882a593Smuzhiyun int sys_link(const char *old, const char *new)
1578*4882a593Smuzhiyun {
1579*4882a593Smuzhiyun #ifdef __NR_linkat
1580*4882a593Smuzhiyun return my_syscall5(__NR_linkat, AT_FDCWD, old, AT_FDCWD, new, 0);
1581*4882a593Smuzhiyun #else
1582*4882a593Smuzhiyun return my_syscall2(__NR_link, old, new);
1583*4882a593Smuzhiyun #endif
1584*4882a593Smuzhiyun }
1585*4882a593Smuzhiyun
1586*4882a593Smuzhiyun static __attribute__((unused))
sys_lseek(int fd,off_t offset,int whence)1587*4882a593Smuzhiyun off_t sys_lseek(int fd, off_t offset, int whence)
1588*4882a593Smuzhiyun {
1589*4882a593Smuzhiyun return my_syscall3(__NR_lseek, fd, offset, whence);
1590*4882a593Smuzhiyun }
1591*4882a593Smuzhiyun
1592*4882a593Smuzhiyun static __attribute__((unused))
sys_mkdir(const char * path,mode_t mode)1593*4882a593Smuzhiyun int sys_mkdir(const char *path, mode_t mode)
1594*4882a593Smuzhiyun {
1595*4882a593Smuzhiyun #ifdef __NR_mkdirat
1596*4882a593Smuzhiyun return my_syscall3(__NR_mkdirat, AT_FDCWD, path, mode);
1597*4882a593Smuzhiyun #else
1598*4882a593Smuzhiyun return my_syscall2(__NR_mkdir, path, mode);
1599*4882a593Smuzhiyun #endif
1600*4882a593Smuzhiyun }
1601*4882a593Smuzhiyun
1602*4882a593Smuzhiyun static __attribute__((unused))
sys_mknod(const char * path,mode_t mode,dev_t dev)1603*4882a593Smuzhiyun long sys_mknod(const char *path, mode_t mode, dev_t dev)
1604*4882a593Smuzhiyun {
1605*4882a593Smuzhiyun #ifdef __NR_mknodat
1606*4882a593Smuzhiyun return my_syscall4(__NR_mknodat, AT_FDCWD, path, mode, dev);
1607*4882a593Smuzhiyun #else
1608*4882a593Smuzhiyun return my_syscall3(__NR_mknod, path, mode, dev);
1609*4882a593Smuzhiyun #endif
1610*4882a593Smuzhiyun }
1611*4882a593Smuzhiyun
1612*4882a593Smuzhiyun static __attribute__((unused))
sys_mount(const char * src,const char * tgt,const char * fst,unsigned long flags,const void * data)1613*4882a593Smuzhiyun int sys_mount(const char *src, const char *tgt, const char *fst,
1614*4882a593Smuzhiyun unsigned long flags, const void *data)
1615*4882a593Smuzhiyun {
1616*4882a593Smuzhiyun return my_syscall5(__NR_mount, src, tgt, fst, flags, data);
1617*4882a593Smuzhiyun }
1618*4882a593Smuzhiyun
1619*4882a593Smuzhiyun static __attribute__((unused))
sys_open(const char * path,int flags,mode_t mode)1620*4882a593Smuzhiyun int sys_open(const char *path, int flags, mode_t mode)
1621*4882a593Smuzhiyun {
1622*4882a593Smuzhiyun #ifdef __NR_openat
1623*4882a593Smuzhiyun return my_syscall4(__NR_openat, AT_FDCWD, path, flags, mode);
1624*4882a593Smuzhiyun #else
1625*4882a593Smuzhiyun return my_syscall3(__NR_open, path, flags, mode);
1626*4882a593Smuzhiyun #endif
1627*4882a593Smuzhiyun }
1628*4882a593Smuzhiyun
1629*4882a593Smuzhiyun static __attribute__((unused))
sys_pivot_root(const char * new,const char * old)1630*4882a593Smuzhiyun int sys_pivot_root(const char *new, const char *old)
1631*4882a593Smuzhiyun {
1632*4882a593Smuzhiyun return my_syscall2(__NR_pivot_root, new, old);
1633*4882a593Smuzhiyun }
1634*4882a593Smuzhiyun
1635*4882a593Smuzhiyun static __attribute__((unused))
sys_poll(struct pollfd * fds,int nfds,int timeout)1636*4882a593Smuzhiyun int sys_poll(struct pollfd *fds, int nfds, int timeout)
1637*4882a593Smuzhiyun {
1638*4882a593Smuzhiyun return my_syscall3(__NR_poll, fds, nfds, timeout);
1639*4882a593Smuzhiyun }
1640*4882a593Smuzhiyun
1641*4882a593Smuzhiyun static __attribute__((unused))
sys_read(int fd,void * buf,size_t count)1642*4882a593Smuzhiyun ssize_t sys_read(int fd, void *buf, size_t count)
1643*4882a593Smuzhiyun {
1644*4882a593Smuzhiyun return my_syscall3(__NR_read, fd, buf, count);
1645*4882a593Smuzhiyun }
1646*4882a593Smuzhiyun
1647*4882a593Smuzhiyun static __attribute__((unused))
sys_reboot(int magic1,int magic2,int cmd,void * arg)1648*4882a593Smuzhiyun ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg)
1649*4882a593Smuzhiyun {
1650*4882a593Smuzhiyun return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg);
1651*4882a593Smuzhiyun }
1652*4882a593Smuzhiyun
1653*4882a593Smuzhiyun static __attribute__((unused))
sys_sched_yield(void)1654*4882a593Smuzhiyun int sys_sched_yield(void)
1655*4882a593Smuzhiyun {
1656*4882a593Smuzhiyun return my_syscall0(__NR_sched_yield);
1657*4882a593Smuzhiyun }
1658*4882a593Smuzhiyun
1659*4882a593Smuzhiyun static __attribute__((unused))
sys_select(int nfds,fd_set * rfds,fd_set * wfds,fd_set * efds,struct timeval * timeout)1660*4882a593Smuzhiyun int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
1661*4882a593Smuzhiyun {
1662*4882a593Smuzhiyun #if defined(__ARCH_WANT_SYS_OLD_SELECT) && !defined(__NR__newselect)
1663*4882a593Smuzhiyun struct sel_arg_struct {
1664*4882a593Smuzhiyun unsigned long n;
1665*4882a593Smuzhiyun fd_set *r, *w, *e;
1666*4882a593Smuzhiyun struct timeval *t;
1667*4882a593Smuzhiyun } arg = { .n = nfds, .r = rfds, .w = wfds, .e = efds, .t = timeout };
1668*4882a593Smuzhiyun return my_syscall1(__NR_select, &arg);
1669*4882a593Smuzhiyun #elif defined(__ARCH_WANT_SYS_PSELECT6) && defined(__NR_pselect6)
1670*4882a593Smuzhiyun struct timespec t;
1671*4882a593Smuzhiyun
1672*4882a593Smuzhiyun if (timeout) {
1673*4882a593Smuzhiyun t.tv_sec = timeout->tv_sec;
1674*4882a593Smuzhiyun t.tv_nsec = timeout->tv_usec * 1000;
1675*4882a593Smuzhiyun }
1676*4882a593Smuzhiyun return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL);
1677*4882a593Smuzhiyun #else
1678*4882a593Smuzhiyun #ifndef __NR__newselect
1679*4882a593Smuzhiyun #define __NR__newselect __NR_select
1680*4882a593Smuzhiyun #endif
1681*4882a593Smuzhiyun return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout);
1682*4882a593Smuzhiyun #endif
1683*4882a593Smuzhiyun }
1684*4882a593Smuzhiyun
1685*4882a593Smuzhiyun static __attribute__((unused))
sys_setpgid(pid_t pid,pid_t pgid)1686*4882a593Smuzhiyun int sys_setpgid(pid_t pid, pid_t pgid)
1687*4882a593Smuzhiyun {
1688*4882a593Smuzhiyun return my_syscall2(__NR_setpgid, pid, pgid);
1689*4882a593Smuzhiyun }
1690*4882a593Smuzhiyun
1691*4882a593Smuzhiyun static __attribute__((unused))
sys_setsid(void)1692*4882a593Smuzhiyun pid_t sys_setsid(void)
1693*4882a593Smuzhiyun {
1694*4882a593Smuzhiyun return my_syscall0(__NR_setsid);
1695*4882a593Smuzhiyun }
1696*4882a593Smuzhiyun
1697*4882a593Smuzhiyun static __attribute__((unused))
sys_stat(const char * path,struct stat * buf)1698*4882a593Smuzhiyun int sys_stat(const char *path, struct stat *buf)
1699*4882a593Smuzhiyun {
1700*4882a593Smuzhiyun struct sys_stat_struct stat;
1701*4882a593Smuzhiyun long ret;
1702*4882a593Smuzhiyun
1703*4882a593Smuzhiyun #ifdef __NR_newfstatat
1704*4882a593Smuzhiyun /* only solution for arm64 */
1705*4882a593Smuzhiyun ret = my_syscall4(__NR_newfstatat, AT_FDCWD, path, &stat, 0);
1706*4882a593Smuzhiyun #else
1707*4882a593Smuzhiyun ret = my_syscall2(__NR_stat, path, &stat);
1708*4882a593Smuzhiyun #endif
1709*4882a593Smuzhiyun buf->st_dev = stat.st_dev;
1710*4882a593Smuzhiyun buf->st_ino = stat.st_ino;
1711*4882a593Smuzhiyun buf->st_mode = stat.st_mode;
1712*4882a593Smuzhiyun buf->st_nlink = stat.st_nlink;
1713*4882a593Smuzhiyun buf->st_uid = stat.st_uid;
1714*4882a593Smuzhiyun buf->st_gid = stat.st_gid;
1715*4882a593Smuzhiyun buf->st_rdev = stat.st_rdev;
1716*4882a593Smuzhiyun buf->st_size = stat.st_size;
1717*4882a593Smuzhiyun buf->st_blksize = stat.st_blksize;
1718*4882a593Smuzhiyun buf->st_blocks = stat.st_blocks;
1719*4882a593Smuzhiyun buf->st_atime = stat.st_atime;
1720*4882a593Smuzhiyun buf->st_mtime = stat.st_mtime;
1721*4882a593Smuzhiyun buf->st_ctime = stat.st_ctime;
1722*4882a593Smuzhiyun return ret;
1723*4882a593Smuzhiyun }
1724*4882a593Smuzhiyun
1725*4882a593Smuzhiyun
1726*4882a593Smuzhiyun static __attribute__((unused))
sys_symlink(const char * old,const char * new)1727*4882a593Smuzhiyun int sys_symlink(const char *old, const char *new)
1728*4882a593Smuzhiyun {
1729*4882a593Smuzhiyun #ifdef __NR_symlinkat
1730*4882a593Smuzhiyun return my_syscall3(__NR_symlinkat, old, AT_FDCWD, new);
1731*4882a593Smuzhiyun #else
1732*4882a593Smuzhiyun return my_syscall2(__NR_symlink, old, new);
1733*4882a593Smuzhiyun #endif
1734*4882a593Smuzhiyun }
1735*4882a593Smuzhiyun
1736*4882a593Smuzhiyun static __attribute__((unused))
sys_umask(mode_t mode)1737*4882a593Smuzhiyun mode_t sys_umask(mode_t mode)
1738*4882a593Smuzhiyun {
1739*4882a593Smuzhiyun return my_syscall1(__NR_umask, mode);
1740*4882a593Smuzhiyun }
1741*4882a593Smuzhiyun
1742*4882a593Smuzhiyun static __attribute__((unused))
sys_umount2(const char * path,int flags)1743*4882a593Smuzhiyun int sys_umount2(const char *path, int flags)
1744*4882a593Smuzhiyun {
1745*4882a593Smuzhiyun return my_syscall2(__NR_umount2, path, flags);
1746*4882a593Smuzhiyun }
1747*4882a593Smuzhiyun
1748*4882a593Smuzhiyun static __attribute__((unused))
sys_unlink(const char * path)1749*4882a593Smuzhiyun int sys_unlink(const char *path)
1750*4882a593Smuzhiyun {
1751*4882a593Smuzhiyun #ifdef __NR_unlinkat
1752*4882a593Smuzhiyun return my_syscall3(__NR_unlinkat, AT_FDCWD, path, 0);
1753*4882a593Smuzhiyun #else
1754*4882a593Smuzhiyun return my_syscall1(__NR_unlink, path);
1755*4882a593Smuzhiyun #endif
1756*4882a593Smuzhiyun }
1757*4882a593Smuzhiyun
1758*4882a593Smuzhiyun static __attribute__((unused))
sys_wait4(pid_t pid,int * status,int options,struct rusage * rusage)1759*4882a593Smuzhiyun pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
1760*4882a593Smuzhiyun {
1761*4882a593Smuzhiyun return my_syscall4(__NR_wait4, pid, status, options, rusage);
1762*4882a593Smuzhiyun }
1763*4882a593Smuzhiyun
1764*4882a593Smuzhiyun static __attribute__((unused))
sys_waitpid(pid_t pid,int * status,int options)1765*4882a593Smuzhiyun pid_t sys_waitpid(pid_t pid, int *status, int options)
1766*4882a593Smuzhiyun {
1767*4882a593Smuzhiyun return sys_wait4(pid, status, options, 0);
1768*4882a593Smuzhiyun }
1769*4882a593Smuzhiyun
1770*4882a593Smuzhiyun static __attribute__((unused))
sys_wait(int * status)1771*4882a593Smuzhiyun pid_t sys_wait(int *status)
1772*4882a593Smuzhiyun {
1773*4882a593Smuzhiyun return sys_waitpid(-1, status, 0);
1774*4882a593Smuzhiyun }
1775*4882a593Smuzhiyun
1776*4882a593Smuzhiyun static __attribute__((unused))
sys_write(int fd,const void * buf,size_t count)1777*4882a593Smuzhiyun ssize_t sys_write(int fd, const void *buf, size_t count)
1778*4882a593Smuzhiyun {
1779*4882a593Smuzhiyun return my_syscall3(__NR_write, fd, buf, count);
1780*4882a593Smuzhiyun }
1781*4882a593Smuzhiyun
1782*4882a593Smuzhiyun
1783*4882a593Smuzhiyun /* Below are the libc-compatible syscalls which return x or -1 and set errno.
1784*4882a593Smuzhiyun * They rely on the functions above. Similarly they're marked static so that it
1785*4882a593Smuzhiyun * is possible to assign pointers to them if needed.
1786*4882a593Smuzhiyun */
1787*4882a593Smuzhiyun
1788*4882a593Smuzhiyun static __attribute__((unused))
brk(void * addr)1789*4882a593Smuzhiyun int brk(void *addr)
1790*4882a593Smuzhiyun {
1791*4882a593Smuzhiyun void *ret = sys_brk(addr);
1792*4882a593Smuzhiyun
1793*4882a593Smuzhiyun if (!ret) {
1794*4882a593Smuzhiyun SET_ERRNO(ENOMEM);
1795*4882a593Smuzhiyun return -1;
1796*4882a593Smuzhiyun }
1797*4882a593Smuzhiyun return 0;
1798*4882a593Smuzhiyun }
1799*4882a593Smuzhiyun
1800*4882a593Smuzhiyun static __attribute__((noreturn,unused))
exit(int status)1801*4882a593Smuzhiyun void exit(int status)
1802*4882a593Smuzhiyun {
1803*4882a593Smuzhiyun sys_exit(status);
1804*4882a593Smuzhiyun }
1805*4882a593Smuzhiyun
1806*4882a593Smuzhiyun static __attribute__((unused))
chdir(const char * path)1807*4882a593Smuzhiyun int chdir(const char *path)
1808*4882a593Smuzhiyun {
1809*4882a593Smuzhiyun int ret = sys_chdir(path);
1810*4882a593Smuzhiyun
1811*4882a593Smuzhiyun if (ret < 0) {
1812*4882a593Smuzhiyun SET_ERRNO(-ret);
1813*4882a593Smuzhiyun ret = -1;
1814*4882a593Smuzhiyun }
1815*4882a593Smuzhiyun return ret;
1816*4882a593Smuzhiyun }
1817*4882a593Smuzhiyun
1818*4882a593Smuzhiyun static __attribute__((unused))
chmod(const char * path,mode_t mode)1819*4882a593Smuzhiyun int chmod(const char *path, mode_t mode)
1820*4882a593Smuzhiyun {
1821*4882a593Smuzhiyun int ret = sys_chmod(path, mode);
1822*4882a593Smuzhiyun
1823*4882a593Smuzhiyun if (ret < 0) {
1824*4882a593Smuzhiyun SET_ERRNO(-ret);
1825*4882a593Smuzhiyun ret = -1;
1826*4882a593Smuzhiyun }
1827*4882a593Smuzhiyun return ret;
1828*4882a593Smuzhiyun }
1829*4882a593Smuzhiyun
1830*4882a593Smuzhiyun static __attribute__((unused))
chown(const char * path,uid_t owner,gid_t group)1831*4882a593Smuzhiyun int chown(const char *path, uid_t owner, gid_t group)
1832*4882a593Smuzhiyun {
1833*4882a593Smuzhiyun int ret = sys_chown(path, owner, group);
1834*4882a593Smuzhiyun
1835*4882a593Smuzhiyun if (ret < 0) {
1836*4882a593Smuzhiyun SET_ERRNO(-ret);
1837*4882a593Smuzhiyun ret = -1;
1838*4882a593Smuzhiyun }
1839*4882a593Smuzhiyun return ret;
1840*4882a593Smuzhiyun }
1841*4882a593Smuzhiyun
1842*4882a593Smuzhiyun static __attribute__((unused))
chroot(const char * path)1843*4882a593Smuzhiyun int chroot(const char *path)
1844*4882a593Smuzhiyun {
1845*4882a593Smuzhiyun int ret = sys_chroot(path);
1846*4882a593Smuzhiyun
1847*4882a593Smuzhiyun if (ret < 0) {
1848*4882a593Smuzhiyun SET_ERRNO(-ret);
1849*4882a593Smuzhiyun ret = -1;
1850*4882a593Smuzhiyun }
1851*4882a593Smuzhiyun return ret;
1852*4882a593Smuzhiyun }
1853*4882a593Smuzhiyun
1854*4882a593Smuzhiyun static __attribute__((unused))
close(int fd)1855*4882a593Smuzhiyun int close(int fd)
1856*4882a593Smuzhiyun {
1857*4882a593Smuzhiyun int ret = sys_close(fd);
1858*4882a593Smuzhiyun
1859*4882a593Smuzhiyun if (ret < 0) {
1860*4882a593Smuzhiyun SET_ERRNO(-ret);
1861*4882a593Smuzhiyun ret = -1;
1862*4882a593Smuzhiyun }
1863*4882a593Smuzhiyun return ret;
1864*4882a593Smuzhiyun }
1865*4882a593Smuzhiyun
1866*4882a593Smuzhiyun static __attribute__((unused))
dup2(int old,int new)1867*4882a593Smuzhiyun int dup2(int old, int new)
1868*4882a593Smuzhiyun {
1869*4882a593Smuzhiyun int ret = sys_dup2(old, new);
1870*4882a593Smuzhiyun
1871*4882a593Smuzhiyun if (ret < 0) {
1872*4882a593Smuzhiyun SET_ERRNO(-ret);
1873*4882a593Smuzhiyun ret = -1;
1874*4882a593Smuzhiyun }
1875*4882a593Smuzhiyun return ret;
1876*4882a593Smuzhiyun }
1877*4882a593Smuzhiyun
1878*4882a593Smuzhiyun static __attribute__((unused))
execve(const char * filename,char * const argv[],char * const envp[])1879*4882a593Smuzhiyun int execve(const char *filename, char *const argv[], char *const envp[])
1880*4882a593Smuzhiyun {
1881*4882a593Smuzhiyun int ret = sys_execve(filename, argv, envp);
1882*4882a593Smuzhiyun
1883*4882a593Smuzhiyun if (ret < 0) {
1884*4882a593Smuzhiyun SET_ERRNO(-ret);
1885*4882a593Smuzhiyun ret = -1;
1886*4882a593Smuzhiyun }
1887*4882a593Smuzhiyun return ret;
1888*4882a593Smuzhiyun }
1889*4882a593Smuzhiyun
1890*4882a593Smuzhiyun static __attribute__((unused))
fork(void)1891*4882a593Smuzhiyun pid_t fork(void)
1892*4882a593Smuzhiyun {
1893*4882a593Smuzhiyun pid_t ret = sys_fork();
1894*4882a593Smuzhiyun
1895*4882a593Smuzhiyun if (ret < 0) {
1896*4882a593Smuzhiyun SET_ERRNO(-ret);
1897*4882a593Smuzhiyun ret = -1;
1898*4882a593Smuzhiyun }
1899*4882a593Smuzhiyun return ret;
1900*4882a593Smuzhiyun }
1901*4882a593Smuzhiyun
1902*4882a593Smuzhiyun static __attribute__((unused))
fsync(int fd)1903*4882a593Smuzhiyun int fsync(int fd)
1904*4882a593Smuzhiyun {
1905*4882a593Smuzhiyun int ret = sys_fsync(fd);
1906*4882a593Smuzhiyun
1907*4882a593Smuzhiyun if (ret < 0) {
1908*4882a593Smuzhiyun SET_ERRNO(-ret);
1909*4882a593Smuzhiyun ret = -1;
1910*4882a593Smuzhiyun }
1911*4882a593Smuzhiyun return ret;
1912*4882a593Smuzhiyun }
1913*4882a593Smuzhiyun
1914*4882a593Smuzhiyun static __attribute__((unused))
getdents64(int fd,struct linux_dirent64 * dirp,int count)1915*4882a593Smuzhiyun int getdents64(int fd, struct linux_dirent64 *dirp, int count)
1916*4882a593Smuzhiyun {
1917*4882a593Smuzhiyun int ret = sys_getdents64(fd, dirp, count);
1918*4882a593Smuzhiyun
1919*4882a593Smuzhiyun if (ret < 0) {
1920*4882a593Smuzhiyun SET_ERRNO(-ret);
1921*4882a593Smuzhiyun ret = -1;
1922*4882a593Smuzhiyun }
1923*4882a593Smuzhiyun return ret;
1924*4882a593Smuzhiyun }
1925*4882a593Smuzhiyun
1926*4882a593Smuzhiyun static __attribute__((unused))
getpgrp(void)1927*4882a593Smuzhiyun pid_t getpgrp(void)
1928*4882a593Smuzhiyun {
1929*4882a593Smuzhiyun pid_t ret = sys_getpgrp();
1930*4882a593Smuzhiyun
1931*4882a593Smuzhiyun if (ret < 0) {
1932*4882a593Smuzhiyun SET_ERRNO(-ret);
1933*4882a593Smuzhiyun ret = -1;
1934*4882a593Smuzhiyun }
1935*4882a593Smuzhiyun return ret;
1936*4882a593Smuzhiyun }
1937*4882a593Smuzhiyun
1938*4882a593Smuzhiyun static __attribute__((unused))
getpid(void)1939*4882a593Smuzhiyun pid_t getpid(void)
1940*4882a593Smuzhiyun {
1941*4882a593Smuzhiyun pid_t ret = sys_getpid();
1942*4882a593Smuzhiyun
1943*4882a593Smuzhiyun if (ret < 0) {
1944*4882a593Smuzhiyun SET_ERRNO(-ret);
1945*4882a593Smuzhiyun ret = -1;
1946*4882a593Smuzhiyun }
1947*4882a593Smuzhiyun return ret;
1948*4882a593Smuzhiyun }
1949*4882a593Smuzhiyun
1950*4882a593Smuzhiyun static __attribute__((unused))
gettimeofday(struct timeval * tv,struct timezone * tz)1951*4882a593Smuzhiyun int gettimeofday(struct timeval *tv, struct timezone *tz)
1952*4882a593Smuzhiyun {
1953*4882a593Smuzhiyun int ret = sys_gettimeofday(tv, tz);
1954*4882a593Smuzhiyun
1955*4882a593Smuzhiyun if (ret < 0) {
1956*4882a593Smuzhiyun SET_ERRNO(-ret);
1957*4882a593Smuzhiyun ret = -1;
1958*4882a593Smuzhiyun }
1959*4882a593Smuzhiyun return ret;
1960*4882a593Smuzhiyun }
1961*4882a593Smuzhiyun
1962*4882a593Smuzhiyun static __attribute__((unused))
ioctl(int fd,unsigned long req,void * value)1963*4882a593Smuzhiyun int ioctl(int fd, unsigned long req, void *value)
1964*4882a593Smuzhiyun {
1965*4882a593Smuzhiyun int ret = sys_ioctl(fd, req, value);
1966*4882a593Smuzhiyun
1967*4882a593Smuzhiyun if (ret < 0) {
1968*4882a593Smuzhiyun SET_ERRNO(-ret);
1969*4882a593Smuzhiyun ret = -1;
1970*4882a593Smuzhiyun }
1971*4882a593Smuzhiyun return ret;
1972*4882a593Smuzhiyun }
1973*4882a593Smuzhiyun
1974*4882a593Smuzhiyun static __attribute__((unused))
kill(pid_t pid,int signal)1975*4882a593Smuzhiyun int kill(pid_t pid, int signal)
1976*4882a593Smuzhiyun {
1977*4882a593Smuzhiyun int ret = sys_kill(pid, signal);
1978*4882a593Smuzhiyun
1979*4882a593Smuzhiyun if (ret < 0) {
1980*4882a593Smuzhiyun SET_ERRNO(-ret);
1981*4882a593Smuzhiyun ret = -1;
1982*4882a593Smuzhiyun }
1983*4882a593Smuzhiyun return ret;
1984*4882a593Smuzhiyun }
1985*4882a593Smuzhiyun
1986*4882a593Smuzhiyun static __attribute__((unused))
link(const char * old,const char * new)1987*4882a593Smuzhiyun int link(const char *old, const char *new)
1988*4882a593Smuzhiyun {
1989*4882a593Smuzhiyun int ret = sys_link(old, new);
1990*4882a593Smuzhiyun
1991*4882a593Smuzhiyun if (ret < 0) {
1992*4882a593Smuzhiyun SET_ERRNO(-ret);
1993*4882a593Smuzhiyun ret = -1;
1994*4882a593Smuzhiyun }
1995*4882a593Smuzhiyun return ret;
1996*4882a593Smuzhiyun }
1997*4882a593Smuzhiyun
1998*4882a593Smuzhiyun static __attribute__((unused))
lseek(int fd,off_t offset,int whence)1999*4882a593Smuzhiyun off_t lseek(int fd, off_t offset, int whence)
2000*4882a593Smuzhiyun {
2001*4882a593Smuzhiyun off_t ret = sys_lseek(fd, offset, whence);
2002*4882a593Smuzhiyun
2003*4882a593Smuzhiyun if (ret < 0) {
2004*4882a593Smuzhiyun SET_ERRNO(-ret);
2005*4882a593Smuzhiyun ret = -1;
2006*4882a593Smuzhiyun }
2007*4882a593Smuzhiyun return ret;
2008*4882a593Smuzhiyun }
2009*4882a593Smuzhiyun
2010*4882a593Smuzhiyun static __attribute__((unused))
mkdir(const char * path,mode_t mode)2011*4882a593Smuzhiyun int mkdir(const char *path, mode_t mode)
2012*4882a593Smuzhiyun {
2013*4882a593Smuzhiyun int ret = sys_mkdir(path, mode);
2014*4882a593Smuzhiyun
2015*4882a593Smuzhiyun if (ret < 0) {
2016*4882a593Smuzhiyun SET_ERRNO(-ret);
2017*4882a593Smuzhiyun ret = -1;
2018*4882a593Smuzhiyun }
2019*4882a593Smuzhiyun return ret;
2020*4882a593Smuzhiyun }
2021*4882a593Smuzhiyun
2022*4882a593Smuzhiyun static __attribute__((unused))
mknod(const char * path,mode_t mode,dev_t dev)2023*4882a593Smuzhiyun int mknod(const char *path, mode_t mode, dev_t dev)
2024*4882a593Smuzhiyun {
2025*4882a593Smuzhiyun int ret = sys_mknod(path, mode, dev);
2026*4882a593Smuzhiyun
2027*4882a593Smuzhiyun if (ret < 0) {
2028*4882a593Smuzhiyun SET_ERRNO(-ret);
2029*4882a593Smuzhiyun ret = -1;
2030*4882a593Smuzhiyun }
2031*4882a593Smuzhiyun return ret;
2032*4882a593Smuzhiyun }
2033*4882a593Smuzhiyun
2034*4882a593Smuzhiyun static __attribute__((unused))
mount(const char * src,const char * tgt,const char * fst,unsigned long flags,const void * data)2035*4882a593Smuzhiyun int mount(const char *src, const char *tgt,
2036*4882a593Smuzhiyun const char *fst, unsigned long flags,
2037*4882a593Smuzhiyun const void *data)
2038*4882a593Smuzhiyun {
2039*4882a593Smuzhiyun int ret = sys_mount(src, tgt, fst, flags, data);
2040*4882a593Smuzhiyun
2041*4882a593Smuzhiyun if (ret < 0) {
2042*4882a593Smuzhiyun SET_ERRNO(-ret);
2043*4882a593Smuzhiyun ret = -1;
2044*4882a593Smuzhiyun }
2045*4882a593Smuzhiyun return ret;
2046*4882a593Smuzhiyun }
2047*4882a593Smuzhiyun
2048*4882a593Smuzhiyun static __attribute__((unused))
open(const char * path,int flags,mode_t mode)2049*4882a593Smuzhiyun int open(const char *path, int flags, mode_t mode)
2050*4882a593Smuzhiyun {
2051*4882a593Smuzhiyun int ret = sys_open(path, flags, mode);
2052*4882a593Smuzhiyun
2053*4882a593Smuzhiyun if (ret < 0) {
2054*4882a593Smuzhiyun SET_ERRNO(-ret);
2055*4882a593Smuzhiyun ret = -1;
2056*4882a593Smuzhiyun }
2057*4882a593Smuzhiyun return ret;
2058*4882a593Smuzhiyun }
2059*4882a593Smuzhiyun
2060*4882a593Smuzhiyun static __attribute__((unused))
pivot_root(const char * new,const char * old)2061*4882a593Smuzhiyun int pivot_root(const char *new, const char *old)
2062*4882a593Smuzhiyun {
2063*4882a593Smuzhiyun int ret = sys_pivot_root(new, old);
2064*4882a593Smuzhiyun
2065*4882a593Smuzhiyun if (ret < 0) {
2066*4882a593Smuzhiyun SET_ERRNO(-ret);
2067*4882a593Smuzhiyun ret = -1;
2068*4882a593Smuzhiyun }
2069*4882a593Smuzhiyun return ret;
2070*4882a593Smuzhiyun }
2071*4882a593Smuzhiyun
2072*4882a593Smuzhiyun static __attribute__((unused))
poll(struct pollfd * fds,int nfds,int timeout)2073*4882a593Smuzhiyun int poll(struct pollfd *fds, int nfds, int timeout)
2074*4882a593Smuzhiyun {
2075*4882a593Smuzhiyun int ret = sys_poll(fds, nfds, timeout);
2076*4882a593Smuzhiyun
2077*4882a593Smuzhiyun if (ret < 0) {
2078*4882a593Smuzhiyun SET_ERRNO(-ret);
2079*4882a593Smuzhiyun ret = -1;
2080*4882a593Smuzhiyun }
2081*4882a593Smuzhiyun return ret;
2082*4882a593Smuzhiyun }
2083*4882a593Smuzhiyun
2084*4882a593Smuzhiyun static __attribute__((unused))
read(int fd,void * buf,size_t count)2085*4882a593Smuzhiyun ssize_t read(int fd, void *buf, size_t count)
2086*4882a593Smuzhiyun {
2087*4882a593Smuzhiyun ssize_t ret = sys_read(fd, buf, count);
2088*4882a593Smuzhiyun
2089*4882a593Smuzhiyun if (ret < 0) {
2090*4882a593Smuzhiyun SET_ERRNO(-ret);
2091*4882a593Smuzhiyun ret = -1;
2092*4882a593Smuzhiyun }
2093*4882a593Smuzhiyun return ret;
2094*4882a593Smuzhiyun }
2095*4882a593Smuzhiyun
2096*4882a593Smuzhiyun static __attribute__((unused))
reboot(int cmd)2097*4882a593Smuzhiyun int reboot(int cmd)
2098*4882a593Smuzhiyun {
2099*4882a593Smuzhiyun int ret = sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0);
2100*4882a593Smuzhiyun
2101*4882a593Smuzhiyun if (ret < 0) {
2102*4882a593Smuzhiyun SET_ERRNO(-ret);
2103*4882a593Smuzhiyun ret = -1;
2104*4882a593Smuzhiyun }
2105*4882a593Smuzhiyun return ret;
2106*4882a593Smuzhiyun }
2107*4882a593Smuzhiyun
2108*4882a593Smuzhiyun static __attribute__((unused))
sbrk(intptr_t inc)2109*4882a593Smuzhiyun void *sbrk(intptr_t inc)
2110*4882a593Smuzhiyun {
2111*4882a593Smuzhiyun void *ret;
2112*4882a593Smuzhiyun
2113*4882a593Smuzhiyun /* first call to find current end */
2114*4882a593Smuzhiyun if ((ret = sys_brk(0)) && (sys_brk(ret + inc) == ret + inc))
2115*4882a593Smuzhiyun return ret + inc;
2116*4882a593Smuzhiyun
2117*4882a593Smuzhiyun SET_ERRNO(ENOMEM);
2118*4882a593Smuzhiyun return (void *)-1;
2119*4882a593Smuzhiyun }
2120*4882a593Smuzhiyun
2121*4882a593Smuzhiyun static __attribute__((unused))
sched_yield(void)2122*4882a593Smuzhiyun int sched_yield(void)
2123*4882a593Smuzhiyun {
2124*4882a593Smuzhiyun int ret = sys_sched_yield();
2125*4882a593Smuzhiyun
2126*4882a593Smuzhiyun if (ret < 0) {
2127*4882a593Smuzhiyun SET_ERRNO(-ret);
2128*4882a593Smuzhiyun ret = -1;
2129*4882a593Smuzhiyun }
2130*4882a593Smuzhiyun return ret;
2131*4882a593Smuzhiyun }
2132*4882a593Smuzhiyun
2133*4882a593Smuzhiyun static __attribute__((unused))
select(int nfds,fd_set * rfds,fd_set * wfds,fd_set * efds,struct timeval * timeout)2134*4882a593Smuzhiyun int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
2135*4882a593Smuzhiyun {
2136*4882a593Smuzhiyun int ret = sys_select(nfds, rfds, wfds, efds, timeout);
2137*4882a593Smuzhiyun
2138*4882a593Smuzhiyun if (ret < 0) {
2139*4882a593Smuzhiyun SET_ERRNO(-ret);
2140*4882a593Smuzhiyun ret = -1;
2141*4882a593Smuzhiyun }
2142*4882a593Smuzhiyun return ret;
2143*4882a593Smuzhiyun }
2144*4882a593Smuzhiyun
2145*4882a593Smuzhiyun static __attribute__((unused))
setpgid(pid_t pid,pid_t pgid)2146*4882a593Smuzhiyun int setpgid(pid_t pid, pid_t pgid)
2147*4882a593Smuzhiyun {
2148*4882a593Smuzhiyun int ret = sys_setpgid(pid, pgid);
2149*4882a593Smuzhiyun
2150*4882a593Smuzhiyun if (ret < 0) {
2151*4882a593Smuzhiyun SET_ERRNO(-ret);
2152*4882a593Smuzhiyun ret = -1;
2153*4882a593Smuzhiyun }
2154*4882a593Smuzhiyun return ret;
2155*4882a593Smuzhiyun }
2156*4882a593Smuzhiyun
2157*4882a593Smuzhiyun static __attribute__((unused))
setsid(void)2158*4882a593Smuzhiyun pid_t setsid(void)
2159*4882a593Smuzhiyun {
2160*4882a593Smuzhiyun pid_t ret = sys_setsid();
2161*4882a593Smuzhiyun
2162*4882a593Smuzhiyun if (ret < 0) {
2163*4882a593Smuzhiyun SET_ERRNO(-ret);
2164*4882a593Smuzhiyun ret = -1;
2165*4882a593Smuzhiyun }
2166*4882a593Smuzhiyun return ret;
2167*4882a593Smuzhiyun }
2168*4882a593Smuzhiyun
2169*4882a593Smuzhiyun static __attribute__((unused))
sleep(unsigned int seconds)2170*4882a593Smuzhiyun unsigned int sleep(unsigned int seconds)
2171*4882a593Smuzhiyun {
2172*4882a593Smuzhiyun struct timeval my_timeval = { seconds, 0 };
2173*4882a593Smuzhiyun
2174*4882a593Smuzhiyun if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
2175*4882a593Smuzhiyun return my_timeval.tv_sec + !!my_timeval.tv_usec;
2176*4882a593Smuzhiyun else
2177*4882a593Smuzhiyun return 0;
2178*4882a593Smuzhiyun }
2179*4882a593Smuzhiyun
2180*4882a593Smuzhiyun static __attribute__((unused))
stat(const char * path,struct stat * buf)2181*4882a593Smuzhiyun int stat(const char *path, struct stat *buf)
2182*4882a593Smuzhiyun {
2183*4882a593Smuzhiyun int ret = sys_stat(path, buf);
2184*4882a593Smuzhiyun
2185*4882a593Smuzhiyun if (ret < 0) {
2186*4882a593Smuzhiyun SET_ERRNO(-ret);
2187*4882a593Smuzhiyun ret = -1;
2188*4882a593Smuzhiyun }
2189*4882a593Smuzhiyun return ret;
2190*4882a593Smuzhiyun }
2191*4882a593Smuzhiyun
2192*4882a593Smuzhiyun static __attribute__((unused))
symlink(const char * old,const char * new)2193*4882a593Smuzhiyun int symlink(const char *old, const char *new)
2194*4882a593Smuzhiyun {
2195*4882a593Smuzhiyun int ret = sys_symlink(old, new);
2196*4882a593Smuzhiyun
2197*4882a593Smuzhiyun if (ret < 0) {
2198*4882a593Smuzhiyun SET_ERRNO(-ret);
2199*4882a593Smuzhiyun ret = -1;
2200*4882a593Smuzhiyun }
2201*4882a593Smuzhiyun return ret;
2202*4882a593Smuzhiyun }
2203*4882a593Smuzhiyun
2204*4882a593Smuzhiyun static __attribute__((unused))
tcsetpgrp(int fd,pid_t pid)2205*4882a593Smuzhiyun int tcsetpgrp(int fd, pid_t pid)
2206*4882a593Smuzhiyun {
2207*4882a593Smuzhiyun return ioctl(fd, TIOCSPGRP, &pid);
2208*4882a593Smuzhiyun }
2209*4882a593Smuzhiyun
2210*4882a593Smuzhiyun static __attribute__((unused))
umask(mode_t mode)2211*4882a593Smuzhiyun mode_t umask(mode_t mode)
2212*4882a593Smuzhiyun {
2213*4882a593Smuzhiyun return sys_umask(mode);
2214*4882a593Smuzhiyun }
2215*4882a593Smuzhiyun
2216*4882a593Smuzhiyun static __attribute__((unused))
umount2(const char * path,int flags)2217*4882a593Smuzhiyun int umount2(const char *path, int flags)
2218*4882a593Smuzhiyun {
2219*4882a593Smuzhiyun int ret = sys_umount2(path, flags);
2220*4882a593Smuzhiyun
2221*4882a593Smuzhiyun if (ret < 0) {
2222*4882a593Smuzhiyun SET_ERRNO(-ret);
2223*4882a593Smuzhiyun ret = -1;
2224*4882a593Smuzhiyun }
2225*4882a593Smuzhiyun return ret;
2226*4882a593Smuzhiyun }
2227*4882a593Smuzhiyun
2228*4882a593Smuzhiyun static __attribute__((unused))
unlink(const char * path)2229*4882a593Smuzhiyun int unlink(const char *path)
2230*4882a593Smuzhiyun {
2231*4882a593Smuzhiyun int ret = sys_unlink(path);
2232*4882a593Smuzhiyun
2233*4882a593Smuzhiyun if (ret < 0) {
2234*4882a593Smuzhiyun SET_ERRNO(-ret);
2235*4882a593Smuzhiyun ret = -1;
2236*4882a593Smuzhiyun }
2237*4882a593Smuzhiyun return ret;
2238*4882a593Smuzhiyun }
2239*4882a593Smuzhiyun
2240*4882a593Smuzhiyun static __attribute__((unused))
wait4(pid_t pid,int * status,int options,struct rusage * rusage)2241*4882a593Smuzhiyun pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage)
2242*4882a593Smuzhiyun {
2243*4882a593Smuzhiyun pid_t ret = sys_wait4(pid, status, options, rusage);
2244*4882a593Smuzhiyun
2245*4882a593Smuzhiyun if (ret < 0) {
2246*4882a593Smuzhiyun SET_ERRNO(-ret);
2247*4882a593Smuzhiyun ret = -1;
2248*4882a593Smuzhiyun }
2249*4882a593Smuzhiyun return ret;
2250*4882a593Smuzhiyun }
2251*4882a593Smuzhiyun
2252*4882a593Smuzhiyun static __attribute__((unused))
waitpid(pid_t pid,int * status,int options)2253*4882a593Smuzhiyun pid_t waitpid(pid_t pid, int *status, int options)
2254*4882a593Smuzhiyun {
2255*4882a593Smuzhiyun pid_t ret = sys_waitpid(pid, status, options);
2256*4882a593Smuzhiyun
2257*4882a593Smuzhiyun if (ret < 0) {
2258*4882a593Smuzhiyun SET_ERRNO(-ret);
2259*4882a593Smuzhiyun ret = -1;
2260*4882a593Smuzhiyun }
2261*4882a593Smuzhiyun return ret;
2262*4882a593Smuzhiyun }
2263*4882a593Smuzhiyun
2264*4882a593Smuzhiyun static __attribute__((unused))
wait(int * status)2265*4882a593Smuzhiyun pid_t wait(int *status)
2266*4882a593Smuzhiyun {
2267*4882a593Smuzhiyun pid_t ret = sys_wait(status);
2268*4882a593Smuzhiyun
2269*4882a593Smuzhiyun if (ret < 0) {
2270*4882a593Smuzhiyun SET_ERRNO(-ret);
2271*4882a593Smuzhiyun ret = -1;
2272*4882a593Smuzhiyun }
2273*4882a593Smuzhiyun return ret;
2274*4882a593Smuzhiyun }
2275*4882a593Smuzhiyun
2276*4882a593Smuzhiyun static __attribute__((unused))
write(int fd,const void * buf,size_t count)2277*4882a593Smuzhiyun ssize_t write(int fd, const void *buf, size_t count)
2278*4882a593Smuzhiyun {
2279*4882a593Smuzhiyun ssize_t ret = sys_write(fd, buf, count);
2280*4882a593Smuzhiyun
2281*4882a593Smuzhiyun if (ret < 0) {
2282*4882a593Smuzhiyun SET_ERRNO(-ret);
2283*4882a593Smuzhiyun ret = -1;
2284*4882a593Smuzhiyun }
2285*4882a593Smuzhiyun return ret;
2286*4882a593Smuzhiyun }
2287*4882a593Smuzhiyun
2288*4882a593Smuzhiyun /* some size-optimized reimplementations of a few common str* and mem*
2289*4882a593Smuzhiyun * functions. They're marked static, except memcpy() and raise() which are used
2290*4882a593Smuzhiyun * by libgcc on ARM, so they are marked weak instead in order not to cause an
2291*4882a593Smuzhiyun * error when building a program made of multiple files (not recommended).
2292*4882a593Smuzhiyun */
2293*4882a593Smuzhiyun
2294*4882a593Smuzhiyun static __attribute__((unused))
memmove(void * dst,const void * src,size_t len)2295*4882a593Smuzhiyun void *memmove(void *dst, const void *src, size_t len)
2296*4882a593Smuzhiyun {
2297*4882a593Smuzhiyun ssize_t pos = (dst <= src) ? -1 : (long)len;
2298*4882a593Smuzhiyun void *ret = dst;
2299*4882a593Smuzhiyun
2300*4882a593Smuzhiyun while (len--) {
2301*4882a593Smuzhiyun pos += (dst <= src) ? 1 : -1;
2302*4882a593Smuzhiyun ((char *)dst)[pos] = ((char *)src)[pos];
2303*4882a593Smuzhiyun }
2304*4882a593Smuzhiyun return ret;
2305*4882a593Smuzhiyun }
2306*4882a593Smuzhiyun
2307*4882a593Smuzhiyun static __attribute__((unused))
memset(void * dst,int b,size_t len)2308*4882a593Smuzhiyun void *memset(void *dst, int b, size_t len)
2309*4882a593Smuzhiyun {
2310*4882a593Smuzhiyun char *p = dst;
2311*4882a593Smuzhiyun
2312*4882a593Smuzhiyun while (len--)
2313*4882a593Smuzhiyun *(p++) = b;
2314*4882a593Smuzhiyun return dst;
2315*4882a593Smuzhiyun }
2316*4882a593Smuzhiyun
2317*4882a593Smuzhiyun static __attribute__((unused))
memcmp(const void * s1,const void * s2,size_t n)2318*4882a593Smuzhiyun int memcmp(const void *s1, const void *s2, size_t n)
2319*4882a593Smuzhiyun {
2320*4882a593Smuzhiyun size_t ofs = 0;
2321*4882a593Smuzhiyun int c1 = 0;
2322*4882a593Smuzhiyun
2323*4882a593Smuzhiyun while (ofs < n && !(c1 = ((unsigned char *)s1)[ofs] - ((unsigned char *)s2)[ofs])) {
2324*4882a593Smuzhiyun ofs++;
2325*4882a593Smuzhiyun }
2326*4882a593Smuzhiyun return c1;
2327*4882a593Smuzhiyun }
2328*4882a593Smuzhiyun
2329*4882a593Smuzhiyun static __attribute__((unused))
strcpy(char * dst,const char * src)2330*4882a593Smuzhiyun char *strcpy(char *dst, const char *src)
2331*4882a593Smuzhiyun {
2332*4882a593Smuzhiyun char *ret = dst;
2333*4882a593Smuzhiyun
2334*4882a593Smuzhiyun while ((*dst++ = *src++));
2335*4882a593Smuzhiyun return ret;
2336*4882a593Smuzhiyun }
2337*4882a593Smuzhiyun
2338*4882a593Smuzhiyun static __attribute__((unused))
strchr(const char * s,int c)2339*4882a593Smuzhiyun char *strchr(const char *s, int c)
2340*4882a593Smuzhiyun {
2341*4882a593Smuzhiyun while (*s) {
2342*4882a593Smuzhiyun if (*s == (char)c)
2343*4882a593Smuzhiyun return (char *)s;
2344*4882a593Smuzhiyun s++;
2345*4882a593Smuzhiyun }
2346*4882a593Smuzhiyun return NULL;
2347*4882a593Smuzhiyun }
2348*4882a593Smuzhiyun
2349*4882a593Smuzhiyun static __attribute__((unused))
strrchr(const char * s,int c)2350*4882a593Smuzhiyun char *strrchr(const char *s, int c)
2351*4882a593Smuzhiyun {
2352*4882a593Smuzhiyun const char *ret = NULL;
2353*4882a593Smuzhiyun
2354*4882a593Smuzhiyun while (*s) {
2355*4882a593Smuzhiyun if (*s == (char)c)
2356*4882a593Smuzhiyun ret = s;
2357*4882a593Smuzhiyun s++;
2358*4882a593Smuzhiyun }
2359*4882a593Smuzhiyun return (char *)ret;
2360*4882a593Smuzhiyun }
2361*4882a593Smuzhiyun
2362*4882a593Smuzhiyun static __attribute__((unused))
nolibc_strlen(const char * str)2363*4882a593Smuzhiyun size_t nolibc_strlen(const char *str)
2364*4882a593Smuzhiyun {
2365*4882a593Smuzhiyun size_t len;
2366*4882a593Smuzhiyun
2367*4882a593Smuzhiyun for (len = 0; str[len]; len++);
2368*4882a593Smuzhiyun return len;
2369*4882a593Smuzhiyun }
2370*4882a593Smuzhiyun
2371*4882a593Smuzhiyun #define strlen(str) ({ \
2372*4882a593Smuzhiyun __builtin_constant_p((str)) ? \
2373*4882a593Smuzhiyun __builtin_strlen((str)) : \
2374*4882a593Smuzhiyun nolibc_strlen((str)); \
2375*4882a593Smuzhiyun })
2376*4882a593Smuzhiyun
2377*4882a593Smuzhiyun static __attribute__((unused))
isdigit(int c)2378*4882a593Smuzhiyun int isdigit(int c)
2379*4882a593Smuzhiyun {
2380*4882a593Smuzhiyun return (unsigned int)(c - '0') <= 9;
2381*4882a593Smuzhiyun }
2382*4882a593Smuzhiyun
2383*4882a593Smuzhiyun static __attribute__((unused))
atol(const char * s)2384*4882a593Smuzhiyun long atol(const char *s)
2385*4882a593Smuzhiyun {
2386*4882a593Smuzhiyun unsigned long ret = 0;
2387*4882a593Smuzhiyun unsigned long d;
2388*4882a593Smuzhiyun int neg = 0;
2389*4882a593Smuzhiyun
2390*4882a593Smuzhiyun if (*s == '-') {
2391*4882a593Smuzhiyun neg = 1;
2392*4882a593Smuzhiyun s++;
2393*4882a593Smuzhiyun }
2394*4882a593Smuzhiyun
2395*4882a593Smuzhiyun while (1) {
2396*4882a593Smuzhiyun d = (*s++) - '0';
2397*4882a593Smuzhiyun if (d > 9)
2398*4882a593Smuzhiyun break;
2399*4882a593Smuzhiyun ret *= 10;
2400*4882a593Smuzhiyun ret += d;
2401*4882a593Smuzhiyun }
2402*4882a593Smuzhiyun
2403*4882a593Smuzhiyun return neg ? -ret : ret;
2404*4882a593Smuzhiyun }
2405*4882a593Smuzhiyun
2406*4882a593Smuzhiyun static __attribute__((unused))
atoi(const char * s)2407*4882a593Smuzhiyun int atoi(const char *s)
2408*4882a593Smuzhiyun {
2409*4882a593Smuzhiyun return atol(s);
2410*4882a593Smuzhiyun }
2411*4882a593Smuzhiyun
2412*4882a593Smuzhiyun static __attribute__((unused))
ltoa(long in)2413*4882a593Smuzhiyun const char *ltoa(long in)
2414*4882a593Smuzhiyun {
2415*4882a593Smuzhiyun /* large enough for -9223372036854775808 */
2416*4882a593Smuzhiyun static char buffer[21];
2417*4882a593Smuzhiyun char *pos = buffer + sizeof(buffer) - 1;
2418*4882a593Smuzhiyun int neg = in < 0;
2419*4882a593Smuzhiyun unsigned long n = neg ? -in : in;
2420*4882a593Smuzhiyun
2421*4882a593Smuzhiyun *pos-- = '\0';
2422*4882a593Smuzhiyun do {
2423*4882a593Smuzhiyun *pos-- = '0' + n % 10;
2424*4882a593Smuzhiyun n /= 10;
2425*4882a593Smuzhiyun if (pos < buffer)
2426*4882a593Smuzhiyun return pos + 1;
2427*4882a593Smuzhiyun } while (n);
2428*4882a593Smuzhiyun
2429*4882a593Smuzhiyun if (neg)
2430*4882a593Smuzhiyun *pos-- = '-';
2431*4882a593Smuzhiyun return pos + 1;
2432*4882a593Smuzhiyun }
2433*4882a593Smuzhiyun
2434*4882a593Smuzhiyun __attribute__((weak,unused))
memcpy(void * dst,const void * src,size_t len)2435*4882a593Smuzhiyun void *memcpy(void *dst, const void *src, size_t len)
2436*4882a593Smuzhiyun {
2437*4882a593Smuzhiyun return memmove(dst, src, len);
2438*4882a593Smuzhiyun }
2439*4882a593Smuzhiyun
2440*4882a593Smuzhiyun /* needed by libgcc for divide by zero */
2441*4882a593Smuzhiyun __attribute__((weak,unused))
raise(int signal)2442*4882a593Smuzhiyun int raise(int signal)
2443*4882a593Smuzhiyun {
2444*4882a593Smuzhiyun return kill(getpid(), signal);
2445*4882a593Smuzhiyun }
2446*4882a593Smuzhiyun
2447*4882a593Smuzhiyun /* Here come a few helper functions */
2448*4882a593Smuzhiyun
2449*4882a593Smuzhiyun static __attribute__((unused))
FD_ZERO(fd_set * set)2450*4882a593Smuzhiyun void FD_ZERO(fd_set *set)
2451*4882a593Smuzhiyun {
2452*4882a593Smuzhiyun memset(set, 0, sizeof(*set));
2453*4882a593Smuzhiyun }
2454*4882a593Smuzhiyun
2455*4882a593Smuzhiyun static __attribute__((unused))
FD_SET(int fd,fd_set * set)2456*4882a593Smuzhiyun void FD_SET(int fd, fd_set *set)
2457*4882a593Smuzhiyun {
2458*4882a593Smuzhiyun if (fd < 0 || fd >= FD_SETSIZE)
2459*4882a593Smuzhiyun return;
2460*4882a593Smuzhiyun set->fd32[fd / 32] |= 1 << (fd & 31);
2461*4882a593Smuzhiyun }
2462*4882a593Smuzhiyun
2463*4882a593Smuzhiyun /* WARNING, it only deals with the 4096 first majors and 256 first minors */
2464*4882a593Smuzhiyun static __attribute__((unused))
makedev(unsigned int major,unsigned int minor)2465*4882a593Smuzhiyun dev_t makedev(unsigned int major, unsigned int minor)
2466*4882a593Smuzhiyun {
2467*4882a593Smuzhiyun return ((major & 0xfff) << 8) | (minor & 0xff);
2468*4882a593Smuzhiyun }
2469