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 \ 743c6ce7a5SWilly 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 */ 83930c4accSWilly Tarreau #ifndef _NOLIBC_H 84930c4accSWilly Tarreau #define _NOLIBC_H 8530ca2051SWilly Tarreau 86967cce19SWilly Tarreau /* standard type definitions */ 87967cce19SWilly Tarreau #include "std.h" 88967cce19SWilly Tarreau 89967cce19SWilly Tarreau /* system includes */ 9030ca2051SWilly Tarreau #include <asm/unistd.h> 91967cce19SWilly Tarreau #include <asm/signal.h> // for SIGCHLD 9230ca2051SWilly Tarreau #include <asm/ioctls.h> 9330ca2051SWilly Tarreau #include <asm/errno.h> 9430ca2051SWilly Tarreau #include <linux/fs.h> 9530ca2051SWilly Tarreau #include <linux/loop.h> 9670ca7aeaSWilly Tarreau #include <linux/time.h> 97*cc7a492aSWilly Tarreau #include "types.h" 9830ca2051SWilly Tarreau 99*cc7a492aSWilly Tarreau /* Used by programs to avoid std includes */ 10030ca2051SWilly Tarreau #define NOLIBC 10130ca2051SWilly Tarreau 10230ca2051SWilly Tarreau /* this way it will be removed if unused */ 10330ca2051SWilly Tarreau static int errno; 10430ca2051SWilly Tarreau 10530ca2051SWilly Tarreau #ifndef NOLIBC_IGNORE_ERRNO 10630ca2051SWilly Tarreau #define SET_ERRNO(v) do { errno = (v); } while (0) 10730ca2051SWilly Tarreau #else 10830ca2051SWilly Tarreau #define SET_ERRNO(v) do { } while (0) 10930ca2051SWilly Tarreau #endif 11030ca2051SWilly Tarreau 11130ca2051SWilly Tarreau /* errno codes all ensure that they will not conflict with a valid pointer 11206dc8d45SBhaskar Chowdhury * because they all correspond to the highest addressable memory page. 11330ca2051SWilly Tarreau */ 11430ca2051SWilly Tarreau #define MAX_ERRNO 4095 11530ca2051SWilly Tarreau 11630ca2051SWilly Tarreau /* Below comes the architecture-specific code. For each architecture, we have 11730ca2051SWilly Tarreau * the syscall declarations and the _start code definition. This is the only 11830ca2051SWilly Tarreau * global part. On all architectures the kernel puts everything in the stack 11930ca2051SWilly Tarreau * before jumping to _start just above us, without any return address (_start 12030ca2051SWilly Tarreau * is not a function but an entry pint). So at the stack pointer we find argc. 12130ca2051SWilly Tarreau * Then argv[] begins, and ends at the first NULL. Then we have envp which 12230ca2051SWilly Tarreau * starts and ends with a NULL as well. So envp=argv+argc+1. 12330ca2051SWilly Tarreau */ 12430ca2051SWilly Tarreau 12530ca2051SWilly Tarreau #if defined(__x86_64__) 12630ca2051SWilly Tarreau /* Syscalls for x86_64 : 12730ca2051SWilly Tarreau * - registers are 64-bit 12830ca2051SWilly Tarreau * - syscall number is passed in rax 12930ca2051SWilly Tarreau * - arguments are in rdi, rsi, rdx, r10, r8, r9 respectively 13030ca2051SWilly Tarreau * - the system call is performed by calling the syscall instruction 13130ca2051SWilly Tarreau * - syscall return comes in rax 132bf916669SAmmar Faizi * - rcx and r11 are clobbered, others are preserved. 13330ca2051SWilly Tarreau * - the arguments are cast to long and assigned into the target registers 13430ca2051SWilly Tarreau * which are then simply passed as registers to the asm code, so that we 13530ca2051SWilly Tarreau * don't have to experience issues with register constraints. 13630ca2051SWilly Tarreau * - the syscall number is always specified last in order to allow to force 13730ca2051SWilly Tarreau * some registers before (gcc refuses a %-register at the last position). 138bf916669SAmmar Faizi * - see also x86-64 ABI section A.2 AMD64 Linux Kernel Conventions, A.2.1 139bf916669SAmmar Faizi * Calling Conventions. 140bf916669SAmmar Faizi * 141bf916669SAmmar Faizi * Link x86-64 ABI: https://gitlab.com/x86-psABIs/x86-64-ABI/-/wikis/x86-64-psABI 142bf916669SAmmar Faizi * 14330ca2051SWilly Tarreau */ 14430ca2051SWilly Tarreau 14530ca2051SWilly Tarreau #define my_syscall0(num) \ 14630ca2051SWilly Tarreau ({ \ 14730ca2051SWilly Tarreau long _ret; \ 14830ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 14930ca2051SWilly Tarreau \ 15030ca2051SWilly Tarreau asm volatile ( \ 15130ca2051SWilly Tarreau "syscall\n" \ 15230ca2051SWilly Tarreau : "=a"(_ret) \ 15330ca2051SWilly Tarreau : "0"(_num) \ 154bf916669SAmmar Faizi : "rcx", "r11", "memory", "cc" \ 15530ca2051SWilly Tarreau ); \ 15630ca2051SWilly Tarreau _ret; \ 15730ca2051SWilly Tarreau }) 15830ca2051SWilly Tarreau 15930ca2051SWilly Tarreau #define my_syscall1(num, arg1) \ 16030ca2051SWilly Tarreau ({ \ 16130ca2051SWilly Tarreau long _ret; \ 16230ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 16330ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 16430ca2051SWilly Tarreau \ 16530ca2051SWilly Tarreau asm volatile ( \ 16630ca2051SWilly Tarreau "syscall\n" \ 16730ca2051SWilly Tarreau : "=a"(_ret) \ 16830ca2051SWilly Tarreau : "r"(_arg1), \ 16930ca2051SWilly Tarreau "0"(_num) \ 170bf916669SAmmar Faizi : "rcx", "r11", "memory", "cc" \ 17130ca2051SWilly Tarreau ); \ 17230ca2051SWilly Tarreau _ret; \ 17330ca2051SWilly Tarreau }) 17430ca2051SWilly Tarreau 17530ca2051SWilly Tarreau #define my_syscall2(num, arg1, arg2) \ 17630ca2051SWilly Tarreau ({ \ 17730ca2051SWilly Tarreau long _ret; \ 17830ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 17930ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 18030ca2051SWilly Tarreau register long _arg2 asm("rsi") = (long)(arg2); \ 18130ca2051SWilly Tarreau \ 18230ca2051SWilly Tarreau asm volatile ( \ 18330ca2051SWilly Tarreau "syscall\n" \ 18430ca2051SWilly Tarreau : "=a"(_ret) \ 18530ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), \ 18630ca2051SWilly Tarreau "0"(_num) \ 187bf916669SAmmar Faizi : "rcx", "r11", "memory", "cc" \ 18830ca2051SWilly Tarreau ); \ 18930ca2051SWilly Tarreau _ret; \ 19030ca2051SWilly Tarreau }) 19130ca2051SWilly Tarreau 19230ca2051SWilly Tarreau #define my_syscall3(num, arg1, arg2, arg3) \ 19330ca2051SWilly Tarreau ({ \ 19430ca2051SWilly Tarreau long _ret; \ 19530ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 19630ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 19730ca2051SWilly Tarreau register long _arg2 asm("rsi") = (long)(arg2); \ 19830ca2051SWilly Tarreau register long _arg3 asm("rdx") = (long)(arg3); \ 19930ca2051SWilly Tarreau \ 20030ca2051SWilly Tarreau asm volatile ( \ 20130ca2051SWilly Tarreau "syscall\n" \ 20230ca2051SWilly Tarreau : "=a"(_ret) \ 20330ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 20430ca2051SWilly Tarreau "0"(_num) \ 205bf916669SAmmar Faizi : "rcx", "r11", "memory", "cc" \ 20630ca2051SWilly Tarreau ); \ 20730ca2051SWilly Tarreau _ret; \ 20830ca2051SWilly Tarreau }) 20930ca2051SWilly Tarreau 21030ca2051SWilly Tarreau #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 21130ca2051SWilly Tarreau ({ \ 21230ca2051SWilly Tarreau long _ret; \ 21330ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 21430ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 21530ca2051SWilly Tarreau register long _arg2 asm("rsi") = (long)(arg2); \ 21630ca2051SWilly Tarreau register long _arg3 asm("rdx") = (long)(arg3); \ 21730ca2051SWilly Tarreau register long _arg4 asm("r10") = (long)(arg4); \ 21830ca2051SWilly Tarreau \ 21930ca2051SWilly Tarreau asm volatile ( \ 22030ca2051SWilly Tarreau "syscall\n" \ 221bf916669SAmmar Faizi : "=a"(_ret) \ 22230ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 22330ca2051SWilly Tarreau "0"(_num) \ 224bf916669SAmmar Faizi : "rcx", "r11", "memory", "cc" \ 22530ca2051SWilly Tarreau ); \ 22630ca2051SWilly Tarreau _ret; \ 22730ca2051SWilly Tarreau }) 22830ca2051SWilly Tarreau 22930ca2051SWilly Tarreau #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 23030ca2051SWilly Tarreau ({ \ 23130ca2051SWilly Tarreau long _ret; \ 23230ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 23330ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 23430ca2051SWilly Tarreau register long _arg2 asm("rsi") = (long)(arg2); \ 23530ca2051SWilly Tarreau register long _arg3 asm("rdx") = (long)(arg3); \ 23630ca2051SWilly Tarreau register long _arg4 asm("r10") = (long)(arg4); \ 23730ca2051SWilly Tarreau register long _arg5 asm("r8") = (long)(arg5); \ 23830ca2051SWilly Tarreau \ 23930ca2051SWilly Tarreau asm volatile ( \ 24030ca2051SWilly Tarreau "syscall\n" \ 241bf916669SAmmar Faizi : "=a"(_ret) \ 24230ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 24330ca2051SWilly Tarreau "0"(_num) \ 244bf916669SAmmar Faizi : "rcx", "r11", "memory", "cc" \ 24530ca2051SWilly Tarreau ); \ 24630ca2051SWilly Tarreau _ret; \ 24730ca2051SWilly Tarreau }) 24830ca2051SWilly Tarreau 24930ca2051SWilly Tarreau #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 25030ca2051SWilly Tarreau ({ \ 25130ca2051SWilly Tarreau long _ret; \ 25230ca2051SWilly Tarreau register long _num asm("rax") = (num); \ 25330ca2051SWilly Tarreau register long _arg1 asm("rdi") = (long)(arg1); \ 25430ca2051SWilly Tarreau register long _arg2 asm("rsi") = (long)(arg2); \ 25530ca2051SWilly Tarreau register long _arg3 asm("rdx") = (long)(arg3); \ 25630ca2051SWilly Tarreau register long _arg4 asm("r10") = (long)(arg4); \ 25730ca2051SWilly Tarreau register long _arg5 asm("r8") = (long)(arg5); \ 25830ca2051SWilly Tarreau register long _arg6 asm("r9") = (long)(arg6); \ 25930ca2051SWilly Tarreau \ 26030ca2051SWilly Tarreau asm volatile ( \ 26130ca2051SWilly Tarreau "syscall\n" \ 262bf916669SAmmar Faizi : "=a"(_ret) \ 26330ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 26430ca2051SWilly Tarreau "r"(_arg6), "0"(_num) \ 26530ca2051SWilly Tarreau : "rcx", "r11", "memory", "cc" \ 26630ca2051SWilly Tarreau ); \ 26730ca2051SWilly Tarreau _ret; \ 26830ca2051SWilly Tarreau }) 26930ca2051SWilly Tarreau 27030ca2051SWilly Tarreau /* startup code */ 271937ed91cSAmmar Faizi /* 272937ed91cSAmmar Faizi * x86-64 System V ABI mandates: 273937ed91cSAmmar Faizi * 1) %rsp must be 16-byte aligned right before the function call. 274937ed91cSAmmar Faizi * 2) The deepest stack frame should be zero (the %rbp). 275937ed91cSAmmar Faizi * 276937ed91cSAmmar Faizi */ 27730ca2051SWilly Tarreau asm(".section .text\n" 27830ca2051SWilly Tarreau ".global _start\n" 27930ca2051SWilly Tarreau "_start:\n" 28030ca2051SWilly Tarreau "pop %rdi\n" // argc (first arg, %rdi) 28130ca2051SWilly Tarreau "mov %rsp, %rsi\n" // argv[] (second arg, %rsi) 28230ca2051SWilly Tarreau "lea 8(%rsi,%rdi,8),%rdx\n" // then a NULL then envp (third arg, %rdx) 283937ed91cSAmmar Faizi "xor %ebp, %ebp\n" // zero the stack frame 284937ed91cSAmmar Faizi "and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned before call 28530ca2051SWilly Tarreau "call main\n" // main() returns the status code, we'll exit with it. 286de0244aeSWilly Tarreau "mov %eax, %edi\n" // retrieve exit code (32 bit) 2877bdc0e7aSAmmar Faizi "mov $60, %eax\n" // NR_exit == 60 28830ca2051SWilly Tarreau "syscall\n" // really exit 28930ca2051SWilly Tarreau "hlt\n" // ensure it does not return 29030ca2051SWilly Tarreau ""); 29130ca2051SWilly Tarreau 29230ca2051SWilly Tarreau /* fcntl / open */ 29330ca2051SWilly Tarreau #define O_RDONLY 0 29430ca2051SWilly Tarreau #define O_WRONLY 1 29530ca2051SWilly Tarreau #define O_RDWR 2 29630ca2051SWilly Tarreau #define O_CREAT 0x40 29730ca2051SWilly Tarreau #define O_EXCL 0x80 29830ca2051SWilly Tarreau #define O_NOCTTY 0x100 29930ca2051SWilly Tarreau #define O_TRUNC 0x200 30030ca2051SWilly Tarreau #define O_APPEND 0x400 30130ca2051SWilly Tarreau #define O_NONBLOCK 0x800 30230ca2051SWilly Tarreau #define O_DIRECTORY 0x10000 30330ca2051SWilly Tarreau 30430ca2051SWilly Tarreau /* The struct returned by the stat() syscall, equivalent to stat64(). The 30530ca2051SWilly Tarreau * syscall returns 116 bytes and stops in the middle of __unused. 30630ca2051SWilly Tarreau */ 30730ca2051SWilly Tarreau struct sys_stat_struct { 30830ca2051SWilly Tarreau unsigned long st_dev; 30930ca2051SWilly Tarreau unsigned long st_ino; 31030ca2051SWilly Tarreau unsigned long st_nlink; 31130ca2051SWilly Tarreau unsigned int st_mode; 31230ca2051SWilly Tarreau unsigned int st_uid; 31330ca2051SWilly Tarreau 31430ca2051SWilly Tarreau unsigned int st_gid; 31530ca2051SWilly Tarreau unsigned int __pad0; 31630ca2051SWilly Tarreau unsigned long st_rdev; 31730ca2051SWilly Tarreau long st_size; 31830ca2051SWilly Tarreau long st_blksize; 31930ca2051SWilly Tarreau 32030ca2051SWilly Tarreau long st_blocks; 32130ca2051SWilly Tarreau unsigned long st_atime; 32230ca2051SWilly Tarreau unsigned long st_atime_nsec; 32330ca2051SWilly Tarreau unsigned long st_mtime; 32430ca2051SWilly Tarreau 32530ca2051SWilly Tarreau unsigned long st_mtime_nsec; 32630ca2051SWilly Tarreau unsigned long st_ctime; 32730ca2051SWilly Tarreau unsigned long st_ctime_nsec; 32830ca2051SWilly Tarreau long __unused[3]; 32930ca2051SWilly Tarreau }; 33030ca2051SWilly Tarreau 33130ca2051SWilly Tarreau #elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) 33230ca2051SWilly Tarreau /* Syscalls for i386 : 33330ca2051SWilly Tarreau * - mostly similar to x86_64 33430ca2051SWilly Tarreau * - registers are 32-bit 33530ca2051SWilly Tarreau * - syscall number is passed in eax 33630ca2051SWilly Tarreau * - arguments are in ebx, ecx, edx, esi, edi, ebp respectively 33730ca2051SWilly Tarreau * - all registers are preserved (except eax of course) 33830ca2051SWilly Tarreau * - the system call is performed by calling int $0x80 33930ca2051SWilly Tarreau * - syscall return comes in eax 34030ca2051SWilly Tarreau * - the arguments are cast to long and assigned into the target registers 34130ca2051SWilly Tarreau * which are then simply passed as registers to the asm code, so that we 34230ca2051SWilly Tarreau * don't have to experience issues with register constraints. 34330ca2051SWilly Tarreau * - the syscall number is always specified last in order to allow to force 34430ca2051SWilly Tarreau * some registers before (gcc refuses a %-register at the last position). 34530ca2051SWilly Tarreau * 34630ca2051SWilly Tarreau * Also, i386 supports the old_select syscall if newselect is not available 34730ca2051SWilly Tarreau */ 34830ca2051SWilly Tarreau #define __ARCH_WANT_SYS_OLD_SELECT 34930ca2051SWilly Tarreau 35030ca2051SWilly Tarreau #define my_syscall0(num) \ 35130ca2051SWilly Tarreau ({ \ 35230ca2051SWilly Tarreau long _ret; \ 35330ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 35430ca2051SWilly Tarreau \ 35530ca2051SWilly Tarreau asm volatile ( \ 35630ca2051SWilly Tarreau "int $0x80\n" \ 35730ca2051SWilly Tarreau : "=a" (_ret) \ 35830ca2051SWilly Tarreau : "0"(_num) \ 35930ca2051SWilly Tarreau : "memory", "cc" \ 36030ca2051SWilly Tarreau ); \ 36130ca2051SWilly Tarreau _ret; \ 36230ca2051SWilly Tarreau }) 36330ca2051SWilly Tarreau 36430ca2051SWilly Tarreau #define my_syscall1(num, arg1) \ 36530ca2051SWilly Tarreau ({ \ 36630ca2051SWilly Tarreau long _ret; \ 36730ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 36830ca2051SWilly Tarreau register long _arg1 asm("ebx") = (long)(arg1); \ 36930ca2051SWilly Tarreau \ 37030ca2051SWilly Tarreau asm volatile ( \ 37130ca2051SWilly Tarreau "int $0x80\n" \ 37230ca2051SWilly Tarreau : "=a" (_ret) \ 37330ca2051SWilly Tarreau : "r"(_arg1), \ 37430ca2051SWilly Tarreau "0"(_num) \ 37530ca2051SWilly Tarreau : "memory", "cc" \ 37630ca2051SWilly Tarreau ); \ 37730ca2051SWilly Tarreau _ret; \ 37830ca2051SWilly Tarreau }) 37930ca2051SWilly Tarreau 38030ca2051SWilly Tarreau #define my_syscall2(num, arg1, arg2) \ 38130ca2051SWilly Tarreau ({ \ 38230ca2051SWilly Tarreau long _ret; \ 38330ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 38430ca2051SWilly Tarreau register long _arg1 asm("ebx") = (long)(arg1); \ 38530ca2051SWilly Tarreau register long _arg2 asm("ecx") = (long)(arg2); \ 38630ca2051SWilly Tarreau \ 38730ca2051SWilly Tarreau asm volatile ( \ 38830ca2051SWilly Tarreau "int $0x80\n" \ 38930ca2051SWilly Tarreau : "=a" (_ret) \ 39030ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), \ 39130ca2051SWilly Tarreau "0"(_num) \ 39230ca2051SWilly Tarreau : "memory", "cc" \ 39330ca2051SWilly Tarreau ); \ 39430ca2051SWilly Tarreau _ret; \ 39530ca2051SWilly Tarreau }) 39630ca2051SWilly Tarreau 39730ca2051SWilly Tarreau #define my_syscall3(num, arg1, arg2, arg3) \ 39830ca2051SWilly Tarreau ({ \ 39930ca2051SWilly Tarreau long _ret; \ 40030ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 40130ca2051SWilly Tarreau register long _arg1 asm("ebx") = (long)(arg1); \ 40230ca2051SWilly Tarreau register long _arg2 asm("ecx") = (long)(arg2); \ 40330ca2051SWilly Tarreau register long _arg3 asm("edx") = (long)(arg3); \ 40430ca2051SWilly Tarreau \ 40530ca2051SWilly Tarreau asm volatile ( \ 40630ca2051SWilly Tarreau "int $0x80\n" \ 40730ca2051SWilly Tarreau : "=a" (_ret) \ 40830ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 40930ca2051SWilly Tarreau "0"(_num) \ 41030ca2051SWilly Tarreau : "memory", "cc" \ 41130ca2051SWilly Tarreau ); \ 41230ca2051SWilly Tarreau _ret; \ 41330ca2051SWilly Tarreau }) 41430ca2051SWilly Tarreau 41530ca2051SWilly Tarreau #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 41630ca2051SWilly Tarreau ({ \ 41730ca2051SWilly Tarreau long _ret; \ 41830ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 41930ca2051SWilly Tarreau register long _arg1 asm("ebx") = (long)(arg1); \ 42030ca2051SWilly Tarreau register long _arg2 asm("ecx") = (long)(arg2); \ 42130ca2051SWilly Tarreau register long _arg3 asm("edx") = (long)(arg3); \ 42230ca2051SWilly Tarreau register long _arg4 asm("esi") = (long)(arg4); \ 42330ca2051SWilly Tarreau \ 42430ca2051SWilly Tarreau asm volatile ( \ 42530ca2051SWilly Tarreau "int $0x80\n" \ 42630ca2051SWilly Tarreau : "=a" (_ret) \ 42730ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 42830ca2051SWilly Tarreau "0"(_num) \ 42930ca2051SWilly Tarreau : "memory", "cc" \ 43030ca2051SWilly Tarreau ); \ 43130ca2051SWilly Tarreau _ret; \ 43230ca2051SWilly Tarreau }) 43330ca2051SWilly Tarreau 43430ca2051SWilly Tarreau #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 43530ca2051SWilly Tarreau ({ \ 43630ca2051SWilly Tarreau long _ret; \ 43730ca2051SWilly Tarreau register long _num asm("eax") = (num); \ 43830ca2051SWilly Tarreau register long _arg1 asm("ebx") = (long)(arg1); \ 43930ca2051SWilly Tarreau register long _arg2 asm("ecx") = (long)(arg2); \ 44030ca2051SWilly Tarreau register long _arg3 asm("edx") = (long)(arg3); \ 44130ca2051SWilly Tarreau register long _arg4 asm("esi") = (long)(arg4); \ 44230ca2051SWilly Tarreau register long _arg5 asm("edi") = (long)(arg5); \ 44330ca2051SWilly Tarreau \ 44430ca2051SWilly Tarreau asm volatile ( \ 44530ca2051SWilly Tarreau "int $0x80\n" \ 44630ca2051SWilly Tarreau : "=a" (_ret) \ 44730ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 44830ca2051SWilly Tarreau "0"(_num) \ 44930ca2051SWilly Tarreau : "memory", "cc" \ 45030ca2051SWilly Tarreau ); \ 45130ca2051SWilly Tarreau _ret; \ 45230ca2051SWilly Tarreau }) 45330ca2051SWilly Tarreau 45430ca2051SWilly Tarreau /* startup code */ 455ebbe0d8aSWilly Tarreau /* 456ebbe0d8aSWilly Tarreau * i386 System V ABI mandates: 457ebbe0d8aSWilly Tarreau * 1) last pushed argument must be 16-byte aligned. 458ebbe0d8aSWilly Tarreau * 2) The deepest stack frame should be set to zero 459ebbe0d8aSWilly Tarreau * 460ebbe0d8aSWilly Tarreau */ 46130ca2051SWilly Tarreau asm(".section .text\n" 46230ca2051SWilly Tarreau ".global _start\n" 46330ca2051SWilly Tarreau "_start:\n" 46430ca2051SWilly Tarreau "pop %eax\n" // argc (first arg, %eax) 46530ca2051SWilly Tarreau "mov %esp, %ebx\n" // argv[] (second arg, %ebx) 46630ca2051SWilly Tarreau "lea 4(%ebx,%eax,4),%ecx\n" // then a NULL then envp (third arg, %ecx) 467ebbe0d8aSWilly Tarreau "xor %ebp, %ebp\n" // zero the stack frame 468ebbe0d8aSWilly Tarreau "and $-16, %esp\n" // x86 ABI : esp must be 16-byte aligned before 469ebbe0d8aSWilly Tarreau "sub $4, %esp\n" // the call instruction (args are aligned) 47030ca2051SWilly Tarreau "push %ecx\n" // push all registers on the stack so that we 47130ca2051SWilly Tarreau "push %ebx\n" // support both regparm and plain stack modes 47230ca2051SWilly Tarreau "push %eax\n" 47330ca2051SWilly Tarreau "call main\n" // main() returns the status code in %eax 474de0244aeSWilly Tarreau "mov %eax, %ebx\n" // retrieve exit code (32-bit int) 47530ca2051SWilly Tarreau "movl $1, %eax\n" // NR_exit == 1 47630ca2051SWilly Tarreau "int $0x80\n" // exit now 47730ca2051SWilly Tarreau "hlt\n" // ensure it does not 47830ca2051SWilly Tarreau ""); 47930ca2051SWilly Tarreau 48030ca2051SWilly Tarreau /* fcntl / open */ 48130ca2051SWilly Tarreau #define O_RDONLY 0 48230ca2051SWilly Tarreau #define O_WRONLY 1 48330ca2051SWilly Tarreau #define O_RDWR 2 48430ca2051SWilly Tarreau #define O_CREAT 0x40 48530ca2051SWilly Tarreau #define O_EXCL 0x80 48630ca2051SWilly Tarreau #define O_NOCTTY 0x100 48730ca2051SWilly Tarreau #define O_TRUNC 0x200 48830ca2051SWilly Tarreau #define O_APPEND 0x400 48930ca2051SWilly Tarreau #define O_NONBLOCK 0x800 49030ca2051SWilly Tarreau #define O_DIRECTORY 0x10000 49130ca2051SWilly Tarreau 49230ca2051SWilly Tarreau /* The struct returned by the stat() syscall, 32-bit only, the syscall returns 49330ca2051SWilly Tarreau * exactly 56 bytes (stops before the unused array). 49430ca2051SWilly Tarreau */ 49530ca2051SWilly Tarreau struct sys_stat_struct { 49630ca2051SWilly Tarreau unsigned long st_dev; 49730ca2051SWilly Tarreau unsigned long st_ino; 49830ca2051SWilly Tarreau unsigned short st_mode; 49930ca2051SWilly Tarreau unsigned short st_nlink; 50030ca2051SWilly Tarreau unsigned short st_uid; 50130ca2051SWilly Tarreau unsigned short st_gid; 50230ca2051SWilly Tarreau 50330ca2051SWilly Tarreau unsigned long st_rdev; 50430ca2051SWilly Tarreau unsigned long st_size; 50530ca2051SWilly Tarreau unsigned long st_blksize; 50630ca2051SWilly Tarreau unsigned long st_blocks; 50730ca2051SWilly Tarreau 50830ca2051SWilly Tarreau unsigned long st_atime; 50930ca2051SWilly Tarreau unsigned long st_atime_nsec; 51030ca2051SWilly Tarreau unsigned long st_mtime; 51130ca2051SWilly Tarreau unsigned long st_mtime_nsec; 51230ca2051SWilly Tarreau 51330ca2051SWilly Tarreau unsigned long st_ctime; 51430ca2051SWilly Tarreau unsigned long st_ctime_nsec; 51530ca2051SWilly Tarreau unsigned long __unused[2]; 51630ca2051SWilly Tarreau }; 51730ca2051SWilly Tarreau 51830ca2051SWilly Tarreau #elif defined(__ARM_EABI__) 51930ca2051SWilly Tarreau /* Syscalls for ARM in ARM or Thumb modes : 52030ca2051SWilly Tarreau * - registers are 32-bit 52130ca2051SWilly Tarreau * - stack is 8-byte aligned 52230ca2051SWilly Tarreau * ( http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka4127.html) 52330ca2051SWilly Tarreau * - syscall number is passed in r7 52430ca2051SWilly Tarreau * - arguments are in r0, r1, r2, r3, r4, r5 52530ca2051SWilly Tarreau * - the system call is performed by calling svc #0 52630ca2051SWilly Tarreau * - syscall return comes in r0. 52730ca2051SWilly Tarreau * - only lr is clobbered. 52830ca2051SWilly Tarreau * - the arguments are cast to long and assigned into the target registers 52930ca2051SWilly Tarreau * which are then simply passed as registers to the asm code, so that we 53030ca2051SWilly Tarreau * don't have to experience issues with register constraints. 53130ca2051SWilly Tarreau * - the syscall number is always specified last in order to allow to force 53230ca2051SWilly Tarreau * some registers before (gcc refuses a %-register at the last position). 53330ca2051SWilly Tarreau * 53430ca2051SWilly Tarreau * Also, ARM supports the old_select syscall if newselect is not available 53530ca2051SWilly Tarreau */ 53630ca2051SWilly Tarreau #define __ARCH_WANT_SYS_OLD_SELECT 53730ca2051SWilly Tarreau 53830ca2051SWilly Tarreau #define my_syscall0(num) \ 53930ca2051SWilly Tarreau ({ \ 54030ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 54130ca2051SWilly Tarreau register long _arg1 asm("r0"); \ 54230ca2051SWilly Tarreau \ 54330ca2051SWilly Tarreau asm volatile ( \ 54430ca2051SWilly Tarreau "svc #0\n" \ 54530ca2051SWilly Tarreau : "=r"(_arg1) \ 54630ca2051SWilly Tarreau : "r"(_num) \ 54730ca2051SWilly Tarreau : "memory", "cc", "lr" \ 54830ca2051SWilly Tarreau ); \ 54930ca2051SWilly Tarreau _arg1; \ 55030ca2051SWilly Tarreau }) 55130ca2051SWilly Tarreau 55230ca2051SWilly Tarreau #define my_syscall1(num, arg1) \ 55330ca2051SWilly Tarreau ({ \ 55430ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 55530ca2051SWilly Tarreau register long _arg1 asm("r0") = (long)(arg1); \ 55630ca2051SWilly Tarreau \ 55730ca2051SWilly Tarreau asm volatile ( \ 55830ca2051SWilly Tarreau "svc #0\n" \ 55930ca2051SWilly Tarreau : "=r"(_arg1) \ 56030ca2051SWilly Tarreau : "r"(_arg1), \ 56130ca2051SWilly Tarreau "r"(_num) \ 56230ca2051SWilly Tarreau : "memory", "cc", "lr" \ 56330ca2051SWilly Tarreau ); \ 56430ca2051SWilly Tarreau _arg1; \ 56530ca2051SWilly Tarreau }) 56630ca2051SWilly Tarreau 56730ca2051SWilly Tarreau #define my_syscall2(num, arg1, arg2) \ 56830ca2051SWilly Tarreau ({ \ 56930ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 57030ca2051SWilly Tarreau register long _arg1 asm("r0") = (long)(arg1); \ 57130ca2051SWilly Tarreau register long _arg2 asm("r1") = (long)(arg2); \ 57230ca2051SWilly Tarreau \ 57330ca2051SWilly Tarreau asm volatile ( \ 57430ca2051SWilly Tarreau "svc #0\n" \ 57530ca2051SWilly Tarreau : "=r"(_arg1) \ 57630ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), \ 57730ca2051SWilly Tarreau "r"(_num) \ 57830ca2051SWilly Tarreau : "memory", "cc", "lr" \ 57930ca2051SWilly Tarreau ); \ 58030ca2051SWilly Tarreau _arg1; \ 58130ca2051SWilly Tarreau }) 58230ca2051SWilly Tarreau 58330ca2051SWilly Tarreau #define my_syscall3(num, arg1, arg2, arg3) \ 58430ca2051SWilly Tarreau ({ \ 58530ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 58630ca2051SWilly Tarreau register long _arg1 asm("r0") = (long)(arg1); \ 58730ca2051SWilly Tarreau register long _arg2 asm("r1") = (long)(arg2); \ 58830ca2051SWilly Tarreau register long _arg3 asm("r2") = (long)(arg3); \ 58930ca2051SWilly Tarreau \ 59030ca2051SWilly Tarreau asm volatile ( \ 59130ca2051SWilly Tarreau "svc #0\n" \ 59230ca2051SWilly Tarreau : "=r"(_arg1) \ 59330ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 59430ca2051SWilly Tarreau "r"(_num) \ 59530ca2051SWilly Tarreau : "memory", "cc", "lr" \ 59630ca2051SWilly Tarreau ); \ 59730ca2051SWilly Tarreau _arg1; \ 59830ca2051SWilly Tarreau }) 59930ca2051SWilly Tarreau 60030ca2051SWilly Tarreau #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 60130ca2051SWilly Tarreau ({ \ 60230ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 60330ca2051SWilly Tarreau register long _arg1 asm("r0") = (long)(arg1); \ 60430ca2051SWilly Tarreau register long _arg2 asm("r1") = (long)(arg2); \ 60530ca2051SWilly Tarreau register long _arg3 asm("r2") = (long)(arg3); \ 60630ca2051SWilly Tarreau register long _arg4 asm("r3") = (long)(arg4); \ 60730ca2051SWilly Tarreau \ 60830ca2051SWilly Tarreau asm volatile ( \ 60930ca2051SWilly Tarreau "svc #0\n" \ 61030ca2051SWilly Tarreau : "=r"(_arg1) \ 61130ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 61230ca2051SWilly Tarreau "r"(_num) \ 61330ca2051SWilly Tarreau : "memory", "cc", "lr" \ 61430ca2051SWilly Tarreau ); \ 61530ca2051SWilly Tarreau _arg1; \ 61630ca2051SWilly Tarreau }) 61730ca2051SWilly Tarreau 61830ca2051SWilly Tarreau #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 61930ca2051SWilly Tarreau ({ \ 62030ca2051SWilly Tarreau register long _num asm("r7") = (num); \ 62130ca2051SWilly Tarreau register long _arg1 asm("r0") = (long)(arg1); \ 62230ca2051SWilly Tarreau register long _arg2 asm("r1") = (long)(arg2); \ 62330ca2051SWilly Tarreau register long _arg3 asm("r2") = (long)(arg3); \ 62430ca2051SWilly Tarreau register long _arg4 asm("r3") = (long)(arg4); \ 62530ca2051SWilly Tarreau register long _arg5 asm("r4") = (long)(arg5); \ 62630ca2051SWilly Tarreau \ 62730ca2051SWilly Tarreau asm volatile ( \ 62830ca2051SWilly Tarreau "svc #0\n" \ 62930ca2051SWilly Tarreau : "=r" (_arg1) \ 63030ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 63130ca2051SWilly Tarreau "r"(_num) \ 63230ca2051SWilly Tarreau : "memory", "cc", "lr" \ 63330ca2051SWilly Tarreau ); \ 63430ca2051SWilly Tarreau _arg1; \ 63530ca2051SWilly Tarreau }) 63630ca2051SWilly Tarreau 63730ca2051SWilly Tarreau /* startup code */ 63830ca2051SWilly Tarreau asm(".section .text\n" 63930ca2051SWilly Tarreau ".global _start\n" 64030ca2051SWilly Tarreau "_start:\n" 64130ca2051SWilly Tarreau #if defined(__THUMBEB__) || defined(__THUMBEL__) 64230ca2051SWilly Tarreau /* We enter here in 32-bit mode but if some previous functions were in 64330ca2051SWilly Tarreau * 16-bit mode, the assembler cannot know, so we need to tell it we're in 64430ca2051SWilly Tarreau * 32-bit now, then switch to 16-bit (is there a better way to do it than 64530ca2051SWilly Tarreau * adding 1 by hand ?) and tell the asm we're now in 16-bit mode so that 64630ca2051SWilly Tarreau * it generates correct instructions. Note that we do not support thumb1. 64730ca2051SWilly Tarreau */ 64830ca2051SWilly Tarreau ".code 32\n" 64930ca2051SWilly Tarreau "add r0, pc, #1\n" 65030ca2051SWilly Tarreau "bx r0\n" 65130ca2051SWilly Tarreau ".code 16\n" 65230ca2051SWilly Tarreau #endif 65330ca2051SWilly Tarreau "pop {%r0}\n" // argc was in the stack 65430ca2051SWilly Tarreau "mov %r1, %sp\n" // argv = sp 65530ca2051SWilly Tarreau "add %r2, %r1, %r0, lsl #2\n" // envp = argv + 4*argc ... 65630ca2051SWilly Tarreau "add %r2, %r2, $4\n" // ... + 4 65730ca2051SWilly Tarreau "and %r3, %r1, $-8\n" // AAPCS : sp must be 8-byte aligned in the 65830ca2051SWilly Tarreau "mov %sp, %r3\n" // callee, an bl doesn't push (lr=pc) 65930ca2051SWilly Tarreau "bl main\n" // main() returns the status code, we'll exit with it. 66030ca2051SWilly Tarreau "movs r7, $1\n" // NR_exit == 1 66130ca2051SWilly Tarreau "svc $0x00\n" 66230ca2051SWilly Tarreau ""); 66330ca2051SWilly Tarreau 66430ca2051SWilly Tarreau /* fcntl / open */ 66530ca2051SWilly Tarreau #define O_RDONLY 0 66630ca2051SWilly Tarreau #define O_WRONLY 1 66730ca2051SWilly Tarreau #define O_RDWR 2 66830ca2051SWilly Tarreau #define O_CREAT 0x40 66930ca2051SWilly Tarreau #define O_EXCL 0x80 67030ca2051SWilly Tarreau #define O_NOCTTY 0x100 67130ca2051SWilly Tarreau #define O_TRUNC 0x200 67230ca2051SWilly Tarreau #define O_APPEND 0x400 67330ca2051SWilly Tarreau #define O_NONBLOCK 0x800 67430ca2051SWilly Tarreau #define O_DIRECTORY 0x4000 67530ca2051SWilly Tarreau 67630ca2051SWilly Tarreau /* The struct returned by the stat() syscall, 32-bit only, the syscall returns 67730ca2051SWilly Tarreau * exactly 56 bytes (stops before the unused array). In big endian, the format 67830ca2051SWilly Tarreau * differs as devices are returned as short only. 67930ca2051SWilly Tarreau */ 68030ca2051SWilly Tarreau struct sys_stat_struct { 68130ca2051SWilly Tarreau #if defined(__ARMEB__) 68230ca2051SWilly Tarreau unsigned short st_dev; 68330ca2051SWilly Tarreau unsigned short __pad1; 68430ca2051SWilly Tarreau #else 68530ca2051SWilly Tarreau unsigned long st_dev; 68630ca2051SWilly Tarreau #endif 68730ca2051SWilly Tarreau unsigned long st_ino; 68830ca2051SWilly Tarreau unsigned short st_mode; 68930ca2051SWilly Tarreau unsigned short st_nlink; 69030ca2051SWilly Tarreau unsigned short st_uid; 69130ca2051SWilly Tarreau unsigned short st_gid; 69230ca2051SWilly Tarreau #if defined(__ARMEB__) 69330ca2051SWilly Tarreau unsigned short st_rdev; 69430ca2051SWilly Tarreau unsigned short __pad2; 69530ca2051SWilly Tarreau #else 69630ca2051SWilly Tarreau unsigned long st_rdev; 69730ca2051SWilly Tarreau #endif 69830ca2051SWilly Tarreau unsigned long st_size; 69930ca2051SWilly Tarreau unsigned long st_blksize; 70030ca2051SWilly Tarreau unsigned long st_blocks; 70130ca2051SWilly Tarreau unsigned long st_atime; 70230ca2051SWilly Tarreau unsigned long st_atime_nsec; 70330ca2051SWilly Tarreau unsigned long st_mtime; 70430ca2051SWilly Tarreau unsigned long st_mtime_nsec; 70530ca2051SWilly Tarreau unsigned long st_ctime; 70630ca2051SWilly Tarreau unsigned long st_ctime_nsec; 70730ca2051SWilly Tarreau unsigned long __unused[2]; 70830ca2051SWilly Tarreau }; 70930ca2051SWilly Tarreau 71030ca2051SWilly Tarreau #elif defined(__aarch64__) 71130ca2051SWilly Tarreau /* Syscalls for AARCH64 : 71230ca2051SWilly Tarreau * - registers are 64-bit 71330ca2051SWilly Tarreau * - stack is 16-byte aligned 71430ca2051SWilly Tarreau * - syscall number is passed in x8 71530ca2051SWilly Tarreau * - arguments are in x0, x1, x2, x3, x4, x5 71630ca2051SWilly Tarreau * - the system call is performed by calling svc 0 71730ca2051SWilly Tarreau * - syscall return comes in x0. 71830ca2051SWilly Tarreau * - the arguments are cast to long and assigned into the target registers 71930ca2051SWilly Tarreau * which are then simply passed as registers to the asm code, so that we 72030ca2051SWilly Tarreau * don't have to experience issues with register constraints. 72130ca2051SWilly Tarreau * 72230ca2051SWilly Tarreau * On aarch64, select() is not implemented so we have to use pselect6(). 72330ca2051SWilly Tarreau */ 72430ca2051SWilly Tarreau #define __ARCH_WANT_SYS_PSELECT6 72530ca2051SWilly Tarreau 72630ca2051SWilly Tarreau #define my_syscall0(num) \ 72730ca2051SWilly Tarreau ({ \ 72830ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 72930ca2051SWilly Tarreau register long _arg1 asm("x0"); \ 73030ca2051SWilly Tarreau \ 73130ca2051SWilly Tarreau asm volatile ( \ 73230ca2051SWilly Tarreau "svc #0\n" \ 73330ca2051SWilly Tarreau : "=r"(_arg1) \ 73430ca2051SWilly Tarreau : "r"(_num) \ 73530ca2051SWilly Tarreau : "memory", "cc" \ 73630ca2051SWilly Tarreau ); \ 73730ca2051SWilly Tarreau _arg1; \ 73830ca2051SWilly Tarreau }) 73930ca2051SWilly Tarreau 74030ca2051SWilly Tarreau #define my_syscall1(num, arg1) \ 74130ca2051SWilly Tarreau ({ \ 74230ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 74330ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 74430ca2051SWilly Tarreau \ 74530ca2051SWilly Tarreau asm volatile ( \ 74630ca2051SWilly Tarreau "svc #0\n" \ 74730ca2051SWilly Tarreau : "=r"(_arg1) \ 74830ca2051SWilly Tarreau : "r"(_arg1), \ 74930ca2051SWilly Tarreau "r"(_num) \ 75030ca2051SWilly Tarreau : "memory", "cc" \ 75130ca2051SWilly Tarreau ); \ 75230ca2051SWilly Tarreau _arg1; \ 75330ca2051SWilly Tarreau }) 75430ca2051SWilly Tarreau 75530ca2051SWilly Tarreau #define my_syscall2(num, arg1, arg2) \ 75630ca2051SWilly Tarreau ({ \ 75730ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 75830ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 75930ca2051SWilly Tarreau register long _arg2 asm("x1") = (long)(arg2); \ 76030ca2051SWilly Tarreau \ 76130ca2051SWilly Tarreau asm volatile ( \ 76230ca2051SWilly Tarreau "svc #0\n" \ 76330ca2051SWilly Tarreau : "=r"(_arg1) \ 76430ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), \ 76530ca2051SWilly Tarreau "r"(_num) \ 76630ca2051SWilly Tarreau : "memory", "cc" \ 76730ca2051SWilly Tarreau ); \ 76830ca2051SWilly Tarreau _arg1; \ 76930ca2051SWilly Tarreau }) 77030ca2051SWilly Tarreau 77130ca2051SWilly Tarreau #define my_syscall3(num, arg1, arg2, arg3) \ 77230ca2051SWilly Tarreau ({ \ 77330ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 77430ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 77530ca2051SWilly Tarreau register long _arg2 asm("x1") = (long)(arg2); \ 77630ca2051SWilly Tarreau register long _arg3 asm("x2") = (long)(arg3); \ 77730ca2051SWilly Tarreau \ 77830ca2051SWilly Tarreau asm volatile ( \ 77930ca2051SWilly Tarreau "svc #0\n" \ 78030ca2051SWilly Tarreau : "=r"(_arg1) \ 78130ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 78230ca2051SWilly Tarreau "r"(_num) \ 78330ca2051SWilly Tarreau : "memory", "cc" \ 78430ca2051SWilly Tarreau ); \ 78530ca2051SWilly Tarreau _arg1; \ 78630ca2051SWilly Tarreau }) 78730ca2051SWilly Tarreau 78830ca2051SWilly Tarreau #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 78930ca2051SWilly Tarreau ({ \ 79030ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 79130ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 79230ca2051SWilly Tarreau register long _arg2 asm("x1") = (long)(arg2); \ 79330ca2051SWilly Tarreau register long _arg3 asm("x2") = (long)(arg3); \ 79430ca2051SWilly Tarreau register long _arg4 asm("x3") = (long)(arg4); \ 79530ca2051SWilly Tarreau \ 79630ca2051SWilly Tarreau asm volatile ( \ 79730ca2051SWilly Tarreau "svc #0\n" \ 79830ca2051SWilly Tarreau : "=r"(_arg1) \ 79930ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 80030ca2051SWilly Tarreau "r"(_num) \ 80130ca2051SWilly Tarreau : "memory", "cc" \ 80230ca2051SWilly Tarreau ); \ 80330ca2051SWilly Tarreau _arg1; \ 80430ca2051SWilly Tarreau }) 80530ca2051SWilly Tarreau 80630ca2051SWilly Tarreau #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 80730ca2051SWilly Tarreau ({ \ 80830ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 80930ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 81030ca2051SWilly Tarreau register long _arg2 asm("x1") = (long)(arg2); \ 81130ca2051SWilly Tarreau register long _arg3 asm("x2") = (long)(arg3); \ 81230ca2051SWilly Tarreau register long _arg4 asm("x3") = (long)(arg4); \ 81330ca2051SWilly Tarreau register long _arg5 asm("x4") = (long)(arg5); \ 81430ca2051SWilly Tarreau \ 81530ca2051SWilly Tarreau asm volatile ( \ 81630ca2051SWilly Tarreau "svc #0\n" \ 81730ca2051SWilly Tarreau : "=r" (_arg1) \ 81830ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 81930ca2051SWilly Tarreau "r"(_num) \ 82030ca2051SWilly Tarreau : "memory", "cc" \ 82130ca2051SWilly Tarreau ); \ 82230ca2051SWilly Tarreau _arg1; \ 82330ca2051SWilly Tarreau }) 82430ca2051SWilly Tarreau 82530ca2051SWilly Tarreau #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 82630ca2051SWilly Tarreau ({ \ 82730ca2051SWilly Tarreau register long _num asm("x8") = (num); \ 82830ca2051SWilly Tarreau register long _arg1 asm("x0") = (long)(arg1); \ 82930ca2051SWilly Tarreau register long _arg2 asm("x1") = (long)(arg2); \ 83030ca2051SWilly Tarreau register long _arg3 asm("x2") = (long)(arg3); \ 83130ca2051SWilly Tarreau register long _arg4 asm("x3") = (long)(arg4); \ 83230ca2051SWilly Tarreau register long _arg5 asm("x4") = (long)(arg5); \ 83330ca2051SWilly Tarreau register long _arg6 asm("x5") = (long)(arg6); \ 83430ca2051SWilly Tarreau \ 83530ca2051SWilly Tarreau asm volatile ( \ 83630ca2051SWilly Tarreau "svc #0\n" \ 83730ca2051SWilly Tarreau : "=r" (_arg1) \ 83830ca2051SWilly Tarreau : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 83930ca2051SWilly Tarreau "r"(_arg6), "r"(_num) \ 84030ca2051SWilly Tarreau : "memory", "cc" \ 84130ca2051SWilly Tarreau ); \ 84230ca2051SWilly Tarreau _arg1; \ 84330ca2051SWilly Tarreau }) 84430ca2051SWilly Tarreau 84530ca2051SWilly Tarreau /* startup code */ 84630ca2051SWilly Tarreau asm(".section .text\n" 84730ca2051SWilly Tarreau ".global _start\n" 84830ca2051SWilly Tarreau "_start:\n" 84930ca2051SWilly Tarreau "ldr x0, [sp]\n" // argc (x0) was in the stack 85030ca2051SWilly Tarreau "add x1, sp, 8\n" // argv (x1) = sp 85130ca2051SWilly Tarreau "lsl x2, x0, 3\n" // envp (x2) = 8*argc ... 85230ca2051SWilly Tarreau "add x2, x2, 8\n" // + 8 (skip null) 85330ca2051SWilly Tarreau "add x2, x2, x1\n" // + argv 85430ca2051SWilly Tarreau "and sp, x1, -16\n" // sp must be 16-byte aligned in the callee 85530ca2051SWilly Tarreau "bl main\n" // main() returns the status code, we'll exit with it. 85630ca2051SWilly Tarreau "mov x8, 93\n" // NR_exit == 93 85730ca2051SWilly Tarreau "svc #0\n" 85830ca2051SWilly Tarreau ""); 85930ca2051SWilly Tarreau 86030ca2051SWilly Tarreau /* fcntl / open */ 86130ca2051SWilly Tarreau #define O_RDONLY 0 86230ca2051SWilly Tarreau #define O_WRONLY 1 86330ca2051SWilly Tarreau #define O_RDWR 2 86430ca2051SWilly Tarreau #define O_CREAT 0x40 86530ca2051SWilly Tarreau #define O_EXCL 0x80 86630ca2051SWilly Tarreau #define O_NOCTTY 0x100 86730ca2051SWilly Tarreau #define O_TRUNC 0x200 86830ca2051SWilly Tarreau #define O_APPEND 0x400 86930ca2051SWilly Tarreau #define O_NONBLOCK 0x800 87030ca2051SWilly Tarreau #define O_DIRECTORY 0x4000 87130ca2051SWilly Tarreau 87230ca2051SWilly Tarreau /* The struct returned by the newfstatat() syscall. Differs slightly from the 87330ca2051SWilly Tarreau * x86_64's stat one by field ordering, so be careful. 87430ca2051SWilly Tarreau */ 87530ca2051SWilly Tarreau struct sys_stat_struct { 87630ca2051SWilly Tarreau unsigned long st_dev; 87730ca2051SWilly Tarreau unsigned long st_ino; 87830ca2051SWilly Tarreau unsigned int st_mode; 87930ca2051SWilly Tarreau unsigned int st_nlink; 88030ca2051SWilly Tarreau unsigned int st_uid; 88130ca2051SWilly Tarreau unsigned int st_gid; 88230ca2051SWilly Tarreau 88330ca2051SWilly Tarreau unsigned long st_rdev; 88430ca2051SWilly Tarreau unsigned long __pad1; 88530ca2051SWilly Tarreau long st_size; 88630ca2051SWilly Tarreau int st_blksize; 88730ca2051SWilly Tarreau int __pad2; 88830ca2051SWilly Tarreau 88930ca2051SWilly Tarreau long st_blocks; 89030ca2051SWilly Tarreau long st_atime; 89130ca2051SWilly Tarreau unsigned long st_atime_nsec; 89230ca2051SWilly Tarreau long st_mtime; 89330ca2051SWilly Tarreau 89430ca2051SWilly Tarreau unsigned long st_mtime_nsec; 89530ca2051SWilly Tarreau long st_ctime; 89630ca2051SWilly Tarreau unsigned long st_ctime_nsec; 89730ca2051SWilly Tarreau unsigned int __unused[2]; 89830ca2051SWilly Tarreau }; 89930ca2051SWilly Tarreau 90030ca2051SWilly Tarreau #elif defined(__mips__) && defined(_ABIO32) 90130ca2051SWilly Tarreau /* Syscalls for MIPS ABI O32 : 90230ca2051SWilly Tarreau * - WARNING! there's always a delayed slot! 90330ca2051SWilly Tarreau * - WARNING again, the syntax is different, registers take a '$' and numbers 90430ca2051SWilly Tarreau * do not. 90530ca2051SWilly Tarreau * - registers are 32-bit 90630ca2051SWilly Tarreau * - stack is 8-byte aligned 90730ca2051SWilly Tarreau * - syscall number is passed in v0 (starts at 0xfa0). 90830ca2051SWilly Tarreau * - arguments are in a0, a1, a2, a3, then the stack. The caller needs to 90930ca2051SWilly Tarreau * leave some room in the stack for the callee to save a0..a3 if needed. 91030ca2051SWilly Tarreau * - Many registers are clobbered, in fact only a0..a2 and s0..s8 are 91130ca2051SWilly Tarreau * preserved. See: https://www.linux-mips.org/wiki/Syscall as well as 91230ca2051SWilly Tarreau * scall32-o32.S in the kernel sources. 91330ca2051SWilly Tarreau * - the system call is performed by calling "syscall" 91430ca2051SWilly Tarreau * - syscall return comes in v0, and register a3 needs to be checked to know 9159a83f9aeSBhaskar Chowdhury * if an error occurred, in which case errno is in v0. 91630ca2051SWilly Tarreau * - the arguments are cast to long and assigned into the target registers 91730ca2051SWilly Tarreau * which are then simply passed as registers to the asm code, so that we 91830ca2051SWilly Tarreau * don't have to experience issues with register constraints. 91930ca2051SWilly Tarreau */ 92030ca2051SWilly Tarreau 92130ca2051SWilly Tarreau #define my_syscall0(num) \ 92230ca2051SWilly Tarreau ({ \ 92330ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 92430ca2051SWilly Tarreau register long _arg4 asm("a3"); \ 92530ca2051SWilly Tarreau \ 92630ca2051SWilly Tarreau asm volatile ( \ 92730ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 92830ca2051SWilly Tarreau "syscall\n" \ 92930ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 93030ca2051SWilly Tarreau : "=r"(_num), "=r"(_arg4) \ 93130ca2051SWilly Tarreau : "r"(_num) \ 93230ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 93330ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 93430ca2051SWilly Tarreau ); \ 93530ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 93630ca2051SWilly Tarreau }) 93730ca2051SWilly Tarreau 93830ca2051SWilly Tarreau #define my_syscall1(num, arg1) \ 93930ca2051SWilly Tarreau ({ \ 94030ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 94130ca2051SWilly Tarreau register long _arg1 asm("a0") = (long)(arg1); \ 94230ca2051SWilly Tarreau register long _arg4 asm("a3"); \ 94330ca2051SWilly Tarreau \ 94430ca2051SWilly Tarreau asm volatile ( \ 94530ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 94630ca2051SWilly Tarreau "syscall\n" \ 94730ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 94830ca2051SWilly Tarreau : "=r"(_num), "=r"(_arg4) \ 94930ca2051SWilly Tarreau : "0"(_num), \ 95030ca2051SWilly Tarreau "r"(_arg1) \ 95130ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 95230ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 95330ca2051SWilly Tarreau ); \ 95430ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 95530ca2051SWilly Tarreau }) 95630ca2051SWilly Tarreau 95730ca2051SWilly Tarreau #define my_syscall2(num, arg1, arg2) \ 95830ca2051SWilly Tarreau ({ \ 95930ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 96030ca2051SWilly Tarreau register long _arg1 asm("a0") = (long)(arg1); \ 96130ca2051SWilly Tarreau register long _arg2 asm("a1") = (long)(arg2); \ 96230ca2051SWilly Tarreau register long _arg4 asm("a3"); \ 96330ca2051SWilly Tarreau \ 96430ca2051SWilly Tarreau asm volatile ( \ 96530ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 96630ca2051SWilly Tarreau "syscall\n" \ 96730ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 96830ca2051SWilly Tarreau : "=r"(_num), "=r"(_arg4) \ 96930ca2051SWilly Tarreau : "0"(_num), \ 97030ca2051SWilly Tarreau "r"(_arg1), "r"(_arg2) \ 97130ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 97230ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 97330ca2051SWilly Tarreau ); \ 97430ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 97530ca2051SWilly Tarreau }) 97630ca2051SWilly Tarreau 97730ca2051SWilly Tarreau #define my_syscall3(num, arg1, arg2, arg3) \ 97830ca2051SWilly Tarreau ({ \ 97930ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 98030ca2051SWilly Tarreau register long _arg1 asm("a0") = (long)(arg1); \ 98130ca2051SWilly Tarreau register long _arg2 asm("a1") = (long)(arg2); \ 98230ca2051SWilly Tarreau register long _arg3 asm("a2") = (long)(arg3); \ 98330ca2051SWilly Tarreau register long _arg4 asm("a3"); \ 98430ca2051SWilly Tarreau \ 98530ca2051SWilly Tarreau asm volatile ( \ 98630ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 98730ca2051SWilly Tarreau "syscall\n" \ 98830ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 98930ca2051SWilly Tarreau : "=r"(_num), "=r"(_arg4) \ 99030ca2051SWilly Tarreau : "0"(_num), \ 99130ca2051SWilly Tarreau "r"(_arg1), "r"(_arg2), "r"(_arg3) \ 99230ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 99330ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 99430ca2051SWilly Tarreau ); \ 99530ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 99630ca2051SWilly Tarreau }) 99730ca2051SWilly Tarreau 99830ca2051SWilly Tarreau #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 99930ca2051SWilly Tarreau ({ \ 100030ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 100130ca2051SWilly Tarreau register long _arg1 asm("a0") = (long)(arg1); \ 100230ca2051SWilly Tarreau register long _arg2 asm("a1") = (long)(arg2); \ 100330ca2051SWilly Tarreau register long _arg3 asm("a2") = (long)(arg3); \ 100430ca2051SWilly Tarreau register long _arg4 asm("a3") = (long)(arg4); \ 100530ca2051SWilly Tarreau \ 100630ca2051SWilly Tarreau asm volatile ( \ 100730ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 100830ca2051SWilly Tarreau "syscall\n" \ 100930ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 101030ca2051SWilly Tarreau : "=r" (_num), "=r"(_arg4) \ 101130ca2051SWilly Tarreau : "0"(_num), \ 101230ca2051SWilly Tarreau "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4) \ 101330ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 101430ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 101530ca2051SWilly Tarreau ); \ 101630ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 101730ca2051SWilly Tarreau }) 101830ca2051SWilly Tarreau 101930ca2051SWilly Tarreau #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 102030ca2051SWilly Tarreau ({ \ 102130ca2051SWilly Tarreau register long _num asm("v0") = (num); \ 102230ca2051SWilly Tarreau register long _arg1 asm("a0") = (long)(arg1); \ 102330ca2051SWilly Tarreau register long _arg2 asm("a1") = (long)(arg2); \ 102430ca2051SWilly Tarreau register long _arg3 asm("a2") = (long)(arg3); \ 102530ca2051SWilly Tarreau register long _arg4 asm("a3") = (long)(arg4); \ 102630ca2051SWilly Tarreau register long _arg5 = (long)(arg5); \ 102730ca2051SWilly Tarreau \ 102830ca2051SWilly Tarreau asm volatile ( \ 102930ca2051SWilly Tarreau "addiu $sp, $sp, -32\n" \ 103030ca2051SWilly Tarreau "sw %7, 16($sp)\n" \ 103130ca2051SWilly Tarreau "syscall\n " \ 103230ca2051SWilly Tarreau "addiu $sp, $sp, 32\n" \ 103330ca2051SWilly Tarreau : "=r" (_num), "=r"(_arg4) \ 103430ca2051SWilly Tarreau : "0"(_num), \ 103530ca2051SWilly Tarreau "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5) \ 103630ca2051SWilly Tarreau : "memory", "cc", "at", "v1", "hi", "lo", \ 103730ca2051SWilly Tarreau "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 103830ca2051SWilly Tarreau ); \ 103930ca2051SWilly Tarreau _arg4 ? -_num : _num; \ 104030ca2051SWilly Tarreau }) 104130ca2051SWilly Tarreau 104230ca2051SWilly Tarreau /* startup code, note that it's called __start on MIPS */ 104330ca2051SWilly Tarreau asm(".section .text\n" 104430ca2051SWilly Tarreau ".set nomips16\n" 104530ca2051SWilly Tarreau ".global __start\n" 104630ca2051SWilly Tarreau ".set noreorder\n" 104730ca2051SWilly Tarreau ".option pic0\n" 104830ca2051SWilly Tarreau ".ent __start\n" 104930ca2051SWilly Tarreau "__start:\n" 105030ca2051SWilly Tarreau "lw $a0,($sp)\n" // argc was in the stack 105130ca2051SWilly Tarreau "addiu $a1, $sp, 4\n" // argv = sp + 4 105230ca2051SWilly Tarreau "sll $a2, $a0, 2\n" // a2 = argc * 4 105330ca2051SWilly Tarreau "add $a2, $a2, $a1\n" // envp = argv + 4*argc ... 105430ca2051SWilly Tarreau "addiu $a2, $a2, 4\n" // ... + 4 105530ca2051SWilly Tarreau "li $t0, -8\n" 105630ca2051SWilly Tarreau "and $sp, $sp, $t0\n" // sp must be 8-byte aligned 105730ca2051SWilly Tarreau "addiu $sp,$sp,-16\n" // the callee expects to save a0..a3 there! 105830ca2051SWilly Tarreau "jal main\n" // main() returns the status code, we'll exit with it. 105930ca2051SWilly Tarreau "nop\n" // delayed slot 1060de0244aeSWilly Tarreau "move $a0, $v0\n" // retrieve 32-bit exit code from v0 106130ca2051SWilly Tarreau "li $v0, 4001\n" // NR_exit == 4001 106230ca2051SWilly Tarreau "syscall\n" 106330ca2051SWilly Tarreau ".end __start\n" 106430ca2051SWilly Tarreau ""); 106530ca2051SWilly Tarreau 106630ca2051SWilly Tarreau /* fcntl / open */ 106730ca2051SWilly Tarreau #define O_RDONLY 0 106830ca2051SWilly Tarreau #define O_WRONLY 1 106930ca2051SWilly Tarreau #define O_RDWR 2 107030ca2051SWilly Tarreau #define O_APPEND 0x0008 107130ca2051SWilly Tarreau #define O_NONBLOCK 0x0080 107230ca2051SWilly Tarreau #define O_CREAT 0x0100 107330ca2051SWilly Tarreau #define O_TRUNC 0x0200 107430ca2051SWilly Tarreau #define O_EXCL 0x0400 107530ca2051SWilly Tarreau #define O_NOCTTY 0x0800 107630ca2051SWilly Tarreau #define O_DIRECTORY 0x10000 107730ca2051SWilly Tarreau 107830ca2051SWilly Tarreau /* The struct returned by the stat() syscall. 88 bytes are returned by the 107930ca2051SWilly Tarreau * syscall. 108030ca2051SWilly Tarreau */ 108130ca2051SWilly Tarreau struct sys_stat_struct { 108230ca2051SWilly Tarreau unsigned int st_dev; 108330ca2051SWilly Tarreau long st_pad1[3]; 108430ca2051SWilly Tarreau unsigned long st_ino; 108530ca2051SWilly Tarreau unsigned int st_mode; 108630ca2051SWilly Tarreau unsigned int st_nlink; 108730ca2051SWilly Tarreau unsigned int st_uid; 108830ca2051SWilly Tarreau unsigned int st_gid; 108930ca2051SWilly Tarreau unsigned int st_rdev; 109030ca2051SWilly Tarreau long st_pad2[2]; 109130ca2051SWilly Tarreau long st_size; 109230ca2051SWilly Tarreau long st_pad3; 109330ca2051SWilly Tarreau long st_atime; 109430ca2051SWilly Tarreau long st_atime_nsec; 109530ca2051SWilly Tarreau long st_mtime; 109630ca2051SWilly Tarreau long st_mtime_nsec; 109730ca2051SWilly Tarreau long st_ctime; 109830ca2051SWilly Tarreau long st_ctime_nsec; 109930ca2051SWilly Tarreau long st_blksize; 110030ca2051SWilly Tarreau long st_blocks; 110130ca2051SWilly Tarreau long st_pad4[14]; 110230ca2051SWilly Tarreau }; 110330ca2051SWilly Tarreau 1104582e84f7SPranith Kumar #elif defined(__riscv) 1105582e84f7SPranith Kumar 1106582e84f7SPranith Kumar #if __riscv_xlen == 64 1107582e84f7SPranith Kumar #define PTRLOG "3" 1108582e84f7SPranith Kumar #define SZREG "8" 1109582e84f7SPranith Kumar #elif __riscv_xlen == 32 1110582e84f7SPranith Kumar #define PTRLOG "2" 1111582e84f7SPranith Kumar #define SZREG "4" 1112582e84f7SPranith Kumar #endif 1113582e84f7SPranith Kumar 1114582e84f7SPranith Kumar /* Syscalls for RISCV : 1115582e84f7SPranith Kumar * - stack is 16-byte aligned 1116582e84f7SPranith Kumar * - syscall number is passed in a7 1117582e84f7SPranith Kumar * - arguments are in a0, a1, a2, a3, a4, a5 1118582e84f7SPranith Kumar * - the system call is performed by calling ecall 1119582e84f7SPranith Kumar * - syscall return comes in a0 1120582e84f7SPranith Kumar * - the arguments are cast to long and assigned into the target 1121582e84f7SPranith Kumar * registers which are then simply passed as registers to the asm code, 1122582e84f7SPranith Kumar * so that we don't have to experience issues with register constraints. 11239c2970fbSWilly Tarreau * 11249c2970fbSWilly Tarreau * On riscv, select() is not implemented so we have to use pselect6(). 1125582e84f7SPranith Kumar */ 11269c2970fbSWilly Tarreau #define __ARCH_WANT_SYS_PSELECT6 1127582e84f7SPranith Kumar 1128582e84f7SPranith Kumar #define my_syscall0(num) \ 1129582e84f7SPranith Kumar ({ \ 1130582e84f7SPranith Kumar register long _num asm("a7") = (num); \ 1131582e84f7SPranith Kumar register long _arg1 asm("a0"); \ 1132582e84f7SPranith Kumar \ 1133582e84f7SPranith Kumar asm volatile ( \ 1134582e84f7SPranith Kumar "ecall\n\t" \ 1135582e84f7SPranith Kumar : "=r"(_arg1) \ 1136582e84f7SPranith Kumar : "r"(_num) \ 1137582e84f7SPranith Kumar : "memory", "cc" \ 1138582e84f7SPranith Kumar ); \ 1139582e84f7SPranith Kumar _arg1; \ 1140582e84f7SPranith Kumar }) 1141582e84f7SPranith Kumar 1142582e84f7SPranith Kumar #define my_syscall1(num, arg1) \ 1143582e84f7SPranith Kumar ({ \ 1144582e84f7SPranith Kumar register long _num asm("a7") = (num); \ 1145582e84f7SPranith Kumar register long _arg1 asm("a0") = (long)(arg1); \ 1146582e84f7SPranith Kumar \ 1147582e84f7SPranith Kumar asm volatile ( \ 1148582e84f7SPranith Kumar "ecall\n" \ 1149582e84f7SPranith Kumar : "+r"(_arg1) \ 1150582e84f7SPranith Kumar : "r"(_num) \ 1151582e84f7SPranith Kumar : "memory", "cc" \ 1152582e84f7SPranith Kumar ); \ 1153582e84f7SPranith Kumar _arg1; \ 1154582e84f7SPranith Kumar }) 1155582e84f7SPranith Kumar 1156582e84f7SPranith Kumar #define my_syscall2(num, arg1, arg2) \ 1157582e84f7SPranith Kumar ({ \ 1158582e84f7SPranith Kumar register long _num asm("a7") = (num); \ 1159582e84f7SPranith Kumar register long _arg1 asm("a0") = (long)(arg1); \ 1160582e84f7SPranith Kumar register long _arg2 asm("a1") = (long)(arg2); \ 1161582e84f7SPranith Kumar \ 1162582e84f7SPranith Kumar asm volatile ( \ 1163582e84f7SPranith Kumar "ecall\n" \ 1164582e84f7SPranith Kumar : "+r"(_arg1) \ 1165582e84f7SPranith Kumar : "r"(_arg2), \ 1166582e84f7SPranith Kumar "r"(_num) \ 1167582e84f7SPranith Kumar : "memory", "cc" \ 1168582e84f7SPranith Kumar ); \ 1169582e84f7SPranith Kumar _arg1; \ 1170582e84f7SPranith Kumar }) 1171582e84f7SPranith Kumar 1172582e84f7SPranith Kumar #define my_syscall3(num, arg1, arg2, arg3) \ 1173582e84f7SPranith Kumar ({ \ 1174582e84f7SPranith Kumar register long _num asm("a7") = (num); \ 1175582e84f7SPranith Kumar register long _arg1 asm("a0") = (long)(arg1); \ 1176582e84f7SPranith Kumar register long _arg2 asm("a1") = (long)(arg2); \ 1177582e84f7SPranith Kumar register long _arg3 asm("a2") = (long)(arg3); \ 1178582e84f7SPranith Kumar \ 1179582e84f7SPranith Kumar asm volatile ( \ 1180582e84f7SPranith Kumar "ecall\n\t" \ 1181582e84f7SPranith Kumar : "+r"(_arg1) \ 1182582e84f7SPranith Kumar : "r"(_arg2), "r"(_arg3), \ 1183582e84f7SPranith Kumar "r"(_num) \ 1184582e84f7SPranith Kumar : "memory", "cc" \ 1185582e84f7SPranith Kumar ); \ 1186582e84f7SPranith Kumar _arg1; \ 1187582e84f7SPranith Kumar }) 1188582e84f7SPranith Kumar 1189582e84f7SPranith Kumar #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 1190582e84f7SPranith Kumar ({ \ 1191582e84f7SPranith Kumar register long _num asm("a7") = (num); \ 1192582e84f7SPranith Kumar register long _arg1 asm("a0") = (long)(arg1); \ 1193582e84f7SPranith Kumar register long _arg2 asm("a1") = (long)(arg2); \ 1194582e84f7SPranith Kumar register long _arg3 asm("a2") = (long)(arg3); \ 1195582e84f7SPranith Kumar register long _arg4 asm("a3") = (long)(arg4); \ 1196582e84f7SPranith Kumar \ 1197582e84f7SPranith Kumar asm volatile ( \ 1198582e84f7SPranith Kumar "ecall\n" \ 1199582e84f7SPranith Kumar : "+r"(_arg1) \ 1200582e84f7SPranith Kumar : "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 1201582e84f7SPranith Kumar "r"(_num) \ 1202582e84f7SPranith Kumar : "memory", "cc" \ 1203582e84f7SPranith Kumar ); \ 1204582e84f7SPranith Kumar _arg1; \ 1205582e84f7SPranith Kumar }) 1206582e84f7SPranith Kumar 1207582e84f7SPranith Kumar #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 1208582e84f7SPranith Kumar ({ \ 1209582e84f7SPranith Kumar register long _num asm("a7") = (num); \ 1210582e84f7SPranith Kumar register long _arg1 asm("a0") = (long)(arg1); \ 1211582e84f7SPranith Kumar register long _arg2 asm("a1") = (long)(arg2); \ 1212582e84f7SPranith Kumar register long _arg3 asm("a2") = (long)(arg3); \ 1213582e84f7SPranith Kumar register long _arg4 asm("a3") = (long)(arg4); \ 1214582e84f7SPranith Kumar register long _arg5 asm("a4") = (long)(arg5); \ 1215582e84f7SPranith Kumar \ 1216582e84f7SPranith Kumar asm volatile ( \ 1217582e84f7SPranith Kumar "ecall\n" \ 1218582e84f7SPranith Kumar : "+r"(_arg1) \ 1219582e84f7SPranith Kumar : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 1220582e84f7SPranith Kumar "r"(_num) \ 1221582e84f7SPranith Kumar : "memory", "cc" \ 1222582e84f7SPranith Kumar ); \ 1223582e84f7SPranith Kumar _arg1; \ 1224582e84f7SPranith Kumar }) 1225582e84f7SPranith Kumar 1226582e84f7SPranith Kumar #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 1227582e84f7SPranith Kumar ({ \ 1228582e84f7SPranith Kumar register long _num asm("a7") = (num); \ 1229582e84f7SPranith Kumar register long _arg1 asm("a0") = (long)(arg1); \ 1230582e84f7SPranith Kumar register long _arg2 asm("a1") = (long)(arg2); \ 1231582e84f7SPranith Kumar register long _arg3 asm("a2") = (long)(arg3); \ 1232582e84f7SPranith Kumar register long _arg4 asm("a3") = (long)(arg4); \ 1233582e84f7SPranith Kumar register long _arg5 asm("a4") = (long)(arg5); \ 1234582e84f7SPranith Kumar register long _arg6 asm("a5") = (long)(arg6); \ 1235582e84f7SPranith Kumar \ 1236582e84f7SPranith Kumar asm volatile ( \ 1237582e84f7SPranith Kumar "ecall\n" \ 1238582e84f7SPranith Kumar : "+r"(_arg1) \ 1239582e84f7SPranith Kumar : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), "r"(_arg6), \ 1240582e84f7SPranith Kumar "r"(_num) \ 1241582e84f7SPranith Kumar : "memory", "cc" \ 1242582e84f7SPranith Kumar ); \ 1243582e84f7SPranith Kumar _arg1; \ 1244582e84f7SPranith Kumar }) 1245582e84f7SPranith Kumar 1246582e84f7SPranith Kumar /* startup code */ 1247582e84f7SPranith Kumar asm(".section .text\n" 1248582e84f7SPranith Kumar ".global _start\n" 1249582e84f7SPranith Kumar "_start:\n" 1250582e84f7SPranith Kumar ".option push\n" 1251582e84f7SPranith Kumar ".option norelax\n" 1252582e84f7SPranith Kumar "lla gp, __global_pointer$\n" 1253582e84f7SPranith Kumar ".option pop\n" 1254582e84f7SPranith Kumar "ld a0, 0(sp)\n" // argc (a0) was in the stack 1255582e84f7SPranith Kumar "add a1, sp, "SZREG"\n" // argv (a1) = sp 1256582e84f7SPranith Kumar "slli a2, a0, "PTRLOG"\n" // envp (a2) = SZREG*argc ... 1257582e84f7SPranith Kumar "add a2, a2, "SZREG"\n" // + SZREG (skip null) 1258582e84f7SPranith Kumar "add a2,a2,a1\n" // + argv 1259582e84f7SPranith Kumar "andi sp,a1,-16\n" // sp must be 16-byte aligned 1260582e84f7SPranith Kumar "call main\n" // main() returns the status code, we'll exit with it. 1261582e84f7SPranith Kumar "li a7, 93\n" // NR_exit == 93 1262582e84f7SPranith Kumar "ecall\n" 1263582e84f7SPranith Kumar ""); 1264582e84f7SPranith Kumar 1265582e84f7SPranith Kumar /* fcntl / open */ 1266582e84f7SPranith Kumar #define O_RDONLY 0 1267582e84f7SPranith Kumar #define O_WRONLY 1 1268582e84f7SPranith Kumar #define O_RDWR 2 1269582e84f7SPranith Kumar #define O_CREAT 0x100 1270582e84f7SPranith Kumar #define O_EXCL 0x200 1271582e84f7SPranith Kumar #define O_NOCTTY 0x400 1272582e84f7SPranith Kumar #define O_TRUNC 0x1000 1273582e84f7SPranith Kumar #define O_APPEND 0x2000 1274582e84f7SPranith Kumar #define O_NONBLOCK 0x4000 1275582e84f7SPranith Kumar #define O_DIRECTORY 0x200000 1276582e84f7SPranith Kumar 1277582e84f7SPranith Kumar struct sys_stat_struct { 1278582e84f7SPranith Kumar unsigned long st_dev; /* Device. */ 1279582e84f7SPranith Kumar unsigned long st_ino; /* File serial number. */ 1280582e84f7SPranith Kumar unsigned int st_mode; /* File mode. */ 1281582e84f7SPranith Kumar unsigned int st_nlink; /* Link count. */ 1282582e84f7SPranith Kumar unsigned int st_uid; /* User ID of the file's owner. */ 1283582e84f7SPranith Kumar unsigned int st_gid; /* Group ID of the file's group. */ 1284582e84f7SPranith Kumar unsigned long st_rdev; /* Device number, if device. */ 1285582e84f7SPranith Kumar unsigned long __pad1; 1286582e84f7SPranith Kumar long st_size; /* Size of file, in bytes. */ 1287582e84f7SPranith Kumar int st_blksize; /* Optimal block size for I/O. */ 1288582e84f7SPranith Kumar int __pad2; 1289582e84f7SPranith Kumar long st_blocks; /* Number 512-byte blocks allocated. */ 1290582e84f7SPranith Kumar long st_atime; /* Time of last access. */ 1291582e84f7SPranith Kumar unsigned long st_atime_nsec; 1292582e84f7SPranith Kumar long st_mtime; /* Time of last modification. */ 1293582e84f7SPranith Kumar unsigned long st_mtime_nsec; 1294582e84f7SPranith Kumar long st_ctime; /* Time of last status change. */ 1295582e84f7SPranith Kumar unsigned long st_ctime_nsec; 1296582e84f7SPranith Kumar unsigned int __unused4; 1297582e84f7SPranith Kumar unsigned int __unused5; 1298582e84f7SPranith Kumar }; 1299582e84f7SPranith Kumar 130030ca2051SWilly Tarreau #endif 130130ca2051SWilly Tarreau 130230ca2051SWilly Tarreau 130330ca2051SWilly Tarreau /* Below are the C functions used to declare the raw syscalls. They try to be 130430ca2051SWilly Tarreau * architecture-agnostic, and return either a success or -errno. Declaring them 130530ca2051SWilly Tarreau * static will lead to them being inlined in most cases, but it's still possible 130630ca2051SWilly Tarreau * to reference them by a pointer if needed. 130730ca2051SWilly Tarreau */ 130830ca2051SWilly Tarreau static __attribute__((unused)) 130930ca2051SWilly Tarreau void *sys_brk(void *addr) 131030ca2051SWilly Tarreau { 131130ca2051SWilly Tarreau return (void *)my_syscall1(__NR_brk, addr); 131230ca2051SWilly Tarreau } 131330ca2051SWilly Tarreau 131430ca2051SWilly Tarreau static __attribute__((noreturn,unused)) 131530ca2051SWilly Tarreau void sys_exit(int status) 131630ca2051SWilly Tarreau { 131730ca2051SWilly Tarreau my_syscall1(__NR_exit, status & 255); 131830ca2051SWilly Tarreau while(1); // shut the "noreturn" warnings. 131930ca2051SWilly Tarreau } 132030ca2051SWilly Tarreau 132130ca2051SWilly Tarreau static __attribute__((unused)) 132230ca2051SWilly Tarreau int sys_chdir(const char *path) 132330ca2051SWilly Tarreau { 132430ca2051SWilly Tarreau return my_syscall1(__NR_chdir, path); 132530ca2051SWilly Tarreau } 132630ca2051SWilly Tarreau 132730ca2051SWilly Tarreau static __attribute__((unused)) 132830ca2051SWilly Tarreau int sys_chmod(const char *path, mode_t mode) 132930ca2051SWilly Tarreau { 133030ca2051SWilly Tarreau #ifdef __NR_fchmodat 133130ca2051SWilly Tarreau return my_syscall4(__NR_fchmodat, AT_FDCWD, path, mode, 0); 133235635d7fSWilly Tarreau #elif defined(__NR_chmod) 133330ca2051SWilly Tarreau return my_syscall2(__NR_chmod, path, mode); 133435635d7fSWilly Tarreau #else 133535635d7fSWilly Tarreau #error Neither __NR_fchmodat nor __NR_chmod defined, cannot implement sys_chmod() 133630ca2051SWilly Tarreau #endif 133730ca2051SWilly Tarreau } 133830ca2051SWilly Tarreau 133930ca2051SWilly Tarreau static __attribute__((unused)) 134030ca2051SWilly Tarreau int sys_chown(const char *path, uid_t owner, gid_t group) 134130ca2051SWilly Tarreau { 134230ca2051SWilly Tarreau #ifdef __NR_fchownat 134330ca2051SWilly Tarreau return my_syscall5(__NR_fchownat, AT_FDCWD, path, owner, group, 0); 134435635d7fSWilly Tarreau #elif defined(__NR_chown) 134530ca2051SWilly Tarreau return my_syscall3(__NR_chown, path, owner, group); 134635635d7fSWilly Tarreau #else 134735635d7fSWilly Tarreau #error Neither __NR_fchownat nor __NR_chown defined, cannot implement sys_chown() 134830ca2051SWilly Tarreau #endif 134930ca2051SWilly Tarreau } 135030ca2051SWilly Tarreau 135130ca2051SWilly Tarreau static __attribute__((unused)) 135230ca2051SWilly Tarreau int sys_chroot(const char *path) 135330ca2051SWilly Tarreau { 135430ca2051SWilly Tarreau return my_syscall1(__NR_chroot, path); 135530ca2051SWilly Tarreau } 135630ca2051SWilly Tarreau 135730ca2051SWilly Tarreau static __attribute__((unused)) 135830ca2051SWilly Tarreau int sys_close(int fd) 135930ca2051SWilly Tarreau { 136030ca2051SWilly Tarreau return my_syscall1(__NR_close, fd); 136130ca2051SWilly Tarreau } 136230ca2051SWilly Tarreau 136330ca2051SWilly Tarreau static __attribute__((unused)) 136430ca2051SWilly Tarreau int sys_dup(int fd) 136530ca2051SWilly Tarreau { 136630ca2051SWilly Tarreau return my_syscall1(__NR_dup, fd); 136730ca2051SWilly Tarreau } 136830ca2051SWilly Tarreau 136979f220e5SWilly Tarreau #ifdef __NR_dup3 137079f220e5SWilly Tarreau static __attribute__((unused)) 137179f220e5SWilly Tarreau int sys_dup3(int old, int new, int flags) 137279f220e5SWilly Tarreau { 137379f220e5SWilly Tarreau return my_syscall3(__NR_dup3, old, new, flags); 137479f220e5SWilly Tarreau } 137579f220e5SWilly Tarreau #endif 137679f220e5SWilly Tarreau 137730ca2051SWilly Tarreau static __attribute__((unused)) 137830ca2051SWilly Tarreau int sys_dup2(int old, int new) 137930ca2051SWilly Tarreau { 138079f220e5SWilly Tarreau #ifdef __NR_dup3 138179f220e5SWilly Tarreau return my_syscall3(__NR_dup3, old, new, 0); 138235635d7fSWilly Tarreau #elif defined(__NR_dup2) 138330ca2051SWilly Tarreau return my_syscall2(__NR_dup2, old, new); 138435635d7fSWilly Tarreau #else 138535635d7fSWilly Tarreau #error Neither __NR_dup3 nor __NR_dup2 defined, cannot implement sys_dup2() 138679f220e5SWilly Tarreau #endif 138730ca2051SWilly Tarreau } 138830ca2051SWilly Tarreau 138930ca2051SWilly Tarreau static __attribute__((unused)) 139030ca2051SWilly Tarreau int sys_execve(const char *filename, char *const argv[], char *const envp[]) 139130ca2051SWilly Tarreau { 139230ca2051SWilly Tarreau return my_syscall3(__NR_execve, filename, argv, envp); 139330ca2051SWilly Tarreau } 139430ca2051SWilly Tarreau 139530ca2051SWilly Tarreau static __attribute__((unused)) 139630ca2051SWilly Tarreau pid_t sys_fork(void) 139730ca2051SWilly Tarreau { 1398be60ca41SWilly Tarreau #ifdef __NR_clone 1399be60ca41SWilly Tarreau /* note: some archs only have clone() and not fork(). Different archs 1400be60ca41SWilly Tarreau * have a different API, but most archs have the flags on first arg and 1401be60ca41SWilly Tarreau * will not use the rest with no other flag. 1402be60ca41SWilly Tarreau */ 1403be60ca41SWilly Tarreau return my_syscall5(__NR_clone, SIGCHLD, 0, 0, 0, 0); 140435635d7fSWilly Tarreau #elif defined(__NR_fork) 140530ca2051SWilly Tarreau return my_syscall0(__NR_fork); 140635635d7fSWilly Tarreau #else 140735635d7fSWilly Tarreau #error Neither __NR_clone nor __NR_fork defined, cannot implement sys_fork() 1408be60ca41SWilly Tarreau #endif 140930ca2051SWilly Tarreau } 141030ca2051SWilly Tarreau 141130ca2051SWilly Tarreau static __attribute__((unused)) 141230ca2051SWilly Tarreau int sys_fsync(int fd) 141330ca2051SWilly Tarreau { 141430ca2051SWilly Tarreau return my_syscall1(__NR_fsync, fd); 141530ca2051SWilly Tarreau } 141630ca2051SWilly Tarreau 141730ca2051SWilly Tarreau static __attribute__((unused)) 141830ca2051SWilly Tarreau int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count) 141930ca2051SWilly Tarreau { 142030ca2051SWilly Tarreau return my_syscall3(__NR_getdents64, fd, dirp, count); 142130ca2051SWilly Tarreau } 142230ca2051SWilly Tarreau 142330ca2051SWilly Tarreau static __attribute__((unused)) 1424c0c7c103SWilly Tarreau pid_t sys_getpgid(pid_t pid) 1425c0c7c103SWilly Tarreau { 1426c0c7c103SWilly Tarreau return my_syscall1(__NR_getpgid, pid); 1427c0c7c103SWilly Tarreau } 1428c0c7c103SWilly Tarreau 1429c0c7c103SWilly Tarreau static __attribute__((unused)) 143030ca2051SWilly Tarreau pid_t sys_getpgrp(void) 143130ca2051SWilly Tarreau { 1432c0c7c103SWilly Tarreau return sys_getpgid(0); 143330ca2051SWilly Tarreau } 143430ca2051SWilly Tarreau 143530ca2051SWilly Tarreau static __attribute__((unused)) 143630ca2051SWilly Tarreau pid_t sys_getpid(void) 143730ca2051SWilly Tarreau { 143830ca2051SWilly Tarreau return my_syscall0(__NR_getpid); 143930ca2051SWilly Tarreau } 144030ca2051SWilly Tarreau 144130ca2051SWilly Tarreau static __attribute__((unused)) 1442b0fe9decSMark Brown pid_t sys_gettid(void) 1443b0fe9decSMark Brown { 1444b0fe9decSMark Brown return my_syscall0(__NR_gettid); 1445b0fe9decSMark Brown } 1446b0fe9decSMark Brown 1447b0fe9decSMark Brown static __attribute__((unused)) 144830ca2051SWilly Tarreau int sys_gettimeofday(struct timeval *tv, struct timezone *tz) 144930ca2051SWilly Tarreau { 145030ca2051SWilly Tarreau return my_syscall2(__NR_gettimeofday, tv, tz); 145130ca2051SWilly Tarreau } 145230ca2051SWilly Tarreau 145330ca2051SWilly Tarreau static __attribute__((unused)) 145430ca2051SWilly Tarreau int sys_ioctl(int fd, unsigned long req, void *value) 145530ca2051SWilly Tarreau { 145630ca2051SWilly Tarreau return my_syscall3(__NR_ioctl, fd, req, value); 145730ca2051SWilly Tarreau } 145830ca2051SWilly Tarreau 145930ca2051SWilly Tarreau static __attribute__((unused)) 146030ca2051SWilly Tarreau int sys_kill(pid_t pid, int signal) 146130ca2051SWilly Tarreau { 146230ca2051SWilly Tarreau return my_syscall2(__NR_kill, pid, signal); 146330ca2051SWilly Tarreau } 146430ca2051SWilly Tarreau 146530ca2051SWilly Tarreau static __attribute__((unused)) 146630ca2051SWilly Tarreau int sys_link(const char *old, const char *new) 146730ca2051SWilly Tarreau { 146830ca2051SWilly Tarreau #ifdef __NR_linkat 146930ca2051SWilly Tarreau return my_syscall5(__NR_linkat, AT_FDCWD, old, AT_FDCWD, new, 0); 147035635d7fSWilly Tarreau #elif defined(__NR_link) 147130ca2051SWilly Tarreau return my_syscall2(__NR_link, old, new); 147235635d7fSWilly Tarreau #else 147335635d7fSWilly Tarreau #error Neither __NR_linkat nor __NR_link defined, cannot implement sys_link() 147430ca2051SWilly Tarreau #endif 147530ca2051SWilly Tarreau } 147630ca2051SWilly Tarreau 147730ca2051SWilly Tarreau static __attribute__((unused)) 147830ca2051SWilly Tarreau off_t sys_lseek(int fd, off_t offset, int whence) 147930ca2051SWilly Tarreau { 148030ca2051SWilly Tarreau return my_syscall3(__NR_lseek, fd, offset, whence); 148130ca2051SWilly Tarreau } 148230ca2051SWilly Tarreau 148330ca2051SWilly Tarreau static __attribute__((unused)) 148430ca2051SWilly Tarreau int sys_mkdir(const char *path, mode_t mode) 148530ca2051SWilly Tarreau { 148630ca2051SWilly Tarreau #ifdef __NR_mkdirat 148730ca2051SWilly Tarreau return my_syscall3(__NR_mkdirat, AT_FDCWD, path, mode); 148835635d7fSWilly Tarreau #elif defined(__NR_mkdir) 148930ca2051SWilly Tarreau return my_syscall2(__NR_mkdir, path, mode); 149035635d7fSWilly Tarreau #else 149135635d7fSWilly Tarreau #error Neither __NR_mkdirat nor __NR_mkdir defined, cannot implement sys_mkdir() 149230ca2051SWilly Tarreau #endif 149330ca2051SWilly Tarreau } 149430ca2051SWilly Tarreau 149530ca2051SWilly Tarreau static __attribute__((unused)) 149630ca2051SWilly Tarreau long sys_mknod(const char *path, mode_t mode, dev_t dev) 149730ca2051SWilly Tarreau { 149830ca2051SWilly Tarreau #ifdef __NR_mknodat 149930ca2051SWilly Tarreau return my_syscall4(__NR_mknodat, AT_FDCWD, path, mode, dev); 150035635d7fSWilly Tarreau #elif defined(__NR_mknod) 150130ca2051SWilly Tarreau return my_syscall3(__NR_mknod, path, mode, dev); 150235635d7fSWilly Tarreau #else 150335635d7fSWilly Tarreau #error Neither __NR_mknodat nor __NR_mknod defined, cannot implement sys_mknod() 150430ca2051SWilly Tarreau #endif 150530ca2051SWilly Tarreau } 150630ca2051SWilly Tarreau 150730ca2051SWilly Tarreau static __attribute__((unused)) 150830ca2051SWilly Tarreau int sys_mount(const char *src, const char *tgt, const char *fst, 150930ca2051SWilly Tarreau unsigned long flags, const void *data) 151030ca2051SWilly Tarreau { 151130ca2051SWilly Tarreau return my_syscall5(__NR_mount, src, tgt, fst, flags, data); 151230ca2051SWilly Tarreau } 151330ca2051SWilly Tarreau 151430ca2051SWilly Tarreau static __attribute__((unused)) 151530ca2051SWilly Tarreau int sys_open(const char *path, int flags, mode_t mode) 151630ca2051SWilly Tarreau { 151730ca2051SWilly Tarreau #ifdef __NR_openat 151830ca2051SWilly Tarreau return my_syscall4(__NR_openat, AT_FDCWD, path, flags, mode); 151935635d7fSWilly Tarreau #elif defined(__NR_open) 152030ca2051SWilly Tarreau return my_syscall3(__NR_open, path, flags, mode); 152135635d7fSWilly Tarreau #else 152235635d7fSWilly Tarreau #error Neither __NR_openat nor __NR_open defined, cannot implement sys_open() 152330ca2051SWilly Tarreau #endif 152430ca2051SWilly Tarreau } 152530ca2051SWilly Tarreau 152630ca2051SWilly Tarreau static __attribute__((unused)) 152730ca2051SWilly Tarreau int sys_pivot_root(const char *new, const char *old) 152830ca2051SWilly Tarreau { 152930ca2051SWilly Tarreau return my_syscall2(__NR_pivot_root, new, old); 153030ca2051SWilly Tarreau } 153130ca2051SWilly Tarreau 153230ca2051SWilly Tarreau static __attribute__((unused)) 153330ca2051SWilly Tarreau int sys_poll(struct pollfd *fds, int nfds, int timeout) 153430ca2051SWilly Tarreau { 15355b1c827cSWilly Tarreau #if defined(__NR_ppoll) 15365b1c827cSWilly Tarreau struct timespec t; 15375b1c827cSWilly Tarreau 15385b1c827cSWilly Tarreau if (timeout >= 0) { 15395b1c827cSWilly Tarreau t.tv_sec = timeout / 1000; 15405b1c827cSWilly Tarreau t.tv_nsec = (timeout % 1000) * 1000000; 15415b1c827cSWilly Tarreau } 15425b1c827cSWilly Tarreau return my_syscall4(__NR_ppoll, fds, nfds, (timeout >= 0) ? &t : NULL, NULL); 154335635d7fSWilly Tarreau #elif defined(__NR_poll) 154430ca2051SWilly Tarreau return my_syscall3(__NR_poll, fds, nfds, timeout); 154535635d7fSWilly Tarreau #else 154635635d7fSWilly Tarreau #error Neither __NR_ppoll nor __NR_poll defined, cannot implement sys_poll() 15475b1c827cSWilly Tarreau #endif 154830ca2051SWilly Tarreau } 154930ca2051SWilly Tarreau 155030ca2051SWilly Tarreau static __attribute__((unused)) 155130ca2051SWilly Tarreau ssize_t sys_read(int fd, void *buf, size_t count) 155230ca2051SWilly Tarreau { 155330ca2051SWilly Tarreau return my_syscall3(__NR_read, fd, buf, count); 155430ca2051SWilly Tarreau } 155530ca2051SWilly Tarreau 155630ca2051SWilly Tarreau static __attribute__((unused)) 155730ca2051SWilly Tarreau ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg) 155830ca2051SWilly Tarreau { 155930ca2051SWilly Tarreau return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg); 156030ca2051SWilly Tarreau } 156130ca2051SWilly Tarreau 156230ca2051SWilly Tarreau static __attribute__((unused)) 156330ca2051SWilly Tarreau int sys_sched_yield(void) 156430ca2051SWilly Tarreau { 156530ca2051SWilly Tarreau return my_syscall0(__NR_sched_yield); 156630ca2051SWilly Tarreau } 156730ca2051SWilly Tarreau 156830ca2051SWilly Tarreau static __attribute__((unused)) 156930ca2051SWilly Tarreau int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) 157030ca2051SWilly Tarreau { 157130ca2051SWilly Tarreau #if defined(__ARCH_WANT_SYS_OLD_SELECT) && !defined(__NR__newselect) 157230ca2051SWilly Tarreau struct sel_arg_struct { 157330ca2051SWilly Tarreau unsigned long n; 157430ca2051SWilly Tarreau fd_set *r, *w, *e; 157530ca2051SWilly Tarreau struct timeval *t; 157630ca2051SWilly Tarreau } arg = { .n = nfds, .r = rfds, .w = wfds, .e = efds, .t = timeout }; 157730ca2051SWilly Tarreau return my_syscall1(__NR_select, &arg); 157830ca2051SWilly Tarreau #elif defined(__ARCH_WANT_SYS_PSELECT6) && defined(__NR_pselect6) 157930ca2051SWilly Tarreau struct timespec t; 158030ca2051SWilly Tarreau 158130ca2051SWilly Tarreau if (timeout) { 158230ca2051SWilly Tarreau t.tv_sec = timeout->tv_sec; 158330ca2051SWilly Tarreau t.tv_nsec = timeout->tv_usec * 1000; 158430ca2051SWilly Tarreau } 158530ca2051SWilly Tarreau return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL); 158635635d7fSWilly Tarreau #elif defined(__NR__newselect) || defined(__NR_select) 158730ca2051SWilly Tarreau #ifndef __NR__newselect 158830ca2051SWilly Tarreau #define __NR__newselect __NR_select 158930ca2051SWilly Tarreau #endif 159030ca2051SWilly Tarreau return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout); 159135635d7fSWilly Tarreau #else 159235635d7fSWilly Tarreau #error None of __NR_select, __NR_pselect6, nor __NR__newselect defined, cannot implement sys_select() 159330ca2051SWilly Tarreau #endif 159430ca2051SWilly Tarreau } 159530ca2051SWilly Tarreau 159630ca2051SWilly Tarreau static __attribute__((unused)) 159730ca2051SWilly Tarreau int sys_setpgid(pid_t pid, pid_t pgid) 159830ca2051SWilly Tarreau { 159930ca2051SWilly Tarreau return my_syscall2(__NR_setpgid, pid, pgid); 160030ca2051SWilly Tarreau } 160130ca2051SWilly Tarreau 160230ca2051SWilly Tarreau static __attribute__((unused)) 160330ca2051SWilly Tarreau pid_t sys_setsid(void) 160430ca2051SWilly Tarreau { 160530ca2051SWilly Tarreau return my_syscall0(__NR_setsid); 160630ca2051SWilly Tarreau } 160730ca2051SWilly Tarreau 160830ca2051SWilly Tarreau static __attribute__((unused)) 160930ca2051SWilly Tarreau int sys_stat(const char *path, struct stat *buf) 161030ca2051SWilly Tarreau { 161130ca2051SWilly Tarreau struct sys_stat_struct stat; 161230ca2051SWilly Tarreau long ret; 161330ca2051SWilly Tarreau 161430ca2051SWilly Tarreau #ifdef __NR_newfstatat 161530ca2051SWilly Tarreau /* only solution for arm64 */ 161630ca2051SWilly Tarreau ret = my_syscall4(__NR_newfstatat, AT_FDCWD, path, &stat, 0); 161735635d7fSWilly Tarreau #elif defined(__NR_stat) 161830ca2051SWilly Tarreau ret = my_syscall2(__NR_stat, path, &stat); 161935635d7fSWilly Tarreau #else 162035635d7fSWilly Tarreau #error Neither __NR_newfstatat nor __NR_stat defined, cannot implement sys_stat() 162130ca2051SWilly Tarreau #endif 162230ca2051SWilly Tarreau buf->st_dev = stat.st_dev; 162330ca2051SWilly Tarreau buf->st_ino = stat.st_ino; 162430ca2051SWilly Tarreau buf->st_mode = stat.st_mode; 162530ca2051SWilly Tarreau buf->st_nlink = stat.st_nlink; 162630ca2051SWilly Tarreau buf->st_uid = stat.st_uid; 162730ca2051SWilly Tarreau buf->st_gid = stat.st_gid; 162830ca2051SWilly Tarreau buf->st_rdev = stat.st_rdev; 162930ca2051SWilly Tarreau buf->st_size = stat.st_size; 163030ca2051SWilly Tarreau buf->st_blksize = stat.st_blksize; 163130ca2051SWilly Tarreau buf->st_blocks = stat.st_blocks; 163230ca2051SWilly Tarreau buf->st_atime = stat.st_atime; 163330ca2051SWilly Tarreau buf->st_mtime = stat.st_mtime; 163430ca2051SWilly Tarreau buf->st_ctime = stat.st_ctime; 163530ca2051SWilly Tarreau return ret; 163630ca2051SWilly Tarreau } 163730ca2051SWilly Tarreau 163830ca2051SWilly Tarreau 163930ca2051SWilly Tarreau static __attribute__((unused)) 164030ca2051SWilly Tarreau int sys_symlink(const char *old, const char *new) 164130ca2051SWilly Tarreau { 164230ca2051SWilly Tarreau #ifdef __NR_symlinkat 164330ca2051SWilly Tarreau return my_syscall3(__NR_symlinkat, old, AT_FDCWD, new); 164435635d7fSWilly Tarreau #elif defined(__NR_symlink) 164530ca2051SWilly Tarreau return my_syscall2(__NR_symlink, old, new); 164635635d7fSWilly Tarreau #else 164735635d7fSWilly Tarreau #error Neither __NR_symlinkat nor __NR_symlink defined, cannot implement sys_symlink() 164830ca2051SWilly Tarreau #endif 164930ca2051SWilly Tarreau } 165030ca2051SWilly Tarreau 165130ca2051SWilly Tarreau static __attribute__((unused)) 165230ca2051SWilly Tarreau mode_t sys_umask(mode_t mode) 165330ca2051SWilly Tarreau { 165430ca2051SWilly Tarreau return my_syscall1(__NR_umask, mode); 165530ca2051SWilly Tarreau } 165630ca2051SWilly Tarreau 165730ca2051SWilly Tarreau static __attribute__((unused)) 165830ca2051SWilly Tarreau int sys_umount2(const char *path, int flags) 165930ca2051SWilly Tarreau { 166030ca2051SWilly Tarreau return my_syscall2(__NR_umount2, path, flags); 166130ca2051SWilly Tarreau } 166230ca2051SWilly Tarreau 166330ca2051SWilly Tarreau static __attribute__((unused)) 166430ca2051SWilly Tarreau int sys_unlink(const char *path) 166530ca2051SWilly Tarreau { 166630ca2051SWilly Tarreau #ifdef __NR_unlinkat 166730ca2051SWilly Tarreau return my_syscall3(__NR_unlinkat, AT_FDCWD, path, 0); 166835635d7fSWilly Tarreau #elif defined(__NR_unlink) 166930ca2051SWilly Tarreau return my_syscall1(__NR_unlink, path); 167035635d7fSWilly Tarreau #else 167135635d7fSWilly Tarreau #error Neither __NR_unlinkat nor __NR_unlink defined, cannot implement sys_unlink() 167230ca2051SWilly Tarreau #endif 167330ca2051SWilly Tarreau } 167430ca2051SWilly Tarreau 167530ca2051SWilly Tarreau static __attribute__((unused)) 167630ca2051SWilly Tarreau pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage) 167730ca2051SWilly Tarreau { 167830ca2051SWilly Tarreau return my_syscall4(__NR_wait4, pid, status, options, rusage); 167930ca2051SWilly Tarreau } 168030ca2051SWilly Tarreau 168130ca2051SWilly Tarreau static __attribute__((unused)) 168230ca2051SWilly Tarreau pid_t sys_waitpid(pid_t pid, int *status, int options) 168330ca2051SWilly Tarreau { 168430ca2051SWilly Tarreau return sys_wait4(pid, status, options, 0); 168530ca2051SWilly Tarreau } 168630ca2051SWilly Tarreau 168730ca2051SWilly Tarreau static __attribute__((unused)) 168830ca2051SWilly Tarreau pid_t sys_wait(int *status) 168930ca2051SWilly Tarreau { 169030ca2051SWilly Tarreau return sys_waitpid(-1, status, 0); 169130ca2051SWilly Tarreau } 169230ca2051SWilly Tarreau 169330ca2051SWilly Tarreau static __attribute__((unused)) 169430ca2051SWilly Tarreau ssize_t sys_write(int fd, const void *buf, size_t count) 169530ca2051SWilly Tarreau { 169630ca2051SWilly Tarreau return my_syscall3(__NR_write, fd, buf, count); 169730ca2051SWilly Tarreau } 169830ca2051SWilly Tarreau 169930ca2051SWilly Tarreau 170030ca2051SWilly Tarreau /* Below are the libc-compatible syscalls which return x or -1 and set errno. 170130ca2051SWilly Tarreau * They rely on the functions above. Similarly they're marked static so that it 170230ca2051SWilly Tarreau * is possible to assign pointers to them if needed. 170330ca2051SWilly Tarreau */ 170430ca2051SWilly Tarreau 170530ca2051SWilly Tarreau static __attribute__((unused)) 170630ca2051SWilly Tarreau int brk(void *addr) 170730ca2051SWilly Tarreau { 170830ca2051SWilly Tarreau void *ret = sys_brk(addr); 170930ca2051SWilly Tarreau 171030ca2051SWilly Tarreau if (!ret) { 171130ca2051SWilly Tarreau SET_ERRNO(ENOMEM); 171230ca2051SWilly Tarreau return -1; 171330ca2051SWilly Tarreau } 171430ca2051SWilly Tarreau return 0; 171530ca2051SWilly Tarreau } 171630ca2051SWilly Tarreau 171730ca2051SWilly Tarreau static __attribute__((noreturn,unused)) 171830ca2051SWilly Tarreau void exit(int status) 171930ca2051SWilly Tarreau { 172030ca2051SWilly Tarreau sys_exit(status); 172130ca2051SWilly Tarreau } 172230ca2051SWilly Tarreau 172330ca2051SWilly Tarreau static __attribute__((unused)) 172430ca2051SWilly Tarreau int chdir(const char *path) 172530ca2051SWilly Tarreau { 172630ca2051SWilly Tarreau int ret = sys_chdir(path); 172730ca2051SWilly Tarreau 172830ca2051SWilly Tarreau if (ret < 0) { 172930ca2051SWilly Tarreau SET_ERRNO(-ret); 173030ca2051SWilly Tarreau ret = -1; 173130ca2051SWilly Tarreau } 173230ca2051SWilly Tarreau return ret; 173330ca2051SWilly Tarreau } 173430ca2051SWilly Tarreau 173530ca2051SWilly Tarreau static __attribute__((unused)) 173630ca2051SWilly Tarreau int chmod(const char *path, mode_t mode) 173730ca2051SWilly Tarreau { 173830ca2051SWilly Tarreau int ret = sys_chmod(path, mode); 173930ca2051SWilly Tarreau 174030ca2051SWilly Tarreau if (ret < 0) { 174130ca2051SWilly Tarreau SET_ERRNO(-ret); 174230ca2051SWilly Tarreau ret = -1; 174330ca2051SWilly Tarreau } 174430ca2051SWilly Tarreau return ret; 174530ca2051SWilly Tarreau } 174630ca2051SWilly Tarreau 174730ca2051SWilly Tarreau static __attribute__((unused)) 174830ca2051SWilly Tarreau int chown(const char *path, uid_t owner, gid_t group) 174930ca2051SWilly Tarreau { 175030ca2051SWilly Tarreau int ret = sys_chown(path, owner, group); 175130ca2051SWilly Tarreau 175230ca2051SWilly Tarreau if (ret < 0) { 175330ca2051SWilly Tarreau SET_ERRNO(-ret); 175430ca2051SWilly Tarreau ret = -1; 175530ca2051SWilly Tarreau } 175630ca2051SWilly Tarreau return ret; 175730ca2051SWilly Tarreau } 175830ca2051SWilly Tarreau 175930ca2051SWilly Tarreau static __attribute__((unused)) 176030ca2051SWilly Tarreau int chroot(const char *path) 176130ca2051SWilly Tarreau { 176230ca2051SWilly Tarreau int ret = sys_chroot(path); 176330ca2051SWilly Tarreau 176430ca2051SWilly Tarreau if (ret < 0) { 176530ca2051SWilly Tarreau SET_ERRNO(-ret); 176630ca2051SWilly Tarreau ret = -1; 176730ca2051SWilly Tarreau } 176830ca2051SWilly Tarreau return ret; 176930ca2051SWilly Tarreau } 177030ca2051SWilly Tarreau 177130ca2051SWilly Tarreau static __attribute__((unused)) 177230ca2051SWilly Tarreau int close(int fd) 177330ca2051SWilly Tarreau { 177430ca2051SWilly Tarreau int ret = sys_close(fd); 177530ca2051SWilly Tarreau 177630ca2051SWilly Tarreau if (ret < 0) { 177730ca2051SWilly Tarreau SET_ERRNO(-ret); 177830ca2051SWilly Tarreau ret = -1; 177930ca2051SWilly Tarreau } 178030ca2051SWilly Tarreau return ret; 178130ca2051SWilly Tarreau } 178230ca2051SWilly Tarreau 178330ca2051SWilly Tarreau static __attribute__((unused)) 1784c261145aSWilly Tarreau int dup(int fd) 1785c261145aSWilly Tarreau { 1786c261145aSWilly Tarreau int ret = sys_dup(fd); 1787c261145aSWilly Tarreau 1788c261145aSWilly Tarreau if (ret < 0) { 1789c261145aSWilly Tarreau SET_ERRNO(-ret); 1790c261145aSWilly Tarreau ret = -1; 1791c261145aSWilly Tarreau } 1792c261145aSWilly Tarreau return ret; 1793c261145aSWilly Tarreau } 1794c261145aSWilly Tarreau 1795c261145aSWilly Tarreau static __attribute__((unused)) 179630ca2051SWilly Tarreau int dup2(int old, int new) 179730ca2051SWilly Tarreau { 179830ca2051SWilly Tarreau int ret = sys_dup2(old, new); 179930ca2051SWilly Tarreau 180030ca2051SWilly Tarreau if (ret < 0) { 180130ca2051SWilly Tarreau SET_ERRNO(-ret); 180230ca2051SWilly Tarreau ret = -1; 180330ca2051SWilly Tarreau } 180430ca2051SWilly Tarreau return ret; 180530ca2051SWilly Tarreau } 180630ca2051SWilly Tarreau 180779f220e5SWilly Tarreau #ifdef __NR_dup3 180879f220e5SWilly Tarreau static __attribute__((unused)) 180979f220e5SWilly Tarreau int dup3(int old, int new, int flags) 181079f220e5SWilly Tarreau { 181179f220e5SWilly Tarreau int ret = sys_dup3(old, new, flags); 181279f220e5SWilly Tarreau 181379f220e5SWilly Tarreau if (ret < 0) { 181479f220e5SWilly Tarreau SET_ERRNO(-ret); 181579f220e5SWilly Tarreau ret = -1; 181679f220e5SWilly Tarreau } 181779f220e5SWilly Tarreau return ret; 181879f220e5SWilly Tarreau } 181979f220e5SWilly Tarreau #endif 182079f220e5SWilly Tarreau 182130ca2051SWilly Tarreau static __attribute__((unused)) 182230ca2051SWilly Tarreau int execve(const char *filename, char *const argv[], char *const envp[]) 182330ca2051SWilly Tarreau { 182430ca2051SWilly Tarreau int ret = sys_execve(filename, argv, envp); 182530ca2051SWilly Tarreau 182630ca2051SWilly Tarreau if (ret < 0) { 182730ca2051SWilly Tarreau SET_ERRNO(-ret); 182830ca2051SWilly Tarreau ret = -1; 182930ca2051SWilly Tarreau } 183030ca2051SWilly Tarreau return ret; 183130ca2051SWilly Tarreau } 183230ca2051SWilly Tarreau 183330ca2051SWilly Tarreau static __attribute__((unused)) 183430ca2051SWilly Tarreau pid_t fork(void) 183530ca2051SWilly Tarreau { 183630ca2051SWilly Tarreau pid_t ret = sys_fork(); 183730ca2051SWilly Tarreau 183830ca2051SWilly Tarreau if (ret < 0) { 183930ca2051SWilly Tarreau SET_ERRNO(-ret); 184030ca2051SWilly Tarreau ret = -1; 184130ca2051SWilly Tarreau } 184230ca2051SWilly Tarreau return ret; 184330ca2051SWilly Tarreau } 184430ca2051SWilly Tarreau 184530ca2051SWilly Tarreau static __attribute__((unused)) 184630ca2051SWilly Tarreau int fsync(int fd) 184730ca2051SWilly Tarreau { 184830ca2051SWilly Tarreau int ret = sys_fsync(fd); 184930ca2051SWilly Tarreau 185030ca2051SWilly Tarreau if (ret < 0) { 185130ca2051SWilly Tarreau SET_ERRNO(-ret); 185230ca2051SWilly Tarreau ret = -1; 185330ca2051SWilly Tarreau } 185430ca2051SWilly Tarreau return ret; 185530ca2051SWilly Tarreau } 185630ca2051SWilly Tarreau 185730ca2051SWilly Tarreau static __attribute__((unused)) 185830ca2051SWilly Tarreau int getdents64(int fd, struct linux_dirent64 *dirp, int count) 185930ca2051SWilly Tarreau { 186030ca2051SWilly Tarreau int ret = sys_getdents64(fd, dirp, count); 186130ca2051SWilly Tarreau 186230ca2051SWilly Tarreau if (ret < 0) { 186330ca2051SWilly Tarreau SET_ERRNO(-ret); 186430ca2051SWilly Tarreau ret = -1; 186530ca2051SWilly Tarreau } 186630ca2051SWilly Tarreau return ret; 186730ca2051SWilly Tarreau } 186830ca2051SWilly Tarreau 186930ca2051SWilly Tarreau static __attribute__((unused)) 1870c0c7c103SWilly Tarreau pid_t getpgid(pid_t pid) 1871c0c7c103SWilly Tarreau { 1872c0c7c103SWilly Tarreau pid_t ret = sys_getpgid(pid); 1873c0c7c103SWilly Tarreau 1874c0c7c103SWilly Tarreau if (ret < 0) { 1875c0c7c103SWilly Tarreau SET_ERRNO(-ret); 1876c0c7c103SWilly Tarreau ret = -1; 1877c0c7c103SWilly Tarreau } 1878c0c7c103SWilly Tarreau return ret; 1879c0c7c103SWilly Tarreau } 1880c0c7c103SWilly Tarreau 1881c0c7c103SWilly Tarreau static __attribute__((unused)) 188230ca2051SWilly Tarreau pid_t getpgrp(void) 188330ca2051SWilly Tarreau { 188430ca2051SWilly Tarreau pid_t ret = sys_getpgrp(); 188530ca2051SWilly Tarreau 188630ca2051SWilly Tarreau if (ret < 0) { 188730ca2051SWilly Tarreau SET_ERRNO(-ret); 188830ca2051SWilly Tarreau ret = -1; 188930ca2051SWilly Tarreau } 189030ca2051SWilly Tarreau return ret; 189130ca2051SWilly Tarreau } 189230ca2051SWilly Tarreau 189330ca2051SWilly Tarreau static __attribute__((unused)) 189430ca2051SWilly Tarreau pid_t getpid(void) 189530ca2051SWilly Tarreau { 189630ca2051SWilly Tarreau pid_t ret = sys_getpid(); 189730ca2051SWilly Tarreau 189830ca2051SWilly Tarreau if (ret < 0) { 189930ca2051SWilly Tarreau SET_ERRNO(-ret); 190030ca2051SWilly Tarreau ret = -1; 190130ca2051SWilly Tarreau } 190230ca2051SWilly Tarreau return ret; 190330ca2051SWilly Tarreau } 190430ca2051SWilly Tarreau 190530ca2051SWilly Tarreau static __attribute__((unused)) 1906b0fe9decSMark Brown pid_t gettid(void) 1907b0fe9decSMark Brown { 1908b0fe9decSMark Brown pid_t ret = sys_gettid(); 1909b0fe9decSMark Brown 1910b0fe9decSMark Brown if (ret < 0) { 1911b0fe9decSMark Brown SET_ERRNO(-ret); 1912b0fe9decSMark Brown ret = -1; 1913b0fe9decSMark Brown } 1914b0fe9decSMark Brown return ret; 1915b0fe9decSMark Brown } 1916b0fe9decSMark Brown 1917b0fe9decSMark Brown static __attribute__((unused)) 191830ca2051SWilly Tarreau int gettimeofday(struct timeval *tv, struct timezone *tz) 191930ca2051SWilly Tarreau { 192030ca2051SWilly Tarreau int ret = sys_gettimeofday(tv, tz); 192130ca2051SWilly Tarreau 192230ca2051SWilly Tarreau if (ret < 0) { 192330ca2051SWilly Tarreau SET_ERRNO(-ret); 192430ca2051SWilly Tarreau ret = -1; 192530ca2051SWilly Tarreau } 192630ca2051SWilly Tarreau return ret; 192730ca2051SWilly Tarreau } 192830ca2051SWilly Tarreau 192930ca2051SWilly Tarreau static __attribute__((unused)) 193030ca2051SWilly Tarreau int ioctl(int fd, unsigned long req, void *value) 193130ca2051SWilly Tarreau { 193230ca2051SWilly Tarreau int ret = sys_ioctl(fd, req, value); 193330ca2051SWilly Tarreau 193430ca2051SWilly Tarreau if (ret < 0) { 193530ca2051SWilly Tarreau SET_ERRNO(-ret); 193630ca2051SWilly Tarreau ret = -1; 193730ca2051SWilly Tarreau } 193830ca2051SWilly Tarreau return ret; 193930ca2051SWilly Tarreau } 194030ca2051SWilly Tarreau 194130ca2051SWilly Tarreau static __attribute__((unused)) 194230ca2051SWilly Tarreau int kill(pid_t pid, int signal) 194330ca2051SWilly Tarreau { 194430ca2051SWilly Tarreau int ret = sys_kill(pid, signal); 194530ca2051SWilly Tarreau 194630ca2051SWilly Tarreau if (ret < 0) { 194730ca2051SWilly Tarreau SET_ERRNO(-ret); 194830ca2051SWilly Tarreau ret = -1; 194930ca2051SWilly Tarreau } 195030ca2051SWilly Tarreau return ret; 195130ca2051SWilly Tarreau } 195230ca2051SWilly Tarreau 195330ca2051SWilly Tarreau static __attribute__((unused)) 195430ca2051SWilly Tarreau int link(const char *old, const char *new) 195530ca2051SWilly Tarreau { 195630ca2051SWilly Tarreau int ret = sys_link(old, new); 195730ca2051SWilly Tarreau 195830ca2051SWilly Tarreau if (ret < 0) { 195930ca2051SWilly Tarreau SET_ERRNO(-ret); 196030ca2051SWilly Tarreau ret = -1; 196130ca2051SWilly Tarreau } 196230ca2051SWilly Tarreau return ret; 196330ca2051SWilly Tarreau } 196430ca2051SWilly Tarreau 196530ca2051SWilly Tarreau static __attribute__((unused)) 196630ca2051SWilly Tarreau off_t lseek(int fd, off_t offset, int whence) 196730ca2051SWilly Tarreau { 196830ca2051SWilly Tarreau off_t ret = sys_lseek(fd, offset, whence); 196930ca2051SWilly Tarreau 197030ca2051SWilly Tarreau if (ret < 0) { 197130ca2051SWilly Tarreau SET_ERRNO(-ret); 197230ca2051SWilly Tarreau ret = -1; 197330ca2051SWilly Tarreau } 197430ca2051SWilly Tarreau return ret; 197530ca2051SWilly Tarreau } 197630ca2051SWilly Tarreau 197730ca2051SWilly Tarreau static __attribute__((unused)) 197830ca2051SWilly Tarreau int mkdir(const char *path, mode_t mode) 197930ca2051SWilly Tarreau { 198030ca2051SWilly Tarreau int ret = sys_mkdir(path, mode); 198130ca2051SWilly Tarreau 198230ca2051SWilly Tarreau if (ret < 0) { 198330ca2051SWilly Tarreau SET_ERRNO(-ret); 198430ca2051SWilly Tarreau ret = -1; 198530ca2051SWilly Tarreau } 198630ca2051SWilly Tarreau return ret; 198730ca2051SWilly Tarreau } 198830ca2051SWilly Tarreau 198930ca2051SWilly Tarreau static __attribute__((unused)) 199030ca2051SWilly Tarreau int mknod(const char *path, mode_t mode, dev_t dev) 199130ca2051SWilly Tarreau { 199230ca2051SWilly Tarreau int ret = sys_mknod(path, mode, dev); 199330ca2051SWilly Tarreau 199430ca2051SWilly Tarreau if (ret < 0) { 199530ca2051SWilly Tarreau SET_ERRNO(-ret); 199630ca2051SWilly Tarreau ret = -1; 199730ca2051SWilly Tarreau } 199830ca2051SWilly Tarreau return ret; 199930ca2051SWilly Tarreau } 200030ca2051SWilly Tarreau 200130ca2051SWilly Tarreau static __attribute__((unused)) 200230ca2051SWilly Tarreau int mount(const char *src, const char *tgt, 200330ca2051SWilly Tarreau const char *fst, unsigned long flags, 200430ca2051SWilly Tarreau const void *data) 200530ca2051SWilly Tarreau { 200630ca2051SWilly Tarreau int ret = sys_mount(src, tgt, fst, flags, data); 200730ca2051SWilly Tarreau 200830ca2051SWilly Tarreau if (ret < 0) { 200930ca2051SWilly Tarreau SET_ERRNO(-ret); 201030ca2051SWilly Tarreau ret = -1; 201130ca2051SWilly Tarreau } 201230ca2051SWilly Tarreau return ret; 201330ca2051SWilly Tarreau } 201430ca2051SWilly Tarreau 201530ca2051SWilly Tarreau static __attribute__((unused)) 201630ca2051SWilly Tarreau int open(const char *path, int flags, mode_t mode) 201730ca2051SWilly Tarreau { 201830ca2051SWilly Tarreau int ret = sys_open(path, flags, mode); 201930ca2051SWilly Tarreau 202030ca2051SWilly Tarreau if (ret < 0) { 202130ca2051SWilly Tarreau SET_ERRNO(-ret); 202230ca2051SWilly Tarreau ret = -1; 202330ca2051SWilly Tarreau } 202430ca2051SWilly Tarreau return ret; 202530ca2051SWilly Tarreau } 202630ca2051SWilly Tarreau 202730ca2051SWilly Tarreau static __attribute__((unused)) 202830ca2051SWilly Tarreau int pivot_root(const char *new, const char *old) 202930ca2051SWilly Tarreau { 203030ca2051SWilly Tarreau int ret = sys_pivot_root(new, old); 203130ca2051SWilly Tarreau 203230ca2051SWilly Tarreau if (ret < 0) { 203330ca2051SWilly Tarreau SET_ERRNO(-ret); 203430ca2051SWilly Tarreau ret = -1; 203530ca2051SWilly Tarreau } 203630ca2051SWilly Tarreau return ret; 203730ca2051SWilly Tarreau } 203830ca2051SWilly Tarreau 203930ca2051SWilly Tarreau static __attribute__((unused)) 204030ca2051SWilly Tarreau int poll(struct pollfd *fds, int nfds, int timeout) 204130ca2051SWilly Tarreau { 204230ca2051SWilly Tarreau int ret = sys_poll(fds, nfds, timeout); 204330ca2051SWilly Tarreau 204430ca2051SWilly Tarreau if (ret < 0) { 204530ca2051SWilly Tarreau SET_ERRNO(-ret); 204630ca2051SWilly Tarreau ret = -1; 204730ca2051SWilly Tarreau } 204830ca2051SWilly Tarreau return ret; 204930ca2051SWilly Tarreau } 205030ca2051SWilly Tarreau 205130ca2051SWilly Tarreau static __attribute__((unused)) 205230ca2051SWilly Tarreau ssize_t read(int fd, void *buf, size_t count) 205330ca2051SWilly Tarreau { 205430ca2051SWilly Tarreau ssize_t ret = sys_read(fd, buf, count); 205530ca2051SWilly Tarreau 205630ca2051SWilly Tarreau if (ret < 0) { 205730ca2051SWilly Tarreau SET_ERRNO(-ret); 205830ca2051SWilly Tarreau ret = -1; 205930ca2051SWilly Tarreau } 206030ca2051SWilly Tarreau return ret; 206130ca2051SWilly Tarreau } 206230ca2051SWilly Tarreau 206330ca2051SWilly Tarreau static __attribute__((unused)) 206430ca2051SWilly Tarreau int reboot(int cmd) 206530ca2051SWilly Tarreau { 206630ca2051SWilly Tarreau int ret = sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0); 206730ca2051SWilly Tarreau 206830ca2051SWilly Tarreau if (ret < 0) { 206930ca2051SWilly Tarreau SET_ERRNO(-ret); 207030ca2051SWilly Tarreau ret = -1; 207130ca2051SWilly Tarreau } 207230ca2051SWilly Tarreau return ret; 207330ca2051SWilly Tarreau } 207430ca2051SWilly Tarreau 207530ca2051SWilly Tarreau static __attribute__((unused)) 207630ca2051SWilly Tarreau void *sbrk(intptr_t inc) 207730ca2051SWilly Tarreau { 207830ca2051SWilly Tarreau void *ret; 207930ca2051SWilly Tarreau 208030ca2051SWilly Tarreau /* first call to find current end */ 208130ca2051SWilly Tarreau if ((ret = sys_brk(0)) && (sys_brk(ret + inc) == ret + inc)) 208230ca2051SWilly Tarreau return ret + inc; 208330ca2051SWilly Tarreau 208430ca2051SWilly Tarreau SET_ERRNO(ENOMEM); 208530ca2051SWilly Tarreau return (void *)-1; 208630ca2051SWilly Tarreau } 208730ca2051SWilly Tarreau 208830ca2051SWilly Tarreau static __attribute__((unused)) 208930ca2051SWilly Tarreau int sched_yield(void) 209030ca2051SWilly Tarreau { 209130ca2051SWilly Tarreau int ret = sys_sched_yield(); 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 select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) 210230ca2051SWilly Tarreau { 210330ca2051SWilly Tarreau int ret = sys_select(nfds, rfds, wfds, efds, timeout); 210430ca2051SWilly Tarreau 210530ca2051SWilly Tarreau if (ret < 0) { 210630ca2051SWilly Tarreau SET_ERRNO(-ret); 210730ca2051SWilly Tarreau ret = -1; 210830ca2051SWilly Tarreau } 210930ca2051SWilly Tarreau return ret; 211030ca2051SWilly Tarreau } 211130ca2051SWilly Tarreau 211230ca2051SWilly Tarreau static __attribute__((unused)) 211330ca2051SWilly Tarreau int setpgid(pid_t pid, pid_t pgid) 211430ca2051SWilly Tarreau { 211530ca2051SWilly Tarreau int ret = sys_setpgid(pid, pgid); 211630ca2051SWilly Tarreau 211730ca2051SWilly Tarreau if (ret < 0) { 211830ca2051SWilly Tarreau SET_ERRNO(-ret); 211930ca2051SWilly Tarreau ret = -1; 212030ca2051SWilly Tarreau } 212130ca2051SWilly Tarreau return ret; 212230ca2051SWilly Tarreau } 212330ca2051SWilly Tarreau 212430ca2051SWilly Tarreau static __attribute__((unused)) 212530ca2051SWilly Tarreau pid_t setsid(void) 212630ca2051SWilly Tarreau { 212730ca2051SWilly Tarreau pid_t ret = sys_setsid(); 212830ca2051SWilly Tarreau 212930ca2051SWilly Tarreau if (ret < 0) { 213030ca2051SWilly Tarreau SET_ERRNO(-ret); 213130ca2051SWilly Tarreau ret = -1; 213230ca2051SWilly Tarreau } 213330ca2051SWilly Tarreau return ret; 213430ca2051SWilly Tarreau } 213530ca2051SWilly Tarreau 213630ca2051SWilly Tarreau static __attribute__((unused)) 213730ca2051SWilly Tarreau unsigned int sleep(unsigned int seconds) 213830ca2051SWilly Tarreau { 213930ca2051SWilly Tarreau struct timeval my_timeval = { seconds, 0 }; 214030ca2051SWilly Tarreau 214130ca2051SWilly Tarreau if (sys_select(0, 0, 0, 0, &my_timeval) < 0) 214230ca2051SWilly Tarreau return my_timeval.tv_sec + !!my_timeval.tv_usec; 214330ca2051SWilly Tarreau else 214430ca2051SWilly Tarreau return 0; 214530ca2051SWilly Tarreau } 214630ca2051SWilly Tarreau 214730ca2051SWilly Tarreau static __attribute__((unused)) 2148f916d77eSMark Brown int msleep(unsigned int msecs) 2149f916d77eSMark Brown { 2150f916d77eSMark Brown struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 }; 2151f916d77eSMark Brown 2152f916d77eSMark Brown if (sys_select(0, 0, 0, 0, &my_timeval) < 0) 2153f916d77eSMark Brown return (my_timeval.tv_sec * 1000) + 2154f916d77eSMark Brown (my_timeval.tv_usec / 1000) + 2155f916d77eSMark Brown !!(my_timeval.tv_usec % 1000); 2156f916d77eSMark Brown else 2157f916d77eSMark Brown return 0; 2158f916d77eSMark Brown } 2159f916d77eSMark Brown 2160f916d77eSMark Brown static __attribute__((unused)) 216130ca2051SWilly Tarreau int stat(const char *path, struct stat *buf) 216230ca2051SWilly Tarreau { 216330ca2051SWilly Tarreau int ret = sys_stat(path, buf); 216430ca2051SWilly Tarreau 216530ca2051SWilly Tarreau if (ret < 0) { 216630ca2051SWilly Tarreau SET_ERRNO(-ret); 216730ca2051SWilly Tarreau ret = -1; 216830ca2051SWilly Tarreau } 216930ca2051SWilly Tarreau return ret; 217030ca2051SWilly Tarreau } 217130ca2051SWilly Tarreau 217230ca2051SWilly Tarreau static __attribute__((unused)) 217330ca2051SWilly Tarreau int symlink(const char *old, const char *new) 217430ca2051SWilly Tarreau { 217530ca2051SWilly Tarreau int ret = sys_symlink(old, new); 217630ca2051SWilly Tarreau 217730ca2051SWilly Tarreau if (ret < 0) { 217830ca2051SWilly Tarreau SET_ERRNO(-ret); 217930ca2051SWilly Tarreau ret = -1; 218030ca2051SWilly Tarreau } 218130ca2051SWilly Tarreau return ret; 218230ca2051SWilly Tarreau } 218330ca2051SWilly Tarreau 218430ca2051SWilly Tarreau static __attribute__((unused)) 218530ca2051SWilly Tarreau int tcsetpgrp(int fd, pid_t pid) 218630ca2051SWilly Tarreau { 218730ca2051SWilly Tarreau return ioctl(fd, TIOCSPGRP, &pid); 218830ca2051SWilly Tarreau } 218930ca2051SWilly Tarreau 219030ca2051SWilly Tarreau static __attribute__((unused)) 219130ca2051SWilly Tarreau mode_t umask(mode_t mode) 219230ca2051SWilly Tarreau { 219330ca2051SWilly Tarreau return sys_umask(mode); 219430ca2051SWilly Tarreau } 219530ca2051SWilly Tarreau 219630ca2051SWilly Tarreau static __attribute__((unused)) 219730ca2051SWilly Tarreau int umount2(const char *path, int flags) 219830ca2051SWilly Tarreau { 219930ca2051SWilly Tarreau int ret = sys_umount2(path, flags); 220030ca2051SWilly Tarreau 220130ca2051SWilly Tarreau if (ret < 0) { 220230ca2051SWilly Tarreau SET_ERRNO(-ret); 220330ca2051SWilly Tarreau ret = -1; 220430ca2051SWilly Tarreau } 220530ca2051SWilly Tarreau return ret; 220630ca2051SWilly Tarreau } 220730ca2051SWilly Tarreau 220830ca2051SWilly Tarreau static __attribute__((unused)) 220930ca2051SWilly Tarreau int unlink(const char *path) 221030ca2051SWilly Tarreau { 221130ca2051SWilly Tarreau int ret = sys_unlink(path); 221230ca2051SWilly Tarreau 221330ca2051SWilly Tarreau if (ret < 0) { 221430ca2051SWilly Tarreau SET_ERRNO(-ret); 221530ca2051SWilly Tarreau ret = -1; 221630ca2051SWilly Tarreau } 221730ca2051SWilly Tarreau return ret; 221830ca2051SWilly Tarreau } 221930ca2051SWilly Tarreau 222030ca2051SWilly Tarreau static __attribute__((unused)) 222130ca2051SWilly Tarreau pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage) 222230ca2051SWilly Tarreau { 222330ca2051SWilly Tarreau pid_t ret = sys_wait4(pid, status, options, rusage); 222430ca2051SWilly Tarreau 222530ca2051SWilly Tarreau if (ret < 0) { 222630ca2051SWilly Tarreau SET_ERRNO(-ret); 222730ca2051SWilly Tarreau ret = -1; 222830ca2051SWilly Tarreau } 222930ca2051SWilly Tarreau return ret; 223030ca2051SWilly Tarreau } 223130ca2051SWilly Tarreau 223230ca2051SWilly Tarreau static __attribute__((unused)) 223330ca2051SWilly Tarreau pid_t waitpid(pid_t pid, int *status, int options) 223430ca2051SWilly Tarreau { 223530ca2051SWilly Tarreau pid_t ret = sys_waitpid(pid, status, options); 223630ca2051SWilly Tarreau 223730ca2051SWilly Tarreau if (ret < 0) { 223830ca2051SWilly Tarreau SET_ERRNO(-ret); 223930ca2051SWilly Tarreau ret = -1; 224030ca2051SWilly Tarreau } 224130ca2051SWilly Tarreau return ret; 224230ca2051SWilly Tarreau } 224330ca2051SWilly Tarreau 224430ca2051SWilly Tarreau static __attribute__((unused)) 224530ca2051SWilly Tarreau pid_t wait(int *status) 224630ca2051SWilly Tarreau { 224730ca2051SWilly Tarreau pid_t ret = sys_wait(status); 224830ca2051SWilly Tarreau 224930ca2051SWilly Tarreau if (ret < 0) { 225030ca2051SWilly Tarreau SET_ERRNO(-ret); 225130ca2051SWilly Tarreau ret = -1; 225230ca2051SWilly Tarreau } 225330ca2051SWilly Tarreau return ret; 225430ca2051SWilly Tarreau } 225530ca2051SWilly Tarreau 225630ca2051SWilly Tarreau static __attribute__((unused)) 225730ca2051SWilly Tarreau ssize_t write(int fd, const void *buf, size_t count) 225830ca2051SWilly Tarreau { 225930ca2051SWilly Tarreau ssize_t ret = sys_write(fd, buf, count); 226030ca2051SWilly Tarreau 226130ca2051SWilly Tarreau if (ret < 0) { 226230ca2051SWilly Tarreau SET_ERRNO(-ret); 226330ca2051SWilly Tarreau ret = -1; 226430ca2051SWilly Tarreau } 226530ca2051SWilly Tarreau return ret; 226630ca2051SWilly Tarreau } 226730ca2051SWilly Tarreau 226830ca2051SWilly Tarreau /* some size-optimized reimplementations of a few common str* and mem* 226930ca2051SWilly Tarreau * functions. They're marked static, except memcpy() and raise() which are used 227030ca2051SWilly Tarreau * by libgcc on ARM, so they are marked weak instead in order not to cause an 227130ca2051SWilly Tarreau * error when building a program made of multiple files (not recommended). 227230ca2051SWilly Tarreau */ 227330ca2051SWilly Tarreau 227430ca2051SWilly Tarreau static __attribute__((unused)) 227530ca2051SWilly Tarreau void *memmove(void *dst, const void *src, size_t len) 227630ca2051SWilly Tarreau { 227730ca2051SWilly Tarreau ssize_t pos = (dst <= src) ? -1 : (long)len; 227830ca2051SWilly Tarreau void *ret = dst; 227930ca2051SWilly Tarreau 228030ca2051SWilly Tarreau while (len--) { 228130ca2051SWilly Tarreau pos += (dst <= src) ? 1 : -1; 228230ca2051SWilly Tarreau ((char *)dst)[pos] = ((char *)src)[pos]; 228330ca2051SWilly Tarreau } 228430ca2051SWilly Tarreau return ret; 228530ca2051SWilly Tarreau } 228630ca2051SWilly Tarreau 228730ca2051SWilly Tarreau static __attribute__((unused)) 228830ca2051SWilly Tarreau void *memset(void *dst, int b, size_t len) 228930ca2051SWilly Tarreau { 229030ca2051SWilly Tarreau char *p = dst; 229130ca2051SWilly Tarreau 229230ca2051SWilly Tarreau while (len--) 229330ca2051SWilly Tarreau *(p++) = b; 229430ca2051SWilly Tarreau return dst; 229530ca2051SWilly Tarreau } 229630ca2051SWilly Tarreau 229730ca2051SWilly Tarreau static __attribute__((unused)) 229830ca2051SWilly Tarreau int memcmp(const void *s1, const void *s2, size_t n) 229930ca2051SWilly Tarreau { 230030ca2051SWilly Tarreau size_t ofs = 0; 230130ca2051SWilly Tarreau char c1 = 0; 230230ca2051SWilly Tarreau 230330ca2051SWilly Tarreau while (ofs < n && !(c1 = ((char *)s1)[ofs] - ((char *)s2)[ofs])) { 230430ca2051SWilly Tarreau ofs++; 230530ca2051SWilly Tarreau } 230630ca2051SWilly Tarreau return c1; 230730ca2051SWilly Tarreau } 230830ca2051SWilly Tarreau 230930ca2051SWilly Tarreau static __attribute__((unused)) 231030ca2051SWilly Tarreau char *strcpy(char *dst, const char *src) 231130ca2051SWilly Tarreau { 231230ca2051SWilly Tarreau char *ret = dst; 231330ca2051SWilly Tarreau 231430ca2051SWilly Tarreau while ((*dst++ = *src++)); 231530ca2051SWilly Tarreau return ret; 231630ca2051SWilly Tarreau } 231730ca2051SWilly Tarreau 231830ca2051SWilly Tarreau static __attribute__((unused)) 231930ca2051SWilly Tarreau char *strchr(const char *s, int c) 232030ca2051SWilly Tarreau { 232130ca2051SWilly Tarreau while (*s) { 232230ca2051SWilly Tarreau if (*s == (char)c) 232330ca2051SWilly Tarreau return (char *)s; 232430ca2051SWilly Tarreau s++; 232530ca2051SWilly Tarreau } 232630ca2051SWilly Tarreau return NULL; 232730ca2051SWilly Tarreau } 232830ca2051SWilly Tarreau 232930ca2051SWilly Tarreau static __attribute__((unused)) 233030ca2051SWilly Tarreau char *strrchr(const char *s, int c) 233130ca2051SWilly Tarreau { 233230ca2051SWilly Tarreau const char *ret = NULL; 233330ca2051SWilly Tarreau 233430ca2051SWilly Tarreau while (*s) { 233530ca2051SWilly Tarreau if (*s == (char)c) 233630ca2051SWilly Tarreau ret = s; 233730ca2051SWilly Tarreau s++; 233830ca2051SWilly Tarreau } 233930ca2051SWilly Tarreau return (char *)ret; 234030ca2051SWilly Tarreau } 234130ca2051SWilly Tarreau 234230ca2051SWilly Tarreau static __attribute__((unused)) 234330ca2051SWilly Tarreau size_t nolibc_strlen(const char *str) 234430ca2051SWilly Tarreau { 234530ca2051SWilly Tarreau size_t len; 234630ca2051SWilly Tarreau 234730ca2051SWilly Tarreau for (len = 0; str[len]; len++); 234830ca2051SWilly Tarreau return len; 234930ca2051SWilly Tarreau } 235030ca2051SWilly Tarreau 235130ca2051SWilly Tarreau #define strlen(str) ({ \ 235230ca2051SWilly Tarreau __builtin_constant_p((str)) ? \ 235330ca2051SWilly Tarreau __builtin_strlen((str)) : \ 235430ca2051SWilly Tarreau nolibc_strlen((str)); \ 235530ca2051SWilly Tarreau }) 235630ca2051SWilly Tarreau 235730ca2051SWilly Tarreau static __attribute__((unused)) 235830ca2051SWilly Tarreau int isdigit(int c) 235930ca2051SWilly Tarreau { 236030ca2051SWilly Tarreau return (unsigned int)(c - '0') <= 9; 236130ca2051SWilly Tarreau } 236230ca2051SWilly Tarreau 236330ca2051SWilly Tarreau static __attribute__((unused)) 236430ca2051SWilly Tarreau long atol(const char *s) 236530ca2051SWilly Tarreau { 236630ca2051SWilly Tarreau unsigned long ret = 0; 236730ca2051SWilly Tarreau unsigned long d; 236830ca2051SWilly Tarreau int neg = 0; 236930ca2051SWilly Tarreau 237030ca2051SWilly Tarreau if (*s == '-') { 237130ca2051SWilly Tarreau neg = 1; 237230ca2051SWilly Tarreau s++; 237330ca2051SWilly Tarreau } 237430ca2051SWilly Tarreau 237530ca2051SWilly Tarreau while (1) { 237630ca2051SWilly Tarreau d = (*s++) - '0'; 237730ca2051SWilly Tarreau if (d > 9) 237830ca2051SWilly Tarreau break; 237930ca2051SWilly Tarreau ret *= 10; 238030ca2051SWilly Tarreau ret += d; 238130ca2051SWilly Tarreau } 238230ca2051SWilly Tarreau 238330ca2051SWilly Tarreau return neg ? -ret : ret; 238430ca2051SWilly Tarreau } 238530ca2051SWilly Tarreau 238630ca2051SWilly Tarreau static __attribute__((unused)) 238730ca2051SWilly Tarreau int atoi(const char *s) 238830ca2051SWilly Tarreau { 238930ca2051SWilly Tarreau return atol(s); 239030ca2051SWilly Tarreau } 239130ca2051SWilly Tarreau 239230ca2051SWilly Tarreau static __attribute__((unused)) 239330ca2051SWilly Tarreau const char *ltoa(long in) 239430ca2051SWilly Tarreau { 239530ca2051SWilly Tarreau /* large enough for -9223372036854775808 */ 239630ca2051SWilly Tarreau static char buffer[21]; 239730ca2051SWilly Tarreau char *pos = buffer + sizeof(buffer) - 1; 239830ca2051SWilly Tarreau int neg = in < 0; 239930ca2051SWilly Tarreau unsigned long n = neg ? -in : in; 240030ca2051SWilly Tarreau 240130ca2051SWilly Tarreau *pos-- = '\0'; 240230ca2051SWilly Tarreau do { 240330ca2051SWilly Tarreau *pos-- = '0' + n % 10; 240430ca2051SWilly Tarreau n /= 10; 240530ca2051SWilly Tarreau if (pos < buffer) 240630ca2051SWilly Tarreau return pos + 1; 240730ca2051SWilly Tarreau } while (n); 240830ca2051SWilly Tarreau 240930ca2051SWilly Tarreau if (neg) 241030ca2051SWilly Tarreau *pos-- = '-'; 241130ca2051SWilly Tarreau return pos + 1; 241230ca2051SWilly Tarreau } 241330ca2051SWilly Tarreau 241430ca2051SWilly Tarreau __attribute__((weak,unused)) 241530ca2051SWilly Tarreau void *memcpy(void *dst, const void *src, size_t len) 241630ca2051SWilly Tarreau { 241730ca2051SWilly Tarreau return memmove(dst, src, len); 241830ca2051SWilly Tarreau } 241930ca2051SWilly Tarreau 242030ca2051SWilly Tarreau /* needed by libgcc for divide by zero */ 242130ca2051SWilly Tarreau __attribute__((weak,unused)) 242230ca2051SWilly Tarreau int raise(int signal) 242330ca2051SWilly Tarreau { 242430ca2051SWilly Tarreau return kill(getpid(), signal); 242530ca2051SWilly Tarreau } 242630ca2051SWilly Tarreau 242730ca2051SWilly Tarreau /* Here come a few helper functions */ 242830ca2051SWilly Tarreau 242930ca2051SWilly Tarreau static __attribute__((unused)) 243030ca2051SWilly Tarreau void FD_ZERO(fd_set *set) 243130ca2051SWilly Tarreau { 243230ca2051SWilly Tarreau memset(set, 0, sizeof(*set)); 243330ca2051SWilly Tarreau } 243430ca2051SWilly Tarreau 243530ca2051SWilly Tarreau static __attribute__((unused)) 243630ca2051SWilly Tarreau void FD_SET(int fd, fd_set *set) 243730ca2051SWilly Tarreau { 243830ca2051SWilly Tarreau if (fd < 0 || fd >= FD_SETSIZE) 243930ca2051SWilly Tarreau return; 244030ca2051SWilly Tarreau set->fd32[fd / 32] |= 1 << (fd & 31); 244130ca2051SWilly Tarreau } 244230ca2051SWilly Tarreau 244330ca2051SWilly Tarreau /* WARNING, it only deals with the 4096 first majors and 256 first minors */ 244430ca2051SWilly Tarreau static __attribute__((unused)) 244530ca2051SWilly Tarreau dev_t makedev(unsigned int major, unsigned int minor) 244630ca2051SWilly Tarreau { 244730ca2051SWilly Tarreau return ((major & 0xfff) << 8) | (minor & 0xff); 244830ca2051SWilly Tarreau } 2449930c4accSWilly Tarreau 2450930c4accSWilly Tarreau #endif /* _NOLIBC_H */ 2451