]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/cpu_data.h
8ad34142ad42131c002a899b033526e8dd47436e
[apple/xnu.git] / osfmk / i386 / cpu_data.h
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 *
25 */
26
27 #ifndef I386_CPU_DATA
28 #define I386_CPU_DATA
29
30 #include <mach_assert.h>
31
32 #if defined(__GNUC__)
33
34 #include <kern/assert.h>
35 #include <kern/kern_types.h>
36 #include <kern/processor.h>
37 #include <kern/pms.h>
38 #include <pexpert/pexpert.h>
39 #include <mach/i386/thread_status.h>
40 #include <i386/rtclock.h>
41 #include <i386/pmCPU.h>
42
43 /*
44 * Data structures referenced (anonymously) from per-cpu data:
45 */
46 struct cpu_core;
47 struct cpu_cons_buffer;
48 struct cpu_desc_table;
49
50
51 /*
52 * Data structures embedded in per-cpu data:
53 */
54 typedef struct rtclock_timer {
55 uint64_t deadline;
56 boolean_t is_set;
57 boolean_t has_expired;
58 } rtclock_timer_t;
59
60 typedef struct {
61 uint64_t tsc_base; /* timestamp */
62 uint64_t ns_base; /* nanoseconds */
63 uint32_t scale; /* tsc -> nanosec multiplier */
64 uint32_t shift; /* tsc -> nanosec shift/div */
65 } rtc_nanotime_t;
66
67 typedef struct {
68 struct i386_tss *cdi_ktss;
69 #if MACH_KDB
70 struct i386_tss *cdi_dbtss;
71 #endif /* MACH_KDB */
72 struct fake_descriptor *cdi_gdt;
73 struct fake_descriptor *cdi_idt;
74 struct fake_descriptor *cdi_ldt;
75 vm_offset_t cdi_sstk;
76 } cpu_desc_index_t;
77
78 typedef enum {
79 TASK_MAP_32BIT, /* 32-bit, compatibility mode */
80 TASK_MAP_64BIT, /* 64-bit, separate address space */
81 TASK_MAP_64BIT_SHARED /* 64-bit, kernel-shared addr space */
82 } task_map_t;
83
84 /*
85 * This structure is used on entry into the (uber-)kernel on syscall from
86 * a 64-bit user. It contains the address of the machine state save area
87 * for the current thread and a temporary place to save the user's rsp
88 * before loading this address into rsp.
89 */
90 typedef struct {
91 addr64_t cu_isf; /* thread->pcb->iss.isf */
92 uint64_t cu_tmp; /* temporary scratch */
93 addr64_t cu_user_gs_base;
94 } cpu_uber_t;
95
96 /*
97 * Per-cpu data.
98 *
99 * Each processor has a per-cpu data area which is dereferenced through the
100 * current_cpu_datap() macro. For speed, the %gs segment is based here, and
101 * using this, inlines provides single-instruction access to frequently used
102 * members - such as get_cpu_number()/cpu_number(), and get_active_thread()/
103 * current_thread().
104 *
105 * Cpu data owned by another processor can be accessed using the
106 * cpu_datap(cpu_number) macro which uses the cpu_data_ptr[] array of per-cpu
107 * pointers.
108 */
109 typedef struct cpu_data
110 {
111 struct cpu_data *cpu_this; /* pointer to myself */
112 thread_t cpu_active_thread;
113 void *cpu_int_state; /* interrupt state */
114 vm_offset_t cpu_active_stack; /* kernel stack base */
115 vm_offset_t cpu_kernel_stack; /* kernel stack top */
116 vm_offset_t cpu_int_stack_top;
117 int cpu_preemption_level;
118 int cpu_simple_lock_count;
119 int cpu_interrupt_level;
120 int cpu_number; /* Logical CPU */
121 int cpu_phys_number; /* Physical CPU */
122 cpu_id_t cpu_id; /* Platform Expert */
123 int cpu_signals; /* IPI events */
124 int cpu_mcount_off; /* mcount recursion */
125 ast_t cpu_pending_ast;
126 int cpu_type;
127 int cpu_subtype;
128 int cpu_threadtype;
129 int cpu_running;
130 uint64_t rtclock_intr_deadline;
131 rtclock_timer_t rtclock_timer;
132 boolean_t cpu_is64bit;
133 task_map_t cpu_task_map;
134 addr64_t cpu_task_cr3;
135 addr64_t cpu_active_cr3;
136 addr64_t cpu_kernel_cr3;
137 cpu_uber_t cpu_uber;
138 void *cpu_chud;
139 void *cpu_console_buf;
140 struct cpu_core *cpu_core; /* cpu's parent core */
141 struct processor *cpu_processor;
142 struct cpu_pmap *cpu_pmap;
143 struct cpu_desc_table *cpu_desc_tablep;
144 struct fake_descriptor *cpu_ldtp;
145 cpu_desc_index_t cpu_desc_index;
146 int cpu_ldt;
147 #ifdef MACH_KDB
148 /* XXX Untested: */
149 int cpu_db_pass_thru;
150 vm_offset_t cpu_db_stacks;
151 void *cpu_kdb_saved_state;
152 spl_t cpu_kdb_saved_ipl;
153 int cpu_kdb_is_slave;
154 int cpu_kdb_active;
155 #endif /* MACH_KDB */
156 boolean_t cpu_iflag;
157 boolean_t cpu_boot_complete;
158 int cpu_hibernate;
159 pmsd pms; /* Power Management Stepper control */
160 uint64_t rtcPop; /* when the etimer wants a timer pop */
161
162 vm_offset_t cpu_copywindow_base;
163 uint64_t *cpu_copywindow_pdp;
164
165 vm_offset_t cpu_physwindow_base;
166 uint64_t *cpu_physwindow_ptep;
167 void *cpu_hi_iss;
168 boolean_t cpu_tlb_invalid;
169
170 uint64_t *cpu_pmHpet; /* Address of the HPET for this processor */
171 uint32_t cpu_pmHpetVec; /* Interrupt vector for HPET for this processor */
172 /* Statistics */
173 pmStats_t cpu_pmStats; /* Power management data */
174 uint32_t cpu_hwIntCnt[256]; /* Interrupt counts */
175
176 uint64_t cpu_dr7; /* debug control register */
177 } cpu_data_t;
178
179 extern cpu_data_t *cpu_data_ptr[];
180 extern cpu_data_t cpu_data_master;
181
182 /* Macro to generate inline bodies to retrieve per-cpu data fields. */
183 #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
184 #define CPU_DATA_GET(member,type) \
185 type ret; \
186 __asm__ volatile ("movl %%gs:%P1,%0" \
187 : "=r" (ret) \
188 : "i" (offsetof(cpu_data_t,member))); \
189 return ret;
190
191 /*
192 * Everyone within the osfmk part of the kernel can use the fast
193 * inline versions of these routines. Everyone outside, must call
194 * the real thing,
195 */
196 static inline thread_t
197 get_active_thread(void)
198 {
199 CPU_DATA_GET(cpu_active_thread,thread_t)
200 }
201 #define current_thread_fast() get_active_thread()
202 #define current_thread() current_thread_fast()
203
204 static inline boolean_t
205 get_is64bit(void)
206 {
207 CPU_DATA_GET(cpu_is64bit, boolean_t)
208 }
209 #define cpu_mode_is64bit() get_is64bit()
210
211 static inline int
212 get_preemption_level(void)
213 {
214 CPU_DATA_GET(cpu_preemption_level,int)
215 }
216 static inline int
217 get_simple_lock_count(void)
218 {
219 CPU_DATA_GET(cpu_simple_lock_count,int)
220 }
221 static inline int
222 get_interrupt_level(void)
223 {
224 CPU_DATA_GET(cpu_interrupt_level,int)
225 }
226 static inline int
227 get_cpu_number(void)
228 {
229 CPU_DATA_GET(cpu_number,int)
230 }
231 static inline int
232 get_cpu_phys_number(void)
233 {
234 CPU_DATA_GET(cpu_phys_number,int)
235 }
236 static inline struct
237 cpu_core * get_cpu_core(void)
238 {
239 CPU_DATA_GET(cpu_core,struct cpu_core *)
240 }
241
242 static inline void
243 disable_preemption(void)
244 {
245 __asm__ volatile ("incl %%gs:%P0"
246 :
247 : "i" (offsetof(cpu_data_t, cpu_preemption_level)));
248 }
249
250 static inline void
251 enable_preemption(void)
252 {
253 assert(get_preemption_level() > 0);
254
255 __asm__ volatile ("decl %%gs:%P0 \n\t"
256 "jne 1f \n\t"
257 "call _kernel_preempt_check \n\t"
258 "1:"
259 : /* no outputs */
260 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
261 : "eax", "ecx", "edx", "cc", "memory");
262 }
263
264 static inline void
265 enable_preemption_no_check(void)
266 {
267 assert(get_preemption_level() > 0);
268
269 __asm__ volatile ("decl %%gs:%P0"
270 : /* no outputs */
271 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
272 : "cc", "memory");
273 }
274
275 static inline void
276 mp_disable_preemption(void)
277 {
278 disable_preemption();
279 }
280
281 static inline void
282 mp_enable_preemption(void)
283 {
284 enable_preemption();
285 }
286
287 static inline void
288 mp_enable_preemption_no_check(void)
289 {
290 enable_preemption_no_check();
291 }
292
293 static inline cpu_data_t *
294 current_cpu_datap(void)
295 {
296 CPU_DATA_GET(cpu_this, cpu_data_t *);
297 }
298
299 static inline cpu_data_t *
300 cpu_datap(int cpu)
301 {
302 assert(cpu_data_ptr[cpu]);
303 return cpu_data_ptr[cpu];
304 }
305
306 extern cpu_data_t *cpu_data_alloc(boolean_t is_boot_cpu);
307
308 #else /* !defined(__GNUC__) */
309
310 #endif /* defined(__GNUC__) */
311
312 #endif /* I386_CPU_DATA */