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
;
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); */
271 crgetgid(const cred_t
*cr
) { return cr
->cr_groups
[0]; }
274 crgetuid(const cred_t
*cr
) { return cr
->cr_uid
; }
280 /* osfmk/kern/timer_call.h */
281 typedef void *call_entry_param_t
;
282 typedef void (*call_entry_func_t
)(
283 call_entry_param_t param0
,
284 call_entry_param_t param1
);
286 typedef struct call_entry
{
287 queue_chain_t q_link
;
288 call_entry_func_t func
;
289 call_entry_param_t param0
;
290 call_entry_param_t param1
;
299 typedef struct call_entry
*timer_call_t
;
300 typedef void *timer_call_param_t
;
301 typedef void (*timer_call_func_t
)(
302 timer_call_param_t param0
,
303 timer_call_param_t param1
);
308 timer_call_func_t func
,
309 timer_call_param_t param0
);
314 timer_call_param_t param1
,
321 typedef struct wrap_timer_call
{
325 struct call_entry call
;
328 #define WAKEUP_REAPER 0x7FFFFFFFFFFFFFFFLL
329 #define NEARLY_FOREVER 0x7FFFFFFFFFFFFFFELL
332 _timer_call_apply_cyclic( void *ignore
, void *vTChdl
)
334 #pragma unused(ignore)
335 wrap_timer_call_t
*wrapTC
= (wrap_timer_call_t
*)vTChdl
;
337 (*(wrapTC
->hdlr
.cyh_func
))( wrapTC
->hdlr
.cyh_arg
);
339 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, mach_absolute_time(), &(wrapTC
->deadline
) );
340 timer_call_enter1( &(wrapTC
->call
), (void *)wrapTC
, wrapTC
->deadline
);
342 /* Did timer_call_remove_cyclic request a wakeup call when this timer call was re-armed? */
343 if (wrapTC
->when
.cyt_interval
== WAKEUP_REAPER
)
344 thread_wakeup((event_t
)wrapTC
);
348 timer_call_add_cyclic(wrap_timer_call_t
*wrapTC
, cyc_handler_t
*handler
, cyc_time_t
*when
)
352 timer_call_setup( &(wrapTC
->call
), _timer_call_apply_cyclic
, NULL
);
353 wrapTC
->hdlr
= *handler
;
354 wrapTC
->when
= *when
;
356 nanoseconds_to_absolutetime( wrapTC
->when
.cyt_interval
, (uint64_t *)&wrapTC
->when
.cyt_interval
);
358 now
= mach_absolute_time();
359 wrapTC
->deadline
= now
;
361 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, now
, &(wrapTC
->deadline
) );
362 timer_call_enter1( &(wrapTC
->call
), (void *)wrapTC
, wrapTC
->deadline
);
364 return (cyclic_id_t
)wrapTC
;
368 timer_call_remove_cyclic(cyclic_id_t cyclic
)
370 wrap_timer_call_t
*wrapTC
= (wrap_timer_call_t
*)cyclic
;
372 while (!timer_call_cancel(&(wrapTC
->call
))) {
373 int ret
= assert_wait(wrapTC
, THREAD_UNINT
);
374 ASSERT(ret
== THREAD_WAITING
);
376 wrapTC
->when
.cyt_interval
= WAKEUP_REAPER
;
378 ret
= thread_block(THREAD_CONTINUE_NULL
);
379 ASSERT(ret
== THREAD_AWAKENED
);
384 timer_call_get_cyclic_arg(cyclic_id_t cyclic
)
386 wrap_timer_call_t
*wrapTC
= (wrap_timer_call_t
*)cyclic
;
388 return (wrapTC
? wrapTC
->hdlr
.cyh_arg
: NULL
);
392 cyclic_timer_add(cyc_handler_t
*handler
, cyc_time_t
*when
)
394 wrap_timer_call_t
*wrapTC
= _MALLOC(sizeof(wrap_timer_call_t
), M_TEMP
, M_ZERO
| M_WAITOK
);
398 return timer_call_add_cyclic( wrapTC
, handler
, when
);
402 cyclic_timer_remove(cyclic_id_t cyclic
)
404 ASSERT( cyclic
!= CYCLIC_NONE
);
406 timer_call_remove_cyclic( cyclic
);
407 _FREE((void *)cyclic
, M_TEMP
);
411 _cyclic_add_omni(cyclic_id_list_t cyc_list
)
415 wrap_timer_call_t
*wrapTC
;
416 cyc_omni_handler_t
*omni
= (cyc_omni_handler_t
*)cyc_list
;
419 (omni
->cyo_online
)(omni
->cyo_arg
, CPU
, &cH
, &cT
);
421 t
= (char *)cyc_list
;
422 t
+= sizeof(cyc_omni_handler_t
);
423 cyc_list
= (cyclic_id_list_t
)(uintptr_t)t
;
425 t
+= sizeof(cyclic_id_t
)*NCPU
;
426 t
+= (sizeof(wrap_timer_call_t
))*cpu_number();
427 wrapTC
= (wrap_timer_call_t
*)(uintptr_t)t
;
429 cyc_list
[cpu_number()] = timer_call_add_cyclic(wrapTC
, &cH
, &cT
);
433 cyclic_add_omni(cyc_omni_handler_t
*omni
)
435 cyclic_id_list_t cyc_list
=
436 _MALLOC( (sizeof(wrap_timer_call_t
))*NCPU
+
437 sizeof(cyclic_id_t
)*NCPU
+
438 sizeof(cyc_omni_handler_t
), M_TEMP
, M_ZERO
| M_WAITOK
);
439 if (NULL
== cyc_list
)
440 return (cyclic_id_list_t
)CYCLIC_NONE
;
442 *(cyc_omni_handler_t
*)cyc_list
= *omni
;
443 dtrace_xcall(DTRACE_CPUALL
, (dtrace_xcall_t
)_cyclic_add_omni
, (void *)cyc_list
);
449 _cyclic_remove_omni(cyclic_id_list_t cyc_list
)
451 cyc_omni_handler_t
*omni
= (cyc_omni_handler_t
*)cyc_list
;
456 t
= (char *)cyc_list
;
457 t
+= sizeof(cyc_omni_handler_t
);
458 cyc_list
= (cyclic_id_list_t
)(uintptr_t)t
;
460 cid
= cyc_list
[cpu_number()];
461 oarg
= timer_call_get_cyclic_arg(cid
);
463 timer_call_remove_cyclic( cid
);
464 (omni
->cyo_offline
)(omni
->cyo_arg
, CPU
, oarg
);
468 cyclic_remove_omni(cyclic_id_list_t cyc_list
)
470 ASSERT( cyc_list
!= (cyclic_id_list_t
)CYCLIC_NONE
);
472 dtrace_xcall(DTRACE_CPUALL
, (dtrace_xcall_t
)_cyclic_remove_omni
, (void *)cyc_list
);
473 _FREE(cyc_list
, M_TEMP
);
476 typedef struct wrap_thread_call
{
481 } wrap_thread_call_t
;
484 * _cyclic_apply will run on some thread under kernel_task. That's OK for the
485 * cleaner and the deadman, but too distant in time and place for the profile provider.
488 _cyclic_apply( void *ignore
, void *vTChdl
)
490 #pragma unused(ignore)
491 wrap_thread_call_t
*wrapTC
= (wrap_thread_call_t
*)vTChdl
;
493 (*(wrapTC
->hdlr
.cyh_func
))( wrapTC
->hdlr
.cyh_arg
);
495 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, mach_absolute_time(), &(wrapTC
->deadline
) );
496 (void)thread_call_enter1_delayed( wrapTC
->TChdl
, (void *)wrapTC
, wrapTC
->deadline
);
498 /* Did cyclic_remove request a wakeup call when this thread call was re-armed? */
499 if (wrapTC
->when
.cyt_interval
== WAKEUP_REAPER
)
500 thread_wakeup((event_t
)wrapTC
);
504 cyclic_add(cyc_handler_t
*handler
, cyc_time_t
*when
)
508 wrap_thread_call_t
*wrapTC
= _MALLOC(sizeof(wrap_thread_call_t
), M_TEMP
, M_ZERO
| M_WAITOK
);
512 wrapTC
->TChdl
= thread_call_allocate( _cyclic_apply
, NULL
);
513 wrapTC
->hdlr
= *handler
;
514 wrapTC
->when
= *when
;
516 ASSERT(when
->cyt_when
== 0);
517 ASSERT(when
->cyt_interval
< WAKEUP_REAPER
);
519 nanoseconds_to_absolutetime(wrapTC
->when
.cyt_interval
, (uint64_t *)&wrapTC
->when
.cyt_interval
);
521 now
= mach_absolute_time();
522 wrapTC
->deadline
= now
;
524 clock_deadline_for_periodic_event( wrapTC
->when
.cyt_interval
, now
, &(wrapTC
->deadline
) );
525 (void)thread_call_enter1_delayed( wrapTC
->TChdl
, (void *)wrapTC
, wrapTC
->deadline
);
527 return (cyclic_id_t
)wrapTC
;
531 noop_cyh_func(void * ignore
)
533 #pragma unused(ignore)
537 cyclic_remove(cyclic_id_t cyclic
)
539 wrap_thread_call_t
*wrapTC
= (wrap_thread_call_t
*)cyclic
;
541 ASSERT(cyclic
!= CYCLIC_NONE
);
543 while (!thread_call_cancel(wrapTC
->TChdl
)) {
544 int ret
= assert_wait(wrapTC
, THREAD_UNINT
);
545 ASSERT(ret
== THREAD_WAITING
);
547 wrapTC
->when
.cyt_interval
= WAKEUP_REAPER
;
549 ret
= thread_block(THREAD_CONTINUE_NULL
);
550 ASSERT(ret
== THREAD_AWAKENED
);
553 if (thread_call_free(wrapTC
->TChdl
))
554 _FREE(wrapTC
, M_TEMP
);
556 /* Gut this cyclic and move on ... */
557 wrapTC
->hdlr
.cyh_func
= noop_cyh_func
;
558 wrapTC
->when
.cyt_interval
= NEARLY_FOREVER
;
563 * timeout / untimeout (converted to dtrace_timeout / dtrace_untimeout due to name collision)
567 dtrace_timeout(void (*func
)(void *, void *), void* arg
, uint64_t nanos
)
570 thread_call_t call
= thread_call_allocate(func
, NULL
);
572 nanoseconds_to_absolutetime(nanos
, &nanos
);
575 * This method does not use clock_deadline_for_periodic_event() because it is a one-shot,
576 * and clock drift on later invocations is not a worry.
578 uint64_t deadline
= mach_absolute_time() + nanos
;
580 thread_call_enter_delayed(call
, deadline
);
589 ddi_report_dev(dev_info_t
*devi
)
594 #define NSOFT_STATES 32 /* XXX No more than 32 clients at a time, please. */
595 static void *soft
[NSOFT_STATES
];
598 ddi_soft_state_init(void **state_p
, size_t size
, size_t n_items
)
600 #pragma unused(n_items)
603 for (i
= 0; i
< NSOFT_STATES
; ++i
) soft
[i
] = _MALLOC(size
, M_TEMP
, M_ZERO
| M_WAITOK
);
604 *(size_t *)state_p
= size
;
609 ddi_soft_state_zalloc(void *state
, int item
)
611 #pragma unused(state)
612 if (item
< NSOFT_STATES
)
619 ddi_get_soft_state(void *state
, int item
)
621 #pragma unused(state)
622 ASSERT(item
< NSOFT_STATES
);
627 ddi_soft_state_free(void *state
, int item
)
629 ASSERT(item
< NSOFT_STATES
);
630 bzero( soft
[item
], (size_t)state
);
635 ddi_soft_state_fini(void **state_p
)
637 #pragma unused(state_p)
640 for (i
= 0; i
< NSOFT_STATES
; ++i
) _FREE( soft
[i
], M_TEMP
);
643 static unsigned int gRegisteredProps
= 0;
645 char name
[32]; /* enough for "dof-data-" + digits */
650 kern_return_t
_dtrace_register_anon_DOF(char *, uchar_t
*, uint_t
);
653 _dtrace_register_anon_DOF(char *name
, uchar_t
*data
, uint_t nelements
)
655 if (gRegisteredProps
< sizeof(gPropTable
)/sizeof(gPropTable
[0])) {
656 int *p
= (int *)_MALLOC(nelements
*sizeof(int), M_TEMP
, M_WAITOK
);
661 strlcpy(gPropTable
[gRegisteredProps
].name
, name
, sizeof(gPropTable
[0].name
));
662 gPropTable
[gRegisteredProps
].nelements
= nelements
;
663 gPropTable
[gRegisteredProps
].data
= p
;
665 while (nelements
-- > 0) {
666 *p
++ = (int)(*data
++);
677 ddi_prop_lookup_int_array(dev_t match_dev
, dev_info_t
*dip
, uint_t flags
,
678 const char *name
, int **data
, uint_t
*nelements
)
680 #pragma unused(match_dev,dip,flags)
682 for (i
= 0; i
< gRegisteredProps
; ++i
)
684 if (0 == strncmp(name
, gPropTable
[i
].name
,
685 sizeof(gPropTable
[i
].name
))) {
686 *data
= gPropTable
[i
].data
;
687 *nelements
= gPropTable
[i
].nelements
;
695 ddi_prop_free(void *buf
)
702 ddi_driver_major(dev_info_t
*devi
) { return (int)major(CAST_DOWN_EXPLICIT(int,devi
)); }
705 ddi_create_minor_node(dev_info_t
*dip
, const char *name
, int spec_type
,
706 minor_t minor_num
, const char *node_type
, int flag
)
708 #pragma unused(spec_type,node_type,flag)
709 dev_t dev
= makedev( ddi_driver_major(dip
), minor_num
);
711 if (NULL
== devfs_make_node( dev
, DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666, name
, 0 ))
718 ddi_remove_minor_node(dev_info_t
*dip
, char *name
)
720 #pragma unused(dip,name)
721 /* XXX called from dtrace_detach, so NOTREACHED for now. */
727 return (major_t
) major(d
);
733 return (minor_t
) minor(d
);
737 makedevice(major_t major
, minor_t minor
)
739 return makedev( major
, minor
);
742 int ddi_getprop(dev_t dev
, dev_info_t
*dip
, int flags
, const char *name
, int defvalue
)
744 #pragma unused(dev, dip, flags, name)
750 * Kernel Debug Interface
753 kdi_dtrace_set(kdi_dtrace_set_t ignore
)
755 #pragma unused(ignore)
756 return 0; /* Success */
759 extern void Debugger(const char*);
762 debug_enter(char *c
) { Debugger(c
); }
769 dt_kmem_alloc(size_t size
, int kmflag
)
771 #pragma unused(kmflag)
774 * We ignore the M_NOWAIT bit in kmflag (all of kmflag, in fact).
775 * Requests larger than 8K with M_NOWAIT fail in kalloc_canblock.
777 #if defined(DTRACE_MEMORY_ZONES)
778 return dtrace_alloc(size
);
785 dt_kmem_zalloc(size_t size
, int kmflag
)
787 #pragma unused(kmflag)
790 * We ignore the M_NOWAIT bit in kmflag (all of kmflag, in fact).
791 * Requests larger than 8K with M_NOWAIT fail in kalloc_canblock.
793 #if defined(DTRACE_MEMORY_ZONES)
794 void* buf
= dtrace_alloc(size
);
796 void* buf
= kalloc(size
);
808 dt_kmem_free(void *buf
, size_t size
)
812 * DTrace relies on this, its doing a lot of NULL frees.
813 * A null free causes the debug builds to panic.
815 if (buf
== NULL
) return;
819 #if defined(DTRACE_MEMORY_ZONES)
820 dtrace_free(buf
, size
);
829 * aligned kmem allocator
830 * align should be a power of two
833 void* dt_kmem_alloc_aligned(size_t size
, size_t align
, int kmflag
)
839 buf
= dt_kmem_alloc(align
+ sizeof(void*) + size
, kmflag
);
845 p
+= sizeof(void*); /* now we have enough room to store the backup */
846 p
= P2ROUNDUP(p
, align
); /* and now we're aligned */
848 buf_backup
= (void**)(p
- sizeof(void*));
849 *buf_backup
= buf
; /* back up the address we need to free */
854 void* dt_kmem_zalloc_aligned(size_t size
, size_t align
, int kmflag
)
858 buf
= dt_kmem_alloc_aligned(size
, align
, kmflag
);
868 void dt_kmem_free_aligned(void* buf
, size_t size
)
876 buf_backup
= (void**)(p
);
878 dt_kmem_free(*buf_backup
, size
+ ((char*)buf
- (char*)*buf_backup
));
882 * dtrace wants to manage just a single block: dtrace_state_percpu_t * NCPU, and
883 * doesn't specify constructor, destructor, or reclaim methods.
884 * At present, it always zeroes the block it obtains from kmem_cache_alloc().
885 * We'll manage this constricted use of kmem_cache with ordinary _MALLOC and _FREE.
889 const char *name
, /* descriptive name for this cache */
890 size_t bufsize
, /* size of the objects it manages */
891 size_t align
, /* required object alignment */
892 int (*constructor
)(void *, void *, int), /* object constructor */
893 void (*destructor
)(void *, void *), /* object destructor */
894 void (*reclaim
)(void *), /* memory reclaim callback */
895 void *private, /* pass-thru arg for constr/destr/reclaim */
896 vmem_t
*vmp
, /* vmem source for slab allocation */
897 int cflags
) /* cache creation flags */
899 #pragma unused(name,align,constructor,destructor,reclaim,private,vmp,cflags)
900 return (kmem_cache_t
*)bufsize
; /* A cookie that tracks the single object size. */
904 kmem_cache_alloc(kmem_cache_t
*cp
, int kmflag
)
906 #pragma unused(kmflag)
907 size_t bufsize
= (size_t)cp
;
908 return (void *)_MALLOC(bufsize
, M_TEMP
, M_WAITOK
);
912 kmem_cache_free(kmem_cache_t
*cp
, void *buf
)
919 kmem_cache_destroy(kmem_cache_t
*cp
)
927 extern void thread_call_setup(thread_call_t
, thread_call_func_t
, thread_call_param_t
); /* XXX MACH_KERNEL_PRIVATE */
930 _taskq_apply( task_func_t func
, thread_call_param_t arg
)
936 taskq_create(const char *name
, int nthreads
, pri_t pri
, int minalloc
,
937 int maxalloc
, uint_t flags
)
939 #pragma unused(name,nthreads,pri,minalloc,maxalloc,flags)
941 return (taskq_t
*)thread_call_allocate( (thread_call_func_t
)_taskq_apply
, NULL
);
945 taskq_dispatch(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
)
947 #pragma unused(flags)
948 thread_call_setup( (thread_call_t
) tq
, (thread_call_func_t
)_taskq_apply
, (thread_call_param_t
)func
);
949 thread_call_enter1( (thread_call_t
) tq
, (thread_call_param_t
)arg
);
950 return (taskqid_t
) tq
/* for lack of anything better */;
954 taskq_destroy(taskq_t
*tq
)
956 thread_call_cancel( (thread_call_t
) tq
);
957 thread_call_free( (thread_call_t
) tq
);
963 * vmem (Solaris "slab" allocator) used by DTrace solely to hand out resource ids
965 typedef unsigned int u_daddr_t
;
968 /* By passing around blist *handles*, the underlying blist can be resized as needed. */
974 vmem_create(const char *name
, void *base
, size_t size
, size_t quantum
, void *ignore5
,
975 void *ignore6
, vmem_t
*source
, size_t qcache_max
, int vmflag
)
977 #pragma unused(name,quantum,ignore5,ignore6,source,qcache_max,vmflag)
979 struct blist_hdl
*p
= _MALLOC(sizeof(struct blist_hdl
), M_TEMP
, M_WAITOK
);
981 ASSERT(quantum
== 1);
982 ASSERT(NULL
== ignore5
);
983 ASSERT(NULL
== ignore6
);
984 ASSERT(NULL
== source
);
985 ASSERT(0 == qcache_max
);
986 ASSERT(vmflag
& VMC_IDENTIFIER
);
988 size
= MIN(128, size
); /* Clamp to 128 initially, since the underlying data structure is pre-allocated */
990 p
->blist
= bl
= blist_create( size
);
991 blist_free(bl
, 0, size
);
992 if (base
) blist_alloc( bl
, (daddr_t
)(uintptr_t)base
); /* Chomp off initial ID(s) */
998 vmem_alloc(vmem_t
*vmp
, size_t size
, int vmflag
)
1000 #pragma unused(vmflag)
1001 struct blist_hdl
*q
= (struct blist_hdl
*)vmp
;
1002 blist_t bl
= q
->blist
;
1005 p
= blist_alloc(bl
, (daddr_t
)size
);
1007 if ((daddr_t
)-1 == p
) {
1008 blist_resize(&bl
, (bl
->bl_blocks
) << 1, 1);
1010 p
= blist_alloc(bl
, (daddr_t
)size
);
1011 if ((daddr_t
)-1 == p
)
1012 panic("vmem_alloc: failure after blist_resize!");
1015 return (void *)(uintptr_t)p
;
1019 vmem_free(vmem_t
*vmp
, void *vaddr
, size_t size
)
1021 struct blist_hdl
*p
= (struct blist_hdl
*)vmp
;
1023 blist_free( p
->blist
, (daddr_t
)(uintptr_t)vaddr
, (daddr_t
)size
);
1027 vmem_destroy(vmem_t
*vmp
)
1029 struct blist_hdl
*p
= (struct blist_hdl
*)vmp
;
1031 blist_destroy( p
->blist
);
1032 _FREE( p
, sizeof(struct blist_hdl
) );
1040 * dtrace_gethrestime() provides the "walltimestamp", a value that is anchored at
1041 * January 1, 1970. Because it can be called from probe context, it must take no locks.
1045 dtrace_gethrestime(void)
1048 clock_nsec_t nanosecs
;
1049 uint64_t secs64
, ns64
;
1051 clock_get_calendar_nanotime_nowait(&secs
, &nanosecs
);
1052 secs64
= (uint64_t)secs
;
1053 ns64
= (uint64_t)nanosecs
;
1055 ns64
= ns64
+ (secs64
* 1000000000LL);
1060 * dtrace_gethrtime() provides high-resolution timestamps with machine-dependent origin.
1061 * Hence its primary use is to specify intervals.
1065 dtrace_abs_to_nano(uint64_t elapsed
)
1067 static mach_timebase_info_data_t sTimebaseInfo
= { 0, 0 };
1070 * If this is the first time we've run, get the timebase.
1071 * We can use denom == 0 to indicate that sTimebaseInfo is
1072 * uninitialised because it makes no sense to have a zero
1073 * denominator in a fraction.
1076 if ( sTimebaseInfo
.denom
== 0 ) {
1077 (void) clock_timebase_info(&sTimebaseInfo
);
1081 * Convert to nanoseconds.
1082 * return (elapsed * (uint64_t)sTimebaseInfo.numer)/(uint64_t)sTimebaseInfo.denom;
1084 * Provided the final result is representable in 64 bits the following maneuver will
1085 * deliver that result without intermediate overflow.
1087 if (sTimebaseInfo
.denom
== sTimebaseInfo
.numer
)
1089 else if (sTimebaseInfo
.denom
== 1)
1090 return elapsed
* (uint64_t)sTimebaseInfo
.numer
;
1092 /* Decompose elapsed = eta32 * 2^32 + eps32: */
1093 uint64_t eta32
= elapsed
>> 32;
1094 uint64_t eps32
= elapsed
& 0x00000000ffffffffLL
;
1096 uint32_t numer
= sTimebaseInfo
.numer
, denom
= sTimebaseInfo
.denom
;
1098 /* Form product of elapsed64 (decomposed) and numer: */
1099 uint64_t mu64
= numer
* eta32
;
1100 uint64_t lambda64
= numer
* eps32
;
1102 /* Divide the constituents by denom: */
1103 uint64_t q32
= mu64
/denom
;
1104 uint64_t r32
= mu64
- (q32
* denom
); /* mu64 % denom */
1106 return (q32
<< 32) + ((r32
<< 32) + lambda64
)/denom
;
1111 dtrace_gethrtime(void)
1113 static uint64_t start
= 0;
1116 start
= mach_absolute_time();
1118 return dtrace_abs_to_nano(mach_absolute_time() - start
);
1122 * Atomicity and synchronization
1125 dtrace_cas32(uint32_t *target
, uint32_t cmp
, uint32_t new)
1127 if (OSCompareAndSwap( (UInt32
)cmp
, (UInt32
)new, (volatile UInt32
*)target
))
1130 return ~cmp
; /* Must return something *other* than cmp */
1134 dtrace_casptr(void *target
, void *cmp
, void *new)
1136 if (OSCompareAndSwapPtr( cmp
, new, (void**)target
))
1139 return (void *)(~(uintptr_t)cmp
); /* Must return something *other* than cmp */
1143 * Interrupt manipulation
1146 dtrace_interrupt_disable(void)
1148 return (dtrace_icookie_t
)ml_set_interrupts_enabled(FALSE
);
1152 dtrace_interrupt_enable(dtrace_icookie_t reenable
)
1154 (void)ml_set_interrupts_enabled((boolean_t
)reenable
);
1161 dtrace_sync_func(void) {}
1164 * dtrace_sync() is not called from probe context.
1169 dtrace_xcall(DTRACE_CPUALL
, (dtrace_xcall_t
)dtrace_sync_func
, NULL
);
1173 * The dtrace_copyin/out/instr and dtrace_fuword* routines can be called from probe context.
1176 extern kern_return_t
dtrace_copyio_preflight(addr64_t
);
1177 extern kern_return_t
dtrace_copyio_postflight(addr64_t
);
1180 dtrace_copycheck(user_addr_t uaddr
, uintptr_t kaddr
, size_t size
)
1182 #pragma unused(kaddr)
1184 vm_offset_t recover
= dtrace_set_thread_recover( current_thread(), 0 ); /* Snare any extant recovery point. */
1185 dtrace_set_thread_recover( current_thread(), recover
); /* Put it back. We *must not* re-enter and overwrite. */
1187 ASSERT(kaddr
+ size
>= kaddr
);
1189 if (ml_at_interrupt_context() || /* Avoid possible copyio page fault on int stack, which panics! */
1190 0 != recover
|| /* Avoid reentrancy into copyio facility. */
1191 uaddr
+ size
< uaddr
|| /* Avoid address wrap. */
1192 KERN_FAILURE
== dtrace_copyio_preflight(uaddr
)) /* Machine specific setup/constraints. */
1194 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1195 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1202 dtrace_copyin(user_addr_t src
, uintptr_t dst
, size_t len
, volatile uint16_t *flags
)
1204 #pragma unused(flags)
1206 if (dtrace_copycheck( src
, dst
, len
)) {
1207 if (copyin((const user_addr_t
)src
, (char *)dst
, (vm_size_t
)len
)) {
1208 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1209 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= src
;
1211 dtrace_copyio_postflight(src
);
1216 dtrace_copyinstr(user_addr_t src
, uintptr_t dst
, size_t len
, volatile uint16_t *flags
)
1218 #pragma unused(flags)
1222 if (dtrace_copycheck( src
, dst
, len
)) {
1223 /* copyin as many as 'len' bytes. */
1224 int error
= copyinstr((const user_addr_t
)src
, (char *)dst
, (vm_size_t
)len
, &actual
);
1227 * ENAMETOOLONG is returned when 'len' bytes have been copied in but the NUL terminator was
1228 * not encountered. That does not require raising CPU_DTRACE_BADADDR, and we press on.
1229 * Note that we do *not* stuff a NUL terminator when returning ENAMETOOLONG, that's left
1232 if (error
&& error
!= ENAMETOOLONG
) {
1233 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1234 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= src
;
1236 dtrace_copyio_postflight(src
);
1241 dtrace_copyout(uintptr_t src
, user_addr_t dst
, size_t len
, volatile uint16_t *flags
)
1243 #pragma unused(flags)
1245 if (dtrace_copycheck( dst
, src
, len
)) {
1246 if (copyout((const void *)src
, dst
, (vm_size_t
)len
)) {
1247 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1248 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= dst
;
1250 dtrace_copyio_postflight(dst
);
1255 dtrace_copyoutstr(uintptr_t src
, user_addr_t dst
, size_t len
, volatile uint16_t *flags
)
1257 #pragma unused(flags)
1261 if (dtrace_copycheck( dst
, src
, len
)) {
1264 * ENAMETOOLONG is returned when 'len' bytes have been copied out but the NUL terminator was
1265 * not encountered. We raise CPU_DTRACE_BADADDR in that case.
1266 * Note that we do *not* stuff a NUL terminator when returning ENAMETOOLONG, that's left
1269 if (copyoutstr((const void *)src
, dst
, (size_t)len
, &actual
)) {
1270 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1271 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= dst
;
1273 dtrace_copyio_postflight(dst
);
1278 dtrace_fuword8(user_addr_t uaddr
)
1282 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1283 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1284 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1285 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1286 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1288 dtrace_copyio_postflight(uaddr
);
1290 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1296 dtrace_fuword16(user_addr_t uaddr
)
1300 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1301 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1302 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1303 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1304 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1306 dtrace_copyio_postflight(uaddr
);
1308 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1314 dtrace_fuword32(user_addr_t uaddr
)
1318 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1319 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1320 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1321 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1322 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1324 dtrace_copyio_postflight(uaddr
);
1326 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1332 dtrace_fuword64(user_addr_t uaddr
)
1336 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1337 if (dtrace_copycheck( uaddr
, (uintptr_t)&ret
, sizeof(ret
))) {
1338 if (copyin((const user_addr_t
)uaddr
, (char *)&ret
, sizeof(ret
))) {
1339 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1340 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1342 dtrace_copyio_postflight(uaddr
);
1344 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1350 * Emulation of Solaris fuword / suword
1351 * Called from the fasttrap provider, so the use of copyin/out requires fewer safegaurds.
1355 fuword8(user_addr_t uaddr
, uint8_t *value
)
1357 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint8_t)) != 0) {
1365 fuword16(user_addr_t uaddr
, uint16_t *value
)
1367 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint16_t)) != 0) {
1375 fuword32(user_addr_t uaddr
, uint32_t *value
)
1377 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint32_t)) != 0) {
1385 fuword64(user_addr_t uaddr
, uint64_t *value
)
1387 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint64_t)) != 0) {
1395 fuword8_noerr(user_addr_t uaddr
, uint8_t *value
)
1397 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint8_t))) {
1403 fuword16_noerr(user_addr_t uaddr
, uint16_t *value
)
1405 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint16_t))) {
1411 fuword32_noerr(user_addr_t uaddr
, uint32_t *value
)
1413 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint32_t))) {
1419 fuword64_noerr(user_addr_t uaddr
, uint64_t *value
)
1421 if (copyin((const user_addr_t
)uaddr
, (char *)value
, sizeof(uint64_t))) {
1427 suword64(user_addr_t addr
, uint64_t value
)
1429 if (copyout((const void *)&value
, addr
, sizeof(value
)) != 0) {
1437 suword32(user_addr_t addr
, uint32_t value
)
1439 if (copyout((const void *)&value
, addr
, sizeof(value
)) != 0) {
1447 suword16(user_addr_t addr
, uint16_t value
)
1449 if (copyout((const void *)&value
, addr
, sizeof(value
)) != 0) {
1457 suword8(user_addr_t addr
, uint8_t value
)
1459 if (copyout((const void *)&value
, addr
, sizeof(value
)) != 0) {
1470 extern boolean_t
dtrace_tally_fault(user_addr_t
);
1473 dtrace_tally_fault(user_addr_t uaddr
)
1475 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1476 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= uaddr
;
1477 return( DTRACE_CPUFLAG_ISSET(CPU_DTRACE_NOFAULT
) ? TRUE
: FALSE
);
1481 dtrace_vpanic(const char *format
, va_list alist
)
1483 vuprintf( format
, alist
);
1484 panic("dtrace_vpanic");
1488 extern int prf(const char *, va_list, int, struct tty
*); /* bsd/kern/subr_prf.h */
1491 vuprintf(const char *format
, va_list ap
)
1493 return prf(format
, ap
, TOTTY
, NULL
);
1496 /* Not called from probe context */
1497 void cmn_err( int level
, const char *format
, ... )
1499 #pragma unused(level)
1502 va_start(alist
, format
);
1503 vuprintf(format
, alist
);
1510 * 2002-01-24 gvdl Initial implementation of strstr
1513 __private_extern__
const char *
1514 strstr(const char *in
, const char *str
)
1521 return (const char *) in
; // Trivial empty string case
1532 } while (strncmp(in
, str
, len
) != 0);
1534 return (const char *) (in
- 1);
1541 dtrace_caller(int ignore
)
1543 #pragma unused(ignore)
1544 return -1; /* Just as in Solaris dtrace_asm.s */
1548 dtrace_getstackdepth(int aframes
)
1550 struct frame
*fp
= (struct frame
*)__builtin_frame_address(0);
1551 struct frame
*nextfp
, *minfp
, *stacktop
;
1555 if ((on_intr
= CPU_ON_INTR(CPU
)) != 0)
1556 stacktop
= (struct frame
*)dtrace_get_cpu_int_stack_top();
1558 stacktop
= (struct frame
*)(dtrace_get_kernel_stack(current_thread()) + kernel_stack_size
);
1567 nextfp
= *(struct frame
**)fp
;
1569 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
1572 * Hop from interrupt stack to thread stack.
1574 vm_offset_t kstack_base
= dtrace_get_kernel_stack(current_thread());
1576 minfp
= (struct frame
*)kstack_base
;
1577 stacktop
= (struct frame
*)(kstack_base
+ kernel_stack_size
);
1589 if (depth
<= aframes
)
1592 return (depth
- aframes
);
1599 dtrace_vtime_enable(void) {}
1602 dtrace_vtime_disable(void) {}
1604 #else /* else ! CONFIG_DTRACE */
1606 #include <sys/types.h>
1607 #include <mach/vm_types.h>
1608 #include <mach/kmod.h>
1611 * This exists to prevent build errors when dtrace is unconfigured.
1614 kern_return_t
_dtrace_register_anon_DOF(char *, unsigned char *, uint32_t);
1616 kern_return_t
_dtrace_register_anon_DOF(char *arg1
, unsigned char *arg2
, uint32_t arg3
) {
1617 #pragma unused(arg1, arg2, arg3)
1619 return KERN_FAILURE
;
1622 #endif /* CONFIG_DTRACE */