1 /* $NetBSD: syscall.c,v 1.51 2023/10/05 19:41:04 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe and by Charles M. Hannum. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1988 University of Utah. 34 * Copyright (c) 1992, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * This code is derived from software contributed to Berkeley by 38 * the Systems Programming Group of the University of Utah Computer 39 * Science Department and Ralph Campbell. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 3. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * from: Utah Hdr: trap.c 1.32 91/04/06 66 * 67 * @(#)trap.c 8.5 (Berkeley) 1/11/94 68 */ 69 70 #include <sys/cdefs.h> 71 __KERNEL_RCSID(0, "$NetBSD: syscall.c,v 1.51 2023/10/05 19:41:04 ad Exp $"); 72 73 #include <sys/param.h> 74 #include <sys/cpu.h> 75 #include <sys/endian.h> 76 #include <sys/proc.h> 77 #include <sys/signal.h> 78 #include <sys/syscall.h> 79 #include <sys/syscallvar.h> 80 #include <sys/systm.h> 81 82 #include <uvm/uvm_extern.h> 83 84 #include <mips/locore.h> 85 #include <mips/trap.h> 86 #include <mips/reg.h> 87 #include <mips/regnum.h> /* symbolic register indices */ 88 #include <mips/userret.h> 89 90 #ifndef EMULNAME 91 #define EMULNAME(x) (x) 92 #endif 93 94 #ifndef SYSCALL_SHIFT 95 #define SYSCALL_SHIFT 0 96 #endif 97 98 void EMULNAME(syscall_intern)(struct proc *); 99 static void EMULNAME(syscall)(struct lwp *, uint32_t, uint32_t, vaddr_t); 100 101 void 102 EMULNAME(syscall_intern)(struct proc *p) 103 { 104 p->p_md.md_syscall = EMULNAME(syscall); 105 } 106 107 /* 108 * Process a system call. 109 * 110 * System calls are strange beasts. They are passed the syscall number 111 * in v0, and the arguments in the registers (as normal). They return 112 * an error flag in a3 (if a3 != 0 on return, the syscall had an error), 113 * and the return value (if any) in v0 and possibly v1. 114 */ 115 116 void 117 EMULNAME(syscall)(struct lwp *l, u_int status, u_int cause, vaddr_t pc) 118 { 119 struct proc *p = l->l_proc; 120 struct trapframe *tf = l->l_md.md_utf; 121 struct reg *reg = &tf->tf_registers; 122 mips_reg_t *fargs = ®->r_regs[_R_A0]; 123 register_t *args = NULL; 124 register_t copyargs[2+SYS_MAXSYSARGS]; 125 vaddr_t usp; 126 size_t nargs; 127 const struct sysent *callp; 128 int code, error; 129 #if defined(__mips_o32) 130 const int abi = _MIPS_BSD_API_O32; 131 KASSERTMSG(p->p_md.md_abi == abi, 132 "pid %d(%p): md_abi(%d) != abi(%d)", 133 p->p_pid, p, p->p_md.md_abi, abi); 134 size_t nregs = 4; 135 #else 136 const int abi = p->p_md.md_abi; 137 size_t nregs = _MIPS_SIM_NEWABI_P(abi) ? 8 : 4; 138 size_t i; 139 #endif 140 141 curcpu()->ci_data.cpu_nsyscall++; 142 143 if (cause & MIPS_CR_BR_DELAY) 144 reg->r_regs[_R_PC] = mips_emul_branch(tf, pc, 0, false); 145 else 146 reg->r_regs[_R_PC] = pc + sizeof(uint32_t); 147 148 callp = p->p_emul->e_sysent; 149 const mips_reg_t saved_v0 = reg->r_regs[_R_V0]; 150 code = saved_v0 - SYSCALL_SHIFT; 151 152 if (code == SYS_syscall 153 || (code == SYS___syscall && abi != _MIPS_BSD_API_O32)) { 154 /* 155 * Code is first argument, followed by actual args. 156 */ 157 code = *fargs++ - SYSCALL_SHIFT; 158 nregs--; 159 } else if (code == SYS___syscall) { 160 /* 161 * Like syscall, but code is a quad, so as to maintain 162 * quad alignment for the rest of the arguments. 163 */ 164 code = fargs[_QUAD_LOWWORD] - SYSCALL_SHIFT; 165 fargs += 2; 166 nregs -= 2; 167 } 168 169 if (code >= p->p_emul->e_nsysent) 170 callp += p->p_emul->e_nosys; 171 else 172 callp += code; 173 174 nargs = callp->sy_narg; 175 #if !defined(__mips_o32) 176 if (abi != _MIPS_BSD_API_O32) { 177 #endif 178 CTASSERT(sizeof(copyargs[0]) == sizeof(fargs[0])); 179 if (nargs <= nregs) { 180 /* 181 * Just use the trapframe for the source of arguments 182 */ 183 args = fargs; 184 } else { 185 const size_t nsaved = _MIPS_SIM_NEWABI_P(abi) ? 0 : 4; 186 KASSERT(nargs <= __arraycount(copyargs)); 187 args = copyargs; 188 /* 189 * Copy the arguments passed via register from the 190 * trapframe to our argument array 191 */ 192 memcpy(copyargs, fargs, nregs * sizeof(register_t)); 193 /* 194 * Start copying args skipping the register slots 195 * slots on the stack. 196 */ 197 usp = reg->r_regs[_R_SP] + nsaved * sizeof(register_t); 198 error = copyin((register_t *)usp, ©args[nregs], 199 (nargs - nregs) * sizeof(copyargs[0])); 200 if (error) 201 goto bad; 202 } 203 #if !defined(__mips_o32) 204 } else do { 205 /* 206 * The only difference between O32 and N32 is the calling 207 * sequence. If you make O32 208 */ 209 int32_t copy32args[SYS_MAXSYSARGS]; 210 int32_t *cargs = copy32args; 211 unsigned int arg64mask = SYCALL_ARG_64_MASK(callp); 212 bool doing_arg64; 213 size_t narg64 = SYCALL_NARGS64(callp); 214 /* 215 * All arguments are 32bits wide and 64bit arguments use 216 * two 32bit registers or stack slots. We need to remarshall 217 * them into 64bit slots 218 */ 219 args = copyargs; 220 CTASSERT(sizeof(copy32args[0]) != sizeof(fargs[0])); 221 222 /* 223 * If there are no 64bit arguments and all arguments were in 224 * registers, just use the trapframe for the source of arguments 225 */ 226 if (nargs <= nregs && narg64 == 0) { 227 args = fargs; 228 break; 229 } 230 231 if (nregs <= nargs + narg64) { 232 /* 233 * Grab the non-register arguments from the stack 234 * after skipping the slots for the 4 register passed 235 * arguments. 236 */ 237 usp = reg->r_regs[_R_SP] + 4 * sizeof(int32_t); 238 error = copyin((int32_t *)usp, copy32args, 239 (nargs + narg64 - nregs) * sizeof(copy32args[0])); 240 if (error) 241 goto bad; 242 } 243 /* 244 * Copy all the arguments to copyargs, starting with the ones 245 * in registers. Using the hints in the 64bit argmask, 246 * we marshall the passed 32bit values into 64bit slots. If we 247 * encounter a 64 bit argument, we grab two adjacent 32bit 248 * values and synthesize the 64bit argument. 249 */ 250 for (i = 0, doing_arg64 = false; i < nargs + narg64;) { 251 register_t arg; 252 if (nregs > 0) { 253 arg = (int32_t) *fargs++; 254 nregs--; 255 } else { 256 arg = *cargs++; 257 } 258 if (__predict_true((arg64mask & 1) == 0)) { 259 /* 260 * Just copy it with sign extension on 261 */ 262 copyargs[i++] = (int32_t) arg; 263 arg64mask >>= 1; 264 continue; 265 } 266 /* 267 * 64bit arg. grab the low 32 bits, discard the high. 268 */ 269 arg = (uint32_t)arg; 270 if (!doing_arg64) { 271 /* 272 * Pick up the 1st word of a 64bit arg. 273 * If lowword == 1 then highword == 0, 274 * so this is the highword and thus 275 * shifted left by 32, otherwise 276 * lowword == 0 and highword == 1 so 277 * it isn't shifted at all. Remember 278 * we still need another word. 279 */ 280 doing_arg64 = true; 281 copyargs[i] = arg << (_QUAD_LOWWORD*32); 282 narg64--; /* one less 64bit arg */ 283 } else { 284 /* 285 * Pick up the 2nd word of a 64bit arg. 286 * if highword == 1, it's shifted left 287 * by 32, otherwise lowword == 1 and 288 * highword == 0 so it isn't shifted at 289 * all. And now head to the next argument. 290 */ 291 doing_arg64 = false; 292 copyargs[i++] |= arg << (_QUAD_HIGHWORD*32); 293 arg64mask >>= 1; 294 } 295 } 296 } while (/*CONSTCOND*/ 0); /* avoid a goto */ 297 #endif 298 299 #ifdef MIPS_SYSCALL_DEBUG 300 if (p->p_emul->e_syscallnames) 301 printf("syscall %s:", p->p_emul->e_syscallnames[code]); 302 else 303 printf("syscall %u:", code); 304 if (nargs == 0) 305 printf(" <no args>"); 306 else for (size_t j = 0; j < nargs; j++) { 307 if (j == nregs) printf(" *"); 308 printf(" [%s%zu]=%#"PRIxREGISTER, 309 SYCALL_ARG_64_P(callp, j) ? "+" : "", 310 j, args[j]); 311 } 312 printf("\n"); 313 #endif 314 315 error = sy_invoke(callp, l, args, ®->r_regs[_R_V0], code); 316 317 switch (error) { 318 case 0: 319 #if !defined(__mips_o32) 320 if (abi == _MIPS_BSD_API_O32 && SYCALL_RET_64_P(callp)) { 321 /* 322 * If this is from O32 and it's a 64bit quantity, 323 * split it into 2 32bit values in adjacent registers. 324 */ 325 mips_reg_t tmp = reg->r_regs[_R_V0]; 326 reg->r_regs[_R_V0 + _QUAD_LOWWORD] = (int32_t) tmp; 327 reg->r_regs[_R_V0 + _QUAD_HIGHWORD] = tmp >> 32; 328 } 329 #endif 330 #ifdef MIPS_SYSCALL_DEBUG 331 if (p->p_emul->e_syscallnames) 332 printf("syscall %s:", p->p_emul->e_syscallnames[code]); 333 else 334 printf("syscall %u:", code); 335 printf(" return v0=%#"PRIxREGISTER" v1=%#"PRIxREGISTER"\n", 336 reg->r_regs[_R_V0], reg->r_regs[_R_V1]); 337 #endif 338 reg->r_regs[_R_A3] = 0; 339 break; 340 case ERESTART: 341 reg->r_regs[_R_V0] = saved_v0; /* restore syscall code */ 342 reg->r_regs[_R_PC] = pc; 343 break; 344 case EJUSTRETURN: 345 break; /* nothing to do */ 346 default: 347 bad: 348 if (p->p_emul->e_errno) 349 error = p->p_emul->e_errno[error]; 350 reg->r_regs[_R_V0] = error; 351 reg->r_regs[_R_A3] = 1; 352 #ifdef MIPS_SYSCALL_DEBUG 353 if (p->p_emul->e_syscallnames) 354 printf("syscall %s:", p->p_emul->e_syscallnames[code]); 355 else 356 printf("syscall %u:", code); 357 printf(" return error=%d\n", error); 358 #endif 359 break; 360 } 361 362 KASSERT(l->l_blcnt == 0); 363 KASSERT(curcpu()->ci_biglock_count == 0); 364 365 userret(l); 366 } 367