2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
29 * All Rights Reserved.
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.
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.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
55 * processor.h: Processor and processor-related definitions.
58 #ifndef _KERN_PROCESSOR_H_
59 #define _KERN_PROCESSOR_H_
61 #include <mach/boolean.h>
62 #include <mach/kern_return.h>
63 #include <kern/kern_types.h>
65 #include <sys/cdefs.h>
67 #ifdef MACH_KERNEL_PRIVATE
69 #include <mach/mach_types.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>
77 #include <machine/ast_types.h>
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 */
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 */
88 struct run_queue runq
; /* runq for this set */
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 */
98 int timeshare_quanta
; /* timeshare quantum factor */
100 struct ipc_port
* pset_self
; /* port for operations */
101 struct ipc_port
* pset_name_self
; /* port for information */
103 uint32_t run_count
; /* threads running in set */
104 uint32_t share_count
; /* timeshare threads running in set */
106 integer_t mach_factor
; /* mach_factor */
107 integer_t load_average
; /* load_average */
109 uint32_t pri_shift
; /* timeshare usage -> priority */
112 extern struct processor_set default_pset
;
115 queue_chain_t processor_queue
;/* idle/active queue link,
116 * MUST remain the first element */
117 int state
; /* See below */
119 *active_thread
, /* thread running on processor */
120 *next_thread
, /* next thread when dispatched */
121 *idle_thread
; /* this processor's idle thread. */
123 processor_set_t processor_set
; /* current membership */
125 int current_pri
; /* priority of current thread */
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 */
131 int timeslice
; /* quanta before timeslice ends */
132 uint64_t deadline
; /* current deadline */
134 struct run_queue runq
; /* local runq for this processor */
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 */
143 extern processor_t processor_list
;
144 extern unsigned int processor_count
;
145 decl_simple_lock_data(extern,processor_list_lock
)
147 extern processor_t master_processor
;
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.
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.
163 * Processor state locking:
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.
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 */
180 extern processor_t
current_processor(void);
182 extern processor_t
cpu_to_processor(
185 /* Useful lock macros */
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)
191 #define processor_lock(pr) simple_lock(&(pr)->lock)
192 #define processor_unlock(pr) simple_unlock(&(pr)->lock)
194 extern void processor_bootstrap(void);
196 extern void processor_init(
197 processor_t processor
,
200 extern void timeshare_quanta_update(
201 processor_set_t pset
);
203 extern void pset_init(
204 processor_set_t pset
);
206 #define pset_run_incr(pset) \
207 hw_atomic_add(&(pset)->run_count, 1)
209 #define pset_run_decr(pset) \
210 hw_atomic_sub(&(pset)->run_count, 1)
212 #define pset_share_incr(pset) \
213 hw_atomic_add(&(pset)->share_count, 1)
215 #define pset_share_decr(pset) \
216 hw_atomic_sub(&(pset)->share_count, 1)
218 extern kern_return_t
processor_shutdown(
219 processor_t processor
);
221 extern void pset_remove_processor(
222 processor_set_t pset
,
223 processor_t processor
);
225 extern void pset_add_processor(
226 processor_set_t pset
,
227 processor_t processor
);
229 extern void pset_remove_task(
230 processor_set_t pset
,
233 extern void pset_add_task(
234 processor_set_t pset
,
237 extern void pset_remove_thread(
238 processor_set_t pset
,
241 extern void pset_add_thread(
242 processor_set_t pset
,
245 extern void thread_change_psets(
247 processor_set_t old_pset
,
248 processor_set_t new_pset
);
251 extern kern_return_t
processor_info_count(
252 processor_flavor_t flavor
,
253 mach_msg_type_number_t
*count
);
255 #endif /* MACH_KERNEL_PRIVATE */
259 extern void pset_deallocate(
260 processor_set_t pset
);
262 extern void pset_reference(
263 processor_set_t pset
);
267 #endif /* _KERN_PROCESSOR_H_ */