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