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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
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 $
42 #error "This file should not be included by userland programs."
45 #ifndef _MACHINE_TYPES_H_
46 #include <machine/types.h>
48 #include <machine/stdarg.h>
49 #include <machine/atomic.h>
50 #include <machine/cpufunc.h>
52 extern int securelevel; /* system security level (see init(8)) */
53 extern int kernel_mem_readonly; /* disable writes to kernel memory */
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 */
64 extern u_char curpriority; /* priority of current process */
65 extern int cpu_mwait_spin; /* typically set in machdep, used by lwkt */
67 extern long physmem; /* physical memory */
69 extern cdev_t dumpdev; /* dump device */
70 extern u_int64_t dumplo64; /* block number into dumpdev, start of dump */
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;
77 extern int boothowto; /* reboot flags, from console subsystem */
78 extern int bootverbose; /* nonzero to print verbose messages */
80 extern int maxusers; /* system tune hint */
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 */
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;
93 extern int nfs_diskless_valid; /* NFS diskless params were obtained */
94 extern vm_paddr_t Maxmem; /* Highest physical memory address in system */
96 #ifdef INVARIANTS /* The option is always available */
97 #define KASSERT(exp,msg) do { if (__predict_false(!(exp))) \
98 panic msg; } while (0)
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)
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)
112 #define __assert_unreachable() \
114 panic("executing segment marked " \
115 "as unreachable at %s:%d (%s)\n", \
116 __FILE__, __LINE__, __func__); \
119 #define KASSERT(exp,msg) do { } while (0)
120 #define KKASSERT(exp) do { } while (0)
121 #define KKASSERT_UNSPIN(exp, spin) do { } while (0)
122 #define __debugvar __attribute__((__unused__))
123 #define __assert_unreachable() __unreachable()
129 #define __read_mostly __section(".data.read_mostly")
130 #define __read_frequently __section(".data.read_frequently")
131 #define __exclusive_cache_line __aligned(__VM_CACHELINE_SIZE*2) \
132 __section(".data.exclusive_cache_line")
135 * General function declarations.
142 struct lwkt_serialize;
157 void Debugger (const char *msg);
158 void print_backtrace(int count);
159 void mi_gdinit (struct globaldata *gd, int cpu);
160 void slab_gdinit (struct globaldata *gd);
161 void mi_proc0init(struct globaldata *gd, struct user *proc0paddr);
162 int setjmp(struct _jmp_buf *) __returns_twice;
163 void longjmp(struct _jmp_buf *, int) __dead2;
165 int seltrue (cdev_t dev, int which);
166 int ureadc (int, struct uio *);
167 void *hashinit (int count, struct malloc_type *type, u_long *hashmask);
168 void hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask);
169 void *phashinit (int count, struct malloc_type *type, u_long *nentries);
170 void *hashinit_ext (int count, size_t size,
171 struct malloc_type *type, u_long *hashmask);
172 void *phashinit_ext (int count, size_t size,
173 struct malloc_type *type, u_long *nentries);
175 int cpu_sanitize_frame (struct trapframe *);
176 int cpu_sanitize_tls (struct savetls *);
177 void cpu_spinlock_contested(void);
178 void cpu_halt (void) __dead2;
179 void cpu_idle_halt (void);
180 void cpu_reset (void);
182 void cpu_rootconf (void);
183 void cpu_vmspace_alloc(struct vmspace *);
184 void cpu_vmspace_free(struct vmspace *);
185 void cpu_vkernel_trap(struct trapframe *, int);
186 enum vmm_guest_type detect_virtual(void);
187 void set_user_TLS(void);
188 void set_vkernel_fp(struct trapframe *);
189 int kvm_access_check(vm_offset_t, vm_offset_t, int);
194 extern const uint32_t crc32_tab[];
195 uint32_t crc32(const void *buf, size_t size);
196 uint32_t crc32_ext(const void *buf, size_t size, uint32_t ocrc);
199 * Newer (fast) iscsi CRC32 APIs.
201 * NOTE: iscsi_crc32_ext() inverts ocrc input and output as expected,
202 * pass a seed of 0 for the equivalent of iscsi_crc32().
204 * calculate_crc32c() does not invert crc32c input and output
205 * to maintain FreeBSD API compatibility.
207 uint32_t iscsi_crc32(const void *buf, size_t size);
208 uint32_t iscsi_crc32_ext(const void *buf, size_t size, uint32_t ocrc);
209 uint32_t calculate_crc32c(uint32_t crc32c, const unsigned char *buffer,
210 unsigned int length);
212 void init_param1 (void);
213 void init_param2 (int physpages);
214 void tablefull (const char *);
215 int kvcprintf (char const *, void (*)(int, void*), void *,
216 __va_list) __printf0like(1, 0);
217 void kvcreinitspin(void);
218 int log (int, const char *, ...) __printflike(2, 3);
219 void logwakeup (void);
220 void log_console (struct uio *);
221 int kprintf (const char *, ...) __printflike(1, 2);
222 void kprintf0 (const char *, ...) __printflike(1, 2);
223 int krateprintf (struct krate *, const char *, ...) __printflike(2, 3);
224 int ksnprintf (char *, size_t, const char *, ...) __printflike(3, 4);
225 int ksprintf (char *buf, const char *, ...) __printflike(2, 3);
226 int uprintf (const char *, ...) __printflike(1, 2);
227 int kvprintf (const char *, __va_list) __printflike(1, 0);
228 int kvsnprintf (char *, size_t, const char *,
229 __va_list) __printflike(3, 0);
230 int kvasnprintf (char **, size_t, const char *,
231 __va_list) __printflike(3, 0);
232 int kvsprintf (char *buf, const char *,
233 __va_list) __printflike(2, 0);
234 int ttyprintf (struct tty *, const char *, ...) __printflike(2, 3);
235 void hexdump (const void *ptr, int length, const char *hdr, int flags);
236 void kprint_cpuset(cpumask_t *mask);
237 #define HD_COLUMN_MASK 0xff
238 #define HD_DELIM_MASK 0xff00
239 #define HD_OMIT_COUNT (1 << 16)
240 #define HD_OMIT_HEX (1 << 17)
241 #define HD_OMIT_CHARS (1 << 18)
242 int ksscanf(const char *, char const *, ...)
243 __nonnull(1, 2) __scanflike(2, 3);
244 int kvsscanf(const char *, char const *, __va_list)
245 __nonnull(1, 2) __scanflike(2, 0);
246 void kvasfree(char **);
248 long strtol(const char *, char **, int) __nonnull(1);
249 u_long strtoul(const char *, char **, int) __nonnull(1);
250 quad_t strtoq(const char *, char **, int) __nonnull(1);
251 u_quad_t strtouq(const char *, char **, int) __nonnull(1);
254 * NOTE: some functions commonly used by device drivers may be passed
255 * pointers to volatile storage, volatile set to avoid warnings.
257 * NOTE: When using builtin's, GCC does enormous optimizations and makes
258 * a number of assumptions that can cause later unrelated conditionals
259 * to be optimized out. In the case of memset and memmove, GCC
260 * assumes that (to) and (from) cannot be NULL (even when (len) might
261 * be 0), and will optimize-out later NULL tests on (to) or (from).
264 #define bcopy(from, to, len) \
265 __builtin_memmove(__DEQUALIFY(void *, (to)), \
266 __DEQUALIFY(void *, (from)), \
268 #define memcpy(to, from, len) \
269 __builtin_memcpy(__DEQUALIFY(void *, (to)), \
270 __DEQUALIFY(void *, (from)), \
272 #define memset(ptr, c, len) \
273 __builtin_memset(__DEQUALIFY(void *, (ptr)), (c), (len))
274 #define memmove(to, from, len) \
275 __builtin_memmove(__DEQUALIFY(void *, (to)), \
276 __DEQUALIFY(void *, (from)), \
278 #define bzero(buf, len) \
279 __builtin_memset(__DEQUALIFY(void *, (buf)), 0, (len))
281 void bcopy(volatile const void *from, volatile void *to, size_t len)
283 void *memcpy(void *to, const void *from, size_t len)
285 void *memmove(void *, const void *, size_t);
286 void *memset(void *, int, size_t);
287 void bzero(volatile void *buf, size_t len) __nonnull(1);
289 void _bcopy(volatile const void *from, volatile void *to, size_t len)
291 void *_memcpy(void *to, const void *from, size_t len)
293 void *_memmove(void *, const void *, size_t);
294 void *_memset(void *, int, size_t);
295 void _bzero(volatile void *buf, size_t len) __nonnull(1);
296 void bzeront(volatile void *buf, size_t len) __nonnull(1);
298 long kreadmem64(const void *addr);
300 int copystr (const void *kfaddr, void *kdaddr, size_t len,
301 size_t *lencopied) __nonnull(1, 2);
302 int copyinstr (const void *udaddr, void *kaddr, size_t len,
303 size_t *lencopied) __nonnull(1, 2);
304 int copyin(const void *udaddr, void *kaddr, size_t len) __nonnull(1, 2);
305 int copyin_nofault(const void *udaddr, void *kaddr, size_t len)
307 int copyout(const void *kaddr, void *udaddr, size_t len) __nonnull(1, 2);
308 int copyout_nofault(const void *kaddr, void *udaddr, size_t len)
311 int fubyte (const uint8_t *base);
312 int subyte (uint8_t *base, uint8_t byte);
313 int32_t fuword32 (const uint32_t *base);
314 int64_t fuword64 (const uint64_t *base);
315 int suword32 (uint32_t *base, int word);
316 int suword64 (uint64_t *base, uint64_t word);
317 uint32_t casu32(volatile uint32_t *p, uint32_t oldval, uint32_t newval);
318 uint64_t casu64(volatile uint64_t *p, uint64_t oldval, uint64_t newval);
319 uint32_t swapu32(volatile uint32_t *p, uint32_t val);
320 uint64_t swapu64(volatile uint64_t *p, uint64_t val);
321 uint32_t fuwordadd32(volatile uint32_t *p, uint32_t val);
322 uint64_t fuwordadd64(volatile uint64_t *p, uint64_t val);
324 void DELAY(int usec);
325 void DRIVERSLEEP(int usec);
327 void startprofclock (struct proc *);
328 void stopprofclock (struct proc *);
329 void setstatclockrate (int hzrate);
332 * Kernel environment support functions and sundry.
334 char *kgetenv (const char *name);
335 int ksetenv(const char *name, const char *value);
336 int kunsetenv(const char *name);
337 void kfreeenv(char *env);
338 int ktestenv(const char *name);
339 int kgetenv_int (const char *name, int *data);
340 int kgetenv_string (const char *name, char *data, int size);
341 int kgetenv_ulong(const char *name, unsigned long *data);
342 int kgetenv_quad (const char *name, quad_t *data);
343 int kgetenv_long(const char *name, long *data);
344 extern char *kern_envp;
346 #include <sys/libkern.h>
348 /* Initialize the world */
349 void mi_startup (void);
352 /* Finalize the world. */
353 void shutdown_nice (int);
356 * Kernel to clock driver interface.
358 void inittodr (time_t base);
359 void resettodr (void);
360 void startrtclock (void);
363 typedef void timeout_t (void *); /* timeout function type */
365 /* Interrupt management */
368 * For the alpha arch, some of these functions are static __inline, and
369 * the others should be.
371 #if defined(__x86_64__)
372 void setdelayed (void);
373 void setsoftcambio (void);
374 void setsoftcamnet (void);
375 void setsoftunused02 (void);
376 void setsoftcrypto (void);
377 void setsoftunused01 (void);
378 void setsofttty (void);
379 void setsoftvm (void);
380 void setsofttq (void);
381 void schedsofttty (void);
383 void splz_check (void);
384 void cpu_mmw_pause_int(int*, int, int, int);
385 void cpu_mmw_pause_long(long*, long, int, int);
386 void cpu_smp_stopped(void);
387 #endif /* __x86_64__ */
390 * Various callout lists.
393 /* Exit callout list declarations. */
394 typedef void (*exitlist_fn) (struct thread *td);
396 int at_exit (exitlist_fn function);
397 int rm_at_exit (exitlist_fn function);
399 /* Fork callout list declarations. */
400 typedef void (*forklist_fn) (struct proc *parent, struct proc *child,
403 int at_fork (forklist_fn function);
404 int rm_at_fork (forklist_fn function);
406 extern struct globaldata *panic_cpu_gd;
409 * Common `proc' functions are declared here so that proc.h can be included
412 int tsleep(const volatile void *, int, const char *, int);
413 int ssleep(const volatile void *, struct spinlock *, int, const char *, int)
415 int lksleep(const volatile void *, struct lock *, int, const char *, int)
417 int mtxsleep(const volatile void *, struct mtx *, int, const char *, int)
419 int zsleep(const volatile void *, struct lwkt_serialize *, int, const char *, int)
421 void tsleep_interlock(const volatile void *, int) __nonnull(1);
422 void tsleep_remove (struct thread *);
423 int lwkt_sleep (const char *, int);
425 void wakeup(const volatile void *chan) __nonnull(1);
426 void wakeup_one(const volatile void *chan) __nonnull(1);
427 void wakeup_mycpu(const volatile void *chan) __nonnull(1);
428 void wakeup_mycpu_one(const volatile void *chan) __nonnull(1);
429 void wakeup_oncpu(struct globaldata *gd, const volatile void *chan)
431 void wakeup_oncpu_one(struct globaldata *gd, const volatile void *chan)
433 void wakeup_domain(const volatile void *chan, int domain) __nonnull(1);
434 void wakeup_domain_one(const volatile void *chan, int domain) __nonnull(1);
435 void wakeup_start_delayed(void);
436 void wakeup_end_delayed(void);
439 * Common `cdev_t' stuff are declared here to avoid #include poisoning
444 dev_t devid_from_dev(cdev_t x);
445 cdev_t dev_from_devid(dev_t x, int b);
446 int uminor(dev_t dev);
447 int umajor(dev_t dev);
448 dev_t makeudev(int x, int y);
451 * Unit number allocation API. (kern/subr_unit.c)
454 struct unrhdr *new_unrhdr(int low, int high, struct lock *lock);
455 void delete_unrhdr(struct unrhdr *uh);
456 int alloc_unr(struct unrhdr *uh);
457 int alloc_unrl(struct unrhdr *uh);
458 void free_unr(struct unrhdr *uh, u_int item);
461 * Population count algorithm using SWAR approach
462 * - "SIMD Within A Register".
465 static __inline uint16_t
466 bitcount16(uint32_t x)
469 x = (x & 0x5555) + ((x & 0xaaaa) >> 1);
470 x = (x & 0x3333) + ((x & 0xcccc) >> 2);
471 x = (x + (x >> 4)) & 0x0f0f;
472 x = (x + (x >> 8)) & 0x00ff;
476 static __inline uint32_t
477 bitcount32(uint32_t x)
480 x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
481 x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
482 x = (x + (x >> 4)) & 0x0f0f0f0f;
484 x = (x + (x >> 16)) & 0x000000ff;
488 static __inline uint64_t
489 bitcount64(uint64_t x)
492 x = (x & 0x5555555555555555) + ((x & 0xaaaaaaaaaaaaaaaa) >> 1);
493 x = (x & 0x3333333333333333) + ((x & 0xcccccccccccccccc) >> 2);
494 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f;
497 x = (x + (x >> 32)) & 0x000000ff;
502 * Calculate (a * b) / c with a 128-bit intermediate computation to
505 static __inline uint64_t
506 muldivu64(uint64_t a, uint64_t b, uint64_t d)
510 t = (unsigned __int128)a * b;
511 if (t / d > 0xFFFFFFFFFFFFFFFFLU) {
512 kprintf("muldivu64: overflow %ld,%ld,%ld\n", a, b, d);
518 #endif /* !_SYS_SYSTM_H_ */