1 /* $NetBSD: systm.h,v 1.268 2015/08/28 07:18:40 knakahara 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 sysent; 61 struct timeval; 62 struct tty; 63 struct uio; 64 struct vnode; 65 struct vmspace; 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 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 extern const char buildinfo[]; /* information from build environment */ 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 uint32_t sy_entry; /* DTrace entry ID for systrace. */ 125 uint32_t sy_return; /* DTrace return ID for systrace. */ 126 } sysent[]; 127 extern int nsysent; 128 #if BYTE_ORDER == BIG_ENDIAN 129 #define SCARG(p,k) ((p)->k.be.datum) /* get arg from args pointer */ 130 #elif BYTE_ORDER == LITTLE_ENDIAN 131 #define SCARG(p,k) ((p)->k.le.datum) /* get arg from args pointer */ 132 #else 133 #error "what byte order is this machine?" 134 #endif 135 136 #define SYCALL_INDIRECT 0x0000002 /* indirect (ie syscall() or __syscall()) */ 137 #define SYCALL_NARGS64_MASK 0x000f000 /* count of 64bit args */ 138 #define SYCALL_RET_64 0x0010000 /* retval is a 64bit integer value */ 139 #define SYCALL_ARG0_64 0x0020000 140 #define SYCALL_ARG1_64 0x0040000 141 #define SYCALL_ARG2_64 0x0080000 142 #define SYCALL_ARG3_64 0x0100000 143 #define SYCALL_ARG4_64 0x0200000 144 #define SYCALL_ARG5_64 0x0400000 145 #define SYCALL_ARG6_64 0x0800000 146 #define SYCALL_ARG7_64 0x1000000 147 #define SYCALL_NOSYS 0x2000000 /* permanent nosys in sysent[] */ 148 #define SYCALL_ARG_PTR 0x4000000 /* at least one argument is a pointer */ 149 #define SYCALL_RET_64_P(sy) ((sy)->sy_flags & SYCALL_RET_64) 150 #define SYCALL_ARG_64_P(sy, n) ((sy)->sy_flags & (SYCALL_ARG0_64 << (n))) 151 #define SYCALL_ARG_64_MASK(sy) (((sy)->sy_flags >> 17) & 0xff) 152 #define SYCALL_ARG_PTR_P(sy) ((sy)->sy_flags & SYCALL_ARG_PTR) 153 #define SYCALL_NARGS64(sy) (((sy)->sy_flags >> 12) & 0x0f) 154 #define SYCALL_NARGS64_VAL(n) ((n) << 12) 155 156 extern int boothowto; /* reboot flags, from console subsystem */ 157 #define bootverbose (boothowto & AB_VERBOSE) 158 #define bootquiet (boothowto & AB_QUIET) 159 160 extern void (*v_putc)(int); /* Virtual console putc routine */ 161 162 /* 163 * General function declarations. 164 */ 165 void voidop(void); 166 int nullop(void *); 167 void* nullret(void); 168 int enodev(void); 169 int enosys(void); 170 int enoioctl(void); 171 int enxio(void); 172 int eopnotsupp(void); 173 174 enum hashtype { 175 HASH_LIST, 176 HASH_SLIST, 177 HASH_TAILQ 178 }; 179 180 #ifdef _KERNEL 181 void *hashinit(u_int, enum hashtype, bool, u_long *); 182 void hashdone(void *, enum hashtype, u_long); 183 int seltrue(dev_t, int, struct lwp *); 184 int sys_nosys(struct lwp *, const void *, register_t *); 185 int sys_nomodule(struct lwp *, const void *, register_t *); 186 187 void aprint_normal(const char *, ...) __printflike(1, 2); 188 void aprint_error(const char *, ...) __printflike(1, 2); 189 void aprint_naive(const char *, ...) __printflike(1, 2); 190 void aprint_verbose(const char *, ...) __printflike(1, 2); 191 void aprint_debug(const char *, ...) __printflike(1, 2); 192 193 void device_printf(device_t, const char *fmt, ...) __printflike(2, 3); 194 195 void aprint_normal_dev(device_t, const char *, ...) __printflike(2, 3); 196 void aprint_error_dev(device_t, const char *, ...) __printflike(2, 3); 197 void aprint_naive_dev(device_t, const char *, ...) __printflike(2, 3); 198 void aprint_verbose_dev(device_t, const char *, ...) __printflike(2, 3); 199 void aprint_debug_dev(device_t, const char *, ...) __printflike(2, 3); 200 201 struct ifnet; 202 203 void aprint_normal_ifnet(struct ifnet *, const char *, ...) 204 __printflike(2, 3); 205 void aprint_error_ifnet(struct ifnet *, const char *, ...) 206 __printflike(2, 3); 207 void aprint_naive_ifnet(struct ifnet *, const char *, ...) 208 __printflike(2, 3); 209 void aprint_verbose_ifnet(struct ifnet *, const char *, ...) 210 __printflike(2, 3); 211 void aprint_debug_ifnet(struct ifnet *, const char *, ...) 212 __printflike(2, 3); 213 214 int aprint_get_error_count(void); 215 216 void printf_tolog(const char *, ...) __printflike(1, 2); 217 218 void printf_nolog(const char *, ...) __printflike(1, 2); 219 220 void printf(const char *, ...) __printflike(1, 2); 221 222 int snprintf(char *, size_t, const char *, ...) __printflike(3, 4); 223 224 void vprintf(const char *, va_list) __printflike(1, 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 struct sysent *, const void *); 396 void trace_exit(register_t, const struct sysent *, const void *, 397 register_t [], int); 398 #endif 399 400 int uiomove(void *, size_t, struct uio *); 401 int uiomove_frombuf(void *, size_t, struct uio *); 402 403 #ifdef _KERNEL 404 int setjmp(label_t *) __returns_twice; 405 void longjmp(label_t *) __dead; 406 #endif 407 408 void consinit(void); 409 410 void cpu_startup(void); 411 void cpu_configure(void); 412 void cpu_bootconf(void); 413 void cpu_rootconf(void); 414 void cpu_dumpconf(void); 415 416 #ifdef GPROF 417 void kmstartup(void); 418 #endif 419 420 void machdep_init(void); 421 422 #ifdef _KERNEL 423 #include <lib/libkern/libkern.h> 424 425 /* 426 * Stuff to handle debugger magic key sequences. 427 */ 428 #define CNS_LEN 128 429 #define CNS_MAGIC_VAL(x) ((x)&0x1ff) 430 #define CNS_MAGIC_NEXT(x) (((x)>>9)&0x7f) 431 #define CNS_TERM 0x7f /* End of sequence */ 432 433 typedef struct cnm_state { 434 int cnm_state; 435 u_short *cnm_magic; 436 } cnm_state_t; 437 438 /* Override db_console() in MD headers */ 439 #ifndef cn_trap 440 #define cn_trap() console_debugger() 441 #endif 442 #ifndef cn_isconsole 443 #define cn_isconsole(d) (cn_tab != NULL && (d) == cn_tab->cn_dev) 444 #endif 445 446 void cn_init_magic(cnm_state_t *); 447 void cn_destroy_magic(cnm_state_t *); 448 int cn_set_magic(const char *); 449 int cn_get_magic(char *, size_t); 450 /* This should be called for each byte read */ 451 #ifndef cn_check_magic 452 #define cn_check_magic(d, k, s) \ 453 do { \ 454 if (cn_isconsole(d)) { \ 455 int _v = (s).cnm_magic[(s).cnm_state]; \ 456 if ((k) == CNS_MAGIC_VAL(_v)) { \ 457 (s).cnm_state = CNS_MAGIC_NEXT(_v); \ 458 if ((s).cnm_state == CNS_TERM) { \ 459 cn_trap(); \ 460 (s).cnm_state = 0; \ 461 } \ 462 } else { \ 463 (s).cnm_state = 0; \ 464 } \ 465 } \ 466 } while (/* CONSTCOND */ 0) 467 #endif 468 469 /* Encode out-of-band events this way when passing to cn_check_magic() */ 470 #define CNC_BREAK 0x100 471 472 #if defined(DDB) || defined(sun3) || defined(sun2) 473 /* note that cpu_Debugger() is always available on sun[23] */ 474 void cpu_Debugger(void); 475 #define Debugger cpu_Debugger 476 #endif 477 478 #ifdef DDB 479 /* 480 * Enter debugger(s) from console attention if enabled 481 */ 482 extern int db_fromconsole; /* XXX ddb/ddbvar.h */ 483 #define console_debugger() if (db_fromconsole) Debugger() 484 #elif defined(Debugger) 485 #define console_debugger() Debugger() 486 #else 487 #define console_debugger() do {} while (/* CONSTCOND */ 0) /* NOP */ 488 #endif 489 #endif /* _KERNEL */ 490 491 /* For SYSCALL_DEBUG */ 492 void scdebug_call(register_t, const register_t[]); 493 void scdebug_ret(register_t, int, const register_t[]); 494 495 void kernel_lock_init(void); 496 void _kernel_lock(int); 497 void _kernel_unlock(int, int *); 498 bool _kernel_locked_p(void); 499 500 #ifdef _KERNEL 501 void kernconfig_lock_init(void); 502 void kernconfig_lock(void); 503 void kernconfig_unlock(void); 504 bool kernconfig_is_held(void); 505 #endif 506 507 #if defined(MULTIPROCESSOR) || defined(_MODULE) 508 #define KERNEL_LOCK(count, lwp) \ 509 do { \ 510 if ((count) != 0) \ 511 _kernel_lock((count)); \ 512 } while (/* CONSTCOND */ 0) 513 #define KERNEL_UNLOCK(all, lwp, p) _kernel_unlock((all), (p)) 514 #define KERNEL_LOCKED_P() _kernel_locked_p() 515 #else 516 #define KERNEL_LOCK(count, lwp) do {(void)(count); (void)(lwp);} while (/* CONSTCOND */ 0) /*NOP*/ 517 #define KERNEL_UNLOCK(all, lwp, ptr) do {(void)(all); (void)(lwp); (void)(ptr);} while (/* CONSTCOND */ 0) /*NOP*/ 518 #define KERNEL_LOCKED_P() (true) 519 #endif 520 521 #define KERNEL_UNLOCK_LAST(l) KERNEL_UNLOCK(-1, (l), NULL) 522 #define KERNEL_UNLOCK_ALL(l, p) KERNEL_UNLOCK(0, (l), (p)) 523 #define KERNEL_UNLOCK_ONE(l) KERNEL_UNLOCK(1, (l), NULL) 524 525 /* Preemption control. */ 526 #ifdef _KERNEL 527 void kpreempt_disable(void); 528 void kpreempt_enable(void); 529 bool kpreempt_disabled(void); 530 #endif 531 532 void assert_sleepable(void); 533 #if defined(DEBUG) 534 #define ASSERT_SLEEPABLE() assert_sleepable() 535 #else /* defined(DEBUG) */ 536 #define ASSERT_SLEEPABLE() do {} while (0) 537 #endif /* defined(DEBUG) */ 538 539 vaddr_t calc_cache_size(vsize_t , int, int); 540 541 #endif /* !_SYS_SYSTM_H_ */ 542