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