]> Git Repo - linux.git/blob - tools/include/nolibc/arch-i386.h
arm64: avoid prototype warnings for syscalls
[linux.git] / tools / include / nolibc / arch-i386.h
1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3  * i386 specific definitions for NOLIBC
4  * Copyright (C) 2017-2022 Willy Tarreau <[email protected]>
5  */
6
7 #ifndef _NOLIBC_ARCH_I386_H
8 #define _NOLIBC_ARCH_I386_H
9
10 /* The struct returned by the stat() syscall, 32-bit only, the syscall returns
11  * exactly 56 bytes (stops before the unused array).
12  */
13 struct sys_stat_struct {
14         unsigned long  st_dev;
15         unsigned long  st_ino;
16         unsigned short st_mode;
17         unsigned short st_nlink;
18         unsigned short st_uid;
19         unsigned short st_gid;
20
21         unsigned long  st_rdev;
22         unsigned long  st_size;
23         unsigned long  st_blksize;
24         unsigned long  st_blocks;
25
26         unsigned long  st_atime;
27         unsigned long  st_atime_nsec;
28         unsigned long  st_mtime;
29         unsigned long  st_mtime_nsec;
30
31         unsigned long  st_ctime;
32         unsigned long  st_ctime_nsec;
33         unsigned long  __unused[2];
34 };
35
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).
49  *
50  * Also, i386 supports the old_select syscall if newselect is not available
51  */
52 #define __ARCH_WANT_SYS_OLD_SELECT
53
54 #define my_syscall0(num)                                                      \
55 ({                                                                            \
56         long _ret;                                                            \
57         register long _num __asm__ ("eax") = (num);                           \
58                                                                               \
59         __asm__  volatile (                                                   \
60                 "int $0x80\n"                                                 \
61                 : "=a" (_ret)                                                 \
62                 : "0"(_num)                                                   \
63                 : "memory", "cc"                                              \
64         );                                                                    \
65         _ret;                                                                 \
66 })
67
68 #define my_syscall1(num, arg1)                                                \
69 ({                                                                            \
70         long _ret;                                                            \
71         register long _num __asm__ ("eax") = (num);                           \
72         register long _arg1 __asm__ ("ebx") = (long)(arg1);                   \
73                                                                               \
74         __asm__  volatile (                                                   \
75                 "int $0x80\n"                                                 \
76                 : "=a" (_ret)                                                 \
77                 : "r"(_arg1),                                                 \
78                   "0"(_num)                                                   \
79                 : "memory", "cc"                                              \
80         );                                                                    \
81         _ret;                                                                 \
82 })
83
84 #define my_syscall2(num, arg1, arg2)                                          \
85 ({                                                                            \
86         long _ret;                                                            \
87         register long _num __asm__ ("eax") = (num);                           \
88         register long _arg1 __asm__ ("ebx") = (long)(arg1);                   \
89         register long _arg2 __asm__ ("ecx") = (long)(arg2);                   \
90                                                                               \
91         __asm__  volatile (                                                   \
92                 "int $0x80\n"                                                 \
93                 : "=a" (_ret)                                                 \
94                 : "r"(_arg1), "r"(_arg2),                                     \
95                   "0"(_num)                                                   \
96                 : "memory", "cc"                                              \
97         );                                                                    \
98         _ret;                                                                 \
99 })
100
101 #define my_syscall3(num, arg1, arg2, arg3)                                    \
102 ({                                                                            \
103         long _ret;                                                            \
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);                   \
108                                                                               \
109         __asm__  volatile (                                                   \
110                 "int $0x80\n"                                                 \
111                 : "=a" (_ret)                                                 \
112                 : "r"(_arg1), "r"(_arg2), "r"(_arg3),                         \
113                   "0"(_num)                                                   \
114                 : "memory", "cc"                                              \
115         );                                                                    \
116         _ret;                                                                 \
117 })
118
119 #define my_syscall4(num, arg1, arg2, arg3, arg4)                              \
120 ({                                                                            \
121         long _ret;                                                            \
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);                   \
127                                                                               \
128         __asm__  volatile (                                                   \
129                 "int $0x80\n"                                                 \
130                 : "=a" (_ret)                                                 \
131                 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4),             \
132                   "0"(_num)                                                   \
133                 : "memory", "cc"                                              \
134         );                                                                    \
135         _ret;                                                                 \
136 })
137
138 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5)                        \
139 ({                                                                            \
140         long _ret;                                                            \
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);                   \
147                                                                               \
148         __asm__  volatile (                                                   \
149                 "int $0x80\n"                                                 \
150                 : "=a" (_ret)                                                 \
151                 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
152                   "0"(_num)                                                   \
153                 : "memory", "cc"                                              \
154         );                                                                    \
155         _ret;                                                                 \
156 })
157
158 #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6)    \
159 ({                                                              \
160         long _eax  = (long)(num);                               \
161         long _arg6 = (long)(arg6); /* Always in memory */       \
162         __asm__ volatile (                                      \
163                 "pushl  %[_arg6]\n\t"                           \
164                 "pushl  %%ebp\n\t"                              \
165                 "movl   4(%%esp),%%ebp\n\t"                     \
166                 "int    $0x80\n\t"                              \
167                 "popl   %%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 */            \
176                 : "memory", "cc"                                \
177         );                                                      \
178         _eax;                                                   \
179 })
180
181 char **environ __attribute__((weak));
182 const unsigned long *_auxv __attribute__((weak));
183
184 #define __ARCH_SUPPORTS_STACK_PROTECTOR
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
This page took 0.046188 seconds and 4 git commands to generate.