+ kd_iop_t* iop;
+ if (kmem_alloc(kernel_map, (vm_offset_t *)&iop, sizeof(kd_iop_t)) == KERN_SUCCESS) {
+ memcpy(&iop->callback, &callback, sizeof(kd_callback_t));
+
+ /*
+ * <rdar://problem/13351477> Some IOP clients are not providing a name.
+ *
+ * Remove when fixed.
+ */
+ {
+ boolean_t is_valid_name = FALSE;
+ for (uint32_t length=0; length<sizeof(callback.iop_name); ++length) {
+ /* This is roughly isprintable(c) */
+ if (callback.iop_name[length] > 0x20 && callback.iop_name[length] < 0x7F)
+ continue;
+ if (callback.iop_name[length] == 0) {
+ if (length)
+ is_valid_name = TRUE;
+ break;
+ }
+ }
+
+ if (!is_valid_name) {
+ strlcpy(iop->callback.iop_name, "IOP-???", sizeof(iop->callback.iop_name));
+ }
+ }
+
+ iop->last_timestamp = 0;
+
+ do {
+ /*
+ * We use two pieces of state, the old list head
+ * pointer, and the value of old_list_head->cpu_id.
+ * If we read kd_iops more than once, it can change
+ * between reads.
+ *
+ * TLDR; Must not read kd_iops more than once per loop.
+ */
+ iop->next = kd_iops;
+ iop->cpu_id = iop->next ? (iop->next->cpu_id+1) : kdbg_cpu_count(FALSE);
+
+ /*
+ * Header says OSCompareAndSwapPtr has a memory barrier
+ */
+ } while (!OSCompareAndSwapPtr(iop->next, iop, (void* volatile*)&kd_iops));
+
+ return iop->cpu_id;
+ }
+
+ return 0;
+}
+
+void
+kernel_debug_enter(
+ uint32_t coreid,
+ uint32_t debugid,
+ uint64_t timestamp,
+ uintptr_t arg1,
+ uintptr_t arg2,
+ uintptr_t arg3,
+ uintptr_t arg4,
+ uintptr_t threadid
+ )
+{
+ uint32_t bindx;
+ kd_buf *kd;
+ struct kd_bufinfo *kdbp;
+ struct kd_storage *kdsp_actual;
+ union kds_ptr kds_raw;
+
+ if (kd_ctrl_page.kdebug_slowcheck) {
+
+ if ( (kd_ctrl_page.kdebug_slowcheck & SLOW_NOLOG) || !(kdebug_enable & (KDEBUG_ENABLE_TRACE|KDEBUG_ENABLE_PPT)))
+ goto out1;
+
+ if (kd_ctrl_page.kdebug_flags & KDBG_TYPEFILTER_CHECK) {
+ if (isset(type_filter_bitmap, EXTRACT_CSC(debugid)))
+ goto record_event;
+ goto out1;
+ }
+ else if (kd_ctrl_page.kdebug_flags & KDBG_RANGECHECK) {
+ if (debugid >= kdlog_beg && debugid <= kdlog_end)
+ goto record_event;
+ goto out1;
+ }
+ else if (kd_ctrl_page.kdebug_flags & KDBG_VALCHECK) {
+ if ((debugid & DBG_FUNC_MASK) != kdlog_value1 &&
+ (debugid & DBG_FUNC_MASK) != kdlog_value2 &&
+ (debugid & DBG_FUNC_MASK) != kdlog_value3 &&
+ (debugid & DBG_FUNC_MASK) != kdlog_value4)
+ goto out1;
+ }
+ }
+
+record_event:
+
+ disable_preemption();
+
+ if (kd_ctrl_page.enabled == 0)
+ goto out;
+
+ kdbp = &kdbip[coreid];
+ timestamp &= KDBG_TIMESTAMP_MASK;
+
+#if KDEBUG_MOJO_TRACE
+ if (kdebug_enable & KDEBUG_ENABLE_SERIAL)
+ kdebug_serial_print(coreid, debugid, timestamp,
+ arg1, arg2, arg3, arg4, threadid);
+#endif
+
+retry_q:
+ kds_raw = kdbp->kd_list_tail;
+
+ if (kds_raw.raw != KDS_PTR_NULL) {
+ kdsp_actual = POINTER_FROM_KDS_PTR(kds_raw);
+ bindx = kdsp_actual->kds_bufindx;
+ } else
+ kdsp_actual = NULL;
+
+ if (kdsp_actual == NULL || bindx >= EVENTS_PER_STORAGE_UNIT) {
+ if (allocate_storage_unit(coreid) == FALSE) {
+ /*
+ * this can only happen if wrapping
+ * has been disabled
+ */
+ goto out;
+ }
+ goto retry_q;
+ }
+ if ( !OSCompareAndSwap(bindx, bindx + 1, &kdsp_actual->kds_bufindx))
+ goto retry_q;
+
+ // IOP entries can be allocated before xnu allocates and inits the buffer
+ if (timestamp < kdsp_actual->kds_timestamp)
+ kdsp_actual->kds_timestamp = timestamp;
+
+ kd = &kdsp_actual->kds_records[bindx];
+
+ kd->debugid = debugid;
+ kd->arg1 = arg1;
+ kd->arg2 = arg2;
+ kd->arg3 = arg3;
+ kd->arg4 = arg4;
+ kd->arg5 = threadid;
+
+ kdbg_set_timestamp_and_cpu(kd, timestamp, coreid);
+
+ OSAddAtomic(1, &kdsp_actual->kds_bufcnt);
+out:
+ enable_preemption();
+out1:
+ if ((kds_waiter && kd_ctrl_page.kds_inuse_count >= n_storage_threshold)) {
+ boolean_t need_kds_wakeup = FALSE;
+ int s;
+
+ /*
+ * try to take the lock here to synchronize with the
+ * waiter entering the blocked state... use the try
+ * mode to prevent deadlocks caused by re-entering this
+ * routine due to various trace points triggered in the
+ * lck_spin_sleep_xxxx routines used to actually enter
+ * our wait condition... no problem if we fail,
+ * there will be lots of additional events coming in that
+ * will eventually succeed in grabbing this lock
+ */
+ s = ml_set_interrupts_enabled(FALSE);
+
+ if (lck_spin_try_lock(kdw_spin_lock)) {
+
+ if (kds_waiter && kd_ctrl_page.kds_inuse_count >= n_storage_threshold) {
+ kds_waiter = 0;
+ need_kds_wakeup = TRUE;
+ }
+ lck_spin_unlock(kdw_spin_lock);
+
+ ml_set_interrupts_enabled(s);
+
+ if (need_kds_wakeup == TRUE)
+ wakeup(&kds_waiter);
+ }
+ }
+}
+
+
+
+static void
+kernel_debug_internal(
+ uint32_t debugid,
+ uintptr_t arg1,
+ uintptr_t arg2,
+ uintptr_t arg3,
+ uintptr_t arg4,
+ uintptr_t arg5)
+{
+ struct proc *curproc;
+ uint64_t now;
+ uint32_t bindx;
+ boolean_t s;
+ kd_buf *kd;
+ int cpu;
+ struct kd_bufinfo *kdbp;
+ struct kd_storage *kdsp_actual;
+ union kds_ptr kds_raw;
+
+
+
+ if (kd_ctrl_page.kdebug_slowcheck) {
+
+ if (kdebug_enable & KDEBUG_ENABLE_CHUD) {
+ kd_chudhook_fn chudhook;
+ /*
+ * Mask interrupts to minimize the interval across
+ * which the driver providing the hook could be
+ * unloaded.
+ */
+ s = ml_set_interrupts_enabled(FALSE);
+ chudhook = kdebug_chudhook;
+ if (chudhook)
+ chudhook(debugid, arg1, arg2, arg3, arg4, arg5);
+ ml_set_interrupts_enabled(s);
+ }
+ if ( (kd_ctrl_page.kdebug_slowcheck & SLOW_NOLOG) || !(kdebug_enable & (KDEBUG_ENABLE_TRACE|KDEBUG_ENABLE_PPT)))
+ goto out1;
+
+ if ( !ml_at_interrupt_context()) {
+ if (kd_ctrl_page.kdebug_flags & KDBG_PIDCHECK) {
+ /*
+ * If kdebug flag is not set for current proc, return
+ */
+ curproc = current_proc();
+
+ if ((curproc && !(curproc->p_kdebug)) &&
+ ((debugid & 0xffff0000) != (MACHDBG_CODE(DBG_MACH_SCHED, 0) | DBG_FUNC_NONE)) &&
+ (debugid >> 24 != DBG_TRACE))
+ goto out1;
+ }
+ else if (kd_ctrl_page.kdebug_flags & KDBG_PIDEXCLUDE) {
+ /*
+ * If kdebug flag is set for current proc, return
+ */
+ curproc = current_proc();
+
+ if ((curproc && curproc->p_kdebug) &&
+ ((debugid & 0xffff0000) != (MACHDBG_CODE(DBG_MACH_SCHED, 0) | DBG_FUNC_NONE)) &&
+ (debugid >> 24 != DBG_TRACE))
+ goto out1;
+ }
+ }
+
+ if (kd_ctrl_page.kdebug_flags & KDBG_TYPEFILTER_CHECK) {
+ /* Always record trace system info */
+ if (EXTRACT_CLASS(debugid) == DBG_TRACE)
+ goto record_event;
+
+ if (isset(type_filter_bitmap, EXTRACT_CSC(debugid)))
+ goto record_event;
+ goto out1;
+ }
+ else if (kd_ctrl_page.kdebug_flags & KDBG_RANGECHECK) {
+ /* Always record trace system info */
+ if (EXTRACT_CLASS(debugid) == DBG_TRACE)
+ goto record_event;
+
+ if (debugid < kdlog_beg || debugid > kdlog_end)
+ goto out1;
+ }
+ else if (kd_ctrl_page.kdebug_flags & KDBG_VALCHECK) {
+ /* Always record trace system info */
+ if (EXTRACT_CLASS(debugid) == DBG_TRACE)
+ goto record_event;
+
+ if ((debugid & DBG_FUNC_MASK) != kdlog_value1 &&
+ (debugid & DBG_FUNC_MASK) != kdlog_value2 &&
+ (debugid & DBG_FUNC_MASK) != kdlog_value3 &&
+ (debugid & DBG_FUNC_MASK) != kdlog_value4)
+ goto out1;
+ }
+ }
+record_event:
+ disable_preemption();
+
+ if (kd_ctrl_page.enabled == 0)
+ goto out;
+
+ cpu = cpu_number();
+ kdbp = &kdbip[cpu];
+
+#if KDEBUG_MOJO_TRACE
+ if (kdebug_enable & KDEBUG_ENABLE_SERIAL)
+ kdebug_serial_print(cpu, debugid,
+ mach_absolute_time() & KDBG_TIMESTAMP_MASK,
+ arg1, arg2, arg3, arg4, arg5);
+#endif
+
+retry_q:
+ kds_raw = kdbp->kd_list_tail;
+
+ if (kds_raw.raw != KDS_PTR_NULL) {
+ kdsp_actual = POINTER_FROM_KDS_PTR(kds_raw);
+ bindx = kdsp_actual->kds_bufindx;
+ } else
+ kdsp_actual = NULL;
+
+ if (kdsp_actual == NULL || bindx >= EVENTS_PER_STORAGE_UNIT) {
+ if (allocate_storage_unit(cpu) == FALSE) {
+ /*
+ * this can only happen if wrapping
+ * has been disabled
+ */
+ goto out;
+ }
+ goto retry_q;
+ }
+ now = mach_absolute_time() & KDBG_TIMESTAMP_MASK;
+
+ if ( !OSCompareAndSwap(bindx, bindx + 1, &kdsp_actual->kds_bufindx))
+ goto retry_q;
+
+ kd = &kdsp_actual->kds_records[bindx];
+
+ kd->debugid = debugid;
+ kd->arg1 = arg1;
+ kd->arg2 = arg2;
+ kd->arg3 = arg3;
+ kd->arg4 = arg4;
+ kd->arg5 = arg5;
+
+ kdbg_set_timestamp_and_cpu(kd, now, cpu);
+
+ OSAddAtomic(1, &kdsp_actual->kds_bufcnt);
+out:
+ enable_preemption();
+out1:
+ if (kds_waiter && kd_ctrl_page.kds_inuse_count >= n_storage_threshold) {
+ uint32_t etype;
+ uint32_t stype;
+
+ etype = debugid & DBG_FUNC_MASK;
+ stype = debugid & DBG_SCALL_MASK;
+
+ if (etype == INTERRUPT || etype == MACH_vmfault ||
+ stype == BSC_SysCall || stype == MACH_SysCall) {
+
+ boolean_t need_kds_wakeup = FALSE;
+
+ /*
+ * try to take the lock here to synchronize with the
+ * waiter entering the blocked state... use the try
+ * mode to prevent deadlocks caused by re-entering this
+ * routine due to various trace points triggered in the
+ * lck_spin_sleep_xxxx routines used to actually enter
+ * one of our 2 wait conditions... no problem if we fail,
+ * there will be lots of additional events coming in that
+ * will eventually succeed in grabbing this lock
+ */
+ s = ml_set_interrupts_enabled(FALSE);
+
+ if (lck_spin_try_lock(kdw_spin_lock)) {
+
+ if (kds_waiter && kd_ctrl_page.kds_inuse_count >= n_storage_threshold) {
+ kds_waiter = 0;
+ need_kds_wakeup = TRUE;
+ }
+ lck_spin_unlock(kdw_spin_lock);
+ }
+ ml_set_interrupts_enabled(s);
+
+ if (need_kds_wakeup == TRUE)
+ wakeup(&kds_waiter);
+ }
+ }
+}
+
+void
+kernel_debug(
+ uint32_t debugid,
+ uintptr_t arg1,
+ uintptr_t arg2,
+ uintptr_t arg3,
+ uintptr_t arg4,
+ __unused uintptr_t arg5)
+{
+ kernel_debug_internal(debugid, arg1, arg2, arg3, arg4, (uintptr_t)thread_tid(current_thread()));
+}
+
+void
+kernel_debug1(
+ uint32_t debugid,
+ uintptr_t arg1,
+ uintptr_t arg2,
+ uintptr_t arg3,
+ uintptr_t arg4,
+ uintptr_t arg5)
+{
+ kernel_debug_internal(debugid, arg1, arg2, arg3, arg4, arg5);
+}
+
+void
+kernel_debug_string(const char *message)
+{
+ uintptr_t arg[4] = {0, 0, 0, 0};
+
+ /* Stuff the message string in the args and log it. */
+ strncpy((char *)arg, message, MIN(sizeof(arg), strlen(message)));
+ KERNEL_DEBUG_EARLY(
+ TRACE_INFO_STRING,
+ arg[0], arg[1], arg[2], arg[3]);
+}
+
+extern int master_cpu; /* MACH_KERNEL_PRIVATE */
+/*
+ * Used prior to start_kern_tracing() being called.
+ * Log temporarily into a static buffer.
+ */
+void
+kernel_debug_early(
+ uint32_t debugid,
+ uintptr_t arg1,
+ uintptr_t arg2,
+ uintptr_t arg3,
+ uintptr_t arg4)
+{
+ /* If tracing is already initialized, use it */
+ if (nkdbufs) {
+ KERNEL_DEBUG_CONSTANT(debugid, arg1, arg2, arg3, arg4, 0);
+ return;
+ }
+
+ /* Do nothing if the buffer is full or we're not on the boot cpu */
+ kd_early_overflow = kd_early_index >= KD_EARLY_BUFFER_MAX;
+ if (kd_early_overflow ||
+ cpu_number() != master_cpu)
+ return;
+
+ kd_early_buffer[kd_early_index].debugid = debugid;
+ kd_early_buffer[kd_early_index].timestamp = mach_absolute_time();
+ kd_early_buffer[kd_early_index].arg1 = arg1;
+ kd_early_buffer[kd_early_index].arg2 = arg2;
+ kd_early_buffer[kd_early_index].arg3 = arg3;
+ kd_early_buffer[kd_early_index].arg4 = arg4;
+ kd_early_buffer[kd_early_index].arg5 = 0;
+ kd_early_index++;
+}
+
+/*
+ * Transfen the contents of the temporary buffer into the trace buffers.
+ * Precede that by logging the rebase time (offset) - the TSC-based time (in ns)
+ * when mach_absolute_time is set to 0.
+ */
+static void
+kernel_debug_early_end(void)
+{
+ int i;
+
+ if (cpu_number() != master_cpu)
+ panic("kernel_debug_early_end() not call on boot processor");
+
+ /* Fake sentinel marking the start of kernel time relative to TSC */
+ kernel_debug_enter(
+ 0,
+ TRACE_TIMESTAMPS,
+ 0,
+ (uint32_t)(tsc_rebase_abs_time >> 32),
+ (uint32_t)tsc_rebase_abs_time,
+ 0,
+ 0,
+ 0);
+ for (i = 0; i < kd_early_index; i++) {
+ kernel_debug_enter(
+ 0,
+ kd_early_buffer[i].debugid,
+ kd_early_buffer[i].timestamp,
+ kd_early_buffer[i].arg1,
+ kd_early_buffer[i].arg2,
+ kd_early_buffer[i].arg3,
+ kd_early_buffer[i].arg4,
+ 0);
+ }
+
+ /* Cut events-lost event on overflow */
+ if (kd_early_overflow)
+ KERNEL_DEBUG_CONSTANT(
+ TRACE_LOST_EVENTS, 0, 0, 0, 0, 0);
+
+ /* This trace marks the start of kernel tracing */
+ kernel_debug_string("early trace done");
+}
+
+/*
+ * Support syscall SYS_kdebug_trace. U64->K32 args may get truncated in kdebug_trace64
+ */
+int
+kdebug_trace(struct proc *p, struct kdebug_trace_args *uap, int32_t *retval)
+{
+ struct kdebug_trace64_args uap64;
+
+ uap64.code = uap->code;
+ uap64.arg1 = uap->arg1;
+ uap64.arg2 = uap->arg2;
+ uap64.arg3 = uap->arg3;
+ uap64.arg4 = uap->arg4;
+
+ return kdebug_trace64(p, &uap64, retval);
+}
+
+/*
+ * Support syscall SYS_kdebug_trace64. 64-bit args on K32 will get truncated to fit in 32-bit record format.
+ */
+int kdebug_trace64(__unused struct proc *p, struct kdebug_trace64_args *uap, __unused int32_t *retval)
+{
+ uint8_t code_class;
+
+ /*
+ * Not all class are supported for injection from userspace, especially ones used by the core
+ * kernel tracing infrastructure.
+ */
+ code_class = EXTRACT_CLASS(uap->code);
+
+ switch (code_class) {
+ case DBG_TRACE:
+ return EPERM;
+ }
+
+ if ( __probable(kdebug_enable == 0) )
+ return(0);
+
+ kernel_debug_internal(uap->code, (uintptr_t)uap->arg1, (uintptr_t)uap->arg2, (uintptr_t)uap->arg3, (uintptr_t)uap->arg4, (uintptr_t)thread_tid(current_thread()));
+
+ return(0);
+}
+
+static void
+kdbg_lock_init(void)
+{
+ if (kd_ctrl_page.kdebug_flags & KDBG_LOCKINIT)
+ return;
+
+ /*
+ * allocate lock group attribute and group
+ */
+ kd_trace_mtx_sysctl_grp_attr = lck_grp_attr_alloc_init();
+ kd_trace_mtx_sysctl_grp = lck_grp_alloc_init("kdebug", kd_trace_mtx_sysctl_grp_attr);
+
+ /*
+ * allocate the lock attribute
+ */
+ kd_trace_mtx_sysctl_attr = lck_attr_alloc_init();
+
+
+ /*
+ * allocate and initialize mutex's
+ */
+ kd_trace_mtx_sysctl = lck_mtx_alloc_init(kd_trace_mtx_sysctl_grp, kd_trace_mtx_sysctl_attr);
+ kds_spin_lock = lck_spin_alloc_init(kd_trace_mtx_sysctl_grp, kd_trace_mtx_sysctl_attr);
+ kdw_spin_lock = lck_spin_alloc_init(kd_trace_mtx_sysctl_grp, kd_trace_mtx_sysctl_attr);
+
+ kd_ctrl_page.kdebug_flags |= KDBG_LOCKINIT;
+}
+
+
+int
+kdbg_bootstrap(boolean_t early_trace)
+{
+ kd_ctrl_page.kdebug_flags &= ~KDBG_WRAPPED;
+
+ return (create_buffers(early_trace));
+}
+
+int
+kdbg_reinit(boolean_t early_trace)
+{
+ int ret = 0;
+
+ /*
+ * Disable trace collecting
+ * First make sure we're not in
+ * the middle of cutting a trace
+ */
+ kdbg_set_tracing_enabled(FALSE, KDEBUG_ENABLE_TRACE);
+
+ /*
+ * make sure the SLOW_NOLOG is seen
+ * by everyone that might be trying
+ * to cut a trace..
+ */
+ IOSleep(100);
+
+ delete_buffers();
+
+ if ((kd_ctrl_page.kdebug_flags & KDBG_MAPINIT) && kd_mapsize && kd_mapptr) {
+ kmem_free(kernel_map, (vm_offset_t)kd_mapptr, kd_mapsize);
+ kd_ctrl_page.kdebug_flags &= ~KDBG_MAPINIT;
+ kd_mapsize = 0;
+ kd_mapptr = (kd_threadmap *) 0;
+ kd_mapcount = 0;
+ }
+ ret = kdbg_bootstrap(early_trace);
+
+ RAW_file_offset = 0;
+ RAW_file_written = 0;
+
+ return(ret);
+}
+
+void
+kdbg_trace_data(struct proc *proc, long *arg_pid)
+{
+ if (!proc)
+ *arg_pid = 0;
+ else
+ *arg_pid = proc->p_pid;
+}
+
+
+void
+kdbg_trace_string(struct proc *proc, long *arg1, long *arg2, long *arg3, long *arg4)
+{
+ char *dbg_nameptr;
+ int dbg_namelen;
+ long dbg_parms[4];
+
+ if (!proc) {
+ *arg1 = 0;
+ *arg2 = 0;
+ *arg3 = 0;
+ *arg4 = 0;
+ return;
+ }
+ /*
+ * Collect the pathname for tracing
+ */
+ dbg_nameptr = proc->p_comm;
+ dbg_namelen = (int)strlen(proc->p_comm);
+ dbg_parms[0]=0L;
+ dbg_parms[1]=0L;
+ dbg_parms[2]=0L;
+ dbg_parms[3]=0L;
+
+ if(dbg_namelen > (int)sizeof(dbg_parms))
+ dbg_namelen = (int)sizeof(dbg_parms);
+
+ strncpy((char *)dbg_parms, dbg_nameptr, dbg_namelen);
+
+ *arg1=dbg_parms[0];
+ *arg2=dbg_parms[1];
+ *arg3=dbg_parms[2];
+ *arg4=dbg_parms[3];
+}
+
+static void
+kdbg_resolve_map(thread_t th_act, void *opaque)
+{
+ kd_threadmap *mapptr;
+ krt_t *t = (krt_t *)opaque;
+
+ if (t->count < t->maxcount) {
+ mapptr = &t->map[t->count];
+ mapptr->thread = (uintptr_t)thread_tid(th_act);
+
+ (void) strlcpy (mapptr->command, t->atts->task_comm,
+ sizeof(t->atts->task_comm));
+ /*
+ * Some kernel threads have no associated pid.
+ * We still need to mark the entry as valid.
+ */
+ if (t->atts->pid)
+ mapptr->valid = t->atts->pid;
+ else
+ mapptr->valid = 1;
+
+ t->count++;
+ }
+}
+
+/*
+ *
+ * Writes a cpumap for the given iops_list/cpu_count to the provided buffer.
+ *
+ * You may provide a buffer and size, or if you set the buffer to NULL, a
+ * buffer of sufficient size will be allocated.
+ *
+ * If you provide a buffer and it is too small, sets cpumap_size to the number
+ * of bytes required and returns EINVAL.
+ *
+ * On success, if you provided a buffer, cpumap_size is set to the number of
+ * bytes written. If you did not provide a buffer, cpumap is set to the newly
+ * allocated buffer and cpumap_size is set to the number of bytes allocated.
+ *
+ * NOTE: It may seem redundant to pass both iops and a cpu_count.
+ *
+ * We may be reporting data from "now", or from the "past".
+ *
+ * The "now" data would be for something like kdbg_readcurcpumap().
+ * The "past" data would be for kdbg_readcpumap().
+ *
+ * If we do not pass both iops and cpu_count, and iops is NULL, this function
+ * will need to read "now" state to get the number of cpus, which would be in
+ * error if we were reporting "past" state.
+ */
+
+int
+kdbg_cpumap_init_internal(kd_iop_t* iops, uint32_t cpu_count, uint8_t** cpumap, uint32_t* cpumap_size)
+{
+ assert(cpumap);
+ assert(cpumap_size);
+ assert(cpu_count);
+ assert(!iops || iops->cpu_id + 1 == cpu_count);
+
+ uint32_t bytes_needed = sizeof(kd_cpumap_header) + cpu_count * sizeof(kd_cpumap);
+ uint32_t bytes_available = *cpumap_size;
+ *cpumap_size = bytes_needed;
+
+ if (*cpumap == NULL) {
+ if (kmem_alloc(kernel_map, (vm_offset_t*)cpumap, (vm_size_t)*cpumap_size) != KERN_SUCCESS) {
+ return ENOMEM;
+ }
+ } else if (bytes_available < bytes_needed) {
+ return EINVAL;
+ }
+
+ kd_cpumap_header* header = (kd_cpumap_header*)(uintptr_t)*cpumap;
+
+ header->version_no = RAW_VERSION1;
+ header->cpu_count = cpu_count;
+
+ kd_cpumap* cpus = (kd_cpumap*)&header[1];
+
+ int32_t index = cpu_count - 1;
+ while (iops) {
+ cpus[index].cpu_id = iops->cpu_id;
+ cpus[index].flags = KDBG_CPUMAP_IS_IOP;
+ bzero(cpus[index].name, sizeof(cpus->name));
+ strlcpy(cpus[index].name, iops->callback.iop_name, sizeof(cpus->name));
+
+ iops = iops->next;
+ index--;
+ }
+
+ while (index >= 0) {
+ cpus[index].cpu_id = index;
+ cpus[index].flags = 0;
+ bzero(cpus[index].name, sizeof(cpus->name));
+ strlcpy(cpus[index].name, "AP", sizeof(cpus->name));
+
+ index--;
+ }
+
+ return KERN_SUCCESS;
+}
+
+void
+kdbg_thrmap_init(void)
+{
+ if (kd_ctrl_page.kdebug_flags & KDBG_MAPINIT)
+ return;
+
+ kd_mapptr = kdbg_thrmap_init_internal(0, &kd_mapsize, &kd_mapcount);
+
+ if (kd_mapptr)
+ kd_ctrl_page.kdebug_flags |= KDBG_MAPINIT;
+}
+
+
+kd_threadmap* kdbg_thrmap_init_internal(unsigned int count, unsigned int *mapsize, unsigned int *mapcount)
+{
+ kd_threadmap *mapptr;
+ struct proc *p;
+ struct krt akrt;
+ int tts_count; /* number of task-to-string structures */
+ struct tts *tts_mapptr;
+ unsigned int tts_mapsize = 0;
+ int i;
+ vm_offset_t kaddr;
+
+ /*
+ * need to use PROC_SCANPROCLIST with proc_iterate
+ */
+ proc_list_lock();
+
+ /*
+ * Calculate the sizes of map buffers
+ */
+ for (p = allproc.lh_first, *mapcount=0, tts_count=0; p; p = p->p_list.le_next) {
+ *mapcount += get_task_numacts((task_t)p->task);
+ tts_count++;
+ }
+ proc_list_unlock();
+
+ /*
+ * The proc count could change during buffer allocation,
+ * so introduce a small fudge factor to bump up the
+ * buffer sizes. This gives new tasks some chance of
+ * making into the tables. Bump up by 25%.
+ */
+ *mapcount += *mapcount/4;
+ tts_count += tts_count/4;
+
+ *mapsize = *mapcount * sizeof(kd_threadmap);
+
+ if (count && count < *mapcount)
+ return (0);
+
+ if ((kmem_alloc(kernel_map, &kaddr, (vm_size_t)*mapsize) == KERN_SUCCESS)) {
+ bzero((void *)kaddr, *mapsize);
+ mapptr = (kd_threadmap *)kaddr;
+ } else
+ return (0);
+
+ tts_mapsize = tts_count * sizeof(struct tts);
+
+ if ((kmem_alloc(kernel_map, &kaddr, (vm_size_t)tts_mapsize) == KERN_SUCCESS)) {
+ bzero((void *)kaddr, tts_mapsize);
+ tts_mapptr = (struct tts *)kaddr;
+ } else {
+ kmem_free(kernel_map, (vm_offset_t)mapptr, *mapsize);
+
+ return (0);
+ }
+ /*
+ * We need to save the procs command string
+ * and take a reference for each task associated
+ * with a valid process
+ */
+
+ proc_list_lock();
+
+ /*
+ * should use proc_iterate
+ */
+ for (p = allproc.lh_first, i=0; p && i < tts_count; p = p->p_list.le_next) {
+ if (p->p_lflag & P_LEXIT)
+ continue;
+
+ if (p->task) {
+ task_reference(p->task);
+ tts_mapptr[i].task = p->task;
+ tts_mapptr[i].pid = p->p_pid;
+ (void)strlcpy(tts_mapptr[i].task_comm, p->p_comm, sizeof(tts_mapptr[i].task_comm));
+ i++;
+ }
+ }
+ tts_count = i;
+
+ proc_list_unlock();
+
+ /*
+ * Initialize thread map data
+ */
+ akrt.map = mapptr;
+ akrt.count = 0;
+ akrt.maxcount = *mapcount;
+
+ for (i = 0; i < tts_count; i++) {
+ akrt.atts = &tts_mapptr[i];
+ task_act_iterate_wth_args(tts_mapptr[i].task, kdbg_resolve_map, &akrt);
+ task_deallocate((task_t) tts_mapptr[i].task);
+ }
+ kmem_free(kernel_map, (vm_offset_t)tts_mapptr, tts_mapsize);
+
+ *mapcount = akrt.count;
+
+ return (mapptr);
+}
+
+static void
+kdbg_clear(void)
+{
+ /*
+ * Clean up the trace buffer
+ * First make sure we're not in
+ * the middle of cutting a trace
+ */
+ kdbg_set_tracing_enabled(FALSE, KDEBUG_ENABLE_TRACE);
+
+ /*
+ * make sure the SLOW_NOLOG is seen
+ * by everyone that might be trying
+ * to cut a trace..
+ */
+ IOSleep(100);
+
+ global_state_pid = -1;
+ kd_ctrl_page.kdebug_flags &= (unsigned int)~KDBG_CKTYPES;
+ kd_ctrl_page.kdebug_flags &= ~(KDBG_NOWRAP | KDBG_RANGECHECK | KDBG_VALCHECK);
+ kd_ctrl_page.kdebug_flags &= ~(KDBG_PIDCHECK | KDBG_PIDEXCLUDE);
+
+ kdbg_disable_typefilter();
+
+ delete_buffers();
+ nkdbufs = 0;
+
+ /* Clean up the thread map buffer */
+ kd_ctrl_page.kdebug_flags &= ~KDBG_MAPINIT;
+ if (kd_mapptr) {
+ kmem_free(kernel_map, (vm_offset_t)kd_mapptr, kd_mapsize);
+ kd_mapptr = (kd_threadmap *) 0;
+ }
+ kd_mapsize = 0;
+ kd_mapcount = 0;
+
+ RAW_file_offset = 0;
+ RAW_file_written = 0;
+}
+
+int
+kdbg_setpid(kd_regtype *kdr)
+{
+ pid_t pid;
+ int flag, ret=0;
+ struct proc *p;
+
+ pid = (pid_t)kdr->value1;
+ flag = (int)kdr->value2;
+
+ if (pid > 0) {
+ if ((p = proc_find(pid)) == NULL)
+ ret = ESRCH;
+ else {
+ if (flag == 1) {
+ /*
+ * turn on pid check for this and all pids
+ */
+ kd_ctrl_page.kdebug_flags |= KDBG_PIDCHECK;
+ kd_ctrl_page.kdebug_flags &= ~KDBG_PIDEXCLUDE;
+ kdbg_set_flags(SLOW_CHECKS, 0, TRUE);
+
+ p->p_kdebug = 1;
+ } else {
+ /*
+ * turn off pid check for this pid value
+ * Don't turn off all pid checking though
+ *
+ * kd_ctrl_page.kdebug_flags &= ~KDBG_PIDCHECK;
+ */
+ p->p_kdebug = 0;
+ }
+ proc_rele(p);
+ }
+ }
+ else
+ ret = EINVAL;
+
+ return(ret);
+}
+
+/* This is for pid exclusion in the trace buffer */
+int
+kdbg_setpidex(kd_regtype *kdr)
+{
+ pid_t pid;
+ int flag, ret=0;
+ struct proc *p;
+
+ pid = (pid_t)kdr->value1;
+ flag = (int)kdr->value2;
+
+ if (pid > 0) {
+ if ((p = proc_find(pid)) == NULL)
+ ret = ESRCH;
+ else {
+ if (flag == 1) {
+ /*
+ * turn on pid exclusion
+ */
+ kd_ctrl_page.kdebug_flags |= KDBG_PIDEXCLUDE;
+ kd_ctrl_page.kdebug_flags &= ~KDBG_PIDCHECK;
+ kdbg_set_flags(SLOW_CHECKS, 0, TRUE);
+
+ p->p_kdebug = 1;
+ }
+ else {
+ /*
+ * turn off pid exclusion for this pid value
+ * Don't turn off all pid exclusion though
+ *
+ * kd_ctrl_page.kdebug_flags &= ~KDBG_PIDEXCLUDE;
+ */
+ p->p_kdebug = 0;
+ }
+ proc_rele(p);
+ }
+ } else
+ ret = EINVAL;
+
+ return(ret);
+}
+
+
+/*
+ * This is for setting a maximum decrementer value
+ */
+int
+kdbg_setrtcdec(kd_regtype *kdr)
+{
+ int ret = 0;
+ natural_t decval;
+
+ decval = (natural_t)kdr->value1;
+
+ if (decval && decval < KDBG_MINRTCDEC)
+ ret = EINVAL;
+ else
+ ret = ENOTSUP;
+
+ return(ret);
+}
+
+int
+kdbg_enable_typefilter(void)
+{
+ if (kd_ctrl_page.kdebug_flags & KDBG_TYPEFILTER_CHECK) {
+ /* free the old filter */
+ kdbg_disable_typefilter();
+ }
+
+ if (kmem_alloc(kernel_map, (vm_offset_t *)&type_filter_bitmap, KDBG_TYPEFILTER_BITMAP_SIZE) != KERN_SUCCESS) {
+ return ENOSPC;
+ }
+
+ bzero(type_filter_bitmap, KDBG_TYPEFILTER_BITMAP_SIZE);
+
+ /* Turn off range and value checks */
+ kd_ctrl_page.kdebug_flags &= ~(KDBG_RANGECHECK | KDBG_VALCHECK);
+
+ /* Enable filter checking */
+ kd_ctrl_page.kdebug_flags |= KDBG_TYPEFILTER_CHECK;
+ kdbg_set_flags(SLOW_CHECKS, 0, TRUE);
+ return 0;
+}
+
+int
+kdbg_disable_typefilter(void)
+{
+ /* Disable filter checking */
+ kd_ctrl_page.kdebug_flags &= ~KDBG_TYPEFILTER_CHECK;
+
+ /* Turn off slow checks unless pid checks are using them */
+ if ( (kd_ctrl_page.kdebug_flags & (KDBG_PIDCHECK | KDBG_PIDEXCLUDE)) )
+ kdbg_set_flags(SLOW_CHECKS, 0, TRUE);
+ else
+ kdbg_set_flags(SLOW_CHECKS, 0, FALSE);
+
+ if(type_filter_bitmap == NULL)
+ return 0;
+
+ vm_offset_t old_bitmap = (vm_offset_t)type_filter_bitmap;
+ type_filter_bitmap = NULL;
+
+ kmem_free(kernel_map, old_bitmap, KDBG_TYPEFILTER_BITMAP_SIZE);
+ return 0;
+}
+
+int
+kdbg_setreg(kd_regtype * kdr)
+{
+ int ret=0;
+ unsigned int val_1, val_2, val;
+ switch (kdr->type) {
+
+ case KDBG_CLASSTYPE :