130ca2051SWilly Tarreau /* SPDX-License-Identifier: LGPL-2.1 OR MIT */ 230ca2051SWilly Tarreau /* nolibc.h 330ca2051SWilly Tarreau * Copyright (C) 2017-2018 Willy Tarreau <w@1wt.eu> 430ca2051SWilly Tarreau */ 530ca2051SWilly Tarreau 630ca2051SWilly Tarreau /* 730ca2051SWilly Tarreau * This file is designed to be used as a libc alternative for minimal programs 830ca2051SWilly Tarreau * with very limited requirements. It consists of a small number of syscall and 930ca2051SWilly Tarreau * type definitions, and the minimal startup code needed to call main(). 1030ca2051SWilly Tarreau * All syscalls are declared as static functions so that they can be optimized 1130ca2051SWilly Tarreau * away by the compiler when not used. 1230ca2051SWilly Tarreau * 1330ca2051SWilly Tarreau * Syscalls are split into 3 levels: 1430ca2051SWilly Tarreau * - The lower level is the arch-specific syscall() definition, consisting in 1530ca2051SWilly Tarreau * assembly code in compound expressions. These are called my_syscall0() to 1630ca2051SWilly Tarreau * my_syscall6() depending on the number of arguments. The MIPS 1730ca2051SWilly Tarreau * implementation is limited to 5 arguments. All input arguments are cast 1830ca2051SWilly Tarreau * to a long stored in a register. These expressions always return the 1930ca2051SWilly Tarreau * syscall's return value as a signed long value which is often either a 2030ca2051SWilly Tarreau * pointer or the negated errno value. 2130ca2051SWilly Tarreau * 2230ca2051SWilly Tarreau * - The second level is mostly architecture-independent. It is made of 2330ca2051SWilly Tarreau * static functions called sys_<name>() which rely on my_syscallN() 2430ca2051SWilly Tarreau * depending on the syscall definition. These functions are responsible 2530ca2051SWilly Tarreau * for exposing the appropriate types for the syscall arguments (int, 2630ca2051SWilly Tarreau * pointers, etc) and for setting the appropriate return type (often int). 2730ca2051SWilly Tarreau * A few of them are architecture-specific because the syscalls are not all 2830ca2051SWilly Tarreau * mapped exactly the same among architectures. For example, some archs do 2930ca2051SWilly Tarreau * not implement select() and need pselect6() instead, so the sys_select() 3030ca2051SWilly Tarreau * function will have to abstract this. 3130ca2051SWilly Tarreau * 3230ca2051SWilly Tarreau * - The third level is the libc call definition. It exposes the lower raw 3330ca2051SWilly Tarreau * sys_<name>() calls in a way that looks like what a libc usually does, 3430ca2051SWilly Tarreau * takes care of specific input values, and of setting errno upon error. 3530ca2051SWilly Tarreau * There can be minor variations compared to standard libc calls. For 3630ca2051SWilly Tarreau * example the open() call always takes 3 args here. 3730ca2051SWilly Tarreau * 3830ca2051SWilly Tarreau * The errno variable is declared static and unused. This way it can be 3930ca2051SWilly Tarreau * optimized away if not used. However this means that a program made of 4030ca2051SWilly Tarreau * multiple C files may observe different errno values (one per C file). For 4130ca2051SWilly Tarreau * the type of programs this project targets it usually is not a problem. The 4230ca2051SWilly Tarreau * resulting program may even be reduced by defining the NOLIBC_IGNORE_ERRNO 4330ca2051SWilly Tarreau * macro, in which case the errno value will never be assigned. 4430ca2051SWilly Tarreau * 4530ca2051SWilly Tarreau * Some stdint-like integer types are defined. These are valid on all currently 4630ca2051SWilly Tarreau * supported architectures, because signs are enforced, ints are assumed to be 4730ca2051SWilly Tarreau * 32 bits, longs the size of a pointer and long long 64 bits. If more 4830ca2051SWilly Tarreau * architectures have to be supported, this may need to be adapted. 4930ca2051SWilly Tarreau * 5030ca2051SWilly Tarreau * Some macro definitions like the O_* values passed to open(), and some 5130ca2051SWilly Tarreau * structures like the sys_stat struct depend on the architecture. 5230ca2051SWilly Tarreau * 5330ca2051SWilly Tarreau * The definitions start with the architecture-specific parts, which are picked 5430ca2051SWilly Tarreau * based on what the compiler knows about the target architecture, and are 5530ca2051SWilly Tarreau * completed with the generic code. Since it is the compiler which sets the 5630ca2051SWilly Tarreau * target architecture, cross-compiling normally works out of the box without 5730ca2051SWilly Tarreau * having to specify anything. 5830ca2051SWilly Tarreau * 5930ca2051SWilly Tarreau * Finally some very common libc-level functions are provided. It is the case 6030ca2051SWilly Tarreau * for a few functions usually found in string.h, ctype.h, or stdlib.h. Nothing 6130ca2051SWilly Tarreau * is currently provided regarding stdio emulation. 6230ca2051SWilly Tarreau * 6330ca2051SWilly Tarreau * The macro NOLIBC is always defined, so that it is possible for a program to 6430ca2051SWilly Tarreau * check this macro to know if it is being built against and decide to disable 6530ca2051SWilly Tarreau * some features or simply not to include some standard libc files. 6630ca2051SWilly Tarreau * 6730ca2051SWilly Tarreau * Ideally this file should be split in multiple files for easier long term 6830ca2051SWilly Tarreau * maintenance, but provided as a single file as it is now, it's quite 6930ca2051SWilly Tarreau * convenient to use. Maybe some variations involving a set of includes at the 7030ca2051SWilly Tarreau * top could work. 7130ca2051SWilly Tarreau * 7230ca2051SWilly Tarreau * A simple static executable may be built this way : 7330ca2051SWilly Tarreau * $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \ 7430ca2051SWilly Tarreau * -static -include nolibc.h -lgcc -o hello hello.c 7530ca2051SWilly Tarreau * 7630ca2051SWilly Tarreau * A very useful calling convention table may be found here : 7730ca2051SWilly Tarreau * http://man7.org/linux/man-pages/man2/syscall.2.html 7830ca2051SWilly Tarreau * 7930ca2051SWilly Tarreau * This doc is quite convenient though not necessarily up to date : 8030ca2051SWilly Tarreau * https://w3challs.com/syscalls/ 8130ca2051SWilly Tarreau * 8230ca2051SWilly Tarreau */ 8330ca2051SWilly Tarreau 8430ca2051SWilly Tarreau /* Some archs (at least aarch64) don't expose the regular syscalls anymore by 8530ca2051SWilly Tarreau * default, either because they have an "_at" replacement, or because there are 8630ca2051SWilly Tarreau * more modern alternatives. For now we'd rather still use them. 8730ca2051SWilly Tarreau */ 8830ca2051SWilly Tarreau #define __ARCH_WANT_SYSCALL_NO_AT 8930ca2051SWilly Tarreau #define __ARCH_WANT_SYSCALL_NO_FLAGS 9030ca2051SWilly Tarreau #define __ARCH_WANT_SYSCALL_DEPRECATED 9130ca2051SWilly Tarreau 9230ca2051SWilly Tarreau #include <asm/unistd.h> 9330ca2051SWilly Tarreau #include <asm/ioctls.h> 9430ca2051SWilly Tarreau #include <asm/errno.h> 9530ca2051SWilly Tarreau #include <linux/fs.h> 9630ca2051SWilly Tarreau #include <linux/loop.h> 9730ca2051SWilly Tarreau 9830ca2051SWilly Tarreau #define NOLIBC 9930ca2051SWilly Tarreau 10030ca2051SWilly Tarreau /* this way it will be removed if unused */ 10130ca2051SWilly Tarreau static int errno; 10230ca2051SWilly Tarreau 10330ca2051SWilly Tarreau #ifndef NOLIBC_IGNORE_ERRNO 10430ca2051SWilly Tarreau #define SET_ERRNO(v) do { errno = (v); } while (0) 10530ca2051SWilly Tarreau #else 10630ca2051SWilly Tarreau #define SET_ERRNO(v) do { } while (0) 10730ca2051SWilly Tarreau #endif 10830ca2051SWilly Tarreau 10930ca2051SWilly Tarreau /* errno codes all ensure that they will not conflict with a valid pointer 11030ca2051SWilly Tarreau * because they all correspond to the highest addressable memry page. 11130ca2051SWilly Tarreau */ 11230ca2051SWilly Tarreau #define MAX_ERRNO 4095 11330ca2051SWilly Tarreau 11430ca2051SWilly Tarreau /* Declare a few quite common macros and types that usually are in stdlib.h, 11530ca2051SWilly Tarreau * stdint.h, ctype.h, unistd.h and a few other common locations. 11630ca2051SWilly Tarreau */ 11730ca2051SWilly Tarreau 11830ca2051SWilly Tarreau #define NULL ((void *)0) 11930ca2051SWilly Tarreau 12030ca2051SWilly Tarreau /* stdint types */ 12130ca2051SWilly Tarreau typedef unsigned char uint8_t; 12230ca2051SWilly Tarreau typedef signed char int8_t; 12330ca2051SWilly Tarreau typedef unsigned short uint16_t; 12430ca2051SWilly Tarreau typedef signed short int16_t; 12530ca2051SWilly Tarreau typedef unsigned int uint32_t; 12630ca2051SWilly Tarreau typedef signed int int32_t; 12730ca2051SWilly Tarreau typedef unsigned long long uint64_t; 12830ca2051SWilly Tarreau typedef signed long long int64_t; 12930ca2051SWilly Tarreau typedef unsigned long size_t; 13030ca2051SWilly Tarreau typedef signed long ssize_t; 13130ca2051SWilly Tarreau typedef unsigned long uintptr_t; 13230ca2051SWilly Tarreau typedef signed long intptr_t; 13330ca2051SWilly Tarreau typedef signed long ptrdiff_t; 13430ca2051SWilly Tarreau 13530ca2051SWilly Tarreau /* for stat() */ 13630ca2051SWilly Tarreau typedef unsigned int dev_t; 13730ca2051SWilly Tarreau typedef unsigned long ino_t; 13830ca2051SWilly Tarreau typedef unsigned int mode_t; 13930ca2051SWilly Tarreau typedef signed int pid_t; 14030ca2051SWilly Tarreau typedef unsigned int uid_t; 14130ca2051SWilly Tarreau typedef unsigned int gid_t; 14230ca2051SWilly Tarreau typedef unsigned long nlink_t; 14330ca2051SWilly Tarreau typedef signed long off_t; 14430ca2051SWilly Tarreau typedef signed long blksize_t; 14530ca2051SWilly Tarreau typedef signed long blkcnt_t; 14630ca2051SWilly Tarreau typedef signed long time_t; 14730ca2051SWilly Tarreau 14830ca2051SWilly Tarreau /* for poll() */ 14930ca2051SWilly Tarreau struct pollfd { 15030ca2051SWilly Tarreau int fd; 15130ca2051SWilly Tarreau short int events; 15230ca2051SWilly Tarreau short int revents; 15330ca2051SWilly Tarreau }; 15430ca2051SWilly Tarreau 15530ca2051SWilly Tarreau /* for select() */ 15630ca2051SWilly Tarreau struct timeval { 15730ca2051SWilly Tarreau long tv_sec; 15830ca2051SWilly Tarreau long tv_usec; 15930ca2051SWilly Tarreau }; 16030ca2051SWilly Tarreau 16130ca2051SWilly Tarreau /* for pselect() */ 16230ca2051SWilly Tarreau struct timespec { 16330ca2051SWilly Tarreau long tv_sec; 16430ca2051SWilly Tarreau long tv_nsec; 16530ca2051SWilly Tarreau }; 16630ca2051SWilly Tarreau 16730ca2051SWilly Tarreau /* for gettimeofday() */ 16830ca2051SWilly Tarreau struct timezone { 16930ca2051SWilly Tarreau int tz_minuteswest; 17030ca2051SWilly Tarreau int tz_dsttime; 17130ca2051SWilly Tarreau }; 17230ca2051SWilly Tarreau 17330ca2051SWilly Tarreau /* for getdents64() */ 17430ca2051SWilly Tarreau struct linux_dirent64 { 17530ca2051SWilly Tarreau uint64_t d_ino; 17630ca2051SWilly Tarreau int64_t d_off; 17730ca2051SWilly Tarreau unsigned short d_reclen; 17830ca2051SWilly Tarreau unsigned char d_type; 17930ca2051SWilly Tarreau char d_name[]; 18030ca2051SWilly Tarreau }; 18130ca2051SWilly Tarreau 18230ca2051SWilly Tarreau /* commonly an fd_set represents 256 FDs */ 18330ca2051SWilly Tarreau #define FD_SETSIZE 256 18430ca2051SWilly Tarreau typedef struct { uint32_t fd32[FD_SETSIZE/32]; } fd_set; 18530ca2051SWilly Tarreau 18630ca2051SWilly Tarreau /* needed by wait4() */ 18730ca2051SWilly Tarreau struct rusage { 18830ca2051SWilly Tarreau struct timeval ru_utime; 18930ca2051SWilly Tarreau struct timeval ru_stime; 19030ca2051SWilly Tarreau long ru_maxrss; 19130ca2051SWilly Tarreau long ru_ixrss; 19230ca2051SWilly Tarreau long ru_idrss; 19330ca2051SWilly Tarreau long ru_isrss; 19430ca2051SWilly Tarreau long ru_minflt; 19530ca2051SWilly Tarreau long ru_majflt; 19630ca2051SWilly Tarreau long ru_nswap; 19730ca2051SWilly Tarreau long ru_inblock; 19830ca2051SWilly Tarreau long ru_oublock; 19930ca2051SWilly Tarreau long ru_msgsnd; 20030ca2051SWilly Tarreau long ru_msgrcv; 20130ca2051SWilly Tarreau long ru_nsignals; 20230ca2051SWilly Tarreau long ru_nvcsw; 20330ca2051SWilly Tarreau long ru_nivcsw; 20430ca2051SWilly Tarreau }; 20530ca2051SWilly Tarreau 20630ca2051SWilly Tarreau /* stat flags (WARNING, octal here) */ 20730ca2051SWilly Tarreau #define S_IFDIR 0040000 20830ca2051SWilly Tarreau #define S_IFCHR 0020000 20930ca2051SWilly Tarreau #define S_IFBLK 0060000 21030ca2051SWilly Tarreau #define S_IFREG 0100000 21130ca2051SWilly Tarreau #define S_IFIFO 0010000 21230ca2051SWilly Tarreau #define S_IFLNK 0120000 21330ca2051SWilly Tarreau #define S_IFSOCK 0140000 21430ca2051SWilly Tarreau #define S_IFMT 0170000 21530ca2051SWilly Tarreau 21630ca2051SWilly Tarreau #define S_ISDIR(mode) (((mode) & S_IFDIR) == S_IFDIR) 21730ca2051SWilly Tarreau #define S_ISCHR(mode) (((mode) & S_IFCHR) == S_IFCHR) 21830ca2051SWilly Tarreau #define S_ISBLK(mode) (((mode) & S_IFBLK) == S_IFBLK) 21930ca2051SWilly Tarreau #define S_ISREG(mode) (((mode) & S_IFREG) == S_IFREG) 22030ca2051SWilly Tarreau #define S_ISFIFO(mode) (((mode) & S_IFIFO) == S_IFIFO) 22130ca2051SWilly Tarreau #define S_ISLNK(mode) (((mode) & S_IFLNK) == S_IFLNK) 22230ca2051SWilly Tarreau #define S_ISSOCK(mode) (((mode) & S_IFSOCK) == S_IFSOCK) 22330ca2051SWilly Tarreau 22430ca2051SWilly Tarreau #define DT_UNKNOWN 0 22530ca2051SWilly Tarreau #define DT_FIFO 1 22630ca2051SWilly Tarreau #define DT_CHR 2 22730ca2051SWilly Tarreau #define DT_DIR 4 22830ca2051SWilly Tarreau #define DT_BLK 6 22930ca2051SWilly Tarreau #define DT_REG 8 23030ca2051SWilly Tarreau #define DT_LNK 10 23130ca2051SWilly Tarreau #define DT_SOCK 12 23230ca2051SWilly Tarreau 23330ca2051SWilly Tarreau /* all the *at functions */ 23430ca2051SWilly Tarreau #ifndef AT_FDWCD 23530ca2051SWilly Tarreau #define AT_FDCWD -100 23630ca2051SWilly Tarreau #endif 23730ca2051SWilly Tarreau 23830ca2051SWilly Tarreau /* lseek */ 23930ca2051SWilly Tarreau #define SEEK_SET 0 24030ca2051SWilly Tarreau #define SEEK_CUR 1 24130ca2051SWilly Tarreau #define SEEK_END 2 24230ca2051SWilly Tarreau 24330ca2051SWilly Tarreau /* reboot */ 24430ca2051SWilly Tarreau #define LINUX_REBOOT_MAGIC1 0xfee1dead 24530ca2051SWilly Tarreau #define LINUX_REBOOT_MAGIC2 0x28121969 24630ca2051SWilly Tarreau #define LINUX_REBOOT_CMD_HALT 0xcdef0123 24730ca2051SWilly Tarreau #define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc 24830ca2051SWilly Tarreau #define LINUX_REBOOT_CMD_RESTART 0x01234567 24930ca2051SWilly Tarreau #define LINUX_REBOOT_CMD_SW_SUSPEND 0xd000fce2 25030ca2051SWilly Tarreau 25130ca2051SWilly Tarreau 25230ca2051SWilly Tarreau /* The format of the struct as returned by the libc to the application, which 25330ca2051SWilly Tarreau * significantly differs from the format returned by the stat() syscall flavours. 25430ca2051SWilly Tarreau */ 25530ca2051SWilly Tarreau struct stat { 25630ca2051SWilly Tarreau dev_t st_dev; /* ID of device containing file */ 25730ca2051SWilly Tarreau ino_t st_ino; /* inode number */ 25830ca2051SWilly Tarreau mode_t st_mode; /* protection */ 25930ca2051SWilly Tarreau nlink_t st_nlink; /* number of hard links */ 26030ca2051SWilly Tarreau uid_t st_uid; /* user ID of owner */ 26130ca2051SWilly Tarreau gid_t st_gid; /* group ID of owner */ 26230ca2051SWilly Tarreau dev_t st_rdev; /* device ID (if special file) */ 26330ca2051SWilly Tarreau off_t st_size; /* total size, in bytes */ 26430ca2051SWilly Tarreau blksize_t st_blksize; /* blocksize for file system I/O */ 26530ca2051SWilly Tarreau blkcnt_t st_blocks; /* number of 512B blocks allocated */ 26630ca2051SWilly Tarreau time_t st_atime; /* time of last access */ 26730ca2051SWilly Tarreau time_t st_mtime; /* time of last modification */ 26830ca2051SWilly Tarreau time_t st_ctime; /* time of last status change */ 26930ca2051SWilly Tarreau }; 27030ca2051SWilly Tarreau 27130ca2051SWilly Tarreau #define WEXITSTATUS(status) (((status) & 0xff00) >> 8) 27230ca2051SWilly Tarreau #define WIFEXITED(status) (((status) & 0x7f) == 0) 27330ca2051SWilly Tarreau 27430ca2051SWilly Tarreau 27530ca2051SWilly Tarreau /* Below comes the architecture-specific code. For each architecture, we have 27630ca2051SWilly Tarreau * the syscall declarations and the _start code definition. This is the only 27730ca2051SWilly Tarreau * global part. On all architectures the kernel puts everything in the stack 27830ca2051SWilly Tarreau * before jumping to _start just above us, without any return address (_start 27930ca2051SWilly Tarreau * is not a function but an entry pint). So at the stack pointer we find argc. 28030ca2051SWilly Tarreau * Then argv[] begins, and ends at the first NULL. Then we have envp which 28130ca2051SWilly Tarreau * starts and ends with a NULL as well. So envp=argv+argc+1. 28230ca2051SWilly Tarreau */ 28330ca2051SWilly Tarreau 28430ca2051SWilly Tarreau #if defined(__x86_64__) 28530ca2051SWilly Tarreau /* Syscalls for x86_64 : 28630ca2051SWilly Tarreau * - registers are 64-bit 28730ca2051SWilly Tarreau * - syscall number is passed in rax 28830ca2051SWilly Tarreau * - arguments are in rdi, rsi, rdx, r10, r8, r9 respectively 28930ca2051SWilly Tarreau * - the system call is performed by calling the syscall instruction 29030ca2051SWilly Tarreau * - syscall return comes in rax 29130ca2051SWilly Tarreau * - rcx and r8..r11 may be clobbered, others are preserved. 29230ca2051SWilly Tarreau * - the arguments are cast to long and assigned into the target registers 29330ca2051SWilly Tarreau * which are then simply passed as registers to the asm code, so that we 29430ca2051SWilly Tarreau * don't have to experience issues with register constraints. 29530ca2051SWilly Tarreau * - the syscall number is always specified last in order to allow to force 29630ca2051SWilly Tarreau * some registers before (gcc refuses a %-register at the last position). 29730ca2051SWilly Tarreau */ 29830ca2051SWilly Tarreau 29930ca2051SWilly Tarreau #define my_syscall0(num) \ 30030ca2051SWilly Tarreau ({ \ 30130ca2051SWilly Tarreau long _ret; \ 30230ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 30330ca2051SWilly Tarreau \ 30430ca2051SWilly Tarreau asm volatile ( \ 30530ca2051SWilly Tarreau "syscall\n" \ 30630ca2051SWilly Tarreau : "=a" (_ret) \ 30730ca2051SWilly Tarreau : "0"(_num) \ 30830ca2051SWilly Tarreau : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \ 30930ca2051SWilly Tarreau ); \ 31030ca2051SWilly Tarreau _ret; \ 31130ca2051SWilly Tarreau }) 31230ca2051SWilly Tarreau 31330ca2051SWilly Tarreau #define my_syscall1(num, arg1) \ 31430ca2051SWilly Tarreau ({ \ 31530ca2051SWilly Tarreau long _ret; \ 31630ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 31730ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 31830ca2051SWilly Tarreau \ 31930ca2051SWilly Tarreau asm volatile ( \ 32030ca2051SWilly Tarreau "syscall\n" \ 32130ca2051SWilly Tarreau : "=a" (_ret) \ 32230ca2051SWilly Tarreau : "r"(_arg1), \ 32330ca2051SWilly Tarreau "0"(_num) \ 32430ca2051SWilly Tarreau : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \ 32530ca2051SWilly Tarreau ); \ 32630ca2051SWilly Tarreau _ret; \ 32730ca2051SWilly Tarreau }) 32830ca2051SWilly Tarreau 32930ca2051SWilly Tarreau #define my_syscall2(num, arg1, arg2) \ 33030ca2051SWilly Tarreau ({ \ 33130ca2051SWilly Tarreau long _ret; \ 33230ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 33330ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 33430ca2051SWilly Tarreau register long _arg2 asm("rsi") = (long)(arg2); \ 33530ca2051SWilly Tarreau \ 33630ca2051SWilly Tarreau asm volatile ( \ 33730ca2051SWilly Tarreau "syscall\n" \ 33830ca2051SWilly Tarreau : "=a" (_ret) \ 33930ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), \ 34030ca2051SWilly Tarreau "0"(_num) \ 34130ca2051SWilly Tarreau : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \ 34230ca2051SWilly Tarreau ); \ 34330ca2051SWilly Tarreau _ret; \ 34430ca2051SWilly Tarreau }) 34530ca2051SWilly Tarreau 34630ca2051SWilly Tarreau #define my_syscall3(num, arg1, arg2, arg3) \ 34730ca2051SWilly Tarreau ({ \ 34830ca2051SWilly Tarreau long _ret; \ 34930ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 35030ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 35130ca2051SWilly Tarreau register long _arg2 asm("rsi") = (long)(arg2); \ 35230ca2051SWilly Tarreau register long _arg3 asm("rdx") = (long)(arg3); \ 35330ca2051SWilly Tarreau \ 35430ca2051SWilly Tarreau asm volatile ( \ 35530ca2051SWilly Tarreau "syscall\n" \ 35630ca2051SWilly Tarreau : "=a" (_ret) \ 35730ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 35830ca2051SWilly Tarreau "0"(_num) \ 35930ca2051SWilly Tarreau : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \ 36030ca2051SWilly Tarreau ); \ 36130ca2051SWilly Tarreau _ret; \ 36230ca2051SWilly Tarreau }) 36330ca2051SWilly Tarreau 36430ca2051SWilly Tarreau #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 36530ca2051SWilly Tarreau ({ \ 36630ca2051SWilly Tarreau long _ret; \ 36730ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 36830ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 36930ca2051SWilly Tarreau register long _arg2 asm("rsi") = (long)(arg2); \ 37030ca2051SWilly Tarreau register long _arg3 asm("rdx") = (long)(arg3); \ 37130ca2051SWilly Tarreau register long _arg4 asm("r10") = (long)(arg4); \ 37230ca2051SWilly Tarreau \ 37330ca2051SWilly Tarreau asm volatile ( \ 37430ca2051SWilly Tarreau "syscall\n" \ 37530ca2051SWilly Tarreau : "=a" (_ret), "=r"(_arg4) \ 37630ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 37730ca2051SWilly Tarreau "0"(_num) \ 37830ca2051SWilly Tarreau : "rcx", "r8", "r9", "r11", "memory", "cc" \ 37930ca2051SWilly Tarreau ); \ 38030ca2051SWilly Tarreau _ret; \ 38130ca2051SWilly Tarreau }) 38230ca2051SWilly Tarreau 38330ca2051SWilly Tarreau #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 38430ca2051SWilly Tarreau ({ \ 38530ca2051SWilly Tarreau long _ret; \ 38630ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 38730ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 38830ca2051SWilly Tarreau register long _arg2 asm("rsi") = (long)(arg2); \ 38930ca2051SWilly Tarreau register long _arg3 asm("rdx") = (long)(arg3); \ 39030ca2051SWilly Tarreau register long _arg4 asm("r10") = (long)(arg4); \ 39130ca2051SWilly Tarreau register long _arg5 asm("r8") = (long)(arg5); \ 39230ca2051SWilly Tarreau \ 39330ca2051SWilly Tarreau asm volatile ( \ 39430ca2051SWilly Tarreau "syscall\n" \ 39530ca2051SWilly Tarreau : "=a" (_ret), "=r"(_arg4), "=r"(_arg5) \ 39630ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 39730ca2051SWilly Tarreau "0"(_num) \ 39830ca2051SWilly Tarreau : "rcx", "r9", "r11", "memory", "cc" \ 39930ca2051SWilly Tarreau ); \ 40030ca2051SWilly Tarreau _ret; \ 40130ca2051SWilly Tarreau }) 40230ca2051SWilly Tarreau 40330ca2051SWilly Tarreau #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 40430ca2051SWilly Tarreau ({ \ 40530ca2051SWilly Tarreau long _ret; \ 40630ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 40730ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 40830ca2051SWilly Tarreau register long _arg2 asm("rsi") = (long)(arg2); \ 40930ca2051SWilly Tarreau register long _arg3 asm("rdx") = (long)(arg3); \ 41030ca2051SWilly Tarreau register long _arg4 asm("r10") = (long)(arg4); \ 41130ca2051SWilly Tarreau register long _arg5 asm("r8") = (long)(arg5); \ 41230ca2051SWilly Tarreau register long _arg6 asm("r9") = (long)(arg6); \ 41330ca2051SWilly Tarreau \ 41430ca2051SWilly Tarreau asm volatile ( \ 41530ca2051SWilly Tarreau "syscall\n" \ 41630ca2051SWilly Tarreau : "=a" (_ret), "=r"(_arg4), "=r"(_arg5) \ 41730ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 41830ca2051SWilly Tarreau "r"(_arg6), "0"(_num) \ 41930ca2051SWilly Tarreau : "rcx", "r11", "memory", "cc" \ 42030ca2051SWilly Tarreau ); \ 42130ca2051SWilly Tarreau _ret; \ 42230ca2051SWilly Tarreau }) 42330ca2051SWilly Tarreau 42430ca2051SWilly Tarreau /* startup code */ 42530ca2051SWilly Tarreau asm(".section .text\n" 42630ca2051SWilly Tarreau ".global _start\n" 42730ca2051SWilly Tarreau "_start:\n" 42830ca2051SWilly Tarreau "pop %rdi\n" // argc (first arg, %rdi) 42930ca2051SWilly Tarreau "mov %rsp, %rsi\n" // argv[] (second arg, %rsi) 43030ca2051SWilly Tarreau "lea 8(%rsi,%rdi,8),%rdx\n" // then a NULL then envp (third arg, %rdx) 43130ca2051SWilly Tarreau "and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned when 43230ca2051SWilly Tarreau "sub $8, %rsp\n" // entering the callee 43330ca2051SWilly Tarreau "call main\n" // main() returns the status code, we'll exit with it. 43430ca2051SWilly Tarreau "movzb %al, %rdi\n" // retrieve exit code from 8 lower bits 43530ca2051SWilly Tarreau "mov $60, %rax\n" // NR_exit == 60 43630ca2051SWilly Tarreau "syscall\n" // really exit 43730ca2051SWilly Tarreau "hlt\n" // ensure it does not return 43830ca2051SWilly Tarreau ""); 43930ca2051SWilly Tarreau 44030ca2051SWilly Tarreau /* fcntl / open */ 44130ca2051SWilly Tarreau #define O_RDONLY 0 44230ca2051SWilly Tarreau #define O_WRONLY 1 44330ca2051SWilly Tarreau #define O_RDWR 2 44430ca2051SWilly Tarreau #define O_CREAT 0x40 44530ca2051SWilly Tarreau #define O_EXCL 0x80 44630ca2051SWilly Tarreau #define O_NOCTTY 0x100 44730ca2051SWilly Tarreau #define O_TRUNC 0x200 44830ca2051SWilly Tarreau #define O_APPEND 0x400 44930ca2051SWilly Tarreau #define O_NONBLOCK 0x800 45030ca2051SWilly Tarreau #define O_DIRECTORY 0x10000 45130ca2051SWilly Tarreau 45230ca2051SWilly Tarreau /* The struct returned by the stat() syscall, equivalent to stat64(). The 45330ca2051SWilly Tarreau * syscall returns 116 bytes and stops in the middle of __unused. 45430ca2051SWilly Tarreau */ 45530ca2051SWilly Tarreau struct sys_stat_struct { 45630ca2051SWilly Tarreau unsigned long st_dev; 45730ca2051SWilly Tarreau unsigned long st_ino; 45830ca2051SWilly Tarreau unsigned long st_nlink; 45930ca2051SWilly Tarreau unsigned int st_mode; 46030ca2051SWilly Tarreau unsigned int st_uid; 46130ca2051SWilly Tarreau 46230ca2051SWilly Tarreau unsigned int st_gid; 46330ca2051SWilly Tarreau unsigned int __pad0; 46430ca2051SWilly Tarreau unsigned long st_rdev; 46530ca2051SWilly Tarreau long st_size; 46630ca2051SWilly Tarreau long st_blksize; 46730ca2051SWilly Tarreau 46830ca2051SWilly Tarreau long st_blocks; 46930ca2051SWilly Tarreau unsigned long st_atime; 47030ca2051SWilly Tarreau unsigned long st_atime_nsec; 47130ca2051SWilly Tarreau unsigned long st_mtime; 47230ca2051SWilly Tarreau 47330ca2051SWilly Tarreau unsigned long st_mtime_nsec; 47430ca2051SWilly Tarreau unsigned long st_ctime; 47530ca2051SWilly Tarreau unsigned long st_ctime_nsec; 47630ca2051SWilly Tarreau long __unused[3]; 47730ca2051SWilly Tarreau }; 47830ca2051SWilly Tarreau 47930ca2051SWilly Tarreau #elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) 48030ca2051SWilly Tarreau /* Syscalls for i386 : 48130ca2051SWilly Tarreau * - mostly similar to x86_64 48230ca2051SWilly Tarreau * - registers are 32-bit 48330ca2051SWilly Tarreau * - syscall number is passed in eax 48430ca2051SWilly Tarreau * - arguments are in ebx, ecx, edx, esi, edi, ebp respectively 48530ca2051SWilly Tarreau * - all registers are preserved (except eax of course) 48630ca2051SWilly Tarreau * - the system call is performed by calling int $0x80 48730ca2051SWilly Tarreau * - syscall return comes in eax 48830ca2051SWilly Tarreau * - the arguments are cast to long and assigned into the target registers 48930ca2051SWilly Tarreau * which are then simply passed as registers to the asm code, so that we 49030ca2051SWilly Tarreau * don't have to experience issues with register constraints. 49130ca2051SWilly Tarreau * - the syscall number is always specified last in order to allow to force 49230ca2051SWilly Tarreau * some registers before (gcc refuses a %-register at the last position). 49330ca2051SWilly Tarreau * 49430ca2051SWilly Tarreau * Also, i386 supports the old_select syscall if newselect is not available 49530ca2051SWilly Tarreau */ 49630ca2051SWilly Tarreau #define __ARCH_WANT_SYS_OLD_SELECT 49730ca2051SWilly Tarreau 49830ca2051SWilly Tarreau #define my_syscall0(num) \ 49930ca2051SWilly Tarreau ({ \ 50030ca2051SWilly Tarreau long _ret; \ 50130ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 50230ca2051SWilly Tarreau \ 50330ca2051SWilly Tarreau asm volatile ( \ 50430ca2051SWilly Tarreau "int $0x80\n" \ 50530ca2051SWilly Tarreau : "=a" (_ret) \ 50630ca2051SWilly Tarreau : "0"(_num) \ 50730ca2051SWilly Tarreau : "memory", "cc" \ 50830ca2051SWilly Tarreau ); \ 50930ca2051SWilly Tarreau _ret; \ 51030ca2051SWilly Tarreau }) 51130ca2051SWilly Tarreau 51230ca2051SWilly Tarreau #define my_syscall1(num, arg1) \ 51330ca2051SWilly Tarreau ({ \ 51430ca2051SWilly Tarreau long _ret; \ 51530ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 51630ca2051SWilly Tarreau register long _arg1 asm("ebx") = (long)(arg1); \ 51730ca2051SWilly Tarreau \ 51830ca2051SWilly Tarreau asm volatile ( \ 51930ca2051SWilly Tarreau "int $0x80\n" \ 52030ca2051SWilly Tarreau : "=a" (_ret) \ 52130ca2051SWilly Tarreau : "r"(_arg1), \ 52230ca2051SWilly Tarreau "0"(_num) \ 52330ca2051SWilly Tarreau : "memory", "cc" \ 52430ca2051SWilly Tarreau ); \ 52530ca2051SWilly Tarreau _ret; \ 52630ca2051SWilly Tarreau }) 52730ca2051SWilly Tarreau 52830ca2051SWilly Tarreau #define my_syscall2(num, arg1, arg2) \ 52930ca2051SWilly Tarreau ({ \ 53030ca2051SWilly Tarreau long _ret; \ 53130ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 53230ca2051SWilly Tarreau register long _arg1 asm("ebx") = (long)(arg1); \ 53330ca2051SWilly Tarreau register long _arg2 asm("ecx") = (long)(arg2); \ 53430ca2051SWilly Tarreau \ 53530ca2051SWilly Tarreau asm volatile ( \ 53630ca2051SWilly Tarreau "int $0x80\n" \ 53730ca2051SWilly Tarreau : "=a" (_ret) \ 53830ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), \ 53930ca2051SWilly Tarreau "0"(_num) \ 54030ca2051SWilly Tarreau : "memory", "cc" \ 54130ca2051SWilly Tarreau ); \ 54230ca2051SWilly Tarreau _ret; \ 54330ca2051SWilly Tarreau }) 54430ca2051SWilly Tarreau 54530ca2051SWilly Tarreau #define my_syscall3(num, arg1, arg2, arg3) \ 54630ca2051SWilly Tarreau ({ \ 54730ca2051SWilly Tarreau long _ret; \ 54830ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 54930ca2051SWilly Tarreau register long _arg1 asm("ebx") = (long)(arg1); \ 55030ca2051SWilly Tarreau register long _arg2 asm("ecx") = (long)(arg2); \ 55130ca2051SWilly Tarreau register long _arg3 asm("edx") = (long)(arg3); \ 55230ca2051SWilly Tarreau \ 55330ca2051SWilly Tarreau asm volatile ( \ 55430ca2051SWilly Tarreau "int $0x80\n" \ 55530ca2051SWilly Tarreau : "=a" (_ret) \ 55630ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 55730ca2051SWilly Tarreau "0"(_num) \ 55830ca2051SWilly Tarreau : "memory", "cc" \ 55930ca2051SWilly Tarreau ); \ 56030ca2051SWilly Tarreau _ret; \ 56130ca2051SWilly Tarreau }) 56230ca2051SWilly Tarreau 56330ca2051SWilly Tarreau #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 56430ca2051SWilly Tarreau ({ \ 56530ca2051SWilly Tarreau long _ret; \ 56630ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 56730ca2051SWilly Tarreau register long _arg1 asm("ebx") = (long)(arg1); \ 56830ca2051SWilly Tarreau register long _arg2 asm("ecx") = (long)(arg2); \ 56930ca2051SWilly Tarreau register long _arg3 asm("edx") = (long)(arg3); \ 57030ca2051SWilly Tarreau register long _arg4 asm("esi") = (long)(arg4); \ 57130ca2051SWilly Tarreau \ 57230ca2051SWilly Tarreau asm volatile ( \ 57330ca2051SWilly Tarreau "int $0x80\n" \ 57430ca2051SWilly Tarreau : "=a" (_ret) \ 57530ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 57630ca2051SWilly Tarreau "0"(_num) \ 57730ca2051SWilly Tarreau : "memory", "cc" \ 57830ca2051SWilly Tarreau ); \ 57930ca2051SWilly Tarreau _ret; \ 58030ca2051SWilly Tarreau }) 58130ca2051SWilly Tarreau 58230ca2051SWilly Tarreau #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 58330ca2051SWilly Tarreau ({ \ 58430ca2051SWilly Tarreau long _ret; \ 58530ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 58630ca2051SWilly Tarreau register long _arg1 asm("ebx") = (long)(arg1); \ 58730ca2051SWilly Tarreau register long _arg2 asm("ecx") = (long)(arg2); \ 58830ca2051SWilly Tarreau register long _arg3 asm("edx") = (long)(arg3); \ 58930ca2051SWilly Tarreau register long _arg4 asm("esi") = (long)(arg4); \ 59030ca2051SWilly Tarreau register long _arg5 asm("edi") = (long)(arg5); \ 59130ca2051SWilly Tarreau \ 59230ca2051SWilly Tarreau asm volatile ( \ 59330ca2051SWilly Tarreau "int $0x80\n" \ 59430ca2051SWilly Tarreau : "=a" (_ret) \ 59530ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 59630ca2051SWilly Tarreau "0"(_num) \ 59730ca2051SWilly Tarreau : "memory", "cc" \ 59830ca2051SWilly Tarreau ); \ 59930ca2051SWilly Tarreau _ret; \ 60030ca2051SWilly Tarreau }) 60130ca2051SWilly Tarreau 60230ca2051SWilly Tarreau /* startup code */ 60330ca2051SWilly Tarreau asm(".section .text\n" 60430ca2051SWilly Tarreau ".global _start\n" 60530ca2051SWilly Tarreau "_start:\n" 60630ca2051SWilly Tarreau "pop %eax\n" // argc (first arg, %eax) 60730ca2051SWilly Tarreau "mov %esp, %ebx\n" // argv[] (second arg, %ebx) 60830ca2051SWilly Tarreau "lea 4(%ebx,%eax,4),%ecx\n" // then a NULL then envp (third arg, %ecx) 60930ca2051SWilly Tarreau "and $-16, %esp\n" // x86 ABI : esp must be 16-byte aligned when 61030ca2051SWilly Tarreau "push %ecx\n" // push all registers on the stack so that we 61130ca2051SWilly Tarreau "push %ebx\n" // support both regparm and plain stack modes 61230ca2051SWilly Tarreau "push %eax\n" 61330ca2051SWilly Tarreau "call main\n" // main() returns the status code in %eax 61430ca2051SWilly Tarreau "movzbl %al, %ebx\n" // retrieve exit code from lower 8 bits 61530ca2051SWilly Tarreau "movl $1, %eax\n" // NR_exit == 1 61630ca2051SWilly Tarreau "int $0x80\n" // exit now 61730ca2051SWilly Tarreau "hlt\n" // ensure it does not 61830ca2051SWilly Tarreau ""); 61930ca2051SWilly Tarreau 62030ca2051SWilly Tarreau /* fcntl / open */ 62130ca2051SWilly Tarreau #define O_RDONLY 0 62230ca2051SWilly Tarreau #define O_WRONLY 1 62330ca2051SWilly Tarreau #define O_RDWR 2 62430ca2051SWilly Tarreau #define O_CREAT 0x40 62530ca2051SWilly Tarreau #define O_EXCL 0x80 62630ca2051SWilly Tarreau #define O_NOCTTY 0x100 62730ca2051SWilly Tarreau #define O_TRUNC 0x200 62830ca2051SWilly Tarreau #define O_APPEND 0x400 62930ca2051SWilly Tarreau #define O_NONBLOCK 0x800 63030ca2051SWilly Tarreau #define O_DIRECTORY 0x10000 63130ca2051SWilly Tarreau 63230ca2051SWilly Tarreau /* The struct returned by the stat() syscall, 32-bit only, the syscall returns 63330ca2051SWilly Tarreau * exactly 56 bytes (stops before the unused array). 63430ca2051SWilly Tarreau */ 63530ca2051SWilly Tarreau struct sys_stat_struct { 63630ca2051SWilly Tarreau unsigned long st_dev; 63730ca2051SWilly Tarreau unsigned long st_ino; 63830ca2051SWilly Tarreau unsigned short st_mode; 63930ca2051SWilly Tarreau unsigned short st_nlink; 64030ca2051SWilly Tarreau unsigned short st_uid; 64130ca2051SWilly Tarreau unsigned short st_gid; 64230ca2051SWilly Tarreau 64330ca2051SWilly Tarreau unsigned long st_rdev; 64430ca2051SWilly Tarreau unsigned long st_size; 64530ca2051SWilly Tarreau unsigned long st_blksize; 64630ca2051SWilly Tarreau unsigned long st_blocks; 64730ca2051SWilly Tarreau 64830ca2051SWilly Tarreau unsigned long st_atime; 64930ca2051SWilly Tarreau unsigned long st_atime_nsec; 65030ca2051SWilly Tarreau unsigned long st_mtime; 65130ca2051SWilly Tarreau unsigned long st_mtime_nsec; 65230ca2051SWilly Tarreau 65330ca2051SWilly Tarreau unsigned long st_ctime; 65430ca2051SWilly Tarreau unsigned long st_ctime_nsec; 65530ca2051SWilly Tarreau unsigned long __unused[2]; 65630ca2051SWilly Tarreau }; 65730ca2051SWilly Tarreau 65830ca2051SWilly Tarreau #elif defined(__ARM_EABI__) 65930ca2051SWilly Tarreau /* Syscalls for ARM in ARM or Thumb modes : 66030ca2051SWilly Tarreau * - registers are 32-bit 66130ca2051SWilly Tarreau * - stack is 8-byte aligned 66230ca2051SWilly Tarreau * ( http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka4127.html) 66330ca2051SWilly Tarreau * - syscall number is passed in r7 66430ca2051SWilly Tarreau * - arguments are in r0, r1, r2, r3, r4, r5 66530ca2051SWilly Tarreau * - the system call is performed by calling svc #0 66630ca2051SWilly Tarreau * - syscall return comes in r0. 66730ca2051SWilly Tarreau * - only lr is clobbered. 66830ca2051SWilly Tarreau * - the arguments are cast to long and assigned into the target registers 66930ca2051SWilly Tarreau * which are then simply passed as registers to the asm code, so that we 67030ca2051SWilly Tarreau * don't have to experience issues with register constraints. 67130ca2051SWilly Tarreau * - the syscall number is always specified last in order to allow to force 67230ca2051SWilly Tarreau * some registers before (gcc refuses a %-register at the last position). 67330ca2051SWilly Tarreau * 67430ca2051SWilly Tarreau * Also, ARM supports the old_select syscall if newselect is not available 67530ca2051SWilly Tarreau */ 67630ca2051SWilly Tarreau #define __ARCH_WANT_SYS_OLD_SELECT 67730ca2051SWilly Tarreau 67830ca2051SWilly Tarreau #define my_syscall0(num) \ 67930ca2051SWilly Tarreau ({ \ 68030ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 68130ca2051SWilly Tarreau register long _arg1 asm("r0"); \ 68230ca2051SWilly Tarreau \ 68330ca2051SWilly Tarreau asm volatile ( \ 68430ca2051SWilly Tarreau "svc #0\n" \ 68530ca2051SWilly Tarreau : "=r"(_arg1) \ 68630ca2051SWilly Tarreau : "r"(_num) \ 68730ca2051SWilly Tarreau : "memory", "cc", "lr" \ 68830ca2051SWilly Tarreau ); \ 68930ca2051SWilly Tarreau _arg1; \ 69030ca2051SWilly Tarreau }) 69130ca2051SWilly Tarreau 69230ca2051SWilly Tarreau #define my_syscall1(num, arg1) \ 69330ca2051SWilly Tarreau ({ \ 69430ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 69530ca2051SWilly Tarreau register long _arg1 asm("r0") = (long)(arg1); \ 69630ca2051SWilly Tarreau \ 69730ca2051SWilly Tarreau asm volatile ( \ 69830ca2051SWilly Tarreau "svc #0\n" \ 69930ca2051SWilly Tarreau : "=r"(_arg1) \ 70030ca2051SWilly Tarreau : "r"(_arg1), \ 70130ca2051SWilly Tarreau "r"(_num) \ 70230ca2051SWilly Tarreau : "memory", "cc", "lr" \ 70330ca2051SWilly Tarreau ); \ 70430ca2051SWilly Tarreau _arg1; \ 70530ca2051SWilly Tarreau }) 70630ca2051SWilly Tarreau 70730ca2051SWilly Tarreau #define my_syscall2(num, arg1, arg2) \ 70830ca2051SWilly Tarreau ({ \ 70930ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 71030ca2051SWilly Tarreau register long _arg1 asm("r0") = (long)(arg1); \ 71130ca2051SWilly Tarreau register long _arg2 asm("r1") = (long)(arg2); \ 71230ca2051SWilly Tarreau \ 71330ca2051SWilly Tarreau asm volatile ( \ 71430ca2051SWilly Tarreau "svc #0\n" \ 71530ca2051SWilly Tarreau : "=r"(_arg1) \ 71630ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), \ 71730ca2051SWilly Tarreau "r"(_num) \ 71830ca2051SWilly Tarreau : "memory", "cc", "lr" \ 71930ca2051SWilly Tarreau ); \ 72030ca2051SWilly Tarreau _arg1; \ 72130ca2051SWilly Tarreau }) 72230ca2051SWilly Tarreau 72330ca2051SWilly Tarreau #define my_syscall3(num, arg1, arg2, arg3) \ 72430ca2051SWilly Tarreau ({ \ 72530ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 72630ca2051SWilly Tarreau register long _arg1 asm("r0") = (long)(arg1); \ 72730ca2051SWilly Tarreau register long _arg2 asm("r1") = (long)(arg2); \ 72830ca2051SWilly Tarreau register long _arg3 asm("r2") = (long)(arg3); \ 72930ca2051SWilly Tarreau \ 73030ca2051SWilly Tarreau asm volatile ( \ 73130ca2051SWilly Tarreau "svc #0\n" \ 73230ca2051SWilly Tarreau : "=r"(_arg1) \ 73330ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 73430ca2051SWilly Tarreau "r"(_num) \ 73530ca2051SWilly Tarreau : "memory", "cc", "lr" \ 73630ca2051SWilly Tarreau ); \ 73730ca2051SWilly Tarreau _arg1; \ 73830ca2051SWilly Tarreau }) 73930ca2051SWilly Tarreau 74030ca2051SWilly Tarreau #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 74130ca2051SWilly Tarreau ({ \ 74230ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 74330ca2051SWilly Tarreau register long _arg1 asm("r0") = (long)(arg1); \ 74430ca2051SWilly Tarreau register long _arg2 asm("r1") = (long)(arg2); \ 74530ca2051SWilly Tarreau register long _arg3 asm("r2") = (long)(arg3); \ 74630ca2051SWilly Tarreau register long _arg4 asm("r3") = (long)(arg4); \ 74730ca2051SWilly Tarreau \ 74830ca2051SWilly Tarreau asm volatile ( \ 74930ca2051SWilly Tarreau "svc #0\n" \ 75030ca2051SWilly Tarreau : "=r"(_arg1) \ 75130ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 75230ca2051SWilly Tarreau "r"(_num) \ 75330ca2051SWilly Tarreau : "memory", "cc", "lr" \ 75430ca2051SWilly Tarreau ); \ 75530ca2051SWilly Tarreau _arg1; \ 75630ca2051SWilly Tarreau }) 75730ca2051SWilly Tarreau 75830ca2051SWilly Tarreau #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 75930ca2051SWilly Tarreau ({ \ 76030ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 76130ca2051SWilly Tarreau register long _arg1 asm("r0") = (long)(arg1); \ 76230ca2051SWilly Tarreau register long _arg2 asm("r1") = (long)(arg2); \ 76330ca2051SWilly Tarreau register long _arg3 asm("r2") = (long)(arg3); \ 76430ca2051SWilly Tarreau register long _arg4 asm("r3") = (long)(arg4); \ 76530ca2051SWilly Tarreau register long _arg5 asm("r4") = (long)(arg5); \ 76630ca2051SWilly Tarreau \ 76730ca2051SWilly Tarreau asm volatile ( \ 76830ca2051SWilly Tarreau "svc #0\n" \ 76930ca2051SWilly Tarreau : "=r" (_arg1) \ 77030ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 77130ca2051SWilly Tarreau "r"(_num) \ 77230ca2051SWilly Tarreau : "memory", "cc", "lr" \ 77330ca2051SWilly Tarreau ); \ 77430ca2051SWilly Tarreau _arg1; \ 77530ca2051SWilly Tarreau }) 77630ca2051SWilly Tarreau 77730ca2051SWilly Tarreau /* startup code */ 77830ca2051SWilly Tarreau asm(".section .text\n" 77930ca2051SWilly Tarreau ".global _start\n" 78030ca2051SWilly Tarreau "_start:\n" 78130ca2051SWilly Tarreau #if defined(__THUMBEB__) || defined(__THUMBEL__) 78230ca2051SWilly Tarreau /* We enter here in 32-bit mode but if some previous functions were in 78330ca2051SWilly Tarreau * 16-bit mode, the assembler cannot know, so we need to tell it we're in 78430ca2051SWilly Tarreau * 32-bit now, then switch to 16-bit (is there a better way to do it than 78530ca2051SWilly Tarreau * adding 1 by hand ?) and tell the asm we're now in 16-bit mode so that 78630ca2051SWilly Tarreau * it generates correct instructions. Note that we do not support thumb1. 78730ca2051SWilly Tarreau */ 78830ca2051SWilly Tarreau ".code 32\n" 78930ca2051SWilly Tarreau "add r0, pc, #1\n" 79030ca2051SWilly Tarreau "bx r0\n" 79130ca2051SWilly Tarreau ".code 16\n" 79230ca2051SWilly Tarreau #endif 79330ca2051SWilly Tarreau "pop {%r0}\n" // argc was in the stack 79430ca2051SWilly Tarreau "mov %r1, %sp\n" // argv = sp 79530ca2051SWilly Tarreau "add %r2, %r1, %r0, lsl #2\n" // envp = argv + 4*argc ... 79630ca2051SWilly Tarreau "add %r2, %r2, $4\n" // ... + 4 79730ca2051SWilly Tarreau "and %r3, %r1, $-8\n" // AAPCS : sp must be 8-byte aligned in the 79830ca2051SWilly Tarreau "mov %sp, %r3\n" // callee, an bl doesn't push (lr=pc) 79930ca2051SWilly Tarreau "bl main\n" // main() returns the status code, we'll exit with it. 80030ca2051SWilly Tarreau "and %r0, %r0, $0xff\n" // limit exit code to 8 bits 80130ca2051SWilly Tarreau "movs r7, $1\n" // NR_exit == 1 80230ca2051SWilly Tarreau "svc $0x00\n" 80330ca2051SWilly Tarreau ""); 80430ca2051SWilly Tarreau 80530ca2051SWilly Tarreau /* fcntl / open */ 80630ca2051SWilly Tarreau #define O_RDONLY 0 80730ca2051SWilly Tarreau #define O_WRONLY 1 80830ca2051SWilly Tarreau #define O_RDWR 2 80930ca2051SWilly Tarreau #define O_CREAT 0x40 81030ca2051SWilly Tarreau #define O_EXCL 0x80 81130ca2051SWilly Tarreau #define O_NOCTTY 0x100 81230ca2051SWilly Tarreau #define O_TRUNC 0x200 81330ca2051SWilly Tarreau #define O_APPEND 0x400 81430ca2051SWilly Tarreau #define O_NONBLOCK 0x800 81530ca2051SWilly Tarreau #define O_DIRECTORY 0x4000 81630ca2051SWilly Tarreau 81730ca2051SWilly Tarreau /* The struct returned by the stat() syscall, 32-bit only, the syscall returns 81830ca2051SWilly Tarreau * exactly 56 bytes (stops before the unused array). In big endian, the format 81930ca2051SWilly Tarreau * differs as devices are returned as short only. 82030ca2051SWilly Tarreau */ 82130ca2051SWilly Tarreau struct sys_stat_struct { 82230ca2051SWilly Tarreau #if defined(__ARMEB__) 82330ca2051SWilly Tarreau unsigned short st_dev; 82430ca2051SWilly Tarreau unsigned short __pad1; 82530ca2051SWilly Tarreau #else 82630ca2051SWilly Tarreau unsigned long st_dev; 82730ca2051SWilly Tarreau #endif 82830ca2051SWilly Tarreau unsigned long st_ino; 82930ca2051SWilly Tarreau unsigned short st_mode; 83030ca2051SWilly Tarreau unsigned short st_nlink; 83130ca2051SWilly Tarreau unsigned short st_uid; 83230ca2051SWilly Tarreau unsigned short st_gid; 83330ca2051SWilly Tarreau #if defined(__ARMEB__) 83430ca2051SWilly Tarreau unsigned short st_rdev; 83530ca2051SWilly Tarreau unsigned short __pad2; 83630ca2051SWilly Tarreau #else 83730ca2051SWilly Tarreau unsigned long st_rdev; 83830ca2051SWilly Tarreau #endif 83930ca2051SWilly Tarreau unsigned long st_size; 84030ca2051SWilly Tarreau unsigned long st_blksize; 84130ca2051SWilly Tarreau unsigned long st_blocks; 84230ca2051SWilly Tarreau unsigned long st_atime; 84330ca2051SWilly Tarreau unsigned long st_atime_nsec; 84430ca2051SWilly Tarreau unsigned long st_mtime; 84530ca2051SWilly Tarreau unsigned long st_mtime_nsec; 84630ca2051SWilly Tarreau unsigned long st_ctime; 84730ca2051SWilly Tarreau unsigned long st_ctime_nsec; 84830ca2051SWilly Tarreau unsigned long __unused[2]; 84930ca2051SWilly Tarreau }; 85030ca2051SWilly Tarreau 85130ca2051SWilly Tarreau #elif defined(__aarch64__) 85230ca2051SWilly Tarreau /* Syscalls for AARCH64 : 85330ca2051SWilly Tarreau * - registers are 64-bit 85430ca2051SWilly Tarreau * - stack is 16-byte aligned 85530ca2051SWilly Tarreau * - syscall number is passed in x8 85630ca2051SWilly Tarreau * - arguments are in x0, x1, x2, x3, x4, x5 85730ca2051SWilly Tarreau * - the system call is performed by calling svc 0 85830ca2051SWilly Tarreau * - syscall return comes in x0. 85930ca2051SWilly Tarreau * - the arguments are cast to long and assigned into the target registers 86030ca2051SWilly Tarreau * which are then simply passed as registers to the asm code, so that we 86130ca2051SWilly Tarreau * don't have to experience issues with register constraints. 86230ca2051SWilly Tarreau * 86330ca2051SWilly Tarreau * On aarch64, select() is not implemented so we have to use pselect6(). 86430ca2051SWilly Tarreau */ 86530ca2051SWilly Tarreau #define __ARCH_WANT_SYS_PSELECT6 86630ca2051SWilly Tarreau 86730ca2051SWilly Tarreau #define my_syscall0(num) \ 86830ca2051SWilly Tarreau ({ \ 86930ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 87030ca2051SWilly Tarreau register long _arg1 asm("x0"); \ 87130ca2051SWilly Tarreau \ 87230ca2051SWilly Tarreau asm volatile ( \ 87330ca2051SWilly Tarreau "svc #0\n" \ 87430ca2051SWilly Tarreau : "=r"(_arg1) \ 87530ca2051SWilly Tarreau : "r"(_num) \ 87630ca2051SWilly Tarreau : "memory", "cc" \ 87730ca2051SWilly Tarreau ); \ 87830ca2051SWilly Tarreau _arg1; \ 87930ca2051SWilly Tarreau }) 88030ca2051SWilly Tarreau 88130ca2051SWilly Tarreau #define my_syscall1(num, arg1) \ 88230ca2051SWilly Tarreau ({ \ 88330ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 88430ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 88530ca2051SWilly Tarreau \ 88630ca2051SWilly Tarreau asm volatile ( \ 88730ca2051SWilly Tarreau "svc #0\n" \ 88830ca2051SWilly Tarreau : "=r"(_arg1) \ 88930ca2051SWilly Tarreau : "r"(_arg1), \ 89030ca2051SWilly Tarreau "r"(_num) \ 89130ca2051SWilly Tarreau : "memory", "cc" \ 89230ca2051SWilly Tarreau ); \ 89330ca2051SWilly Tarreau _arg1; \ 89430ca2051SWilly Tarreau }) 89530ca2051SWilly Tarreau 89630ca2051SWilly Tarreau #define my_syscall2(num, arg1, arg2) \ 89730ca2051SWilly Tarreau ({ \ 89830ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 89930ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 90030ca2051SWilly Tarreau register long _arg2 asm("x1") = (long)(arg2); \ 90130ca2051SWilly Tarreau \ 90230ca2051SWilly Tarreau asm volatile ( \ 90330ca2051SWilly Tarreau "svc #0\n" \ 90430ca2051SWilly Tarreau : "=r"(_arg1) \ 90530ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), \ 90630ca2051SWilly Tarreau "r"(_num) \ 90730ca2051SWilly Tarreau : "memory", "cc" \ 90830ca2051SWilly Tarreau ); \ 90930ca2051SWilly Tarreau _arg1; \ 91030ca2051SWilly Tarreau }) 91130ca2051SWilly Tarreau 91230ca2051SWilly Tarreau #define my_syscall3(num, arg1, arg2, arg3) \ 91330ca2051SWilly Tarreau ({ \ 91430ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 91530ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 91630ca2051SWilly Tarreau register long _arg2 asm("x1") = (long)(arg2); \ 91730ca2051SWilly Tarreau register long _arg3 asm("x2") = (long)(arg3); \ 91830ca2051SWilly Tarreau \ 91930ca2051SWilly Tarreau asm volatile ( \ 92030ca2051SWilly Tarreau "svc #0\n" \ 92130ca2051SWilly Tarreau : "=r"(_arg1) \ 92230ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 92330ca2051SWilly Tarreau "r"(_num) \ 92430ca2051SWilly Tarreau : "memory", "cc" \ 92530ca2051SWilly Tarreau ); \ 92630ca2051SWilly Tarreau _arg1; \ 92730ca2051SWilly Tarreau }) 92830ca2051SWilly Tarreau 92930ca2051SWilly Tarreau #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 93030ca2051SWilly Tarreau ({ \ 93130ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 93230ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 93330ca2051SWilly Tarreau register long _arg2 asm("x1") = (long)(arg2); \ 93430ca2051SWilly Tarreau register long _arg3 asm("x2") = (long)(arg3); \ 93530ca2051SWilly Tarreau register long _arg4 asm("x3") = (long)(arg4); \ 93630ca2051SWilly Tarreau \ 93730ca2051SWilly Tarreau asm volatile ( \ 93830ca2051SWilly Tarreau "svc #0\n" \ 93930ca2051SWilly Tarreau : "=r"(_arg1) \ 94030ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 94130ca2051SWilly Tarreau "r"(_num) \ 94230ca2051SWilly Tarreau : "memory", "cc" \ 94330ca2051SWilly Tarreau ); \ 94430ca2051SWilly Tarreau _arg1; \ 94530ca2051SWilly Tarreau }) 94630ca2051SWilly Tarreau 94730ca2051SWilly Tarreau #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 94830ca2051SWilly Tarreau ({ \ 94930ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 95030ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 95130ca2051SWilly Tarreau register long _arg2 asm("x1") = (long)(arg2); \ 95230ca2051SWilly Tarreau register long _arg3 asm("x2") = (long)(arg3); \ 95330ca2051SWilly Tarreau register long _arg4 asm("x3") = (long)(arg4); \ 95430ca2051SWilly Tarreau register long _arg5 asm("x4") = (long)(arg5); \ 95530ca2051SWilly Tarreau \ 95630ca2051SWilly Tarreau asm volatile ( \ 95730ca2051SWilly Tarreau "svc #0\n" \ 95830ca2051SWilly Tarreau : "=r" (_arg1) \ 95930ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 96030ca2051SWilly Tarreau "r"(_num) \ 96130ca2051SWilly Tarreau : "memory", "cc" \ 96230ca2051SWilly Tarreau ); \ 96330ca2051SWilly Tarreau _arg1; \ 96430ca2051SWilly Tarreau }) 96530ca2051SWilly Tarreau 96630ca2051SWilly Tarreau #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 96730ca2051SWilly Tarreau ({ \ 96830ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 96930ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 97030ca2051SWilly Tarreau register long _arg2 asm("x1") = (long)(arg2); \ 97130ca2051SWilly Tarreau register long _arg3 asm("x2") = (long)(arg3); \ 97230ca2051SWilly Tarreau register long _arg4 asm("x3") = (long)(arg4); \ 97330ca2051SWilly Tarreau register long _arg5 asm("x4") = (long)(arg5); \ 97430ca2051SWilly Tarreau register long _arg6 asm("x5") = (long)(arg6); \ 97530ca2051SWilly Tarreau \ 97630ca2051SWilly Tarreau asm volatile ( \ 97730ca2051SWilly Tarreau "svc #0\n" \ 97830ca2051SWilly Tarreau : "=r" (_arg1) \ 97930ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 98030ca2051SWilly Tarreau "r"(_arg6), "r"(_num) \ 98130ca2051SWilly Tarreau : "memory", "cc" \ 98230ca2051SWilly Tarreau ); \ 98330ca2051SWilly Tarreau _arg1; \ 98430ca2051SWilly Tarreau }) 98530ca2051SWilly Tarreau 98630ca2051SWilly Tarreau /* startup code */ 98730ca2051SWilly Tarreau asm(".section .text\n" 98830ca2051SWilly Tarreau ".global _start\n" 98930ca2051SWilly Tarreau "_start:\n" 99030ca2051SWilly Tarreau "ldr x0, [sp]\n" // argc (x0) was in the stack 99130ca2051SWilly Tarreau "add x1, sp, 8\n" // argv (x1) = sp 99230ca2051SWilly Tarreau "lsl x2, x0, 3\n" // envp (x2) = 8*argc ... 99330ca2051SWilly Tarreau "add x2, x2, 8\n" // + 8 (skip null) 99430ca2051SWilly Tarreau "add x2, x2, x1\n" // + argv 99530ca2051SWilly Tarreau "and sp, x1, -16\n" // sp must be 16-byte aligned in the callee 99630ca2051SWilly Tarreau "bl main\n" // main() returns the status code, we'll exit with it. 99730ca2051SWilly Tarreau "and x0, x0, 0xff\n" // limit exit code to 8 bits 99830ca2051SWilly Tarreau "mov x8, 93\n" // NR_exit == 93 99930ca2051SWilly Tarreau "svc #0\n" 100030ca2051SWilly Tarreau ""); 100130ca2051SWilly Tarreau 100230ca2051SWilly Tarreau /* fcntl / open */ 100330ca2051SWilly Tarreau #define O_RDONLY 0 100430ca2051SWilly Tarreau #define O_WRONLY 1 100530ca2051SWilly Tarreau #define O_RDWR 2 100630ca2051SWilly Tarreau #define O_CREAT 0x40 100730ca2051SWilly Tarreau #define O_EXCL 0x80 100830ca2051SWilly Tarreau #define O_NOCTTY 0x100 100930ca2051SWilly Tarreau #define O_TRUNC 0x200 101030ca2051SWilly Tarreau #define O_APPEND 0x400 101130ca2051SWilly Tarreau #define O_NONBLOCK 0x800 101230ca2051SWilly Tarreau #define O_DIRECTORY 0x4000 101330ca2051SWilly Tarreau 101430ca2051SWilly Tarreau /* The struct returned by the newfstatat() syscall. Differs slightly from the 101530ca2051SWilly Tarreau * x86_64's stat one by field ordering, so be careful. 101630ca2051SWilly Tarreau */ 101730ca2051SWilly Tarreau struct sys_stat_struct { 101830ca2051SWilly Tarreau unsigned long st_dev; 101930ca2051SWilly Tarreau unsigned long st_ino; 102030ca2051SWilly Tarreau unsigned int st_mode; 102130ca2051SWilly Tarreau unsigned int st_nlink; 102230ca2051SWilly Tarreau unsigned int st_uid; 102330ca2051SWilly Tarreau unsigned int st_gid; 102430ca2051SWilly Tarreau 102530ca2051SWilly Tarreau unsigned long st_rdev; 102630ca2051SWilly Tarreau unsigned long __pad1; 102730ca2051SWilly Tarreau long st_size; 102830ca2051SWilly Tarreau int st_blksize; 102930ca2051SWilly Tarreau int __pad2; 103030ca2051SWilly Tarreau 103130ca2051SWilly Tarreau long st_blocks; 103230ca2051SWilly Tarreau long st_atime; 103330ca2051SWilly Tarreau unsigned long st_atime_nsec; 103430ca2051SWilly Tarreau long st_mtime; 103530ca2051SWilly Tarreau 103630ca2051SWilly Tarreau unsigned long st_mtime_nsec; 103730ca2051SWilly Tarreau long st_ctime; 103830ca2051SWilly Tarreau unsigned long st_ctime_nsec; 103930ca2051SWilly Tarreau unsigned int __unused[2]; 104030ca2051SWilly Tarreau }; 104130ca2051SWilly Tarreau 104230ca2051SWilly Tarreau #elif defined(__mips__) && defined(_ABIO32) 104330ca2051SWilly Tarreau /* Syscalls for MIPS ABI O32 : 104430ca2051SWilly Tarreau * - WARNING! there's always a delayed slot! 104530ca2051SWilly Tarreau * - WARNING again, the syntax is different, registers take a '$' and numbers 104630ca2051SWilly Tarreau * do not. 104730ca2051SWilly Tarreau * - registers are 32-bit 104830ca2051SWilly Tarreau * - stack is 8-byte aligned 104930ca2051SWilly Tarreau * - syscall number is passed in v0 (starts at 0xfa0). 105030ca2051SWilly Tarreau * - arguments are in a0, a1, a2, a3, then the stack. The caller needs to 105130ca2051SWilly Tarreau * leave some room in the stack for the callee to save a0..a3 if needed. 105230ca2051SWilly Tarreau * - Many registers are clobbered, in fact only a0..a2 and s0..s8 are 105330ca2051SWilly Tarreau * preserved. See: https://www.linux-mips.org/wiki/Syscall as well as 105430ca2051SWilly Tarreau * scall32-o32.S in the kernel sources. 105530ca2051SWilly Tarreau * - the system call is performed by calling "syscall" 105630ca2051SWilly Tarreau * - syscall return comes in v0, and register a3 needs to be checked to know 105730ca2051SWilly Tarreau * if an error occured, in which case errno is in v0. 105830ca2051SWilly Tarreau * - the arguments are cast to long and assigned into the target registers 105930ca2051SWilly Tarreau * which are then simply passed as registers to the asm code, so that we 106030ca2051SWilly Tarreau * don't have to experience issues with register constraints. 106130ca2051SWilly Tarreau */ 106230ca2051SWilly Tarreau 106330ca2051SWilly Tarreau #define my_syscall0(num) \ 106430ca2051SWilly Tarreau ({ \ 106530ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 106630ca2051SWilly Tarreau register long _arg4 asm("a3"); \ 106730ca2051SWilly Tarreau \ 106830ca2051SWilly Tarreau asm volatile ( \ 106930ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 107030ca2051SWilly Tarreau "syscall\n" \ 107130ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 107230ca2051SWilly Tarreau : "=r"(_num), "=r"(_arg4) \ 107330ca2051SWilly Tarreau : "r"(_num) \ 107430ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 107530ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 107630ca2051SWilly Tarreau ); \ 107730ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 107830ca2051SWilly Tarreau }) 107930ca2051SWilly Tarreau 108030ca2051SWilly Tarreau #define my_syscall1(num, arg1) \ 108130ca2051SWilly Tarreau ({ \ 108230ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 108330ca2051SWilly Tarreau register long _arg1 asm("a0") = (long)(arg1); \ 108430ca2051SWilly Tarreau register long _arg4 asm("a3"); \ 108530ca2051SWilly Tarreau \ 108630ca2051SWilly Tarreau asm volatile ( \ 108730ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 108830ca2051SWilly Tarreau "syscall\n" \ 108930ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 109030ca2051SWilly Tarreau : "=r"(_num), "=r"(_arg4) \ 109130ca2051SWilly Tarreau : "0"(_num), \ 109230ca2051SWilly Tarreau "r"(_arg1) \ 109330ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 109430ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 109530ca2051SWilly Tarreau ); \ 109630ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 109730ca2051SWilly Tarreau }) 109830ca2051SWilly Tarreau 109930ca2051SWilly Tarreau #define my_syscall2(num, arg1, arg2) \ 110030ca2051SWilly Tarreau ({ \ 110130ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 110230ca2051SWilly Tarreau register long _arg1 asm("a0") = (long)(arg1); \ 110330ca2051SWilly Tarreau register long _arg2 asm("a1") = (long)(arg2); \ 110430ca2051SWilly Tarreau register long _arg4 asm("a3"); \ 110530ca2051SWilly Tarreau \ 110630ca2051SWilly Tarreau asm volatile ( \ 110730ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 110830ca2051SWilly Tarreau "syscall\n" \ 110930ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 111030ca2051SWilly Tarreau : "=r"(_num), "=r"(_arg4) \ 111130ca2051SWilly Tarreau : "0"(_num), \ 111230ca2051SWilly Tarreau "r"(_arg1), "r"(_arg2) \ 111330ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 111430ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 111530ca2051SWilly Tarreau ); \ 111630ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 111730ca2051SWilly Tarreau }) 111830ca2051SWilly Tarreau 111930ca2051SWilly Tarreau #define my_syscall3(num, arg1, arg2, arg3) \ 112030ca2051SWilly Tarreau ({ \ 112130ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 112230ca2051SWilly Tarreau register long _arg1 asm("a0") = (long)(arg1); \ 112330ca2051SWilly Tarreau register long _arg2 asm("a1") = (long)(arg2); \ 112430ca2051SWilly Tarreau register long _arg3 asm("a2") = (long)(arg3); \ 112530ca2051SWilly Tarreau register long _arg4 asm("a3"); \ 112630ca2051SWilly Tarreau \ 112730ca2051SWilly Tarreau asm volatile ( \ 112830ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 112930ca2051SWilly Tarreau "syscall\n" \ 113030ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 113130ca2051SWilly Tarreau : "=r"(_num), "=r"(_arg4) \ 113230ca2051SWilly Tarreau : "0"(_num), \ 113330ca2051SWilly Tarreau "r"(_arg1), "r"(_arg2), "r"(_arg3) \ 113430ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 113530ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 113630ca2051SWilly Tarreau ); \ 113730ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 113830ca2051SWilly Tarreau }) 113930ca2051SWilly Tarreau 114030ca2051SWilly Tarreau #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 114130ca2051SWilly Tarreau ({ \ 114230ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 114330ca2051SWilly Tarreau register long _arg1 asm("a0") = (long)(arg1); \ 114430ca2051SWilly Tarreau register long _arg2 asm("a1") = (long)(arg2); \ 114530ca2051SWilly Tarreau register long _arg3 asm("a2") = (long)(arg3); \ 114630ca2051SWilly Tarreau register long _arg4 asm("a3") = (long)(arg4); \ 114730ca2051SWilly Tarreau \ 114830ca2051SWilly Tarreau asm volatile ( \ 114930ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 115030ca2051SWilly Tarreau "syscall\n" \ 115130ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 115230ca2051SWilly Tarreau : "=r" (_num), "=r"(_arg4) \ 115330ca2051SWilly Tarreau : "0"(_num), \ 115430ca2051SWilly Tarreau "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4) \ 115530ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 115630ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 115730ca2051SWilly Tarreau ); \ 115830ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 115930ca2051SWilly Tarreau }) 116030ca2051SWilly Tarreau 116130ca2051SWilly Tarreau #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 116230ca2051SWilly Tarreau ({ \ 116330ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 116430ca2051SWilly Tarreau register long _arg1 asm("a0") = (long)(arg1); \ 116530ca2051SWilly Tarreau register long _arg2 asm("a1") = (long)(arg2); \ 116630ca2051SWilly Tarreau register long _arg3 asm("a2") = (long)(arg3); \ 116730ca2051SWilly Tarreau register long _arg4 asm("a3") = (long)(arg4); \ 116830ca2051SWilly Tarreau register long _arg5 = (long)(arg5); \ 116930ca2051SWilly Tarreau \ 117030ca2051SWilly Tarreau asm volatile ( \ 117130ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 117230ca2051SWilly Tarreau "sw %7, 16($sp)\n" \ 117330ca2051SWilly Tarreau "syscall\n " \ 117430ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 117530ca2051SWilly Tarreau : "=r" (_num), "=r"(_arg4) \ 117630ca2051SWilly Tarreau : "0"(_num), \ 117730ca2051SWilly Tarreau "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5) \ 117830ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 117930ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 118030ca2051SWilly Tarreau ); \ 118130ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 118230ca2051SWilly Tarreau }) 118330ca2051SWilly Tarreau 118430ca2051SWilly Tarreau /* startup code, note that it's called __start on MIPS */ 118530ca2051SWilly Tarreau asm(".section .text\n" 118630ca2051SWilly Tarreau ".set nomips16\n" 118730ca2051SWilly Tarreau ".global __start\n" 118830ca2051SWilly Tarreau ".set noreorder\n" 118930ca2051SWilly Tarreau ".option pic0\n" 119030ca2051SWilly Tarreau ".ent __start\n" 119130ca2051SWilly Tarreau "__start:\n" 119230ca2051SWilly Tarreau "lw $a0,($sp)\n" // argc was in the stack 119330ca2051SWilly Tarreau "addiu $a1, $sp, 4\n" // argv = sp + 4 119430ca2051SWilly Tarreau "sll $a2, $a0, 2\n" // a2 = argc * 4 119530ca2051SWilly Tarreau "add $a2, $a2, $a1\n" // envp = argv + 4*argc ... 119630ca2051SWilly Tarreau "addiu $a2, $a2, 4\n" // ... + 4 119730ca2051SWilly Tarreau "li $t0, -8\n" 119830ca2051SWilly Tarreau "and $sp, $sp, $t0\n" // sp must be 8-byte aligned 119930ca2051SWilly Tarreau "addiu $sp,$sp,-16\n" // the callee expects to save a0..a3 there! 120030ca2051SWilly Tarreau "jal main\n" // main() returns the status code, we'll exit with it. 120130ca2051SWilly Tarreau "nop\n" // delayed slot 120230ca2051SWilly Tarreau "and $a0, $v0, 0xff\n" // limit exit code to 8 bits 120330ca2051SWilly Tarreau "li $v0, 4001\n" // NR_exit == 4001 120430ca2051SWilly Tarreau "syscall\n" 120530ca2051SWilly Tarreau ".end __start\n" 120630ca2051SWilly Tarreau ""); 120730ca2051SWilly Tarreau 120830ca2051SWilly Tarreau /* fcntl / open */ 120930ca2051SWilly Tarreau #define O_RDONLY 0 121030ca2051SWilly Tarreau #define O_WRONLY 1 121130ca2051SWilly Tarreau #define O_RDWR 2 121230ca2051SWilly Tarreau #define O_APPEND 0x0008 121330ca2051SWilly Tarreau #define O_NONBLOCK 0x0080 121430ca2051SWilly Tarreau #define O_CREAT 0x0100 121530ca2051SWilly Tarreau #define O_TRUNC 0x0200 121630ca2051SWilly Tarreau #define O_EXCL 0x0400 121730ca2051SWilly Tarreau #define O_NOCTTY 0x0800 121830ca2051SWilly Tarreau #define O_DIRECTORY 0x10000 121930ca2051SWilly Tarreau 122030ca2051SWilly Tarreau /* The struct returned by the stat() syscall. 88 bytes are returned by the 122130ca2051SWilly Tarreau * syscall. 122230ca2051SWilly Tarreau */ 122330ca2051SWilly Tarreau struct sys_stat_struct { 122430ca2051SWilly Tarreau unsigned int st_dev; 122530ca2051SWilly Tarreau long st_pad1[3]; 122630ca2051SWilly Tarreau unsigned long st_ino; 122730ca2051SWilly Tarreau unsigned int st_mode; 122830ca2051SWilly Tarreau unsigned int st_nlink; 122930ca2051SWilly Tarreau unsigned int st_uid; 123030ca2051SWilly Tarreau unsigned int st_gid; 123130ca2051SWilly Tarreau unsigned int st_rdev; 123230ca2051SWilly Tarreau long st_pad2[2]; 123330ca2051SWilly Tarreau long st_size; 123430ca2051SWilly Tarreau long st_pad3; 123530ca2051SWilly Tarreau long st_atime; 123630ca2051SWilly Tarreau long st_atime_nsec; 123730ca2051SWilly Tarreau long st_mtime; 123830ca2051SWilly Tarreau long st_mtime_nsec; 123930ca2051SWilly Tarreau long st_ctime; 124030ca2051SWilly Tarreau long st_ctime_nsec; 124130ca2051SWilly Tarreau long st_blksize; 124230ca2051SWilly Tarreau long st_blocks; 124330ca2051SWilly Tarreau long st_pad4[14]; 124430ca2051SWilly Tarreau }; 124530ca2051SWilly Tarreau 124630ca2051SWilly Tarreau #endif 124730ca2051SWilly Tarreau 124830ca2051SWilly Tarreau 124930ca2051SWilly Tarreau /* Below are the C functions used to declare the raw syscalls. They try to be 125030ca2051SWilly Tarreau * architecture-agnostic, and return either a success or -errno. Declaring them 125130ca2051SWilly Tarreau * static will lead to them being inlined in most cases, but it's still possible 125230ca2051SWilly Tarreau * to reference them by a pointer if needed. 125330ca2051SWilly Tarreau */ 125430ca2051SWilly Tarreau static __attribute__((unused)) 125530ca2051SWilly Tarreau void *sys_brk(void *addr) 125630ca2051SWilly Tarreau { 125730ca2051SWilly Tarreau return (void *)my_syscall1(__NR_brk, addr); 125830ca2051SWilly Tarreau } 125930ca2051SWilly Tarreau 126030ca2051SWilly Tarreau static __attribute__((noreturn,unused)) 126130ca2051SWilly Tarreau void sys_exit(int status) 126230ca2051SWilly Tarreau { 126330ca2051SWilly Tarreau my_syscall1(__NR_exit, status & 255); 126430ca2051SWilly Tarreau while(1); // shut the "noreturn" warnings. 126530ca2051SWilly Tarreau } 126630ca2051SWilly Tarreau 126730ca2051SWilly Tarreau static __attribute__((unused)) 126830ca2051SWilly Tarreau int sys_chdir(const char *path) 126930ca2051SWilly Tarreau { 127030ca2051SWilly Tarreau return my_syscall1(__NR_chdir, path); 127130ca2051SWilly Tarreau } 127230ca2051SWilly Tarreau 127330ca2051SWilly Tarreau static __attribute__((unused)) 127430ca2051SWilly Tarreau int sys_chmod(const char *path, mode_t mode) 127530ca2051SWilly Tarreau { 127630ca2051SWilly Tarreau #ifdef __NR_fchmodat 127730ca2051SWilly Tarreau return my_syscall4(__NR_fchmodat, AT_FDCWD, path, mode, 0); 127830ca2051SWilly Tarreau #else 127930ca2051SWilly Tarreau return my_syscall2(__NR_chmod, path, mode); 128030ca2051SWilly Tarreau #endif 128130ca2051SWilly Tarreau } 128230ca2051SWilly Tarreau 128330ca2051SWilly Tarreau static __attribute__((unused)) 128430ca2051SWilly Tarreau int sys_chown(const char *path, uid_t owner, gid_t group) 128530ca2051SWilly Tarreau { 128630ca2051SWilly Tarreau #ifdef __NR_fchownat 128730ca2051SWilly Tarreau return my_syscall5(__NR_fchownat, AT_FDCWD, path, owner, group, 0); 128830ca2051SWilly Tarreau #else 128930ca2051SWilly Tarreau return my_syscall3(__NR_chown, path, owner, group); 129030ca2051SWilly Tarreau #endif 129130ca2051SWilly Tarreau } 129230ca2051SWilly Tarreau 129330ca2051SWilly Tarreau static __attribute__((unused)) 129430ca2051SWilly Tarreau int sys_chroot(const char *path) 129530ca2051SWilly Tarreau { 129630ca2051SWilly Tarreau return my_syscall1(__NR_chroot, path); 129730ca2051SWilly Tarreau } 129830ca2051SWilly Tarreau 129930ca2051SWilly Tarreau static __attribute__((unused)) 130030ca2051SWilly Tarreau int sys_close(int fd) 130130ca2051SWilly Tarreau { 130230ca2051SWilly Tarreau return my_syscall1(__NR_close, fd); 130330ca2051SWilly Tarreau } 130430ca2051SWilly Tarreau 130530ca2051SWilly Tarreau static __attribute__((unused)) 130630ca2051SWilly Tarreau int sys_dup(int fd) 130730ca2051SWilly Tarreau { 130830ca2051SWilly Tarreau return my_syscall1(__NR_dup, fd); 130930ca2051SWilly Tarreau } 131030ca2051SWilly Tarreau 131130ca2051SWilly Tarreau static __attribute__((unused)) 131230ca2051SWilly Tarreau int sys_dup2(int old, int new) 131330ca2051SWilly Tarreau { 131430ca2051SWilly Tarreau return my_syscall2(__NR_dup2, old, new); 131530ca2051SWilly Tarreau } 131630ca2051SWilly Tarreau 131730ca2051SWilly Tarreau static __attribute__((unused)) 131830ca2051SWilly Tarreau int sys_execve(const char *filename, char *const argv[], char *const envp[]) 131930ca2051SWilly Tarreau { 132030ca2051SWilly Tarreau return my_syscall3(__NR_execve, filename, argv, envp); 132130ca2051SWilly Tarreau } 132230ca2051SWilly Tarreau 132330ca2051SWilly Tarreau static __attribute__((unused)) 132430ca2051SWilly Tarreau pid_t sys_fork(void) 132530ca2051SWilly Tarreau { 132630ca2051SWilly Tarreau return my_syscall0(__NR_fork); 132730ca2051SWilly Tarreau } 132830ca2051SWilly Tarreau 132930ca2051SWilly Tarreau static __attribute__((unused)) 133030ca2051SWilly Tarreau int sys_fsync(int fd) 133130ca2051SWilly Tarreau { 133230ca2051SWilly Tarreau return my_syscall1(__NR_fsync, fd); 133330ca2051SWilly Tarreau } 133430ca2051SWilly Tarreau 133530ca2051SWilly Tarreau static __attribute__((unused)) 133630ca2051SWilly Tarreau int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count) 133730ca2051SWilly Tarreau { 133830ca2051SWilly Tarreau return my_syscall3(__NR_getdents64, fd, dirp, count); 133930ca2051SWilly Tarreau } 134030ca2051SWilly Tarreau 134130ca2051SWilly Tarreau static __attribute__((unused)) 134230ca2051SWilly Tarreau pid_t sys_getpgrp(void) 134330ca2051SWilly Tarreau { 134430ca2051SWilly Tarreau return my_syscall0(__NR_getpgrp); 134530ca2051SWilly Tarreau } 134630ca2051SWilly Tarreau 134730ca2051SWilly Tarreau static __attribute__((unused)) 134830ca2051SWilly Tarreau pid_t sys_getpid(void) 134930ca2051SWilly Tarreau { 135030ca2051SWilly Tarreau return my_syscall0(__NR_getpid); 135130ca2051SWilly Tarreau } 135230ca2051SWilly Tarreau 135330ca2051SWilly Tarreau static __attribute__((unused)) 135430ca2051SWilly Tarreau int sys_gettimeofday(struct timeval *tv, struct timezone *tz) 135530ca2051SWilly Tarreau { 135630ca2051SWilly Tarreau return my_syscall2(__NR_gettimeofday, tv, tz); 135730ca2051SWilly Tarreau } 135830ca2051SWilly Tarreau 135930ca2051SWilly Tarreau static __attribute__((unused)) 136030ca2051SWilly Tarreau int sys_ioctl(int fd, unsigned long req, void *value) 136130ca2051SWilly Tarreau { 136230ca2051SWilly Tarreau return my_syscall3(__NR_ioctl, fd, req, value); 136330ca2051SWilly Tarreau } 136430ca2051SWilly Tarreau 136530ca2051SWilly Tarreau static __attribute__((unused)) 136630ca2051SWilly Tarreau int sys_kill(pid_t pid, int signal) 136730ca2051SWilly Tarreau { 136830ca2051SWilly Tarreau return my_syscall2(__NR_kill, pid, signal); 136930ca2051SWilly Tarreau } 137030ca2051SWilly Tarreau 137130ca2051SWilly Tarreau static __attribute__((unused)) 137230ca2051SWilly Tarreau int sys_link(const char *old, const char *new) 137330ca2051SWilly Tarreau { 137430ca2051SWilly Tarreau #ifdef __NR_linkat 137530ca2051SWilly Tarreau return my_syscall5(__NR_linkat, AT_FDCWD, old, AT_FDCWD, new, 0); 137630ca2051SWilly Tarreau #else 137730ca2051SWilly Tarreau return my_syscall2(__NR_link, old, new); 137830ca2051SWilly Tarreau #endif 137930ca2051SWilly Tarreau } 138030ca2051SWilly Tarreau 138130ca2051SWilly Tarreau static __attribute__((unused)) 138230ca2051SWilly Tarreau off_t sys_lseek(int fd, off_t offset, int whence) 138330ca2051SWilly Tarreau { 138430ca2051SWilly Tarreau return my_syscall3(__NR_lseek, fd, offset, whence); 138530ca2051SWilly Tarreau } 138630ca2051SWilly Tarreau 138730ca2051SWilly Tarreau static __attribute__((unused)) 138830ca2051SWilly Tarreau int sys_mkdir(const char *path, mode_t mode) 138930ca2051SWilly Tarreau { 139030ca2051SWilly Tarreau #ifdef __NR_mkdirat 139130ca2051SWilly Tarreau return my_syscall3(__NR_mkdirat, AT_FDCWD, path, mode); 139230ca2051SWilly Tarreau #else 139330ca2051SWilly Tarreau return my_syscall2(__NR_mkdir, path, mode); 139430ca2051SWilly Tarreau #endif 139530ca2051SWilly Tarreau } 139630ca2051SWilly Tarreau 139730ca2051SWilly Tarreau static __attribute__((unused)) 139830ca2051SWilly Tarreau long sys_mknod(const char *path, mode_t mode, dev_t dev) 139930ca2051SWilly Tarreau { 140030ca2051SWilly Tarreau #ifdef __NR_mknodat 140130ca2051SWilly Tarreau return my_syscall4(__NR_mknodat, AT_FDCWD, path, mode, dev); 140230ca2051SWilly Tarreau #else 140330ca2051SWilly Tarreau return my_syscall3(__NR_mknod, path, mode, dev); 140430ca2051SWilly Tarreau #endif 140530ca2051SWilly Tarreau } 140630ca2051SWilly Tarreau 140730ca2051SWilly Tarreau static __attribute__((unused)) 140830ca2051SWilly Tarreau int sys_mount(const char *src, const char *tgt, const char *fst, 140930ca2051SWilly Tarreau unsigned long flags, const void *data) 141030ca2051SWilly Tarreau { 141130ca2051SWilly Tarreau return my_syscall5(__NR_mount, src, tgt, fst, flags, data); 141230ca2051SWilly Tarreau } 141330ca2051SWilly Tarreau 141430ca2051SWilly Tarreau static __attribute__((unused)) 141530ca2051SWilly Tarreau int sys_open(const char *path, int flags, mode_t mode) 141630ca2051SWilly Tarreau { 141730ca2051SWilly Tarreau #ifdef __NR_openat 141830ca2051SWilly Tarreau return my_syscall4(__NR_openat, AT_FDCWD, path, flags, mode); 141930ca2051SWilly Tarreau #else 142030ca2051SWilly Tarreau return my_syscall3(__NR_open, path, flags, mode); 142130ca2051SWilly Tarreau #endif 142230ca2051SWilly Tarreau } 142330ca2051SWilly Tarreau 142430ca2051SWilly Tarreau static __attribute__((unused)) 142530ca2051SWilly Tarreau int sys_pivot_root(const char *new, const char *old) 142630ca2051SWilly Tarreau { 142730ca2051SWilly Tarreau return my_syscall2(__NR_pivot_root, new, old); 142830ca2051SWilly Tarreau } 142930ca2051SWilly Tarreau 143030ca2051SWilly Tarreau static __attribute__((unused)) 143130ca2051SWilly Tarreau int sys_poll(struct pollfd *fds, int nfds, int timeout) 143230ca2051SWilly Tarreau { 143330ca2051SWilly Tarreau return my_syscall3(__NR_poll, fds, nfds, timeout); 143430ca2051SWilly Tarreau } 143530ca2051SWilly Tarreau 143630ca2051SWilly Tarreau static __attribute__((unused)) 143730ca2051SWilly Tarreau ssize_t sys_read(int fd, void *buf, size_t count) 143830ca2051SWilly Tarreau { 143930ca2051SWilly Tarreau return my_syscall3(__NR_read, fd, buf, count); 144030ca2051SWilly Tarreau } 144130ca2051SWilly Tarreau 144230ca2051SWilly Tarreau static __attribute__((unused)) 144330ca2051SWilly Tarreau ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg) 144430ca2051SWilly Tarreau { 144530ca2051SWilly Tarreau return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg); 144630ca2051SWilly Tarreau } 144730ca2051SWilly Tarreau 144830ca2051SWilly Tarreau static __attribute__((unused)) 144930ca2051SWilly Tarreau int sys_sched_yield(void) 145030ca2051SWilly Tarreau { 145130ca2051SWilly Tarreau return my_syscall0(__NR_sched_yield); 145230ca2051SWilly Tarreau } 145330ca2051SWilly Tarreau 145430ca2051SWilly Tarreau static __attribute__((unused)) 145530ca2051SWilly Tarreau int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) 145630ca2051SWilly Tarreau { 145730ca2051SWilly Tarreau #if defined(__ARCH_WANT_SYS_OLD_SELECT) && !defined(__NR__newselect) 145830ca2051SWilly Tarreau struct sel_arg_struct { 145930ca2051SWilly Tarreau unsigned long n; 146030ca2051SWilly Tarreau fd_set *r, *w, *e; 146130ca2051SWilly Tarreau struct timeval *t; 146230ca2051SWilly Tarreau } arg = { .n = nfds, .r = rfds, .w = wfds, .e = efds, .t = timeout }; 146330ca2051SWilly Tarreau return my_syscall1(__NR_select, &arg); 146430ca2051SWilly Tarreau #elif defined(__ARCH_WANT_SYS_PSELECT6) && defined(__NR_pselect6) 146530ca2051SWilly Tarreau struct timespec t; 146630ca2051SWilly Tarreau 146730ca2051SWilly Tarreau if (timeout) { 146830ca2051SWilly Tarreau t.tv_sec = timeout->tv_sec; 146930ca2051SWilly Tarreau t.tv_nsec = timeout->tv_usec * 1000; 147030ca2051SWilly Tarreau } 147130ca2051SWilly Tarreau return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL); 147230ca2051SWilly Tarreau #else 147330ca2051SWilly Tarreau #ifndef __NR__newselect 147430ca2051SWilly Tarreau #define __NR__newselect __NR_select 147530ca2051SWilly Tarreau #endif 147630ca2051SWilly Tarreau return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout); 147730ca2051SWilly Tarreau #endif 147830ca2051SWilly Tarreau } 147930ca2051SWilly Tarreau 148030ca2051SWilly Tarreau static __attribute__((unused)) 148130ca2051SWilly Tarreau int sys_setpgid(pid_t pid, pid_t pgid) 148230ca2051SWilly Tarreau { 148330ca2051SWilly Tarreau return my_syscall2(__NR_setpgid, pid, pgid); 148430ca2051SWilly Tarreau } 148530ca2051SWilly Tarreau 148630ca2051SWilly Tarreau static __attribute__((unused)) 148730ca2051SWilly Tarreau pid_t sys_setsid(void) 148830ca2051SWilly Tarreau { 148930ca2051SWilly Tarreau return my_syscall0(__NR_setsid); 149030ca2051SWilly Tarreau } 149130ca2051SWilly Tarreau 149230ca2051SWilly Tarreau static __attribute__((unused)) 149330ca2051SWilly Tarreau int sys_stat(const char *path, struct stat *buf) 149430ca2051SWilly Tarreau { 149530ca2051SWilly Tarreau struct sys_stat_struct stat; 149630ca2051SWilly Tarreau long ret; 149730ca2051SWilly Tarreau 149830ca2051SWilly Tarreau #ifdef __NR_newfstatat 149930ca2051SWilly Tarreau /* only solution for arm64 */ 150030ca2051SWilly Tarreau ret = my_syscall4(__NR_newfstatat, AT_FDCWD, path, &stat, 0); 150130ca2051SWilly Tarreau #else 150230ca2051SWilly Tarreau ret = my_syscall2(__NR_stat, path, &stat); 150330ca2051SWilly Tarreau #endif 150430ca2051SWilly Tarreau buf->st_dev = stat.st_dev; 150530ca2051SWilly Tarreau buf->st_ino = stat.st_ino; 150630ca2051SWilly Tarreau buf->st_mode = stat.st_mode; 150730ca2051SWilly Tarreau buf->st_nlink = stat.st_nlink; 150830ca2051SWilly Tarreau buf->st_uid = stat.st_uid; 150930ca2051SWilly Tarreau buf->st_gid = stat.st_gid; 151030ca2051SWilly Tarreau buf->st_rdev = stat.st_rdev; 151130ca2051SWilly Tarreau buf->st_size = stat.st_size; 151230ca2051SWilly Tarreau buf->st_blksize = stat.st_blksize; 151330ca2051SWilly Tarreau buf->st_blocks = stat.st_blocks; 151430ca2051SWilly Tarreau buf->st_atime = stat.st_atime; 151530ca2051SWilly Tarreau buf->st_mtime = stat.st_mtime; 151630ca2051SWilly Tarreau buf->st_ctime = stat.st_ctime; 151730ca2051SWilly Tarreau return ret; 151830ca2051SWilly Tarreau } 151930ca2051SWilly Tarreau 152030ca2051SWilly Tarreau 152130ca2051SWilly Tarreau static __attribute__((unused)) 152230ca2051SWilly Tarreau int sys_symlink(const char *old, const char *new) 152330ca2051SWilly Tarreau { 152430ca2051SWilly Tarreau #ifdef __NR_symlinkat 152530ca2051SWilly Tarreau return my_syscall3(__NR_symlinkat, old, AT_FDCWD, new); 152630ca2051SWilly Tarreau #else 152730ca2051SWilly Tarreau return my_syscall2(__NR_symlink, old, new); 152830ca2051SWilly Tarreau #endif 152930ca2051SWilly Tarreau } 153030ca2051SWilly Tarreau 153130ca2051SWilly Tarreau static __attribute__((unused)) 153230ca2051SWilly Tarreau mode_t sys_umask(mode_t mode) 153330ca2051SWilly Tarreau { 153430ca2051SWilly Tarreau return my_syscall1(__NR_umask, mode); 153530ca2051SWilly Tarreau } 153630ca2051SWilly Tarreau 153730ca2051SWilly Tarreau static __attribute__((unused)) 153830ca2051SWilly Tarreau int sys_umount2(const char *path, int flags) 153930ca2051SWilly Tarreau { 154030ca2051SWilly Tarreau return my_syscall2(__NR_umount2, path, flags); 154130ca2051SWilly Tarreau } 154230ca2051SWilly Tarreau 154330ca2051SWilly Tarreau static __attribute__((unused)) 154430ca2051SWilly Tarreau int sys_unlink(const char *path) 154530ca2051SWilly Tarreau { 154630ca2051SWilly Tarreau #ifdef __NR_unlinkat 154730ca2051SWilly Tarreau return my_syscall3(__NR_unlinkat, AT_FDCWD, path, 0); 154830ca2051SWilly Tarreau #else 154930ca2051SWilly Tarreau return my_syscall1(__NR_unlink, path); 155030ca2051SWilly Tarreau #endif 155130ca2051SWilly Tarreau } 155230ca2051SWilly Tarreau 155330ca2051SWilly Tarreau static __attribute__((unused)) 155430ca2051SWilly Tarreau pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage) 155530ca2051SWilly Tarreau { 155630ca2051SWilly Tarreau return my_syscall4(__NR_wait4, pid, status, options, rusage); 155730ca2051SWilly Tarreau } 155830ca2051SWilly Tarreau 155930ca2051SWilly Tarreau static __attribute__((unused)) 156030ca2051SWilly Tarreau pid_t sys_waitpid(pid_t pid, int *status, int options) 156130ca2051SWilly Tarreau { 156230ca2051SWilly Tarreau return sys_wait4(pid, status, options, 0); 156330ca2051SWilly Tarreau } 156430ca2051SWilly Tarreau 156530ca2051SWilly Tarreau static __attribute__((unused)) 156630ca2051SWilly Tarreau pid_t sys_wait(int *status) 156730ca2051SWilly Tarreau { 156830ca2051SWilly Tarreau return sys_waitpid(-1, status, 0); 156930ca2051SWilly Tarreau } 157030ca2051SWilly Tarreau 157130ca2051SWilly Tarreau static __attribute__((unused)) 157230ca2051SWilly Tarreau ssize_t sys_write(int fd, const void *buf, size_t count) 157330ca2051SWilly Tarreau { 157430ca2051SWilly Tarreau return my_syscall3(__NR_write, fd, buf, count); 157530ca2051SWilly Tarreau } 157630ca2051SWilly Tarreau 157730ca2051SWilly Tarreau 157830ca2051SWilly Tarreau /* Below are the libc-compatible syscalls which return x or -1 and set errno. 157930ca2051SWilly Tarreau * They rely on the functions above. Similarly they're marked static so that it 158030ca2051SWilly Tarreau * is possible to assign pointers to them if needed. 158130ca2051SWilly Tarreau */ 158230ca2051SWilly Tarreau 158330ca2051SWilly Tarreau static __attribute__((unused)) 158430ca2051SWilly Tarreau int brk(void *addr) 158530ca2051SWilly Tarreau { 158630ca2051SWilly Tarreau void *ret = sys_brk(addr); 158730ca2051SWilly Tarreau 158830ca2051SWilly Tarreau if (!ret) { 158930ca2051SWilly Tarreau SET_ERRNO(ENOMEM); 159030ca2051SWilly Tarreau return -1; 159130ca2051SWilly Tarreau } 159230ca2051SWilly Tarreau return 0; 159330ca2051SWilly Tarreau } 159430ca2051SWilly Tarreau 159530ca2051SWilly Tarreau static __attribute__((noreturn,unused)) 159630ca2051SWilly Tarreau void exit(int status) 159730ca2051SWilly Tarreau { 159830ca2051SWilly Tarreau sys_exit(status); 159930ca2051SWilly Tarreau } 160030ca2051SWilly Tarreau 160130ca2051SWilly Tarreau static __attribute__((unused)) 160230ca2051SWilly Tarreau int chdir(const char *path) 160330ca2051SWilly Tarreau { 160430ca2051SWilly Tarreau int ret = sys_chdir(path); 160530ca2051SWilly Tarreau 160630ca2051SWilly Tarreau if (ret < 0) { 160730ca2051SWilly Tarreau SET_ERRNO(-ret); 160830ca2051SWilly Tarreau ret = -1; 160930ca2051SWilly Tarreau } 161030ca2051SWilly Tarreau return ret; 161130ca2051SWilly Tarreau } 161230ca2051SWilly Tarreau 161330ca2051SWilly Tarreau static __attribute__((unused)) 161430ca2051SWilly Tarreau int chmod(const char *path, mode_t mode) 161530ca2051SWilly Tarreau { 161630ca2051SWilly Tarreau int ret = sys_chmod(path, mode); 161730ca2051SWilly Tarreau 161830ca2051SWilly Tarreau if (ret < 0) { 161930ca2051SWilly Tarreau SET_ERRNO(-ret); 162030ca2051SWilly Tarreau ret = -1; 162130ca2051SWilly Tarreau } 162230ca2051SWilly Tarreau return ret; 162330ca2051SWilly Tarreau } 162430ca2051SWilly Tarreau 162530ca2051SWilly Tarreau static __attribute__((unused)) 162630ca2051SWilly Tarreau int chown(const char *path, uid_t owner, gid_t group) 162730ca2051SWilly Tarreau { 162830ca2051SWilly Tarreau int ret = sys_chown(path, owner, group); 162930ca2051SWilly Tarreau 163030ca2051SWilly Tarreau if (ret < 0) { 163130ca2051SWilly Tarreau SET_ERRNO(-ret); 163230ca2051SWilly Tarreau ret = -1; 163330ca2051SWilly Tarreau } 163430ca2051SWilly Tarreau return ret; 163530ca2051SWilly Tarreau } 163630ca2051SWilly Tarreau 163730ca2051SWilly Tarreau static __attribute__((unused)) 163830ca2051SWilly Tarreau int chroot(const char *path) 163930ca2051SWilly Tarreau { 164030ca2051SWilly Tarreau int ret = sys_chroot(path); 164130ca2051SWilly Tarreau 164230ca2051SWilly Tarreau if (ret < 0) { 164330ca2051SWilly Tarreau SET_ERRNO(-ret); 164430ca2051SWilly Tarreau ret = -1; 164530ca2051SWilly Tarreau } 164630ca2051SWilly Tarreau return ret; 164730ca2051SWilly Tarreau } 164830ca2051SWilly Tarreau 164930ca2051SWilly Tarreau static __attribute__((unused)) 165030ca2051SWilly Tarreau int close(int fd) 165130ca2051SWilly Tarreau { 165230ca2051SWilly Tarreau int ret = sys_close(fd); 165330ca2051SWilly Tarreau 165430ca2051SWilly Tarreau if (ret < 0) { 165530ca2051SWilly Tarreau SET_ERRNO(-ret); 165630ca2051SWilly Tarreau ret = -1; 165730ca2051SWilly Tarreau } 165830ca2051SWilly Tarreau return ret; 165930ca2051SWilly Tarreau } 166030ca2051SWilly Tarreau 166130ca2051SWilly Tarreau static __attribute__((unused)) 166230ca2051SWilly Tarreau int dup2(int old, int new) 166330ca2051SWilly Tarreau { 166430ca2051SWilly Tarreau int ret = sys_dup2(old, new); 166530ca2051SWilly Tarreau 166630ca2051SWilly Tarreau if (ret < 0) { 166730ca2051SWilly Tarreau SET_ERRNO(-ret); 166830ca2051SWilly Tarreau ret = -1; 166930ca2051SWilly Tarreau } 167030ca2051SWilly Tarreau return ret; 167130ca2051SWilly Tarreau } 167230ca2051SWilly Tarreau 167330ca2051SWilly Tarreau static __attribute__((unused)) 167430ca2051SWilly Tarreau int execve(const char *filename, char *const argv[], char *const envp[]) 167530ca2051SWilly Tarreau { 167630ca2051SWilly Tarreau int ret = sys_execve(filename, argv, envp); 167730ca2051SWilly Tarreau 167830ca2051SWilly Tarreau if (ret < 0) { 167930ca2051SWilly Tarreau SET_ERRNO(-ret); 168030ca2051SWilly Tarreau ret = -1; 168130ca2051SWilly Tarreau } 168230ca2051SWilly Tarreau return ret; 168330ca2051SWilly Tarreau } 168430ca2051SWilly Tarreau 168530ca2051SWilly Tarreau static __attribute__((unused)) 168630ca2051SWilly Tarreau pid_t fork(void) 168730ca2051SWilly Tarreau { 168830ca2051SWilly Tarreau pid_t ret = sys_fork(); 168930ca2051SWilly Tarreau 169030ca2051SWilly Tarreau if (ret < 0) { 169130ca2051SWilly Tarreau SET_ERRNO(-ret); 169230ca2051SWilly Tarreau ret = -1; 169330ca2051SWilly Tarreau } 169430ca2051SWilly Tarreau return ret; 169530ca2051SWilly Tarreau } 169630ca2051SWilly Tarreau 169730ca2051SWilly Tarreau static __attribute__((unused)) 169830ca2051SWilly Tarreau int fsync(int fd) 169930ca2051SWilly Tarreau { 170030ca2051SWilly Tarreau int ret = sys_fsync(fd); 170130ca2051SWilly Tarreau 170230ca2051SWilly Tarreau if (ret < 0) { 170330ca2051SWilly Tarreau SET_ERRNO(-ret); 170430ca2051SWilly Tarreau ret = -1; 170530ca2051SWilly Tarreau } 170630ca2051SWilly Tarreau return ret; 170730ca2051SWilly Tarreau } 170830ca2051SWilly Tarreau 170930ca2051SWilly Tarreau static __attribute__((unused)) 171030ca2051SWilly Tarreau int getdents64(int fd, struct linux_dirent64 *dirp, int count) 171130ca2051SWilly Tarreau { 171230ca2051SWilly Tarreau int ret = sys_getdents64(fd, dirp, count); 171330ca2051SWilly Tarreau 171430ca2051SWilly Tarreau if (ret < 0) { 171530ca2051SWilly Tarreau SET_ERRNO(-ret); 171630ca2051SWilly Tarreau ret = -1; 171730ca2051SWilly Tarreau } 171830ca2051SWilly Tarreau return ret; 171930ca2051SWilly Tarreau } 172030ca2051SWilly Tarreau 172130ca2051SWilly Tarreau static __attribute__((unused)) 172230ca2051SWilly Tarreau pid_t getpgrp(void) 172330ca2051SWilly Tarreau { 172430ca2051SWilly Tarreau pid_t ret = sys_getpgrp(); 172530ca2051SWilly Tarreau 172630ca2051SWilly Tarreau if (ret < 0) { 172730ca2051SWilly Tarreau SET_ERRNO(-ret); 172830ca2051SWilly Tarreau ret = -1; 172930ca2051SWilly Tarreau } 173030ca2051SWilly Tarreau return ret; 173130ca2051SWilly Tarreau } 173230ca2051SWilly Tarreau 173330ca2051SWilly Tarreau static __attribute__((unused)) 173430ca2051SWilly Tarreau pid_t getpid(void) 173530ca2051SWilly Tarreau { 173630ca2051SWilly Tarreau pid_t ret = sys_getpid(); 173730ca2051SWilly Tarreau 173830ca2051SWilly Tarreau if (ret < 0) { 173930ca2051SWilly Tarreau SET_ERRNO(-ret); 174030ca2051SWilly Tarreau ret = -1; 174130ca2051SWilly Tarreau } 174230ca2051SWilly Tarreau return ret; 174330ca2051SWilly Tarreau } 174430ca2051SWilly Tarreau 174530ca2051SWilly Tarreau static __attribute__((unused)) 174630ca2051SWilly Tarreau int gettimeofday(struct timeval *tv, struct timezone *tz) 174730ca2051SWilly Tarreau { 174830ca2051SWilly Tarreau int ret = sys_gettimeofday(tv, tz); 174930ca2051SWilly Tarreau 175030ca2051SWilly Tarreau if (ret < 0) { 175130ca2051SWilly Tarreau SET_ERRNO(-ret); 175230ca2051SWilly Tarreau ret = -1; 175330ca2051SWilly Tarreau } 175430ca2051SWilly Tarreau return ret; 175530ca2051SWilly Tarreau } 175630ca2051SWilly Tarreau 175730ca2051SWilly Tarreau static __attribute__((unused)) 175830ca2051SWilly Tarreau int ioctl(int fd, unsigned long req, void *value) 175930ca2051SWilly Tarreau { 176030ca2051SWilly Tarreau int ret = sys_ioctl(fd, req, value); 176130ca2051SWilly Tarreau 176230ca2051SWilly Tarreau if (ret < 0) { 176330ca2051SWilly Tarreau SET_ERRNO(-ret); 176430ca2051SWilly Tarreau ret = -1; 176530ca2051SWilly Tarreau } 176630ca2051SWilly Tarreau return ret; 176730ca2051SWilly Tarreau } 176830ca2051SWilly Tarreau 176930ca2051SWilly Tarreau static __attribute__((unused)) 177030ca2051SWilly Tarreau int kill(pid_t pid, int signal) 177130ca2051SWilly Tarreau { 177230ca2051SWilly Tarreau int ret = sys_kill(pid, signal); 177330ca2051SWilly Tarreau 177430ca2051SWilly Tarreau if (ret < 0) { 177530ca2051SWilly Tarreau SET_ERRNO(-ret); 177630ca2051SWilly Tarreau ret = -1; 177730ca2051SWilly Tarreau } 177830ca2051SWilly Tarreau return ret; 177930ca2051SWilly Tarreau } 178030ca2051SWilly Tarreau 178130ca2051SWilly Tarreau static __attribute__((unused)) 178230ca2051SWilly Tarreau int link(const char *old, const char *new) 178330ca2051SWilly Tarreau { 178430ca2051SWilly Tarreau int ret = sys_link(old, new); 178530ca2051SWilly Tarreau 178630ca2051SWilly Tarreau if (ret < 0) { 178730ca2051SWilly Tarreau SET_ERRNO(-ret); 178830ca2051SWilly Tarreau ret = -1; 178930ca2051SWilly Tarreau } 179030ca2051SWilly Tarreau return ret; 179130ca2051SWilly Tarreau } 179230ca2051SWilly Tarreau 179330ca2051SWilly Tarreau static __attribute__((unused)) 179430ca2051SWilly Tarreau off_t lseek(int fd, off_t offset, int whence) 179530ca2051SWilly Tarreau { 179630ca2051SWilly Tarreau off_t ret = sys_lseek(fd, offset, whence); 179730ca2051SWilly Tarreau 179830ca2051SWilly Tarreau if (ret < 0) { 179930ca2051SWilly Tarreau SET_ERRNO(-ret); 180030ca2051SWilly Tarreau ret = -1; 180130ca2051SWilly Tarreau } 180230ca2051SWilly Tarreau return ret; 180330ca2051SWilly Tarreau } 180430ca2051SWilly Tarreau 180530ca2051SWilly Tarreau static __attribute__((unused)) 180630ca2051SWilly Tarreau int mkdir(const char *path, mode_t mode) 180730ca2051SWilly Tarreau { 180830ca2051SWilly Tarreau int ret = sys_mkdir(path, mode); 180930ca2051SWilly Tarreau 181030ca2051SWilly Tarreau if (ret < 0) { 181130ca2051SWilly Tarreau SET_ERRNO(-ret); 181230ca2051SWilly Tarreau ret = -1; 181330ca2051SWilly Tarreau } 181430ca2051SWilly Tarreau return ret; 181530ca2051SWilly Tarreau } 181630ca2051SWilly Tarreau 181730ca2051SWilly Tarreau static __attribute__((unused)) 181830ca2051SWilly Tarreau int mknod(const char *path, mode_t mode, dev_t dev) 181930ca2051SWilly Tarreau { 182030ca2051SWilly Tarreau int ret = sys_mknod(path, mode, dev); 182130ca2051SWilly Tarreau 182230ca2051SWilly Tarreau if (ret < 0) { 182330ca2051SWilly Tarreau SET_ERRNO(-ret); 182430ca2051SWilly Tarreau ret = -1; 182530ca2051SWilly Tarreau } 182630ca2051SWilly Tarreau return ret; 182730ca2051SWilly Tarreau } 182830ca2051SWilly Tarreau 182930ca2051SWilly Tarreau static __attribute__((unused)) 183030ca2051SWilly Tarreau int mount(const char *src, const char *tgt, 183130ca2051SWilly Tarreau const char *fst, unsigned long flags, 183230ca2051SWilly Tarreau const void *data) 183330ca2051SWilly Tarreau { 183430ca2051SWilly Tarreau int ret = sys_mount(src, tgt, fst, flags, data); 183530ca2051SWilly Tarreau 183630ca2051SWilly Tarreau if (ret < 0) { 183730ca2051SWilly Tarreau SET_ERRNO(-ret); 183830ca2051SWilly Tarreau ret = -1; 183930ca2051SWilly Tarreau } 184030ca2051SWilly Tarreau return ret; 184130ca2051SWilly Tarreau } 184230ca2051SWilly Tarreau 184330ca2051SWilly Tarreau static __attribute__((unused)) 184430ca2051SWilly Tarreau int open(const char *path, int flags, mode_t mode) 184530ca2051SWilly Tarreau { 184630ca2051SWilly Tarreau int ret = sys_open(path, flags, mode); 184730ca2051SWilly Tarreau 184830ca2051SWilly Tarreau if (ret < 0) { 184930ca2051SWilly Tarreau SET_ERRNO(-ret); 185030ca2051SWilly Tarreau ret = -1; 185130ca2051SWilly Tarreau } 185230ca2051SWilly Tarreau return ret; 185330ca2051SWilly Tarreau } 185430ca2051SWilly Tarreau 185530ca2051SWilly Tarreau static __attribute__((unused)) 185630ca2051SWilly Tarreau int pivot_root(const char *new, const char *old) 185730ca2051SWilly Tarreau { 185830ca2051SWilly Tarreau int ret = sys_pivot_root(new, old); 185930ca2051SWilly Tarreau 186030ca2051SWilly Tarreau if (ret < 0) { 186130ca2051SWilly Tarreau SET_ERRNO(-ret); 186230ca2051SWilly Tarreau ret = -1; 186330ca2051SWilly Tarreau } 186430ca2051SWilly Tarreau return ret; 186530ca2051SWilly Tarreau } 186630ca2051SWilly Tarreau 186730ca2051SWilly Tarreau static __attribute__((unused)) 186830ca2051SWilly Tarreau int poll(struct pollfd *fds, int nfds, int timeout) 186930ca2051SWilly Tarreau { 187030ca2051SWilly Tarreau int ret = sys_poll(fds, nfds, timeout); 187130ca2051SWilly Tarreau 187230ca2051SWilly Tarreau if (ret < 0) { 187330ca2051SWilly Tarreau SET_ERRNO(-ret); 187430ca2051SWilly Tarreau ret = -1; 187530ca2051SWilly Tarreau } 187630ca2051SWilly Tarreau return ret; 187730ca2051SWilly Tarreau } 187830ca2051SWilly Tarreau 187930ca2051SWilly Tarreau static __attribute__((unused)) 188030ca2051SWilly Tarreau ssize_t read(int fd, void *buf, size_t count) 188130ca2051SWilly Tarreau { 188230ca2051SWilly Tarreau ssize_t ret = sys_read(fd, buf, count); 188330ca2051SWilly Tarreau 188430ca2051SWilly Tarreau if (ret < 0) { 188530ca2051SWilly Tarreau SET_ERRNO(-ret); 188630ca2051SWilly Tarreau ret = -1; 188730ca2051SWilly Tarreau } 188830ca2051SWilly Tarreau return ret; 188930ca2051SWilly Tarreau } 189030ca2051SWilly Tarreau 189130ca2051SWilly Tarreau static __attribute__((unused)) 189230ca2051SWilly Tarreau int reboot(int cmd) 189330ca2051SWilly Tarreau { 189430ca2051SWilly Tarreau int ret = sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0); 189530ca2051SWilly Tarreau 189630ca2051SWilly Tarreau if (ret < 0) { 189730ca2051SWilly Tarreau SET_ERRNO(-ret); 189830ca2051SWilly Tarreau ret = -1; 189930ca2051SWilly Tarreau } 190030ca2051SWilly Tarreau return ret; 190130ca2051SWilly Tarreau } 190230ca2051SWilly Tarreau 190330ca2051SWilly Tarreau static __attribute__((unused)) 190430ca2051SWilly Tarreau void *sbrk(intptr_t inc) 190530ca2051SWilly Tarreau { 190630ca2051SWilly Tarreau void *ret; 190730ca2051SWilly Tarreau 190830ca2051SWilly Tarreau /* first call to find current end */ 190930ca2051SWilly Tarreau if ((ret = sys_brk(0)) && (sys_brk(ret + inc) == ret + inc)) 191030ca2051SWilly Tarreau return ret + inc; 191130ca2051SWilly Tarreau 191230ca2051SWilly Tarreau SET_ERRNO(ENOMEM); 191330ca2051SWilly Tarreau return (void *)-1; 191430ca2051SWilly Tarreau } 191530ca2051SWilly Tarreau 191630ca2051SWilly Tarreau static __attribute__((unused)) 191730ca2051SWilly Tarreau int sched_yield(void) 191830ca2051SWilly Tarreau { 191930ca2051SWilly Tarreau int ret = sys_sched_yield(); 192030ca2051SWilly Tarreau 192130ca2051SWilly Tarreau if (ret < 0) { 192230ca2051SWilly Tarreau SET_ERRNO(-ret); 192330ca2051SWilly Tarreau ret = -1; 192430ca2051SWilly Tarreau } 192530ca2051SWilly Tarreau return ret; 192630ca2051SWilly Tarreau } 192730ca2051SWilly Tarreau 192830ca2051SWilly Tarreau static __attribute__((unused)) 192930ca2051SWilly Tarreau int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) 193030ca2051SWilly Tarreau { 193130ca2051SWilly Tarreau int ret = sys_select(nfds, rfds, wfds, efds, timeout); 193230ca2051SWilly Tarreau 193330ca2051SWilly Tarreau if (ret < 0) { 193430ca2051SWilly Tarreau SET_ERRNO(-ret); 193530ca2051SWilly Tarreau ret = -1; 193630ca2051SWilly Tarreau } 193730ca2051SWilly Tarreau return ret; 193830ca2051SWilly Tarreau } 193930ca2051SWilly Tarreau 194030ca2051SWilly Tarreau static __attribute__((unused)) 194130ca2051SWilly Tarreau int setpgid(pid_t pid, pid_t pgid) 194230ca2051SWilly Tarreau { 194330ca2051SWilly Tarreau int ret = sys_setpgid(pid, pgid); 194430ca2051SWilly Tarreau 194530ca2051SWilly Tarreau if (ret < 0) { 194630ca2051SWilly Tarreau SET_ERRNO(-ret); 194730ca2051SWilly Tarreau ret = -1; 194830ca2051SWilly Tarreau } 194930ca2051SWilly Tarreau return ret; 195030ca2051SWilly Tarreau } 195130ca2051SWilly Tarreau 195230ca2051SWilly Tarreau static __attribute__((unused)) 195330ca2051SWilly Tarreau pid_t setsid(void) 195430ca2051SWilly Tarreau { 195530ca2051SWilly Tarreau pid_t ret = sys_setsid(); 195630ca2051SWilly Tarreau 195730ca2051SWilly Tarreau if (ret < 0) { 195830ca2051SWilly Tarreau SET_ERRNO(-ret); 195930ca2051SWilly Tarreau ret = -1; 196030ca2051SWilly Tarreau } 196130ca2051SWilly Tarreau return ret; 196230ca2051SWilly Tarreau } 196330ca2051SWilly Tarreau 196430ca2051SWilly Tarreau static __attribute__((unused)) 196530ca2051SWilly Tarreau unsigned int sleep(unsigned int seconds) 196630ca2051SWilly Tarreau { 196730ca2051SWilly Tarreau struct timeval my_timeval = { seconds, 0 }; 196830ca2051SWilly Tarreau 196930ca2051SWilly Tarreau if (sys_select(0, 0, 0, 0, &my_timeval) < 0) 197030ca2051SWilly Tarreau return my_timeval.tv_sec + !!my_timeval.tv_usec; 197130ca2051SWilly Tarreau else 197230ca2051SWilly Tarreau return 0; 197330ca2051SWilly Tarreau } 197430ca2051SWilly Tarreau 197530ca2051SWilly Tarreau static __attribute__((unused)) 197630ca2051SWilly Tarreau int stat(const char *path, struct stat *buf) 197730ca2051SWilly Tarreau { 197830ca2051SWilly Tarreau int ret = sys_stat(path, buf); 197930ca2051SWilly Tarreau 198030ca2051SWilly Tarreau if (ret < 0) { 198130ca2051SWilly Tarreau SET_ERRNO(-ret); 198230ca2051SWilly Tarreau ret = -1; 198330ca2051SWilly Tarreau } 198430ca2051SWilly Tarreau return ret; 198530ca2051SWilly Tarreau } 198630ca2051SWilly Tarreau 198730ca2051SWilly Tarreau static __attribute__((unused)) 198830ca2051SWilly Tarreau int symlink(const char *old, const char *new) 198930ca2051SWilly Tarreau { 199030ca2051SWilly Tarreau int ret = sys_symlink(old, new); 199130ca2051SWilly Tarreau 199230ca2051SWilly Tarreau if (ret < 0) { 199330ca2051SWilly Tarreau SET_ERRNO(-ret); 199430ca2051SWilly Tarreau ret = -1; 199530ca2051SWilly Tarreau } 199630ca2051SWilly Tarreau return ret; 199730ca2051SWilly Tarreau } 199830ca2051SWilly Tarreau 199930ca2051SWilly Tarreau static __attribute__((unused)) 200030ca2051SWilly Tarreau int tcsetpgrp(int fd, pid_t pid) 200130ca2051SWilly Tarreau { 200230ca2051SWilly Tarreau return ioctl(fd, TIOCSPGRP, &pid); 200330ca2051SWilly Tarreau } 200430ca2051SWilly Tarreau 200530ca2051SWilly Tarreau static __attribute__((unused)) 200630ca2051SWilly Tarreau mode_t umask(mode_t mode) 200730ca2051SWilly Tarreau { 200830ca2051SWilly Tarreau return sys_umask(mode); 200930ca2051SWilly Tarreau } 201030ca2051SWilly Tarreau 201130ca2051SWilly Tarreau static __attribute__((unused)) 201230ca2051SWilly Tarreau int umount2(const char *path, int flags) 201330ca2051SWilly Tarreau { 201430ca2051SWilly Tarreau int ret = sys_umount2(path, flags); 201530ca2051SWilly Tarreau 201630ca2051SWilly Tarreau if (ret < 0) { 201730ca2051SWilly Tarreau SET_ERRNO(-ret); 201830ca2051SWilly Tarreau ret = -1; 201930ca2051SWilly Tarreau } 202030ca2051SWilly Tarreau return ret; 202130ca2051SWilly Tarreau } 202230ca2051SWilly Tarreau 202330ca2051SWilly Tarreau static __attribute__((unused)) 202430ca2051SWilly Tarreau int unlink(const char *path) 202530ca2051SWilly Tarreau { 202630ca2051SWilly Tarreau int ret = sys_unlink(path); 202730ca2051SWilly Tarreau 202830ca2051SWilly Tarreau if (ret < 0) { 202930ca2051SWilly Tarreau SET_ERRNO(-ret); 203030ca2051SWilly Tarreau ret = -1; 203130ca2051SWilly Tarreau } 203230ca2051SWilly Tarreau return ret; 203330ca2051SWilly Tarreau } 203430ca2051SWilly Tarreau 203530ca2051SWilly Tarreau static __attribute__((unused)) 203630ca2051SWilly Tarreau pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage) 203730ca2051SWilly Tarreau { 203830ca2051SWilly Tarreau pid_t ret = sys_wait4(pid, status, options, rusage); 203930ca2051SWilly Tarreau 204030ca2051SWilly Tarreau if (ret < 0) { 204130ca2051SWilly Tarreau SET_ERRNO(-ret); 204230ca2051SWilly Tarreau ret = -1; 204330ca2051SWilly Tarreau } 204430ca2051SWilly Tarreau return ret; 204530ca2051SWilly Tarreau } 204630ca2051SWilly Tarreau 204730ca2051SWilly Tarreau static __attribute__((unused)) 204830ca2051SWilly Tarreau pid_t waitpid(pid_t pid, int *status, int options) 204930ca2051SWilly Tarreau { 205030ca2051SWilly Tarreau pid_t ret = sys_waitpid(pid, status, options); 205130ca2051SWilly Tarreau 205230ca2051SWilly Tarreau if (ret < 0) { 205330ca2051SWilly Tarreau SET_ERRNO(-ret); 205430ca2051SWilly Tarreau ret = -1; 205530ca2051SWilly Tarreau } 205630ca2051SWilly Tarreau return ret; 205730ca2051SWilly Tarreau } 205830ca2051SWilly Tarreau 205930ca2051SWilly Tarreau static __attribute__((unused)) 206030ca2051SWilly Tarreau pid_t wait(int *status) 206130ca2051SWilly Tarreau { 206230ca2051SWilly Tarreau pid_t ret = sys_wait(status); 206330ca2051SWilly Tarreau 206430ca2051SWilly Tarreau if (ret < 0) { 206530ca2051SWilly Tarreau SET_ERRNO(-ret); 206630ca2051SWilly Tarreau ret = -1; 206730ca2051SWilly Tarreau } 206830ca2051SWilly Tarreau return ret; 206930ca2051SWilly Tarreau } 207030ca2051SWilly Tarreau 207130ca2051SWilly Tarreau static __attribute__((unused)) 207230ca2051SWilly Tarreau ssize_t write(int fd, const void *buf, size_t count) 207330ca2051SWilly Tarreau { 207430ca2051SWilly Tarreau ssize_t ret = sys_write(fd, buf, count); 207530ca2051SWilly Tarreau 207630ca2051SWilly Tarreau if (ret < 0) { 207730ca2051SWilly Tarreau SET_ERRNO(-ret); 207830ca2051SWilly Tarreau ret = -1; 207930ca2051SWilly Tarreau } 208030ca2051SWilly Tarreau return ret; 208130ca2051SWilly Tarreau } 208230ca2051SWilly Tarreau 208330ca2051SWilly Tarreau /* some size-optimized reimplementations of a few common str* and mem* 208430ca2051SWilly Tarreau * functions. They're marked static, except memcpy() and raise() which are used 208530ca2051SWilly Tarreau * by libgcc on ARM, so they are marked weak instead in order not to cause an 208630ca2051SWilly Tarreau * error when building a program made of multiple files (not recommended). 208730ca2051SWilly Tarreau */ 208830ca2051SWilly Tarreau 208930ca2051SWilly Tarreau static __attribute__((unused)) 209030ca2051SWilly Tarreau void *memmove(void *dst, const void *src, size_t len) 209130ca2051SWilly Tarreau { 209230ca2051SWilly Tarreau ssize_t pos = (dst <= src) ? -1 : (long)len; 209330ca2051SWilly Tarreau void *ret = dst; 209430ca2051SWilly Tarreau 209530ca2051SWilly Tarreau while (len--) { 209630ca2051SWilly Tarreau pos += (dst <= src) ? 1 : -1; 209730ca2051SWilly Tarreau ((char *)dst)[pos] = ((char *)src)[pos]; 209830ca2051SWilly Tarreau } 209930ca2051SWilly Tarreau return ret; 210030ca2051SWilly Tarreau } 210130ca2051SWilly Tarreau 210230ca2051SWilly Tarreau static __attribute__((unused)) 210330ca2051SWilly Tarreau void *memset(void *dst, int b, size_t len) 210430ca2051SWilly Tarreau { 210530ca2051SWilly Tarreau char *p = dst; 210630ca2051SWilly Tarreau 210730ca2051SWilly Tarreau while (len--) 210830ca2051SWilly Tarreau *(p++) = b; 210930ca2051SWilly Tarreau return dst; 211030ca2051SWilly Tarreau } 211130ca2051SWilly Tarreau 211230ca2051SWilly Tarreau static __attribute__((unused)) 211330ca2051SWilly Tarreau int memcmp(const void *s1, const void *s2, size_t n) 211430ca2051SWilly Tarreau { 211530ca2051SWilly Tarreau size_t ofs = 0; 211630ca2051SWilly Tarreau char c1 = 0; 211730ca2051SWilly Tarreau 211830ca2051SWilly Tarreau while (ofs < n && !(c1 = ((char *)s1)[ofs] - ((char *)s2)[ofs])) { 211930ca2051SWilly Tarreau ofs++; 212030ca2051SWilly Tarreau } 212130ca2051SWilly Tarreau return c1; 212230ca2051SWilly Tarreau } 212330ca2051SWilly Tarreau 212430ca2051SWilly Tarreau static __attribute__((unused)) 212530ca2051SWilly Tarreau char *strcpy(char *dst, const char *src) 212630ca2051SWilly Tarreau { 212730ca2051SWilly Tarreau char *ret = dst; 212830ca2051SWilly Tarreau 212930ca2051SWilly Tarreau while ((*dst++ = *src++)); 213030ca2051SWilly Tarreau return ret; 213130ca2051SWilly Tarreau } 213230ca2051SWilly Tarreau 213330ca2051SWilly Tarreau static __attribute__((unused)) 213430ca2051SWilly Tarreau char *strchr(const char *s, int c) 213530ca2051SWilly Tarreau { 213630ca2051SWilly Tarreau while (*s) { 213730ca2051SWilly Tarreau if (*s == (char)c) 213830ca2051SWilly Tarreau return (char *)s; 213930ca2051SWilly Tarreau s++; 214030ca2051SWilly Tarreau } 214130ca2051SWilly Tarreau return NULL; 214230ca2051SWilly Tarreau } 214330ca2051SWilly Tarreau 214430ca2051SWilly Tarreau static __attribute__((unused)) 214530ca2051SWilly Tarreau char *strrchr(const char *s, int c) 214630ca2051SWilly Tarreau { 214730ca2051SWilly Tarreau const char *ret = NULL; 214830ca2051SWilly Tarreau 214930ca2051SWilly Tarreau while (*s) { 215030ca2051SWilly Tarreau if (*s == (char)c) 215130ca2051SWilly Tarreau ret = s; 215230ca2051SWilly Tarreau s++; 215330ca2051SWilly Tarreau } 215430ca2051SWilly Tarreau return (char *)ret; 215530ca2051SWilly Tarreau } 215630ca2051SWilly Tarreau 215730ca2051SWilly Tarreau static __attribute__((unused)) 215830ca2051SWilly Tarreau size_t nolibc_strlen(const char *str) 215930ca2051SWilly Tarreau { 216030ca2051SWilly Tarreau size_t len; 216130ca2051SWilly Tarreau 216230ca2051SWilly Tarreau for (len = 0; str[len]; len++); 216330ca2051SWilly Tarreau return len; 216430ca2051SWilly Tarreau } 216530ca2051SWilly Tarreau 216630ca2051SWilly Tarreau #define strlen(str) ({ \ 216730ca2051SWilly Tarreau __builtin_constant_p((str)) ? \ 216830ca2051SWilly Tarreau __builtin_strlen((str)) : \ 216930ca2051SWilly Tarreau nolibc_strlen((str)); \ 217030ca2051SWilly Tarreau }) 217130ca2051SWilly Tarreau 217230ca2051SWilly Tarreau static __attribute__((unused)) 217330ca2051SWilly Tarreau int isdigit(int c) 217430ca2051SWilly Tarreau { 217530ca2051SWilly Tarreau return (unsigned int)(c - '0') <= 9; 217630ca2051SWilly Tarreau } 217730ca2051SWilly Tarreau 217830ca2051SWilly Tarreau static __attribute__((unused)) 217930ca2051SWilly Tarreau long atol(const char *s) 218030ca2051SWilly Tarreau { 218130ca2051SWilly Tarreau unsigned long ret = 0; 218230ca2051SWilly Tarreau unsigned long d; 218330ca2051SWilly Tarreau int neg = 0; 218430ca2051SWilly Tarreau 218530ca2051SWilly Tarreau if (*s == '-') { 218630ca2051SWilly Tarreau neg = 1; 218730ca2051SWilly Tarreau s++; 218830ca2051SWilly Tarreau } 218930ca2051SWilly Tarreau 219030ca2051SWilly Tarreau while (1) { 219130ca2051SWilly Tarreau d = (*s++) - '0'; 219230ca2051SWilly Tarreau if (d > 9) 219330ca2051SWilly Tarreau break; 219430ca2051SWilly Tarreau ret *= 10; 219530ca2051SWilly Tarreau ret += d; 219630ca2051SWilly Tarreau } 219730ca2051SWilly Tarreau 219830ca2051SWilly Tarreau return neg ? -ret : ret; 219930ca2051SWilly Tarreau } 220030ca2051SWilly Tarreau 220130ca2051SWilly Tarreau static __attribute__((unused)) 220230ca2051SWilly Tarreau int atoi(const char *s) 220330ca2051SWilly Tarreau { 220430ca2051SWilly Tarreau return atol(s); 220530ca2051SWilly Tarreau } 220630ca2051SWilly Tarreau 220730ca2051SWilly Tarreau static __attribute__((unused)) 220830ca2051SWilly Tarreau const char *ltoa(long in) 220930ca2051SWilly Tarreau { 221030ca2051SWilly Tarreau /* large enough for -9223372036854775808 */ 221130ca2051SWilly Tarreau static char buffer[21]; 221230ca2051SWilly Tarreau char *pos = buffer + sizeof(buffer) - 1; 221330ca2051SWilly Tarreau int neg = in < 0; 221430ca2051SWilly Tarreau unsigned long n = neg ? -in : in; 221530ca2051SWilly Tarreau 221630ca2051SWilly Tarreau *pos-- = '\0'; 221730ca2051SWilly Tarreau do { 221830ca2051SWilly Tarreau *pos-- = '0' + n % 10; 221930ca2051SWilly Tarreau n /= 10; 222030ca2051SWilly Tarreau if (pos < buffer) 222130ca2051SWilly Tarreau return pos + 1; 222230ca2051SWilly Tarreau } while (n); 222330ca2051SWilly Tarreau 222430ca2051SWilly Tarreau if (neg) 222530ca2051SWilly Tarreau *pos-- = '-'; 222630ca2051SWilly Tarreau return pos + 1; 222730ca2051SWilly Tarreau } 222830ca2051SWilly Tarreau 222930ca2051SWilly Tarreau __attribute__((weak,unused)) 223030ca2051SWilly Tarreau void *memcpy(void *dst, const void *src, size_t len) 223130ca2051SWilly Tarreau { 223230ca2051SWilly Tarreau return memmove(dst, src, len); 223330ca2051SWilly Tarreau } 223430ca2051SWilly Tarreau 223530ca2051SWilly Tarreau /* needed by libgcc for divide by zero */ 223630ca2051SWilly Tarreau __attribute__((weak,unused)) 223730ca2051SWilly Tarreau int raise(int signal) 223830ca2051SWilly Tarreau { 223930ca2051SWilly Tarreau return kill(getpid(), signal); 224030ca2051SWilly Tarreau } 224130ca2051SWilly Tarreau 224230ca2051SWilly Tarreau /* Here come a few helper functions */ 224330ca2051SWilly Tarreau 224430ca2051SWilly Tarreau static __attribute__((unused)) 224530ca2051SWilly Tarreau void FD_ZERO(fd_set *set) 224630ca2051SWilly Tarreau { 224730ca2051SWilly Tarreau memset(set, 0, sizeof(*set)); 224830ca2051SWilly Tarreau } 224930ca2051SWilly Tarreau 225030ca2051SWilly Tarreau static __attribute__((unused)) 225130ca2051SWilly Tarreau void FD_SET(int fd, fd_set *set) 225230ca2051SWilly Tarreau { 225330ca2051SWilly Tarreau if (fd < 0 || fd >= FD_SETSIZE) 225430ca2051SWilly Tarreau return; 225530ca2051SWilly Tarreau set->fd32[fd / 32] |= 1 << (fd & 31); 225630ca2051SWilly Tarreau } 225730ca2051SWilly Tarreau 225830ca2051SWilly Tarreau /* WARNING, it only deals with the 4096 first majors and 256 first minors */ 225930ca2051SWilly Tarreau static __attribute__((unused)) 226030ca2051SWilly Tarreau dev_t makedev(unsigned int major, unsigned int minor) 226130ca2051SWilly Tarreau { 226230ca2051SWilly Tarreau return ((major & 0xfff) << 8) | (minor & 0xff); 226330ca2051SWilly Tarreau } 2264