]> git.saurik.com Git - apple/xnu.git/blob - osfmk/chud/chud_thread.c
xnu-4570.41.2.tar.gz
[apple/xnu.git] / osfmk / chud / chud_thread.c
1 /*
2 * Copyright (c) 2003-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <mach/mach_types.h>
30 #include <mach/task.h>
31 #include <mach/thread_act.h>
32
33 #include <kern/kern_types.h>
34 #include <kern/processor.h>
35 #include <kern/thread.h>
36 #include <kern/kalloc.h>
37
38 #include <chud/chud_xnu.h>
39 #include <chud/chud_xnu_private.h>
40 #include <chud/chud_thread.h>
41
42 #include <machine/machine_routines.h>
43
44 #include <libkern/OSAtomic.h>
45
46 #if KPC
47 #include <kern/kpc.h>
48 #endif
49
50 #if KPERF
51 #include <kperf/kperf.h>
52 #endif
53
54 // include the correct file to find real_ncpus
55 #if defined(__i386__) || defined(__x86_64__)
56 # include <i386/mp.h>
57 #elif defined(__arm__) || defined (__arm64__)
58 # include <arm/cpu_internal.h>
59 #else
60 // fall back on declaring it extern. The linker will sort us out.
61 extern unsigned int real_ncpus;
62 #endif
63
64 // Mask for supported options
65 #define T_CHUD_BIND_OPT_MASK (-1UL)
66
67 #if 0
68 #pragma mark **** thread binding ****
69 #endif
70
71 /*
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.
76 */
77 __private_extern__ kern_return_t
78 chudxnu_bind_thread(thread_t thread, int cpu, __unused int options)
79 {
80 processor_t proc = NULL;
81
82 if(cpu < 0 || (unsigned int)cpu >= real_ncpus) // sanity check
83 return KERN_FAILURE;
84
85 // temporary restriction until after phase 2 of the scheduler
86 if(thread != current_thread())
87 return KERN_FAILURE;
88
89 proc = cpu_to_processor(cpu);
90
91 /*
92 * Potentially racey, but mainly to prevent bind to shutdown
93 * processor.
94 */
95 if(proc && !(proc->state == PROCESSOR_OFF_LINE) &&
96 !(proc->state == PROCESSOR_SHUTDOWN)) {
97
98 thread_bind(proc);
99
100 /*
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.
105 */
106 if(thread == current_thread() &&
107 !ml_at_interrupt_context() && cpu_number() != cpu) {
108 (void)thread_block(THREAD_CONTINUE_NULL);
109 }
110 return KERN_SUCCESS;
111 }
112 return KERN_FAILURE;
113 }
114
115 __private_extern__ kern_return_t
116 chudxnu_unbind_thread(thread_t thread, __unused int options)
117 {
118 if(thread == current_thread())
119 thread_bind(PROCESSOR_NULL);
120 return KERN_SUCCESS;
121 }