systm.h revision 192323
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1982, 1988, 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes * (c) UNIX System Laboratories, Inc.
51541Srgrimes * All or some portions of this file are derived from material licensed
61541Srgrimes * to the University of California by American Telephone and Telegraph
71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
81541Srgrimes * the permission of UNIX System Laboratories, Inc.
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 4. Neither the name of the University nor the names of its contributors
191541Srgrimes *    may be used to endorse or promote products derived from this software
201541Srgrimes *    without specific prior written permission.
211541Srgrimes *
221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321541Srgrimes * SUCH DAMAGE.
331541Srgrimes *
3414508Shsu *	@(#)systm.h	8.7 (Berkeley) 3/29/95
3550477Speter * $FreeBSD: head/sys/sys/systm.h 192323 2009-05-18 18:37:18Z marcel $
361541Srgrimes */
371541Srgrimes
382165Spaul#ifndef _SYS_SYSTM_H_
392865Sbde#define	_SYS_SYSTM_H_
402165Spaul
4149043Salc#include <machine/atomic.h>
421549Srgrimes#include <machine/cpufunc.h>
4329683Sgibbs#include <sys/callout.h>
44120610Smux#include <sys/cdefs.h>
4594936Smux#include <sys/queue.h>
46112367Sphk#include <sys/stdint.h>		/* for people using printf mainly */
471549Srgrimes
4818883Sbdeextern int securelevel;		/* system security level (see init(8)) */
497090Sbde
507090Sbdeextern int cold;		/* nonzero if we are doing a cold boot */
51155383Sjeffextern int rebooting;		/* boot() has been called. */
521541Srgrimesextern const char *panicstr;	/* panic message */
531541Srgrimesextern char version[];		/* system version */
541541Srgrimesextern char copyright[];	/* system copyright */
55103083Speterextern int kstack_pages;	/* number of kernel stack pages */
561541Srgrimes
571541Srgrimesextern int nswap;		/* size of swap space */
581541Srgrimes
59102600Speterextern long physmem;		/* physical memory */
60142834Swesextern long realmem;		/* 'real' memory */
611541Srgrimes
6236809Sbdeextern char *rootdevnames[2];	/* names of possible root devices */
631541Srgrimes
641541Srgrimesextern int boothowto;		/* reboot flags, from console subsystem */
6515113Sbdeextern int bootverbose;		/* nonzero to print verbose messages */
661541Srgrimes
6780418Speterextern int maxusers;		/* system tune hint */
6880418Speter
6942453Seivind#ifdef	INVARIANTS		/* The option is always available */
70120610Smux#define	KASSERT(exp,msg) do {						\
71120610Smux	if (__predict_false(!(exp)))					\
72120610Smux		panic msg;						\
73120610Smux} while (0)
74141458Sphk#define	VNASSERT(exp, vp, msg) do {					\
75141458Sphk	if (__predict_false(!(exp))) {					\
76182909Sjhb		vn_printf(vp, "VNASSERT failed\n");			\
77141458Sphk		panic msg;						\
78141458Sphk	}								\
79141458Sphk} while (0)
8042408Seivind#else
81165547Skmacy#define	KASSERT(exp,msg) do { \
82165547Skmacy} while (0)
83165547Skmacy
84165547Skmacy#define	VNASSERT(exp, vp, msg) do { \
85165547Skmacy} while (0)
8642408Seivind#endif
8742408Seivind
88109316Sphk#ifndef CTASSERT		/* Allow lint to override */
8993600Sjake#define	CTASSERT(x)		_CTASSERT(x, __LINE__)
9093600Sjake#define	_CTASSERT(x, y)		__CTASSERT(x, y)
9193600Sjake#define	__CTASSERT(x, y)	typedef char __assert ## y[(x) ? 1 : -1]
92109316Sphk#endif
9393600Sjake
941541Srgrimes/*
9583595Speter * XXX the hints declarations are even more misplaced than most declarations
9683595Speter * in this file, since they are needed in one file (per arch) and only used
9783595Speter * in two files.
9883595Speter * XXX most of these variables should be const.
9983595Speter */
100174253Skibextern int osreldate;
10183595Speterextern int envmode;
10283595Speterextern int hintmode;		/* 0 = off. 1 = config, 2 = fallback */
10394936Smuxextern int dynamic_kenv;
104160217Sscottlextern struct mtx kenv_lock;
10583595Speterextern char *kern_envp;
10683595Speterextern char static_env[];
10783595Speterextern char static_hints[];	/* by config for now */
10883595Speter
10994936Smuxextern char **kenvp;
11094936Smux
11183595Speter/*
1121541Srgrimes * General function declarations.
1131541Srgrimes */
11430282Sphk
115183982Sbzstruct inpcb;
116167387Sjhbstruct lock_object;
11730282Sphkstruct malloc_type;
11865708Sjakestruct mtx;
11933777Sbdestruct proc;
12092976Srwatsonstruct socket;
12183366Sjulianstruct thread;
12233777Sbdestruct tty;
12367893Sphkstruct ucred;
12433777Sbdestruct uio;
12579418Sjulianstruct _jmp_buf;
12630282Sphk
12792719Salfredint	setjmp(struct _jmp_buf *);
12892719Salfredvoid	longjmp(struct _jmp_buf *, int) __dead2;
12992719Salfredint	dumpstatus(vm_offset_t addr, off_t count);
13092719Salfredint	nullop(void);
13192719Salfredint	eopnotsupp(void);
13292719Salfredint	ureadc(int, struct uio *);
13399098Siedowsevoid	hashdestroy(void *, struct malloc_type *, u_long);
134166022Srrsvoid	*hashinit(int count, struct malloc_type *type, u_long *hashmark);
135166022Srrsvoid	*hashinit_flags(int count, struct malloc_type *type,
136166022Srrs    u_long *hashmask, int flags);
137166022Srrs#define	HASH_NOWAIT	0x00000001
138166022Srrs#define	HASH_WAITOK	0x00000002
139166022Srrs
14092719Salfredvoid	*phashinit(int count, struct malloc_type *type, u_long *nentries);
141136836Sphkvoid	g_waitidle(void);
1421541Srgrimes
14390503Sbde#ifdef RESTARTABLE_PANICS
144130164Sphkvoid	panic(const char *, ...) __printflike(1, 2);
14590503Sbde#else
146130164Sphkvoid	panic(const char *, ...) __dead2 __printflike(1, 2);
14790503Sbde#endif
14890503Sbde
14992719Salfredvoid	cpu_boot(int);
150192323Smarcelvoid	cpu_flush_dcache(void *, size_t);
15192719Salfredvoid	cpu_rootconf(void);
15292719Salfredvoid	critical_enter(void);
15392719Salfredvoid	critical_exit(void);
15492719Salfredvoid	init_param1(void);
155102600Spetervoid	init_param2(long physpages);
156117391Ssilbyvoid	init_param3(long kmempages);
15792719Salfredvoid	tablefull(const char *);
15892719Salfredint	kvprintf(char const *, void (*)(int, void*), void *, int,
159102227Smike	    __va_list) __printflike(1, 0);
16092719Salfredvoid	log(int, const char *, ...) __printflike(2, 3);
16192719Salfredvoid	log_console(struct uio *);
16292719Salfredint	printf(const char *, ...) __printflike(1, 2);
16392719Salfredint	snprintf(char *, size_t, const char *, ...) __printflike(3, 4);
16492719Salfredint	sprintf(char *buf, const char *, ...) __printflike(2, 3);
16592719Salfredint	uprintf(const char *, ...) __printflike(1, 2);
166102227Smikeint	vprintf(const char *, __va_list) __printflike(1, 0);
167102227Smikeint	vsnprintf(char *, size_t, const char *, __va_list) __printflike(3, 0);
168137098Sdesint	vsnrprintf(char *, size_t, int, const char *, __va_list) __printflike(4, 0);
169102227Smikeint	vsprintf(char *buf, const char *, __va_list) __printflike(2, 0);
17092719Salfredint	ttyprintf(struct tty *, const char *, ...) __printflike(2, 3);
171117837Sphkint	sscanf(const char *, char const *, ...) __nonnull(1) __nonnull(2);
172117837Sphkint	vsscanf(const char *, char const *, __va_list) __nonnull(1) __nonnull(2);
173117860Sphklong	strtol(const char *, char **, int) __nonnull(1);
174117860Sphku_long	strtoul(const char *, char **, int) __nonnull(1);
175117837Sphkquad_t	strtoq(const char *, char **, int) __nonnull(1);
176117837Sphku_quad_t strtouq(const char *, char **, int) __nonnull(1);
17792719Salfredvoid	tprintf(struct proc *p, int pri, const char *, ...) __printflike(3, 4);
178144706Sphkvoid	hexdump(const void *ptr, int length, const char *hdr, int flags);
179123215Sscottl#define	HD_COLUMN_MASK	0xff
180123215Sscottl#define	HD_DELIM_MASK	0xff00
181123215Sscottl#define	HD_OMIT_COUNT	(1 << 16)
182123215Sscottl#define	HD_OMIT_HEX	(1 << 17)
183123215Sscottl#define	HD_OMIT_CHARS	(1 << 18)
1841541Srgrimes
185113090Sdes#define ovbcopy(f, t, l) bcopy((f), (t), (l))
186117837Sphkvoid	bcopy(const void *from, void *to, size_t len) __nonnull(1) __nonnull(2);
187117837Sphkvoid	bzero(void *buf, size_t len) __nonnull(1);
1881541Srgrimes
189117837Sphkvoid	*memcpy(void *to, const void *from, size_t len) __nonnull(1) __nonnull(2);
190189170Sedvoid	*memmove(void *dest, const void *src, size_t n) __nonnull(1) __nonnull(2);
1918215Sdg
192123852Salfredint	copystr(const void * __restrict kfaddr, void * __restrict kdaddr,
193123852Salfred	    size_t len, size_t * __restrict lencopied)
194123852Salfred	    __nonnull(1) __nonnull(2);
195123852Salfredint	copyinstr(const void * __restrict udaddr, void * __restrict kaddr,
196123852Salfred	    size_t len, size_t * __restrict lencopied)
197123852Salfred	    __nonnull(1) __nonnull(2);
198123852Salfredint	copyin(const void * __restrict udaddr, void * __restrict kaddr,
199123852Salfred	    size_t len) __nonnull(1) __nonnull(2);
200123852Salfredint	copyout(const void * __restrict kaddr, void * __restrict udaddr,
201123852Salfred	    size_t len) __nonnull(1) __nonnull(2);
2021541Srgrimes
20392719Salfredint	fubyte(const void *base);
20498482Speterlong	fuword(const void *base);
20598482Speterint	fuword16(void *base);
20697307Sdfrint32_t	fuword32(const void *base);
20797307Sdfrint64_t	fuword64(const void *base);
20898482Speterint	subyte(void *base, int byte);
20992719Salfredint	suword(void *base, long word);
21098482Speterint	suword16(void *base, int word);
21197307Sdfrint	suword32(void *base, int32_t word);
21297307Sdfrint	suword64(void *base, int64_t word);
213163449Sdavidxuuint32_t casuword32(volatile uint32_t *base, uint32_t oldval, uint32_t newval);
214163449Sdavidxuu_long	 casuword(volatile u_long *p, u_long oldval, u_long newval);
2151541Srgrimes
21692719Salfredvoid	realitexpire(void *);
2171541Srgrimes
218177642Sphkint	sysbeep(int hertz, int period);
219177642Sphk
220153666Sjhbvoid	hardclock(int usermode, uintfptr_t pc);
221153666Sjhbvoid	hardclock_cpu(int usermode);
22292719Salfredvoid	softclock(void *);
223153666Sjhbvoid	statclock(int usermode);
224153666Sjhbvoid	profclock(int usermode, uintfptr_t pc);
2251541Srgrimes
22692719Salfredvoid	startprofclock(struct proc *);
22792719Salfredvoid	stopprofclock(struct proc *);
228110296Sjakevoid	cpu_startprofclock(void);
229110296Sjakevoid	cpu_stopprofclock(void);
2301541Srgrimes
23192719Salfredint	cr_cansee(struct ucred *u1, struct ucred *u2);
23292976Srwatsonint	cr_canseesocket(struct ucred *cred, struct socket *so);
233183982Sbzint	cr_canseeinpcb(struct ucred *cred, struct inpcb *inp);
23467893Sphk
23592719Salfredchar	*getenv(const char *name);
23694936Smuxvoid	freeenv(char *env);
23792719Salfredint	getenv_int(const char *name, int *data);
238172612Sdesint	getenv_uint(const char *name, unsigned int *data);
239172612Sdesint	getenv_long(const char *name, long *data);
240172612Sdesint	getenv_ulong(const char *name, unsigned long *data);
24192719Salfredint	getenv_string(const char *name, char *data, int size);
24292719Salfredint	getenv_quad(const char *name, quad_t *data);
24394959Smuxint	setenv(const char *name, const char *value);
24494936Smuxint	unsetenv(const char *name);
24594936Smuxint	testenv(const char *name);
24640096Smsmith
247155534Sphktypedef uint64_t (cpu_tick_f)(void);
248155534Sphkvoid set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var);
249155534Sphkextern cpu_tick_f *cpu_ticks;
250155534Sphkuint64_t cpu_tickrate(void);
251155534Sphkuint64_t cputick2usec(uint64_t tick);
252155444Sphk
253137098Sdes#ifdef APM_FIXUP_CALLTODO
25483595Speterstruct timeval;
255137098Sdesvoid	adjust_timeout_calltodo(struct timeval *time_change);
256137098Sdes#endif /* APM_FIXUP_CALLTODO */
25731960Snate
2587109Sphk#include <sys/libkern.h>
2592112Swollman
2602112Swollman/* Initialize the world */
26192719Salfredvoid	consinit(void);
26292719Salfredvoid	cpu_initclocks(void);
26392719Salfredvoid	usrinfoinit(void);
2642112Swollman
265167111Sthomas/* Finalize the world */
26692719Salfredvoid	shutdown_nice(int);
2677090Sbde
2682112Swollman/* Timeouts */
26992719Salfredtypedef void timeout_t(void *);	/* timeout function type */
27029683Sgibbs#define CALLOUT_HANDLE_INITIALIZER(handle)	\
27129683Sgibbs	{ NULL }
2722112Swollman
27392719Salfredvoid	callout_handle_init(struct callout_handle *);
27492719Salfredstruct	callout_handle timeout(timeout_t *, void *, int);
27592719Salfredvoid	untimeout(timeout_t *, void *, struct callout_handle);
27692719Salfredcaddr_t	kern_timeout_callwheel_alloc(caddr_t v);
27792719Salfredvoid	kern_timeout_callwheel_init(void);
2782165Spaul
279167111Sthomas/* Stubs for obsolete functions that used to be for interrupt management */
28071240Speterstatic __inline void		spl0(void)		{ return; }
28171240Speterstatic __inline intrmask_t	splbio(void)		{ return 0; }
28271240Speterstatic __inline intrmask_t	splcam(void)		{ return 0; }
28371240Speterstatic __inline intrmask_t	splclock(void)		{ return 0; }
28471240Speterstatic __inline intrmask_t	splhigh(void)		{ return 0; }
28571240Speterstatic __inline intrmask_t	splimp(void)		{ return 0; }
28671240Speterstatic __inline intrmask_t	splnet(void)		{ return 0; }
28771240Speterstatic __inline intrmask_t	splsoftcam(void)	{ return 0; }
28871240Speterstatic __inline intrmask_t	splsoftclock(void)	{ return 0; }
28971240Speterstatic __inline intrmask_t	splsofttty(void)	{ return 0; }
29071240Speterstatic __inline intrmask_t	splsoftvm(void)		{ return 0; }
29171240Speterstatic __inline intrmask_t	splsofttq(void)		{ return 0; }
29271240Speterstatic __inline intrmask_t	splstatclock(void)	{ return 0; }
29371240Speterstatic __inline intrmask_t	spltty(void)		{ return 0; }
29471240Speterstatic __inline intrmask_t	splvm(void)		{ return 0; }
295100073Smarkmstatic __inline void		splx(intrmask_t ipl __unused)	{ return; }
29626312Speter
297137098Sdes/*
29818884Sbde * Common `proc' functions are declared here so that proc.h can be included
29918884Sbde * less often.
30018884Sbde */
301167387Sjhbint	_sleep(void *chan, struct lock_object *lock, int pri, const char *wmesg,
302167387Sjhb	    int timo) __nonnull(1);
303167387Sjhb#define	msleep(chan, mtx, pri, wmesg, timo)				\
304167787Sjhb	_sleep((chan), &(mtx)->lock_object, (pri), (wmesg), (timo))
305167387Sjhbint	msleep_spin(void *chan, struct mtx *mtx, const char *wmesg, int timo)
306167387Sjhb	    __nonnull(1);
307166908Sjhbint	pause(const char *wmesg, int timo);
308167387Sjhb#define	tsleep(chan, pri, wmesg, timo)					\
309167387Sjhb	_sleep((chan), NULL, (pri), (wmesg), (timo))
310117837Sphkvoid	wakeup(void *chan) __nonnull(1);
311117837Sphkvoid	wakeup_one(void *chan) __nonnull(1);
31218884Sbde
31347028Sphk/*
314130585Sphk * Common `struct cdev *' stuff are declared here to avoid #include poisoning
31547028Sphk */
31647028Sphk
317138509Sphkstruct cdev;
318130640Sphkdev_t dev2udev(struct cdev *x);
319143622Sphkconst char *devtoname(struct cdev *cdev);
32067158Sphk
321189450Skibint poll_no_poll(int events);
322189450Skib
32367158Sphk/* XXX: Should be void nanodelay(u_int nsec); */
32492719Salfredvoid	DELAY(int usec);
32567158Sphk
326145250Sphk/* Root mount holdback API */
327145250Sphkstruct root_hold_token;
328145250Sphk
329190878Sthompsastruct root_hold_token *root_mount_hold(const char *identifier);
330145250Sphkvoid root_mount_rel(struct root_hold_token *h);
331168300Spjdvoid root_mount_wait(void);
332168506Spjdint root_mounted(void);
333145250Sphk
334145250Sphk
335135956Sphk/*
336135956Sphk * Unit number allocation API. (kern/subr_unit.c)
337135956Sphk */
338135956Sphkstruct unrhdr;
339143283Sphkstruct unrhdr *new_unrhdr(int low, int high, struct mtx *mutex);
340136945Sphkvoid delete_unrhdr(struct unrhdr *uh);
341171202Skibvoid clean_unrhdr(struct unrhdr *uh);
342171202Skibvoid clean_unrhdrl(struct unrhdr *uh);
343143283Sphkint alloc_unr(struct unrhdr *uh);
344143283Sphkint alloc_unrl(struct unrhdr *uh);
345135956Sphkvoid free_unr(struct unrhdr *uh, u_int item);
346135956Sphk
347149300Spjd/*
348149300Spjd * This is about as magic as it gets.  fortune(1) has got similar code
349149300Spjd * for reversing bits in a word.  Who thinks up this stuff??
350149300Spjd *
351149300Spjd * Yes, it does appear to be consistently faster than:
352149300Spjd * while (i = ffs(m)) {
353149300Spjd *	m >>= i;
354149300Spjd *	bits++;
355149300Spjd * }
356149300Spjd * and
357149300Spjd * while (lsb = (m & -m)) {	// This is magic too
358149300Spjd * 	m &= ~lsb;		// or: m ^= lsb
359149300Spjd *	bits++;
360149300Spjd * }
361149300Spjd * Both of these latter forms do some very strange things on gcc-3.1 with
362149300Spjd * -mcpu=pentiumpro and/or -march=pentiumpro and/or -O or -O2.
363149300Spjd * There is probably an SSE or MMX popcnt instruction.
364149300Spjd *
365149300Spjd * I wonder if this should be in libkern?
366149300Spjd *
367149300Spjd * XXX Stop the presses!  Another one:
368149300Spjd * static __inline u_int32_t
369149300Spjd * popcnt1(u_int32_t v)
370149300Spjd * {
371149300Spjd *	v -= ((v >> 1) & 0x55555555);
372149300Spjd *	v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
373149300Spjd *	v = (v + (v >> 4)) & 0x0F0F0F0F;
374149300Spjd *	return (v * 0x01010101) >> 24;
375149300Spjd * }
376149300Spjd * The downside is that it has a multiply.  With a pentium3 with
377149300Spjd * -mcpu=pentiumpro and -march=pentiumpro then gcc-3.1 will use
378149300Spjd * an imull, and in that case it is faster.  In most other cases
379149300Spjd * it appears slightly slower.
380149300Spjd *
381149300Spjd * Another variant (also from fortune):
382149300Spjd * #define BITCOUNT(x) (((BX_(x)+(BX_(x)>>4)) & 0x0F0F0F0F) % 255)
383149300Spjd * #define  BX_(x)     ((x) - (((x)>>1)&0x77777777)            \
384149300Spjd *                          - (((x)>>2)&0x33333333)            \
385149300Spjd *                          - (((x)>>3)&0x11111111))
386149300Spjd */
387149300Spjdstatic __inline uint32_t
388149300Spjdbitcount32(uint32_t x)
389149300Spjd{
390149300Spjd
391149300Spjd	x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
392149300Spjd	x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
393166698Scperciva	x = (x + (x >> 4)) & 0x0f0f0f0f;
394166698Scperciva	x = (x + (x >> 8));
395166698Scperciva	x = (x + (x >> 16)) & 0x000000ff;
396149300Spjd	return (x);
397149300Spjd}
398149300Spjd
3992865Sbde#endif /* !_SYS_SYSTM_H_ */
400