xref: /openbmc/linux/tools/include/nolibc/arch-i386.h (revision 2a598d0b)
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 #include "compiler.h"
11 
12 /* The struct returned by the stat() syscall, 32-bit only, the syscall returns
13  * exactly 56 bytes (stops before the unused array).
14  */
15 struct sys_stat_struct {
16 	unsigned long  st_dev;
17 	unsigned long  st_ino;
18 	unsigned short st_mode;
19 	unsigned short st_nlink;
20 	unsigned short st_uid;
21 	unsigned short st_gid;
22 
23 	unsigned long  st_rdev;
24 	unsigned long  st_size;
25 	unsigned long  st_blksize;
26 	unsigned long  st_blocks;
27 
28 	unsigned long  st_atime;
29 	unsigned long  st_atime_nsec;
30 	unsigned long  st_mtime;
31 	unsigned long  st_mtime_nsec;
32 
33 	unsigned long  st_ctime;
34 	unsigned long  st_ctime_nsec;
35 	unsigned long  __unused[2];
36 };
37 
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).
51  *
52  * Also, i386 supports the old_select syscall if newselect is not available
53  */
54 #define __ARCH_WANT_SYS_OLD_SELECT
55 
56 #define my_syscall0(num)                                                      \
57 ({                                                                            \
58 	long _ret;                                                            \
59 	register long _num __asm__ ("eax") = (num);                           \
60 	                                                                      \
61 	__asm__  volatile (                                                   \
62 		"int $0x80\n"                                                 \
63 		: "=a" (_ret)                                                 \
64 		: "0"(_num)                                                   \
65 		: "memory", "cc"                                              \
66 	);                                                                    \
67 	_ret;                                                                 \
68 })
69 
70 #define my_syscall1(num, arg1)                                                \
71 ({                                                                            \
72 	long _ret;                                                            \
73 	register long _num __asm__ ("eax") = (num);                           \
74 	register long _arg1 __asm__ ("ebx") = (long)(arg1);                   \
75 	                                                                      \
76 	__asm__  volatile (                                                   \
77 		"int $0x80\n"                                                 \
78 		: "=a" (_ret)                                                 \
79 		: "r"(_arg1),                                                 \
80 		  "0"(_num)                                                   \
81 		: "memory", "cc"                                              \
82 	);                                                                    \
83 	_ret;                                                                 \
84 })
85 
86 #define my_syscall2(num, arg1, arg2)                                          \
87 ({                                                                            \
88 	long _ret;                                                            \
89 	register long _num __asm__ ("eax") = (num);                           \
90 	register long _arg1 __asm__ ("ebx") = (long)(arg1);                   \
91 	register long _arg2 __asm__ ("ecx") = (long)(arg2);                   \
92 	                                                                      \
93 	__asm__  volatile (                                                   \
94 		"int $0x80\n"                                                 \
95 		: "=a" (_ret)                                                 \
96 		: "r"(_arg1), "r"(_arg2),                                     \
97 		  "0"(_num)                                                   \
98 		: "memory", "cc"                                              \
99 	);                                                                    \
100 	_ret;                                                                 \
101 })
102 
103 #define my_syscall3(num, arg1, arg2, arg3)                                    \
104 ({                                                                            \
105 	long _ret;                                                            \
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);                   \
110 	                                                                      \
111 	__asm__  volatile (                                                   \
112 		"int $0x80\n"                                                 \
113 		: "=a" (_ret)                                                 \
114 		: "r"(_arg1), "r"(_arg2), "r"(_arg3),                         \
115 		  "0"(_num)                                                   \
116 		: "memory", "cc"                                              \
117 	);                                                                    \
118 	_ret;                                                                 \
119 })
120 
121 #define my_syscall4(num, arg1, arg2, arg3, arg4)                              \
122 ({                                                                            \
123 	long _ret;                                                            \
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);                   \
129 	                                                                      \
130 	__asm__  volatile (                                                   \
131 		"int $0x80\n"                                                 \
132 		: "=a" (_ret)                                                 \
133 		: "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4),             \
134 		  "0"(_num)                                                   \
135 		: "memory", "cc"                                              \
136 	);                                                                    \
137 	_ret;                                                                 \
138 })
139 
140 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5)                        \
141 ({                                                                            \
142 	long _ret;                                                            \
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);                   \
149 	                                                                      \
150 	__asm__  volatile (                                                   \
151 		"int $0x80\n"                                                 \
152 		: "=a" (_ret)                                                 \
153 		: "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
154 		  "0"(_num)                                                   \
155 		: "memory", "cc"                                              \
156 	);                                                                    \
157 	_ret;                                                                 \
158 })
159 
160 #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6)	\
161 ({								\
162 	long _eax  = (long)(num);				\
163 	long _arg6 = (long)(arg6); /* Always in memory */	\
164 	__asm__ volatile (					\
165 		"pushl	%[_arg6]\n\t"				\
166 		"pushl	%%ebp\n\t"				\
167 		"movl	4(%%esp),%%ebp\n\t"			\
168 		"int	$0x80\n\t"				\
169 		"popl	%%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 */		\
178 		: "memory", "cc"				\
179 	);							\
180 	_eax;							\
181 })
182 
183 char **environ __attribute__((weak));
184 const unsigned long *_auxv __attribute__((weak));
185 
186 /* startup code */
187 /*
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
191  *
192  */
193 void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
194 {
195 	__asm__ volatile (
196 #ifdef _NOLIBC_STACKPROTECTOR
197 		"call __stack_chk_init\n"   /* initialize stack protector                    */
198 #endif
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) */
205 		"0:\n"
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)    */
208 		"jnz 0b\n"
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    */
214 		"push %eax\n"
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                            */
220 	);
221 	__builtin_unreachable();
222 }
223 
224 #endif /* _NOLIBC_ARCH_I386_H */
225