2 * Copyright (c) 2005-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@
31 * APPLE NOTE: This file is compiled even if dtrace is unconfig'd. A symbol
32 * from this file (_dtrace_register_anon_DOF) always needs to be exported for
33 * an external kext to link against.
38 #define MACH__POSIX_C_SOURCE_PRIVATE 1 /* pulls in suitable savearea from mach/ppc/thread_status.h */
39 #include <kern/thread.h>
40 #include <mach/thread_status.h>
44 #include <sys/malloc.h>
47 #include <sys/proc_internal.h>
48 #include <sys/kauth.h>
50 #include <sys/systm.h>
51 #include <sys/dtrace.h>
52 #include <sys/dtrace_impl.h>
53 #include <libkern/OSAtomic.h>
54 #include <libkern/OSKextLibPrivate.h>
55 #include <kern/kern_types.h>
56 #include <kern/timer_call.h>
57 #include <kern/thread_call.h>
58 #include <kern/task.h>
59 #include <kern/sched_prim.h>
60 #include <kern/queue.h>
61 #include <miscfs/devfs/devfs.h>
62 #include <kern/kalloc.h>
64 #include <mach/vm_param.h>
65 #include <mach/mach_vm.h>
66 #include <mach/task.h>
68 #include <vm/vm_map.h> /* All the bits we care about are guarded by MACH_KERNEL_PRIVATE :-( */
73 /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
74 #define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
77 dtrace_sprlock(proc_t
*p
)
79 lck_mtx_assert(&p
->p_mlock
, LCK_MTX_ASSERT_NOTOWNED
);
80 lck_mtx_lock(&p
->p_dtrace_sprlock
);
84 dtrace_sprunlock(proc_t
*p
)
86 lck_mtx_unlock(&p
->p_dtrace_sprlock
);
90 /* Not called from probe context */
96 if ((p
= proc_find(pid
)) == PROC_NULL
) {
100 task_suspend_internal(p
->task
);
109 /* Not called from probe context */
113 if (p
!= PROC_NULL
) {
118 task_resume_internal(p
->task
);
128 // These are not exported from vm_map.h.
129 extern kern_return_t
vm_map_read_user(vm_map_t map
, vm_map_address_t src_addr
, void *dst_p
, vm_size_t size
);
130 extern kern_return_t
vm_map_write_user(vm_map_t map
, void *src_p
, vm_map_address_t dst_addr
, vm_size_t size
);
132 /* Not called from probe context */
134 uread(proc_t
*p
, void *buf
, user_size_t len
, user_addr_t a
)
138 ASSERT(p
!= PROC_NULL
);
139 ASSERT(p
->task
!= NULL
);
141 task_t task
= p
->task
;
144 * Grab a reference to the task vm_map_t to make sure
145 * the map isn't pulled out from under us.
147 * Because the proc_lock is not held at all times on all code
148 * paths leading here, it is possible for the proc to have
149 * exited. If the map is null, fail.
151 vm_map_t map
= get_task_map_reference(task
);
153 ret
= vm_map_read_user( map
, (vm_map_address_t
)a
, buf
, (vm_size_t
)len
);
154 vm_map_deallocate(map
);
156 ret
= KERN_TERMINATED
;
162 /* Not called from probe context */
164 uwrite(proc_t
*p
, void *buf
, user_size_t len
, user_addr_t a
)
169 ASSERT(p
->task
!= NULL
);
171 task_t task
= p
->task
;
174 * Grab a reference to the task vm_map_t to make sure
175 * the map isn't pulled out from under us.
177 * Because the proc_lock is not held at all times on all code
178 * paths leading here, it is possible for the proc to have
179 * exited. If the map is null, fail.
181 vm_map_t map
= get_task_map_reference(task
);
183 /* Find the memory permissions. */
184 uint32_t nestingDepth
=999999;
185 vm_region_submap_short_info_data_64_t info
;
186 mach_msg_type_number_t count
= VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
;
187 mach_vm_address_t address
= (mach_vm_address_t
)a
;
188 mach_vm_size_t sizeOfRegion
= (mach_vm_size_t
)len
;
190 ret
= mach_vm_region_recurse(map
, &address
, &sizeOfRegion
, &nestingDepth
, (vm_region_recurse_info_t
)&info
, &count
);
191 if (ret
!= KERN_SUCCESS
)
196 if (!(info
.protection
& VM_PROT_WRITE
)) {
197 /* Save the original protection values for restoration later */
198 reprotect
= info
.protection
;
200 if (info
.max_protection
& VM_PROT_WRITE
) {
201 /* The memory is not currently writable, but can be made writable. */
202 ret
= mach_vm_protect (map
, (mach_vm_offset_t
)a
, (mach_vm_size_t
)len
, 0, (reprotect
& ~VM_PROT_EXECUTE
) | VM_PROT_WRITE
);
205 * The memory is not currently writable, and cannot be made writable. We need to COW this memory.
207 * Strange, we can't just say "reprotect | VM_PROT_COPY", that fails.
209 ret
= mach_vm_protect (map
, (mach_vm_offset_t
)a
, (mach_vm_size_t
)len
, 0, VM_PROT_COPY
| VM_PROT_READ
| VM_PROT_WRITE
);
212 if (ret
!= KERN_SUCCESS
)
216 /* The memory was already writable. */
217 reprotect
= VM_PROT_NONE
;
220 ret
= vm_map_write_user( map
,
225 dtrace_flush_caches();
227 if (ret
!= KERN_SUCCESS
)
230 if (reprotect
!= VM_PROT_NONE
) {
231 ASSERT(reprotect
& VM_PROT_EXECUTE
);
232 ret
= mach_vm_protect (map
, (mach_vm_offset_t
)a
, (mach_vm_size_t
)len
, 0, reprotect
);
236 vm_map_deallocate(map
);
238 ret
= KERN_TERMINATED
;
250 dtrace_cpu_t
*cpu_list
;
251 cpu_core_t
*cpu_core
; /* XXX TLB lockdown? */
258 * dtrace_CRED() can be called from probe context. We cannot simply call kauth_cred_get() since
259 * that function may try to resolve a lazy credential binding, which entails taking the proc_lock.
264 struct uthread
*uthread
= get_bsdthread_info(current_thread());
269 return uthread
->uu_ucred
; /* May return NOCRED which is defined to be 0 */
272 #define HAS_ALLPRIVS(cr) priv_isfullset(&CR_OEPRIV(cr))
273 #define HAS_PRIVILEGE(cr, pr) ((pr) == PRIV_ALL ? \
275 PRIV_ISASSERT(&CR_OEPRIV(cr), pr))
277 int PRIV_POLICY_CHOICE(void* cred
, int priv
, int all
)
279 #pragma unused(priv, all)
280 return kauth_cred_issuser(cred
); /* XXX TODO: How is this different from PRIV_POLICY_ONLY? */
284 PRIV_POLICY_ONLY(void *cr
, int priv
, int boolean
)
286 #pragma unused(priv, boolean)
287 return kauth_cred_issuser(cr
); /* XXX TODO: HAS_PRIVILEGE(cr, priv); */
291 crgetuid(const cred_t
*cr
) { cred_t copy_cr
= *cr
; return kauth_cred_getuid(©_cr
); }
297 typedef struct wrap_timer_call
{
298 /* node attributes */
304 struct timer_call call
;
306 /* next item in the linked list */
307 LIST_ENTRY(wrap_timer_call
) entries
;
310 #define WAKEUP_REAPER 0x7FFFFFFFFFFFFFFFLL
311 #define NEARLY_FOREVER 0x7FFFFFFFFFFFFFFELL
314 typedef struct cyc_list
{
315 cyc_omni_handler_t cyl_omni
;
316 wrap_timer_call_t cyl_wrap_by_cpus
[];
317 #if __arm__ && (__BIGGEST_ALIGNMENT__ > 4)
318 } __attribute__ ((aligned (8))) cyc_list_t
;
323 /* CPU going online/offline notifications */
324 void (*dtrace_cpu_state_changed_hook
)(int, boolean_t
) = NULL
;
325 void dtrace_cpu_state_changed(int, boolean_t
);
328 dtrace_install_cpu_hooks(void) {
329 dtrace_cpu_state_changed_hook
= dtrace_cpu_state_changed
;
333 dtrace_cpu_state_changed(int cpuid
, boolean_t is_running
) {
334 #pragma unused(cpuid)
335 wrap_timer_call_t
*wrapTC
= NULL
;
336 boolean_t suspend
= (is_running
? FALSE
: TRUE
);
339 /* Ensure that we're not going to leave the CPU */
340 s
= dtrace_interrupt_disable();
341 assert(cpuid
== cpu_number());
343 LIST_FOREACH(wrapTC
, &(cpu_list
[cpu_number()].cpu_cyc_list
), entries
) {
344 assert(wrapTC
->cpuid
== cpu_number());
346 assert(!wrapTC
->suspended
);
347 /* If this fails, we'll panic anyway, so let's do this now. */
348 if (!timer_call_cancel(&wrapTC
->call
))
349 panic("timer_call_set_suspend() failed to cancel a timer call");
350 wrapTC
->suspended
= TRUE
;
352 /* Rearm the timer, but ensure it was suspended first. */
353 assert(wrapTC
->suspended
);
354 clock_deadline_for_periodic_event(wrapTC
->when
.cyt_interval
, mach_absolute_time(),
356 timer_call_enter1(&wrapTC
->call
, (void*) wrapTC
, wrapTC
->deadline
,
357 TIMER_CALL_SYS_CRITICAL
| TIMER_CALL_LOCAL
);
358 wrapTC
->suspended
= FALSE
;
363 /* Restore the previous interrupt state. */
364 dtrace_interrupt_enable(s
);
368 _timer_call_apply_cyclic( void *ignore
, void *vTChdl
)
370 #pragma unused(ignore)
371 wrap_timer_call_t
*wrapTC
= (wrap_timer_call_t
*)vTChdl
;
373 (*(wrapTC
->hdlr
.cyh_func
))( wrapTC
->hdlr
.cyh_arg
);
375 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, mach_absolute_time(), &(wrapTC
->deadline
) );
376 timer_call_enter1( &(wrapTC
->call
), (void *)wrapTC
, wrapTC
->deadline
, TIMER_CALL_SYS_CRITICAL
| TIMER_CALL_LOCAL
);
380 timer_call_add_cyclic(wrap_timer_call_t
*wrapTC
, cyc_handler_t
*handler
, cyc_time_t
*when
)
385 timer_call_setup( &(wrapTC
->call
), _timer_call_apply_cyclic
, NULL
);
386 wrapTC
->hdlr
= *handler
;
387 wrapTC
->when
= *when
;
389 nanoseconds_to_absolutetime( wrapTC
->when
.cyt_interval
, (uint64_t *)&wrapTC
->when
.cyt_interval
);
391 now
= mach_absolute_time();
392 wrapTC
->deadline
= now
;
394 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, now
, &(wrapTC
->deadline
) );
396 /* Insert the timer to the list of the running timers on this CPU, and start it. */
397 s
= dtrace_interrupt_disable();
398 wrapTC
->cpuid
= cpu_number();
399 LIST_INSERT_HEAD(&cpu_list
[wrapTC
->cpuid
].cpu_cyc_list
, wrapTC
, entries
);
400 timer_call_enter1(&wrapTC
->call
, (void*) wrapTC
, wrapTC
->deadline
,
401 TIMER_CALL_SYS_CRITICAL
| TIMER_CALL_LOCAL
);
402 wrapTC
->suspended
= FALSE
;
403 dtrace_interrupt_enable(s
);
405 return (cyclic_id_t
)wrapTC
;
409 * Executed on the CPU the timer is running on.
412 timer_call_remove_cyclic(wrap_timer_call_t
*wrapTC
)
415 assert(cpu_number() == wrapTC
->cpuid
);
417 if (!timer_call_cancel(&wrapTC
->call
))
418 panic("timer_call_remove_cyclic() failed to cancel a timer call");
420 LIST_REMOVE(wrapTC
, entries
);
424 timer_call_get_cyclic_arg(wrap_timer_call_t
*wrapTC
)
426 return (wrapTC
? wrapTC
->hdlr
.cyh_arg
: NULL
);
430 cyclic_timer_add(cyc_handler_t
*handler
, cyc_time_t
*when
)
432 wrap_timer_call_t
*wrapTC
= _MALLOC(sizeof(wrap_timer_call_t
), M_TEMP
, M_ZERO
| M_WAITOK
);
436 return timer_call_add_cyclic( wrapTC
, handler
, when
);
440 cyclic_timer_remove(cyclic_id_t cyclic
)
442 ASSERT( cyclic
!= CYCLIC_NONE
);
444 /* Removing a timer call must be done on the CPU the timer is running on. */
445 wrap_timer_call_t
*wrapTC
= (wrap_timer_call_t
*) cyclic
;
446 dtrace_xcall(wrapTC
->cpuid
, (dtrace_xcall_t
) timer_call_remove_cyclic
, (void*) cyclic
);
448 _FREE((void *)cyclic
, M_TEMP
);
452 _cyclic_add_omni(cyc_list_t
*cyc_list
)
456 cyc_omni_handler_t
*omni
= &cyc_list
->cyl_omni
;
458 (omni
->cyo_online
)(omni
->cyo_arg
, CPU
, &cH
, &cT
);
460 wrap_timer_call_t
*wrapTC
= &cyc_list
->cyl_wrap_by_cpus
[cpu_number()];
461 timer_call_add_cyclic(wrapTC
, &cH
, &cT
);
465 cyclic_add_omni(cyc_omni_handler_t
*omni
)
467 cyc_list_t
*cyc_list
=
468 _MALLOC(sizeof(cyc_list_t
) + NCPU
* sizeof(wrap_timer_call_t
), M_TEMP
, M_ZERO
| M_WAITOK
);
470 if (NULL
== cyc_list
)
473 cyc_list
->cyl_omni
= *omni
;
475 dtrace_xcall(DTRACE_CPUALL
, (dtrace_xcall_t
)_cyclic_add_omni
, (void *)cyc_list
);
477 return (cyclic_id_list_t
)cyc_list
;
481 _cyclic_remove_omni(cyc_list_t
*cyc_list
)
483 cyc_omni_handler_t
*omni
= &cyc_list
->cyl_omni
;
485 wrap_timer_call_t
*wrapTC
;
488 * If the processor was offline when dtrace started, we did not allocate
489 * a cyclic timer for this CPU.
491 if ((wrapTC
= &cyc_list
->cyl_wrap_by_cpus
[cpu_number()]) != NULL
) {
492 oarg
= timer_call_get_cyclic_arg(wrapTC
);
493 timer_call_remove_cyclic(wrapTC
);
494 (omni
->cyo_offline
)(omni
->cyo_arg
, CPU
, oarg
);
499 cyclic_remove_omni(cyclic_id_list_t cyc_list
)
501 ASSERT(cyc_list
!= NULL
);
503 dtrace_xcall(DTRACE_CPUALL
, (dtrace_xcall_t
)_cyclic_remove_omni
, (void *)cyc_list
);
504 _FREE(cyc_list
, M_TEMP
);
507 typedef struct wrap_thread_call
{
512 } wrap_thread_call_t
;
515 * _cyclic_apply will run on some thread under kernel_task. That's OK for the
516 * cleaner and the deadman, but too distant in time and place for the profile provider.
519 _cyclic_apply( void *ignore
, void *vTChdl
)
521 #pragma unused(ignore)
522 wrap_thread_call_t
*wrapTC
= (wrap_thread_call_t
*)vTChdl
;
524 (*(wrapTC
->hdlr
.cyh_func
))( wrapTC
->hdlr
.cyh_arg
);
526 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, mach_absolute_time(), &(wrapTC
->deadline
) );
527 (void)thread_call_enter1_delayed( wrapTC
->TChdl
, (void *)wrapTC
, wrapTC
->deadline
);
529 /* Did cyclic_remove request a wakeup call when this thread call was re-armed? */
530 if (wrapTC
->when
.cyt_interval
== WAKEUP_REAPER
)
531 thread_wakeup((event_t
)wrapTC
);
535 cyclic_add(cyc_handler_t
*handler
, cyc_time_t
*when
)
539 wrap_thread_call_t
*wrapTC
= _MALLOC(sizeof(wrap_thread_call_t
), M_TEMP
, M_ZERO
| M_WAITOK
);
543 wrapTC
->TChdl
= thread_call_allocate( _cyclic_apply
, NULL
);
544 wrapTC
->hdlr
= *handler
;
545 wrapTC
->when
= *when
;
547 ASSERT(when
->cyt_when
== 0);
548 ASSERT(when
->cyt_interval
< WAKEUP_REAPER
);
550 nanoseconds_to_absolutetime(wrapTC
->when
.cyt_interval
, (uint64_t *)&wrapTC
->when
.cyt_interval
);
552 now
= mach_absolute_time();
553 wrapTC
->deadline
= now
;
555 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, now
, &(wrapTC
->deadline
) );
556 (void)thread_call_enter1_delayed( wrapTC
->TChdl
, (void *)wrapTC
, wrapTC
->deadline
);
558 return (cyclic_id_t
)wrapTC
;
562 noop_cyh_func(void * ignore
)
564 #pragma unused(ignore)
568 cyclic_remove(cyclic_id_t cyclic
)
570 wrap_thread_call_t
*wrapTC
= (wrap_thread_call_t
*)cyclic
;
572 ASSERT(cyclic
!= CYCLIC_NONE
);
574 while (!thread_call_cancel(wrapTC
->TChdl
)) {
575 int ret
= assert_wait(wrapTC
, THREAD_UNINT
);
576 ASSERT(ret
== THREAD_WAITING
);
578 wrapTC
->when
.cyt_interval
= WAKEUP_REAPER
;
580 ret
= thread_block(THREAD_CONTINUE_NULL
);
581 ASSERT(ret
== THREAD_AWAKENED
);
584 if (thread_call_free(wrapTC
->TChdl
))
585 _FREE(wrapTC
, M_TEMP
);
587 /* Gut this cyclic and move on ... */
588 wrapTC
->hdlr
.cyh_func
= noop_cyh_func
;
589 wrapTC
->when
.cyt_interval
= NEARLY_FOREVER
;
593 kern_return_t
_dtrace_register_anon_DOF(char *, uchar_t
*, uint_t
);
596 _dtrace_register_anon_DOF(char *name
, uchar_t
*data
, uint_t nelements
)
598 #pragma unused(name, data, nelements)
603 ddi_driver_major(dev_info_t
*devi
) { return (int)major(CAST_DOWN_EXPLICIT(int,devi
)); }
606 ddi_create_minor_node(dev_info_t
*dip
, const char *name
, int spec_type
,
607 minor_t minor_num
, const char *node_type
, int flag
)
609 #pragma unused(spec_type,node_type,flag)
610 dev_t dev
= makedev( ddi_driver_major(dip
), minor_num
);
612 if (NULL
== devfs_make_node( dev
, DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666, name
, 0 ))
619 ddi_remove_minor_node(dev_info_t
*dip
, char *name
)
621 #pragma unused(dip,name)
622 /* XXX called from dtrace_detach, so NOTREACHED for now. */
628 return (major_t
) major(d
);
634 return (minor_t
) minor(d
);
637 extern void Debugger(const char*);
640 debug_enter(char *c
) { Debugger(c
); }
647 dt_kmem_alloc_site(size_t size
, int kmflag
, vm_allocation_site_t
*site
)
649 #pragma unused(kmflag)
652 * We ignore the M_NOWAIT bit in kmflag (all of kmflag, in fact).
653 * Requests larger than 8K with M_NOWAIT fail in kalloc_canblock.
655 vm_size_t vsize
= size
;
656 return kalloc_canblock(&vsize
, TRUE
, site
);
660 dt_kmem_zalloc_site(size_t size
, int kmflag
, vm_allocation_site_t
*site
)
662 #pragma unused(kmflag)
665 * We ignore the M_NOWAIT bit in kmflag (all of kmflag, in fact).
666 * Requests larger than 8K with M_NOWAIT fail in kalloc_canblock.
668 vm_size_t vsize
= size
;
669 void* buf
= kalloc_canblock(&vsize
, TRUE
, site
);
680 dt_kmem_free(void *buf
, size_t size
)
684 * DTrace relies on this, its doing a lot of NULL frees.
685 * A null free causes the debug builds to panic.
687 if (buf
== NULL
) return;
697 * aligned dt_kmem allocator
698 * align should be a power of two
702 dt_kmem_alloc_aligned_site(size_t size
, size_t align
, int kmflag
, vm_allocation_site_t
*site
)
704 void *mem
, **addr_to_free
;
705 intptr_t mem_aligned
;
706 size_t *size_to_free
, hdr_size
;
708 /* Must be a power of two. */
710 assert((align
& (align
- 1)) == 0);
713 * We are going to add a header to the allocation. It contains
714 * the address to free and the total size of the buffer.
716 hdr_size
= sizeof(size_t) + sizeof(void*);
717 mem
= dt_kmem_alloc_site(size
+ align
+ hdr_size
, kmflag
, site
);
721 mem_aligned
= (intptr_t) (((intptr_t) mem
+ align
+ hdr_size
) & ~(align
- 1));
723 /* Write the address to free in the header. */
724 addr_to_free
= (void**) (mem_aligned
- sizeof(void*));
727 /* Write the size to free in the header. */
728 size_to_free
= (size_t*) (mem_aligned
- hdr_size
);
729 *size_to_free
= size
+ align
+ hdr_size
;
731 return (void*) mem_aligned
;
735 dt_kmem_zalloc_aligned_site(size_t size
, size_t align
, int kmflag
, vm_allocation_site_t
*s
)
739 buf
= dt_kmem_alloc_aligned_site(size
, align
, kmflag
, s
);
750 dt_kmem_free_aligned(void* buf
, size_t size
)
753 intptr_t ptr
= (intptr_t) buf
;
754 void **addr_to_free
= (void**) (ptr
- sizeof(void*));
755 size_t *size_to_free
= (size_t*) (ptr
- (sizeof(size_t) + sizeof(void*)));
760 dt_kmem_free(*addr_to_free
, *size_to_free
);
764 * dtrace wants to manage just a single block: dtrace_state_percpu_t * NCPU, and
765 * doesn't specify constructor, destructor, or reclaim methods.
766 * At present, it always zeroes the block it obtains from kmem_cache_alloc().
767 * We'll manage this constricted use of kmem_cache with ordinary _MALLOC and _FREE.
771 const char *name
, /* descriptive name for this cache */
772 size_t bufsize
, /* size of the objects it manages */
773 size_t align
, /* required object alignment */
774 int (*constructor
)(void *, void *, int), /* object constructor */
775 void (*destructor
)(void *, void *), /* object destructor */
776 void (*reclaim
)(void *), /* memory reclaim callback */
777 void *private, /* pass-thru arg for constr/destr/reclaim */
778 vmem_t
*vmp
, /* vmem source for slab allocation */
779 int cflags
) /* cache creation flags */
781 #pragma unused(name,align,constructor,destructor,reclaim,private,vmp,cflags)
782 return (kmem_cache_t
*)bufsize
; /* A cookie that tracks the single object size. */
786 kmem_cache_alloc(kmem_cache_t
*cp
, int kmflag
)
788 #pragma unused(kmflag)
789 size_t bufsize
= (size_t)cp
;
790 return (void *)_MALLOC(bufsize
, M_TEMP
, M_WAITOK
);
794 kmem_cache_free(kmem_cache_t
*cp
, void *buf
)
801 kmem_cache_destroy(kmem_cache_t
*cp
)
807 * vmem (Solaris "slab" allocator) used by DTrace solely to hand out resource ids
809 typedef unsigned int u_daddr_t
;
812 /* By passing around blist *handles*, the underlying blist can be resized as needed. */
818 vmem_create(const char *name
, void *base
, size_t size
, size_t quantum
, void *ignore5
,
819 void *ignore6
, vmem_t
*source
, size_t qcache_max
, int vmflag
)
821 #pragma unused(name,quantum,ignore5,ignore6,source,qcache_max,vmflag)
823 struct blist_hdl
*p
= _MALLOC(sizeof(struct blist_hdl
), M_TEMP
, M_WAITOK
);
825 ASSERT(quantum
== 1);
826 ASSERT(NULL
== ignore5
);
827 ASSERT(NULL
== ignore6
);
828 ASSERT(NULL
== source
);
829 ASSERT(0 == qcache_max
);
830 ASSERT(vmflag
& VMC_IDENTIFIER
);
832 size
= MIN(128, size
); /* Clamp to 128 initially, since the underlying data structure is pre-allocated */
834 p
->blist
= bl
= blist_create( size
);
835 blist_free(bl
, 0, size
);
836 if (base
) blist_alloc( bl
, (daddr_t
)(uintptr_t)base
); /* Chomp off initial ID(s) */
842 vmem_alloc(vmem_t
*vmp
, size_t size
, int vmflag
)
844 #pragma unused(vmflag)
845 struct blist_hdl
*q
= (struct blist_hdl
*)vmp
;
846 blist_t bl
= q
->blist
;
849 p
= blist_alloc(bl
, (daddr_t
)size
);
851 if ((daddr_t
)-1 == p
) {
852 blist_resize(&bl
, (bl
->bl_blocks
) << 1, 1);
854 p
= blist_alloc(bl
, (daddr_t
)size
);
855 if ((daddr_t
)-1 == p
)
856 panic("vmem_alloc: failure after blist_resize!");
859 return (void *)(uintptr_t)p
;
863 vmem_free(vmem_t
*vmp
, void *vaddr
, size_t size
)
865 struct blist_hdl
*p
= (struct blist_hdl
*)vmp
;
867 blist_free( p
->blist
, (daddr_t
)(uintptr_t)vaddr
, (daddr_t
)size
);
871 vmem_destroy(vmem_t
*vmp
)
873 struct blist_hdl
*p
= (struct blist_hdl
*)vmp
;
875 blist_destroy( p
->blist
);
876 _FREE( p
, sizeof(struct blist_hdl
) );
884 * dtrace_gethrestime() provides the "walltimestamp", a value that is anchored at
885 * January 1, 1970. Because it can be called from probe context, it must take no locks.
889 dtrace_gethrestime(void)
892 clock_nsec_t nanosecs
;
893 uint64_t secs64
, ns64
;
895 clock_get_calendar_nanotime_nowait(&secs
, &nanosecs
);
896 secs64
= (uint64_t)secs
;
897 ns64
= (uint64_t)nanosecs
;
899 ns64
= ns64
+ (secs64
* 1000000000LL);
904 * dtrace_gethrtime() provides high-resolution timestamps with machine-dependent origin.
905 * Hence its primary use is to specify intervals.
909 dtrace_abs_to_nano(uint64_t elapsed
)
911 static mach_timebase_info_data_t sTimebaseInfo
= { 0, 0 };
914 * If this is the first time we've run, get the timebase.
915 * We can use denom == 0 to indicate that sTimebaseInfo is
916 * uninitialised because it makes no sense to have a zero
917 * denominator in a fraction.
920 if ( sTimebaseInfo
.denom
== 0 ) {
921 (void) clock_timebase_info(&sTimebaseInfo
);
925 * Convert to nanoseconds.
926 * return (elapsed * (uint64_t)sTimebaseInfo.numer)/(uint64_t)sTimebaseInfo.denom;
928 * Provided the final result is representable in 64 bits the following maneuver will
929 * deliver that result without intermediate overflow.
931 if (sTimebaseInfo
.denom
== sTimebaseInfo
.numer
)
933 else if (sTimebaseInfo
.denom
== 1)
934 return elapsed
* (uint64_t)sTimebaseInfo
.numer
;
936 /* Decompose elapsed = eta32 * 2^32 + eps32: */
937 uint64_t eta32
= elapsed
>> 32;
938 uint64_t eps32
= elapsed
& 0x00000000ffffffffLL
;
940 uint32_t numer
= sTimebaseInfo
.numer
, denom
= sTimebaseInfo
.denom
;
942 /* Form product of elapsed64 (decomposed) and numer: */
943 uint64_t mu64
= numer
* eta32
;
944 uint64_t lambda64
= numer
* eps32
;
946 /* Divide the constituents by denom: */
947 uint64_t q32
= mu64
/denom
;
948 uint64_t r32
= mu64
- (q32
* denom
); /* mu64 % denom */
950 return (q32
<< 32) + ((r32
<< 32) + lambda64
)/denom
;
955 dtrace_gethrtime(void)
957 static uint64_t start
= 0;
960 start
= mach_absolute_time();
962 return dtrace_abs_to_nano(mach_absolute_time() - start
);
966 * Atomicity and synchronization
969 dtrace_cas32(uint32_t *target
, uint32_t cmp
, uint32_t new)
971 if (OSCompareAndSwap( (UInt32
)cmp
, (UInt32
)new, (volatile UInt32
*)target
))
974 return ~cmp
; /* Must return something *other* than cmp */
978 dtrace_casptr(void *target
, void *cmp
, void *new)
980 if (OSCompareAndSwapPtr( cmp
, new, (void**)target
))
983 return (void *)(~(uintptr_t)cmp
); /* Must return something *other* than cmp */
987 * Interrupt manipulation
990 dtrace_interrupt_disable(void)
992 return (dtrace_icookie_t
)ml_set_interrupts_enabled(FALSE
);
996 dtrace_interrupt_enable(dtrace_icookie_t reenable
)
998 (void)ml_set_interrupts_enabled((boolean_t
)reenable
);
1005 dtrace_sync_func(void) {}
1008 * dtrace_sync() is not called from probe context.
1013 dtrace_xcall(DTRACE_CPUALL
, (dtrace_xcall_t
)dtrace_sync_func
, NULL
);
1017 * The dtrace_copyin/out/instr and dtrace_fuword* routines can be called from probe context.
1020 extern kern_return_t
dtrace_copyio_preflight(addr64_t
);
1021 extern kern_return_t
dtrace_copyio_postflight(addr64_t
);
1024 dtrace_copycheck(user_addr_t uaddr
, uintptr_t kaddr
, size_t size
)
1026 #pragma unused(kaddr)
1028 vm_offset_t recover
= dtrace_set_thread_recover( current_thread(), 0 ); /* Snare any extant recovery point. */
1029 dtrace_set_thread_recover( current_thread(), recover
); /* Put it back. We *must not* re-enter and overwrite. */
1031 ASSERT(kaddr
+ size
>= kaddr
);
1033 if ( uaddr
+ size
< uaddr
|| /* Avoid address wrap. */
1034 KERN_FAILURE
== dtrace_copyio_preflight(uaddr
)) /* Machine specific setup/constraints. */
1036 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1037 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1044 dtrace_copyin(user_addr_t src
, uintptr_t dst
, size_t len
, volatile uint16_t *flags
)
1046 #pragma unused(flags)
1048 if (dtrace_copycheck( src
, dst
, len
)) {
1049 if (copyin((const user_addr_t
)src
, (char *)dst
, (vm_size_t
)len
)) {
1050 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1051 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= src
;
1053 dtrace_copyio_postflight(src
);
1058 dtrace_copyinstr(user_addr_t src
, uintptr_t dst
, size_t len
, volatile uint16_t *flags
)
1060 #pragma unused(flags)
1064 if (dtrace_copycheck( src
, dst
, len
)) {
1065 /* copyin as many as 'len' bytes. */
1066 int error
= copyinstr((const user_addr_t
)src
, (char *)dst
, (vm_size_t
)len
, &actual
);
1069 * ENAMETOOLONG is returned when 'len' bytes have been copied in but the NUL terminator was
1070 * not encountered. That does not require raising CPU_DTRACE_BADADDR, and we press on.
1071 * Note that we do *not* stuff a NUL terminator when returning ENAMETOOLONG, that's left
1074 if (error
&& error
!= ENAMETOOLONG
) {
1075 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1076 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= src
;
1078 dtrace_copyio_postflight(src
);
1083 dtrace_copyout(uintptr_t src
, user_addr_t dst
, size_t len
, volatile uint16_t *flags
)
1085 #pragma unused(flags)
1087 if (dtrace_copycheck( dst
, src
, len
)) {
1088 if (copyout((const void *)src
, dst
, (vm_size_t
)len
)) {
1089 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1090 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= dst
;
1092 dtrace_copyio_postflight(dst
);
1097 dtrace_copyoutstr(uintptr_t src
, user_addr_t dst
, size_t len
, volatile uint16_t *flags
)
1099 #pragma unused(flags)
1103 if (dtrace_copycheck( dst
, src
, len
)) {
1106 * ENAMETOOLONG is returned when 'len' bytes have been copied out but the NUL terminator was
1107 * not encountered. We raise CPU_DTRACE_BADADDR in that case.
1108 * Note that we do *not* stuff a NUL terminator when returning ENAMETOOLONG, that's left
1111 if (copyoutstr((const void *)src
, dst
, (size_t)len
, &actual
)) {
1112 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1113 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= dst
;
1115 dtrace_copyio_postflight(dst
);
1119 extern const int copysize_limit_panic
;
1121 int dtrace_copy_maxsize(void)
1123 return copysize_limit_panic
;
1128 dtrace_buffer_copyout(const void *kaddr
, user_addr_t uaddr
, vm_size_t nbytes
)
1130 int maxsize
= dtrace_copy_maxsize();
1132 * Partition the copyout in copysize_limit_panic-sized chunks
1134 while (nbytes
>= (vm_size_t
)maxsize
) {
1135 if (copyout(kaddr
, uaddr
, maxsize
) != 0)
1143 if (copyout(kaddr
, uaddr
, nbytes
) != 0)
1151 dtrace_fuword8(user_addr_t uaddr
)
1155 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1156 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1157 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1158 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1159 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1161 dtrace_copyio_postflight(uaddr
);
1163 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1169 dtrace_fuword16(user_addr_t uaddr
)
1173 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1174 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1175 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1176 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1177 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1179 dtrace_copyio_postflight(uaddr
);
1181 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1187 dtrace_fuword32(user_addr_t uaddr
)
1191 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1192 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1193 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1194 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1195 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1197 dtrace_copyio_postflight(uaddr
);
1199 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1205 dtrace_fuword64(user_addr_t uaddr
)
1209 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1210 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1211 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1212 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1213 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1215 dtrace_copyio_postflight(uaddr
);
1217 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1223 * Emulation of Solaris fuword / suword
1224 * Called from the fasttrap provider, so the use of copyin/out requires fewer safegaurds.
1228 fuword8(user_addr_t uaddr
, uint8_t *value
)
1230 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint8_t)) != 0) {
1238 fuword16(user_addr_t uaddr
, uint16_t *value
)
1240 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint16_t)) != 0) {
1248 fuword32(user_addr_t uaddr
, uint32_t *value
)
1250 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint32_t)) != 0) {
1258 fuword64(user_addr_t uaddr
, uint64_t *value
)
1260 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint64_t)) != 0) {
1268 fuword32_noerr(user_addr_t uaddr
, uint32_t *value
)
1270 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint32_t))) {
1276 fuword64_noerr(user_addr_t uaddr
, uint64_t *value
)
1278 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint64_t))) {
1284 suword64(user_addr_t addr
, uint64_t value
)
1286 if (copyout((const void *)&value
, addr
, sizeof(value
)) != 0) {
1294 suword32(user_addr_t addr
, uint32_t value
)
1296 if (copyout((const void *)&value
, addr
, sizeof(value
)) != 0) {
1306 extern boolean_t
dtrace_tally_fault(user_addr_t
);
1309 dtrace_tally_fault(user_addr_t uaddr
)
1311 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1312 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1313 return( DTRACE_CPUFLAG_ISSET(CPU_DTRACE_NOFAULT
) ? TRUE
: FALSE
);
1317 extern int prf(const char *, va_list, int, struct tty
*); /* bsd/kern/subr_prf.h */
1320 vuprintf(const char *format
, va_list ap
)
1322 return prf(format
, ap
, TOTTY
, NULL
);
1325 /* Not called from probe context */
1326 void cmn_err( int level
, const char *format
, ... )
1328 #pragma unused(level)
1331 va_start(alist
, format
);
1332 vuprintf(format
, alist
);
1339 * 2002-01-24 gvdl Initial implementation of strstr
1342 __private_extern__
const char *
1343 strstr(const char *in
, const char *str
)
1352 return (const char *) in
; // Trivial empty string case
1363 } while (strncmp(in
, str
, len
) != 0);
1365 return (const char *) (in
- 1);
1369 bsearch(const void *key
, const void *base0
, size_t nmemb
, size_t size
, int (*compar
)(const void *, const void *))
1371 const char *base
= base0
;
1375 for (lim
= nmemb
; lim
!= 0; lim
>>= 1) {
1376 p
= base
+ (lim
>> 1) * size
;
1377 cmp
= (*compar
)(key
, p
);
1380 if (cmp
> 0) { /* key > p: move right */
1381 base
= (const char *)p
+ size
;
1383 } /* else move left */
1392 dtrace_caller(int ignore
)
1394 #pragma unused(ignore)
1395 return -1; /* Just as in Solaris dtrace_asm.s */
1399 dtrace_getstackdepth(int aframes
)
1401 struct frame
*fp
= (struct frame
*)__builtin_frame_address(0);
1402 struct frame
*nextfp
, *minfp
, *stacktop
;
1406 if ((on_intr
= CPU_ON_INTR(CPU
)) != 0)
1407 stacktop
= (struct frame
*)dtrace_get_cpu_int_stack_top();
1409 stacktop
= (struct frame
*)(dtrace_get_kernel_stack(current_thread()) + kernel_stack_size
);
1418 nextfp
= *(struct frame
**)fp
;
1420 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
1423 * Hop from interrupt stack to thread stack.
1425 vm_offset_t kstack_base
= dtrace_get_kernel_stack(current_thread());
1427 minfp
= (struct frame
*)kstack_base
;
1428 stacktop
= (struct frame
*)(kstack_base
+ kernel_stack_size
);
1440 if (depth
<= aframes
)
1443 return (depth
- aframes
);
1447 dtrace_addr_in_module(void* addr
, struct modctl
*ctl
)
1449 return OSKextKextForAddress(addr
) == (void*)ctl
->mod_address
;
1456 dtrace_vtime_enable(void) {}
1459 dtrace_vtime_disable(void) {}
1461 #else /* else ! CONFIG_DTRACE */
1463 #include <sys/types.h>
1464 #include <mach/vm_types.h>
1465 #include <mach/kmod.h>
1468 * This exists to prevent build errors when dtrace is unconfigured.
1471 kern_return_t
_dtrace_register_anon_DOF(char *, unsigned char *, uint32_t);
1473 kern_return_t
_dtrace_register_anon_DOF(char *arg1
, unsigned char *arg2
, uint32_t arg3
) {
1474 #pragma unused(arg1, arg2, arg3)
1476 return KERN_FAILURE
;
1479 #endif /* CONFIG_DTRACE */