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