]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kdp/kdp_core.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / osfmk / kdp / kdp_core.c
index 5cf505770059970168578d81175b34b79127265e..08edfb7c64bb37f354952b7e4042896c2024d119 100644 (file)
@@ -178,6 +178,10 @@ boolean_t kdp_has_polled_corefile(void)
     return (NULL != gIOPolledCoreFileVars);
 }
 
+kern_return_t kdp_polled_corefile_error(void)
+{
+    return gIOPolledCoreFileOpenRet;
+}
 #if CONFIG_EMBEDDED
 /*
  * Whenever we start a coredump, make sure the buffers
@@ -461,7 +465,7 @@ kern_dump_disk_proc(unsigned int request, __unused char *corename,
         case KDP_DATA:
            err = IOPolledFileWrite(gIOPolledCoreFileVars, data, length, NULL);
            if (kIOReturnSuccess != err) {
-                   kern_coredump_log(NULL, "IOPolledFileWrite(gIOPolledCoreFileVars, 0x%p, 0x%llx, NULL) returned 0x%x\n",
+                   kern_coredump_log(NULL, "IOPolledFileWrite(gIOPolledCoreFileVars, %p, 0x%llx, NULL) returned 0x%x\n",
                                    data, length, err);
                    break;
            }
@@ -510,7 +514,7 @@ kdp_core_zoutput(z_streamp strm, Bytef *buf, unsigned len)
     {
        if ((ret = (*vars->outproc)(KDP_DATA, NULL, len, buf)) != kIOReturnSuccess)
        { 
-           kern_coredump_log(NULL, "(kdp_core_zoutput) outproc(KDP_DATA, NULL, 0x%x, 0x%p) returned 0x%x\n",
+           kern_coredump_log(NULL, "(kdp_core_zoutput) outproc(KDP_DATA, NULL, 0x%x, %p) returned 0x%x\n",
                            len, buf, ret);
            vars->error = ret;
        }
@@ -553,7 +557,7 @@ kdp_core_zoutputbuf(z_streamp strm, Bytef *inbuf, unsigned inlen)
                                        vars->outlen - vars->outremain, 
                                        vars->outbuf)) != kIOReturnSuccess)
        { 
-           kern_coredump_log(NULL, "(kdp_core_zoutputbuf) outproc(KDP_DATA, NULL, 0x%x, 0x%p) returned 0x%x\n",
+           kern_coredump_log(NULL, "(kdp_core_zoutputbuf) outproc(KDP_DATA, NULL, 0x%x, %p) returned 0x%x\n",
                            (vars->outlen - vars->outremain), vars->outbuf, ret);
            vars->error = ret;
        }
@@ -708,8 +712,8 @@ kernel_pmap_present_mapping(uint64_t vaddr, uint64_t * pvincr, uintptr_t * pvphy
         vincr = kdp_core_ramdisk_size;
     }
     else
-#if defined(__arm64__)
-    if (vaddr == _COMM_PAGE64_BASE_ADDRESS)
+#if defined(__arm64__) && defined(CONFIG_XNUPOST)
+    if (vaddr == _COMM_HIGH_PAGE64_BASE_ADDRESS)
     {
        /* not readable */
        ppn = 0;
@@ -795,22 +799,25 @@ pmap_traverse_present_mappings(pmap_t __unused pmap,
                    ppn = VM_PAGE_GET_PHYS_PAGE(m);
                    break;
                }
-               m = (vm_page_t)vm_page_queue_next(&m->listq);
-           }
-           vcur = phystokv(ptoa(ppn));
-           if (vcur != vprev)
-           {
-               ret = callback(vcurstart, vprev, context);
-               lastvavalid = FALSE;
+               m = (vm_page_t)vm_page_queue_next(&m->vmp_listq);
            }
            vincr = PAGE_SIZE_64;
            if (ppn == atop(avail_end))
            {
                vm_object_unlock(&pmap_object_store);
                m = VM_PAGE_NULL;
+               // avail_end is not a valid physical address,
+               // so phystokv(avail_end) may not produce the expected result.
+               vcur = phystokv(avail_start) + (avail_end - avail_start);
+           } else {
+               m = (vm_page_t)vm_page_queue_next(&m->vmp_listq);
+               vcur = phystokv(ptoa(ppn));
+           }
+           if (vcur != vprev)
+           {
+               ret = callback(vcurstart, vprev, context);
+               lastvavalid = FALSE;
            }
