#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: syscall.c,v 1.27 2023/10/05 19:41:06 ad Exp $");
#include "opt_multiprocessor.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/cpu.h>
#include <sys/ktrace.h>
#include <sys/proc.h>
#include <sys/syscall.h>
#include <sys/syscallvar.h>
#include <machine/userret.h>
#ifdef TRAPDEBUG
int startsysc = 0;
#define TDB(a) if (startsysc) printf a
#else
#define TDB(a)
#endif
void syscall(struct trapframe *);
void
syscall_intern(struct proc *p)
{
p->p_trace_enabled = trace_is_enabled(p);
p->p_md.md_syscall = syscall;
}
void
syscall(struct trapframe *tf)
{
int error;
int rval[2];
int args[2+SYS_MAXSYSARGS];
struct lwp * const l = curlwp;
struct proc * const p = l->l_proc;
const struct emul * const emul = p->p_emul;
const struct sysent *callp = emul->e_sysent;
const u_quad_t oticks = p->p_sticks;
TDB(("trap syscall %s pc %lx, psl %lx, sp %lx, pid %d, frame %p\n",
syscallnames[tf->tf_code], tf->tf_pc, tf->tf_psl,tf->tf_sp,
p->p_pid,tf));
curcpu()->ci_data.cpu_nsyscall++;
l->l_md.md_utf = tf;
if ((unsigned long) tf->tf_code >= emul->e_nsysent)
callp += emul->e_nosys;
else
callp += tf->tf_code;
rval[0] = 0;
rval[1] = tf->tf_r1;
if (callp->sy_narg) {
error = copyin((char*)tf->tf_ap + 4, args, callp->sy_argsize);
if (error)
goto bad;
}
error = sy_invoke(callp, curlwp, args, rval, tf->tf_code);
TDB(("return %s pc %lx, psl %lx, sp %lx, pid %d, err %d r0 %d, r1 %d, "
"tf %p\n", syscallnames[tf->tf_code], tf->tf_pc, tf->tf_psl,
tf->tf_sp, p->p_pid, error, rval[0], rval[1], tf));
bad:
switch (error) {
case 0:
tf->tf_r1 = rval[1];
tf->tf_r0 = rval[0];
tf->tf_psl &= ~PSL_C;
break;
case EJUSTRETURN:
break;
case ERESTART:
tf->tf_pc -= (tf->tf_code > 63 ? 4 : 2);
break;
default:
tf->tf_r0 = error;
tf->tf_psl |= PSL_C;
break;
}
userret(l, tf, oticks);
}
void
md_child_return(struct lwp *l)
{
userret(l, l->l_md.md_utf, 0);
}
void
cpu_spawn_return(struct lwp *l)
{
userret(l, l->l_md.md_utf, 0);
}