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