xref: /netbsd-src/sys/kern/kern_subr.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: kern_subr.c,v 1.207 2010/04/14 14:46:59 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center, and by Luke Mewburn.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1991, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  * (c) UNIX System Laboratories, Inc.
37  * All or some portions of this file are derived from material licensed
38  * to the University of California by American Telephone and Telegraph
39  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40  * the permission of UNIX System Laboratories, Inc.
41  *
42  * Copyright (c) 1992, 1993
43  *	The Regents of the University of California.  All rights reserved.
44  *
45  * This software was developed by the Computer Systems Engineering group
46  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
47  * contributed to Berkeley.
48  *
49  * All advertising materials mentioning features or use of this software
50  * must display the following acknowledgement:
51  *	This product includes software developed by the University of
52  *	California, Lawrence Berkeley Laboratory.
53  *
54  * Redistribution and use in source and binary forms, with or without
55  * modification, are permitted provided that the following conditions
56  * are met:
57  * 1. Redistributions of source code must retain the above copyright
58  *    notice, this list of conditions and the following disclaimer.
59  * 2. Redistributions in binary form must reproduce the above copyright
60  *    notice, this list of conditions and the following disclaimer in the
61  *    documentation and/or other materials provided with the distribution.
62  * 3. Neither the name of the University nor the names of its contributors
63  *    may be used to endorse or promote products derived from this software
64  *    without specific prior written permission.
65  *
66  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76  * SUCH DAMAGE.
77  *
78  *	@(#)kern_subr.c	8.4 (Berkeley) 2/14/95
79  */
80 
81 #include <sys/cdefs.h>
82 __KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.207 2010/04/14 14:46:59 pooka Exp $");
83 
84 #include "opt_ddb.h"
85 #include "opt_md.h"
86 #include "opt_syscall_debug.h"
87 #include "opt_ktrace.h"
88 #include "opt_ptrace.h"
89 #include "opt_tftproot.h"
90 
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/proc.h>
94 #include <sys/mount.h>
95 #include <sys/device.h>
96 #include <sys/reboot.h>
97 #include <sys/conf.h>
98 #include <sys/disk.h>
99 #include <sys/disklabel.h>
100 #include <sys/queue.h>
101 #include <sys/ktrace.h>
102 #include <sys/ptrace.h>
103 #include <sys/fcntl.h>
104 #include <sys/kauth.h>
105 #include <sys/vnode.h>
106 #include <sys/module.h>
107 
108 #include <uvm/uvm_extern.h>
109 
110 #include <dev/cons.h>
111 
112 #include <net/if.h>
113 
114 /* XXX these should eventually move to subr_autoconf.c */
115 static device_t finddevice(const char *);
116 static device_t getdisk(char *, int, int, dev_t *, int);
117 static device_t parsedisk(char *, int, int, dev_t *);
118 static const char *getwedgename(const char *, int);
119 
120 #ifdef TFTPROOT
121 int tftproot_dhcpboot(device_t);
122 #endif
123 
124 dev_t	dumpcdev;	/* for savecore */
125 
126 static int
127 isswap(device_t dv)
128 {
129 	struct dkwedge_info wi;
130 	struct vnode *vn;
131 	int error;
132 
133 	if (device_class(dv) != DV_DISK || !device_is_a(dv, "dk"))
134 		return 0;
135 
136 	if ((vn = opendisk(dv)) == NULL)
137 		return 0;
138 
139 	error = VOP_IOCTL(vn, DIOCGWEDGEINFO, &wi, FREAD, NOCRED);
140 	VOP_CLOSE(vn, FREAD, NOCRED);
141 	vput(vn);
142 	if (error) {
143 #ifdef DEBUG_WEDGE
144 		printf("%s: Get wedge info returned %d\n", device_xname(dv), error);
145 #endif
146 		return 0;
147 	}
148 	return strcmp(wi.dkw_ptype, DKW_PTYPE_SWAP) == 0;
149 }
150 
151 /*
152  * Determine the root device and, if instructed to, the root file system.
153  */
154 
155 #include "md.h"
156 
157 #if NMD > 0
158 extern struct cfdriver md_cd;
159 #ifdef MEMORY_DISK_IS_ROOT
160 int md_is_root = 1;
161 #else
162 int md_is_root = 0;
163 #endif
164 #endif
165 
166 /*
167  * The device and wedge that we booted from.  If booted_wedge is NULL,
168  * the we might consult booted_partition.
169  */
170 device_t booted_device;
171 device_t booted_wedge;
172 int booted_partition;
173 
174 /*
175  * Use partition letters if it's a disk class but not a wedge.
176  * XXX Check for wedge is kinda gross.
177  */
178 #define	DEV_USES_PARTITIONS(dv)						\
179 	(device_class((dv)) == DV_DISK &&				\
180 	 !device_is_a((dv), "dk"))
181 
182 void
183 setroot(device_t bootdv, int bootpartition)
184 {
185 	device_t dv;
186 	deviter_t di;
187 	int len, majdev;
188 	dev_t nrootdev;
189 	dev_t ndumpdev = NODEV;
190 	char buf[128];
191 	const char *rootdevname;
192 	const char *dumpdevname;
193 	device_t rootdv = NULL;		/* XXX gcc -Wuninitialized */
194 	device_t dumpdv = NULL;
195 	struct ifnet *ifp;
196 	const char *deffsname;
197 	struct vfsops *vops;
198 
199 #ifdef TFTPROOT
200 	if (tftproot_dhcpboot(bootdv) != 0)
201 		boothowto |= RB_ASKNAME;
202 #endif
203 
204 #if NMD > 0
205 	if (md_is_root) {
206 		/*
207 		 * XXX there should be "root on md0" in the config file,
208 		 * but it isn't always
209 		 */
210 		bootdv = md_cd.cd_devs[0];
211 		bootpartition = 0;
212 	}
213 #endif
214 
215 	/*
216 	 * If NFS is specified as the file system, and we found
217 	 * a DV_DISK boot device (or no boot device at all), then
218 	 * find a reasonable network interface for "rootspec".
219 	 */
220 	vops = vfs_getopsbyname(MOUNT_NFS);
221 	if (vops != NULL && strcmp(rootfstype, MOUNT_NFS) == 0 &&
222 	    rootspec == NULL &&
223 	    (bootdv == NULL || device_class(bootdv) != DV_IFNET)) {
224 		IFNET_FOREACH(ifp) {
225 			if ((ifp->if_flags &
226 			     (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
227 				break;
228 		}
229 		if (ifp == NULL) {
230 			/*
231 			 * Can't find a suitable interface; ask the
232 			 * user.
233 			 */
234 			boothowto |= RB_ASKNAME;
235 		} else {
236 			/*
237 			 * Have a suitable interface; behave as if
238 			 * the user specified this interface.
239 			 */
240 			rootspec = (const char *)ifp->if_xname;
241 		}
242 	}
243 	if (vops != NULL)
244 		vfs_delref(vops);
245 
246 	/*
247 	 * If wildcarded root and we the boot device wasn't determined,
248 	 * ask the user.
249 	 */
250 	if (rootspec == NULL && bootdv == NULL)
251 		boothowto |= RB_ASKNAME;
252 
253  top:
254 	if (boothowto & RB_ASKNAME) {
255 		device_t defdumpdv;
256 
257 		for (;;) {
258 			printf("root device");
259 			if (bootdv != NULL) {
260 				printf(" (default %s", device_xname(bootdv));
261 				if (DEV_USES_PARTITIONS(bootdv))
262 					printf("%c", bootpartition + 'a');
263 				printf(")");
264 			}
265 			printf(": ");
266 			len = cngetsn(buf, sizeof(buf));
267 			if (len == 0 && bootdv != NULL) {
268 				strlcpy(buf, device_xname(bootdv), sizeof(buf));
269 				len = strlen(buf);
270 			}
271 			if (len > 0 && buf[len - 1] == '*') {
272 				buf[--len] = '\0';
273 				dv = getdisk(buf, len, 1, &nrootdev, 0);
274 				if (dv != NULL) {
275 					rootdv = dv;
276 					break;
277 				}
278 			}
279 			dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
280 			if (dv != NULL) {
281 				rootdv = dv;
282 				break;
283 			}
284 		}
285 
286 		/*
287 		 * Set up the default dump device.  If root is on
288 		 * a network device, there is no default dump
289 		 * device, since we don't support dumps to the
290 		 * network.
291 		 */
292 		if (DEV_USES_PARTITIONS(rootdv) == 0)
293 			defdumpdv = NULL;
294 		else
295 			defdumpdv = rootdv;
296 
297 		for (;;) {
298 			printf("dump device");
299 			if (defdumpdv != NULL) {
300 				/*
301 				 * Note, we know it's a disk if we get here.
302 				 */
303 				printf(" (default %sb)", device_xname(defdumpdv));
304 			}
305 			printf(": ");
306 			len = cngetsn(buf, sizeof(buf));
307 			if (len == 0) {
308 				if (defdumpdv != NULL) {
309 					ndumpdev = MAKEDISKDEV(major(nrootdev),
310 					    DISKUNIT(nrootdev), 1);
311 				}
312 				dumpdv = defdumpdv;
313 				break;
314 			}
315 			if (len == 4 && strcmp(buf, "none") == 0) {
316 				dumpdv = NULL;
317 				break;
318 			}
319 			dv = getdisk(buf, len, 1, &ndumpdev, 1);
320 			if (dv != NULL) {
321 				dumpdv = dv;
322 				break;
323 			}
324 		}
325 
326 		rootdev = nrootdev;
327 		dumpdev = ndumpdev;
328 
329 		for (vops = LIST_FIRST(&vfs_list); vops != NULL;
330 		     vops = LIST_NEXT(vops, vfs_list)) {
331 			if (vops->vfs_mountroot != NULL &&
332 			    strcmp(rootfstype, vops->vfs_name) == 0)
333 			break;
334 		}
335 
336 		if (vops == NULL) {
337 			deffsname = "generic";
338 		} else
339 			deffsname = vops->vfs_name;
340 
341 		for (;;) {
342 			printf("file system (default %s): ", deffsname);
343 			len = cngetsn(buf, sizeof(buf));
344 			if (len == 0) {
345 				if (strcmp(deffsname, "generic") == 0)
346 					rootfstype = ROOT_FSTYPE_ANY;
347 				break;
348 			}
349 			if (len == 4 && strcmp(buf, "halt") == 0)
350 				cpu_reboot(RB_HALT, NULL);
351 			else if (len == 6 && strcmp(buf, "reboot") == 0)
352 				cpu_reboot(0, NULL);
353 #if defined(DDB)
354 			else if (len == 3 && strcmp(buf, "ddb") == 0) {
355 				console_debugger();
356 			}
357 #endif
358 			else if (len == 7 && strcmp(buf, "generic") == 0) {
359 				rootfstype = ROOT_FSTYPE_ANY;
360 				break;
361 			}
362 			vops = vfs_getopsbyname(buf);
363 			if (vops == NULL || vops->vfs_mountroot == NULL) {
364 				printf("use one of: generic");
365 				for (vops = LIST_FIRST(&vfs_list);
366 				     vops != NULL;
367 				     vops = LIST_NEXT(vops, vfs_list)) {
368 					if (vops->vfs_mountroot != NULL)
369 						printf(" %s", vops->vfs_name);
370 				}
371 				if (vops != NULL)
372 					vfs_delref(vops);
373 #if defined(DDB)
374 				printf(" ddb");
375 #endif
376 				printf(" halt reboot\n");
377 			} else {
378 				/*
379 				 * XXX If *vops gets freed between here and
380 				 * the call to mountroot(), rootfstype will
381 				 * point to something unexpected.  But in
382 				 * this case the system will fail anyway.
383 				 */
384 				rootfstype = vops->vfs_name;
385 				vfs_delref(vops);
386 				break;
387 			}
388 		}
389 
390 	} else if (rootspec == NULL) {
391 		/*
392 		 * Wildcarded root; use the boot device.
393 		 */
394 		rootdv = bootdv;
395 
396 		if (bootdv)
397 			majdev = devsw_name2blk(device_xname(bootdv), NULL, 0);
398 		else
399 			majdev = -1;
400 		if (majdev >= 0) {
401 			/*
402 			 * Root is on a disk.  `bootpartition' is root,
403 			 * unless the device does not use partitions.
404 			 */
405 			if (DEV_USES_PARTITIONS(bootdv))
406 				rootdev = MAKEDISKDEV(majdev,
407 						      device_unit(bootdv),
408 						      bootpartition);
409 			else
410 				rootdev = makedev(majdev, device_unit(bootdv));
411 		}
412 	} else {
413 
414 		/*
415 		 * `root on <dev> ...'
416 		 */
417 
418 		/*
419 		 * If it's a network interface, we can bail out
420 		 * early.
421 		 */
422 		dv = finddevice(rootspec);
423 		if (dv != NULL && device_class(dv) == DV_IFNET) {
424 			rootdv = dv;
425 			goto haveroot;
426 		}
427 
428 		if (rootdev == NODEV &&
429 		    device_class(dv) == DV_DISK && device_is_a(dv, "dk") &&
430 		    (majdev = devsw_name2blk(device_xname(dv), NULL, 0)) >= 0)
431 			rootdev = makedev(majdev, device_unit(dv));
432 
433 		rootdevname = devsw_blk2name(major(rootdev));
434 		if (rootdevname == NULL) {
435 			printf("unknown device major 0x%llx\n",
436 			    (unsigned long long)rootdev);
437 			boothowto |= RB_ASKNAME;
438 			goto top;
439 		}
440 		memset(buf, 0, sizeof(buf));
441 		snprintf(buf, sizeof(buf), "%s%llu", rootdevname,
442 		    (unsigned long long)DISKUNIT(rootdev));
443 
444 		rootdv = finddevice(buf);
445 		if (rootdv == NULL) {
446 			printf("device %s (0x%llx) not configured\n",
447 			    buf, (unsigned long long)rootdev);
448 			boothowto |= RB_ASKNAME;
449 			goto top;
450 		}
451 	}
452 
453  haveroot:
454 
455 	root_device = rootdv;
456 
457 	switch (device_class(rootdv)) {
458 	case DV_IFNET:
459 	case DV_DISK:
460 		aprint_normal("root on %s", device_xname(rootdv));
461 		if (DEV_USES_PARTITIONS(rootdv))
462 			aprint_normal("%c", (int)DISKPART(rootdev) + 'a');
463 		break;
464 
465 	default:
466 		printf("can't determine root device\n");
467 		boothowto |= RB_ASKNAME;
468 		goto top;
469 	}
470 
471 	/*
472 	 * Now configure the dump device.
473 	 *
474 	 * If we haven't figured out the dump device, do so, with
475 	 * the following rules:
476 	 *
477 	 *	(a) We already know dumpdv in the RB_ASKNAME case.
478 	 *
479 	 *	(b) If dumpspec is set, try to use it.  If the device
480 	 *	    is not available, punt.
481 	 *
482 	 *	(c) If dumpspec is not set, the dump device is
483 	 *	    wildcarded or unspecified.  If the root device
484 	 *	    is DV_IFNET, punt.  Otherwise, use partition b
485 	 *	    of the root device.
486 	 */
487 
488 	if (boothowto & RB_ASKNAME) {		/* (a) */
489 		if (dumpdv == NULL)
490 			goto nodumpdev;
491 	} else if (dumpspec != NULL) {		/* (b) */
492 		if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
493 			/*
494 			 * Operator doesn't want a dump device.
495 			 * Or looks like they tried to pick a network
496 			 * device.  Oops.
497 			 */
498 			goto nodumpdev;
499 		}
500 
501 		dumpdevname = devsw_blk2name(major(dumpdev));
502 		if (dumpdevname == NULL)
503 			goto nodumpdev;
504 		memset(buf, 0, sizeof(buf));
505 		snprintf(buf, sizeof(buf), "%s%llu", dumpdevname,
506 		    (unsigned long long)DISKUNIT(dumpdev));
507 
508 		dumpdv = finddevice(buf);
509 		if (dumpdv == NULL) {
510 			/*
511 			 * Device not configured.
512 			 */
513 			goto nodumpdev;
514 		}
515 	} else {				/* (c) */
516 		if (DEV_USES_PARTITIONS(rootdv) == 0) {
517 			for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
518 			     dv != NULL;
519 			     dv = deviter_next(&di))
520 				if (isswap(dv))
521 					break;
522 			deviter_release(&di);
523 			if (dv == NULL)
524 				goto nodumpdev;
525 
526 			majdev = devsw_name2blk(device_xname(dv), NULL, 0);
527 			if (majdev < 0)
528 				goto nodumpdev;
529 			dumpdv = dv;
530 			dumpdev = makedev(majdev, device_unit(dumpdv));
531 		} else {
532 			dumpdv = rootdv;
533 			dumpdev = MAKEDISKDEV(major(rootdev),
534 			    device_unit(dumpdv), 1);
535 		}
536 	}
537 
538 	dumpcdev = devsw_blk2chr(dumpdev);
539 	aprint_normal(" dumps on %s", device_xname(dumpdv));
540 	if (DEV_USES_PARTITIONS(dumpdv))
541 		aprint_normal("%c", (int)DISKPART(dumpdev) + 'a');
542 	aprint_normal("\n");
543 	return;
544 
545  nodumpdev:
546 	dumpdev = NODEV;
547 	dumpcdev = NODEV;
548 	aprint_normal("\n");
549 }
550 
551 static device_t
552 finddevice(const char *name)
553 {
554 	const char *wname;
555 
556 	if ((wname = getwedgename(name, strlen(name))) != NULL)
557 		return dkwedge_find_by_wname(wname);
558 
559 	return device_find_by_xname(name);
560 }
561 
562 static device_t
563 getdisk(char *str, int len, int defpart, dev_t *devp, int isdump)
564 {
565 	device_t dv;
566 	deviter_t di;
567 
568 	if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
569 		printf("use one of:");
570 		for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
571 		     dv = deviter_next(&di)) {
572 			if (DEV_USES_PARTITIONS(dv))
573 				printf(" %s[a-%c]", device_xname(dv),
574 				    'a' + MAXPARTITIONS - 1);
575 			else if (device_class(dv) == DV_DISK)
576 				printf(" %s", device_xname(dv));
577 			if (isdump == 0 && device_class(dv) == DV_IFNET)
578 				printf(" %s", device_xname(dv));
579 		}
580 		deviter_release(&di);
581 		dkwedge_print_wnames();
582 		if (isdump)
583 			printf(" none");
584 #if defined(DDB)
585 		printf(" ddb");
586 #endif
587 		printf(" halt reboot\n");
588 	}
589 	return dv;
590 }
591 
592 static const char *
593 getwedgename(const char *name, int namelen)
594 {
595 	const char *wpfx = "wedge:";
596 	const int wpfxlen = strlen(wpfx);
597 
598 	if (namelen < wpfxlen || strncmp(name, wpfx, wpfxlen) != 0)
599 		return NULL;
600 
601 	return name + wpfxlen;
602 }
603 
604 static device_t
605 parsedisk(char *str, int len, int defpart, dev_t *devp)
606 {
607 	device_t dv;
608 	const char *wname;
609 	char *cp, c;
610 	int majdev, part;
611 	if (len == 0)
612 		return (NULL);
613 
614 	if (len == 4 && strcmp(str, "halt") == 0)
615 		cpu_reboot(RB_HALT, NULL);
616 	else if (len == 6 && strcmp(str, "reboot") == 0)
617 		cpu_reboot(0, NULL);
618 #if defined(DDB)
619 	else if (len == 3 && strcmp(str, "ddb") == 0)
620 		console_debugger();
621 #endif
622 
623 	cp = str + len - 1;
624 	c = *cp;
625 
626 	if ((wname = getwedgename(str, len)) != NULL) {
627 		if ((dv = dkwedge_find_by_wname(wname)) == NULL)
628 			return NULL;
629 		part = defpart;
630 		goto gotdisk;
631 	} else if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
632 		part = c - 'a';
633 		*cp = '\0';
634 	} else
635 		part = defpart;
636 
637 	dv = finddevice(str);
638 	if (dv != NULL) {
639 		if (device_class(dv) == DV_DISK) {
640  gotdisk:
641 			majdev = devsw_name2blk(device_xname(dv), NULL, 0);
642 			if (majdev < 0)
643 				panic("parsedisk");
644 			if (DEV_USES_PARTITIONS(dv))
645 				*devp = MAKEDISKDEV(majdev, device_unit(dv),
646 						    part);
647 			else
648 				*devp = makedev(majdev, device_unit(dv));
649 		}
650 
651 		if (device_class(dv) == DV_IFNET)
652 			*devp = NODEV;
653 	}
654 
655 	*cp = c;
656 	return (dv);
657 }
658 
659 /*
660  * Return true if system call tracing is enabled for the specified process.
661  */
662 bool
663 trace_is_enabled(struct proc *p)
664 {
665 #ifdef SYSCALL_DEBUG
666 	return (true);
667 #endif
668 #ifdef KTRACE
669 	if (ISSET(p->p_traceflag, (KTRFAC_SYSCALL | KTRFAC_SYSRET)))
670 		return (true);
671 #endif
672 #ifdef PTRACE
673 	if (ISSET(p->p_slflag, PSL_SYSCALL))
674 		return (true);
675 #endif
676 
677 	return (false);
678 }
679 
680 /*
681  * Start trace of particular system call. If process is being traced,
682  * this routine is called by MD syscall dispatch code just before
683  * a system call is actually executed.
684  */
685 int
686 trace_enter(register_t code, const register_t *args, int narg)
687 {
688 #ifdef SYSCALL_DEBUG
689 	scdebug_call(code, args);
690 #endif /* SYSCALL_DEBUG */
691 
692 	ktrsyscall(code, args, narg);
693 
694 #ifdef PTRACE
695 	if ((curlwp->l_proc->p_slflag & (PSL_SYSCALL|PSL_TRACED)) ==
696 	    (PSL_SYSCALL|PSL_TRACED))
697 		process_stoptrace();
698 #endif
699 	return 0;
700 }
701 
702 /*
703  * End trace of particular system call. If process is being traced,
704  * this routine is called by MD syscall dispatch code just after
705  * a system call finishes.
706  * MD caller guarantees the passed 'code' is within the supported
707  * system call number range for emulation the process runs under.
708  */
709 void
710 trace_exit(register_t code, register_t rval[], int error)
711 {
712 #ifdef SYSCALL_DEBUG
713 	scdebug_ret(code, error, rval);
714 #endif /* SYSCALL_DEBUG */
715 
716 	ktrsysret(code, error, rval);
717 
718 #ifdef PTRACE
719 	if ((curlwp->l_proc->p_slflag & (PSL_SYSCALL|PSL_TRACED)) ==
720 	    (PSL_SYSCALL|PSL_TRACED))
721 		process_stoptrace();
722 #endif
723 }
724