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 #define __assert_unreachable() \ 113 do { \ 114 panic("executing segment marked " \ 115 "as unreachable at %s:%d (%s)\n", \ 116 __FILE__, __LINE__, __func__); \ 117 } while (0) 118 #else 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() 124 #endif 125 126 /* 127 * Align variables. 128 */ 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") 133 134 /* 135 * General function declarations. 136 */ 137 138 struct intrframe; 139 struct spinlock; 140 struct lock; 141 struct mtx; 142 struct lwkt_serialize; 143 struct malloc_type; 144 struct proc; 145 struct timeval; 146 struct tty; 147 struct uio; 148 struct globaldata; 149 struct thread; 150 struct trapframe; 151 struct user; 152 struct vmspace; 153 struct savetls; 154 struct krate; 155 struct _jmp_buf; 156 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; 164 int nullop (void); 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); 174 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); 181 void cpu_boot (int); 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); 190 191 /* 192 * Old CRC32 API 193 */ 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); 197 198 /* 199 * Newer (fast) iscsi CRC32 APIs. 200 * 201 * NOTE: iscsi_crc32_ext() inverts ocrc input and output as expected, 202 * pass a seed of 0 for the equivalent of iscsi_crc32(). 203 * 204 * calculate_crc32c() does not invert crc32c input and output 205 * to maintain FreeBSD API compatibility. 206 */ 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); 211 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 **); 247 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); 252 253 /* 254 * NOTE: some functions commonly used by device drivers may be passed 255 * pointers to volatile storage, volatile set to avoid warnings. 256 * 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). 262 */ 263 #if 1 264 #define bcopy(from, to, len) \ 265 __builtin_memmove(__DEQUALIFY(void *, (to)), \ 266 __DEQUALIFY(void *, (from)), \ 267 (len)) 268 #define memcpy(to, from, len) \ 269 __builtin_memcpy(__DEQUALIFY(void *, (to)), \ 270 __DEQUALIFY(void *, (from)), \ 271 (len)) 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)), \ 277 (len)) 278 #define bzero(buf, len) \ 279 __builtin_memset(__DEQUALIFY(void *, (buf)), 0, (len)) 280 #else 281 void bcopy(volatile const void *from, volatile void *to, size_t len) 282 __nonnull(1, 2); 283 void *memcpy(void *to, const void *from, size_t len) 284 __nonnull(1, 2); 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); 288 #endif 289 void _bcopy(volatile const void *from, volatile void *to, size_t len) 290 __nonnull(1, 2); 291 void *_memcpy(void *to, const void *from, size_t len) 292 __nonnull(1, 2); 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); 297 298 long kreadmem64(const void *addr); 299 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) 306 __nonnull(1, 2); 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) 309 __nonnull(1, 2); 310 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); 323 324 void DELAY(int usec); 325 void DRIVERSLEEP(int usec); 326 327 void startprofclock (struct proc *); 328 void stopprofclock (struct proc *); 329 void setstatclockrate (int hzrate); 330 331 /* 332 * Kernel environment support functions and sundry. 333 */ 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; 345 346 #include <sys/libkern.h> 347 348 /* Initialize the world */ 349 void mi_startup (void); 350 void nchinit (void); 351 352 /* Finalize the world. */ 353 void shutdown_nice (int); 354 355 /* 356 * Kernel to clock driver interface. 357 */ 358 void inittodr (time_t base); 359 void resettodr (void); 360 void startrtclock (void); 361 362 /* Timeouts */ 363 typedef void timeout_t (void *); /* timeout function type */ 364 365 /* Interrupt management */ 366 367 /* 368 * For the alpha arch, some of these functions are static __inline, and 369 * the others should be. 370 */ 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); 382 void splz (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__ */ 388 389 /* 390 * Various callout lists. 391 */ 392 393 /* Exit callout list declarations. */ 394 typedef void (*exitlist_fn) (struct thread *td); 395 396 int at_exit (exitlist_fn function); 397 int rm_at_exit (exitlist_fn function); 398 399 /* Fork callout list declarations. */ 400 typedef void (*forklist_fn) (struct proc *parent, struct proc *child, 401 int flags); 402 403 int at_fork (forklist_fn function); 404 int rm_at_fork (forklist_fn function); 405 406 extern struct globaldata *panic_cpu_gd; 407 408 /* 409 * Common `proc' functions are declared here so that proc.h can be included 410 * less often. 411 */ 412 int tsleep(const volatile void *, int, const char *, int); 413 int ssleep(const volatile void *, struct spinlock *, int, const char *, int) 414 __nonnull(1); 415 int lksleep(const volatile void *, struct lock *, int, const char *, int) 416 __nonnull(1); 417 int mtxsleep(const volatile void *, struct mtx *, int, const char *, int) 418 __nonnull(1); 419 int zsleep(const volatile void *, struct lwkt_serialize *, int, const char *, int) 420 __nonnull(1); 421 void tsleep_interlock(const volatile void *, int) __nonnull(1); 422 void tsleep_remove (struct thread *); 423 int lwkt_sleep (const char *, int); 424 void tstop (void); 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) 430 __nonnull(2); 431 void wakeup_oncpu_one(struct globaldata *gd, const volatile void *chan) 432 __nonnull(2); 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); 437 438 /* 439 * Common `cdev_t' stuff are declared here to avoid #include poisoning 440 */ 441 442 int major(cdev_t x); 443 int minor(cdev_t x); 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); 449 450 /* 451 * Unit number allocation API. (kern/subr_unit.c) 452 */ 453 struct unrhdr; 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); 459 460 /* 461 * Population count algorithm using SWAR approach 462 * - "SIMD Within A Register". 463 */ 464 465 static __inline uint16_t 466 bitcount16(uint32_t x) 467 { 468 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; 473 return (x); 474 } 475 476 static __inline uint32_t 477 bitcount32(uint32_t x) 478 { 479 480 x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1); 481 x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2); 482 x = (x + (x >> 4)) & 0x0f0f0f0f; 483 x = (x + (x >> 8)); 484 x = (x + (x >> 16)) & 0x000000ff; 485 return (x); 486 } 487 488 static __inline uint64_t 489 bitcount64(uint64_t x) 490 { 491 492 x = (x & 0x5555555555555555) + ((x & 0xaaaaaaaaaaaaaaaa) >> 1); 493 x = (x & 0x3333333333333333) + ((x & 0xcccccccccccccccc) >> 2); 494 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f; 495 x = (x + (x >> 8)); 496 x = (x + (x >> 16)); 497 x = (x + (x >> 32)) & 0x000000ff; 498 return (x); 499 } 500 501 /* 502 * Calculate (a * b) / c with a 128-bit intermediate computation to 503 * avoid overflow. 504 */ 505 static __inline uint64_t 506 muldivu64(uint64_t a, uint64_t b, uint64_t d) 507 { 508 unsigned __int128 t; 509 510 t = (unsigned __int128)a * b; 511 if (t / d > 0xFFFFFFFFFFFFFFFFLU) { 512 kprintf("muldivu64: overflow %ld,%ld,%ld\n", a, b, d); 513 print_backtrace(-1); 514 } 515 return (t / d); 516 } 517 518 #endif /* !_SYS_SYSTM_H_ */ 519