+ if (kdbp_try->kd_list_head.raw == KDS_PTR_NULL) {
+ /*
+ * no storage unit to steal
+ */
+ continue;
+ }
+
+ kdsp_actual = POINTER_FROM_KDS_PTR(kdbp_try->kd_list_head);
+
+ if (kdsp_actual->kds_bufcnt < EVENTS_PER_STORAGE_UNIT) {
+ /*
+ * make sure we don't steal the storage unit
+ * being actively recorded to... need to
+ * move on because we don't want an out-of-order
+ * set of events showing up later
+ */
+ continue;
+ }
+ ts = kdbg_get_timestamp(&kdsp_actual->kds_records[0]);
+
+ if (ts < oldest_ts) {
+ /*
+ * when 'wrapping', we want to steal the
+ * storage unit that has the 'earliest' time
+ * associated with it (first event time)
+ */
+ oldest_ts = ts;
+ kdbp_vict = kdbp_try;
+ }
+ }
+ if (kdbp_vict == NULL) {
+ kdebug_enable = 0;
+ kd_ctrl_page.enabled = 0;
+ retval = FALSE;
+ goto out;
+ }
+ kdsp = kdbp_vict->kd_list_head;
+ kdsp_actual = POINTER_FROM_KDS_PTR(kdsp);
+
+ kdbp_vict->kd_list_head = kdsp_actual->kds_next;
+
+ kd_ctrl_page.kdebug_flags |= KDBG_WRAPPED;
+ }
+ kdsp_actual->kds_timestamp = mach_absolute_time();
+ kdsp_actual->kds_next.raw = KDS_PTR_NULL;
+ kdsp_actual->kds_bufcnt = 0;
+ kdsp_actual->kds_readlast = 0;
+
+ kdsp_actual->kds_lostevents = kdbp->kd_lostevents;
+ kdbp->kd_lostevents = FALSE;
+ kdsp_actual->kds_bufindx = 0;
+
+ if (kdbp->kd_list_head.raw == KDS_PTR_NULL)
+ kdbp->kd_list_head = kdsp;
+ else
+ POINTER_FROM_KDS_PTR(kdbp->kd_list_tail)->kds_next = kdsp;
+ kdbp->kd_list_tail = kdsp;
+out:
+ lck_spin_unlock(kds_spin_lock);
+ ml_set_interrupts_enabled(s);
+
+ return (retval);
+}
+#endif
+
+void
+kernel_debug_internal(
+ uint32_t debugid,
+ uintptr_t arg1,
+ uintptr_t arg2,
+ uintptr_t arg3,
+ uintptr_t arg4,
+ uintptr_t arg5,
+ int entropy_flag);
+
+__attribute__((always_inline)) void
+kernel_debug_internal(
+ uint32_t debugid,
+ uintptr_t arg1,
+ uintptr_t arg2,
+ uintptr_t arg3,
+ uintptr_t arg4,
+ uintptr_t arg5,
+ int entropy_flag)
+{
+ 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;
+
+
+ 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 ((kdebug_enable & KDEBUG_ENABLE_ENTROPY) && entropy_flag) {
+
+ now = mach_absolute_time();
+
+ s = ml_set_interrupts_enabled(FALSE);
+ lck_spin_lock(kds_spin_lock);
+
+ if (kdebug_enable & KDEBUG_ENABLE_ENTROPY) {
+
+ if (kd_entropy_indx < kd_entropy_count) {
+ kd_entropy_buffer[kd_entropy_indx] = now;
+ kd_entropy_indx++;
+ }
+ if (kd_entropy_indx == kd_entropy_count) {
+ /*
+ * Disable entropy collection
+ */
+ kdebug_enable &= ~KDEBUG_ENABLE_ENTROPY;
+ kd_ctrl_page.kdebug_slowcheck &= ~SLOW_ENTROPY;
+ }
+ }
+ lck_spin_unlock(kds_spin_lock);
+ ml_set_interrupts_enabled(s);
+ }
+ if ( (kd_ctrl_page.kdebug_slowcheck & SLOW_NOLOG) || !(kdebug_enable & KDEBUG_ENABLE_TRACE))
+ goto out1;