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
37 #ifndef _SYS_SYSTM_H_
38 #define _SYS_SYSTM_H_
39
40 #include <sys/types.h>
41 #include <sys/callout.h>
42 #include <sys/kassert.h>
43 #include <sys/queue.h>
44 #include <sys/stdint.h> /* for people using printf mainly */
45 #include <machine/atomic.h>
46 #include <machine/cpufunc.h>
47
48 __NULLABILITY_PRAGMA_PUSH
49
50 #ifdef _KERNEL
51 extern int cold; /* nonzero if we are doing a cold boot */
52 extern int suspend_blocked; /* block suspend due to pending shutdown */
53 extern int rebooting; /* kern_reboot() has been called. */
54 extern const char version[]; /* system version */
55 extern const char compiler_version[]; /* compiler version */
56 extern const char copyright[]; /* system copyright */
57 extern int kstack_pages; /* number of kernel stack pages */
58
59 extern u_long pagesizes[]; /* supported page sizes */
60 extern long physmem; /* physical memory */
61 extern long realmem; /* 'real' memory */
62
63 extern char *rootdevnames[2]; /* names of possible root devices */
64
65 extern int boothowto; /* reboot flags, from console subsystem */
66 extern int bootverbose; /* nonzero to print verbose messages */
67
68 extern int maxusers; /* system tune hint */
69 extern int ngroups_max; /* max # of supplemental groups */
70 extern int vm_guest; /* Running as virtual machine guest? */
71
72 extern u_long maxphys; /* max raw I/O transfer size */
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_GUEST_VBOX,
82 VM_GUEST_PARALLELS, VM_GUEST_NVMM, VM_GUEST_LAST };
83
84 #endif /* KERNEL */
85
86 /*
87 * Align variables.
88 */
89 #define __read_mostly __section(".data.read_mostly")
90 #define __read_frequently __section(".data.read_frequently")
91 #define __exclusive_cache_line __aligned(CACHE_LINE_SIZE) \
92 __section(".data.exclusive_cache_line")
93 #if defined(_STANDALONE)
94 struct ucred;
95 #endif
96
97 #ifdef _KERNEL
98 #include <sys/param.h> /* MAXCPU */
99 #include <sys/pcpu.h> /* curthread */
100 #include <sys/kpilite.h>
101 #include <sys/limits.h>
102
103 extern bool scheduler_stopped;
104
105 /*
106 * If we have already panic'd and this is the thread that called
107 * panic(), then don't block on any mutexes but silently succeed.
108 * Otherwise, the kernel will deadlock since the scheduler isn't
109 * going to run the thread that holds any lock we need.
110 */
111 #define SCHEDULER_STOPPED() __predict_false(scheduler_stopped)
112
113 extern const int osreldate;
114
115 extern const void *zero_region; /* address space maps to a zeroed page */
116
117 extern int unmapped_buf_allowed;
118
119 #ifdef __LP64__
120 #define IOSIZE_MAX iosize_max()
121 #define DEVFS_IOSIZE_MAX devfs_iosize_max()
122 #else
123 #define IOSIZE_MAX SSIZE_MAX
124 #define DEVFS_IOSIZE_MAX SSIZE_MAX
125 #endif
126
127 /*
128 * General function declarations.
129 */
130
131 struct lock_object;
132 struct malloc_type;
133 struct mtx;
134 struct proc;
135 struct thread;
136 struct tty;
137 struct ucred;
138 struct uio;
139 struct _jmp_buf;
140 struct trapframe;
141 struct eventtimer;
142
143 int setjmp(struct _jmp_buf *) __returns_twice;
144 void longjmp(struct _jmp_buf *, int) __dead2;
145 int dumpstatus(vm_offset_t addr, off_t count);
146 int nullop(void);
147 int eopnotsupp(void);
148 int ureadc(int, struct uio *);
149 void hashdestroy(void *, struct malloc_type *, u_long);
150 void *hashinit(int count, struct malloc_type *type, u_long *hashmask);
151 void *hashinit_flags(int count, struct malloc_type *type,
152 u_long *hashmask, int flags);
153 #define HASH_NOWAIT 0x00000001
154 #define HASH_WAITOK 0x00000002
155
156 void *phashinit(int count, struct malloc_type *type, u_long *nentries);
157 void *phashinit_flags(int count, struct malloc_type *type, u_long *nentries,
158 int flags);
159
160 void cpu_flush_dcache(void *, size_t);
161 void cpu_rootconf(void);
162 void critical_enter_KBI(void);
163 void critical_exit_KBI(void);
164 void critical_exit_preempt(void);
165 void init_param1(void);
166 void init_param2(long physpages);
167 void init_static_kenv(char *, size_t);
168 void tablefull(const char *);
169
170 /*
171 * Allocate per-thread "current" state in the linuxkpi
172 */
173 extern int (*lkpi_alloc_current)(struct thread *, int);
174 int linux_alloc_current_noop(struct thread *, int);
175
176 #if (defined(KLD_MODULE) && !defined(KLD_TIED)) || defined(KTR_CRITICAL) || !defined(_KERNEL) || defined(GENOFFSET)
177 #define critical_enter() critical_enter_KBI()
178 #define critical_exit() critical_exit_KBI()
179 #else
180 static __inline void
critical_enter(void)181 critical_enter(void)
182 {
183 struct thread_lite *td;
184
185 td = (struct thread_lite *)curthread;
186 td->td_critnest++;
187 atomic_interrupt_fence();
188 }
189
190 static __inline void
critical_exit(void)191 critical_exit(void)
192 {
193 struct thread_lite *td;
194
195 td = (struct thread_lite *)curthread;
196 KASSERT(td->td_critnest != 0,
197 ("critical_exit: td_critnest == 0"));
198 atomic_interrupt_fence();
199 td->td_critnest--;
200 atomic_interrupt_fence();
201 if (__predict_false(td->td_owepreempt))
202 critical_exit_preempt();
203
204 }
205 #endif
206
207 #ifdef EARLY_PRINTF
208 typedef void early_putc_t(int ch);
209 extern early_putc_t *early_putc;
210 #define CHECK_EARLY_PRINTF(x) \
211 __CONCAT(early_printf_, EARLY_PRINTF) == __CONCAT(early_printf_, x)
212 #define early_printf_1 1
213 #define early_printf_mvebu 2
214 #define early_printf_ns8250 3
215 #define early_printf_pl011 4
216 #define early_printf_snps 5
217 #define early_printf_sbi 6
218 #else
219 #define CHECK_EARLY_PRINTF(x) 0
220 #endif
221 int kvprintf(char const *, void (*)(int, void*), void *, int,
222 __va_list) __printflike(1, 0);
223 void log(int, const char *, ...) __printflike(2, 3);
224 void log_console(struct uio *);
225 void vlog(int, const char *, __va_list) __printflike(2, 0);
226 int asprintf(char **ret, struct malloc_type *mtp, const char *format,
227 ...) __printflike(3, 4);
228 int printf(const char *, ...) __printflike(1, 2);
229 int snprintf(char *, size_t, const char *, ...) __printflike(3, 4);
230 int sprintf(char *buf, const char *, ...) __printflike(2, 3);
231 int uprintf(const char *, ...) __printflike(1, 2);
232 int vprintf(const char *, __va_list) __printflike(1, 0);
233 int vasprintf(char **ret, struct malloc_type *mtp, const char *format,
234 __va_list ap) __printflike(3, 0);
235 int vsnprintf(char *, size_t, const char *, __va_list) __printflike(3, 0);
236 int vsnrprintf(char *, size_t, int, const char *, __va_list) __printflike(4, 0);
237 int vsprintf(char *buf, const char *, __va_list) __printflike(2, 0);
238 int sscanf(const char *, char const * _Nonnull, ...) __scanflike(2, 3);
239 int vsscanf(const char * _Nonnull, char const * _Nonnull, __va_list) __scanflike(2, 0);
240 long strtol(const char *, char **, int);
241 u_long strtoul(const char *, char **, int);
242 quad_t strtoq(const char *, char **, int);
243 u_quad_t strtouq(const char *, char **, int);
244 void tprintf(struct proc *p, int pri, const char *, ...) __printflike(3, 4);
245 void vtprintf(struct proc *, int, const char *, __va_list) __printflike(3, 0);
246 void hexdump(const void *ptr, int length, const char *hdr, int flags);
247 #define HD_COLUMN_MASK 0xff
248 #define HD_DELIM_MASK 0xff00
249 #define HD_OMIT_COUNT (1 << 16)
250 #define HD_OMIT_HEX (1 << 17)
251 #define HD_OMIT_CHARS (1 << 18)
252
253 #define ovbcopy(f, t, l) bcopy((f), (t), (l))
254 void explicit_bzero(void * _Nonnull, size_t);
255
256 void *memset(void * _Nonnull buf, int c, size_t len);
257 void *memcpy(void * _Nonnull to, const void * _Nonnull from, size_t len);
258 void *memmove(void * _Nonnull dest, const void * _Nonnull src, size_t n);
259 int memcmp(const void *b1, const void *b2, size_t len);
260 #ifdef __CHERI__
261 void *memcpy_data(void * _Nonnull to, const void * _Nonnull from,
262 size_t len);
263 void *memmove_data(void * _Nonnull dest, const void * _Nonnull src,
264 size_t n);
265 #else
266 #define memcpy_data memcpy
267 #define memmove_data memmove
268 #endif
269
270 #ifdef SAN_NEEDS_INTERCEPTORS
271 #define SAN_INTERCEPTOR(func) \
272 __CONCAT(SAN_INTERCEPTOR_PREFIX, __CONCAT(_, func))
273 void *SAN_INTERCEPTOR(memset)(void *, int, size_t);
274 void *SAN_INTERCEPTOR(memcpy)(void *, const void *, size_t);
275 void *SAN_INTERCEPTOR(memmove)(void *, const void *, size_t);
276 int SAN_INTERCEPTOR(memcmp)(const void *, const void *, size_t);
277 #ifndef SAN_RUNTIME
278 #define bcopy(from, to, len) SAN_INTERCEPTOR(memmove)((to), (from), (len))
279 #define bzero(buf, len) SAN_INTERCEPTOR(memset)((buf), 0, (len))
280 #define bcmp(b1, b2, len) SAN_INTERCEPTOR(memcmp)((b1), (b2), (len))
281 #define memset(buf, c, len) SAN_INTERCEPTOR(memset)((buf), (c), (len))
282 #define memcpy(to, from, len) SAN_INTERCEPTOR(memcpy)((to), (from), (len))
283 #define memmove(dest, src, n) SAN_INTERCEPTOR(memmove)((dest), (src), (n))
284 #define memcmp(b1, b2, len) SAN_INTERCEPTOR(memcmp)((b1), (b2), (len))
285 #endif /* !SAN_RUNTIME */
286 #else /* !SAN_NEEDS_INTERCEPTORS */
287 #define bcopy(from, to, len) __builtin_memmove((to), (from), (len))
288 #define bzero(buf, len) __builtin_memset((buf), 0, (len))
289 #define bcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len))
290 #define memset(buf, c, len) __builtin_memset((buf), (c), (len))
291 #define memcpy(to, from, len) __builtin_memcpy((to), (from), (len))
292 #define memmove(dest, src, n) __builtin_memmove((dest), (src), (n))
293 #define memcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len))
294 #endif /* SAN_NEEDS_INTERCEPTORS */
295
296 void *memset_early(void * _Nonnull buf, int c, size_t len);
297 #define bzero_early(buf, len) memset_early((buf), 0, (len))
298 void *memcpy_early(void * _Nonnull to, const void * _Nonnull from, size_t len);
299 void *memmove_early(void * _Nonnull dest, const void * _Nonnull src, size_t n);
300 #define bcopy_early(from, to, len) memmove_early((to), (from), (len))
301
302 #define copystr(src, dst, len, outlen) ({ \
303 size_t __r, __len, *__outlen; \
304 \
305 __len = (len); \
306 __outlen = (outlen); \
307 __r = strlcpy((dst), (src), __len); \
308 if (__outlen != NULL) \
309 *__outlen = ((__r >= __len) ? __len : __r + 1); \
310 ((__r >= __len) ? ENAMETOOLONG : 0); \
311 })
312
313 __nodiscard int copyinstr(const void * __restrict udaddr,
314 void * _Nonnull __restrict kaddr, size_t len,
315 size_t * __restrict lencopied);
316 __nodiscard int copyin(const void * __restrict udaddr,
317 void * _Nonnull __restrict kaddr, size_t len);
318 __nodiscard int copyin_nofault(const void * __restrict udaddr,
319 void * _Nonnull __restrict kaddr, size_t len);
320 __nodiscard int copyout(const void * _Nonnull __restrict kaddr,
321 void * __restrict udaddr, size_t len);
322 __nodiscard int copyout_nofault(
323 const void * _Nonnull __restrict kaddr, void * __restrict udaddr,
324 size_t len);
325
326 #ifdef __CHERI__
327 __nodiscard int copyinptr(const void * __restrict udaddr,
328 void * _Nonnull __restrict kaddr, size_t len);
329 __nodiscard int copyinptr_nofault(const void * __restrict udaddr,
330 void * _Nonnull __restrict kaddr, size_t len);
331 __nodiscard int copyoutptr(
332 const void * _Nonnull __restrict kaddr, void * __restrict udaddr,
333 size_t len);
334 __nodiscard int copyoutptr_nofault(
335 const void * _Nonnull __restrict kaddr, void * __restrict udaddr,
336 size_t len);
337 #else
338 #define copyinptr copyin
339 #define copyinptr_nofault copyin_nofault
340 #define copyoutptr copyout
341 #define copyoutptr_nofault copyout_nofault
342 #endif
343
344 #ifdef SAN_NEEDS_INTERCEPTORS
345 int SAN_INTERCEPTOR(copyin)(const void *, void *, size_t);
346 int SAN_INTERCEPTOR(copyinstr)(const void *, void *, size_t, size_t *);
347 int SAN_INTERCEPTOR(copyout)(const void *, void *, size_t);
348 #ifndef SAN_RUNTIME
349 #define copyin(u, k, l) SAN_INTERCEPTOR(copyin)((u), (k), (l))
350 #define copyinstr(u, k, l, lc) SAN_INTERCEPTOR(copyinstr)((u), (k), (l), (lc))
351 #define copyout(k, u, l) SAN_INTERCEPTOR(copyout)((k), (u), (l))
352 #endif /* !SAN_RUNTIME */
353 #endif /* SAN_NEEDS_INTERCEPTORS */
354
355 int fubyte(volatile const void *base);
356 long fuword(volatile const void *base);
357 int fuword16(volatile const void *base);
358 int32_t fuword32(volatile const void *base);
359 int64_t fuword64(volatile const void *base);
360 __nodiscard int fueword(volatile const void *base, long *val);
361 __nodiscard int fueword32(volatile const void *base, int32_t *val);
362 __nodiscard int fueword64(volatile const void *base, int64_t *val);
363 #ifdef __CHERI__
364 __nodiscard int fueptr(volatile const void *base, intptr_t *val);
365 #else
366 #define fueptr(base, val) fueword((base), (long *)(val))
367 #endif
368 __nodiscard int subyte(volatile void *base, int byte);
369 __nodiscard int suword(volatile void *base, long word);
370 __nodiscard int suword16(volatile void *base, int word);
371 __nodiscard int suword32(volatile void *base, int32_t word);
372 __nodiscard int suword64(volatile void *base, int64_t word);
373 #ifdef __CHERI__
374 __nodiscard int suptr(volatile void *base, intptr_t ptr);
375 #else
376 #define suptr(base, val) suword((base), (val))
377 #endif
378 uint32_t casuword32(volatile uint32_t *base, uint32_t oldval, uint32_t newval);
379 u_long casuword(volatile u_long *p, u_long oldval, u_long newval);
380 int casueword32(volatile uint32_t *base, uint32_t oldval, uint32_t *oldvalp,
381 uint32_t newval);
382 int casueword(volatile u_long *p, u_long oldval, u_long *oldvalp,
383 u_long newval);
384
385 #if defined(SAN_NEEDS_INTERCEPTORS) && !defined(KCSAN)
386 int SAN_INTERCEPTOR(fubyte)(volatile const void *base);
387 int SAN_INTERCEPTOR(fuword16)(volatile const void *base);
388 int SAN_INTERCEPTOR(fueword)(volatile const void *base, long *val);
389 int SAN_INTERCEPTOR(fueword32)(volatile const void *base, int32_t *val);
390 int SAN_INTERCEPTOR(fueword64)(volatile const void *base, int64_t *val);
391 int SAN_INTERCEPTOR(subyte)(volatile void *base, int byte);
392 int SAN_INTERCEPTOR(suword)(volatile void *base, long word);
393 int SAN_INTERCEPTOR(suword16)(volatile void *base, int word);
394 int SAN_INTERCEPTOR(suword32)(volatile void *base, int32_t word);
395 int SAN_INTERCEPTOR(suword64)(volatile void *base, int64_t word);
396 int SAN_INTERCEPTOR(casueword32)(volatile uint32_t *base, uint32_t oldval,
397 uint32_t *oldvalp, uint32_t newval);
398 int SAN_INTERCEPTOR(casueword)(volatile u_long *p, u_long oldval,
399 u_long *oldvalp, u_long newval);
400 #ifndef SAN_RUNTIME
401 #define fubyte(b) SAN_INTERCEPTOR(fubyte)((b))
402 #define fuword16(b) SAN_INTERCEPTOR(fuword16)((b))
403 #define fueword(b, v) SAN_INTERCEPTOR(fueword)((b), (v))
404 #define fueword32(b, v) SAN_INTERCEPTOR(fueword32)((b), (v))
405 #define fueword64(b, v) SAN_INTERCEPTOR(fueword64)((b), (v))
406 #define subyte(b, w) SAN_INTERCEPTOR(subyte)((b), (w))
407 #define suword(b, w) SAN_INTERCEPTOR(suword)((b), (w))
408 #define suword16(b, w) SAN_INTERCEPTOR(suword16)((b), (w))
409 #define suword32(b, w) SAN_INTERCEPTOR(suword32)((b), (w))
410 #define suword64(b, w) SAN_INTERCEPTOR(suword64)((b), (w))
411 #define casueword32(b, o, p, n) SAN_INTERCEPTOR(casueword32)((b), (o), (p), (n))
412 #define casueword(b, o, p, n) SAN_INTERCEPTOR(casueword)((b), (o), (p), (n))
413 #endif /* !SAN_RUNTIME */
414 #endif /* SAN_NEEDS_INTERCEPTORS && !KCSAN */
415
416 int sysbeep(int hertz, sbintime_t duration);
417
418 void hardclock(int cnt, int usermode);
419 void hardclock_sync(int cpu);
420 void statclock(int cnt, int usermode);
421 void profclock(int cnt, int usermode, uintfptr_t pc);
422
423 int hardclockintr(void);
424
425 void startprofclock(struct proc *);
426 void stopprofclock(struct proc *);
427 void cpu_startprofclock(void);
428 void cpu_stopprofclock(void);
429 void suspendclock(void);
430 void resumeclock(void);
431 sbintime_t cpu_idleclock(void);
432 void cpu_activeclock(void);
433 void cpu_new_callout(int cpu, sbintime_t bt, sbintime_t bt_opt);
434 void cpu_et_frequency(struct eventtimer *et, uint64_t newfreq);
435 extern int cpu_disable_c2_sleep;
436 extern int cpu_disable_c3_sleep;
437
438 char *kern_getenv(const char *name);
439 void freeenv(char *env);
440 int getenv_int(const char *name, int *data);
441 int getenv_uint(const char *name, unsigned int *data);
442 int getenv_long(const char *name, long *data);
443 int getenv_ulong(const char *name, unsigned long *data);
444 int getenv_string(const char *name, char *data, int size);
445 int getenv_int64(const char *name, int64_t *data);
446 int getenv_uint64(const char *name, uint64_t *data);
447 int getenv_quad(const char *name, quad_t *data);
448 int getenv_bool(const char *name, bool *data);
449 bool getenv_is_true(const char *name);
450 bool getenv_is_false(const char *name);
451 int kern_setenv(const char *name, const char *value);
452 int kern_unsetenv(const char *name);
453 int testenv(const char *name);
454
455 int getenv_array(const char *name, void *data, int size, int *psize,
456 int type_size, bool allow_signed);
457 #define GETENV_UNSIGNED false /* negative numbers not allowed */
458 #define GETENV_SIGNED true /* negative numbers allowed */
459
460 typedef uint64_t (cpu_tick_f)(void);
461 void set_cputicker(cpu_tick_f *func, uint64_t freq, bool isvariable);
462 extern cpu_tick_f *cpu_ticks;
463 uint64_t cpu_tickrate(void);
464 uint64_t cputick2usec(uint64_t tick);
465
466 #include <sys/libkern.h>
467
468 /* Initialize the world */
469 void consinit(void);
470 void cpu_initclocks(void);
471 void cpu_initclocks_bsp(void);
472 void cpu_initclocks_ap(void);
473 void usrinfoinit(void);
474
475 /* Finalize the world */
476 void kern_reboot(int) __dead2;
477 void shutdown_nice(int);
478
479 /* Stubs for obsolete functions that used to be for interrupt management */
splhigh(void)480 static __inline intrmask_t splhigh(void) { return 0; }
splimp(void)481 static __inline intrmask_t splimp(void) { return 0; }
splnet(void)482 static __inline intrmask_t splnet(void) { return 0; }
spltty(void)483 static __inline intrmask_t spltty(void) { return 0; }
splx(intrmask_t ipl __unused)484 static __inline void splx(intrmask_t ipl __unused) { return; }
485
486 /*
487 * Common `proc' functions are declared here so that proc.h can be included
488 * less often.
489 */
490 int _sleep(const void * _Nonnull chan, struct lock_object *lock, int pri,
491 const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags);
492 #define msleep(chan, mtx, pri, wmesg, timo) \
493 _sleep((chan), &(mtx)->lock_object, (pri), (wmesg), \
494 tick_sbt * (timo), 0, C_HARDCLOCK)
495 #define msleep_sbt(chan, mtx, pri, wmesg, bt, pr, flags) \
496 _sleep((chan), &(mtx)->lock_object, (pri), (wmesg), (bt), (pr), \
497 (flags))
498 int msleep_spin_sbt(const void * _Nonnull chan, struct mtx *mtx,
499 const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags);
500 #define msleep_spin(chan, mtx, wmesg, timo) \
501 msleep_spin_sbt((chan), (mtx), (wmesg), tick_sbt * (timo), \
502 0, C_HARDCLOCK)
503 int pause_sbt(const char *wmesg, sbintime_t sbt, sbintime_t pr,
504 int flags);
505 static __inline int
pause(const char * wmesg,int timo)506 pause(const char *wmesg, int timo)
507 {
508 return (pause_sbt(wmesg, tick_sbt * timo, 0, C_HARDCLOCK));
509 }
510 #define pause_sig(wmesg, timo) \
511 pause_sbt((wmesg), tick_sbt * (timo), 0, C_HARDCLOCK | C_CATCH)
512 #define tsleep(chan, pri, wmesg, timo) \
513 _sleep((chan), NULL, (pri), (wmesg), tick_sbt * (timo), \
514 0, C_HARDCLOCK)
515 #define tsleep_sbt(chan, pri, wmesg, bt, pr, flags) \
516 _sleep((chan), NULL, (pri), (wmesg), (bt), (pr), (flags))
517 void wakeup(const void *chan);
518 void wakeup_one(const void *chan);
519 void wakeup_any(const void *chan);
520
521 /*
522 * Common `struct cdev *' stuff are declared here to avoid #include poisoning
523 */
524
525 struct cdev;
526 dev_t dev2udev(struct cdev *x);
527 const char *devtoname(struct cdev *cdev);
528
529 #ifdef __LP64__
530 size_t devfs_iosize_max(void);
531 size_t iosize_max(void);
532 #endif
533
534 int poll_no_poll(int events);
535
536 /* XXX: Should be void nanodelay(u_int nsec); */
537 void DELAY(int usec);
538
539 int kcmp_cmp(uintptr_t a, uintptr_t b);
540
541 /* Root mount holdback API */
542 struct root_hold_token {
543 int flags;
544 const char *who;
545 TAILQ_ENTRY(root_hold_token) list;
546 };
547
548 struct root_hold_token *root_mount_hold(const char *identifier);
549 void root_mount_hold_token(const char *identifier, struct root_hold_token *h);
550 void root_mount_rel(struct root_hold_token *h);
551 int root_mounted(void);
552
553 /*
554 * Unit number allocation API. (kern/subr_unit.c)
555 */
556 struct unrhdr;
557 #define UNR_NO_MTX ((void *)(uintptr_t)-1)
558 struct unrhdr *new_unrhdr(int low, int high, struct mtx *mutex);
559 void init_unrhdr(struct unrhdr *uh, int low, int high, struct mtx *mutex);
560 void delete_unrhdr(struct unrhdr *uh);
561 void clear_unrhdr(struct unrhdr *uh);
562 void clean_unrhdr(struct unrhdr *uh);
563 void clean_unrhdrl(struct unrhdr *uh);
564 int alloc_unr(struct unrhdr *uh);
565 int alloc_unr_specific(struct unrhdr *uh, u_int item);
566 int alloc_unrl(struct unrhdr *uh);
567 void free_unr(struct unrhdr *uh, u_int item);
568 void *create_iter_unr(struct unrhdr *uh);
569 int next_iter_unr(void *handle);
570 void free_iter_unr(void *handle);
571
572 struct unrhdr64 {
573 uint64_t counter;
574 };
575
576 static __inline void
new_unrhdr64(struct unrhdr64 * unr64,uint64_t low)577 new_unrhdr64(struct unrhdr64 *unr64, uint64_t low)
578 {
579
580 unr64->counter = low;
581 }
582
583 static __inline uint64_t
alloc_unr64(struct unrhdr64 * unr64)584 alloc_unr64(struct unrhdr64 *unr64)
585 {
586
587 return (atomic_fetchadd_64(&unr64->counter, 1));
588 }
589
590 void intr_prof_stack_use(struct thread *td, struct trapframe *frame);
591
592 void counted_warning(unsigned *counter, const char *msg);
593
594 /*
595 * Safely read one byte of kernel memory at address addr, placing the
596 * value into *valp. Returns 0 on success, EFAULT if read was
597 * impossible, e.g. due to the address not being mapped or not having
598 * necessary permissions.
599 */
600 int safe_read(vm_offset_t addr, char *valp);
601
602 /*
603 * APIs to manage deprecation and obsolescence.
604 */
605 void _gone_in(int major, const char *msg, ...) __printflike(2, 3);
606 void _gone_in_dev(device_t dev, int major, const char *msg, ...)
607 __printflike(3, 4);
608 #ifdef NO_OBSOLETE_CODE
609 #define __gone_ok(m, msg) \
610 _Static_assert(m < P_OSREL_MAJOR(__FreeBSD_version)), \
611 "Obsolete code: " msg)
612 #else
613 #define __gone_ok(m, msg)
614 #endif
615 #define gone_in(major, msg, ...) do { \
616 static bool __read_mostly __gone_in_ ## __LINE__ = true; \
617 __gone_ok(major, msg); \
618 if (__predict_false(__gone_in_ ## __LINE__)) { \
619 __gone_in_ ## __LINE__ = false; \
620 _gone_in(major, msg __VA_OPT__(,) __VA_ARGS__); \
621 } \
622 } while (0)
623 #define gone_in_dev(dev, major, msg, ...) do { \
624 static bool __read_mostly __gone_in_ ## __LINE__ = true; \
625 __gone_ok(major, msg); \
626 if (__predict_false(__gone_in_ ## __LINE__)) { \
627 __gone_in_ ## __LINE__ = false; \
628 _gone_in_dev(dev, major, msg __VA_OPT__(,) __VA_ARGS__);\
629 } \
630 } while (0)
631
632 #ifdef INVARIANTS
633 #define __diagused
634 #else
635 #define __diagused __unused
636 #endif
637
638 #ifdef WITNESS
639 #define __witness_used
640 #else
641 #define __witness_used __unused
642 #endif
643
644 #endif /* _KERNEL */
645
646 __NULLABILITY_PRAGMA_POP
647 #endif /* !_SYS_SYSTM_H_ */
648