]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/processor.h
c96731742d90ed9461c759ca1afe1b1ed09d47eb
[apple/xnu.git] / osfmk / kern / processor.h
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * @OSF_COPYRIGHT@
25 */
26 /*
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
29 * All Rights Reserved.
30 *
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
36 *
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 *
41 * Carnegie Mellon requests users of this software to return to
42 *
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
47 *
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
50 */
51 /*
52 */
53
54 /*
55 * processor.h: Processor and processor-related definitions.
56 */
57
58 #ifndef _KERN_PROCESSOR_H_
59 #define _KERN_PROCESSOR_H_
60
61 #include <mach/boolean.h>
62 #include <mach/kern_return.h>
63 #include <kern/kern_types.h>
64
65 #include <sys/cdefs.h>
66
67 #ifdef MACH_KERNEL_PRIVATE
68
69 #include <mach/mach_types.h>
70 #include <kern/ast.h>
71 #include <kern/cpu_number.h>
72 #include <kern/lock.h>
73 #include <kern/queue.h>
74 #include <kern/sched.h>
75 #include <kern/processor_data.h>
76
77 #include <machine/ast_types.h>
78
79 struct processor_set {
80 queue_head_t idle_queue; /* idle processors */
81 int idle_count; /* how many ? */
82 queue_head_t active_queue; /* active processors */
83
84 queue_head_t processors; /* all processors here */
85 int processor_count;/* how many ? */
86 decl_simple_lock_data(,sched_lock) /* lock for runq and above */
87
88 struct run_queue runq; /* runq for this set */
89
90 queue_head_t tasks; /* tasks assigned */
91 int task_count; /* how many */
92 queue_head_t threads; /* threads in this set */
93 int thread_count; /* how many */
94 int ref_count; /* structure ref count */
95 boolean_t active; /* is pset in use */
96 decl_mutex_data(, lock) /* lock for above */
97
98 int timeshare_quanta; /* timeshare quantum factor */
99
100 struct ipc_port * pset_self; /* port for operations */
101 struct ipc_port * pset_name_self; /* port for information */
102
103 uint32_t run_count; /* threads running in set */
104 uint32_t share_count; /* timeshare threads running in set */
105
106 integer_t mach_factor; /* mach_factor */
107 integer_t load_average; /* load_average */
108
109 uint32_t pri_shift; /* timeshare usage -> priority */
110 };
111
112 extern struct processor_set default_pset;
113
114 struct processor {
115 queue_chain_t processor_queue;/* idle/active queue link,
116 * MUST remain the first element */
117 int state; /* See below */
118 struct thread
119 *active_thread, /* thread running on processor */
120 *next_thread, /* next thread when dispatched */
121 *idle_thread; /* this processor's idle thread. */
122
123 processor_set_t processor_set; /* current membership */
124
125 int current_pri; /* priority of current thread */
126
127 timer_call_data_t quantum_timer; /* timer for quantum expiration */
128 uint64_t quantum_end; /* time when current quantum ends */
129 uint64_t last_dispatch; /* time of last dispatch */
130
131 int timeslice; /* quanta before timeslice ends */
132 uint64_t deadline; /* current deadline */
133
134 struct run_queue runq; /* local runq for this processor */
135
136 queue_chain_t processors; /* processors in set */
137 decl_simple_lock_data(,lock)
138 struct ipc_port * processor_self; /* port for operations */
139 processor_t processor_list; /* all existing processors */
140 processor_data_t processor_data; /* per-processor data */
141 };
142
143 extern processor_t processor_list;
144 extern unsigned int processor_count;
145 decl_simple_lock_data(extern,processor_list_lock)
146
147 extern processor_t master_processor;
148
149 /*
150 * NOTE: The processor->processor_set link is needed in one of the
151 * scheduler's critical paths. [Figure out where to look for another
152 * thread to run on this processor.] It is accessed without locking.
153 * The following access protocol controls this field.
154 *
155 * Read from own processor - just read.
156 * Read from another processor - lock processor structure during read.
157 * Write from own processor - lock processor structure during write.
158 * Write from another processor - NOT PERMITTED.
159 *
160 */
161
162 /*
163 * Processor state locking:
164 *
165 * Values for the processor state are defined below. If the processor
166 * is off-line or being shutdown, then it is only necessary to lock
167 * the processor to change its state. Otherwise it is only necessary
168 * to lock its processor set's sched_lock. Scheduler code will
169 * typically lock only the sched_lock, but processor manipulation code
170 * will often lock both.
171 */
172
173 #define PROCESSOR_OFF_LINE 0 /* Not available */
174 #define PROCESSOR_RUNNING 1 /* Normal execution */
175 #define PROCESSOR_IDLE 2 /* Idle */
176 #define PROCESSOR_DISPATCHING 3 /* Dispatching (idle -> running) */
177 #define PROCESSOR_SHUTDOWN 4 /* Going off-line */
178 #define PROCESSOR_START 5 /* Being started */
179
180 extern processor_t current_processor(void);
181
182 extern processor_t cpu_to_processor(
183 int cpu);
184
185 /* Useful lock macros */
186
187 #define pset_lock(pset) mutex_lock(&(pset)->lock)
188 #define pset_lock_try(pset) mutex_try(&(pset)->lock)
189 #define pset_unlock(pset) mutex_unlock(&(pset)->lock)
190
191 #define processor_lock(pr) simple_lock(&(pr)->lock)
192 #define processor_unlock(pr) simple_unlock(&(pr)->lock)
193
194 extern void processor_bootstrap(void);
195
196 extern void processor_init(
197 processor_t processor,
198 int slot_num);
199
200 extern void timeshare_quanta_update(
201 processor_set_t pset);
202
203 extern void pset_init(
204 processor_set_t pset);
205
206 #define pset_run_incr(pset) \
207 hw_atomic_add(&(pset)->run_count, 1)
208
209 #define pset_run_decr(pset) \
210 hw_atomic_sub(&(pset)->run_count, 1)
211
212 #define pset_share_incr(pset) \
213 hw_atomic_add(&(pset)->share_count, 1)
214
215 #define pset_share_decr(pset) \
216 hw_atomic_sub(&(pset)->share_count, 1)
217
218 extern kern_return_t processor_shutdown(
219 processor_t processor);
220
221 extern void pset_remove_processor(
222 processor_set_t pset,
223 processor_t processor);
224
225 extern void pset_add_processor(
226 processor_set_t pset,
227 processor_t processor);
228
229 extern void pset_remove_task(
230 processor_set_t pset,
231 task_t task);
232
233 extern void pset_add_task(
234 processor_set_t pset,
235 task_t task);
236
237 extern void pset_remove_thread(
238 processor_set_t pset,
239 thread_t thread);
240
241 extern void pset_add_thread(
242 processor_set_t pset,
243 thread_t thread);
244
245 extern void thread_change_psets(
246 thread_t thread,
247 processor_set_t old_pset,
248 processor_set_t new_pset);
249
250
251 extern kern_return_t processor_info_count(
252 processor_flavor_t flavor,
253 mach_msg_type_number_t *count);
254
255 #endif /* MACH_KERNEL_PRIVATE */
256
257 __BEGIN_DECLS
258
259 extern void pset_deallocate(
260 processor_set_t pset);
261
262 extern void pset_reference(
263 processor_set_t pset);
264
265 __END_DECLS
266
267 #endif /* _KERN_PROCESSOR_H_ */