1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */ 2 /* 3 * i386 specific definitions for NOLIBC 4 * Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu> 5 */ 6 7 #ifndef _NOLIBC_ARCH_I386_H 8 #define _NOLIBC_ARCH_I386_H 9 10 /* O_* macros for fcntl/open are architecture-specific */ 11 #define O_RDONLY 0 12 #define O_WRONLY 1 13 #define O_RDWR 2 14 #define O_CREAT 0x40 15 #define O_EXCL 0x80 16 #define O_NOCTTY 0x100 17 #define O_TRUNC 0x200 18 #define O_APPEND 0x400 19 #define O_NONBLOCK 0x800 20 #define O_DIRECTORY 0x10000 21 22 /* The struct returned by the stat() syscall, 32-bit only, the syscall returns 23 * exactly 56 bytes (stops before the unused array). 24 */ 25 struct sys_stat_struct { 26 unsigned long st_dev; 27 unsigned long st_ino; 28 unsigned short st_mode; 29 unsigned short st_nlink; 30 unsigned short st_uid; 31 unsigned short st_gid; 32 33 unsigned long st_rdev; 34 unsigned long st_size; 35 unsigned long st_blksize; 36 unsigned long st_blocks; 37 38 unsigned long st_atime; 39 unsigned long st_atime_nsec; 40 unsigned long st_mtime; 41 unsigned long st_mtime_nsec; 42 43 unsigned long st_ctime; 44 unsigned long st_ctime_nsec; 45 unsigned long __unused[2]; 46 }; 47 48 /* Syscalls for i386 : 49 * - mostly similar to x86_64 50 * - registers are 32-bit 51 * - syscall number is passed in eax 52 * - arguments are in ebx, ecx, edx, esi, edi, ebp respectively 53 * - all registers are preserved (except eax of course) 54 * - the system call is performed by calling int $0x80 55 * - syscall return comes in eax 56 * - the arguments are cast to long and assigned into the target registers 57 * which are then simply passed as registers to the asm code, so that we 58 * don't have to experience issues with register constraints. 59 * - the syscall number is always specified last in order to allow to force 60 * some registers before (gcc refuses a %-register at the last position). 61 * 62 * Also, i386 supports the old_select syscall if newselect is not available 63 */ 64 #define __ARCH_WANT_SYS_OLD_SELECT 65 66 #define my_syscall0(num) \ 67 ({ \ 68 long _ret; \ 69 register long _num asm("eax") = (num); \ 70 \ 71 asm volatile ( \ 72 "int $0x80\n" \ 73 : "=a" (_ret) \ 74 : "0"(_num) \ 75 : "memory", "cc" \ 76 ); \ 77 _ret; \ 78 }) 79 80 #define my_syscall1(num, arg1) \ 81 ({ \ 82 long _ret; \ 83 register long _num asm("eax") = (num); \ 84 register long _arg1 asm("ebx") = (long)(arg1); \ 85 \ 86 asm volatile ( \ 87 "int $0x80\n" \ 88 : "=a" (_ret) \ 89 : "r"(_arg1), \ 90 "0"(_num) \ 91 : "memory", "cc" \ 92 ); \ 93 _ret; \ 94 }) 95 96 #define my_syscall2(num, arg1, arg2) \ 97 ({ \ 98 long _ret; \ 99 register long _num asm("eax") = (num); \ 100 register long _arg1 asm("ebx") = (long)(arg1); \ 101 register long _arg2 asm("ecx") = (long)(arg2); \ 102 \ 103 asm volatile ( \ 104 "int $0x80\n" \ 105 : "=a" (_ret) \ 106 : "r"(_arg1), "r"(_arg2), \ 107 "0"(_num) \ 108 : "memory", "cc" \ 109 ); \ 110 _ret; \ 111 }) 112 113 #define my_syscall3(num, arg1, arg2, arg3) \ 114 ({ \ 115 long _ret; \ 116 register long _num asm("eax") = (num); \ 117 register long _arg1 asm("ebx") = (long)(arg1); \ 118 register long _arg2 asm("ecx") = (long)(arg2); \ 119 register long _arg3 asm("edx") = (long)(arg3); \ 120 \ 121 asm volatile ( \ 122 "int $0x80\n" \ 123 : "=a" (_ret) \ 124 : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 125 "0"(_num) \ 126 : "memory", "cc" \ 127 ); \ 128 _ret; \ 129 }) 130 131 #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 132 ({ \ 133 long _ret; \ 134 register long _num asm("eax") = (num); \ 135 register long _arg1 asm("ebx") = (long)(arg1); \ 136 register long _arg2 asm("ecx") = (long)(arg2); \ 137 register long _arg3 asm("edx") = (long)(arg3); \ 138 register long _arg4 asm("esi") = (long)(arg4); \ 139 \ 140 asm volatile ( \ 141 "int $0x80\n" \ 142 : "=a" (_ret) \ 143 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 144 "0"(_num) \ 145 : "memory", "cc" \ 146 ); \ 147 _ret; \ 148 }) 149 150 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 151 ({ \ 152 long _ret; \ 153 register long _num asm("eax") = (num); \ 154 register long _arg1 asm("ebx") = (long)(arg1); \ 155 register long _arg2 asm("ecx") = (long)(arg2); \ 156 register long _arg3 asm("edx") = (long)(arg3); \ 157 register long _arg4 asm("esi") = (long)(arg4); \ 158 register long _arg5 asm("edi") = (long)(arg5); \ 159 \ 160 asm volatile ( \ 161 "int $0x80\n" \ 162 : "=a" (_ret) \ 163 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 164 "0"(_num) \ 165 : "memory", "cc" \ 166 ); \ 167 _ret; \ 168 }) 169 170 /* startup code */ 171 /* 172 * i386 System V ABI mandates: 173 * 1) last pushed argument must be 16-byte aligned. 174 * 2) The deepest stack frame should be set to zero 175 * 176 */ 177 asm(".section .text\n" 178 ".weak _start\n" 179 "_start:\n" 180 "pop %eax\n" // argc (first arg, %eax) 181 "mov %esp, %ebx\n" // argv[] (second arg, %ebx) 182 "lea 4(%ebx,%eax,4),%ecx\n" // then a NULL then envp (third arg, %ecx) 183 "xor %ebp, %ebp\n" // zero the stack frame 184 "and $-16, %esp\n" // x86 ABI : esp must be 16-byte aligned before 185 "sub $4, %esp\n" // the call instruction (args are aligned) 186 "push %ecx\n" // push all registers on the stack so that we 187 "push %ebx\n" // support both regparm and plain stack modes 188 "push %eax\n" 189 "call main\n" // main() returns the status code in %eax 190 "mov %eax, %ebx\n" // retrieve exit code (32-bit int) 191 "movl $1, %eax\n" // NR_exit == 1 192 "int $0x80\n" // exit now 193 "hlt\n" // ensure it does not 194 ""); 195 196 #endif // _NOLIBC_ARCH_I386_H 197