1 | /* $NetBSD: kern_xxx.c,v 1.73 2015/10/29 00:27:08 mrg Exp $ */ |
2 | |
3 | /* |
4 | * Copyright (c) 1982, 1986, 1989, 1993 |
5 | * The Regents of the University of California. All rights reserved. |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions |
9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * 3. Neither the name of the University nor the names of its contributors |
16 | * may be used to endorse or promote products derived from this software |
17 | * without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 | * SUCH DAMAGE. |
30 | * |
31 | * @(#)kern_xxx.c 8.3 (Berkeley) 2/14/95 |
32 | */ |
33 | |
34 | #include <sys/cdefs.h> |
35 | __KERNEL_RCSID(0, "$NetBSD: kern_xxx.c,v 1.73 2015/10/29 00:27:08 mrg Exp $" ); |
36 | |
37 | #ifdef _KERNEL_OPT |
38 | #include "opt_syscall_debug.h" |
39 | #include "opt_kernhist.h" |
40 | #endif |
41 | |
42 | #include <sys/param.h> |
43 | #include <sys/systm.h> |
44 | #include <sys/kernel.h> |
45 | #include <sys/proc.h> |
46 | #include <sys/reboot.h> |
47 | #include <sys/syscall.h> |
48 | #include <sys/sysctl.h> |
49 | #include <sys/mount.h> |
50 | #include <sys/syscall.h> |
51 | #include <sys/syscallargs.h> |
52 | #include <sys/kauth.h> |
53 | #include <sys/kernhist.h> |
54 | |
55 | /* ARGSUSED */ |
56 | int |
57 | sys_reboot(struct lwp *l, const struct sys_reboot_args *uap, register_t *retval) |
58 | { |
59 | /* { |
60 | syscallarg(int) opt; |
61 | syscallarg(char *) bootstr; |
62 | } */ |
63 | int error; |
64 | char *bootstr, bs[128]; |
65 | |
66 | if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_REBOOT, |
67 | 0, NULL, NULL, NULL)) != 0) |
68 | return (error); |
69 | |
70 | /* |
71 | * Only use the boot string if RB_STRING is set. |
72 | */ |
73 | if ((SCARG(uap, opt) & RB_STRING) && |
74 | (error = copyinstr(SCARG(uap, bootstr), bs, sizeof(bs), 0)) == 0) |
75 | bootstr = bs; |
76 | else |
77 | bootstr = NULL; |
78 | /* |
79 | * Not all ports use the bootstr currently. |
80 | */ |
81 | KERNEL_LOCK(1, NULL); |
82 | cpu_reboot(SCARG(uap, opt), bootstr); |
83 | KERNEL_UNLOCK_ONE(NULL); |
84 | return (0); |
85 | } |
86 | |
87 | /* |
88 | * Pull in the indirect syscall functions here. |
89 | * They are only actually used if the ports syscall entry code |
90 | * doesn't special-case SYS_SYSCALL and SYS___SYSCALL |
91 | * |
92 | * In some cases the generated code for the two functions is identical, |
93 | * but there isn't a MI way of determining that - so we don't try. |
94 | */ |
95 | |
96 | #define SYS_SYSCALL sys_syscall |
97 | #include "sys_syscall.c" |
98 | #undef SYS_SYSCALL |
99 | |
100 | #define SYS_SYSCALL sys___syscall |
101 | #include "sys_syscall.c" |
102 | #undef SYS_SYSCALL |
103 | |
104 | #ifdef SYSCALL_DEBUG |
105 | #define SCDEBUG_CALLS 0x0001 /* show calls */ |
106 | #define SCDEBUG_RETURNS 0x0002 /* show returns */ |
107 | #define SCDEBUG_ALL 0x0004 /* even syscalls that are not implemented */ |
108 | #define SCDEBUG_SHOWARGS 0x0008 /* show arguments to calls */ |
109 | #define SCDEBUG_KERNHIST 0x0010 /* use kernhist instead of printf */ |
110 | |
111 | #ifndef SCDEBUG_DEFAULT |
112 | #define SCDEBUG_DEFAULT (SCDEBUG_CALLS|SCDEBUG_RETURNS|SCDEBUG_SHOWARGS) |
113 | #endif |
114 | |
115 | int scdebug = SCDEBUG_DEFAULT; |
116 | |
117 | #ifdef KERNHIST |
118 | KERNHIST_DEFINE(scdebughist); |
119 | #define SCDEBUG_KERNHIST_FUNC(a) KERNHIST_FUNC(a) |
120 | #define SCDEBUG_KERNHIST_CALLED(a) KERNHIST_CALLED(a) |
121 | #define SCDEBUG_KERNHIST_LOG(a,b,c,d,e,f) KERNHIST_LOG(a,b,c,d,e,f) |
122 | #else |
123 | #define SCDEBUG_KERNHIST_FUNC(a) /* nothing */ |
124 | #define SCDEBUG_KERNHIST_CALLED(a) /* nothing */ |
125 | #define SCDEBUG_KERNHIST_LOG(a,b,c,d,e,f) /* nothing */ |
126 | /* The non-kernhist support version can elide all this code easily. */ |
127 | #undef SCDEBUG_KERNHIST |
128 | #define SCDEBUG_KERNHIST 0 |
129 | #endif |
130 | |
131 | #ifdef __HAVE_MINIMAL_EMUL |
132 | #define CODE_NOT_OK(code, em) ((int)(code) < 0) |
133 | #else |
134 | #define CODE_NOT_OK(code, em) (((int)(code) < 0) || \ |
135 | ((int)(code) >= (em)->e_nsysent)) |
136 | #endif |
137 | |
138 | void |
139 | scdebug_call(register_t code, const register_t args[]) |
140 | { |
141 | SCDEBUG_KERNHIST_FUNC("scdebug_call" ); |
142 | struct lwp *l = curlwp; |
143 | struct proc *p = l->l_proc; |
144 | const struct sysent *sy; |
145 | const struct emul *em; |
146 | int i; |
147 | |
148 | if ((scdebug & SCDEBUG_CALLS) == 0) |
149 | return; |
150 | |
151 | if (scdebug & SCDEBUG_KERNHIST) |
152 | SCDEBUG_KERNHIST_CALLED(scdebughist); |
153 | |
154 | em = p->p_emul; |
155 | sy = &em->e_sysent[code]; |
156 | |
157 | if ((scdebug & SCDEBUG_ALL) == 0 && |
158 | (CODE_NOT_OK(code, em) || sy->sy_call == sys_nosys)) { |
159 | if (scdebug & SCDEBUG_KERNHIST) |
160 | SCDEBUG_KERNHIST_LOG(scdebughist, "" , 0, 0, 0, 0); |
161 | return; |
162 | } |
163 | |
164 | /* |
165 | * The kernhist version of scdebug needs to restrict the usage |
166 | * compared to the normal version. histories must avoid these |
167 | * sorts of usage: |
168 | * |
169 | * - the format string *must* be literal, as it is used |
170 | * at display time in the kernel or userland |
171 | * - strings in the format will cause vmstat -u to crash |
172 | * so avoid using %s formats |
173 | * |
174 | * to avoid these, we have a fairly long block to print args |
175 | * as the format needs to change for each, and we can't just |
176 | * call printf() on each argument until we're done. |
177 | */ |
178 | if (scdebug & SCDEBUG_KERNHIST) { |
179 | if (CODE_NOT_OK(code, em)) { |
180 | SCDEBUG_KERNHIST_LOG(scdebughist, |
181 | "pid %d:%d: OUT OF RANGE (%ld)" , |
182 | p->p_pid, l->l_lid, code, 0); |
183 | } else { |
184 | SCDEBUG_KERNHIST_LOG(scdebughist, |
185 | "pid %d:%d: num %d call %p" , |
186 | p->p_pid, l->l_lid, code, sy->sy_call); |
187 | if ((scdebug & SCDEBUG_SHOWARGS) == 0) |
188 | return; |
189 | |
190 | if (sy->sy_narg > 7) { |
191 | SCDEBUG_KERNHIST_LOG(scdebughist, |
192 | "args[4-7]: (%lx, %lx, %lx, %lx, ...)" , |
193 | (long)args[4], (long)args[5], |
194 | (long)args[6], (long)args[7]); |
195 | } else if (sy->sy_narg > 6) { |
196 | SCDEBUG_KERNHIST_LOG(scdebughist, |
197 | "args[4-6]: (%lx, %lx, %lx)" , |
198 | (long)args[4], (long)args[5], |
199 | (long)args[6], 0); |
200 | } else if (sy->sy_narg > 5) { |
201 | SCDEBUG_KERNHIST_LOG(scdebughist, |
202 | "args[4-5]: (%lx, %lx)" , |
203 | (long)args[4], (long)args[5], 0, 0); |
204 | } else if (sy->sy_narg == 5) { |
205 | SCDEBUG_KERNHIST_LOG(scdebughist, |
206 | "args[4]: (%lx)" , |
207 | (long)args[4], 0, 0, 0); |
208 | } |
209 | |
210 | if (sy->sy_narg > 3) { |
211 | SCDEBUG_KERNHIST_LOG(scdebughist, |
212 | "args[0-3]: (%lx, %lx, %lx, %lx, ...)" , |
213 | (long)args[0], (long)args[1], |
214 | (long)args[2], (long)args[3]); |
215 | } else if (sy->sy_narg > 2) { |
216 | SCDEBUG_KERNHIST_LOG(scdebughist, |
217 | "args[0-2]: (%lx, %lx, %lx)" , |
218 | (long)args[0], (long)args[1], |
219 | (long)args[2], 0); |
220 | } else if (sy->sy_narg > 1) { |
221 | SCDEBUG_KERNHIST_LOG(scdebughist, |
222 | "args[0-1]: (%lx, %lx)" , |
223 | (long)args[0], (long)args[1], 0, 0); |
224 | } else if (sy->sy_narg == 1) { |
225 | SCDEBUG_KERNHIST_LOG(scdebughist, |
226 | "args[0]: (%lx)" , |
227 | (long)args[0], 0, 0, 0); |
228 | } |
229 | } |
230 | return; |
231 | } |
232 | |
233 | printf("proc %d (%s): %s num " , p->p_pid, p->p_comm, em->e_name); |
234 | if (CODE_NOT_OK(code, em)) |
235 | printf("OUT OF RANGE (%ld)" , (long)code); |
236 | else { |
237 | printf("%ld call: %s" , (long)code, em->e_syscallnames[code]); |
238 | if (scdebug & SCDEBUG_SHOWARGS) { |
239 | printf("(" ); |
240 | for (i = 0; i < sy->sy_argsize/sizeof(register_t); i++) |
241 | printf("%s0x%lx" , i == 0 ? "" : ", " , |
242 | (long)args[i]); |
243 | printf(")" ); |
244 | } |
245 | } |
246 | printf("\n" ); |
247 | } |
248 | |
249 | void |
250 | scdebug_ret(register_t code, int error, const register_t retval[]) |
251 | { |
252 | SCDEBUG_KERNHIST_FUNC("scdebug_ret" ); |
253 | struct lwp *l = curlwp; |
254 | struct proc *p = l->l_proc; |
255 | const struct sysent *sy; |
256 | const struct emul *em; |
257 | |
258 | if ((scdebug & SCDEBUG_RETURNS) == 0) |
259 | return; |
260 | |
261 | if (scdebug & SCDEBUG_KERNHIST) |
262 | SCDEBUG_KERNHIST_CALLED(scdebughist); |
263 | |
264 | em = p->p_emul; |
265 | sy = &em->e_sysent[code]; |
266 | if ((scdebug & SCDEBUG_ALL) == 0 && |
267 | (CODE_NOT_OK(code, em) || sy->sy_call == sys_nosys)) { |
268 | if (scdebug & SCDEBUG_KERNHIST) |
269 | SCDEBUG_KERNHIST_LOG(scdebughist, "" , 0, 0, 0, 0); |
270 | return; |
271 | } |
272 | |
273 | if (scdebug & SCDEBUG_KERNHIST) { |
274 | if (CODE_NOT_OK(code, em)) { |
275 | SCDEBUG_KERNHIST_LOG(scdebughist, |
276 | "pid %d:%d: OUT OF RANGE (%ld)" , |
277 | p->p_pid, l->l_lid, code, 0); |
278 | } else { |
279 | SCDEBUG_KERNHIST_LOG(scdebughist, |
280 | "pid %d:%d: num %ld" , |
281 | p->p_pid, l->l_lid, code, 0); |
282 | SCDEBUG_KERNHIST_LOG(scdebughist, |
283 | "ret: err = %d, rv = 0x%lx,0x%lx" , |
284 | error, (long)retval[0], (long)retval[1], 0); |
285 | } |
286 | return; |
287 | } |
288 | |
289 | printf("proc %d (%s): %s num " , p->p_pid, p->p_comm, em->e_name); |
290 | if (CODE_NOT_OK(code, em)) |
291 | printf("OUT OF RANGE (%ld)" , (long)code); |
292 | else |
293 | printf("%ld ret %s: err = %d, rv = 0x%lx,0x%lx" , (long)code, |
294 | em->e_syscallnames[code], error, |
295 | (long)retval[0], (long)retval[1]); |
296 | printf("\n" ); |
297 | } |
298 | #endif /* SYSCALL_DEBUG */ |
299 | |
300 | #ifndef SCDEBUG_KERNHIST_SIZE |
301 | #define SCDEBUG_KERNHIST_SIZE 500 |
302 | #endif |
303 | |
304 | void |
305 | scdebug_init(void) |
306 | { |
307 | #if defined(SYSCALL_DEBUG) && defined(KERNHIST) |
308 | /* Setup scdebughist kernel history */ |
309 | KERNHIST_INIT(scdebughist, SCDEBUG_KERNHIST_SIZE); |
310 | #endif |
311 | } |
312 | |