]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/processor.h
xnu-517.9.4.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 *
e5568f75
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
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.
1c79356b
A
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-set definitions.
55 */
56
57#ifndef _KERN_PROCESSOR_H_
58#define _KERN_PROCESSOR_H_
59
60/*
61 * Data structures for managing processors and sets of processors.
62 */
63#include <mach/boolean.h>
64#include <mach/kern_return.h>
65#include <kern/kern_types.h>
66
9bccf70c
A
67#include <sys/appleapiopts.h>
68
69#ifdef __APPLE_API_PRIVATE
1c79356b 70
9bccf70c 71#ifdef MACH_KERNEL_PRIVATE
1c79356b
A
72
73#include <cpus.h>
1c79356b
A
74
75#include <mach/mach_types.h>
76#include <kern/cpu_number.h>
77#include <kern/lock.h>
78#include <kern/queue.h>
79#include <kern/sched.h>
80
1c79356b 81#include <machine/ast_types.h>
1c79356b
A
82
83struct processor_set {
1c79356b
A
84 queue_head_t idle_queue; /* idle processors */
85 int idle_count; /* how many ? */
9bccf70c 86 queue_head_t active_queue; /* active processors */
9bccf70c 87
1c79356b
A
88 queue_head_t processors; /* all processors here */
89 int processor_count;/* how many ? */
55e303ae 90 decl_simple_lock_data(,sched_lock) /* lock for above */
9bccf70c
A
91
92 struct run_queue runq; /* runq for this set */
93
1c79356b
A
94 queue_head_t tasks; /* tasks assigned */
95 int task_count; /* how many */
96 queue_head_t threads; /* threads in this set */
97 int thread_count; /* how many */
98 int ref_count; /* structure ref count */
99 boolean_t active; /* is pset in use */
9bccf70c
A
100 decl_mutex_data(, lock) /* lock for above */
101
55e303ae
A
102 int timeshare_quanta; /* timeshare quantum factor */
103 int quantum_factors[NCPUS+1];
9bccf70c
A
104
105 struct ipc_port * pset_self; /* port for operations */
106 struct ipc_port * pset_name_self; /* port for information */
107
55e303ae
A
108 uint32_t run_count; /* threads running in set */
109 uint32_t share_count; /* timeshare threads running in set */
9bccf70c 110
1c79356b
A
111 integer_t mach_factor; /* mach_factor */
112 integer_t load_average; /* load_average */
9bccf70c 113 uint32_t sched_load; /* load avg for scheduler */
1c79356b
A
114};
115
116struct processor {
9bccf70c
A
117 queue_chain_t processor_queue;/* idle/active/action queue link,
118 * MUST remain the first element */
1c79356b 119 int state; /* See below */
55e303ae
A
120 struct thread
121 *active_thread, /* thread running on processor */
1c79356b
A
122 *next_thread, /* next thread to run if dispatched */
123 *idle_thread; /* this processor's idle thread. */
55e303ae
A
124
125 processor_set_t processor_set; /* current membership */
126
127 int current_pri; /* priority of current thread */
128
0b4e3aa0 129 timer_call_data_t quantum_timer; /* timer for quantum expiration */
0b4e3aa0
A
130 uint64_t quantum_end; /* time when current quantum ends */
131 uint64_t last_dispatch; /* time of last dispatch */
1c79356b 132
55e303ae
A
133 int timeslice; /* quanta before timeslice ends */
134 uint64_t deadline; /* current deadline */
135
9bccf70c
A
136 struct run_queue runq; /* local runq for this processor */
137
55e303ae 138 queue_chain_t processors; /* all processors in set */
1c79356b
A
139 decl_simple_lock_data(,lock)
140 struct ipc_port *processor_self;/* port for operations */
141 int slot_num; /* machine-indep slot number */
1c79356b
A
142};
143
9bccf70c
A
144extern struct processor_set default_pset;
145extern processor_t master_processor;
146
1c79356b
A
147extern struct processor processor_array[NCPUS];
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
9bccf70c
A
168 * to lock its processor set's sched_lock. Scheduler code will
169 * typically lock only the sched_lock, but processor manipulation code
1c79356b
A
170 * will often lock both.
171 */
172
55e303ae
A
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 */
1c79356b
A
179
180/*
181 * Use processor ptr array to find current processor's data structure.
182 * This replaces a multiplication (index into processor_array) with
183 * an array lookup and a memory reference. It also allows us to save
184 * space if processor numbering gets too sparse.
185 */
186
187extern processor_t processor_ptr[NCPUS];
188
189#define cpu_to_processor(i) (processor_ptr[i])
190
191#define current_processor() (processor_ptr[cpu_number()])
1c79356b
A
192
193/* Compatibility -- will go away */
194
195#define cpu_state(slot_num) (processor_ptr[slot_num]->state)
196#define cpu_idle(slot_num) (cpu_state(slot_num) == PROCESSOR_IDLE)
197
198/* Useful lock macros */
199
200#define pset_lock(pset) mutex_lock(&(pset)->lock)
201#define pset_lock_try(pset) mutex_try(&(pset)->lock)
202#define pset_unlock(pset) mutex_unlock(&(pset)->lock)
203
204#define processor_lock(pr) simple_lock(&(pr)->lock)
205#define processor_unlock(pr) simple_unlock(&(pr)->lock)
206
1c79356b
A
207extern void pset_sys_bootstrap(void);
208
55e303ae 209#define timeshare_quanta_update(pset) \
9bccf70c
A
210MACRO_BEGIN \
211 int proc_count = (pset)->processor_count; \
212 int runq_count = (pset)->runq.count; \
213 \
55e303ae 214 (pset)->timeshare_quanta = (pset)->quantum_factors[ \
9bccf70c
A
215 (runq_count > proc_count)? \
216 proc_count: runq_count]; \
217MACRO_END
218
55e303ae
A
219#define pset_run_incr(pset) \
220 hw_atomic_add(&(pset)->run_count, 1)
221
222#define pset_run_decr(pset) \
223 hw_atomic_sub(&(pset)->run_count, 1)
224
225#define pset_share_incr(pset) \
226 hw_atomic_add(&(pset)->share_count, 1)
227
228#define pset_share_decr(pset) \
229 hw_atomic_sub(&(pset)->share_count, 1)
1c79356b
A
230
231extern void cpu_up(
55e303ae 232 int cpu);
1c79356b
A
233
234extern kern_return_t processor_shutdown(
55e303ae 235 processor_t processor);
1c79356b
A
236
237extern void pset_remove_processor(
55e303ae
A
238 processor_set_t pset,
239 processor_t processor);
1c79356b
A
240
241extern void pset_add_processor(
55e303ae
A
242 processor_set_t pset,
243 processor_t processor);
1c79356b
A
244
245extern void pset_remove_task(
55e303ae
A
246 processor_set_t pset,
247 task_t task);
1c79356b
A
248
249extern void pset_add_task(
55e303ae
A
250 processor_set_t pset,
251 task_t task);
1c79356b
A
252
253extern void pset_remove_thread(
55e303ae
A
254 processor_set_t pset,
255 thread_t thread);
1c79356b
A
256
257extern void pset_add_thread(
55e303ae
A
258 processor_set_t pset,
259 thread_t thread);
1c79356b
A
260
261extern void thread_change_psets(
55e303ae
A
262 thread_t thread,
263 processor_set_t old_pset,
264 processor_set_t new_pset);
1c79356b 265
1c79356b 266extern kern_return_t processor_assign(
55e303ae
A
267 processor_t processor,
268 processor_set_t new_pset,
269 boolean_t wait);
1c79356b
A
270
271extern kern_return_t processor_info_count(
55e303ae
A
272 processor_flavor_t flavor,
273 mach_msg_type_number_t *count);
9bccf70c
A
274
275#endif /* MACH_KERNEL_PRIVATE */
1c79356b
A
276
277extern kern_return_t processor_start(
55e303ae 278 processor_t processor);
1c79356b
A
279
280extern kern_return_t processor_exit(
55e303ae 281 processor_t processor);
1c79356b 282
9bccf70c
A
283#endif /* __APPLE_API_PRIVATE */
284
285extern void pset_deallocate(
55e303ae 286 processor_set_t pset);
9bccf70c
A
287
288extern void pset_reference(
55e303ae 289 processor_set_t pset);
9bccf70c 290
1c79356b 291#endif /* _KERN_PROCESSOR_H_ */