]>
Commit | Line | Data |
---|---|---|
fe8ab488 A |
1 | /* |
2 | * Copyright (c) 2013 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 <kern/locks.h> | |
30 | #include <kern/task.h> | |
31 | #include <kern/thread.h> | |
32 | #include <libkern/OSAtomic.h> | |
33 | #include <vm/vm_pageout.h> | |
34 | ||
35 | #if defined(__x86_64__) && CONFIG_VMX | |
36 | #include <i386/vmx/vmx_cpu.h> | |
37 | #endif | |
38 | ||
39 | #include <kern/hv_support.h> | |
40 | ||
41 | int hv_support_available = 0; | |
42 | ||
43 | /* callbacks for tasks/threads with associated hv objects */ | |
44 | hv_callbacks_t hv_callbacks = { | |
45 | .dispatch = NULL, /* thread is being dispatched for execution */ | |
46 | .preempt = NULL, /* thread is being preempted */ | |
47 | .thread_destroy = NULL, /* thread is being destroyed */ | |
48 | .task_destroy = NULL, /* task is being destroyed */ | |
49 | .volatile_state = NULL, /* thread state is becoming volatile */ | |
50 | .memory_pressure = NULL /* memory pressure notification */ | |
51 | }; | |
52 | ||
53 | /* trap tables for hv_*_trap syscalls */ | |
54 | static hv_trap_table_t hv_trap_table[] = { | |
55 | [HV_TASK_TRAP] = { | |
56 | .traps = NULL, | |
57 | .trap_count = 0 | |
58 | }, | |
59 | [HV_THREAD_TRAP] = { | |
60 | .traps = NULL, | |
61 | .trap_count = 0 | |
62 | } | |
63 | }; | |
64 | ||
65 | static int hv_callbacks_enabled = 0; | |
66 | static int hv_mp_notify_enabled = 0; | |
67 | static int hv_mp_notify_destroy = 0; | |
68 | static lck_grp_t *hv_support_lck_grp = NULL; | |
69 | static lck_mtx_t *hv_support_lck_mtx = NULL; | |
70 | static thread_t hv_mp_notify_thread = THREAD_NULL; | |
71 | static void hv_mp_notify(void); | |
72 | ||
73 | /* hv_support boot initialization */ | |
74 | void | |
75 | hv_support_init(void) { | |
76 | #if defined(__x86_64__) && CONFIG_VMX | |
77 | hv_support_available = vmx_hv_support(); | |
78 | #endif | |
79 | ||
80 | hv_support_lck_grp = lck_grp_alloc_init("hv_support", LCK_GRP_ATTR_NULL); | |
81 | assert(hv_support_lck_grp); | |
82 | ||
83 | hv_support_lck_mtx = lck_mtx_alloc_init(hv_support_lck_grp, LCK_ATTR_NULL); | |
84 | assert(hv_support_lck_mtx); | |
85 | } | |
86 | ||
87 | /* returns true if hv_support is available on this machine */ | |
88 | int | |
89 | hv_get_support(void) { | |
90 | return hv_support_available; | |
91 | } | |
92 | ||
93 | /* associate an hv object with the current task */ | |
94 | void | |
95 | hv_set_task_target(void *target) { | |
96 | current_task()->hv_task_target = target; | |
97 | } | |
98 | ||
99 | /* associate an hv object with the current thread */ | |
100 | void | |
101 | hv_set_thread_target(void *target) { | |
102 | current_thread()->hv_thread_target = target; | |
103 | } | |
104 | ||
105 | /* get hv object associated with the current task */ | |
106 | void* | |
107 | hv_get_task_target(void) { | |
108 | return current_task()->hv_task_target; | |
109 | } | |
110 | ||
111 | /* get hv object associated with the current thread */ | |
112 | void* | |
113 | hv_get_thread_target(void) { | |
114 | return current_thread()->hv_thread_target; | |
115 | } | |
116 | ||
117 | /* test if a given thread state may be volatile between dispatch | |
118 | and preemption */ | |
119 | int | |
120 | hv_get_volatile_state(hv_volatile_state_t state) { | |
121 | int is_volatile = 0; | |
122 | ||
123 | #if (defined(__x86_64__)) | |
124 | if (state == HV_DEBUG_STATE) { | |
125 | is_volatile = (current_thread()->machine.ids != NULL); | |
126 | } | |
127 | #endif | |
128 | ||
129 | return is_volatile; | |
130 | } | |
131 | ||
132 | /* memory pressure monitor thread */ | |
133 | static void | |
134 | hv_mp_notify(void) { | |
135 | while (1) { | |
136 | mach_vm_pressure_monitor(TRUE, 0, NULL, NULL); | |
137 | ||
138 | lck_mtx_lock(hv_support_lck_mtx); | |
139 | if (hv_mp_notify_destroy == 1) { | |
140 | hv_mp_notify_destroy = 0; | |
141 | hv_mp_notify_enabled = 0; | |
142 | lck_mtx_unlock(hv_support_lck_mtx); | |
143 | break; | |
144 | } else { | |
145 | hv_callbacks.memory_pressure(NULL); | |
146 | } | |
147 | lck_mtx_unlock(hv_support_lck_mtx); | |
148 | } | |
149 | ||
150 | thread_deallocate(current_thread()); | |
151 | } | |
152 | ||
153 | /* subscribe to memory pressure notifications */ | |
154 | kern_return_t | |
155 | hv_set_mp_notify(void) { | |
156 | kern_return_t kr; | |
157 | ||
158 | lck_mtx_lock(hv_support_lck_mtx); | |
159 | if (hv_callbacks_enabled == 0) { | |
160 | lck_mtx_unlock(hv_support_lck_mtx); | |
161 | return KERN_FAILURE; | |
162 | } | |
163 | ||
164 | if (hv_mp_notify_enabled == 1) { | |
165 | hv_mp_notify_destroy = 0; | |
166 | lck_mtx_unlock(hv_support_lck_mtx); | |
167 | return KERN_SUCCESS; | |
168 | } | |
169 | ||
170 | kr = kernel_thread_start((thread_continue_t) &hv_mp_notify, NULL, | |
171 | &hv_mp_notify_thread); | |
172 | ||
173 | if (kr == KERN_SUCCESS) { | |
174 | hv_mp_notify_enabled = 1; | |
175 | } | |
176 | lck_mtx_unlock(hv_support_lck_mtx); | |
177 | ||
178 | return kr; | |
179 | } | |
180 | ||
181 | /* unsubscribe from memory pressure notifications */ | |
182 | void | |
183 | hv_release_mp_notify(void) { | |
184 | lck_mtx_lock(hv_support_lck_mtx); | |
185 | if (hv_mp_notify_enabled == 1) { | |
186 | hv_mp_notify_destroy = 1; | |
187 | } | |
188 | lck_mtx_unlock(hv_support_lck_mtx); | |
189 | } | |
190 | ||
191 | /* register a list of trap handlers for the hv_*_trap syscalls */ | |
192 | kern_return_t | |
193 | hv_set_traps(hv_trap_type_t trap_type, const hv_trap_t *traps, | |
194 | unsigned trap_count) | |
195 | { | |
196 | hv_trap_table_t *trap_table = &hv_trap_table[trap_type]; | |
197 | kern_return_t kr = KERN_FAILURE; | |
198 | ||
199 | lck_mtx_lock(hv_support_lck_mtx); | |
200 | if (trap_table->trap_count == 0) { | |
201 | trap_table->traps = traps; | |
202 | OSMemoryBarrier(); | |
203 | trap_table->trap_count = trap_count; | |
204 | kr = KERN_SUCCESS; | |
205 | } | |
206 | lck_mtx_unlock(hv_support_lck_mtx); | |
207 | ||
208 | return kr; | |
209 | } | |
210 | ||
211 | /* release hv_*_trap traps */ | |
212 | void | |
213 | hv_release_traps(hv_trap_type_t trap_type) { | |
214 | hv_trap_table_t *trap_table = &hv_trap_table[trap_type]; | |
215 | ||
216 | lck_mtx_lock(hv_support_lck_mtx); | |
217 | trap_table->trap_count = 0; | |
218 | OSMemoryBarrier(); | |
219 | trap_table->traps = NULL; | |
220 | lck_mtx_unlock(hv_support_lck_mtx); | |
221 | } | |
222 | ||
223 | /* register callbacks for certain task/thread events for tasks/threads with | |
224 | associated hv objects */ | |
225 | kern_return_t | |
226 | hv_set_callbacks(hv_callbacks_t callbacks) { | |
227 | kern_return_t kr = KERN_FAILURE; | |
228 | ||
229 | lck_mtx_lock(hv_support_lck_mtx); | |
230 | if (hv_callbacks_enabled == 0) { | |
231 | hv_callbacks = callbacks; | |
232 | hv_callbacks_enabled = 1; | |
233 | kr = KERN_SUCCESS; | |
234 | } | |
235 | lck_mtx_unlock(hv_support_lck_mtx); | |
236 | ||
237 | return kr; | |
238 | } | |
239 | ||
240 | /* release callbacks for task/thread events */ | |
241 | void | |
242 | hv_release_callbacks(void) { | |
243 | lck_mtx_lock(hv_support_lck_mtx); | |
244 | hv_callbacks = (hv_callbacks_t) { | |
245 | .dispatch = NULL, | |
246 | .preempt = NULL, | |
247 | .thread_destroy = NULL, | |
248 | .task_destroy = NULL, | |
249 | .volatile_state = NULL, | |
250 | .memory_pressure = NULL | |
251 | }; | |
252 | ||
253 | hv_callbacks_enabled = 0; | |
254 | lck_mtx_unlock(hv_support_lck_mtx); | |
255 | } | |
256 | ||
257 | /* dispatch hv_task_trap/hv_thread_trap syscalls to trap handlers, | |
258 | fail for invalid index or absence of trap handlers, trap handler is | |
259 | responsible for validating targets */ | |
260 | #define HV_TRAP_DISPATCH(type, index, target, argument)\ | |
261 | ((__probable(index < hv_trap_table[type].trap_count)) ? \ | |
262 | hv_trap_table[type].traps[index](target, argument) \ | |
263 | : KERN_INVALID_ARGUMENT) | |
264 | ||
265 | kern_return_t hv_task_trap(uint64_t index, uint64_t arg) { | |
266 | return HV_TRAP_DISPATCH(HV_TASK_TRAP, index, hv_get_task_target(), arg); | |
267 | } | |
268 | ||
269 | kern_return_t hv_thread_trap(uint64_t index, uint64_t arg) { | |
270 | return HV_TRAP_DISPATCH(HV_THREAD_TRAP, index, hv_get_thread_target(), arg); | |
271 | } |