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