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