2 * Copyright (c) 2003-2007 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__)
49 #elif defined(__ppc__) || defined(__ppc64__)
50 # include <ppc/cpu_internal.h>
52 // fall back on declaring it extern. The linker will sort us out.
53 extern unsigned int real_ncpus
;
56 // Mask for supported options
57 #define T_CHUD_BIND_OPT_MASK (-1UL)
59 #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 #pragma mark **** task and thread info ****
127 __private_extern__ boolean_t
128 chudxnu_is_64bit_task(task_t task
)
130 return (task_has_64BitAddr(task
));
134 #define THING_THREAD 1
136 // an exact copy of processor_set_things() except no mig conversion at the end!
138 chudxnu_private_processor_set_things(
139 processor_set_t pset
,
140 mach_port_t
**thing_list
,
141 mach_msg_type_number_t
*count
,
144 unsigned int actual
; /* this many things */
145 unsigned int maxthings
;
148 vm_size_t size
, size_needed
;
151 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
152 return (KERN_INVALID_ARGUMENT
);
154 size
= 0; addr
= NULL
;
157 mutex_lock(&tasks_threads_lock
);
159 if (type
== THING_TASK
)
160 maxthings
= tasks_count
;
162 maxthings
= threads_count
;
164 /* do we have the memory we need? */
166 size_needed
= maxthings
* sizeof (mach_port_t
);
167 if (size_needed
<= size
)
170 mutex_unlock(&tasks_threads_lock
);
175 assert(size_needed
> 0);
180 return (KERN_RESOURCE_SHORTAGE
);
183 /* OK, have memory and the processor_set is locked & active */
190 task_t task
, *task_list
= (task_t
*)addr
;
192 for (task
= (task_t
)queue_first(&tasks
);
193 !queue_end(&tasks
, (queue_entry_t
)task
);
194 task
= (task_t
)queue_next(&task
->tasks
)) {
195 task_reference_internal(task
);
196 task_list
[actual
++] = task
;
204 thread_t thread
, *thread_list
= (thread_t
*)addr
;
206 for (i
= 0, thread
= (thread_t
)queue_first(&threads
);
207 !queue_end(&threads
, (queue_entry_t
)thread
);
208 thread
= (thread_t
)queue_next(&thread
->threads
)) {
209 thread_reference_internal(thread
);
210 thread_list
[actual
++] = thread
;
217 mutex_unlock(&tasks_threads_lock
);
219 if (actual
< maxthings
)
220 size_needed
= actual
* sizeof (mach_port_t
);
223 /* no things, so return null pointer and deallocate memory */
231 /* if we allocated too much, must copy */
233 if (size_needed
< size
) {
236 newaddr
= kalloc(size_needed
);
242 task_t
*task_list
= (task_t
*)addr
;
244 for (i
= 0; i
< actual
; i
++)
245 task_deallocate(task_list
[i
]);
251 thread_t
*thread_list
= (thread_t
*)addr
;
253 for (i
= 0; i
< actual
; i
++)
254 thread_deallocate(thread_list
[i
]);
260 return (KERN_RESOURCE_SHORTAGE
);
263 bcopy((void *) addr
, (void *) newaddr
, size_needed
);
268 *thing_list
= (mach_port_t
*)addr
;
272 return (KERN_SUCCESS
);
275 // an exact copy of task_threads() except no mig conversion at the end!
277 chudxnu_private_task_threads(
279 thread_act_array_t
*threads_out
,
280 mach_msg_type_number_t
*count
)
282 mach_msg_type_number_t actual
;
283 thread_t
*thread_list
;
285 vm_size_t size
, size_needed
;
289 if (task
== TASK_NULL
)
290 return (KERN_INVALID_ARGUMENT
);
292 size
= 0; addr
= NULL
;
302 return (KERN_FAILURE
);
305 actual
= task
->thread_count
;
307 /* do we have the memory we need? */
308 size_needed
= actual
* sizeof (mach_port_t
);
309 if (size_needed
<= size
)
312 /* unlock the task and allocate more memory */
318 assert(size_needed
> 0);
323 return (KERN_RESOURCE_SHORTAGE
);
326 /* OK, have memory and the task is locked & active */
327 thread_list
= (thread_t
*)addr
;
331 for (thread
= (thread_t
)queue_first(&task
->threads
); i
< actual
;
332 ++i
, thread
= (thread_t
)queue_next(&thread
->task_threads
)) {
333 thread_reference_internal(thread
);
334 thread_list
[j
++] = thread
;
337 assert(queue_end(&task
->threads
, (queue_entry_t
)thread
));
340 size_needed
= actual
* sizeof (mach_port_t
);
342 /* can unlock task now that we've got the thread refs */
346 /* no threads, so return null pointer and deallocate memory */
355 /* if we allocated too much, must copy */
357 if (size_needed
< size
) {
360 newaddr
= kalloc(size_needed
);
362 for (i
= 0; i
< actual
; ++i
)
363 thread_deallocate(thread_list
[i
]);
365 return (KERN_RESOURCE_SHORTAGE
);
368 bcopy(addr
, newaddr
, size_needed
);
370 thread_list
= (thread_t
*)newaddr
;
373 *threads_out
= thread_list
;
377 return (KERN_SUCCESS
);
381 __private_extern__ kern_return_t
383 task_array_t
*task_list
,
384 mach_msg_type_number_t
*count
)
386 return chudxnu_private_processor_set_things(&pset0
, (mach_port_t
**)task_list
, count
, THING_TASK
);
389 __private_extern__ kern_return_t
390 chudxnu_free_task_list(
391 task_array_t
*task_list
,
392 mach_msg_type_number_t
*count
)
394 vm_size_t size
= (*count
)*sizeof(mach_port_t
);
395 void *addr
= *task_list
;
398 int i
, maxCount
= *count
;
399 for(i
=0; i
<maxCount
; i
++) {
400 task_deallocate((*task_list
)[i
]);
410 __private_extern__ kern_return_t
412 thread_array_t
*thread_list
,
413 mach_msg_type_number_t
*count
)
415 return chudxnu_private_processor_set_things(&pset0
, (mach_port_t
**)thread_list
, count
, THING_THREAD
);
418 __private_extern__ kern_return_t
419 chudxnu_task_threads(
421 thread_array_t
*thread_list
,
422 mach_msg_type_number_t
*count
)
424 return chudxnu_private_task_threads(task
, thread_list
, count
);
427 __private_extern__ kern_return_t
428 chudxnu_free_thread_list(
429 thread_array_t
*thread_list
,
430 mach_msg_type_number_t
*count
)
432 vm_size_t size
= (*count
)*sizeof(mach_port_t
);
433 void *addr
= *thread_list
;
436 int i
, maxCount
= *count
;
437 for(i
=0; i
<maxCount
; i
++) {
438 thread_deallocate((*thread_list
)[i
]);
449 __private_extern__ task_t
450 chudxnu_current_task(void)
452 return current_task();
455 __private_extern__ thread_t
456 chudxnu_current_thread(void)
458 return current_thread();
461 __private_extern__ task_t
462 chudxnu_task_for_thread(thread_t thread
)
464 return get_threadtask(thread
);
467 __private_extern__ kern_return_t
470 thread_flavor_t flavor
,
471 thread_info_t thread_info_out
,
472 mach_msg_type_number_t
*thread_info_count
)
474 return thread_info(thread
, flavor
, thread_info_out
, thread_info_count
);
478 __private_extern__ kern_return_t
479 chudxnu_thread_last_context_switch(thread_t thread
, uint64_t *timestamp
)
481 *timestamp
= thread
->last_switch
;
485 /* thread marking stuff */
487 __private_extern__ boolean_t
488 chudxnu_thread_get_marked(thread_t thread
)
491 return ((thread
->t_chud
& T_CHUD_MARKED
) != 0);
495 __private_extern__ boolean_t
496 chudxnu_thread_set_marked(thread_t thread
, boolean_t new_value
)
502 // set the marked bit
503 old_val
= OSBitOrAtomic(T_CHUD_MARKED
, (UInt32
*) &(thread
->t_chud
));
505 // clear the marked bit
506 old_val
= OSBitAndAtomic(~T_CHUD_MARKED
, (UInt32
*) &(thread
->t_chud
));
508 return (old_val
& T_CHUD_MARKED
) == T_CHUD_MARKED
;