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