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__)
57 #elif defined(__arm__) || defined (__arm64__)
58 # include <arm/cpu_internal.h>
60 // fall back on declaring it extern. The linker will sort us out.
61 extern unsigned int real_ncpus
;
64 // Mask for supported options
65 #define T_CHUD_BIND_OPT_MASK (-1UL)
68 #pragma mark **** thread binding ****
72 * This method will bind a given thread to the requested CPU starting at the
73 * next time quantum. If the thread is the current thread, this method will
74 * force a thread_block(). The result is that if you call this method on the
75 * current thread, you will be on the requested CPU when this method returns.
77 __private_extern__ kern_return_t
78 chudxnu_bind_thread(thread_t thread
, int cpu
, __unused
int options
)
80 processor_t proc
= NULL
;
82 if(cpu
< 0 || (unsigned int)cpu
>= real_ncpus
) // sanity check
85 // temporary restriction until after phase 2 of the scheduler
86 if(thread
!= current_thread())
89 proc
= cpu_to_processor(cpu
);
92 * Potentially racey, but mainly to prevent bind to shutdown
95 if(proc
&& !(proc
->state
== PROCESSOR_OFF_LINE
) &&
96 !(proc
->state
== PROCESSOR_SHUTDOWN
)) {
101 * If we're trying to bind the current thread, and
102 * we're not on the target cpu, and not at interrupt
103 * context, block the current thread to force a
104 * reschedule on the target CPU.
106 if(thread
== current_thread() &&
107 !ml_at_interrupt_context() && cpu_number() != cpu
) {
108 (void)thread_block(THREAD_CONTINUE_NULL
);
115 __private_extern__ kern_return_t
116 chudxnu_unbind_thread(thread_t thread
, __unused
int options
)
118 if(thread
== current_thread())
119 thread_bind(PROCESSOR_NULL
);