-           else
-               m = (vm_page_t)vm_page_queue_next(&m->listq);
        }
        if (m == VM_PAGE_NULL)
            ppn = kernel_pmap_present_mapping(vcur, &vincr, NULL);
@@ -843,7 +850,7 @@ pmap_traverse_present_mappings(pmap_t __unused pmap,
 
 #if defined(__x86_64__)
            /* Try to skip by 2MB if possible */
-           if (((vcur & PDMASK) == 0) && cpu_64bit) {
+           if ((vcur & PDMASK) == 0) {
                pd_entry_t *pde;
                pde = pmap_pde(pmap, vcur);
                if (0 == pde || ((*pde & INTEL_PTE_VALID) == 0)) {
@@ -862,6 +869,13 @@ pmap_traverse_present_mappings(pmap_t __unused pmap,
        /* send previous run */
        ret = callback(vcurstart, vcur, context);
     }
+
+#if KASAN
+    if (ret == KERN_SUCCESS) {
+       ret = kasan_traverse_mappings(callback, context);
+    }
+#endif
+
     return (ret);
 }
 
@@ -1059,13 +1073,13 @@ kern_dump_update_header(struct kdp_core_out_vars *outvars)
        /* Write the file header -- first seek to the beginning of the file */
        foffset = 0;
        if ((ret = (outvars->outproc)(KDP_SEEK, NULL, sizeof(foffset), &foffset)) != kIOReturnSuccess) {
-               kern_coredump_log(NULL, "(kern_dump_update_header) outproc(KDP_SEEK, NULL, %lu, 0x%p) foffset = 0x%llx returned 0x%x\n",
+               kern_coredump_log(NULL, "(kern_dump_update_header) outproc(KDP_SEEK, NULL, %lu, %p) foffset = 0x%llx returned 0x%x\n",
                                sizeof(foffset), &foffset, foffset, ret);
                return ret;
        }
 
        if ((ret = (outvars->outproc)(KDP_DATA, NULL, sizeof(kdp_core_header), &kdp_core_header)) != kIOReturnSuccess) {
-               kern_coredump_log(NULL, "(kern_dump_update_header) outproc(KDP_DATA, NULL, %lu, 0x%p) returned 0x%x\n",
+               kern_coredump_log(NULL, "(kern_dump_update_header) outproc(KDP_DATA, NULL, %lu, %p) returned 0x%x\n",
                                sizeof(kdp_core_header), &kdp_core_header, ret);
                 return ret;
        }
@@ -1118,7 +1132,7 @@ kern_dump_seek_to_next_file(void *kdp_core_out_vars, uint64_t next_file_offset)
        int ret;
 
        if ((ret = (outvars->outproc)(KDP_SEEK, NULL, sizeof(next_file_offset), &next_file_offset)) != kIOReturnSuccess) {
-               kern_coredump_log(NULL, "(kern_dump_seek_to_next_file) outproc(KDP_SEEK, NULL, %lu, 0x%p) foffset = 0x%llx returned 0x%x\n",
+               kern_coredump_log(NULL, "(kern_dump_seek_to_next_file) outproc(KDP_SEEK, NULL, %lu, %p) foffset = 0x%llx returned 0x%x\n",
                                sizeof(next_file_offset), &next_file_offset, next_file_offset, ret);
        }
 
@@ -1148,8 +1162,10 @@ do_kern_dump(kern_dump_output_proc outproc, enum kern_dump_type kd_variant)
        existing_log_size = (panic_info->eph_panic_log_offset - sizeof(struct embedded_panic_header)) +
                                panic_info->eph_panic_log_len + panic_info->eph_other_log_len;
 #else /* CONFIG_EMBEDDED */
-       existing_log_size = (panic_info->mph_panic_log_offset - sizeof(struct macos_panic_header)) +
+       if (panic_info->mph_panic_log_offset != 0) {
+               existing_log_size = (panic_info->mph_panic_log_offset - sizeof(struct macos_panic_header)) +
                                panic_info->mph_panic_log_len + panic_info->mph_other_log_len;
+       }
 #endif /* CONFIG_EMBEDDED */
 
        assert (existing_log_size <= debug_buf_size);
@@ -1177,7 +1193,7 @@ do_kern_dump(kern_dump_output_proc outproc, enum kern_dump_type kd_variant)
 
                /* Seek the calculated offset (we'll scrollback later to flush the logs and header) */
                if ((ret = (*outproc)(KDP_SEEK, NULL, sizeof(foffset), &foffset)) != kIOReturnSuccess) {
-                       kern_coredump_log(NULL, "(do_kern_dump seek begin) outproc(KDP_SEEK, NULL, %lu, 0x%p) foffset = 0x%llx returned 0x%x\n",
+                       kern_coredump_log(NULL, "(do_kern_dump seek begin) outproc(KDP_SEEK, NULL, %lu, %p) foffset = 0x%llx returned 0x%x\n",
                                        sizeof(foffset), &foffset, foffset, ret);
                        dump_succeeded = FALSE;
                        goto exit;
@@ -1228,11 +1244,11 @@ do_kern_dump(kern_dump_output_proc outproc, enum kern_dump_type kd_variant)
                                kern_coredump_log(NULL, "Failed to reset outvars for stackshot with len 0x%zx, returned 0x%x\n", panic_stackshot_len, ret);
                                dump_succeeded = FALSE;
                        } else if ((ret = kdp_core_output(&outvars, panic_stackshot_len, (void *)panic_stackshot_buf)) != KERN_SUCCESS) {
-                               kern_coredump_log(NULL, "Failed to write panic stackshot to file, kdp_coreoutput(outvars, %lu, 0x%p) returned 0x%x\n",
+                               kern_coredump_log(NULL, "Failed to write panic stackshot to file, kdp_coreoutput(outvars, %lu, %p) returned 0x%x\n",
                                               panic_stackshot_len, (void *) panic_stackshot_buf, ret);
                                dump_succeeded = FALSE;
                        } else if ((ret = kdp_core_output(&outvars, 0, NULL)) != KERN_SUCCESS) {
-                               kern_coredump_log(NULL, "Failed to flush stackshot data : kdp_core_output(0x%p, 0, NULL) returned 0x%x\n", &outvars, ret);
+                               kern_coredump_log(NULL, "Failed to flush stackshot data : kdp_core_output(%p, 0, NULL) returned 0x%x\n", &outvars, ret);
                                dump_succeeded = FALSE;
                        } else if ((ret = kern_dump_record_file(&outvars, "panic_stackshot.kcdata", foffset, &compressed_stackshot_len)) != KERN_SUCCESS) {
                                kern_coredump_log(NULL, "Failed to record panic stackshot in corefile header, kern_dump_record_file returned 0x%x\n", ret);
@@ -1246,7 +1262,7 @@ do_kern_dump(kern_dump_output_proc outproc, enum kern_dump_type kd_variant)
                /* Write the debug log -- first seek to the end of the corefile header */
                foffset = KERN_COREDUMP_HEADERSIZE;
                if ((ret = (*outproc)(KDP_SEEK, NULL, sizeof(foffset), &foffset)) != kIOReturnSuccess) {
-                       kern_coredump_log(NULL, "(do_kern_dump seek logfile) outproc(KDP_SEEK, NULL, %lu, 0x%p) foffset = 0x%llx returned 0x%x\n",
+                       kern_coredump_log(NULL, "(do_kern_dump seek logfile) outproc(KDP_SEEK, NULL, %lu, %p) foffset = 0x%llx returned 0x%x\n",
                                        sizeof(foffset), &foffset, foffset, ret);
                        dump_succeeded = FALSE;
                        goto exit;
@@ -1261,7 +1277,9 @@ do_kern_dump(kern_dump_output_proc outproc, enum kern_dump_type kd_variant)
 #if CONFIG_EMBEDDED
                existing_log_size -= panic_info->eph_other_log_len;
 #else
-               existing_log_size -= panic_info->mph_other_log_len;
+               if (existing_log_size) {
+                       existing_log_size -= panic_info->mph_other_log_len;
+               }
 #endif
 
                /*
@@ -1270,7 +1288,7 @@ do_kern_dump(kern_dump_output_proc outproc, enum kern_dump_type kd_variant)
                 */
                buf = debug_buf_base;
                if ((ret = (*outproc)(KDP_DATA, NULL, existing_log_size, buf)) != kIOReturnSuccess) {
-                               kern_coredump_log(NULL, "(do_kern_dump paniclog) outproc(KDP_DATA, NULL, %lu, 0x%p) returned 0x%x\n",
+                               kern_coredump_log(NULL, "(do_kern_dump paniclog) outproc(KDP_DATA, NULL, %lu, %p) returned 0x%x\n",
                                                existing_log_size, buf, ret);
                                dump_succeeded = FALSE;
                                goto exit;
@@ -1291,7 +1309,7 @@ do_kern_dump(kern_dump_output_proc outproc, enum kern_dump_type kd_variant)
 
                /* Write the coredump log */
                if ((ret = (*outproc)(KDP_DATA, NULL, new_log_len, buf)) != kIOReturnSuccess) {
-                       kern_coredump_log(NULL, "(do_kern_dump coredump log) outproc(KDP_DATA, NULL, %lu, 0x%p) returned 0x%x\n",
+                       kern_coredump_log(NULL, "(do_kern_dump coredump log) outproc(KDP_DATA, NULL, %lu, %p) returned 0x%x\n",
                                        new_log_len, buf, ret);
                        dump_succeeded = FALSE;
                        goto exit;
@@ -1308,15 +1326,18 @@ exit:
                dump_succeeded = FALSE;
        }
 
+       /* If applicable, update the panic header and flush it so we update the CRC */
 #if CONFIG_EMBEDDED
        panic_info->eph_panic_flags |= (dump_succeeded ? EMBEDDED_PANIC_HEADER_FLAG_COREDUMP_COMPLETE :
                        EMBEDDED_PANIC_HEADER_FLAG_COREDUMP_FAILED);
+       paniclog_flush();
 #else
-       panic_info->mph_panic_flags |= (dump_succeeded ? MACOS_PANIC_HEADER_FLAG_COREDUMP_COMPLETE :
+       if (panic_info->mph_panic_log_offset != 0) {
+               panic_info->mph_panic_flags |= (dump_succeeded ? MACOS_PANIC_HEADER_FLAG_COREDUMP_COMPLETE :
                        MACOS_PANIC_HEADER_FLAG_COREDUMP_FAILED);
+               paniclog_flush();
+       }
 #endif
-       /* We touched the panic header, flush it so we update the CRC */
-       paniclog_flush();
 
        return (dump_succeeded ? 0 : -1);
 }
@@ -1368,17 +1389,18 @@ kern_dump(enum kern_dump_type kd_variant)
 }
 
 #if CONFIG_EMBEDDED
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wmissing-noreturn"
 void
 panic_spin_shmcon()
 {
-#pragma clang diagnostic pop
+       if (hwsd_info == NULL) {
+               kern_coredump_log(NULL, "handshake structure not initialized\n");
+               return;
+       }
+
        kern_coredump_log(NULL, "\nPlease go to https://panic.apple.com to report this panic\n");
        kern_coredump_log(NULL, "Waiting for hardware shared memory debugger, handshake structure is at virt: %p, phys %p\n",
                        hwsd_info, (void *)kvtophys((vm_offset_t)hwsd_info));
 
-       assert(hwsd_info != NULL);
        hwsd_info->xhsdci_status = XHSDCI_STATUS_KERNEL_READY;
        hwsd_info->xhsdci_seq_no = 0;
        FlushPoC_DcacheRegion((vm_offset_t) hwsd_info, sizeof(*hwsd_info));