freebsd_syscall.c revision 1.2.2.2 1 1.2.2.2 skrll /* $NetBSD: freebsd_syscall.c,v 1.2.2.2 2017/08/28 17:51:58 skrll Exp $ */
2 1.2.2.2 skrll
3 1.2.2.2 skrll /*-
4 1.2.2.2 skrll * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 1.2.2.2 skrll * All rights reserved.
6 1.2.2.2 skrll *
7 1.2.2.2 skrll * This code is derived from software contributed to The NetBSD Foundation
8 1.2.2.2 skrll * by Charles M. Hannum.
9 1.2.2.2 skrll *
10 1.2.2.2 skrll * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 skrll * modification, are permitted provided that the following conditions
12 1.2.2.2 skrll * are met:
13 1.2.2.2 skrll * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 skrll * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 skrll * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 skrll * documentation and/or other materials provided with the distribution.
18 1.2.2.2 skrll *
19 1.2.2.2 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.2.2 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.2.2 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.2.2 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.2.2 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.2.2 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.2.2 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.2.2 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.2.2 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.2.2 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.2.2 skrll * POSSIBILITY OF SUCH DAMAGE.
30 1.2.2.2 skrll */
31 1.2.2.2 skrll
32 1.2.2.2 skrll #include <sys/cdefs.h>
33 1.2.2.2 skrll __KERNEL_RCSID(0, "$NetBSD: freebsd_syscall.c,v 1.2.2.2 2017/08/28 17:51:58 skrll Exp $");
34 1.2.2.2 skrll
35 1.2.2.2 skrll #include <sys/param.h>
36 1.2.2.2 skrll #include <sys/systm.h>
37 1.2.2.2 skrll #include <sys/proc.h>
38 1.2.2.2 skrll #include <sys/signal.h>
39 1.2.2.2 skrll #include <sys/syscall.h>
40 1.2.2.2 skrll #include <sys/syscallvar.h>
41 1.2.2.2 skrll
42 1.2.2.2 skrll #include <uvm/uvm_extern.h>
43 1.2.2.2 skrll
44 1.2.2.2 skrll #include <machine/cpu.h>
45 1.2.2.2 skrll #include <machine/psl.h>
46 1.2.2.2 skrll #include <machine/userret.h>
47 1.2.2.2 skrll
48 1.2.2.2 skrll #include <compat/sys/signal.h>
49 1.2.2.2 skrll
50 1.2.2.2 skrll #include <compat/freebsd/freebsd_machdep.h>
51 1.2.2.2 skrll #include <compat/freebsd/freebsd_syscall.h>
52 1.2.2.2 skrll
53 1.2.2.2 skrll void freebsd_syscall(struct trapframe *);
54 1.2.2.2 skrll
55 1.2.2.2 skrll void
56 1.2.2.2 skrll freebsd_syscall_intern(struct proc *p)
57 1.2.2.2 skrll {
58 1.2.2.2 skrll
59 1.2.2.2 skrll p->p_md.md_syscall = freebsd_syscall;
60 1.2.2.2 skrll }
61 1.2.2.2 skrll
62 1.2.2.2 skrll /*
63 1.2.2.2 skrll * syscall(frame):
64 1.2.2.2 skrll * System call request from POSIX system call gate interface to kernel.
65 1.2.2.2 skrll * Like trap(), argument is call by reference.
66 1.2.2.2 skrll */
67 1.2.2.2 skrll void
68 1.2.2.2 skrll freebsd_syscall(struct trapframe *frame)
69 1.2.2.2 skrll {
70 1.2.2.2 skrll char *params;
71 1.2.2.2 skrll const struct sysent *callp;
72 1.2.2.2 skrll struct lwp *l;
73 1.2.2.2 skrll struct proc *p;
74 1.2.2.2 skrll int error;
75 1.2.2.2 skrll size_t argsize;
76 1.2.2.2 skrll register_t code, args[8], rval[2];
77 1.2.2.2 skrll
78 1.2.2.2 skrll l = curlwp;
79 1.2.2.2 skrll p = l->l_proc;
80 1.2.2.2 skrll LWP_CACHE_CREDS(l, p);
81 1.2.2.2 skrll
82 1.2.2.2 skrll code = frame->tf_eax;
83 1.2.2.2 skrll callp = p->p_emul->e_sysent;
84 1.2.2.2 skrll params = (char *)frame->tf_esp + sizeof(int);
85 1.2.2.2 skrll
86 1.2.2.2 skrll switch (code) {
87 1.2.2.2 skrll case SYS_syscall:
88 1.2.2.2 skrll /*
89 1.2.2.2 skrll * Code is first argument, followed by actual args.
90 1.2.2.2 skrll */
91 1.2.2.2 skrll code = fuword(params);
92 1.2.2.2 skrll params += sizeof(int);
93 1.2.2.2 skrll break;
94 1.2.2.2 skrll case SYS___syscall:
95 1.2.2.2 skrll /*
96 1.2.2.2 skrll * Like syscall, but code is a quad, so as to maintain
97 1.2.2.2 skrll * quad alignment for the rest of the arguments.
98 1.2.2.2 skrll */
99 1.2.2.2 skrll code = fuword(params + _QUAD_LOWWORD * sizeof(int));
100 1.2.2.2 skrll params += sizeof(quad_t);
101 1.2.2.2 skrll break;
102 1.2.2.2 skrll default:
103 1.2.2.2 skrll break;
104 1.2.2.2 skrll }
105 1.2.2.2 skrll
106 1.2.2.2 skrll code &= (SYS_NSYSENT - 1);
107 1.2.2.2 skrll callp += code;
108 1.2.2.2 skrll argsize = callp->sy_argsize;
109 1.2.2.2 skrll if (argsize) {
110 1.2.2.2 skrll error = copyin(params, (void *)args, argsize);
111 1.2.2.2 skrll if (error)
112 1.2.2.2 skrll goto bad;
113 1.2.2.2 skrll }
114 1.2.2.2 skrll
115 1.2.2.2 skrll if (!__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_entry))
116 1.2.2.2 skrll || (error = trace_enter(code, callp, args)) == 0) {
117 1.2.2.2 skrll rval[0] = 0;
118 1.2.2.2 skrll rval[1] = frame->tf_edx; /* need to keep edx for shared FreeBSD bins */
119 1.2.2.2 skrll error = sy_call(callp, l, args, rval);
120 1.2.2.2 skrll }
121 1.2.2.2 skrll
122 1.2.2.2 skrll switch (error) {
123 1.2.2.2 skrll case 0:
124 1.2.2.2 skrll frame->tf_eax = rval[0];
125 1.2.2.2 skrll frame->tf_edx = rval[1];
126 1.2.2.2 skrll frame->tf_eflags &= ~PSL_C; /* carry bit */
127 1.2.2.2 skrll break;
128 1.2.2.2 skrll case ERESTART:
129 1.2.2.2 skrll /*
130 1.2.2.2 skrll * The offset to adjust the PC by depends on whether we entered
131 1.2.2.2 skrll * the kernel through the trap or call gate. We pushed the
132 1.2.2.2 skrll * size of the instruction into tf_err on entry.
133 1.2.2.2 skrll */
134 1.2.2.2 skrll frame->tf_eip -= frame->tf_err;
135 1.2.2.2 skrll break;
136 1.2.2.2 skrll case EJUSTRETURN:
137 1.2.2.2 skrll /* nothing to do */
138 1.2.2.2 skrll break;
139 1.2.2.2 skrll default:
140 1.2.2.2 skrll bad:
141 1.2.2.2 skrll frame->tf_eax = error;
142 1.2.2.2 skrll frame->tf_eflags |= PSL_C; /* carry bit */
143 1.2.2.2 skrll break;
144 1.2.2.2 skrll }
145 1.2.2.2 skrll
146 1.2.2.2 skrll if (__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_return)))
147 1.2.2.2 skrll trace_exit(code, callp, args, rval, error);
148 1.2.2.2 skrll
149 1.2.2.2 skrll userret(l);
150 1.2.2.2 skrll }
151