void syscall(void) { lwp_t *l = curlwp; const struct proc * const p = l->l_proc; const struct sysent *callp; struct pcb *pcb = lwp_getpcb(l); ucontext_t *ucp = &pcb->pcb_userret_ucp; register_t copyargs[2+SYS_MAXSYSARGS]; register_t *args; register_t rval[2]; uint32_t code, opcode; uint nargs, argsize; int error; /* system call accounting */ curcpu()->ci_data.cpu_nsyscall++; LWP_CACHE_CREDS(l, l->l_proc); /* XXX do we want do do emulation? */ md_syscall_get_opcode(ucp, &opcode); md_syscall_get_syscallnumber(ucp, &code); code &= (SYS_NSYSENT -1); callp = p->p_emul->e_sysent + code; nargs = callp->sy_narg; argsize = callp->sy_argsize; args = copyargs; rval[0] = rval[1] = 0; error = md_syscall_getargs(l, ucp, nargs, argsize, args); #if 0 aprint_debug("syscall no. %d, ", code); aprint_debug("nargs %d, argsize %d => ", nargs, argsize); thunk_printf_debug("syscall no. %d, ", code); thunk_printf_debug("nargs %d, argsize %d => ", nargs, argsize); #endif /* * TODO change the pre and post printing into functions so they can be * easily adjusted and dont clobber up this space */ if (!error) syscall_args_print(l, code, nargs, argsize, args); md_syscall_inc_pc(ucp, opcode); if (!error) { error = sy_invoke(callp, l, args, rval, code); } syscall_retvals_print(l, curlwp, code, nargs, args, error, rval); //out: switch (error) { default: /* fall trough */ case 0: md_syscall_set_returnargs(l, ucp, error, rval); /* fall trough */ case EJUSTRETURN: break; case ERESTART: md_syscall_dec_pc(ucp, opcode); /* nothing to do */ break; } //thunk_printf_debug("end of syscall : return to userland\n"); //if (code != 4) thunk_printf("userret() code %d\n", code); }
void EMULNAME(syscall)(struct lwp *l, u_int status, u_int cause, vaddr_t pc) { struct proc *p = l->l_proc; struct trapframe *tf = l->l_md.md_utf; struct reg *reg = &tf->tf_registers; mips_reg_t *fargs = ®->r_regs[_R_A0]; register_t *args = NULL; register_t copyargs[2+SYS_MAXSYSARGS]; vaddr_t usp; size_t nargs; const struct sysent *callp; int code, error; #if defined(__mips_o32) const int abi = _MIPS_BSD_API_O32; KASSERTMSG(p->p_md.md_abi == abi, "pid %d(%p): md_abi(%d) != abi(%d)", p->p_pid, p, p->p_md.md_abi, abi); size_t nregs = 4; #else const int abi = p->p_md.md_abi; size_t nregs = _MIPS_SIM_NEWABI_P(abi) ? 8 : 4; size_t i; #endif LWP_CACHE_CREDS(l, p); curcpu()->ci_data.cpu_nsyscall++; if (cause & MIPS_CR_BR_DELAY) reg->r_regs[_R_PC] = mips_emul_branch(tf, pc, 0, false); else reg->r_regs[_R_PC] = pc + sizeof(uint32_t); callp = p->p_emul->e_sysent; const mips_reg_t saved_v0 = reg->r_regs[_R_V0]; code = saved_v0 - SYSCALL_SHIFT; if (code == SYS_syscall || (code == SYS___syscall && abi != _MIPS_BSD_API_O32)) { /* * Code is first argument, followed by actual args. */ code = *fargs++ - SYSCALL_SHIFT; nregs--; } else if (code == SYS___syscall) { /* * Like syscall, but code is a quad, so as to maintain * quad alignment for the rest of the arguments. */ code = fargs[_QUAD_LOWWORD] - SYSCALL_SHIFT; fargs += 2; nregs -= 2; } if (code >= p->p_emul->e_nsysent) callp += p->p_emul->e_nosys; else callp += code; nargs = callp->sy_narg; #if !defined(__mips_o32) if (abi != _MIPS_BSD_API_O32) { #endif CTASSERT(sizeof(copyargs[0]) == sizeof(fargs[0])); if (nargs <= nregs) { /* * Just use the trapframe for the source of arguments */ args = fargs; } else { const size_t nsaved = _MIPS_SIM_NEWABI_P(abi) ? 0 : 4; KASSERT(nargs <= __arraycount(copyargs)); args = copyargs; /* * Copy the arguments passed via register from the * trapframe to our argument array */ memcpy(copyargs, fargs, nregs * sizeof(register_t)); /* * Start copying args skipping the register slots * slots on the stack. */ usp = reg->r_regs[_R_SP] + nsaved*sizeof(register_t); error = copyin((register_t *)usp, ©args[nregs], (nargs - nregs) * sizeof(copyargs[0])); if (error) goto bad; } #if !defined(__mips_o32) } else do { /* * The only difference between O32 and N32 is the calling * sequence. If you make O32 */ int32_t copy32args[SYS_MAXSYSARGS]; int32_t *cargs = copy32args; unsigned int arg64mask = SYCALL_ARG_64_MASK(callp); bool doing_arg64; size_t narg64 = SYCALL_NARGS64(callp); /* * All arguments are 32bits wide and 64bit arguments use * two 32bit registers or stack slots. We need to remarshall * them into 64bit slots */ args = copyargs; CTASSERT(sizeof(copy32args[0]) != sizeof(fargs[0])); /* * If there are no 64bit arguments and all arguments were in * registers, just use the trapframe for the source of arguments */ if (nargs <= nregs && narg64 == 0) { args = fargs; break; } if (nregs <= nargs + narg64) { /* * Grab the non-register arguments from the stack * after skipping the slots for the 4 register passed * arguments. */ usp = reg->r_regs[_R_SP] + 4*sizeof(int32_t); error = copyin((int32_t *)usp, copy32args, (nargs + narg64 - nregs) * sizeof(copy32args[0])); if (error) goto bad; } /* * Copy all the arguments to copyargs, starting with the ones * in registers. Using the hints in the 64bit argmask, * we marshall the passed 32bit values into 64bit slots. If we * encounter a 64 bit argument, we grab two adjacent 32bit * values and synthesize the 64bit argument. */ for (i = 0, doing_arg64 = false; i < nargs + narg64;) { register_t arg; if (nregs > 0) { arg = (int32_t) *fargs++; nregs--; } else { arg = *cargs++; } if (__predict_true((arg64mask & 1) == 0)) { /* * Just copy it with sign extension on */ copyargs[i++] = (int32_t) arg; arg64mask >>= 1; continue; } /* * 64bit arg. grab the low 32 bits, discard the high. */ arg = (uint32_t)arg; if (!doing_arg64) { /* * Pick up the 1st word of a 64bit arg. * If lowword == 1 then highword == 0, * so this is the highword and thus * shifted left by 32, otherwise * lowword == 0 and highword == 1 so * it isn't shifted at all. Remember * we still need another word. */ doing_arg64 = true; copyargs[i] = arg << (_QUAD_LOWWORD*32); narg64--; /* one less 64bit arg */ } else { /* * Pick up the 2nd word of a 64bit arg. * if highword == 1, it's shifted left * by 32, otherwise lowword == 1 and * highword == 0 so it isn't shifted at * all. And now head to the next argument. */ doing_arg64 = false; copyargs[i++] |= arg << (_QUAD_HIGHWORD*32); arg64mask >>= 1; } } } while (/*CONSTCOND*/ 0); /* avoid a goto */ #endif #ifdef MIPS_SYSCALL_DEBUG if (p->p_emul->e_syscallnames) printf("syscall %s:", p->p_emul->e_syscallnames[code]); else printf("syscall %u:", code); if (nargs == 0) printf(" <no args>"); else for (size_t j = 0; j < nargs; j++) { if (j == nregs) printf(" *"); printf(" [%s%zu]=%#"PRIxREGISTER, SYCALL_ARG_64_P(callp, j) ? "+" : "", j, args[j]); } printf("\n"); #endif error = sy_invoke(callp, l, args, ®->r_regs[_R_V0], code); switch (error) { case 0: #if !defined(__mips_o32) if (abi == _MIPS_BSD_API_O32 && SYCALL_RET_64_P(callp)) { /* * If this is from O32 and it's a 64bit quantity, * split it into 2 32bit values in adjacent registers. */ mips_reg_t tmp = reg->r_regs[_R_V0]; reg->r_regs[_R_V0 + _QUAD_LOWWORD] = (int32_t) tmp; reg->r_regs[_R_V0 + _QUAD_HIGHWORD] = tmp >> 32; } #endif #ifdef MIPS_SYSCALL_DEBUG if (p->p_emul->e_syscallnames) printf("syscall %s:", p->p_emul->e_syscallnames[code]); else printf("syscall %u:", code); printf(" return v0=%#"PRIxREGISTER" v1=%#"PRIxREGISTER"\n", reg->r_regs[_R_V0], reg->r_regs[_R_V1]); #endif reg->r_regs[_R_A3] = 0; break; case ERESTART: reg->r_regs[_R_V0] = saved_v0; /* restore syscall code */ reg->r_regs[_R_PC] = pc; break; case EJUSTRETURN: break; /* nothing to do */ default: bad: if (p->p_emul->e_errno) error = p->p_emul->e_errno[error]; reg->r_regs[_R_V0] = error; reg->r_regs[_R_A3] = 1; #ifdef MIPS_SYSCALL_DEBUG if (p->p_emul->e_syscallnames) printf("syscall %s:", p->p_emul->e_syscallnames[code]); else printf("syscall %u:", code); printf(" return error=%d\n", error); #endif break; }
/* * System call request from POSIX system call gate interface to kernel. * l ... curlwp when trap occurs. * tf ... full user context. */ static void syscall(struct lwp *l, struct trapframe *tf) { struct proc *p = l->l_proc; void *params; const struct sysent *callp; int error, opc, nsys; size_t argsize; register_t code, args[8], rval[2], ocode; curcpu()->ci_data.cpu_nsyscall++; opc = tf->tf_spc; ocode = code = tf->tf_r0; nsys = p->p_emul->e_nsysent; callp = p->p_emul->e_sysent; params = (void *)tf->tf_r15; switch (code) { case SYS_syscall: /* * Code is first argument, followed by actual args. */ code = tf->tf_r4; /* fuword(params); */ /* params += sizeof(int); */ break; case SYS___syscall: /* * Like syscall, but code is a quad, so as to maintain * quad alignment for the rest of the arguments. */ if (callp != sysent) break; /* fuword(params + _QUAD_LOWWORD * sizeof(int)); */ #if _BYTE_ORDER == BIG_ENDIAN code = tf->tf_r5; #else code = tf->tf_r4; #endif /* params += sizeof(quad_t); */ break; default: break; } if (code < 0 || code >= nsys) callp += p->p_emul->e_nosys; /* illegal */ else callp += code; argsize = callp->sy_argsize; if (ocode == SYS_syscall) { if (argsize) { args[0] = tf->tf_r5; args[1] = tf->tf_r6; args[2] = tf->tf_r7; if (argsize > 3 * sizeof(int)) { argsize -= 3 * sizeof(int); error = copyin(params, (void *)&args[3], argsize); } else error = 0; } else error = 0; } else if (ocode == SYS___syscall) { if (argsize) { args[0] = tf->tf_r6; args[1] = tf->tf_r7; if (argsize > 2 * sizeof(int)) { argsize -= 2 * sizeof(int); error = copyin(params, (void *)&args[2], argsize); } else error = 0; } else error = 0; } else { if (argsize) { args[0] = tf->tf_r4; args[1] = tf->tf_r5; args[2] = tf->tf_r6; args[3] = tf->tf_r7; if (argsize > 4 * sizeof(int)) { argsize -= 4 * sizeof(int); error = copyin(params, (void *)&args[4], argsize); } else error = 0; } else error = 0; } if (error) goto bad; rval[0] = 0; rval[1] = tf->tf_r1; error = sy_invoke(callp, l, args, rval, code); switch (error) { case 0: tf->tf_r0 = rval[0]; tf->tf_r1 = rval[1]; tf->tf_ssr |= PSL_TBIT; /* T bit */ break; case ERESTART: /* 2 = TRAPA instruction size */ tf->tf_spc = opc - 2; break; case EJUSTRETURN: /* nothing to do */ break; default: bad: if (p->p_emul->e_errno) error = p->p_emul->e_errno[error]; tf->tf_r0 = error; tf->tf_ssr &= ~PSL_TBIT; /* T bit */ break; } userret(l); }
void EMULNAME(syscall)(struct trapframe *tf) { struct lwp * const l = curlwp; struct proc * const p = l->l_proc; const struct sysent *callp; size_t argsize; register_t code; register_t realcode; register_t *params, rval[2]; register_t args[10]; int error; int n; LWP_CACHE_CREDS(l, p); curcpu()->ci_ev_scalls.ev_count++; code = tf->tf_fixreg[0]; params = tf->tf_fixreg + FIRSTARG; n = NARGREG; realcode = code; { switch (code) { case EMULNAMEU(SYS_syscall): /* * code is first argument, * followed by actual args. */ code = *params++; n -= 1; break; #if !defined(COMPAT_LINUX) case EMULNAMEU(SYS___syscall): params++; code = *params++; n -= 2; break; #endif default: break; } code &= EMULNAMEU(SYS_NSYSENT) - 1; callp = p->p_emul->e_sysent + code; realcode = code; } argsize = callp->sy_argsize; if (argsize > n * sizeof(register_t)) { memcpy(args, params, n * sizeof(register_t)); error = copyin(MOREARGS(tf->tf_fixreg[1]), args + n, argsize - n * sizeof(register_t)); if (error) goto bad; params = args; } error = sy_invoke(callp, l, params, rval, code); if (__predict_true(error == 0)) { tf->tf_fixreg[FIRSTARG] = rval[0]; tf->tf_fixreg[FIRSTARG + 1] = rval[1]; tf->tf_cr &= ~0x10000000; } else { switch (error) { case ERESTART: /* * Set user's pc back to redo the system call. */ tf->tf_srr0 -= 4; break; case EJUSTRETURN: /* nothing to do */ break; default: bad: if (p->p_emul->e_errno) error = p->p_emul->e_errno[error]; tf->tf_fixreg[FIRSTARG] = error; tf->tf_cr |= 0x10000000; break; } } userret(l, tf); }