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