8f66f6f04f7a750d1bd719b45bd610358419a3b8
[dragonfly.git] / sys / sys / globaldata.h
1 /*
2  * Copyright (c) 2003-2011 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * Copyright (c) Peter Wemm <peter@netplex.com.au> All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57
58 #ifndef _SYS_GLOBALDATA_H_
59 #define _SYS_GLOBALDATA_H_
60
61 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
62
63 #ifndef _SYS_STDINT_H_
64 #include <sys/stdint.h>         /* __int types */
65 #endif
66 #ifndef _SYS_TIME_H_
67 #include <sys/time.h>           /* struct timeval */
68 #endif
69 #ifndef _SYS_VMMETER_H_
70 #include <sys/vmmeter.h>        /* struct vmmeter, pcpu vmstats adj */
71 #endif
72 #ifndef _SYS_THREAD_H_
73 #include <sys/thread.h>         /* struct thread */
74 #endif
75 #ifndef _SYS_SLABALLOC_H_
76 #include <sys/slaballoc.h>      /* SLGlobalData */
77 #endif
78 #ifndef _SYS_SYSTIMER_H_
79 #include <sys/systimer.h>       /* fine-grained system timers */
80 #endif
81 #ifndef _SYS_NCHSTATS_H_
82 #include <sys/nchstats.h>
83 #endif
84 #ifndef _SYS_SYSID_H_
85 #include <sys/sysid.h>          /* sysid_t */
86 #endif
87 #ifndef _SYS_CALLOUT_H_
88 #include <sys/callout.h>
89 #endif
90 #ifndef _SYS_INDEFINITE_H_
91 #include <sys/indefinite.h>
92 #endif
93 #ifndef _SYS_LOCK_H_
94 #include <sys/lock.h>
95 #endif
96
97 /*
98  * This structure maps out the global data that needs to be kept on a
99  * per-cpu basis.  genassym uses this to generate offsets for the assembler
100  * code.  The machine-dependant portions of this file can be found in
101  * <machine/globaldata.h>, but only MD code should retrieve it.
102  *
103  * The SMP parts are setup in pmap.c and locore.s for the BSP, and
104  * mp_machdep.c sets up the data for the AP's to "see" when they awake.
105  * The reason for doing it via a struct is so that an array of pointers
106  * to each CPU's data can be set up for things like "check curproc on all
107  * other processors"
108  *
109  * NOTE! this structure needs to remain compatible between module accessors
110  * and the kernel, so we can't throw in lots of #ifdef's.
111  *
112  * gd_reqflags serves serveral purposes, but it is primarily an interrupt
113  * rollup flag used by the task switcher and spl mechanisms to decide that
114  * further checks are necessary.  Interrupts are typically managed on a
115  * per-processor basis at least until you leave a critical section, but
116  * may then be scheduled to other cpus.
117  *
118  * gd_vme_avail and gd_vme_base cache free vm_map_entry structures for use
119  * in various vm_map related operations.  gd_vme_avail is *NOT* a count of
120  * the number of structures in the cache but is instead a count of the number
121  * of unreserved structures in the cache.  See vm_map_entry_reserve().
122  */
123
124 struct sysmsg;
125 struct tslpque;
126 struct privatespace;
127 struct vm_map_entry;
128 struct spinlock;
129 struct pipe;
130
131 struct globaldata {
132         struct privatespace *gd_prvspace;       /* self-reference */
133         struct thread   *gd_curthread;
134         struct thread   *gd_freetd;             /* cache one free td */
135         __uint32_t      gd_reqflags;            /* (see note above) */
136         long            gd_flags;
137         lwkt_queue      gd_tdallq;              /* all threads */
138         lwkt_queue      gd_tdrunq;              /* runnable threads */
139         __uint32_t      gd_cpuid;
140         cpumask_t       gd_cpumask;             /* CPUMASK_ASSBIT(cpuid) */
141         cpumask_t       gd_other_cpus;          /* mask of 'other' cpus */
142         union {
143                 struct timeval  gd_stattv;
144                 sysclock_t      gd_statcv;
145         } statint;
146         int             gd_intr_nesting_level;  /* hard code, intrs, ipis */
147         struct vmmeter  gd_cnt;
148         struct vmtotal  gd_vmtotal;
149         cpumask_t       gd_ipimask;             /* pending ipis from cpus */
150         struct lwkt_ipiq *gd_ipiq;              /* array[ncpu] of ipiq's */
151         struct lwkt_ipiq gd_cpusyncq;           /* ipiq for cpu synchro */
152         u_int           gd_npoll;               /* ipiq synchronization */
153         int             gd_tdrunqcount;
154         struct thread   gd_unused02B;
155         struct thread   gd_idlethread;
156         SLGlobalData    gd_slab;                /* slab allocator */
157         int             gd_trap_nesting_level;  /* track traps */
158         int             gd_vme_avail;           /* vm_map_entry reservation */
159         struct vm_map_entry *gd_vme_base;       /* vm_map_entry reservation */
160         struct systimerq gd_systimerq;          /* per-cpu system timers */
161         int             gd_syst_nest;
162         struct systimer gd_hardclock;           /* scheduler periodic */
163         struct systimer gd_statclock;           /* statistics periodic */
164         struct systimer gd_schedclock;          /* scheduler periodic */
165         volatile __uint32_t gd_time_seconds;    /* uptime in seconds */
166         volatile sysclock_t gd_cpuclock_base;   /* cpuclock relative base */
167
168         struct pipe     *gd_pipeq;              /* cache pipe structures */
169         struct nchstats *gd_nchstats;           /* namecache effectiveness */
170         int             gd_pipeqcount;          /* number of structures */
171         sysid_t         gd_sysid_alloc;         /* allocate unique sysid */
172
173         struct tslpque  *gd_tsleep_hash;        /* tsleep/wakeup support */
174         long            gd_processing_ipiq;
175         int             gd_spinlocks;           /* Exclusive spinlocks held */
176         struct systimer *gd_systimer_inprog;    /* in-progress systimer */
177         int             gd_timer_running;
178         u_int           gd_idle_repeat;         /* repeated switches to idle */
179         int             gd_quick_color;         /* page-coloring helper */
180         int             gd_cachedvnodes;        /* accum across all cpus */
181         int             gd_rand_incr;           /* random pcpu incrementor */
182         int             gd_activevnodes;        /* accum across all cpus */
183         int             gd_inactivevnodes;      /* accum across all cpus */
184         int             gd_ireserved[2];
185         const char      *gd_infomsg;            /* debugging */
186         struct lwkt_tokref gd_handoff;          /* hand-off tokref */
187         void            *gd_delayed_wakeup[2];
188         void            *gd_sample_pc;          /* sample program ctr/tr */
189         void            *gd_reserved_pcpu_mmap; /* future */
190         uint64_t        gd_forkid;              /* per-cpu unique inc ncpus */
191         void            *gd_sample_sp;          /* sample stack pointer */
192         uint64_t        gd_cpumask_simple;
193         uint64_t        gd_cpumask_offset;
194         struct vmstats  gd_vmstats;             /* pcpu local copy of vmstats */
195         struct vmstats  gd_vmstats_adj;         /* pcpu adj for vmstats */
196         struct callout  gd_loadav_callout;      /* loadavg calc */
197         struct callout  gd_schedcpu_callout;    /* scheduler/stats */
198         indefinite_info_t gd_indefinite;        /* scheduler cpu-bound */
199         uint32_t        gd_loadav_nrunnable;    /* pcpu lwps nrunnable */
200         uint32_t        gd_reserved32[1];
201         struct lock     gd_sysctllock;          /* sysctl topology lock */
202         void            *gd_preserved[4];       /* future fields */
203         /* extended by <machine/globaldata.h> */
204 };
205
206 typedef struct globaldata *globaldata_t;
207
208 #define RQB_IPIQ                0       /* 0001 */
209 #define RQB_INTPEND             1       /* 0002 */
210 #define RQB_AST_OWEUPC          2       /* 0004 */
211 #define RQB_AST_SIGNAL          3       /* 0008 */
212 #define RQB_AST_USER_RESCHED    4       /* 0010 */
213 #define RQB_AST_LWKT_RESCHED    5       /* 0020 */
214 #define RQB_UNUSED6             6       /* 0040 */
215 #define RQB_TIMER               7       /* 0080 */
216 #define RQB_RUNNING             8       /* 0100 */
217 #define RQB_SPINNING            9       /* 0200 */
218 #define RQB_QUICKRET            10      /* 0400 */
219 #define RQB_KQUEUE              11      /* 0800 (only used by vkernel) */
220
221 #define RQF_IPIQ                (1 << RQB_IPIQ)
222 #define RQF_INTPEND             (1 << RQB_INTPEND)
223 #define RQF_TIMER               (1 << RQB_TIMER)
224 #define RQF_AST_OWEUPC          (1 << RQB_AST_OWEUPC)
225 #define RQF_AST_SIGNAL          (1 << RQB_AST_SIGNAL)
226 #define RQF_AST_USER_RESCHED    (1 << RQB_AST_USER_RESCHED)
227 #define RQF_AST_LWKT_RESCHED    (1 << RQB_AST_LWKT_RESCHED)
228 #define RQF_RUNNING             (1 << RQB_RUNNING)
229 #define RQF_SPINNING            (1 << RQB_SPINNING)
230 #define RQF_QUICKRET            (1 << RQB_QUICKRET)
231 #define RQF_KQUEUE              (1 << RQB_KQUEUE)
232
233 #define RQF_AST_MASK            (RQF_AST_OWEUPC|RQF_AST_SIGNAL|\
234                                 RQF_AST_USER_RESCHED|RQF_AST_LWKT_RESCHED)
235 #define RQF_IDLECHECK_MASK      (RQF_IPIQ|RQF_INTPEND|RQF_TIMER|RQF_KQUEUE)
236 #define RQF_IDLECHECK_WK_MASK   (RQF_IDLECHECK_MASK|RQF_AST_LWKT_RESCHED)
237
238 /*
239  * globaldata flags
240  */
241 #define GDF_KPRINTF             0x0001  /* kprintf() reentrancy */
242 #define GDF_VIRTUSER            0x0002  /* used by vmm & vkernel */
243
244 #endif
245
246 /*
247  * MANUAL DEBUG CODE FOR DEBUGGING LOCKUPS
248  */
249 #ifdef _KERNEL
250
251 #if 0
252
253 #define DEBUG_PUSH_INFO(msg)                            \
254         const char *save_infomsg;                       \
255         save_infomsg = mycpu->gd_infomsg;               \
256         mycpu->gd_infomsg = msg                         \
257
258 #define DEBUG_POP_INFO()        mycpu->gd_infomsg = save_infomsg
259
260 #else
261
262 #define DEBUG_PUSH_INFO(msg)
263 #define DEBUG_POP_INFO()
264
265 #endif
266
267 #endif
268
269 #ifdef _KERNEL
270 struct globaldata *globaldata_find(int cpu);
271 int is_globaldata_space(vm_offset_t saddr, vm_offset_t eaddr);
272 #endif
273
274 #endif