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