kern_subr.c revision 1.203 1 1.203 dyoung /* $NetBSD: kern_subr.c,v 1.203 2009/11/05 18:07:19 dyoung Exp $ */
2 1.31 thorpej
3 1.31 thorpej /*-
4 1.185 ad * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 The NetBSD Foundation, Inc.
5 1.31 thorpej * All rights reserved.
6 1.31 thorpej *
7 1.31 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.31 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.48 lukem * NASA Ames Research Center, and by Luke Mewburn.
10 1.31 thorpej *
11 1.31 thorpej * Redistribution and use in source and binary forms, with or without
12 1.31 thorpej * modification, are permitted provided that the following conditions
13 1.31 thorpej * are met:
14 1.31 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.31 thorpej * notice, this list of conditions and the following disclaimer.
16 1.31 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.31 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.31 thorpej * documentation and/or other materials provided with the distribution.
19 1.31 thorpej *
20 1.31 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.31 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.31 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.31 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.31 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.31 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.31 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.31 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.31 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.31 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.31 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.31 thorpej */
32 1.12 cgd
33 1.9 cgd /*
34 1.10 cgd * Copyright (c) 1982, 1986, 1991, 1993
35 1.10 cgd * The Regents of the University of California. All rights reserved.
36 1.9 cgd * (c) UNIX System Laboratories, Inc.
37 1.9 cgd * All or some portions of this file are derived from material licensed
38 1.9 cgd * to the University of California by American Telephone and Telegraph
39 1.9 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 1.9 cgd * the permission of UNIX System Laboratories, Inc.
41 1.9 cgd *
42 1.18 thorpej * Copyright (c) 1992, 1993
43 1.18 thorpej * The Regents of the University of California. All rights reserved.
44 1.18 thorpej *
45 1.18 thorpej * This software was developed by the Computer Systems Engineering group
46 1.18 thorpej * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
47 1.18 thorpej * contributed to Berkeley.
48 1.18 thorpej *
49 1.18 thorpej * All advertising materials mentioning features or use of this software
50 1.18 thorpej * must display the following acknowledgement:
51 1.18 thorpej * This product includes software developed by the University of
52 1.18 thorpej * California, Lawrence Berkeley Laboratory.
53 1.18 thorpej *
54 1.9 cgd * Redistribution and use in source and binary forms, with or without
55 1.9 cgd * modification, are permitted provided that the following conditions
56 1.9 cgd * are met:
57 1.9 cgd * 1. Redistributions of source code must retain the above copyright
58 1.9 cgd * notice, this list of conditions and the following disclaimer.
59 1.9 cgd * 2. Redistributions in binary form must reproduce the above copyright
60 1.9 cgd * notice, this list of conditions and the following disclaimer in the
61 1.9 cgd * documentation and/or other materials provided with the distribution.
62 1.103 agc * 3. Neither the name of the University nor the names of its contributors
63 1.9 cgd * may be used to endorse or promote products derived from this software
64 1.9 cgd * without specific prior written permission.
65 1.9 cgd *
66 1.9 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67 1.9 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 1.9 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 1.9 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70 1.9 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 1.9 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 1.9 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 1.9 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 1.9 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 1.9 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 1.9 cgd * SUCH DAMAGE.
77 1.9 cgd *
78 1.38 fvdl * @(#)kern_subr.c 8.4 (Berkeley) 2/14/95
79 1.9 cgd */
80 1.77 lukem
81 1.77 lukem #include <sys/cdefs.h>
82 1.203 dyoung __KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.203 2009/11/05 18:07:19 dyoung Exp $");
83 1.34 mrg
84 1.78 thorpej #include "opt_ddb.h"
85 1.43 jonathan #include "opt_md.h"
86 1.81 christos #include "opt_syscall_debug.h"
87 1.81 christos #include "opt_ktrace.h"
88 1.142 matt #include "opt_ptrace.h"
89 1.144 jmcneill #include "opt_powerhook.h"
90 1.157 manu #include "opt_tftproot.h"
91 1.9 cgd
92 1.9 cgd #include <sys/param.h>
93 1.9 cgd #include <sys/systm.h>
94 1.9 cgd #include <sys/proc.h>
95 1.10 cgd #include <sys/malloc.h>
96 1.18 thorpej #include <sys/mount.h>
97 1.18 thorpej #include <sys/device.h>
98 1.18 thorpej #include <sys/reboot.h>
99 1.18 thorpej #include <sys/conf.h>
100 1.159 dyoung #include <sys/disk.h>
101 1.18 thorpej #include <sys/disklabel.h>
102 1.10 cgd #include <sys/queue.h>
103 1.81 christos #include <sys/ktrace.h>
104 1.128 christos #include <sys/ptrace.h>
105 1.119 reinoud #include <sys/fcntl.h>
106 1.160 christos #include <sys/kauth.h>
107 1.160 christos #include <sys/vnode.h>
108 1.194 ad #include <sys/syscallvar.h>
109 1.194 ad #include <sys/xcall.h>
110 1.194 ad #include <sys/module.h>
111 1.9 cgd
112 1.82 thorpej #include <uvm/uvm_extern.h>
113 1.82 thorpej
114 1.18 thorpej #include <dev/cons.h>
115 1.18 thorpej
116 1.18 thorpej #include <net/if.h>
117 1.18 thorpej
118 1.18 thorpej /* XXX these should eventually move to subr_autoconf.c */
119 1.200 dyoung static device_t finddevice(const char *);
120 1.200 dyoung static device_t getdisk(char *, int, int, dev_t *, int);
121 1.200 dyoung static device_t parsedisk(char *, int, int, dev_t *);
122 1.159 dyoung static const char *getwedgename(const char *, int);
123 1.18 thorpej
124 1.80 christos /*
125 1.80 christos * A generic linear hook.
126 1.80 christos */
127 1.80 christos struct hook_desc {
128 1.80 christos LIST_ENTRY(hook_desc) hk_list;
129 1.109 junyoung void (*hk_fn)(void *);
130 1.80 christos void *hk_arg;
131 1.80 christos };
132 1.80 christos typedef LIST_HEAD(, hook_desc) hook_list_t;
133 1.80 christos
134 1.157 manu #ifdef TFTPROOT
135 1.200 dyoung int tftproot_dhcpboot(device_t);
136 1.157 manu #endif
137 1.157 manu
138 1.175 ad dev_t dumpcdev; /* for savecore */
139 1.175 ad
140 1.80 christos static void *
141 1.117 thorpej hook_establish(hook_list_t *list, void (*fn)(void *), void *arg)
142 1.14 cgd {
143 1.80 christos struct hook_desc *hd;
144 1.14 cgd
145 1.80 christos hd = malloc(sizeof(*hd), M_DEVBUF, M_NOWAIT);
146 1.80 christos if (hd == NULL)
147 1.59 enami return (NULL);
148 1.14 cgd
149 1.80 christos hd->hk_fn = fn;
150 1.80 christos hd->hk_arg = arg;
151 1.80 christos LIST_INSERT_HEAD(list, hd, hk_list);
152 1.14 cgd
153 1.80 christos return (hd);
154 1.14 cgd }
155 1.14 cgd
156 1.80 christos static void
157 1.150 yamt hook_disestablish(hook_list_t *list, void *vhook)
158 1.14 cgd {
159 1.14 cgd #ifdef DIAGNOSTIC
160 1.80 christos struct hook_desc *hd;
161 1.14 cgd
162 1.83 matt LIST_FOREACH(hd, list, hk_list) {
163 1.80 christos if (hd == vhook)
164 1.14 cgd break;
165 1.83 matt }
166 1.83 matt
167 1.80 christos if (hd == NULL)
168 1.83 matt panic("hook_disestablish: hook %p not established", vhook);
169 1.14 cgd #endif
170 1.80 christos LIST_REMOVE((struct hook_desc *)vhook, hk_list);
171 1.80 christos free(vhook, M_DEVBUF);
172 1.80 christos }
173 1.80 christos
174 1.80 christos static void
175 1.117 thorpej hook_destroy(hook_list_t *list)
176 1.80 christos {
177 1.80 christos struct hook_desc *hd;
178 1.80 christos
179 1.87 matt while ((hd = LIST_FIRST(list)) != NULL) {
180 1.80 christos LIST_REMOVE(hd, hk_list);
181 1.80 christos free(hd, M_DEVBUF);
182 1.80 christos }
183 1.80 christos }
184 1.14 cgd
185 1.80 christos static void
186 1.117 thorpej hook_proc_run(hook_list_t *list, struct proc *p)
187 1.80 christos {
188 1.80 christos struct hook_desc *hd;
189 1.80 christos
190 1.178 dyoung LIST_FOREACH(hd, list, hk_list)
191 1.178 dyoung ((void (*)(struct proc *, void *))*hd->hk_fn)(p, hd->hk_arg);
192 1.80 christos }
193 1.80 christos
194 1.80 christos /*
195 1.80 christos * "Shutdown hook" types, functions, and variables.
196 1.80 christos *
197 1.80 christos * Should be invoked immediately before the
198 1.80 christos * system is halted or rebooted, i.e. after file systems unmounted,
199 1.80 christos * after crash dump done, etc.
200 1.80 christos *
201 1.80 christos * Each shutdown hook is removed from the list before it's run, so that
202 1.80 christos * it won't be run again.
203 1.80 christos */
204 1.80 christos
205 1.117 thorpej static hook_list_t shutdownhook_list;
206 1.80 christos
207 1.80 christos void *
208 1.117 thorpej shutdownhook_establish(void (*fn)(void *), void *arg)
209 1.80 christos {
210 1.80 christos return hook_establish(&shutdownhook_list, fn, arg);
211 1.80 christos }
212 1.80 christos
213 1.80 christos void
214 1.117 thorpej shutdownhook_disestablish(void *vhook)
215 1.80 christos {
216 1.89 simonb hook_disestablish(&shutdownhook_list, vhook);
217 1.14 cgd }
218 1.14 cgd
219 1.14 cgd /*
220 1.14 cgd * Run shutdown hooks. Should be invoked immediately before the
221 1.14 cgd * system is halted or rebooted, i.e. after file systems unmounted,
222 1.14 cgd * after crash dump done, etc.
223 1.17 cgd *
224 1.17 cgd * Each shutdown hook is removed from the list before it's run, so that
225 1.17 cgd * it won't be run again.
226 1.14 cgd */
227 1.14 cgd void
228 1.117 thorpej doshutdownhooks(void)
229 1.14 cgd {
230 1.80 christos struct hook_desc *dp;
231 1.14 cgd
232 1.87 matt while ((dp = LIST_FIRST(&shutdownhook_list)) != NULL) {
233 1.80 christos LIST_REMOVE(dp, hk_list);
234 1.80 christos (*dp->hk_fn)(dp->hk_arg);
235 1.17 cgd #if 0
236 1.17 cgd /*
237 1.17 cgd * Don't bother freeing the hook structure,, since we may
238 1.17 cgd * be rebooting because of a memory corruption problem,
239 1.17 cgd * and this might only make things worse. It doesn't
240 1.17 cgd * matter, anyway, since the system is just about to
241 1.17 cgd * reboot.
242 1.17 cgd */
243 1.17 cgd free(dp, M_DEVBUF);
244 1.17 cgd #endif
245 1.52 augustss }
246 1.52 augustss }
247 1.52 augustss
248 1.52 augustss /*
249 1.80 christos * "Mountroot hook" types, functions, and variables.
250 1.80 christos */
251 1.80 christos
252 1.117 thorpej static hook_list_t mountroothook_list;
253 1.80 christos
254 1.80 christos void *
255 1.200 dyoung mountroothook_establish(void (*fn)(device_t), device_t dev)
256 1.80 christos {
257 1.109 junyoung return hook_establish(&mountroothook_list, (void (*)(void *))fn, dev);
258 1.80 christos }
259 1.80 christos
260 1.80 christos void
261 1.117 thorpej mountroothook_disestablish(void *vhook)
262 1.80 christos {
263 1.89 simonb hook_disestablish(&mountroothook_list, vhook);
264 1.80 christos }
265 1.80 christos
266 1.80 christos void
267 1.117 thorpej mountroothook_destroy(void)
268 1.80 christos {
269 1.80 christos hook_destroy(&mountroothook_list);
270 1.80 christos }
271 1.80 christos
272 1.80 christos void
273 1.117 thorpej domountroothook(void)
274 1.80 christos {
275 1.80 christos struct hook_desc *hd;
276 1.80 christos
277 1.83 matt LIST_FOREACH(hd, &mountroothook_list, hk_list) {
278 1.80 christos if (hd->hk_arg == (void *)root_device) {
279 1.80 christos (*hd->hk_fn)(hd->hk_arg);
280 1.80 christos return;
281 1.80 christos }
282 1.80 christos }
283 1.80 christos }
284 1.80 christos
285 1.117 thorpej static hook_list_t exechook_list;
286 1.80 christos
287 1.80 christos void *
288 1.117 thorpej exechook_establish(void (*fn)(struct proc *, void *), void *arg)
289 1.80 christos {
290 1.109 junyoung return hook_establish(&exechook_list, (void (*)(void *))fn, arg);
291 1.80 christos }
292 1.80 christos
293 1.80 christos void
294 1.117 thorpej exechook_disestablish(void *vhook)
295 1.80 christos {
296 1.80 christos hook_disestablish(&exechook_list, vhook);
297 1.80 christos }
298 1.80 christos
299 1.80 christos /*
300 1.80 christos * Run exec hooks.
301 1.80 christos */
302 1.80 christos void
303 1.117 thorpej doexechooks(struct proc *p)
304 1.80 christos {
305 1.80 christos hook_proc_run(&exechook_list, p);
306 1.80 christos }
307 1.80 christos
308 1.117 thorpej static hook_list_t exithook_list;
309 1.195 ad extern krwlock_t exec_lock;
310 1.80 christos
311 1.80 christos void *
312 1.117 thorpej exithook_establish(void (*fn)(struct proc *, void *), void *arg)
313 1.80 christos {
314 1.195 ad void *rv;
315 1.195 ad
316 1.195 ad rw_enter(&exec_lock, RW_WRITER);
317 1.195 ad rv = hook_establish(&exithook_list, (void (*)(void *))fn, arg);
318 1.195 ad rw_exit(&exec_lock);
319 1.195 ad return rv;
320 1.80 christos }
321 1.80 christos
322 1.80 christos void
323 1.117 thorpej exithook_disestablish(void *vhook)
324 1.80 christos {
325 1.195 ad
326 1.195 ad rw_enter(&exec_lock, RW_WRITER);
327 1.80 christos hook_disestablish(&exithook_list, vhook);
328 1.195 ad rw_exit(&exec_lock);
329 1.80 christos }
330 1.80 christos
331 1.80 christos /*
332 1.80 christos * Run exit hooks.
333 1.80 christos */
334 1.80 christos void
335 1.117 thorpej doexithooks(struct proc *p)
336 1.80 christos {
337 1.80 christos hook_proc_run(&exithook_list, p);
338 1.96 thorpej }
339 1.96 thorpej
340 1.117 thorpej static hook_list_t forkhook_list;
341 1.96 thorpej
342 1.96 thorpej void *
343 1.117 thorpej forkhook_establish(void (*fn)(struct proc *, struct proc *))
344 1.96 thorpej {
345 1.109 junyoung return hook_establish(&forkhook_list, (void (*)(void *))fn, NULL);
346 1.96 thorpej }
347 1.96 thorpej
348 1.96 thorpej void
349 1.117 thorpej forkhook_disestablish(void *vhook)
350 1.96 thorpej {
351 1.96 thorpej hook_disestablish(&forkhook_list, vhook);
352 1.96 thorpej }
353 1.96 thorpej
354 1.96 thorpej /*
355 1.96 thorpej * Run fork hooks.
356 1.96 thorpej */
357 1.96 thorpej void
358 1.117 thorpej doforkhooks(struct proc *p2, struct proc *p1)
359 1.96 thorpej {
360 1.96 thorpej struct hook_desc *hd;
361 1.96 thorpej
362 1.96 thorpej LIST_FOREACH(hd, &forkhook_list, hk_list) {
363 1.109 junyoung ((void (*)(struct proc *, struct proc *))*hd->hk_fn)
364 1.96 thorpej (p2, p1);
365 1.96 thorpej }
366 1.80 christos }
367 1.80 christos
368 1.80 christos /*
369 1.52 augustss * "Power hook" types, functions, and variables.
370 1.71 augustss * The list of power hooks is kept ordered with the last registered hook
371 1.71 augustss * first.
372 1.71 augustss * When running the hooks on power down the hooks are called in reverse
373 1.71 augustss * registration order, when powering up in registration order.
374 1.52 augustss */
375 1.52 augustss struct powerhook_desc {
376 1.71 augustss CIRCLEQ_ENTRY(powerhook_desc) sfd_list;
377 1.109 junyoung void (*sfd_fn)(int, void *);
378 1.52 augustss void *sfd_arg;
379 1.144 jmcneill char sfd_name[16];
380 1.52 augustss };
381 1.52 augustss
382 1.117 thorpej static CIRCLEQ_HEAD(, powerhook_desc) powerhook_list =
383 1.117 thorpej CIRCLEQ_HEAD_INITIALIZER(powerhook_list);
384 1.52 augustss
385 1.52 augustss void *
386 1.145 dogcow powerhook_establish(const char *name, void (*fn)(int, void *), void *arg)
387 1.52 augustss {
388 1.52 augustss struct powerhook_desc *ndp;
389 1.52 augustss
390 1.52 augustss ndp = (struct powerhook_desc *)
391 1.52 augustss malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT);
392 1.52 augustss if (ndp == NULL)
393 1.59 enami return (NULL);
394 1.52 augustss
395 1.52 augustss ndp->sfd_fn = fn;
396 1.52 augustss ndp->sfd_arg = arg;
397 1.144 jmcneill strlcpy(ndp->sfd_name, name, sizeof(ndp->sfd_name));
398 1.71 augustss CIRCLEQ_INSERT_HEAD(&powerhook_list, ndp, sfd_list);
399 1.52 augustss
400 1.167 jmcneill aprint_error("%s: WARNING: powerhook_establish is deprecated\n", name);
401 1.52 augustss return (ndp);
402 1.52 augustss }
403 1.52 augustss
404 1.52 augustss void
405 1.117 thorpej powerhook_disestablish(void *vhook)
406 1.52 augustss {
407 1.52 augustss #ifdef DIAGNOSTIC
408 1.52 augustss struct powerhook_desc *dp;
409 1.52 augustss
410 1.71 augustss CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list)
411 1.52 augustss if (dp == vhook)
412 1.71 augustss goto found;
413 1.83 matt panic("powerhook_disestablish: hook %p not established", vhook);
414 1.71 augustss found:
415 1.52 augustss #endif
416 1.52 augustss
417 1.71 augustss CIRCLEQ_REMOVE(&powerhook_list, (struct powerhook_desc *)vhook,
418 1.71 augustss sfd_list);
419 1.52 augustss free(vhook, M_DEVBUF);
420 1.52 augustss }
421 1.52 augustss
422 1.52 augustss /*
423 1.52 augustss * Run power hooks.
424 1.52 augustss */
425 1.52 augustss void
426 1.117 thorpej dopowerhooks(int why)
427 1.52 augustss {
428 1.52 augustss struct powerhook_desc *dp;
429 1.52 augustss
430 1.144 jmcneill #ifdef POWERHOOK_DEBUG
431 1.162 christos const char *why_name;
432 1.163 degroote static const char * pwr_names[] = {PWR_NAMES};
433 1.163 degroote why_name = why < __arraycount(pwr_names) ? pwr_names[why] : "???";
434 1.144 jmcneill #endif
435 1.144 jmcneill
436 1.73 takemura if (why == PWR_RESUME || why == PWR_SOFTRESUME) {
437 1.71 augustss CIRCLEQ_FOREACH_REVERSE(dp, &powerhook_list, sfd_list) {
438 1.144 jmcneill #ifdef POWERHOOK_DEBUG
439 1.162 christos printf("dopowerhooks %s: %s (%p)\n", why_name, dp->sfd_name, dp);
440 1.144 jmcneill #endif
441 1.71 augustss (*dp->sfd_fn)(why, dp->sfd_arg);
442 1.71 augustss }
443 1.71 augustss } else {
444 1.71 augustss CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list) {
445 1.144 jmcneill #ifdef POWERHOOK_DEBUG
446 1.162 christos printf("dopowerhooks %s: %s (%p)\n", why_name, dp->sfd_name, dp);
447 1.144 jmcneill #endif
448 1.71 augustss (*dp->sfd_fn)(why, dp->sfd_arg);
449 1.71 augustss }
450 1.18 thorpej }
451 1.144 jmcneill
452 1.144 jmcneill #ifdef POWERHOOK_DEBUG
453 1.162 christos printf("dopowerhooks: %s done\n", why_name);
454 1.144 jmcneill #endif
455 1.18 thorpej }
456 1.18 thorpej
457 1.160 christos static int
458 1.200 dyoung isswap(device_t dv)
459 1.160 christos {
460 1.160 christos struct dkwedge_info wi;
461 1.160 christos struct vnode *vn;
462 1.160 christos int error;
463 1.160 christos
464 1.160 christos if (device_class(dv) != DV_DISK || !device_is_a(dv, "dk"))
465 1.160 christos return 0;
466 1.160 christos
467 1.160 christos if ((vn = opendisk(dv)) == NULL)
468 1.160 christos return 0;
469 1.160 christos
470 1.166 pooka error = VOP_IOCTL(vn, DIOCGWEDGEINFO, &wi, FREAD, NOCRED);
471 1.166 pooka VOP_CLOSE(vn, FREAD, NOCRED);
472 1.160 christos vput(vn);
473 1.160 christos if (error) {
474 1.160 christos #ifdef DEBUG_WEDGE
475 1.184 cegger printf("%s: Get wedge info returned %d\n", device_xname(dv), error);
476 1.160 christos #endif
477 1.160 christos return 0;
478 1.160 christos }
479 1.160 christos return strcmp(wi.dkw_ptype, DKW_PTYPE_SWAP) == 0;
480 1.160 christos }
481 1.160 christos
482 1.18 thorpej /*
483 1.18 thorpej * Determine the root device and, if instructed to, the root file system.
484 1.18 thorpej */
485 1.18 thorpej
486 1.18 thorpej #include "md.h"
487 1.18 thorpej
488 1.187 ad #if NMD > 0
489 1.127 cube extern struct cfdriver md_cd;
490 1.108 christos #ifdef MEMORY_DISK_IS_ROOT
491 1.187 ad int md_is_root = 1;
492 1.187 ad #else
493 1.187 ad int md_is_root = 0;
494 1.108 christos #endif
495 1.190 drochner #endif
496 1.108 christos
497 1.113 thorpej /*
498 1.113 thorpej * The device and wedge that we booted from. If booted_wedge is NULL,
499 1.113 thorpej * the we might consult booted_partition.
500 1.113 thorpej */
501 1.200 dyoung device_t booted_device;
502 1.200 dyoung device_t booted_wedge;
503 1.113 thorpej int booted_partition;
504 1.113 thorpej
505 1.113 thorpej /*
506 1.113 thorpej * Use partition letters if it's a disk class but not a wedge.
507 1.113 thorpej * XXX Check for wedge is kinda gross.
508 1.113 thorpej */
509 1.113 thorpej #define DEV_USES_PARTITIONS(dv) \
510 1.124 thorpej (device_class((dv)) == DV_DISK && \
511 1.125 thorpej !device_is_a((dv), "dk"))
512 1.113 thorpej
513 1.18 thorpej void
514 1.200 dyoung setroot(device_t bootdv, int bootpartition)
515 1.18 thorpej {
516 1.200 dyoung device_t dv;
517 1.203 dyoung deviter_t di;
518 1.159 dyoung int len, majdev;
519 1.25 mrg dev_t nrootdev;
520 1.26 thorpej dev_t ndumpdev = NODEV;
521 1.18 thorpej char buf[128];
522 1.18 thorpej const char *rootdevname;
523 1.26 thorpej const char *dumpdevname;
524 1.200 dyoung device_t rootdv = NULL; /* XXX gcc -Wuninitialized */
525 1.200 dyoung device_t dumpdv = NULL;
526 1.18 thorpej struct ifnet *ifp;
527 1.18 thorpej const char *deffsname;
528 1.18 thorpej struct vfsops *vops;
529 1.18 thorpej
530 1.157 manu #ifdef TFTPROOT
531 1.157 manu if (tftproot_dhcpboot(bootdv) != 0)
532 1.157 manu boothowto |= RB_ASKNAME;
533 1.157 manu #endif
534 1.157 manu
535 1.187 ad #if NMD > 0
536 1.187 ad if (md_is_root) {
537 1.190 drochner /*
538 1.190 drochner * XXX there should be "root on md0" in the config file,
539 1.190 drochner * but it isn't always
540 1.190 drochner */
541 1.190 drochner bootdv = md_cd.cd_devs[0];
542 1.187 ad bootpartition = 0;
543 1.28 leo }
544 1.18 thorpej #endif
545 1.18 thorpej
546 1.18 thorpej /*
547 1.18 thorpej * If NFS is specified as the file system, and we found
548 1.18 thorpej * a DV_DISK boot device (or no boot device at all), then
549 1.18 thorpej * find a reasonable network interface for "rootspec".
550 1.18 thorpej */
551 1.197 pgoyette vops = vfs_getopsbyname(MOUNT_NFS);
552 1.197 pgoyette if (vops != NULL && strcmp(rootfstype, MOUNT_NFS) == 0 &&
553 1.18 thorpej rootspec == NULL &&
554 1.124 thorpej (bootdv == NULL || device_class(bootdv) != DV_IFNET)) {
555 1.115 matt IFNET_FOREACH(ifp) {
556 1.18 thorpej if ((ifp->if_flags &
557 1.18 thorpej (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
558 1.18 thorpej break;
559 1.83 matt }
560 1.18 thorpej if (ifp == NULL) {
561 1.18 thorpej /*
562 1.18 thorpej * Can't find a suitable interface; ask the
563 1.18 thorpej * user.
564 1.18 thorpej */
565 1.18 thorpej boothowto |= RB_ASKNAME;
566 1.18 thorpej } else {
567 1.18 thorpej /*
568 1.18 thorpej * Have a suitable interface; behave as if
569 1.18 thorpej * the user specified this interface.
570 1.18 thorpej */
571 1.18 thorpej rootspec = (const char *)ifp->if_xname;
572 1.18 thorpej }
573 1.18 thorpej }
574 1.165 ad if (vops != NULL)
575 1.165 ad vfs_delref(vops);
576 1.24 thorpej
577 1.24 thorpej /*
578 1.24 thorpej * If wildcarded root and we the boot device wasn't determined,
579 1.24 thorpej * ask the user.
580 1.24 thorpej */
581 1.24 thorpej if (rootspec == NULL && bootdv == NULL)
582 1.24 thorpej boothowto |= RB_ASKNAME;
583 1.18 thorpej
584 1.18 thorpej top:
585 1.18 thorpej if (boothowto & RB_ASKNAME) {
586 1.200 dyoung device_t defdumpdv;
587 1.26 thorpej
588 1.18 thorpej for (;;) {
589 1.18 thorpej printf("root device");
590 1.18 thorpej if (bootdv != NULL) {
591 1.184 cegger printf(" (default %s", device_xname(bootdv));
592 1.113 thorpej if (DEV_USES_PARTITIONS(bootdv))
593 1.18 thorpej printf("%c", bootpartition + 'a');
594 1.18 thorpej printf(")");
595 1.18 thorpej }
596 1.18 thorpej printf(": ");
597 1.64 itojun len = cngetsn(buf, sizeof(buf));
598 1.18 thorpej if (len == 0 && bootdv != NULL) {
599 1.184 cegger strlcpy(buf, device_xname(bootdv), sizeof(buf));
600 1.18 thorpej len = strlen(buf);
601 1.18 thorpej }
602 1.18 thorpej if (len > 0 && buf[len - 1] == '*') {
603 1.18 thorpej buf[--len] = '\0';
604 1.51 thorpej dv = getdisk(buf, len, 1, &nrootdev, 0);
605 1.18 thorpej if (dv != NULL) {
606 1.18 thorpej rootdv = dv;
607 1.25 mrg break;
608 1.18 thorpej }
609 1.18 thorpej }
610 1.51 thorpej dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
611 1.18 thorpej if (dv != NULL) {
612 1.18 thorpej rootdv = dv;
613 1.18 thorpej break;
614 1.18 thorpej }
615 1.18 thorpej }
616 1.18 thorpej
617 1.26 thorpej /*
618 1.26 thorpej * Set up the default dump device. If root is on
619 1.26 thorpej * a network device, there is no default dump
620 1.26 thorpej * device, since we don't support dumps to the
621 1.26 thorpej * network.
622 1.26 thorpej */
623 1.113 thorpej if (DEV_USES_PARTITIONS(rootdv) == 0)
624 1.26 thorpej defdumpdv = NULL;
625 1.26 thorpej else
626 1.26 thorpej defdumpdv = rootdv;
627 1.26 thorpej
628 1.26 thorpej for (;;) {
629 1.26 thorpej printf("dump device");
630 1.26 thorpej if (defdumpdv != NULL) {
631 1.26 thorpej /*
632 1.26 thorpej * Note, we know it's a disk if we get here.
633 1.26 thorpej */
634 1.184 cegger printf(" (default %sb)", device_xname(defdumpdv));
635 1.26 thorpej }
636 1.26 thorpej printf(": ");
637 1.64 itojun len = cngetsn(buf, sizeof(buf));
638 1.26 thorpej if (len == 0) {
639 1.26 thorpej if (defdumpdv != NULL) {
640 1.26 thorpej ndumpdev = MAKEDISKDEV(major(nrootdev),
641 1.26 thorpej DISKUNIT(nrootdev), 1);
642 1.26 thorpej }
643 1.54 enami dumpdv = defdumpdv;
644 1.26 thorpej break;
645 1.26 thorpej }
646 1.26 thorpej if (len == 4 && strcmp(buf, "none") == 0) {
647 1.55 enami dumpdv = NULL;
648 1.54 enami break;
649 1.26 thorpej }
650 1.51 thorpej dv = getdisk(buf, len, 1, &ndumpdev, 1);
651 1.55 enami if (dv != NULL) {
652 1.26 thorpej dumpdv = dv;
653 1.26 thorpej break;
654 1.26 thorpej }
655 1.26 thorpej }
656 1.26 thorpej
657 1.18 thorpej rootdev = nrootdev;
658 1.26 thorpej dumpdev = ndumpdev;
659 1.18 thorpej
660 1.36 thorpej for (vops = LIST_FIRST(&vfs_list); vops != NULL;
661 1.36 thorpej vops = LIST_NEXT(vops, vfs_list)) {
662 1.36 thorpej if (vops->vfs_mountroot != NULL &&
663 1.197 pgoyette strcmp(rootfstype, vops->vfs_name) == 0)
664 1.38 fvdl break;
665 1.18 thorpej }
666 1.36 thorpej
667 1.36 thorpej if (vops == NULL) {
668 1.18 thorpej deffsname = "generic";
669 1.18 thorpej } else
670 1.36 thorpej deffsname = vops->vfs_name;
671 1.36 thorpej
672 1.18 thorpej for (;;) {
673 1.18 thorpej printf("file system (default %s): ", deffsname);
674 1.64 itojun len = cngetsn(buf, sizeof(buf));
675 1.197 pgoyette if (len == 0) {
676 1.197 pgoyette if (strcmp(deffsname, "generic") == 0)
677 1.197 pgoyette rootfstype = ROOT_FSTYPE_ANY;
678 1.18 thorpej break;
679 1.197 pgoyette }
680 1.18 thorpej if (len == 4 && strcmp(buf, "halt") == 0)
681 1.23 gwr cpu_reboot(RB_HALT, NULL);
682 1.76 thorpej else if (len == 6 && strcmp(buf, "reboot") == 0)
683 1.76 thorpej cpu_reboot(0, NULL);
684 1.78 thorpej #if defined(DDB)
685 1.78 thorpej else if (len == 3 && strcmp(buf, "ddb") == 0) {
686 1.78 thorpej console_debugger();
687 1.78 thorpej }
688 1.78 thorpej #endif
689 1.18 thorpej else if (len == 7 && strcmp(buf, "generic") == 0) {
690 1.197 pgoyette rootfstype = ROOT_FSTYPE_ANY;
691 1.18 thorpej break;
692 1.18 thorpej }
693 1.18 thorpej vops = vfs_getopsbyname(buf);
694 1.18 thorpej if (vops == NULL || vops->vfs_mountroot == NULL) {
695 1.18 thorpej printf("use one of: generic");
696 1.36 thorpej for (vops = LIST_FIRST(&vfs_list);
697 1.36 thorpej vops != NULL;
698 1.36 thorpej vops = LIST_NEXT(vops, vfs_list)) {
699 1.36 thorpej if (vops->vfs_mountroot != NULL)
700 1.36 thorpej printf(" %s", vops->vfs_name);
701 1.36 thorpej }
702 1.197 pgoyette if (vops != NULL)
703 1.197 pgoyette vfs_delref(vops);
704 1.78 thorpej #if defined(DDB)
705 1.78 thorpej printf(" ddb");
706 1.78 thorpej #endif
707 1.76 thorpej printf(" halt reboot\n");
708 1.18 thorpej } else {
709 1.197 pgoyette /*
710 1.197 pgoyette * XXX If *vops gets freed between here and
711 1.197 pgoyette * the call to mountroot(), rootfstype will
712 1.197 pgoyette * point to something unexpected. But in
713 1.197 pgoyette * this case the system will fail anyway.
714 1.197 pgoyette */
715 1.197 pgoyette rootfstype = vops->vfs_name;
716 1.165 ad vfs_delref(vops);
717 1.18 thorpej break;
718 1.18 thorpej }
719 1.18 thorpej }
720 1.18 thorpej
721 1.18 thorpej } else if (rootspec == NULL) {
722 1.18 thorpej /*
723 1.18 thorpej * Wildcarded root; use the boot device.
724 1.18 thorpej */
725 1.26 thorpej rootdv = bootdv;
726 1.26 thorpej
727 1.188 christos if (bootdv)
728 1.188 christos majdev = devsw_name2blk(device_xname(bootdv), NULL, 0);
729 1.188 christos else
730 1.188 christos majdev = -1;
731 1.18 thorpej if (majdev >= 0) {
732 1.18 thorpej /*
733 1.113 thorpej * Root is on a disk. `bootpartition' is root,
734 1.113 thorpej * unless the device does not use partitions.
735 1.18 thorpej */
736 1.113 thorpej if (DEV_USES_PARTITIONS(bootdv))
737 1.135 thorpej rootdev = MAKEDISKDEV(majdev,
738 1.135 thorpej device_unit(bootdv),
739 1.135 thorpej bootpartition);
740 1.113 thorpej else
741 1.135 thorpej rootdev = makedev(majdev, device_unit(bootdv));
742 1.18 thorpej }
743 1.18 thorpej } else {
744 1.18 thorpej
745 1.18 thorpej /*
746 1.25 mrg * `root on <dev> ...'
747 1.18 thorpej */
748 1.18 thorpej
749 1.18 thorpej /*
750 1.18 thorpej * If it's a network interface, we can bail out
751 1.18 thorpej * early.
752 1.18 thorpej */
753 1.56 enami dv = finddevice(rootspec);
754 1.124 thorpej if (dv != NULL && device_class(dv) == DV_IFNET) {
755 1.26 thorpej rootdv = dv;
756 1.26 thorpej goto haveroot;
757 1.18 thorpej }
758 1.18 thorpej
759 1.159 dyoung if (rootdev == NODEV &&
760 1.159 dyoung device_class(dv) == DV_DISK && device_is_a(dv, "dk") &&
761 1.184 cegger (majdev = devsw_name2blk(device_xname(dv), NULL, 0)) >= 0)
762 1.159 dyoung rootdev = makedev(majdev, device_unit(dv));
763 1.159 dyoung
764 1.88 gehenna rootdevname = devsw_blk2name(major(rootdev));
765 1.18 thorpej if (rootdevname == NULL) {
766 1.198 christos printf("unknown device major 0x%llx\n",
767 1.198 christos (unsigned long long)rootdev);
768 1.18 thorpej boothowto |= RB_ASKNAME;
769 1.18 thorpej goto top;
770 1.18 thorpej }
771 1.41 perry memset(buf, 0, sizeof(buf));
772 1.198 christos snprintf(buf, sizeof(buf), "%s%llu", rootdevname,
773 1.198 christos (unsigned long long)DISKUNIT(rootdev));
774 1.18 thorpej
775 1.56 enami rootdv = finddevice(buf);
776 1.26 thorpej if (rootdv == NULL) {
777 1.198 christos printf("device %s (0x%llx) not configured\n",
778 1.198 christos buf, (unsigned long long)rootdev);
779 1.18 thorpej boothowto |= RB_ASKNAME;
780 1.18 thorpej goto top;
781 1.18 thorpej }
782 1.26 thorpej }
783 1.18 thorpej
784 1.26 thorpej haveroot:
785 1.18 thorpej
786 1.18 thorpej root_device = rootdv;
787 1.18 thorpej
788 1.124 thorpej switch (device_class(rootdv)) {
789 1.18 thorpej case DV_IFNET:
790 1.139 christos case DV_DISK:
791 1.184 cegger aprint_normal("root on %s", device_xname(rootdv));
792 1.140 christos if (DEV_USES_PARTITIONS(rootdv))
793 1.198 christos aprint_normal("%c", (int)DISKPART(rootdev) + 'a');
794 1.18 thorpej break;
795 1.18 thorpej
796 1.18 thorpej default:
797 1.18 thorpej printf("can't determine root device\n");
798 1.18 thorpej boothowto |= RB_ASKNAME;
799 1.18 thorpej goto top;
800 1.18 thorpej }
801 1.26 thorpej
802 1.26 thorpej /*
803 1.26 thorpej * Now configure the dump device.
804 1.55 enami *
805 1.26 thorpej * If we haven't figured out the dump device, do so, with
806 1.26 thorpej * the following rules:
807 1.26 thorpej *
808 1.26 thorpej * (a) We already know dumpdv in the RB_ASKNAME case.
809 1.26 thorpej *
810 1.26 thorpej * (b) If dumpspec is set, try to use it. If the device
811 1.26 thorpej * is not available, punt.
812 1.26 thorpej *
813 1.26 thorpej * (c) If dumpspec is not set, the dump device is
814 1.26 thorpej * wildcarded or unspecified. If the root device
815 1.26 thorpej * is DV_IFNET, punt. Otherwise, use partition b
816 1.26 thorpej * of the root device.
817 1.26 thorpej */
818 1.26 thorpej
819 1.55 enami if (boothowto & RB_ASKNAME) { /* (a) */
820 1.55 enami if (dumpdv == NULL)
821 1.55 enami goto nodumpdev;
822 1.55 enami } else if (dumpspec != NULL) { /* (b) */
823 1.55 enami if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
824 1.26 thorpej /*
825 1.55 enami * Operator doesn't want a dump device.
826 1.55 enami * Or looks like they tried to pick a network
827 1.26 thorpej * device. Oops.
828 1.26 thorpej */
829 1.26 thorpej goto nodumpdev;
830 1.26 thorpej }
831 1.26 thorpej
832 1.88 gehenna dumpdevname = devsw_blk2name(major(dumpdev));
833 1.26 thorpej if (dumpdevname == NULL)
834 1.26 thorpej goto nodumpdev;
835 1.41 perry memset(buf, 0, sizeof(buf));
836 1.198 christos snprintf(buf, sizeof(buf), "%s%llu", dumpdevname,
837 1.198 christos (unsigned long long)DISKUNIT(dumpdev));
838 1.26 thorpej
839 1.56 enami dumpdv = finddevice(buf);
840 1.56 enami if (dumpdv == NULL) {
841 1.26 thorpej /*
842 1.26 thorpej * Device not configured.
843 1.26 thorpej */
844 1.26 thorpej goto nodumpdev;
845 1.26 thorpej }
846 1.55 enami } else { /* (c) */
847 1.160 christos if (DEV_USES_PARTITIONS(rootdv) == 0) {
848 1.203 dyoung for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
849 1.203 dyoung dv != NULL;
850 1.203 dyoung dv = deviter_next(&di))
851 1.160 christos if (isswap(dv))
852 1.160 christos break;
853 1.203 dyoung deviter_release(&di);
854 1.160 christos if (dv == NULL)
855 1.160 christos goto nodumpdev;
856 1.160 christos
857 1.184 cegger majdev = devsw_name2blk(device_xname(dv), NULL, 0);
858 1.160 christos if (majdev < 0)
859 1.160 christos goto nodumpdev;
860 1.160 christos dumpdv = dv;
861 1.160 christos dumpdev = makedev(majdev, device_unit(dumpdv));
862 1.160 christos } else {
863 1.55 enami dumpdv = rootdv;
864 1.55 enami dumpdev = MAKEDISKDEV(major(rootdev),
865 1.135 thorpej device_unit(dumpdv), 1);
866 1.55 enami }
867 1.26 thorpej }
868 1.26 thorpej
869 1.175 ad dumpcdev = devsw_blk2chr(dumpdev);
870 1.184 cegger aprint_normal(" dumps on %s", device_xname(dumpdv));
871 1.140 christos if (DEV_USES_PARTITIONS(dumpdv))
872 1.198 christos aprint_normal("%c", (int)DISKPART(dumpdev) + 'a');
873 1.140 christos aprint_normal("\n");
874 1.26 thorpej return;
875 1.26 thorpej
876 1.26 thorpej nodumpdev:
877 1.26 thorpej dumpdev = NODEV;
878 1.175 ad dumpcdev = NODEV;
879 1.100 thorpej aprint_normal("\n");
880 1.18 thorpej }
881 1.18 thorpej
882 1.200 dyoung static device_t
883 1.117 thorpej finddevice(const char *name)
884 1.56 enami {
885 1.159 dyoung const char *wname;
886 1.108 christos
887 1.159 dyoung if ((wname = getwedgename(name, strlen(name))) != NULL)
888 1.159 dyoung return dkwedge_find_by_wname(wname);
889 1.159 dyoung
890 1.179 joerg return device_find_by_xname(name);
891 1.56 enami }
892 1.56 enami
893 1.200 dyoung static device_t
894 1.117 thorpej getdisk(char *str, int len, int defpart, dev_t *devp, int isdump)
895 1.18 thorpej {
896 1.200 dyoung device_t dv;
897 1.203 dyoung deviter_t di;
898 1.18 thorpej
899 1.51 thorpej if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
900 1.18 thorpej printf("use one of:");
901 1.203 dyoung for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
902 1.203 dyoung dv = deviter_next(&di)) {
903 1.113 thorpej if (DEV_USES_PARTITIONS(dv))
904 1.184 cegger printf(" %s[a-%c]", device_xname(dv),
905 1.19 cgd 'a' + MAXPARTITIONS - 1);
906 1.124 thorpej else if (device_class(dv) == DV_DISK)
907 1.184 cegger printf(" %s", device_xname(dv));
908 1.124 thorpej if (isdump == 0 && device_class(dv) == DV_IFNET)
909 1.184 cegger printf(" %s", device_xname(dv));
910 1.18 thorpej }
911 1.203 dyoung deviter_release(&di);
912 1.159 dyoung dkwedge_print_wnames();
913 1.26 thorpej if (isdump)
914 1.26 thorpej printf(" none");
915 1.78 thorpej #if defined(DDB)
916 1.78 thorpej printf(" ddb");
917 1.78 thorpej #endif
918 1.76 thorpej printf(" halt reboot\n");
919 1.18 thorpej }
920 1.159 dyoung return dv;
921 1.159 dyoung }
922 1.159 dyoung
923 1.159 dyoung static const char *
924 1.159 dyoung getwedgename(const char *name, int namelen)
925 1.159 dyoung {
926 1.159 dyoung const char *wpfx = "wedge:";
927 1.159 dyoung const int wpfxlen = strlen(wpfx);
928 1.159 dyoung
929 1.159 dyoung if (namelen < wpfxlen || strncmp(name, wpfx, wpfxlen) != 0)
930 1.159 dyoung return NULL;
931 1.159 dyoung
932 1.159 dyoung return name + wpfxlen;
933 1.18 thorpej }
934 1.18 thorpej
935 1.200 dyoung static device_t
936 1.117 thorpej parsedisk(char *str, int len, int defpart, dev_t *devp)
937 1.18 thorpej {
938 1.200 dyoung device_t dv;
939 1.159 dyoung const char *wname;
940 1.18 thorpej char *cp, c;
941 1.29 drochner int majdev, part;
942 1.18 thorpej if (len == 0)
943 1.18 thorpej return (NULL);
944 1.18 thorpej
945 1.18 thorpej if (len == 4 && strcmp(str, "halt") == 0)
946 1.23 gwr cpu_reboot(RB_HALT, NULL);
947 1.76 thorpej else if (len == 6 && strcmp(str, "reboot") == 0)
948 1.76 thorpej cpu_reboot(0, NULL);
949 1.78 thorpej #if defined(DDB)
950 1.78 thorpej else if (len == 3 && strcmp(str, "ddb") == 0)
951 1.78 thorpej console_debugger();
952 1.78 thorpej #endif
953 1.18 thorpej
954 1.18 thorpej cp = str + len - 1;
955 1.18 thorpej c = *cp;
956 1.159 dyoung
957 1.159 dyoung if ((wname = getwedgename(str, len)) != NULL) {
958 1.159 dyoung if ((dv = dkwedge_find_by_wname(wname)) == NULL)
959 1.159 dyoung return NULL;
960 1.159 dyoung part = defpart;
961 1.159 dyoung goto gotdisk;
962 1.159 dyoung } else if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
963 1.18 thorpej part = c - 'a';
964 1.18 thorpej *cp = '\0';
965 1.18 thorpej } else
966 1.18 thorpej part = defpart;
967 1.18 thorpej
968 1.56 enami dv = finddevice(str);
969 1.56 enami if (dv != NULL) {
970 1.124 thorpej if (device_class(dv) == DV_DISK) {
971 1.18 thorpej gotdisk:
972 1.184 cegger majdev = devsw_name2blk(device_xname(dv), NULL, 0);
973 1.18 thorpej if (majdev < 0)
974 1.18 thorpej panic("parsedisk");
975 1.113 thorpej if (DEV_USES_PARTITIONS(dv))
976 1.135 thorpej *devp = MAKEDISKDEV(majdev, device_unit(dv),
977 1.135 thorpej part);
978 1.113 thorpej else
979 1.135 thorpej *devp = makedev(majdev, device_unit(dv));
980 1.18 thorpej }
981 1.18 thorpej
982 1.124 thorpej if (device_class(dv) == DV_IFNET)
983 1.18 thorpej *devp = NODEV;
984 1.18 thorpej }
985 1.18 thorpej
986 1.18 thorpej *cp = c;
987 1.18 thorpej return (dv);
988 1.48 lukem }
989 1.48 lukem
990 1.48 lukem /*
991 1.154 thorpej * Return true if system call tracing is enabled for the specified process.
992 1.130 thorpej */
993 1.153 thorpej bool
994 1.130 thorpej trace_is_enabled(struct proc *p)
995 1.130 thorpej {
996 1.131 thorpej #ifdef SYSCALL_DEBUG
997 1.154 thorpej return (true);
998 1.131 thorpej #endif
999 1.130 thorpej #ifdef KTRACE
1000 1.130 thorpej if (ISSET(p->p_traceflag, (KTRFAC_SYSCALL | KTRFAC_SYSRET)))
1001 1.154 thorpej return (true);
1002 1.130 thorpej #endif
1003 1.142 matt #ifdef PTRACE
1004 1.151 ad if (ISSET(p->p_slflag, PSL_SYSCALL))
1005 1.154 thorpej return (true);
1006 1.142 matt #endif
1007 1.130 thorpej
1008 1.154 thorpej return (false);
1009 1.130 thorpej }
1010 1.130 thorpej
1011 1.130 thorpej /*
1012 1.92 jdolecek * Start trace of particular system call. If process is being traced,
1013 1.92 jdolecek * this routine is called by MD syscall dispatch code just before
1014 1.92 jdolecek * a system call is actually executed.
1015 1.92 jdolecek */
1016 1.81 christos int
1017 1.177 dsl trace_enter(register_t code, const register_t *args, int narg)
1018 1.81 christos {
1019 1.81 christos #ifdef SYSCALL_DEBUG
1020 1.171 dsl scdebug_call(code, args);
1021 1.81 christos #endif /* SYSCALL_DEBUG */
1022 1.81 christos
1023 1.177 dsl ktrsyscall(code, args, narg);
1024 1.81 christos
1025 1.142 matt #ifdef PTRACE
1026 1.172 dsl if ((curlwp->l_proc->p_slflag & (PSL_SYSCALL|PSL_TRACED)) ==
1027 1.151 ad (PSL_SYSCALL|PSL_TRACED))
1028 1.172 dsl process_stoptrace();
1029 1.142 matt #endif
1030 1.81 christos return 0;
1031 1.81 christos }
1032 1.81 christos
1033 1.92 jdolecek /*
1034 1.92 jdolecek * End trace of particular system call. If process is being traced,
1035 1.92 jdolecek * this routine is called by MD syscall dispatch code just after
1036 1.92 jdolecek * a system call finishes.
1037 1.92 jdolecek * MD caller guarantees the passed 'code' is within the supported
1038 1.92 jdolecek * system call number range for emulation the process runs under.
1039 1.92 jdolecek */
1040 1.81 christos void
1041 1.177 dsl trace_exit(register_t code, register_t rval[], int error)
1042 1.81 christos {
1043 1.81 christos #ifdef SYSCALL_DEBUG
1044 1.171 dsl scdebug_ret(code, error, rval);
1045 1.81 christos #endif /* SYSCALL_DEBUG */
1046 1.81 christos
1047 1.164 ad ktrsysret(code, error, rval);
1048 1.128 christos
1049 1.142 matt #ifdef PTRACE
1050 1.172 dsl if ((curlwp->l_proc->p_slflag & (PSL_SYSCALL|PSL_TRACED)) ==
1051 1.151 ad (PSL_SYSCALL|PSL_TRACED))
1052 1.172 dsl process_stoptrace();
1053 1.142 matt #endif
1054 1.10 cgd }
1055 1.194 ad
1056 1.194 ad int
1057 1.194 ad syscall_establish(const struct emul *em, const struct syscall_package *sp)
1058 1.194 ad {
1059 1.194 ad struct sysent *sy;
1060 1.194 ad int i;
1061 1.194 ad
1062 1.194 ad KASSERT(mutex_owned(&module_lock));
1063 1.194 ad
1064 1.194 ad if (em == NULL) {
1065 1.194 ad em = &emul_netbsd;
1066 1.194 ad }
1067 1.194 ad sy = em->e_sysent;
1068 1.194 ad
1069 1.194 ad /*
1070 1.194 ad * Ensure that all preconditions are valid, since this is
1071 1.194 ad * an all or nothing deal. Once a system call is entered,
1072 1.194 ad * it can become busy and we could be unable to remove it
1073 1.194 ad * on error.
1074 1.194 ad */
1075 1.194 ad for (i = 0; sp[i].sp_call != NULL; i++) {
1076 1.194 ad if (sy[sp[i].sp_code].sy_call != sys_nomodule) {
1077 1.196 christos #ifdef DIAGNOSTIC
1078 1.196 christos printf("syscall %d is busy\n", sp[i].sp_code);
1079 1.196 christos #endif
1080 1.194 ad return EBUSY;
1081 1.194 ad }
1082 1.194 ad }
1083 1.194 ad /* Everything looks good, patch them in. */
1084 1.194 ad for (i = 0; sp[i].sp_call != NULL; i++) {
1085 1.194 ad sy[sp[i].sp_code].sy_call = sp[i].sp_call;
1086 1.194 ad }
1087 1.194 ad
1088 1.194 ad return 0;
1089 1.194 ad }
1090 1.194 ad
1091 1.194 ad int
1092 1.194 ad syscall_disestablish(const struct emul *em, const struct syscall_package *sp)
1093 1.194 ad {
1094 1.194 ad struct sysent *sy;
1095 1.194 ad uint64_t where;
1096 1.194 ad lwp_t *l;
1097 1.194 ad int i;
1098 1.194 ad
1099 1.194 ad KASSERT(mutex_owned(&module_lock));
1100 1.194 ad
1101 1.194 ad if (em == NULL) {
1102 1.194 ad em = &emul_netbsd;
1103 1.194 ad }
1104 1.194 ad sy = em->e_sysent;
1105 1.194 ad
1106 1.194 ad /*
1107 1.194 ad * First, patch the system calls to sys_nomodule to gate further
1108 1.194 ad * activity.
1109 1.194 ad */
1110 1.194 ad for (i = 0; sp[i].sp_call != NULL; i++) {
1111 1.194 ad KASSERT(sy[sp[i].sp_code].sy_call == sp[i].sp_call);
1112 1.194 ad sy[sp[i].sp_code].sy_call = sys_nomodule;
1113 1.194 ad }
1114 1.194 ad
1115 1.194 ad /*
1116 1.194 ad * Run a cross call to cycle through all CPUs. This does two
1117 1.194 ad * things: lock activity provides a barrier and makes our update
1118 1.194 ad * of sy_call visible to all CPUs, and upon return we can be sure
1119 1.194 ad * that we see pertinent values of l_sysent posted by remote CPUs.
1120 1.194 ad */
1121 1.194 ad where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
1122 1.194 ad xc_wait(where);
1123 1.194 ad
1124 1.194 ad /*
1125 1.194 ad * Now it's safe to check l_sysent. Run through all LWPs and see
1126 1.194 ad * if anyone is still using the system call.
1127 1.194 ad */
1128 1.194 ad for (i = 0; sp[i].sp_call != NULL; i++) {
1129 1.194 ad mutex_enter(proc_lock);
1130 1.194 ad LIST_FOREACH(l, &alllwp, l_list) {
1131 1.194 ad if (l->l_sysent == &sy[sp[i].sp_code]) {
1132 1.194 ad break;
1133 1.194 ad }
1134 1.194 ad }
1135 1.194 ad mutex_exit(proc_lock);
1136 1.194 ad if (l == NULL) {
1137 1.194 ad continue;
1138 1.194 ad }
1139 1.194 ad /*
1140 1.194 ad * We lose: one or more calls are still in use. Put back
1141 1.194 ad * the old entrypoints and act like nothing happened.
1142 1.194 ad * When we drop module_lock, any system calls held in
1143 1.194 ad * sys_nomodule() will be restarted.
1144 1.194 ad */
1145 1.194 ad for (i = 0; sp[i].sp_call != NULL; i++) {
1146 1.194 ad sy[sp[i].sp_code].sy_call = sp[i].sp_call;
1147 1.194 ad }
1148 1.194 ad return EBUSY;
1149 1.194 ad }
1150 1.194 ad
1151 1.194 ad return 0;
1152 1.194 ad }
1153