1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
3 * MIPS specific definitions for NOLIBC
4 * Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu>
7 #ifndef _NOLIBC_ARCH_MIPS_H
8 #define _NOLIBC_ARCH_MIPS_H
14 #error Unsupported MIPS ABI
17 /* Syscalls for MIPS ABI O32 :
18 * - WARNING! there's always a delayed slot!
19 * - WARNING again, the syntax is different, registers take a '$' and numbers
21 * - registers are 32-bit
22 * - stack is 8-byte aligned
23 * - syscall number is passed in v0 (starts at 0xfa0).
24 * - arguments are in a0, a1, a2, a3, then the stack. The caller needs to
25 * leave some room in the stack for the callee to save a0..a3 if needed.
26 * - Many registers are clobbered, in fact only a0..a2 and s0..s8 are
27 * preserved. See: https://www.linux-mips.org/wiki/Syscall as well as
28 * scall32-o32.S in the kernel sources.
29 * - the system call is performed by calling "syscall"
30 * - syscall return comes in v0, and register a3 needs to be checked to know
31 * if an error occurred, in which case errno is in v0.
32 * - the arguments are cast to long and assigned into the target registers
33 * which are then simply passed as registers to the asm code, so that we
34 * don't have to experience issues with register constraints.
37 #define _NOLIBC_SYSCALL_CLOBBERLIST \
38 "memory", "cc", "at", "v1", "hi", "lo", \
39 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9"
41 #define my_syscall0(num) \
43 register long _num __asm__ ("v0") = (num); \
44 register long _arg4 __asm__ ("a3"); \
47 "addiu $sp, $sp, -32\n" \
49 "addiu $sp, $sp, 32\n" \
50 : "=r"(_num), "=r"(_arg4) \
52 : _NOLIBC_SYSCALL_CLOBBERLIST \
54 _arg4 ? -_num : _num; \
57 #define my_syscall1(num, arg1) \
59 register long _num __asm__ ("v0") = (num); \
60 register long _arg1 __asm__ ("a0") = (long)(arg1); \
61 register long _arg4 __asm__ ("a3"); \
64 "addiu $sp, $sp, -32\n" \
66 "addiu $sp, $sp, 32\n" \
67 : "=r"(_num), "=r"(_arg4) \
70 : _NOLIBC_SYSCALL_CLOBBERLIST \
72 _arg4 ? -_num : _num; \
75 #define my_syscall2(num, arg1, arg2) \
77 register long _num __asm__ ("v0") = (num); \
78 register long _arg1 __asm__ ("a0") = (long)(arg1); \
79 register long _arg2 __asm__ ("a1") = (long)(arg2); \
80 register long _arg4 __asm__ ("a3"); \
83 "addiu $sp, $sp, -32\n" \
85 "addiu $sp, $sp, 32\n" \
86 : "=r"(_num), "=r"(_arg4) \
88 "r"(_arg1), "r"(_arg2) \
89 : _NOLIBC_SYSCALL_CLOBBERLIST \
91 _arg4 ? -_num : _num; \
94 #define my_syscall3(num, arg1, arg2, arg3) \
96 register long _num __asm__ ("v0") = (num); \
97 register long _arg1 __asm__ ("a0") = (long)(arg1); \
98 register long _arg2 __asm__ ("a1") = (long)(arg2); \
99 register long _arg3 __asm__ ("a2") = (long)(arg3); \
100 register long _arg4 __asm__ ("a3"); \
103 "addiu $sp, $sp, -32\n" \
105 "addiu $sp, $sp, 32\n" \
106 : "=r"(_num), "=r"(_arg4) \
108 "r"(_arg1), "r"(_arg2), "r"(_arg3) \
109 : _NOLIBC_SYSCALL_CLOBBERLIST \
111 _arg4 ? -_num : _num; \
114 #define my_syscall4(num, arg1, arg2, arg3, arg4) \
116 register long _num __asm__ ("v0") = (num); \
117 register long _arg1 __asm__ ("a0") = (long)(arg1); \
118 register long _arg2 __asm__ ("a1") = (long)(arg2); \
119 register long _arg3 __asm__ ("a2") = (long)(arg3); \
120 register long _arg4 __asm__ ("a3") = (long)(arg4); \
123 "addiu $sp, $sp, -32\n" \
125 "addiu $sp, $sp, 32\n" \
126 : "=r" (_num), "=r"(_arg4) \
128 "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4) \
129 : _NOLIBC_SYSCALL_CLOBBERLIST \
131 _arg4 ? -_num : _num; \
134 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
136 register long _num __asm__ ("v0") = (num); \
137 register long _arg1 __asm__ ("a0") = (long)(arg1); \
138 register long _arg2 __asm__ ("a1") = (long)(arg2); \
139 register long _arg3 __asm__ ("a2") = (long)(arg3); \
140 register long _arg4 __asm__ ("a3") = (long)(arg4); \
141 register long _arg5 = (long)(arg5); \
144 "addiu $sp, $sp, -32\n" \
147 "addiu $sp, $sp, 32\n" \
148 : "=r" (_num), "=r"(_arg4) \
150 "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5) \
151 : _NOLIBC_SYSCALL_CLOBBERLIST \
153 _arg4 ? -_num : _num; \
156 #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
158 register long _num __asm__ ("v0") = (num); \
159 register long _arg1 __asm__ ("a0") = (long)(arg1); \
160 register long _arg2 __asm__ ("a1") = (long)(arg2); \
161 register long _arg3 __asm__ ("a2") = (long)(arg3); \
162 register long _arg4 __asm__ ("a3") = (long)(arg4); \
163 register long _arg5 = (long)(arg5); \
164 register long _arg6 = (long)(arg6); \
167 "addiu $sp, $sp, -32\n" \
171 "addiu $sp, $sp, 32\n" \
172 : "=r" (_num), "=r"(_arg4) \
174 "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
176 : _NOLIBC_SYSCALL_CLOBBERLIST \
178 _arg4 ? -_num : _num; \
181 /* startup code, note that it's called __start on MIPS */
182 void __attribute__((weak
, noreturn
)) __nolibc_entrypoint __no_stack_protector
__start(void)
187 "bal 1f\n" /* prime $ra for .cpload */
191 "move $a0, $sp\n" /* save stack pointer to $a0, as arg1 of _start_c */
192 "addiu $sp, $sp, -4\n" /* space for .cprestore to store $gp */
195 "and $sp, $sp, $t0\n" /* $sp must be 8-byte aligned */
196 "addiu $sp, $sp, -16\n" /* the callee expects to save a0..a3 there */
197 "lui $t9, %hi(_start_c)\n" /* ABI requires current function address in $t9 */
198 "ori $t9, %lo(_start_c)\n"
199 "jalr $t9\n" /* transfer to c runtime */
200 " nop\n" /* delayed slot */
203 __nolibc_entrypoint_epilogue();
206 #endif /* _NOLIBC_ARCH_MIPS_H */