]>
Commit | Line | Data |
---|---|---|
fe8ab488 A |
1 | /* |
2 | * Copyright (c) 2013 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
0a7de745 | 5 | * |
fe8ab488 A |
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. | |
0a7de745 | 14 | * |
fe8ab488 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
fe8ab488 A |
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. | |
0a7de745 | 25 | * |
fe8ab488 A |
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 = { | |
0a7de745 A |
45 | .dispatch = NULL, /* thread is being dispatched for execution */ |
46 | .preempt = NULL, /* thread is being preempted */ | |
47 | .suspend = NULL, /* system is being suspended */ | |
48 | .thread_destroy = NULL, /* thread is being destroyed */ | |
49 | .task_destroy = NULL, /* task is being destroyed */ | |
50 | .volatile_state = NULL, /* thread state is becoming volatile */ | |
fe8ab488 A |
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; | |
fe8ab488 A |
66 | static lck_grp_t *hv_support_lck_grp = NULL; |
67 | static lck_mtx_t *hv_support_lck_mtx = NULL; | |
fe8ab488 A |
68 | |
69 | /* hv_support boot initialization */ | |
70 | void | |
0a7de745 A |
71 | hv_support_init(void) |
72 | { | |
fe8ab488 A |
73 | #if defined(__x86_64__) && CONFIG_VMX |
74 | hv_support_available = vmx_hv_support(); | |
75 | #endif | |
76 | ||
77 | hv_support_lck_grp = lck_grp_alloc_init("hv_support", LCK_GRP_ATTR_NULL); | |
78 | assert(hv_support_lck_grp); | |
79 | ||
80 | hv_support_lck_mtx = lck_mtx_alloc_init(hv_support_lck_grp, LCK_ATTR_NULL); | |
81 | assert(hv_support_lck_mtx); | |
82 | } | |
83 | ||
84 | /* returns true if hv_support is available on this machine */ | |
85 | int | |
0a7de745 A |
86 | hv_get_support(void) |
87 | { | |
fe8ab488 A |
88 | return hv_support_available; |
89 | } | |
90 | ||
91 | /* associate an hv object with the current task */ | |
92 | void | |
0a7de745 A |
93 | hv_set_task_target(void *target) |
94 | { | |
fe8ab488 A |
95 | current_task()->hv_task_target = target; |
96 | } | |
97 | ||
98 | /* associate an hv object with the current thread */ | |
99 | void | |
0a7de745 A |
100 | hv_set_thread_target(void *target) |
101 | { | |
fe8ab488 A |
102 | current_thread()->hv_thread_target = target; |
103 | } | |
104 | ||
105 | /* get hv object associated with the current task */ | |
106 | void* | |
0a7de745 A |
107 | hv_get_task_target(void) |
108 | { | |
fe8ab488 A |
109 | return current_task()->hv_task_target; |
110 | } | |
111 | ||
112 | /* get hv object associated with the current thread */ | |
113 | void* | |
0a7de745 A |
114 | hv_get_thread_target(void) |
115 | { | |
fe8ab488 A |
116 | return current_thread()->hv_thread_target; |
117 | } | |
118 | ||
119 | /* test if a given thread state may be volatile between dispatch | |
0a7de745 | 120 | * and preemption */ |
fe8ab488 | 121 | int |
0a7de745 A |
122 | hv_get_volatile_state(hv_volatile_state_t state) |
123 | { | |
fe8ab488 A |
124 | int is_volatile = 0; |
125 | ||
126 | #if (defined(__x86_64__)) | |
127 | if (state == HV_DEBUG_STATE) { | |
128 | is_volatile = (current_thread()->machine.ids != NULL); | |
129 | } | |
130 | #endif | |
131 | ||
132 | return is_volatile; | |
133 | } | |
134 | ||
fe8ab488 A |
135 | /* register a list of trap handlers for the hv_*_trap syscalls */ |
136 | kern_return_t | |
137 | hv_set_traps(hv_trap_type_t trap_type, const hv_trap_t *traps, | |
0a7de745 | 138 | unsigned trap_count) |
fe8ab488 A |
139 | { |
140 | hv_trap_table_t *trap_table = &hv_trap_table[trap_type]; | |
141 | kern_return_t kr = KERN_FAILURE; | |
142 | ||
143 | lck_mtx_lock(hv_support_lck_mtx); | |
0a7de745 | 144 | if (trap_table->trap_count == 0) { |
fe8ab488 A |
145 | trap_table->traps = traps; |
146 | OSMemoryBarrier(); | |
147 | trap_table->trap_count = trap_count; | |
148 | kr = KERN_SUCCESS; | |
149 | } | |
150 | lck_mtx_unlock(hv_support_lck_mtx); | |
151 | ||
152 | return kr; | |
153 | } | |
154 | ||
155 | /* release hv_*_trap traps */ | |
156 | void | |
0a7de745 A |
157 | hv_release_traps(hv_trap_type_t trap_type) |
158 | { | |
fe8ab488 A |
159 | hv_trap_table_t *trap_table = &hv_trap_table[trap_type]; |
160 | ||
161 | lck_mtx_lock(hv_support_lck_mtx); | |
162 | trap_table->trap_count = 0; | |
163 | OSMemoryBarrier(); | |
164 | trap_table->traps = NULL; | |
165 | lck_mtx_unlock(hv_support_lck_mtx); | |
166 | } | |
167 | ||
168 | /* register callbacks for certain task/thread events for tasks/threads with | |
0a7de745 | 169 | * associated hv objects */ |
fe8ab488 | 170 | kern_return_t |
0a7de745 A |
171 | hv_set_callbacks(hv_callbacks_t callbacks) |
172 | { | |
fe8ab488 A |
173 | kern_return_t kr = KERN_FAILURE; |
174 | ||
175 | lck_mtx_lock(hv_support_lck_mtx); | |
0a7de745 | 176 | if (hv_callbacks_enabled == 0) { |
fe8ab488 A |
177 | hv_callbacks = callbacks; |
178 | hv_callbacks_enabled = 1; | |
179 | kr = KERN_SUCCESS; | |
180 | } | |
181 | lck_mtx_unlock(hv_support_lck_mtx); | |
182 | ||
183 | return kr; | |
184 | } | |
185 | ||
186 | /* release callbacks for task/thread events */ | |
187 | void | |
0a7de745 A |
188 | hv_release_callbacks(void) |
189 | { | |
190 | lck_mtx_lock(hv_support_lck_mtx); | |
fe8ab488 A |
191 | hv_callbacks = (hv_callbacks_t) { |
192 | .dispatch = NULL, | |
193 | .preempt = NULL, | |
04b8595b | 194 | .suspend = NULL, |
fe8ab488 A |
195 | .thread_destroy = NULL, |
196 | .task_destroy = NULL, | |
197 | .volatile_state = NULL, | |
198 | .memory_pressure = NULL | |
199 | }; | |
200 | ||
201 | hv_callbacks_enabled = 0; | |
202 | lck_mtx_unlock(hv_support_lck_mtx); | |
203 | } | |
204 | ||
04b8595b A |
205 | /* system suspend notification */ |
206 | void | |
0a7de745 A |
207 | hv_suspend(void) |
208 | { | |
04b8595b A |
209 | if (hv_callbacks_enabled) { |
210 | hv_callbacks.suspend(); | |
211 | } | |
212 | } | |
213 | ||
fe8ab488 | 214 | /* dispatch hv_task_trap/hv_thread_trap syscalls to trap handlers, |
0a7de745 A |
215 | * fail for invalid index or absence of trap handlers, trap handler is |
216 | * responsible for validating targets */ | |
217 | #define HV_TRAP_DISPATCH(type, index, target, argument) \ | |
fe8ab488 | 218 | ((__probable(index < hv_trap_table[type].trap_count)) ? \ |
0a7de745 A |
219 | hv_trap_table[type].traps[index](target, argument) \ |
220 | : KERN_INVALID_ARGUMENT) | |
fe8ab488 | 221 | |
0a7de745 A |
222 | kern_return_t |
223 | hv_task_trap(uint64_t index, uint64_t arg) | |
224 | { | |
fe8ab488 A |
225 | return HV_TRAP_DISPATCH(HV_TASK_TRAP, index, hv_get_task_target(), arg); |
226 | } | |
227 | ||
0a7de745 A |
228 | kern_return_t |
229 | hv_thread_trap(uint64_t index, uint64_t arg) | |
230 | { | |
fe8ab488 A |
231 | return HV_TRAP_DISPATCH(HV_THREAD_TRAP, index, hv_get_thread_target(), arg); |
232 | } |