2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/types.h>
31 #include <sys/proc_internal.h>
32 #include <sys/systm.h>
34 #include <sys/dtrace_ptss.h>
36 #include <mach/vm_map.h>
37 #include <mach/vm_param.h>
38 #include <mach/mach_vm.h>
40 #include <kern/task.h>
42 #include <vm/vm_map.h>
45 * This function requires the sprlock to be held
47 * In general, it will not block. If it needs to allocate a new
48 * page of memory, the underlying kernel _MALLOC may block.
50 struct dtrace_ptss_page_entry
*
51 dtrace_ptss_claim_entry_locked(struct proc
* p
)
53 LCK_MTX_ASSERT(&p
->p_dtrace_sprlock
, LCK_MTX_ASSERT_OWNED
);
55 struct dtrace_ptss_page_entry
* entry
= NULL
;
58 struct dtrace_ptss_page_entry
* temp
= p
->p_dtrace_ptss_free_list
;
61 // Nothing on the free list. Allocate a new page, its okay if multiple threads race here.
62 struct dtrace_ptss_page
* page
= dtrace_ptss_allocate_page(p
);
64 // Make sure we actually got a page
69 // Add the page to the page list
70 page
->next
= p
->p_dtrace_ptss_pages
;
71 p
->p_dtrace_ptss_pages
= page
;
73 // CAS the entries onto the free list.
75 page
->entries
[DTRACE_PTSS_ENTRIES_PER_PAGE
- 1].next
= p
->p_dtrace_ptss_free_list
;
76 } while (!OSCompareAndSwapPtr((void *)page
->entries
[DTRACE_PTSS_ENTRIES_PER_PAGE
- 1].next
,
77 (void *)&page
->entries
[0],
78 (void * volatile *)&p
->p_dtrace_ptss_free_list
));
80 // Now that we've added to the free list, try again.
85 if (!OSCompareAndSwapPtr((void *)temp
, (void *)temp
->next
, (void * volatile *)&p
->p_dtrace_ptss_free_list
)) {
89 // At this point, we own temp.
99 * This function does not require any locks to be held on entry.
101 struct dtrace_ptss_page_entry
*
102 dtrace_ptss_claim_entry(struct proc
* p
)
104 // Verify no locks held on entry
105 LCK_MTX_ASSERT(&p
->p_dtrace_sprlock
, LCK_MTX_ASSERT_NOTOWNED
);
106 LCK_MTX_ASSERT(&p
->p_mlock
, LCK_MTX_ASSERT_NOTOWNED
);
108 struct dtrace_ptss_page_entry
* entry
= NULL
;
111 struct dtrace_ptss_page_entry
* temp
= p
->p_dtrace_ptss_free_list
;
114 lck_mtx_lock(&p
->p_dtrace_sprlock
);
115 temp
= dtrace_ptss_claim_entry_locked(p
);
116 lck_mtx_unlock(&p
->p_dtrace_sprlock
);
121 if (!OSCompareAndSwapPtr((void *)temp
, (void *)temp
->next
, (void * volatile *)&p
->p_dtrace_ptss_free_list
)) {
125 // At this point, we own temp.
135 * This function does not require any locks to be held on entry.
137 * (PR-11138709) A NULL p->p_dtrace_ptss_pages means the entry can
138 * no longer be referenced safely. When found in this state, the chore
139 * of releasing an entry to the free list is ignored.
142 dtrace_ptss_release_entry(struct proc
* p
, struct dtrace_ptss_page_entry
* e
)
144 if (p
&& p
->p_dtrace_ptss_pages
&& e
) {
146 e
->next
= p
->p_dtrace_ptss_free_list
;
147 } while (!OSCompareAndSwapPtr((void *)e
->next
, (void *)e
, (void * volatile *)&p
->p_dtrace_ptss_free_list
));
152 * This function allocates a new page in the target process's address space.
154 * It returns a dtrace_ptss_page that has its entries chained, with the last
155 * entries next field set to NULL. It does not add the page or the entries to
156 * the process's page/entry lists.
158 * This function does not require that any locks be held when it is invoked.
160 struct dtrace_ptss_page
*
161 dtrace_ptss_allocate_page(struct proc
* p
)
163 // Allocate the kernel side data
164 struct dtrace_ptss_page
* ptss_page
= _MALLOC(sizeof(struct dtrace_ptss_page
), M_TEMP
, M_ZERO
| M_WAITOK
);
165 if (ptss_page
== NULL
) {
169 // Now allocate a page in user space and set its protections to allow execute.
170 task_t task
= p
->task
;
171 vm_map_t map
= get_task_map_reference(task
);
176 mach_vm_size_t size
= PAGE_MAX_SIZE
;
177 mach_vm_offset_t addr
= 0;
178 mach_vm_offset_t write_addr
= 0;
180 * The embedded OS has extra permissions for writable and executable pages.
181 * To ensure correct permissions, we must set the page protections separately.
183 vm_prot_t cur_protection
= VM_PROT_READ
| VM_PROT_EXECUTE
;
184 vm_prot_t max_protection
= VM_PROT_READ
| VM_PROT_EXECUTE
| VM_PROT_WRITE
;
186 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 if (kr
!= KERN_SUCCESS
) {
191 * If on embedded, remap the scratch space as writable at another
194 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 if (kr
!= KERN_SUCCESS
|| !(max_protection
& VM_PROT_WRITE
)) {
199 kr
= mach_vm_protect(map
, (mach_vm_offset_t
)write_addr
, (mach_vm_size_t
)size
, 0, VM_PROT_READ
| VM_PROT_WRITE
);
200 if (kr
!= KERN_SUCCESS
) {
204 // Chain the page entries.
206 for (i
= 0; i
< DTRACE_PTSS_ENTRIES_PER_PAGE
; i
++) {
207 ptss_page
->entries
[i
].addr
= addr
+ (i
* DTRACE_PTSS_SCRATCH_SPACE_PER_THREAD
);
208 ptss_page
->entries
[i
].write_addr
= write_addr
+ (i
* DTRACE_PTSS_SCRATCH_SPACE_PER_THREAD
);
209 ptss_page
->entries
[i
].next
= &ptss_page
->entries
[i
+ 1];
212 // The last entry should point to NULL
213 ptss_page
->entries
[DTRACE_PTSS_ENTRIES_PER_PAGE
- 1].next
= NULL
;
215 vm_map_deallocate(map
);
220 _FREE(ptss_page
, M_TEMP
);
223 vm_map_deallocate(map
);
230 * This function frees an existing page in the target process's address space.
232 * It does not alter any of the process's page/entry lists.
234 * TODO: Inline in dtrace_ptrace_exec_exit?
237 dtrace_ptss_free_page(struct proc
* p
, struct dtrace_ptss_page
* ptss_page
)
239 // Grab the task and get a reference to its vm_map
240 task_t task
= p
->task
;
241 vm_map_t map
= get_task_map_reference(task
);
243 mach_vm_address_t addr
= ptss_page
->entries
[0].addr
;
244 mach_vm_size_t size
= PAGE_SIZE
; // We need some way to assert that this matches vm_map_round_page() !!!
246 // Silent failures, no point in checking return code.
247 mach_vm_deallocate(map
, addr
, size
);
249 mach_vm_address_t write_addr
= ptss_page
->entries
[0].write_addr
;
250 mach_vm_deallocate(map
, write_addr
, size
);
252 vm_map_deallocate(map
);
256 * This function assumes that the target process has been
257 * suspended, and the proc_lock & sprlock is held
260 dtrace_ptss_enable(struct proc
* p
)
262 LCK_MTX_ASSERT(&p
->p_dtrace_sprlock
, LCK_MTX_ASSERT_OWNED
);
263 LCK_MTX_ASSERT(&p
->p_mlock
, LCK_MTX_ASSERT_OWNED
);
267 * XXX There has been a concern raised about holding the proc_lock
268 * while calling dtrace_ptss_claim_entry(), due to the fact
269 * that dtrace_ptss_claim_entry() can potentially malloc.
271 TAILQ_FOREACH(uth
, &p
->p_uthlist
, uu_list
) {
272 uth
->t_dtrace_scratch
= dtrace_ptss_claim_entry_locked(p
);
277 * This function is not thread safe.
279 * It assumes the sprlock is held, and the proc_lock is not.
282 dtrace_ptss_exec_exit(struct proc
* p
)
285 * Should hold sprlock to touch the pages list. Must not
286 * hold the proc lock to avoid deadlock.
288 LCK_MTX_ASSERT(&p
->p_dtrace_sprlock
, LCK_MTX_ASSERT_OWNED
);
289 LCK_MTX_ASSERT(&p
->p_mlock
, LCK_MTX_ASSERT_NOTOWNED
);
291 p
->p_dtrace_ptss_free_list
= NULL
;
293 struct dtrace_ptss_page
* temp
= p
->p_dtrace_ptss_pages
;
294 p
->p_dtrace_ptss_pages
= NULL
;
296 while (temp
!= NULL
) {
297 struct dtrace_ptss_page
* next
= temp
->next
;
299 // Do we need to specifically mach_vm_deallocate the user pages?
300 // This can be called when the process is exiting, I believe the proc's
301 // vm_map_t may already be toast.
303 // Must be certain to free the kernel memory!
310 * This function is not thread safe. It is not used for vfork.
312 * The child proc ptss fields are initialized to NULL at fork time.
313 * Pages allocated in the parent are copied as part of the vm_map copy, though.
314 * We need to deallocate those pages.
316 * Parent and child sprlock should be held, and proc_lock must NOT be held.
319 dtrace_ptss_fork(struct proc
* parent
, struct proc
* child
)
321 // The child should not have any pages/entries allocated at this point.
322 // ASSERT(child->p_dtrace_ptss_pages == NULL);
323 // ASSERT(child->p_dtrace_ptss_free_list == NULL);
326 * The parent's sprlock should be held, to protect its pages list
327 * from changing while the child references it. The child's sprlock
328 * must also be held, because we are modifying its pages list.
329 * Finally, to prevent a deadlock with the fasttrap cleanup code,
330 * neither the parent or child proc_lock should be held.
332 LCK_MTX_ASSERT(&parent
->p_dtrace_sprlock
, LCK_MTX_ASSERT_OWNED
);
333 LCK_MTX_ASSERT(&parent
->p_mlock
, LCK_MTX_ASSERT_NOTOWNED
);
334 LCK_MTX_ASSERT(&child
->p_dtrace_sprlock
, LCK_MTX_ASSERT_OWNED
);
335 LCK_MTX_ASSERT(&child
->p_mlock
, LCK_MTX_ASSERT_NOTOWNED
);
337 // Get page list from *PARENT*
338 struct dtrace_ptss_page
* temp
= parent
->p_dtrace_ptss_pages
;
340 while (temp
!= NULL
) {
341 // Freeing the page in the *CHILD*
342 dtrace_ptss_free_page(child
, temp
);
344 // Do not free the kernel memory, it belong to the parent.