2 * Copyright (c) 2003-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <mach/mach_types.h>
30 #include <mach/task.h>
31 #include <mach/thread_act.h>
33 #include <kern/kern_types.h>
34 #include <kern/processor.h>
35 #include <kern/thread.h>
36 #include <kern/kalloc.h>
38 #include <chud/chud_xnu.h>
39 #include <chud/chud_xnu_private.h>
40 #include <chud/chud_thread.h>
42 #include <machine/machine_routines.h>
44 #include <libkern/OSAtomic.h>
51 #include <kperf/kperf.h>
54 // include the correct file to find real_ncpus
55 #if defined(__i386__) || defined(__x86_64__)
58 // fall back on declaring it extern. The linker will sort us out.
59 extern unsigned int real_ncpus
;
62 // Mask for supported options
63 #define T_CHUD_BIND_OPT_MASK (-1UL)
66 #pragma mark **** thread binding ****
70 * This method will bind a given thread to the requested CPU starting at the
71 * next time quantum. If the thread is the current thread, this method will
72 * force a thread_block(). The result is that if you call this method on the
73 * current thread, you will be on the requested CPU when this method returns.
75 __private_extern__ kern_return_t
76 chudxnu_bind_thread(thread_t thread
, int cpu
, __unused
int options
)
78 processor_t proc
= NULL
;
80 if(cpu
< 0 || (unsigned int)cpu
>= real_ncpus
) // sanity check
83 // temporary restriction until after phase 2 of the scheduler
84 if(thread
!= current_thread())
87 proc
= cpu_to_processor(cpu
);
90 * Potentially racey, but mainly to prevent bind to shutdown
93 if(proc
&& !(proc
->state
== PROCESSOR_OFF_LINE
) &&
94 !(proc
->state
== PROCESSOR_SHUTDOWN
)) {
99 * If we're trying to bind the current thread, and
100 * we're not on the target cpu, and not at interrupt
101 * context, block the current thread to force a
102 * reschedule on the target CPU.
104 if(thread
== current_thread() &&
105 !ml_at_interrupt_context() && cpu_number() != cpu
) {
106 (void)thread_block(THREAD_CONTINUE_NULL
);
113 __private_extern__ kern_return_t
114 chudxnu_unbind_thread(thread_t thread
, __unused
int options
)
116 if(thread
== current_thread())
117 thread_bind(PROCESSOR_NULL
);
121 __private_extern__ boolean_t
122 chudxnu_thread_get_idle(thread_t thread
) {
124 * Instantaneous snapshot of the idle state of
127 * Should be called only on an interrupted or
128 * suspended thread to avoid a race.
130 return ((thread
->state
& TH_IDLE
) == TH_IDLE
);
133 __private_extern__
int
134 chudxnu_thread_get_scheduler_state(thread_t thread
) {
136 * Instantaneous snapshot of the scheduler state of
139 * MUST ONLY be called on an interrupted or
140 * locked thread, to avoid a race.
144 int schedulerState
= (volatile int)(thread
->state
);
145 processor_t lastProcessor
= (volatile processor_t
)(thread
->last_processor
);
147 if ((PROCESSOR_NULL
!= lastProcessor
) && (thread
== lastProcessor
->active_thread
)) {
148 state
|= CHUDXNU_TS_RUNNING
;
151 if (schedulerState
& TH_RUN
) {
152 state
|= CHUDXNU_TS_RUNNABLE
;
155 if (schedulerState
& TH_WAIT
) {
156 state
|= CHUDXNU_TS_WAIT
;
159 if (schedulerState
& TH_UNINT
) {
160 state
|= CHUDXNU_TS_UNINT
;
163 if (schedulerState
& TH_SUSP
) {
164 state
|= CHUDXNU_TS_SUSP
;
167 if (schedulerState
& TH_TERMINATE
) {
168 state
|= CHUDXNU_TS_TERMINATE
;
171 if (schedulerState
& TH_IDLE
) {
172 state
|= CHUDXNU_TS_IDLE
;
179 #pragma mark **** task and thread info ****
182 __private_extern__ boolean_t
183 chudxnu_is_64bit_task(task_t task
)
185 return (task_has_64BitAddr(task
));
189 #define THING_THREAD 1
191 // an exact copy of processor_set_things() except no mig conversion at the end!
193 chudxnu_private_processor_set_things(
194 processor_set_t pset
,
195 mach_port_t
**thing_list
,
196 mach_msg_type_number_t
*count
,
199 unsigned int actual
; /* this many things */
200 unsigned int maxthings
;
203 vm_size_t size
, size_needed
;
206 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
207 return (KERN_INVALID_ARGUMENT
);
209 size
= 0; addr
= NULL
;
212 lck_mtx_lock(&tasks_threads_lock
);
214 if (type
== THING_TASK
)
215 maxthings
= tasks_count
;
217 maxthings
= threads_count
;
219 /* do we have the memory we need? */
221 size_needed
= maxthings
* sizeof (mach_port_t
);
222 if (size_needed
<= size
)
225 lck_mtx_unlock(&tasks_threads_lock
);
230 assert(size_needed
> 0);
235 return (KERN_RESOURCE_SHORTAGE
);
238 /* OK, have memory and the processor_set is locked & active */
245 task_t task
, *task_list
= (task_t
*)addr
;
247 for (task
= (task_t
)queue_first(&tasks
);
248 !queue_end(&tasks
, (queue_entry_t
)task
);
249 task
= (task_t
)queue_next(&task
->tasks
)) {
250 task_reference_internal(task
);
251 task_list
[actual
++] = task
;
259 thread_t thread
, *thread_list
= (thread_t
*)addr
;
261 for (i
= 0, thread
= (thread_t
)queue_first(&threads
);
262 !queue_end(&threads
, (queue_entry_t
)thread
);
263 thread
= (thread_t
)queue_next(&thread
->threads
)) {
264 thread_reference_internal(thread
);
265 thread_list
[actual
++] = thread
;
272 lck_mtx_unlock(&tasks_threads_lock
);
274 if (actual
< maxthings
)
275 size_needed
= actual
* sizeof (mach_port_t
);
278 /* no things, so return null pointer and deallocate memory */
286 /* if we allocated too much, must copy */
288 if (size_needed
< size
) {
291 newaddr
= kalloc(size_needed
);
297 task_t
*task_list
= (task_t
*)addr
;
299 for (i
= 0; i
< actual
; i
++)
300 task_deallocate(task_list
[i
]);
306 thread_t
*thread_list
= (thread_t
*)addr
;
308 for (i
= 0; i
< actual
; i
++)
309 thread_deallocate(thread_list
[i
]);
315 return (KERN_RESOURCE_SHORTAGE
);
318 bcopy((void *) addr
, (void *) newaddr
, size_needed
);
323 *thing_list
= (mach_port_t
*)addr
;
327 return (KERN_SUCCESS
);
330 // an exact copy of task_threads() except no mig conversion at the end!
332 chudxnu_private_task_threads(
334 thread_act_array_t
*threads_out
,
335 mach_msg_type_number_t
*count
)
337 mach_msg_type_number_t actual
;
338 thread_t
*thread_list
;
340 vm_size_t size
, size_needed
;
344 if (task
== TASK_NULL
)
345 return (KERN_INVALID_ARGUMENT
);
347 size
= 0; addr
= NULL
;
357 return (KERN_FAILURE
);
360 actual
= task
->thread_count
;
362 /* do we have the memory we need? */
363 size_needed
= actual
* sizeof (mach_port_t
);
364 if (size_needed
<= size
)
367 /* unlock the task and allocate more memory */
373 assert(size_needed
> 0);
378 return (KERN_RESOURCE_SHORTAGE
);
381 /* OK, have memory and the task is locked & active */
382 thread_list
= (thread_t
*)addr
;
386 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
387 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
388 thread_reference_internal(thread
);
389 thread_list
[j
++] = thread
;
392 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
395 size_needed
= actual
* sizeof (mach_port_t
);
397 /* can unlock task now that we've got the thread refs */
401 /* no threads, so return null pointer and deallocate memory */
410 /* if we allocated too much, must copy */
412 if (size_needed
< size
) {
415 newaddr
= kalloc(size_needed
);
417 for (i
= 0; i
< actual
; ++i
)
418 thread_deallocate(thread_list
[i
]);
420 return (KERN_RESOURCE_SHORTAGE
);
423 bcopy(addr
, newaddr
, size_needed
);
425 thread_list
= (thread_t
*)newaddr
;
428 *threads_out
= thread_list
;
432 return (KERN_SUCCESS
);
436 __private_extern__ kern_return_t
438 task_array_t
*task_list
,
439 mach_msg_type_number_t
*count
)
441 return chudxnu_private_processor_set_things(&pset0
, (mach_port_t
**)task_list
, count
, THING_TASK
);
444 __private_extern__ kern_return_t
445 chudxnu_free_task_list(
446 task_array_t
*task_list
,
447 mach_msg_type_number_t
*count
)
449 vm_size_t size
= (*count
)*sizeof(mach_port_t
);
450 void *addr
= *task_list
;
453 int i
, maxCount
= *count
;
454 for(i
=0; i
<maxCount
; i
++) {
455 task_deallocate((*task_list
)[i
]);
465 __private_extern__ kern_return_t
467 thread_array_t
*thread_list
,
468 mach_msg_type_number_t
*count
)
470 return chudxnu_private_processor_set_things(&pset0
, (mach_port_t
**)thread_list
, count
, THING_THREAD
);
473 __private_extern__ kern_return_t
474 chudxnu_task_threads(
476 thread_array_t
*thread_list
,
477 mach_msg_type_number_t
*count
)
479 return chudxnu_private_task_threads(task
, thread_list
, count
);
482 __private_extern__ kern_return_t
483 chudxnu_free_thread_list(
484 thread_array_t
*thread_list
,
485 mach_msg_type_number_t
*count
)
487 vm_size_t size
= (*count
)*sizeof(mach_port_t
);
488 void *addr
= *thread_list
;
491 int i
, maxCount
= *count
;
492 for(i
=0; i
<maxCount
; i
++) {
493 thread_deallocate((*thread_list
)[i
]);
504 __private_extern__ task_t
505 chudxnu_current_task(void)
507 return current_task();
510 __private_extern__ thread_t
511 chudxnu_current_thread(void)
513 return current_thread();
516 __private_extern__ task_t
517 chudxnu_task_for_thread(thread_t thread
)
519 return get_threadtask(thread
);
522 __private_extern__ kern_return_t
525 thread_flavor_t flavor
,
526 thread_info_t thread_info_out
,
527 mach_msg_type_number_t
*thread_info_count
)
529 return thread_info(thread
, flavor
, thread_info_out
, thread_info_count
);
533 /* thread marking stuff */
535 __private_extern__ boolean_t
536 chudxnu_thread_get_marked(thread_t thread
)
539 return ((thread
->t_chud
& T_CHUD_MARKED
) != 0);
543 __private_extern__ boolean_t
544 chudxnu_thread_set_marked(thread_t thread
, boolean_t new_value
)
550 // set the marked bit
551 old_val
= OSBitOrAtomic(T_CHUD_MARKED
, &(thread
->t_chud
));
553 // clear the marked bit
554 old_val
= OSBitAndAtomic(~T_CHUD_MARKED
, &(thread
->t_chud
));
556 return (old_val
& T_CHUD_MARKED
) == T_CHUD_MARKED
;
561 /* XXX: good thing this code is experimental... */
563 /* external handler */
564 extern void (*chudxnu_thread_ast_handler
)(thread_t
);
565 void (*chudxnu_thread_ast_handler
)(thread_t
) = NULL
;
567 /* AST callback to dispatch to AppleProfile */
568 extern void chudxnu_thread_ast(thread_t
);
570 chudxnu_thread_ast(thread_t thread
)
573 /* check for PMC work */
574 kpc_thread_ast_handler(thread
);
578 /* check for kperf work */
579 kperf_thread_ast_handler(thread
);
582 /* atomicness for kdebug events */
583 void (*handler
)(thread_t
) = chudxnu_thread_ast_handler
;
592 /* Get and set bits on the thread and trigger an AST handler */
593 void chudxnu_set_thread_ast( thread_t thread
);
595 chudxnu_set_thread_ast( thread_t thread
)
597 /* FIXME: only call this on current thread from an interrupt handler for now... */
598 if( thread
!= current_thread() )
599 panic( "unsafe AST set" );
601 act_set_kperf(thread
);
604 /* get and set the thread bits */
605 extern uint32_t chudxnu_get_thread_bits( thread_t thread
);
606 extern void chudxnu_set_thread_bits( thread_t thread
, uint32_t bits
);
609 chudxnu_get_thread_bits( thread_t thread
)
611 return thread
->t_chud
;
615 chudxnu_set_thread_bits( thread_t thread
, uint32_t bits
)
617 thread
->t_chud
= bits
;
620 /* get and set thread dirty bits. so CHUD can track whether the thread
621 * has been dispatched since it last looked. caller must hold the
625 chudxnu_thread_get_dirty(thread_t thread
)
627 if( thread
->c_switch
!= thread
->chud_c_switch
)
634 chudxnu_thread_set_dirty(thread_t thread
, boolean_t makedirty
)
637 thread
->chud_c_switch
= thread
->c_switch
- 1;
639 thread
->chud_c_switch
= thread
->c_switch
;