2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
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.
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.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
54 * processor.h: Processor and processor-related definitions.
57 #ifndef _KERN_PROCESSOR_H_
58 #define _KERN_PROCESSOR_H_
60 #include <mach/boolean.h>
61 #include <mach/kern_return.h>
62 #include <kern/kern_types.h>
64 #include <sys/cdefs.h>
66 #ifdef MACH_KERNEL_PRIVATE
68 #include <mach/mach_types.h>
70 #include <kern/cpu_number.h>
71 #include <kern/lock.h>
72 #include <kern/queue.h>
73 #include <kern/sched.h>
74 #include <kern/processor_data.h>
76 #include <machine/ast_types.h>
78 struct processor_set
{
79 queue_head_t idle_queue
; /* idle processors */
80 int idle_count
; /* how many ? */
81 queue_head_t active_queue
; /* active processors */
83 queue_head_t processors
; /* all processors here */
84 int processor_count
;/* how many ? */
85 decl_simple_lock_data(,sched_lock
) /* lock for runq and above */
87 struct run_queue runq
; /* runq for this set */
89 queue_head_t tasks
; /* tasks assigned */
90 int task_count
; /* how many */
91 queue_head_t threads
; /* threads in this set */
92 int thread_count
; /* how many */
93 int ref_count
; /* structure ref count */
94 boolean_t active
; /* is pset in use */
95 decl_mutex_data(, lock
) /* lock for above */
97 int timeshare_quanta
; /* timeshare quantum factor */
99 struct ipc_port
* pset_self
; /* port for operations */
100 struct ipc_port
* pset_name_self
; /* port for information */
102 uint32_t run_count
; /* threads running in set */
103 uint32_t share_count
; /* timeshare threads running in set */
105 integer_t mach_factor
; /* mach_factor */
106 integer_t load_average
; /* load_average */
108 uint32_t pri_shift
; /* timeshare usage -> priority */
111 extern struct processor_set default_pset
;
114 queue_chain_t processor_queue
;/* idle/active queue link,
115 * MUST remain the first element */
116 int state
; /* See below */
118 *active_thread
, /* thread running on processor */
119 *next_thread
, /* next thread when dispatched */
120 *idle_thread
; /* this processor's idle thread. */
122 processor_set_t processor_set
; /* current membership */
124 int current_pri
; /* priority of current thread */
126 timer_call_data_t quantum_timer
; /* timer for quantum expiration */
127 uint64_t quantum_end
; /* time when current quantum ends */
128 uint64_t last_dispatch
; /* time of last dispatch */
130 int timeslice
; /* quanta before timeslice ends */
131 uint64_t deadline
; /* current deadline */
133 struct run_queue runq
; /* local runq for this processor */
135 queue_chain_t processors
; /* processors in set */
136 decl_simple_lock_data(,lock
)
137 struct ipc_port
* processor_self
; /* port for operations */
138 processor_t processor_list
; /* all existing processors */
139 processor_data_t processor_data
; /* per-processor data */
142 extern processor_t processor_list
;
143 extern unsigned int processor_count
;
144 decl_simple_lock_data(extern,processor_list_lock
)
146 extern processor_t master_processor
;
149 * NOTE: The processor->processor_set link is needed in one of the
150 * scheduler's critical paths. [Figure out where to look for another
151 * thread to run on this processor.] It is accessed without locking.
152 * The following access protocol controls this field.
154 * Read from own processor - just read.
155 * Read from another processor - lock processor structure during read.
156 * Write from own processor - lock processor structure during write.
157 * Write from another processor - NOT PERMITTED.
162 * Processor state locking:
164 * Values for the processor state are defined below. If the processor
165 * is off-line or being shutdown, then it is only necessary to lock
166 * the processor to change its state. Otherwise it is only necessary
167 * to lock its processor set's sched_lock. Scheduler code will
168 * typically lock only the sched_lock, but processor manipulation code
169 * will often lock both.
172 #define PROCESSOR_OFF_LINE 0 /* Not available */
173 #define PROCESSOR_RUNNING 1 /* Normal execution */
174 #define PROCESSOR_IDLE 2 /* Idle */
175 #define PROCESSOR_DISPATCHING 3 /* Dispatching (idle -> running) */
176 #define PROCESSOR_SHUTDOWN 4 /* Going off-line */
177 #define PROCESSOR_START 5 /* Being started */
179 extern processor_t
current_processor(void);
181 extern processor_t
cpu_to_processor(
184 /* Useful lock macros */
186 #define pset_lock(pset) mutex_lock(&(pset)->lock)
187 #define pset_lock_try(pset) mutex_try(&(pset)->lock)
188 #define pset_unlock(pset) mutex_unlock(&(pset)->lock)
190 #define processor_lock(pr) simple_lock(&(pr)->lock)
191 #define processor_unlock(pr) simple_unlock(&(pr)->lock)
193 extern void processor_bootstrap(void);
195 extern void processor_init(
196 processor_t processor
,
199 extern void timeshare_quanta_update(
200 processor_set_t pset
);
202 extern void pset_init(
203 processor_set_t pset
);
205 #define pset_run_incr(pset) \
206 hw_atomic_add(&(pset)->run_count, 1)
208 #define pset_run_decr(pset) \
209 hw_atomic_sub(&(pset)->run_count, 1)
211 #define pset_share_incr(pset) \
212 hw_atomic_add(&(pset)->share_count, 1)
214 #define pset_share_decr(pset) \
215 hw_atomic_sub(&(pset)->share_count, 1)
217 extern kern_return_t
processor_shutdown(
218 processor_t processor
);
220 extern void pset_remove_processor(
221 processor_set_t pset
,
222 processor_t processor
);
224 extern void pset_add_processor(
225 processor_set_t pset
,
226 processor_t processor
);
228 extern void pset_remove_task(
229 processor_set_t pset
,
232 extern void pset_add_task(
233 processor_set_t pset
,
236 extern void pset_remove_thread(
237 processor_set_t pset
,
240 extern void pset_add_thread(
241 processor_set_t pset
,
244 extern void thread_change_psets(
246 processor_set_t old_pset
,
247 processor_set_t new_pset
);
250 extern kern_return_t
processor_info_count(
251 processor_flavor_t flavor
,
252 mach_msg_type_number_t
*count
);
254 #endif /* MACH_KERNEL_PRIVATE */
258 extern void pset_deallocate(
259 processor_set_t pset
);
261 extern void pset_reference(
262 processor_set_t pset
);
266 #endif /* _KERN_PROCESSOR_H_ */