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/kassert.h>
48 #include <sys/queue.h>
49 #include <sys/stdint.h> /* for people using printf mainly */
51 __NULLABILITY_PRAGMA_PUSH
54 extern int cold; /* nonzero if we are doing a cold boot */
55 extern int suspend_blocked; /* block suspend due to pending shutdown */
56 extern int rebooting; /* kern_reboot() has been called. */
57 extern char version[]; /* system version */
58 extern char compiler_version[]; /* compiler version */
59 extern char copyright[]; /* system copyright */
60 extern int kstack_pages; /* number of kernel stack pages */
62 extern u_long pagesizes[]; /* supported page sizes */
63 extern long physmem; /* physical memory */
64 extern long realmem; /* 'real' memory */
66 extern char *rootdevnames[2]; /* names of possible root devices */
68 extern int boothowto; /* reboot flags, from console subsystem */
69 extern int bootverbose; /* nonzero to print verbose messages */
71 extern int maxusers; /* system tune hint */
72 extern int ngroups_max; /* max # of supplemental groups */
73 extern int vm_guest; /* Running as virtual machine guest? */
75 extern u_long maxphys; /* max raw I/O transfer size */
78 * Detected virtual machine guest types. The intention is to expand
79 * and/or add to the VM_GUEST_VM type if specific VM functionality is
80 * ever implemented (e.g. vendor-specific paravirtualization features).
81 * Keep in sync with vm_guest_sysctl_names[].
83 enum VM_GUEST { VM_GUEST_NO = 0, VM_GUEST_VM, VM_GUEST_XEN, VM_GUEST_HV,
84 VM_GUEST_VMWARE, VM_GUEST_KVM, VM_GUEST_BHYVE, VM_GUEST_VBOX,
85 VM_GUEST_PARALLELS, VM_LAST };
92 #define __read_mostly __section(".data.read_mostly")
93 #define __read_frequently __section(".data.read_frequently")
94 #define __exclusive_cache_line __aligned(CACHE_LINE_SIZE) \
95 __section(".data.exclusive_cache_line")
96 #if defined(_STANDALONE)
101 #include <sys/param.h> /* MAXCPU */
102 #include <sys/pcpu.h> /* curthread */
103 #include <sys/kpilite.h>
105 extern int osreldate;
107 extern const void *zero_region; /* address space maps to a zeroed page */
109 extern int unmapped_buf_allowed;
112 #define IOSIZE_MAX iosize_max()
113 #define DEVFS_IOSIZE_MAX devfs_iosize_max()
115 #define IOSIZE_MAX SSIZE_MAX
116 #define DEVFS_IOSIZE_MAX SSIZE_MAX
120 * General function declarations.
137 int setjmp(struct _jmp_buf *) __returns_twice;
138 void longjmp(struct _jmp_buf *, int) __dead2;
139 int dumpstatus(vm_offset_t addr, off_t count);
141 int eopnotsupp(void);
142 int ureadc(int, struct uio *);
143 void hashdestroy(void *, struct malloc_type *, u_long);
144 void *hashinit(int count, struct malloc_type *type, u_long *hashmask);
145 void *hashinit_flags(int count, struct malloc_type *type,
146 u_long *hashmask, int flags);
147 #define HASH_NOWAIT 0x00000001
148 #define HASH_WAITOK 0x00000002
150 void *phashinit(int count, struct malloc_type *type, u_long *nentries);
151 void *phashinit_flags(int count, struct malloc_type *type, u_long *nentries,
153 void g_waitidle(void);
155 void cpu_flush_dcache(void *, size_t);
156 void cpu_rootconf(void);
157 void critical_enter_KBI(void);
158 void critical_exit_KBI(void);
159 void critical_exit_preempt(void);
160 void init_param1(void);
161 void init_param2(long physpages);
162 void init_static_kenv(char *, size_t);
163 void tablefull(const char *);
166 * Allocate per-thread "current" state in the linuxkpi
168 extern int (*lkpi_alloc_current)(struct thread *, int);
169 int linux_alloc_current_noop(struct thread *, int);
171 #if defined(KLD_MODULE) || defined(KTR_CRITICAL) || !defined(_KERNEL) || defined(GENOFFSET)
172 #define critical_enter() critical_enter_KBI()
173 #define critical_exit() critical_exit_KBI()
178 struct thread_lite *td;
180 td = (struct thread_lite *)curthread;
182 atomic_interrupt_fence();
188 struct thread_lite *td;
190 td = (struct thread_lite *)curthread;
191 KASSERT(td->td_critnest != 0,
192 ("critical_exit: td_critnest == 0"));
193 atomic_interrupt_fence();
195 atomic_interrupt_fence();
196 if (__predict_false(td->td_owepreempt))
197 critical_exit_preempt();
203 typedef void early_putc_t(int ch);
204 extern early_putc_t *early_putc;
206 int kvprintf(char const *, void (*)(int, void*), void *, int,
207 __va_list) __printflike(1, 0);
208 void log(int, const char *, ...) __printflike(2, 3);
209 void log_console(struct uio *);
210 void vlog(int, const char *, __va_list) __printflike(2, 0);
211 int asprintf(char **ret, struct malloc_type *mtp, const char *format,
212 ...) __printflike(3, 4);
213 int printf(const char *, ...) __printflike(1, 2);
214 int snprintf(char *, size_t, const char *, ...) __printflike(3, 4);
215 int sprintf(char *buf, const char *, ...) __printflike(2, 3);
216 int uprintf(const char *, ...) __printflike(1, 2);
217 int vprintf(const char *, __va_list) __printflike(1, 0);
218 int vasprintf(char **ret, struct malloc_type *mtp, const char *format,
219 __va_list ap) __printflike(3, 0);
220 int vsnprintf(char *, size_t, const char *, __va_list) __printflike(3, 0);
221 int vsnrprintf(char *, size_t, int, const char *, __va_list) __printflike(4, 0);
222 int vsprintf(char *buf, const char *, __va_list) __printflike(2, 0);
223 int sscanf(const char *, char const * _Nonnull, ...) __scanflike(2, 3);
224 int vsscanf(const char * _Nonnull, char const * _Nonnull, __va_list) __scanflike(2, 0);
225 long strtol(const char *, char **, int);
226 u_long strtoul(const char *, char **, int);
227 quad_t strtoq(const char *, char **, int);
228 u_quad_t strtouq(const char *, char **, int);
229 void tprintf(struct proc *p, int pri, const char *, ...) __printflike(3, 4);
230 void vtprintf(struct proc *, int, const char *, __va_list) __printflike(3, 0);
231 void hexdump(const void *ptr, int length, const char *hdr, int flags);
232 #define HD_COLUMN_MASK 0xff
233 #define HD_DELIM_MASK 0xff00
234 #define HD_OMIT_COUNT (1 << 16)
235 #define HD_OMIT_HEX (1 << 17)
236 #define HD_OMIT_CHARS (1 << 18)
238 #define ovbcopy(f, t, l) bcopy((f), (t), (l))
239 void bcopy(const void * _Nonnull from, void * _Nonnull to, size_t len);
240 void bzero(void * _Nonnull buf, size_t len);
241 void explicit_bzero(void * _Nonnull, size_t);
242 int bcmp(const void *b1, const void *b2, size_t len);
244 void *memset(void * _Nonnull buf, int c, size_t len);
245 void *memcpy(void * _Nonnull to, const void * _Nonnull from, size_t len);
246 void *memmove(void * _Nonnull dest, const void * _Nonnull src, size_t n);
247 int memcmp(const void *b1, const void *b2, size_t len);
249 #ifdef SAN_NEEDS_INTERCEPTORS
250 #define SAN_INTERCEPTOR(func) \
251 __CONCAT(SAN_INTERCEPTOR_PREFIX, __CONCAT(_, func))
252 void *SAN_INTERCEPTOR(memset)(void *, int, size_t);
253 void *SAN_INTERCEPTOR(memcpy)(void *, const void *, size_t);
254 void *SAN_INTERCEPTOR(memmove)(void *, const void *, size_t);
255 int SAN_INTERCEPTOR(memcmp)(const void *, const void *, size_t);
257 #define bcopy(from, to, len) SAN_INTERCEPTOR(memmove)((to), (from), (len))
258 #define bzero(buf, len) SAN_INTERCEPTOR(memset)((buf), 0, (len))
259 #define bcmp(b1, b2, len) SAN_INTERCEPTOR(memcmp)((b1), (b2), (len))
260 #define memset(buf, c, len) SAN_INTERCEPTOR(memset)((buf), (c), (len))
261 #define memcpy(to, from, len) SAN_INTERCEPTOR(memcpy)((to), (from), (len))
262 #define memmove(dest, src, n) SAN_INTERCEPTOR(memmove)((dest), (src), (n))
263 #define memcmp(b1, b2, len) SAN_INTERCEPTOR(memcmp)((b1), (b2), (len))
264 #endif /* !SAN_RUNTIME */
265 #else /* !SAN_NEEDS_INTERCEPTORS */
266 #define bcopy(from, to, len) __builtin_memmove((to), (from), (len))
267 #define bzero(buf, len) __builtin_memset((buf), 0, (len))
268 #define bcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len))
269 #define memset(buf, c, len) __builtin_memset((buf), (c), (len))
270 #define memcpy(to, from, len) __builtin_memcpy((to), (from), (len))
271 #define memmove(dest, src, n) __builtin_memmove((dest), (src), (n))
272 #define memcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len))
273 #endif /* SAN_NEEDS_INTERCEPTORS */
275 void *memset_early(void * _Nonnull buf, int c, size_t len);
276 #define bzero_early(buf, len) memset_early((buf), 0, (len))
277 void *memcpy_early(void * _Nonnull to, const void * _Nonnull from, size_t len);
278 void *memmove_early(void * _Nonnull dest, const void * _Nonnull src, size_t n);
279 #define bcopy_early(from, to, len) memmove_early((to), (from), (len))
281 #define copystr(src, dst, len, outlen) ({ \
282 size_t __r, __len, *__outlen; \
285 __outlen = (outlen); \
286 __r = strlcpy((dst), (src), __len); \
287 if (__outlen != NULL) \
288 *__outlen = ((__r >= __len) ? __len : __r + 1); \
289 ((__r >= __len) ? ENAMETOOLONG : 0); \
292 int copyinstr(const void * __restrict udaddr,
293 void * _Nonnull __restrict kaddr, size_t len,
294 size_t * __restrict lencopied);
295 int copyin(const void * __restrict udaddr,
296 void * _Nonnull __restrict kaddr, size_t len);
297 int copyin_nofault(const void * __restrict udaddr,
298 void * _Nonnull __restrict kaddr, size_t len);
299 int copyout(const void * _Nonnull __restrict kaddr,
300 void * __restrict udaddr, size_t len);
301 int copyout_nofault(const void * _Nonnull __restrict kaddr,
302 void * __restrict udaddr, size_t len);
304 #ifdef SAN_NEEDS_INTERCEPTORS
305 int SAN_INTERCEPTOR(copyin)(const void *, void *, size_t);
306 int SAN_INTERCEPTOR(copyinstr)(const void *, void *, size_t, size_t *);
307 int SAN_INTERCEPTOR(copyout)(const void *, void *, size_t);
309 #define copyin(u, k, l) SAN_INTERCEPTOR(copyin)((u), (k), (l))
310 #define copyinstr(u, k, l, lc) SAN_INTERCEPTOR(copyinstr)((u), (k), (l), (lc))
311 #define copyout(k, u, l) SAN_INTERCEPTOR(copyout)((k), (u), (l))
312 #endif /* !SAN_RUNTIME */
313 #endif /* SAN_NEEDS_INTERCEPTORS */
315 int fubyte(volatile const void *base);
316 long fuword(volatile const void *base);
317 int fuword16(volatile const void *base);
318 int32_t fuword32(volatile const void *base);
319 int64_t fuword64(volatile const void *base);
320 int fueword(volatile const void *base, long *val);
321 int fueword32(volatile const void *base, int32_t *val);
322 int fueword64(volatile const void *base, int64_t *val);
323 int subyte(volatile void *base, int byte);
324 int suword(volatile void *base, long word);
325 int suword16(volatile void *base, int word);
326 int suword32(volatile void *base, int32_t word);
327 int suword64(volatile void *base, int64_t word);
328 uint32_t casuword32(volatile uint32_t *base, uint32_t oldval, uint32_t newval);
329 u_long casuword(volatile u_long *p, u_long oldval, u_long newval);
330 int casueword32(volatile uint32_t *base, uint32_t oldval, uint32_t *oldvalp,
332 int casueword(volatile u_long *p, u_long oldval, u_long *oldvalp,
335 int sysbeep(int hertz, sbintime_t duration);
337 void hardclock(int cnt, int usermode);
338 void hardclock_sync(int cpu);
339 void softclock(void *);
340 void statclock(int cnt, int usermode);
341 void profclock(int cnt, int usermode, uintfptr_t pc);
343 int hardclockintr(void);
345 void startprofclock(struct proc *);
346 void stopprofclock(struct proc *);
347 void cpu_startprofclock(void);
348 void cpu_stopprofclock(void);
349 void suspendclock(void);
350 void resumeclock(void);
351 sbintime_t cpu_idleclock(void);
352 void cpu_activeclock(void);
353 void cpu_new_callout(int cpu, sbintime_t bt, sbintime_t bt_opt);
354 void cpu_et_frequency(struct eventtimer *et, uint64_t newfreq);
355 extern int cpu_disable_c2_sleep;
356 extern int cpu_disable_c3_sleep;
358 char *kern_getenv(const char *name);
359 void freeenv(char *env);
360 int getenv_int(const char *name, int *data);
361 int getenv_uint(const char *name, unsigned int *data);
362 int getenv_long(const char *name, long *data);
363 int getenv_ulong(const char *name, unsigned long *data);
364 int getenv_string(const char *name, char *data, int size);
365 int getenv_int64(const char *name, int64_t *data);
366 int getenv_uint64(const char *name, uint64_t *data);
367 int getenv_quad(const char *name, quad_t *data);
368 int getenv_bool(const char *name, bool *data);
369 bool getenv_is_true(const char *name);
370 bool getenv_is_false(const char *name);
371 int kern_setenv(const char *name, const char *value);
372 int kern_unsetenv(const char *name);
373 int testenv(const char *name);
375 int getenv_array(const char *name, void *data, int size, int *psize,
376 int type_size, bool allow_signed);
377 #define GETENV_UNSIGNED false /* negative numbers not allowed */
378 #define GETENV_SIGNED true /* negative numbers allowed */
380 typedef uint64_t (cpu_tick_f)(void);
381 void set_cputicker(cpu_tick_f *func, uint64_t freq, bool isvariable);
382 extern cpu_tick_f *cpu_ticks;
383 uint64_t cpu_tickrate(void);
384 uint64_t cputick2usec(uint64_t tick);
386 #include <sys/libkern.h>
388 /* Initialize the world */
390 void cpu_initclocks(void);
391 void cpu_initclocks_bsp(void);
392 void cpu_initclocks_ap(void);
393 void usrinfoinit(void);
395 /* Finalize the world */
396 void kern_reboot(int) __dead2;
397 void shutdown_nice(int);
399 /* Stubs for obsolete functions that used to be for interrupt management */
400 static __inline intrmask_t splhigh(void) { return 0; }
401 static __inline intrmask_t splimp(void) { return 0; }
402 static __inline intrmask_t splnet(void) { return 0; }
403 static __inline intrmask_t spltty(void) { return 0; }
404 static __inline void splx(intrmask_t ipl __unused) { return; }
407 * Common `proc' functions are declared here so that proc.h can be included
410 int _sleep(const void * _Nonnull chan, struct lock_object *lock, int pri,
411 const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags);
412 #define msleep(chan, mtx, pri, wmesg, timo) \
413 _sleep((chan), &(mtx)->lock_object, (pri), (wmesg), \
414 tick_sbt * (timo), 0, C_HARDCLOCK)
415 #define msleep_sbt(chan, mtx, pri, wmesg, bt, pr, flags) \
416 _sleep((chan), &(mtx)->lock_object, (pri), (wmesg), (bt), (pr), \
418 int msleep_spin_sbt(const void * _Nonnull chan, struct mtx *mtx,
419 const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags);
420 #define msleep_spin(chan, mtx, wmesg, timo) \
421 msleep_spin_sbt((chan), (mtx), (wmesg), tick_sbt * (timo), \
423 int pause_sbt(const char *wmesg, sbintime_t sbt, sbintime_t pr,
426 pause(const char *wmesg, int timo)
428 return (pause_sbt(wmesg, tick_sbt * timo, 0, C_HARDCLOCK));
430 #define pause_sig(wmesg, timo) \
431 pause_sbt((wmesg), tick_sbt * (timo), 0, C_HARDCLOCK | C_CATCH)
432 #define tsleep(chan, pri, wmesg, timo) \
433 _sleep((chan), NULL, (pri), (wmesg), tick_sbt * (timo), \
435 #define tsleep_sbt(chan, pri, wmesg, bt, pr, flags) \
436 _sleep((chan), NULL, (pri), (wmesg), (bt), (pr), (flags))
437 void wakeup(const void *chan);
438 void wakeup_one(const void *chan);
439 void wakeup_any(const void *chan);
442 * Common `struct cdev *' stuff are declared here to avoid #include poisoning
446 dev_t dev2udev(struct cdev *x);
447 const char *devtoname(struct cdev *cdev);
450 size_t devfs_iosize_max(void);
451 size_t iosize_max(void);
454 int poll_no_poll(int events);
456 /* XXX: Should be void nanodelay(u_int nsec); */
457 void DELAY(int usec);
459 /* Root mount holdback API */
460 struct root_hold_token {
463 TAILQ_ENTRY(root_hold_token) list;
466 struct root_hold_token *root_mount_hold(const char *identifier);
467 void root_mount_hold_token(const char *identifier, struct root_hold_token *h);
468 void root_mount_rel(struct root_hold_token *h);
469 int root_mounted(void);
472 * Unit number allocation API. (kern/subr_unit.c)
475 #define UNR_NO_MTX ((void *)(uintptr_t)-1)
476 struct unrhdr *new_unrhdr(int low, int high, struct mtx *mutex);
477 void init_unrhdr(struct unrhdr *uh, int low, int high, struct mtx *mutex);
478 void delete_unrhdr(struct unrhdr *uh);
479 void clear_unrhdr(struct unrhdr *uh);
480 void clean_unrhdr(struct unrhdr *uh);
481 void clean_unrhdrl(struct unrhdr *uh);
482 int alloc_unr(struct unrhdr *uh);
483 int alloc_unr_specific(struct unrhdr *uh, u_int item);
484 int alloc_unrl(struct unrhdr *uh);
485 void free_unr(struct unrhdr *uh, u_int item);
496 new_unrhdr64(struct unrhdr64 *unr64, uint64_t low)
499 unr64->counter = low;
503 uint64_t alloc_unr64(struct unrhdr64 *);
505 static __inline uint64_t
506 alloc_unr64(struct unrhdr64 *unr64)
509 return (atomic_fetchadd_64(&unr64->counter, 1));
513 void intr_prof_stack_use(struct thread *td, struct trapframe *frame);
515 void counted_warning(unsigned *counter, const char *msg);
518 * APIs to manage deprecation and obsolescence.
520 void _gone_in(int major, const char *msg);
521 void _gone_in_dev(device_t dev, int major, const char *msg);
522 #ifdef NO_OBSOLETE_CODE
523 #define __gone_ok(m, msg) \
524 _Static_assert(m < P_OSREL_MAJOR(__FreeBSD_version)), \
525 "Obsolete code: " msg);
527 #define __gone_ok(m, msg)
529 #define gone_in(major, msg) __gone_ok(major, msg) _gone_in(major, msg)
530 #define gone_in_dev(dev, major, msg) __gone_ok(major, msg) _gone_in_dev(dev, major, msg)
532 #if defined(INVARIANTS) || defined(WITNESS)
535 #define __diagused __unused
540 __NULLABILITY_PRAGMA_POP
541 #endif /* !_SYS_SYSTM_H_ */