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