2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
62 * processor.h: Processor and processor-related definitions.
65 #ifndef _KERN_PROCESSOR_H_
66 #define _KERN_PROCESSOR_H_
68 #include <mach/boolean.h>
69 #include <mach/kern_return.h>
70 #include <kern/kern_types.h>
72 #include <sys/cdefs.h>
74 #ifdef MACH_KERNEL_PRIVATE
76 #include <mach/mach_types.h>
78 #include <kern/cpu_number.h>
79 #include <kern/lock.h>
80 #include <kern/queue.h>
81 #include <kern/sched.h>
82 #include <kern/processor_data.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 runq and 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 */
107 struct ipc_port
* pset_self
; /* port for operations */
108 struct ipc_port
* pset_name_self
; /* port for information */
110 uint32_t run_count
; /* threads running in set */
111 uint32_t share_count
; /* timeshare threads running in set */
113 integer_t mach_factor
; /* mach_factor */
114 integer_t load_average
; /* load_average */
116 uint32_t pri_shift
; /* timeshare usage -> priority */
119 extern struct processor_set default_pset
;
122 queue_chain_t processor_queue
;/* idle/active queue link,
123 * MUST remain the first element */
124 int state
; /* See below */
126 *active_thread
, /* thread running on processor */
127 *next_thread
, /* next thread when dispatched */
128 *idle_thread
; /* this processor's idle thread. */
130 processor_set_t processor_set
; /* current membership */
132 int current_pri
; /* priority of current thread */
134 timer_call_data_t quantum_timer
; /* timer for quantum expiration */
135 uint64_t quantum_end
; /* time when current quantum ends */
136 uint64_t last_dispatch
; /* time of last dispatch */
138 int timeslice
; /* quanta before timeslice ends */
139 uint64_t deadline
; /* current deadline */
141 struct run_queue runq
; /* local runq for this processor */
143 queue_chain_t processors
; /* processors in set */
144 decl_simple_lock_data(,lock
)
145 struct ipc_port
* processor_self
; /* port for operations */
146 processor_t processor_list
; /* all existing processors */
147 processor_data_t processor_data
; /* per-processor data */
150 extern processor_t processor_list
;
151 extern unsigned int processor_count
;
152 decl_simple_lock_data(extern,processor_list_lock
)
154 extern processor_t master_processor
;
157 * NOTE: The processor->processor_set link is needed in one of the
158 * scheduler's critical paths. [Figure out where to look for another
159 * thread to run on this processor.] It is accessed without locking.
160 * The following access protocol controls this field.
162 * Read from own processor - just read.
163 * Read from another processor - lock processor structure during read.
164 * Write from own processor - lock processor structure during write.
165 * Write from another processor - NOT PERMITTED.
170 * Processor state locking:
172 * Values for the processor state are defined below. If the processor
173 * is off-line or being shutdown, then it is only necessary to lock
174 * the processor to change its state. Otherwise it is only necessary
175 * to lock its processor set's sched_lock. Scheduler code will
176 * typically lock only the sched_lock, but processor manipulation code
177 * will often lock both.
180 #define PROCESSOR_OFF_LINE 0 /* Not available */
181 #define PROCESSOR_RUNNING 1 /* Normal execution */
182 #define PROCESSOR_IDLE 2 /* Idle */
183 #define PROCESSOR_DISPATCHING 3 /* Dispatching (idle -> running) */
184 #define PROCESSOR_SHUTDOWN 4 /* Going off-line */
185 #define PROCESSOR_START 5 /* Being started */
187 extern processor_t
current_processor(void);
189 extern processor_t
cpu_to_processor(
192 /* Useful lock macros */
194 #define pset_lock(pset) mutex_lock(&(pset)->lock)
195 #define pset_lock_try(pset) mutex_try(&(pset)->lock)
196 #define pset_unlock(pset) mutex_unlock(&(pset)->lock)
198 #define processor_lock(pr) simple_lock(&(pr)->lock)
199 #define processor_unlock(pr) simple_unlock(&(pr)->lock)
201 extern void processor_bootstrap(void);
203 extern void processor_init(
204 processor_t processor
,
207 extern void timeshare_quanta_update(
208 processor_set_t pset
);
210 extern void pset_init(
211 processor_set_t pset
);
213 #define pset_run_incr(pset) \
214 hw_atomic_add(&(pset)->run_count, 1)
216 #define pset_run_decr(pset) \
217 hw_atomic_sub(&(pset)->run_count, 1)
219 #define pset_share_incr(pset) \
220 hw_atomic_add(&(pset)->share_count, 1)
222 #define pset_share_decr(pset) \
223 hw_atomic_sub(&(pset)->share_count, 1)
225 extern kern_return_t
processor_shutdown(
226 processor_t processor
);
228 extern void pset_remove_processor(
229 processor_set_t pset
,
230 processor_t processor
);
232 extern void pset_add_processor(
233 processor_set_t pset
,
234 processor_t processor
);
236 extern void pset_remove_task(
237 processor_set_t pset
,
240 extern void pset_add_task(
241 processor_set_t pset
,
244 extern void pset_remove_thread(
245 processor_set_t pset
,
248 extern void pset_add_thread(
249 processor_set_t pset
,
252 extern void thread_change_psets(
254 processor_set_t old_pset
,
255 processor_set_t new_pset
);
258 extern kern_return_t
processor_info_count(
259 processor_flavor_t flavor
,
260 mach_msg_type_number_t
*count
);
262 #endif /* MACH_KERNEL_PRIVATE */
266 extern void pset_deallocate(
267 processor_set_t pset
);
269 extern void pset_reference(
270 processor_set_t pset
);
274 #endif /* _KERN_PROCESSOR_H_ */