Home | History | Annotate | Line # | Download | only in amd64
      1 /*	$NetBSD: netbsd32_syscall.c,v 1.34 2023/10/05 19:41:03 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * 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 #if defined(_KERNEL) && defined(_KERNEL_OPT)
     33 #include "opt_dtrace.h"
     34 #endif
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: netbsd32_syscall.c,v 1.34 2023/10/05 19:41:03 ad Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/proc.h>
     42 #include <sys/signal.h>
     43 /* XXX this file ought to include the netbsd32 version of these 2 headers */
     44 #include <sys/syscall.h>
     45 #include <sys/syscallvar.h>
     46 #include <sys/syscallargs.h>
     47 #include <sys/syscall_stats.h>
     48 
     49 #include <machine/cpu.h>
     50 #include <machine/psl.h>
     51 #include <machine/userret.h>
     52 
     53 void netbsd32_syscall_intern(struct proc *);
     54 static void netbsd32_syscall(struct trapframe *);
     55 
     56 void
     57 netbsd32_syscall_intern(struct proc *p)
     58 {
     59 
     60 	p->p_md.md_syscall = netbsd32_syscall;
     61 }
     62 
     63 void
     64 netbsd32_syscall(struct trapframe *frame)
     65 {
     66 	char *params;
     67 	const struct sysent *callp;
     68 	struct proc *p;
     69 	struct lwp *l;
     70 	int error;
     71 	int i;
     72 	register32_t code, args[2 + SYS_MAXSYSARGS];
     73 	register_t rval[2];
     74 	register_t args64[SYS_MAXSYSARGS];
     75 
     76 	l = curlwp;
     77 	p = l->l_proc;
     78 
     79 	code = frame->tf_rax & (SYS_NSYSENT - 1);
     80 	callp = p->p_emul->e_sysent + code;
     81 
     82 	SYSCALL_COUNT(syscall_counts, code);
     83 	SYSCALL_TIME_SYS_ENTRY(l, syscall_times, code);
     84 
     85 	params = (char *)frame->tf_rsp + sizeof(int);
     86 
     87 	if (callp->sy_argsize) {
     88 		error = copyin(params, args, callp->sy_argsize);
     89 		if (__predict_false(error != 0))
     90 			goto bad;
     91 	}
     92 
     93 	if (__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_entry))
     94 	    && !__predict_false(callp->sy_flags & SYCALL_INDIRECT)) {
     95 		int narg = callp->sy_argsize >> 2;
     96 		for (i = 0; i < narg; i++)
     97 			args64[i] = args[i];
     98 		error = trace_enter(code, callp, args64);
     99 		if (__predict_false(error != 0))
    100 			goto out;
    101 	}
    102 
    103 	rval[0] = 0;
    104 	rval[1] = 0;
    105 	error = sy_call(callp, l, args, rval);
    106 
    107 out:
    108 	if (__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_return))
    109 	    && !__predict_false(callp->sy_flags & SYCALL_INDIRECT)) {
    110 		int narg = callp->sy_argsize >> 2;
    111 		for (i = 0; i < narg; i++)
    112 			args64[i] = args[i];
    113 		trace_exit(code, callp, args64, rval, error);
    114 	}
    115 
    116 	if (__predict_true(error == 0)) {
    117 		frame->tf_rax = rval[0];
    118 		frame->tf_rdx = rval[1];
    119 		frame->tf_rflags &= ~PSL_C;	/* carry bit */
    120 	} else {
    121 		switch (error) {
    122 		case ERESTART:
    123 			/*
    124 			 * The offset to adjust the PC by depends on whether we
    125 			 * entered the kernel through the trap or call gate.
    126 			 * We saved the instruction size in tf_err on entry.
    127 			 */
    128 			frame->tf_rip -= frame->tf_err;
    129 			break;
    130 		case EJUSTRETURN:
    131 			/* nothing to do */
    132 			break;
    133 		default:
    134 		bad:
    135 			frame->tf_rax = error;
    136 			frame->tf_rflags |= PSL_C;	/* carry bit */
    137 			break;
    138 		}
    139 	}
    140 
    141 	SYSCALL_TIME_SYS_EXIT(l);
    142 	userret(l);
    143 }
    144