1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3 * MIPS specific definitions for NOLIBC
4 * Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu>
5 */
6
7 #ifndef _NOLIBC_ARCH_MIPS_H
8 #define _NOLIBC_ARCH_MIPS_H
9
10 #include <linux/unistd.h>
11
12 #include "compiler.h"
13 #include "crt.h"
14 #include "std.h"
15
16 #if !defined(_ABIO32) && !defined(_ABIN32) && !defined(_ABI64)
17 #error Unsupported MIPS ABI
18 #endif
19
20 /* Syscalls for MIPS ABI O32 :
21 * - WARNING! there's always a delayed slot!
22 * - WARNING again, the syntax is different, registers take a '$' and numbers
23 * do not.
24 * - registers are 32-bit
25 * - stack is 8-byte aligned
26 * - syscall number is passed in v0 (starts at 0xfa0).
27 * - arguments are in a0, a1, a2, a3, then the stack. The caller needs to
28 * leave some room in the stack for the callee to save a0..a3 if needed.
29 * - Many registers are clobbered, in fact only a0..a2 and s0..s8 are
30 * preserved. See: https://www.linux-mips.org/wiki/Syscall as well as
31 * scall32-o32.S in the kernel sources.
32 * - the system call is performed by calling "syscall"
33 * - syscall return comes in v0, and register a3 needs to be checked to know
34 * if an error occurred, in which case errno is in v0.
35 * - the arguments are cast to long and assigned into the target registers
36 * which are then simply passed as registers to the asm code, so that we
37 * don't have to experience issues with register constraints.
38 *
39 * Syscalls for MIPS ABI N32, same as ABI O32 with the following differences :
40 * - arguments are in a0, a1, a2, a3, t0, t1, t2, t3.
41 * t0..t3 are also known as a4..a7.
42 * - stack is 16-byte aligned
43 */
44
45 #if !defined(__mips_isa_rev) || __mips_isa_rev < 6
46 #define _NOLIBC_SYSCALL_CLOBBER_HI_LO "hi", "lo"
47 #else
48 #define _NOLIBC_SYSCALL_CLOBBER_HI_LO "$0"
49 #endif
50
51 #if defined(_ABIO32)
52
53 #define _NOLIBC_SYSCALL_CLOBBERLIST \
54 "memory", "cc", "at", "v1", \
55 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9", \
56 _NOLIBC_SYSCALL_CLOBBER_HI_LO
57
58 #define _NOLIBC_SYSCALL_STACK_RESERVE "addiu $sp, $sp, -32\n"
59 #define _NOLIBC_SYSCALL_STACK_UNRESERVE "addiu $sp, $sp, 32\n"
60
61 #define _NOLIBC_SYSCALL_REG register long
62
63 #else /* _ABIN32 || _ABI64 */
64
65 /* binutils, GCC and clang disagree about register aliases, use numbers instead. */
66 #define _NOLIBC_SYSCALL_CLOBBERLIST \
67 "memory", "cc", "at", "v1", \
68 "10", "11", "12", "13", "14", "15", "24", "25", \
69 _NOLIBC_SYSCALL_CLOBBER_HI_LO
70
71 #define _NOLIBC_SYSCALL_STACK_RESERVE
72 #define _NOLIBC_SYSCALL_STACK_UNRESERVE
73
74 #define _NOLIBC_SYSCALL_REG register long long
75 #define __NOLIBC_BITS_PER_SYSCALL_ARG 64
76
77 #endif /* _ABIO32 */
78
79 #define __nolibc_syscall0(num) \
80 ({ \
81 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \
82 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("a3"); \
83 \
84 __asm__ volatile ( \
85 _NOLIBC_SYSCALL_STACK_RESERVE \
86 "syscall\n" \
87 _NOLIBC_SYSCALL_STACK_UNRESERVE \
88 : "=r"(_num), "=r"(_arg4) \
89 : "r"(_num) \
90 : _NOLIBC_SYSCALL_CLOBBERLIST \
91 ); \
92 _arg4 ? -_num : _num; \
93 })
94
95 #define __nolibc_syscall1(num, arg1) \
96 ({ \
97 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \
98 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("a0") = __nolibc_arg_to_reg(arg1); \
99 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("a3"); \
100 \
101 __asm__ volatile ( \
102 _NOLIBC_SYSCALL_STACK_RESERVE \
103 "syscall\n" \
104 _NOLIBC_SYSCALL_STACK_UNRESERVE \
105 : "=r"(_num), "=r"(_arg4) \
106 : "0"(_num), \
107 "r"(_arg1) \
108 : _NOLIBC_SYSCALL_CLOBBERLIST \
109 ); \
110 _arg4 ? -_num : _num; \
111 })
112
113 #define __nolibc_syscall2(num, arg1, arg2) \
114 ({ \
115 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \
116 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("a0") = __nolibc_arg_to_reg(arg1); \
117 _NOLIBC_SYSCALL_REG _arg2 __asm__ ("a1") = __nolibc_arg_to_reg(arg2); \
118 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("a3"); \
119 \
120 __asm__ volatile ( \
121 _NOLIBC_SYSCALL_STACK_RESERVE \
122 "syscall\n" \
123 _NOLIBC_SYSCALL_STACK_UNRESERVE \
124 : "=r"(_num), "=r"(_arg4) \
125 : "0"(_num), \
126 "r"(_arg1), "r"(_arg2) \
127 : _NOLIBC_SYSCALL_CLOBBERLIST \
128 ); \
129 _arg4 ? -_num : _num; \
130 })
131
132 #define __nolibc_syscall3(num, arg1, arg2, arg3) \
133 ({ \
134 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \
135 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("a0") = __nolibc_arg_to_reg(arg1); \
136 _NOLIBC_SYSCALL_REG _arg2 __asm__ ("a1") = __nolibc_arg_to_reg(arg2); \
137 _NOLIBC_SYSCALL_REG _arg3 __asm__ ("a2") = __nolibc_arg_to_reg(arg3); \
138 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("a3"); \
139 \
140 __asm__ volatile ( \
141 _NOLIBC_SYSCALL_STACK_RESERVE \
142 "syscall\n" \
143 _NOLIBC_SYSCALL_STACK_UNRESERVE \
144 : "=r"(_num), "=r"(_arg4) \
145 : "0"(_num), \
146 "r"(_arg1), "r"(_arg2), "r"(_arg3) \
147 : _NOLIBC_SYSCALL_CLOBBERLIST \
148 ); \
149 _arg4 ? -_num : _num; \
150 })
151
152 #define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \
153 ({ \
154 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \
155 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("a0") = __nolibc_arg_to_reg(arg1); \
156 _NOLIBC_SYSCALL_REG _arg2 __asm__ ("a1") = __nolibc_arg_to_reg(arg2); \
157 _NOLIBC_SYSCALL_REG _arg3 __asm__ ("a2") = __nolibc_arg_to_reg(arg3); \
158 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("a3") = __nolibc_arg_to_reg(arg4); \
159 \
160 __asm__ volatile ( \
161 _NOLIBC_SYSCALL_STACK_RESERVE \
162 "syscall\n" \
163 _NOLIBC_SYSCALL_STACK_UNRESERVE \
164 : "=r" (_num), "=r"(_arg4) \
165 : "0"(_num), \
166 "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4) \
167 : _NOLIBC_SYSCALL_CLOBBERLIST \
168 ); \
169 _arg4 ? -_num : _num; \
170 })
171
172 #if defined(_ABIO32)
173
174 #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
175 ({ \
176 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \
177 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("a0") = __nolibc_arg_to_reg(arg1); \
178 _NOLIBC_SYSCALL_REG _arg2 __asm__ ("a1") = __nolibc_arg_to_reg(arg2); \
179 _NOLIBC_SYSCALL_REG _arg3 __asm__ ("a2") = __nolibc_arg_to_reg(arg3); \
180 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("a3") = __nolibc_arg_to_reg(arg4); \
181 _NOLIBC_SYSCALL_REG _arg5 = __nolibc_arg_to_reg(arg5); \
182 \
183 __asm__ volatile ( \
184 _NOLIBC_SYSCALL_STACK_RESERVE \
185 "sw %7, 16($sp)\n" \
186 "syscall\n" \
187 _NOLIBC_SYSCALL_STACK_UNRESERVE \
188 : "=r" (_num), "=r"(_arg4) \
189 : "0"(_num), \
190 "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5) \
191 : _NOLIBC_SYSCALL_CLOBBERLIST \
192 ); \
193 _arg4 ? -_num : _num; \
194 })
195
196 #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
197 ({ \
198 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \
199 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("a0") = __nolibc_arg_to_reg(arg1); \
200 _NOLIBC_SYSCALL_REG _arg2 __asm__ ("a1") = __nolibc_arg_to_reg(arg2); \
201 _NOLIBC_SYSCALL_REG _arg3 __asm__ ("a2") = __nolibc_arg_to_reg(arg3); \
202 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("a3") = __nolibc_arg_to_reg(arg4); \
203 _NOLIBC_SYSCALL_REG _arg5 = __nolibc_arg_to_reg(arg5); \
204 _NOLIBC_SYSCALL_REG _arg6 = __nolibc_arg_to_reg(arg6); \
205 \
206 __asm__ volatile ( \
207 _NOLIBC_SYSCALL_STACK_RESERVE \
208 "sw %7, 16($sp)\n" \
209 "sw %8, 20($sp)\n" \
210 "syscall\n" \
211 _NOLIBC_SYSCALL_STACK_UNRESERVE \
212 : "=r" (_num), "=r"(_arg4) \
213 : "0"(_num), \
214 "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
215 "r"(_arg6) \
216 : _NOLIBC_SYSCALL_CLOBBERLIST \
217 ); \
218 _arg4 ? -_num : _num; \
219 })
220
221 #else /* _ABIN32 || _ABI64 */
222
223 #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
224 ({ \
225 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \
226 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("$4") = __nolibc_arg_to_reg(arg1); \
227 _NOLIBC_SYSCALL_REG _arg2 __asm__ ("$5") = __nolibc_arg_to_reg(arg2); \
228 _NOLIBC_SYSCALL_REG _arg3 __asm__ ("$6") = __nolibc_arg_to_reg(arg3); \
229 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("$7") = __nolibc_arg_to_reg(arg4); \
230 _NOLIBC_SYSCALL_REG _arg5 __asm__ ("$8") = __nolibc_arg_to_reg(arg5); \
231 \
232 __asm__ volatile ( \
233 "syscall\n" \
234 : "=r" (_num), "=r"(_arg4) \
235 : "0"(_num), \
236 "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5) \
237 : _NOLIBC_SYSCALL_CLOBBERLIST \
238 ); \
239 _arg4 ? -_num : _num; \
240 })
241
242 #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
243 ({ \
244 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \
245 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("$4") = __nolibc_arg_to_reg(arg1); \
246 _NOLIBC_SYSCALL_REG _arg2 __asm__ ("$5") = __nolibc_arg_to_reg(arg2); \
247 _NOLIBC_SYSCALL_REG _arg3 __asm__ ("$6") = __nolibc_arg_to_reg(arg3); \
248 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("$7") = __nolibc_arg_to_reg(arg4); \
249 _NOLIBC_SYSCALL_REG _arg5 __asm__ ("$8") = __nolibc_arg_to_reg(arg5); \
250 _NOLIBC_SYSCALL_REG _arg6 __asm__ ("$9") = __nolibc_arg_to_reg(arg6); \
251 \
252 __asm__ volatile ( \
253 "syscall\n" \
254 : "=r" (_num), "=r"(_arg4) \
255 : "0"(_num), \
256 "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
257 "r"(_arg6) \
258 : _NOLIBC_SYSCALL_CLOBBERLIST \
259 ); \
260 _arg4 ? -_num : _num; \
261 })
262
263 #endif /* _ABIO32 */
264
265 #ifndef NOLIBC_NO_RUNTIME
266 /* startup code, note that it's called __start on MIPS */
267 void __start(void);
__start(void)268 void __attribute__((weak, noreturn)) __nolibc_entrypoint __nolibc_no_stack_protector __start(void)
269 {
270 __asm__ volatile (
271 "move $a0, $sp\n" /* save stack pointer to $a0, as arg1 of _start_c */
272 #if defined(_ABIO32)
273 "addiu $sp, $sp, -16\n" /* the callee expects to save a0..a3 there */
274 #endif /* _ABIO32 */
275 "lui $t9, %hi(_start_c)\n" /* ABI requires current function address in $t9 */
276 "ori $t9, %lo(_start_c)\n"
277 #if defined(_ABI64)
278 "lui $t0, %highest(_start_c)\n"
279 "ori $t0, %higher(_start_c)\n"
280 "dsll $t0, 0x20\n"
281 "or $t9, $t0\n"
282 #endif /* _ABI64 */
283 "jalr $t9\n" /* transfer to c runtime */
284 );
285 __nolibc_entrypoint_epilogue();
286 }
287 #endif /* NOLIBC_NO_RUNTIME */
288
289 #if defined(_ABIO32)
290 #define __NOLIBC_PAD_64BIT_SYSCALL_ARGUMENT_PAIR
291 #endif
292
293 #endif /* _NOLIBC_ARCH_MIPS_H */
294