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