]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/dtrace/dtrace_ptss.c
xnu-4570.71.2.tar.gz
[apple/xnu.git] / bsd / dev / dtrace / dtrace_ptss.c
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, 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 <sys/types.h>
30 #include <sys/proc.h>
31 #include <sys/proc_internal.h>
32 #include <sys/systm.h>
33 #include <sys/user.h>
34 #include <sys/dtrace_ptss.h>
35
36 #include <mach/vm_map.h>
37 #include <mach/vm_param.h>
38 #include <mach/mach_vm.h>
39
40 #include <kern/task.h>
41
42 #include <vm/vm_map.h>
43
44 /*
45 * This function requires the sprlock to be held
46 *
47 * In general, it will not block. If it needs to allocate a new
48 * page of memory, the underlying kernel _MALLOC may block.
49 */
50 struct dtrace_ptss_page_entry*
51 dtrace_ptss_claim_entry_locked(struct proc* p) {
52 LCK_MTX_ASSERT(&p->p_dtrace_sprlock, LCK_MTX_ASSERT_OWNED);
53
54 struct dtrace_ptss_page_entry* entry = NULL;
55
56 while (TRUE) {
57 struct dtrace_ptss_page_entry* temp = p->p_dtrace_ptss_free_list;
58
59 if (temp == NULL) {
60 // Nothing on the free list. Allocate a new page, its okay if multiple threads race here.
61 struct dtrace_ptss_page* page = dtrace_ptss_allocate_page(p);
62
63 // Make sure we actually got a page
64 if (page == NULL)
65 return NULL;
66
67 // Add the page to the page list
68 page->next = p->p_dtrace_ptss_pages;
69 p->p_dtrace_ptss_pages = page;
70
71 // CAS the entries onto the free list.
72 do {
73 page->entries[DTRACE_PTSS_ENTRIES_PER_PAGE-1].next = p->p_dtrace_ptss_free_list;
74 } while (!OSCompareAndSwapPtr((void *)page->entries[DTRACE_PTSS_ENTRIES_PER_PAGE-1].next,
75 (void *)&page->entries[0],
76 (void * volatile *)&p->p_dtrace_ptss_free_list));
77
78 // Now that we've added to the free list, try again.
79 continue;
80 }
81
82 // Claim temp
83 if (!OSCompareAndSwapPtr((void *)temp, (void *)temp->next, (void * volatile *)&p->p_dtrace_ptss_free_list))
84 continue;
85
86 // At this point, we own temp.
87 entry = temp;
88
89 break;
90 }
91
92 return entry;
93 }
94
95 /*
96 * This function does not require any locks to be held on entry.
97 */
98 struct dtrace_ptss_page_entry*
99 dtrace_ptss_claim_entry(struct proc* p) {
100 // Verify no locks held on entry
101 LCK_MTX_ASSERT(&p->p_dtrace_sprlock, LCK_MTX_ASSERT_NOTOWNED);
102 LCK_MTX_ASSERT(&p->p_mlock, LCK_MTX_ASSERT_NOTOWNED);
103
104 struct dtrace_ptss_page_entry* entry = NULL;
105
106 while (TRUE) {
107 struct dtrace_ptss_page_entry* temp = p->p_dtrace_ptss_free_list;
108
109 if (temp == NULL) {
110 lck_mtx_lock(&p->p_dtrace_sprlock);
111 temp = dtrace_ptss_claim_entry_locked(p);
112 lck_mtx_unlock(&p->p_dtrace_sprlock);
113 return temp;
114 }
115
116 // Claim temp
117 if (!OSCompareAndSwapPtr((void *)temp, (void *)temp->next, (void * volatile *)&p->p_dtrace_ptss_free_list))
118 continue;
119
120 // At this point, we own temp.
121 entry = temp;
122
123 break;
124 }
125
126 return entry;
127 }
128
129 /*
130 * This function does not require any locks to be held on entry.
131 *
132 * (PR-11138709) A NULL p->p_dtrace_ptss_pages means the entry can
133 * no longer be referenced safely. When found in this state, the chore
134 * of releasing an entry to the free list is ignored.
135 */
136 void
137 dtrace_ptss_release_entry(struct proc* p, struct dtrace_ptss_page_entry* e) {
138 if (p && p->p_dtrace_ptss_pages && e) {
139 do {
140 e->next = p->p_dtrace_ptss_free_list;
141 } while (!OSCompareAndSwapPtr((void *)e->next, (void *)e, (void * volatile *)&p->p_dtrace_ptss_free_list));
142 }
143 }
144
145 /*
146 * This function allocates a new page in the target process's address space.
147 *
148 * It returns a dtrace_ptss_page that has its entries chained, with the last
149 * entries next field set to NULL. It does not add the page or the entries to
150 * the process's page/entry lists.
151 *
152 * This function does not require that any locks be held when it is invoked.
153 */
154 struct dtrace_ptss_page*
155 dtrace_ptss_allocate_page(struct proc* p)
156 {
157 // Allocate the kernel side data
158 struct dtrace_ptss_page* ptss_page = _MALLOC(sizeof(struct dtrace_ptss_page), M_TEMP, M_ZERO | M_WAITOK);
159 if (ptss_page == NULL)
160 return NULL;
161
162 // Now allocate a page in user space and set its protections to allow execute.
163 task_t task = p->task;
164 vm_map_t map = get_task_map_reference(task);
165 if (map == NULL)
166 goto err;
167
168 mach_vm_size_t size = PAGE_MAX_SIZE;
169 mach_vm_offset_t addr = 0;
170 #if CONFIG_EMBEDDED
171 mach_vm_offset_t write_addr = 0;
172 /*
173 * The embedded OS has extra permissions for writable and executable pages.
174 * To ensure correct permissions, we must set the page protections separately.
175 */
176 vm_prot_t cur_protection = VM_PROT_READ|VM_PROT_EXECUTE;
177 vm_prot_t max_protection = VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE;
178 #else
179 vm_prot_t cur_protection = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
180 vm_prot_t max_protection = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
181 #endif /* CONFIG_EMBEDDED */
182
183 kern_return_t kr = mach_vm_map_kernel(map, &addr, size, 0, VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_NONE, IPC_PORT_NULL, 0, FALSE, cur_protection, max_protection, VM_INHERIT_DEFAULT);
184 if (kr != KERN_SUCCESS) {
185 goto err;
186 }
187 #if CONFIG_EMBEDDED
188 /*
189 * If on embedded, remap the scratch space as writable at another
190 * virtual address
191 */
192 kr = mach_vm_remap_kernel(map, &write_addr, size, 0, VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_NONE, map, addr, FALSE, &cur_protection, &max_protection, VM_INHERIT_DEFAULT);
193 if (kr != KERN_SUCCESS || !(max_protection & VM_PROT_WRITE))
194 goto err;
195
196 kr = mach_vm_protect (map, (mach_vm_offset_t)write_addr, (mach_vm_size_t)size, 0, VM_PROT_READ | VM_PROT_WRITE);
197 if (kr != KERN_SUCCESS)
198 goto err;
199 #endif
200 // Chain the page entries.
201 int i;
202 for (i=0; i<DTRACE_PTSS_ENTRIES_PER_PAGE; i++) {
203 ptss_page->entries[i].addr = addr + (i * DTRACE_PTSS_SCRATCH_SPACE_PER_THREAD);
204 #if CONFIG_EMBEDDED
205 ptss_page->entries[i].write_addr = write_addr + (i * DTRACE_PTSS_SCRATCH_SPACE_PER_THREAD);
206 #endif
207 ptss_page->entries[i].next = &ptss_page->entries[i+1];
208 }
209
210 // The last entry should point to NULL
211 ptss_page->entries[DTRACE_PTSS_ENTRIES_PER_PAGE-1].next = NULL;
212
213 vm_map_deallocate(map);
214
215 return ptss_page;
216
217 err:
218 _FREE(ptss_page, M_TEMP);
219
220 if (map)
221 vm_map_deallocate(map);
222
223 return NULL;
224 }
225
226 /*
227 * This function frees an existing page in the target process's address space.
228 *
229 * It does not alter any of the process's page/entry lists.
230 *
231 * TODO: Inline in dtrace_ptrace_exec_exit?
232 */
233 void
234 dtrace_ptss_free_page(struct proc* p, struct dtrace_ptss_page* ptss_page)
235 {
236 // Grab the task and get a reference to its vm_map
237 task_t task = p->task;
238 vm_map_t map = get_task_map_reference(task);
239
240 mach_vm_address_t addr = ptss_page->entries[0].addr;
241 mach_vm_size_t size = PAGE_SIZE; // We need some way to assert that this matches vm_map_round_page() !!!
242
243 // Silent failures, no point in checking return code.
244 mach_vm_deallocate(map, addr, size);
245
246 #ifdef CONFIG_EMBEDDED
247 mach_vm_address_t write_addr = ptss_page->entries[0].write_addr;
248 mach_vm_deallocate(map, write_addr, size);
249 #endif
250
251 vm_map_deallocate(map);
252 }
253
254 /*
255 * This function assumes that the target process has been
256 * suspended, and the proc_lock & sprlock is held
257 */
258 void
259 dtrace_ptss_enable(struct proc* p) {
260 LCK_MTX_ASSERT(&p->p_dtrace_sprlock, LCK_MTX_ASSERT_OWNED);
261 LCK_MTX_ASSERT(&p->p_mlock, LCK_MTX_ASSERT_OWNED);
262
263 struct uthread* uth;
264 /*
265 * XXX There has been a concern raised about holding the proc_lock
266 * while calling dtrace_ptss_claim_entry(), due to the fact
267 * that dtrace_ptss_claim_entry() can potentially malloc.
268 */
269 TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
270 uth->t_dtrace_scratch = dtrace_ptss_claim_entry_locked(p);
271 }
272 }
273
274 /*
275 * This function is not thread safe.
276 *
277 * It assumes the sprlock is held, and the proc_lock is not.
278 */
279 void
280 dtrace_ptss_exec_exit(struct proc* p) {
281 /*
282 * Should hold sprlock to touch the pages list. Must not
283 * hold the proc lock to avoid deadlock.
284 */
285 LCK_MTX_ASSERT(&p->p_dtrace_sprlock, LCK_MTX_ASSERT_OWNED);
286 LCK_MTX_ASSERT(&p->p_mlock, LCK_MTX_ASSERT_NOTOWNED);
287
288 p->p_dtrace_ptss_free_list = NULL;
289
290 struct dtrace_ptss_page* temp = p->p_dtrace_ptss_pages;
291 p->p_dtrace_ptss_pages = NULL;
292
293 while (temp != NULL) {
294 struct dtrace_ptss_page* next = temp->next;
295
296 // Do we need to specifically mach_vm_deallocate the user pages?
297 // This can be called when the process is exiting, I believe the proc's
298 // vm_map_t may already be toast.
299
300 // Must be certain to free the kernel memory!
301 _FREE(temp, M_TEMP);
302 temp = next;
303 }
304 }
305
306 /*
307 * This function is not thread safe. It is not used for vfork.
308 *
309 * The child proc ptss fields are initialized to NULL at fork time.
310 * Pages allocated in the parent are copied as part of the vm_map copy, though.
311 * We need to deallocate those pages.
312 *
313 * Parent and child sprlock should be held, and proc_lock must NOT be held.
314 */
315 void
316 dtrace_ptss_fork(struct proc* parent, struct proc* child) {
317 // The child should not have any pages/entries allocated at this point.
318 // ASSERT(child->p_dtrace_ptss_pages == NULL);
319 // ASSERT(child->p_dtrace_ptss_free_list == NULL);
320
321 /*
322 * The parent's sprlock should be held, to protect its pages list
323 * from changing while the child references it. The child's sprlock
324 * must also be held, because we are modifying its pages list.
325 * Finally, to prevent a deadlock with the fasttrap cleanup code,
326 * neither the parent or child proc_lock should be held.
327 */
328 LCK_MTX_ASSERT(&parent->p_dtrace_sprlock, LCK_MTX_ASSERT_OWNED);
329 LCK_MTX_ASSERT(&parent->p_mlock, LCK_MTX_ASSERT_NOTOWNED);
330 LCK_MTX_ASSERT(&child->p_dtrace_sprlock, LCK_MTX_ASSERT_OWNED);
331 LCK_MTX_ASSERT(&child->p_mlock, LCK_MTX_ASSERT_NOTOWNED);
332
333 // Get page list from *PARENT*
334 struct dtrace_ptss_page* temp = parent->p_dtrace_ptss_pages;
335
336 while (temp != NULL) {
337 // Freeing the page in the *CHILD*
338 dtrace_ptss_free_page(child, temp);
339
340 // Do not free the kernel memory, it belong to the parent.
341 temp = temp->next;
342 }
343 }