| 1 | /*	$OpenBSD: systm.h,v 1.177 2025/07/28 05:08:35 dlg Exp $	*/ |
| 2 | /*	$NetBSD: systm.h,v 1.50 1996/06/09 04:55:09 briggs Exp $	*/ |
| 3 | |
| 4 | /*- |
| 5 | * Copyright (c) 1982, 1988, 1991, 1993 |
| 6 | *	The Regents of the University of California. All rights reserved. |
| 7 | * (c) UNIX System Laboratories, Inc. |
| 8 | * All or some portions of this file are derived from material licensed |
| 9 | * to the University of California by American Telephone and Telegraph |
| 10 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with |
| 11 | * the permission of UNIX System Laboratories, Inc. |
| 12 | * |
| 13 | * Redistribution and use in source and binary forms, with or without |
| 14 | * modification, are permitted provided that the following conditions |
| 15 | * are met: |
| 16 | * 1. Redistributions of source code must retain the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer. |
| 18 | * 2. Redistributions in binary form must reproduce the above copyright |
| 19 | * notice, this list of conditions and the following disclaimer in the |
| 20 | * documentation and/or other materials provided with the distribution. |
| 21 | * 3. Neither the name of the University nor the names of its contributors |
| 22 | * may be used to endorse or promote products derived from this software |
| 23 | * without specific prior written permission. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 35 | * SUCH DAMAGE. |
| 36 | * |
| 37 | *	@(#)systm.h	8.4 (Berkeley) 2/23/94 |
| 38 | */ |
| 39 | |
| 40 | #ifndef __SYSTM_H__ |
| 41 | #define __SYSTM_H__ |
| 42 | |
| 43 | #include <sys/queue.h> |
| 44 | #include <sys/stdarg.h> |
| 45 | |
| 46 | /* |
| 47 | * The `securelevel' variable controls the security level of the system. |
| 48 | * It can only be decreased by process 1 (/sbin/init). |
| 49 | * |
| 50 | * Security levels are as follows: |
| 51 | * -1	permanently insecure mode - always run system in level 0 mode. |
| 52 | * 0	insecure mode - immutable and append-only flags may be turned off. |
| 53 | *	All devices may be read or written subject to permission modes. |
| 54 | * 1	secure mode - immutable and append-only flags may not be changed; |
| 55 | *	raw disks of mounted filesystems, /dev/mem, and /dev/kmem are |
| 56 | *	read-only. |
| 57 | * 2	highly secure mode - same as (1) plus raw disks are always |
| 58 | *	read-only whether mounted or not. This level precludes tampering |
| 59 | *	with filesystems by unmounting them, but also inhibits running |
| 60 | *	newfs while the system is secured. |
| 61 | * |
| 62 | * In normal operation, the system runs in level 0 mode while single user |
| 63 | * and in level 1 mode while multiuser. If level 2 mode is desired while |
| 64 | * running multiuser, it can be set in the multiuser startup script |
| 65 | * (/etc/rc.local) using sysctl(1). If it is desired to run the system |
| 66 | * in level 0 mode while multiuser, initialize the variable securelevel |
| 67 | * in /sys/kern/kern_sysctl.c to -1. Note that it is NOT initialized to |
| 68 | * zero as that would allow the vmunix binary to be patched to -1. |
| 69 | * Without initialization, securelevel loads in the BSS area which only |
| 70 | * comes into existence when the kernel is loaded and hence cannot be |
| 71 | * patched by a stalking hacker. |
| 72 | */ |
| 73 | extern int securelevel;		/* system security level */ |
| 74 | extern const char *panicstr;	/* panic message */ |
| 75 | extern const char version[];		/* system version */ |
| 76 | extern const char copyright[];	/* system copyright */ |
| 77 | extern const char ostype[]; |
| 78 | extern const char osversion[]; |
| 79 | extern const char osrelease[]; |
| 80 | extern int cold;		/* cold start flag initialized in locore */ |
| 81 | extern int db_active;		/* running currently inside ddb(4) */ |
| 82 | |
| 83 | extern char *hw_vendor;		/* sysctl hw.vendor */ |
| 84 | extern char *hw_prod;		/* sysctl hw.product */ |
| 85 | extern char *hw_uuid;		/* sysctl hw.uuid */ |
| 86 | extern char *hw_serial;		/* sysctl hw.serialno */ |
| 87 | extern char *hw_ver;		/* sysctl hw.version */ |
| 88 | |
| 89 | extern int ncpus;		/* number of CPUs used */ |
| 90 | extern int ncpusfound;		/* number of CPUs found */ |
| 91 | extern int nblkdev;		/* number of entries in bdevsw */ |
| 92 | extern int nchrdev;		/* number of entries in cdevsw */ |
| 93 | |
| 94 | extern int physmem;		/* physical memory */ |
| 95 | |
| 96 | extern dev_t dumpdev;		/* dump device */ |
| 97 | extern long dumplo;		/* offset into dumpdev */ |
| 98 | |
| 99 | extern dev_t rootdev;		/* root device */ |
| 100 | extern u_char bootduid[8];	/* boot device disklabel uid */ |
| 101 | extern u_char rootduid[8];	/* root device disklabel uid */ |
| 102 | extern struct vnode *rootvp;	/* vnode equivalent to above */ |
| 103 | extern struct device *rootdv;	/* device equivalent to above */ |
| 104 | |
| 105 | extern dev_t swapdev;		/* swapping device */ |
| 106 | extern struct vnode *swapdev_vp;/* vnode equivalent to above */ |
| 107 | |
| 108 | extern int nowake;		/* dead wakeup(9) channel */ |
| 109 | |
| 110 | #ifdef MP_LOCKDEBUG |
| 111 | extern long __mp_lock_spinout; |
| 112 | #endif |
| 113 | |
| 114 | struct proc; |
| 115 | struct process; |
| 116 | #define curproc curcpu()->ci_curproc |
| 117 | |
| 118 | typedef int	sy_call_t(struct proc *, void *, register_t *); |
| 119 | |
| 120 | extern const struct sysent {	/* system call table */ |
| 121 | 	short	sy_narg;	/* number of args */ |
| 122 | 	short	sy_argsize;	/* total size of arguments */ |
| 123 | 	int	sy_flags; |
| 124 | 	sy_call_t *sy_call;	/* implementing function */ |
| 125 | } sysent[]; |
| 126 | |
| 127 | #define SY_NOLOCK		0x01 |
| 128 | |
| 129 | #if	_BYTE_ORDER == _BIG_ENDIAN |
| 130 | #define SCARG(p, k)	((p)->k.be.datum)	/* get arg from args pointer */ |
| 131 | #elif	_BYTE_ORDER == _LITTLE_ENDIAN |
| 132 | #define SCARG(p, k)	((p)->k.le.datum)	/* get arg from args pointer */ |
| 133 | #else |
| 134 | #error	"what byte order is this machine?" |
| 135 | #endif |
| 136 | |
| 137 | #if defined(_KERNEL) && defined(SYSCALL_DEBUG) |
| 138 | void scdebug_call(struct proc *p, register_t code, const register_t retval[]); |
| 139 | void scdebug_ret(struct proc *p, register_t code, int error, |
| 140 | const register_t retval[]); |
| 141 | #endif /* _KERNEL && SYSCALL_DEBUG */ |
| 142 | |
| 143 | extern int boothowto;		/* reboot flags, from console subsystem */ |
| 144 | |
| 145 | extern void (*v_putc)(int); /* Virtual console putc routine */ |
| 146 | |
| 147 | /* |
| 148 | * General function declarations. |
| 149 | */ |
| 150 | int	nullop(void *); |
| 151 | int	enodev(void); |
| 152 | int	enosys(void); |
| 153 | int	enoioctl(void); |
| 154 | int	enxio(void); |
| 155 | int	eopnotsupp(void *); |
| 156 | |
| 157 | void	*hashinit(int, int, int, u_long *); |
| 158 | void	 hashfree(void *, int, int); |
| 159 | int	sys_nosys(struct proc *, void *, register_t *); |
| 160 | |
| 161 | void	panic(const char *, ...) |
| 162 | __attribute__((__noreturn__,__format__(__kprintf__,1,2))); |
| 163 | void	__assert(const char *, const char *, int, const char *) |
| 164 | __attribute__((__noreturn__)); |
| 165 | int	printf(const char *, ...) |
| 166 | __attribute__((__format__(__kprintf__,1,2))); |
| 167 | void	uprintf(const char *, ...) |
| 168 | __attribute__((__format__(__kprintf__,1,2))); |
| 169 | int	vprintf(const char *, va_list) |
| 170 | __attribute__((__format__(__kprintf__,1,0))); |
| 171 | int	vsnprintf(char *, size_t, const char *, va_list) |
| 172 | __attribute__((__format__(__kprintf__,3,0))); |
| 173 | int	snprintf(char *buf, size_t, const char *, ...) |
| 174 | __attribute__((__format__(__kprintf__,3,4))); |
| 175 | struct tty; |
| 176 | void	ttyprintf(struct tty *, const char *, ...) |
| 177 | __attribute__((__format__(__kprintf__,2,3))); |
| 178 | |
| 179 | void	splassert_fail(int, int, const char *); |
| 180 | extern	int splassert_ctl; |
| 181 | |
| 182 | void	assertwaitok(void); |
| 183 | |
| 184 | void	tablefull(const char *); |
| 185 | |
| 186 | int	kcopy(const void *, void *, size_t) |
| 187 | 		__attribute__ ((__bounded__(__buffer__,1,3))) |
| 188 | 		__attribute__ ((__bounded__(__buffer__,2,3))); |
| 189 | |
| 190 | void	bcopy(const void *, void *, size_t) |
| 191 | 		__attribute__ ((__bounded__(__buffer__,1,3))) |
| 192 | 		__attribute__ ((__bounded__(__buffer__,2,3))); |
| 193 | void	bzero(void *, size_t) |
| 194 | 		__attribute__ ((__bounded__(__buffer__,1,2))); |
| 195 | void	explicit_bzero(void *, size_t) |
| 196 | 		__attribute__ ((__bounded__(__buffer__,1,2))); |
| 197 | int	bcmp(const void *, const void *, size_t); |
| 198 | void	*memcpy(void *, const void *, size_t) |
| 199 | 		__attribute__ ((__bounded__(__buffer__,1,3))) |
| 200 | 		__attribute__ ((__bounded__(__buffer__,2,3))); |
| 201 | void	*memmove(void *, const void *, size_t) |
| 202 | 		__attribute__ ((__bounded__(__buffer__,1,3))) |
| 203 | 		__attribute__ ((__bounded__(__buffer__,2,3))); |
| 204 | void	*memset(void *, int, size_t) |
| 205 | 		__attribute__ ((__bounded__(__buffer__,1,3))); |
| 206 | |
| 207 | int	copyinstr(const void *, void *, size_t, size_t *) |
| 208 | 		__attribute__ ((__bounded__(__string__,2,3))); |
| 209 | int	_copyinstr(const void *, void *, size_t, size_t *) |
| 210 | 		__attribute__ ((__bounded__(__string__,2,3))); |
| 211 | int	copyoutstr(const void *, void *, size_t, size_t *); |
| 212 | int	copyin(const void *, void *, size_t) |
| 213 | 		__attribute__ ((__bounded__(__buffer__,2,3))); |
| 214 | int	_copyin(const void *, void *, size_t) |
| 215 | 		__attribute__ ((__bounded__(__buffer__,2,3))); |
| 216 | int	copyout(const void *, void *, size_t); |
| 217 | int	copyin32(const uint32_t *, uint32_t *); |
| 218 | |
| 219 | void	random_start(int); |
| 220 | void	enqueue_randomness(unsigned int); |
| 221 | void	suspend_randomness(void); |
| 222 | void	resume_randomness(char *, size_t); |
| 223 | |
| 224 | struct arc4random_ctx; |
| 225 | void	arc4random_buf(void *, size_t) |
| 226 | 		__attribute__ ((__bounded__(__buffer__,1,2))); |
| 227 | struct arc4random_ctx	*arc4random_ctx_new(void); |
| 228 | void	arc4random_ctx_free(struct arc4random_ctx *); |
| 229 | void	arc4random_ctx_buf(struct arc4random_ctx *, void *, size_t); |
| 230 | u_int32_t arc4random(void); |
| 231 | u_int32_t arc4random_uniform(u_int32_t); |
| 232 | |
| 233 | struct timeval; |
| 234 | struct timespec; |
| 235 | int	tvtohz(const struct timeval *); |
| 236 | int	tstohz(const struct timespec *); |
| 237 | void	realitexpire(void *); |
| 238 | |
| 239 | extern uint64_t hardclock_period; |
| 240 | extern uint64_t statclock_avg; |
| 241 | extern int statclock_is_randomized; |
| 242 | |
| 243 | struct clockframe; |
| 244 | void	hardclock(struct clockframe *); |
| 245 | |
| 246 | struct clockrequest; |
| 247 | void	statclock(struct clockrequest *, void *, void *); |
| 248 | |
| 249 | void	initclocks(void); |
| 250 | void	inittodr(time_t); |
| 251 | void	resettodr(void); |
| 252 | void	cpu_initclocks(void); |
| 253 | void	cpu_startclock(void); |
| 254 | |
| 255 | void	startprofclock(struct process *); |
| 256 | void	stopprofclock(struct process *); |
| 257 | void	setstatclockrate(int); |
| 258 | void	prof_fork(struct process *); |
| 259 | void	prof_exec(struct process *); |
| 260 | void	prof_write(struct proc *); |
| 261 | |
| 262 | void	start_periodic_resettodr(void); |
| 263 | void	stop_periodic_resettodr(void); |
| 264 | |
| 265 | void	sleep_setup(const volatile void *, int, const char *); |
| 266 | int	sleep_finish(uint64_t, int); |
| 267 | void	sleep_queue_init(void); |
| 268 | |
| 269 | struct cond; |
| 270 | void	cond_init(struct cond *); |
| 271 | void	cond_wait(struct cond *, const char *); |
| 272 | void	cond_signal_handler(void *); |
| 273 | |
| 274 | static inline void |
| 275 | cond_signal(struct cond *c) |
| 276 | { |
| 277 | 	cond_signal_handler(c); |
| 278 | } |
| 279 | |
| 280 | #define	INFSLP	UINT64_MAX |
| 281 | #define	MAXTSLP	(UINT64_MAX - 1) |
| 282 | |
| 283 | struct mutex; |
| 284 | struct rwlock; |
| 285 | void wakeup_n(const volatile void *, int); |
| 286 | void wakeup(const volatile void *); |
| 287 | #define wakeup_one(c) wakeup_n((c), 1) |
| 288 | int	tsleep(const volatile void *, int, const char *, int); |
| 289 | int	tsleep_nsec(const volatile void *, int, const char *, uint64_t); |
| 290 | int	msleep(const volatile void *, struct mutex *, int, const char*, int); |
| 291 | int	msleep_nsec(const volatile void *, struct mutex *, int, const char*, |
| 292 | 	 uint64_t); |
| 293 | int	rwsleep(const volatile void *, struct rwlock *, int, const char *, int); |
| 294 | int	rwsleep_nsec(const volatile void *, struct rwlock *, int, const char *, |
| 295 | 	 uint64_t); |
| 296 | void	yield(void); |
| 297 | |
| 298 | void	wdog_register(int (*)(void *, int), void *); |
| 299 | void	wdog_shutdown(void *); |
| 300 | |
| 301 | /* |
| 302 | * Startup hooks are functions running after the scheduler has started |
| 303 | * but before any threads have been created or root has been mounted. |
| 304 | */ |
| 305 | |
| 306 | struct hook_desc { |
| 307 | 	TAILQ_ENTRY(hook_desc) hd_list; |
| 308 | 	void	(*hd_fn)(void *); |
| 309 | 	void	*hd_arg; |
| 310 | }; |
| 311 | TAILQ_HEAD(hook_desc_head, hook_desc); |
| 312 | |
| 313 | extern struct hook_desc_head startuphook_list; |
| 314 | |
| 315 | void	*hook_establish(struct hook_desc_head *, int, void (*)(void *), void *); |
| 316 | void	hook_disestablish(struct hook_desc_head *, void *); |
| 317 | void	dohooks(struct hook_desc_head *, int); |
| 318 | |
| 319 | #define HOOK_REMOVE	0x01 |
| 320 | #define HOOK_FREE	0x02 |
| 321 | |
| 322 | #define startuphook_establish(fn, arg) \ |
| 323 | 	hook_establish(&startuphook_list, 1, (fn), (arg)) |
| 324 | #define startuphook_disestablish(vhook) \ |
| 325 | 	hook_disestablish(&startuphook_list, (vhook)) |
| 326 | #define dostartuphooks() dohooks(&startuphook_list, HOOK_REMOVE|HOOK_FREE) |
| 327 | |
| 328 | struct uio; |
| 329 | int	uiomove(void *, size_t, struct uio *); |
| 330 | |
| 331 | #if defined(_KERNEL) |
| 332 | |
| 333 | #include <sys/rwlock.h> |
| 334 | |
| 335 | extern struct rwlock netlock; |
| 336 | |
| 337 | /* |
| 338 | * Network stack data structures are, unless stated otherwise, protected |
| 339 | * by the NET_LOCK(). It's a single non-recursive lock for the whole |
| 340 | * subsystem. |
| 341 | */ |
| 342 | #define	NET_LOCK()	do { rw_enter_write(&netlock); } while (0) |
| 343 | #define	NET_UNLOCK()	do { rw_exit_write(&netlock); } while (0) |
| 344 | |
| 345 | /* |
| 346 | * Reader version of NET_LOCK(). |
| 347 | * The "softnet" thread should be the only thread processing packets |
| 348 | * without holding an exclusive lock. This is done to allow read-only |
| 349 | * ioctl(2) to not block. |
| 350 | * Shared lock can be grabbed instead of the exclusive version if no field |
| 351 | * protected by the NET_LOCK() is modified by the ioctl/sysctl. |
| 352 | * Socket system call can use shared netlock if it has additional locks |
| 353 | * to protect socket and pcb data structures. |
| 354 | */ |
| 355 | #define	NET_LOCK_SHARED()	do { rw_enter_read(&netlock); } while (0) |
| 356 | #define	NET_UNLOCK_SHARED()	do { rw_exit_read(&netlock); } while (0) |
| 357 | |
| 358 | #ifdef DIAGNOSTIC |
| 359 | |
| 360 | #define	NET_ASSERT_UNLOCKED()						\ |
| 361 | do {									\ |
| 362 | 	int _s = rw_status(&netlock);					\ |
| 363 | 	if ((splassert_ctl > 0) && (_s == RW_WRITE))			\ |
| 364 | 		splassert_fail(0, RW_WRITE, __func__);			\ |
| 365 | } while (0) |
| 366 | |
| 367 | #define	NET_ASSERT_LOCKED()						\ |
| 368 | do {									\ |
| 369 | 	int _s = rw_status(&netlock);					\ |
| 370 | 	if ((splassert_ctl > 0) && (_s != RW_WRITE && _s != RW_READ))	\ |
| 371 | 		splassert_fail(RW_READ, _s, __func__);			\ |
| 372 | } while (0) |
| 373 | |
| 374 | #define	NET_ASSERT_LOCKED_EXCLUSIVE()					\ |
| 375 | do {									\ |
| 376 | 	int _s = rw_status(&netlock);					\ |
| 377 | 	if ((splassert_ctl > 0) && (_s != RW_WRITE))			\ |
| 378 | 		splassert_fail(RW_WRITE, _s, __func__);			\ |
| 379 | } while (0) |
| 380 | |
| 381 | #else /* DIAGNOSTIC */ |
| 382 | #define	NET_ASSERT_UNLOCKED()		do {} while (0) |
| 383 | #define	NET_ASSERT_LOCKED()		do {} while (0) |
| 384 | #define	NET_ASSERT_LOCKED_EXCLUSIVE()	do {} while (0) |
| 385 | #endif /* !DIAGNOSTIC */ |
| 386 | |
| 387 | __returns_twice int	setjmp(label_t *); |
| 388 | __dead void	longjmp(label_t *); |
| 389 | #endif |
| 390 | |
| 391 | void	consinit(void); |
| 392 | |
| 393 | void	cpu_startup(void); |
| 394 | void	cpu_configure(void); |
| 395 | void	diskconf(void); |
| 396 | |
| 397 | void	powerbutton_event(void); |
| 398 | |
| 399 | int nfs_mountroot(void); |
| 400 | int dk_mountroot(void); |
| 401 | extern int (*mountroot)(void); |
| 402 | |
| 403 | #include <lib/libkern/libkern.h> |
| 404 | |
| 405 | #define bzero(b, n)		__builtin_bzero((b), (n)) |
| 406 | #define memcmp(b1, b2, n)	__builtin_memcmp((b1), (b2), (n)) |
| 407 | #define memcpy(d, s, n)		__builtin_memcpy((d), (s), (n)) |
| 408 | #define memset(b, c, n)		__builtin_memset((b), (c), (n)) |
| 409 | #if (defined(__GNUC__) && __GNUC__ >= 4) |
| 410 | #define memmove(d, s, n)	__builtin_memmove((d), (s), (n)) |
| 411 | #endif |
| 412 | #if !defined(__clang__) && (defined(__GNUC__) && __GNUC__ >= 4) |
| 413 | #define bcmp(b1, b2, n)		__builtin_bcmp((b1), (b2), (n)) |
| 414 | #define bcopy(s, d, n)		__builtin_bcopy((s), (d), (n)) |
| 415 | #endif |
| 416 | |
| 417 | #if defined(DDB) |
| 418 | /* debugger entry points */ |
| 419 | void	db_enter(void);	/* in DDB only */ |
| 420 | int	db_rint(int); |
| 421 | #endif |
| 422 | |
| 423 | #ifdef BOOT_CONFIG |
| 424 | void	user_config(void); |
| 425 | #endif |
| 426 | |
| 427 | #if defined(MULTIPROCESSOR) |
| 428 | void	_kernel_lock_init(void); |
| 429 | void	_kernel_lock(void); |
| 430 | void	_kernel_unlock(void); |
| 431 | int	_kernel_lock_held(void); |
| 432 | |
| 433 | #define	KERNEL_LOCK_INIT()		_kernel_lock_init() |
| 434 | #define	KERNEL_LOCK()			_kernel_lock() |
| 435 | #define	KERNEL_UNLOCK()			_kernel_unlock() |
| 436 | #define	KERNEL_ASSERT_LOCKED()		KASSERT(_kernel_lock_held()) |
| 437 | #define	KERNEL_ASSERT_UNLOCKED()	KASSERT(panicstr || db_active || !_kernel_lock_held()) |
| 438 | |
| 439 | #else /* ! MULTIPROCESSOR */ |
| 440 | |
| 441 | #define	KERNEL_LOCK_INIT()		/* nothing */ |
| 442 | #define	KERNEL_LOCK()			/* nothing */ |
| 443 | #define	KERNEL_UNLOCK()			/* nothing */ |
| 444 | #define	KERNEL_ASSERT_LOCKED()		/* nothing */ |
| 445 | #define	KERNEL_ASSERT_UNLOCKED()	/* nothing */ |
| 446 | |
| 447 | #endif /* MULTIPROCESSOR */ |
| 448 | |
| 449 | #endif /* __SYSTM_H__ */ |