]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/processor.h
xnu-3789.51.2.tar.gz
[apple/xnu.git] / osfmk / kern / processor.h
1 /*
2 * Copyright (c) 2000-2009 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 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 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 /*
60 * processor.h: Processor and processor-related definitions.
61 */
62
63 #ifndef _KERN_PROCESSOR_H_
64 #define _KERN_PROCESSOR_H_
65
66 #include <mach/boolean.h>
67 #include <mach/kern_return.h>
68 #include <kern/kern_types.h>
69
70 #include <sys/cdefs.h>
71
72 #ifdef MACH_KERNEL_PRIVATE
73
74 #include <mach/mach_types.h>
75 #include <kern/ast.h>
76 #include <kern/cpu_number.h>
77 #include <kern/smp.h>
78 #include <kern/simple_lock.h>
79 #include <kern/locks.h>
80 #include <kern/queue.h>
81 #include <kern/sched.h>
82 #include <mach/sfi_class.h>
83 #include <kern/processor_data.h>
84
85 struct processor_set {
86 queue_head_t active_queue; /* active processors */
87 queue_head_t idle_queue; /* idle processors */
88 queue_head_t idle_secondary_queue; /* idle secondary processors */
89
90 int online_processor_count;
91
92 int cpu_set_low, cpu_set_hi;
93 int cpu_set_count;
94 uint64_t recommended_bitmask;
95
96 #if __SMP__
97 decl_simple_lock_data(,sched_lock) /* lock for above */
98 #endif
99
100 #if defined(CONFIG_SCHED_TRADITIONAL) || defined(CONFIG_SCHED_MULTIQ)
101 struct run_queue pset_runq; /* runq for this processor set */
102 #endif
103
104 #if defined(CONFIG_SCHED_TRADITIONAL)
105 int pset_runq_bound_count;
106 /* # of threads in runq bound to any processor in pset */
107 #endif
108
109 /* CPUs that have been sent an unacknowledged remote AST for scheduling purposes */
110 uint64_t pending_AST_cpu_mask;
111 #if defined(CONFIG_SCHED_DEFERRED_AST)
112 /*
113 * A seperate mask, for ASTs that we may be able to cancel. This is dependent on
114 * some level of support for requesting an AST on a processor, and then quashing
115 * that request later.
116 *
117 * The purpose of this field (and the associated codepaths) is to infer when we
118 * no longer need a processor that is DISPATCHING to come up, and to prevent it
119 * from coming out of IDLE if possible. This should serve to decrease the number
120 * of spurious ASTs in the system, and let processors spend longer periods in
121 * IDLE.
122 */
123 uint64_t pending_deferred_AST_cpu_mask;
124 #endif
125
126 struct ipc_port * pset_self; /* port for operations */
127 struct ipc_port * pset_name_self; /* port for information */
128
129 processor_set_t pset_list; /* chain of associated psets */
130 pset_node_t node;
131 };
132
133 extern struct processor_set pset0;
134
135 struct pset_node {
136 processor_set_t psets; /* list of associated psets */
137
138 pset_node_t nodes; /* list of associated subnodes */
139 pset_node_t node_list; /* chain of associated nodes */
140
141 pset_node_t parent;
142 };
143
144 extern struct pset_node pset_node0;
145
146 extern queue_head_t tasks, terminated_tasks, threads, corpse_tasks; /* Terminated tasks are ONLY for stackshot */
147 extern int tasks_count, terminated_tasks_count, threads_count;
148 decl_lck_mtx_data(extern,tasks_threads_lock)
149 decl_lck_mtx_data(extern,tasks_corpse_lock)
150
151 struct processor {
152 queue_chain_t processor_queue;/* idle/active queue link,
153 * MUST remain the first element */
154 int state; /* See below */
155 boolean_t is_SMT;
156 boolean_t is_recommended;
157 struct thread
158 *active_thread, /* thread running on processor */
159 *next_thread, /* next thread when dispatched */
160 *idle_thread; /* this processor's idle thread. */
161
162 processor_set_t processor_set; /* assigned set */
163
164 int current_pri; /* priority of current thread */
165 sched_mode_t current_thmode; /* sched mode of current thread */
166 sfi_class_id_t current_sfi_class; /* SFI class of current thread */
167 int starting_pri; /* priority of current thread as it was when scheduled */
168 int cpu_id; /* platform numeric id */
169
170 timer_call_data_t quantum_timer; /* timer for quantum expiration */
171 uint64_t quantum_end; /* time when current quantum ends */
172 uint64_t last_dispatch; /* time of last dispatch */
173
174 uint64_t deadline; /* current deadline */
175 boolean_t first_timeslice; /* has the quantum expired since context switch */
176
177 #if defined(CONFIG_SCHED_TRADITIONAL) || defined(CONFIG_SCHED_MULTIQ)
178 struct run_queue runq; /* runq for this processor */
179 #endif
180
181 #if defined(CONFIG_SCHED_TRADITIONAL)
182 int runq_bound_count; /* # of threads bound to this processor */
183 #endif
184 #if defined(CONFIG_SCHED_GRRR)
185 struct grrr_run_queue grrr_runq; /* Group Ratio Round-Robin runq */
186 #endif
187
188 processor_t processor_primary; /* pointer to primary processor for
189 * secondary SMT processors, or a pointer
190 * to ourselves for primaries or non-SMT */
191 processor_t processor_secondary;
192 struct ipc_port * processor_self; /* port for operations */
193
194 processor_t processor_list; /* all existing processors */
195 processor_data_t processor_data; /* per-processor data */
196 };
197
198 extern processor_t processor_list;
199 extern unsigned int processor_count;
200 decl_simple_lock_data(extern,processor_list_lock)
201
202 extern uint32_t processor_avail_count;
203
204 extern processor_t master_processor;
205
206 extern boolean_t sched_stats_active;
207
208 /*
209 * Processor state is accessed by locking the scheduling lock
210 * for the assigned processor set.
211 *
212 * -------------------- SHUTDOWN
213 * / ^ ^
214 * _/ | \
215 * OFF_LINE ---> START ---> RUNNING ---> IDLE ---> DISPATCHING
216 * \_________________^ ^ ^______/ /
217 * \__________________/
218 *
219 * Most of these state transitions are externally driven as a
220 * a directive (for instance telling an IDLE processor to start
221 * coming out of the idle state to run a thread). However these
222 * are typically paired with a handshake by the processor itself
223 * to indicate that it has completed a transition of indeterminate
224 * length (for example, the DISPATCHING->RUNNING or START->RUNNING
225 * transitions must occur on the processor itself).
226 *
227 * The boot processor has some special cases, and skips the START state,
228 * since it has already bootstrapped and is ready to context switch threads.
229 *
230 * When a processor is in DISPATCHING or RUNNING state, the current_pri,
231 * current_thmode, and deadline fields should be set, so that other
232 * processors can evaluate if it is an appropriate candidate for preemption.
233 */
234 #if defined(CONFIG_SCHED_DEFERRED_AST)
235 /*
236 * -------------------- SHUTDOWN
237 * / ^ ^
238 * _/ | \
239 * OFF_LINE ---> START ---> RUNNING ---> IDLE ---> DISPATCHING
240 * \_________________^ ^ ^______/ ^_____ / /
241 * \__________________/
242 *
243 * A DISPATCHING processor may be put back into IDLE, if another
244 * processor determines that the target processor will have nothing to do
245 * upon reaching the RUNNING state. This is racy, but if the target
246 * responds and becomes RUNNING, it will not break the processor state
247 * machine.
248 *
249 * This change allows us to cancel an outstanding signal/AST on a processor
250 * (if such an operation is supported through hardware or software), and
251 * push the processor back into the IDLE state as a power optimization.
252 */
253 #endif
254
255 #define PROCESSOR_OFF_LINE 0 /* Not available */
256 #define PROCESSOR_SHUTDOWN 1 /* Going off-line */
257 #define PROCESSOR_START 2 /* Being started */
258 /* 3 Formerly Inactive (unavailable) */
259 #define PROCESSOR_IDLE 4 /* Idle (available) */
260 #define PROCESSOR_DISPATCHING 5 /* Dispatching (idle -> active) */
261 #define PROCESSOR_RUNNING 6 /* Normal execution */
262
263 extern processor_t current_processor(void);
264
265 /* Lock macros, always acquired and released with interrupts disabled (splsched()) */
266
267 #if __SMP__
268 #define pset_lock(p) simple_lock(&(p)->sched_lock)
269 #define pset_unlock(p) simple_unlock(&(p)->sched_lock)
270 #define pset_lock_init(p) simple_lock_init(&(p)->sched_lock, 0)
271 #else
272 #define pset_lock(p) do { (void)p; } while(0)
273 #define pset_unlock(p) do { (void)p; } while(0)
274 #define pset_lock_init(p) do { (void)p; } while(0)
275 #endif
276
277 extern void processor_bootstrap(void);
278
279 extern void processor_init(
280 processor_t processor,
281 int cpu_id,
282 processor_set_t processor_set);
283
284 extern void processor_set_primary(
285 processor_t processor,
286 processor_t primary);
287
288 extern kern_return_t processor_shutdown(
289 processor_t processor);
290
291 extern void processor_queue_shutdown(
292 processor_t processor);
293
294 extern processor_set_t processor_pset(
295 processor_t processor);
296
297 extern pset_node_t pset_node_root(void);
298
299 extern processor_set_t pset_create(
300 pset_node_t node);
301
302 extern void pset_init(
303 processor_set_t pset,
304 pset_node_t node);
305
306 extern kern_return_t processor_info_count(
307 processor_flavor_t flavor,
308 mach_msg_type_number_t *count);
309
310 #define pset_deallocate(x)
311 #define pset_reference(x)
312
313 extern void machine_run_count(
314 uint32_t count);
315
316 extern processor_t machine_choose_processor(
317 processor_set_t pset,
318 processor_t processor);
319
320 #define next_pset(p) (((p)->pset_list != PROCESSOR_SET_NULL)? (p)->pset_list: (p)->node->psets)
321
322 #define PSET_THING_TASK 0
323 #define PSET_THING_THREAD 1
324
325 extern kern_return_t processor_set_things(
326 processor_set_t pset,
327 void **thing_list,
328 mach_msg_type_number_t *count,
329 int type);
330
331 #else /* MACH_KERNEL_PRIVATE */
332
333 __BEGIN_DECLS
334
335 extern void pset_deallocate(
336 processor_set_t pset);
337
338 extern void pset_reference(
339 processor_set_t pset);
340
341 __END_DECLS
342
343 #endif /* MACH_KERNEL_PRIVATE */
344
345 #ifdef KERNEL_PRIVATE
346 __BEGIN_DECLS
347 extern processor_t cpu_to_processor(int cpu);
348 __END_DECLS
349
350 #endif /* KERNEL_PRIVATE */
351
352 #endif /* _KERN_PROCESSOR_H_ */