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