2 * SPDX-License-Identifier: BSD-3-Clause
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
36 * @(#)systm.h 8.7 (Berkeley) 3/29/95
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 */
50 __NULLABILITY_PRAGMA_PUSH
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 */
61 extern u_long pagesizes[]; /* supported page sizes */
62 extern long physmem; /* physical memory */
63 extern long realmem; /* 'real' memory */
65 extern char *rootdevnames[2]; /* names of possible root devices */
67 extern int boothowto; /* reboot flags, from console subsystem */
68 extern int bootverbose; /* nonzero to print verbose messages */
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? */
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[].
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_GUEST_VBOX,
82 VM_GUEST_PARALLELS, VM_LAST };
85 * These functions need to be declared before the KASSERT macro is invoked in
86 * !KASSERT_PANIC_OPTIONAL builds, so their declarations are sort of out of
87 * place compared to other function definitions in this header. On the other
88 * hand, this header is a bit disorganized anyway.
90 void panic(const char *, ...) __dead2 __printflike(1, 2);
91 void vpanic(const char *, __va_list) __dead2 __printflike(1, 0);
93 #if defined(WITNESS) || defined(INVARIANT_SUPPORT)
94 #ifdef KASSERT_PANIC_OPTIONAL
95 void kassert_panic(const char *fmt, ...) __printflike(1, 2);
97 #define kassert_panic panic
101 #ifdef INVARIANTS /* The option is always available */
102 #define KASSERT(exp,msg) do { \
103 if (__predict_false(!(exp))) \
106 #define VNASSERT(exp, vp, msg) do { \
107 if (__predict_false(!(exp))) { \
108 vn_printf(vp, "VNASSERT failed\n"); \
113 #define KASSERT(exp,msg) do { \
116 #define VNASSERT(exp, vp, msg) do { \
120 #ifndef CTASSERT /* Allow lint to override */
121 #define CTASSERT(x) _Static_assert(x, "compile-time assertion failed")
125 #include <sys/param.h> /* MAXCPU */
126 #include <sys/pcpu.h> /* curthread */
127 #include <sys/kpilite.h>
131 * Assert that a pointer can be loaded from memory atomically.
133 * This assertion enforces stronger alignment than necessary. For example,
134 * on some architectures, atomicity for unaligned loads will depend on
135 * whether or not the load spans multiple cache lines.
137 #define ASSERT_ATOMIC_LOAD_PTR(var, msg) \
138 KASSERT(sizeof(var) == sizeof(void *) && \
139 ((uintptr_t)&(var) & (sizeof(void *) - 1)) == 0, msg)
142 * Assert that a thread is in critical(9) section.
144 #define CRITICAL_ASSERT(td) \
145 KASSERT((td)->td_critnest >= 1, ("Not in critical section"));
148 * If we have already panic'd and this is the thread that called
149 * panic(), then don't block on any mutexes but silently succeed.
150 * Otherwise, the kernel will deadlock since the scheduler isn't
151 * going to run the thread that holds any lock we need.
153 #define SCHEDULER_STOPPED_TD(td) ({ \
154 MPASS((td) == curthread); \
155 __predict_false((td)->td_stopsched); \
157 #define SCHEDULER_STOPPED() SCHEDULER_STOPPED_TD(curthread)
162 #define __read_mostly __section(".data.read_mostly")
163 #define __read_frequently __section(".data.read_frequently")
164 #define __exclusive_cache_line __aligned(CACHE_LINE_SIZE) \
165 __section(".data.exclusive_cache_line")
167 * XXX the hints declarations are even more misplaced than most declarations
168 * in this file, since they are needed in one file (per arch) and only used
170 * XXX most of these variables should be const.
172 extern int osreldate;
173 extern bool dynamic_kenv;
174 extern struct mtx kenv_lock;
175 extern char *kern_envp;
176 extern char *md_envp;
177 extern char static_env[];
178 extern char static_hints[]; /* by config for now */
182 extern const void *zero_region; /* address space maps to a zeroed page */
184 extern int unmapped_buf_allowed;
187 #define IOSIZE_MAX iosize_max()
188 #define DEVFS_IOSIZE_MAX devfs_iosize_max()
190 #define IOSIZE_MAX SSIZE_MAX
191 #define DEVFS_IOSIZE_MAX SSIZE_MAX
195 * General function declarations.
212 int setjmp(struct _jmp_buf *) __returns_twice;
213 void longjmp(struct _jmp_buf *, int) __dead2;
214 int dumpstatus(vm_offset_t addr, off_t count);
216 int eopnotsupp(void);
217 int ureadc(int, struct uio *);
218 void hashdestroy(void *, struct malloc_type *, u_long);
219 void *hashinit(int count, struct malloc_type *type, u_long *hashmask);
220 void *hashinit_flags(int count, struct malloc_type *type,
221 u_long *hashmask, int flags);
222 #define HASH_NOWAIT 0x00000001
223 #define HASH_WAITOK 0x00000002
225 void *phashinit(int count, struct malloc_type *type, u_long *nentries);
226 void *phashinit_flags(int count, struct malloc_type *type, u_long *nentries,
228 void g_waitidle(void);
231 void cpu_flush_dcache(void *, size_t);
232 void cpu_rootconf(void);
233 void critical_enter_KBI(void);
234 void critical_exit_KBI(void);
235 void critical_exit_preempt(void);
236 void init_param1(void);
237 void init_param2(long physpages);
238 void init_static_kenv(char *, size_t);
239 void tablefull(const char *);
242 * Allocate per-thread "current" state in the linuxkpi
244 extern int (*lkpi_alloc_current)(struct thread *, int);
245 int linux_alloc_current_noop(struct thread *, int);
248 #if defined(KLD_MODULE) || defined(KTR_CRITICAL) || !defined(_KERNEL) || defined(GENOFFSET)
249 #define critical_enter() critical_enter_KBI()
250 #define critical_exit() critical_exit_KBI()
255 struct thread_lite *td;
257 td = (struct thread_lite *)curthread;
265 struct thread_lite *td;
267 td = (struct thread_lite *)curthread;
268 KASSERT(td->td_critnest != 0,
269 ("critical_exit: td_critnest == 0"));
273 if (__predict_false(td->td_owepreempt))
274 critical_exit_preempt();
281 typedef void early_putc_t(int ch);
282 extern early_putc_t *early_putc;
284 int kvprintf(char const *, void (*)(int, void*), void *, int,
285 __va_list) __printflike(1, 0);
286 void log(int, const char *, ...) __printflike(2, 3);
287 void log_console(struct uio *);
288 void vlog(int, const char *, __va_list) __printflike(2, 0);
289 int asprintf(char **ret, struct malloc_type *mtp, const char *format,
290 ...) __printflike(3, 4);
291 int printf(const char *, ...) __printflike(1, 2);
292 int snprintf(char *, size_t, const char *, ...) __printflike(3, 4);
293 int sprintf(char *buf, const char *, ...) __printflike(2, 3);
294 int uprintf(const char *, ...) __printflike(1, 2);
295 int vprintf(const char *, __va_list) __printflike(1, 0);
296 int vasprintf(char **ret, struct malloc_type *mtp, const char *format,
297 __va_list ap) __printflike(3, 0);
298 int vsnprintf(char *, size_t, const char *, __va_list) __printflike(3, 0);
299 int vsnrprintf(char *, size_t, int, const char *, __va_list) __printflike(4, 0);
300 int vsprintf(char *buf, const char *, __va_list) __printflike(2, 0);
301 int sscanf(const char *, char const * _Nonnull, ...) __scanflike(2, 3);
302 int vsscanf(const char * _Nonnull, char const * _Nonnull, __va_list) __scanflike(2, 0);
303 long strtol(const char *, char **, int);
304 u_long strtoul(const char *, char **, int);
305 quad_t strtoq(const char *, char **, int);
306 u_quad_t strtouq(const char *, char **, int);
307 void tprintf(struct proc *p, int pri, const char *, ...) __printflike(3, 4);
308 void vtprintf(struct proc *, int, const char *, __va_list) __printflike(3, 0);
309 void hexdump(const void *ptr, int length, const char *hdr, int flags);
310 #define HD_COLUMN_MASK 0xff
311 #define HD_DELIM_MASK 0xff00
312 #define HD_OMIT_COUNT (1 << 16)
313 #define HD_OMIT_HEX (1 << 17)
314 #define HD_OMIT_CHARS (1 << 18)
316 #define ovbcopy(f, t, l) bcopy((f), (t), (l))
317 void bcopy(const void * _Nonnull from, void * _Nonnull to, size_t len);
318 void bzero(void * _Nonnull buf, size_t len);
319 void explicit_bzero(void * _Nonnull, size_t);
320 int bcmp(const void *b1, const void *b2, size_t len);
322 void *memset(void * _Nonnull buf, int c, size_t len);
323 void *memcpy(void * _Nonnull to, const void * _Nonnull from, size_t len);
324 void *memmove(void * _Nonnull dest, const void * _Nonnull src, size_t n);
325 int memcmp(const void *b1, const void *b2, size_t len);
328 void *kcsan_memset(void *, int, size_t);
329 void *kcsan_memcpy(void *, const void *, size_t);
330 void *kcsan_memmove(void *, const void *, size_t);
331 int kcsan_memcmp(const void *, const void *, size_t);
332 #define bcopy(from, to, len) kcsan_memmove((to), (from), (len))
333 #define bzero(buf, len) kcsan_memset((buf), 0, (len))
334 #define bcmp(b1, b2, len) kcsan_memcmp((b1), (b2), (len))
335 #define memset(buf, c, len) kcsan_memset((buf), (c), (len))
336 #define memcpy(to, from, len) kcsan_memcpy((to), (from), (len))
337 #define memmove(dest, src, n) kcsan_memmove((dest), (src), (n))
338 #define memcmp(b1, b2, len) kcsan_memcmp((b1), (b2), (len))
340 #define bcopy(from, to, len) __builtin_memmove((to), (from), (len))
341 #define bzero(buf, len) __builtin_memset((buf), 0, (len))
342 #define bcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len))
343 #define memset(buf, c, len) __builtin_memset((buf), (c), (len))
344 #define memcpy(to, from, len) __builtin_memcpy((to), (from), (len))
345 #define memmove(dest, src, n) __builtin_memmove((dest), (src), (n))
346 #define memcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len))
349 void *memset_early(void * _Nonnull buf, int c, size_t len);
350 #define bzero_early(buf, len) memset_early((buf), 0, (len))
351 void *memcpy_early(void * _Nonnull to, const void * _Nonnull from, size_t len);
352 void *memmove_early(void * _Nonnull dest, const void * _Nonnull src, size_t n);
353 #define bcopy_early(from, to, len) memmove_early((to), (from), (len))
355 int copystr(const void * _Nonnull __restrict kfaddr,
356 void * _Nonnull __restrict kdaddr, size_t len,
357 size_t * __restrict lencopied);
358 int copyinstr(const void * __restrict udaddr,
359 void * _Nonnull __restrict kaddr, size_t len,
360 size_t * __restrict lencopied);
361 int copyin(const void * __restrict udaddr,
362 void * _Nonnull __restrict kaddr, size_t len);
363 int copyin_nofault(const void * __restrict udaddr,
364 void * _Nonnull __restrict kaddr, size_t len);
365 int copyout(const void * _Nonnull __restrict kaddr,
366 void * __restrict udaddr, size_t len);
367 int copyout_nofault(const void * _Nonnull __restrict kaddr,
368 void * __restrict udaddr, size_t len);
371 int kcsan_copystr(const void *, void *, size_t, size_t *);
372 int kcsan_copyin(const void *, void *, size_t);
373 int kcsan_copyinstr(const void *, void *, size_t, size_t *);
374 int kcsan_copyout(const void *, void *, size_t);
375 #define copystr(kf, k, l, lc) kcsan_copystr((kf), (k), (l), (lc))
376 #define copyin(u, k, l) kcsan_copyin((u), (k), (l))
377 #define copyinstr(u, k, l, lc) kcsan_copyinstr((u), (k), (l), (lc))
378 #define copyout(k, u, l) kcsan_copyout((k), (u), (l))
381 int fubyte(volatile const void *base);
382 long fuword(volatile const void *base);
383 int fuword16(volatile const void *base);
384 int32_t fuword32(volatile const void *base);
385 int64_t fuword64(volatile const void *base);
386 int fueword(volatile const void *base, long *val);
387 int fueword32(volatile const void *base, int32_t *val);
388 int fueword64(volatile const void *base, int64_t *val);
389 int subyte(volatile void *base, int byte);
390 int suword(volatile void *base, long word);
391 int suword16(volatile void *base, int word);
392 int suword32(volatile void *base, int32_t word);
393 int suword64(volatile void *base, int64_t word);
394 uint32_t casuword32(volatile uint32_t *base, uint32_t oldval, uint32_t newval);
395 u_long casuword(volatile u_long *p, u_long oldval, u_long newval);
396 int casueword32(volatile uint32_t *base, uint32_t oldval, uint32_t *oldvalp,
398 int casueword(volatile u_long *p, u_long oldval, u_long *oldvalp,
401 void realitexpire(void *);
403 int sysbeep(int hertz, int period);
405 void hardclock(int cnt, int usermode);
406 void hardclock_sync(int cpu);
407 void softclock(void *);
408 void statclock(int cnt, int usermode);
409 void profclock(int cnt, int usermode, uintfptr_t pc);
411 int hardclockintr(void);
413 void startprofclock(struct proc *);
414 void stopprofclock(struct proc *);
415 void cpu_startprofclock(void);
416 void cpu_stopprofclock(void);
417 void suspendclock(void);
418 void resumeclock(void);
419 sbintime_t cpu_idleclock(void);
420 void cpu_activeclock(void);
421 void cpu_new_callout(int cpu, sbintime_t bt, sbintime_t bt_opt);
422 void cpu_et_frequency(struct eventtimer *et, uint64_t newfreq);
423 extern int cpu_disable_c2_sleep;
424 extern int cpu_disable_c3_sleep;
426 char *kern_getenv(const char *name);
427 void freeenv(char *env);
428 int getenv_int(const char *name, int *data);
429 int getenv_uint(const char *name, unsigned int *data);
430 int getenv_long(const char *name, long *data);
431 int getenv_ulong(const char *name, unsigned long *data);
432 int getenv_string(const char *name, char *data, int size);
433 int getenv_int64(const char *name, int64_t *data);
434 int getenv_uint64(const char *name, uint64_t *data);
435 int getenv_quad(const char *name, quad_t *data);
436 int kern_setenv(const char *name, const char *value);
437 int kern_unsetenv(const char *name);
438 int testenv(const char *name);
440 int getenv_array(const char *name, void *data, int size, int *psize,
441 int type_size, bool allow_signed);
442 #define GETENV_UNSIGNED false /* negative numbers not allowed */
443 #define GETENV_SIGNED true /* negative numbers allowed */
445 typedef uint64_t (cpu_tick_f)(void);
446 void set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var);
447 extern cpu_tick_f *cpu_ticks;
448 uint64_t cpu_tickrate(void);
449 uint64_t cputick2usec(uint64_t tick);
451 #ifdef APM_FIXUP_CALLTODO
453 void adjust_timeout_calltodo(struct timeval *time_change);
454 #endif /* APM_FIXUP_CALLTODO */
456 #include <sys/libkern.h>
458 /* Initialize the world */
460 void cpu_initclocks(void);
461 void cpu_initclocks_bsp(void);
462 void cpu_initclocks_ap(void);
463 void usrinfoinit(void);
465 /* Finalize the world */
466 void kern_reboot(int) __dead2;
467 void shutdown_nice(int);
469 /* Stubs for obsolete functions that used to be for interrupt management */
470 static __inline intrmask_t splbio(void) { return 0; }
471 static __inline intrmask_t splcam(void) { return 0; }
472 static __inline intrmask_t splclock(void) { return 0; }
473 static __inline intrmask_t splhigh(void) { return 0; }
474 static __inline intrmask_t splimp(void) { return 0; }
475 static __inline intrmask_t splnet(void) { return 0; }
476 static __inline intrmask_t spltty(void) { return 0; }
477 static __inline void splx(intrmask_t ipl __unused) { return; }
480 * Common `proc' functions are declared here so that proc.h can be included
483 int _sleep(void * _Nonnull chan, struct lock_object *lock, int pri,
484 const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags);
485 #define msleep(chan, mtx, pri, wmesg, timo) \
486 _sleep((chan), &(mtx)->lock_object, (pri), (wmesg), \
487 tick_sbt * (timo), 0, C_HARDCLOCK)
488 #define msleep_sbt(chan, mtx, pri, wmesg, bt, pr, flags) \
489 _sleep((chan), &(mtx)->lock_object, (pri), (wmesg), (bt), (pr), \
491 int msleep_spin_sbt(void * _Nonnull chan, struct mtx *mtx,
492 const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags);
493 #define msleep_spin(chan, mtx, wmesg, timo) \
494 msleep_spin_sbt((chan), (mtx), (wmesg), tick_sbt * (timo), \
496 int pause_sbt(const char *wmesg, sbintime_t sbt, sbintime_t pr,
498 #define pause(wmesg, timo) \
499 pause_sbt((wmesg), tick_sbt * (timo), 0, C_HARDCLOCK)
500 #define pause_sig(wmesg, timo) \
501 pause_sbt((wmesg), tick_sbt * (timo), 0, C_HARDCLOCK | C_CATCH)
502 #define tsleep(chan, pri, wmesg, timo) \
503 _sleep((chan), NULL, (pri), (wmesg), tick_sbt * (timo), \
505 #define tsleep_sbt(chan, pri, wmesg, bt, pr, flags) \
506 _sleep((chan), NULL, (pri), (wmesg), (bt), (pr), (flags))
507 void wakeup(void * chan);
508 void wakeup_one(void * chan);
509 void wakeup_any(void * chan);
512 * Common `struct cdev *' stuff are declared here to avoid #include poisoning
516 dev_t dev2udev(struct cdev *x);
517 const char *devtoname(struct cdev *cdev);
520 size_t devfs_iosize_max(void);
521 size_t iosize_max(void);
524 int poll_no_poll(int events);
526 /* XXX: Should be void nanodelay(u_int nsec); */
527 void DELAY(int usec);
529 /* Root mount holdback API */
530 struct root_hold_token {
533 TAILQ_ENTRY(root_hold_token) list;
536 struct root_hold_token *root_mount_hold(const char *identifier);
537 void root_mount_hold_token(const char *identifier, struct root_hold_token *h);
538 void root_mount_rel(struct root_hold_token *h);
539 int root_mounted(void);
543 * Unit number allocation API. (kern/subr_unit.c)
546 struct unrhdr *new_unrhdr(int low, int high, struct mtx *mutex);
547 void init_unrhdr(struct unrhdr *uh, int low, int high, struct mtx *mutex);
548 void delete_unrhdr(struct unrhdr *uh);
549 void clear_unrhdr(struct unrhdr *uh);
550 void clean_unrhdr(struct unrhdr *uh);
551 void clean_unrhdrl(struct unrhdr *uh);
552 int alloc_unr(struct unrhdr *uh);
553 int alloc_unr_specific(struct unrhdr *uh, u_int item);
554 int alloc_unrl(struct unrhdr *uh);
555 void free_unr(struct unrhdr *uh, u_int item);
566 new_unrhdr64(struct unrhdr64 *unr64, uint64_t low)
569 unr64->counter = low;
573 uint64_t alloc_unr64(struct unrhdr64 *);
575 static __inline uint64_t
576 alloc_unr64(struct unrhdr64 *unr64)
579 return (atomic_fetchadd_64(&unr64->counter, 1));
583 void intr_prof_stack_use(struct thread *td, struct trapframe *frame);
585 void counted_warning(unsigned *counter, const char *msg);
588 * APIs to manage deprecation and obsolescence.
591 void _gone_in(int major, const char *msg);
592 void _gone_in_dev(struct device *dev, int major, const char *msg);
593 #ifdef NO_OBSOLETE_CODE
594 #define __gone_ok(m, msg) \
595 _Static_assert(m < P_OSREL_MAJOR(__FreeBSD_version)), \
596 "Obsolete code" msg);
598 #define __gone_ok(m, msg)
600 #define gone_in(major, msg) __gone_ok(major, msg) _gone_in(major, msg)
601 #define gone_in_dev(dev, major, msg) __gone_ok(major, msg) _gone_in_dev(dev, major, msg)
603 __NULLABILITY_PRAGMA_POP
605 #endif /* !_SYS_SYSTM_H_ */