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