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
57struct clockframe;
58struct lwp;
59struct proc;
60struct sysent;
61struct timeval;
62struct tty;
63struct uio;
64struct vnode;
65struct vmspace;
66
67extern const char *panicstr; /* panic message */
68extern int doing_shutdown; /* shutting down */
69
70extern const char copyright[]; /* system copyright */
71extern char machine[]; /* machine type */
72extern char machine_arch[]; /* machine architecture */
73extern const char osrelease[]; /* short system version */
74extern const char ostype[]; /* system type */
75extern const char kernel_ident[];/* kernel configuration ID */
76extern const char version[]; /* system version */
77extern const char buildinfo[]; /* information from build environment */
78
79extern int autonicetime; /* time (in seconds) before autoniceval */
80extern int autoniceval; /* proc priority after autonicetime */
81
82extern int selwait; /* select timeout address */
83
84extern int maxmem; /* max memory per process */
85extern int physmem; /* physical memory */
86
87extern dev_t dumpdev; /* dump device */
88extern dev_t dumpcdev; /* dump device (character equivalent) */
89extern long dumplo; /* offset into dumpdev */
90extern int dumpsize; /* size of dump in pages */
91extern const char *dumpspec; /* how dump device was specified */
92
93extern dev_t rootdev; /* root device */
94extern struct vnode *rootvp; /* vnode equivalent to above */
95extern device_t root_device; /* device equivalent to above */
96extern const char *rootspec; /* how root device was specified */
97
98extern int ncpu; /* number of CPUs configured */
99extern int ncpuonline; /* number of CPUs online */
100#if defined(_KERNEL)
101extern bool mp_online; /* secondary processors are started */
102#endif /* defined(_KERNEL) */
103
104extern const char hexdigits[]; /* "0123456789abcdef" in subr_prf.c */
105extern 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 */
112extern const dev_t swapdev; /* swapping device */
113extern struct vnode *swapdev_vp;/* vnode equivalent to above */
114
115extern const dev_t zerodev; /* /dev/zero */
116
117#if defined(_KERNEL)
118typedef int sy_call_t(struct lwp *, const void *, register_t *);
119
120extern 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[];
128extern 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
159extern int boothowto; /* reboot flags, from console subsystem */
160#define bootverbose (boothowto & AB_VERBOSE)
161#define bootquiet (boothowto & AB_QUIET)
162
163extern void (*v_putc)(int); /* Virtual console putc routine */
164
165/*
166 * General function declarations.
167 */
168void voidop(void);
169int nullop(void *);
170void* nullret(void);
171int enodev(void);
172int enosys(void);
173int enoioctl(void);
174int enxio(void);
175int eopnotsupp(void);
176
177enum hashtype {
178 HASH_LIST,
179 HASH_SLIST,
180 HASH_TAILQ,
181 HASH_PSLIST
182};
183
184#ifdef _KERNEL
185void *hashinit(u_int, enum hashtype, bool, u_long *);
186void hashdone(void *, enum hashtype, u_long);
187int seltrue(dev_t, int, struct lwp *);
188int sys_nosys(struct lwp *, const void *, register_t *);
189int sys_nomodule(struct lwp *, const void *, register_t *);
190
191void aprint_normal(const char *, ...) __printflike(1, 2);
192void aprint_error(const char *, ...) __printflike(1, 2);
193void aprint_naive(const char *, ...) __printflike(1, 2);
194void aprint_verbose(const char *, ...) __printflike(1, 2);
195void aprint_debug(const char *, ...) __printflike(1, 2);
196
197void device_printf(device_t, const char *fmt, ...) __printflike(2, 3);
198
199void aprint_normal_dev(device_t, const char *, ...) __printflike(2, 3);
200void aprint_error_dev(device_t, const char *, ...) __printflike(2, 3);
201void aprint_naive_dev(device_t, const char *, ...) __printflike(2, 3);
202void aprint_verbose_dev(device_t, const char *, ...) __printflike(2, 3);
203void aprint_debug_dev(device_t, const char *, ...) __printflike(2, 3);
204
205struct ifnet;
206
207void aprint_normal_ifnet(struct ifnet *, const char *, ...)
208 __printflike(2, 3);
209void aprint_error_ifnet(struct ifnet *, const char *, ...)
210 __printflike(2, 3);
211void aprint_naive_ifnet(struct ifnet *, const char *, ...)
212 __printflike(2, 3);
213void aprint_verbose_ifnet(struct ifnet *, const char *, ...)
214 __printflike(2, 3);
215void aprint_debug_ifnet(struct ifnet *, const char *, ...)
216 __printflike(2, 3);
217
218int aprint_get_error_count(void);
219
220void printf_tolog(const char *, ...) __printflike(1, 2);
221
222void printf_nolog(const char *, ...) __printflike(1, 2);
223
224void printf(const char *, ...) __printflike(1, 2);
225
226int snprintf(char *, size_t, const char *, ...) __printflike(3, 4);
227
228void vprintf(const char *, va_list) __printflike(1, 0);
229
230int vsnprintf(char *, size_t, const char *, va_list) __printflike(3, 0);
231
232int humanize_number(char *, size_t, uint64_t, const char *, int);
233
234void twiddle(void);
235void (void);
236#endif /* _KERNEL */
237
238void panic(const char *, ...) __dead __printflike(1, 2);
239void vpanic(const char *, va_list) __dead __printflike(1, 0);
240void uprintf(const char *, ...) __printflike(1, 2);
241void uprintf_locked(const char *, ...) __printflike(1, 2);
242void ttyprintf(struct tty *, const char *, ...) __printflike(2, 3);
243
244int format_bytes(char *, size_t, uint64_t);
245
246void tablefull(const char *, const char *);
247
248int 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
256int copystr(const void *, void *, size_t, size_t *);
257int copyinstr(const void *, void *, size_t, size_t *);
258int copyoutstr(const void *, void *, size_t, size_t *);
259int copyin(const void *, void *, size_t);
260int copyout(const void *, void *, size_t);
261
262#ifdef _KERNEL
263typedef int (*copyin_t)(const void *, void *, size_t);
264typedef int (*copyout_t)(const void *, void *, size_t);
265#endif
266
267int copyin_proc(struct proc *, const void *, void *, size_t);
268int copyout_proc(struct proc *, const void *, void *, size_t);
269int copyin_vmspace(struct vmspace *, const void *, void *, size_t);
270int copyout_vmspace(struct vmspace *, const void *, void *, size_t);
271
272int ioctl_copyin(int ioctlflags, const void *src, void *dst, size_t len);
273int ioctl_copyout(int ioctlflags, const void *src, void *dst, size_t len);
274
275int ucas_ptr(volatile void *, void *, void *, void *);
276int ucas_int(volatile int *, int, int, int *);
277
278int subyte(void *, int);
279int suibyte(void *, int);
280int susword(void *, short);
281int suisword(void *, short);
282int suswintr(void *, short);
283int suword(void *, long);
284int suiword(void *, long);
285
286int fubyte(const void *);
287int fuibyte(const void *);
288int fusword(const void *);
289int fuisword(const void *);
290int fuswintr(const void *);
291long fuword(const void *);
292long fuiword(const void *);
293
294void hardclock(struct clockframe *);
295void softclock(void *);
296void statclock(struct clockframe *);
297
298#ifdef NTP
299void ntp_init(void);
300#ifdef PPS_SYNC
301struct timespec;
302void hardpps(struct timespec *, long);
303#endif /* PPS_SYNC */
304#else
305void ntp_init(void); /* also provides adjtime() functionality */
306#endif /* NTP */
307
308void ssp_init(void);
309
310void initclocks(void);
311void inittodr(time_t);
312void resettodr(void);
313void cpu_initclocks(void);
314void setrootfstime(time_t);
315
316void startprofclock(struct proc *);
317void stopprofclock(struct proc *);
318void proftick(struct clockframe *);
319void 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 */
325void *critpollhook_establish(void (*)(void *), void *);
326void critpollhook_disestablish(void *);
327void 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 */
333void *shutdownhook_establish(void (*)(void *), void *);
334void shutdownhook_disestablish(void *);
335void doshutdownhooks(void);
336
337/*
338 * Power management hooks.
339 */
340void *powerhook_establish(const char *, void (*)(int, void *), void *);
341void powerhook_disestablish(void *);
342void 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
365extern const char *rootfstype;
366void *mountroothook_establish(void (*)(device_t), device_t);
367void mountroothook_disestablish(void *);
368void mountroothook_destroy(void);
369void domountroothook(device_t);
370
371/*
372 * Exec hooks. Subsystems may want to do cleanup when a process
373 * execs.
374 */
375void *exechook_establish(void (*)(struct proc *, void *), void *);
376void exechook_disestablish(void *);
377void doexechooks(struct proc *);
378
379/*
380 * Exit hooks. Subsystems may want to do cleanup when a process exits.
381 */
382void *exithook_establish(void (*)(struct proc *, void *), void *);
383void exithook_disestablish(void *);
384void doexithooks(struct proc *);
385
386/*
387 * Fork hooks. Subsystems may want to do special processing when a process
388 * forks.
389 */
390void *forkhook_establish(void (*)(struct proc *, struct proc *));
391void forkhook_disestablish(void *);
392void doforkhooks(struct proc *, struct proc *);
393
394/*
395 * kernel syscall tracing/debugging hooks.
396 */
397#ifdef _KERNEL
398bool trace_is_enabled(struct proc *);
399int trace_enter(register_t, const struct sysent *, const void *);
400void trace_exit(register_t, const struct sysent *, const void *,
401 register_t [], int);
402#endif
403
404int uiomove(void *, size_t, struct uio *);
405int uiomove_frombuf(void *, size_t, struct uio *);
406
407#ifdef _KERNEL
408int setjmp(label_t *) __returns_twice;
409void longjmp(label_t *) __dead;
410#endif
411
412void consinit(void);
413
414void cpu_startup(void);
415void cpu_configure(void);
416void cpu_bootconf(void);
417void cpu_rootconf(void);
418void cpu_dumpconf(void);
419
420#ifdef GPROF
421void kmstartup(void);
422#endif
423
424void 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
437typedef 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
450void cn_init_magic(cnm_state_t *);
451void cn_destroy_magic(cnm_state_t *);
452int cn_set_magic(const char *);
453int 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] */
478void 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 */
486extern 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 */
495void scdebug_init(void);
496void scdebug_call(register_t, const register_t[]);
497void scdebug_ret(register_t, int, const register_t[]);
498
499void kernel_lock_init(void);
500void _kernel_lock(int);
501void _kernel_unlock(int, int *);
502bool _kernel_locked_p(void);
503
504void kernconfig_lock_init(void);
505void kernconfig_lock(void);
506void kernconfig_unlock(void);
507bool kernconfig_is_held(void);
508#endif
509
510#if defined(MULTIPROCESSOR) || defined(_MODULE)
511#define KERNEL_LOCK(count, lwp) \
512do { \
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. */
530void kpreempt_disable(void);
531void kpreempt_enable(void);
532bool kpreempt_disabled(void);
533
534vaddr_t calc_cache_size(vsize_t , int, int);
535#endif
536
537void 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