1 /*	$NetBSD: systm.h,v 1.271 2016/07/06 05:20:48 ozaki-r Exp $	*/
2 
3 /*-
4  * Copyright (c) 1982, 1988, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)systm.h	8.7 (Berkeley) 3/29/95
37  */
38 
39 #ifndef _SYS_SYSTM_H_
40 #define _SYS_SYSTM_H_
41 
42 #if defined(_KERNEL_OPT)
43 #include "opt_ddb.h"
44 #include "opt_multiprocessor.h"
45 #endif
46 #if !defined(_KERNEL) && !defined(_STANDALONE)
47 #include <stdbool.h>
48 #endif
49 
50 #include <machine/endian.h>
51 
52 #include <sys/types.h>
53 #include <sys/stdarg.h>
54 
55 #include <sys/device_if.h>
56 
57 struct clockframe;
58 struct lwp;
59 struct proc;
60 struct sysent;
61 struct timeval;
62 struct tty;
63 struct uio;
64 struct vnode;
65 struct vmspace;
66 
67 extern const char *panicstr;	/* panic message */
68 extern int doing_shutdown;	/* shutting down */
69 
70 extern const char copyright[];	/* system copyright */
71 extern char machine[];		/* machine type */
72 extern char machine_arch[];	/* machine architecture */
73 extern const char osrelease[];	/* short system version */
74 extern const char ostype[];	/* system type */
75 extern const char kernel_ident[];/* kernel configuration ID */
76 extern const char version[];	/* system version */
77 extern const char buildinfo[];	/* information from build environment */
78 
79 extern int autonicetime;        /* time (in seconds) before autoniceval */
80 extern int autoniceval;         /* proc priority after autonicetime */
81 
82 extern int selwait;		/* select timeout address */
83 
84 extern int maxmem;		/* max memory per process */
85 extern int physmem;		/* physical memory */
86 
87 extern dev_t dumpdev;		/* dump device */
88 extern dev_t dumpcdev;		/* dump device (character equivalent) */
89 extern long dumplo;		/* offset into dumpdev */
90 extern int dumpsize;		/* size of dump in pages */
91 extern const char *dumpspec;	/* how dump device was specified */
92 
93 extern dev_t rootdev;		/* root device */
94 extern struct vnode *rootvp;	/* vnode equivalent to above */
95 extern device_t root_device; /* device equivalent to above */
96 extern const char *rootspec;	/* how root device was specified */
97 
98 extern int ncpu;		/* number of CPUs configured */
99 extern int ncpuonline;		/* number of CPUs online */
100 #if defined(_KERNEL)
101 extern bool mp_online;		/* secondary processors are started */
102 #endif /* defined(_KERNEL) */
103 
104 extern const char hexdigits[];	/* "0123456789abcdef" in subr_prf.c */
105 extern const char HEXDIGITS[];	/* "0123456789ABCDEF" in subr_prf.c */
106 
107 /*
108  * These represent the swap pseudo-device (`sw').  This device
109  * is used by the swap pager to indirect through the routines
110  * in sys/vm/vm_swap.c.
111  */
112 extern const dev_t swapdev;	/* swapping device */
113 extern struct vnode *swapdev_vp;/* vnode equivalent to above */
114 
115 extern const dev_t zerodev;	/* /dev/zero */
116 
117 #if defined(_KERNEL)
118 typedef int	sy_call_t(struct lwp *, const void *, register_t *);
119 
120 extern struct sysent {		/* system call table */
121 	short	sy_narg;	/* number of args */
122 	short	sy_argsize;	/* total size of arguments */
123 	int	sy_flags;	/* flags. see below */
124 	sy_call_t *sy_call;     /* implementing function */
125 	uint32_t sy_entry;	/* DTrace entry ID for systrace. */
126 	uint32_t sy_return;	/* DTrace return ID for systrace. */
127 } sysent[];
128 extern int nsysent;
129 #endif
130 
131 #if	BYTE_ORDER == BIG_ENDIAN
132 #define	SCARG(p,k)	((p)->k.be.datum)	/* get arg from args pointer */
133 #elif	BYTE_ORDER == LITTLE_ENDIAN
134 #define	SCARG(p,k)	((p)->k.le.datum)	/* get arg from args pointer */
135 #else
136 #error	"what byte order is this machine?"
137 #endif
138 
139 #define	SYCALL_INDIRECT	0x0000002 /* indirect (ie syscall() or __syscall()) */
140 #define	SYCALL_NARGS64_MASK	0x000f000 /* count of 64bit args */
141 #define SYCALL_RET_64	0x0010000 /* retval is a 64bit integer value */
142 #define SYCALL_ARG0_64  0x0020000
143 #define SYCALL_ARG1_64  0x0040000
144 #define SYCALL_ARG2_64  0x0080000
145 #define SYCALL_ARG3_64  0x0100000
146 #define SYCALL_ARG4_64  0x0200000
147 #define SYCALL_ARG5_64  0x0400000
148 #define SYCALL_ARG6_64  0x0800000
149 #define SYCALL_ARG7_64  0x1000000
150 #define SYCALL_NOSYS    0x2000000 /* permanent nosys in sysent[] */
151 #define	SYCALL_ARG_PTR	0x4000000 /* at least one argument is a pointer */
152 #define SYCALL_RET_64_P(sy)	((sy)->sy_flags & SYCALL_RET_64)
153 #define SYCALL_ARG_64_P(sy, n)	((sy)->sy_flags & (SYCALL_ARG0_64 << (n)))
154 #define	SYCALL_ARG_64_MASK(sy)	(((sy)->sy_flags >> 17) & 0xff)
155 #define	SYCALL_ARG_PTR_P(sy)	((sy)->sy_flags & SYCALL_ARG_PTR)
156 #define	SYCALL_NARGS64(sy)	(((sy)->sy_flags >> 12) & 0x0f)
157 #define	SYCALL_NARGS64_VAL(n)	((n) << 12)
158 
159 extern int boothowto;		/* reboot flags, from console subsystem */
160 #define	bootverbose	(boothowto & AB_VERBOSE)
161 #define	bootquiet	(boothowto & AB_QUIET)
162 
163 extern void (*v_putc)(int); /* Virtual console putc routine */
164 
165 /*
166  * General function declarations.
167  */
168 void	voidop(void);
169 int	nullop(void *);
170 void*	nullret(void);
171 int	enodev(void);
172 int	enosys(void);
173 int	enoioctl(void);
174 int	enxio(void);
175 int	eopnotsupp(void);
176 
177 enum hashtype {
178 	HASH_LIST,
179 	HASH_SLIST,
180 	HASH_TAILQ,
181 	HASH_PSLIST
182 };
183 
184 #ifdef _KERNEL
185 void	*hashinit(u_int, enum hashtype, bool, u_long *);
186 void	hashdone(void *, enum hashtype, u_long);
187 int	seltrue(dev_t, int, struct lwp *);
188 int	sys_nosys(struct lwp *, const void *, register_t *);
189 int	sys_nomodule(struct lwp *, const void *, register_t *);
190 
191 void	aprint_normal(const char *, ...) __printflike(1, 2);
192 void	aprint_error(const char *, ...) __printflike(1, 2);
193 void	aprint_naive(const char *, ...) __printflike(1, 2);
194 void	aprint_verbose(const char *, ...) __printflike(1, 2);
195 void	aprint_debug(const char *, ...) __printflike(1, 2);
196 
197 void	device_printf(device_t, const char *fmt, ...) __printflike(2, 3);
198 
199 void	aprint_normal_dev(device_t, const char *, ...) __printflike(2, 3);
200 void	aprint_error_dev(device_t, const char *, ...) __printflike(2, 3);
201 void	aprint_naive_dev(device_t, const char *, ...) __printflike(2, 3);
202 void	aprint_verbose_dev(device_t, const char *, ...) __printflike(2, 3);
203 void	aprint_debug_dev(device_t, const char *, ...) __printflike(2, 3);
204 
205 struct ifnet;
206 
207 void	aprint_normal_ifnet(struct ifnet *, const char *, ...)
208     __printflike(2, 3);
209 void	aprint_error_ifnet(struct ifnet *, const char *, ...)
210     __printflike(2, 3);
211 void	aprint_naive_ifnet(struct ifnet *, const char *, ...)
212     __printflike(2, 3);
213 void	aprint_verbose_ifnet(struct ifnet *, const char *, ...)
214     __printflike(2, 3);
215 void	aprint_debug_ifnet(struct ifnet *, const char *, ...)
216     __printflike(2, 3);
217 
218 int	aprint_get_error_count(void);
219 
220 void	printf_tolog(const char *, ...) __printflike(1, 2);
221 
222 void	printf_nolog(const char *, ...) __printflike(1, 2);
223 
224 void	printf(const char *, ...) __printflike(1, 2);
225 
226 int	snprintf(char *, size_t, const char *, ...) __printflike(3, 4);
227 
228 void	vprintf(const char *, va_list) __printflike(1, 0);
229 
230 int	vsnprintf(char *, size_t, const char *, va_list) __printflike(3, 0);
231 
232 int	humanize_number(char *, size_t, uint64_t, const char *, int);
233 
234 void	twiddle(void);
235 void	banner(void);
236 #endif /* _KERNEL */
237 
238 void	panic(const char *, ...) __dead __printflike(1, 2);
239 void	vpanic(const char *, va_list) __dead __printflike(1, 0);
240 void	uprintf(const char *, ...) __printflike(1, 2);
241 void	uprintf_locked(const char *, ...) __printflike(1, 2);
242 void	ttyprintf(struct tty *, const char *, ...) __printflike(2, 3);
243 
244 int	format_bytes(char *, size_t, uint64_t);
245 
246 void	tablefull(const char *, const char *);
247 
248 int	kcopy(const void *, void *, size_t);
249 
250 #ifdef _KERNEL
251 #define bcopy(src, dst, len)	memcpy((dst), (src), (len))
252 #define bzero(src, len)		memset((src), 0, (len))
253 #define bcmp(a, b, len)		memcmp((a), (b), (len))
254 #endif /* KERNEL */
255 
256 int	copystr(const void *, void *, size_t, size_t *);
257 int	copyinstr(const void *, void *, size_t, size_t *);
258 int	copyoutstr(const void *, void *, size_t, size_t *);
259 int	copyin(const void *, void *, size_t);
260 int	copyout(const void *, void *, size_t);
261 
262 #ifdef _KERNEL
263 typedef	int	(*copyin_t)(const void *, void *, size_t);
264 typedef int	(*copyout_t)(const void *, void *, size_t);
265 #endif
266 
267 int	copyin_proc(struct proc *, const void *, void *, size_t);
268 int	copyout_proc(struct proc *, const void *, void *, size_t);
269 int	copyin_vmspace(struct vmspace *, const void *, void *, size_t);
270 int	copyout_vmspace(struct vmspace *, const void *, void *, size_t);
271 
272 int	ioctl_copyin(int ioctlflags, const void *src, void *dst, size_t len);
273 int	ioctl_copyout(int ioctlflags, const void *src, void *dst, size_t len);
274 
275 int	ucas_ptr(volatile void *, void *, void *, void *);
276 int	ucas_int(volatile int *, int, int, int *);
277 
278 int	subyte(void *, int);
279 int	suibyte(void *, int);
280 int	susword(void *, short);
281 int	suisword(void *, short);
282 int	suswintr(void *, short);
283 int	suword(void *, long);
284 int	suiword(void *, long);
285 
286 int	fubyte(const void *);
287 int	fuibyte(const void *);
288 int	fusword(const void *);
289 int	fuisword(const void *);
290 int	fuswintr(const void *);
291 long	fuword(const void *);
292 long	fuiword(const void *);
293 
294 void	hardclock(struct clockframe *);
295 void	softclock(void *);
296 void	statclock(struct clockframe *);
297 
298 #ifdef NTP
299 void	ntp_init(void);
300 #ifdef PPS_SYNC
301 struct timespec;
302 void	hardpps(struct timespec *, long);
303 #endif /* PPS_SYNC */
304 #else
305 void	ntp_init(void);	/* also provides adjtime() functionality */
306 #endif /* NTP */
307 
308 void	ssp_init(void);
309 
310 void	initclocks(void);
311 void	inittodr(time_t);
312 void	resettodr(void);
313 void	cpu_initclocks(void);
314 void	setrootfstime(time_t);
315 
316 void	startprofclock(struct proc *);
317 void	stopprofclock(struct proc *);
318 void	proftick(struct clockframe *);
319 void	setstatclockrate(int);
320 
321 /*
322  * Critical polling hooks.  Functions to be run while the kernel stays
323  * elevated IPL for a "long" time.  (watchdogs).
324  */
325 void	*critpollhook_establish(void (*)(void *), void *);
326 void	critpollhook_disestablish(void *);
327 void	docritpollhooks(void);
328 
329 /*
330  * Shutdown hooks.  Functions to be run with all interrupts disabled
331  * immediately before the system is halted or rebooted.
332  */
333 void	*shutdownhook_establish(void (*)(void *), void *);
334 void	shutdownhook_disestablish(void *);
335 void	doshutdownhooks(void);
336 
337 /*
338  * Power management hooks.
339  */
340 void	*powerhook_establish(const char *, void (*)(int, void *), void *);
341 void	powerhook_disestablish(void *);
342 void	dopowerhooks(int);
343 #define PWR_RESUME	0
344 #define PWR_SUSPEND	1
345 #define PWR_STANDBY	2
346 #define PWR_SOFTRESUME	3
347 #define PWR_SOFTSUSPEND	4
348 #define PWR_SOFTSTANDBY	5
349 #define PWR_NAMES \
350 	"resume",	/* 0 */ \
351 	"suspend",	/* 1 */ \
352 	"standby",	/* 2 */ \
353 	"softresume",	/* 3 */ \
354 	"softsuspend",	/* 4 */ \
355 	"softstandby"	/* 5 */
356 
357 /*
358  * Mountroot hooks (and mountroot declaration).  Device drivers establish
359  * these to be executed just before (*mountroot)() if the passed device is
360  * selected as the root device.
361  */
362 
363 #define	ROOT_FSTYPE_ANY	"?"
364 
365 extern const char *rootfstype;
366 void	*mountroothook_establish(void (*)(device_t), device_t);
367 void	mountroothook_disestablish(void *);
368 void	mountroothook_destroy(void);
369 void	domountroothook(device_t);
370 
371 /*
372  * Exec hooks. Subsystems may want to do cleanup when a process
373  * execs.
374  */
375 void	*exechook_establish(void (*)(struct proc *, void *), void *);
376 void	exechook_disestablish(void *);
377 void	doexechooks(struct proc *);
378 
379 /*
380  * Exit hooks. Subsystems may want to do cleanup when a process exits.
381  */
382 void	*exithook_establish(void (*)(struct proc *, void *), void *);
383 void	exithook_disestablish(void *);
384 void	doexithooks(struct proc *);
385 
386 /*
387  * Fork hooks.  Subsystems may want to do special processing when a process
388  * forks.
389  */
390 void	*forkhook_establish(void (*)(struct proc *, struct proc *));
391 void	forkhook_disestablish(void *);
392 void	doforkhooks(struct proc *, struct proc *);
393 
394 /*
395  * kernel syscall tracing/debugging hooks.
396  */
397 #ifdef _KERNEL
398 bool	trace_is_enabled(struct proc *);
399 int	trace_enter(register_t, const struct sysent *, const void *);
400 void	trace_exit(register_t, const struct sysent *, const void *,
401     register_t [], int);
402 #endif
403 
404 int	uiomove(void *, size_t, struct uio *);
405 int	uiomove_frombuf(void *, size_t, struct uio *);
406 
407 #ifdef _KERNEL
408 int	setjmp(label_t *) __returns_twice;
409 void	longjmp(label_t *) __dead;
410 #endif
411 
412 void	consinit(void);
413 
414 void	cpu_startup(void);
415 void	cpu_configure(void);
416 void	cpu_bootconf(void);
417 void	cpu_rootconf(void);
418 void	cpu_dumpconf(void);
419 
420 #ifdef GPROF
421 void	kmstartup(void);
422 #endif
423 
424 void	machdep_init(void);
425 
426 #ifdef _KERNEL
427 #include <lib/libkern/libkern.h>
428 
429 /*
430  * Stuff to handle debugger magic key sequences.
431  */
432 #define CNS_LEN			128
433 #define CNS_MAGIC_VAL(x)	((x)&0x1ff)
434 #define CNS_MAGIC_NEXT(x)	(((x)>>9)&0x7f)
435 #define CNS_TERM		0x7f	/* End of sequence */
436 
437 typedef struct cnm_state {
438 	int	cnm_state;
439 	u_short	*cnm_magic;
440 } cnm_state_t;
441 
442 /* Override db_console() in MD headers */
443 #ifndef cn_trap
444 #define cn_trap()	console_debugger()
445 #endif
446 #ifndef cn_isconsole
447 #define cn_isconsole(d)	(cn_tab != NULL && (d) == cn_tab->cn_dev)
448 #endif
449 
450 void cn_init_magic(cnm_state_t *);
451 void cn_destroy_magic(cnm_state_t *);
452 int cn_set_magic(const char *);
453 int cn_get_magic(char *, size_t);
454 /* This should be called for each byte read */
455 #ifndef cn_check_magic
456 #define cn_check_magic(d, k, s)						\
457 	do {								\
458 		if (cn_isconsole(d)) {					\
459 			int _v = (s).cnm_magic[(s).cnm_state];		\
460 			if ((k) == CNS_MAGIC_VAL(_v)) {			\
461 				(s).cnm_state = CNS_MAGIC_NEXT(_v);	\
462 				if ((s).cnm_state == CNS_TERM) {	\
463 					cn_trap();			\
464 					(s).cnm_state = 0;		\
465 				}					\
466 			} else {					\
467 				(s).cnm_state = 0;			\
468 			}						\
469 		}							\
470 	} while (/* CONSTCOND */ 0)
471 #endif
472 
473 /* Encode out-of-band events this way when passing to cn_check_magic() */
474 #define	CNC_BREAK		0x100
475 
476 #if defined(DDB) || defined(sun3) || defined(sun2)
477 /* note that cpu_Debugger() is always available on sun[23] */
478 void	cpu_Debugger(void);
479 #define Debugger	cpu_Debugger
480 #endif
481 
482 #ifdef DDB
483 /*
484  * Enter debugger(s) from console attention if enabled
485  */
486 extern int db_fromconsole; /* XXX ddb/ddbvar.h */
487 #define console_debugger() if (db_fromconsole) Debugger()
488 #elif defined(Debugger)
489 #define console_debugger() Debugger()
490 #else
491 #define console_debugger() do {} while (/* CONSTCOND */ 0) /* NOP */
492 #endif
493 
494 /* For SYSCALL_DEBUG */
495 void scdebug_init(void);
496 void scdebug_call(register_t, const register_t[]);
497 void scdebug_ret(register_t, int, const register_t[]);
498 
499 void	kernel_lock_init(void);
500 void	_kernel_lock(int);
501 void	_kernel_unlock(int, int *);
502 bool	_kernel_locked_p(void);
503 
504 void	kernconfig_lock_init(void);
505 void	kernconfig_lock(void);
506 void	kernconfig_unlock(void);
507 bool	kernconfig_is_held(void);
508 #endif
509 
510 #if defined(MULTIPROCESSOR) || defined(_MODULE)
511 #define	KERNEL_LOCK(count, lwp)			\
512 do {						\
513 	if ((count) != 0)			\
514 		_kernel_lock((count));	\
515 } while (/* CONSTCOND */ 0)
516 #define	KERNEL_UNLOCK(all, lwp, p)	_kernel_unlock((all), (p))
517 #define	KERNEL_LOCKED_P()		_kernel_locked_p()
518 #else
519 #define	KERNEL_LOCK(count, lwp)		do {(void)(count); (void)(lwp);} while (/* CONSTCOND */ 0) /*NOP*/
520 #define	KERNEL_UNLOCK(all, lwp, ptr)	do {(void)(all); (void)(lwp); (void)(ptr);} while (/* CONSTCOND */ 0) /*NOP*/
521 #define	KERNEL_LOCKED_P()		(true)
522 #endif
523 
524 #define	KERNEL_UNLOCK_LAST(l)		KERNEL_UNLOCK(-1, (l), NULL)
525 #define	KERNEL_UNLOCK_ALL(l, p)		KERNEL_UNLOCK(0, (l), (p))
526 #define	KERNEL_UNLOCK_ONE(l)		KERNEL_UNLOCK(1, (l), NULL)
527 
528 #ifdef _KERNEL
529 /* Preemption control. */
530 void	kpreempt_disable(void);
531 void	kpreempt_enable(void);
532 bool	kpreempt_disabled(void);
533 
534 vaddr_t calc_cache_size(vsize_t , int, int);
535 #endif
536 
537 void assert_sleepable(void);
538 #if defined(DEBUG)
539 #define	ASSERT_SLEEPABLE()	assert_sleepable()
540 #else /* defined(DEBUG) */
541 #define	ASSERT_SLEEPABLE()	do {} while (0)
542 #endif /* defined(DEBUG) */
543 
544 
545 #endif	/* !_SYS_SYSTM_H_ */
546