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