X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/2d21ac55c334faf3a56e5634905ed6987fc787d4..cc8bc92ae4a8e9f1a1ab61bf83d34ad8150b3405:/bsd/dev/dtrace/dtrace_glue.c?ds=inline diff --git a/bsd/dev/dtrace/dtrace_glue.c b/bsd/dev/dtrace/dtrace_glue.c index 035150aa7..57f0f3207 100644 --- a/bsd/dev/dtrace/dtrace_glue.c +++ b/bsd/dev/dtrace/dtrace_glue.c @@ -51,6 +51,8 @@ #include #include #include +#include +#include #include #include #include @@ -67,7 +69,8 @@ /* * pid/proc */ -#define proc_t struct proc +/* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */ +#define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */ /* Not called from probe context */ proc_t * @@ -79,7 +82,7 @@ sprlock(pid_t pid) return PROC_NULL; } - task_suspend(p->task); + task_suspend_internal(p->task); proc_lock(p); @@ -97,7 +100,7 @@ sprunlock(proc_t *p) proc_unlock(p); - task_resume(p->task); + task_resume_internal(p->task); proc_rele(p); } @@ -224,9 +227,10 @@ done: * cpuvar */ lck_mtx_t cpu_lock; +lck_mtx_t cyc_lock; lck_mtx_t mod_lock; -cpu_t *cpu_list; +dtrace_cpu_t *cpu_list; cpu_core_t *cpu_core; /* XXX TLB lockdown? */ /* @@ -266,66 +270,86 @@ PRIV_POLICY_ONLY(void *cr, int priv, int boolean) return kauth_cred_issuser(cr); /* XXX TODO: HAS_PRIVILEGE(cr, priv); */ } +/* XXX Get around const poisoning using structure assigns */ gid_t -crgetgid(const cred_t *cr) { return cr->cr_groups[0]; } +crgetgid(const cred_t *cr) { cred_t copy_cr = *cr; return kauth_cred_getgid(©_cr); } uid_t -crgetuid(const cred_t *cr) { return cr->cr_uid; } +crgetuid(const cred_t *cr) { cred_t copy_cr = *cr; return kauth_cred_getuid(©_cr); } /* * "cyclic" */ -/* osfmk/kern/timer_call.h */ -typedef void *call_entry_param_t; -typedef void (*call_entry_func_t)( - call_entry_param_t param0, - call_entry_param_t param1); - -typedef struct call_entry { - queue_chain_t q_link; - call_entry_func_t func; - call_entry_param_t param0; - call_entry_param_t param1; - uint64_t deadline; - enum { - IDLE, - PENDING, - DELAYED } state; -} call_entry_data_t; - - -typedef struct call_entry *timer_call_t; -typedef void *timer_call_param_t; -typedef void (*timer_call_func_t)( - timer_call_param_t param0, - timer_call_param_t param1); - -extern void -timer_call_setup( - timer_call_t call, - timer_call_func_t func, - timer_call_param_t param0); - -extern boolean_t -timer_call_enter1( - timer_call_t call, - timer_call_param_t param1, - uint64_t deadline); - -extern boolean_t -timer_call_cancel( - timer_call_t call); - typedef struct wrap_timer_call { - cyc_handler_t hdlr; - cyc_time_t when; - uint64_t deadline; - struct call_entry call; + /* node attributes */ + cyc_handler_t hdlr; + cyc_time_t when; + uint64_t deadline; + int cpuid; + boolean_t suspended; + struct timer_call call; + + /* next item in the linked list */ + LIST_ENTRY(wrap_timer_call) entries; } wrap_timer_call_t; -#define WAKEUP_REAPER 0x7FFFFFFFFFFFFFFFLL -#define NEARLY_FOREVER 0x7FFFFFFFFFFFFFFELL +#define WAKEUP_REAPER 0x7FFFFFFFFFFFFFFFLL +#define NEARLY_FOREVER 0x7FFFFFFFFFFFFFFELL + + +typedef struct cyc_list { + cyc_omni_handler_t cyl_omni; + wrap_timer_call_t cyl_wrap_by_cpus[]; +#if __arm__ && (__BIGGEST_ALIGNMENT__ > 4) +} __attribute__ ((aligned (8))) cyc_list_t; +#else +} cyc_list_t; +#endif + +/* CPU going online/offline notifications */ +void (*dtrace_cpu_state_changed_hook)(int, boolean_t) = NULL; +void dtrace_cpu_state_changed(int, boolean_t); + +void +dtrace_install_cpu_hooks(void) { + dtrace_cpu_state_changed_hook = dtrace_cpu_state_changed; +} + +void +dtrace_cpu_state_changed(int cpuid, boolean_t is_running) { +#pragma unused(cpuid) + wrap_timer_call_t *wrapTC = NULL; + boolean_t suspend = (is_running ? FALSE : TRUE); + dtrace_icookie_t s; + + /* Ensure that we're not going to leave the CPU */ + s = dtrace_interrupt_disable(); + assert(cpuid == cpu_number()); + + LIST_FOREACH(wrapTC, &(cpu_list[cpu_number()].cpu_cyc_list), entries) { + assert(wrapTC->cpuid == cpu_number()); + if (suspend) { + assert(!wrapTC->suspended); + /* If this fails, we'll panic anyway, so let's do this now. */ + if (!timer_call_cancel(&wrapTC->call)) + panic("timer_call_set_suspend() failed to cancel a timer call"); + wrapTC->suspended = TRUE; + } else { + /* Rearm the timer, but ensure it was suspended first. */ + assert(wrapTC->suspended); + clock_deadline_for_periodic_event(wrapTC->when.cyt_interval, mach_absolute_time(), + &wrapTC->deadline); + timer_call_enter1(&wrapTC->call, (void*) wrapTC, wrapTC->deadline, + TIMER_CALL_SYS_CRITICAL | TIMER_CALL_LOCAL); + wrapTC->suspended = FALSE; + } + + } + + /* Restore the previous interrupt state. */ + dtrace_interrupt_enable(s); +} static void _timer_call_apply_cyclic( void *ignore, void *vTChdl ) @@ -336,17 +360,14 @@ _timer_call_apply_cyclic( void *ignore, void *vTChdl ) (*(wrapTC->hdlr.cyh_func))( wrapTC->hdlr.cyh_arg ); clock_deadline_for_periodic_event( wrapTC->when.cyt_interval, mach_absolute_time(), &(wrapTC->deadline) ); - timer_call_enter1( &(wrapTC->call), (void *)wrapTC, wrapTC->deadline ); - - /* Did timer_call_remove_cyclic request a wakeup call when this timer call was re-armed? */ - if (wrapTC->when.cyt_interval == WAKEUP_REAPER) - thread_wakeup((event_t)wrapTC); + timer_call_enter1( &(wrapTC->call), (void *)wrapTC, wrapTC->deadline, TIMER_CALL_SYS_CRITICAL | TIMER_CALL_LOCAL ); } static cyclic_id_t timer_call_add_cyclic(wrap_timer_call_t *wrapTC, cyc_handler_t *handler, cyc_time_t *when) { uint64_t now; + dtrace_icookie_t s; timer_call_setup( &(wrapTC->call), _timer_call_apply_cyclic, NULL ); wrapTC->hdlr = *handler; @@ -358,34 +379,39 @@ timer_call_add_cyclic(wrap_timer_call_t *wrapTC, cyc_handler_t *handler, cyc_tim wrapTC->deadline = now; clock_deadline_for_periodic_event( wrapTC->when.cyt_interval, now, &(wrapTC->deadline) ); - timer_call_enter1( &(wrapTC->call), (void *)wrapTC, wrapTC->deadline ); + + /* Insert the timer to the list of the running timers on this CPU, and start it. */ + s = dtrace_interrupt_disable(); + wrapTC->cpuid = cpu_number(); + LIST_INSERT_HEAD(&cpu_list[wrapTC->cpuid].cpu_cyc_list, wrapTC, entries); + timer_call_enter1(&wrapTC->call, (void*) wrapTC, wrapTC->deadline, + TIMER_CALL_SYS_CRITICAL | TIMER_CALL_LOCAL); + wrapTC->suspended = FALSE; + dtrace_interrupt_enable(s); return (cyclic_id_t)wrapTC; } +/* + * Executed on the CPU the timer is running on. + */ static void -timer_call_remove_cyclic(cyclic_id_t cyclic) +timer_call_remove_cyclic(wrap_timer_call_t *wrapTC) { - wrap_timer_call_t *wrapTC = (wrap_timer_call_t *)cyclic; + assert(wrapTC); + assert(cpu_number() == wrapTC->cpuid); - while (!timer_call_cancel(&(wrapTC->call))) { - int ret = assert_wait(wrapTC, THREAD_UNINT); - ASSERT(ret == THREAD_WAITING); - - wrapTC->when.cyt_interval = WAKEUP_REAPER; + if (!timer_call_cancel(&wrapTC->call)) + panic("timer_call_remove_cyclic() failed to cancel a timer call"); - ret = thread_block(THREAD_CONTINUE_NULL); - ASSERT(ret == THREAD_AWAKENED); - } + LIST_REMOVE(wrapTC, entries); } static void * -timer_call_get_cyclic_arg(cyclic_id_t cyclic) -{ - wrap_timer_call_t *wrapTC = (wrap_timer_call_t *)cyclic; - +timer_call_get_cyclic_arg(wrap_timer_call_t *wrapTC) +{ return (wrapTC ? wrapTC->hdlr.cyh_arg : NULL); -} +} cyclic_id_t cyclic_timer_add(cyc_handler_t *handler, cyc_time_t *when) @@ -402,71 +428,64 @@ cyclic_timer_remove(cyclic_id_t cyclic) { ASSERT( cyclic != CYCLIC_NONE ); - timer_call_remove_cyclic( cyclic ); + /* Removing a timer call must be done on the CPU the timer is running on. */ + wrap_timer_call_t *wrapTC = (wrap_timer_call_t *) cyclic; + dtrace_xcall(wrapTC->cpuid, (dtrace_xcall_t) timer_call_remove_cyclic, (void*) cyclic); + _FREE((void *)cyclic, M_TEMP); } static void -_cyclic_add_omni(cyclic_id_list_t cyc_list) +_cyclic_add_omni(cyc_list_t *cyc_list) { cyc_time_t cT; cyc_handler_t cH; - wrap_timer_call_t *wrapTC; - cyc_omni_handler_t *omni = (cyc_omni_handler_t *)cyc_list; - char *t; - - (omni->cyo_online)(omni->cyo_arg, CPU, &cH, &cT); + cyc_omni_handler_t *omni = &cyc_list->cyl_omni; - t = (char *)cyc_list; - t += sizeof(cyc_omni_handler_t); - cyc_list = (cyclic_id_list_t)t; + (omni->cyo_online)(omni->cyo_arg, CPU, &cH, &cT); - t += sizeof(cyclic_id_t)*NCPU; - t += (sizeof(wrap_timer_call_t))*cpu_number(); - wrapTC = (wrap_timer_call_t *)t; - - cyc_list[cpu_number()] = timer_call_add_cyclic(wrapTC, &cH, &cT); + wrap_timer_call_t *wrapTC = &cyc_list->cyl_wrap_by_cpus[cpu_number()]; + timer_call_add_cyclic(wrapTC, &cH, &cT); } cyclic_id_list_t cyclic_add_omni(cyc_omni_handler_t *omni) { - cyclic_id_list_t cyc_list = - _MALLOC( (sizeof(wrap_timer_call_t))*NCPU + - sizeof(cyclic_id_t)*NCPU + - sizeof(cyc_omni_handler_t), M_TEMP, M_ZERO | M_WAITOK); + cyc_list_t *cyc_list = + _MALLOC(sizeof(cyc_list_t) + NCPU * sizeof(wrap_timer_call_t), M_TEMP, M_ZERO | M_WAITOK); + if (NULL == cyc_list) - return (cyclic_id_list_t)CYCLIC_NONE; + return NULL; + + cyc_list->cyl_omni = *omni; - *(cyc_omni_handler_t *)cyc_list = *omni; dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)_cyclic_add_omni, (void *)cyc_list); - return cyc_list; + return (cyclic_id_list_t)cyc_list; } static void -_cyclic_remove_omni(cyclic_id_list_t cyc_list) +_cyclic_remove_omni(cyc_list_t *cyc_list) { - cyc_omni_handler_t *omni = (cyc_omni_handler_t *)cyc_list; + cyc_omni_handler_t *omni = &cyc_list->cyl_omni; void *oarg; - cyclic_id_t cid; - char *t; - - t = (char *)cyc_list; - t += sizeof(cyc_omni_handler_t); - cyc_list = (cyclic_id_list_t)t; - - cid = cyc_list[cpu_number()]; - oarg = timer_call_get_cyclic_arg(cid); + wrap_timer_call_t *wrapTC; - timer_call_remove_cyclic( cid ); - (omni->cyo_offline)(omni->cyo_arg, CPU, oarg); + /* + * If the processor was offline when dtrace started, we did not allocate + * a cyclic timer for this CPU. + */ + if ((wrapTC = &cyc_list->cyl_wrap_by_cpus[cpu_number()]) != NULL) { + oarg = timer_call_get_cyclic_arg(wrapTC); + timer_call_remove_cyclic(wrapTC); + (omni->cyo_offline)(omni->cyo_arg, CPU, oarg); + } } void cyclic_remove_omni(cyclic_id_list_t cyc_list) { - ASSERT( cyc_list != (cyclic_id_list_t)CYCLIC_NONE ); + ASSERT(cyc_list != NULL); dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)_cyclic_remove_omni, (void *)cyc_list); _FREE(cyc_list, M_TEMP); @@ -558,29 +577,6 @@ cyclic_remove(cyclic_id_t cyclic) } } -/* - * timeout / untimeout (converted to dtrace_timeout / dtrace_untimeout due to name collision) - */ - -thread_call_t -dtrace_timeout(void (*func)(void *, void *), void* arg, uint64_t nanos) -{ -#pragma unused(arg) - thread_call_t call = thread_call_allocate(func, NULL); - - nanoseconds_to_absolutetime(nanos, &nanos); - - /* - * This method does not use clock_deadline_for_periodic_event() because it is a one-shot, - * and clock drift on later invocations is not a worry. - */ - uint64_t deadline = mach_absolute_time() + nanos; - - thread_call_enter_delayed(call, deadline); - - return call; -} - /* * ddi */ @@ -590,54 +586,6 @@ ddi_report_dev(dev_info_t *devi) #pragma unused(devi) } -#define NSOFT_STATES 32 /* XXX No more than 32 clients at a time, please. */ -static void *soft[NSOFT_STATES]; - -int -ddi_soft_state_init(void **state_p, size_t size, size_t n_items) -{ -#pragma unused(n_items) - int i; - - for (i = 0; i < NSOFT_STATES; ++i) soft[i] = _MALLOC(size, M_TEMP, M_ZERO | M_WAITOK); - *(size_t *)state_p = size; - return 0; -} - -int -ddi_soft_state_zalloc(void *state, int item) -{ -#pragma unused(state) - if (item < NSOFT_STATES) - return DDI_SUCCESS; - else - return DDI_FAILURE; -} - -void * -ddi_get_soft_state(void *state, int item) -{ -#pragma unused(state) - ASSERT(item < NSOFT_STATES); - return soft[item]; -} - -int -ddi_soft_state_free(void *state, int item) -{ - ASSERT(item < NSOFT_STATES); - bzero( soft[item], (size_t)state ); - return DDI_SUCCESS; -} - -void -ddi_soft_state_fini(void **state_p) -{ -#pragma unused(state_p) - int i; - - for (i = 0; i < NSOFT_STATES; ++i) _FREE( soft[i], M_TEMP ); -} static unsigned int gRegisteredProps = 0; static struct { @@ -674,7 +622,7 @@ _dtrace_register_anon_DOF(char *name, uchar_t *data, uint_t nelements) int ddi_prop_lookup_int_array(dev_t match_dev, dev_info_t *dip, uint_t flags, - char *name, int **data, uint_t *nelements) + const char *name, int **data, uint_t *nelements) { #pragma unused(match_dev,dip,flags) unsigned int i; @@ -698,14 +646,14 @@ ddi_prop_free(void *buf) } int -ddi_driver_major(dev_info_t *devi) { return (int)major(devi); } +ddi_driver_major(dev_info_t *devi) { return (int)major(CAST_DOWN_EXPLICIT(int,devi)); } int ddi_create_minor_node(dev_info_t *dip, const char *name, int spec_type, minor_t minor_num, const char *node_type, int flag) { #pragma unused(spec_type,node_type,flag) - dev_t dev = makedev( (uint32_t)dip, minor_num ); + dev_t dev = makedev( ddi_driver_major(dip), minor_num ); if (NULL == devfs_make_node( dev, DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666, name, 0 )) return DDI_FAILURE; @@ -831,23 +779,34 @@ dt_kmem_free(void *buf, size_t size) void* dt_kmem_alloc_aligned(size_t size, size_t align, int kmflag) { - void* buf; - intptr_t p; - void** buf_backup; + void *mem, **addr_to_free; + intptr_t mem_aligned; + size_t *size_to_free, hdr_size; - buf = dt_kmem_alloc(align + sizeof(void*) + size, kmflag); + /* Must be a power of two. */ + assert(align != 0); + assert((align & (align - 1)) == 0); - if(!buf) + /* + * We are going to add a header to the allocation. It contains + * the address to free and the total size of the buffer. + */ + hdr_size = sizeof(size_t) + sizeof(void*); + mem = dt_kmem_alloc(size + align + hdr_size, kmflag); + if (mem == NULL) return NULL; - p = (intptr_t)buf; - p += sizeof(void*); /* now we have enough room to store the backup */ - p = P2ROUNDUP(p, align); /* and now we're aligned */ + mem_aligned = (intptr_t) (((intptr_t) mem + align + hdr_size) & ~(align - 1)); + + /* Write the address to free in the header. */ + addr_to_free = (void**) (mem_aligned - sizeof(void*)); + *addr_to_free = mem; - buf_backup = (void**)(p - sizeof(void*)); - *buf_backup = buf; /* back up the address we need to free */ + /* Write the size to free in the header. */ + size_to_free = (size_t*) (mem_aligned - hdr_size); + *size_to_free = size + align + hdr_size; - return (void*)p; + return (void*) mem_aligned; } void* dt_kmem_zalloc_aligned(size_t size, size_t align, int kmflag) @@ -867,14 +826,14 @@ void* dt_kmem_zalloc_aligned(size_t size, size_t align, int kmflag) void dt_kmem_free_aligned(void* buf, size_t size) { #pragma unused(size) - intptr_t p; - void** buf_backup; + intptr_t ptr = (intptr_t) buf; + void **addr_to_free = (void**) (ptr - sizeof(void*)); + size_t *size_to_free = (size_t*) (ptr - (sizeof(size_t) + sizeof(void*))); - p = (intptr_t)buf; - p -= sizeof(void*); - buf_backup = (void**)(p); + if (buf == NULL) + return; - dt_kmem_free(*buf_backup, size + ((char*)buf - (char*)*buf_backup)); + dt_kmem_free(*addr_to_free, *size_to_free); } /* @@ -885,7 +844,7 @@ void dt_kmem_free_aligned(void* buf, size_t size) */ kmem_cache_t * kmem_cache_create( - char *name, /* descriptive name for this cache */ + const char *name, /* descriptive name for this cache */ size_t bufsize, /* size of the objects it manages */ size_t align, /* required object alignment */ int (*constructor)(void *, void *, int), /* object constructor */ @@ -988,7 +947,7 @@ vmem_create(const char *name, void *base, size_t size, size_t quantum, void *ign p->blist = bl = blist_create( size ); blist_free(bl, 0, size); - if (base) blist_alloc( bl, (daddr_t)base ); /* Chomp off initial ID(s) */ + if (base) blist_alloc( bl, (daddr_t)(uintptr_t)base ); /* Chomp off initial ID(s) */ return (vmem_t *)p; } @@ -1011,7 +970,7 @@ vmem_alloc(vmem_t *vmp, size_t size, int vmflag) panic("vmem_alloc: failure after blist_resize!"); } - return (void *)p; + return (void *)(uintptr_t)p; } void @@ -1019,7 +978,7 @@ vmem_free(vmem_t *vmp, void *vaddr, size_t size) { struct blist_hdl *p = (struct blist_hdl *)vmp; - blist_free( p->blist, (daddr_t)vaddr, (daddr_t)size ); + blist_free( p->blist, (daddr_t)(uintptr_t)vaddr, (daddr_t)size ); } void @@ -1043,7 +1002,8 @@ vmem_destroy(vmem_t *vmp) hrtime_t dtrace_gethrestime(void) { - uint32_t secs, nanosecs; + clock_sec_t secs; + clock_nsec_t nanosecs; uint64_t secs64, ns64; clock_get_calendar_nanotime_nowait(&secs, &nanosecs); @@ -1122,7 +1082,7 @@ dtrace_gethrtime(void) uint32_t dtrace_cas32(uint32_t *target, uint32_t cmp, uint32_t new) { - if (OSCompareAndSwap( cmp, new, (unsigned long *)target )) + if (OSCompareAndSwap( (UInt32)cmp, (UInt32)new, (volatile UInt32 *)target )) return cmp; else return ~cmp; /* Must return something *other* than cmp */ @@ -1131,14 +1091,10 @@ dtrace_cas32(uint32_t *target, uint32_t cmp, uint32_t new) void * dtrace_casptr(void *target, void *cmp, void *new) { -#if defined(__LP64__) -#error dtrace_casptr implementation missing for LP64 -#else - if (OSCompareAndSwap( (uint32_t)cmp, (uint32_t)new, (unsigned long *)target )) + if (OSCompareAndSwapPtr( cmp, new, (void**)target )) return cmp; else return (void *)(~(uintptr_t)cmp); /* Must return something *other* than cmp */ -#endif } /* @@ -1188,9 +1144,7 @@ dtrace_copycheck(user_addr_t uaddr, uintptr_t kaddr, size_t size) ASSERT(kaddr + size >= kaddr); - if (ml_at_interrupt_context() || /* Avoid possible copyio page fault on int stack, which panics! */ - 0 != recover || /* Avoid reentrancy into copyio facility. */ - uaddr + size < uaddr || /* Avoid address wrap. */ + if ( uaddr + size < uaddr || /* Avoid address wrap. */ KERN_FAILURE == dtrace_copyio_preflight(uaddr)) /* Machine specific setup/constraints. */ { DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); @@ -1201,8 +1155,10 @@ dtrace_copycheck(user_addr_t uaddr, uintptr_t kaddr, size_t size) } void -dtrace_copyin(user_addr_t src, uintptr_t dst, size_t len) +dtrace_copyin(user_addr_t src, uintptr_t dst, size_t len, volatile uint16_t *flags) { +#pragma unused(flags) + if (dtrace_copycheck( src, dst, len )) { if (copyin((const user_addr_t)src, (char *)dst, (vm_size_t)len)) { DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); @@ -1213,12 +1169,23 @@ dtrace_copyin(user_addr_t src, uintptr_t dst, size_t len) } void -dtrace_copyinstr(user_addr_t src, uintptr_t dst, size_t len) +dtrace_copyinstr(user_addr_t src, uintptr_t dst, size_t len, volatile uint16_t *flags) { +#pragma unused(flags) + size_t actual; if (dtrace_copycheck( src, dst, len )) { - if (copyinstr((const user_addr_t)src, (char *)dst, (vm_size_t)len, &actual)) { + /* copyin as many as 'len' bytes. */ + int error = copyinstr((const user_addr_t)src, (char *)dst, (vm_size_t)len, &actual); + + /* + * ENAMETOOLONG is returned when 'len' bytes have been copied in but the NUL terminator was + * not encountered. That does not require raising CPU_DTRACE_BADADDR, and we press on. + * Note that we do *not* stuff a NUL terminator when returning ENAMETOOLONG, that's left + * to the caller. + */ + if (error && error != ENAMETOOLONG) { DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); cpu_core[CPU->cpu_id].cpuc_dtrace_illval = src; } @@ -1227,8 +1194,10 @@ dtrace_copyinstr(user_addr_t src, uintptr_t dst, size_t len) } void -dtrace_copyout(uintptr_t src, user_addr_t dst, size_t len) +dtrace_copyout(uintptr_t src, user_addr_t dst, size_t len, volatile uint16_t *flags) { +#pragma unused(flags) + if (dtrace_copycheck( dst, src, len )) { if (copyout((const void *)src, dst, (vm_size_t)len)) { DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); @@ -1239,11 +1208,20 @@ dtrace_copyout(uintptr_t src, user_addr_t dst, size_t len) } void -dtrace_copyoutstr(uintptr_t src, user_addr_t dst, size_t len) +dtrace_copyoutstr(uintptr_t src, user_addr_t dst, size_t len, volatile uint16_t *flags) { +#pragma unused(flags) + size_t actual; if (dtrace_copycheck( dst, src, len )) { + + /* + * ENAMETOOLONG is returned when 'len' bytes have been copied out but the NUL terminator was + * not encountered. We raise CPU_DTRACE_BADADDR in that case. + * Note that we do *not* stuff a NUL terminator when returning ENAMETOOLONG, that's left + * to the caller. + */ if (copyoutstr((const void *)src, dst, (size_t)len, &actual)) { DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); cpu_core[CPU->cpu_id].cpuc_dtrace_illval = dst; @@ -1252,6 +1230,30 @@ dtrace_copyoutstr(uintptr_t src, user_addr_t dst, size_t len) } } +extern const int copysize_limit_panic; + +int +dtrace_buffer_copyout(const void *kaddr, user_addr_t uaddr, vm_size_t nbytes) +{ + /* + * Partition the copyout in copysize_limit_panic-sized chunks + */ + while (nbytes >= (vm_size_t)copysize_limit_panic) { + if (copyout(kaddr, uaddr, copysize_limit_panic) != 0) + return (EFAULT); + + nbytes -= copysize_limit_panic; + uaddr += copysize_limit_panic; + kaddr += copysize_limit_panic; + } + if (nbytes > 0) { + if (copyout(kaddr, uaddr, nbytes) != 0) + return (EFAULT); + } + + return (0); +} + uint8_t dtrace_fuword8(user_addr_t uaddr) { @@ -1455,13 +1457,6 @@ dtrace_tally_fault(user_addr_t uaddr) return( DTRACE_CPUFLAG_ISSET(CPU_DTRACE_NOFAULT) ? TRUE : FALSE ); } -void -dtrace_vpanic(const char *format, va_list alist) -{ - vuprintf( format, alist ); - panic("dtrace_vpanic"); -} - #define TOTTY 0x02 extern int prf(const char *, va_list, int, struct tty *); /* bsd/kern/subr_prf.h */ @@ -1488,15 +1483,17 @@ void cmn_err( int level, const char *format, ... ) * 2002-01-24 gvdl Initial implementation of strstr */ -__private_extern__ char * +__private_extern__ const char * strstr(const char *in, const char *str) { char c; size_t len; + if (!in || !str) + return in; c = *str++; if (!c) - return (char *) in; // Trivial empty string case + return (const char *) in; // Trivial empty string case len = strlen(str); do { @@ -1509,7 +1506,27 @@ strstr(const char *in, const char *str) } while (sc != c); } while (strncmp(in, str, len) != 0); - return (char *) (in - 1); + return (const char *) (in - 1); +} + +const void* +bsearch(const void *key, const void *base0, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) +{ + const char *base = base0; + size_t lim; + int cmp; + const void *p; + for (lim = nmemb; lim != 0; lim >>= 1) { + p = base + (lim >> 1) * size; + cmp = (*compar)(key, p); + if (cmp == 0) + return p; + if (cmp > 0) { /* key > p: move right */ + base = (const char *)p + size; + lim--; + } /* else move left */ + } + return (NULL); } /* @@ -1525,7 +1542,7 @@ dtrace_caller(int ignore) int dtrace_getstackdepth(int aframes) { - struct frame *fp = (struct frame *)dtrace_getfp(); + struct frame *fp = (struct frame *)__builtin_frame_address(0); struct frame *nextfp, *minfp, *stacktop; int depth = 0; int on_intr; @@ -1533,7 +1550,7 @@ dtrace_getstackdepth(int aframes) if ((on_intr = CPU_ON_INTR(CPU)) != 0) stacktop = (struct frame *)dtrace_get_cpu_int_stack_top(); else - stacktop = (struct frame *)(dtrace_get_kernel_stack(current_thread()) + KERNEL_STACK_SIZE); + stacktop = (struct frame *)(dtrace_get_kernel_stack(current_thread()) + kernel_stack_size); minfp = fp; @@ -1552,7 +1569,7 @@ dtrace_getstackdepth(int aframes) vm_offset_t kstack_base = dtrace_get_kernel_stack(current_thread()); minfp = (struct frame *)kstack_base; - stacktop = (struct frame *)(kstack_base + KERNEL_STACK_SIZE); + stacktop = (struct frame *)(kstack_base + kernel_stack_size); on_intr = 0; continue;