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