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