1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
3 * i386 specific definitions for NOLIBC
7 #ifndef _NOLIBC_ARCH_I386_H
8 #define _NOLIBC_ARCH_I386_H
10 /* The struct returned by the stat() syscall, 32-bit only, the syscall returns
11 * exactly 56 bytes (stops before the unused array).
13 struct sys_stat_struct {
16 unsigned short st_mode;
17 unsigned short st_nlink;
18 unsigned short st_uid;
19 unsigned short st_gid;
21 unsigned long st_rdev;
22 unsigned long st_size;
23 unsigned long st_blksize;
24 unsigned long st_blocks;
26 unsigned long st_atime;
27 unsigned long st_atime_nsec;
28 unsigned long st_mtime;
29 unsigned long st_mtime_nsec;
31 unsigned long st_ctime;
32 unsigned long st_ctime_nsec;
33 unsigned long __unused[2];
36 /* Syscalls for i386 :
37 * - mostly similar to x86_64
38 * - registers are 32-bit
39 * - syscall number is passed in eax
40 * - arguments are in ebx, ecx, edx, esi, edi, ebp respectively
41 * - all registers are preserved (except eax of course)
42 * - the system call is performed by calling int $0x80
43 * - syscall return comes in eax
44 * - the arguments are cast to long and assigned into the target registers
45 * which are then simply passed as registers to the asm code, so that we
46 * don't have to experience issues with register constraints.
47 * - the syscall number is always specified last in order to allow to force
48 * some registers before (gcc refuses a %-register at the last position).
50 * Also, i386 supports the old_select syscall if newselect is not available
52 #define __ARCH_WANT_SYS_OLD_SELECT
54 #define my_syscall0(num) \
57 register long _num __asm__ ("eax") = (num); \
68 #define my_syscall1(num, arg1) \
71 register long _num __asm__ ("eax") = (num); \
72 register long _arg1 __asm__ ("ebx") = (long)(arg1); \
84 #define my_syscall2(num, arg1, arg2) \
87 register long _num __asm__ ("eax") = (num); \
88 register long _arg1 __asm__ ("ebx") = (long)(arg1); \
89 register long _arg2 __asm__ ("ecx") = (long)(arg2); \
94 : "r"(_arg1), "r"(_arg2), \
101 #define my_syscall3(num, arg1, arg2, arg3) \
104 register long _num __asm__ ("eax") = (num); \
105 register long _arg1 __asm__ ("ebx") = (long)(arg1); \
106 register long _arg2 __asm__ ("ecx") = (long)(arg2); \
107 register long _arg3 __asm__ ("edx") = (long)(arg3); \
112 : "r"(_arg1), "r"(_arg2), "r"(_arg3), \
119 #define my_syscall4(num, arg1, arg2, arg3, arg4) \
122 register long _num __asm__ ("eax") = (num); \
123 register long _arg1 __asm__ ("ebx") = (long)(arg1); \
124 register long _arg2 __asm__ ("ecx") = (long)(arg2); \
125 register long _arg3 __asm__ ("edx") = (long)(arg3); \
126 register long _arg4 __asm__ ("esi") = (long)(arg4); \
131 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \
138 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
141 register long _num __asm__ ("eax") = (num); \
142 register long _arg1 __asm__ ("ebx") = (long)(arg1); \
143 register long _arg2 __asm__ ("ecx") = (long)(arg2); \
144 register long _arg3 __asm__ ("edx") = (long)(arg3); \
145 register long _arg4 __asm__ ("esi") = (long)(arg4); \
146 register long _arg5 __asm__ ("edi") = (long)(arg5); \
151 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
158 #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
160 long _eax = (long)(num); \
161 long _arg6 = (long)(arg6); /* Always in memory */ \
163 "pushl %[_arg6]\n\t" \
165 "movl 4(%%esp),%%ebp\n\t" \
168 "addl $4,%%esp\n\t" \
169 : "+a"(_eax) /* %eax */ \
170 : "b"(arg1), /* %ebx */ \
171 "c"(arg2), /* %ecx */ \
172 "d"(arg3), /* %edx */ \
173 "S"(arg4), /* %esi */ \
174 "D"(arg5), /* %edi */ \
175 [_arg6]"m"(_arg6) /* memory */ \
181 char **environ __attribute__((weak));
182 const unsigned long *_auxv __attribute__((weak));
184 #define __ARCH_SUPPORTS_STACK_PROTECTOR
188 * i386 System V ABI mandates:
189 * 1) last pushed argument must be 16-byte aligned.
190 * 2) The deepest stack frame should be set to zero
193 void __attribute__((weak,noreturn,optimize("omit-frame-pointer"),no_stack_protector)) _start(void)
196 #ifdef NOLIBC_STACKPROTECTOR
197 "call __stack_chk_init\n" // initialize stack protector
199 "pop %eax\n" // argc (first arg, %eax)
200 "mov %esp, %ebx\n" // argv[] (second arg, %ebx)
201 "lea 4(%ebx,%eax,4),%ecx\n" // then a NULL then envp (third arg, %ecx)
202 "mov %ecx, environ\n" // save environ
203 "xor %ebp, %ebp\n" // zero the stack frame
204 "mov %ecx, %edx\n" // search for auxv (follows NULL after last env)
206 "add $4, %edx\n" // search for auxv using edx, it follows the
207 "cmp -4(%edx), %ebp\n" // ... NULL after last env (ebp is zero here)
209 "mov %edx, _auxv\n" // save it into _auxv
210 "and $-16, %esp\n" // x86 ABI : esp must be 16-byte aligned before
211 "sub $4, %esp\n" // the call instruction (args are aligned)
212 "push %ecx\n" // push all registers on the stack so that we
213 "push %ebx\n" // support both regparm and plain stack modes
215 "call main\n" // main() returns the status code in %eax
216 "mov %eax, %ebx\n" // retrieve exit code (32-bit int)
217 "movl $1, %eax\n" // NR_exit == 1
218 "int $0x80\n" // exit now
219 "hlt\n" // ensure it does not
221 __builtin_unreachable();
224 #endif // _NOLIBC_ARCH_I386_H