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 \ 74*3c6ce7a5SWilly Tarreau * -static -include nolibc.h -o hello hello.c -lgcc 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 #include <asm/unistd.h> 8530ca2051SWilly Tarreau #include <asm/ioctls.h> 8630ca2051SWilly Tarreau #include <asm/errno.h> 8730ca2051SWilly Tarreau #include <linux/fs.h> 8830ca2051SWilly Tarreau #include <linux/loop.h> 8970ca7aeaSWilly Tarreau #include <linux/time.h> 9030ca2051SWilly Tarreau 9130ca2051SWilly Tarreau #define NOLIBC 9230ca2051SWilly Tarreau 9330ca2051SWilly Tarreau /* this way it will be removed if unused */ 9430ca2051SWilly Tarreau static int errno; 9530ca2051SWilly Tarreau 9630ca2051SWilly Tarreau #ifndef NOLIBC_IGNORE_ERRNO 9730ca2051SWilly Tarreau #define SET_ERRNO(v) do { errno = (v); } while (0) 9830ca2051SWilly Tarreau #else 9930ca2051SWilly Tarreau #define SET_ERRNO(v) do { } while (0) 10030ca2051SWilly Tarreau #endif 10130ca2051SWilly Tarreau 10230ca2051SWilly Tarreau /* errno codes all ensure that they will not conflict with a valid pointer 10306dc8d45SBhaskar Chowdhury * because they all correspond to the highest addressable memory page. 10430ca2051SWilly Tarreau */ 10530ca2051SWilly Tarreau #define MAX_ERRNO 4095 10630ca2051SWilly Tarreau 10730ca2051SWilly Tarreau /* Declare a few quite common macros and types that usually are in stdlib.h, 10830ca2051SWilly Tarreau * stdint.h, ctype.h, unistd.h and a few other common locations. 10930ca2051SWilly Tarreau */ 11030ca2051SWilly Tarreau 11130ca2051SWilly Tarreau #define NULL ((void *)0) 11230ca2051SWilly Tarreau 11330ca2051SWilly Tarreau /* stdint types */ 11430ca2051SWilly Tarreau typedef unsigned char uint8_t; 11530ca2051SWilly Tarreau typedef signed char int8_t; 11630ca2051SWilly Tarreau typedef unsigned short uint16_t; 11730ca2051SWilly Tarreau typedef signed short int16_t; 11830ca2051SWilly Tarreau typedef unsigned int uint32_t; 11930ca2051SWilly Tarreau typedef signed int int32_t; 12030ca2051SWilly Tarreau typedef unsigned long long uint64_t; 12130ca2051SWilly Tarreau typedef signed long long int64_t; 12230ca2051SWilly Tarreau typedef unsigned long size_t; 12330ca2051SWilly Tarreau typedef signed long ssize_t; 12430ca2051SWilly Tarreau typedef unsigned long uintptr_t; 12530ca2051SWilly Tarreau typedef signed long intptr_t; 12630ca2051SWilly Tarreau typedef signed long ptrdiff_t; 12730ca2051SWilly Tarreau 12830ca2051SWilly Tarreau /* for stat() */ 12930ca2051SWilly Tarreau typedef unsigned int dev_t; 13030ca2051SWilly Tarreau typedef unsigned long ino_t; 13130ca2051SWilly Tarreau typedef unsigned int mode_t; 13230ca2051SWilly Tarreau typedef signed int pid_t; 13330ca2051SWilly Tarreau typedef unsigned int uid_t; 13430ca2051SWilly Tarreau typedef unsigned int gid_t; 13530ca2051SWilly Tarreau typedef unsigned long nlink_t; 13630ca2051SWilly Tarreau typedef signed long off_t; 13730ca2051SWilly Tarreau typedef signed long blksize_t; 13830ca2051SWilly Tarreau typedef signed long blkcnt_t; 13930ca2051SWilly Tarreau typedef signed long time_t; 14030ca2051SWilly Tarreau 14130ca2051SWilly Tarreau /* for poll() */ 14230ca2051SWilly Tarreau struct pollfd { 14330ca2051SWilly Tarreau int fd; 14430ca2051SWilly Tarreau short int events; 14530ca2051SWilly Tarreau short int revents; 14630ca2051SWilly Tarreau }; 14730ca2051SWilly Tarreau 14830ca2051SWilly Tarreau /* for getdents64() */ 14930ca2051SWilly Tarreau struct linux_dirent64 { 15030ca2051SWilly Tarreau uint64_t d_ino; 15130ca2051SWilly Tarreau int64_t d_off; 15230ca2051SWilly Tarreau unsigned short d_reclen; 15330ca2051SWilly Tarreau unsigned char d_type; 15430ca2051SWilly Tarreau char d_name[]; 15530ca2051SWilly Tarreau }; 15630ca2051SWilly Tarreau 15730ca2051SWilly Tarreau /* commonly an fd_set represents 256 FDs */ 15830ca2051SWilly Tarreau #define FD_SETSIZE 256 15930ca2051SWilly Tarreau typedef struct { uint32_t fd32[FD_SETSIZE/32]; } fd_set; 16030ca2051SWilly Tarreau 16130ca2051SWilly Tarreau /* needed by wait4() */ 16230ca2051SWilly Tarreau struct rusage { 16330ca2051SWilly Tarreau struct timeval ru_utime; 16430ca2051SWilly Tarreau struct timeval ru_stime; 16530ca2051SWilly Tarreau long ru_maxrss; 16630ca2051SWilly Tarreau long ru_ixrss; 16730ca2051SWilly Tarreau long ru_idrss; 16830ca2051SWilly Tarreau long ru_isrss; 16930ca2051SWilly Tarreau long ru_minflt; 17030ca2051SWilly Tarreau long ru_majflt; 17130ca2051SWilly Tarreau long ru_nswap; 17230ca2051SWilly Tarreau long ru_inblock; 17330ca2051SWilly Tarreau long ru_oublock; 17430ca2051SWilly Tarreau long ru_msgsnd; 17530ca2051SWilly Tarreau long ru_msgrcv; 17630ca2051SWilly Tarreau long ru_nsignals; 17730ca2051SWilly Tarreau long ru_nvcsw; 17830ca2051SWilly Tarreau long ru_nivcsw; 17930ca2051SWilly Tarreau }; 18030ca2051SWilly Tarreau 18130ca2051SWilly Tarreau /* stat flags (WARNING, octal here) */ 18230ca2051SWilly Tarreau #define S_IFDIR 0040000 18330ca2051SWilly Tarreau #define S_IFCHR 0020000 18430ca2051SWilly Tarreau #define S_IFBLK 0060000 18530ca2051SWilly Tarreau #define S_IFREG 0100000 18630ca2051SWilly Tarreau #define S_IFIFO 0010000 18730ca2051SWilly Tarreau #define S_IFLNK 0120000 18830ca2051SWilly Tarreau #define S_IFSOCK 0140000 18930ca2051SWilly Tarreau #define S_IFMT 0170000 19030ca2051SWilly Tarreau 19130ca2051SWilly Tarreau #define S_ISDIR(mode) (((mode) & S_IFDIR) == S_IFDIR) 19230ca2051SWilly Tarreau #define S_ISCHR(mode) (((mode) & S_IFCHR) == S_IFCHR) 19330ca2051SWilly Tarreau #define S_ISBLK(mode) (((mode) & S_IFBLK) == S_IFBLK) 19430ca2051SWilly Tarreau #define S_ISREG(mode) (((mode) & S_IFREG) == S_IFREG) 19530ca2051SWilly Tarreau #define S_ISFIFO(mode) (((mode) & S_IFIFO) == S_IFIFO) 19630ca2051SWilly Tarreau #define S_ISLNK(mode) (((mode) & S_IFLNK) == S_IFLNK) 19730ca2051SWilly Tarreau #define S_ISSOCK(mode) (((mode) & S_IFSOCK) == S_IFSOCK) 19830ca2051SWilly Tarreau 19930ca2051SWilly Tarreau #define DT_UNKNOWN 0 20030ca2051SWilly Tarreau #define DT_FIFO 1 20130ca2051SWilly Tarreau #define DT_CHR 2 20230ca2051SWilly Tarreau #define DT_DIR 4 20330ca2051SWilly Tarreau #define DT_BLK 6 20430ca2051SWilly Tarreau #define DT_REG 8 20530ca2051SWilly Tarreau #define DT_LNK 10 20630ca2051SWilly Tarreau #define DT_SOCK 12 20730ca2051SWilly Tarreau 20830ca2051SWilly Tarreau /* all the *at functions */ 2096c5b9de2SSamuel Hernandez #ifndef AT_FDCWD 21030ca2051SWilly Tarreau #define AT_FDCWD -100 21130ca2051SWilly Tarreau #endif 21230ca2051SWilly Tarreau 21330ca2051SWilly Tarreau /* lseek */ 21430ca2051SWilly Tarreau #define SEEK_SET 0 21530ca2051SWilly Tarreau #define SEEK_CUR 1 21630ca2051SWilly Tarreau #define SEEK_END 2 21730ca2051SWilly Tarreau 21830ca2051SWilly Tarreau /* reboot */ 21930ca2051SWilly Tarreau #define LINUX_REBOOT_MAGIC1 0xfee1dead 22030ca2051SWilly Tarreau #define LINUX_REBOOT_MAGIC2 0x28121969 22130ca2051SWilly Tarreau #define LINUX_REBOOT_CMD_HALT 0xcdef0123 22230ca2051SWilly Tarreau #define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc 22330ca2051SWilly Tarreau #define LINUX_REBOOT_CMD_RESTART 0x01234567 22430ca2051SWilly Tarreau #define LINUX_REBOOT_CMD_SW_SUSPEND 0xd000fce2 22530ca2051SWilly Tarreau 22630ca2051SWilly Tarreau 22730ca2051SWilly Tarreau /* The format of the struct as returned by the libc to the application, which 22830ca2051SWilly Tarreau * significantly differs from the format returned by the stat() syscall flavours. 22930ca2051SWilly Tarreau */ 23030ca2051SWilly Tarreau struct stat { 23130ca2051SWilly Tarreau dev_t st_dev; /* ID of device containing file */ 23230ca2051SWilly Tarreau ino_t st_ino; /* inode number */ 23330ca2051SWilly Tarreau mode_t st_mode; /* protection */ 23430ca2051SWilly Tarreau nlink_t st_nlink; /* number of hard links */ 23530ca2051SWilly Tarreau uid_t st_uid; /* user ID of owner */ 23630ca2051SWilly Tarreau gid_t st_gid; /* group ID of owner */ 23730ca2051SWilly Tarreau dev_t st_rdev; /* device ID (if special file) */ 23830ca2051SWilly Tarreau off_t st_size; /* total size, in bytes */ 23930ca2051SWilly Tarreau blksize_t st_blksize; /* blocksize for file system I/O */ 24030ca2051SWilly Tarreau blkcnt_t st_blocks; /* number of 512B blocks allocated */ 24130ca2051SWilly Tarreau time_t st_atime; /* time of last access */ 24230ca2051SWilly Tarreau time_t st_mtime; /* time of last modification */ 24330ca2051SWilly Tarreau time_t st_ctime; /* time of last status change */ 24430ca2051SWilly Tarreau }; 24530ca2051SWilly Tarreau 24630ca2051SWilly Tarreau #define WEXITSTATUS(status) (((status) & 0xff00) >> 8) 24730ca2051SWilly Tarreau #define WIFEXITED(status) (((status) & 0x7f) == 0) 24830ca2051SWilly Tarreau 249be60ca41SWilly Tarreau /* for SIGCHLD */ 250be60ca41SWilly Tarreau #include <asm/signal.h> 25130ca2051SWilly Tarreau 25230ca2051SWilly Tarreau /* Below comes the architecture-specific code. For each architecture, we have 25330ca2051SWilly Tarreau * the syscall declarations and the _start code definition. This is the only 25430ca2051SWilly Tarreau * global part. On all architectures the kernel puts everything in the stack 25530ca2051SWilly Tarreau * before jumping to _start just above us, without any return address (_start 25630ca2051SWilly Tarreau * is not a function but an entry pint). So at the stack pointer we find argc. 25730ca2051SWilly Tarreau * Then argv[] begins, and ends at the first NULL. Then we have envp which 25830ca2051SWilly Tarreau * starts and ends with a NULL as well. So envp=argv+argc+1. 25930ca2051SWilly Tarreau */ 26030ca2051SWilly Tarreau 26130ca2051SWilly Tarreau #if defined(__x86_64__) 26230ca2051SWilly Tarreau /* Syscalls for x86_64 : 26330ca2051SWilly Tarreau * - registers are 64-bit 26430ca2051SWilly Tarreau * - syscall number is passed in rax 26530ca2051SWilly Tarreau * - arguments are in rdi, rsi, rdx, r10, r8, r9 respectively 26630ca2051SWilly Tarreau * - the system call is performed by calling the syscall instruction 26730ca2051SWilly Tarreau * - syscall return comes in rax 26830ca2051SWilly Tarreau * - rcx and r8..r11 may be clobbered, others are preserved. 26930ca2051SWilly Tarreau * - the arguments are cast to long and assigned into the target registers 27030ca2051SWilly Tarreau * which are then simply passed as registers to the asm code, so that we 27130ca2051SWilly Tarreau * don't have to experience issues with register constraints. 27230ca2051SWilly Tarreau * - the syscall number is always specified last in order to allow to force 27330ca2051SWilly Tarreau * some registers before (gcc refuses a %-register at the last position). 27430ca2051SWilly Tarreau */ 27530ca2051SWilly Tarreau 27630ca2051SWilly Tarreau #define my_syscall0(num) \ 27730ca2051SWilly Tarreau ({ \ 27830ca2051SWilly Tarreau long _ret; \ 27930ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 28030ca2051SWilly Tarreau \ 28130ca2051SWilly Tarreau asm volatile ( \ 28230ca2051SWilly Tarreau "syscall\n" \ 28330ca2051SWilly Tarreau : "=a" (_ret) \ 28430ca2051SWilly Tarreau : "0"(_num) \ 28530ca2051SWilly Tarreau : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \ 28630ca2051SWilly Tarreau ); \ 28730ca2051SWilly Tarreau _ret; \ 28830ca2051SWilly Tarreau }) 28930ca2051SWilly Tarreau 29030ca2051SWilly Tarreau #define my_syscall1(num, arg1) \ 29130ca2051SWilly Tarreau ({ \ 29230ca2051SWilly Tarreau long _ret; \ 29330ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 29430ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 29530ca2051SWilly Tarreau \ 29630ca2051SWilly Tarreau asm volatile ( \ 29730ca2051SWilly Tarreau "syscall\n" \ 29830ca2051SWilly Tarreau : "=a" (_ret) \ 29930ca2051SWilly Tarreau : "r"(_arg1), \ 30030ca2051SWilly Tarreau "0"(_num) \ 30130ca2051SWilly Tarreau : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \ 30230ca2051SWilly Tarreau ); \ 30330ca2051SWilly Tarreau _ret; \ 30430ca2051SWilly Tarreau }) 30530ca2051SWilly Tarreau 30630ca2051SWilly Tarreau #define my_syscall2(num, arg1, arg2) \ 30730ca2051SWilly Tarreau ({ \ 30830ca2051SWilly Tarreau long _ret; \ 30930ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 31030ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 31130ca2051SWilly Tarreau register long _arg2 asm("rsi") = (long)(arg2); \ 31230ca2051SWilly Tarreau \ 31330ca2051SWilly Tarreau asm volatile ( \ 31430ca2051SWilly Tarreau "syscall\n" \ 31530ca2051SWilly Tarreau : "=a" (_ret) \ 31630ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), \ 31730ca2051SWilly Tarreau "0"(_num) \ 31830ca2051SWilly Tarreau : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \ 31930ca2051SWilly Tarreau ); \ 32030ca2051SWilly Tarreau _ret; \ 32130ca2051SWilly Tarreau }) 32230ca2051SWilly Tarreau 32330ca2051SWilly Tarreau #define my_syscall3(num, arg1, arg2, arg3) \ 32430ca2051SWilly Tarreau ({ \ 32530ca2051SWilly Tarreau long _ret; \ 32630ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 32730ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 32830ca2051SWilly Tarreau register long _arg2 asm("rsi") = (long)(arg2); \ 32930ca2051SWilly Tarreau register long _arg3 asm("rdx") = (long)(arg3); \ 33030ca2051SWilly Tarreau \ 33130ca2051SWilly Tarreau asm volatile ( \ 33230ca2051SWilly Tarreau "syscall\n" \ 33330ca2051SWilly Tarreau : "=a" (_ret) \ 33430ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 33530ca2051SWilly Tarreau "0"(_num) \ 33630ca2051SWilly Tarreau : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \ 33730ca2051SWilly Tarreau ); \ 33830ca2051SWilly Tarreau _ret; \ 33930ca2051SWilly Tarreau }) 34030ca2051SWilly Tarreau 34130ca2051SWilly Tarreau #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 34230ca2051SWilly Tarreau ({ \ 34330ca2051SWilly Tarreau long _ret; \ 34430ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 34530ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 34630ca2051SWilly Tarreau register long _arg2 asm("rsi") = (long)(arg2); \ 34730ca2051SWilly Tarreau register long _arg3 asm("rdx") = (long)(arg3); \ 34830ca2051SWilly Tarreau register long _arg4 asm("r10") = (long)(arg4); \ 34930ca2051SWilly Tarreau \ 35030ca2051SWilly Tarreau asm volatile ( \ 35130ca2051SWilly Tarreau "syscall\n" \ 35230ca2051SWilly Tarreau : "=a" (_ret), "=r"(_arg4) \ 35330ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 35430ca2051SWilly Tarreau "0"(_num) \ 35530ca2051SWilly Tarreau : "rcx", "r8", "r9", "r11", "memory", "cc" \ 35630ca2051SWilly Tarreau ); \ 35730ca2051SWilly Tarreau _ret; \ 35830ca2051SWilly Tarreau }) 35930ca2051SWilly Tarreau 36030ca2051SWilly Tarreau #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 36130ca2051SWilly Tarreau ({ \ 36230ca2051SWilly Tarreau long _ret; \ 36330ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 36430ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 36530ca2051SWilly Tarreau register long _arg2 asm("rsi") = (long)(arg2); \ 36630ca2051SWilly Tarreau register long _arg3 asm("rdx") = (long)(arg3); \ 36730ca2051SWilly Tarreau register long _arg4 asm("r10") = (long)(arg4); \ 36830ca2051SWilly Tarreau register long _arg5 asm("r8") = (long)(arg5); \ 36930ca2051SWilly Tarreau \ 37030ca2051SWilly Tarreau asm volatile ( \ 37130ca2051SWilly Tarreau "syscall\n" \ 37230ca2051SWilly Tarreau : "=a" (_ret), "=r"(_arg4), "=r"(_arg5) \ 37330ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 37430ca2051SWilly Tarreau "0"(_num) \ 37530ca2051SWilly Tarreau : "rcx", "r9", "r11", "memory", "cc" \ 37630ca2051SWilly Tarreau ); \ 37730ca2051SWilly Tarreau _ret; \ 37830ca2051SWilly Tarreau }) 37930ca2051SWilly Tarreau 38030ca2051SWilly Tarreau #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 38130ca2051SWilly Tarreau ({ \ 38230ca2051SWilly Tarreau long _ret; \ 38330ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 38430ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 38530ca2051SWilly Tarreau register long _arg2 asm("rsi") = (long)(arg2); \ 38630ca2051SWilly Tarreau register long _arg3 asm("rdx") = (long)(arg3); \ 38730ca2051SWilly Tarreau register long _arg4 asm("r10") = (long)(arg4); \ 38830ca2051SWilly Tarreau register long _arg5 asm("r8") = (long)(arg5); \ 38930ca2051SWilly Tarreau register long _arg6 asm("r9") = (long)(arg6); \ 39030ca2051SWilly Tarreau \ 39130ca2051SWilly Tarreau asm volatile ( \ 39230ca2051SWilly Tarreau "syscall\n" \ 39330ca2051SWilly Tarreau : "=a" (_ret), "=r"(_arg4), "=r"(_arg5) \ 39430ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 39530ca2051SWilly Tarreau "r"(_arg6), "0"(_num) \ 39630ca2051SWilly Tarreau : "rcx", "r11", "memory", "cc" \ 39730ca2051SWilly Tarreau ); \ 39830ca2051SWilly Tarreau _ret; \ 39930ca2051SWilly Tarreau }) 40030ca2051SWilly Tarreau 40130ca2051SWilly Tarreau /* startup code */ 40230ca2051SWilly Tarreau asm(".section .text\n" 40330ca2051SWilly Tarreau ".global _start\n" 40430ca2051SWilly Tarreau "_start:\n" 40530ca2051SWilly Tarreau "pop %rdi\n" // argc (first arg, %rdi) 40630ca2051SWilly Tarreau "mov %rsp, %rsi\n" // argv[] (second arg, %rsi) 40730ca2051SWilly Tarreau "lea 8(%rsi,%rdi,8),%rdx\n" // then a NULL then envp (third arg, %rdx) 40830ca2051SWilly Tarreau "and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned when 40930ca2051SWilly Tarreau "sub $8, %rsp\n" // entering the callee 41030ca2051SWilly Tarreau "call main\n" // main() returns the status code, we'll exit with it. 41130ca2051SWilly Tarreau "movzb %al, %rdi\n" // retrieve exit code from 8 lower bits 41230ca2051SWilly Tarreau "mov $60, %rax\n" // NR_exit == 60 41330ca2051SWilly Tarreau "syscall\n" // really exit 41430ca2051SWilly Tarreau "hlt\n" // ensure it does not return 41530ca2051SWilly Tarreau ""); 41630ca2051SWilly Tarreau 41730ca2051SWilly Tarreau /* fcntl / open */ 41830ca2051SWilly Tarreau #define O_RDONLY 0 41930ca2051SWilly Tarreau #define O_WRONLY 1 42030ca2051SWilly Tarreau #define O_RDWR 2 42130ca2051SWilly Tarreau #define O_CREAT 0x40 42230ca2051SWilly Tarreau #define O_EXCL 0x80 42330ca2051SWilly Tarreau #define O_NOCTTY 0x100 42430ca2051SWilly Tarreau #define O_TRUNC 0x200 42530ca2051SWilly Tarreau #define O_APPEND 0x400 42630ca2051SWilly Tarreau #define O_NONBLOCK 0x800 42730ca2051SWilly Tarreau #define O_DIRECTORY 0x10000 42830ca2051SWilly Tarreau 42930ca2051SWilly Tarreau /* The struct returned by the stat() syscall, equivalent to stat64(). The 43030ca2051SWilly Tarreau * syscall returns 116 bytes and stops in the middle of __unused. 43130ca2051SWilly Tarreau */ 43230ca2051SWilly Tarreau struct sys_stat_struct { 43330ca2051SWilly Tarreau unsigned long st_dev; 43430ca2051SWilly Tarreau unsigned long st_ino; 43530ca2051SWilly Tarreau unsigned long st_nlink; 43630ca2051SWilly Tarreau unsigned int st_mode; 43730ca2051SWilly Tarreau unsigned int st_uid; 43830ca2051SWilly Tarreau 43930ca2051SWilly Tarreau unsigned int st_gid; 44030ca2051SWilly Tarreau unsigned int __pad0; 44130ca2051SWilly Tarreau unsigned long st_rdev; 44230ca2051SWilly Tarreau long st_size; 44330ca2051SWilly Tarreau long st_blksize; 44430ca2051SWilly Tarreau 44530ca2051SWilly Tarreau long st_blocks; 44630ca2051SWilly Tarreau unsigned long st_atime; 44730ca2051SWilly Tarreau unsigned long st_atime_nsec; 44830ca2051SWilly Tarreau unsigned long st_mtime; 44930ca2051SWilly Tarreau 45030ca2051SWilly Tarreau unsigned long st_mtime_nsec; 45130ca2051SWilly Tarreau unsigned long st_ctime; 45230ca2051SWilly Tarreau unsigned long st_ctime_nsec; 45330ca2051SWilly Tarreau long __unused[3]; 45430ca2051SWilly Tarreau }; 45530ca2051SWilly Tarreau 45630ca2051SWilly Tarreau #elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) 45730ca2051SWilly Tarreau /* Syscalls for i386 : 45830ca2051SWilly Tarreau * - mostly similar to x86_64 45930ca2051SWilly Tarreau * - registers are 32-bit 46030ca2051SWilly Tarreau * - syscall number is passed in eax 46130ca2051SWilly Tarreau * - arguments are in ebx, ecx, edx, esi, edi, ebp respectively 46230ca2051SWilly Tarreau * - all registers are preserved (except eax of course) 46330ca2051SWilly Tarreau * - the system call is performed by calling int $0x80 46430ca2051SWilly Tarreau * - syscall return comes in eax 46530ca2051SWilly Tarreau * - the arguments are cast to long and assigned into the target registers 46630ca2051SWilly Tarreau * which are then simply passed as registers to the asm code, so that we 46730ca2051SWilly Tarreau * don't have to experience issues with register constraints. 46830ca2051SWilly Tarreau * - the syscall number is always specified last in order to allow to force 46930ca2051SWilly Tarreau * some registers before (gcc refuses a %-register at the last position). 47030ca2051SWilly Tarreau * 47130ca2051SWilly Tarreau * Also, i386 supports the old_select syscall if newselect is not available 47230ca2051SWilly Tarreau */ 47330ca2051SWilly Tarreau #define __ARCH_WANT_SYS_OLD_SELECT 47430ca2051SWilly Tarreau 47530ca2051SWilly Tarreau #define my_syscall0(num) \ 47630ca2051SWilly Tarreau ({ \ 47730ca2051SWilly Tarreau long _ret; \ 47830ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 47930ca2051SWilly Tarreau \ 48030ca2051SWilly Tarreau asm volatile ( \ 48130ca2051SWilly Tarreau "int $0x80\n" \ 48230ca2051SWilly Tarreau : "=a" (_ret) \ 48330ca2051SWilly Tarreau : "0"(_num) \ 48430ca2051SWilly Tarreau : "memory", "cc" \ 48530ca2051SWilly Tarreau ); \ 48630ca2051SWilly Tarreau _ret; \ 48730ca2051SWilly Tarreau }) 48830ca2051SWilly Tarreau 48930ca2051SWilly Tarreau #define my_syscall1(num, arg1) \ 49030ca2051SWilly Tarreau ({ \ 49130ca2051SWilly Tarreau long _ret; \ 49230ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 49330ca2051SWilly Tarreau register long _arg1 asm("ebx") = (long)(arg1); \ 49430ca2051SWilly Tarreau \ 49530ca2051SWilly Tarreau asm volatile ( \ 49630ca2051SWilly Tarreau "int $0x80\n" \ 49730ca2051SWilly Tarreau : "=a" (_ret) \ 49830ca2051SWilly Tarreau : "r"(_arg1), \ 49930ca2051SWilly Tarreau "0"(_num) \ 50030ca2051SWilly Tarreau : "memory", "cc" \ 50130ca2051SWilly Tarreau ); \ 50230ca2051SWilly Tarreau _ret; \ 50330ca2051SWilly Tarreau }) 50430ca2051SWilly Tarreau 50530ca2051SWilly Tarreau #define my_syscall2(num, arg1, arg2) \ 50630ca2051SWilly Tarreau ({ \ 50730ca2051SWilly Tarreau long _ret; \ 50830ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 50930ca2051SWilly Tarreau register long _arg1 asm("ebx") = (long)(arg1); \ 51030ca2051SWilly Tarreau register long _arg2 asm("ecx") = (long)(arg2); \ 51130ca2051SWilly Tarreau \ 51230ca2051SWilly Tarreau asm volatile ( \ 51330ca2051SWilly Tarreau "int $0x80\n" \ 51430ca2051SWilly Tarreau : "=a" (_ret) \ 51530ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), \ 51630ca2051SWilly Tarreau "0"(_num) \ 51730ca2051SWilly Tarreau : "memory", "cc" \ 51830ca2051SWilly Tarreau ); \ 51930ca2051SWilly Tarreau _ret; \ 52030ca2051SWilly Tarreau }) 52130ca2051SWilly Tarreau 52230ca2051SWilly Tarreau #define my_syscall3(num, arg1, arg2, arg3) \ 52330ca2051SWilly Tarreau ({ \ 52430ca2051SWilly Tarreau long _ret; \ 52530ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 52630ca2051SWilly Tarreau register long _arg1 asm("ebx") = (long)(arg1); \ 52730ca2051SWilly Tarreau register long _arg2 asm("ecx") = (long)(arg2); \ 52830ca2051SWilly Tarreau register long _arg3 asm("edx") = (long)(arg3); \ 52930ca2051SWilly Tarreau \ 53030ca2051SWilly Tarreau asm volatile ( \ 53130ca2051SWilly Tarreau "int $0x80\n" \ 53230ca2051SWilly Tarreau : "=a" (_ret) \ 53330ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 53430ca2051SWilly Tarreau "0"(_num) \ 53530ca2051SWilly Tarreau : "memory", "cc" \ 53630ca2051SWilly Tarreau ); \ 53730ca2051SWilly Tarreau _ret; \ 53830ca2051SWilly Tarreau }) 53930ca2051SWilly Tarreau 54030ca2051SWilly Tarreau #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 54130ca2051SWilly Tarreau ({ \ 54230ca2051SWilly Tarreau long _ret; \ 54330ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 54430ca2051SWilly Tarreau register long _arg1 asm("ebx") = (long)(arg1); \ 54530ca2051SWilly Tarreau register long _arg2 asm("ecx") = (long)(arg2); \ 54630ca2051SWilly Tarreau register long _arg3 asm("edx") = (long)(arg3); \ 54730ca2051SWilly Tarreau register long _arg4 asm("esi") = (long)(arg4); \ 54830ca2051SWilly Tarreau \ 54930ca2051SWilly Tarreau asm volatile ( \ 55030ca2051SWilly Tarreau "int $0x80\n" \ 55130ca2051SWilly Tarreau : "=a" (_ret) \ 55230ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 55330ca2051SWilly Tarreau "0"(_num) \ 55430ca2051SWilly Tarreau : "memory", "cc" \ 55530ca2051SWilly Tarreau ); \ 55630ca2051SWilly Tarreau _ret; \ 55730ca2051SWilly Tarreau }) 55830ca2051SWilly Tarreau 55930ca2051SWilly Tarreau #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 56030ca2051SWilly Tarreau ({ \ 56130ca2051SWilly Tarreau long _ret; \ 56230ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 56330ca2051SWilly Tarreau register long _arg1 asm("ebx") = (long)(arg1); \ 56430ca2051SWilly Tarreau register long _arg2 asm("ecx") = (long)(arg2); \ 56530ca2051SWilly Tarreau register long _arg3 asm("edx") = (long)(arg3); \ 56630ca2051SWilly Tarreau register long _arg4 asm("esi") = (long)(arg4); \ 56730ca2051SWilly Tarreau register long _arg5 asm("edi") = (long)(arg5); \ 56830ca2051SWilly Tarreau \ 56930ca2051SWilly Tarreau asm volatile ( \ 57030ca2051SWilly Tarreau "int $0x80\n" \ 57130ca2051SWilly Tarreau : "=a" (_ret) \ 57230ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 57330ca2051SWilly Tarreau "0"(_num) \ 57430ca2051SWilly Tarreau : "memory", "cc" \ 57530ca2051SWilly Tarreau ); \ 57630ca2051SWilly Tarreau _ret; \ 57730ca2051SWilly Tarreau }) 57830ca2051SWilly Tarreau 57930ca2051SWilly Tarreau /* startup code */ 58030ca2051SWilly Tarreau asm(".section .text\n" 58130ca2051SWilly Tarreau ".global _start\n" 58230ca2051SWilly Tarreau "_start:\n" 58330ca2051SWilly Tarreau "pop %eax\n" // argc (first arg, %eax) 58430ca2051SWilly Tarreau "mov %esp, %ebx\n" // argv[] (second arg, %ebx) 58530ca2051SWilly Tarreau "lea 4(%ebx,%eax,4),%ecx\n" // then a NULL then envp (third arg, %ecx) 58630ca2051SWilly Tarreau "and $-16, %esp\n" // x86 ABI : esp must be 16-byte aligned when 58730ca2051SWilly Tarreau "push %ecx\n" // push all registers on the stack so that we 58830ca2051SWilly Tarreau "push %ebx\n" // support both regparm and plain stack modes 58930ca2051SWilly Tarreau "push %eax\n" 59030ca2051SWilly Tarreau "call main\n" // main() returns the status code in %eax 59130ca2051SWilly Tarreau "movzbl %al, %ebx\n" // retrieve exit code from lower 8 bits 59230ca2051SWilly Tarreau "movl $1, %eax\n" // NR_exit == 1 59330ca2051SWilly Tarreau "int $0x80\n" // exit now 59430ca2051SWilly Tarreau "hlt\n" // ensure it does not 59530ca2051SWilly Tarreau ""); 59630ca2051SWilly Tarreau 59730ca2051SWilly Tarreau /* fcntl / open */ 59830ca2051SWilly Tarreau #define O_RDONLY 0 59930ca2051SWilly Tarreau #define O_WRONLY 1 60030ca2051SWilly Tarreau #define O_RDWR 2 60130ca2051SWilly Tarreau #define O_CREAT 0x40 60230ca2051SWilly Tarreau #define O_EXCL 0x80 60330ca2051SWilly Tarreau #define O_NOCTTY 0x100 60430ca2051SWilly Tarreau #define O_TRUNC 0x200 60530ca2051SWilly Tarreau #define O_APPEND 0x400 60630ca2051SWilly Tarreau #define O_NONBLOCK 0x800 60730ca2051SWilly Tarreau #define O_DIRECTORY 0x10000 60830ca2051SWilly Tarreau 60930ca2051SWilly Tarreau /* The struct returned by the stat() syscall, 32-bit only, the syscall returns 61030ca2051SWilly Tarreau * exactly 56 bytes (stops before the unused array). 61130ca2051SWilly Tarreau */ 61230ca2051SWilly Tarreau struct sys_stat_struct { 61330ca2051SWilly Tarreau unsigned long st_dev; 61430ca2051SWilly Tarreau unsigned long st_ino; 61530ca2051SWilly Tarreau unsigned short st_mode; 61630ca2051SWilly Tarreau unsigned short st_nlink; 61730ca2051SWilly Tarreau unsigned short st_uid; 61830ca2051SWilly Tarreau unsigned short st_gid; 61930ca2051SWilly Tarreau 62030ca2051SWilly Tarreau unsigned long st_rdev; 62130ca2051SWilly Tarreau unsigned long st_size; 62230ca2051SWilly Tarreau unsigned long st_blksize; 62330ca2051SWilly Tarreau unsigned long st_blocks; 62430ca2051SWilly Tarreau 62530ca2051SWilly Tarreau unsigned long st_atime; 62630ca2051SWilly Tarreau unsigned long st_atime_nsec; 62730ca2051SWilly Tarreau unsigned long st_mtime; 62830ca2051SWilly Tarreau unsigned long st_mtime_nsec; 62930ca2051SWilly Tarreau 63030ca2051SWilly Tarreau unsigned long st_ctime; 63130ca2051SWilly Tarreau unsigned long st_ctime_nsec; 63230ca2051SWilly Tarreau unsigned long __unused[2]; 63330ca2051SWilly Tarreau }; 63430ca2051SWilly Tarreau 63530ca2051SWilly Tarreau #elif defined(__ARM_EABI__) 63630ca2051SWilly Tarreau /* Syscalls for ARM in ARM or Thumb modes : 63730ca2051SWilly Tarreau * - registers are 32-bit 63830ca2051SWilly Tarreau * - stack is 8-byte aligned 63930ca2051SWilly Tarreau * ( http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka4127.html) 64030ca2051SWilly Tarreau * - syscall number is passed in r7 64130ca2051SWilly Tarreau * - arguments are in r0, r1, r2, r3, r4, r5 64230ca2051SWilly Tarreau * - the system call is performed by calling svc #0 64330ca2051SWilly Tarreau * - syscall return comes in r0. 64430ca2051SWilly Tarreau * - only lr is clobbered. 64530ca2051SWilly Tarreau * - the arguments are cast to long and assigned into the target registers 64630ca2051SWilly Tarreau * which are then simply passed as registers to the asm code, so that we 64730ca2051SWilly Tarreau * don't have to experience issues with register constraints. 64830ca2051SWilly Tarreau * - the syscall number is always specified last in order to allow to force 64930ca2051SWilly Tarreau * some registers before (gcc refuses a %-register at the last position). 65030ca2051SWilly Tarreau * 65130ca2051SWilly Tarreau * Also, ARM supports the old_select syscall if newselect is not available 65230ca2051SWilly Tarreau */ 65330ca2051SWilly Tarreau #define __ARCH_WANT_SYS_OLD_SELECT 65430ca2051SWilly Tarreau 65530ca2051SWilly Tarreau #define my_syscall0(num) \ 65630ca2051SWilly Tarreau ({ \ 65730ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 65830ca2051SWilly Tarreau register long _arg1 asm("r0"); \ 65930ca2051SWilly Tarreau \ 66030ca2051SWilly Tarreau asm volatile ( \ 66130ca2051SWilly Tarreau "svc #0\n" \ 66230ca2051SWilly Tarreau : "=r"(_arg1) \ 66330ca2051SWilly Tarreau : "r"(_num) \ 66430ca2051SWilly Tarreau : "memory", "cc", "lr" \ 66530ca2051SWilly Tarreau ); \ 66630ca2051SWilly Tarreau _arg1; \ 66730ca2051SWilly Tarreau }) 66830ca2051SWilly Tarreau 66930ca2051SWilly Tarreau #define my_syscall1(num, arg1) \ 67030ca2051SWilly Tarreau ({ \ 67130ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 67230ca2051SWilly Tarreau register long _arg1 asm("r0") = (long)(arg1); \ 67330ca2051SWilly Tarreau \ 67430ca2051SWilly Tarreau asm volatile ( \ 67530ca2051SWilly Tarreau "svc #0\n" \ 67630ca2051SWilly Tarreau : "=r"(_arg1) \ 67730ca2051SWilly Tarreau : "r"(_arg1), \ 67830ca2051SWilly Tarreau "r"(_num) \ 67930ca2051SWilly Tarreau : "memory", "cc", "lr" \ 68030ca2051SWilly Tarreau ); \ 68130ca2051SWilly Tarreau _arg1; \ 68230ca2051SWilly Tarreau }) 68330ca2051SWilly Tarreau 68430ca2051SWilly Tarreau #define my_syscall2(num, arg1, arg2) \ 68530ca2051SWilly Tarreau ({ \ 68630ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 68730ca2051SWilly Tarreau register long _arg1 asm("r0") = (long)(arg1); \ 68830ca2051SWilly Tarreau register long _arg2 asm("r1") = (long)(arg2); \ 68930ca2051SWilly Tarreau \ 69030ca2051SWilly Tarreau asm volatile ( \ 69130ca2051SWilly Tarreau "svc #0\n" \ 69230ca2051SWilly Tarreau : "=r"(_arg1) \ 69330ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), \ 69430ca2051SWilly Tarreau "r"(_num) \ 69530ca2051SWilly Tarreau : "memory", "cc", "lr" \ 69630ca2051SWilly Tarreau ); \ 69730ca2051SWilly Tarreau _arg1; \ 69830ca2051SWilly Tarreau }) 69930ca2051SWilly Tarreau 70030ca2051SWilly Tarreau #define my_syscall3(num, arg1, arg2, arg3) \ 70130ca2051SWilly Tarreau ({ \ 70230ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 70330ca2051SWilly Tarreau register long _arg1 asm("r0") = (long)(arg1); \ 70430ca2051SWilly Tarreau register long _arg2 asm("r1") = (long)(arg2); \ 70530ca2051SWilly Tarreau register long _arg3 asm("r2") = (long)(arg3); \ 70630ca2051SWilly Tarreau \ 70730ca2051SWilly Tarreau asm volatile ( \ 70830ca2051SWilly Tarreau "svc #0\n" \ 70930ca2051SWilly Tarreau : "=r"(_arg1) \ 71030ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 71130ca2051SWilly Tarreau "r"(_num) \ 71230ca2051SWilly Tarreau : "memory", "cc", "lr" \ 71330ca2051SWilly Tarreau ); \ 71430ca2051SWilly Tarreau _arg1; \ 71530ca2051SWilly Tarreau }) 71630ca2051SWilly Tarreau 71730ca2051SWilly Tarreau #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 71830ca2051SWilly Tarreau ({ \ 71930ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 72030ca2051SWilly Tarreau register long _arg1 asm("r0") = (long)(arg1); \ 72130ca2051SWilly Tarreau register long _arg2 asm("r1") = (long)(arg2); \ 72230ca2051SWilly Tarreau register long _arg3 asm("r2") = (long)(arg3); \ 72330ca2051SWilly Tarreau register long _arg4 asm("r3") = (long)(arg4); \ 72430ca2051SWilly Tarreau \ 72530ca2051SWilly Tarreau asm volatile ( \ 72630ca2051SWilly Tarreau "svc #0\n" \ 72730ca2051SWilly Tarreau : "=r"(_arg1) \ 72830ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 72930ca2051SWilly Tarreau "r"(_num) \ 73030ca2051SWilly Tarreau : "memory", "cc", "lr" \ 73130ca2051SWilly Tarreau ); \ 73230ca2051SWilly Tarreau _arg1; \ 73330ca2051SWilly Tarreau }) 73430ca2051SWilly Tarreau 73530ca2051SWilly Tarreau #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 73630ca2051SWilly Tarreau ({ \ 73730ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 73830ca2051SWilly Tarreau register long _arg1 asm("r0") = (long)(arg1); \ 73930ca2051SWilly Tarreau register long _arg2 asm("r1") = (long)(arg2); \ 74030ca2051SWilly Tarreau register long _arg3 asm("r2") = (long)(arg3); \ 74130ca2051SWilly Tarreau register long _arg4 asm("r3") = (long)(arg4); \ 74230ca2051SWilly Tarreau register long _arg5 asm("r4") = (long)(arg5); \ 74330ca2051SWilly Tarreau \ 74430ca2051SWilly Tarreau asm volatile ( \ 74530ca2051SWilly Tarreau "svc #0\n" \ 74630ca2051SWilly Tarreau : "=r" (_arg1) \ 74730ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 74830ca2051SWilly Tarreau "r"(_num) \ 74930ca2051SWilly Tarreau : "memory", "cc", "lr" \ 75030ca2051SWilly Tarreau ); \ 75130ca2051SWilly Tarreau _arg1; \ 75230ca2051SWilly Tarreau }) 75330ca2051SWilly Tarreau 75430ca2051SWilly Tarreau /* startup code */ 75530ca2051SWilly Tarreau asm(".section .text\n" 75630ca2051SWilly Tarreau ".global _start\n" 75730ca2051SWilly Tarreau "_start:\n" 75830ca2051SWilly Tarreau #if defined(__THUMBEB__) || defined(__THUMBEL__) 75930ca2051SWilly Tarreau /* We enter here in 32-bit mode but if some previous functions were in 76030ca2051SWilly Tarreau * 16-bit mode, the assembler cannot know, so we need to tell it we're in 76130ca2051SWilly Tarreau * 32-bit now, then switch to 16-bit (is there a better way to do it than 76230ca2051SWilly Tarreau * adding 1 by hand ?) and tell the asm we're now in 16-bit mode so that 76330ca2051SWilly Tarreau * it generates correct instructions. Note that we do not support thumb1. 76430ca2051SWilly Tarreau */ 76530ca2051SWilly Tarreau ".code 32\n" 76630ca2051SWilly Tarreau "add r0, pc, #1\n" 76730ca2051SWilly Tarreau "bx r0\n" 76830ca2051SWilly Tarreau ".code 16\n" 76930ca2051SWilly Tarreau #endif 77030ca2051SWilly Tarreau "pop {%r0}\n" // argc was in the stack 77130ca2051SWilly Tarreau "mov %r1, %sp\n" // argv = sp 77230ca2051SWilly Tarreau "add %r2, %r1, %r0, lsl #2\n" // envp = argv + 4*argc ... 77330ca2051SWilly Tarreau "add %r2, %r2, $4\n" // ... + 4 77430ca2051SWilly Tarreau "and %r3, %r1, $-8\n" // AAPCS : sp must be 8-byte aligned in the 77530ca2051SWilly Tarreau "mov %sp, %r3\n" // callee, an bl doesn't push (lr=pc) 77630ca2051SWilly Tarreau "bl main\n" // main() returns the status code, we'll exit with it. 77730ca2051SWilly Tarreau "and %r0, %r0, $0xff\n" // limit exit code to 8 bits 77830ca2051SWilly Tarreau "movs r7, $1\n" // NR_exit == 1 77930ca2051SWilly Tarreau "svc $0x00\n" 78030ca2051SWilly Tarreau ""); 78130ca2051SWilly Tarreau 78230ca2051SWilly Tarreau /* fcntl / open */ 78330ca2051SWilly Tarreau #define O_RDONLY 0 78430ca2051SWilly Tarreau #define O_WRONLY 1 78530ca2051SWilly Tarreau #define O_RDWR 2 78630ca2051SWilly Tarreau #define O_CREAT 0x40 78730ca2051SWilly Tarreau #define O_EXCL 0x80 78830ca2051SWilly Tarreau #define O_NOCTTY 0x100 78930ca2051SWilly Tarreau #define O_TRUNC 0x200 79030ca2051SWilly Tarreau #define O_APPEND 0x400 79130ca2051SWilly Tarreau #define O_NONBLOCK 0x800 79230ca2051SWilly Tarreau #define O_DIRECTORY 0x4000 79330ca2051SWilly Tarreau 79430ca2051SWilly Tarreau /* The struct returned by the stat() syscall, 32-bit only, the syscall returns 79530ca2051SWilly Tarreau * exactly 56 bytes (stops before the unused array). In big endian, the format 79630ca2051SWilly Tarreau * differs as devices are returned as short only. 79730ca2051SWilly Tarreau */ 79830ca2051SWilly Tarreau struct sys_stat_struct { 79930ca2051SWilly Tarreau #if defined(__ARMEB__) 80030ca2051SWilly Tarreau unsigned short st_dev; 80130ca2051SWilly Tarreau unsigned short __pad1; 80230ca2051SWilly Tarreau #else 80330ca2051SWilly Tarreau unsigned long st_dev; 80430ca2051SWilly Tarreau #endif 80530ca2051SWilly Tarreau unsigned long st_ino; 80630ca2051SWilly Tarreau unsigned short st_mode; 80730ca2051SWilly Tarreau unsigned short st_nlink; 80830ca2051SWilly Tarreau unsigned short st_uid; 80930ca2051SWilly Tarreau unsigned short st_gid; 81030ca2051SWilly Tarreau #if defined(__ARMEB__) 81130ca2051SWilly Tarreau unsigned short st_rdev; 81230ca2051SWilly Tarreau unsigned short __pad2; 81330ca2051SWilly Tarreau #else 81430ca2051SWilly Tarreau unsigned long st_rdev; 81530ca2051SWilly Tarreau #endif 81630ca2051SWilly Tarreau unsigned long st_size; 81730ca2051SWilly Tarreau unsigned long st_blksize; 81830ca2051SWilly Tarreau unsigned long st_blocks; 81930ca2051SWilly Tarreau unsigned long st_atime; 82030ca2051SWilly Tarreau unsigned long st_atime_nsec; 82130ca2051SWilly Tarreau unsigned long st_mtime; 82230ca2051SWilly Tarreau unsigned long st_mtime_nsec; 82330ca2051SWilly Tarreau unsigned long st_ctime; 82430ca2051SWilly Tarreau unsigned long st_ctime_nsec; 82530ca2051SWilly Tarreau unsigned long __unused[2]; 82630ca2051SWilly Tarreau }; 82730ca2051SWilly Tarreau 82830ca2051SWilly Tarreau #elif defined(__aarch64__) 82930ca2051SWilly Tarreau /* Syscalls for AARCH64 : 83030ca2051SWilly Tarreau * - registers are 64-bit 83130ca2051SWilly Tarreau * - stack is 16-byte aligned 83230ca2051SWilly Tarreau * - syscall number is passed in x8 83330ca2051SWilly Tarreau * - arguments are in x0, x1, x2, x3, x4, x5 83430ca2051SWilly Tarreau * - the system call is performed by calling svc 0 83530ca2051SWilly Tarreau * - syscall return comes in x0. 83630ca2051SWilly Tarreau * - the arguments are cast to long and assigned into the target registers 83730ca2051SWilly Tarreau * which are then simply passed as registers to the asm code, so that we 83830ca2051SWilly Tarreau * don't have to experience issues with register constraints. 83930ca2051SWilly Tarreau * 84030ca2051SWilly Tarreau * On aarch64, select() is not implemented so we have to use pselect6(). 84130ca2051SWilly Tarreau */ 84230ca2051SWilly Tarreau #define __ARCH_WANT_SYS_PSELECT6 84330ca2051SWilly Tarreau 84430ca2051SWilly Tarreau #define my_syscall0(num) \ 84530ca2051SWilly Tarreau ({ \ 84630ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 84730ca2051SWilly Tarreau register long _arg1 asm("x0"); \ 84830ca2051SWilly Tarreau \ 84930ca2051SWilly Tarreau asm volatile ( \ 85030ca2051SWilly Tarreau "svc #0\n" \ 85130ca2051SWilly Tarreau : "=r"(_arg1) \ 85230ca2051SWilly Tarreau : "r"(_num) \ 85330ca2051SWilly Tarreau : "memory", "cc" \ 85430ca2051SWilly Tarreau ); \ 85530ca2051SWilly Tarreau _arg1; \ 85630ca2051SWilly Tarreau }) 85730ca2051SWilly Tarreau 85830ca2051SWilly Tarreau #define my_syscall1(num, arg1) \ 85930ca2051SWilly Tarreau ({ \ 86030ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 86130ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 86230ca2051SWilly Tarreau \ 86330ca2051SWilly Tarreau asm volatile ( \ 86430ca2051SWilly Tarreau "svc #0\n" \ 86530ca2051SWilly Tarreau : "=r"(_arg1) \ 86630ca2051SWilly Tarreau : "r"(_arg1), \ 86730ca2051SWilly Tarreau "r"(_num) \ 86830ca2051SWilly Tarreau : "memory", "cc" \ 86930ca2051SWilly Tarreau ); \ 87030ca2051SWilly Tarreau _arg1; \ 87130ca2051SWilly Tarreau }) 87230ca2051SWilly Tarreau 87330ca2051SWilly Tarreau #define my_syscall2(num, arg1, arg2) \ 87430ca2051SWilly Tarreau ({ \ 87530ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 87630ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 87730ca2051SWilly Tarreau register long _arg2 asm("x1") = (long)(arg2); \ 87830ca2051SWilly Tarreau \ 87930ca2051SWilly Tarreau asm volatile ( \ 88030ca2051SWilly Tarreau "svc #0\n" \ 88130ca2051SWilly Tarreau : "=r"(_arg1) \ 88230ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), \ 88330ca2051SWilly Tarreau "r"(_num) \ 88430ca2051SWilly Tarreau : "memory", "cc" \ 88530ca2051SWilly Tarreau ); \ 88630ca2051SWilly Tarreau _arg1; \ 88730ca2051SWilly Tarreau }) 88830ca2051SWilly Tarreau 88930ca2051SWilly Tarreau #define my_syscall3(num, arg1, arg2, arg3) \ 89030ca2051SWilly Tarreau ({ \ 89130ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 89230ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 89330ca2051SWilly Tarreau register long _arg2 asm("x1") = (long)(arg2); \ 89430ca2051SWilly Tarreau register long _arg3 asm("x2") = (long)(arg3); \ 89530ca2051SWilly Tarreau \ 89630ca2051SWilly Tarreau asm volatile ( \ 89730ca2051SWilly Tarreau "svc #0\n" \ 89830ca2051SWilly Tarreau : "=r"(_arg1) \ 89930ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 90030ca2051SWilly Tarreau "r"(_num) \ 90130ca2051SWilly Tarreau : "memory", "cc" \ 90230ca2051SWilly Tarreau ); \ 90330ca2051SWilly Tarreau _arg1; \ 90430ca2051SWilly Tarreau }) 90530ca2051SWilly Tarreau 90630ca2051SWilly Tarreau #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 90730ca2051SWilly Tarreau ({ \ 90830ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 90930ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 91030ca2051SWilly Tarreau register long _arg2 asm("x1") = (long)(arg2); \ 91130ca2051SWilly Tarreau register long _arg3 asm("x2") = (long)(arg3); \ 91230ca2051SWilly Tarreau register long _arg4 asm("x3") = (long)(arg4); \ 91330ca2051SWilly Tarreau \ 91430ca2051SWilly Tarreau asm volatile ( \ 91530ca2051SWilly Tarreau "svc #0\n" \ 91630ca2051SWilly Tarreau : "=r"(_arg1) \ 91730ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 91830ca2051SWilly Tarreau "r"(_num) \ 91930ca2051SWilly Tarreau : "memory", "cc" \ 92030ca2051SWilly Tarreau ); \ 92130ca2051SWilly Tarreau _arg1; \ 92230ca2051SWilly Tarreau }) 92330ca2051SWilly Tarreau 92430ca2051SWilly Tarreau #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 92530ca2051SWilly Tarreau ({ \ 92630ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 92730ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 92830ca2051SWilly Tarreau register long _arg2 asm("x1") = (long)(arg2); \ 92930ca2051SWilly Tarreau register long _arg3 asm("x2") = (long)(arg3); \ 93030ca2051SWilly Tarreau register long _arg4 asm("x3") = (long)(arg4); \ 93130ca2051SWilly Tarreau register long _arg5 asm("x4") = (long)(arg5); \ 93230ca2051SWilly Tarreau \ 93330ca2051SWilly Tarreau asm volatile ( \ 93430ca2051SWilly Tarreau "svc #0\n" \ 93530ca2051SWilly Tarreau : "=r" (_arg1) \ 93630ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 93730ca2051SWilly Tarreau "r"(_num) \ 93830ca2051SWilly Tarreau : "memory", "cc" \ 93930ca2051SWilly Tarreau ); \ 94030ca2051SWilly Tarreau _arg1; \ 94130ca2051SWilly Tarreau }) 94230ca2051SWilly Tarreau 94330ca2051SWilly Tarreau #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 94430ca2051SWilly Tarreau ({ \ 94530ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 94630ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 94730ca2051SWilly Tarreau register long _arg2 asm("x1") = (long)(arg2); \ 94830ca2051SWilly Tarreau register long _arg3 asm("x2") = (long)(arg3); \ 94930ca2051SWilly Tarreau register long _arg4 asm("x3") = (long)(arg4); \ 95030ca2051SWilly Tarreau register long _arg5 asm("x4") = (long)(arg5); \ 95130ca2051SWilly Tarreau register long _arg6 asm("x5") = (long)(arg6); \ 95230ca2051SWilly Tarreau \ 95330ca2051SWilly Tarreau asm volatile ( \ 95430ca2051SWilly Tarreau "svc #0\n" \ 95530ca2051SWilly Tarreau : "=r" (_arg1) \ 95630ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 95730ca2051SWilly Tarreau "r"(_arg6), "r"(_num) \ 95830ca2051SWilly Tarreau : "memory", "cc" \ 95930ca2051SWilly Tarreau ); \ 96030ca2051SWilly Tarreau _arg1; \ 96130ca2051SWilly Tarreau }) 96230ca2051SWilly Tarreau 96330ca2051SWilly Tarreau /* startup code */ 96430ca2051SWilly Tarreau asm(".section .text\n" 96530ca2051SWilly Tarreau ".global _start\n" 96630ca2051SWilly Tarreau "_start:\n" 96730ca2051SWilly Tarreau "ldr x0, [sp]\n" // argc (x0) was in the stack 96830ca2051SWilly Tarreau "add x1, sp, 8\n" // argv (x1) = sp 96930ca2051SWilly Tarreau "lsl x2, x0, 3\n" // envp (x2) = 8*argc ... 97030ca2051SWilly Tarreau "add x2, x2, 8\n" // + 8 (skip null) 97130ca2051SWilly Tarreau "add x2, x2, x1\n" // + argv 97230ca2051SWilly Tarreau "and sp, x1, -16\n" // sp must be 16-byte aligned in the callee 97330ca2051SWilly Tarreau "bl main\n" // main() returns the status code, we'll exit with it. 97430ca2051SWilly Tarreau "and x0, x0, 0xff\n" // limit exit code to 8 bits 97530ca2051SWilly Tarreau "mov x8, 93\n" // NR_exit == 93 97630ca2051SWilly Tarreau "svc #0\n" 97730ca2051SWilly Tarreau ""); 97830ca2051SWilly Tarreau 97930ca2051SWilly Tarreau /* fcntl / open */ 98030ca2051SWilly Tarreau #define O_RDONLY 0 98130ca2051SWilly Tarreau #define O_WRONLY 1 98230ca2051SWilly Tarreau #define O_RDWR 2 98330ca2051SWilly Tarreau #define O_CREAT 0x40 98430ca2051SWilly Tarreau #define O_EXCL 0x80 98530ca2051SWilly Tarreau #define O_NOCTTY 0x100 98630ca2051SWilly Tarreau #define O_TRUNC 0x200 98730ca2051SWilly Tarreau #define O_APPEND 0x400 98830ca2051SWilly Tarreau #define O_NONBLOCK 0x800 98930ca2051SWilly Tarreau #define O_DIRECTORY 0x4000 99030ca2051SWilly Tarreau 99130ca2051SWilly Tarreau /* The struct returned by the newfstatat() syscall. Differs slightly from the 99230ca2051SWilly Tarreau * x86_64's stat one by field ordering, so be careful. 99330ca2051SWilly Tarreau */ 99430ca2051SWilly Tarreau struct sys_stat_struct { 99530ca2051SWilly Tarreau unsigned long st_dev; 99630ca2051SWilly Tarreau unsigned long st_ino; 99730ca2051SWilly Tarreau unsigned int st_mode; 99830ca2051SWilly Tarreau unsigned int st_nlink; 99930ca2051SWilly Tarreau unsigned int st_uid; 100030ca2051SWilly Tarreau unsigned int st_gid; 100130ca2051SWilly Tarreau 100230ca2051SWilly Tarreau unsigned long st_rdev; 100330ca2051SWilly Tarreau unsigned long __pad1; 100430ca2051SWilly Tarreau long st_size; 100530ca2051SWilly Tarreau int st_blksize; 100630ca2051SWilly Tarreau int __pad2; 100730ca2051SWilly Tarreau 100830ca2051SWilly Tarreau long st_blocks; 100930ca2051SWilly Tarreau long st_atime; 101030ca2051SWilly Tarreau unsigned long st_atime_nsec; 101130ca2051SWilly Tarreau long st_mtime; 101230ca2051SWilly Tarreau 101330ca2051SWilly Tarreau unsigned long st_mtime_nsec; 101430ca2051SWilly Tarreau long st_ctime; 101530ca2051SWilly Tarreau unsigned long st_ctime_nsec; 101630ca2051SWilly Tarreau unsigned int __unused[2]; 101730ca2051SWilly Tarreau }; 101830ca2051SWilly Tarreau 101930ca2051SWilly Tarreau #elif defined(__mips__) && defined(_ABIO32) 102030ca2051SWilly Tarreau /* Syscalls for MIPS ABI O32 : 102130ca2051SWilly Tarreau * - WARNING! there's always a delayed slot! 102230ca2051SWilly Tarreau * - WARNING again, the syntax is different, registers take a '$' and numbers 102330ca2051SWilly Tarreau * do not. 102430ca2051SWilly Tarreau * - registers are 32-bit 102530ca2051SWilly Tarreau * - stack is 8-byte aligned 102630ca2051SWilly Tarreau * - syscall number is passed in v0 (starts at 0xfa0). 102730ca2051SWilly Tarreau * - arguments are in a0, a1, a2, a3, then the stack. The caller needs to 102830ca2051SWilly Tarreau * leave some room in the stack for the callee to save a0..a3 if needed. 102930ca2051SWilly Tarreau * - Many registers are clobbered, in fact only a0..a2 and s0..s8 are 103030ca2051SWilly Tarreau * preserved. See: https://www.linux-mips.org/wiki/Syscall as well as 103130ca2051SWilly Tarreau * scall32-o32.S in the kernel sources. 103230ca2051SWilly Tarreau * - the system call is performed by calling "syscall" 103330ca2051SWilly Tarreau * - syscall return comes in v0, and register a3 needs to be checked to know 103430ca2051SWilly Tarreau * if an error occured, in which case errno is in v0. 103530ca2051SWilly Tarreau * - the arguments are cast to long and assigned into the target registers 103630ca2051SWilly Tarreau * which are then simply passed as registers to the asm code, so that we 103730ca2051SWilly Tarreau * don't have to experience issues with register constraints. 103830ca2051SWilly Tarreau */ 103930ca2051SWilly Tarreau 104030ca2051SWilly Tarreau #define my_syscall0(num) \ 104130ca2051SWilly Tarreau ({ \ 104230ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 104330ca2051SWilly Tarreau register long _arg4 asm("a3"); \ 104430ca2051SWilly Tarreau \ 104530ca2051SWilly Tarreau asm volatile ( \ 104630ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 104730ca2051SWilly Tarreau "syscall\n" \ 104830ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 104930ca2051SWilly Tarreau : "=r"(_num), "=r"(_arg4) \ 105030ca2051SWilly Tarreau : "r"(_num) \ 105130ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 105230ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 105330ca2051SWilly Tarreau ); \ 105430ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 105530ca2051SWilly Tarreau }) 105630ca2051SWilly Tarreau 105730ca2051SWilly Tarreau #define my_syscall1(num, arg1) \ 105830ca2051SWilly Tarreau ({ \ 105930ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 106030ca2051SWilly Tarreau register long _arg1 asm("a0") = (long)(arg1); \ 106130ca2051SWilly Tarreau register long _arg4 asm("a3"); \ 106230ca2051SWilly Tarreau \ 106330ca2051SWilly Tarreau asm volatile ( \ 106430ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 106530ca2051SWilly Tarreau "syscall\n" \ 106630ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 106730ca2051SWilly Tarreau : "=r"(_num), "=r"(_arg4) \ 106830ca2051SWilly Tarreau : "0"(_num), \ 106930ca2051SWilly Tarreau "r"(_arg1) \ 107030ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 107130ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 107230ca2051SWilly Tarreau ); \ 107330ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 107430ca2051SWilly Tarreau }) 107530ca2051SWilly Tarreau 107630ca2051SWilly Tarreau #define my_syscall2(num, arg1, arg2) \ 107730ca2051SWilly Tarreau ({ \ 107830ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 107930ca2051SWilly Tarreau register long _arg1 asm("a0") = (long)(arg1); \ 108030ca2051SWilly Tarreau register long _arg2 asm("a1") = (long)(arg2); \ 108130ca2051SWilly Tarreau register long _arg4 asm("a3"); \ 108230ca2051SWilly Tarreau \ 108330ca2051SWilly Tarreau asm volatile ( \ 108430ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 108530ca2051SWilly Tarreau "syscall\n" \ 108630ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 108730ca2051SWilly Tarreau : "=r"(_num), "=r"(_arg4) \ 108830ca2051SWilly Tarreau : "0"(_num), \ 108930ca2051SWilly Tarreau "r"(_arg1), "r"(_arg2) \ 109030ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 109130ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 109230ca2051SWilly Tarreau ); \ 109330ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 109430ca2051SWilly Tarreau }) 109530ca2051SWilly Tarreau 109630ca2051SWilly Tarreau #define my_syscall3(num, arg1, arg2, arg3) \ 109730ca2051SWilly Tarreau ({ \ 109830ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 109930ca2051SWilly Tarreau register long _arg1 asm("a0") = (long)(arg1); \ 110030ca2051SWilly Tarreau register long _arg2 asm("a1") = (long)(arg2); \ 110130ca2051SWilly Tarreau register long _arg3 asm("a2") = (long)(arg3); \ 110230ca2051SWilly Tarreau register long _arg4 asm("a3"); \ 110330ca2051SWilly Tarreau \ 110430ca2051SWilly Tarreau asm volatile ( \ 110530ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 110630ca2051SWilly Tarreau "syscall\n" \ 110730ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 110830ca2051SWilly Tarreau : "=r"(_num), "=r"(_arg4) \ 110930ca2051SWilly Tarreau : "0"(_num), \ 111030ca2051SWilly Tarreau "r"(_arg1), "r"(_arg2), "r"(_arg3) \ 111130ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 111230ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 111330ca2051SWilly Tarreau ); \ 111430ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 111530ca2051SWilly Tarreau }) 111630ca2051SWilly Tarreau 111730ca2051SWilly Tarreau #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 111830ca2051SWilly Tarreau ({ \ 111930ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 112030ca2051SWilly Tarreau register long _arg1 asm("a0") = (long)(arg1); \ 112130ca2051SWilly Tarreau register long _arg2 asm("a1") = (long)(arg2); \ 112230ca2051SWilly Tarreau register long _arg3 asm("a2") = (long)(arg3); \ 112330ca2051SWilly Tarreau register long _arg4 asm("a3") = (long)(arg4); \ 112430ca2051SWilly Tarreau \ 112530ca2051SWilly Tarreau asm volatile ( \ 112630ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 112730ca2051SWilly Tarreau "syscall\n" \ 112830ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 112930ca2051SWilly Tarreau : "=r" (_num), "=r"(_arg4) \ 113030ca2051SWilly Tarreau : "0"(_num), \ 113130ca2051SWilly Tarreau "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4) \ 113230ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 113330ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 113430ca2051SWilly Tarreau ); \ 113530ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 113630ca2051SWilly Tarreau }) 113730ca2051SWilly Tarreau 113830ca2051SWilly Tarreau #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 113930ca2051SWilly Tarreau ({ \ 114030ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 114130ca2051SWilly Tarreau register long _arg1 asm("a0") = (long)(arg1); \ 114230ca2051SWilly Tarreau register long _arg2 asm("a1") = (long)(arg2); \ 114330ca2051SWilly Tarreau register long _arg3 asm("a2") = (long)(arg3); \ 114430ca2051SWilly Tarreau register long _arg4 asm("a3") = (long)(arg4); \ 114530ca2051SWilly Tarreau register long _arg5 = (long)(arg5); \ 114630ca2051SWilly Tarreau \ 114730ca2051SWilly Tarreau asm volatile ( \ 114830ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 114930ca2051SWilly Tarreau "sw %7, 16($sp)\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), "r"(_arg5) \ 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 /* startup code, note that it's called __start on MIPS */ 116230ca2051SWilly Tarreau asm(".section .text\n" 116330ca2051SWilly Tarreau ".set nomips16\n" 116430ca2051SWilly Tarreau ".global __start\n" 116530ca2051SWilly Tarreau ".set noreorder\n" 116630ca2051SWilly Tarreau ".option pic0\n" 116730ca2051SWilly Tarreau ".ent __start\n" 116830ca2051SWilly Tarreau "__start:\n" 116930ca2051SWilly Tarreau "lw $a0,($sp)\n" // argc was in the stack 117030ca2051SWilly Tarreau "addiu $a1, $sp, 4\n" // argv = sp + 4 117130ca2051SWilly Tarreau "sll $a2, $a0, 2\n" // a2 = argc * 4 117230ca2051SWilly Tarreau "add $a2, $a2, $a1\n" // envp = argv + 4*argc ... 117330ca2051SWilly Tarreau "addiu $a2, $a2, 4\n" // ... + 4 117430ca2051SWilly Tarreau "li $t0, -8\n" 117530ca2051SWilly Tarreau "and $sp, $sp, $t0\n" // sp must be 8-byte aligned 117630ca2051SWilly Tarreau "addiu $sp,$sp,-16\n" // the callee expects to save a0..a3 there! 117730ca2051SWilly Tarreau "jal main\n" // main() returns the status code, we'll exit with it. 117830ca2051SWilly Tarreau "nop\n" // delayed slot 117930ca2051SWilly Tarreau "and $a0, $v0, 0xff\n" // limit exit code to 8 bits 118030ca2051SWilly Tarreau "li $v0, 4001\n" // NR_exit == 4001 118130ca2051SWilly Tarreau "syscall\n" 118230ca2051SWilly Tarreau ".end __start\n" 118330ca2051SWilly Tarreau ""); 118430ca2051SWilly Tarreau 118530ca2051SWilly Tarreau /* fcntl / open */ 118630ca2051SWilly Tarreau #define O_RDONLY 0 118730ca2051SWilly Tarreau #define O_WRONLY 1 118830ca2051SWilly Tarreau #define O_RDWR 2 118930ca2051SWilly Tarreau #define O_APPEND 0x0008 119030ca2051SWilly Tarreau #define O_NONBLOCK 0x0080 119130ca2051SWilly Tarreau #define O_CREAT 0x0100 119230ca2051SWilly Tarreau #define O_TRUNC 0x0200 119330ca2051SWilly Tarreau #define O_EXCL 0x0400 119430ca2051SWilly Tarreau #define O_NOCTTY 0x0800 119530ca2051SWilly Tarreau #define O_DIRECTORY 0x10000 119630ca2051SWilly Tarreau 119730ca2051SWilly Tarreau /* The struct returned by the stat() syscall. 88 bytes are returned by the 119830ca2051SWilly Tarreau * syscall. 119930ca2051SWilly Tarreau */ 120030ca2051SWilly Tarreau struct sys_stat_struct { 120130ca2051SWilly Tarreau unsigned int st_dev; 120230ca2051SWilly Tarreau long st_pad1[3]; 120330ca2051SWilly Tarreau unsigned long st_ino; 120430ca2051SWilly Tarreau unsigned int st_mode; 120530ca2051SWilly Tarreau unsigned int st_nlink; 120630ca2051SWilly Tarreau unsigned int st_uid; 120730ca2051SWilly Tarreau unsigned int st_gid; 120830ca2051SWilly Tarreau unsigned int st_rdev; 120930ca2051SWilly Tarreau long st_pad2[2]; 121030ca2051SWilly Tarreau long st_size; 121130ca2051SWilly Tarreau long st_pad3; 121230ca2051SWilly Tarreau long st_atime; 121330ca2051SWilly Tarreau long st_atime_nsec; 121430ca2051SWilly Tarreau long st_mtime; 121530ca2051SWilly Tarreau long st_mtime_nsec; 121630ca2051SWilly Tarreau long st_ctime; 121730ca2051SWilly Tarreau long st_ctime_nsec; 121830ca2051SWilly Tarreau long st_blksize; 121930ca2051SWilly Tarreau long st_blocks; 122030ca2051SWilly Tarreau long st_pad4[14]; 122130ca2051SWilly Tarreau }; 122230ca2051SWilly Tarreau 1223582e84f7SPranith Kumar #elif defined(__riscv) 1224582e84f7SPranith Kumar 1225582e84f7SPranith Kumar #if __riscv_xlen == 64 1226582e84f7SPranith Kumar #define PTRLOG "3" 1227582e84f7SPranith Kumar #define SZREG "8" 1228582e84f7SPranith Kumar #elif __riscv_xlen == 32 1229582e84f7SPranith Kumar #define PTRLOG "2" 1230582e84f7SPranith Kumar #define SZREG "4" 1231582e84f7SPranith Kumar #endif 1232582e84f7SPranith Kumar 1233582e84f7SPranith Kumar /* Syscalls for RISCV : 1234582e84f7SPranith Kumar * - stack is 16-byte aligned 1235582e84f7SPranith Kumar * - syscall number is passed in a7 1236582e84f7SPranith Kumar * - arguments are in a0, a1, a2, a3, a4, a5 1237582e84f7SPranith Kumar * - the system call is performed by calling ecall 1238582e84f7SPranith Kumar * - syscall return comes in a0 1239582e84f7SPranith Kumar * - the arguments are cast to long and assigned into the target 1240582e84f7SPranith Kumar * registers which are then simply passed as registers to the asm code, 1241582e84f7SPranith Kumar * so that we don't have to experience issues with register constraints. 1242582e84f7SPranith Kumar */ 1243582e84f7SPranith Kumar 1244582e84f7SPranith Kumar #define my_syscall0(num) \ 1245582e84f7SPranith Kumar ({ \ 1246582e84f7SPranith Kumar register long _num asm("a7") = (num); \ 1247582e84f7SPranith Kumar register long _arg1 asm("a0"); \ 1248582e84f7SPranith Kumar \ 1249582e84f7SPranith Kumar asm volatile ( \ 1250582e84f7SPranith Kumar "ecall\n\t" \ 1251582e84f7SPranith Kumar : "=r"(_arg1) \ 1252582e84f7SPranith Kumar : "r"(_num) \ 1253582e84f7SPranith Kumar : "memory", "cc" \ 1254582e84f7SPranith Kumar ); \ 1255582e84f7SPranith Kumar _arg1; \ 1256582e84f7SPranith Kumar }) 1257582e84f7SPranith Kumar 1258582e84f7SPranith Kumar #define my_syscall1(num, arg1) \ 1259582e84f7SPranith Kumar ({ \ 1260582e84f7SPranith Kumar register long _num asm("a7") = (num); \ 1261582e84f7SPranith Kumar register long _arg1 asm("a0") = (long)(arg1); \ 1262582e84f7SPranith Kumar \ 1263582e84f7SPranith Kumar asm volatile ( \ 1264582e84f7SPranith Kumar "ecall\n" \ 1265582e84f7SPranith Kumar : "+r"(_arg1) \ 1266582e84f7SPranith Kumar : "r"(_num) \ 1267582e84f7SPranith Kumar : "memory", "cc" \ 1268582e84f7SPranith Kumar ); \ 1269582e84f7SPranith Kumar _arg1; \ 1270582e84f7SPranith Kumar }) 1271582e84f7SPranith Kumar 1272582e84f7SPranith Kumar #define my_syscall2(num, arg1, arg2) \ 1273582e84f7SPranith Kumar ({ \ 1274582e84f7SPranith Kumar register long _num asm("a7") = (num); \ 1275582e84f7SPranith Kumar register long _arg1 asm("a0") = (long)(arg1); \ 1276582e84f7SPranith Kumar register long _arg2 asm("a1") = (long)(arg2); \ 1277582e84f7SPranith Kumar \ 1278582e84f7SPranith Kumar asm volatile ( \ 1279582e84f7SPranith Kumar "ecall\n" \ 1280582e84f7SPranith Kumar : "+r"(_arg1) \ 1281582e84f7SPranith Kumar : "r"(_arg2), \ 1282582e84f7SPranith Kumar "r"(_num) \ 1283582e84f7SPranith Kumar : "memory", "cc" \ 1284582e84f7SPranith Kumar ); \ 1285582e84f7SPranith Kumar _arg1; \ 1286582e84f7SPranith Kumar }) 1287582e84f7SPranith Kumar 1288582e84f7SPranith Kumar #define my_syscall3(num, arg1, arg2, arg3) \ 1289582e84f7SPranith Kumar ({ \ 1290582e84f7SPranith Kumar register long _num asm("a7") = (num); \ 1291582e84f7SPranith Kumar register long _arg1 asm("a0") = (long)(arg1); \ 1292582e84f7SPranith Kumar register long _arg2 asm("a1") = (long)(arg2); \ 1293582e84f7SPranith Kumar register long _arg3 asm("a2") = (long)(arg3); \ 1294582e84f7SPranith Kumar \ 1295582e84f7SPranith Kumar asm volatile ( \ 1296582e84f7SPranith Kumar "ecall\n\t" \ 1297582e84f7SPranith Kumar : "+r"(_arg1) \ 1298582e84f7SPranith Kumar : "r"(_arg2), "r"(_arg3), \ 1299582e84f7SPranith Kumar "r"(_num) \ 1300582e84f7SPranith Kumar : "memory", "cc" \ 1301582e84f7SPranith Kumar ); \ 1302582e84f7SPranith Kumar _arg1; \ 1303582e84f7SPranith Kumar }) 1304582e84f7SPranith Kumar 1305582e84f7SPranith Kumar #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 1306582e84f7SPranith Kumar ({ \ 1307582e84f7SPranith Kumar register long _num asm("a7") = (num); \ 1308582e84f7SPranith Kumar register long _arg1 asm("a0") = (long)(arg1); \ 1309582e84f7SPranith Kumar register long _arg2 asm("a1") = (long)(arg2); \ 1310582e84f7SPranith Kumar register long _arg3 asm("a2") = (long)(arg3); \ 1311582e84f7SPranith Kumar register long _arg4 asm("a3") = (long)(arg4); \ 1312582e84f7SPranith Kumar \ 1313582e84f7SPranith Kumar asm volatile ( \ 1314582e84f7SPranith Kumar "ecall\n" \ 1315582e84f7SPranith Kumar : "+r"(_arg1) \ 1316582e84f7SPranith Kumar : "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 1317582e84f7SPranith Kumar "r"(_num) \ 1318582e84f7SPranith Kumar : "memory", "cc" \ 1319582e84f7SPranith Kumar ); \ 1320582e84f7SPranith Kumar _arg1; \ 1321582e84f7SPranith Kumar }) 1322582e84f7SPranith Kumar 1323582e84f7SPranith Kumar #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 1324582e84f7SPranith Kumar ({ \ 1325582e84f7SPranith Kumar register long _num asm("a7") = (num); \ 1326582e84f7SPranith Kumar register long _arg1 asm("a0") = (long)(arg1); \ 1327582e84f7SPranith Kumar register long _arg2 asm("a1") = (long)(arg2); \ 1328582e84f7SPranith Kumar register long _arg3 asm("a2") = (long)(arg3); \ 1329582e84f7SPranith Kumar register long _arg4 asm("a3") = (long)(arg4); \ 1330582e84f7SPranith Kumar register long _arg5 asm("a4") = (long)(arg5); \ 1331582e84f7SPranith Kumar \ 1332582e84f7SPranith Kumar asm volatile ( \ 1333582e84f7SPranith Kumar "ecall\n" \ 1334582e84f7SPranith Kumar : "+r"(_arg1) \ 1335582e84f7SPranith Kumar : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 1336582e84f7SPranith Kumar "r"(_num) \ 1337582e84f7SPranith Kumar : "memory", "cc" \ 1338582e84f7SPranith Kumar ); \ 1339582e84f7SPranith Kumar _arg1; \ 1340582e84f7SPranith Kumar }) 1341582e84f7SPranith Kumar 1342582e84f7SPranith Kumar #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 1343582e84f7SPranith Kumar ({ \ 1344582e84f7SPranith Kumar register long _num asm("a7") = (num); \ 1345582e84f7SPranith Kumar register long _arg1 asm("a0") = (long)(arg1); \ 1346582e84f7SPranith Kumar register long _arg2 asm("a1") = (long)(arg2); \ 1347582e84f7SPranith Kumar register long _arg3 asm("a2") = (long)(arg3); \ 1348582e84f7SPranith Kumar register long _arg4 asm("a3") = (long)(arg4); \ 1349582e84f7SPranith Kumar register long _arg5 asm("a4") = (long)(arg5); \ 1350582e84f7SPranith Kumar register long _arg6 asm("a5") = (long)(arg6); \ 1351582e84f7SPranith Kumar \ 1352582e84f7SPranith Kumar asm volatile ( \ 1353582e84f7SPranith Kumar "ecall\n" \ 1354582e84f7SPranith Kumar : "+r"(_arg1) \ 1355582e84f7SPranith Kumar : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), "r"(_arg6), \ 1356582e84f7SPranith Kumar "r"(_num) \ 1357582e84f7SPranith Kumar : "memory", "cc" \ 1358582e84f7SPranith Kumar ); \ 1359582e84f7SPranith Kumar _arg1; \ 1360582e84f7SPranith Kumar }) 1361582e84f7SPranith Kumar 1362582e84f7SPranith Kumar /* startup code */ 1363582e84f7SPranith Kumar asm(".section .text\n" 1364582e84f7SPranith Kumar ".global _start\n" 1365582e84f7SPranith Kumar "_start:\n" 1366582e84f7SPranith Kumar ".option push\n" 1367582e84f7SPranith Kumar ".option norelax\n" 1368582e84f7SPranith Kumar "lla gp, __global_pointer$\n" 1369582e84f7SPranith Kumar ".option pop\n" 1370582e84f7SPranith Kumar "ld a0, 0(sp)\n" // argc (a0) was in the stack 1371582e84f7SPranith Kumar "add a1, sp, "SZREG"\n" // argv (a1) = sp 1372582e84f7SPranith Kumar "slli a2, a0, "PTRLOG"\n" // envp (a2) = SZREG*argc ... 1373582e84f7SPranith Kumar "add a2, a2, "SZREG"\n" // + SZREG (skip null) 1374582e84f7SPranith Kumar "add a2,a2,a1\n" // + argv 1375582e84f7SPranith Kumar "andi sp,a1,-16\n" // sp must be 16-byte aligned 1376582e84f7SPranith Kumar "call main\n" // main() returns the status code, we'll exit with it. 1377582e84f7SPranith Kumar "andi a0, a0, 0xff\n" // limit exit code to 8 bits 1378582e84f7SPranith Kumar "li a7, 93\n" // NR_exit == 93 1379582e84f7SPranith Kumar "ecall\n" 1380582e84f7SPranith Kumar ""); 1381582e84f7SPranith Kumar 1382582e84f7SPranith Kumar /* fcntl / open */ 1383582e84f7SPranith Kumar #define O_RDONLY 0 1384582e84f7SPranith Kumar #define O_WRONLY 1 1385582e84f7SPranith Kumar #define O_RDWR 2 1386582e84f7SPranith Kumar #define O_CREAT 0x100 1387582e84f7SPranith Kumar #define O_EXCL 0x200 1388582e84f7SPranith Kumar #define O_NOCTTY 0x400 1389582e84f7SPranith Kumar #define O_TRUNC 0x1000 1390582e84f7SPranith Kumar #define O_APPEND 0x2000 1391582e84f7SPranith Kumar #define O_NONBLOCK 0x4000 1392582e84f7SPranith Kumar #define O_DIRECTORY 0x200000 1393582e84f7SPranith Kumar 1394582e84f7SPranith Kumar struct sys_stat_struct { 1395582e84f7SPranith Kumar unsigned long st_dev; /* Device. */ 1396582e84f7SPranith Kumar unsigned long st_ino; /* File serial number. */ 1397582e84f7SPranith Kumar unsigned int st_mode; /* File mode. */ 1398582e84f7SPranith Kumar unsigned int st_nlink; /* Link count. */ 1399582e84f7SPranith Kumar unsigned int st_uid; /* User ID of the file's owner. */ 1400582e84f7SPranith Kumar unsigned int st_gid; /* Group ID of the file's group. */ 1401582e84f7SPranith Kumar unsigned long st_rdev; /* Device number, if device. */ 1402582e84f7SPranith Kumar unsigned long __pad1; 1403582e84f7SPranith Kumar long st_size; /* Size of file, in bytes. */ 1404582e84f7SPranith Kumar int st_blksize; /* Optimal block size for I/O. */ 1405582e84f7SPranith Kumar int __pad2; 1406582e84f7SPranith Kumar long st_blocks; /* Number 512-byte blocks allocated. */ 1407582e84f7SPranith Kumar long st_atime; /* Time of last access. */ 1408582e84f7SPranith Kumar unsigned long st_atime_nsec; 1409582e84f7SPranith Kumar long st_mtime; /* Time of last modification. */ 1410582e84f7SPranith Kumar unsigned long st_mtime_nsec; 1411582e84f7SPranith Kumar long st_ctime; /* Time of last status change. */ 1412582e84f7SPranith Kumar unsigned long st_ctime_nsec; 1413582e84f7SPranith Kumar unsigned int __unused4; 1414582e84f7SPranith Kumar unsigned int __unused5; 1415582e84f7SPranith Kumar }; 1416582e84f7SPranith Kumar 141730ca2051SWilly Tarreau #endif 141830ca2051SWilly Tarreau 141930ca2051SWilly Tarreau 142030ca2051SWilly Tarreau /* Below are the C functions used to declare the raw syscalls. They try to be 142130ca2051SWilly Tarreau * architecture-agnostic, and return either a success or -errno. Declaring them 142230ca2051SWilly Tarreau * static will lead to them being inlined in most cases, but it's still possible 142330ca2051SWilly Tarreau * to reference them by a pointer if needed. 142430ca2051SWilly Tarreau */ 142530ca2051SWilly Tarreau static __attribute__((unused)) 142630ca2051SWilly Tarreau void *sys_brk(void *addr) 142730ca2051SWilly Tarreau { 142830ca2051SWilly Tarreau return (void *)my_syscall1(__NR_brk, addr); 142930ca2051SWilly Tarreau } 143030ca2051SWilly Tarreau 143130ca2051SWilly Tarreau static __attribute__((noreturn,unused)) 143230ca2051SWilly Tarreau void sys_exit(int status) 143330ca2051SWilly Tarreau { 143430ca2051SWilly Tarreau my_syscall1(__NR_exit, status & 255); 143530ca2051SWilly Tarreau while(1); // shut the "noreturn" warnings. 143630ca2051SWilly Tarreau } 143730ca2051SWilly Tarreau 143830ca2051SWilly Tarreau static __attribute__((unused)) 143930ca2051SWilly Tarreau int sys_chdir(const char *path) 144030ca2051SWilly Tarreau { 144130ca2051SWilly Tarreau return my_syscall1(__NR_chdir, path); 144230ca2051SWilly Tarreau } 144330ca2051SWilly Tarreau 144430ca2051SWilly Tarreau static __attribute__((unused)) 144530ca2051SWilly Tarreau int sys_chmod(const char *path, mode_t mode) 144630ca2051SWilly Tarreau { 144730ca2051SWilly Tarreau #ifdef __NR_fchmodat 144830ca2051SWilly Tarreau return my_syscall4(__NR_fchmodat, AT_FDCWD, path, mode, 0); 144935635d7fSWilly Tarreau #elif defined(__NR_chmod) 145030ca2051SWilly Tarreau return my_syscall2(__NR_chmod, path, mode); 145135635d7fSWilly Tarreau #else 145235635d7fSWilly Tarreau #error Neither __NR_fchmodat nor __NR_chmod defined, cannot implement sys_chmod() 145330ca2051SWilly Tarreau #endif 145430ca2051SWilly Tarreau } 145530ca2051SWilly Tarreau 145630ca2051SWilly Tarreau static __attribute__((unused)) 145730ca2051SWilly Tarreau int sys_chown(const char *path, uid_t owner, gid_t group) 145830ca2051SWilly Tarreau { 145930ca2051SWilly Tarreau #ifdef __NR_fchownat 146030ca2051SWilly Tarreau return my_syscall5(__NR_fchownat, AT_FDCWD, path, owner, group, 0); 146135635d7fSWilly Tarreau #elif defined(__NR_chown) 146230ca2051SWilly Tarreau return my_syscall3(__NR_chown, path, owner, group); 146335635d7fSWilly Tarreau #else 146435635d7fSWilly Tarreau #error Neither __NR_fchownat nor __NR_chown defined, cannot implement sys_chown() 146530ca2051SWilly Tarreau #endif 146630ca2051SWilly Tarreau } 146730ca2051SWilly Tarreau 146830ca2051SWilly Tarreau static __attribute__((unused)) 146930ca2051SWilly Tarreau int sys_chroot(const char *path) 147030ca2051SWilly Tarreau { 147130ca2051SWilly Tarreau return my_syscall1(__NR_chroot, path); 147230ca2051SWilly Tarreau } 147330ca2051SWilly Tarreau 147430ca2051SWilly Tarreau static __attribute__((unused)) 147530ca2051SWilly Tarreau int sys_close(int fd) 147630ca2051SWilly Tarreau { 147730ca2051SWilly Tarreau return my_syscall1(__NR_close, fd); 147830ca2051SWilly Tarreau } 147930ca2051SWilly Tarreau 148030ca2051SWilly Tarreau static __attribute__((unused)) 148130ca2051SWilly Tarreau int sys_dup(int fd) 148230ca2051SWilly Tarreau { 148330ca2051SWilly Tarreau return my_syscall1(__NR_dup, fd); 148430ca2051SWilly Tarreau } 148530ca2051SWilly Tarreau 148679f220e5SWilly Tarreau #ifdef __NR_dup3 148779f220e5SWilly Tarreau static __attribute__((unused)) 148879f220e5SWilly Tarreau int sys_dup3(int old, int new, int flags) 148979f220e5SWilly Tarreau { 149079f220e5SWilly Tarreau return my_syscall3(__NR_dup3, old, new, flags); 149179f220e5SWilly Tarreau } 149279f220e5SWilly Tarreau #endif 149379f220e5SWilly Tarreau 149430ca2051SWilly Tarreau static __attribute__((unused)) 149530ca2051SWilly Tarreau int sys_dup2(int old, int new) 149630ca2051SWilly Tarreau { 149779f220e5SWilly Tarreau #ifdef __NR_dup3 149879f220e5SWilly Tarreau return my_syscall3(__NR_dup3, old, new, 0); 149935635d7fSWilly Tarreau #elif defined(__NR_dup2) 150030ca2051SWilly Tarreau return my_syscall2(__NR_dup2, old, new); 150135635d7fSWilly Tarreau #else 150235635d7fSWilly Tarreau #error Neither __NR_dup3 nor __NR_dup2 defined, cannot implement sys_dup2() 150379f220e5SWilly Tarreau #endif 150430ca2051SWilly Tarreau } 150530ca2051SWilly Tarreau 150630ca2051SWilly Tarreau static __attribute__((unused)) 150730ca2051SWilly Tarreau int sys_execve(const char *filename, char *const argv[], char *const envp[]) 150830ca2051SWilly Tarreau { 150930ca2051SWilly Tarreau return my_syscall3(__NR_execve, filename, argv, envp); 151030ca2051SWilly Tarreau } 151130ca2051SWilly Tarreau 151230ca2051SWilly Tarreau static __attribute__((unused)) 151330ca2051SWilly Tarreau pid_t sys_fork(void) 151430ca2051SWilly Tarreau { 1515be60ca41SWilly Tarreau #ifdef __NR_clone 1516be60ca41SWilly Tarreau /* note: some archs only have clone() and not fork(). Different archs 1517be60ca41SWilly Tarreau * have a different API, but most archs have the flags on first arg and 1518be60ca41SWilly Tarreau * will not use the rest with no other flag. 1519be60ca41SWilly Tarreau */ 1520be60ca41SWilly Tarreau return my_syscall5(__NR_clone, SIGCHLD, 0, 0, 0, 0); 152135635d7fSWilly Tarreau #elif defined(__NR_fork) 152230ca2051SWilly Tarreau return my_syscall0(__NR_fork); 152335635d7fSWilly Tarreau #else 152435635d7fSWilly Tarreau #error Neither __NR_clone nor __NR_fork defined, cannot implement sys_fork() 1525be60ca41SWilly Tarreau #endif 152630ca2051SWilly Tarreau } 152730ca2051SWilly Tarreau 152830ca2051SWilly Tarreau static __attribute__((unused)) 152930ca2051SWilly Tarreau int sys_fsync(int fd) 153030ca2051SWilly Tarreau { 153130ca2051SWilly Tarreau return my_syscall1(__NR_fsync, fd); 153230ca2051SWilly Tarreau } 153330ca2051SWilly Tarreau 153430ca2051SWilly Tarreau static __attribute__((unused)) 153530ca2051SWilly Tarreau int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count) 153630ca2051SWilly Tarreau { 153730ca2051SWilly Tarreau return my_syscall3(__NR_getdents64, fd, dirp, count); 153830ca2051SWilly Tarreau } 153930ca2051SWilly Tarreau 154030ca2051SWilly Tarreau static __attribute__((unused)) 1541c0c7c103SWilly Tarreau pid_t sys_getpgid(pid_t pid) 1542c0c7c103SWilly Tarreau { 1543c0c7c103SWilly Tarreau return my_syscall1(__NR_getpgid, pid); 1544c0c7c103SWilly Tarreau } 1545c0c7c103SWilly Tarreau 1546c0c7c103SWilly Tarreau static __attribute__((unused)) 154730ca2051SWilly Tarreau pid_t sys_getpgrp(void) 154830ca2051SWilly Tarreau { 1549c0c7c103SWilly Tarreau return sys_getpgid(0); 155030ca2051SWilly Tarreau } 155130ca2051SWilly Tarreau 155230ca2051SWilly Tarreau static __attribute__((unused)) 155330ca2051SWilly Tarreau pid_t sys_getpid(void) 155430ca2051SWilly Tarreau { 155530ca2051SWilly Tarreau return my_syscall0(__NR_getpid); 155630ca2051SWilly Tarreau } 155730ca2051SWilly Tarreau 155830ca2051SWilly Tarreau static __attribute__((unused)) 155930ca2051SWilly Tarreau int sys_gettimeofday(struct timeval *tv, struct timezone *tz) 156030ca2051SWilly Tarreau { 156130ca2051SWilly Tarreau return my_syscall2(__NR_gettimeofday, tv, tz); 156230ca2051SWilly Tarreau } 156330ca2051SWilly Tarreau 156430ca2051SWilly Tarreau static __attribute__((unused)) 156530ca2051SWilly Tarreau int sys_ioctl(int fd, unsigned long req, void *value) 156630ca2051SWilly Tarreau { 156730ca2051SWilly Tarreau return my_syscall3(__NR_ioctl, fd, req, value); 156830ca2051SWilly Tarreau } 156930ca2051SWilly Tarreau 157030ca2051SWilly Tarreau static __attribute__((unused)) 157130ca2051SWilly Tarreau int sys_kill(pid_t pid, int signal) 157230ca2051SWilly Tarreau { 157330ca2051SWilly Tarreau return my_syscall2(__NR_kill, pid, signal); 157430ca2051SWilly Tarreau } 157530ca2051SWilly Tarreau 157630ca2051SWilly Tarreau static __attribute__((unused)) 157730ca2051SWilly Tarreau int sys_link(const char *old, const char *new) 157830ca2051SWilly Tarreau { 157930ca2051SWilly Tarreau #ifdef __NR_linkat 158030ca2051SWilly Tarreau return my_syscall5(__NR_linkat, AT_FDCWD, old, AT_FDCWD, new, 0); 158135635d7fSWilly Tarreau #elif defined(__NR_link) 158230ca2051SWilly Tarreau return my_syscall2(__NR_link, old, new); 158335635d7fSWilly Tarreau #else 158435635d7fSWilly Tarreau #error Neither __NR_linkat nor __NR_link defined, cannot implement sys_link() 158530ca2051SWilly Tarreau #endif 158630ca2051SWilly Tarreau } 158730ca2051SWilly Tarreau 158830ca2051SWilly Tarreau static __attribute__((unused)) 158930ca2051SWilly Tarreau off_t sys_lseek(int fd, off_t offset, int whence) 159030ca2051SWilly Tarreau { 159130ca2051SWilly Tarreau return my_syscall3(__NR_lseek, fd, offset, whence); 159230ca2051SWilly Tarreau } 159330ca2051SWilly Tarreau 159430ca2051SWilly Tarreau static __attribute__((unused)) 159530ca2051SWilly Tarreau int sys_mkdir(const char *path, mode_t mode) 159630ca2051SWilly Tarreau { 159730ca2051SWilly Tarreau #ifdef __NR_mkdirat 159830ca2051SWilly Tarreau return my_syscall3(__NR_mkdirat, AT_FDCWD, path, mode); 159935635d7fSWilly Tarreau #elif defined(__NR_mkdir) 160030ca2051SWilly Tarreau return my_syscall2(__NR_mkdir, path, mode); 160135635d7fSWilly Tarreau #else 160235635d7fSWilly Tarreau #error Neither __NR_mkdirat nor __NR_mkdir defined, cannot implement sys_mkdir() 160330ca2051SWilly Tarreau #endif 160430ca2051SWilly Tarreau } 160530ca2051SWilly Tarreau 160630ca2051SWilly Tarreau static __attribute__((unused)) 160730ca2051SWilly Tarreau long sys_mknod(const char *path, mode_t mode, dev_t dev) 160830ca2051SWilly Tarreau { 160930ca2051SWilly Tarreau #ifdef __NR_mknodat 161030ca2051SWilly Tarreau return my_syscall4(__NR_mknodat, AT_FDCWD, path, mode, dev); 161135635d7fSWilly Tarreau #elif defined(__NR_mknod) 161230ca2051SWilly Tarreau return my_syscall3(__NR_mknod, path, mode, dev); 161335635d7fSWilly Tarreau #else 161435635d7fSWilly Tarreau #error Neither __NR_mknodat nor __NR_mknod defined, cannot implement sys_mknod() 161530ca2051SWilly Tarreau #endif 161630ca2051SWilly Tarreau } 161730ca2051SWilly Tarreau 161830ca2051SWilly Tarreau static __attribute__((unused)) 161930ca2051SWilly Tarreau int sys_mount(const char *src, const char *tgt, const char *fst, 162030ca2051SWilly Tarreau unsigned long flags, const void *data) 162130ca2051SWilly Tarreau { 162230ca2051SWilly Tarreau return my_syscall5(__NR_mount, src, tgt, fst, flags, data); 162330ca2051SWilly Tarreau } 162430ca2051SWilly Tarreau 162530ca2051SWilly Tarreau static __attribute__((unused)) 162630ca2051SWilly Tarreau int sys_open(const char *path, int flags, mode_t mode) 162730ca2051SWilly Tarreau { 162830ca2051SWilly Tarreau #ifdef __NR_openat 162930ca2051SWilly Tarreau return my_syscall4(__NR_openat, AT_FDCWD, path, flags, mode); 163035635d7fSWilly Tarreau #elif defined(__NR_open) 163130ca2051SWilly Tarreau return my_syscall3(__NR_open, path, flags, mode); 163235635d7fSWilly Tarreau #else 163335635d7fSWilly Tarreau #error Neither __NR_openat nor __NR_open defined, cannot implement sys_open() 163430ca2051SWilly Tarreau #endif 163530ca2051SWilly Tarreau } 163630ca2051SWilly Tarreau 163730ca2051SWilly Tarreau static __attribute__((unused)) 163830ca2051SWilly Tarreau int sys_pivot_root(const char *new, const char *old) 163930ca2051SWilly Tarreau { 164030ca2051SWilly Tarreau return my_syscall2(__NR_pivot_root, new, old); 164130ca2051SWilly Tarreau } 164230ca2051SWilly Tarreau 164330ca2051SWilly Tarreau static __attribute__((unused)) 164430ca2051SWilly Tarreau int sys_poll(struct pollfd *fds, int nfds, int timeout) 164530ca2051SWilly Tarreau { 16465b1c827cSWilly Tarreau #if defined(__NR_ppoll) 16475b1c827cSWilly Tarreau struct timespec t; 16485b1c827cSWilly Tarreau 16495b1c827cSWilly Tarreau if (timeout >= 0) { 16505b1c827cSWilly Tarreau t.tv_sec = timeout / 1000; 16515b1c827cSWilly Tarreau t.tv_nsec = (timeout % 1000) * 1000000; 16525b1c827cSWilly Tarreau } 16535b1c827cSWilly Tarreau return my_syscall4(__NR_ppoll, fds, nfds, (timeout >= 0) ? &t : NULL, NULL); 165435635d7fSWilly Tarreau #elif defined(__NR_poll) 165530ca2051SWilly Tarreau return my_syscall3(__NR_poll, fds, nfds, timeout); 165635635d7fSWilly Tarreau #else 165735635d7fSWilly Tarreau #error Neither __NR_ppoll nor __NR_poll defined, cannot implement sys_poll() 16585b1c827cSWilly Tarreau #endif 165930ca2051SWilly Tarreau } 166030ca2051SWilly Tarreau 166130ca2051SWilly Tarreau static __attribute__((unused)) 166230ca2051SWilly Tarreau ssize_t sys_read(int fd, void *buf, size_t count) 166330ca2051SWilly Tarreau { 166430ca2051SWilly Tarreau return my_syscall3(__NR_read, fd, buf, count); 166530ca2051SWilly Tarreau } 166630ca2051SWilly Tarreau 166730ca2051SWilly Tarreau static __attribute__((unused)) 166830ca2051SWilly Tarreau ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg) 166930ca2051SWilly Tarreau { 167030ca2051SWilly Tarreau return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg); 167130ca2051SWilly Tarreau } 167230ca2051SWilly Tarreau 167330ca2051SWilly Tarreau static __attribute__((unused)) 167430ca2051SWilly Tarreau int sys_sched_yield(void) 167530ca2051SWilly Tarreau { 167630ca2051SWilly Tarreau return my_syscall0(__NR_sched_yield); 167730ca2051SWilly Tarreau } 167830ca2051SWilly Tarreau 167930ca2051SWilly Tarreau static __attribute__((unused)) 168030ca2051SWilly Tarreau int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) 168130ca2051SWilly Tarreau { 168230ca2051SWilly Tarreau #if defined(__ARCH_WANT_SYS_OLD_SELECT) && !defined(__NR__newselect) 168330ca2051SWilly Tarreau struct sel_arg_struct { 168430ca2051SWilly Tarreau unsigned long n; 168530ca2051SWilly Tarreau fd_set *r, *w, *e; 168630ca2051SWilly Tarreau struct timeval *t; 168730ca2051SWilly Tarreau } arg = { .n = nfds, .r = rfds, .w = wfds, .e = efds, .t = timeout }; 168830ca2051SWilly Tarreau return my_syscall1(__NR_select, &arg); 168930ca2051SWilly Tarreau #elif defined(__ARCH_WANT_SYS_PSELECT6) && defined(__NR_pselect6) 169030ca2051SWilly Tarreau struct timespec t; 169130ca2051SWilly Tarreau 169230ca2051SWilly Tarreau if (timeout) { 169330ca2051SWilly Tarreau t.tv_sec = timeout->tv_sec; 169430ca2051SWilly Tarreau t.tv_nsec = timeout->tv_usec * 1000; 169530ca2051SWilly Tarreau } 169630ca2051SWilly Tarreau return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL); 169735635d7fSWilly Tarreau #elif defined(__NR__newselect) || defined(__NR_select) 169830ca2051SWilly Tarreau #ifndef __NR__newselect 169930ca2051SWilly Tarreau #define __NR__newselect __NR_select 170030ca2051SWilly Tarreau #endif 170130ca2051SWilly Tarreau return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout); 170235635d7fSWilly Tarreau #else 170335635d7fSWilly Tarreau #error None of __NR_select, __NR_pselect6, nor __NR__newselect defined, cannot implement sys_select() 170430ca2051SWilly Tarreau #endif 170530ca2051SWilly Tarreau } 170630ca2051SWilly Tarreau 170730ca2051SWilly Tarreau static __attribute__((unused)) 170830ca2051SWilly Tarreau int sys_setpgid(pid_t pid, pid_t pgid) 170930ca2051SWilly Tarreau { 171030ca2051SWilly Tarreau return my_syscall2(__NR_setpgid, pid, pgid); 171130ca2051SWilly Tarreau } 171230ca2051SWilly Tarreau 171330ca2051SWilly Tarreau static __attribute__((unused)) 171430ca2051SWilly Tarreau pid_t sys_setsid(void) 171530ca2051SWilly Tarreau { 171630ca2051SWilly Tarreau return my_syscall0(__NR_setsid); 171730ca2051SWilly Tarreau } 171830ca2051SWilly Tarreau 171930ca2051SWilly Tarreau static __attribute__((unused)) 172030ca2051SWilly Tarreau int sys_stat(const char *path, struct stat *buf) 172130ca2051SWilly Tarreau { 172230ca2051SWilly Tarreau struct sys_stat_struct stat; 172330ca2051SWilly Tarreau long ret; 172430ca2051SWilly Tarreau 172530ca2051SWilly Tarreau #ifdef __NR_newfstatat 172630ca2051SWilly Tarreau /* only solution for arm64 */ 172730ca2051SWilly Tarreau ret = my_syscall4(__NR_newfstatat, AT_FDCWD, path, &stat, 0); 172835635d7fSWilly Tarreau #elif defined(__NR_stat) 172930ca2051SWilly Tarreau ret = my_syscall2(__NR_stat, path, &stat); 173035635d7fSWilly Tarreau #else 173135635d7fSWilly Tarreau #error Neither __NR_newfstatat nor __NR_stat defined, cannot implement sys_stat() 173230ca2051SWilly Tarreau #endif 173330ca2051SWilly Tarreau buf->st_dev = stat.st_dev; 173430ca2051SWilly Tarreau buf->st_ino = stat.st_ino; 173530ca2051SWilly Tarreau buf->st_mode = stat.st_mode; 173630ca2051SWilly Tarreau buf->st_nlink = stat.st_nlink; 173730ca2051SWilly Tarreau buf->st_uid = stat.st_uid; 173830ca2051SWilly Tarreau buf->st_gid = stat.st_gid; 173930ca2051SWilly Tarreau buf->st_rdev = stat.st_rdev; 174030ca2051SWilly Tarreau buf->st_size = stat.st_size; 174130ca2051SWilly Tarreau buf->st_blksize = stat.st_blksize; 174230ca2051SWilly Tarreau buf->st_blocks = stat.st_blocks; 174330ca2051SWilly Tarreau buf->st_atime = stat.st_atime; 174430ca2051SWilly Tarreau buf->st_mtime = stat.st_mtime; 174530ca2051SWilly Tarreau buf->st_ctime = stat.st_ctime; 174630ca2051SWilly Tarreau return ret; 174730ca2051SWilly Tarreau } 174830ca2051SWilly Tarreau 174930ca2051SWilly Tarreau 175030ca2051SWilly Tarreau static __attribute__((unused)) 175130ca2051SWilly Tarreau int sys_symlink(const char *old, const char *new) 175230ca2051SWilly Tarreau { 175330ca2051SWilly Tarreau #ifdef __NR_symlinkat 175430ca2051SWilly Tarreau return my_syscall3(__NR_symlinkat, old, AT_FDCWD, new); 175535635d7fSWilly Tarreau #elif defined(__NR_symlink) 175630ca2051SWilly Tarreau return my_syscall2(__NR_symlink, old, new); 175735635d7fSWilly Tarreau #else 175835635d7fSWilly Tarreau #error Neither __NR_symlinkat nor __NR_symlink defined, cannot implement sys_symlink() 175930ca2051SWilly Tarreau #endif 176030ca2051SWilly Tarreau } 176130ca2051SWilly Tarreau 176230ca2051SWilly Tarreau static __attribute__((unused)) 176330ca2051SWilly Tarreau mode_t sys_umask(mode_t mode) 176430ca2051SWilly Tarreau { 176530ca2051SWilly Tarreau return my_syscall1(__NR_umask, mode); 176630ca2051SWilly Tarreau } 176730ca2051SWilly Tarreau 176830ca2051SWilly Tarreau static __attribute__((unused)) 176930ca2051SWilly Tarreau int sys_umount2(const char *path, int flags) 177030ca2051SWilly Tarreau { 177130ca2051SWilly Tarreau return my_syscall2(__NR_umount2, path, flags); 177230ca2051SWilly Tarreau } 177330ca2051SWilly Tarreau 177430ca2051SWilly Tarreau static __attribute__((unused)) 177530ca2051SWilly Tarreau int sys_unlink(const char *path) 177630ca2051SWilly Tarreau { 177730ca2051SWilly Tarreau #ifdef __NR_unlinkat 177830ca2051SWilly Tarreau return my_syscall3(__NR_unlinkat, AT_FDCWD, path, 0); 177935635d7fSWilly Tarreau #elif defined(__NR_unlink) 178030ca2051SWilly Tarreau return my_syscall1(__NR_unlink, path); 178135635d7fSWilly Tarreau #else 178235635d7fSWilly Tarreau #error Neither __NR_unlinkat nor __NR_unlink defined, cannot implement sys_unlink() 178330ca2051SWilly Tarreau #endif 178430ca2051SWilly Tarreau } 178530ca2051SWilly Tarreau 178630ca2051SWilly Tarreau static __attribute__((unused)) 178730ca2051SWilly Tarreau pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage) 178830ca2051SWilly Tarreau { 178930ca2051SWilly Tarreau return my_syscall4(__NR_wait4, pid, status, options, rusage); 179030ca2051SWilly Tarreau } 179130ca2051SWilly Tarreau 179230ca2051SWilly Tarreau static __attribute__((unused)) 179330ca2051SWilly Tarreau pid_t sys_waitpid(pid_t pid, int *status, int options) 179430ca2051SWilly Tarreau { 179530ca2051SWilly Tarreau return sys_wait4(pid, status, options, 0); 179630ca2051SWilly Tarreau } 179730ca2051SWilly Tarreau 179830ca2051SWilly Tarreau static __attribute__((unused)) 179930ca2051SWilly Tarreau pid_t sys_wait(int *status) 180030ca2051SWilly Tarreau { 180130ca2051SWilly Tarreau return sys_waitpid(-1, status, 0); 180230ca2051SWilly Tarreau } 180330ca2051SWilly Tarreau 180430ca2051SWilly Tarreau static __attribute__((unused)) 180530ca2051SWilly Tarreau ssize_t sys_write(int fd, const void *buf, size_t count) 180630ca2051SWilly Tarreau { 180730ca2051SWilly Tarreau return my_syscall3(__NR_write, fd, buf, count); 180830ca2051SWilly Tarreau } 180930ca2051SWilly Tarreau 181030ca2051SWilly Tarreau 181130ca2051SWilly Tarreau /* Below are the libc-compatible syscalls which return x or -1 and set errno. 181230ca2051SWilly Tarreau * They rely on the functions above. Similarly they're marked static so that it 181330ca2051SWilly Tarreau * is possible to assign pointers to them if needed. 181430ca2051SWilly Tarreau */ 181530ca2051SWilly Tarreau 181630ca2051SWilly Tarreau static __attribute__((unused)) 181730ca2051SWilly Tarreau int brk(void *addr) 181830ca2051SWilly Tarreau { 181930ca2051SWilly Tarreau void *ret = sys_brk(addr); 182030ca2051SWilly Tarreau 182130ca2051SWilly Tarreau if (!ret) { 182230ca2051SWilly Tarreau SET_ERRNO(ENOMEM); 182330ca2051SWilly Tarreau return -1; 182430ca2051SWilly Tarreau } 182530ca2051SWilly Tarreau return 0; 182630ca2051SWilly Tarreau } 182730ca2051SWilly Tarreau 182830ca2051SWilly Tarreau static __attribute__((noreturn,unused)) 182930ca2051SWilly Tarreau void exit(int status) 183030ca2051SWilly Tarreau { 183130ca2051SWilly Tarreau sys_exit(status); 183230ca2051SWilly Tarreau } 183330ca2051SWilly Tarreau 183430ca2051SWilly Tarreau static __attribute__((unused)) 183530ca2051SWilly Tarreau int chdir(const char *path) 183630ca2051SWilly Tarreau { 183730ca2051SWilly Tarreau int ret = sys_chdir(path); 183830ca2051SWilly Tarreau 183930ca2051SWilly Tarreau if (ret < 0) { 184030ca2051SWilly Tarreau SET_ERRNO(-ret); 184130ca2051SWilly Tarreau ret = -1; 184230ca2051SWilly Tarreau } 184330ca2051SWilly Tarreau return ret; 184430ca2051SWilly Tarreau } 184530ca2051SWilly Tarreau 184630ca2051SWilly Tarreau static __attribute__((unused)) 184730ca2051SWilly Tarreau int chmod(const char *path, mode_t mode) 184830ca2051SWilly Tarreau { 184930ca2051SWilly Tarreau int ret = sys_chmod(path, mode); 185030ca2051SWilly Tarreau 185130ca2051SWilly Tarreau if (ret < 0) { 185230ca2051SWilly Tarreau SET_ERRNO(-ret); 185330ca2051SWilly Tarreau ret = -1; 185430ca2051SWilly Tarreau } 185530ca2051SWilly Tarreau return ret; 185630ca2051SWilly Tarreau } 185730ca2051SWilly Tarreau 185830ca2051SWilly Tarreau static __attribute__((unused)) 185930ca2051SWilly Tarreau int chown(const char *path, uid_t owner, gid_t group) 186030ca2051SWilly Tarreau { 186130ca2051SWilly Tarreau int ret = sys_chown(path, owner, group); 186230ca2051SWilly Tarreau 186330ca2051SWilly Tarreau if (ret < 0) { 186430ca2051SWilly Tarreau SET_ERRNO(-ret); 186530ca2051SWilly Tarreau ret = -1; 186630ca2051SWilly Tarreau } 186730ca2051SWilly Tarreau return ret; 186830ca2051SWilly Tarreau } 186930ca2051SWilly Tarreau 187030ca2051SWilly Tarreau static __attribute__((unused)) 187130ca2051SWilly Tarreau int chroot(const char *path) 187230ca2051SWilly Tarreau { 187330ca2051SWilly Tarreau int ret = sys_chroot(path); 187430ca2051SWilly Tarreau 187530ca2051SWilly Tarreau if (ret < 0) { 187630ca2051SWilly Tarreau SET_ERRNO(-ret); 187730ca2051SWilly Tarreau ret = -1; 187830ca2051SWilly Tarreau } 187930ca2051SWilly Tarreau return ret; 188030ca2051SWilly Tarreau } 188130ca2051SWilly Tarreau 188230ca2051SWilly Tarreau static __attribute__((unused)) 188330ca2051SWilly Tarreau int close(int fd) 188430ca2051SWilly Tarreau { 188530ca2051SWilly Tarreau int ret = sys_close(fd); 188630ca2051SWilly Tarreau 188730ca2051SWilly Tarreau if (ret < 0) { 188830ca2051SWilly Tarreau SET_ERRNO(-ret); 188930ca2051SWilly Tarreau ret = -1; 189030ca2051SWilly Tarreau } 189130ca2051SWilly Tarreau return ret; 189230ca2051SWilly Tarreau } 189330ca2051SWilly Tarreau 189430ca2051SWilly Tarreau static __attribute__((unused)) 1895c261145aSWilly Tarreau int dup(int fd) 1896c261145aSWilly Tarreau { 1897c261145aSWilly Tarreau int ret = sys_dup(fd); 1898c261145aSWilly Tarreau 1899c261145aSWilly Tarreau if (ret < 0) { 1900c261145aSWilly Tarreau SET_ERRNO(-ret); 1901c261145aSWilly Tarreau ret = -1; 1902c261145aSWilly Tarreau } 1903c261145aSWilly Tarreau return ret; 1904c261145aSWilly Tarreau } 1905c261145aSWilly Tarreau 1906c261145aSWilly Tarreau static __attribute__((unused)) 190730ca2051SWilly Tarreau int dup2(int old, int new) 190830ca2051SWilly Tarreau { 190930ca2051SWilly Tarreau int ret = sys_dup2(old, new); 191030ca2051SWilly Tarreau 191130ca2051SWilly Tarreau if (ret < 0) { 191230ca2051SWilly Tarreau SET_ERRNO(-ret); 191330ca2051SWilly Tarreau ret = -1; 191430ca2051SWilly Tarreau } 191530ca2051SWilly Tarreau return ret; 191630ca2051SWilly Tarreau } 191730ca2051SWilly Tarreau 191879f220e5SWilly Tarreau #ifdef __NR_dup3 191979f220e5SWilly Tarreau static __attribute__((unused)) 192079f220e5SWilly Tarreau int dup3(int old, int new, int flags) 192179f220e5SWilly Tarreau { 192279f220e5SWilly Tarreau int ret = sys_dup3(old, new, flags); 192379f220e5SWilly Tarreau 192479f220e5SWilly Tarreau if (ret < 0) { 192579f220e5SWilly Tarreau SET_ERRNO(-ret); 192679f220e5SWilly Tarreau ret = -1; 192779f220e5SWilly Tarreau } 192879f220e5SWilly Tarreau return ret; 192979f220e5SWilly Tarreau } 193079f220e5SWilly Tarreau #endif 193179f220e5SWilly Tarreau 193230ca2051SWilly Tarreau static __attribute__((unused)) 193330ca2051SWilly Tarreau int execve(const char *filename, char *const argv[], char *const envp[]) 193430ca2051SWilly Tarreau { 193530ca2051SWilly Tarreau int ret = sys_execve(filename, argv, envp); 193630ca2051SWilly Tarreau 193730ca2051SWilly Tarreau if (ret < 0) { 193830ca2051SWilly Tarreau SET_ERRNO(-ret); 193930ca2051SWilly Tarreau ret = -1; 194030ca2051SWilly Tarreau } 194130ca2051SWilly Tarreau return ret; 194230ca2051SWilly Tarreau } 194330ca2051SWilly Tarreau 194430ca2051SWilly Tarreau static __attribute__((unused)) 194530ca2051SWilly Tarreau pid_t fork(void) 194630ca2051SWilly Tarreau { 194730ca2051SWilly Tarreau pid_t ret = sys_fork(); 194830ca2051SWilly Tarreau 194930ca2051SWilly Tarreau if (ret < 0) { 195030ca2051SWilly Tarreau SET_ERRNO(-ret); 195130ca2051SWilly Tarreau ret = -1; 195230ca2051SWilly Tarreau } 195330ca2051SWilly Tarreau return ret; 195430ca2051SWilly Tarreau } 195530ca2051SWilly Tarreau 195630ca2051SWilly Tarreau static __attribute__((unused)) 195730ca2051SWilly Tarreau int fsync(int fd) 195830ca2051SWilly Tarreau { 195930ca2051SWilly Tarreau int ret = sys_fsync(fd); 196030ca2051SWilly Tarreau 196130ca2051SWilly Tarreau if (ret < 0) { 196230ca2051SWilly Tarreau SET_ERRNO(-ret); 196330ca2051SWilly Tarreau ret = -1; 196430ca2051SWilly Tarreau } 196530ca2051SWilly Tarreau return ret; 196630ca2051SWilly Tarreau } 196730ca2051SWilly Tarreau 196830ca2051SWilly Tarreau static __attribute__((unused)) 196930ca2051SWilly Tarreau int getdents64(int fd, struct linux_dirent64 *dirp, int count) 197030ca2051SWilly Tarreau { 197130ca2051SWilly Tarreau int ret = sys_getdents64(fd, dirp, count); 197230ca2051SWilly Tarreau 197330ca2051SWilly Tarreau if (ret < 0) { 197430ca2051SWilly Tarreau SET_ERRNO(-ret); 197530ca2051SWilly Tarreau ret = -1; 197630ca2051SWilly Tarreau } 197730ca2051SWilly Tarreau return ret; 197830ca2051SWilly Tarreau } 197930ca2051SWilly Tarreau 198030ca2051SWilly Tarreau static __attribute__((unused)) 1981c0c7c103SWilly Tarreau pid_t getpgid(pid_t pid) 1982c0c7c103SWilly Tarreau { 1983c0c7c103SWilly Tarreau pid_t ret = sys_getpgid(pid); 1984c0c7c103SWilly Tarreau 1985c0c7c103SWilly Tarreau if (ret < 0) { 1986c0c7c103SWilly Tarreau SET_ERRNO(-ret); 1987c0c7c103SWilly Tarreau ret = -1; 1988c0c7c103SWilly Tarreau } 1989c0c7c103SWilly Tarreau return ret; 1990c0c7c103SWilly Tarreau } 1991c0c7c103SWilly Tarreau 1992c0c7c103SWilly Tarreau static __attribute__((unused)) 199330ca2051SWilly Tarreau pid_t getpgrp(void) 199430ca2051SWilly Tarreau { 199530ca2051SWilly Tarreau pid_t ret = sys_getpgrp(); 199630ca2051SWilly Tarreau 199730ca2051SWilly Tarreau if (ret < 0) { 199830ca2051SWilly Tarreau SET_ERRNO(-ret); 199930ca2051SWilly Tarreau ret = -1; 200030ca2051SWilly Tarreau } 200130ca2051SWilly Tarreau return ret; 200230ca2051SWilly Tarreau } 200330ca2051SWilly Tarreau 200430ca2051SWilly Tarreau static __attribute__((unused)) 200530ca2051SWilly Tarreau pid_t getpid(void) 200630ca2051SWilly Tarreau { 200730ca2051SWilly Tarreau pid_t ret = sys_getpid(); 200830ca2051SWilly Tarreau 200930ca2051SWilly Tarreau if (ret < 0) { 201030ca2051SWilly Tarreau SET_ERRNO(-ret); 201130ca2051SWilly Tarreau ret = -1; 201230ca2051SWilly Tarreau } 201330ca2051SWilly Tarreau return ret; 201430ca2051SWilly Tarreau } 201530ca2051SWilly Tarreau 201630ca2051SWilly Tarreau static __attribute__((unused)) 201730ca2051SWilly Tarreau int gettimeofday(struct timeval *tv, struct timezone *tz) 201830ca2051SWilly Tarreau { 201930ca2051SWilly Tarreau int ret = sys_gettimeofday(tv, tz); 202030ca2051SWilly Tarreau 202130ca2051SWilly Tarreau if (ret < 0) { 202230ca2051SWilly Tarreau SET_ERRNO(-ret); 202330ca2051SWilly Tarreau ret = -1; 202430ca2051SWilly Tarreau } 202530ca2051SWilly Tarreau return ret; 202630ca2051SWilly Tarreau } 202730ca2051SWilly Tarreau 202830ca2051SWilly Tarreau static __attribute__((unused)) 202930ca2051SWilly Tarreau int ioctl(int fd, unsigned long req, void *value) 203030ca2051SWilly Tarreau { 203130ca2051SWilly Tarreau int ret = sys_ioctl(fd, req, value); 203230ca2051SWilly Tarreau 203330ca2051SWilly Tarreau if (ret < 0) { 203430ca2051SWilly Tarreau SET_ERRNO(-ret); 203530ca2051SWilly Tarreau ret = -1; 203630ca2051SWilly Tarreau } 203730ca2051SWilly Tarreau return ret; 203830ca2051SWilly Tarreau } 203930ca2051SWilly Tarreau 204030ca2051SWilly Tarreau static __attribute__((unused)) 204130ca2051SWilly Tarreau int kill(pid_t pid, int signal) 204230ca2051SWilly Tarreau { 204330ca2051SWilly Tarreau int ret = sys_kill(pid, signal); 204430ca2051SWilly Tarreau 204530ca2051SWilly Tarreau if (ret < 0) { 204630ca2051SWilly Tarreau SET_ERRNO(-ret); 204730ca2051SWilly Tarreau ret = -1; 204830ca2051SWilly Tarreau } 204930ca2051SWilly Tarreau return ret; 205030ca2051SWilly Tarreau } 205130ca2051SWilly Tarreau 205230ca2051SWilly Tarreau static __attribute__((unused)) 205330ca2051SWilly Tarreau int link(const char *old, const char *new) 205430ca2051SWilly Tarreau { 205530ca2051SWilly Tarreau int ret = sys_link(old, new); 205630ca2051SWilly Tarreau 205730ca2051SWilly Tarreau if (ret < 0) { 205830ca2051SWilly Tarreau SET_ERRNO(-ret); 205930ca2051SWilly Tarreau ret = -1; 206030ca2051SWilly Tarreau } 206130ca2051SWilly Tarreau return ret; 206230ca2051SWilly Tarreau } 206330ca2051SWilly Tarreau 206430ca2051SWilly Tarreau static __attribute__((unused)) 206530ca2051SWilly Tarreau off_t lseek(int fd, off_t offset, int whence) 206630ca2051SWilly Tarreau { 206730ca2051SWilly Tarreau off_t ret = sys_lseek(fd, offset, whence); 206830ca2051SWilly Tarreau 206930ca2051SWilly Tarreau if (ret < 0) { 207030ca2051SWilly Tarreau SET_ERRNO(-ret); 207130ca2051SWilly Tarreau ret = -1; 207230ca2051SWilly Tarreau } 207330ca2051SWilly Tarreau return ret; 207430ca2051SWilly Tarreau } 207530ca2051SWilly Tarreau 207630ca2051SWilly Tarreau static __attribute__((unused)) 207730ca2051SWilly Tarreau int mkdir(const char *path, mode_t mode) 207830ca2051SWilly Tarreau { 207930ca2051SWilly Tarreau int ret = sys_mkdir(path, mode); 208030ca2051SWilly Tarreau 208130ca2051SWilly Tarreau if (ret < 0) { 208230ca2051SWilly Tarreau SET_ERRNO(-ret); 208330ca2051SWilly Tarreau ret = -1; 208430ca2051SWilly Tarreau } 208530ca2051SWilly Tarreau return ret; 208630ca2051SWilly Tarreau } 208730ca2051SWilly Tarreau 208830ca2051SWilly Tarreau static __attribute__((unused)) 208930ca2051SWilly Tarreau int mknod(const char *path, mode_t mode, dev_t dev) 209030ca2051SWilly Tarreau { 209130ca2051SWilly Tarreau int ret = sys_mknod(path, mode, dev); 209230ca2051SWilly Tarreau 209330ca2051SWilly Tarreau if (ret < 0) { 209430ca2051SWilly Tarreau SET_ERRNO(-ret); 209530ca2051SWilly Tarreau ret = -1; 209630ca2051SWilly Tarreau } 209730ca2051SWilly Tarreau return ret; 209830ca2051SWilly Tarreau } 209930ca2051SWilly Tarreau 210030ca2051SWilly Tarreau static __attribute__((unused)) 210130ca2051SWilly Tarreau int mount(const char *src, const char *tgt, 210230ca2051SWilly Tarreau const char *fst, unsigned long flags, 210330ca2051SWilly Tarreau const void *data) 210430ca2051SWilly Tarreau { 210530ca2051SWilly Tarreau int ret = sys_mount(src, tgt, fst, flags, data); 210630ca2051SWilly Tarreau 210730ca2051SWilly Tarreau if (ret < 0) { 210830ca2051SWilly Tarreau SET_ERRNO(-ret); 210930ca2051SWilly Tarreau ret = -1; 211030ca2051SWilly Tarreau } 211130ca2051SWilly Tarreau return ret; 211230ca2051SWilly Tarreau } 211330ca2051SWilly Tarreau 211430ca2051SWilly Tarreau static __attribute__((unused)) 211530ca2051SWilly Tarreau int open(const char *path, int flags, mode_t mode) 211630ca2051SWilly Tarreau { 211730ca2051SWilly Tarreau int ret = sys_open(path, flags, mode); 211830ca2051SWilly Tarreau 211930ca2051SWilly Tarreau if (ret < 0) { 212030ca2051SWilly Tarreau SET_ERRNO(-ret); 212130ca2051SWilly Tarreau ret = -1; 212230ca2051SWilly Tarreau } 212330ca2051SWilly Tarreau return ret; 212430ca2051SWilly Tarreau } 212530ca2051SWilly Tarreau 212630ca2051SWilly Tarreau static __attribute__((unused)) 212730ca2051SWilly Tarreau int pivot_root(const char *new, const char *old) 212830ca2051SWilly Tarreau { 212930ca2051SWilly Tarreau int ret = sys_pivot_root(new, old); 213030ca2051SWilly Tarreau 213130ca2051SWilly Tarreau if (ret < 0) { 213230ca2051SWilly Tarreau SET_ERRNO(-ret); 213330ca2051SWilly Tarreau ret = -1; 213430ca2051SWilly Tarreau } 213530ca2051SWilly Tarreau return ret; 213630ca2051SWilly Tarreau } 213730ca2051SWilly Tarreau 213830ca2051SWilly Tarreau static __attribute__((unused)) 213930ca2051SWilly Tarreau int poll(struct pollfd *fds, int nfds, int timeout) 214030ca2051SWilly Tarreau { 214130ca2051SWilly Tarreau int ret = sys_poll(fds, nfds, timeout); 214230ca2051SWilly Tarreau 214330ca2051SWilly Tarreau if (ret < 0) { 214430ca2051SWilly Tarreau SET_ERRNO(-ret); 214530ca2051SWilly Tarreau ret = -1; 214630ca2051SWilly Tarreau } 214730ca2051SWilly Tarreau return ret; 214830ca2051SWilly Tarreau } 214930ca2051SWilly Tarreau 215030ca2051SWilly Tarreau static __attribute__((unused)) 215130ca2051SWilly Tarreau ssize_t read(int fd, void *buf, size_t count) 215230ca2051SWilly Tarreau { 215330ca2051SWilly Tarreau ssize_t ret = sys_read(fd, buf, count); 215430ca2051SWilly Tarreau 215530ca2051SWilly Tarreau if (ret < 0) { 215630ca2051SWilly Tarreau SET_ERRNO(-ret); 215730ca2051SWilly Tarreau ret = -1; 215830ca2051SWilly Tarreau } 215930ca2051SWilly Tarreau return ret; 216030ca2051SWilly Tarreau } 216130ca2051SWilly Tarreau 216230ca2051SWilly Tarreau static __attribute__((unused)) 216330ca2051SWilly Tarreau int reboot(int cmd) 216430ca2051SWilly Tarreau { 216530ca2051SWilly Tarreau int ret = sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0); 216630ca2051SWilly Tarreau 216730ca2051SWilly Tarreau if (ret < 0) { 216830ca2051SWilly Tarreau SET_ERRNO(-ret); 216930ca2051SWilly Tarreau ret = -1; 217030ca2051SWilly Tarreau } 217130ca2051SWilly Tarreau return ret; 217230ca2051SWilly Tarreau } 217330ca2051SWilly Tarreau 217430ca2051SWilly Tarreau static __attribute__((unused)) 217530ca2051SWilly Tarreau void *sbrk(intptr_t inc) 217630ca2051SWilly Tarreau { 217730ca2051SWilly Tarreau void *ret; 217830ca2051SWilly Tarreau 217930ca2051SWilly Tarreau /* first call to find current end */ 218030ca2051SWilly Tarreau if ((ret = sys_brk(0)) && (sys_brk(ret + inc) == ret + inc)) 218130ca2051SWilly Tarreau return ret + inc; 218230ca2051SWilly Tarreau 218330ca2051SWilly Tarreau SET_ERRNO(ENOMEM); 218430ca2051SWilly Tarreau return (void *)-1; 218530ca2051SWilly Tarreau } 218630ca2051SWilly Tarreau 218730ca2051SWilly Tarreau static __attribute__((unused)) 218830ca2051SWilly Tarreau int sched_yield(void) 218930ca2051SWilly Tarreau { 219030ca2051SWilly Tarreau int ret = sys_sched_yield(); 219130ca2051SWilly Tarreau 219230ca2051SWilly Tarreau if (ret < 0) { 219330ca2051SWilly Tarreau SET_ERRNO(-ret); 219430ca2051SWilly Tarreau ret = -1; 219530ca2051SWilly Tarreau } 219630ca2051SWilly Tarreau return ret; 219730ca2051SWilly Tarreau } 219830ca2051SWilly Tarreau 219930ca2051SWilly Tarreau static __attribute__((unused)) 220030ca2051SWilly Tarreau int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) 220130ca2051SWilly Tarreau { 220230ca2051SWilly Tarreau int ret = sys_select(nfds, rfds, wfds, efds, timeout); 220330ca2051SWilly Tarreau 220430ca2051SWilly Tarreau if (ret < 0) { 220530ca2051SWilly Tarreau SET_ERRNO(-ret); 220630ca2051SWilly Tarreau ret = -1; 220730ca2051SWilly Tarreau } 220830ca2051SWilly Tarreau return ret; 220930ca2051SWilly Tarreau } 221030ca2051SWilly Tarreau 221130ca2051SWilly Tarreau static __attribute__((unused)) 221230ca2051SWilly Tarreau int setpgid(pid_t pid, pid_t pgid) 221330ca2051SWilly Tarreau { 221430ca2051SWilly Tarreau int ret = sys_setpgid(pid, pgid); 221530ca2051SWilly Tarreau 221630ca2051SWilly Tarreau if (ret < 0) { 221730ca2051SWilly Tarreau SET_ERRNO(-ret); 221830ca2051SWilly Tarreau ret = -1; 221930ca2051SWilly Tarreau } 222030ca2051SWilly Tarreau return ret; 222130ca2051SWilly Tarreau } 222230ca2051SWilly Tarreau 222330ca2051SWilly Tarreau static __attribute__((unused)) 222430ca2051SWilly Tarreau pid_t setsid(void) 222530ca2051SWilly Tarreau { 222630ca2051SWilly Tarreau pid_t ret = sys_setsid(); 222730ca2051SWilly Tarreau 222830ca2051SWilly Tarreau if (ret < 0) { 222930ca2051SWilly Tarreau SET_ERRNO(-ret); 223030ca2051SWilly Tarreau ret = -1; 223130ca2051SWilly Tarreau } 223230ca2051SWilly Tarreau return ret; 223330ca2051SWilly Tarreau } 223430ca2051SWilly Tarreau 223530ca2051SWilly Tarreau static __attribute__((unused)) 223630ca2051SWilly Tarreau unsigned int sleep(unsigned int seconds) 223730ca2051SWilly Tarreau { 223830ca2051SWilly Tarreau struct timeval my_timeval = { seconds, 0 }; 223930ca2051SWilly Tarreau 224030ca2051SWilly Tarreau if (sys_select(0, 0, 0, 0, &my_timeval) < 0) 224130ca2051SWilly Tarreau return my_timeval.tv_sec + !!my_timeval.tv_usec; 224230ca2051SWilly Tarreau else 224330ca2051SWilly Tarreau return 0; 224430ca2051SWilly Tarreau } 224530ca2051SWilly Tarreau 224630ca2051SWilly Tarreau static __attribute__((unused)) 224730ca2051SWilly Tarreau int stat(const char *path, struct stat *buf) 224830ca2051SWilly Tarreau { 224930ca2051SWilly Tarreau int ret = sys_stat(path, buf); 225030ca2051SWilly Tarreau 225130ca2051SWilly Tarreau if (ret < 0) { 225230ca2051SWilly Tarreau SET_ERRNO(-ret); 225330ca2051SWilly Tarreau ret = -1; 225430ca2051SWilly Tarreau } 225530ca2051SWilly Tarreau return ret; 225630ca2051SWilly Tarreau } 225730ca2051SWilly Tarreau 225830ca2051SWilly Tarreau static __attribute__((unused)) 225930ca2051SWilly Tarreau int symlink(const char *old, const char *new) 226030ca2051SWilly Tarreau { 226130ca2051SWilly Tarreau int ret = sys_symlink(old, new); 226230ca2051SWilly Tarreau 226330ca2051SWilly Tarreau if (ret < 0) { 226430ca2051SWilly Tarreau SET_ERRNO(-ret); 226530ca2051SWilly Tarreau ret = -1; 226630ca2051SWilly Tarreau } 226730ca2051SWilly Tarreau return ret; 226830ca2051SWilly Tarreau } 226930ca2051SWilly Tarreau 227030ca2051SWilly Tarreau static __attribute__((unused)) 227130ca2051SWilly Tarreau int tcsetpgrp(int fd, pid_t pid) 227230ca2051SWilly Tarreau { 227330ca2051SWilly Tarreau return ioctl(fd, TIOCSPGRP, &pid); 227430ca2051SWilly Tarreau } 227530ca2051SWilly Tarreau 227630ca2051SWilly Tarreau static __attribute__((unused)) 227730ca2051SWilly Tarreau mode_t umask(mode_t mode) 227830ca2051SWilly Tarreau { 227930ca2051SWilly Tarreau return sys_umask(mode); 228030ca2051SWilly Tarreau } 228130ca2051SWilly Tarreau 228230ca2051SWilly Tarreau static __attribute__((unused)) 228330ca2051SWilly Tarreau int umount2(const char *path, int flags) 228430ca2051SWilly Tarreau { 228530ca2051SWilly Tarreau int ret = sys_umount2(path, flags); 228630ca2051SWilly Tarreau 228730ca2051SWilly Tarreau if (ret < 0) { 228830ca2051SWilly Tarreau SET_ERRNO(-ret); 228930ca2051SWilly Tarreau ret = -1; 229030ca2051SWilly Tarreau } 229130ca2051SWilly Tarreau return ret; 229230ca2051SWilly Tarreau } 229330ca2051SWilly Tarreau 229430ca2051SWilly Tarreau static __attribute__((unused)) 229530ca2051SWilly Tarreau int unlink(const char *path) 229630ca2051SWilly Tarreau { 229730ca2051SWilly Tarreau int ret = sys_unlink(path); 229830ca2051SWilly Tarreau 229930ca2051SWilly Tarreau if (ret < 0) { 230030ca2051SWilly Tarreau SET_ERRNO(-ret); 230130ca2051SWilly Tarreau ret = -1; 230230ca2051SWilly Tarreau } 230330ca2051SWilly Tarreau return ret; 230430ca2051SWilly Tarreau } 230530ca2051SWilly Tarreau 230630ca2051SWilly Tarreau static __attribute__((unused)) 230730ca2051SWilly Tarreau pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage) 230830ca2051SWilly Tarreau { 230930ca2051SWilly Tarreau pid_t ret = sys_wait4(pid, status, options, rusage); 231030ca2051SWilly Tarreau 231130ca2051SWilly Tarreau if (ret < 0) { 231230ca2051SWilly Tarreau SET_ERRNO(-ret); 231330ca2051SWilly Tarreau ret = -1; 231430ca2051SWilly Tarreau } 231530ca2051SWilly Tarreau return ret; 231630ca2051SWilly Tarreau } 231730ca2051SWilly Tarreau 231830ca2051SWilly Tarreau static __attribute__((unused)) 231930ca2051SWilly Tarreau pid_t waitpid(pid_t pid, int *status, int options) 232030ca2051SWilly Tarreau { 232130ca2051SWilly Tarreau pid_t ret = sys_waitpid(pid, status, options); 232230ca2051SWilly Tarreau 232330ca2051SWilly Tarreau if (ret < 0) { 232430ca2051SWilly Tarreau SET_ERRNO(-ret); 232530ca2051SWilly Tarreau ret = -1; 232630ca2051SWilly Tarreau } 232730ca2051SWilly Tarreau return ret; 232830ca2051SWilly Tarreau } 232930ca2051SWilly Tarreau 233030ca2051SWilly Tarreau static __attribute__((unused)) 233130ca2051SWilly Tarreau pid_t wait(int *status) 233230ca2051SWilly Tarreau { 233330ca2051SWilly Tarreau pid_t ret = sys_wait(status); 233430ca2051SWilly Tarreau 233530ca2051SWilly Tarreau if (ret < 0) { 233630ca2051SWilly Tarreau SET_ERRNO(-ret); 233730ca2051SWilly Tarreau ret = -1; 233830ca2051SWilly Tarreau } 233930ca2051SWilly Tarreau return ret; 234030ca2051SWilly Tarreau } 234130ca2051SWilly Tarreau 234230ca2051SWilly Tarreau static __attribute__((unused)) 234330ca2051SWilly Tarreau ssize_t write(int fd, const void *buf, size_t count) 234430ca2051SWilly Tarreau { 234530ca2051SWilly Tarreau ssize_t ret = sys_write(fd, buf, count); 234630ca2051SWilly Tarreau 234730ca2051SWilly Tarreau if (ret < 0) { 234830ca2051SWilly Tarreau SET_ERRNO(-ret); 234930ca2051SWilly Tarreau ret = -1; 235030ca2051SWilly Tarreau } 235130ca2051SWilly Tarreau return ret; 235230ca2051SWilly Tarreau } 235330ca2051SWilly Tarreau 235430ca2051SWilly Tarreau /* some size-optimized reimplementations of a few common str* and mem* 235530ca2051SWilly Tarreau * functions. They're marked static, except memcpy() and raise() which are used 235630ca2051SWilly Tarreau * by libgcc on ARM, so they are marked weak instead in order not to cause an 235730ca2051SWilly Tarreau * error when building a program made of multiple files (not recommended). 235830ca2051SWilly Tarreau */ 235930ca2051SWilly Tarreau 236030ca2051SWilly Tarreau static __attribute__((unused)) 236130ca2051SWilly Tarreau void *memmove(void *dst, const void *src, size_t len) 236230ca2051SWilly Tarreau { 236330ca2051SWilly Tarreau ssize_t pos = (dst <= src) ? -1 : (long)len; 236430ca2051SWilly Tarreau void *ret = dst; 236530ca2051SWilly Tarreau 236630ca2051SWilly Tarreau while (len--) { 236730ca2051SWilly Tarreau pos += (dst <= src) ? 1 : -1; 236830ca2051SWilly Tarreau ((char *)dst)[pos] = ((char *)src)[pos]; 236930ca2051SWilly Tarreau } 237030ca2051SWilly Tarreau return ret; 237130ca2051SWilly Tarreau } 237230ca2051SWilly Tarreau 237330ca2051SWilly Tarreau static __attribute__((unused)) 237430ca2051SWilly Tarreau void *memset(void *dst, int b, size_t len) 237530ca2051SWilly Tarreau { 237630ca2051SWilly Tarreau char *p = dst; 237730ca2051SWilly Tarreau 237830ca2051SWilly Tarreau while (len--) 237930ca2051SWilly Tarreau *(p++) = b; 238030ca2051SWilly Tarreau return dst; 238130ca2051SWilly Tarreau } 238230ca2051SWilly Tarreau 238330ca2051SWilly Tarreau static __attribute__((unused)) 238430ca2051SWilly Tarreau int memcmp(const void *s1, const void *s2, size_t n) 238530ca2051SWilly Tarreau { 238630ca2051SWilly Tarreau size_t ofs = 0; 238730ca2051SWilly Tarreau char c1 = 0; 238830ca2051SWilly Tarreau 238930ca2051SWilly Tarreau while (ofs < n && !(c1 = ((char *)s1)[ofs] - ((char *)s2)[ofs])) { 239030ca2051SWilly Tarreau ofs++; 239130ca2051SWilly Tarreau } 239230ca2051SWilly Tarreau return c1; 239330ca2051SWilly Tarreau } 239430ca2051SWilly Tarreau 239530ca2051SWilly Tarreau static __attribute__((unused)) 239630ca2051SWilly Tarreau char *strcpy(char *dst, const char *src) 239730ca2051SWilly Tarreau { 239830ca2051SWilly Tarreau char *ret = dst; 239930ca2051SWilly Tarreau 240030ca2051SWilly Tarreau while ((*dst++ = *src++)); 240130ca2051SWilly Tarreau return ret; 240230ca2051SWilly Tarreau } 240330ca2051SWilly Tarreau 240430ca2051SWilly Tarreau static __attribute__((unused)) 240530ca2051SWilly Tarreau char *strchr(const char *s, int c) 240630ca2051SWilly Tarreau { 240730ca2051SWilly Tarreau while (*s) { 240830ca2051SWilly Tarreau if (*s == (char)c) 240930ca2051SWilly Tarreau return (char *)s; 241030ca2051SWilly Tarreau s++; 241130ca2051SWilly Tarreau } 241230ca2051SWilly Tarreau return NULL; 241330ca2051SWilly Tarreau } 241430ca2051SWilly Tarreau 241530ca2051SWilly Tarreau static __attribute__((unused)) 241630ca2051SWilly Tarreau char *strrchr(const char *s, int c) 241730ca2051SWilly Tarreau { 241830ca2051SWilly Tarreau const char *ret = NULL; 241930ca2051SWilly Tarreau 242030ca2051SWilly Tarreau while (*s) { 242130ca2051SWilly Tarreau if (*s == (char)c) 242230ca2051SWilly Tarreau ret = s; 242330ca2051SWilly Tarreau s++; 242430ca2051SWilly Tarreau } 242530ca2051SWilly Tarreau return (char *)ret; 242630ca2051SWilly Tarreau } 242730ca2051SWilly Tarreau 242830ca2051SWilly Tarreau static __attribute__((unused)) 242930ca2051SWilly Tarreau size_t nolibc_strlen(const char *str) 243030ca2051SWilly Tarreau { 243130ca2051SWilly Tarreau size_t len; 243230ca2051SWilly Tarreau 243330ca2051SWilly Tarreau for (len = 0; str[len]; len++); 243430ca2051SWilly Tarreau return len; 243530ca2051SWilly Tarreau } 243630ca2051SWilly Tarreau 243730ca2051SWilly Tarreau #define strlen(str) ({ \ 243830ca2051SWilly Tarreau __builtin_constant_p((str)) ? \ 243930ca2051SWilly Tarreau __builtin_strlen((str)) : \ 244030ca2051SWilly Tarreau nolibc_strlen((str)); \ 244130ca2051SWilly Tarreau }) 244230ca2051SWilly Tarreau 244330ca2051SWilly Tarreau static __attribute__((unused)) 244430ca2051SWilly Tarreau int isdigit(int c) 244530ca2051SWilly Tarreau { 244630ca2051SWilly Tarreau return (unsigned int)(c - '0') <= 9; 244730ca2051SWilly Tarreau } 244830ca2051SWilly Tarreau 244930ca2051SWilly Tarreau static __attribute__((unused)) 245030ca2051SWilly Tarreau long atol(const char *s) 245130ca2051SWilly Tarreau { 245230ca2051SWilly Tarreau unsigned long ret = 0; 245330ca2051SWilly Tarreau unsigned long d; 245430ca2051SWilly Tarreau int neg = 0; 245530ca2051SWilly Tarreau 245630ca2051SWilly Tarreau if (*s == '-') { 245730ca2051SWilly Tarreau neg = 1; 245830ca2051SWilly Tarreau s++; 245930ca2051SWilly Tarreau } 246030ca2051SWilly Tarreau 246130ca2051SWilly Tarreau while (1) { 246230ca2051SWilly Tarreau d = (*s++) - '0'; 246330ca2051SWilly Tarreau if (d > 9) 246430ca2051SWilly Tarreau break; 246530ca2051SWilly Tarreau ret *= 10; 246630ca2051SWilly Tarreau ret += d; 246730ca2051SWilly Tarreau } 246830ca2051SWilly Tarreau 246930ca2051SWilly Tarreau return neg ? -ret : ret; 247030ca2051SWilly Tarreau } 247130ca2051SWilly Tarreau 247230ca2051SWilly Tarreau static __attribute__((unused)) 247330ca2051SWilly Tarreau int atoi(const char *s) 247430ca2051SWilly Tarreau { 247530ca2051SWilly Tarreau return atol(s); 247630ca2051SWilly Tarreau } 247730ca2051SWilly Tarreau 247830ca2051SWilly Tarreau static __attribute__((unused)) 247930ca2051SWilly Tarreau const char *ltoa(long in) 248030ca2051SWilly Tarreau { 248130ca2051SWilly Tarreau /* large enough for -9223372036854775808 */ 248230ca2051SWilly Tarreau static char buffer[21]; 248330ca2051SWilly Tarreau char *pos = buffer + sizeof(buffer) - 1; 248430ca2051SWilly Tarreau int neg = in < 0; 248530ca2051SWilly Tarreau unsigned long n = neg ? -in : in; 248630ca2051SWilly Tarreau 248730ca2051SWilly Tarreau *pos-- = '\0'; 248830ca2051SWilly Tarreau do { 248930ca2051SWilly Tarreau *pos-- = '0' + n % 10; 249030ca2051SWilly Tarreau n /= 10; 249130ca2051SWilly Tarreau if (pos < buffer) 249230ca2051SWilly Tarreau return pos + 1; 249330ca2051SWilly Tarreau } while (n); 249430ca2051SWilly Tarreau 249530ca2051SWilly Tarreau if (neg) 249630ca2051SWilly Tarreau *pos-- = '-'; 249730ca2051SWilly Tarreau return pos + 1; 249830ca2051SWilly Tarreau } 249930ca2051SWilly Tarreau 250030ca2051SWilly Tarreau __attribute__((weak,unused)) 250130ca2051SWilly Tarreau void *memcpy(void *dst, const void *src, size_t len) 250230ca2051SWilly Tarreau { 250330ca2051SWilly Tarreau return memmove(dst, src, len); 250430ca2051SWilly Tarreau } 250530ca2051SWilly Tarreau 250630ca2051SWilly Tarreau /* needed by libgcc for divide by zero */ 250730ca2051SWilly Tarreau __attribute__((weak,unused)) 250830ca2051SWilly Tarreau int raise(int signal) 250930ca2051SWilly Tarreau { 251030ca2051SWilly Tarreau return kill(getpid(), signal); 251130ca2051SWilly Tarreau } 251230ca2051SWilly Tarreau 251330ca2051SWilly Tarreau /* Here come a few helper functions */ 251430ca2051SWilly Tarreau 251530ca2051SWilly Tarreau static __attribute__((unused)) 251630ca2051SWilly Tarreau void FD_ZERO(fd_set *set) 251730ca2051SWilly Tarreau { 251830ca2051SWilly Tarreau memset(set, 0, sizeof(*set)); 251930ca2051SWilly Tarreau } 252030ca2051SWilly Tarreau 252130ca2051SWilly Tarreau static __attribute__((unused)) 252230ca2051SWilly Tarreau void FD_SET(int fd, fd_set *set) 252330ca2051SWilly Tarreau { 252430ca2051SWilly Tarreau if (fd < 0 || fd >= FD_SETSIZE) 252530ca2051SWilly Tarreau return; 252630ca2051SWilly Tarreau set->fd32[fd / 32] |= 1 << (fd & 31); 252730ca2051SWilly Tarreau } 252830ca2051SWilly Tarreau 252930ca2051SWilly Tarreau /* WARNING, it only deals with the 4096 first majors and 256 first minors */ 253030ca2051SWilly Tarreau static __attribute__((unused)) 253130ca2051SWilly Tarreau dev_t makedev(unsigned int major, unsigned int minor) 253230ca2051SWilly Tarreau { 253330ca2051SWilly Tarreau return ((major & 0xfff) << 8) | (minor & 0xff); 253430ca2051SWilly Tarreau } 2535