1169689Skan/*	$OpenBSD: kern_xxx.c,v 1.41 2022/12/05 23:18:37 deraadt Exp $	*/
2169689Skan/*	$NetBSD: kern_xxx.c,v 1.32 1996/04/22 01:38:41 christos Exp $	*/
3169689Skan
4169689Skan/*
5169689Skan * Copyright (c) 1982, 1986, 1989, 1993
6169689Skan *	The Regents of the University of California.  All rights reserved.
7169689Skan *
8169689Skan * Redistribution and use in source and binary forms, with or without
9169689Skan * modification, are permitted provided that the following conditions
10169689Skan * are met:
11169689Skan * 1. Redistributions of source code must retain the above copyright
12169689Skan *    notice, this list of conditions and the following disclaimer.
13169689Skan * 2. Redistributions in binary form must reproduce the above copyright
14169689Skan *    notice, this list of conditions and the following disclaimer in the
15169689Skan *    documentation and/or other materials provided with the distribution.
16169689Skan * 3. Neither the name of the University nor the names of its contributors
17169689Skan *    may be used to endorse or promote products derived from this software
18169689Skan *    without specific prior written permission.
19169689Skan *
20169689Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23169689Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29169689Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30169689Skan * SUCH DAMAGE.
31169689Skan *
32169689Skan *	@(#)kern_xxx.c	8.2 (Berkeley) 11/14/93
33169689Skan */
34169689Skan
35169689Skan#include <sys/param.h>
36169689Skan#include <sys/systm.h>
37169689Skan#include <sys/reboot.h>
38169689Skan#include <sys/mount.h>
39169689Skan#include <sys/syscallargs.h>
40169689Skan
41169689Skanint rebooting = 0;
42169689Skan
43169689Skanint
44169689Skansys_reboot(struct proc *p, void *v, register_t *retval)
45169689Skan{
46169689Skan	struct sys_reboot_args /* {
47169689Skan		syscallarg(int) opt;
48169689Skan	} */ *uap = v;
49169689Skan	int error;
50169689Skan
51169689Skan	if ((error = suser(p)) != 0)
52169689Skan		return (error);
53169689Skan
54169689Skan#ifdef MULTIPROCESSOR
55169689Skan	sched_stop_secondary_cpus();
56169689Skan	KASSERT(CPU_IS_PRIMARY(curcpu()));
57169689Skan#endif
58169689Skan	reboot(SCARG(uap, opt));
59169689Skan	/* NOTREACHED */
60169689Skan	return (0);
61169689Skan}
62169689Skan
63169689Skan__dead void
64169689Skanreboot(int howto)
65169689Skan{
66169689Skan	KASSERT((howto & RB_NOSYNC) || curproc != NULL);
67169689Skan
68169689Skan	stop_periodic_resettodr();
69169689Skan
70169689Skan	rebooting = 1;
71169689Skan
72169689Skan	boot(howto);
73169689Skan	/* NOTREACHED */
74169689Skan}
75169689Skan
76169689Skan#if !defined(NO_PROPOLICE) && !defined(_RET_PROTECTOR)
77169689Skanvoid __stack_smash_handler(char [], int __attribute__((unused)));
78169689Skan
79169689Skanvoid
80169689Skan__stack_smash_handler(char func[], int damaged)
81169689Skan{
82169689Skan	panic("smashed stack in %s", func);
83169689Skan}
84169689Skan#endif
85169689Skan
86169689Skan#ifdef SYSCALL_DEBUG
87169689Skan#include <sys/proc.h>
88169689Skan#include <sys/syscall.h>
89169689Skan
90#define	SCDEBUG_CALLS		0x0001	/* show calls */
91#define	SCDEBUG_RETURNS		0x0002	/* show returns */
92#define	SCDEBUG_ALL		0x0004	/* even syscalls that are implemented */
93#define	SCDEBUG_SHOWARGS	0x0008	/* show arguments to calls */
94
95int	scdebug = SCDEBUG_CALLS|SCDEBUG_RETURNS|SCDEBUG_SHOWARGS;
96
97extern const char *const syscallnames[];
98
99void
100scdebug_call(struct proc *p, register_t code, const register_t args[])
101{
102	struct process *pr;
103	int i;
104
105	if (!(scdebug & SCDEBUG_CALLS))
106		return;
107
108	if (!(scdebug & SCDEBUG_ALL || code < 0 || code >= SYS_MAXSYSCALL ||
109	     sysent[code].sy_call == sys_nosys))
110		return;
111
112	pr = p->p_p;
113	printf("proc %d (%s): num ", pr->ps_pid, pr->ps_comm);
114	if (code < 0 || code >= SYS_MAXSYSCALL)
115		printf("OUT OF RANGE (%ld)", code);
116	else {
117		printf("%ld call: %s", code, syscallnames[code]);
118		if (scdebug & SCDEBUG_SHOWARGS) {
119			printf("(");
120			for (i = 0; i < sysent[code].sy_argsize / sizeof(register_t);
121			    i++)
122				printf("%s0x%lx", i == 0 ? "" : ", ", args[i]);
123			printf(")");
124		}
125	}
126	printf("\n");
127}
128
129void
130scdebug_ret(struct proc *p, register_t code, int error,
131    const register_t retval[])
132{
133	struct process *pr;
134
135	if (!(scdebug & SCDEBUG_RETURNS))
136		return;
137
138	if (!(scdebug & SCDEBUG_ALL || code < 0 || code >= SYS_MAXSYSCALL ||
139	    sysent[code].sy_call == sys_nosys))
140		return;
141
142	pr = p->p_p;
143	printf("proc %d (%s): num ", pr->ps_pid, pr->ps_comm);
144	if (code < 0 || code >= SYS_MAXSYSCALL)
145		printf("OUT OF RANGE (%ld)", code);
146	else if (code == SYS_lseek)
147		printf("%ld ret: err = %d, rv = 0x%llx", code,
148		    error, *(off_t *)retval);
149	else
150		printf("%ld ret: err = %d, rv = 0x%lx", code,
151		    error, *retval);
152	printf("\n");
153}
154#endif /* SYSCALL_DEBUG */
155