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/thread_call.h>
55 #include <kern/task.h>
56 #include <kern/sched_prim.h>
57 #include <kern/queue.h>
58 #include <miscfs/devfs/devfs.h>
59 #include <kern/kalloc.h>
61 #include <mach/vm_param.h>
62 #include <mach/mach_vm.h>
63 #include <mach/task.h>
65 #include <vm/vm_map.h> /* All the bits we care about are guarded by MACH_KERNEL_PRIVATE :-( */
70 /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
71 #define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
73 /* Not called from probe context */
79 if ((p
= proc_find(pid
)) == PROC_NULL
) {
83 task_suspend(p
->task
);
87 lck_mtx_lock(&p
->p_dtrace_sprlock
);
92 /* Not called from probe context */
97 lck_mtx_unlock(&p
->p_dtrace_sprlock
);
101 task_resume(p
->task
);
111 // These are not exported from vm_map.h.
112 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
);
113 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
);
115 /* Not called from probe context */
117 uread(proc_t
*p
, void *buf
, user_size_t len
, user_addr_t a
)
121 ASSERT(p
!= PROC_NULL
);
122 ASSERT(p
->task
!= NULL
);
124 task_t task
= p
->task
;
127 * Grab a reference to the task vm_map_t to make sure
128 * the map isn't pulled out from under us.
130 * Because the proc_lock is not held at all times on all code
131 * paths leading here, it is possible for the proc to have
132 * exited. If the map is null, fail.
134 vm_map_t map
= get_task_map_reference(task
);
136 ret
= vm_map_read_user( map
, (vm_map_address_t
)a
, buf
, (vm_size_t
)len
);
137 vm_map_deallocate(map
);
139 ret
= KERN_TERMINATED
;
145 /* Not called from probe context */
147 uwrite(proc_t
*p
, void *buf
, user_size_t len
, user_addr_t a
)
152 ASSERT(p
->task
!= NULL
);
154 task_t task
= p
->task
;
157 * Grab a reference to the task vm_map_t to make sure
158 * the map isn't pulled out from under us.
160 * Because the proc_lock is not held at all times on all code
161 * paths leading here, it is possible for the proc to have
162 * exited. If the map is null, fail.
164 vm_map_t map
= get_task_map_reference(task
);
166 /* Find the memory permissions. */
167 uint32_t nestingDepth
=999999;
168 vm_region_submap_short_info_data_64_t info
;
169 mach_msg_type_number_t count
= VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
;
170 mach_vm_address_t address
= (mach_vm_address_t
)a
;
171 mach_vm_size_t sizeOfRegion
= (mach_vm_size_t
)len
;
173 ret
= mach_vm_region_recurse(map
, &address
, &sizeOfRegion
, &nestingDepth
, (vm_region_recurse_info_t
)&info
, &count
);
174 if (ret
!= KERN_SUCCESS
)
179 if (!(info
.protection
& VM_PROT_WRITE
)) {
180 /* Save the original protection values for restoration later */
181 reprotect
= info
.protection
;
183 if (info
.max_protection
& VM_PROT_WRITE
) {
184 /* The memory is not currently writable, but can be made writable. */
185 ret
= mach_vm_protect (map
, (mach_vm_offset_t
)a
, (mach_vm_size_t
)len
, 0, reprotect
| VM_PROT_WRITE
);
188 * The memory is not currently writable, and cannot be made writable. We need to COW this memory.
190 * Strange, we can't just say "reprotect | VM_PROT_COPY", that fails.
192 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
);
195 if (ret
!= KERN_SUCCESS
)
199 /* The memory was already writable. */
200 reprotect
= VM_PROT_NONE
;
203 ret
= vm_map_write_user( map
,
208 if (ret
!= KERN_SUCCESS
)
211 if (reprotect
!= VM_PROT_NONE
) {
212 ASSERT(reprotect
& VM_PROT_EXECUTE
);
213 ret
= mach_vm_protect (map
, (mach_vm_offset_t
)a
, (mach_vm_size_t
)len
, 0, reprotect
);
217 vm_map_deallocate(map
);
219 ret
= KERN_TERMINATED
;
230 dtrace_cpu_t
*cpu_list
;
231 cpu_core_t
*cpu_core
; /* XXX TLB lockdown? */
238 * dtrace_CRED() can be called from probe context. We cannot simply call kauth_cred_get() since
239 * that function may try to resolve a lazy credential binding, which entails taking the proc_lock.
244 struct uthread
*uthread
= get_bsdthread_info(current_thread());
249 return uthread
->uu_ucred
; /* May return NOCRED which is defined to be 0 */
252 #define HAS_ALLPRIVS(cr) priv_isfullset(&CR_OEPRIV(cr))
253 #define HAS_PRIVILEGE(cr, pr) ((pr) == PRIV_ALL ? \
255 PRIV_ISASSERT(&CR_OEPRIV(cr), pr))
257 int PRIV_POLICY_CHOICE(void* cred
, int priv
, int all
)
259 #pragma unused(priv, all)
260 return kauth_cred_issuser(cred
); /* XXX TODO: How is this different from PRIV_POLICY_ONLY? */
264 PRIV_POLICY_ONLY(void *cr
, int priv
, int boolean
)
266 #pragma unused(priv, boolean)
267 return kauth_cred_issuser(cr
); /* XXX TODO: HAS_PRIVILEGE(cr, priv); */
270 /* XXX Get around const poisoning using structure assigns */
272 crgetgid(const cred_t
*cr
) { cred_t copy_cr
= *cr
; return kauth_cred_getgid(©_cr
); }
275 crgetuid(const cred_t
*cr
) { cred_t copy_cr
= *cr
; return kauth_cred_getuid(©_cr
); }
281 /* osfmk/kern/timer_call.h */
282 typedef void *timer_call_param_t
;
283 typedef void (*timer_call_func_t
)(
284 timer_call_param_t param0
,
285 timer_call_param_t param1
);
287 typedef struct timer_call
{
288 queue_chain_t q_link
;
290 timer_call_func_t func
;
291 timer_call_param_t param0
;
292 timer_call_param_t param1
;
293 decl_simple_lock_data(,lock
);
295 uint64_t soft_deadline
;
297 boolean_t async_dequeue
;
300 typedef struct timer_call
*timer_call_t
;
305 timer_call_func_t func
,
306 timer_call_param_t param0
);
311 timer_call_param_t param1
,
315 #ifndef TIMER_CALL_CRITICAL
316 #define TIMER_CALL_CRITICAL 0x1
317 #define TIMER_CALL_LOCAL 0x2
318 #endif /* TIMER_CALL_CRITICAL */
324 typedef struct wrap_timer_call
{
328 struct timer_call call
;
331 #define WAKEUP_REAPER 0x7FFFFFFFFFFFFFFFLL
332 #define NEARLY_FOREVER 0x7FFFFFFFFFFFFFFELL
335 _timer_call_apply_cyclic( void *ignore
, void *vTChdl
)
337 #pragma unused(ignore)
338 wrap_timer_call_t
*wrapTC
= (wrap_timer_call_t
*)vTChdl
;
340 (*(wrapTC
->hdlr
.cyh_func
))( wrapTC
->hdlr
.cyh_arg
);
342 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, mach_absolute_time(), &(wrapTC
->deadline
) );
343 timer_call_enter1( &(wrapTC
->call
), (void *)wrapTC
, wrapTC
->deadline
, TIMER_CALL_CRITICAL
| TIMER_CALL_LOCAL
);
345 /* Did timer_call_remove_cyclic request a wakeup call when this timer call was re-armed? */
346 if (wrapTC
->when
.cyt_interval
== WAKEUP_REAPER
)
347 thread_wakeup((event_t
)wrapTC
);
351 timer_call_add_cyclic(wrap_timer_call_t
*wrapTC
, cyc_handler_t
*handler
, cyc_time_t
*when
)
355 timer_call_setup( &(wrapTC
->call
), _timer_call_apply_cyclic
, NULL
);
356 wrapTC
->hdlr
= *handler
;
357 wrapTC
->when
= *when
;
359 nanoseconds_to_absolutetime( wrapTC
->when
.cyt_interval
, (uint64_t *)&wrapTC
->when
.cyt_interval
);
361 now
= mach_absolute_time();
362 wrapTC
->deadline
= now
;
364 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, now
, &(wrapTC
->deadline
) );
365 timer_call_enter1( &(wrapTC
->call
), (void *)wrapTC
, wrapTC
->deadline
, TIMER_CALL_CRITICAL
| TIMER_CALL_LOCAL
);
367 return (cyclic_id_t
)wrapTC
;
371 timer_call_remove_cyclic(cyclic_id_t cyclic
)
373 wrap_timer_call_t
*wrapTC
= (wrap_timer_call_t
*)cyclic
;
375 while (!timer_call_cancel(&(wrapTC
->call
))) {
376 int ret
= assert_wait(wrapTC
, THREAD_UNINT
);
377 ASSERT(ret
== THREAD_WAITING
);
379 wrapTC
->when
.cyt_interval
= WAKEUP_REAPER
;
381 ret
= thread_block(THREAD_CONTINUE_NULL
);
382 ASSERT(ret
== THREAD_AWAKENED
);
387 timer_call_get_cyclic_arg(cyclic_id_t cyclic
)
389 wrap_timer_call_t
*wrapTC
= (wrap_timer_call_t
*)cyclic
;
391 return (wrapTC
? wrapTC
->hdlr
.cyh_arg
: NULL
);
395 cyclic_timer_add(cyc_handler_t
*handler
, cyc_time_t
*when
)
397 wrap_timer_call_t
*wrapTC
= _MALLOC(sizeof(wrap_timer_call_t
), M_TEMP
, M_ZERO
| M_WAITOK
);
401 return timer_call_add_cyclic( wrapTC
, handler
, when
);
405 cyclic_timer_remove(cyclic_id_t cyclic
)
407 ASSERT( cyclic
!= CYCLIC_NONE
);
409 timer_call_remove_cyclic( cyclic
);
410 _FREE((void *)cyclic
, M_TEMP
);
414 _cyclic_add_omni(cyclic_id_list_t cyc_list
)
418 wrap_timer_call_t
*wrapTC
;
419 cyc_omni_handler_t
*omni
= (cyc_omni_handler_t
*)cyc_list
;
422 (omni
->cyo_online
)(omni
->cyo_arg
, CPU
, &cH
, &cT
);
424 t
= (char *)cyc_list
;
425 t
+= sizeof(cyc_omni_handler_t
);
426 cyc_list
= (cyclic_id_list_t
)(uintptr_t)t
;
428 t
+= sizeof(cyclic_id_t
)*NCPU
;
429 t
+= (sizeof(wrap_timer_call_t
))*cpu_number();
430 wrapTC
= (wrap_timer_call_t
*)(uintptr_t)t
;
432 cyc_list
[cpu_number()] = timer_call_add_cyclic(wrapTC
, &cH
, &cT
);
436 cyclic_add_omni(cyc_omni_handler_t
*omni
)
438 cyclic_id_list_t cyc_list
=
439 _MALLOC( (sizeof(wrap_timer_call_t
))*NCPU
+
440 sizeof(cyclic_id_t
)*NCPU
+
441 sizeof(cyc_omni_handler_t
), M_TEMP
, M_ZERO
| M_WAITOK
);
442 if (NULL
== cyc_list
)
443 return (cyclic_id_list_t
)CYCLIC_NONE
;
445 *(cyc_omni_handler_t
*)cyc_list
= *omni
;
446 dtrace_xcall(DTRACE_CPUALL
, (dtrace_xcall_t
)_cyclic_add_omni
, (void *)cyc_list
);
452 _cyclic_remove_omni(cyclic_id_list_t cyc_list
)
454 cyc_omni_handler_t
*omni
= (cyc_omni_handler_t
*)cyc_list
;
459 t
= (char *)cyc_list
;
460 t
+= sizeof(cyc_omni_handler_t
);
461 cyc_list
= (cyclic_id_list_t
)(uintptr_t)t
;
463 cid
= cyc_list
[cpu_number()];
464 oarg
= timer_call_get_cyclic_arg(cid
);
466 timer_call_remove_cyclic( cid
);
467 (omni
->cyo_offline
)(omni
->cyo_arg
, CPU
, oarg
);
471 cyclic_remove_omni(cyclic_id_list_t cyc_list
)
473 ASSERT( cyc_list
!= (cyclic_id_list_t
)CYCLIC_NONE
);
475 dtrace_xcall(DTRACE_CPUALL
, (dtrace_xcall_t
)_cyclic_remove_omni
, (void *)cyc_list
);
476 _FREE(cyc_list
, M_TEMP
);
479 typedef struct wrap_thread_call
{
484 } wrap_thread_call_t
;
487 * _cyclic_apply will run on some thread under kernel_task. That's OK for the
488 * cleaner and the deadman, but too distant in time and place for the profile provider.
491 _cyclic_apply( void *ignore
, void *vTChdl
)
493 #pragma unused(ignore)
494 wrap_thread_call_t
*wrapTC
= (wrap_thread_call_t
*)vTChdl
;
496 (*(wrapTC
->hdlr
.cyh_func
))( wrapTC
->hdlr
.cyh_arg
);
498 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, mach_absolute_time(), &(wrapTC
->deadline
) );
499 (void)thread_call_enter1_delayed( wrapTC
->TChdl
, (void *)wrapTC
, wrapTC
->deadline
);
501 /* Did cyclic_remove request a wakeup call when this thread call was re-armed? */
502 if (wrapTC
->when
.cyt_interval
== WAKEUP_REAPER
)
503 thread_wakeup((event_t
)wrapTC
);
507 cyclic_add(cyc_handler_t
*handler
, cyc_time_t
*when
)
511 wrap_thread_call_t
*wrapTC
= _MALLOC(sizeof(wrap_thread_call_t
), M_TEMP
, M_ZERO
| M_WAITOK
);
515 wrapTC
->TChdl
= thread_call_allocate( _cyclic_apply
, NULL
);
516 wrapTC
->hdlr
= *handler
;
517 wrapTC
->when
= *when
;
519 ASSERT(when
->cyt_when
== 0);
520 ASSERT(when
->cyt_interval
< WAKEUP_REAPER
);
522 nanoseconds_to_absolutetime(wrapTC
->when
.cyt_interval
, (uint64_t *)&wrapTC
->when
.cyt_interval
);
524 now
= mach_absolute_time();
525 wrapTC
->deadline
= now
;
527 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, now
, &(wrapTC
->deadline
) );
528 (void)thread_call_enter1_delayed( wrapTC
->TChdl
, (void *)wrapTC
, wrapTC
->deadline
);
530 return (cyclic_id_t
)wrapTC
;
534 noop_cyh_func(void * ignore
)
536 #pragma unused(ignore)
540 cyclic_remove(cyclic_id_t cyclic
)
542 wrap_thread_call_t
*wrapTC
= (wrap_thread_call_t
*)cyclic
;
544 ASSERT(cyclic
!= CYCLIC_NONE
);
546 while (!thread_call_cancel(wrapTC
->TChdl
)) {
547 int ret
= assert_wait(wrapTC
, THREAD_UNINT
);
548 ASSERT(ret
== THREAD_WAITING
);
550 wrapTC
->when
.cyt_interval
= WAKEUP_REAPER
;
552 ret
= thread_block(THREAD_CONTINUE_NULL
);
553 ASSERT(ret
== THREAD_AWAKENED
);
556 if (thread_call_free(wrapTC
->TChdl
))
557 _FREE(wrapTC
, M_TEMP
);
559 /* Gut this cyclic and move on ... */
560 wrapTC
->hdlr
.cyh_func
= noop_cyh_func
;
561 wrapTC
->when
.cyt_interval
= NEARLY_FOREVER
;
566 * timeout / untimeout (converted to dtrace_timeout / dtrace_untimeout due to name collision)
570 dtrace_timeout(void (*func
)(void *, void *), void* arg
, uint64_t nanos
)
573 thread_call_t call
= thread_call_allocate(func
, NULL
);
575 nanoseconds_to_absolutetime(nanos
, &nanos
);
578 * This method does not use clock_deadline_for_periodic_event() because it is a one-shot,
579 * and clock drift on later invocations is not a worry.
581 uint64_t deadline
= mach_absolute_time() + nanos
;
583 thread_call_enter_delayed(call
, deadline
);
592 ddi_report_dev(dev_info_t
*devi
)
597 #define NSOFT_STATES 32 /* XXX No more than 32 clients at a time, please. */
598 static void *soft
[NSOFT_STATES
];
601 ddi_soft_state_init(void **state_p
, size_t size
, size_t n_items
)
603 #pragma unused(n_items)
606 for (i
= 0; i
< NSOFT_STATES
; ++i
) soft
[i
] = _MALLOC(size
, M_TEMP
, M_ZERO
| M_WAITOK
);
607 *(size_t *)state_p
= size
;
612 ddi_soft_state_zalloc(void *state
, int item
)
614 #pragma unused(state)
615 if (item
< NSOFT_STATES
)
622 ddi_get_soft_state(void *state
, int item
)
624 #pragma unused(state)
625 ASSERT(item
< NSOFT_STATES
);
630 ddi_soft_state_free(void *state
, int item
)
632 ASSERT(item
< NSOFT_STATES
);
633 bzero( soft
[item
], (size_t)state
);
638 ddi_soft_state_fini(void **state_p
)
640 #pragma unused(state_p)
643 for (i
= 0; i
< NSOFT_STATES
; ++i
) _FREE( soft
[i
], M_TEMP
);
646 static unsigned int gRegisteredProps
= 0;
648 char name
[32]; /* enough for "dof-data-" + digits */
653 kern_return_t
_dtrace_register_anon_DOF(char *, uchar_t
*, uint_t
);
656 _dtrace_register_anon_DOF(char *name
, uchar_t
*data
, uint_t nelements
)
658 if (gRegisteredProps
< sizeof(gPropTable
)/sizeof(gPropTable
[0])) {
659 int *p
= (int *)_MALLOC(nelements
*sizeof(int), M_TEMP
, M_WAITOK
);
664 strlcpy(gPropTable
[gRegisteredProps
].name
, name
, sizeof(gPropTable
[0].name
));
665 gPropTable
[gRegisteredProps
].nelements
= nelements
;
666 gPropTable
[gRegisteredProps
].data
= p
;
668 while (nelements
-- > 0) {
669 *p
++ = (int)(*data
++);
680 ddi_prop_lookup_int_array(dev_t match_dev
, dev_info_t
*dip
, uint_t flags
,
681 const char *name
, int **data
, uint_t
*nelements
)
683 #pragma unused(match_dev,dip,flags)
685 for (i
= 0; i
< gRegisteredProps
; ++i
)
687 if (0 == strncmp(name
, gPropTable
[i
].name
,
688 sizeof(gPropTable
[i
].name
))) {
689 *data
= gPropTable
[i
].data
;
690 *nelements
= gPropTable
[i
].nelements
;
698 ddi_prop_free(void *buf
)
705 ddi_driver_major(dev_info_t
*devi
) { return (int)major(CAST_DOWN_EXPLICIT(int,devi
)); }
708 ddi_create_minor_node(dev_info_t
*dip
, const char *name
, int spec_type
,
709 minor_t minor_num
, const char *node_type
, int flag
)
711 #pragma unused(spec_type,node_type,flag)
712 dev_t dev
= makedev( ddi_driver_major(dip
), minor_num
);
714 if (NULL
== devfs_make_node( dev
, DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666, name
, 0 ))
721 ddi_remove_minor_node(dev_info_t
*dip
, char *name
)
723 #pragma unused(dip,name)
724 /* XXX called from dtrace_detach, so NOTREACHED for now. */
730 return (major_t
) major(d
);
736 return (minor_t
) minor(d
);
740 makedevice(major_t major
, minor_t minor
)
742 return makedev( major
, minor
);
745 int ddi_getprop(dev_t dev
, dev_info_t
*dip
, int flags
, const char *name
, int defvalue
)
747 #pragma unused(dev, dip, flags, name)
753 * Kernel Debug Interface
756 kdi_dtrace_set(kdi_dtrace_set_t ignore
)
758 #pragma unused(ignore)
759 return 0; /* Success */
762 extern void Debugger(const char*);
765 debug_enter(char *c
) { Debugger(c
); }
772 dt_kmem_alloc(size_t size
, int kmflag
)
774 #pragma unused(kmflag)
777 * We ignore the M_NOWAIT bit in kmflag (all of kmflag, in fact).
778 * Requests larger than 8K with M_NOWAIT fail in kalloc_canblock.
780 #if defined(DTRACE_MEMORY_ZONES)
781 return dtrace_alloc(size
);
788 dt_kmem_zalloc(size_t size
, int kmflag
)
790 #pragma unused(kmflag)
793 * We ignore the M_NOWAIT bit in kmflag (all of kmflag, in fact).
794 * Requests larger than 8K with M_NOWAIT fail in kalloc_canblock.
796 #if defined(DTRACE_MEMORY_ZONES)
797 void* buf
= dtrace_alloc(size
);
799 void* buf
= kalloc(size
);
811 dt_kmem_free(void *buf
, size_t size
)
815 * DTrace relies on this, its doing a lot of NULL frees.
816 * A null free causes the debug builds to panic.
818 if (buf
== NULL
) return;
822 #if defined(DTRACE_MEMORY_ZONES)
823 dtrace_free(buf
, size
);
832 * aligned kmem allocator
833 * align should be a power of two
836 void* dt_kmem_alloc_aligned(size_t size
, size_t align
, int kmflag
)
842 buf
= dt_kmem_alloc(align
+ sizeof(void*) + size
, kmflag
);
848 p
+= sizeof(void*); /* now we have enough room to store the backup */
849 p
= P2ROUNDUP(p
, align
); /* and now we're aligned */
851 buf_backup
= (void**)(p
- sizeof(void*));
852 *buf_backup
= buf
; /* back up the address we need to free */
857 void* dt_kmem_zalloc_aligned(size_t size
, size_t align
, int kmflag
)
861 buf
= dt_kmem_alloc_aligned(size
, align
, kmflag
);
871 void dt_kmem_free_aligned(void* buf
, size_t size
)
879 buf_backup
= (void**)(p
);
881 dt_kmem_free(*buf_backup
, size
+ ((char*)buf
- (char*)*buf_backup
));
885 * dtrace wants to manage just a single block: dtrace_state_percpu_t * NCPU, and
886 * doesn't specify constructor, destructor, or reclaim methods.
887 * At present, it always zeroes the block it obtains from kmem_cache_alloc().
888 * We'll manage this constricted use of kmem_cache with ordinary _MALLOC and _FREE.
892 const char *name
, /* descriptive name for this cache */
893 size_t bufsize
, /* size of the objects it manages */
894 size_t align
, /* required object alignment */
895 int (*constructor
)(void *, void *, int), /* object constructor */
896 void (*destructor
)(void *, void *), /* object destructor */
897 void (*reclaim
)(void *), /* memory reclaim callback */
898 void *private, /* pass-thru arg for constr/destr/reclaim */
899 vmem_t
*vmp
, /* vmem source for slab allocation */
900 int cflags
) /* cache creation flags */
902 #pragma unused(name,align,constructor,destructor,reclaim,private,vmp,cflags)
903 return (kmem_cache_t
*)bufsize
; /* A cookie that tracks the single object size. */
907 kmem_cache_alloc(kmem_cache_t
*cp
, int kmflag
)
909 #pragma unused(kmflag)
910 size_t bufsize
= (size_t)cp
;
911 return (void *)_MALLOC(bufsize
, M_TEMP
, M_WAITOK
);
915 kmem_cache_free(kmem_cache_t
*cp
, void *buf
)
922 kmem_cache_destroy(kmem_cache_t
*cp
)
930 extern void thread_call_setup(thread_call_t
, thread_call_func_t
, thread_call_param_t
); /* XXX MACH_KERNEL_PRIVATE */
933 _taskq_apply( task_func_t func
, thread_call_param_t arg
)
939 taskq_create(const char *name
, int nthreads
, pri_t pri
, int minalloc
,
940 int maxalloc
, uint_t flags
)
942 #pragma unused(name,nthreads,pri,minalloc,maxalloc,flags)
944 return (taskq_t
*)thread_call_allocate( (thread_call_func_t
)_taskq_apply
, NULL
);
948 taskq_dispatch(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
)
950 #pragma unused(flags)
951 thread_call_setup( (thread_call_t
) tq
, (thread_call_func_t
)_taskq_apply
, (thread_call_param_t
)func
);
952 thread_call_enter1( (thread_call_t
) tq
, (thread_call_param_t
)arg
);
953 return (taskqid_t
) tq
/* for lack of anything better */;
957 taskq_destroy(taskq_t
*tq
)
959 thread_call_cancel( (thread_call_t
) tq
);
960 thread_call_free( (thread_call_t
) tq
);
966 * vmem (Solaris "slab" allocator) used by DTrace solely to hand out resource ids
968 typedef unsigned int u_daddr_t
;
971 /* By passing around blist *handles*, the underlying blist can be resized as needed. */
977 vmem_create(const char *name
, void *base
, size_t size
, size_t quantum
, void *ignore5
,
978 void *ignore6
, vmem_t
*source
, size_t qcache_max
, int vmflag
)
980 #pragma unused(name,quantum,ignore5,ignore6,source,qcache_max,vmflag)
982 struct blist_hdl
*p
= _MALLOC(sizeof(struct blist_hdl
), M_TEMP
, M_WAITOK
);
984 ASSERT(quantum
== 1);
985 ASSERT(NULL
== ignore5
);
986 ASSERT(NULL
== ignore6
);
987 ASSERT(NULL
== source
);
988 ASSERT(0 == qcache_max
);
989 ASSERT(vmflag
& VMC_IDENTIFIER
);
991 size
= MIN(128, size
); /* Clamp to 128 initially, since the underlying data structure is pre-allocated */
993 p
->blist
= bl
= blist_create( size
);
994 blist_free(bl
, 0, size
);
995 if (base
) blist_alloc( bl
, (daddr_t
)(uintptr_t)base
); /* Chomp off initial ID(s) */
1001 vmem_alloc(vmem_t
*vmp
, size_t size
, int vmflag
)
1003 #pragma unused(vmflag)
1004 struct blist_hdl
*q
= (struct blist_hdl
*)vmp
;
1005 blist_t bl
= q
->blist
;
1008 p
= blist_alloc(bl
, (daddr_t
)size
);
1010 if ((daddr_t
)-1 == p
) {
1011 blist_resize(&bl
, (bl
->bl_blocks
) << 1, 1);
1013 p
= blist_alloc(bl
, (daddr_t
)size
);
1014 if ((daddr_t
)-1 == p
)
1015 panic("vmem_alloc: failure after blist_resize!");
1018 return (void *)(uintptr_t)p
;
1022 vmem_free(vmem_t
*vmp
, void *vaddr
, size_t size
)
1024 struct blist_hdl
*p
= (struct blist_hdl
*)vmp
;
1026 blist_free( p
->blist
, (daddr_t
)(uintptr_t)vaddr
, (daddr_t
)size
);
1030 vmem_destroy(vmem_t
*vmp
)
1032 struct blist_hdl
*p
= (struct blist_hdl
*)vmp
;
1034 blist_destroy( p
->blist
);
1035 _FREE( p
, sizeof(struct blist_hdl
) );
1043 * dtrace_gethrestime() provides the "walltimestamp", a value that is anchored at
1044 * January 1, 1970. Because it can be called from probe context, it must take no locks.
1048 dtrace_gethrestime(void)
1051 clock_nsec_t nanosecs
;
1052 uint64_t secs64
, ns64
;
1054 clock_get_calendar_nanotime_nowait(&secs
, &nanosecs
);
1055 secs64
= (uint64_t)secs
;
1056 ns64
= (uint64_t)nanosecs
;
1058 ns64
= ns64
+ (secs64
* 1000000000LL);
1063 * dtrace_gethrtime() provides high-resolution timestamps with machine-dependent origin.
1064 * Hence its primary use is to specify intervals.
1068 dtrace_abs_to_nano(uint64_t elapsed
)
1070 static mach_timebase_info_data_t sTimebaseInfo
= { 0, 0 };
1073 * If this is the first time we've run, get the timebase.
1074 * We can use denom == 0 to indicate that sTimebaseInfo is
1075 * uninitialised because it makes no sense to have a zero
1076 * denominator in a fraction.
1079 if ( sTimebaseInfo
.denom
== 0 ) {
1080 (void) clock_timebase_info(&sTimebaseInfo
);
1084 * Convert to nanoseconds.
1085 * return (elapsed * (uint64_t)sTimebaseInfo.numer)/(uint64_t)sTimebaseInfo.denom;
1087 * Provided the final result is representable in 64 bits the following maneuver will
1088 * deliver that result without intermediate overflow.
1090 if (sTimebaseInfo
.denom
== sTimebaseInfo
.numer
)
1092 else if (sTimebaseInfo
.denom
== 1)
1093 return elapsed
* (uint64_t)sTimebaseInfo
.numer
;
1095 /* Decompose elapsed = eta32 * 2^32 + eps32: */
1096 uint64_t eta32
= elapsed
>> 32;
1097 uint64_t eps32
= elapsed
& 0x00000000ffffffffLL
;
1099 uint32_t numer
= sTimebaseInfo
.numer
, denom
= sTimebaseInfo
.denom
;
1101 /* Form product of elapsed64 (decomposed) and numer: */
1102 uint64_t mu64
= numer
* eta32
;
1103 uint64_t lambda64
= numer
* eps32
;
1105 /* Divide the constituents by denom: */
1106 uint64_t q32
= mu64
/denom
;
1107 uint64_t r32
= mu64
- (q32
* denom
); /* mu64 % denom */
1109 return (q32
<< 32) + ((r32
<< 32) + lambda64
)/denom
;
1114 dtrace_gethrtime(void)
1116 static uint64_t start
= 0;
1119 start
= mach_absolute_time();
1121 return dtrace_abs_to_nano(mach_absolute_time() - start
);
1125 * Atomicity and synchronization
1128 dtrace_cas32(uint32_t *target
, uint32_t cmp
, uint32_t new)
1130 if (OSCompareAndSwap( (UInt32
)cmp
, (UInt32
)new, (volatile UInt32
*)target
))
1133 return ~cmp
; /* Must return something *other* than cmp */
1137 dtrace_casptr(void *target
, void *cmp
, void *new)
1139 if (OSCompareAndSwapPtr( cmp
, new, (void**)target
))
1142 return (void *)(~(uintptr_t)cmp
); /* Must return something *other* than cmp */
1146 * Interrupt manipulation
1149 dtrace_interrupt_disable(void)
1151 return (dtrace_icookie_t
)ml_set_interrupts_enabled(FALSE
);
1155 dtrace_interrupt_enable(dtrace_icookie_t reenable
)
1157 (void)ml_set_interrupts_enabled((boolean_t
)reenable
);
1164 dtrace_sync_func(void) {}
1167 * dtrace_sync() is not called from probe context.
1172 dtrace_xcall(DTRACE_CPUALL
, (dtrace_xcall_t
)dtrace_sync_func
, NULL
);
1176 * The dtrace_copyin/out/instr and dtrace_fuword* routines can be called from probe context.
1179 extern kern_return_t
dtrace_copyio_preflight(addr64_t
);
1180 extern kern_return_t
dtrace_copyio_postflight(addr64_t
);
1183 dtrace_copycheck(user_addr_t uaddr
, uintptr_t kaddr
, size_t size
)
1185 #pragma unused(kaddr)
1187 vm_offset_t recover
= dtrace_set_thread_recover( current_thread(), 0 ); /* Snare any extant recovery point. */
1188 dtrace_set_thread_recover( current_thread(), recover
); /* Put it back. We *must not* re-enter and overwrite. */
1190 ASSERT(kaddr
+ size
>= kaddr
);
1192 if (ml_at_interrupt_context() || /* Avoid possible copyio page fault on int stack, which panics! */
1193 0 != recover
|| /* Avoid reentrancy into copyio facility. */
1194 uaddr
+ size
< uaddr
|| /* Avoid address wrap. */
1195 KERN_FAILURE
== dtrace_copyio_preflight(uaddr
)) /* Machine specific setup/constraints. */
1197 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1198 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1205 dtrace_copyin(user_addr_t src
, uintptr_t dst
, size_t len
, volatile uint16_t *flags
)
1207 #pragma unused(flags)
1209 if (dtrace_copycheck( src
, dst
, len
)) {
1210 if (copyin((const user_addr_t
)src
, (char *)dst
, (vm_size_t
)len
)) {
1211 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1212 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= src
;
1214 dtrace_copyio_postflight(src
);
1219 dtrace_copyinstr(user_addr_t src
, uintptr_t dst
, size_t len
, volatile uint16_t *flags
)
1221 #pragma unused(flags)
1225 if (dtrace_copycheck( src
, dst
, len
)) {
1226 /* copyin as many as 'len' bytes. */
1227 int error
= copyinstr((const user_addr_t
)src
, (char *)dst
, (vm_size_t
)len
, &actual
);
1230 * ENAMETOOLONG is returned when 'len' bytes have been copied in but the NUL terminator was
1231 * not encountered. That does not require raising CPU_DTRACE_BADADDR, and we press on.
1232 * Note that we do *not* stuff a NUL terminator when returning ENAMETOOLONG, that's left
1235 if (error
&& error
!= ENAMETOOLONG
) {
1236 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1237 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= src
;
1239 dtrace_copyio_postflight(src
);
1244 dtrace_copyout(uintptr_t src
, user_addr_t dst
, size_t len
, volatile uint16_t *flags
)
1246 #pragma unused(flags)
1248 if (dtrace_copycheck( dst
, src
, len
)) {
1249 if (copyout((const void *)src
, dst
, (vm_size_t
)len
)) {
1250 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1251 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= dst
;
1253 dtrace_copyio_postflight(dst
);
1258 dtrace_copyoutstr(uintptr_t src
, user_addr_t dst
, size_t len
, volatile uint16_t *flags
)
1260 #pragma unused(flags)
1264 if (dtrace_copycheck( dst
, src
, len
)) {
1267 * ENAMETOOLONG is returned when 'len' bytes have been copied out but the NUL terminator was
1268 * not encountered. We raise CPU_DTRACE_BADADDR in that case.
1269 * Note that we do *not* stuff a NUL terminator when returning ENAMETOOLONG, that's left
1272 if (copyoutstr((const void *)src
, dst
, (size_t)len
, &actual
)) {
1273 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1274 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= dst
;
1276 dtrace_copyio_postflight(dst
);
1281 dtrace_fuword8(user_addr_t uaddr
)
1285 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1286 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1287 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1288 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1289 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1291 dtrace_copyio_postflight(uaddr
);
1293 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1299 dtrace_fuword16(user_addr_t uaddr
)
1303 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1304 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1305 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1306 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1307 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1309 dtrace_copyio_postflight(uaddr
);
1311 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1317 dtrace_fuword32(user_addr_t uaddr
)
1321 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1322 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1323 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1324 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1325 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1327 dtrace_copyio_postflight(uaddr
);
1329 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1335 dtrace_fuword64(user_addr_t uaddr
)
1339 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1340 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1341 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1342 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1343 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1345 dtrace_copyio_postflight(uaddr
);
1347 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1353 * Emulation of Solaris fuword / suword
1354 * Called from the fasttrap provider, so the use of copyin/out requires fewer safegaurds.
1358 fuword8(user_addr_t uaddr
, uint8_t *value
)
1360 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint8_t)) != 0) {
1368 fuword16(user_addr_t uaddr
, uint16_t *value
)
1370 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint16_t)) != 0) {
1378 fuword32(user_addr_t uaddr
, uint32_t *value
)
1380 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint32_t)) != 0) {
1388 fuword64(user_addr_t uaddr
, uint64_t *value
)
1390 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint64_t)) != 0) {
1398 fuword8_noerr(user_addr_t uaddr
, uint8_t *value
)
1400 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint8_t))) {
1406 fuword16_noerr(user_addr_t uaddr
, uint16_t *value
)
1408 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint16_t))) {
1414 fuword32_noerr(user_addr_t uaddr
, uint32_t *value
)
1416 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint32_t))) {
1422 fuword64_noerr(user_addr_t uaddr
, uint64_t *value
)
1424 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint64_t))) {
1430 suword64(user_addr_t addr
, uint64_t value
)
1432 if (copyout((const void *)&value
, addr
, sizeof(value
)) != 0) {
1440 suword32(user_addr_t addr
, uint32_t value
)
1442 if (copyout((const void *)&value
, addr
, sizeof(value
)) != 0) {
1450 suword16(user_addr_t addr
, uint16_t value
)
1452 if (copyout((const void *)&value
, addr
, sizeof(value
)) != 0) {
1460 suword8(user_addr_t addr
, uint8_t value
)
1462 if (copyout((const void *)&value
, addr
, sizeof(value
)) != 0) {
1473 extern boolean_t
dtrace_tally_fault(user_addr_t
);
1476 dtrace_tally_fault(user_addr_t uaddr
)
1478 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1479 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1480 return( DTRACE_CPUFLAG_ISSET(CPU_DTRACE_NOFAULT
) ? TRUE
: FALSE
);
1484 extern int prf(const char *, va_list, int, struct tty
*); /* bsd/kern/subr_prf.h */
1487 vuprintf(const char *format
, va_list ap
)
1489 return prf(format
, ap
, TOTTY
, NULL
);
1492 /* Not called from probe context */
1493 void cmn_err( int level
, const char *format
, ... )
1495 #pragma unused(level)
1498 va_start(alist
, format
);
1499 vuprintf(format
, alist
);
1506 * 2002-01-24 gvdl Initial implementation of strstr
1509 __private_extern__
const char *
1510 strstr(const char *in
, const char *str
)
1517 return (const char *) in
; // Trivial empty string case
1528 } while (strncmp(in
, str
, len
) != 0);
1530 return (const char *) (in
- 1);
1537 dtrace_caller(int ignore
)
1539 #pragma unused(ignore)
1540 return -1; /* Just as in Solaris dtrace_asm.s */
1544 dtrace_getstackdepth(int aframes
)
1546 struct frame
*fp
= (struct frame
*)__builtin_frame_address(0);
1547 struct frame
*nextfp
, *minfp
, *stacktop
;
1551 if ((on_intr
= CPU_ON_INTR(CPU
)) != 0)
1552 stacktop
= (struct frame
*)dtrace_get_cpu_int_stack_top();
1554 stacktop
= (struct frame
*)(dtrace_get_kernel_stack(current_thread()) + kernel_stack_size
);
1563 nextfp
= *(struct frame
**)fp
;
1565 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
1568 * Hop from interrupt stack to thread stack.
1570 vm_offset_t kstack_base
= dtrace_get_kernel_stack(current_thread());
1572 minfp
= (struct frame
*)kstack_base
;
1573 stacktop
= (struct frame
*)(kstack_base
+ kernel_stack_size
);
1585 if (depth
<= aframes
)
1588 return (depth
- aframes
);
1595 dtrace_vtime_enable(void) {}
1598 dtrace_vtime_disable(void) {}
1600 #else /* else ! CONFIG_DTRACE */
1602 #include <sys/types.h>
1603 #include <mach/vm_types.h>
1604 #include <mach/kmod.h>
1607 * This exists to prevent build errors when dtrace is unconfigured.
1610 kern_return_t
_dtrace_register_anon_DOF(char *, unsigned char *, uint32_t);
1612 kern_return_t
_dtrace_register_anon_DOF(char *arg1
, unsigned char *arg2
, uint32_t arg3
) {
1613 #pragma unused(arg1, arg2, arg3)
1615 return KERN_FAILURE
;
1618 #endif /* CONFIG_DTRACE */