xref: /netbsd/sys/arch/amd64/amd64/netbsd32_syscall.c (revision 6550d01e)
1 /*	$NetBSD: netbsd32_syscall.c,v 1.31 2010/12/20 00:25:24 matt 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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: netbsd32_syscall.c,v 1.31 2010/12/20 00:25:24 matt Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <sys/signal.h>
39 /* XXX this file ought to include the netbsd32 version of these 2 headers */
40 #include <sys/syscall.h>
41 #include <sys/syscallvar.h>
42 #include <sys/syscallargs.h>
43 #include <sys/syscall_stats.h>
44 
45 #include <machine/cpu.h>
46 #include <machine/psl.h>
47 #include <machine/userret.h>
48 
49 void netbsd32_syscall_intern(struct proc *);
50 static void netbsd32_syscall(struct trapframe *);
51 
52 void
53 netbsd32_syscall_intern(struct proc *p)
54 {
55 
56 	p->p_md.md_syscall = netbsd32_syscall;
57 }
58 
59 void
60 netbsd32_syscall(struct trapframe *frame)
61 {
62 	char *params;
63 	const struct sysent *callp;
64 	struct proc *p;
65 	struct lwp *l;
66 	int error;
67 	int i;
68 	register32_t code, args[2 + SYS_MAXSYSARGS];
69 	register_t rval[2];
70 	register_t args64[SYS_MAXSYSARGS];
71 
72 	l = curlwp;
73 	p = l->l_proc;
74 
75 	code = frame->tf_rax & (SYS_NSYSENT - 1);
76 	callp = p->p_emul->e_sysent + code;
77 
78 	LWP_CACHE_CREDS(l, p);
79 
80 	SYSCALL_COUNT(syscall_counts, code);
81 	SYSCALL_TIME_SYS_ENTRY(l, syscall_times, code);
82 
83 	params = (char *)frame->tf_rsp + sizeof(int);
84 
85 	if (callp->sy_argsize) {
86 		error = copyin(params, args, callp->sy_argsize);
87 		if (__predict_false(error != 0))
88 			goto bad;
89 		/* Recover 'code' - not in a register */
90 		code = frame->tf_rax & (SYS_NSYSENT - 1);
91 	}
92 
93 	if (__predict_false(p->p_trace_enabled)
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, args64, narg);
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)
109 	    && !__predict_false(callp->sy_flags & SYCALL_INDIRECT)) {
110 		/* Recover 'code' - the compiler doesn't assign it a register */
111 		code = frame->tf_rax & (SYS_NSYSENT - 1);
112 		trace_exit(code, rval, error);
113 	}
114 
115 	if (__predict_true(error == 0)) {
116 		frame->tf_rax = rval[0];
117 		frame->tf_rdx = rval[1];
118 		frame->tf_rflags &= ~PSL_C;	/* carry bit */
119 	} else {
120 		switch (error) {
121 		case ERESTART:
122 			/*
123 			 * The offset to adjust the PC by depends on whether we
124 			 * entered the kernel through the trap or call gate.
125 			 * We saved the instruction size in tf_err on entry.
126 			 */
127 			frame->tf_rip -= frame->tf_err;
128 			break;
129 		case EJUSTRETURN:
130 			/* nothing to do */
131 			break;
132 		default:
133 		bad:
134 			frame->tf_rax = error;
135 			frame->tf_rflags |= PSL_C;	/* carry bit */
136 			break;
137 		}
138 	}
139 
140 	SYSCALL_TIME_SYS_EXIT(l);
141 	userret(l);
142 }
143