]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/mp.h
9456fc2388289cac6186f43e07c40f0c117a9d2b
[apple/xnu.git] / osfmk / i386 / mp.h
1 /*
2 * Copyright (c) 2000-2005 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 * Mach Operating System
35 * Copyright (c) 1991,1990 Carnegie Mellon University
36 * All Rights Reserved.
37 *
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
43 *
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47 *
48 * Carnegie Mellon requests users of this software to return to
49 *
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
54 *
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
57 */
58
59 /*
60 */
61 #ifdef KERNEL_PRIVATE
62
63 #ifndef _I386AT_MP_H_
64 #define _I386AT_MP_H_
65
66 #ifndef DEBUG
67 #include <debug.h>
68 #endif
69 //#define MP_DEBUG 1
70
71 #include <i386/apic.h>
72 #include <i386/mp_events.h>
73
74 #define LAPIC_ID_MAX (LAPIC_ID_MASK)
75
76 #define MAX_CPUS (LAPIC_ID_MAX + 1)
77
78 #ifndef ASSEMBLER
79 #include <sys/cdefs.h>
80 #include <mach/boolean.h>
81 #include <mach/kern_return.h>
82
83 __BEGIN_DECLS
84
85 extern kern_return_t intel_startCPU(int slot_num);
86 extern void i386_init_slave(void);
87 extern void smp_init(void);
88
89 extern void cpu_interrupt(int cpu);
90
91 extern void lapic_init(void);
92 extern void lapic_shutdown(void);
93 extern void lapic_smm_restore(void);
94 extern boolean_t lapic_probe(void);
95 extern void lapic_dump(void);
96 extern int lapic_interrupt(int interrupt, void *state);
97 extern void lapic_end_of_interrupt(void);
98 extern int lapic_to_cpu[];
99 extern int cpu_to_lapic[];
100 extern int lapic_interrupt_base;
101 extern void lapic_cpu_map(int lapic, int cpu_num);
102
103 extern void lapic_set_timer(
104 boolean_t interrupt,
105 lapic_timer_mode_t mode,
106 lapic_timer_divide_t divisor,
107 lapic_timer_count_t initial_count);
108
109 extern void lapic_get_timer(
110 lapic_timer_mode_t *mode,
111 lapic_timer_divide_t *divisor,
112 lapic_timer_count_t *initial_count,
113 lapic_timer_count_t *current_count);
114
115 typedef void (*i386_intr_func_t)(void *);
116 extern void lapic_set_timer_func(i386_intr_func_t func);
117 extern void lapic_set_pmi_func(i386_intr_func_t func);
118
119 __END_DECLS
120
121 #endif /* ASSEMBLER */
122
123 #define CPU_NUMBER(r) \
124 movl %gs:CPU_NUMBER_GS,r
125
126 #define CPU_NUMBER_FROM_LAPIC(r) \
127 movl EXT(lapic_id),r; \
128 movl 0(r),r; \
129 shrl $(LAPIC_ID_SHIFT),r; \
130 andl $(LAPIC_ID_MASK),r; \
131 movl EXT(lapic_to_cpu)(,r,4),r
132
133
134 /* word describing the reason for the interrupt, one per cpu */
135
136 #ifndef ASSEMBLER
137 #include <kern/lock.h>
138
139 extern unsigned int real_ncpus; /* real number of cpus */
140 extern unsigned int max_ncpus; /* max number of cpus */
141 decl_simple_lock_data(extern,kdb_lock) /* kdb lock */
142
143 __BEGIN_DECLS
144
145 extern void console_init(void);
146 extern void *console_cpu_alloc(boolean_t boot_cpu);
147 extern void console_cpu_free(void *console_buf);
148
149 extern int kdb_cpu; /* current cpu running kdb */
150 extern int kdb_debug;
151 extern int kdb_is_slave[];
152 extern int kdb_active[];
153
154 extern volatile boolean_t mp_kdp_trap;
155 extern void mp_kdp_enter(void);
156 extern void mp_kdp_exit(void);
157
158 /*
159 * All cpu rendezvous:
160 */
161 extern void mp_rendezvous(void (*setup_func)(void *),
162 void (*action_func)(void *),
163 void (*teardown_func)(void *),
164 void *arg);
165
166 __END_DECLS
167
168 #if MP_DEBUG
169 typedef struct {
170 uint64_t time;
171 int cpu;
172 mp_event_t event;
173 } cpu_signal_event_t;
174
175 #define LOG_NENTRIES 100
176 typedef struct {
177 uint64_t count[MP_LAST];
178 int next_entry;
179 cpu_signal_event_t entry[LOG_NENTRIES];
180 } cpu_signal_event_log_t;
181
182 extern cpu_signal_event_log_t *cpu_signal[];
183 extern cpu_signal_event_log_t *cpu_handle[];
184
185 #define DBGLOG(log,_cpu,_event) { \
186 boolean_t spl = ml_set_interrupts_enabled(FALSE); \
187 cpu_signal_event_log_t *logp = log[cpu_number()]; \
188 int next = logp->next_entry; \
189 cpu_signal_event_t *eventp = &logp->entry[next]; \
190 \
191 logp->count[_event]++; \
192 \
193 eventp->time = rdtsc64(); \
194 eventp->cpu = _cpu; \
195 eventp->event = _event; \
196 if (next == (LOG_NENTRIES - 1)) \
197 logp->next_entry = 0; \
198 else \
199 logp->next_entry++; \
200 \
201 (void) ml_set_interrupts_enabled(spl); \
202 }
203
204 #define DBGLOG_CPU_INIT(cpu) { \
205 cpu_signal_event_log_t **sig_logpp = &cpu_signal[cpu]; \
206 cpu_signal_event_log_t **hdl_logpp = &cpu_handle[cpu]; \
207 \
208 if (*sig_logpp == NULL && \
209 kmem_alloc(kernel_map, \
210 (vm_offset_t *) sig_logpp, \
211 sizeof(cpu_signal_event_log_t)) != KERN_SUCCESS)\
212 panic("DBGLOG_CPU_INIT cpu_signal allocation failed\n");\
213 bzero(*sig_logpp, sizeof(cpu_signal_event_log_t)); \
214 if (*hdl_logpp == NULL && \
215 kmem_alloc(kernel_map, \
216 (vm_offset_t *) hdl_logpp, \
217 sizeof(cpu_signal_event_log_t)) != KERN_SUCCESS)\
218 panic("DBGLOG_CPU_INIT cpu_handle allocation failed\n");\
219 bzero(*sig_logpp, sizeof(cpu_signal_event_log_t)); \
220 }
221 #else /* MP_DEBUG */
222 #define DBGLOG(log,_cpu,_event)
223 #define DBGLOG_CPU_INIT(cpu)
224 #endif /* MP_DEBUG */
225
226 #endif /* ASSEMBLER */
227
228 #define i_bit(bit, word) ((long)(*(word)) & ((long)1 << (bit)))
229
230
231 /*
232 * Device driver synchronization.
233 *
234 * at386_io_lock(op) and at386_io_unlock() are called
235 * by device drivers when accessing H/W. The underlying
236 * Processing is machine dependant. But the op argument
237 * to the at386_io_lock is generic
238 */
239
240 #define MP_DEV_OP_MAX 4
241 #define MP_DEV_WAIT MP_DEV_OP_MAX /* Wait for the lock */
242
243 /*
244 * If the caller specifies an op value different than MP_DEV_WAIT, the
245 * at386_io_lock function must return true if lock was successful else
246 * false
247 */
248
249 #define MP_DEV_OP_START 0 /* If lock busy, register a pending start op */
250 #define MP_DEV_OP_INTR 1 /* If lock busy, register a pending intr */
251 #define MP_DEV_OP_TIMEO 2 /* If lock busy, register a pending timeout */
252 #define MP_DEV_OP_CALLB 3 /* If lock busy, register a pending callback */
253
254 #if MACH_RT
255 #define _DISABLE_PREEMPTION \
256 incl %gs:CPU_PREEMPTION_LEVEL
257
258 #define _ENABLE_PREEMPTION \
259 decl %gs:CPU_PREEMPTION_LEVEL ; \
260 jne 9f ; \
261 pushl %eax ; \
262 pushl %ecx ; \
263 pushl %edx ; \
264 call EXT(kernel_preempt_check) ; \
265 popl %edx ; \
266 popl %ecx ; \
267 popl %eax ; \
268 9:
269
270 #define _ENABLE_PREEMPTION_NO_CHECK \
271 decl %gs:CPU_PREEMPTION_LEVEL
272
273 #if MACH_ASSERT
274 #define DISABLE_PREEMPTION \
275 pushl %eax; \
276 pushl %ecx; \
277 pushl %edx; \
278 call EXT(_disable_preemption); \
279 popl %edx; \
280 popl %ecx; \
281 popl %eax
282 #define ENABLE_PREEMPTION \
283 pushl %eax; \
284 pushl %ecx; \
285 pushl %edx; \
286 call EXT(_enable_preemption); \
287 popl %edx; \
288 popl %ecx; \
289 popl %eax
290 #define ENABLE_PREEMPTION_NO_CHECK \
291 pushl %eax; \
292 pushl %ecx; \
293 pushl %edx; \
294 call EXT(_enable_preemption_no_check); \
295 popl %edx; \
296 popl %ecx; \
297 popl %eax
298 #define MP_DISABLE_PREEMPTION \
299 pushl %eax; \
300 pushl %ecx; \
301 pushl %edx; \
302 call EXT(_mp_disable_preemption); \
303 popl %edx; \
304 popl %ecx; \
305 popl %eax
306 #define MP_ENABLE_PREEMPTION \
307 pushl %eax; \
308 pushl %ecx; \
309 pushl %edx; \
310 call EXT(_mp_enable_preemption); \
311 popl %edx; \
312 popl %ecx; \
313 popl %eax
314 #define MP_ENABLE_PREEMPTION_NO_CHECK \
315 pushl %eax; \
316 pushl %ecx; \
317 pushl %edx; \
318 call EXT(_mp_enable_preemption_no_check); \
319 popl %edx; \
320 popl %ecx; \
321 popl %eax
322 #else /* MACH_ASSERT */
323 #define DISABLE_PREEMPTION _DISABLE_PREEMPTION
324 #define ENABLE_PREEMPTION _ENABLE_PREEMPTION
325 #define ENABLE_PREEMPTION_NO_CHECK _ENABLE_PREEMPTION_NO_CHECK
326 #define MP_DISABLE_PREEMPTION _DISABLE_PREEMPTION
327 #define MP_ENABLE_PREEMPTION _ENABLE_PREEMPTION
328 #define MP_ENABLE_PREEMPTION_NO_CHECK _ENABLE_PREEMPTION_NO_CHECK
329 #endif /* MACH_ASSERT */
330
331 #else /* MACH_RT */
332 #define DISABLE_PREEMPTION
333 #define ENABLE_PREEMPTION
334 #define ENABLE_PREEMPTION_NO_CHECK
335 #define MP_DISABLE_PREEMPTION
336 #define MP_ENABLE_PREEMPTION
337 #define MP_ENABLE_PREEMPTION_NO_CHECK
338 #endif /* MACH_RT */
339
340 #endif /* _I386AT_MP_H_ */
341
342 #endif /* KERNEL_PRIVATE */