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 <kern/kern_types.h>
55 #include <kern/timer_call.h>
56 #include <kern/thread_call.h>
57 #include <kern/task.h>
58 #include <kern/sched_prim.h>
59 #include <kern/queue.h>
60 #include <miscfs/devfs/devfs.h>
61 #include <kern/kalloc.h>
63 #include <mach/vm_param.h>
64 #include <mach/mach_vm.h>
65 #include <mach/task.h>
67 #include <vm/vm_map.h> /* All the bits we care about are guarded by MACH_KERNEL_PRIVATE :-( */
72 /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
73 #define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
75 /* Not called from probe context */
81 if ((p
= proc_find(pid
)) == PROC_NULL
) {
85 task_suspend_internal(p
->task
);
89 lck_mtx_lock(&p
->p_dtrace_sprlock
);
94 /* Not called from probe context */
99 lck_mtx_unlock(&p
->p_dtrace_sprlock
);
103 task_resume_internal(p
->task
);
113 // These are not exported from vm_map.h.
114 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
);
115 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
);
117 /* Not called from probe context */
119 uread(proc_t
*p
, void *buf
, user_size_t len
, user_addr_t a
)
123 ASSERT(p
!= PROC_NULL
);
124 ASSERT(p
->task
!= NULL
);
126 task_t task
= p
->task
;
129 * Grab a reference to the task vm_map_t to make sure
130 * the map isn't pulled out from under us.
132 * Because the proc_lock is not held at all times on all code
133 * paths leading here, it is possible for the proc to have
134 * exited. If the map is null, fail.
136 vm_map_t map
= get_task_map_reference(task
);
138 ret
= vm_map_read_user( map
, (vm_map_address_t
)a
, buf
, (vm_size_t
)len
);
139 vm_map_deallocate(map
);
141 ret
= KERN_TERMINATED
;
147 /* Not called from probe context */
149 uwrite(proc_t
*p
, void *buf
, user_size_t len
, user_addr_t a
)
154 ASSERT(p
->task
!= NULL
);
156 task_t task
= p
->task
;
159 * Grab a reference to the task vm_map_t to make sure
160 * the map isn't pulled out from under us.
162 * Because the proc_lock is not held at all times on all code
163 * paths leading here, it is possible for the proc to have
164 * exited. If the map is null, fail.
166 vm_map_t map
= get_task_map_reference(task
);
168 /* Find the memory permissions. */
169 uint32_t nestingDepth
=999999;
170 vm_region_submap_short_info_data_64_t info
;
171 mach_msg_type_number_t count
= VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
;
172 mach_vm_address_t address
= (mach_vm_address_t
)a
;
173 mach_vm_size_t sizeOfRegion
= (mach_vm_size_t
)len
;
175 ret
= mach_vm_region_recurse(map
, &address
, &sizeOfRegion
, &nestingDepth
, (vm_region_recurse_info_t
)&info
, &count
);
176 if (ret
!= KERN_SUCCESS
)
181 if (!(info
.protection
& VM_PROT_WRITE
)) {
182 /* Save the original protection values for restoration later */
183 reprotect
= info
.protection
;
185 if (info
.max_protection
& VM_PROT_WRITE
) {
186 /* The memory is not currently writable, but can be made writable. */
187 ret
= mach_vm_protect (map
, (mach_vm_offset_t
)a
, (mach_vm_size_t
)len
, 0, reprotect
| VM_PROT_WRITE
);
190 * The memory is not currently writable, and cannot be made writable. We need to COW this memory.
192 * Strange, we can't just say "reprotect | VM_PROT_COPY", that fails.
194 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
);
197 if (ret
!= KERN_SUCCESS
)
201 /* The memory was already writable. */
202 reprotect
= VM_PROT_NONE
;
205 ret
= vm_map_write_user( map
,
210 if (ret
!= KERN_SUCCESS
)
213 if (reprotect
!= VM_PROT_NONE
) {
214 ASSERT(reprotect
& VM_PROT_EXECUTE
);
215 ret
= mach_vm_protect (map
, (mach_vm_offset_t
)a
, (mach_vm_size_t
)len
, 0, reprotect
);
219 vm_map_deallocate(map
);
221 ret
= KERN_TERMINATED
;
233 dtrace_cpu_t
*cpu_list
;
234 cpu_core_t
*cpu_core
; /* XXX TLB lockdown? */
241 * dtrace_CRED() can be called from probe context. We cannot simply call kauth_cred_get() since
242 * that function may try to resolve a lazy credential binding, which entails taking the proc_lock.
247 struct uthread
*uthread
= get_bsdthread_info(current_thread());
252 return uthread
->uu_ucred
; /* May return NOCRED which is defined to be 0 */
255 #define HAS_ALLPRIVS(cr) priv_isfullset(&CR_OEPRIV(cr))
256 #define HAS_PRIVILEGE(cr, pr) ((pr) == PRIV_ALL ? \
258 PRIV_ISASSERT(&CR_OEPRIV(cr), pr))
260 int PRIV_POLICY_CHOICE(void* cred
, int priv
, int all
)
262 #pragma unused(priv, all)
263 return kauth_cred_issuser(cred
); /* XXX TODO: How is this different from PRIV_POLICY_ONLY? */
267 PRIV_POLICY_ONLY(void *cr
, int priv
, int boolean
)
269 #pragma unused(priv, boolean)
270 return kauth_cred_issuser(cr
); /* XXX TODO: HAS_PRIVILEGE(cr, priv); */
273 /* XXX Get around const poisoning using structure assigns */
275 crgetgid(const cred_t
*cr
) { cred_t copy_cr
= *cr
; return kauth_cred_getgid(©_cr
); }
278 crgetuid(const cred_t
*cr
) { cred_t copy_cr
= *cr
; return kauth_cred_getuid(©_cr
); }
284 typedef struct wrap_timer_call
{
285 /* node attributes */
291 struct timer_call call
;
293 /* next item in the linked list */
294 LIST_ENTRY(wrap_timer_call
) entries
;
297 #define WAKEUP_REAPER 0x7FFFFFFFFFFFFFFFLL
298 #define NEARLY_FOREVER 0x7FFFFFFFFFFFFFFELL
301 typedef struct cyc_list
{
302 cyc_omni_handler_t cyl_omni
;
303 wrap_timer_call_t cyl_wrap_by_cpus
[];
304 #if __arm__ && (__BIGGEST_ALIGNMENT__ > 4)
305 } __attribute__ ((aligned (8))) cyc_list_t
;
310 /* CPU going online/offline notifications */
311 void (*dtrace_cpu_state_changed_hook
)(int, boolean_t
) = NULL
;
312 void dtrace_cpu_state_changed(int, boolean_t
);
315 dtrace_install_cpu_hooks(void) {
316 dtrace_cpu_state_changed_hook
= dtrace_cpu_state_changed
;
320 dtrace_cpu_state_changed(int cpuid
, boolean_t is_running
) {
321 #pragma unused(cpuid)
322 wrap_timer_call_t
*wrapTC
= NULL
;
323 boolean_t suspend
= (is_running
? FALSE
: TRUE
);
326 /* Ensure that we're not going to leave the CPU */
327 s
= dtrace_interrupt_disable();
328 assert(cpuid
== cpu_number());
330 LIST_FOREACH(wrapTC
, &(cpu_list
[cpu_number()].cpu_cyc_list
), entries
) {
331 assert(wrapTC
->cpuid
== cpu_number());
333 assert(!wrapTC
->suspended
);
334 /* If this fails, we'll panic anyway, so let's do this now. */
335 if (!timer_call_cancel(&wrapTC
->call
))
336 panic("timer_call_set_suspend() failed to cancel a timer call");
337 wrapTC
->suspended
= TRUE
;
339 /* Rearm the timer, but ensure it was suspended first. */
340 assert(wrapTC
->suspended
);
341 clock_deadline_for_periodic_event(wrapTC
->when
.cyt_interval
, mach_absolute_time(),
343 timer_call_enter1(&wrapTC
->call
, (void*) wrapTC
, wrapTC
->deadline
,
344 TIMER_CALL_SYS_CRITICAL
| TIMER_CALL_LOCAL
);
345 wrapTC
->suspended
= FALSE
;
350 /* Restore the previous interrupt state. */
351 dtrace_interrupt_enable(s
);
355 _timer_call_apply_cyclic( void *ignore
, void *vTChdl
)
357 #pragma unused(ignore)
358 wrap_timer_call_t
*wrapTC
= (wrap_timer_call_t
*)vTChdl
;
360 (*(wrapTC
->hdlr
.cyh_func
))( wrapTC
->hdlr
.cyh_arg
);
362 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, mach_absolute_time(), &(wrapTC
->deadline
) );
363 timer_call_enter1( &(wrapTC
->call
), (void *)wrapTC
, wrapTC
->deadline
, TIMER_CALL_SYS_CRITICAL
| TIMER_CALL_LOCAL
);
367 timer_call_add_cyclic(wrap_timer_call_t
*wrapTC
, cyc_handler_t
*handler
, cyc_time_t
*when
)
372 timer_call_setup( &(wrapTC
->call
), _timer_call_apply_cyclic
, NULL
);
373 wrapTC
->hdlr
= *handler
;
374 wrapTC
->when
= *when
;
376 nanoseconds_to_absolutetime( wrapTC
->when
.cyt_interval
, (uint64_t *)&wrapTC
->when
.cyt_interval
);
378 now
= mach_absolute_time();
379 wrapTC
->deadline
= now
;
381 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, now
, &(wrapTC
->deadline
) );
383 /* Insert the timer to the list of the running timers on this CPU, and start it. */
384 s
= dtrace_interrupt_disable();
385 wrapTC
->cpuid
= cpu_number();
386 LIST_INSERT_HEAD(&cpu_list
[wrapTC
->cpuid
].cpu_cyc_list
, wrapTC
, entries
);
387 timer_call_enter1(&wrapTC
->call
, (void*) wrapTC
, wrapTC
->deadline
,
388 TIMER_CALL_SYS_CRITICAL
| TIMER_CALL_LOCAL
);
389 wrapTC
->suspended
= FALSE
;
390 dtrace_interrupt_enable(s
);
392 return (cyclic_id_t
)wrapTC
;
396 * Executed on the CPU the timer is running on.
399 timer_call_remove_cyclic(wrap_timer_call_t
*wrapTC
)
402 assert(cpu_number() == wrapTC
->cpuid
);
404 if (!timer_call_cancel(&wrapTC
->call
))
405 panic("timer_call_remove_cyclic() failed to cancel a timer call");
407 LIST_REMOVE(wrapTC
, entries
);
411 timer_call_get_cyclic_arg(wrap_timer_call_t
*wrapTC
)
413 return (wrapTC
? wrapTC
->hdlr
.cyh_arg
: NULL
);
417 cyclic_timer_add(cyc_handler_t
*handler
, cyc_time_t
*when
)
419 wrap_timer_call_t
*wrapTC
= _MALLOC(sizeof(wrap_timer_call_t
), M_TEMP
, M_ZERO
| M_WAITOK
);
423 return timer_call_add_cyclic( wrapTC
, handler
, when
);
427 cyclic_timer_remove(cyclic_id_t cyclic
)
429 ASSERT( cyclic
!= CYCLIC_NONE
);
431 /* Removing a timer call must be done on the CPU the timer is running on. */
432 wrap_timer_call_t
*wrapTC
= (wrap_timer_call_t
*) cyclic
;
433 dtrace_xcall(wrapTC
->cpuid
, (dtrace_xcall_t
) timer_call_remove_cyclic
, (void*) cyclic
);
435 _FREE((void *)cyclic
, M_TEMP
);
439 _cyclic_add_omni(cyc_list_t
*cyc_list
)
443 cyc_omni_handler_t
*omni
= &cyc_list
->cyl_omni
;
445 (omni
->cyo_online
)(omni
->cyo_arg
, CPU
, &cH
, &cT
);
447 wrap_timer_call_t
*wrapTC
= &cyc_list
->cyl_wrap_by_cpus
[cpu_number()];
448 timer_call_add_cyclic(wrapTC
, &cH
, &cT
);
452 cyclic_add_omni(cyc_omni_handler_t
*omni
)
454 cyc_list_t
*cyc_list
=
455 _MALLOC(sizeof(cyc_list_t
) + NCPU
* sizeof(wrap_timer_call_t
), M_TEMP
, M_ZERO
| M_WAITOK
);
457 if (NULL
== cyc_list
)
460 cyc_list
->cyl_omni
= *omni
;
462 dtrace_xcall(DTRACE_CPUALL
, (dtrace_xcall_t
)_cyclic_add_omni
, (void *)cyc_list
);
464 return (cyclic_id_list_t
)cyc_list
;
468 _cyclic_remove_omni(cyc_list_t
*cyc_list
)
470 cyc_omni_handler_t
*omni
= &cyc_list
->cyl_omni
;
472 wrap_timer_call_t
*wrapTC
;
475 * If the processor was offline when dtrace started, we did not allocate
476 * a cyclic timer for this CPU.
478 if ((wrapTC
= &cyc_list
->cyl_wrap_by_cpus
[cpu_number()]) != NULL
) {
479 oarg
= timer_call_get_cyclic_arg(wrapTC
);
480 timer_call_remove_cyclic(wrapTC
);
481 (omni
->cyo_offline
)(omni
->cyo_arg
, CPU
, oarg
);
486 cyclic_remove_omni(cyclic_id_list_t cyc_list
)
488 ASSERT(cyc_list
!= NULL
);
490 dtrace_xcall(DTRACE_CPUALL
, (dtrace_xcall_t
)_cyclic_remove_omni
, (void *)cyc_list
);
491 _FREE(cyc_list
, M_TEMP
);
494 typedef struct wrap_thread_call
{
499 } wrap_thread_call_t
;
502 * _cyclic_apply will run on some thread under kernel_task. That's OK for the
503 * cleaner and the deadman, but too distant in time and place for the profile provider.
506 _cyclic_apply( void *ignore
, void *vTChdl
)
508 #pragma unused(ignore)
509 wrap_thread_call_t
*wrapTC
= (wrap_thread_call_t
*)vTChdl
;
511 (*(wrapTC
->hdlr
.cyh_func
))( wrapTC
->hdlr
.cyh_arg
);
513 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, mach_absolute_time(), &(wrapTC
->deadline
) );
514 (void)thread_call_enter1_delayed( wrapTC
->TChdl
, (void *)wrapTC
, wrapTC
->deadline
);
516 /* Did cyclic_remove request a wakeup call when this thread call was re-armed? */
517 if (wrapTC
->when
.cyt_interval
== WAKEUP_REAPER
)
518 thread_wakeup((event_t
)wrapTC
);
522 cyclic_add(cyc_handler_t
*handler
, cyc_time_t
*when
)
526 wrap_thread_call_t
*wrapTC
= _MALLOC(sizeof(wrap_thread_call_t
), M_TEMP
, M_ZERO
| M_WAITOK
);
530 wrapTC
->TChdl
= thread_call_allocate( _cyclic_apply
, NULL
);
531 wrapTC
->hdlr
= *handler
;
532 wrapTC
->when
= *when
;
534 ASSERT(when
->cyt_when
== 0);
535 ASSERT(when
->cyt_interval
< WAKEUP_REAPER
);
537 nanoseconds_to_absolutetime(wrapTC
->when
.cyt_interval
, (uint64_t *)&wrapTC
->when
.cyt_interval
);
539 now
= mach_absolute_time();
540 wrapTC
->deadline
= now
;
542 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, now
, &(wrapTC
->deadline
) );
543 (void)thread_call_enter1_delayed( wrapTC
->TChdl
, (void *)wrapTC
, wrapTC
->deadline
);
545 return (cyclic_id_t
)wrapTC
;
549 noop_cyh_func(void * ignore
)
551 #pragma unused(ignore)
555 cyclic_remove(cyclic_id_t cyclic
)
557 wrap_thread_call_t
*wrapTC
= (wrap_thread_call_t
*)cyclic
;
559 ASSERT(cyclic
!= CYCLIC_NONE
);
561 while (!thread_call_cancel(wrapTC
->TChdl
)) {
562 int ret
= assert_wait(wrapTC
, THREAD_UNINT
);
563 ASSERT(ret
== THREAD_WAITING
);
565 wrapTC
->when
.cyt_interval
= WAKEUP_REAPER
;
567 ret
= thread_block(THREAD_CONTINUE_NULL
);
568 ASSERT(ret
== THREAD_AWAKENED
);
571 if (thread_call_free(wrapTC
->TChdl
))
572 _FREE(wrapTC
, M_TEMP
);
574 /* Gut this cyclic and move on ... */
575 wrapTC
->hdlr
.cyh_func
= noop_cyh_func
;
576 wrapTC
->when
.cyt_interval
= NEARLY_FOREVER
;
584 ddi_report_dev(dev_info_t
*devi
)
590 static unsigned int gRegisteredProps
= 0;
592 char name
[32]; /* enough for "dof-data-" + digits */
597 kern_return_t
_dtrace_register_anon_DOF(char *, uchar_t
*, uint_t
);
600 _dtrace_register_anon_DOF(char *name
, uchar_t
*data
, uint_t nelements
)
602 if (gRegisteredProps
< sizeof(gPropTable
)/sizeof(gPropTable
[0])) {
603 int *p
= (int *)_MALLOC(nelements
*sizeof(int), M_TEMP
, M_WAITOK
);
608 strlcpy(gPropTable
[gRegisteredProps
].name
, name
, sizeof(gPropTable
[0].name
));
609 gPropTable
[gRegisteredProps
].nelements
= nelements
;
610 gPropTable
[gRegisteredProps
].data
= p
;
612 while (nelements
-- > 0) {
613 *p
++ = (int)(*data
++);
624 ddi_prop_lookup_int_array(dev_t match_dev
, dev_info_t
*dip
, uint_t flags
,
625 const char *name
, int **data
, uint_t
*nelements
)
627 #pragma unused(match_dev,dip,flags)
629 for (i
= 0; i
< gRegisteredProps
; ++i
)
631 if (0 == strncmp(name
, gPropTable
[i
].name
,
632 sizeof(gPropTable
[i
].name
))) {
633 *data
= gPropTable
[i
].data
;
634 *nelements
= gPropTable
[i
].nelements
;
642 ddi_prop_free(void *buf
)
649 ddi_driver_major(dev_info_t
*devi
) { return (int)major(CAST_DOWN_EXPLICIT(int,devi
)); }
652 ddi_create_minor_node(dev_info_t
*dip
, const char *name
, int spec_type
,
653 minor_t minor_num
, const char *node_type
, int flag
)
655 #pragma unused(spec_type,node_type,flag)
656 dev_t dev
= makedev( ddi_driver_major(dip
), minor_num
);
658 if (NULL
== devfs_make_node( dev
, DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666, name
, 0 ))
665 ddi_remove_minor_node(dev_info_t
*dip
, char *name
)
667 #pragma unused(dip,name)
668 /* XXX called from dtrace_detach, so NOTREACHED for now. */
674 return (major_t
) major(d
);
680 return (minor_t
) minor(d
);
684 makedevice(major_t major
, minor_t minor
)
686 return makedev( major
, minor
);
689 int ddi_getprop(dev_t dev
, dev_info_t
*dip
, int flags
, const char *name
, int defvalue
)
691 #pragma unused(dev, dip, flags, name)
697 * Kernel Debug Interface
700 kdi_dtrace_set(kdi_dtrace_set_t ignore
)
702 #pragma unused(ignore)
703 return 0; /* Success */
706 extern void Debugger(const char*);
709 debug_enter(char *c
) { Debugger(c
); }
716 dt_kmem_alloc(size_t size
, int kmflag
)
718 #pragma unused(kmflag)
721 * We ignore the M_NOWAIT bit in kmflag (all of kmflag, in fact).
722 * Requests larger than 8K with M_NOWAIT fail in kalloc_canblock.
724 #if defined(DTRACE_MEMORY_ZONES)
725 return dtrace_alloc(size
);
732 dt_kmem_zalloc(size_t size
, int kmflag
)
734 #pragma unused(kmflag)
737 * We ignore the M_NOWAIT bit in kmflag (all of kmflag, in fact).
738 * Requests larger than 8K with M_NOWAIT fail in kalloc_canblock.
740 #if defined(DTRACE_MEMORY_ZONES)
741 void* buf
= dtrace_alloc(size
);
743 void* buf
= kalloc(size
);
755 dt_kmem_free(void *buf
, size_t size
)
759 * DTrace relies on this, its doing a lot of NULL frees.
760 * A null free causes the debug builds to panic.
762 if (buf
== NULL
) return;
766 #if defined(DTRACE_MEMORY_ZONES)
767 dtrace_free(buf
, size
);
776 * aligned kmem allocator
777 * align should be a power of two
780 void* dt_kmem_alloc_aligned(size_t size
, size_t align
, int kmflag
)
782 void *mem
, **addr_to_free
;
783 intptr_t mem_aligned
;
784 size_t *size_to_free
, hdr_size
;
786 /* Must be a power of two. */
788 assert((align
& (align
- 1)) == 0);
791 * We are going to add a header to the allocation. It contains
792 * the address to free and the total size of the buffer.
794 hdr_size
= sizeof(size_t) + sizeof(void*);
795 mem
= dt_kmem_alloc(size
+ align
+ hdr_size
, kmflag
);
799 mem_aligned
= (intptr_t) (((intptr_t) mem
+ align
+ hdr_size
) & ~(align
- 1));
801 /* Write the address to free in the header. */
802 addr_to_free
= (void**) (mem_aligned
- sizeof(void*));
805 /* Write the size to free in the header. */
806 size_to_free
= (size_t*) (mem_aligned
- hdr_size
);
807 *size_to_free
= size
+ align
+ hdr_size
;
809 return (void*) mem_aligned
;
812 void* dt_kmem_zalloc_aligned(size_t size
, size_t align
, int kmflag
)
816 buf
= dt_kmem_alloc_aligned(size
, align
, kmflag
);
826 void dt_kmem_free_aligned(void* buf
, size_t size
)
829 intptr_t ptr
= (intptr_t) buf
;
830 void **addr_to_free
= (void**) (ptr
- sizeof(void*));
831 size_t *size_to_free
= (size_t*) (ptr
- (sizeof(size_t) + sizeof(void*)));
836 dt_kmem_free(*addr_to_free
, *size_to_free
);
840 * dtrace wants to manage just a single block: dtrace_state_percpu_t * NCPU, and
841 * doesn't specify constructor, destructor, or reclaim methods.
842 * At present, it always zeroes the block it obtains from kmem_cache_alloc().
843 * We'll manage this constricted use of kmem_cache with ordinary _MALLOC and _FREE.
847 const char *name
, /* descriptive name for this cache */
848 size_t bufsize
, /* size of the objects it manages */
849 size_t align
, /* required object alignment */
850 int (*constructor
)(void *, void *, int), /* object constructor */
851 void (*destructor
)(void *, void *), /* object destructor */
852 void (*reclaim
)(void *), /* memory reclaim callback */
853 void *private, /* pass-thru arg for constr/destr/reclaim */
854 vmem_t
*vmp
, /* vmem source for slab allocation */
855 int cflags
) /* cache creation flags */
857 #pragma unused(name,align,constructor,destructor,reclaim,private,vmp,cflags)
858 return (kmem_cache_t
*)bufsize
; /* A cookie that tracks the single object size. */
862 kmem_cache_alloc(kmem_cache_t
*cp
, int kmflag
)
864 #pragma unused(kmflag)
865 size_t bufsize
= (size_t)cp
;
866 return (void *)_MALLOC(bufsize
, M_TEMP
, M_WAITOK
);
870 kmem_cache_free(kmem_cache_t
*cp
, void *buf
)
877 kmem_cache_destroy(kmem_cache_t
*cp
)
885 extern void thread_call_setup(thread_call_t
, thread_call_func_t
, thread_call_param_t
); /* XXX MACH_KERNEL_PRIVATE */
888 _taskq_apply( task_func_t func
, thread_call_param_t arg
)
894 taskq_create(const char *name
, int nthreads
, pri_t pri
, int minalloc
,
895 int maxalloc
, uint_t flags
)
897 #pragma unused(name,nthreads,pri,minalloc,maxalloc,flags)
899 return (taskq_t
*)thread_call_allocate( (thread_call_func_t
)_taskq_apply
, NULL
);
903 taskq_dispatch(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
)
905 #pragma unused(flags)
906 thread_call_setup( (thread_call_t
) tq
, (thread_call_func_t
)_taskq_apply
, (thread_call_param_t
)func
);
907 thread_call_enter1( (thread_call_t
) tq
, (thread_call_param_t
)arg
);
908 return (taskqid_t
) tq
/* for lack of anything better */;
912 taskq_destroy(taskq_t
*tq
)
914 thread_call_cancel( (thread_call_t
) tq
);
915 thread_call_free( (thread_call_t
) tq
);
921 * vmem (Solaris "slab" allocator) used by DTrace solely to hand out resource ids
923 typedef unsigned int u_daddr_t
;
926 /* By passing around blist *handles*, the underlying blist can be resized as needed. */
932 vmem_create(const char *name
, void *base
, size_t size
, size_t quantum
, void *ignore5
,
933 void *ignore6
, vmem_t
*source
, size_t qcache_max
, int vmflag
)
935 #pragma unused(name,quantum,ignore5,ignore6,source,qcache_max,vmflag)
937 struct blist_hdl
*p
= _MALLOC(sizeof(struct blist_hdl
), M_TEMP
, M_WAITOK
);
939 ASSERT(quantum
== 1);
940 ASSERT(NULL
== ignore5
);
941 ASSERT(NULL
== ignore6
);
942 ASSERT(NULL
== source
);
943 ASSERT(0 == qcache_max
);
944 ASSERT(vmflag
& VMC_IDENTIFIER
);
946 size
= MIN(128, size
); /* Clamp to 128 initially, since the underlying data structure is pre-allocated */
948 p
->blist
= bl
= blist_create( size
);
949 blist_free(bl
, 0, size
);
950 if (base
) blist_alloc( bl
, (daddr_t
)(uintptr_t)base
); /* Chomp off initial ID(s) */
956 vmem_alloc(vmem_t
*vmp
, size_t size
, int vmflag
)
958 #pragma unused(vmflag)
959 struct blist_hdl
*q
= (struct blist_hdl
*)vmp
;
960 blist_t bl
= q
->blist
;
963 p
= blist_alloc(bl
, (daddr_t
)size
);
965 if ((daddr_t
)-1 == p
) {
966 blist_resize(&bl
, (bl
->bl_blocks
) << 1, 1);
968 p
= blist_alloc(bl
, (daddr_t
)size
);
969 if ((daddr_t
)-1 == p
)
970 panic("vmem_alloc: failure after blist_resize!");
973 return (void *)(uintptr_t)p
;
977 vmem_free(vmem_t
*vmp
, void *vaddr
, size_t size
)
979 struct blist_hdl
*p
= (struct blist_hdl
*)vmp
;
981 blist_free( p
->blist
, (daddr_t
)(uintptr_t)vaddr
, (daddr_t
)size
);
985 vmem_destroy(vmem_t
*vmp
)
987 struct blist_hdl
*p
= (struct blist_hdl
*)vmp
;
989 blist_destroy( p
->blist
);
990 _FREE( p
, sizeof(struct blist_hdl
) );
998 * dtrace_gethrestime() provides the "walltimestamp", a value that is anchored at
999 * January 1, 1970. Because it can be called from probe context, it must take no locks.
1003 dtrace_gethrestime(void)
1006 clock_nsec_t nanosecs
;
1007 uint64_t secs64
, ns64
;
1009 clock_get_calendar_nanotime_nowait(&secs
, &nanosecs
);
1010 secs64
= (uint64_t)secs
;
1011 ns64
= (uint64_t)nanosecs
;
1013 ns64
= ns64
+ (secs64
* 1000000000LL);
1018 * dtrace_gethrtime() provides high-resolution timestamps with machine-dependent origin.
1019 * Hence its primary use is to specify intervals.
1023 dtrace_abs_to_nano(uint64_t elapsed
)
1025 static mach_timebase_info_data_t sTimebaseInfo
= { 0, 0 };
1028 * If this is the first time we've run, get the timebase.
1029 * We can use denom == 0 to indicate that sTimebaseInfo is
1030 * uninitialised because it makes no sense to have a zero
1031 * denominator in a fraction.
1034 if ( sTimebaseInfo
.denom
== 0 ) {
1035 (void) clock_timebase_info(&sTimebaseInfo
);
1039 * Convert to nanoseconds.
1040 * return (elapsed * (uint64_t)sTimebaseInfo.numer)/(uint64_t)sTimebaseInfo.denom;
1042 * Provided the final result is representable in 64 bits the following maneuver will
1043 * deliver that result without intermediate overflow.
1045 if (sTimebaseInfo
.denom
== sTimebaseInfo
.numer
)
1047 else if (sTimebaseInfo
.denom
== 1)
1048 return elapsed
* (uint64_t)sTimebaseInfo
.numer
;
1050 /* Decompose elapsed = eta32 * 2^32 + eps32: */
1051 uint64_t eta32
= elapsed
>> 32;
1052 uint64_t eps32
= elapsed
& 0x00000000ffffffffLL
;
1054 uint32_t numer
= sTimebaseInfo
.numer
, denom
= sTimebaseInfo
.denom
;
1056 /* Form product of elapsed64 (decomposed) and numer: */
1057 uint64_t mu64
= numer
* eta32
;
1058 uint64_t lambda64
= numer
* eps32
;
1060 /* Divide the constituents by denom: */
1061 uint64_t q32
= mu64
/denom
;
1062 uint64_t r32
= mu64
- (q32
* denom
); /* mu64 % denom */
1064 return (q32
<< 32) + ((r32
<< 32) + lambda64
)/denom
;
1069 dtrace_gethrtime(void)
1071 static uint64_t start
= 0;
1074 start
= mach_absolute_time();
1076 return dtrace_abs_to_nano(mach_absolute_time() - start
);
1080 * Atomicity and synchronization
1083 dtrace_cas32(uint32_t *target
, uint32_t cmp
, uint32_t new)
1085 if (OSCompareAndSwap( (UInt32
)cmp
, (UInt32
)new, (volatile UInt32
*)target
))
1088 return ~cmp
; /* Must return something *other* than cmp */
1092 dtrace_casptr(void *target
, void *cmp
, void *new)
1094 if (OSCompareAndSwapPtr( cmp
, new, (void**)target
))
1097 return (void *)(~(uintptr_t)cmp
); /* Must return something *other* than cmp */
1101 * Interrupt manipulation
1104 dtrace_interrupt_disable(void)
1106 return (dtrace_icookie_t
)ml_set_interrupts_enabled(FALSE
);
1110 dtrace_interrupt_enable(dtrace_icookie_t reenable
)
1112 (void)ml_set_interrupts_enabled((boolean_t
)reenable
);
1119 dtrace_sync_func(void) {}
1122 * dtrace_sync() is not called from probe context.
1127 dtrace_xcall(DTRACE_CPUALL
, (dtrace_xcall_t
)dtrace_sync_func
, NULL
);
1131 * The dtrace_copyin/out/instr and dtrace_fuword* routines can be called from probe context.
1134 extern kern_return_t
dtrace_copyio_preflight(addr64_t
);
1135 extern kern_return_t
dtrace_copyio_postflight(addr64_t
);
1138 dtrace_copycheck(user_addr_t uaddr
, uintptr_t kaddr
, size_t size
)
1140 #pragma unused(kaddr)
1142 vm_offset_t recover
= dtrace_set_thread_recover( current_thread(), 0 ); /* Snare any extant recovery point. */
1143 dtrace_set_thread_recover( current_thread(), recover
); /* Put it back. We *must not* re-enter and overwrite. */
1145 ASSERT(kaddr
+ size
>= kaddr
);
1147 if ( uaddr
+ size
< uaddr
|| /* Avoid address wrap. */
1148 KERN_FAILURE
== dtrace_copyio_preflight(uaddr
)) /* Machine specific setup/constraints. */
1150 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1151 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1158 dtrace_copyin(user_addr_t src
, uintptr_t dst
, size_t len
, volatile uint16_t *flags
)
1160 #pragma unused(flags)
1162 if (dtrace_copycheck( src
, dst
, len
)) {
1163 if (copyin((const user_addr_t
)src
, (char *)dst
, (vm_size_t
)len
)) {
1164 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1165 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= src
;
1167 dtrace_copyio_postflight(src
);
1172 dtrace_copyinstr(user_addr_t src
, uintptr_t dst
, size_t len
, volatile uint16_t *flags
)
1174 #pragma unused(flags)
1178 if (dtrace_copycheck( src
, dst
, len
)) {
1179 /* copyin as many as 'len' bytes. */
1180 int error
= copyinstr((const user_addr_t
)src
, (char *)dst
, (vm_size_t
)len
, &actual
);
1183 * ENAMETOOLONG is returned when 'len' bytes have been copied in but the NUL terminator was
1184 * not encountered. That does not require raising CPU_DTRACE_BADADDR, and we press on.
1185 * Note that we do *not* stuff a NUL terminator when returning ENAMETOOLONG, that's left
1188 if (error
&& error
!= ENAMETOOLONG
) {
1189 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1190 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= src
;
1192 dtrace_copyio_postflight(src
);
1197 dtrace_copyout(uintptr_t src
, user_addr_t dst
, size_t len
, volatile uint16_t *flags
)
1199 #pragma unused(flags)
1201 if (dtrace_copycheck( dst
, src
, len
)) {
1202 if (copyout((const void *)src
, dst
, (vm_size_t
)len
)) {
1203 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1204 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= dst
;
1206 dtrace_copyio_postflight(dst
);
1211 dtrace_copyoutstr(uintptr_t src
, user_addr_t dst
, size_t len
, volatile uint16_t *flags
)
1213 #pragma unused(flags)
1217 if (dtrace_copycheck( dst
, src
, len
)) {
1220 * ENAMETOOLONG is returned when 'len' bytes have been copied out but the NUL terminator was
1221 * not encountered. We raise CPU_DTRACE_BADADDR in that case.
1222 * Note that we do *not* stuff a NUL terminator when returning ENAMETOOLONG, that's left
1225 if (copyoutstr((const void *)src
, dst
, (size_t)len
, &actual
)) {
1226 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1227 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= dst
;
1229 dtrace_copyio_postflight(dst
);
1233 extern const int copysize_limit_panic
;
1236 dtrace_buffer_copyout(const void *kaddr
, user_addr_t uaddr
, vm_size_t nbytes
)
1239 * Partition the copyout in copysize_limit_panic-sized chunks
1241 while (nbytes
>= (vm_size_t
)copysize_limit_panic
) {
1242 if (copyout(kaddr
, uaddr
, copysize_limit_panic
) != 0)
1245 nbytes
-= copysize_limit_panic
;
1246 uaddr
+= copysize_limit_panic
;
1247 kaddr
+= copysize_limit_panic
;
1250 if (copyout(kaddr
, uaddr
, nbytes
) != 0)
1258 dtrace_fuword8(user_addr_t uaddr
)
1262 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1263 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1264 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1265 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1266 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1268 dtrace_copyio_postflight(uaddr
);
1270 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1276 dtrace_fuword16(user_addr_t uaddr
)
1280 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1281 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1282 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1283 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1284 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1286 dtrace_copyio_postflight(uaddr
);
1288 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1294 dtrace_fuword32(user_addr_t uaddr
)
1298 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1299 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1300 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1301 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1302 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1304 dtrace_copyio_postflight(uaddr
);
1306 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1312 dtrace_fuword64(user_addr_t uaddr
)
1316 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1317 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1318 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1319 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1320 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1322 dtrace_copyio_postflight(uaddr
);
1324 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1330 * Emulation of Solaris fuword / suword
1331 * Called from the fasttrap provider, so the use of copyin/out requires fewer safegaurds.
1335 fuword8(user_addr_t uaddr
, uint8_t *value
)
1337 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint8_t)) != 0) {
1345 fuword16(user_addr_t uaddr
, uint16_t *value
)
1347 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint16_t)) != 0) {
1355 fuword32(user_addr_t uaddr
, uint32_t *value
)
1357 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint32_t)) != 0) {
1365 fuword64(user_addr_t uaddr
, uint64_t *value
)
1367 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint64_t)) != 0) {
1375 fuword8_noerr(user_addr_t uaddr
, uint8_t *value
)
1377 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint8_t))) {
1383 fuword16_noerr(user_addr_t uaddr
, uint16_t *value
)
1385 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint16_t))) {
1391 fuword32_noerr(user_addr_t uaddr
, uint32_t *value
)
1393 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint32_t))) {
1399 fuword64_noerr(user_addr_t uaddr
, uint64_t *value
)
1401 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint64_t))) {
1407 suword64(user_addr_t addr
, uint64_t value
)
1409 if (copyout((const void *)&value
, addr
, sizeof(value
)) != 0) {
1417 suword32(user_addr_t addr
, uint32_t value
)
1419 if (copyout((const void *)&value
, addr
, sizeof(value
)) != 0) {
1427 suword16(user_addr_t addr
, uint16_t value
)
1429 if (copyout((const void *)&value
, addr
, sizeof(value
)) != 0) {
1437 suword8(user_addr_t addr
, uint8_t value
)
1439 if (copyout((const void *)&value
, addr
, sizeof(value
)) != 0) {
1450 extern boolean_t
dtrace_tally_fault(user_addr_t
);
1453 dtrace_tally_fault(user_addr_t uaddr
)
1455 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1456 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1457 return( DTRACE_CPUFLAG_ISSET(CPU_DTRACE_NOFAULT
) ? TRUE
: FALSE
);
1461 extern int prf(const char *, va_list, int, struct tty
*); /* bsd/kern/subr_prf.h */
1464 vuprintf(const char *format
, va_list ap
)
1466 return prf(format
, ap
, TOTTY
, NULL
);
1469 /* Not called from probe context */
1470 void cmn_err( int level
, const char *format
, ... )
1472 #pragma unused(level)
1475 va_start(alist
, format
);
1476 vuprintf(format
, alist
);
1483 * 2002-01-24 gvdl Initial implementation of strstr
1486 __private_extern__
const char *
1487 strstr(const char *in
, const char *str
)
1496 return (const char *) in
; // Trivial empty string case
1507 } while (strncmp(in
, str
, len
) != 0);
1509 return (const char *) (in
- 1);
1513 bsearch(const void *key
, const void *base0
, size_t nmemb
, size_t size
, int (*compar
)(const void *, const void *))
1515 const char *base
= base0
;
1519 for (lim
= nmemb
; lim
!= 0; lim
>>= 1) {
1520 p
= base
+ (lim
>> 1) * size
;
1521 cmp
= (*compar
)(key
, p
);
1524 if (cmp
> 0) { /* key > p: move right */
1525 base
= (const char *)p
+ size
;
1527 } /* else move left */
1536 dtrace_caller(int ignore
)
1538 #pragma unused(ignore)
1539 return -1; /* Just as in Solaris dtrace_asm.s */
1543 dtrace_getstackdepth(int aframes
)
1545 struct frame
*fp
= (struct frame
*)__builtin_frame_address(0);
1546 struct frame
*nextfp
, *minfp
, *stacktop
;
1550 if ((on_intr
= CPU_ON_INTR(CPU
)) != 0)
1551 stacktop
= (struct frame
*)dtrace_get_cpu_int_stack_top();
1553 stacktop
= (struct frame
*)(dtrace_get_kernel_stack(current_thread()) + kernel_stack_size
);
1562 nextfp
= *(struct frame
**)fp
;
1564 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
1567 * Hop from interrupt stack to thread stack.
1569 vm_offset_t kstack_base
= dtrace_get_kernel_stack(current_thread());
1571 minfp
= (struct frame
*)kstack_base
;
1572 stacktop
= (struct frame
*)(kstack_base
+ kernel_stack_size
);
1584 if (depth
<= aframes
)
1587 return (depth
- aframes
);
1594 dtrace_vtime_enable(void) {}
1597 dtrace_vtime_disable(void) {}
1599 #else /* else ! CONFIG_DTRACE */
1601 #include <sys/types.h>
1602 #include <mach/vm_types.h>
1603 #include <mach/kmod.h>
1606 * This exists to prevent build errors when dtrace is unconfigured.
1609 kern_return_t
_dtrace_register_anon_DOF(char *, unsigned char *, uint32_t);
1611 kern_return_t
_dtrace_register_anon_DOF(char *arg1
, unsigned char *arg2
, uint32_t arg3
) {
1612 #pragma unused(arg1, arg2, arg3)
1614 return KERN_FAILURE
;
1617 #endif /* CONFIG_DTRACE */