systm.h revision 257122
1/*-
2 * Copyright (c) 1982, 1988, 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *	@(#)systm.h	8.7 (Berkeley) 3/29/95
35 * $FreeBSD: stable/10/sys/sys/systm.h 257122 2013-10-25 16:33:24Z kib $
36 */
37
38#ifndef _SYS_SYSTM_H_
39#define	_SYS_SYSTM_H_
40
41#include <machine/atomic.h>
42#include <machine/cpufunc.h>
43#include <sys/callout.h>
44#include <sys/cdefs.h>
45#include <sys/queue.h>
46#include <sys/stdint.h>		/* for people using printf mainly */
47
48extern int cold;		/* nonzero if we are doing a cold boot */
49extern int rebooting;		/* kern_reboot() has been called. */
50extern const char *panicstr;	/* panic message */
51extern char version[];		/* system version */
52extern char compiler_version[];	/* compiler version */
53extern char copyright[];	/* system copyright */
54extern int kstack_pages;	/* number of kernel stack pages */
55
56extern u_long pagesizes[];	/* supported page sizes */
57extern long physmem;		/* physical memory */
58extern long realmem;		/* 'real' memory */
59
60extern char *rootdevnames[2];	/* names of possible root devices */
61
62extern int boothowto;		/* reboot flags, from console subsystem */
63extern int bootverbose;		/* nonzero to print verbose messages */
64
65extern int maxusers;		/* system tune hint */
66extern int ngroups_max;		/* max # of supplemental groups */
67extern int vm_guest;		/* Running as virtual machine guest? */
68
69/*
70 * Detected virtual machine guest types. The intention is to expand
71 * and/or add to the VM_GUEST_VM type if specific VM functionality is
72 * ever implemented (e.g. vendor-specific paravirtualization features).
73 */
74enum VM_GUEST { VM_GUEST_NO = 0, VM_GUEST_VM, VM_GUEST_XEN, VM_GUEST_HV };
75
76#if defined(WITNESS) || defined(INVARIANTS)
77void	kassert_panic(const char *fmt, ...)  __printflike(1, 2);
78#endif
79
80#ifdef	INVARIANTS		/* The option is always available */
81#define	KASSERT(exp,msg) do {						\
82	if (__predict_false(!(exp)))					\
83		kassert_panic msg;						\
84} while (0)
85#define	VNASSERT(exp, vp, msg) do {					\
86	if (__predict_false(!(exp))) {					\
87		vn_printf(vp, "VNASSERT failed\n");			\
88		kassert_panic msg;						\
89	}								\
90} while (0)
91#else
92#define	KASSERT(exp,msg) do { \
93} while (0)
94
95#define	VNASSERT(exp, vp, msg) do { \
96} while (0)
97#endif
98
99#ifndef CTASSERT	/* Allow lint to override */
100#define	CTASSERT(x)	_Static_assert(x, "compile-time assertion failed")
101#endif
102
103/*
104 * Assert that a pointer can be loaded from memory atomically.
105 *
106 * This assertion enforces stronger alignment than necessary.  For example,
107 * on some architectures, atomicity for unaligned loads will depend on
108 * whether or not the load spans multiple cache lines.
109 */
110#define	ASSERT_ATOMIC_LOAD_PTR(var, msg)				\
111	KASSERT(sizeof(var) == sizeof(void *) &&			\
112	    ((uintptr_t)&(var) & (sizeof(void *) - 1)) == 0, msg)
113
114/*
115 * Assert that a thread is in critical(9) section.
116 */
117#define	CRITICAL_ASSERT(td)						\
118	KASSERT((td)->td_critnest >= 1, ("Not in critical section"));
119
120/*
121 * If we have already panic'd and this is the thread that called
122 * panic(), then don't block on any mutexes but silently succeed.
123 * Otherwise, the kernel will deadlock since the scheduler isn't
124 * going to run the thread that holds any lock we need.
125 */
126#define	SCHEDULER_STOPPED() __predict_false(curthread->td_stopsched)
127
128/*
129 * XXX the hints declarations are even more misplaced than most declarations
130 * in this file, since they are needed in one file (per arch) and only used
131 * in two files.
132 * XXX most of these variables should be const.
133 */
134extern int osreldate;
135extern int envmode;
136extern int hintmode;		/* 0 = off. 1 = config, 2 = fallback */
137extern int dynamic_kenv;
138extern struct mtx kenv_lock;
139extern char *kern_envp;
140extern char static_env[];
141extern char static_hints[];	/* by config for now */
142
143extern char **kenvp;
144
145extern const void *zero_region;	/* address space maps to a zeroed page	*/
146
147extern int unmapped_buf_allowed;
148extern int iosize_max_clamp;
149extern int devfs_iosize_max_clamp;
150#define	IOSIZE_MAX	(iosize_max_clamp ? INT_MAX : SSIZE_MAX)
151#define	DEVFS_IOSIZE_MAX	(devfs_iosize_max_clamp ? INT_MAX : SSIZE_MAX)
152
153/*
154 * General function declarations.
155 */
156
157struct inpcb;
158struct lock_object;
159struct malloc_type;
160struct mtx;
161struct proc;
162struct socket;
163struct thread;
164struct tty;
165struct ucred;
166struct uio;
167struct _jmp_buf;
168struct trapframe;
169
170int	setjmp(struct _jmp_buf *) __returns_twice;
171void	longjmp(struct _jmp_buf *, int) __dead2;
172int	dumpstatus(vm_offset_t addr, off_t count);
173int	nullop(void);
174int	eopnotsupp(void);
175int	ureadc(int, struct uio *);
176void	hashdestroy(void *, struct malloc_type *, u_long);
177void	*hashinit(int count, struct malloc_type *type, u_long *hashmask);
178void	*hashinit_flags(int count, struct malloc_type *type,
179    u_long *hashmask, int flags);
180#define	HASH_NOWAIT	0x00000001
181#define	HASH_WAITOK	0x00000002
182
183void	*phashinit(int count, struct malloc_type *type, u_long *nentries);
184void	g_waitidle(void);
185
186void	panic(const char *, ...) __dead2 __printflike(1, 2);
187
188void	cpu_boot(int);
189void	cpu_flush_dcache(void *, size_t);
190void	cpu_rootconf(void);
191void	critical_enter(void);
192void	critical_exit(void);
193void	init_param1(void);
194void	init_param2(long physpages);
195void	init_static_kenv(char *, size_t);
196void	tablefull(const char *);
197int	kvprintf(char const *, void (*)(int, void*), void *, int,
198	    __va_list) __printflike(1, 0);
199void	log(int, const char *, ...) __printflike(2, 3);
200void	log_console(struct uio *);
201int	printf(const char *, ...) __printflike(1, 2);
202int	snprintf(char *, size_t, const char *, ...) __printflike(3, 4);
203int	sprintf(char *buf, const char *, ...) __printflike(2, 3);
204int	uprintf(const char *, ...) __printflike(1, 2);
205int	vprintf(const char *, __va_list) __printflike(1, 0);
206int	vsnprintf(char *, size_t, const char *, __va_list) __printflike(3, 0);
207int	vsnrprintf(char *, size_t, int, const char *, __va_list) __printflike(4, 0);
208int	vsprintf(char *buf, const char *, __va_list) __printflike(2, 0);
209int	ttyprintf(struct tty *, const char *, ...) __printflike(2, 3);
210int	sscanf(const char *, char const *, ...) __nonnull(1) __nonnull(2);
211int	vsscanf(const char *, char const *, __va_list) __nonnull(1) __nonnull(2);
212long	strtol(const char *, char **, int) __nonnull(1);
213u_long	strtoul(const char *, char **, int) __nonnull(1);
214quad_t	strtoq(const char *, char **, int) __nonnull(1);
215u_quad_t strtouq(const char *, char **, int) __nonnull(1);
216void	tprintf(struct proc *p, int pri, const char *, ...) __printflike(3, 4);
217void	vtprintf(struct proc *, int, const char *, __va_list) __printflike(3, 0);
218void	hexdump(const void *ptr, int length, const char *hdr, int flags);
219#define	HD_COLUMN_MASK	0xff
220#define	HD_DELIM_MASK	0xff00
221#define	HD_OMIT_COUNT	(1 << 16)
222#define	HD_OMIT_HEX	(1 << 17)
223#define	HD_OMIT_CHARS	(1 << 18)
224
225#define ovbcopy(f, t, l) bcopy((f), (t), (l))
226void	bcopy(const void *from, void *to, size_t len) __nonnull(1) __nonnull(2);
227void	bzero(void *buf, size_t len) __nonnull(1);
228
229void	*memcpy(void *to, const void *from, size_t len) __nonnull(1) __nonnull(2);
230void	*memmove(void *dest, const void *src, size_t n) __nonnull(1) __nonnull(2);
231
232int	copystr(const void * __restrict kfaddr, void * __restrict kdaddr,
233	    size_t len, size_t * __restrict lencopied)
234	    __nonnull(1) __nonnull(2);
235int	copyinstr(const void * __restrict udaddr, void * __restrict kaddr,
236	    size_t len, size_t * __restrict lencopied)
237	    __nonnull(1) __nonnull(2);
238int	copyin(const void * __restrict udaddr, void * __restrict kaddr,
239	    size_t len) __nonnull(1) __nonnull(2);
240int	copyin_nofault(const void * __restrict udaddr, void * __restrict kaddr,
241	    size_t len) __nonnull(1) __nonnull(2);
242int	copyout(const void * __restrict kaddr, void * __restrict udaddr,
243	    size_t len) __nonnull(1) __nonnull(2);
244int	copyout_nofault(const void * __restrict kaddr, void * __restrict udaddr,
245	    size_t len) __nonnull(1) __nonnull(2);
246
247int	fubyte(const void *base);
248long	fuword(const void *base);
249int	fuword16(void *base);
250int32_t	fuword32(const void *base);
251int64_t	fuword64(const void *base);
252int	subyte(void *base, int byte);
253int	suword(void *base, long word);
254int	suword16(void *base, int word);
255int	suword32(void *base, int32_t word);
256int	suword64(void *base, int64_t word);
257uint32_t casuword32(volatile uint32_t *base, uint32_t oldval, uint32_t newval);
258u_long	 casuword(volatile u_long *p, u_long oldval, u_long newval);
259
260void	realitexpire(void *);
261
262int	sysbeep(int hertz, int period);
263
264void	hardclock(int usermode, uintfptr_t pc);
265void	hardclock_cnt(int cnt, int usermode);
266void	hardclock_cpu(int usermode);
267void	hardclock_sync(int cpu);
268void	softclock(void *);
269void	statclock(int usermode);
270void	statclock_cnt(int cnt, int usermode);
271void	profclock(int usermode, uintfptr_t pc);
272void	profclock_cnt(int cnt, int usermode, uintfptr_t pc);
273
274int	hardclockintr(void);
275
276void	startprofclock(struct proc *);
277void	stopprofclock(struct proc *);
278void	cpu_startprofclock(void);
279void	cpu_stopprofclock(void);
280sbintime_t 	cpu_idleclock(void);
281void	cpu_activeclock(void);
282void	cpu_new_callout(int cpu, sbintime_t bt, sbintime_t bt_opt);
283extern int	cpu_can_deep_sleep;
284extern int	cpu_disable_deep_sleep;
285
286int	cr_cansee(struct ucred *u1, struct ucred *u2);
287int	cr_canseesocket(struct ucred *cred, struct socket *so);
288int	cr_canseeinpcb(struct ucred *cred, struct inpcb *inp);
289
290char	*getenv(const char *name);
291void	freeenv(char *env);
292int	getenv_int(const char *name, int *data);
293int	getenv_uint(const char *name, unsigned int *data);
294int	getenv_long(const char *name, long *data);
295int	getenv_ulong(const char *name, unsigned long *data);
296int	getenv_string(const char *name, char *data, int size);
297int	getenv_quad(const char *name, quad_t *data);
298int	setenv(const char *name, const char *value);
299int	unsetenv(const char *name);
300int	testenv(const char *name);
301
302typedef uint64_t (cpu_tick_f)(void);
303void set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var);
304extern cpu_tick_f *cpu_ticks;
305uint64_t cpu_tickrate(void);
306uint64_t cputick2usec(uint64_t tick);
307
308#ifdef APM_FIXUP_CALLTODO
309struct timeval;
310void	adjust_timeout_calltodo(struct timeval *time_change);
311#endif /* APM_FIXUP_CALLTODO */
312
313#include <sys/libkern.h>
314
315/* Initialize the world */
316void	consinit(void);
317void	cpu_initclocks(void);
318void	cpu_initclocks_bsp(void);
319void	cpu_initclocks_ap(void);
320void	usrinfoinit(void);
321
322/* Finalize the world */
323void	kern_reboot(int) __dead2;
324void	shutdown_nice(int);
325
326/* Timeouts */
327typedef void timeout_t(void *);	/* timeout function type */
328#define CALLOUT_HANDLE_INITIALIZER(handle)	\
329	{ NULL }
330
331void	callout_handle_init(struct callout_handle *);
332struct	callout_handle timeout(timeout_t *, void *, int);
333void	untimeout(timeout_t *, void *, struct callout_handle);
334
335/* Stubs for obsolete functions that used to be for interrupt management */
336static __inline intrmask_t	splbio(void)		{ return 0; }
337static __inline intrmask_t	splcam(void)		{ return 0; }
338static __inline intrmask_t	splclock(void)		{ return 0; }
339static __inline intrmask_t	splhigh(void)		{ return 0; }
340static __inline intrmask_t	splimp(void)		{ return 0; }
341static __inline intrmask_t	splnet(void)		{ return 0; }
342static __inline intrmask_t	spltty(void)		{ return 0; }
343static __inline intrmask_t	splvm(void)		{ return 0; }
344static __inline void		splx(intrmask_t ipl __unused)	{ return; }
345
346/*
347 * Common `proc' functions are declared here so that proc.h can be included
348 * less often.
349 */
350int	_sleep(void *chan, struct lock_object *lock, int pri, const char *wmesg,
351	   sbintime_t sbt, sbintime_t pr, int flags) __nonnull(1);
352#define	msleep(chan, mtx, pri, wmesg, timo)				\
353	_sleep((chan), &(mtx)->lock_object, (pri), (wmesg),		\
354	    tick_sbt * (timo), 0, C_HARDCLOCK)
355#define	msleep_sbt(chan, mtx, pri, wmesg, bt, pr, flags)		\
356	_sleep((chan), &(mtx)->lock_object, (pri), (wmesg), (bt), (pr),	\
357	    (flags))
358int	msleep_spin_sbt(void *chan, struct mtx *mtx, const char *wmesg,
359	    sbintime_t sbt, sbintime_t pr, int flags) __nonnull(1);
360#define	msleep_spin(chan, mtx, wmesg, timo)				\
361	msleep_spin_sbt((chan), (mtx), (wmesg), tick_sbt * (timo),	\
362	    0, C_HARDCLOCK)
363int	pause_sbt(const char *wmesg, sbintime_t sbt, sbintime_t pr,
364	    int flags);
365#define	pause(wmesg, timo)						\
366	pause_sbt((wmesg), tick_sbt * (timo), 0, C_HARDCLOCK)
367#define	tsleep(chan, pri, wmesg, timo)					\
368	_sleep((chan), NULL, (pri), (wmesg), tick_sbt * (timo),		\
369	    0, C_HARDCLOCK)
370#define	tsleep_sbt(chan, pri, wmesg, bt, pr, flags)			\
371	_sleep((chan), NULL, (pri), (wmesg), (bt), (pr), (flags))
372void	wakeup(void *chan) __nonnull(1);
373void	wakeup_one(void *chan) __nonnull(1);
374
375/*
376 * Common `struct cdev *' stuff are declared here to avoid #include poisoning
377 */
378
379struct cdev;
380dev_t dev2udev(struct cdev *x);
381const char *devtoname(struct cdev *cdev);
382
383int poll_no_poll(int events);
384
385/* XXX: Should be void nanodelay(u_int nsec); */
386void	DELAY(int usec);
387
388/* Root mount holdback API */
389struct root_hold_token;
390
391struct root_hold_token *root_mount_hold(const char *identifier);
392void root_mount_rel(struct root_hold_token *h);
393void root_mount_wait(void);
394int root_mounted(void);
395
396
397/*
398 * Unit number allocation API. (kern/subr_unit.c)
399 */
400struct unrhdr;
401struct unrhdr *new_unrhdr(int low, int high, struct mtx *mutex);
402void init_unrhdr(struct unrhdr *uh, int low, int high, struct mtx *mutex);
403void delete_unrhdr(struct unrhdr *uh);
404void clean_unrhdr(struct unrhdr *uh);
405void clean_unrhdrl(struct unrhdr *uh);
406int alloc_unr(struct unrhdr *uh);
407int alloc_unr_specific(struct unrhdr *uh, u_int item);
408int alloc_unrl(struct unrhdr *uh);
409void free_unr(struct unrhdr *uh, u_int item);
410
411/*
412 * Population count algorithm using SWAR approach
413 * - "SIMD Within A Register".
414 */
415static __inline uint32_t
416bitcount32(uint32_t x)
417{
418
419	x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
420	x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
421	x = (x + (x >> 4)) & 0x0f0f0f0f;
422	x = (x + (x >> 8));
423	x = (x + (x >> 16)) & 0x000000ff;
424	return (x);
425}
426
427static __inline uint16_t
428bitcount16(uint32_t x)
429{
430
431	x = (x & 0x5555) + ((x & 0xaaaa) >> 1);
432	x = (x & 0x3333) + ((x & 0xcccc) >> 2);
433	x = (x + (x >> 4)) & 0x0f0f;
434	x = (x + (x >> 8)) & 0x00ff;
435	return (x);
436}
437
438#endif /* !_SYS_SYSTM_H_ */
439