]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/mp.h
xnu-6153.141.1.tar.gz
[apple/xnu.git] / osfmk / i386 / mp.h
CommitLineData
1c79356b 1/*
cb323159 2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
0a7de745 31/*
1c79356b
A
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
0a7de745 35 *
1c79356b
A
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.
0a7de745 41 *
1c79356b
A
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.
0a7de745 45 *
1c79356b 46 * Carnegie Mellon requests users of this software to return to
0a7de745 47 *
1c79356b
A
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
0a7de745 52 *
1c79356b
A
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56
57/*
58 */
0a7de745 59#ifdef KERNEL_PRIVATE
1c79356b 60
593a1d5f
A
61#ifndef _I386_MP_H_
62#define _I386_MP_H_
1c79356b 63
91447636 64//#define MP_DEBUG 1
55e303ae 65
1c79356b 66#include <i386/apic.h>
55e303ae 67#include <i386/mp_events.h>
cb323159 68#include <machine/limits.h>
55e303ae 69
0a7de745 70#define MAX_CPUS 64 /* 8 * sizeof(cpumask_t) */
55e303ae 71
0a7de745 72#ifndef ASSEMBLER
593a1d5f 73#include <stdint.h>
91447636
A
74#include <sys/cdefs.h>
75#include <mach/boolean.h>
76#include <mach/kern_return.h>
0c530ab8 77#include <mach/i386/thread_status.h>
b0d623f7 78#include <mach/vm_types.h>
fe8ab488 79#include <kern/simple_lock.h>
cb323159 80#include <kern/assert.h>
91447636
A
81
82__BEGIN_DECLS
83
84extern kern_return_t intel_startCPU(int slot_num);
593a1d5f 85extern kern_return_t intel_startCPU_fast(int slot_num);
cb323159
A
86extern void i386_init_slave(void) __dead2;
87extern void i386_init_slave_fast(void) __dead2;
91447636
A
88extern void smp_init(void);
89
90extern void cpu_interrupt(int cpu);
91447636
A
91__END_DECLS
92
0a7de745
A
93extern unsigned int real_ncpus; /* real number of cpus */
94extern unsigned int max_ncpus; /* max number of cpus */
cb323159 95decl_simple_lock_data(extern, kdb_lock); /* kdb lock */
91447636
A
96
97__BEGIN_DECLS
98
0a7de745
A
99extern void console_init(void);
100extern void *console_cpu_alloc(boolean_t boot_cpu);
101extern void console_cpu_free(void *console_buf);
1c79356b 102
0a7de745
A
103extern int kdb_cpu; /* current cpu running kdb */
104extern int kdb_debug;
105extern int kdb_active[];
55e303ae 106
0a7de745
A
107extern volatile boolean_t mp_kdp_trap;
108extern volatile boolean_t mp_kdp_is_NMI;
109extern volatile boolean_t force_immediate_debugger_NMI;
935ed37a 110extern volatile boolean_t pmap_tlb_flush_timeout;
060df5ea 111extern volatile usimple_lock_t spinlock_timed_out;
6d2010ae 112extern volatile uint32_t spinlock_owner_cpu;
fe8ab488 113extern uint32_t spinlock_timeout_NMI(uintptr_t thread_addr);
060df5ea 114
0a7de745 115extern uint64_t LastDebuggerEntryAllowance;
2d21ac55 116
0a7de745
A
117extern void mp_kdp_enter(boolean_t proceed_on_failure);
118extern void mp_kdp_exit(void);
119extern boolean_t mp_kdp_all_cpus_halted(void);
55e303ae 120
0a7de745
A
121extern boolean_t mp_recent_debugger_activity(void);
122extern void kernel_spin(uint64_t spin_ns);
0c530ab8 123
55e303ae
A
124/*
125 * All cpu rendezvous:
126 */
0c530ab8 127extern void mp_rendezvous(
0a7de745
A
128 void (*setup_func)(void *),
129 void (*action_func)(void *),
130 void (*teardown_func)(void *),
131 void *arg);
0c530ab8 132extern void mp_rendezvous_no_intrs(
0a7de745
A
133 void (*action_func)(void *),
134 void *arg);
0c530ab8 135extern void mp_rendezvous_break_lock(void);
d9a64523
A
136extern void mp_rendezvous_lock(void);
137extern void mp_rendezvous_unlock(void);
55e303ae 138
2d21ac55
A
139/*
140 * All cpu broadcast.
141 * Called from thread context, this blocks until all active cpus have
142 * run action_func:
143 */
144extern void mp_broadcast(
0a7de745
A
145 void (*action_func)(void *),
146 void *arg);
b0d623f7
A
147#if MACH_KDP
148typedef long (*kdp_x86_xcpu_func_t) (void *arg0, void *arg1, uint16_t lcpu);
149
0a7de745
A
150extern long kdp_x86_xcpu_invoke(const uint16_t lcpu,
151 kdp_x86_xcpu_func_t func,
152 void *arg0, void *arg1);
153typedef enum {KDP_XCPU_NONE = 0xffff, KDP_CURRENT_LCPU = 0xfffe} kdp_cpu_t;
b0d623f7 154#endif
2d21ac55
A
155
156typedef uint32_t cpu_t;
3e170ce0 157typedef volatile uint64_t cpumask_t;
cb323159
A
158
159static_assert(sizeof(cpumask_t) * CHAR_BIT >= MAX_CPUS, "cpumask_t bitvector is too small for current MAX_CPUS value");
160
2d21ac55
A
161static inline cpumask_t
162cpu_to_cpumask(cpu_t cpu)
163{
3e170ce0 164 return (cpu < MAX_CPUS) ? (1ULL << cpu) : 0;
2d21ac55 165}
0a7de745
A
166#define CPUMASK_ALL 0xffffffffffffffffULL
167#define CPUMASK_SELF cpu_to_cpumask(cpu_number())
168#define CPUMASK_OTHERS (CPUMASK_ALL & ~CPUMASK_SELF)
2d21ac55 169
fe8ab488
A
170/* Initialation routing called at processor registration */
171extern void mp_cpus_call_cpu_init(int cpu);
172
2d21ac55
A
173/*
174 * Invoke a function (possibly NULL) on a set of cpus specified by a mask.
175 * The mask may include the local cpu.
176 * If the mode is:
6d2010ae 177 * - ASYNC: other cpus make their calls in parallel
0a7de745
A
178 * - SYNC: the calls are performed serially in logical cpu order
179 * - NOSYNC: the calls are queued
6d2010ae
A
180 * Unless the mode is NOSYNC, mp_cpus_call() returns when the function has been
181 * called on all specified cpus.
182 * The return value is the number of cpus where the call was made or queued.
2d21ac55
A
183 * The action function is called with interrupts disabled.
184 */
185extern cpu_t mp_cpus_call(
0a7de745
A
186 cpumask_t cpus,
187 mp_sync_t mode,
188 void (*action_func)(void *),
189 void *arg);
6d2010ae 190extern cpu_t mp_cpus_call1(
0a7de745
A
191 cpumask_t cpus,
192 mp_sync_t mode,
193 void (*action_func)(void *, void*),
194 void *arg0,
195 void *arg1,
196 cpumask_t *cpus_calledp);
2d21ac55 197
5ba3f43e
A
198typedef enum {
199 NONE = 0,
200 SPINLOCK_TIMEOUT,
201 TLB_FLUSH_TIMEOUT,
202 CROSSCALL_TIMEOUT,
203 INTERRUPT_WATCHDOG
204} NMI_reason_t;
205extern void NMIPI_panic(cpumask_t cpus, NMI_reason_t reason);
fe8ab488
A
206
207/* Interrupt a set of cpus, forcing an exit out of non-root mode */
208extern void mp_cpus_kick(cpumask_t cpus);
c910b4d9
A
209/*
210 * Power-management-specific SPI to:
211 * - register a callout function, and
212 * - request the callout (if registered) on a given cpu.
213 */
214extern void PM_interrupt_register(void (*fn)(void));
215extern void cpu_PM_interrupt(int cpu);
216
91447636
A
217__END_DECLS
218
55e303ae
A
219#if MP_DEBUG
220typedef struct {
0a7de745
A
221 uint64_t time;
222 int cpu;
223 mp_event_t event;
55e303ae
A
224} cpu_signal_event_t;
225
0a7de745 226#define LOG_NENTRIES 100
55e303ae 227typedef struct {
0a7de745
A
228 uint64_t count[MP_LAST];
229 int next_entry;
230 cpu_signal_event_t entry[LOG_NENTRIES];
55e303ae
A
231} cpu_signal_event_log_t;
232
0a7de745
A
233extern cpu_signal_event_log_t *cpu_signal[];
234extern cpu_signal_event_log_t *cpu_handle[];
235
236#define DBGLOG(log, _cpu, _event) { \
237 boolean_t spl = ml_set_interrupts_enabled(FALSE); \
238 cpu_signal_event_log_t *logp = log[cpu_number()]; \
239 int next = logp->next_entry; \
240 cpu_signal_event_t *eventp = &logp->entry[next]; \
241 \
242 logp->count[_event]++; \
243 \
244 eventp->time = rdtsc64(); \
245 eventp->cpu = _cpu; \
246 eventp->event = _event; \
247 if (next == (LOG_NENTRIES - 1)) \
248 logp->next_entry = 0; \
249 else \
250 logp->next_entry++; \
251 \
252 (void) ml_set_interrupts_enabled(spl); \
55e303ae 253}
91447636 254
0a7de745
A
255#define DBGLOG_CPU_INIT(cpu) { \
256 cpu_signal_event_log_t **sig_logpp = &cpu_signal[cpu]; \
257 cpu_signal_event_log_t **hdl_logpp = &cpu_handle[cpu]; \
258 \
259 if (*sig_logpp == NULL && \
260 kmem_alloc(kernel_map, \
261 (vm_offset_t *) sig_logpp, \
262 sizeof(cpu_signal_event_log_t)) != KERN_SUCCESS)\
263 panic("DBGLOG_CPU_INIT cpu_signal allocation failed\n");\
264 bzero(*sig_logpp, sizeof(cpu_signal_event_log_t)); \
265 if (*hdl_logpp == NULL && \
266 kmem_alloc(kernel_map, \
267 (vm_offset_t *) hdl_logpp, \
268 sizeof(cpu_signal_event_log_t)) != KERN_SUCCESS)\
269 panic("DBGLOG_CPU_INIT cpu_handle allocation failed\n");\
270 bzero(*hdl_logpp, sizeof(cpu_signal_event_log_t)); \
91447636 271}
0a7de745
A
272#else /* MP_DEBUG */
273#define DBGLOG(log, _cpu, _event)
91447636 274#define DBGLOG_CPU_INIT(cpu)
0a7de745 275#endif /* MP_DEBUG */
55e303ae 276
0a7de745 277#endif /* ASSEMBLER */
1c79356b 278
b0d623f7 279#ifdef ASSEMBLER
0a7de745 280#define i_bit(bit, word) ((long)(*(word)) & (1L << (bit)))
b0d623f7 281#else
0a7de745
A
282__attribute__((always_inline)) static inline long
283i_bit_impl(long word, long bit)
284{
6d2010ae
A
285 long bitmask = 1L << bit;
286 return word & bitmask;
b0d623f7 287}
0a7de745 288#define i_bit(bit, word) i_bit_impl((long)(*(word)), bit)
b0d623f7 289#endif
1c79356b 290
1c79356b 291
593a1d5f 292#endif /* _I386_MP_H_ */
91447636
A
293
294#endif /* KERNEL_PRIVATE */