systm.h revision 231949
11541Srgrimes/*- 21541Srgrimes * Copyright (c) 1982, 1988, 1991, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * (c) UNIX System Laboratories, Inc. 51541Srgrimes * All or some portions of this file are derived from material licensed 61541Srgrimes * to the University of California by American Telephone and Telegraph 71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 81541Srgrimes * the permission of UNIX System Laboratories, Inc. 91541Srgrimes * 101541Srgrimes * Redistribution and use in source and binary forms, with or without 111541Srgrimes * modification, are permitted provided that the following conditions 121541Srgrimes * are met: 131541Srgrimes * 1. Redistributions of source code must retain the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer. 151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 161541Srgrimes * notice, this list of conditions and the following disclaimer in the 171541Srgrimes * documentation and/or other materials provided with the distribution. 181541Srgrimes * 4. Neither the name of the University nor the names of its contributors 191541Srgrimes * may be used to endorse or promote products derived from this software 201541Srgrimes * without specific prior written permission. 211541Srgrimes * 221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321541Srgrimes * SUCH DAMAGE. 331541Srgrimes * 3414508Shsu * @(#)systm.h 8.7 (Berkeley) 3/29/95 3550477Speter * $FreeBSD: head/sys/sys/systm.h 231949 2012-02-21 01:05:12Z kib $ 361541Srgrimes */ 371541Srgrimes 382165Spaul#ifndef _SYS_SYSTM_H_ 392865Sbde#define _SYS_SYSTM_H_ 402165Spaul 4149043Salc#include <machine/atomic.h> 421549Srgrimes#include <machine/cpufunc.h> 4329683Sgibbs#include <sys/callout.h> 44120610Smux#include <sys/cdefs.h> 4594936Smux#include <sys/queue.h> 46112367Sphk#include <sys/stdint.h> /* for people using printf mainly */ 471549Srgrimes 487090Sbdeextern int cold; /* nonzero if we are doing a cold boot */ 49214004Smarcelextern int rebooting; /* kern_reboot() has been called. */ 501541Srgrimesextern const char *panicstr; /* panic message */ 511541Srgrimesextern char version[]; /* system version */ 521541Srgrimesextern char copyright[]; /* system copyright */ 53103083Speterextern int kstack_pages; /* number of kernel stack pages */ 541541Srgrimes 55197316Salcextern u_long pagesizes[]; /* supported page sizes */ 56102600Speterextern long physmem; /* physical memory */ 57142834Swesextern long realmem; /* 'real' memory */ 581541Srgrimes 5936809Sbdeextern char *rootdevnames[2]; /* names of possible root devices */ 601541Srgrimes 611541Srgrimesextern int boothowto; /* reboot flags, from console subsystem */ 6215113Sbdeextern int bootverbose; /* nonzero to print verbose messages */ 631541Srgrimes 6480418Speterextern int maxusers; /* system tune hint */ 65202143Sbrooksextern int ngroups_max; /* max # of supplemental groups */ 66204420Salcextern int vm_guest; /* Running as virtual machine guest? */ 6780418Speter 68204633Sivoras/* 69204633Sivoras * Detected virtual machine guest types. The intention is to expand 70204611Sivoras * and/or add to the VM_GUEST_VM type if specific VM functionality is 71204633Sivoras * ever implemented (e.g. vendor-specific paravirtualization features). 72204633Sivoras */ 73204420Salcenum VM_GUEST { VM_GUEST_NO = 0, VM_GUEST_VM, VM_GUEST_XEN }; 74204420Salc 7542453Seivind#ifdef INVARIANTS /* The option is always available */ 76120610Smux#define KASSERT(exp,msg) do { \ 77120610Smux if (__predict_false(!(exp))) \ 78120610Smux panic msg; \ 79120610Smux} while (0) 80141458Sphk#define VNASSERT(exp, vp, msg) do { \ 81141458Sphk if (__predict_false(!(exp))) { \ 82182909Sjhb vn_printf(vp, "VNASSERT failed\n"); \ 83141458Sphk panic msg; \ 84141458Sphk } \ 85141458Sphk} while (0) 8642408Seivind#else 87165547Skmacy#define KASSERT(exp,msg) do { \ 88165547Skmacy} while (0) 89165547Skmacy 90165547Skmacy#define VNASSERT(exp, vp, msg) do { \ 91165547Skmacy} while (0) 9242408Seivind#endif 9342408Seivind 94228478Sed#ifndef CTASSERT /* Allow lint to override */ 95228478Sed#define CTASSERT(x) _Static_assert(x, "compile-time assertion failed") 96109316Sphk#endif 9793600Sjake 98196334Sattilio/* 99196334Sattilio * Assert that a pointer can be loaded from memory atomically. 100196334Sattilio * 101196334Sattilio * This assertion enforces stronger alignment than necessary. For example, 102196334Sattilio * on some architectures, atomicity for unaligned loads will depend on 103196334Sattilio * whether or not the load spans multiple cache lines. 104196334Sattilio */ 105196334Sattilio#define ASSERT_ATOMIC_LOAD_PTR(var, msg) \ 106196334Sattilio KASSERT(sizeof(var) == sizeof(void *) && \ 107196334Sattilio ((uintptr_t)&(var) & (sizeof(void *) - 1)) == 0, msg) 108196226Sbz 1091541Srgrimes/* 110228424Savg * If we have already panic'd and this is the thread that called 111228424Savg * panic(), then don't block on any mutexes but silently succeed. 112228424Savg * Otherwise, the kernel will deadlock since the scheduler isn't 113228424Savg * going to run the thread that holds any lock we need. 114228424Savg */ 115230643Sattilio#define SCHEDULER_STOPPED() __predict_false(curthread->td_stopsched) 116228424Savg 117228424Savg/* 11883595Speter * XXX the hints declarations are even more misplaced than most declarations 11983595Speter * in this file, since they are needed in one file (per arch) and only used 12083595Speter * in two files. 12183595Speter * XXX most of these variables should be const. 12283595Speter */ 123174253Skibextern int osreldate; 12483595Speterextern int envmode; 12583595Speterextern int hintmode; /* 0 = off. 1 = config, 2 = fallback */ 12694936Smuxextern int dynamic_kenv; 127160217Sscottlextern struct mtx kenv_lock; 12883595Speterextern char *kern_envp; 12983595Speterextern char static_env[]; 13083595Speterextern char static_hints[]; /* by config for now */ 13183595Speter 13294936Smuxextern char **kenvp; 13394936Smux 134221853Smdfextern const void *zero_region; /* address space maps to a zeroed page */ 135221853Smdf 136231949Skibextern int iosize_max_clamp; 137231949Skib#define IOSIZE_MAX (iosize_max_clamp ? INT_MAX : SSIZE_MAX) 138231949Skib 13983595Speter/* 1401541Srgrimes * General function declarations. 1411541Srgrimes */ 14230282Sphk 143183982Sbzstruct inpcb; 144167387Sjhbstruct lock_object; 14530282Sphkstruct malloc_type; 14665708Sjakestruct mtx; 14733777Sbdestruct proc; 14892976Srwatsonstruct socket; 14983366Sjulianstruct thread; 15033777Sbdestruct tty; 15167893Sphkstruct ucred; 15233777Sbdestruct uio; 15379418Sjulianstruct _jmp_buf; 154210131Smavstruct trapframe; 15530282Sphk 15692719Salfredint setjmp(struct _jmp_buf *); 15792719Salfredvoid longjmp(struct _jmp_buf *, int) __dead2; 15892719Salfredint dumpstatus(vm_offset_t addr, off_t count); 15992719Salfredint nullop(void); 16092719Salfredint eopnotsupp(void); 16192719Salfredint ureadc(int, struct uio *); 16299098Siedowsevoid hashdestroy(void *, struct malloc_type *, u_long); 163220987Skibvoid *hashinit(int count, struct malloc_type *type, u_long *hashmask); 164166022Srrsvoid *hashinit_flags(int count, struct malloc_type *type, 165166022Srrs u_long *hashmask, int flags); 166166022Srrs#define HASH_NOWAIT 0x00000001 167166022Srrs#define HASH_WAITOK 0x00000002 168166022Srrs 16992719Salfredvoid *phashinit(int count, struct malloc_type *type, u_long *nentries); 170136836Sphkvoid g_waitidle(void); 1711541Srgrimes 172130164Sphkvoid panic(const char *, ...) __dead2 __printflike(1, 2); 17390503Sbde 17492719Salfredvoid cpu_boot(int); 175192323Smarcelvoid cpu_flush_dcache(void *, size_t); 17692719Salfredvoid cpu_rootconf(void); 17792719Salfredvoid critical_enter(void); 17892719Salfredvoid critical_exit(void); 17992719Salfredvoid init_param1(void); 180102600Spetervoid init_param2(long physpages); 181202050Simpvoid init_static_kenv(char *, size_t); 18292719Salfredvoid tablefull(const char *); 18392719Salfredint kvprintf(char const *, void (*)(int, void*), void *, int, 184102227Smike __va_list) __printflike(1, 0); 18592719Salfredvoid log(int, const char *, ...) __printflike(2, 3); 18692719Salfredvoid log_console(struct uio *); 18792719Salfredint printf(const char *, ...) __printflike(1, 2); 18892719Salfredint snprintf(char *, size_t, const char *, ...) __printflike(3, 4); 18992719Salfredint sprintf(char *buf, const char *, ...) __printflike(2, 3); 19092719Salfredint uprintf(const char *, ...) __printflike(1, 2); 191102227Smikeint vprintf(const char *, __va_list) __printflike(1, 0); 192102227Smikeint vsnprintf(char *, size_t, const char *, __va_list) __printflike(3, 0); 193137098Sdesint vsnrprintf(char *, size_t, int, const char *, __va_list) __printflike(4, 0); 194102227Smikeint vsprintf(char *buf, const char *, __va_list) __printflike(2, 0); 19592719Salfredint ttyprintf(struct tty *, const char *, ...) __printflike(2, 3); 196117837Sphkint sscanf(const char *, char const *, ...) __nonnull(1) __nonnull(2); 197117837Sphkint vsscanf(const char *, char const *, __va_list) __nonnull(1) __nonnull(2); 198117860Sphklong strtol(const char *, char **, int) __nonnull(1); 199117860Sphku_long strtoul(const char *, char **, int) __nonnull(1); 200117837Sphkquad_t strtoq(const char *, char **, int) __nonnull(1); 201117837Sphku_quad_t strtouq(const char *, char **, int) __nonnull(1); 20292719Salfredvoid tprintf(struct proc *p, int pri, const char *, ...) __printflike(3, 4); 203144706Sphkvoid hexdump(const void *ptr, int length, const char *hdr, int flags); 204123215Sscottl#define HD_COLUMN_MASK 0xff 205123215Sscottl#define HD_DELIM_MASK 0xff00 206123215Sscottl#define HD_OMIT_COUNT (1 << 16) 207123215Sscottl#define HD_OMIT_HEX (1 << 17) 208123215Sscottl#define HD_OMIT_CHARS (1 << 18) 2091541Srgrimes 210113090Sdes#define ovbcopy(f, t, l) bcopy((f), (t), (l)) 211117837Sphkvoid bcopy(const void *from, void *to, size_t len) __nonnull(1) __nonnull(2); 212117837Sphkvoid bzero(void *buf, size_t len) __nonnull(1); 2131541Srgrimes 214117837Sphkvoid *memcpy(void *to, const void *from, size_t len) __nonnull(1) __nonnull(2); 215189170Sedvoid *memmove(void *dest, const void *src, size_t n) __nonnull(1) __nonnull(2); 2168215Sdg 217123852Salfredint copystr(const void * __restrict kfaddr, void * __restrict kdaddr, 218123852Salfred size_t len, size_t * __restrict lencopied) 219123852Salfred __nonnull(1) __nonnull(2); 220123852Salfredint copyinstr(const void * __restrict udaddr, void * __restrict kaddr, 221123852Salfred size_t len, size_t * __restrict lencopied) 222123852Salfred __nonnull(1) __nonnull(2); 223123852Salfredint copyin(const void * __restrict udaddr, void * __restrict kaddr, 224123852Salfred size_t len) __nonnull(1) __nonnull(2); 225223889Skibint copyin_nofault(const void * __restrict udaddr, void * __restrict kaddr, 226223889Skib size_t len) __nonnull(1) __nonnull(2); 227123852Salfredint copyout(const void * __restrict kaddr, void * __restrict udaddr, 228123852Salfred size_t len) __nonnull(1) __nonnull(2); 229223889Skibint copyout_nofault(const void * __restrict kaddr, void * __restrict udaddr, 230223889Skib size_t len) __nonnull(1) __nonnull(2); 2311541Srgrimes 23292719Salfredint fubyte(const void *base); 23398482Speterlong fuword(const void *base); 23498482Speterint fuword16(void *base); 23597307Sdfrint32_t fuword32(const void *base); 23697307Sdfrint64_t fuword64(const void *base); 23798482Speterint subyte(void *base, int byte); 23892719Salfredint suword(void *base, long word); 23998482Speterint suword16(void *base, int word); 24097307Sdfrint suword32(void *base, int32_t word); 24197307Sdfrint suword64(void *base, int64_t word); 242163449Sdavidxuuint32_t casuword32(volatile uint32_t *base, uint32_t oldval, uint32_t newval); 243163449Sdavidxuu_long casuword(volatile u_long *p, u_long oldval, u_long newval); 2441541Srgrimes 24592719Salfredvoid realitexpire(void *); 2461541Srgrimes 247177642Sphkint sysbeep(int hertz, int period); 248177642Sphk 249153666Sjhbvoid hardclock(int usermode, uintfptr_t pc); 250212541Smavvoid hardclock_anycpu(int cnt, int usermode); 251153666Sjhbvoid hardclock_cpu(int usermode); 252212541Smavvoid hardclock_sync(int cpu); 25392719Salfredvoid softclock(void *); 254153666Sjhbvoid statclock(int usermode); 255153666Sjhbvoid profclock(int usermode, uintfptr_t pc); 2561541Srgrimes 257212541Smavint hardclockintr(void); 258210131Smav 25992719Salfredvoid startprofclock(struct proc *); 26092719Salfredvoid stopprofclock(struct proc *); 261110296Sjakevoid cpu_startprofclock(void); 262110296Sjakevoid cpu_stopprofclock(void); 263212541Smavvoid cpu_idleclock(void); 264212541Smavvoid cpu_activeclock(void); 265223426Sjkimextern int cpu_can_deep_sleep; 266212541Smavextern int cpu_disable_deep_sleep; 2671541Srgrimes 26892719Salfredint cr_cansee(struct ucred *u1, struct ucred *u2); 26992976Srwatsonint cr_canseesocket(struct ucred *cred, struct socket *so); 270183982Sbzint cr_canseeinpcb(struct ucred *cred, struct inpcb *inp); 27167893Sphk 27292719Salfredchar *getenv(const char *name); 27394936Smuxvoid freeenv(char *env); 27492719Salfredint getenv_int(const char *name, int *data); 275172612Sdesint getenv_uint(const char *name, unsigned int *data); 276172612Sdesint getenv_long(const char *name, long *data); 277172612Sdesint getenv_ulong(const char *name, unsigned long *data); 27892719Salfredint getenv_string(const char *name, char *data, int size); 27992719Salfredint getenv_quad(const char *name, quad_t *data); 28094959Smuxint setenv(const char *name, const char *value); 28194936Smuxint unsetenv(const char *name); 28294936Smuxint testenv(const char *name); 28340096Smsmith 284155534Sphktypedef uint64_t (cpu_tick_f)(void); 285155534Sphkvoid set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var); 286155534Sphkextern cpu_tick_f *cpu_ticks; 287155534Sphkuint64_t cpu_tickrate(void); 288155534Sphkuint64_t cputick2usec(uint64_t tick); 289155444Sphk 290137098Sdes#ifdef APM_FIXUP_CALLTODO 29183595Speterstruct timeval; 292137098Sdesvoid adjust_timeout_calltodo(struct timeval *time_change); 293137098Sdes#endif /* APM_FIXUP_CALLTODO */ 29431960Snate 2957109Sphk#include <sys/libkern.h> 2962112Swollman 2972112Swollman/* Initialize the world */ 29892719Salfredvoid consinit(void); 29992719Salfredvoid cpu_initclocks(void); 300209371Smavvoid cpu_initclocks_bsp(void); 301209371Smavvoid cpu_initclocks_ap(void); 30292719Salfredvoid usrinfoinit(void); 3032112Swollman 304167111Sthomas/* Finalize the world */ 305214004Smarcelvoid kern_reboot(int) __dead2; 30692719Salfredvoid shutdown_nice(int); 3077090Sbde 3082112Swollman/* Timeouts */ 30992719Salfredtypedef void timeout_t(void *); /* timeout function type */ 31029683Sgibbs#define CALLOUT_HANDLE_INITIALIZER(handle) \ 31129683Sgibbs { NULL } 3122112Swollman 31392719Salfredvoid callout_handle_init(struct callout_handle *); 31492719Salfredstruct callout_handle timeout(timeout_t *, void *, int); 31592719Salfredvoid untimeout(timeout_t *, void *, struct callout_handle); 31692719Salfredcaddr_t kern_timeout_callwheel_alloc(caddr_t v); 31792719Salfredvoid kern_timeout_callwheel_init(void); 3182165Spaul 319167111Sthomas/* Stubs for obsolete functions that used to be for interrupt management */ 32071240Speterstatic __inline void spl0(void) { return; } 32171240Speterstatic __inline intrmask_t splbio(void) { return 0; } 32271240Speterstatic __inline intrmask_t splcam(void) { return 0; } 32371240Speterstatic __inline intrmask_t splclock(void) { return 0; } 32471240Speterstatic __inline intrmask_t splhigh(void) { return 0; } 32571240Speterstatic __inline intrmask_t splimp(void) { return 0; } 32671240Speterstatic __inline intrmask_t splnet(void) { return 0; } 32771240Speterstatic __inline intrmask_t splsoftcam(void) { return 0; } 32871240Speterstatic __inline intrmask_t splsoftclock(void) { return 0; } 32971240Speterstatic __inline intrmask_t splsofttty(void) { return 0; } 33071240Speterstatic __inline intrmask_t splsoftvm(void) { return 0; } 33171240Speterstatic __inline intrmask_t splsofttq(void) { return 0; } 33271240Speterstatic __inline intrmask_t splstatclock(void) { return 0; } 33371240Speterstatic __inline intrmask_t spltty(void) { return 0; } 33471240Speterstatic __inline intrmask_t splvm(void) { return 0; } 335100073Smarkmstatic __inline void splx(intrmask_t ipl __unused) { return; } 33626312Speter 337137098Sdes/* 33818884Sbde * Common `proc' functions are declared here so that proc.h can be included 33918884Sbde * less often. 34018884Sbde */ 341167387Sjhbint _sleep(void *chan, struct lock_object *lock, int pri, const char *wmesg, 342167387Sjhb int timo) __nonnull(1); 343167387Sjhb#define msleep(chan, mtx, pri, wmesg, timo) \ 344167787Sjhb _sleep((chan), &(mtx)->lock_object, (pri), (wmesg), (timo)) 345167387Sjhbint msleep_spin(void *chan, struct mtx *mtx, const char *wmesg, int timo) 346167387Sjhb __nonnull(1); 347166908Sjhbint pause(const char *wmesg, int timo); 348167387Sjhb#define tsleep(chan, pri, wmesg, timo) \ 349167387Sjhb _sleep((chan), NULL, (pri), (wmesg), (timo)) 350117837Sphkvoid wakeup(void *chan) __nonnull(1); 351117837Sphkvoid wakeup_one(void *chan) __nonnull(1); 35218884Sbde 35347028Sphk/* 354130585Sphk * Common `struct cdev *' stuff are declared here to avoid #include poisoning 35547028Sphk */ 35647028Sphk 357138509Sphkstruct cdev; 358130640Sphkdev_t dev2udev(struct cdev *x); 359143622Sphkconst char *devtoname(struct cdev *cdev); 36067158Sphk 361189450Skibint poll_no_poll(int events); 362189450Skib 36367158Sphk/* XXX: Should be void nanodelay(u_int nsec); */ 36492719Salfredvoid DELAY(int usec); 36567158Sphk 366145250Sphk/* Root mount holdback API */ 367145250Sphkstruct root_hold_token; 368145250Sphk 369190878Sthompsastruct root_hold_token *root_mount_hold(const char *identifier); 370145250Sphkvoid root_mount_rel(struct root_hold_token *h); 371168300Spjdvoid root_mount_wait(void); 372168506Spjdint root_mounted(void); 373145250Sphk 374145250Sphk 375135956Sphk/* 376135956Sphk * Unit number allocation API. (kern/subr_unit.c) 377135956Sphk */ 378135956Sphkstruct unrhdr; 379143283Sphkstruct unrhdr *new_unrhdr(int low, int high, struct mtx *mutex); 380136945Sphkvoid delete_unrhdr(struct unrhdr *uh); 381171202Skibvoid clean_unrhdr(struct unrhdr *uh); 382171202Skibvoid clean_unrhdrl(struct unrhdr *uh); 383143283Sphkint alloc_unr(struct unrhdr *uh); 384209710Sjhint alloc_unr_specific(struct unrhdr *uh, u_int item); 385143283Sphkint alloc_unrl(struct unrhdr *uh); 386135956Sphkvoid free_unr(struct unrhdr *uh, u_int item); 387135956Sphk 388149300Spjd/* 389221741Savg * Population count algorithm using SWAR approach 390221741Savg * - "SIMD Within A Register". 391149300Spjd */ 392149300Spjdstatic __inline uint32_t 393149300Spjdbitcount32(uint32_t x) 394149300Spjd{ 395149300Spjd 396149300Spjd x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1); 397149300Spjd x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2); 398166698Scperciva x = (x + (x >> 4)) & 0x0f0f0f0f; 399166698Scperciva x = (x + (x >> 8)); 400166698Scperciva x = (x + (x >> 16)) & 0x000000ff; 401149300Spjd return (x); 402149300Spjd} 403149300Spjd 404223884Skibstatic __inline uint16_t 405223884Skibbitcount16(uint32_t x) 406223884Skib{ 407223884Skib 408223884Skib x = (x & 0x5555) + ((x & 0xaaaa) >> 1); 409223884Skib x = (x & 0x3333) + ((x & 0xcccc) >> 2); 410223884Skib x = (x + (x >> 4)) & 0x0f0f; 411223884Skib x = (x + (x >> 8)) & 0x00ff; 412223884Skib return (x); 413223884Skib} 414223884Skib 4152865Sbde#endif /* !_SYS_SYSTM_H_ */ 416