xref: /dragonfly/sys/sys/systm.h (revision a071436b)
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 #endif
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 <machine/int_limits.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 u_char curpriority;	/* priority of current process */
66 extern int cpu_mwait_spin;	/* typically set in machdep, used by lwkt */
67 
68 extern long physmem;		/* physical memory */
69 
70 extern cdev_t dumpdev;		/* dump device */
71 extern u_int64_t dumplo64;	/* block number into dumpdev, start of dump */
72 
73 extern cdev_t rootdev;		/* root device */
74 extern cdev_t rootdevs[2];	/* possible root devices */
75 extern char *rootdevnames[2];	/* names of possible root devices */
76 extern char *ZeroPage;
77 
78 extern int boothowto;		/* reboot flags, from console subsystem */
79 extern int bootverbose;		/* nonzero to print verbose messages */
80 
81 extern int maxusers;		/* system tune hint */
82 
83 extern int ncpus;		/* total number of cpus (real, hyper, virtual)*/
84 extern int ncpus_fit;		/* round up to a power of 2 */
85 extern int ncpus_fit_mask;	/* ncpus_fit - 1 */
86 extern int clocks_running;	/* timing/timeout subsystem is operational */
87 
88 /* XXX TGEN these don't belong here, they're MD on pc64 */
89 extern u_int cpu_feature;	/* CPUID_* features */
90 extern u_int cpu_feature2;	/* CPUID2_* features */
91 extern u_int cpu_mi_feature;	/* CPU_MI_XXX machine-nonspecific features */
92 extern cpumask_t usched_global_cpumask;
93 
94 extern int nfs_diskless_valid;	/* NFS diskless params were obtained */
95 extern vm_paddr_t Maxmem;	/* Highest physical memory address in system */
96 
97 #ifdef	INVARIANTS		/* The option is always available */
98 #define	KASSERT(exp,msg)	do { if (__predict_false(!(exp)))	  \
99 					panic msg; } while (0)
100 
101 #define	KKASSERT(exp)		do { if (__predict_false(!(exp)))	  \
102 					panic("assertion \"%s\" failed "  \
103 					"in %s at %s:%u", #exp, __func__, \
104 					__FILE__, __LINE__); } while (0)
105 
106 #define	KKASSERT_UNSPIN(exp, spin)					  \
107 				do { if (__predict_false(!(exp))) { 	  \
108 					spin_unlock_any(spin);		  \
109 					panic("assertion \"%s\" failed "  \
110 					"in %s at %s:%u", #exp, __func__, \
111 					__FILE__, __LINE__); } } while (0)
112 #define	__debugvar
113 #define	__assert_unreachable()						  \
114 				do {					  \
115 					panic("executing segment marked " \
116 					"as unreachable at %s:%d (%s)\n", \
117 					__FILE__, __LINE__, __func__);    \
118 				} while (0)
119 #else
120 #define	KASSERT(exp,msg)		do { } while (0)
121 #define	KKASSERT(exp)			do { } while (0)
122 #define	KKASSERT_UNSPIN(exp, spin)	do { } while (0)
123 #define	__debugvar		__attribute__((__unused__))
124 #define	__assert_unreachable()	__unreachable()
125 #endif
126 
127 /*
128  * Align variables.
129  */
130 #define	__read_mostly		__section(".data.read_mostly")
131 #define	__read_frequently	__section(".data.read_frequently")
132 #define	__exclusive_cache_line	__aligned(__VM_CACHELINE_SIZE*2)	\
133 				__section(".data.exclusive_cache_line")
134 
135 /*
136  * General function declarations.
137  */
138 
139 struct intrframe;
140 struct spinlock;
141 struct lock;
142 struct mtx;
143 struct lwkt_serialize;
144 struct malloc_type;
145 struct proc;
146 struct timeval;
147 struct tty;
148 struct uio;
149 struct globaldata;
150 struct thread;
151 struct trapframe;
152 struct user;
153 struct vmspace;
154 struct savetls;
155 struct krate;
156 struct _jmp_buf;
157 
158 void	Debugger (const char *msg);
159 void	print_backtrace(int count);
160 void	mi_gdinit (struct globaldata *gd, int cpu);
161 void	slab_gdinit (struct globaldata *gd);
162 void	mi_proc0init(struct globaldata *gd, struct user *proc0paddr);
163 int	setjmp(struct _jmp_buf *) __returns_twice;
164 void	longjmp(struct _jmp_buf *, int) __dead2;
165 int	nullop (void);
166 int	seltrue (cdev_t dev, int which);
167 int	ureadc (int, struct uio *);
168 void	*hashinit (int count, struct malloc_type *type, u_long *hashmask);
169 void	hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask);
170 void	*phashinit (int count, struct malloc_type *type, u_long *nentries);
171 void	*hashinit_ext (int count, size_t size,
172 			struct malloc_type *type, u_long *hashmask);
173 void	*phashinit_ext (int count, size_t size,
174 			struct malloc_type *type, u_long *nentries);
175 
176 int	cpu_sanitize_frame (struct trapframe *);
177 int	cpu_sanitize_tls (struct savetls *);
178 void	cpu_spinlock_contested(void);
179 void	cpu_halt (void) __dead2;
180 void	cpu_idle_halt (void);
181 void	cpu_reset (void);
182 void	cpu_boot (int);
183 void	cpu_rootconf (void);
184 void	cpu_vmspace_alloc(struct vmspace *);
185 void	cpu_vmspace_free(struct vmspace *);
186 void	cpu_vkernel_trap(struct trapframe *, int);
187 enum vmm_guest_type detect_virtual(void);
188 void	set_user_TLS(void);
189 void	set_vkernel_fp(struct trapframe *);
190 int	kvm_access_check(vm_offset_t, vm_offset_t, int);
191 
192 /*
193  * Old CRC32 API
194  */
195 extern const uint32_t crc32_tab[];
196 uint32_t crc32(const void *buf, size_t size);
197 uint32_t crc32_ext(const void *buf, size_t size, uint32_t ocrc);
198 
199 /*
200  * Newer (fast) iscsi CRC32 APIs.
201  *
202  * NOTE: iscsi_crc32_ext() inverts ocrc input and output as expected,
203  *	 pass a seed of 0 for the equivalent of iscsi_crc32().
204  *
205  *	 calculate_crc32c() does not invert crc32c input and output
206  *	 to maintain FreeBSD API compatibility.
207  */
208 uint32_t iscsi_crc32(const void *buf, size_t size);
209 uint32_t iscsi_crc32_ext(const void *buf, size_t size, uint32_t ocrc);
210 uint32_t calculate_crc32c(uint32_t crc32c, const unsigned char *buffer,
211 			unsigned int length);
212 
213 void	init_param1 (void);
214 void	init_param2 (int physpages);
215 void	tablefull (const char *);
216 int	kvcprintf (char const *, void (*)(int, void*), void *,
217 		      __va_list) __printf0like(1, 0);
218 void	kvcreinitspin(void);
219 int	log (int, const char *, ...) __printflike(2, 3);
220 void	logwakeup (void);
221 void	log_console (struct uio *);
222 int	kprintf (const char *, ...) __printflike(1, 2);
223 void	kprintf0 (const char *, ...) __printflike(1, 2);
224 int	krateprintf (struct krate *, const char *, ...) __printflike(2, 3);
225 int	ksnprintf (char *, size_t, const char *, ...) __printflike(3, 4);
226 int	ksprintf (char *buf, const char *, ...) __printflike(2, 3);
227 int	uprintf (const char *, ...) __printflike(1, 2);
228 int	kvprintf (const char *, __va_list) __printflike(1, 0);
229 int	kvsnprintf (char *, size_t, const char *,
230 			__va_list) __printflike(3, 0);
231 int	kvasnprintf (char **, size_t, const char *,
232 			__va_list) __printflike(3, 0);
233 int	kvsprintf (char *buf, const char *,
234 			__va_list) __printflike(2, 0);
235 int	ttyprintf (struct tty *, const char *, ...) __printflike(2, 3);
236 void	hexdump (const void *ptr, int length, const char *hdr, int flags);
237 void	kprint_cpuset(cpumask_t *mask);
238 #define	HD_COLUMN_MASK	0xff
239 #define	HD_DELIM_MASK	0xff00
240 #define	HD_OMIT_COUNT	(1 << 16)
241 #define	HD_OMIT_HEX	(1 << 17)
242 #define	HD_OMIT_CHARS	(1 << 18)
243 int	ksscanf(const char *, char const *, ...)
244 	    __nonnull(1, 2) __scanflike(2, 3);
245 int	kvsscanf(const char *, char const *, __va_list)
246 	    __nonnull(1, 2) __scanflike(2, 0);
247 void	kvasfree(char **);
248 
249 long	strtol(const char *, char **, int) __nonnull(1);
250 u_long	strtoul(const char *, char **, int) __nonnull(1);
251 quad_t	strtoq(const char *, char **, int) __nonnull(1);
252 u_quad_t strtouq(const char *, char **, int) __nonnull(1);
253 
254 /*
255  * NOTE: some functions commonly used by device drivers may be passed
256  *       pointers to volatile storage, volatile set to avoid warnings.
257  *
258  * NOTE: When using builtin's, GCC does enormous optimizations and makes
259  *       a number of assumptions that can cause later unrelated conditionals
260  *       to be optimized out.  In the case of memset and memmove, GCC
261  *       assumes that (to) and (from) cannot be NULL (even when (len) might
262  *       be 0), and will optimize-out later NULL tests on (to) or (from).
263  */
264 #if 1
265 #define bcopy(from, to, len)				\
266 	__builtin_memmove(__DEQUALIFY(void *, (to)),	\
267 			  __DEQUALIFY(void *, (from)),	\
268 			  (len))
269 #define memcpy(to, from, len)				\
270 	__builtin_memcpy(__DEQUALIFY(void *, (to)),	\
271 			  __DEQUALIFY(void *, (from)),	\
272 			  (len))
273 #define memset(ptr, c, len)				\
274 	__builtin_memset(__DEQUALIFY(void *, (ptr)), (c), (len))
275 #define memmove(to, from, len)				\
276 	__builtin_memmove(__DEQUALIFY(void *, (to)),	\
277 			  __DEQUALIFY(void *, (from)),	\
278 			  (len))
279 #define bzero(buf, len)					\
280 	__builtin_memset(__DEQUALIFY(void *, (buf)), 0, (len))
281 #else
282 void	bcopy(volatile const void *from, volatile void *to, size_t len)
283 	    __nonnull(1, 2);
284 void	*memcpy(void *to, const void *from, size_t len)
285 	    __nonnull(1, 2);
286 void	*memmove(void *, const void *, size_t);
287 void	*memset(void *, int, size_t);
288 void	bzero(volatile void *buf, size_t len) __nonnull(1);
289 #endif
290 void	_bcopy(volatile const void *from, volatile void *to, size_t len)
291 	    __nonnull(1, 2);
292 void	*_memcpy(void *to, const void *from, size_t len)
293 	    __nonnull(1, 2);
294 void	*_memmove(void *, const void *, size_t);
295 void	*_memset(void *, int, size_t);
296 void	_bzero(volatile void *buf, size_t len) __nonnull(1);
297 void	bzeront(volatile void *buf, size_t len) __nonnull(1);
298 void	explicit_bzero(void *buf, size_t len) __nonnull(1);
299 
300 long	kreadmem64(const void *addr);
301 
302 int	copystr (const void *kfaddr, void *kdaddr, size_t len,
303 		size_t *lencopied) __nonnull(1, 2);
304 int	copyinstr (const void *udaddr, void *kaddr, size_t len,
305 		size_t *lencopied) __nonnull(1, 2);
306 int	copyin(const void *udaddr, void *kaddr, size_t len) __nonnull(1, 2);
307 int	copyin_nofault(const void *udaddr, void *kaddr, size_t len)
308 	    __nonnull(1, 2);
309 int	copyout(const void *kaddr, void *udaddr, size_t len) __nonnull(1, 2);
310 int	copyout_nofault(const void *kaddr, void *udaddr, size_t len)
311 	    __nonnull(1, 2);
312 
313 int	fubyte (const uint8_t *base);
314 int	subyte (uint8_t *base, uint8_t byte);
315 int32_t	fuword32 (const uint32_t *base);
316 int64_t	fuword64 (const uint64_t *base);
317 int	suword32 (uint32_t *base, int word);
318 int	suword64 (uint64_t *base, uint64_t word);
319 uint32_t casu32(volatile uint32_t *p, uint32_t oldval, uint32_t newval);
320 uint64_t casu64(volatile uint64_t *p, uint64_t oldval, uint64_t newval);
321 uint32_t swapu32(volatile uint32_t *p, uint32_t val);
322 uint64_t swapu64(volatile uint64_t *p, uint64_t val);
323 uint32_t fuwordadd32(volatile uint32_t *p, uint32_t val);
324 uint64_t fuwordadd64(volatile uint64_t *p, uint64_t val);
325 
326 void	DELAY(int usec);
327 void	DRIVERSLEEP(int usec);
328 
329 void	startprofclock (struct proc *);
330 void	stopprofclock (struct proc *);
331 void	setstatclockrate (int hzrate);
332 
333 /*
334  * Kernel environment support functions and sundry.
335  */
336 char	*kgetenv (const char *name);
337 int	ksetenv(const char *name, const char *value);
338 int	kunsetenv(const char *name);
339 void	kfreeenv(char *env);
340 int	ktestenv(const char *name);
341 int	kgetenv_int (const char *name, int *data);
342 int	kgetenv_string (const char *name, char *data, int size);
343 int	kgetenv_ulong(const char *name, unsigned long *data);
344 int	kgetenv_quad (const char *name, quad_t *data);
345 int	kgetenv_long(const char *name, long *data);
346 extern char *kern_envp;
347 
348 #include <sys/libkern.h>
349 
350 /* Initialize the world */
351 void	mi_startup (void);
352 void	nchinit (void);
353 
354 /* Finalize the world. */
355 void	shutdown_nice (int);
356 
357 /*
358  * Kernel to clock driver interface.
359  */
360 void	inittodr (time_t base);
361 void	resettodr (void);
362 void	startrtclock (void);
363 
364 /* Timeouts */
365 typedef void timeout_t (void *);	/* timeout function type */
366 
367 /* Interrupt management */
368 
369 /*
370  * For the alpha arch, some of these functions are static __inline, and
371  * the others should be.
372  */
373 #if defined(__x86_64__)
374 void		setdelayed (void);
375 void		setsoftcambio (void);
376 void		setsoftcamnet (void);
377 void		setsoftunused02 (void);
378 void		setsoftcrypto (void);
379 void		setsoftunused01 (void);
380 void		setsofttty (void);
381 void		setsoftvm (void);
382 void		setsofttq (void);
383 void		schedsofttty (void);
384 void		splz (void);
385 void		splz_check (void);
386 void		cpu_mmw_pause_int(int*, int, int, int);
387 void		cpu_mmw_pause_long(long*, long, int, int);
388 void		cpu_smp_stopped(void);
389 #endif /* __x86_64__ */
390 
391 /*
392  * Various callout lists.
393  */
394 
395 /* Exit callout list declarations. */
396 typedef void (*exitlist_fn) (struct thread *td);
397 
398 int	at_exit (exitlist_fn function);
399 int	rm_at_exit (exitlist_fn function);
400 
401 /* Fork callout list declarations. */
402 typedef void (*forklist_fn) (struct proc *parent, struct proc *child,
403 				 int flags);
404 
405 int	at_fork (forklist_fn function);
406 int	rm_at_fork (forklist_fn function);
407 
408 extern struct globaldata	*panic_cpu_gd;
409 
410 /*
411  * Common `proc' functions are declared here so that proc.h can be included
412  * less often.
413  */
414 int	tsleep(const volatile void *, int, const char *, int);
415 int	ssleep(const volatile void *, struct spinlock *, int, const char *, int)
416 	    __nonnull(1);
417 int	lksleep(const volatile void *, struct lock *, int, const char *, int)
418 	    __nonnull(1);
419 int	mtxsleep(const volatile void *, struct mtx *, int, const char *, int)
420 	    __nonnull(1);
421 int	zsleep(const volatile void *, struct lwkt_serialize *, int, const char *, int)
422 	    __nonnull(1);
423 void	tsleep_interlock(const volatile void *, int) __nonnull(1);
424 void	tsleep_remove (struct thread *);
425 int	lwkt_sleep (const char *, int);
426 void	tstop (void);
427 void	wakeup(const volatile void *chan) __nonnull(1);
428 void	wakeup_one(const volatile void *chan) __nonnull(1);
429 void	wakeup_mycpu(const volatile void *chan) __nonnull(1);
430 void	wakeup_mycpu_one(const volatile void *chan) __nonnull(1);
431 void	wakeup_oncpu(struct globaldata *gd, const volatile void *chan)
432 	    __nonnull(2);
433 void	wakeup_oncpu_one(struct globaldata *gd, const volatile void *chan)
434 	    __nonnull(2);
435 void	wakeup_domain(const volatile void *chan, int domain) __nonnull(1);
436 void	wakeup_domain_one(const volatile void *chan, int domain) __nonnull(1);
437 void	wakeup_start_delayed(void);
438 void	wakeup_end_delayed(void);
439 
440 /*
441  * Common `cdev_t' stuff are declared here to avoid #include poisoning
442  */
443 
444 int major(cdev_t x);
445 int minor(cdev_t x);
446 dev_t devid_from_dev(cdev_t x);
447 cdev_t dev_from_devid(dev_t x, int b);
448 int uminor(dev_t dev);
449 int umajor(dev_t dev);
450 dev_t makeudev(int x, int y);
451 
452 /*
453  * Unit number allocation API. (kern/subr_unit.c)
454  */
455 struct unrhdr;
456 struct unrhdr *new_unrhdr(int low, int high, struct lock *lock);
457 void delete_unrhdr(struct unrhdr *uh);
458 int alloc_unr(struct unrhdr *uh);
459 int alloc_unrl(struct unrhdr *uh);
460 void free_unr(struct unrhdr *uh, u_int item);
461 
462 /*
463  * Population count algorithm using SWAR approach
464  * - "SIMD Within A Register".
465  */
466 
467 static __inline uint16_t
468 bitcount16(uint32_t x)
469 {
470 
471 	x = (x & 0x5555) + ((x & 0xaaaa) >> 1);
472 	x = (x & 0x3333) + ((x & 0xcccc) >> 2);
473 	x = (x + (x >> 4)) & 0x0f0f;
474 	x = (x + (x >> 8)) & 0x00ff;
475 	return (x);
476 }
477 
478 static __inline uint32_t
479 bitcount32(uint32_t x)
480 {
481 
482 	x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
483 	x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
484 	x = (x + (x >> 4)) & 0x0f0f0f0f;
485 	x = (x + (x >> 8));
486 	x = (x + (x >> 16)) & 0x000000ff;
487 	return (x);
488 }
489 
490 static __inline uint64_t
491 bitcount64(uint64_t x)
492 {
493 
494 	x = (x & 0x5555555555555555) + ((x & 0xaaaaaaaaaaaaaaaa) >> 1);
495 	x = (x & 0x3333333333333333) + ((x & 0xcccccccccccccccc) >> 2);
496 	x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f;
497 	x = (x + (x >> 8));
498 	x = (x + (x >> 16));
499 	x = (x + (x >> 32)) & 0x000000ff;
500 	return (x);
501 }
502 
503 /*
504  * Calculate (a * b) / d with a 128-bit intermediate computation to
505  * avoid overflow.
506  */
507 static __inline uint64_t
508 muldivu64(uint64_t a, uint64_t b, uint64_t d)
509 {
510 	_uint128_t t;
511 
512 	t = (_uint128_t)a * b;
513 	if (t / d > UINT64_MAX) {
514 		kprintf("muldivu64: overflow %lu,%lu,%lu\n", a, b, d);
515 		print_backtrace(-1);
516 	}
517 	return (t / d);
518 }
519 
520 #endif /* !_SYS_SYSTM_H_ */
521