2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
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.
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.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
57 * processor.h: Processor and processor-set definitions.
60 #ifndef _KERN_PROCESSOR_H_
61 #define _KERN_PROCESSOR_H_
64 * Data structures for managing processors and sets of processors.
66 #include <mach/boolean.h>
67 #include <mach/kern_return.h>
68 #include <kern/kern_types.h>
70 #include <sys/appleapiopts.h>
72 #ifdef __APPLE_API_PRIVATE
74 #ifdef MACH_KERNEL_PRIVATE
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>
84 #include <machine/ast_types.h>
86 struct processor_set
{
87 queue_head_t idle_queue
; /* idle processors */
88 int idle_count
; /* how many ? */
89 queue_head_t active_queue
; /* active processors */
91 queue_head_t processors
; /* all processors here */
92 int processor_count
;/* how many ? */
93 decl_simple_lock_data(,sched_lock
) /* lock for above */
95 struct run_queue runq
; /* runq for this set */
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 */
103 decl_mutex_data(, lock
) /* lock for above */
105 int timeshare_quanta
; /* timeshare quantum factor */
106 int quantum_factors
[NCPUS
+1];
108 struct ipc_port
* pset_self
; /* port for operations */
109 struct ipc_port
* pset_name_self
; /* port for information */
111 uint32_t run_count
; /* threads running in set */
112 uint32_t share_count
; /* timeshare threads running in set */
114 integer_t mach_factor
; /* mach_factor */
115 integer_t load_average
; /* load_average */
116 uint32_t sched_load
; /* load avg for scheduler */
120 queue_chain_t processor_queue
;/* idle/active/action queue link,
121 * MUST remain the first element */
122 int state
; /* See below */
124 *active_thread
, /* thread running on processor */
125 *next_thread
, /* next thread to run if dispatched */
126 *idle_thread
; /* this processor's idle thread. */
128 processor_set_t processor_set
; /* current membership */
130 int current_pri
; /* priority of current thread */
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 */
136 int timeslice
; /* quanta before timeslice ends */
137 uint64_t deadline
; /* current deadline */
139 struct run_queue runq
; /* local runq for this processor */
141 queue_chain_t processors
; /* all processors in set */
142 decl_simple_lock_data(,lock
)
143 struct ipc_port
*processor_self
;/* port for operations */
144 int slot_num
; /* machine-indep slot number */
147 extern struct processor_set default_pset
;
148 extern processor_t master_processor
;
150 extern struct processor processor_array
[NCPUS
];
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.
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.
166 * Processor state locking:
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
171 * to lock its processor set's sched_lock. Scheduler code will
172 * typically lock only the sched_lock, but processor manipulation code
173 * will often lock both.
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 */
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.
190 extern processor_t processor_ptr
[NCPUS
];
192 #define cpu_to_processor(i) (processor_ptr[i])
194 #define current_processor() (processor_ptr[cpu_number()])
196 /* Compatibility -- will go away */
198 #define cpu_state(slot_num) (processor_ptr[slot_num]->state)
199 #define cpu_idle(slot_num) (cpu_state(slot_num) == PROCESSOR_IDLE)
201 /* Useful lock macros */
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)
207 #define processor_lock(pr) simple_lock(&(pr)->lock)
208 #define processor_unlock(pr) simple_unlock(&(pr)->lock)
210 extern void pset_sys_bootstrap(void);
212 #define timeshare_quanta_update(pset) \
214 int proc_count = (pset)->processor_count; \
215 int runq_count = (pset)->runq.count; \
217 (pset)->timeshare_quanta = (pset)->quantum_factors[ \
218 (runq_count > proc_count)? \
219 proc_count: runq_count]; \
222 #define pset_run_incr(pset) \
223 hw_atomic_add(&(pset)->run_count, 1)
225 #define pset_run_decr(pset) \
226 hw_atomic_sub(&(pset)->run_count, 1)
228 #define pset_share_incr(pset) \
229 hw_atomic_add(&(pset)->share_count, 1)
231 #define pset_share_decr(pset) \
232 hw_atomic_sub(&(pset)->share_count, 1)
237 extern kern_return_t
processor_shutdown(
238 processor_t processor
);
240 extern void pset_remove_processor(
241 processor_set_t pset
,
242 processor_t processor
);
244 extern void pset_add_processor(
245 processor_set_t pset
,
246 processor_t processor
);
248 extern void pset_remove_task(
249 processor_set_t pset
,
252 extern void pset_add_task(
253 processor_set_t pset
,
256 extern void pset_remove_thread(
257 processor_set_t pset
,
260 extern void pset_add_thread(
261 processor_set_t pset
,
264 extern void thread_change_psets(
266 processor_set_t old_pset
,
267 processor_set_t new_pset
);
269 extern kern_return_t
processor_assign(
270 processor_t processor
,
271 processor_set_t new_pset
,
274 extern kern_return_t
processor_info_count(
275 processor_flavor_t flavor
,
276 mach_msg_type_number_t
*count
);
278 #endif /* MACH_KERNEL_PRIVATE */
280 extern kern_return_t
processor_start(
281 processor_t processor
);
283 extern kern_return_t
processor_exit(
284 processor_t processor
);
286 #endif /* __APPLE_API_PRIVATE */
288 extern void pset_deallocate(
289 processor_set_t pset
);
291 extern void pset_reference(
292 processor_set_t pset
);
294 #endif /* _KERN_PROCESSOR_H_ */