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 76 #endif /* _ABIO32 */ 77 78 #define __nolibc_syscall0(num) \ 79 ({ \ 80 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \ 81 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("a3"); \ 82 \ 83 __asm__ volatile ( \ 84 _NOLIBC_SYSCALL_STACK_RESERVE \ 85 "syscall\n" \ 86 _NOLIBC_SYSCALL_STACK_UNRESERVE \ 87 : "=r"(_num), "=r"(_arg4) \ 88 : "r"(_num) \ 89 : _NOLIBC_SYSCALL_CLOBBERLIST \ 90 ); \ 91 _arg4 ? -_num : _num; \ 92 }) 93 94 #define __nolibc_syscall1(num, arg1) \ 95 ({ \ 96 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \ 97 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("a0") = __nolibc_arg_to_reg(arg1); \ 98 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("a3"); \ 99 \ 100 __asm__ volatile ( \ 101 _NOLIBC_SYSCALL_STACK_RESERVE \ 102 "syscall\n" \ 103 _NOLIBC_SYSCALL_STACK_UNRESERVE \ 104 : "=r"(_num), "=r"(_arg4) \ 105 : "0"(_num), \ 106 "r"(_arg1) \ 107 : _NOLIBC_SYSCALL_CLOBBERLIST \ 108 ); \ 109 _arg4 ? -_num : _num; \ 110 }) 111 112 #define __nolibc_syscall2(num, arg1, arg2) \ 113 ({ \ 114 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \ 115 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("a0") = __nolibc_arg_to_reg(arg1); \ 116 _NOLIBC_SYSCALL_REG _arg2 __asm__ ("a1") = __nolibc_arg_to_reg(arg2); \ 117 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("a3"); \ 118 \ 119 __asm__ volatile ( \ 120 _NOLIBC_SYSCALL_STACK_RESERVE \ 121 "syscall\n" \ 122 _NOLIBC_SYSCALL_STACK_UNRESERVE \ 123 : "=r"(_num), "=r"(_arg4) \ 124 : "0"(_num), \ 125 "r"(_arg1), "r"(_arg2) \ 126 : _NOLIBC_SYSCALL_CLOBBERLIST \ 127 ); \ 128 _arg4 ? -_num : _num; \ 129 }) 130 131 #define __nolibc_syscall3(num, arg1, arg2, arg3) \ 132 ({ \ 133 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \ 134 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("a0") = __nolibc_arg_to_reg(arg1); \ 135 _NOLIBC_SYSCALL_REG _arg2 __asm__ ("a1") = __nolibc_arg_to_reg(arg2); \ 136 _NOLIBC_SYSCALL_REG _arg3 __asm__ ("a2") = __nolibc_arg_to_reg(arg3); \ 137 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("a3"); \ 138 \ 139 __asm__ volatile ( \ 140 _NOLIBC_SYSCALL_STACK_RESERVE \ 141 "syscall\n" \ 142 _NOLIBC_SYSCALL_STACK_UNRESERVE \ 143 : "=r"(_num), "=r"(_arg4) \ 144 : "0"(_num), \ 145 "r"(_arg1), "r"(_arg2), "r"(_arg3) \ 146 : _NOLIBC_SYSCALL_CLOBBERLIST \ 147 ); \ 148 _arg4 ? -_num : _num; \ 149 }) 150 151 #define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \ 152 ({ \ 153 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \ 154 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("a0") = __nolibc_arg_to_reg(arg1); \ 155 _NOLIBC_SYSCALL_REG _arg2 __asm__ ("a1") = __nolibc_arg_to_reg(arg2); \ 156 _NOLIBC_SYSCALL_REG _arg3 __asm__ ("a2") = __nolibc_arg_to_reg(arg3); \ 157 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("a3") = __nolibc_arg_to_reg(arg4); \ 158 \ 159 __asm__ volatile ( \ 160 _NOLIBC_SYSCALL_STACK_RESERVE \ 161 "syscall\n" \ 162 _NOLIBC_SYSCALL_STACK_UNRESERVE \ 163 : "=r" (_num), "=r"(_arg4) \ 164 : "0"(_num), \ 165 "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4) \ 166 : _NOLIBC_SYSCALL_CLOBBERLIST \ 167 ); \ 168 _arg4 ? -_num : _num; \ 169 }) 170 171 #if defined(_ABIO32) 172 173 #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 174 ({ \ 175 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \ 176 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("a0") = __nolibc_arg_to_reg(arg1); \ 177 _NOLIBC_SYSCALL_REG _arg2 __asm__ ("a1") = __nolibc_arg_to_reg(arg2); \ 178 _NOLIBC_SYSCALL_REG _arg3 __asm__ ("a2") = __nolibc_arg_to_reg(arg3); \ 179 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("a3") = __nolibc_arg_to_reg(arg4); \ 180 _NOLIBC_SYSCALL_REG _arg5 = __nolibc_arg_to_reg(arg5); \ 181 \ 182 __asm__ volatile ( \ 183 _NOLIBC_SYSCALL_STACK_RESERVE \ 184 "sw %7, 16($sp)\n" \ 185 "syscall\n" \ 186 _NOLIBC_SYSCALL_STACK_UNRESERVE \ 187 : "=r" (_num), "=r"(_arg4) \ 188 : "0"(_num), \ 189 "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5) \ 190 : _NOLIBC_SYSCALL_CLOBBERLIST \ 191 ); \ 192 _arg4 ? -_num : _num; \ 193 }) 194 195 #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 196 ({ \ 197 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \ 198 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("a0") = __nolibc_arg_to_reg(arg1); \ 199 _NOLIBC_SYSCALL_REG _arg2 __asm__ ("a1") = __nolibc_arg_to_reg(arg2); \ 200 _NOLIBC_SYSCALL_REG _arg3 __asm__ ("a2") = __nolibc_arg_to_reg(arg3); \ 201 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("a3") = __nolibc_arg_to_reg(arg4); \ 202 _NOLIBC_SYSCALL_REG _arg5 = __nolibc_arg_to_reg(arg5); \ 203 _NOLIBC_SYSCALL_REG _arg6 = __nolibc_arg_to_reg(arg6); \ 204 \ 205 __asm__ volatile ( \ 206 _NOLIBC_SYSCALL_STACK_RESERVE \ 207 "sw %7, 16($sp)\n" \ 208 "sw %8, 20($sp)\n" \ 209 "syscall\n" \ 210 _NOLIBC_SYSCALL_STACK_UNRESERVE \ 211 : "=r" (_num), "=r"(_arg4) \ 212 : "0"(_num), \ 213 "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 214 "r"(_arg6) \ 215 : _NOLIBC_SYSCALL_CLOBBERLIST \ 216 ); \ 217 _arg4 ? -_num : _num; \ 218 }) 219 220 #else /* _ABIN32 || _ABI64 */ 221 222 #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 223 ({ \ 224 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \ 225 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("$4") = __nolibc_arg_to_reg(arg1); \ 226 _NOLIBC_SYSCALL_REG _arg2 __asm__ ("$5") = __nolibc_arg_to_reg(arg2); \ 227 _NOLIBC_SYSCALL_REG _arg3 __asm__ ("$6") = __nolibc_arg_to_reg(arg3); \ 228 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("$7") = __nolibc_arg_to_reg(arg4); \ 229 _NOLIBC_SYSCALL_REG _arg5 __asm__ ("$8") = __nolibc_arg_to_reg(arg5); \ 230 \ 231 __asm__ volatile ( \ 232 "syscall\n" \ 233 : "=r" (_num), "=r"(_arg4) \ 234 : "0"(_num), \ 235 "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5) \ 236 : _NOLIBC_SYSCALL_CLOBBERLIST \ 237 ); \ 238 _arg4 ? -_num : _num; \ 239 }) 240 241 #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 242 ({ \ 243 _NOLIBC_SYSCALL_REG _num __asm__ ("v0") = (num); \ 244 _NOLIBC_SYSCALL_REG _arg1 __asm__ ("$4") = __nolibc_arg_to_reg(arg1); \ 245 _NOLIBC_SYSCALL_REG _arg2 __asm__ ("$5") = __nolibc_arg_to_reg(arg2); \ 246 _NOLIBC_SYSCALL_REG _arg3 __asm__ ("$6") = __nolibc_arg_to_reg(arg3); \ 247 _NOLIBC_SYSCALL_REG _arg4 __asm__ ("$7") = __nolibc_arg_to_reg(arg4); \ 248 _NOLIBC_SYSCALL_REG _arg5 __asm__ ("$8") = __nolibc_arg_to_reg(arg5); \ 249 _NOLIBC_SYSCALL_REG _arg6 __asm__ ("$9") = __nolibc_arg_to_reg(arg6); \ 250 \ 251 __asm__ volatile ( \ 252 "syscall\n" \ 253 : "=r" (_num), "=r"(_arg4) \ 254 : "0"(_num), \ 255 "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 256 "r"(_arg6) \ 257 : _NOLIBC_SYSCALL_CLOBBERLIST \ 258 ); \ 259 _arg4 ? -_num : _num; \ 260 }) 261 262 #endif /* _ABIO32 */ 263 264 #ifndef NOLIBC_NO_RUNTIME 265 /* startup code, note that it's called __start on MIPS */ 266 void __start(void); 267 void __attribute__((weak, noreturn)) __nolibc_entrypoint __nolibc_no_stack_protector __start(void) 268 { 269 __asm__ volatile ( 270 "move $a0, $sp\n" /* save stack pointer to $a0, as arg1 of _start_c */ 271 #if defined(_ABIO32) 272 "addiu $sp, $sp, -16\n" /* the callee expects to save a0..a3 there */ 273 #endif /* _ABIO32 */ 274 "lui $t9, %hi(_start_c)\n" /* ABI requires current function address in $t9 */ 275 "ori $t9, %lo(_start_c)\n" 276 #if defined(_ABI64) 277 "lui $t0, %highest(_start_c)\n" 278 "ori $t0, %higher(_start_c)\n" 279 "dsll $t0, 0x20\n" 280 "or $t9, $t0\n" 281 #endif /* _ABI64 */ 282 "jalr $t9\n" /* transfer to c runtime */ 283 ); 284 __nolibc_entrypoint_epilogue(); 285 } 286 #endif /* NOLIBC_NO_RUNTIME */ 287 288 #if defined(_ABIO32) 289 static __attribute__((unused)) 290 int _sys_ftruncate64(int fd, uint32_t length0, uint32_t length1) 291 { 292 return __nolibc_syscall4(__NR_ftruncate64, fd, 0, length0, length1); 293 } 294 #define _sys_ftruncate64 _sys_ftruncate64 295 #endif 296 297 #endif /* _NOLIBC_ARCH_MIPS_H */ 298