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