xref: /netbsd-src/sys/compat/freebsd/freebsd_syscall.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: freebsd_syscall.c,v 1.6 2019/04/06 17:42:28 kre 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.6 2019/04/06 17:42:28 kre 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 		error = ufetch_long((void *)params, (u_long *)&code);
92 		if (error)
93 			goto bad;
94 		params += sizeof(int);
95 		break;
96 	case SYS___syscall:
97 		/*
98 		 * Like syscall, but code is a quad, so as to maintain
99 		 * quad alignment for the rest of the arguments.
100 		 */
101 		error = ufetch_long((void *)(params +
102 					     _QUAD_LOWWORD * sizeof(int)),
103 				    (u_long *)&code);
104 		if (error)
105 			goto bad;
106 		params += sizeof(quad_t);
107 		break;
108 	default:
109 		break;
110 	}
111 
112 	code &= (SYS_NSYSENT - 1);
113 	callp += code;
114 	argsize = callp->sy_argsize;
115 	if (argsize) {
116 		error = copyin(params, (void *)args, argsize);
117 		if (error)
118 			goto bad;
119 	}
120 
121 	if (!__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_entry))
122 	    || (error = trace_enter(code, callp, args)) == 0) {
123 		rval[0] = 0;
124 		rval[1] = frame->tf_edx; /* need to keep edx for shared FreeBSD bins */
125 		error = sy_call(callp, l, args, rval);
126 	}
127 
128 	switch (error) {
129 	case 0:
130 		frame->tf_eax = rval[0];
131 		frame->tf_edx = rval[1];
132 		frame->tf_eflags &= ~PSL_C;	/* carry bit */
133 		break;
134 	case ERESTART:
135 		/*
136 		 * The offset to adjust the PC by depends on whether we entered
137 		 * the kernel through the trap or call gate.  We pushed the
138 		 * size of the instruction into tf_err on entry.
139 		 */
140 		frame->tf_eip -= frame->tf_err;
141 		break;
142 	case EJUSTRETURN:
143 		/* nothing to do */
144 		break;
145 	default:
146 	bad:
147 		frame->tf_eax = error;
148 		frame->tf_eflags |= PSL_C;	/* carry bit */
149 		break;
150 	}
151 
152 	if (__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_return)))
153 		trace_exit(code, callp, args, rval, error);
154 
155 	userret(l);
156 }
157