1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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 * $FreeBSD$ 38 */ 39 40 #ifndef _SYS_SYSTM_H_ 41 #define _SYS_SYSTM_H_ 42 43 #include <sys/cdefs.h> 44 #include <machine/atomic.h> 45 #include <machine/cpufunc.h> 46 #include <sys/callout.h> 47 #include <sys/queue.h> 48 #include <sys/stdint.h> /* for people using printf mainly */ 49 50 __NULLABILITY_PRAGMA_PUSH 51 52 extern int cold; /* nonzero if we are doing a cold boot */ 53 extern int suspend_blocked; /* block suspend due to pending shutdown */ 54 extern int rebooting; /* kern_reboot() has been called. */ 55 extern const char *panicstr; /* panic message */ 56 extern char version[]; /* system version */ 57 extern char compiler_version[]; /* compiler version */ 58 extern char copyright[]; /* system copyright */ 59 extern int kstack_pages; /* number of kernel stack pages */ 60 61 extern u_long pagesizes[]; /* supported page sizes */ 62 extern long physmem; /* physical memory */ 63 extern long realmem; /* 'real' memory */ 64 65 extern char *rootdevnames[2]; /* names of possible root devices */ 66 67 extern int boothowto; /* reboot flags, from console subsystem */ 68 extern int bootverbose; /* nonzero to print verbose messages */ 69 70 extern int maxusers; /* system tune hint */ 71 extern int ngroups_max; /* max # of supplemental groups */ 72 extern int vm_guest; /* Running as virtual machine guest? */ 73 74 /* 75 * Detected virtual machine guest types. The intention is to expand 76 * and/or add to the VM_GUEST_VM type if specific VM functionality is 77 * ever implemented (e.g. vendor-specific paravirtualization features). 78 * Keep in sync with vm_guest_sysctl_names[]. 79 */ 80 enum VM_GUEST { VM_GUEST_NO = 0, VM_GUEST_VM, VM_GUEST_XEN, VM_GUEST_HV, 81 VM_GUEST_VMWARE, VM_GUEST_KVM, VM_GUEST_BHYVE, VM_LAST }; 82 83 #if defined(WITNESS) || defined(INVARIANT_SUPPORT) 84 void kassert_panic(const char *fmt, ...) __printflike(1, 2); 85 #endif 86 87 #ifdef INVARIANTS /* The option is always available */ 88 #define KASSERT(exp,msg) do { \ 89 if (__predict_false(!(exp))) \ 90 kassert_panic msg; \ 91 } while (0) 92 #define VNASSERT(exp, vp, msg) do { \ 93 if (__predict_false(!(exp))) { \ 94 vn_printf(vp, "VNASSERT failed\n"); \ 95 kassert_panic msg; \ 96 } \ 97 } while (0) 98 #else 99 #define KASSERT(exp,msg) do { \ 100 } while (0) 101 102 #define VNASSERT(exp, vp, msg) do { \ 103 } while (0) 104 #endif 105 106 #ifndef CTASSERT /* Allow lint to override */ 107 #define CTASSERT(x) _Static_assert(x, "compile-time assertion failed") 108 #endif 109 110 #if defined(_KERNEL) 111 #include <sys/param.h> /* MAXCPU */ 112 #include <sys/pcpu.h> /* curthread */ 113 #include <sys/kpilite.h> 114 #endif 115 116 /* 117 * Assert that a pointer can be loaded from memory atomically. 118 * 119 * This assertion enforces stronger alignment than necessary. For example, 120 * on some architectures, atomicity for unaligned loads will depend on 121 * whether or not the load spans multiple cache lines. 122 */ 123 #define ASSERT_ATOMIC_LOAD_PTR(var, msg) \ 124 KASSERT(sizeof(var) == sizeof(void *) && \ 125 ((uintptr_t)&(var) & (sizeof(void *) - 1)) == 0, msg) 126 127 /* 128 * Assert that a thread is in critical(9) section. 129 */ 130 #define CRITICAL_ASSERT(td) \ 131 KASSERT((td)->td_critnest >= 1, ("Not in critical section")); 132 133 /* 134 * If we have already panic'd and this is the thread that called 135 * panic(), then don't block on any mutexes but silently succeed. 136 * Otherwise, the kernel will deadlock since the scheduler isn't 137 * going to run the thread that holds any lock we need. 138 */ 139 #define SCHEDULER_STOPPED_TD(td) ({ \ 140 MPASS((td) == curthread); \ 141 __predict_false((td)->td_stopsched); \ 142 }) 143 #define SCHEDULER_STOPPED() SCHEDULER_STOPPED_TD(curthread) 144 145 /* 146 * Align variables. 147 */ 148 #define __read_mostly __section(".data.read_mostly") 149 #define __read_frequently __section(".data.read_frequently") 150 #define __exclusive_cache_line __aligned(CACHE_LINE_SIZE) \ 151 __section(".data.exclusive_cache_line") 152 /* 153 * XXX the hints declarations are even more misplaced than most declarations 154 * in this file, since they are needed in one file (per arch) and only used 155 * in two files. 156 * XXX most of these variables should be const. 157 */ 158 extern int osreldate; 159 extern bool dynamic_kenv; 160 extern struct mtx kenv_lock; 161 extern char *kern_envp; 162 extern char *md_envp; 163 extern char static_env[]; 164 extern char static_hints[]; /* by config for now */ 165 166 extern char **kenvp; 167 168 extern const void *zero_region; /* address space maps to a zeroed page */ 169 170 extern int unmapped_buf_allowed; 171 172 #ifdef __LP64__ 173 #define IOSIZE_MAX iosize_max() 174 #define DEVFS_IOSIZE_MAX devfs_iosize_max() 175 #else 176 #define IOSIZE_MAX SSIZE_MAX 177 #define DEVFS_IOSIZE_MAX SSIZE_MAX 178 #endif 179 180 /* 181 * General function declarations. 182 */ 183 184 struct inpcb; 185 struct lock_object; 186 struct malloc_type; 187 struct mtx; 188 struct proc; 189 struct socket; 190 struct thread; 191 struct tty; 192 struct ucred; 193 struct uio; 194 struct _jmp_buf; 195 struct trapframe; 196 struct eventtimer; 197 198 int setjmp(struct _jmp_buf *) __returns_twice; 199 void longjmp(struct _jmp_buf *, int) __dead2; 200 int dumpstatus(vm_offset_t addr, off_t count); 201 int nullop(void); 202 int eopnotsupp(void); 203 int ureadc(int, struct uio *); 204 void hashdestroy(void *, struct malloc_type *, u_long); 205 void *hashinit(int count, struct malloc_type *type, u_long *hashmask); 206 void *hashinit_flags(int count, struct malloc_type *type, 207 u_long *hashmask, int flags); 208 #define HASH_NOWAIT 0x00000001 209 #define HASH_WAITOK 0x00000002 210 211 void *phashinit(int count, struct malloc_type *type, u_long *nentries); 212 void *phashinit_flags(int count, struct malloc_type *type, u_long *nentries, 213 int flags); 214 void g_waitidle(void); 215 216 void panic(const char *, ...) __dead2 __printflike(1, 2); 217 void vpanic(const char *, __va_list) __dead2 __printflike(1, 0); 218 219 void cpu_boot(int); 220 void cpu_flush_dcache(void *, size_t); 221 void cpu_rootconf(void); 222 void critical_enter_KBI(void); 223 void critical_exit_KBI(void); 224 void critical_exit_preempt(void); 225 void init_param1(void); 226 void init_param2(long physpages); 227 void init_static_kenv(char *, size_t); 228 void tablefull(const char *); 229 230 #if defined(KLD_MODULE) || defined(KTR_CRITICAL) || !defined(_KERNEL) || defined(GENOFFSET) 231 #define critical_enter() critical_enter_KBI() 232 #define critical_exit() critical_exit_KBI() 233 #else 234 static __inline void 235 critical_enter(void) 236 { 237 struct thread_lite *td; 238 239 td = (struct thread_lite *)curthread; 240 td->td_critnest++; 241 __compiler_membar(); 242 } 243 244 static __inline void 245 critical_exit(void) 246 { 247 struct thread_lite *td; 248 249 td = (struct thread_lite *)curthread; 250 KASSERT(td->td_critnest != 0, 251 ("critical_exit: td_critnest == 0")); 252 __compiler_membar(); 253 td->td_critnest--; 254 __compiler_membar(); 255 if (__predict_false(td->td_owepreempt)) 256 critical_exit_preempt(); 257 258 } 259 #endif 260 261 262 #ifdef EARLY_PRINTF 263 typedef void early_putc_t(int ch); 264 extern early_putc_t *early_putc; 265 #endif 266 int kvprintf(char const *, void (*)(int, void*), void *, int, 267 __va_list) __printflike(1, 0); 268 void log(int, const char *, ...) __printflike(2, 3); 269 void log_console(struct uio *); 270 void vlog(int, const char *, __va_list) __printflike(2, 0); 271 int asprintf(char **ret, struct malloc_type *mtp, const char *format, 272 ...) __printflike(3, 4); 273 int printf(const char *, ...) __printflike(1, 2); 274 int snprintf(char *, size_t, const char *, ...) __printflike(3, 4); 275 int sprintf(char *buf, const char *, ...) __printflike(2, 3); 276 int uprintf(const char *, ...) __printflike(1, 2); 277 int vprintf(const char *, __va_list) __printflike(1, 0); 278 int vasprintf(char **ret, struct malloc_type *mtp, const char *format, 279 __va_list ap) __printflike(3, 0); 280 int vsnprintf(char *, size_t, const char *, __va_list) __printflike(3, 0); 281 int vsnrprintf(char *, size_t, int, const char *, __va_list) __printflike(4, 0); 282 int vsprintf(char *buf, const char *, __va_list) __printflike(2, 0); 283 int ttyprintf(struct tty *, const char *, ...) __printflike(2, 3); 284 int sscanf(const char *, char const * _Nonnull, ...) __scanflike(2, 3); 285 int vsscanf(const char * _Nonnull, char const * _Nonnull, __va_list) __scanflike(2, 0); 286 long strtol(const char *, char **, int); 287 u_long strtoul(const char *, char **, int); 288 quad_t strtoq(const char *, char **, int); 289 u_quad_t strtouq(const char *, char **, int); 290 void tprintf(struct proc *p, int pri, const char *, ...) __printflike(3, 4); 291 void vtprintf(struct proc *, int, const char *, __va_list) __printflike(3, 0); 292 void hexdump(const void *ptr, int length, const char *hdr, int flags); 293 #define HD_COLUMN_MASK 0xff 294 #define HD_DELIM_MASK 0xff00 295 #define HD_OMIT_COUNT (1 << 16) 296 #define HD_OMIT_HEX (1 << 17) 297 #define HD_OMIT_CHARS (1 << 18) 298 299 #define ovbcopy(f, t, l) bcopy((f), (t), (l)) 300 void bcopy(const void * _Nonnull from, void * _Nonnull to, size_t len); 301 #define bcopy(from, to, len) __builtin_memmove((to), (from), (len)) 302 void bzero(void * _Nonnull buf, size_t len); 303 #define bzero(buf, len) __builtin_memset((buf), 0, (len)) 304 void explicit_bzero(void * _Nonnull, size_t); 305 int bcmp(const void *b1, const void *b2, size_t len); 306 #define bcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len)) 307 308 void *memset(void * _Nonnull buf, int c, size_t len); 309 #define memset(buf, c, len) __builtin_memset((buf), (c), (len)) 310 void *memcpy(void * _Nonnull to, const void * _Nonnull from, size_t len); 311 #define memcpy(to, from, len) __builtin_memcpy((to), (from), (len)) 312 void *memmove(void * _Nonnull dest, const void * _Nonnull src, size_t n); 313 #define memmove(dest, src, n) __builtin_memmove((dest), (src), (n)) 314 int memcmp(const void *b1, const void *b2, size_t len); 315 #define memcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len)) 316 317 int copystr(const void * _Nonnull __restrict kfaddr, 318 void * _Nonnull __restrict kdaddr, size_t len, 319 size_t * __restrict lencopied); 320 int copyinstr(const void * __restrict udaddr, 321 void * _Nonnull __restrict kaddr, size_t len, 322 size_t * __restrict lencopied); 323 int copyin(const void * __restrict udaddr, 324 void * _Nonnull __restrict kaddr, size_t len); 325 int copyin_nofault(const void * __restrict udaddr, 326 void * _Nonnull __restrict kaddr, size_t len); 327 int copyout(const void * _Nonnull __restrict kaddr, 328 void * __restrict udaddr, size_t len); 329 int copyout_nofault(const void * _Nonnull __restrict kaddr, 330 void * __restrict udaddr, size_t len); 331 332 int fubyte(volatile const void *base); 333 long fuword(volatile const void *base); 334 int fuword16(volatile const void *base); 335 int32_t fuword32(volatile const void *base); 336 int64_t fuword64(volatile const void *base); 337 int fueword(volatile const void *base, long *val); 338 int fueword32(volatile const void *base, int32_t *val); 339 int fueword64(volatile const void *base, int64_t *val); 340 int subyte(volatile void *base, int byte); 341 int suword(volatile void *base, long word); 342 int suword16(volatile void *base, int word); 343 int suword32(volatile void *base, int32_t word); 344 int suword64(volatile void *base, int64_t word); 345 uint32_t casuword32(volatile uint32_t *base, uint32_t oldval, uint32_t newval); 346 u_long casuword(volatile u_long *p, u_long oldval, u_long newval); 347 int casueword32(volatile uint32_t *base, uint32_t oldval, uint32_t *oldvalp, 348 uint32_t newval); 349 int casueword(volatile u_long *p, u_long oldval, u_long *oldvalp, 350 u_long newval); 351 352 void realitexpire(void *); 353 354 int sysbeep(int hertz, int period); 355 356 void hardclock(int usermode, uintfptr_t pc); 357 void hardclock_cnt(int cnt, int usermode); 358 void hardclock_cpu(int usermode); 359 void hardclock_sync(int cpu); 360 void softclock(void *); 361 void statclock(int usermode); 362 void statclock_cnt(int cnt, int usermode); 363 void profclock(int usermode, uintfptr_t pc); 364 void profclock_cnt(int cnt, int usermode, uintfptr_t pc); 365 366 int hardclockintr(void); 367 368 void startprofclock(struct proc *); 369 void stopprofclock(struct proc *); 370 void cpu_startprofclock(void); 371 void cpu_stopprofclock(void); 372 void suspendclock(void); 373 void resumeclock(void); 374 sbintime_t cpu_idleclock(void); 375 void cpu_activeclock(void); 376 void cpu_new_callout(int cpu, sbintime_t bt, sbintime_t bt_opt); 377 void cpu_et_frequency(struct eventtimer *et, uint64_t newfreq); 378 extern int cpu_disable_c2_sleep; 379 extern int cpu_disable_c3_sleep; 380 381 char *kern_getenv(const char *name); 382 void freeenv(char *env); 383 int getenv_int(const char *name, int *data); 384 int getenv_uint(const char *name, unsigned int *data); 385 int getenv_long(const char *name, long *data); 386 int getenv_ulong(const char *name, unsigned long *data); 387 int getenv_string(const char *name, char *data, int size); 388 int getenv_int64(const char *name, int64_t *data); 389 int getenv_uint64(const char *name, uint64_t *data); 390 int getenv_quad(const char *name, quad_t *data); 391 int kern_setenv(const char *name, const char *value); 392 int kern_unsetenv(const char *name); 393 int testenv(const char *name); 394 395 int getenv_array(const char *name, void *data, int size, int *psize, 396 int type_size, bool allow_signed); 397 #define GETENV_UNSIGNED false /* negative numbers not allowed */ 398 #define GETENV_SIGNED true /* negative numbers allowed */ 399 400 typedef uint64_t (cpu_tick_f)(void); 401 void set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var); 402 extern cpu_tick_f *cpu_ticks; 403 uint64_t cpu_tickrate(void); 404 uint64_t cputick2usec(uint64_t tick); 405 406 #ifdef APM_FIXUP_CALLTODO 407 struct timeval; 408 void adjust_timeout_calltodo(struct timeval *time_change); 409 #endif /* APM_FIXUP_CALLTODO */ 410 411 #include <sys/libkern.h> 412 413 /* Initialize the world */ 414 void consinit(void); 415 void cpu_initclocks(void); 416 void cpu_initclocks_bsp(void); 417 void cpu_initclocks_ap(void); 418 void usrinfoinit(void); 419 420 /* Finalize the world */ 421 void kern_reboot(int) __dead2; 422 void shutdown_nice(int); 423 424 /* Timeouts */ 425 typedef void timeout_t(void *); /* timeout function type */ 426 #define CALLOUT_HANDLE_INITIALIZER(handle) \ 427 { NULL } 428 429 void callout_handle_init(struct callout_handle *); 430 struct callout_handle timeout(timeout_t *, void *, int); 431 void untimeout(timeout_t *, void *, struct callout_handle); 432 433 /* Stubs for obsolete functions that used to be for interrupt management */ 434 static __inline intrmask_t splbio(void) { return 0; } 435 static __inline intrmask_t splcam(void) { return 0; } 436 static __inline intrmask_t splclock(void) { return 0; } 437 static __inline intrmask_t splhigh(void) { return 0; } 438 static __inline intrmask_t splimp(void) { return 0; } 439 static __inline intrmask_t splnet(void) { return 0; } 440 static __inline intrmask_t spltty(void) { return 0; } 441 static __inline void splx(intrmask_t ipl __unused) { return; } 442 443 /* 444 * Common `proc' functions are declared here so that proc.h can be included 445 * less often. 446 */ 447 int _sleep(void * _Nonnull chan, struct lock_object *lock, int pri, 448 const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags); 449 #define msleep(chan, mtx, pri, wmesg, timo) \ 450 _sleep((chan), &(mtx)->lock_object, (pri), (wmesg), \ 451 tick_sbt * (timo), 0, C_HARDCLOCK) 452 #define msleep_sbt(chan, mtx, pri, wmesg, bt, pr, flags) \ 453 _sleep((chan), &(mtx)->lock_object, (pri), (wmesg), (bt), (pr), \ 454 (flags)) 455 int msleep_spin_sbt(void * _Nonnull chan, struct mtx *mtx, 456 const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags); 457 #define msleep_spin(chan, mtx, wmesg, timo) \ 458 msleep_spin_sbt((chan), (mtx), (wmesg), tick_sbt * (timo), \ 459 0, C_HARDCLOCK) 460 int pause_sbt(const char *wmesg, sbintime_t sbt, sbintime_t pr, 461 int flags); 462 #define pause(wmesg, timo) \ 463 pause_sbt((wmesg), tick_sbt * (timo), 0, C_HARDCLOCK) 464 #define pause_sig(wmesg, timo) \ 465 pause_sbt((wmesg), tick_sbt * (timo), 0, C_HARDCLOCK | C_CATCH) 466 #define tsleep(chan, pri, wmesg, timo) \ 467 _sleep((chan), NULL, (pri), (wmesg), tick_sbt * (timo), \ 468 0, C_HARDCLOCK) 469 #define tsleep_sbt(chan, pri, wmesg, bt, pr, flags) \ 470 _sleep((chan), NULL, (pri), (wmesg), (bt), (pr), (flags)) 471 void wakeup(void * chan); 472 void wakeup_one(void * chan); 473 474 /* 475 * Common `struct cdev *' stuff are declared here to avoid #include poisoning 476 */ 477 478 struct cdev; 479 dev_t dev2udev(struct cdev *x); 480 const char *devtoname(struct cdev *cdev); 481 482 #ifdef __LP64__ 483 size_t devfs_iosize_max(void); 484 size_t iosize_max(void); 485 #endif 486 487 int poll_no_poll(int events); 488 489 /* XXX: Should be void nanodelay(u_int nsec); */ 490 void DELAY(int usec); 491 492 /* Root mount holdback API */ 493 struct root_hold_token; 494 495 struct root_hold_token *root_mount_hold(const char *identifier); 496 void root_mount_rel(struct root_hold_token *h); 497 int root_mounted(void); 498 499 500 /* 501 * Unit number allocation API. (kern/subr_unit.c) 502 */ 503 struct unrhdr; 504 struct unrhdr *new_unrhdr(int low, int high, struct mtx *mutex); 505 void init_unrhdr(struct unrhdr *uh, int low, int high, struct mtx *mutex); 506 void delete_unrhdr(struct unrhdr *uh); 507 void clear_unrhdr(struct unrhdr *uh); 508 void clean_unrhdr(struct unrhdr *uh); 509 void clean_unrhdrl(struct unrhdr *uh); 510 int alloc_unr(struct unrhdr *uh); 511 int alloc_unr_specific(struct unrhdr *uh, u_int item); 512 int alloc_unrl(struct unrhdr *uh); 513 void free_unr(struct unrhdr *uh, u_int item); 514 515 void intr_prof_stack_use(struct thread *td, struct trapframe *frame); 516 517 void counted_warning(unsigned *counter, const char *msg); 518 519 /* 520 * APIs to manage deprecation and obsolescence. 521 */ 522 struct device; 523 void _gone_in(int major, const char *msg); 524 void _gone_in_dev(struct device *dev, int major, const char *msg); 525 #ifdef NO_OBSOLETE_CODE 526 #define __gone_ok(m, msg) \ 527 _Static_assert(m < P_OSREL_MAJOR(__FreeBSD_version)), \ 528 "Obsolete code" msg); 529 #else 530 #define __gone_ok(m, msg) 531 #endif 532 #define gone_in(major, msg) __gone_ok(major, msg) _gone_in(major, msg) 533 #define gone_in_dev(dev, major, msg) __gone_ok(major, msg) _gone_in_dev(dev, major, msg) 534 535 __NULLABILITY_PRAGMA_POP 536 537 #endif /* !_SYS_SYSTM_H_ */ 538