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