xref: /openbsd-src/sys/kern/kern_xxx.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: kern_xxx.c,v 1.11 2009/04/03 09:30:15 art Exp $	*/
2 /*	$NetBSD: kern_xxx.c,v 1.32 1996/04/22 01:38:41 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)kern_xxx.c	8.2 (Berkeley) 11/14/93
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/proc.h>
39 #include <sys/reboot.h>
40 #include <uvm/uvm_extern.h>
41 #include <sys/sysctl.h>
42 #include <sys/mount.h>
43 #include <sys/syscallargs.h>
44 
45 /* ARGSUSED */
46 int
47 sys_reboot(struct proc *p, void *v, register_t *retval)
48 {
49 	struct sys_reboot_args /* {
50 		syscallarg(int) opt;
51 	} */ *uap = v;
52 	CPU_INFO_ITERATOR cii;
53 	struct cpu_info *ci;
54 	int error;
55 
56 	if ((error = suser(p, 0)) != 0)
57 		return (error);
58 
59 	/*
60 	 * Make sure this thread only runs on the primary cpu.
61 	 */
62 	CPU_INFO_FOREACH(cii, ci) {
63 		if (CPU_IS_PRIMARY(ci)) {
64 			sched_peg_curproc(ci);
65 			break;
66 		}
67 	}
68 
69 	boot(SCARG(uap, opt));
70 
71 	atomic_clearbits_int(&p->p_flag, P_CPUPEG);	/* XXX */
72 
73 	return (0);
74 }
75 
76 #if !defined(NO_PROPOLICE)
77 void __stack_smash_handler(char [], int __attribute__((unused)));
78 
79 void
80 __stack_smash_handler(char func[], int damaged)
81 {
82 	panic("smashed stack in %s", func);
83 }
84 #endif
85 
86 #ifdef SYSCALL_DEBUG
87 #define	SCDEBUG_CALLS		0x0001	/* show calls */
88 #define	SCDEBUG_RETURNS		0x0002	/* show returns */
89 #define	SCDEBUG_ALL		0x0004	/* even syscalls that are implemented */
90 #define	SCDEBUG_SHOWARGS	0x0008	/* show arguments to calls */
91 
92 int	scdebug = SCDEBUG_CALLS|SCDEBUG_RETURNS|SCDEBUG_SHOWARGS;
93 
94 void
95 scdebug_call(struct proc *p, register_t code, register_t args[])
96 {
97 	struct sysent *sy;
98 	struct emul *em;
99 	int i;
100 
101 	if (!(scdebug & SCDEBUG_CALLS))
102 		return;
103 
104 	em = p->p_emul;
105 	sy = &em->e_sysent[code];
106 	if (!(scdebug & SCDEBUG_ALL || code < 0 || code >= em->e_nsysent ||
107 	     sy->sy_call == sys_nosys))
108 		return;
109 
110 	printf("proc %d (%s): %s num ", p->p_pid, p->p_comm, em->e_name);
111 	if (code < 0 || code >= em->e_nsysent)
112 		printf("OUT OF RANGE (%d)", code);
113 	else {
114 		printf("%d call: %s", code, em->e_syscallnames[code]);
115 		if (scdebug & SCDEBUG_SHOWARGS) {
116 			printf("(");
117 			for (i = 0; i < sy->sy_argsize / sizeof(register_t);
118 			    i++)
119 				printf("%s0x%lx", i == 0 ? "" : ", ",
120 				    (long)args[i]);
121 			printf(")");
122 		}
123 	}
124 	printf("\n");
125 }
126 
127 void
128 scdebug_ret(struct proc *p, register_t code, int error, register_t retval[])
129 {
130 	struct sysent *sy;
131 	struct emul *em;
132 
133 	if (!(scdebug & SCDEBUG_RETURNS))
134 		return;
135 
136 	em = p->p_emul;
137 	sy = &em->e_sysent[code];
138 	if (!(scdebug & SCDEBUG_ALL || code < 0 || code >= em->e_nsysent ||
139 	    sy->sy_call == sys_nosys))
140 		return;
141 
142 	printf("proc %d (%s): %s num ", p->p_pid, p->p_comm, em->e_name);
143 	if (code < 0 || code >= em->e_nsysent)
144 		printf("OUT OF RANGE (%d)", code);
145 	else
146 		printf("%d ret: err = %d, rv = 0x%lx,0x%lx", code,
147 		    error, (long)retval[0], (long)retval[1]);
148 	printf("\n");
149 }
150 #endif /* SYSCALL_DEBUG */
151