]> git.saurik.com Git - apple/xnu.git/blame_incremental - osfmk/i386/cpu_data.h
xnu-792.12.6.tar.gz
[apple/xnu.git] / osfmk / i386 / cpu_data.h
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30/*
31 * @OSF_COPYRIGHT@
32 *
33 */
34
35#ifndef I386_CPU_DATA
36#define I386_CPU_DATA
37
38#include <mach_assert.h>
39
40#if defined(__GNUC__)
41
42#include <kern/assert.h>
43#include <kern/kern_types.h>
44#include <kern/processor.h>
45#include <pexpert/pexpert.h>
46
47
48/*
49 * Data structures referenced (anonymously) from per-cpu data:
50 */
51struct cpu_core;
52struct cpu_cons_buffer;
53struct mp_desc_table;
54
55
56/*
57 * Data structures embedded in per-cpu data:
58 */
59typedef struct rtclock_timer {
60 uint64_t deadline;
61 boolean_t is_set;
62 boolean_t has_expired;
63} rtclock_timer_t;
64
65typedef struct {
66 uint64_t rnt_tsc; /* timestamp */
67 uint64_t rnt_nanos; /* nanoseconds */
68 uint32_t rnt_scale; /* tsc -> nanosec multiplier */
69 uint32_t rnt_shift; /* tsc -> nanosec shift/div */
70 uint64_t rnt_step_tsc; /* tsc when scale applied */
71 uint64_t rnt_step_nanos; /* ns when scale applied */
72} rtc_nanotime_t;
73
74typedef struct {
75 struct i386_tss *cdi_ktss;
76#if MACH_KDB
77 struct i386_tss *cdi_dbtss;
78#endif /* MACH_KDB */
79 struct fake_descriptor *cdi_gdt;
80 struct fake_descriptor *cdi_idt;
81 struct fake_descriptor *cdi_ldt;
82} cpu_desc_index_t;
83
84
85/*
86 * Per-cpu data.
87 *
88 * Each processor has a per-cpu data area which is dereferenced through the
89 * current_cpu_datap() macro. For speed, the %gs segment is based here, and
90 * using this, inlines provides single-instruction access to frequently used
91 * members - such as get_cpu_number()/cpu_number(), and get_active_thread()/
92 * current_thread().
93 *
94 * Cpu data owned by another processor can be accessed using the
95 * cpu_datap(cpu_number) macro which uses the cpu_data_ptr[] array of per-cpu
96 * pointers.
97 */
98typedef struct cpu_data
99{
100 struct cpu_data *cpu_this; /* pointer to myself */
101 thread_t cpu_active_thread;
102 thread_t cpu_active_kloaded;
103 vm_offset_t cpu_active_stack;
104 vm_offset_t cpu_kernel_stack;
105 vm_offset_t cpu_int_stack_top;
106 int cpu_preemption_level;
107 int cpu_simple_lock_count;
108 int cpu_interrupt_level;
109 int cpu_number; /* Logical CPU */
110 int cpu_phys_number; /* Physical CPU */
111 cpu_id_t cpu_id; /* Platform Expert */
112 int cpu_signals; /* IPI events */
113 int cpu_mcount_off; /* mcount recursion */
114 ast_t cpu_pending_ast;
115 int cpu_type;
116 int cpu_subtype;
117 int cpu_threadtype;
118 int cpu_running;
119 struct cpu_core *cpu_core; /* cpu's parent core */
120 uint64_t cpu_rtc_tick_deadline;
121 uint64_t cpu_rtc_intr_deadline;
122 rtclock_timer_t cpu_rtc_timer;
123 rtc_nanotime_t cpu_rtc_nanotime;
124 void *cpu_console_buf;
125 struct processor *cpu_processor;
126 struct cpu_pmap *cpu_pmap;
127 struct mp_desc_table *cpu_desc_tablep;
128 cpu_desc_index_t cpu_desc_index;
129 boolean_t cpu_iflag;
130#ifdef MACH_KDB
131 /* XXX Untested: */
132 int cpu_db_pass_thru;
133 vm_offset_t cpu_db_stacks;
134 struct i386_saved_state *cpu_kdb_saved_state;
135 spl_t cpu_kdb_saved_ipl;
136 int cpu_kdb_is_slave;
137 int cpu_kdb_active;
138#endif /* MACH_KDB */
139 int cpu_hibernate;
140} cpu_data_t;
141
142extern cpu_data_t *cpu_data_ptr[];
143extern cpu_data_t cpu_data_master;
144
145/* Macro to generate inline bodies to retrieve per-cpu data fields. */
146#define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
147#define CPU_DATA_GET(member,type) \
148 type ret; \
149 __asm__ volatile ("movl %%gs:%P1,%0" \
150 : "=r" (ret) \
151 : "i" (offsetof(cpu_data_t,member))); \
152 return ret;
153
154/*
155 * Everyone within the osfmk part of the kernel can use the fast
156 * inline versions of these routines. Everyone outside, must call
157 * the real thing,
158 */
159static inline thread_t
160get_active_thread(void)
161{
162 CPU_DATA_GET(cpu_active_thread,thread_t)
163}
164#define current_thread_fast() get_active_thread()
165#define current_thread() current_thread_fast()
166
167static inline int
168get_preemption_level(void)
169{
170 CPU_DATA_GET(cpu_preemption_level,int)
171}
172static inline int
173get_simple_lock_count(void)
174{
175 CPU_DATA_GET(cpu_simple_lock_count,int)
176}
177static inline int
178get_interrupt_level(void)
179{
180 CPU_DATA_GET(cpu_interrupt_level,int)
181}
182static inline int
183get_cpu_number(void)
184{
185 CPU_DATA_GET(cpu_number,int)
186}
187static inline int
188get_cpu_phys_number(void)
189{
190 CPU_DATA_GET(cpu_phys_number,int)
191}
192static inline struct
193cpu_core * get_cpu_core(void)
194{
195 CPU_DATA_GET(cpu_core,struct cpu_core *)
196}
197
198static inline void
199disable_preemption(void)
200{
201 __asm__ volatile ("incl %%gs:%P0"
202 :
203 : "i" (offsetof(cpu_data_t, cpu_preemption_level)));
204}
205
206static inline void
207enable_preemption(void)
208{
209 assert(get_preemption_level() > 0);
210
211 __asm__ volatile ("decl %%gs:%P0 \n\t"
212 "jne 1f \n\t"
213 "call _kernel_preempt_check \n\t"
214 "1:"
215 : /* no outputs */
216 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
217 : "eax", "ecx", "edx", "cc", "memory");
218}
219
220static inline void
221enable_preemption_no_check(void)
222{
223 assert(get_preemption_level() > 0);
224
225 __asm__ volatile ("decl %%gs:%P0"
226 : /* no outputs */
227 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
228 : "cc", "memory");
229}
230
231static inline void
232mp_disable_preemption(void)
233{
234 disable_preemption();
235}
236
237static inline void
238mp_enable_preemption(void)
239{
240 enable_preemption();
241}
242
243static inline void
244mp_enable_preemption_no_check(void)
245{
246 enable_preemption_no_check();
247}
248
249static inline cpu_data_t *
250current_cpu_datap(void)
251{
252 CPU_DATA_GET(cpu_this, cpu_data_t *);
253}
254
255static inline cpu_data_t *
256cpu_datap(int cpu)
257{
258 assert(cpu_data_ptr[cpu]);
259 return cpu_data_ptr[cpu];
260}
261
262extern cpu_data_t *cpu_data_alloc(boolean_t is_boot_cpu);
263
264#else /* !defined(__GNUC__) */
265
266#endif /* defined(__GNUC__) */
267
268#endif /* I386_CPU_DATA */