]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/processor.h
22dcf08a1272c7c7eae1c6d24438b0f8936e877f
[apple/xnu.git] / osfmk / kern / processor.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,1989 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 /*
62 * processor.h: Processor and processor-related definitions.
63 */
64
65 #ifndef _KERN_PROCESSOR_H_
66 #define _KERN_PROCESSOR_H_
67
68 #include <mach/boolean.h>
69 #include <mach/kern_return.h>
70 #include <kern/kern_types.h>
71
72 #include <sys/cdefs.h>
73
74 #ifdef MACH_KERNEL_PRIVATE
75
76 #include <mach/mach_types.h>
77 #include <kern/ast.h>
78 #include <kern/cpu_number.h>
79 #include <kern/lock.h>
80 #include <kern/queue.h>
81 #include <kern/sched.h>
82 #include <kern/processor_data.h>
83
84 #include <machine/ast_types.h>
85
86 struct processor_set {
87 queue_head_t idle_queue; /* idle processors */
88 int idle_count; /* how many ? */
89 queue_head_t active_queue; /* active processors */
90
91 queue_head_t processors; /* all processors here */
92 int processor_count;/* how many ? */
93 decl_simple_lock_data(,sched_lock) /* lock for runq and above */
94
95 struct run_queue runq; /* runq for this set */
96
97 queue_head_t tasks; /* tasks assigned */
98 int task_count; /* how many */
99 queue_head_t threads; /* threads in this set */
100 int thread_count; /* how many */
101 int ref_count; /* structure ref count */
102 boolean_t active; /* is pset in use */
103 decl_mutex_data(, lock) /* lock for above */
104
105 int timeshare_quanta; /* timeshare quantum factor */
106
107 struct ipc_port * pset_self; /* port for operations */
108 struct ipc_port * pset_name_self; /* port for information */
109
110 uint32_t run_count; /* threads running in set */
111 uint32_t share_count; /* timeshare threads running in set */
112
113 integer_t mach_factor; /* mach_factor */
114 integer_t load_average; /* load_average */
115
116 uint32_t pri_shift; /* timeshare usage -> priority */
117 };
118
119 extern struct processor_set default_pset;
120
121 struct processor {
122 queue_chain_t processor_queue;/* idle/active queue link,
123 * MUST remain the first element */
124 int state; /* See below */
125 struct thread
126 *active_thread, /* thread running on processor */
127 *next_thread, /* next thread when dispatched */
128 *idle_thread; /* this processor's idle thread. */
129
130 processor_set_t processor_set; /* current membership */
131
132 int current_pri; /* priority of current thread */
133
134 timer_call_data_t quantum_timer; /* timer for quantum expiration */
135 uint64_t quantum_end; /* time when current quantum ends */
136 uint64_t last_dispatch; /* time of last dispatch */
137
138 int timeslice; /* quanta before timeslice ends */
139 uint64_t deadline; /* current deadline */
140
141 struct run_queue runq; /* local runq for this processor */
142
143 queue_chain_t processors; /* processors in set */
144 decl_simple_lock_data(,lock)
145 struct ipc_port * processor_self; /* port for operations */
146 processor_t processor_list; /* all existing processors */
147 processor_data_t processor_data; /* per-processor data */
148 };
149
150 extern processor_t processor_list;
151 extern unsigned int processor_count;
152 decl_simple_lock_data(extern,processor_list_lock)
153
154 extern processor_t master_processor;
155
156 /*
157 * NOTE: The processor->processor_set link is needed in one of the
158 * scheduler's critical paths. [Figure out where to look for another
159 * thread to run on this processor.] It is accessed without locking.
160 * The following access protocol controls this field.
161 *
162 * Read from own processor - just read.
163 * Read from another processor - lock processor structure during read.
164 * Write from own processor - lock processor structure during write.
165 * Write from another processor - NOT PERMITTED.
166 *
167 */
168
169 /*
170 * Processor state locking:
171 *
172 * Values for the processor state are defined below. If the processor
173 * is off-line or being shutdown, then it is only necessary to lock
174 * the processor to change its state. Otherwise it is only necessary
175 * to lock its processor set's sched_lock. Scheduler code will
176 * typically lock only the sched_lock, but processor manipulation code
177 * will often lock both.
178 */
179
180 #define PROCESSOR_OFF_LINE 0 /* Not available */
181 #define PROCESSOR_RUNNING 1 /* Normal execution */
182 #define PROCESSOR_IDLE 2 /* Idle */
183 #define PROCESSOR_DISPATCHING 3 /* Dispatching (idle -> running) */
184 #define PROCESSOR_SHUTDOWN 4 /* Going off-line */
185 #define PROCESSOR_START 5 /* Being started */
186
187 extern processor_t current_processor(void);
188
189 extern processor_t cpu_to_processor(
190 int cpu);
191
192 /* Useful lock macros */
193
194 #define pset_lock(pset) mutex_lock(&(pset)->lock)
195 #define pset_lock_try(pset) mutex_try(&(pset)->lock)
196 #define pset_unlock(pset) mutex_unlock(&(pset)->lock)
197
198 #define processor_lock(pr) simple_lock(&(pr)->lock)
199 #define processor_unlock(pr) simple_unlock(&(pr)->lock)
200
201 extern void processor_bootstrap(void);
202
203 extern void processor_init(
204 processor_t processor,
205 int slot_num);
206
207 extern void timeshare_quanta_update(
208 processor_set_t pset);
209
210 extern void pset_init(
211 processor_set_t pset);
212
213 #define pset_run_incr(pset) \
214 hw_atomic_add(&(pset)->run_count, 1)
215
216 #define pset_run_decr(pset) \
217 hw_atomic_sub(&(pset)->run_count, 1)
218
219 #define pset_share_incr(pset) \
220 hw_atomic_add(&(pset)->share_count, 1)
221
222 #define pset_share_decr(pset) \
223 hw_atomic_sub(&(pset)->share_count, 1)
224
225 extern kern_return_t processor_shutdown(
226 processor_t processor);
227
228 extern void pset_remove_processor(
229 processor_set_t pset,
230 processor_t processor);
231
232 extern void pset_add_processor(
233 processor_set_t pset,
234 processor_t processor);
235
236 extern void pset_remove_task(
237 processor_set_t pset,
238 task_t task);
239
240 extern void pset_add_task(
241 processor_set_t pset,
242 task_t task);
243
244 extern void pset_remove_thread(
245 processor_set_t pset,
246 thread_t thread);
247
248 extern void pset_add_thread(
249 processor_set_t pset,
250 thread_t thread);
251
252 extern void thread_change_psets(
253 thread_t thread,
254 processor_set_t old_pset,
255 processor_set_t new_pset);
256
257
258 extern kern_return_t processor_info_count(
259 processor_flavor_t flavor,
260 mach_msg_type_number_t *count);
261
262 #endif /* MACH_KERNEL_PRIVATE */
263
264 __BEGIN_DECLS
265
266 extern void pset_deallocate(
267 processor_set_t pset);
268
269 extern void pset_reference(
270 processor_set_t pset);
271
272 __END_DECLS
273
274 #endif /* _KERN_PROCESSOR_H_ */