xref: /dragonfly/sys/sys/systm.h (revision 6a8bb22d)
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  * 3. 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: src/sys/sys/systm.h,v 1.111.2.18 2002/12/17 18:04:02 sam Exp $
36  */
37 
38 #ifndef _SYS_SYSTM_H_
39 #define	_SYS_SYSTM_H_
40 
41 #ifndef _KERNEL
42 #error "This file should not be included by userland programs."
43 #else
44 
45 #ifndef _MACHINE_TYPES_H_
46 #include <machine/types.h>
47 #endif
48 #include <machine/stdarg.h>
49 #include <machine/atomic.h>
50 #include <machine/cpufunc.h>
51 #include <sys/callout.h>
52 
53 extern int securelevel;		/* system security level (see init(8)) */
54 extern int kernel_mem_readonly;	/* disable writes to kernel memory */
55 
56 extern int cold;		/* nonzero if we are doing a cold boot */
57 extern int tsleep_now_works;	/* tsleep won't just return any more */
58 extern const char *panicstr;	/* panic message */
59 extern int dumping;		/* system is dumping */
60 extern int safepri;		/* safe ipl when cold or panicing */
61 extern int osreldate;		/* System release date */
62 extern char version[];		/* system version */
63 extern char copyright[];	/* system copyright */
64 
65 extern int selwait;		/* select timeout address */
66 
67 extern u_char curpriority;	/* priority of current process */
68 extern int cpu_mwait_spin;	/* typically set in machdep, used by lwkt */
69 
70 extern long physmem;		/* physical memory */
71 
72 extern cdev_t dumpdev;		/* dump device */
73 extern u_int64_t dumplo64;	/* block number into dumpdev, start of dump */
74 
75 extern cdev_t rootdev;		/* root device */
76 extern cdev_t rootdevs[2];	/* possible root devices */
77 extern char *rootdevnames[2];	/* names of possible root devices */
78 extern char *ZeroPage;
79 
80 extern int boothowto;		/* reboot flags, from console subsystem */
81 extern int bootverbose;		/* nonzero to print verbose messages */
82 
83 extern int maxusers;		/* system tune hint */
84 
85 extern int ncpus;		/* total number of cpus (real, hyper, virtual)*/
86 extern int ncpus_fit;		/* round up to a power of 2 */
87 extern int ncpus_fit_mask;	/* ncpus_fit - 1 */
88 extern int clocks_running;	/* timing/timeout subsystem is operational */
89 
90 /* XXX TGEN these don't belong here, they're MD on pc64 */
91 extern u_int cpu_feature;	/* CPUID_* features */
92 extern u_int cpu_feature2;	/* CPUID2_* features */
93 extern u_int cpu_mi_feature;	/* CPU_MI_XXX machine-nonspecific features */
94 extern cpumask_t usched_global_cpumask;
95 
96 extern int nfs_diskless_valid;	/* NFS diskless params were obtained */
97 extern vm_paddr_t Maxmem;	/* Highest physical memory address in system */
98 
99 #ifdef	INVARIANTS		/* The option is always available */
100 #define	KASSERT(exp,msg)	do { if (__predict_false(!(exp)))	\
101 					panic msg; } while (0)
102 #define KKASSERT(exp)		do { if (__predict_false(!(exp)))	  \
103 					panic("assertion \"%s\" failed "  \
104 					"in %s at %s:%u", #exp, __func__, \
105 					__FILE__, __LINE__); } while (0)
106 #define __debugvar
107 #else
108 #define	KASSERT(exp,msg)	do { } while (0)
109 #define	KKASSERT(exp)		do { } while (0)
110 #define __debugvar		__attribute__((__unused__))
111 #endif
112 
113 /*
114  * General function declarations.
115  */
116 
117 struct intrframe;
118 struct spinlock;
119 struct lock;
120 struct mtx;
121 struct lwkt_serialize;
122 struct malloc_type;
123 struct proc;
124 struct timeval;
125 struct tty;
126 struct uio;
127 struct globaldata;
128 struct thread;
129 struct trapframe;
130 struct user;
131 struct vmspace;
132 struct savetls;
133 struct krate;
134 struct _jmp_buf;
135 
136 void	Debugger (const char *msg);
137 void	print_backtrace(int count);
138 void	mi_gdinit (struct globaldata *gd, int cpu);
139 void	slab_gdinit (struct globaldata *gd);
140 void	mi_proc0init(struct globaldata *gd, struct user *proc0paddr);
141 int	setjmp(struct _jmp_buf *) __returns_twice;
142 void	longjmp(struct _jmp_buf *, int) __dead2;
143 int	nullop (void);
144 int	seltrue (cdev_t dev, int which);
145 int	ureadc (int, struct uio *);
146 void	*hashinit (int count, struct malloc_type *type, u_long *hashmask);
147 void	hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask);
148 void	*phashinit (int count, struct malloc_type *type, u_long *nentries);
149 void	*hashinit_ext (int count, size_t size,
150 			struct malloc_type *type, u_long *hashmask);
151 void	*phashinit_ext (int count, size_t size,
152 			struct malloc_type *type, u_long *nentries);
153 
154 int	cpu_sanitize_frame (struct trapframe *);
155 int	cpu_sanitize_tls (struct savetls *);
156 void	cpu_spinlock_contested(void);
157 void	cpu_halt (void) __dead2;
158 void	cpu_idle_halt (void);
159 void	cpu_reset (void);
160 void	cpu_boot (int);
161 void	cpu_rootconf (void);
162 void	cpu_vmspace_alloc(struct vmspace *);
163 void	cpu_vmspace_free(struct vmspace *);
164 void	cpu_vkernel_trap(struct trapframe *, int);
165 enum vmm_guest_type detect_virtual(void);
166 void	set_user_TLS(void);
167 void	set_vkernel_fp(struct trapframe *);
168 int	kvm_access_check(vm_offset_t, vm_offset_t, int);
169 
170 vm_paddr_t kvtop(void *addr);
171 
172 /*
173  * Old CRC32 API
174  */
175 extern const uint32_t crc32_tab[];
176 uint32_t crc32(const void *buf, size_t size);
177 uint32_t crc32_ext(const void *buf, size_t size, uint32_t ocrc);
178 
179 /*
180  * Newer (fast) iscsi CRC32 APIs.
181  *
182  * NOTE: iscsi_crc32_ext() inverts ocrc input and output as expected,
183  *	 pass a seed of 0 for the equivalent of iscsi_crc32().
184  *
185  *	 calculate_crc32c() does not invert crc32c input and output
186  *	 to maintain FreeBSD API compatibility.
187  */
188 uint32_t iscsi_crc32(const void *buf, size_t size);
189 uint32_t iscsi_crc32_ext(const void *buf, size_t size, uint32_t ocrc);
190 uint32_t calculate_crc32c(uint32_t crc32c, const unsigned char *buffer,
191 			unsigned int length);
192 
193 void	init_param1 (void);
194 void	init_param2 (int physpages);
195 void	tablefull (const char *);
196 int	kvcprintf (char const *, void (*)(int, void*), void *,
197 		      __va_list) __printf0like(1, 0);
198 void	kvcreinitspin(void);
199 int	log (int, const char *, ...) __printflike(2, 3);
200 void	logwakeup (void);
201 void	log_console (struct uio *);
202 int	kprintf (const char *, ...) __printflike(1, 2);
203 void	kprintf0 (const char *, ...) __printflike(1, 2);
204 void	krateprintf (struct krate *, const char *, ...) __printflike(2, 3);
205 int	ksnprintf (char *, size_t, const char *, ...) __printflike(3, 4);
206 int	ksprintf (char *buf, const char *, ...) __printflike(2, 3);
207 int	uprintf (const char *, ...) __printflike(1, 2);
208 int	kvprintf (const char *, __va_list) __printflike(1, 0);
209 int	kvsnprintf (char *, size_t, const char *,
210 			__va_list) __printflike(3, 0);
211 int	kvasnprintf (char **, size_t, const char *,
212 			__va_list) __printflike(3, 0);
213 int	kvsprintf (char *buf, const char *,
214 			__va_list) __printflike(2, 0);
215 int	ttyprintf (struct tty *, const char *, ...) __printflike(2, 3);
216 void	hexdump (const void *ptr, int length, const char *hdr, int flags);
217 void	kprint_cpuset(cpumask_t *mask);
218 #define	HD_COLUMN_MASK	0xff
219 #define	HD_DELIM_MASK	0xff00
220 #define	HD_OMIT_COUNT	(1 << 16)
221 #define	HD_OMIT_HEX	(1 << 17)
222 #define	HD_OMIT_CHARS	(1 << 18)
223 int	ksscanf(const char *, char const *, ...)
224 	    __nonnull(1, 2) __scanflike(2, 3);
225 int	kvsscanf(const char *, char const *, __va_list)
226 	    __nonnull(1, 2) __scanflike(2, 0);
227 void	kvasfree(char **);
228 
229 long	strtol(const char *, char **, int) __nonnull(1);
230 u_long	strtoul(const char *, char **, int) __nonnull(1);
231 quad_t	strtoq(const char *, char **, int) __nonnull(1);
232 u_quad_t strtouq(const char *, char **, int) __nonnull(1);
233 
234 /*
235  * note: some functions commonly used by device drivers may be passed
236  * pointers to volatile storage, volatile set to avoid warnings.
237  */
238 void	bcopy(volatile const void *from, volatile void *to, size_t len)
239 	    __nonnull(1, 2);
240 void	bzero(volatile void *buf, size_t len) __nonnull(1);
241 void	bzeront(volatile void *buf, size_t len) __nonnull(1);
242 void	*memcpy(void *to, const void *from, size_t len)
243 	    __nonnull(1, 2);
244 
245 long	kreadmem64(const void *addr);
246 
247 int	copystr (const void *kfaddr, void *kdaddr, size_t len,
248 		size_t *lencopied) __nonnull(1, 2);
249 int	copyinstr (const void *udaddr, void *kaddr, size_t len,
250 		size_t *lencopied) __nonnull(1, 2);
251 int	copyin(const void *udaddr, void *kaddr, size_t len) __nonnull(1, 2);
252 int	copyin_nofault(const void *udaddr, void *kaddr, size_t len)
253 	    __nonnull(1, 2);
254 int	copyout(const void *kaddr, void *udaddr, size_t len) __nonnull(1, 2);
255 int	copyout_nofault(const void *kaddr, void *udaddr, size_t len)
256 	    __nonnull(1, 2);
257 
258 int	fubyte (const uint8_t *base);
259 int	subyte (uint8_t *base, uint8_t byte);
260 int32_t	fuword32 (const uint32_t *base);
261 int64_t	fuword64 (const uint64_t *base);
262 int	suword32 (uint32_t *base, int word);
263 int	suword64 (uint64_t *base, uint64_t word);
264 uint32_t casu32(volatile uint32_t *p, uint32_t oldval, uint32_t newval);
265 uint64_t casu64(volatile uint64_t *p, uint64_t oldval, uint64_t newval);
266 uint32_t swapu32(volatile uint32_t *p, uint32_t val);
267 uint64_t swapu64(volatile uint64_t *p, uint64_t val);
268 
269 void	DELAY(int usec);
270 void	DRIVERSLEEP(int usec);
271 
272 void	startprofclock (struct proc *);
273 void	stopprofclock (struct proc *);
274 void	setstatclockrate (int hzrate);
275 
276 /*
277  * Kernel environment support functions and sundry.
278  */
279 char	*kgetenv (const char *name);
280 int	ksetenv(const char *name, const char *value);
281 int	kunsetenv(const char *name);
282 void	kfreeenv(char *env);
283 int	ktestenv(const char *name);
284 int	kgetenv_int (const char *name, int *data);
285 int	kgetenv_string (const char *name, char *data, int size);
286 int	kgetenv_ulong(const char *name, unsigned long *data);
287 int	kgetenv_quad (const char *name, quad_t *data);
288 int	kgetenv_long(const char *name, long *data);
289 extern char *kern_envp;
290 
291 #include <sys/libkern.h>
292 
293 /* Initialize the world */
294 void	mi_startup (void);
295 void	nchinit (void);
296 
297 /* Finalize the world. */
298 void	shutdown_nice (int);
299 
300 /*
301  * Kernel to clock driver interface.
302  */
303 void	inittodr (time_t base);
304 void	resettodr (void);
305 void	startrtclock (void);
306 
307 /* Timeouts */
308 typedef void timeout_t (void *);	/* timeout function type */
309 
310 /* Interrupt management */
311 
312 /*
313  * For the alpha arch, some of these functions are static __inline, and
314  * the others should be.
315  */
316 #if defined(__x86_64__)
317 void		setdelayed (void);
318 void		setsoftcambio (void);
319 void		setsoftcamnet (void);
320 void		setsoftunused02 (void);
321 void		setsoftcrypto (void);
322 void		setsoftunused01 (void);
323 void		setsofttty (void);
324 void		setsoftvm (void);
325 void		setsofttq (void);
326 void		schedsofttty (void);
327 void		splz (void);
328 void		splz_check (void);
329 void		cpu_mmw_pause_int(int*, int, int, int);
330 void		cpu_mmw_pause_long(long*, long, int, int);
331 void		cpu_smp_stopped(void);
332 #endif /* __x86_64__ */
333 
334 /*
335  * Various callout lists.
336  */
337 
338 /* Exit callout list declarations. */
339 typedef void (*exitlist_fn) (struct thread *td);
340 
341 int	at_exit (exitlist_fn function);
342 int	rm_at_exit (exitlist_fn function);
343 
344 /* Fork callout list declarations. */
345 typedef void (*forklist_fn) (struct proc *parent, struct proc *child,
346 				 int flags);
347 
348 int	at_fork (forklist_fn function);
349 int	rm_at_fork (forklist_fn function);
350 
351 extern struct globaldata	*panic_cpu_gd;
352 
353 /*
354  * Common `proc' functions are declared here so that proc.h can be included
355  * less often.
356  */
357 int	tsleep(const volatile void *, int, const char *, int);
358 int	ssleep(const volatile void *, struct spinlock *, int, const char *, int)
359 	    __nonnull(1);
360 int	lksleep(const volatile void *, struct lock *, int, const char *, int)
361 	    __nonnull(1);
362 int	mtxsleep(const volatile void *, struct mtx *, int, const char *, int)
363 	    __nonnull(1);
364 int	zsleep(const volatile void *, struct lwkt_serialize *, int, const char *, int)
365 	    __nonnull(1);
366 void	tsleep_interlock(const volatile void *, int) __nonnull(1);
367 void	tsleep_remove (struct thread *);
368 int	lwkt_sleep (const char *, int);
369 void	tstop (void);
370 void	wakeup(const volatile void *chan) __nonnull(1);
371 void	wakeup_one(const volatile void *chan) __nonnull(1);
372 void	wakeup_mycpu(const volatile void *chan) __nonnull(1);
373 void	wakeup_mycpu_one(const volatile void *chan) __nonnull(1);
374 void	wakeup_oncpu(struct globaldata *gd, const volatile void *chan)
375 	    __nonnull(2);
376 void	wakeup_oncpu_one(struct globaldata *gd, const volatile void *chan)
377 	    __nonnull(2);
378 void	wakeup_domain(const volatile void *chan, int domain) __nonnull(1);
379 void	wakeup_domain_one(const volatile void *chan, int domain) __nonnull(1);
380 void	wakeup_start_delayed(void);
381 void	wakeup_end_delayed(void);
382 
383 /*
384  * Common `cdev_t' stuff are declared here to avoid #include poisoning
385  */
386 
387 int major(cdev_t x);
388 int minor(cdev_t x);
389 udev_t dev2udev(cdev_t x);
390 cdev_t udev2dev(udev_t x, int b);
391 int uminor(udev_t dev);
392 int umajor(udev_t dev);
393 udev_t makeudev(int x, int y);
394 
395 /*
396  * Unit number allocation API. (kern/subr_unit.c)
397  */
398 struct unrhdr;
399 struct unrhdr *new_unrhdr(int low, int high, struct lock *lock);
400 void delete_unrhdr(struct unrhdr *uh);
401 int alloc_unr(struct unrhdr *uh);
402 int alloc_unrl(struct unrhdr *uh);
403 void free_unr(struct unrhdr *uh, u_int item);
404 
405 /*
406  * Population count algorithm using SWAR approach
407  * - "SIMD Within A Register".
408  */
409 
410 static __inline uint16_t
411 bitcount16(uint32_t x)
412 {
413 
414 	x = (x & 0x5555) + ((x & 0xaaaa) >> 1);
415 	x = (x & 0x3333) + ((x & 0xcccc) >> 2);
416 	x = (x + (x >> 4)) & 0x0f0f;
417 	x = (x + (x >> 8)) & 0x00ff;
418 	return (x);
419 }
420 
421 static __inline uint32_t
422 bitcount32(uint32_t x)
423 {
424 
425 	x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
426 	x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
427 	x = (x + (x >> 4)) & 0x0f0f0f0f;
428 	x = (x + (x >> 8));
429 	x = (x + (x >> 16)) & 0x000000ff;
430 	return (x);
431 }
432 
433 static __inline uint64_t
434 bitcount64(uint64_t x)
435 {
436 
437 	x = (x & 0x5555555555555555) + ((x & 0xaaaaaaaaaaaaaaaa) >> 1);
438 	x = (x & 0x3333333333333333) + ((x & 0xcccccccccccccccc) >> 2);
439 	x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f;
440 	x = (x + (x >> 8));
441 	x = (x + (x >> 16));
442 	x = (x + (x >> 32)) & 0x000000ff;
443 	return (x);
444 }
445 
446 #endif	/* _KERNEL */
447 #endif /* !_SYS_SYSTM_H_ */
448