]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/processor.h
xnu-517.tar.gz
[apple/xnu.git] / osfmk / kern / processor.h
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
43866e37
A
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
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
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.
1c79356b
A
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
9bccf70c
A
70#include <sys/appleapiopts.h>
71
72#ifdef __APPLE_API_PRIVATE
1c79356b 73
9bccf70c 74#ifdef MACH_KERNEL_PRIVATE
1c79356b
A
75
76#include <cpus.h>
1c79356b
A
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
1c79356b 84#include <machine/ast_types.h>
1c79356b
A
85
86struct processor_set {
1c79356b
A
87 queue_head_t idle_queue; /* idle processors */
88 int idle_count; /* how many ? */
9bccf70c 89 queue_head_t active_queue; /* active processors */
9bccf70c 90
1c79356b
A
91 queue_head_t processors; /* all processors here */
92 int processor_count;/* how many ? */
55e303ae 93 decl_simple_lock_data(,sched_lock) /* lock for above */
9bccf70c
A
94
95 struct run_queue runq; /* runq for this set */
96
1c79356b
A
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 */
9bccf70c
A
103 decl_mutex_data(, lock) /* lock for above */
104
55e303ae
A
105 int timeshare_quanta; /* timeshare quantum factor */
106 int quantum_factors[NCPUS+1];
9bccf70c
A
107
108 struct ipc_port * pset_self; /* port for operations */
109 struct ipc_port * pset_name_self; /* port for information */
110
55e303ae
A
111 uint32_t run_count; /* threads running in set */
112 uint32_t share_count; /* timeshare threads running in set */
9bccf70c 113
1c79356b
A
114 integer_t mach_factor; /* mach_factor */
115 integer_t load_average; /* load_average */
9bccf70c 116 uint32_t sched_load; /* load avg for scheduler */
1c79356b
A
117};
118
119struct processor {
9bccf70c
A
120 queue_chain_t processor_queue;/* idle/active/action queue link,
121 * MUST remain the first element */
1c79356b 122 int state; /* See below */
55e303ae
A
123 struct thread
124 *active_thread, /* thread running on processor */
1c79356b
A
125 *next_thread, /* next thread to run if dispatched */
126 *idle_thread; /* this processor's idle thread. */
55e303ae
A
127
128 processor_set_t processor_set; /* current membership */
129
130 int current_pri; /* priority of current thread */
131
0b4e3aa0 132 timer_call_data_t quantum_timer; /* timer for quantum expiration */
0b4e3aa0
A
133 uint64_t quantum_end; /* time when current quantum ends */
134 uint64_t last_dispatch; /* time of last dispatch */
1c79356b 135
55e303ae
A
136 int timeslice; /* quanta before timeslice ends */
137 uint64_t deadline; /* current deadline */
138
9bccf70c
A
139 struct run_queue runq; /* local runq for this processor */
140
55e303ae 141 queue_chain_t processors; /* all processors in set */
1c79356b
A
142 decl_simple_lock_data(,lock)
143 struct ipc_port *processor_self;/* port for operations */
144 int slot_num; /* machine-indep slot number */
1c79356b
A
145};
146
9bccf70c
A
147extern struct processor_set default_pset;
148extern processor_t master_processor;
149
1c79356b
A
150extern 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
9bccf70c
A
171 * to lock its processor set's sched_lock. Scheduler code will
172 * typically lock only the sched_lock, but processor manipulation code
1c79356b
A
173 * will often lock both.
174 */
175
55e303ae
A
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 */
1c79356b
A
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
190extern processor_t processor_ptr[NCPUS];
191
192#define cpu_to_processor(i) (processor_ptr[i])
193
194#define current_processor() (processor_ptr[cpu_number()])
1c79356b
A
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
1c79356b
A
210extern void pset_sys_bootstrap(void);
211
55e303ae 212#define timeshare_quanta_update(pset) \
9bccf70c
A
213MACRO_BEGIN \
214 int proc_count = (pset)->processor_count; \
215 int runq_count = (pset)->runq.count; \
216 \
55e303ae 217 (pset)->timeshare_quanta = (pset)->quantum_factors[ \
9bccf70c
A
218 (runq_count > proc_count)? \
219 proc_count: runq_count]; \
220MACRO_END
221
55e303ae
A
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)
1c79356b
A
233
234extern void cpu_up(
55e303ae 235 int cpu);
1c79356b
A
236
237extern kern_return_t processor_shutdown(
55e303ae 238 processor_t processor);
1c79356b
A
239
240extern void pset_remove_processor(
55e303ae
A
241 processor_set_t pset,
242 processor_t processor);
1c79356b
A
243
244extern void pset_add_processor(
55e303ae
A
245 processor_set_t pset,
246 processor_t processor);
1c79356b
A
247
248extern void pset_remove_task(
55e303ae
A
249 processor_set_t pset,
250 task_t task);
1c79356b
A
251
252extern void pset_add_task(
55e303ae
A
253 processor_set_t pset,
254 task_t task);
1c79356b
A
255
256extern void pset_remove_thread(
55e303ae
A
257 processor_set_t pset,
258 thread_t thread);
1c79356b
A
259
260extern void pset_add_thread(
55e303ae
A
261 processor_set_t pset,
262 thread_t thread);
1c79356b
A
263
264extern void thread_change_psets(
55e303ae
A
265 thread_t thread,
266 processor_set_t old_pset,
267 processor_set_t new_pset);
1c79356b 268
1c79356b 269extern kern_return_t processor_assign(
55e303ae
A
270 processor_t processor,
271 processor_set_t new_pset,
272 boolean_t wait);
1c79356b
A
273
274extern kern_return_t processor_info_count(
55e303ae
A
275 processor_flavor_t flavor,
276 mach_msg_type_number_t *count);
9bccf70c
A
277
278#endif /* MACH_KERNEL_PRIVATE */
1c79356b
A
279
280extern kern_return_t processor_start(
55e303ae 281 processor_t processor);
1c79356b
A
282
283extern kern_return_t processor_exit(
55e303ae 284 processor_t processor);
1c79356b 285
9bccf70c
A
286#endif /* __APPLE_API_PRIVATE */
287
288extern void pset_deallocate(
55e303ae 289 processor_set_t pset);
9bccf70c
A
290
291extern void pset_reference(
55e303ae 292 processor_set_t pset);
9bccf70c 293
1c79356b 294#endif /* _KERN_PROCESSOR_H_ */