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>
46 // include the correct file to find real_ncpus
47 #if defined(__i386__) || defined(__x86_64__)
50 // fall back on declaring it extern. The linker will sort us out.
51 extern unsigned int real_ncpus
;
54 // Mask for supported options
55 #define T_CHUD_BIND_OPT_MASK (-1UL)
58 #pragma mark **** thread binding ****
62 * This method will bind a given thread to the requested CPU starting at the
63 * next time quantum. If the thread is the current thread, this method will
64 * force a thread_block(). The result is that if you call this method on the
65 * current thread, you will be on the requested CPU when this method returns.
67 __private_extern__ kern_return_t
68 chudxnu_bind_thread(thread_t thread
, int cpu
, __unused
int options
)
70 processor_t proc
= NULL
;
72 if(cpu
< 0 || (unsigned int)cpu
>= real_ncpus
) // sanity check
75 // temporary restriction until after phase 2 of the scheduler
76 if(thread
!= current_thread())
79 proc
= cpu_to_processor(cpu
);
82 * Potentially racey, but mainly to prevent bind to shutdown
85 if(proc
&& !(proc
->state
== PROCESSOR_OFF_LINE
) &&
86 !(proc
->state
== PROCESSOR_SHUTDOWN
)) {
91 * If we're trying to bind the current thread, and
92 * we're not on the target cpu, and not at interrupt
93 * context, block the current thread to force a
94 * reschedule on the target CPU.
96 if(thread
== current_thread() &&
97 !ml_at_interrupt_context() && cpu_number() != cpu
) {
98 (void)thread_block(THREAD_CONTINUE_NULL
);
105 __private_extern__ kern_return_t
106 chudxnu_unbind_thread(thread_t thread
, __unused
int options
)
108 if(thread
== current_thread())
109 thread_bind(PROCESSOR_NULL
);
113 __private_extern__ boolean_t
114 chudxnu_thread_get_idle(thread_t thread
) {
116 * Instantaneous snapshot of the idle state of
119 * Should be called only on an interrupted or
120 * suspended thread to avoid a race.
122 return ((thread
->state
& TH_IDLE
) == TH_IDLE
);
125 __private_extern__
int
126 chudxnu_thread_get_scheduler_state(thread_t thread
) {
128 * Instantaneous snapshot of the scheduler state of
131 * MUST ONLY be called on an interrupted or
132 * locked thread, to avoid a race.
136 int schedulerState
= (volatile int)(thread
->state
);
137 processor_t lastProcessor
= (volatile processor_t
)(thread
->last_processor
);
139 if ((PROCESSOR_NULL
!= lastProcessor
) && (thread
== lastProcessor
->active_thread
)) {
140 state
|= CHUDXNU_TS_RUNNING
;
143 if (schedulerState
& TH_RUN
) {
144 state
|= CHUDXNU_TS_RUNNABLE
;
147 if (schedulerState
& TH_WAIT
) {
148 state
|= CHUDXNU_TS_WAIT
;
151 if (schedulerState
& TH_UNINT
) {
152 state
|= CHUDXNU_TS_UNINT
;
155 if (schedulerState
& TH_SUSP
) {
156 state
|= CHUDXNU_TS_SUSP
;
159 if (schedulerState
& TH_TERMINATE
) {
160 state
|= CHUDXNU_TS_TERMINATE
;
163 if (schedulerState
& TH_IDLE
) {
164 state
|= CHUDXNU_TS_IDLE
;
171 #pragma mark **** task and thread info ****
174 __private_extern__ boolean_t
175 chudxnu_is_64bit_task(task_t task
)
177 return (task_has_64BitAddr(task
));
181 #define THING_THREAD 1
183 // an exact copy of processor_set_things() except no mig conversion at the end!
185 chudxnu_private_processor_set_things(
186 processor_set_t pset
,
187 mach_port_t
**thing_list
,
188 mach_msg_type_number_t
*count
,
191 unsigned int actual
; /* this many things */
192 unsigned int maxthings
;
195 vm_size_t size
, size_needed
;
198 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
199 return (KERN_INVALID_ARGUMENT
);
201 size
= 0; addr
= NULL
;
204 lck_mtx_lock(&tasks_threads_lock
);
206 if (type
== THING_TASK
)
207 maxthings
= tasks_count
;
209 maxthings
= threads_count
;
211 /* do we have the memory we need? */
213 size_needed
= maxthings
* sizeof (mach_port_t
);
214 if (size_needed
<= size
)
217 lck_mtx_unlock(&tasks_threads_lock
);
222 assert(size_needed
> 0);
227 return (KERN_RESOURCE_SHORTAGE
);
230 /* OK, have memory and the processor_set is locked & active */
237 task_t task
, *task_list
= (task_t
*)addr
;
239 for (task
= (task_t
)queue_first(&tasks
);
240 !queue_end(&tasks
, (queue_entry_t
)task
);
241 task
= (task_t
)queue_next(&task
->tasks
)) {
242 task_reference_internal(task
);
243 task_list
[actual
++] = task
;
251 thread_t thread
, *thread_list
= (thread_t
*)addr
;
253 for (i
= 0, thread
= (thread_t
)queue_first(&threads
);
254 !queue_end(&threads
, (queue_entry_t
)thread
);
255 thread
= (thread_t
)queue_next(&thread
->threads
)) {
256 thread_reference_internal(thread
);
257 thread_list
[actual
++] = thread
;
264 lck_mtx_unlock(&tasks_threads_lock
);
266 if (actual
< maxthings
)
267 size_needed
= actual
* sizeof (mach_port_t
);
270 /* no things, so return null pointer and deallocate memory */
278 /* if we allocated too much, must copy */
280 if (size_needed
< size
) {
283 newaddr
= kalloc(size_needed
);
289 task_t
*task_list
= (task_t
*)addr
;
291 for (i
= 0; i
< actual
; i
++)
292 task_deallocate(task_list
[i
]);
298 thread_t
*thread_list
= (thread_t
*)addr
;
300 for (i
= 0; i
< actual
; i
++)
301 thread_deallocate(thread_list
[i
]);
307 return (KERN_RESOURCE_SHORTAGE
);
310 bcopy((void *) addr
, (void *) newaddr
, size_needed
);
315 *thing_list
= (mach_port_t
*)addr
;
319 return (KERN_SUCCESS
);
322 // an exact copy of task_threads() except no mig conversion at the end!
324 chudxnu_private_task_threads(
326 thread_act_array_t
*threads_out
,
327 mach_msg_type_number_t
*count
)
329 mach_msg_type_number_t actual
;
330 thread_t
*thread_list
;
332 vm_size_t size
, size_needed
;
336 if (task
== TASK_NULL
)
337 return (KERN_INVALID_ARGUMENT
);
339 size
= 0; addr
= NULL
;
349 return (KERN_FAILURE
);
352 actual
= task
->thread_count
;
354 /* do we have the memory we need? */
355 size_needed
= actual
* sizeof (mach_port_t
);
356 if (size_needed
<= size
)
359 /* unlock the task and allocate more memory */
365 assert(size_needed
> 0);
370 return (KERN_RESOURCE_SHORTAGE
);
373 /* OK, have memory and the task is locked & active */
374 thread_list
= (thread_t
*)addr
;
378 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
379 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
380 thread_reference_internal(thread
);
381 thread_list
[j
++] = thread
;
384 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
387 size_needed
= actual
* sizeof (mach_port_t
);
389 /* can unlock task now that we've got the thread refs */
393 /* no threads, so return null pointer and deallocate memory */
402 /* if we allocated too much, must copy */
404 if (size_needed
< size
) {
407 newaddr
= kalloc(size_needed
);
409 for (i
= 0; i
< actual
; ++i
)
410 thread_deallocate(thread_list
[i
]);
412 return (KERN_RESOURCE_SHORTAGE
);
415 bcopy(addr
, newaddr
, size_needed
);
417 thread_list
= (thread_t
*)newaddr
;
420 *threads_out
= thread_list
;
424 return (KERN_SUCCESS
);
428 __private_extern__ kern_return_t
430 task_array_t
*task_list
,
431 mach_msg_type_number_t
*count
)
433 return chudxnu_private_processor_set_things(&pset0
, (mach_port_t
**)task_list
, count
, THING_TASK
);
436 __private_extern__ kern_return_t
437 chudxnu_free_task_list(
438 task_array_t
*task_list
,
439 mach_msg_type_number_t
*count
)
441 vm_size_t size
= (*count
)*sizeof(mach_port_t
);
442 void *addr
= *task_list
;
445 int i
, maxCount
= *count
;
446 for(i
=0; i
<maxCount
; i
++) {
447 task_deallocate((*task_list
)[i
]);
457 __private_extern__ kern_return_t
459 thread_array_t
*thread_list
,
460 mach_msg_type_number_t
*count
)
462 return chudxnu_private_processor_set_things(&pset0
, (mach_port_t
**)thread_list
, count
, THING_THREAD
);
465 __private_extern__ kern_return_t
466 chudxnu_task_threads(
468 thread_array_t
*thread_list
,
469 mach_msg_type_number_t
*count
)
471 return chudxnu_private_task_threads(task
, thread_list
, count
);
474 __private_extern__ kern_return_t
475 chudxnu_free_thread_list(
476 thread_array_t
*thread_list
,
477 mach_msg_type_number_t
*count
)
479 vm_size_t size
= (*count
)*sizeof(mach_port_t
);
480 void *addr
= *thread_list
;
483 int i
, maxCount
= *count
;
484 for(i
=0; i
<maxCount
; i
++) {
485 thread_deallocate((*thread_list
)[i
]);
496 __private_extern__ task_t
497 chudxnu_current_task(void)
499 return current_task();
502 __private_extern__ thread_t
503 chudxnu_current_thread(void)
505 return current_thread();
508 __private_extern__ task_t
509 chudxnu_task_for_thread(thread_t thread
)
511 return get_threadtask(thread
);
514 __private_extern__ kern_return_t
517 thread_flavor_t flavor
,
518 thread_info_t thread_info_out
,
519 mach_msg_type_number_t
*thread_info_count
)
521 return thread_info(thread
, flavor
, thread_info_out
, thread_info_count
);
525 /* thread marking stuff */
527 __private_extern__ boolean_t
528 chudxnu_thread_get_marked(thread_t thread
)
531 return ((thread
->t_chud
& T_CHUD_MARKED
) != 0);
535 __private_extern__ boolean_t
536 chudxnu_thread_set_marked(thread_t thread
, boolean_t new_value
)
542 // set the marked bit
543 old_val
= OSBitOrAtomic(T_CHUD_MARKED
, &(thread
->t_chud
));
545 // clear the marked bit
546 old_val
= OSBitAndAtomic(~T_CHUD_MARKED
, &(thread
->t_chud
));
548 return (old_val
& T_CHUD_MARKED
) == T_CHUD_MARKED
;
553 /* XXX: good thing this code is experimental... */
555 /* external handler */
556 extern void (*chudxnu_thread_ast_handler
)(thread_t
);
557 void (*chudxnu_thread_ast_handler
)(thread_t
) = NULL
;
559 /* AST callback to dispatch to AppleProfile */
560 extern void chudxnu_thread_ast(thread_t
);
562 chudxnu_thread_ast(thread_t thread
)
564 /* atomicness for kdebug events */
565 void (*handler
)(thread_t
) = chudxnu_thread_ast_handler
;
574 /* Get and set bits on the thread and trigger an AST handler */
575 void chudxnu_set_thread_ast( thread_t thread
);
577 chudxnu_set_thread_ast( thread_t thread
)
579 /* FIXME: only call this on current thread from an interrupt handler for now... */
580 if( thread
!= current_thread() )
581 panic( "unsafe AST set" );
583 act_set_kperf(thread
);
586 /* get and set the thread bits */
587 extern uint32_t chudxnu_get_thread_bits( thread_t thread
);
588 extern void chudxnu_set_thread_bits( thread_t thread
, uint32_t bits
);
591 chudxnu_get_thread_bits( thread_t thread
)
593 return thread
->t_chud
;
597 chudxnu_set_thread_bits( thread_t thread
, uint32_t bits
)
599 thread
->t_chud
= bits
;
602 /* get and set thread dirty bits. so CHUD can track whether the thread
603 * has been dispatched since it last looked. caller must hold the
607 chudxnu_thread_get_dirty(thread_t thread
)
609 if( thread
->c_switch
!= thread
->chud_c_switch
)
616 chudxnu_thread_set_dirty(thread_t thread
, boolean_t makedirty
)
619 thread
->chud_c_switch
= thread
->c_switch
- 1;
621 thread
->chud_c_switch
= thread
->c_switch
;