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