xref: /netbsd-src/sys/compat/freebsd/freebsd_syscall.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: freebsd_syscall.c,v 1.2 2017/08/08 08:04:06 maxv 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: freebsd_syscall.c,v 1.2 2017/08/08 08:04:06 maxv Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <sys/signal.h>
39 #include <sys/syscall.h>
40 #include <sys/syscallvar.h>
41 
42 #include <uvm/uvm_extern.h>
43 
44 #include <machine/cpu.h>
45 #include <machine/psl.h>
46 #include <machine/userret.h>
47 
48 #include <compat/sys/signal.h>
49 
50 #include <compat/freebsd/freebsd_machdep.h>
51 #include <compat/freebsd/freebsd_syscall.h>
52 
53 void freebsd_syscall(struct trapframe *);
54 
55 void
56 freebsd_syscall_intern(struct proc *p)
57 {
58 
59 	p->p_md.md_syscall = freebsd_syscall;
60 }
61 
62 /*
63  * syscall(frame):
64  *	System call request from POSIX system call gate interface to kernel.
65  * Like trap(), argument is call by reference.
66  */
67 void
68 freebsd_syscall(struct trapframe *frame)
69 {
70 	char *params;
71 	const struct sysent *callp;
72 	struct lwp *l;
73 	struct proc *p;
74 	int error;
75 	size_t argsize;
76 	register_t code, args[8], rval[2];
77 
78 	l = curlwp;
79 	p = l->l_proc;
80 	LWP_CACHE_CREDS(l, p);
81 
82 	code = frame->tf_eax;
83 	callp = p->p_emul->e_sysent;
84 	params = (char *)frame->tf_esp + sizeof(int);
85 
86 	switch (code) {
87 	case SYS_syscall:
88 		/*
89 		 * Code is first argument, followed by actual args.
90 		 */
91 		code = fuword(params);
92 		params += sizeof(int);
93 		break;
94 	case SYS___syscall:
95 		/*
96 		 * Like syscall, but code is a quad, so as to maintain
97 		 * quad alignment for the rest of the arguments.
98 		 */
99 		code = fuword(params + _QUAD_LOWWORD * sizeof(int));
100 		params += sizeof(quad_t);
101 		break;
102 	default:
103 		break;
104 	}
105 
106 	code &= (SYS_NSYSENT - 1);
107 	callp += code;
108 	argsize = callp->sy_argsize;
109 	if (argsize) {
110 		error = copyin(params, (void *)args, argsize);
111 		if (error)
112 			goto bad;
113 	}
114 
115 	if (!__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_entry))
116 	    || (error = trace_enter(code, callp, args)) == 0) {
117 		rval[0] = 0;
118 		rval[1] = frame->tf_edx; /* need to keep edx for shared FreeBSD bins */
119 		error = sy_call(callp, l, args, rval);
120 	}
121 
122 	switch (error) {
123 	case 0:
124 		frame->tf_eax = rval[0];
125 		frame->tf_edx = rval[1];
126 		frame->tf_eflags &= ~PSL_C;	/* carry bit */
127 		break;
128 	case ERESTART:
129 		/*
130 		 * The offset to adjust the PC by depends on whether we entered
131 		 * the kernel through the trap or call gate.  We pushed the
132 		 * size of the instruction into tf_err on entry.
133 		 */
134 		frame->tf_eip -= frame->tf_err;
135 		break;
136 	case EJUSTRETURN:
137 		/* nothing to do */
138 		break;
139 	default:
140 	bad:
141 		frame->tf_eax = error;
142 		frame->tf_eflags |= PSL_C;	/* carry bit */
143 		break;
144 	}
145 
146 	if (__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_return)))
147 		trace_exit(code, callp, args, rval, error);
148 
149 	userret(l);
150 }
151