]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/cpu_data.h
xnu-3247.1.106.tar.gz
[apple/xnu.git] / osfmk / i386 / cpu_data.h
1 /*
2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 *
31 */
32
33 #ifndef I386_CPU_DATA
34 #define I386_CPU_DATA
35
36 #include <mach_assert.h>
37
38 #include <kern/assert.h>
39 #include <kern/kern_types.h>
40 #include <kern/queue.h>
41 #include <kern/processor.h>
42 #include <kern/pms.h>
43 #include <pexpert/pexpert.h>
44 #include <mach/i386/thread_status.h>
45 #include <mach/i386/vm_param.h>
46 #include <i386/locks.h>
47 #include <i386/rtclock_protos.h>
48 #include <i386/pmCPU.h>
49 #include <i386/cpu_topology.h>
50
51 #if CONFIG_VMX
52 #include <i386/vmx/vmx_cpu.h>
53 #endif
54
55 #include <machine/pal_routines.h>
56
57 /*
58 * Data structures referenced (anonymously) from per-cpu data:
59 */
60 struct cpu_cons_buffer;
61 struct cpu_desc_table;
62 struct mca_state;
63 struct prngContext;
64
65 /*
66 * Data structures embedded in per-cpu data:
67 */
68 typedef struct rtclock_timer {
69 mpqueue_head_t queue;
70 uint64_t deadline;
71 uint64_t when_set;
72 boolean_t has_expired;
73 } rtclock_timer_t;
74
75
76 typedef struct {
77 struct x86_64_tss *cdi_ktss;
78 struct __attribute__((packed)) {
79 uint16_t size;
80 void *ptr;
81 } cdi_gdt, cdi_idt;
82 struct fake_descriptor *cdi_ldt;
83 vm_offset_t cdi_sstk;
84 } cpu_desc_index_t;
85
86 typedef enum {
87 TASK_MAP_32BIT, /* 32-bit user, compatibility mode */
88 TASK_MAP_64BIT, /* 64-bit user thread, shared space */
89 } task_map_t;
90
91
92 /*
93 * This structure is used on entry into the (uber-)kernel on syscall from
94 * a 64-bit user. It contains the address of the machine state save area
95 * for the current thread and a temporary place to save the user's rsp
96 * before loading this address into rsp.
97 */
98 typedef struct {
99 addr64_t cu_isf; /* thread->pcb->iss.isf */
100 uint64_t cu_tmp; /* temporary scratch */
101 addr64_t cu_user_gs_base;
102 } cpu_uber_t;
103
104 typedef uint16_t pcid_t;
105 typedef uint8_t pcid_ref_t;
106
107 #define CPU_RTIME_BINS (12)
108 #define CPU_ITIME_BINS (CPU_RTIME_BINS)
109
110 /*
111 * Per-cpu data.
112 *
113 * Each processor has a per-cpu data area which is dereferenced through the
114 * current_cpu_datap() macro. For speed, the %gs segment is based here, and
115 * using this, inlines provides single-instruction access to frequently used
116 * members - such as get_cpu_number()/cpu_number(), and get_active_thread()/
117 * current_thread().
118 *
119 * Cpu data owned by another processor can be accessed using the
120 * cpu_datap(cpu_number) macro which uses the cpu_data_ptr[] array of per-cpu
121 * pointers.
122 */
123 typedef struct cpu_data
124 {
125 struct pal_cpu_data cpu_pal_data; /* PAL-specific data */
126 #define cpu_pd cpu_pal_data /* convenience alias */
127 struct cpu_data *cpu_this; /* pointer to myself */
128 thread_t cpu_active_thread;
129 thread_t cpu_nthread;
130 volatile int cpu_preemption_level;
131 int cpu_number; /* Logical CPU */
132 void *cpu_int_state; /* interrupt state */
133 vm_offset_t cpu_active_stack; /* kernel stack base */
134 vm_offset_t cpu_kernel_stack; /* kernel stack top */
135 vm_offset_t cpu_int_stack_top;
136 int cpu_interrupt_level;
137 int cpu_phys_number; /* Physical CPU */
138 cpu_id_t cpu_id; /* Platform Expert */
139 volatile int cpu_signals; /* IPI events */
140 volatile int cpu_prior_signals; /* Last set of events,
141 * debugging
142 */
143 ast_t cpu_pending_ast;
144 volatile int cpu_running;
145 boolean_t cpu_fixed_pmcs_enabled;
146 rtclock_timer_t rtclock_timer;
147 volatile addr64_t cpu_active_cr3 __attribute((aligned(64)));
148 union {
149 volatile uint32_t cpu_tlb_invalid;
150 struct {
151 volatile uint16_t cpu_tlb_invalid_local;
152 volatile uint16_t cpu_tlb_invalid_global;
153 };
154 };
155 volatile task_map_t cpu_task_map;
156 volatile addr64_t cpu_task_cr3;
157 addr64_t cpu_kernel_cr3;
158 cpu_uber_t cpu_uber;
159 void *cpu_chud;
160 void *cpu_console_buf;
161 struct x86_lcpu lcpu;
162 struct processor *cpu_processor;
163 #if NCOPY_WINDOWS > 0
164 struct cpu_pmap *cpu_pmap;
165 #endif
166 struct cpu_desc_table *cpu_desc_tablep;
167 struct fake_descriptor *cpu_ldtp;
168 cpu_desc_index_t cpu_desc_index;
169 int cpu_ldt;
170 #if NCOPY_WINDOWS > 0
171 vm_offset_t cpu_copywindow_base;
172 uint64_t *cpu_copywindow_pdp;
173
174 vm_offset_t cpu_physwindow_base;
175 uint64_t *cpu_physwindow_ptep;
176 #endif
177
178 #define HWINTCNT_SIZE 256
179 uint32_t cpu_hwIntCnt[HWINTCNT_SIZE]; /* Interrupt counts */
180 uint64_t cpu_hwIntpexits[HWINTCNT_SIZE];
181 uint64_t cpu_hwIntcexits[HWINTCNT_SIZE];
182 uint64_t cpu_dr7; /* debug control register */
183 uint64_t cpu_int_event_time; /* intr entry/exit time */
184 pal_rtc_nanotime_t *cpu_nanotime; /* Nanotime info */
185 #if KPC
186 /* double-buffered performance counter data */
187 uint64_t *cpu_kpc_buf[2];
188 /* PMC shadow and reload value buffers */
189 uint64_t *cpu_kpc_shadow;
190 uint64_t *cpu_kpc_reload;
191 #endif
192 uint32_t cpu_pmap_pcid_enabled;
193 pcid_t cpu_active_pcid;
194 pcid_t cpu_last_pcid;
195 volatile pcid_ref_t *cpu_pmap_pcid_coherentp;
196 volatile pcid_ref_t *cpu_pmap_pcid_coherentp_kernel;
197 #define PMAP_PCID_MAX_PCID (0x1000)
198 pcid_t cpu_pcid_free_hint;
199 pcid_ref_t cpu_pcid_refcounts[PMAP_PCID_MAX_PCID];
200 pmap_t cpu_pcid_last_pmap_dispatched[PMAP_PCID_MAX_PCID];
201 #ifdef PCID_STATS
202 uint64_t cpu_pmap_pcid_flushes;
203 uint64_t cpu_pmap_pcid_preserves;
204 #endif
205 uint64_t cpu_aperf;
206 uint64_t cpu_mperf;
207 uint64_t cpu_c3res;
208 uint64_t cpu_c6res;
209 uint64_t cpu_c7res;
210 uint64_t cpu_itime_total;
211 uint64_t cpu_rtime_total;
212 uint64_t cpu_ixtime;
213 uint64_t cpu_idle_exits;
214 uint64_t cpu_rtimes[CPU_RTIME_BINS];
215 uint64_t cpu_itimes[CPU_ITIME_BINS];
216 uint64_t cpu_cur_insns;
217 uint64_t cpu_cur_ucc;
218 uint64_t cpu_cur_urc;
219 uint64_t cpu_gpmcs[4];
220 uint64_t cpu_max_observed_int_latency;
221 int cpu_max_observed_int_latency_vector;
222 volatile boolean_t cpu_NMI_acknowledged;
223 uint64_t debugger_entry_time;
224 uint64_t debugger_ipi_time;
225 /* A separate nested interrupt stack flag, to account
226 * for non-nested interrupts arriving while on the interrupt stack
227 * Currently only occurs when AICPM enables interrupts on the
228 * interrupt stack during processor offlining.
229 */
230 uint32_t cpu_nested_istack;
231 uint32_t cpu_nested_istack_events;
232 x86_saved_state64_t *cpu_fatal_trap_state;
233 x86_saved_state64_t *cpu_post_fatal_trap_state;
234 #if CONFIG_VMX
235 vmx_cpu_t cpu_vmx; /* wonderful world of virtualization */
236 #endif
237 #if CONFIG_MCA
238 struct mca_state *cpu_mca_state; /* State at MC fault */
239 #endif
240 struct prngContext *cpu_prng; /* PRNG's context */
241 int cpu_type;
242 int cpu_subtype;
243 int cpu_threadtype;
244 boolean_t cpu_iflag;
245 boolean_t cpu_boot_complete;
246 int cpu_hibernate;
247 } cpu_data_t;
248
249 extern cpu_data_t *cpu_data_ptr[];
250
251 /* Macro to generate inline bodies to retrieve per-cpu data fields. */
252 #if defined(__clang__)
253 #define GS_RELATIVE volatile __attribute__((address_space(256)))
254 #ifndef offsetof
255 #define offsetof(TYPE,MEMBER) __builtin_offsetof(TYPE,MEMBER)
256 #endif
257
258 #define CPU_DATA_GET(member,type) \
259 cpu_data_t GS_RELATIVE *cpu_data = \
260 (cpu_data_t GS_RELATIVE *)0UL; \
261 type ret; \
262 ret = cpu_data->member; \
263 return ret;
264
265 #define CPU_DATA_GET_INDEX(member,index,type) \
266 cpu_data_t GS_RELATIVE *cpu_data = \
267 (cpu_data_t GS_RELATIVE *)0UL; \
268 type ret; \
269 ret = cpu_data->member[index]; \
270 return ret;
271
272 #define CPU_DATA_SET(member,value) \
273 cpu_data_t GS_RELATIVE *cpu_data = \
274 (cpu_data_t GS_RELATIVE *)0UL; \
275 cpu_data->member = value;
276
277 #define CPU_DATA_XCHG(member,value,type) \
278 cpu_data_t GS_RELATIVE *cpu_data = \
279 (cpu_data_t GS_RELATIVE *)0UL; \
280 type ret; \
281 ret = cpu_data->member; \
282 cpu_data->member = value; \
283 return ret;
284
285 #else /* !defined(__clang__) */
286
287 #ifndef offsetof
288 #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
289 #endif /* offsetof */
290 #define CPU_DATA_GET(member,type) \
291 type ret; \
292 __asm__ volatile ("mov %%gs:%P1,%0" \
293 : "=r" (ret) \
294 : "i" (offsetof(cpu_data_t,member))); \
295 return ret;
296
297 #define CPU_DATA_GET_INDEX(member,index,type) \
298 type ret; \
299 __asm__ volatile ("mov %%gs:(%1),%0" \
300 : "=r" (ret) \
301 : "r" (offsetof(cpu_data_t,member[index]))); \
302 return ret;
303
304 #define CPU_DATA_SET(member,value) \
305 __asm__ volatile ("mov %0,%%gs:%P1" \
306 : \
307 : "r" (value), "i" (offsetof(cpu_data_t,member)));
308
309 #define CPU_DATA_XCHG(member,value,type) \
310 type ret; \
311 __asm__ volatile ("xchg %0,%%gs:%P1" \
312 : "=r" (ret) \
313 : "i" (offsetof(cpu_data_t,member)), "0" (value)); \
314 return ret;
315
316 #endif /* !defined(__clang__) */
317
318 /*
319 * Everyone within the osfmk part of the kernel can use the fast
320 * inline versions of these routines. Everyone outside, must call
321 * the real thing,
322 */
323 static inline thread_t
324 get_active_thread(void)
325 {
326 CPU_DATA_GET(cpu_active_thread,thread_t)
327 }
328 #define current_thread_fast() get_active_thread()
329 #define current_thread() current_thread_fast()
330
331 #define cpu_mode_is64bit() TRUE
332
333 static inline int
334 get_preemption_level(void)
335 {
336 CPU_DATA_GET(cpu_preemption_level,int)
337 }
338 static inline int
339 get_interrupt_level(void)
340 {
341 CPU_DATA_GET(cpu_interrupt_level,int)
342 }
343 static inline int
344 get_cpu_number(void)
345 {
346 CPU_DATA_GET(cpu_number,int)
347 }
348 static inline int
349 get_cpu_phys_number(void)
350 {
351 CPU_DATA_GET(cpu_phys_number,int)
352 }
353
354
355 static inline void
356 disable_preemption(void)
357 {
358 #if defined(__clang__)
359 cpu_data_t GS_RELATIVE *cpu_data = (cpu_data_t GS_RELATIVE *)0UL;
360 cpu_data->cpu_preemption_level++;
361 #else
362 __asm__ volatile ("incl %%gs:%P0"
363 :
364 : "i" (offsetof(cpu_data_t, cpu_preemption_level)));
365 #endif
366 }
367
368 static inline void
369 enable_preemption(void)
370 {
371 assert(get_preemption_level() > 0);
372
373 #if defined(__clang__)
374 cpu_data_t GS_RELATIVE *cpu_data = (cpu_data_t GS_RELATIVE *)0UL;
375 if (0 == --cpu_data->cpu_preemption_level)
376 kernel_preempt_check();
377 #else
378 __asm__ volatile ("decl %%gs:%P0 \n\t"
379 "jne 1f \n\t"
380 "call _kernel_preempt_check \n\t"
381 "1:"
382 : /* no outputs */
383 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
384 : "eax", "ecx", "edx", "cc", "memory");
385 #endif
386 }
387
388 static inline void
389 enable_preemption_no_check(void)
390 {
391 assert(get_preemption_level() > 0);
392
393 #if defined(__clang__)
394 cpu_data_t GS_RELATIVE *cpu_data = (cpu_data_t GS_RELATIVE *)0UL;
395 cpu_data->cpu_preemption_level--;
396 #else
397 __asm__ volatile ("decl %%gs:%P0"
398 : /* no outputs */
399 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
400 : "cc", "memory");
401 #endif
402 }
403
404 static inline void
405 mp_disable_preemption(void)
406 {
407 disable_preemption();
408 }
409
410 static inline void
411 mp_enable_preemption(void)
412 {
413 enable_preemption();
414 }
415
416 static inline void
417 mp_enable_preemption_no_check(void)
418 {
419 enable_preemption_no_check();
420 }
421
422 static inline cpu_data_t *
423 current_cpu_datap(void)
424 {
425 CPU_DATA_GET(cpu_this, cpu_data_t *);
426 }
427
428 static inline cpu_data_t *
429 cpu_datap(int cpu)
430 {
431 return cpu_data_ptr[cpu];
432 }
433
434 extern cpu_data_t *cpu_data_alloc(boolean_t is_boot_cpu);
435 extern void cpu_data_realloc(void);
436
437 #endif /* I386_CPU_DATA */