+ struct kd_resolver resolver = {
+ .krs_map = map, .krs_count = 0, .krs_maxcount = nthreads,
+ };
+
+ for (int i = 0; i < ntasks; i++) {
+ struct kd_task_name *cur_task = &task_names[i];
+ resolver.krs_task = cur_task;
+ task_act_iterate_wth_args(cur_task->ktn_task, kd_resolve_map,
+ &resolver);
+ task_deallocate(cur_task->ktn_task);
+ }
+
+ return resolver.krs_count;
+}
+
+static kd_threadmap *
+kdbg_thrmap_init_internal(size_t maxthreads, vm_size_t *mapsize,
+ vm_size_t *mapcount)
+{
+ kd_threadmap *thread_map = NULL;
+ struct kd_task_name *task_names;
+ vm_size_t names_size = 0;
+
+ assert(mapsize != NULL);
+ assert(mapcount != NULL);
+
+ vm_size_t nthreads = threads_count;
+ vm_size_t ntasks = tasks_count;
+
+ /*
+ * Allow 25% more threads and tasks to be created between now and taking the
+ * proc_list_lock.
+ */
+ if (os_add_overflow(nthreads, nthreads / 4, &nthreads) ||
+ os_add_overflow(ntasks, ntasks / 4, &ntasks)) {
+ return NULL;
+ }
+
+ *mapcount = nthreads;
+ if (os_mul_overflow(nthreads, sizeof(kd_threadmap), mapsize)) {
+ return NULL;
+ }
+ if (os_mul_overflow(ntasks, sizeof(task_names[0]), &names_size)) {
+ return NULL;
+ }
+
+ /*
+ * Wait until the out-parameters have been filled with the needed size to
+ * do the bounds checking on the provided maximum.
+ */
+ if (maxthreads != 0 && maxthreads < nthreads) {
+ return NULL;
+ }
+
+ thread_map = kalloc_tag(*mapsize, VM_KERN_MEMORY_DIAG);
+ bzero(thread_map, *mapsize);
+ task_names = kheap_alloc(KHEAP_TEMP, names_size, Z_WAITOK | Z_ZERO);
+ ntasks = kd_resolve_tasks(task_names, ntasks);
+ *mapcount = kd_resolve_threads(thread_map, task_names, ntasks, nthreads);
+ kheap_free(KHEAP_TEMP, task_names, names_size);
+ return thread_map;
+}
+
+static void
+kdbg_clear(void)
+{
+ /*
+ * Clean up the trace buffer
+ * First make sure we're not in
+ * the middle of cutting a trace
+ */
+ kernel_debug_disable();
+ kdbg_disable_typefilter();
+
+ /*
+ * make sure the SLOW_NOLOG is seen
+ * by everyone that might be trying
+ * to cut a trace..
+ */
+ IOSleep(100);
+
+ /* reset kdebug state for each process */
+ if (kd_ctrl_page.kdebug_flags & (KDBG_PIDCHECK | KDBG_PIDEXCLUDE)) {
+ proc_list_lock();
+ proc_t p;
+ ALLPROC_FOREACH(p) {
+ p->p_kdebug = 0;
+ }
+ proc_list_unlock();
+ }
+
+ 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);
+
+ kd_ctrl_page.oldest_time = 0;
+
+ delete_buffers();
+ nkdbufs = 0;
+
+ /* Clean up the thread map buffer */
+ kdbg_clear_thread_map();
+
+ RAW_file_offset = 0;
+ RAW_file_written = 0;
+}
+
+void
+kdebug_reset(void)
+{
+ ktrace_assert_lock_held();
+
+ kdbg_clear();
+ if (kdbg_typefilter) {
+ typefilter_reject_all(kdbg_typefilter);
+ typefilter_allow_class(kdbg_typefilter, DBG_TRACE);
+ }
+}
+
+void
+kdebug_free_early_buf(void)
+{
+#if defined(__x86_64__)
+ /*
+ * Make Intel aware that the early buffer is no longer being used. ARM
+ * handles this as part of the BOOTDATA segment.
+ */
+ ml_static_mfree((vm_offset_t)&kd_early_buffer, sizeof(kd_early_buffer));
+#endif /* defined(__x86_64__) */
+}
+
+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;
+}
+
+/*
+ * The following functions all operate on the "global" typefilter singleton.
+ */
+
+/*
+ * The tf param is optional, you may pass either a valid typefilter or NULL.
+ * If you pass a valid typefilter, you release ownership of that typefilter.
+ */
+static int
+kdbg_initialize_typefilter(typefilter_t tf)
+{
+ ktrace_assert_lock_held();
+ assert(!kdbg_typefilter);
+ assert(!kdbg_typefilter_memory_entry);
+ typefilter_t deallocate_tf = NULL;
+
+ if (!tf && ((tf = deallocate_tf = typefilter_create()) == NULL)) {
+ return ENOMEM;
+ }
+
+ if ((kdbg_typefilter_memory_entry = typefilter_create_memory_entry(tf)) == MACH_PORT_NULL) {
+ if (deallocate_tf) {
+ typefilter_deallocate(deallocate_tf);
+ }
+ return ENOMEM;
+ }
+
+ /*
+ * The atomic store closes a race window with
+ * the kdebug_typefilter syscall, which assumes
+ * that any non-null kdbg_typefilter means a
+ * valid memory_entry is available.
+ */
+ os_atomic_store(&kdbg_typefilter, tf, release);
+
+ return KERN_SUCCESS;
+}
+
+static int
+kdbg_copyin_typefilter(user_addr_t addr, size_t size)
+{
+ int ret = ENOMEM;
+ typefilter_t tf;
+
+ ktrace_assert_lock_held();
+
+ if (size != KDBG_TYPEFILTER_BITMAP_SIZE) {
+ return EINVAL;
+ }
+
+ if ((tf = typefilter_create())) {
+ if ((ret = copyin(addr, tf, KDBG_TYPEFILTER_BITMAP_SIZE)) == 0) {
+ /* The kernel typefilter must always allow DBG_TRACE */
+ typefilter_allow_class(tf, DBG_TRACE);
+
+ /*
+ * If this is the first typefilter; claim it.
+ * Otherwise copy and deallocate.
+ *
+ * Allocating a typefilter for the copyin allows
+ * the kernel to hold the invariant that DBG_TRACE
+ * must always be allowed.
+ */
+ if (!kdbg_typefilter) {
+ if ((ret = kdbg_initialize_typefilter(tf))) {
+ return ret;
+ }
+ tf = NULL;
+ } else {
+ typefilter_copy(kdbg_typefilter, tf);
+ }
+
+ kdbg_enable_typefilter();
+ kdbg_iop_list_callback(kd_ctrl_page.kdebug_iops, KD_CALLBACK_TYPEFILTER_CHANGED, kdbg_typefilter);
+ }
+
+ if (tf) {
+ typefilter_deallocate(tf);
+ }
+ }
+
+ return ret;
+}
+
+/*
+ * Enable the flags in the control page for the typefilter. Assumes that
+ * kdbg_typefilter has already been allocated, so events being written
+ * don't see a bad typefilter.
+ */
+static void
+kdbg_enable_typefilter(void)
+{
+ assert(kdbg_typefilter);
+ kd_ctrl_page.kdebug_flags &= ~(KDBG_RANGECHECK | KDBG_VALCHECK);
+ kd_ctrl_page.kdebug_flags |= KDBG_TYPEFILTER_CHECK;
+ kdbg_set_flags(SLOW_CHECKS, 0, true);
+ commpage_update_kdebug_state();
+}
+
+/*
+ * Disable the flags in the control page for the typefilter. The typefilter
+ * may be safely deallocated shortly after this function returns.
+ */
+static void
+kdbg_disable_typefilter(void)
+{
+ bool notify_iops = kd_ctrl_page.kdebug_flags & KDBG_TYPEFILTER_CHECK;
+ kd_ctrl_page.kdebug_flags &= ~KDBG_TYPEFILTER_CHECK;
+
+ 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);
+ }
+ commpage_update_kdebug_state();
+
+ if (notify_iops) {
+ /*
+ * Notify IOPs that the typefilter will now allow everything.
+ * Otherwise, they won't know a typefilter is no longer in
+ * effect.
+ */
+ typefilter_allow_all(kdbg_typefilter);
+ kdbg_iop_list_callback(kd_ctrl_page.kdebug_iops,
+ KD_CALLBACK_TYPEFILTER_CHANGED, kdbg_typefilter);
+ }
+}
+
+uint32_t
+kdebug_commpage_state(void)
+{
+ if (kdebug_enable) {
+ if (kd_ctrl_page.kdebug_flags & KDBG_TYPEFILTER_CHECK) {
+ return KDEBUG_COMMPAGE_ENABLE_TYPEFILTER | KDEBUG_COMMPAGE_ENABLE_TRACE;
+ }
+
+ return KDEBUG_COMMPAGE_ENABLE_TRACE;
+ }
+
+ return 0;
+}
+
+int
+kdbg_setreg(kd_regtype * kdr)
+{
+ int ret = 0;
+ unsigned int val_1, val_2, val;
+ switch (kdr->type) {
+ case KDBG_CLASSTYPE:
+ val_1 = (kdr->value1 & 0xff);
+ val_2 = (kdr->value2 & 0xff);
+ kdlog_beg = (val_1 << 24);
+ kdlog_end = (val_2 << 24);
+ kd_ctrl_page.kdebug_flags &= (unsigned int)~KDBG_CKTYPES;
+ kd_ctrl_page.kdebug_flags &= ~KDBG_VALCHECK; /* Turn off specific value check */
+ kd_ctrl_page.kdebug_flags |= (KDBG_RANGECHECK | KDBG_CLASSTYPE);
+ kdbg_set_flags(SLOW_CHECKS, 0, true);
+ break;
+ case KDBG_SUBCLSTYPE:
+ val_1 = (kdr->value1 & 0xff);
+ val_2 = (kdr->value2 & 0xff);
+ val = val_2 + 1;
+ kdlog_beg = ((val_1 << 24) | (val_2 << 16));
+ kdlog_end = ((val_1 << 24) | (val << 16));
+ kd_ctrl_page.kdebug_flags &= (unsigned int)~KDBG_CKTYPES;
+ kd_ctrl_page.kdebug_flags &= ~KDBG_VALCHECK; /* Turn off specific value check */
+ kd_ctrl_page.kdebug_flags |= (KDBG_RANGECHECK | KDBG_SUBCLSTYPE);
+ kdbg_set_flags(SLOW_CHECKS, 0, true);
+ break;
+ case KDBG_RANGETYPE:
+ kdlog_beg = (kdr->value1);
+ kdlog_end = (kdr->value2);
+ kd_ctrl_page.kdebug_flags &= (unsigned int)~KDBG_CKTYPES;
+ kd_ctrl_page.kdebug_flags &= ~KDBG_VALCHECK; /* Turn off specific value check */
+ kd_ctrl_page.kdebug_flags |= (KDBG_RANGECHECK | KDBG_RANGETYPE);
+ kdbg_set_flags(SLOW_CHECKS, 0, true);
+ break;
+ case KDBG_VALCHECK:
+ kdlog_value1 = (kdr->value1);
+ kdlog_value2 = (kdr->value2);
+ kdlog_value3 = (kdr->value3);
+ kdlog_value4 = (kdr->value4);
+ kd_ctrl_page.kdebug_flags &= (unsigned int)~KDBG_CKTYPES;
+ kd_ctrl_page.kdebug_flags &= ~KDBG_RANGECHECK; /* Turn off range check */
+ kd_ctrl_page.kdebug_flags |= KDBG_VALCHECK; /* Turn on specific value check */
+ kdbg_set_flags(SLOW_CHECKS, 0, true);
+ break;
+ case KDBG_TYPENONE:
+ kd_ctrl_page.kdebug_flags &= (unsigned int)~KDBG_CKTYPES;
+
+ if ((kd_ctrl_page.kdebug_flags & (KDBG_RANGECHECK | KDBG_VALCHECK |
+ KDBG_PIDCHECK | KDBG_PIDEXCLUDE |
+ KDBG_TYPEFILTER_CHECK))) {
+ kdbg_set_flags(SLOW_CHECKS, 0, true);
+ } else {
+ kdbg_set_flags(SLOW_CHECKS, 0, false);
+ }
+
+ kdlog_beg = 0;
+ kdlog_end = 0;
+ break;
+ default:
+ ret = EINVAL;
+ break;
+ }
+ return ret;
+}
+
+static int
+kdbg_write_to_vnode(caddr_t buffer, size_t size, vnode_t vp, vfs_context_t ctx, off_t file_offset)
+{
+ assert(size < INT_MAX);
+ return vn_rdwr(UIO_WRITE, vp, buffer, (int)size, file_offset, UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT,
+ vfs_context_ucred(ctx), (int *) 0, vfs_context_proc(ctx));
+}
+
+int
+kdbg_write_v3_chunk_header(user_addr_t buffer, uint32_t tag, uint32_t sub_tag, uint64_t length, vnode_t vp, vfs_context_t ctx)
+{
+ int ret = KERN_SUCCESS;
+ kd_chunk_header_v3 header = {
+ .tag = tag,
+ .sub_tag = sub_tag,
+ .length = length,
+ };
+
+ // Check that only one of them is valid
+ assert(!buffer ^ !vp);
+ assert((vp == NULL) || (ctx != NULL));
+
+ // Write the 8-byte future_chunk_timestamp field in the payload
+ if (buffer || vp) {
+ if (vp) {
+ ret = kdbg_write_to_vnode((caddr_t)&header, sizeof(kd_chunk_header_v3), vp, ctx, RAW_file_offset);
+ if (ret) {
+ goto write_error;
+ }
+ RAW_file_offset += (sizeof(kd_chunk_header_v3));
+ } else {
+ ret = copyout(&header, buffer, sizeof(kd_chunk_header_v3));
+ if (ret) {
+ goto write_error;
+ }
+ }
+ }
+write_error:
+ return ret;
+}
+
+static int
+kdbg_write_v3_chunk_to_fd(uint32_t tag, uint32_t sub_tag, uint64_t length, void *payload, uint64_t payload_size, int fd)
+{
+ proc_t p;
+ struct vfs_context context;
+ struct fileproc *fp;
+ vnode_t vp;
+ p = current_proc();
+
+ if (fp_get_ftype(p, fd, DTYPE_VNODE, EBADF, &fp)) {
+ return EBADF;
+ }
+
+ vp = fp->fp_glob->fg_data;
+ context.vc_thread = current_thread();
+ context.vc_ucred = fp->fp_glob->fg_cred;
+
+ if ((vnode_getwithref(vp)) == 0) {
+ RAW_file_offset = fp->fp_glob->fg_offset;
+
+ kd_chunk_header_v3 chunk_header = {
+ .tag = tag,
+ .sub_tag = sub_tag,
+ .length = length,
+ };
+
+ int ret = kdbg_write_to_vnode((caddr_t) &chunk_header, sizeof(kd_chunk_header_v3), vp, &context, RAW_file_offset);
+ if (!ret) {
+ RAW_file_offset += sizeof(kd_chunk_header_v3);
+ }
+
+ ret = kdbg_write_to_vnode((caddr_t) payload, (size_t) payload_size, vp, &context, RAW_file_offset);
+ if (!ret) {
+ RAW_file_offset += payload_size;
+ }
+
+ fp->fp_glob->fg_offset = RAW_file_offset;
+ vnode_put(vp);
+ }
+
+ fp_drop(p, fd, fp, 0);
+ return KERN_SUCCESS;
+}
+
+user_addr_t
+kdbg_write_v3_event_chunk_header(user_addr_t buffer, uint32_t tag, uint64_t length, vnode_t vp, vfs_context_t ctx)
+{
+ uint64_t future_chunk_timestamp = 0;
+ length += sizeof(uint64_t);
+
+ if (kdbg_write_v3_chunk_header(buffer, tag, V3_EVENT_DATA_VERSION, length, vp, ctx)) {
+ return 0;
+ }
+ if (buffer) {
+ buffer += sizeof(kd_chunk_header_v3);
+ }
+
+ // Check that only one of them is valid
+ assert(!buffer ^ !vp);
+ assert((vp == NULL) || (ctx != NULL));
+
+ // Write the 8-byte future_chunk_timestamp field in the payload
+ if (buffer || vp) {
+ if (vp) {
+ int ret = kdbg_write_to_vnode((caddr_t)&future_chunk_timestamp, sizeof(uint64_t), vp, ctx, RAW_file_offset);
+ if (!ret) {
+ RAW_file_offset += (sizeof(uint64_t));
+ }
+ } else {
+ if (copyout(&future_chunk_timestamp, buffer, sizeof(uint64_t))) {
+ return 0;
+ }
+ }
+ }
+
+ return buffer + sizeof(uint64_t);
+}
+
+int
+kdbg_write_v3_header(user_addr_t user_header, size_t *user_header_size, int fd)
+{
+ int ret = KERN_SUCCESS;
+
+ uint8_t* cpumap = 0;
+ uint32_t cpumap_size = 0;
+ uint32_t thrmap_size = 0;
+
+ size_t bytes_needed = 0;
+
+ // Check that only one of them is valid
+ assert(!user_header ^ !fd);
+ assert(user_header_size);
+
+ if (!(kd_ctrl_page.kdebug_flags & KDBG_BUFINIT)) {
+ ret = EINVAL;
+ goto bail;
+ }
+
+ if (!(user_header || fd)) {
+ ret = EINVAL;
+ goto bail;
+ }
+
+ // Initialize the cpu map
+ ret = kdbg_cpumap_init_internal(kd_ctrl_page.kdebug_iops, kd_ctrl_page.kdebug_cpus, &cpumap, &cpumap_size);
+ if (ret != KERN_SUCCESS) {
+ goto bail;
+ }
+
+ // Check if a thread map is initialized
+ if (!kd_mapptr) {
+ ret = EINVAL;
+ goto bail;
+ }
+ if (os_mul_overflow(kd_mapcount, sizeof(kd_threadmap), &thrmap_size)) {
+ ret = ERANGE;
+ goto bail;
+ }
+
+ mach_timebase_info_data_t timebase = {0, 0};
+ clock_timebase_info(&timebase);
+
+ // Setup the header.
+ // See v3 header description in sys/kdebug.h for more inforamtion.
+ kd_header_v3 header = {
+ .tag = RAW_VERSION3,
+ .sub_tag = V3_HEADER_VERSION,
+ .length = (sizeof(kd_header_v3) + cpumap_size - sizeof(kd_cpumap_header)),
+ .timebase_numer = timebase.numer,
+ .timebase_denom = timebase.denom,
+ .timestamp = 0, /* FIXME rdar://problem/22053009 */
+ .walltime_secs = 0,
+ .walltime_usecs = 0,
+ .timezone_minuteswest = 0,
+ .timezone_dst = 0,
+#if defined(__LP64__)
+ .flags = 1,
+#else
+ .flags = 0,
+#endif
+ };
+
+ // If its a buffer, check if we have enough space to copy the header and the maps.
+ if (user_header) {
+ bytes_needed = (size_t)header.length + thrmap_size + (2 * sizeof(kd_chunk_header_v3));
+ if (*user_header_size < bytes_needed) {
+ ret = EINVAL;
+ goto bail;
+ }
+ }
+
+ // Start writing the header
+ if (fd) {
+ void *hdr_ptr = (void *)(((uintptr_t) &header) + sizeof(kd_chunk_header_v3));
+ size_t payload_size = (sizeof(kd_header_v3) - sizeof(kd_chunk_header_v3));
+
+ ret = kdbg_write_v3_chunk_to_fd(RAW_VERSION3, V3_HEADER_VERSION, header.length, hdr_ptr, payload_size, fd);
+ if (ret) {
+ goto bail;
+ }
+ } else {
+ if (copyout(&header, user_header, sizeof(kd_header_v3))) {
+ ret = EFAULT;
+ goto bail;
+ }
+ // Update the user pointer
+ user_header += sizeof(kd_header_v3);
+ }
+
+ // Write a cpu map. This is a sub chunk of the header
+ cpumap = (uint8_t*)((uintptr_t) cpumap + sizeof(kd_cpumap_header));
+ size_t payload_size = (size_t)(cpumap_size - sizeof(kd_cpumap_header));
+ if (fd) {
+ ret = kdbg_write_v3_chunk_to_fd(V3_CPU_MAP, V3_CPUMAP_VERSION, payload_size, (void *)cpumap, payload_size, fd);
+ if (ret) {
+ goto bail;
+ }
+ } else {
+ ret = kdbg_write_v3_chunk_header(user_header, V3_CPU_MAP, V3_CPUMAP_VERSION, payload_size, NULL, NULL);
+ if (ret) {
+ goto bail;
+ }
+ user_header += sizeof(kd_chunk_header_v3);
+ if (copyout(cpumap, user_header, payload_size)) {
+ ret = EFAULT;
+ goto bail;
+ }
+ // Update the user pointer
+ user_header += payload_size;
+ }
+
+ // Write a thread map
+ if (fd) {
+ ret = kdbg_write_v3_chunk_to_fd(V3_THREAD_MAP, V3_THRMAP_VERSION, thrmap_size, (void *)kd_mapptr, thrmap_size, fd);
+ if (ret) {
+ goto bail;
+ }
+ } else {
+ ret = kdbg_write_v3_chunk_header(user_header, V3_THREAD_MAP, V3_THRMAP_VERSION, thrmap_size, NULL, NULL);
+ if (ret) {
+ goto bail;
+ }
+ user_header += sizeof(kd_chunk_header_v3);
+ if (copyout(kd_mapptr, user_header, thrmap_size)) {
+ ret = EFAULT;
+ goto bail;
+ }
+ user_header += thrmap_size;
+ }
+
+ if (fd) {
+ RAW_file_written += bytes_needed;
+ }
+
+ *user_header_size = bytes_needed;
+bail:
+ if (cpumap) {
+ kmem_free(kernel_map, (vm_offset_t)cpumap, cpumap_size);
+ }
+ return ret;
+}
+
+int
+kdbg_readcpumap(user_addr_t user_cpumap, size_t *user_cpumap_size)
+{
+ uint8_t* cpumap = NULL;
+ uint32_t cpumap_size = 0;
+ int ret = KERN_SUCCESS;
+
+ if (kd_ctrl_page.kdebug_flags & KDBG_BUFINIT) {
+ if (kdbg_cpumap_init_internal(kd_ctrl_page.kdebug_iops, kd_ctrl_page.kdebug_cpus, &cpumap, &cpumap_size) == KERN_SUCCESS) {
+ if (user_cpumap) {
+ size_t bytes_to_copy = (*user_cpumap_size >= cpumap_size) ? cpumap_size : *user_cpumap_size;
+ if (copyout(cpumap, user_cpumap, (size_t)bytes_to_copy)) {
+ ret = EFAULT;
+ }
+ }
+ *user_cpumap_size = cpumap_size;
+ kmem_free(kernel_map, (vm_offset_t)cpumap, cpumap_size);
+ } else {
+ ret = EINVAL;
+ }
+ } else {
+ ret = EINVAL;
+ }
+
+ return ret;
+}
+
+int
+kdbg_readcurthrmap(user_addr_t buffer, size_t *bufsize)
+{
+ kd_threadmap *mapptr;
+ vm_size_t mapsize;
+ vm_size_t mapcount;
+ int ret = 0;
+ size_t count = *bufsize / sizeof(kd_threadmap);
+
+ *bufsize = 0;
+
+ if ((mapptr = kdbg_thrmap_init_internal(count, &mapsize, &mapcount))) {
+ if (copyout(mapptr, buffer, mapcount * sizeof(kd_threadmap))) {
+ ret = EFAULT;
+ } else {
+ *bufsize = (mapcount * sizeof(kd_threadmap));
+ }
+
+ kfree(mapptr, mapsize);
+ } else {
+ ret = EINVAL;
+ }
+
+ return ret;
+}
+
+static int
+kdbg_write_v1_header(bool write_thread_map, vnode_t vp, vfs_context_t ctx)
+{
+ int ret = 0;
+ RAW_header header;
+ clock_sec_t secs;
+ clock_usec_t usecs;
+ char *pad_buf;
+ uint32_t pad_size;
+ uint32_t extra_thread_count = 0;
+ uint32_t cpumap_size;
+ size_t map_size = 0;
+ uint32_t map_count = 0;
+
+ if (write_thread_map) {
+ assert(kd_ctrl_page.kdebug_flags & KDBG_MAPINIT);
+ if (kd_mapcount > UINT32_MAX) {
+ return ERANGE;
+ }
+ map_count = (uint32_t)kd_mapcount;
+ if (os_mul_overflow(map_count, sizeof(kd_threadmap), &map_size)) {
+ return ERANGE;
+ }
+ if (map_size >= INT_MAX) {
+ return ERANGE;
+ }
+ }
+
+ /*
+ * Without the buffers initialized, we cannot construct a CPU map or a
+ * thread map, and cannot write a header.
+ */
+ if (!(kd_ctrl_page.kdebug_flags & KDBG_BUFINIT)) {
+ return EINVAL;
+ }
+
+ /*
+ * To write a RAW_VERSION1+ file, we must embed a cpumap in the
+ * "padding" used to page align the events following the threadmap. If
+ * the threadmap happens to not require enough padding, we artificially
+ * increase its footprint until it needs enough padding.
+ */
+
+ assert(vp);
+ assert(ctx);
+
+ pad_size = PAGE_16KB - ((sizeof(RAW_header) + map_size) & PAGE_MASK);
+ cpumap_size = sizeof(kd_cpumap_header) + kd_ctrl_page.kdebug_cpus * sizeof(kd_cpumap);
+
+ if (cpumap_size > pad_size) {
+ /* If the cpu map doesn't fit in the current available pad_size,
+ * we increase the pad_size by 16K. We do this so that the event
+ * data is always available on a page aligned boundary for both
+ * 4k and 16k systems. We enforce this alignment for the event
+ * data so that we can take advantage of optimized file/disk writes.
+ */
+ pad_size += PAGE_16KB;
+ }
+
+ /* The way we are silently embedding a cpumap in the "padding" is by artificially
+ * increasing the number of thread entries. However, we'll also need to ensure that
+ * the cpumap is embedded in the last 4K page before when the event data is expected.
+ * This way the tools can read the data starting the next page boundary on both
+ * 4K and 16K systems preserving compatibility with older versions of the tools
+ */
+ if (pad_size > PAGE_4KB) {
+ pad_size -= PAGE_4KB;
+ extra_thread_count = (pad_size / sizeof(kd_threadmap)) + 1;
+ }
+
+ memset(&header, 0, sizeof(header));
+ header.version_no = RAW_VERSION1;
+ header.thread_count = map_count + extra_thread_count;
+
+ clock_get_calendar_microtime(&secs, &usecs);
+ header.TOD_secs = secs;
+ header.TOD_usecs = usecs;
+
+ ret = vn_rdwr(UIO_WRITE, vp, (caddr_t)&header, (int)sizeof(RAW_header), RAW_file_offset,
+ UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT, vfs_context_ucred(ctx), (int *) 0, vfs_context_proc(ctx));
+ if (ret) {
+ goto write_error;
+ }
+ RAW_file_offset += sizeof(RAW_header);
+ RAW_file_written += sizeof(RAW_header);
+
+ if (write_thread_map) {
+ assert(map_size < INT_MAX);
+ ret = vn_rdwr(UIO_WRITE, vp, (caddr_t)kd_mapptr, (int)map_size, RAW_file_offset,
+ UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT, vfs_context_ucred(ctx), (int *) 0, vfs_context_proc(ctx));
+ if (ret) {
+ goto write_error;
+ }
+
+ RAW_file_offset += map_size;
+ RAW_file_written += map_size;
+ }
+
+ if (extra_thread_count) {
+ pad_size = extra_thread_count * sizeof(kd_threadmap);
+ pad_buf = kheap_alloc(KHEAP_TEMP, pad_size, Z_WAITOK | Z_ZERO);
+ if (!pad_buf) {
+ ret = ENOMEM;
+ goto write_error;
+ }
+
+ assert(pad_size < INT_MAX);
+ ret = vn_rdwr(UIO_WRITE, vp, (caddr_t)pad_buf, (int)pad_size, RAW_file_offset,
+ UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT, vfs_context_ucred(ctx), (int *) 0, vfs_context_proc(ctx));
+ kheap_free(KHEAP_TEMP, pad_buf, pad_size);
+ if (ret) {
+ goto write_error;
+ }