1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3*c54f35caSApple OSS Distributions *
4*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions *
6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions *
15*c54f35caSApple OSS Distributions * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions *
18*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions * limitations under the License.
25*c54f35caSApple OSS Distributions *
26*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions */
28*c54f35caSApple OSS Distributions
29*c54f35caSApple OSS Distributions #include <sys/types.h>
30*c54f35caSApple OSS Distributions #include <sys/proc.h>
31*c54f35caSApple OSS Distributions #include <sys/proc_internal.h>
32*c54f35caSApple OSS Distributions #include <sys/systm.h>
33*c54f35caSApple OSS Distributions #include <sys/user.h>
34*c54f35caSApple OSS Distributions #include <sys/dtrace_ptss.h>
35*c54f35caSApple OSS Distributions
36*c54f35caSApple OSS Distributions #include <mach/vm_map.h>
37*c54f35caSApple OSS Distributions #include <mach/vm_param.h>
38*c54f35caSApple OSS Distributions #include <mach/mach_vm.h>
39*c54f35caSApple OSS Distributions
40*c54f35caSApple OSS Distributions #include <kern/task.h>
41*c54f35caSApple OSS Distributions
42*c54f35caSApple OSS Distributions #include <vm/vm_map.h>
43*c54f35caSApple OSS Distributions
44*c54f35caSApple OSS Distributions /*
45*c54f35caSApple OSS Distributions * This function requires the sprlock to be held
46*c54f35caSApple OSS Distributions *
47*c54f35caSApple OSS Distributions * In general, it will not block. If it needs to allocate a new
48*c54f35caSApple OSS Distributions * page of memory, the underlying kernel kalloc may block.
49*c54f35caSApple OSS Distributions */
50*c54f35caSApple OSS Distributions struct dtrace_ptss_page_entry*
dtrace_ptss_claim_entry_locked(struct proc * p)51*c54f35caSApple OSS Distributions dtrace_ptss_claim_entry_locked(struct proc* p)
52*c54f35caSApple OSS Distributions {
53*c54f35caSApple OSS Distributions LCK_MTX_ASSERT(&p->p_dtrace_sprlock, LCK_MTX_ASSERT_OWNED);
54*c54f35caSApple OSS Distributions
55*c54f35caSApple OSS Distributions struct dtrace_ptss_page_entry* entry = NULL;
56*c54f35caSApple OSS Distributions
57*c54f35caSApple OSS Distributions while (TRUE) {
58*c54f35caSApple OSS Distributions struct dtrace_ptss_page_entry* temp = p->p_dtrace_ptss_free_list;
59*c54f35caSApple OSS Distributions
60*c54f35caSApple OSS Distributions if (temp == NULL) {
61*c54f35caSApple OSS Distributions // Nothing on the free list. Allocate a new page, its okay if multiple threads race here.
62*c54f35caSApple OSS Distributions struct dtrace_ptss_page* page = dtrace_ptss_allocate_page(p);
63*c54f35caSApple OSS Distributions
64*c54f35caSApple OSS Distributions // Make sure we actually got a page
65*c54f35caSApple OSS Distributions if (page == NULL) {
66*c54f35caSApple OSS Distributions return NULL;
67*c54f35caSApple OSS Distributions }
68*c54f35caSApple OSS Distributions
69*c54f35caSApple OSS Distributions // Add the page to the page list
70*c54f35caSApple OSS Distributions page->next = p->p_dtrace_ptss_pages;
71*c54f35caSApple OSS Distributions p->p_dtrace_ptss_pages = page;
72*c54f35caSApple OSS Distributions
73*c54f35caSApple OSS Distributions // CAS the entries onto the free list.
74*c54f35caSApple OSS Distributions do {
75*c54f35caSApple OSS Distributions page->entries[DTRACE_PTSS_ENTRIES_PER_PAGE - 1].next = p->p_dtrace_ptss_free_list;
76*c54f35caSApple OSS Distributions } while (!OSCompareAndSwapPtr((void *)page->entries[DTRACE_PTSS_ENTRIES_PER_PAGE - 1].next,
77*c54f35caSApple OSS Distributions (void *)&page->entries[0],
78*c54f35caSApple OSS Distributions (void * volatile *)&p->p_dtrace_ptss_free_list));
79*c54f35caSApple OSS Distributions
80*c54f35caSApple OSS Distributions // Now that we've added to the free list, try again.
81*c54f35caSApple OSS Distributions continue;
82*c54f35caSApple OSS Distributions }
83*c54f35caSApple OSS Distributions
84*c54f35caSApple OSS Distributions // Claim temp
85*c54f35caSApple OSS Distributions if (!OSCompareAndSwapPtr((void *)temp, (void *)temp->next, (void * volatile *)&p->p_dtrace_ptss_free_list)) {
86*c54f35caSApple OSS Distributions continue;
87*c54f35caSApple OSS Distributions }
88*c54f35caSApple OSS Distributions
89*c54f35caSApple OSS Distributions // At this point, we own temp.
90*c54f35caSApple OSS Distributions entry = temp;
91*c54f35caSApple OSS Distributions
92*c54f35caSApple OSS Distributions break;
93*c54f35caSApple OSS Distributions }
94*c54f35caSApple OSS Distributions
95*c54f35caSApple OSS Distributions return entry;
96*c54f35caSApple OSS Distributions }
97*c54f35caSApple OSS Distributions
98*c54f35caSApple OSS Distributions /*
99*c54f35caSApple OSS Distributions * This function does not require any locks to be held on entry.
100*c54f35caSApple OSS Distributions */
101*c54f35caSApple OSS Distributions struct dtrace_ptss_page_entry*
dtrace_ptss_claim_entry(struct proc * p)102*c54f35caSApple OSS Distributions dtrace_ptss_claim_entry(struct proc* p)
103*c54f35caSApple OSS Distributions {
104*c54f35caSApple OSS Distributions // Verify no locks held on entry
105*c54f35caSApple OSS Distributions LCK_MTX_ASSERT(&p->p_dtrace_sprlock, LCK_MTX_ASSERT_NOTOWNED);
106*c54f35caSApple OSS Distributions LCK_MTX_ASSERT(&p->p_mlock, LCK_MTX_ASSERT_NOTOWNED);
107*c54f35caSApple OSS Distributions
108*c54f35caSApple OSS Distributions struct dtrace_ptss_page_entry* entry = NULL;
109*c54f35caSApple OSS Distributions
110*c54f35caSApple OSS Distributions while (TRUE) {
111*c54f35caSApple OSS Distributions struct dtrace_ptss_page_entry* temp = p->p_dtrace_ptss_free_list;
112*c54f35caSApple OSS Distributions
113*c54f35caSApple OSS Distributions if (temp == NULL) {
114*c54f35caSApple OSS Distributions lck_mtx_lock(&p->p_dtrace_sprlock);
115*c54f35caSApple OSS Distributions temp = dtrace_ptss_claim_entry_locked(p);
116*c54f35caSApple OSS Distributions lck_mtx_unlock(&p->p_dtrace_sprlock);
117*c54f35caSApple OSS Distributions return temp;
118*c54f35caSApple OSS Distributions }
119*c54f35caSApple OSS Distributions
120*c54f35caSApple OSS Distributions // Claim temp
121*c54f35caSApple OSS Distributions if (!OSCompareAndSwapPtr((void *)temp, (void *)temp->next, (void * volatile *)&p->p_dtrace_ptss_free_list)) {
122*c54f35caSApple OSS Distributions continue;
123*c54f35caSApple OSS Distributions }
124*c54f35caSApple OSS Distributions
125*c54f35caSApple OSS Distributions // At this point, we own temp.
126*c54f35caSApple OSS Distributions entry = temp;
127*c54f35caSApple OSS Distributions
128*c54f35caSApple OSS Distributions break;
129*c54f35caSApple OSS Distributions }
130*c54f35caSApple OSS Distributions
131*c54f35caSApple OSS Distributions return entry;
132*c54f35caSApple OSS Distributions }
133*c54f35caSApple OSS Distributions
134*c54f35caSApple OSS Distributions /*
135*c54f35caSApple OSS Distributions * This function does not require any locks to be held on entry.
136*c54f35caSApple OSS Distributions *
137*c54f35caSApple OSS Distributions * (PR-11138709) A NULL p->p_dtrace_ptss_pages means the entry can
138*c54f35caSApple OSS Distributions * no longer be referenced safely. When found in this state, the chore
139*c54f35caSApple OSS Distributions * of releasing an entry to the free list is ignored.
140*c54f35caSApple OSS Distributions */
141*c54f35caSApple OSS Distributions void
dtrace_ptss_release_entry(struct proc * p,struct dtrace_ptss_page_entry * e)142*c54f35caSApple OSS Distributions dtrace_ptss_release_entry(struct proc* p, struct dtrace_ptss_page_entry* e)
143*c54f35caSApple OSS Distributions {
144*c54f35caSApple OSS Distributions if (p && p->p_dtrace_ptss_pages && e) {
145*c54f35caSApple OSS Distributions do {
146*c54f35caSApple OSS Distributions e->next = p->p_dtrace_ptss_free_list;
147*c54f35caSApple OSS Distributions } while (!OSCompareAndSwapPtr((void *)e->next, (void *)e, (void * volatile *)&p->p_dtrace_ptss_free_list));
148*c54f35caSApple OSS Distributions }
149*c54f35caSApple OSS Distributions }
150*c54f35caSApple OSS Distributions
151*c54f35caSApple OSS Distributions /*
152*c54f35caSApple OSS Distributions * This function allocates a new page in the target process's address space.
153*c54f35caSApple OSS Distributions *
154*c54f35caSApple OSS Distributions * It returns a dtrace_ptss_page that has its entries chained, with the last
155*c54f35caSApple OSS Distributions * entries next field set to NULL. It does not add the page or the entries to
156*c54f35caSApple OSS Distributions * the process's page/entry lists.
157*c54f35caSApple OSS Distributions *
158*c54f35caSApple OSS Distributions * This function does not require that any locks be held when it is invoked.
159*c54f35caSApple OSS Distributions */
160*c54f35caSApple OSS Distributions struct dtrace_ptss_page*
dtrace_ptss_allocate_page(struct proc * p)161*c54f35caSApple OSS Distributions dtrace_ptss_allocate_page(struct proc* p)
162*c54f35caSApple OSS Distributions {
163*c54f35caSApple OSS Distributions // Allocate the kernel side data
164*c54f35caSApple OSS Distributions struct dtrace_ptss_page* ptss_page = kalloc_type(struct dtrace_ptss_page, Z_ZERO | Z_WAITOK);
165*c54f35caSApple OSS Distributions if (ptss_page == NULL) {
166*c54f35caSApple OSS Distributions return NULL;
167*c54f35caSApple OSS Distributions }
168*c54f35caSApple OSS Distributions
169*c54f35caSApple OSS Distributions // Now allocate a page in user space and set its protections to allow execute.
170*c54f35caSApple OSS Distributions task_t task = proc_task(p);
171*c54f35caSApple OSS Distributions vm_map_t map = get_task_map_reference(task);
172*c54f35caSApple OSS Distributions if (map == NULL) {
173*c54f35caSApple OSS Distributions goto err;
174*c54f35caSApple OSS Distributions }
175*c54f35caSApple OSS Distributions
176*c54f35caSApple OSS Distributions mach_vm_size_t size = PAGE_MAX_SIZE;
177*c54f35caSApple OSS Distributions mach_vm_offset_t addr = 0;
178*c54f35caSApple OSS Distributions mach_vm_offset_t write_addr = 0;
179*c54f35caSApple OSS Distributions /*
180*c54f35caSApple OSS Distributions * The embedded OS has extra permissions for writable and executable pages.
181*c54f35caSApple OSS Distributions * To ensure correct permissions, we must set the page protections separately.
182*c54f35caSApple OSS Distributions */
183*c54f35caSApple OSS Distributions vm_prot_t cur_protection = VM_PROT_READ | VM_PROT_EXECUTE;
184*c54f35caSApple OSS Distributions vm_prot_t max_protection = VM_PROT_READ | VM_PROT_EXECUTE;
185*c54f35caSApple OSS Distributions kern_return_t kr;
186*c54f35caSApple OSS Distributions
187*c54f35caSApple OSS Distributions kr = mach_vm_map_kernel(map, &addr, size, 0,
188*c54f35caSApple OSS Distributions VM_MAP_KERNEL_FLAGS_ANYWHERE(), IPC_PORT_NULL, 0, FALSE,
189*c54f35caSApple OSS Distributions cur_protection, max_protection, VM_INHERIT_DEFAULT);
190*c54f35caSApple OSS Distributions if (kr != KERN_SUCCESS) {
191*c54f35caSApple OSS Distributions goto err;
192*c54f35caSApple OSS Distributions }
193*c54f35caSApple OSS Distributions
194*c54f35caSApple OSS Distributions /*
195*c54f35caSApple OSS Distributions * To ensure the page is properly marked as user debug, temporarily change
196*c54f35caSApple OSS Distributions * the permissions to rw and then back again to rx. The VM will keep track
197*c54f35caSApple OSS Distributions * of this remapping and on fault will pass PMAP_OPTIONS_XNU_USER_DEBUG
198*c54f35caSApple OSS Distributions * properly to the PMAP layer.
199*c54f35caSApple OSS Distributions */
200*c54f35caSApple OSS Distributions kr = mach_vm_protect(map, (mach_vm_offset_t)addr, (mach_vm_size_t)size, 0,
201*c54f35caSApple OSS Distributions VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY);
202*c54f35caSApple OSS Distributions if (kr != KERN_SUCCESS) {
203*c54f35caSApple OSS Distributions goto err;
204*c54f35caSApple OSS Distributions }
205*c54f35caSApple OSS Distributions
206*c54f35caSApple OSS Distributions kr = mach_vm_protect(map, (mach_vm_offset_t)addr, (mach_vm_size_t)size, 0,
207*c54f35caSApple OSS Distributions VM_PROT_READ | VM_PROT_EXECUTE);
208*c54f35caSApple OSS Distributions if (kr != KERN_SUCCESS) {
209*c54f35caSApple OSS Distributions goto err;
210*c54f35caSApple OSS Distributions }
211*c54f35caSApple OSS Distributions
212*c54f35caSApple OSS Distributions /*
213*c54f35caSApple OSS Distributions * If on embedded, remap the scratch space as writable at another
214*c54f35caSApple OSS Distributions * virtual address
215*c54f35caSApple OSS Distributions */
216*c54f35caSApple OSS Distributions 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);
217*c54f35caSApple OSS Distributions if (kr != KERN_SUCCESS || !(max_protection & VM_PROT_WRITE)) {
218*c54f35caSApple OSS Distributions goto err;
219*c54f35caSApple OSS Distributions }
220*c54f35caSApple OSS Distributions
221*c54f35caSApple OSS Distributions kr = mach_vm_protect(map, (mach_vm_offset_t)write_addr, (mach_vm_size_t)size, 0, VM_PROT_READ | VM_PROT_WRITE);
222*c54f35caSApple OSS Distributions if (kr != KERN_SUCCESS) {
223*c54f35caSApple OSS Distributions goto err;
224*c54f35caSApple OSS Distributions }
225*c54f35caSApple OSS Distributions
226*c54f35caSApple OSS Distributions // Chain the page entries.
227*c54f35caSApple OSS Distributions int i;
228*c54f35caSApple OSS Distributions for (i = 0; i < DTRACE_PTSS_ENTRIES_PER_PAGE; i++) {
229*c54f35caSApple OSS Distributions ptss_page->entries[i].addr = addr + (i * DTRACE_PTSS_SCRATCH_SPACE_PER_THREAD);
230*c54f35caSApple OSS Distributions ptss_page->entries[i].write_addr = write_addr + (i * DTRACE_PTSS_SCRATCH_SPACE_PER_THREAD);
231*c54f35caSApple OSS Distributions ptss_page->entries[i].next = &ptss_page->entries[i + 1];
232*c54f35caSApple OSS Distributions }
233*c54f35caSApple OSS Distributions
234*c54f35caSApple OSS Distributions // The last entry should point to NULL
235*c54f35caSApple OSS Distributions ptss_page->entries[DTRACE_PTSS_ENTRIES_PER_PAGE - 1].next = NULL;
236*c54f35caSApple OSS Distributions
237*c54f35caSApple OSS Distributions vm_map_deallocate(map);
238*c54f35caSApple OSS Distributions
239*c54f35caSApple OSS Distributions return ptss_page;
240*c54f35caSApple OSS Distributions
241*c54f35caSApple OSS Distributions err:
242*c54f35caSApple OSS Distributions kfree_type(struct dtrace_ptss_page, ptss_page);
243*c54f35caSApple OSS Distributions
244*c54f35caSApple OSS Distributions if (map) {
245*c54f35caSApple OSS Distributions vm_map_deallocate(map);
246*c54f35caSApple OSS Distributions }
247*c54f35caSApple OSS Distributions
248*c54f35caSApple OSS Distributions return NULL;
249*c54f35caSApple OSS Distributions }
250*c54f35caSApple OSS Distributions
251*c54f35caSApple OSS Distributions /*
252*c54f35caSApple OSS Distributions * This function frees an existing page in the target process's address space.
253*c54f35caSApple OSS Distributions *
254*c54f35caSApple OSS Distributions * It does not alter any of the process's page/entry lists.
255*c54f35caSApple OSS Distributions *
256*c54f35caSApple OSS Distributions * TODO: Inline in dtrace_ptrace_exec_exit?
257*c54f35caSApple OSS Distributions */
258*c54f35caSApple OSS Distributions void
dtrace_ptss_free_page(struct proc * p,struct dtrace_ptss_page * ptss_page)259*c54f35caSApple OSS Distributions dtrace_ptss_free_page(struct proc* p, struct dtrace_ptss_page* ptss_page)
260*c54f35caSApple OSS Distributions {
261*c54f35caSApple OSS Distributions // Grab the task and get a reference to its vm_map
262*c54f35caSApple OSS Distributions task_t task = proc_task(p);
263*c54f35caSApple OSS Distributions vm_map_t map = get_task_map_reference(task);
264*c54f35caSApple OSS Distributions
265*c54f35caSApple OSS Distributions mach_vm_address_t addr = ptss_page->entries[0].addr;
266*c54f35caSApple OSS Distributions mach_vm_size_t size = PAGE_SIZE; // We need some way to assert that this matches vm_map_round_page() !!!
267*c54f35caSApple OSS Distributions
268*c54f35caSApple OSS Distributions // Silent failures, no point in checking return code.
269*c54f35caSApple OSS Distributions mach_vm_deallocate(map, addr, size);
270*c54f35caSApple OSS Distributions
271*c54f35caSApple OSS Distributions mach_vm_address_t write_addr = ptss_page->entries[0].write_addr;
272*c54f35caSApple OSS Distributions mach_vm_deallocate(map, write_addr, size);
273*c54f35caSApple OSS Distributions
274*c54f35caSApple OSS Distributions vm_map_deallocate(map);
275*c54f35caSApple OSS Distributions }
276*c54f35caSApple OSS Distributions
277*c54f35caSApple OSS Distributions /*
278*c54f35caSApple OSS Distributions * This function assumes that the target process has been
279*c54f35caSApple OSS Distributions * suspended, and the proc_lock & sprlock is held
280*c54f35caSApple OSS Distributions */
281*c54f35caSApple OSS Distributions void
dtrace_ptss_enable(struct proc * p)282*c54f35caSApple OSS Distributions dtrace_ptss_enable(struct proc* p)
283*c54f35caSApple OSS Distributions {
284*c54f35caSApple OSS Distributions LCK_MTX_ASSERT(&p->p_dtrace_sprlock, LCK_MTX_ASSERT_OWNED);
285*c54f35caSApple OSS Distributions LCK_MTX_ASSERT(&p->p_mlock, LCK_MTX_ASSERT_OWNED);
286*c54f35caSApple OSS Distributions
287*c54f35caSApple OSS Distributions struct uthread* uth;
288*c54f35caSApple OSS Distributions /*
289*c54f35caSApple OSS Distributions * XXX There has been a concern raised about holding the proc_lock
290*c54f35caSApple OSS Distributions * while calling dtrace_ptss_claim_entry(), due to the fact
291*c54f35caSApple OSS Distributions * that dtrace_ptss_claim_entry() can potentially malloc.
292*c54f35caSApple OSS Distributions */
293*c54f35caSApple OSS Distributions TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
294*c54f35caSApple OSS Distributions uth->t_dtrace_scratch = dtrace_ptss_claim_entry_locked(p);
295*c54f35caSApple OSS Distributions }
296*c54f35caSApple OSS Distributions }
297*c54f35caSApple OSS Distributions
298*c54f35caSApple OSS Distributions /*
299*c54f35caSApple OSS Distributions * This function is not thread safe.
300*c54f35caSApple OSS Distributions *
301*c54f35caSApple OSS Distributions * It assumes the sprlock is held, and the proc_lock is not.
302*c54f35caSApple OSS Distributions */
303*c54f35caSApple OSS Distributions void
dtrace_ptss_exec_exit(struct proc * p)304*c54f35caSApple OSS Distributions dtrace_ptss_exec_exit(struct proc* p)
305*c54f35caSApple OSS Distributions {
306*c54f35caSApple OSS Distributions /*
307*c54f35caSApple OSS Distributions * Should hold sprlock to touch the pages list. Must not
308*c54f35caSApple OSS Distributions * hold the proc lock to avoid deadlock.
309*c54f35caSApple OSS Distributions */
310*c54f35caSApple OSS Distributions LCK_MTX_ASSERT(&p->p_dtrace_sprlock, LCK_MTX_ASSERT_OWNED);
311*c54f35caSApple OSS Distributions LCK_MTX_ASSERT(&p->p_mlock, LCK_MTX_ASSERT_NOTOWNED);
312*c54f35caSApple OSS Distributions
313*c54f35caSApple OSS Distributions p->p_dtrace_ptss_free_list = NULL;
314*c54f35caSApple OSS Distributions
315*c54f35caSApple OSS Distributions struct dtrace_ptss_page* temp = p->p_dtrace_ptss_pages;
316*c54f35caSApple OSS Distributions p->p_dtrace_ptss_pages = NULL;
317*c54f35caSApple OSS Distributions
318*c54f35caSApple OSS Distributions while (temp != NULL) {
319*c54f35caSApple OSS Distributions struct dtrace_ptss_page* next = temp->next;
320*c54f35caSApple OSS Distributions
321*c54f35caSApple OSS Distributions // Do we need to specifically mach_vm_deallocate the user pages?
322*c54f35caSApple OSS Distributions // This can be called when the process is exiting, I believe the proc's
323*c54f35caSApple OSS Distributions // vm_map_t may already be toast.
324*c54f35caSApple OSS Distributions
325*c54f35caSApple OSS Distributions // Must be certain to free the kernel memory!
326*c54f35caSApple OSS Distributions kfree_type(struct dtrace_ptss_page, temp);
327*c54f35caSApple OSS Distributions temp = next;
328*c54f35caSApple OSS Distributions }
329*c54f35caSApple OSS Distributions }
330*c54f35caSApple OSS Distributions
331*c54f35caSApple OSS Distributions /*
332*c54f35caSApple OSS Distributions * This function is not thread safe.
333*c54f35caSApple OSS Distributions *
334*c54f35caSApple OSS Distributions * The child proc ptss fields are initialized to NULL at fork time.
335*c54f35caSApple OSS Distributions * Pages allocated in the parent are copied as part of the vm_map copy, though.
336*c54f35caSApple OSS Distributions * We need to deallocate those pages.
337*c54f35caSApple OSS Distributions *
338*c54f35caSApple OSS Distributions * Parent and child sprlock should be held, and proc_lock must NOT be held.
339*c54f35caSApple OSS Distributions */
340*c54f35caSApple OSS Distributions void
dtrace_ptss_fork(struct proc * parent,struct proc * child)341*c54f35caSApple OSS Distributions dtrace_ptss_fork(struct proc* parent, struct proc* child)
342*c54f35caSApple OSS Distributions {
343*c54f35caSApple OSS Distributions // The child should not have any pages/entries allocated at this point.
344*c54f35caSApple OSS Distributions // ASSERT(child->p_dtrace_ptss_pages == NULL);
345*c54f35caSApple OSS Distributions // ASSERT(child->p_dtrace_ptss_free_list == NULL);
346*c54f35caSApple OSS Distributions
347*c54f35caSApple OSS Distributions /*
348*c54f35caSApple OSS Distributions * The parent's sprlock should be held, to protect its pages list
349*c54f35caSApple OSS Distributions * from changing while the child references it. The child's sprlock
350*c54f35caSApple OSS Distributions * must also be held, because we are modifying its pages list.
351*c54f35caSApple OSS Distributions * Finally, to prevent a deadlock with the fasttrap cleanup code,
352*c54f35caSApple OSS Distributions * neither the parent or child proc_lock should be held.
353*c54f35caSApple OSS Distributions */
354*c54f35caSApple OSS Distributions LCK_MTX_ASSERT(&parent->p_dtrace_sprlock, LCK_MTX_ASSERT_OWNED);
355*c54f35caSApple OSS Distributions LCK_MTX_ASSERT(&parent->p_mlock, LCK_MTX_ASSERT_NOTOWNED);
356*c54f35caSApple OSS Distributions LCK_MTX_ASSERT(&child->p_dtrace_sprlock, LCK_MTX_ASSERT_OWNED);
357*c54f35caSApple OSS Distributions LCK_MTX_ASSERT(&child->p_mlock, LCK_MTX_ASSERT_NOTOWNED);
358*c54f35caSApple OSS Distributions
359*c54f35caSApple OSS Distributions // Get page list from *PARENT*
360*c54f35caSApple OSS Distributions struct dtrace_ptss_page* temp = parent->p_dtrace_ptss_pages;
361*c54f35caSApple OSS Distributions
362*c54f35caSApple OSS Distributions while (temp != NULL) {
363*c54f35caSApple OSS Distributions // Freeing the page in the *CHILD*
364*c54f35caSApple OSS Distributions dtrace_ptss_free_page(child, temp);
365*c54f35caSApple OSS Distributions
366*c54f35caSApple OSS Distributions // Do not free the kernel memory, it belong to the parent.
367*c54f35caSApple OSS Distributions temp = temp->next;
368*c54f35caSApple OSS Distributions }
369*c54f35caSApple OSS Distributions }
370