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