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_LAST };
83 #if defined(WITNESS) || defined(INVARIANT_SUPPORT)
84 void kassert_panic(const char *fmt, ...) __printflike(1, 2);
87 #ifdef INVARIANTS /* The option is always available */
88 #define KASSERT(exp,msg) do { \
89 if (__predict_false(!(exp))) \
92 #define VNASSERT(exp, vp, msg) do { \
93 if (__predict_false(!(exp))) { \
94 vn_printf(vp, "VNASSERT failed\n"); \
99 #define KASSERT(exp,msg) do { \
102 #define VNASSERT(exp, vp, msg) do { \
106 #ifndef CTASSERT /* Allow lint to override */
107 #define CTASSERT(x) _Static_assert(x, "compile-time assertion failed")
111 * Assert that a pointer can be loaded from memory atomically.
113 * This assertion enforces stronger alignment than necessary. For example,
114 * on some architectures, atomicity for unaligned loads will depend on
115 * whether or not the load spans multiple cache lines.
117 #define ASSERT_ATOMIC_LOAD_PTR(var, msg) \
118 KASSERT(sizeof(var) == sizeof(void *) && \
119 ((uintptr_t)&(var) & (sizeof(void *) - 1)) == 0, msg)
122 * Assert that a thread is in critical(9) section.
124 #define CRITICAL_ASSERT(td) \
125 KASSERT((td)->td_critnest >= 1, ("Not in critical section"));
128 * If we have already panic'd and this is the thread that called
129 * panic(), then don't block on any mutexes but silently succeed.
130 * Otherwise, the kernel will deadlock since the scheduler isn't
131 * going to run the thread that holds any lock we need.
133 #define SCHEDULER_STOPPED_TD(td) ({ \
134 MPASS((td) == curthread); \
135 __predict_false((td)->td_stopsched); \
137 #define SCHEDULER_STOPPED() SCHEDULER_STOPPED_TD(curthread)
142 #define __read_mostly __section(".data.read_mostly")
143 #define __read_frequently __section(".data.read_frequently")
144 #define __exclusive_cache_line __aligned(CACHE_LINE_SIZE) \
145 __section(".data.exclusive_cache_line")
147 * XXX the hints declarations are even more misplaced than most declarations
148 * in this file, since they are needed in one file (per arch) and only used
150 * XXX most of these variables should be const.
152 extern int osreldate;
154 extern int hintmode; /* 0 = off. 1 = config, 2 = fallback */
155 extern int dynamic_kenv;
156 extern struct mtx kenv_lock;
157 extern char *kern_envp;
158 extern char static_env[];
159 extern char static_hints[]; /* by config for now */
163 extern const void *zero_region; /* address space maps to a zeroed page */
165 extern int unmapped_buf_allowed;
168 #define IOSIZE_MAX iosize_max()
169 #define DEVFS_IOSIZE_MAX devfs_iosize_max()
171 #define IOSIZE_MAX SSIZE_MAX
172 #define DEVFS_IOSIZE_MAX SSIZE_MAX
176 * General function declarations.
193 int setjmp(struct _jmp_buf *) __returns_twice;
194 void longjmp(struct _jmp_buf *, int) __dead2;
195 int dumpstatus(vm_offset_t addr, off_t count);
197 int eopnotsupp(void);
198 int ureadc(int, struct uio *);
199 void hashdestroy(void *, struct malloc_type *, u_long);
200 void *hashinit(int count, struct malloc_type *type, u_long *hashmask);
201 void *hashinit_flags(int count, struct malloc_type *type,
202 u_long *hashmask, int flags);
203 #define HASH_NOWAIT 0x00000001
204 #define HASH_WAITOK 0x00000002
206 void *phashinit(int count, struct malloc_type *type, u_long *nentries);
207 void *phashinit_flags(int count, struct malloc_type *type, u_long *nentries,
209 void g_waitidle(void);
211 void panic(const char *, ...) __dead2 __printflike(1, 2);
212 void vpanic(const char *, __va_list) __dead2 __printflike(1, 0);
215 void cpu_flush_dcache(void *, size_t);
216 void cpu_rootconf(void);
217 void critical_enter(void);
218 void critical_exit(void);
219 void init_param1(void);
220 void init_param2(long physpages);
221 void init_static_kenv(char *, size_t);
222 void tablefull(const char *);
224 typedef void early_putc_t(int ch);
225 extern early_putc_t *early_putc;
227 int kvprintf(char const *, void (*)(int, void*), void *, int,
228 __va_list) __printflike(1, 0);
229 void log(int, const char *, ...) __printflike(2, 3);
230 void log_console(struct uio *);
231 void vlog(int, const char *, __va_list) __printflike(2, 0);
232 int asprintf(char **ret, struct malloc_type *mtp, const char *format,
233 ...) __printflike(3, 4);
234 int printf(const char *, ...) __printflike(1, 2);
235 int snprintf(char *, size_t, const char *, ...) __printflike(3, 4);
236 int sprintf(char *buf, const char *, ...) __printflike(2, 3);
237 int uprintf(const char *, ...) __printflike(1, 2);
238 int vprintf(const char *, __va_list) __printflike(1, 0);
239 int vasprintf(char **ret, struct malloc_type *mtp, const char *format,
240 __va_list ap) __printflike(3, 0);
241 int vsnprintf(char *, size_t, const char *, __va_list) __printflike(3, 0);
242 int vsnrprintf(char *, size_t, int, const char *, __va_list) __printflike(4, 0);
243 int vsprintf(char *buf, const char *, __va_list) __printflike(2, 0);
244 int ttyprintf(struct tty *, const char *, ...) __printflike(2, 3);
245 int sscanf(const char *, char const * _Nonnull, ...) __scanflike(2, 3);
246 int vsscanf(const char * _Nonnull, char const * _Nonnull, __va_list) __scanflike(2, 0);
247 long strtol(const char *, char **, int);
248 u_long strtoul(const char *, char **, int);
249 quad_t strtoq(const char *, char **, int);
250 u_quad_t strtouq(const char *, char **, int);
251 void tprintf(struct proc *p, int pri, const char *, ...) __printflike(3, 4);
252 void vtprintf(struct proc *, int, const char *, __va_list) __printflike(3, 0);
253 void hexdump(const void *ptr, int length, const char *hdr, int flags);
254 #define HD_COLUMN_MASK 0xff
255 #define HD_DELIM_MASK 0xff00
256 #define HD_OMIT_COUNT (1 << 16)
257 #define HD_OMIT_HEX (1 << 17)
258 #define HD_OMIT_CHARS (1 << 18)
260 #define ovbcopy(f, t, l) bcopy((f), (t), (l))
261 void bcopy(const void * _Nonnull from, void * _Nonnull to, size_t len);
262 #define bcopy(from, to, len) __builtin_memmove((to), (from), (len))
263 void bzero(void * _Nonnull buf, size_t len);
264 #define bzero(buf, len) __builtin_memset((buf), 0, (len))
265 void explicit_bzero(void * _Nonnull, size_t);
266 int bcmp(const void *b1, const void *b2, size_t len);
267 #define bcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len))
269 void *memset(void * _Nonnull buf, int c, size_t len);
270 #define memset(buf, c, len) __builtin_memset((buf), (c), (len))
271 void *memcpy(void * _Nonnull to, const void * _Nonnull from, size_t len);
272 #define memcpy(to, from, len) __builtin_memcpy((to), (from), (len))
273 void *memmove(void * _Nonnull dest, const void * _Nonnull src, size_t n);
274 #define memmove(dest, src, n) __builtin_memmove((dest), (src), (n))
275 int memcmp(const void *b1, const void *b2, size_t len);
276 #define memcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len))
278 int copystr(const void * _Nonnull __restrict kfaddr,
279 void * _Nonnull __restrict kdaddr, size_t len,
280 size_t * __restrict lencopied);
281 int copyinstr(const void * __restrict udaddr,
282 void * _Nonnull __restrict kaddr, size_t len,
283 size_t * __restrict lencopied);
284 int copyin(const void * __restrict udaddr,
285 void * _Nonnull __restrict kaddr, size_t len);
286 int copyin_nofault(const void * __restrict udaddr,
287 void * _Nonnull __restrict kaddr, size_t len);
288 int copyout(const void * _Nonnull __restrict kaddr,
289 void * __restrict udaddr, size_t len);
290 int copyout_nofault(const void * _Nonnull __restrict kaddr,
291 void * __restrict udaddr, size_t len);
293 int fubyte(volatile const void *base);
294 long fuword(volatile const void *base);
295 int fuword16(volatile const void *base);
296 int32_t fuword32(volatile const void *base);
297 int64_t fuword64(volatile const void *base);
298 int fueword(volatile const void *base, long *val);
299 int fueword32(volatile const void *base, int32_t *val);
300 int fueword64(volatile const void *base, int64_t *val);
301 int subyte(volatile void *base, int byte);
302 int suword(volatile void *base, long word);
303 int suword16(volatile void *base, int word);
304 int suword32(volatile void *base, int32_t word);
305 int suword64(volatile void *base, int64_t word);
306 uint32_t casuword32(volatile uint32_t *base, uint32_t oldval, uint32_t newval);
307 u_long casuword(volatile u_long *p, u_long oldval, u_long newval);
308 int casueword32(volatile uint32_t *base, uint32_t oldval, uint32_t *oldvalp,
310 int casueword(volatile u_long *p, u_long oldval, u_long *oldvalp,
313 void realitexpire(void *);
315 int sysbeep(int hertz, int period);
317 void hardclock(int usermode, uintfptr_t pc);
318 void hardclock_cnt(int cnt, int usermode);
319 void hardclock_cpu(int usermode);
320 void hardclock_sync(int cpu);
321 void softclock(void *);
322 void statclock(int usermode);
323 void statclock_cnt(int cnt, int usermode);
324 void profclock(int usermode, uintfptr_t pc);
325 void profclock_cnt(int cnt, int usermode, uintfptr_t pc);
327 int hardclockintr(void);
329 void startprofclock(struct proc *);
330 void stopprofclock(struct proc *);
331 void cpu_startprofclock(void);
332 void cpu_stopprofclock(void);
333 void suspendclock(void);
334 void resumeclock(void);
335 sbintime_t cpu_idleclock(void);
336 void cpu_activeclock(void);
337 void cpu_new_callout(int cpu, sbintime_t bt, sbintime_t bt_opt);
338 void cpu_et_frequency(struct eventtimer *et, uint64_t newfreq);
339 extern int cpu_disable_c2_sleep;
340 extern int cpu_disable_c3_sleep;
342 char *kern_getenv(const char *name);
343 void freeenv(char *env);
344 int getenv_int(const char *name, int *data);
345 int getenv_uint(const char *name, unsigned int *data);
346 int getenv_long(const char *name, long *data);
347 int getenv_ulong(const char *name, unsigned long *data);
348 int getenv_string(const char *name, char *data, int size);
349 int getenv_int64(const char *name, int64_t *data);
350 int getenv_uint64(const char *name, uint64_t *data);
351 int getenv_quad(const char *name, quad_t *data);
352 int kern_setenv(const char *name, const char *value);
353 int kern_unsetenv(const char *name);
354 int testenv(const char *name);
356 int getenv_array(const char *name, void *data, int size, int *psize,
357 int type_size, bool allow_signed);
358 #define GETENV_UNSIGNED false /* negative numbers not allowed */
359 #define GETENV_SIGNED true /* negative numbers allowed */
361 typedef uint64_t (cpu_tick_f)(void);
362 void set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var);
363 extern cpu_tick_f *cpu_ticks;
364 uint64_t cpu_tickrate(void);
365 uint64_t cputick2usec(uint64_t tick);
367 #ifdef APM_FIXUP_CALLTODO
369 void adjust_timeout_calltodo(struct timeval *time_change);
370 #endif /* APM_FIXUP_CALLTODO */
372 #include <sys/libkern.h>
374 /* Initialize the world */
376 void cpu_initclocks(void);
377 void cpu_initclocks_bsp(void);
378 void cpu_initclocks_ap(void);
379 void usrinfoinit(void);
381 /* Finalize the world */
382 void kern_reboot(int) __dead2;
383 void shutdown_nice(int);
386 typedef void timeout_t(void *); /* timeout function type */
387 #define CALLOUT_HANDLE_INITIALIZER(handle) \
390 void callout_handle_init(struct callout_handle *);
391 struct callout_handle timeout(timeout_t *, void *, int);
392 void untimeout(timeout_t *, void *, struct callout_handle);
394 /* Stubs for obsolete functions that used to be for interrupt management */
395 static __inline intrmask_t splbio(void) { return 0; }
396 static __inline intrmask_t splcam(void) { return 0; }
397 static __inline intrmask_t splclock(void) { return 0; }
398 static __inline intrmask_t splhigh(void) { return 0; }
399 static __inline intrmask_t splimp(void) { return 0; }
400 static __inline intrmask_t splnet(void) { return 0; }
401 static __inline intrmask_t spltty(void) { return 0; }
402 static __inline void splx(intrmask_t ipl __unused) { return; }
405 * Common `proc' functions are declared here so that proc.h can be included
408 int _sleep(void * _Nonnull chan, struct lock_object *lock, int pri,
409 const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags);
410 #define msleep(chan, mtx, pri, wmesg, timo) \
411 _sleep((chan), &(mtx)->lock_object, (pri), (wmesg), \
412 tick_sbt * (timo), 0, C_HARDCLOCK)
413 #define msleep_sbt(chan, mtx, pri, wmesg, bt, pr, flags) \
414 _sleep((chan), &(mtx)->lock_object, (pri), (wmesg), (bt), (pr), \
416 int msleep_spin_sbt(void * _Nonnull chan, struct mtx *mtx,
417 const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags);
418 #define msleep_spin(chan, mtx, wmesg, timo) \
419 msleep_spin_sbt((chan), (mtx), (wmesg), tick_sbt * (timo), \
421 int pause_sbt(const char *wmesg, sbintime_t sbt, sbintime_t pr,
423 #define pause(wmesg, timo) \
424 pause_sbt((wmesg), tick_sbt * (timo), 0, C_HARDCLOCK)
425 #define pause_sig(wmesg, timo) \
426 pause_sbt((wmesg), tick_sbt * (timo), 0, C_HARDCLOCK | C_CATCH)
427 #define tsleep(chan, pri, wmesg, timo) \
428 _sleep((chan), NULL, (pri), (wmesg), tick_sbt * (timo), \
430 #define tsleep_sbt(chan, pri, wmesg, bt, pr, flags) \
431 _sleep((chan), NULL, (pri), (wmesg), (bt), (pr), (flags))
432 void wakeup(void * chan);
433 void wakeup_one(void * chan);
436 * Common `struct cdev *' stuff are declared here to avoid #include poisoning
440 dev_t dev2udev(struct cdev *x);
441 const char *devtoname(struct cdev *cdev);
444 size_t devfs_iosize_max(void);
445 size_t iosize_max(void);
448 int poll_no_poll(int events);
450 /* XXX: Should be void nanodelay(u_int nsec); */
451 void DELAY(int usec);
453 /* Root mount holdback API */
454 struct root_hold_token;
456 struct root_hold_token *root_mount_hold(const char *identifier);
457 void root_mount_rel(struct root_hold_token *h);
458 int root_mounted(void);
462 * Unit number allocation API. (kern/subr_unit.c)
465 struct unrhdr *new_unrhdr(int low, int high, struct mtx *mutex);
466 void init_unrhdr(struct unrhdr *uh, int low, int high, struct mtx *mutex);
467 void delete_unrhdr(struct unrhdr *uh);
468 void clear_unrhdr(struct unrhdr *uh);
469 void clean_unrhdr(struct unrhdr *uh);
470 void clean_unrhdrl(struct unrhdr *uh);
471 int alloc_unr(struct unrhdr *uh);
472 int alloc_unr_specific(struct unrhdr *uh, u_int item);
473 int alloc_unrl(struct unrhdr *uh);
474 void free_unr(struct unrhdr *uh, u_int item);
476 void intr_prof_stack_use(struct thread *td, struct trapframe *frame);
478 void counted_warning(unsigned *counter, const char *msg);
481 * APIs to manage deprecation and obsolescence.
484 void _gone_in(int major, const char *msg);
485 void _gone_in_dev(struct device *dev, int major, const char *msg);
486 #ifdef NO_OBSOLETE_CODE
487 #define __gone_ok(m, msg) \
488 _Static_assert(m < P_OSREL_MAJOR(__FreeBSD_version)), \
489 "Obsolete code" msg);
491 #define __gone_ok(m, msg)
493 #define gone_in(major, msg) __gone_ok(major, msg) _gone_in(major, msg)
494 #define gone_in_dev(dev, major, msg) __gone_ok(major, msg) _gone_in_dev(dev, major, msg)
496 __NULLABILITY_PRAGMA_POP
498 #endif /* !_SYS_SYSTM_H_ */