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
12 /* The struct returned by the stat() syscall, 32-bit only, the syscall returns
13 * exactly 56 bytes (stops before the unused array).
15 struct sys_stat_struct {
18 unsigned short st_mode;
19 unsigned short st_nlink;
20 unsigned short st_uid;
21 unsigned short st_gid;
23 unsigned long st_rdev;
24 unsigned long st_size;
25 unsigned long st_blksize;
26 unsigned long st_blocks;
28 unsigned long st_atime;
29 unsigned long st_atime_nsec;
30 unsigned long st_mtime;
31 unsigned long st_mtime_nsec;
33 unsigned long st_ctime;
34 unsigned long st_ctime_nsec;
35 unsigned long __unused[2];
38 /* Syscalls for i386 :
39 * - mostly similar to x86_64
40 * - registers are 32-bit
41 * - syscall number is passed in eax
42 * - arguments are in ebx, ecx, edx, esi, edi, ebp respectively
43 * - all registers are preserved (except eax of course)
44 * - the system call is performed by calling int $0x80
45 * - syscall return comes in eax
46 * - the arguments are cast to long and assigned into the target registers
47 * which are then simply passed as registers to the asm code, so that we
48 * don't have to experience issues with register constraints.
49 * - the syscall number is always specified last in order to allow to force
50 * some registers before (gcc refuses a %-register at the last position).
52 * Also, i386 supports the old_select syscall if newselect is not available
54 #define __ARCH_WANT_SYS_OLD_SELECT
56 #define my_syscall0(num) \
59 register long _num __asm__ ("eax") = (num); \
70 #define my_syscall1(num, arg1) \
73 register long _num __asm__ ("eax") = (num); \
74 register long _arg1 __asm__ ("ebx") = (long)(arg1); \
86 #define my_syscall2(num, arg1, arg2) \
89 register long _num __asm__ ("eax") = (num); \
90 register long _arg1 __asm__ ("ebx") = (long)(arg1); \
91 register long _arg2 __asm__ ("ecx") = (long)(arg2); \
96 : "r"(_arg1), "r"(_arg2), \
103 #define my_syscall3(num, arg1, arg2, arg3) \
106 register long _num __asm__ ("eax") = (num); \
107 register long _arg1 __asm__ ("ebx") = (long)(arg1); \
108 register long _arg2 __asm__ ("ecx") = (long)(arg2); \
109 register long _arg3 __asm__ ("edx") = (long)(arg3); \
114 : "r"(_arg1), "r"(_arg2), "r"(_arg3), \
121 #define my_syscall4(num, arg1, arg2, arg3, arg4) \
124 register long _num __asm__ ("eax") = (num); \
125 register long _arg1 __asm__ ("ebx") = (long)(arg1); \
126 register long _arg2 __asm__ ("ecx") = (long)(arg2); \
127 register long _arg3 __asm__ ("edx") = (long)(arg3); \
128 register long _arg4 __asm__ ("esi") = (long)(arg4); \
133 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \
140 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
143 register long _num __asm__ ("eax") = (num); \
144 register long _arg1 __asm__ ("ebx") = (long)(arg1); \
145 register long _arg2 __asm__ ("ecx") = (long)(arg2); \
146 register long _arg3 __asm__ ("edx") = (long)(arg3); \
147 register long _arg4 __asm__ ("esi") = (long)(arg4); \
148 register long _arg5 __asm__ ("edi") = (long)(arg5); \
153 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
160 #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
162 long _eax = (long)(num); \
163 long _arg6 = (long)(arg6); /* Always in memory */ \
165 "pushl %[_arg6]\n\t" \
167 "movl 4(%%esp),%%ebp\n\t" \
170 "addl $4,%%esp\n\t" \
171 : "+a"(_eax) /* %eax */ \
172 : "b"(arg1), /* %ebx */ \
173 "c"(arg2), /* %ecx */ \
174 "d"(arg3), /* %edx */ \
175 "S"(arg4), /* %esi */ \
176 "D"(arg5), /* %edi */ \
177 [_arg6]"m"(_arg6) /* memory */ \
183 char **environ __attribute__((weak));
184 const unsigned long *_auxv __attribute__((weak));
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 */