1 /* $NetBSD: systm.h,v 1.152 2002/11/16 07:40:42 uebayasi 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. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)systm.h 8.7 (Berkeley) 3/29/95 41 */ 42 43 /* 44 * The `securelevel' variable controls the security level of the system. 45 * It can only be decreased by process 1 (/sbin/init). 46 * 47 * Security levels are as follows: 48 * -1 permanently insecure mode - always run system in level 0 mode. 49 * 0 insecure mode - immutable and append-only flags may be turned off. 50 * All devices may be read or written subject to permission modes. 51 * 1 secure mode - immutable and append-only flags may not be changed; 52 * raw disks of mounted filesystems, /dev/mem, and /dev/kmem are 53 * read-only. 54 * 2 highly secure mode - same as (1) plus raw disks are always 55 * read-only whether mounted or not. This level precludes tampering 56 * with filesystems by unmounting them, but also inhibits running 57 * newfs while the system is secured. 58 * 59 * In normal operation, the system runs in level 0 mode while single user 60 * and in level 1 mode while multiuser. If level 2 mode is desired while 61 * running multiuser, it can be set in the multiuser startup script 62 * (/etc/rc.local) using sysctl(8). If it is desired to run the system 63 * in level 0 mode while multiuser, initialize the variable securelevel 64 * in /sys/kern/kern_sysctl.c to -1. Note that it is NOT initialized to 65 * zero as that would allow the vmunix binary to be patched to -1. 66 * Without initialization, securelevel loads in the BSS area which only 67 * comes into existence when the kernel is loaded and hence cannot be 68 * patched by a stalking hacker. 69 */ 70 71 #ifndef _SYS_SYSTM_H_ 72 #define _SYS_SYSTM_H_ 73 74 #if defined(_KERNEL_OPT) 75 #include "opt_ddb.h" 76 #include "opt_multiprocessor.h" 77 #endif 78 79 #include <machine/endian.h> 80 81 struct clockframe; 82 struct device; 83 struct proc; 84 struct timeval; 85 struct tty; 86 struct uio; 87 struct vnode; 88 89 extern int securelevel; /* system security level */ 90 extern const char *panicstr; /* panic message */ 91 extern int doing_shutdown; /* shutting down */ 92 93 extern const char copyright[]; /* system copyright */ 94 extern char cpu_model[]; /* machine/cpu model name */ 95 extern char machine[]; /* machine type */ 96 extern char machine_arch[]; /* machine architecture */ 97 extern const char osrelease[]; /* short system version */ 98 extern const char ostype[]; /* system type */ 99 extern const char version[]; /* system version */ 100 101 extern int autonicetime; /* time (in seconds) before autoniceval */ 102 extern int autoniceval; /* proc priority after autonicetime */ 103 104 extern int selwait; /* select timeout address */ 105 106 extern int maxmem; /* max memory per process */ 107 extern int physmem; /* physical memory */ 108 109 extern dev_t dumpdev; /* dump device */ 110 extern long dumplo; /* offset into dumpdev */ 111 extern int dumpsize; /* size of dump in pages */ 112 extern const char *dumpspec; /* how dump device was specified */ 113 114 extern dev_t rootdev; /* root device */ 115 extern struct vnode *rootvp; /* vnode equivalent to above */ 116 extern struct device *root_device; /* device equivalent to above */ 117 extern const char *rootspec; /* how root device was specified */ 118 119 /* 120 * These represent the swap pseudo-device (`sw'). This device 121 * is used by the swap pager to indirect through the routines 122 * in sys/vm/vm_swap.c. 123 */ 124 extern const dev_t swapdev; /* swapping device */ 125 extern struct vnode *swapdev_vp;/* vnode equivalent to above */ 126 127 extern const dev_t zerodev; /* /dev/zero */ 128 129 typedef int sy_call_t(struct proc *, void *, register_t *); 130 131 extern struct sysent { /* system call table */ 132 short sy_narg; /* number of args */ 133 short sy_argsize; /* total size of arguments */ 134 int sy_flags; /* flags. see below */ 135 sy_call_t *sy_call; /* implementing function */ 136 } sysent[]; 137 extern int nsysent; 138 #if BYTE_ORDER == BIG_ENDIAN 139 #define SCARG(p,k) ((p)->k.be.datum) /* get arg from args pointer */ 140 #elif BYTE_ORDER == LITTLE_ENDIAN 141 #define SCARG(p,k) ((p)->k.le.datum) /* get arg from args pointer */ 142 #else 143 #error "what byte order is this machine?" 144 #endif 145 146 #define SYCALL_MPSAFE 0x0001 /* syscall is MP-safe */ 147 148 extern int boothowto; /* reboot flags, from console subsystem */ 149 #define bootverbose (boothowto & AB_VERBOSE) 150 #define bootquiet (boothowto & AB_QUIET) 151 152 extern void (*v_putc) __P((int)); /* Virtual console putc routine */ 153 154 extern void _insque __P((void *, void *)); 155 extern void _remque __P((void *)); 156 157 /* casts to keep lint happy, but it should be happy with void * */ 158 #define insque(q,p) _insque(q, p) 159 #define remque(q) _remque(q) 160 161 /* 162 * General function declarations. 163 */ 164 int nullop __P((void *)); 165 int enodev __P((void)); 166 int enosys __P((void)); 167 int enoioctl __P((void)); 168 int enxio __P((void)); 169 int eopnotsupp __P((void)); 170 171 enum hashtype { 172 HASH_LIST, 173 HASH_TAILQ 174 }; 175 176 void *hashinit __P((u_int, enum hashtype, int, int, u_long *)); 177 void hashdone __P((void *, int)); 178 int seltrue __P((dev_t, int, struct proc *)); 179 int sys_nosys __P((struct proc *, void *, register_t *)); 180 181 182 #ifdef _KERNEL 183 void printf __P((const char *, ...)) 184 __attribute__((__format__(__printf__,1,2))); 185 int sprintf __P((char *, const char *, ...)) 186 __attribute__((__format__(__printf__,2,3))); 187 int snprintf __P((char *, size_t, const char *, ...)) 188 __attribute__((__format__(__printf__,3,4))); 189 void vprintf __P((const char *, _BSD_VA_LIST_)); 190 int vsprintf __P((char *, const char *, _BSD_VA_LIST_)); 191 int vsnprintf __P((char *, size_t, const char *, _BSD_VA_LIST_)); 192 int humanize_number __P((char *, size_t, u_int64_t, const char *, int)); 193 #endif /* _KERNEL */ 194 195 void panic __P((const char *, ...)) 196 __attribute__((__noreturn__,__format__(__printf__,1,2))); 197 void uprintf __P((const char *, ...)) 198 __attribute__((__format__(__printf__,1,2))); 199 void ttyprintf __P((struct tty *, const char *, ...)) 200 __attribute__((__format__(__printf__,2,3))); 201 202 char *bitmask_snprintf __P((u_quad_t, const char *, char *, size_t)); 203 204 int format_bytes __P((char *, size_t, u_int64_t)); 205 206 void tablefull __P((const char *, const char *)); 207 208 int kcopy __P((const void *, void *, size_t)); 209 210 #ifdef _KERNEL 211 #define bcopy(src, dst, len) memcpy((dst), (src), (len)) 212 #define bzero(src, len) memset((src), 0, (len)) 213 #define bcmp(a, b, len) memcmp((a), (b), (len)) 214 #endif /* KERNEL */ 215 216 int copystr __P((const void *, void *, size_t, size_t *)); 217 int copyinstr __P((const void *, void *, size_t, size_t *)); 218 int copyoutstr __P((const void *, void *, size_t, size_t *)); 219 int copyin __P((const void *, void *, size_t)); 220 int copyout __P((const void *, void *, size_t)); 221 222 int copyin_proc __P((struct proc *, const void *, void *, size_t)); 223 int copyout_proc __P((struct proc *, const void *, void *, size_t)); 224 225 int subyte __P((void *, int)); 226 int suibyte __P((void *, int)); 227 int susword __P((void *, short)); 228 int suisword __P((void *, short)); 229 int suswintr __P((void *, short)); 230 int suword __P((void *, long)); 231 int suiword __P((void *, long)); 232 233 int fubyte __P((const void *)); 234 int fuibyte __P((const void *)); 235 int fusword __P((const void *)); 236 int fuisword __P((const void *)); 237 int fuswintr __P((const void *)); 238 long fuword __P((const void *)); 239 long fuiword __P((const void *)); 240 241 int hzto __P((struct timeval *)); 242 void realitexpire __P((void *)); 243 244 void hardclock __P((struct clockframe *)); 245 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS 246 void softclock __P((void *)); 247 #endif 248 void statclock __P((struct clockframe *)); 249 #ifdef NTP 250 void hardupdate __P((long offset)); 251 #ifdef PPS_SYNC 252 void hardpps __P((struct timeval *, long)); 253 #endif 254 #endif 255 256 void initclocks __P((void)); 257 void inittodr __P((time_t)); 258 void resettodr __P((void)); 259 void cpu_initclocks __P((void)); 260 261 void startprofclock __P((struct proc *)); 262 void stopprofclock __P((struct proc *)); 263 void proftick __P((struct clockframe *)); 264 void setstatclockrate __P((int)); 265 266 /* 267 * Shutdown hooks. Functions to be run with all interrupts disabled 268 * immediately before the system is halted or rebooted. 269 */ 270 void *shutdownhook_establish __P((void (*)(void *), void *)); 271 void shutdownhook_disestablish __P((void *)); 272 void doshutdownhooks __P((void)); 273 274 /* 275 * Power managment hooks. 276 */ 277 void *powerhook_establish __P((void (*)(int, void *), void *)); 278 void powerhook_disestablish __P((void *)); 279 void dopowerhooks __P((int)); 280 #define PWR_RESUME 0 281 #define PWR_SUSPEND 1 282 #define PWR_STANDBY 2 283 #define PWR_SOFTRESUME 3 284 #define PWR_SOFTSUSPEND 4 285 #define PWR_SOFTSTANDBY 5 286 287 /* 288 * Mountroot hooks (and mountroot declaration). Device drivers establish 289 * these to be executed just before (*mountroot)() if the passed device is 290 * selected as the root device. 291 */ 292 extern int (*mountroot)(void); 293 void *mountroothook_establish __P((void (*)(struct device *), 294 struct device *)); 295 void mountroothook_disestablish __P((void *)); 296 void mountroothook_destroy __P((void)); 297 void domountroothook __P((void)); 298 299 /* 300 * Exec hooks. Subsystems may want to do cleanup when a process 301 * execs. 302 */ 303 void *exechook_establish __P((void (*)(struct proc *, void *), void *)); 304 void exechook_disestablish __P((void *)); 305 void doexechooks __P((struct proc *)); 306 307 /* 308 * Exit hooks. Subsystems may want to do cleanup when a process exits. 309 */ 310 void *exithook_establish __P((void (*)(struct proc *, void *), void *)); 311 void exithook_disestablish __P((void *)); 312 void doexithooks __P((struct proc *)); 313 314 /* 315 * kernel syscall tracing/debugging hooks. 316 */ 317 int trace_enter __P((struct proc *, register_t, register_t, 318 void *, register_t [])); 319 void trace_exit __P((struct proc *, register_t, void *, register_t [], int)); 320 321 int uiomove __P((void *, size_t, struct uio *)); 322 323 #ifdef _KERNEL 324 caddr_t allocsys __P((caddr_t, caddr_t (*)(caddr_t))); 325 #define ALLOCSYS(base, name, type, num) \ 326 (name) = (type *)(base); (base) = (caddr_t)ALIGN((name)+(num)) 327 328 int setjmp __P((label_t *)); 329 void longjmp __P((label_t *)); 330 #endif 331 332 void consinit __P((void)); 333 334 void cpu_startup __P((void)); 335 void cpu_configure __P((void)); 336 void cpu_rootconf __P((void)); 337 void cpu_dumpconf __P((void)); 338 339 #ifdef GPROF 340 void kmstartup __P((void)); 341 #endif 342 343 #ifdef _KERNEL 344 #include <lib/libkern/libkern.h> 345 346 /* 347 * Stuff to handle debugger magic key sequences. 348 */ 349 #define CNS_LEN 128 350 #define CNS_MAGIC_VAL(x) ((x)&0x1ff) 351 #define CNS_MAGIC_NEXT(x) (((x)>>9)&0x7f) 352 #define CNS_TERM 0x7f /* End of sequence */ 353 354 typedef struct cnm_state { 355 int cnm_state; 356 u_short *cnm_magic; 357 } cnm_state_t; 358 359 /* Override db_console() in MD headers */ 360 #ifndef cn_trap 361 #define cn_trap() console_debugger() 362 #endif 363 #ifndef cn_isconsole 364 #define cn_isconsole(d) (cn_tab != NULL && (d) == cn_tab->cn_dev) 365 #endif 366 367 void cn_init_magic __P((cnm_state_t *cnm)); 368 void cn_destroy_magic __P((cnm_state_t *cnm)); 369 int cn_set_magic __P((char *magic)); 370 int cn_get_magic __P((char *magic, int len)); 371 /* This should be called for each byte read */ 372 #ifndef cn_check_magic 373 #define cn_check_magic(d, k, s) \ 374 do { \ 375 if (cn_isconsole(d)) { \ 376 int v = (s).cnm_magic[(s).cnm_state]; \ 377 if ((k) == CNS_MAGIC_VAL(v)) { \ 378 (s).cnm_state = CNS_MAGIC_NEXT(v); \ 379 if ((s).cnm_state == CNS_TERM) { \ 380 cn_trap(); \ 381 (s).cnm_state = 0; \ 382 } \ 383 } else { \ 384 (s).cnm_state = 0; \ 385 } \ 386 } \ 387 } while (/* CONSTCOND */ 0) 388 #endif 389 390 /* Encode out-of-band events this way when passing to cn_check_magic() */ 391 #define CNC_BREAK 0x100 392 393 #if defined(DDB) || defined(sun3) || defined(sun2) 394 /* note that cpu_Debugger() is always available on sun[23] */ 395 void cpu_Debugger __P((void)); 396 #define Debugger cpu_Debugger 397 #endif 398 399 #ifdef DDB 400 /* 401 * Enter debugger(s) from console attention if enabled 402 */ 403 extern int db_fromconsole; /* XXX ddb/ddbvar.h */ 404 #define console_debugger() if (db_fromconsole) Debugger() 405 #elif defined(Debugger) 406 #define console_debugger() Debugger() 407 #else 408 #define console_debugger() do {} while (/* CONSTCOND */ 0) /* NOP */ 409 #endif 410 #endif /* _KERNEL */ 411 412 #ifdef SYSCALL_DEBUG 413 void scdebug_call __P((struct proc *, register_t, register_t[])); 414 void scdebug_ret __P((struct proc *, register_t, int, register_t[])); 415 #endif /* SYSCALL_DEBUG */ 416 417 #if defined(MULTIPROCESSOR) 418 void _kernel_lock_init(void); 419 void _kernel_lock(int); 420 void _kernel_unlock(void); 421 void _kernel_proc_lock(struct proc *); 422 void _kernel_proc_unlock(struct proc *); 423 424 #define KERNEL_LOCK_INIT() _kernel_lock_init() 425 #define KERNEL_LOCK(flag) _kernel_lock((flag)) 426 #define KERNEL_UNLOCK() _kernel_unlock() 427 #define KERNEL_PROC_LOCK(p) _kernel_proc_lock((p)) 428 #define KERNEL_PROC_UNLOCK(p) _kernel_proc_unlock((p)) 429 430 #else /* ! MULTIPROCESSOR */ 431 432 #define KERNEL_LOCK_INIT() /* nothing */ 433 #define KERNEL_LOCK(flag) /* nothing */ 434 #define KERNEL_UNLOCK() /* nothing */ 435 #define KERNEL_PROC_LOCK(p) /* nothing */ 436 #define KERNEL_PROC_UNLOCK(p) /* nothing */ 437 438 #endif /* MULTIPROCESSOR */ 439 440 #endif /* !_SYS_SYSTM_H_ */ 441