]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/processor.h
6c238db71a2d3df50326814efe2c6f07f6f12eef
[apple/xnu.git] / osfmk / kern / processor.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
32 *
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
38 *
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53 /*
54 */
55
56 /*
57 * processor.h: Processor and processor-set definitions.
58 */
59
60 #ifndef _KERN_PROCESSOR_H_
61 #define _KERN_PROCESSOR_H_
62
63 /*
64 * Data structures for managing processors and sets of processors.
65 */
66 #include <mach/boolean.h>
67 #include <mach/kern_return.h>
68 #include <kern/kern_types.h>
69
70 #include <sys/appleapiopts.h>
71
72 #ifdef __APPLE_API_PRIVATE
73
74 #ifdef MACH_KERNEL_PRIVATE
75
76 #include <cpus.h>
77
78 #include <mach/mach_types.h>
79 #include <kern/cpu_number.h>
80 #include <kern/lock.h>
81 #include <kern/queue.h>
82 #include <kern/sched.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 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 int quantum_factors[NCPUS+1];
107
108 struct ipc_port * pset_self; /* port for operations */
109 struct ipc_port * pset_name_self; /* port for information */
110
111 uint32_t run_count; /* threads running in set */
112 uint32_t share_count; /* timeshare threads running in set */
113
114 integer_t mach_factor; /* mach_factor */
115 integer_t load_average; /* load_average */
116 uint32_t sched_load; /* load avg for scheduler */
117 };
118
119 struct processor {
120 queue_chain_t processor_queue;/* idle/active/action queue link,
121 * MUST remain the first element */
122 int state; /* See below */
123 struct thread
124 *active_thread, /* thread running on processor */
125 *next_thread, /* next thread to run if dispatched */
126 *idle_thread; /* this processor's idle thread. */
127
128 processor_set_t processor_set; /* current membership */
129
130 int current_pri; /* priority of current thread */
131
132 timer_call_data_t quantum_timer; /* timer for quantum expiration */
133 uint64_t quantum_end; /* time when current quantum ends */
134 uint64_t last_dispatch; /* time of last dispatch */
135
136 int timeslice; /* quanta before timeslice ends */
137 uint64_t deadline; /* current deadline */
138
139 struct run_queue runq; /* local runq for this processor */
140
141 queue_chain_t processors; /* all processors in set */
142 decl_simple_lock_data(,lock)
143 struct ipc_port *processor_self;/* port for operations */
144 int slot_num; /* machine-indep slot number */
145 };
146
147 extern struct processor_set default_pset;
148 extern processor_t master_processor;
149
150 extern struct processor processor_array[NCPUS];
151
152 /*
153 * NOTE: The processor->processor_set link is needed in one of the
154 * scheduler's critical paths. [Figure out where to look for another
155 * thread to run on this processor.] It is accessed without locking.
156 * The following access protocol controls this field.
157 *
158 * Read from own processor - just read.
159 * Read from another processor - lock processor structure during read.
160 * Write from own processor - lock processor structure during write.
161 * Write from another processor - NOT PERMITTED.
162 *
163 */
164
165 /*
166 * Processor state locking:
167 *
168 * Values for the processor state are defined below. If the processor
169 * is off-line or being shutdown, then it is only necessary to lock
170 * the processor to change its state. Otherwise it is only necessary
171 * to lock its processor set's sched_lock. Scheduler code will
172 * typically lock only the sched_lock, but processor manipulation code
173 * will often lock both.
174 */
175
176 #define PROCESSOR_OFF_LINE 0 /* Not available */
177 #define PROCESSOR_RUNNING 1 /* Normal execution */
178 #define PROCESSOR_IDLE 2 /* Idle */
179 #define PROCESSOR_DISPATCHING 3 /* Dispatching (idle -> running) */
180 #define PROCESSOR_SHUTDOWN 4 /* Going off-line */
181 #define PROCESSOR_START 5 /* Being started */
182
183 /*
184 * Use processor ptr array to find current processor's data structure.
185 * This replaces a multiplication (index into processor_array) with
186 * an array lookup and a memory reference. It also allows us to save
187 * space if processor numbering gets too sparse.
188 */
189
190 extern processor_t processor_ptr[NCPUS];
191
192 #define cpu_to_processor(i) (processor_ptr[i])
193
194 #define current_processor() (processor_ptr[cpu_number()])
195
196 /* Compatibility -- will go away */
197
198 #define cpu_state(slot_num) (processor_ptr[slot_num]->state)
199 #define cpu_idle(slot_num) (cpu_state(slot_num) == PROCESSOR_IDLE)
200
201 /* Useful lock macros */
202
203 #define pset_lock(pset) mutex_lock(&(pset)->lock)
204 #define pset_lock_try(pset) mutex_try(&(pset)->lock)
205 #define pset_unlock(pset) mutex_unlock(&(pset)->lock)
206
207 #define processor_lock(pr) simple_lock(&(pr)->lock)
208 #define processor_unlock(pr) simple_unlock(&(pr)->lock)
209
210 extern void pset_sys_bootstrap(void);
211
212 #define timeshare_quanta_update(pset) \
213 MACRO_BEGIN \
214 int proc_count = (pset)->processor_count; \
215 int runq_count = (pset)->runq.count; \
216 \
217 (pset)->timeshare_quanta = (pset)->quantum_factors[ \
218 (runq_count > proc_count)? \
219 proc_count: runq_count]; \
220 MACRO_END
221
222 #define pset_run_incr(pset) \
223 hw_atomic_add(&(pset)->run_count, 1)
224
225 #define pset_run_decr(pset) \
226 hw_atomic_sub(&(pset)->run_count, 1)
227
228 #define pset_share_incr(pset) \
229 hw_atomic_add(&(pset)->share_count, 1)
230
231 #define pset_share_decr(pset) \
232 hw_atomic_sub(&(pset)->share_count, 1)
233
234 extern void cpu_up(
235 int cpu);
236
237 extern kern_return_t processor_shutdown(
238 processor_t processor);
239
240 extern void pset_remove_processor(
241 processor_set_t pset,
242 processor_t processor);
243
244 extern void pset_add_processor(
245 processor_set_t pset,
246 processor_t processor);
247
248 extern void pset_remove_task(
249 processor_set_t pset,
250 task_t task);
251
252 extern void pset_add_task(
253 processor_set_t pset,
254 task_t task);
255
256 extern void pset_remove_thread(
257 processor_set_t pset,
258 thread_t thread);
259
260 extern void pset_add_thread(
261 processor_set_t pset,
262 thread_t thread);
263
264 extern void thread_change_psets(
265 thread_t thread,
266 processor_set_t old_pset,
267 processor_set_t new_pset);
268
269 extern kern_return_t processor_assign(
270 processor_t processor,
271 processor_set_t new_pset,
272 boolean_t wait);
273
274 extern kern_return_t processor_info_count(
275 processor_flavor_t flavor,
276 mach_msg_type_number_t *count);
277
278 #endif /* MACH_KERNEL_PRIVATE */
279
280 extern kern_return_t processor_start(
281 processor_t processor);
282
283 extern kern_return_t processor_exit(
284 processor_t processor);
285
286 #endif /* __APPLE_API_PRIVATE */
287
288 extern void pset_deallocate(
289 processor_set_t pset);
290
291 extern void pset_reference(
292 processor_set_t pset);
293
294 #endif /* _KERN_PROCESSOR_H_ */