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