]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kdp/ml/i386/kdp_x86_common.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / kdp / ml / i386 / kdp_x86_common.c
index 3ce3b191dad6b74ca6f6e0e0a312f1fb0465994b..96eb2dd932ebc6f62dcdefd13e47c8a85794dec6 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 2008 Apple Inc. All rights reserved.
+ * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
- * 
+ *
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * unlawful or unlicensed copies of an Apple operating system, or to
  * circumvent, violate, or enable the circumvention or violation of, any
  * terms of an Apple operating system software license agreement.
- * 
+ *
  * Please obtain a copy of the License at
  * http://www.opensource.apple.com/apsl/ and read it before using this file.
- * 
+ *
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
@@ -22,7 +22,7 @@
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
- * 
+ *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 
  * A (potentially valid) physical address is not a kernel address
  * i.e. it'a a user address.
  */
-#define IS_PHYS_ADDR(addr)             IS_USERADDR64_CANONICAL(addr)
+#define IS_PHYS_ADDR(addr)              IS_USERADDR64_CANONICAL(addr)
 
 boolean_t kdp_read_io;
 boolean_t kdp_trans_off;
 
-addr64_t kdp_vtophys(pmap_t pmap, addr64_t va);
+pmap_paddr_t kdp_vtophys(pmap_t pmap, vm_offset_t va);
 
 pmap_t kdp_pmap = 0;
 
-addr64_t
+kdp_jtag_coredump_t kdp_jtag_coredump;
+
+pmap_paddr_t
 kdp_vtophys(
        pmap_t pmap,
-       addr64_t va)
+       vm_offset_t va)
 {
-       addr64_t    pa;
-       ppnum_t pp;
+       pmap_paddr_t    pa;
 
-       pp = pmap_find_phys(pmap, va);
-       if(!pp) return 0;
-        
-       pa = ((addr64_t)pp << PAGE_SHIFT) | (va & PAGE_MASK);
+       pa = pmap_find_pa(pmap, va);
 
-       return(pa);
+       return pa;
 }
 
 mach_vm_size_t
@@ -114,62 +112,70 @@ kdp_machine_vm_read( mach_vm_address_t src, caddr_t dst, mach_vm_size_t len)
  * source (cur_virt_src); otherwise, the source is translated using the
  * kernel_pmap.
  */
-       if (kdp_pmap)
+       if (kdp_pmap) {
                src_pmap = kdp_pmap;
+       }
 
        while (resid != 0) {
                if (!(cur_phys_src = kdp_vtophys(src_pmap,
-                           cur_virt_src)))
+                   cur_virt_src))) {
                        goto exit;
+               }
 
 /* Always translate the destination buffer using the kernel_pmap */
-               if(!(cur_phys_dst = kdp_vtophys(kernel_pmap, cur_virt_dst)))
+               if (!(cur_phys_dst = kdp_vtophys(kernel_pmap, cur_virt_dst))) {
                        goto exit;
+               }
 
                /* Validate physical page numbers unless kdp_read_io is set */
-               if (kdp_read_io == FALSE)
-                       if (!pmap_valid_page(i386_btop(cur_phys_dst)) || !pmap_valid_page(i386_btop(cur_phys_src)))
+               if (kdp_read_io == FALSE) {
+                       if (!pmap_valid_page(i386_btop(cur_phys_dst)) || !pmap_valid_page(i386_btop(cur_phys_src))) {
                                goto exit;
+                       }
+               }
 
 /* Get length left on page */
                cnt_src = PAGE_SIZE - (cur_phys_src & PAGE_MASK);
                cnt_dst = PAGE_SIZE - (cur_phys_dst & PAGE_MASK);
-               if (cnt_src > cnt_dst)
+               if (cnt_src > cnt_dst) {
                        cnt = cnt_dst;
-               else
+               } else {
                        cnt = cnt_src;
-               if (cnt > resid) 
+               }
+               if (cnt > resid) {
                        cnt = resid;
+               }
 
 /* Do a physical copy */
                if (EFAULT == ml_copy_phys(cur_phys_src,
-                                          cur_phys_dst,
-                                          (vm_size_t)cnt))
+                   cur_phys_dst,
+                   (vm_size_t)cnt)) {
                        goto exit;
+               }
                cur_virt_src += cnt;
                cur_virt_dst += cnt;
                resid -= cnt;
        }
 exit:
-       return (len - resid);
+       return len - resid;
 }
 
 mach_vm_size_t
 kdp_machine_phys_read(kdp_readphysmem64_req_t *rq, caddr_t dst,
-                     uint16_t lcpu)
+    uint16_t lcpu)
 {
        mach_vm_address_t src = rq->address;
        mach_vm_size_t    len = rq->nbytes;
-       
+
        addr64_t cur_virt_dst;
        addr64_t cur_phys_dst, cur_phys_src;
        mach_vm_size_t resid = len;
        mach_vm_size_t cnt = 0, cnt_src, cnt_dst;
 
-        if ((lcpu != KDP_CURRENT_LCPU) && (lcpu != cpu_number())) {
-               return (mach_vm_size_t) 
-                       kdp_x86_xcpu_invoke(lcpu, (kdp_x86_xcpu_func_t)kdp_machine_phys_read, rq, dst);
-        }
+       if ((lcpu != KDP_CURRENT_LCPU) && (lcpu != cpu_number())) {
+               return (mach_vm_size_t)
+                      kdp_x86_xcpu_invoke(lcpu, (kdp_x86_xcpu_func_t)kdp_machine_phys_read, rq, dst);
+       }
 
 #ifdef KDP_VM_READ_DEBUG
        printf("kdp_phys_read: src %llx dst %p len %llx\n", src, (void *)dst, len);
@@ -179,41 +185,44 @@ kdp_machine_phys_read(kdp_readphysmem64_req_t *rq, caddr_t dst,
        cur_phys_src = (addr64_t)src;
 
        while (resid != 0) {
-
-               if(!(cur_phys_dst = kdp_vtophys(kernel_pmap, cur_virt_dst)))
+               if (!(cur_phys_dst = kdp_vtophys(kernel_pmap, cur_virt_dst))) {
                        goto exit;
+               }
 
 /* Get length left on page */
                cnt_src = PAGE_SIZE - (cur_phys_src & PAGE_MASK);
                cnt_dst = PAGE_SIZE - (cur_phys_dst & PAGE_MASK);
-               if (cnt_src > cnt_dst)
+               if (cnt_src > cnt_dst) {
                        cnt = cnt_dst;
-               else
+               } else {
                        cnt = cnt_src;
-               if (cnt > resid) 
+               }
+               if (cnt > resid) {
                        cnt = resid;
+               }
 
-       /* Do a physical copy; use ml_copy_phys() in the event this is
-        * a short read with potential side effects.
-        */
+               /* Do a physical copy; use ml_copy_phys() in the event this is
+                * a short read with potential side effects.
+                */
                if (EFAULT == ml_copy_phys(cur_phys_src,
-                                          cur_phys_dst,
-                                          (vm_size_t)cnt))
+                   cur_phys_dst,
+                   (vm_size_t)cnt)) {
                        goto exit;
+               }
                cur_phys_src += cnt;
                cur_virt_dst += cnt;
                resid -= cnt;
        }
 exit:
-       return (len - resid);
+       return len - resid;
 }
 
 /*
- * 
+ *
  */
 mach_vm_size_t
 kdp_machine_vm_write( caddr_t src, mach_vm_address_t dst, mach_vm_size_t len)
-{       
+{
        addr64_t cur_virt_src, cur_virt_dst;
        addr64_t cur_phys_src, cur_phys_dst;
        unsigned resid, cnt, cnt_src, cnt_dst;
@@ -228,51 +237,55 @@ kdp_machine_vm_write( caddr_t src, mach_vm_address_t dst, mach_vm_size_t len)
        resid = (unsigned)len;
 
        while (resid != 0) {
-               if ((cur_phys_dst = kdp_vtophys(kernel_pmap, cur_virt_dst)) == 0) 
+               if ((cur_phys_dst = kdp_vtophys(kernel_pmap, cur_virt_dst)) == 0) {
                        goto exit;
+               }
 
-               if ((cur_phys_src = kdp_vtophys(kernel_pmap, cur_virt_src)) == 0) 
+               if ((cur_phys_src = kdp_vtophys(kernel_pmap, cur_virt_src)) == 0) {
                        goto exit;
+               }
 
                /* Copy as many bytes as possible without crossing a page */
                cnt_src = (unsigned)(PAGE_SIZE - (cur_phys_src & PAGE_MASK));
                cnt_dst = (unsigned)(PAGE_SIZE - (cur_phys_dst & PAGE_MASK));
 
-               if (cnt_src > cnt_dst)
+               if (cnt_src > cnt_dst) {
                        cnt = cnt_dst;
-               else
+               } else {
                        cnt = cnt_src;
-               if (cnt > resid) 
+               }
+               if (cnt > resid) {
                        cnt = resid;
+               }
 
-               if (EFAULT == ml_copy_phys(cur_phys_src, cur_phys_dst, cnt))
-                       goto exit;              /* Copy stuff over */
-
-               cur_virt_src +=cnt;
-               cur_virt_dst +=cnt;
+               if (EFAULT == ml_copy_phys(cur_phys_src, cur_phys_dst, cnt)) {
+                       goto exit;              /* Copy stuff over */
+               }
+               cur_virt_src += cnt;
+               cur_virt_dst += cnt;
                resid -= cnt;
        }
 exit:
-       return (len - resid);
+       return len - resid;
 }
 
 /*
- * 
+ *
  */
 mach_vm_size_t
 kdp_machine_phys_write(kdp_writephysmem64_req_t *rq, caddr_t src,
-                      uint16_t lcpu)
-{       
+    uint16_t lcpu)
+{
        mach_vm_address_t dst = rq->address;
        mach_vm_size_t    len = rq->nbytes;
        addr64_t cur_virt_src;
        addr64_t cur_phys_src, cur_phys_dst;
        unsigned resid, cnt, cnt_src, cnt_dst;
 
-        if ((lcpu != KDP_CURRENT_LCPU) && (lcpu != cpu_number())) {
-               return (mach_vm_size_t) 
-                       kdp_x86_xcpu_invoke(lcpu, (kdp_x86_xcpu_func_t)kdp_machine_phys_write, rq, src);
-        }
+       if ((lcpu != KDP_CURRENT_LCPU) && (lcpu != cpu_number())) {
+               return (mach_vm_size_t)
+                      kdp_x86_xcpu_invoke(lcpu, (kdp_x86_xcpu_func_t)kdp_machine_phys_write, rq, src);
+       }
 
 #ifdef KDP_VM_WRITE_DEBUG
        printf("kdp_phys_write: src %p dst %llx len %llx - %08X %08X\n", (void *)src, dst, len, ((unsigned int *)src)[0], ((unsigned int *)src)[1]);
@@ -284,30 +297,33 @@ kdp_machine_phys_write(kdp_writephysmem64_req_t *rq, caddr_t src,
        resid = (unsigned)len;
 
        while (resid != 0) {
-               if ((cur_phys_src = kdp_vtophys(kernel_pmap, cur_virt_src)) == 0) 
+               if ((cur_phys_src = kdp_vtophys(kernel_pmap, cur_virt_src)) == 0) {
                        goto exit;
+               }
 
                /* Copy as many bytes as possible without crossing a page */
                cnt_src = (unsigned)(PAGE_SIZE - (cur_phys_src & PAGE_MASK));
                cnt_dst = (unsigned)(PAGE_SIZE - (cur_phys_dst & PAGE_MASK));
 
-               if (cnt_src > cnt_dst)
+               if (cnt_src > cnt_dst) {
                        cnt = cnt_dst;
-               else
+               } else {
                        cnt = cnt_src;
-               if (cnt > resid) 
+               }
+               if (cnt > resid) {
                        cnt = resid;
+               }
 
-               if (EFAULT == ml_copy_phys(cur_phys_src, cur_phys_dst, cnt))
-                       goto exit;              /* Copy stuff over */
-
-               cur_virt_src +=cnt;
-               cur_phys_dst +=cnt;
+               if (EFAULT == ml_copy_phys(cur_phys_src, cur_phys_dst, cnt)) {
+                       goto exit;              /* Copy stuff over */
+               }
+               cur_virt_src += cnt;
+               cur_phys_dst += cnt;
                resid -= cnt;
        }
 
 exit:
-       return (len - resid);
+       return len - resid;
 }
 
 int
@@ -318,10 +334,9 @@ kdp_machine_ioport_read(kdp_readioport_req_t *rq, caddr_t data, uint16_t lcpu)
 
        if ((lcpu != KDP_CURRENT_LCPU) && (lcpu != cpu_number())) {
                return (int) kdp_x86_xcpu_invoke(lcpu, (kdp_x86_xcpu_func_t)kdp_machine_ioport_read, rq, data);
-        }
+       }
 
-        switch (size)
-       {
+       switch (size) {
        case 1:
                *((uint8_t *) data)  = inb(addr);
                break;
@@ -333,7 +348,6 @@ kdp_machine_ioport_read(kdp_readioport_req_t *rq, caddr_t data, uint16_t lcpu)
                break;
        default:
                return KDPERR_BADFLAVOR;
-               break;
        }
 
        return KDPERR_NO_ERROR;
@@ -349,8 +363,7 @@ kdp_machine_ioport_write(kdp_writeioport_req_t *rq, caddr_t data, uint16_t lcpu)
                return (int) kdp_x86_xcpu_invoke(lcpu, (kdp_x86_xcpu_func_t)kdp_machine_ioport_write, rq, data);
        }
 
-       switch (size)
-       {
+       switch (size) {
        case 1:
                outb(addr, *((uint8_t *) data));
                break;
@@ -362,7 +375,6 @@ kdp_machine_ioport_write(kdp_writeioport_req_t *rq, caddr_t data, uint16_t lcpu)
                break;
        default:
                return KDPERR_BADFLAVOR;
-               break;
        }
 
        return KDPERR_NO_ERROR;
@@ -405,15 +417,18 @@ vm_map_offset_t debugger_window_kva;
  */
 
 void
-kdp_machine_init(void) {
-       if (debug_boot_arg == 0)
-               return;
-
+kdp_map_debug_pagetable_window(void)
+{
        vm_map_entry_t e;
-       kern_return_t kr = vm_map_find_space(kernel_map,
+       kern_return_t kr;
+
+       kr = vm_map_find_space(kernel_map,
            &debugger_window_kva,
            PAGE_SIZE, 0,
-           VM_MAKE_TAG(VM_KERN_MEMORY_OSFMK), &e);
+           0,
+           VM_MAP_KERNEL_FLAGS_NONE,
+           VM_KERN_MEMORY_OSFMK,
+           &e);
 
        if (kr != KERN_SUCCESS) {
                panic("%s: vm_map_find_space failed with %d\n", __FUNCTION__, kr);
@@ -429,5 +444,31 @@ kdp_machine_init(void) {
        }
 }
 
+/* initialize kdp_jtag_coredump with data needed for JTAG coredump extraction */
 
+void
+kdp_jtag_coredump_init(void)
+{
+       kdp_jtag_coredump.version                   = (uint64_t) KDP_JTAG_COREDUMP_VERSION_1;
+       kdp_jtag_coredump.kernel_map_start          = (uint64_t) kernel_map->min_offset;
+       kdp_jtag_coredump.kernel_map_end            = (uint64_t) kernel_map->max_offset;
+       kdp_jtag_coredump.kernel_pmap_pml4          = (uint64_t) kernel_pmap->pm_pml4;
+       kdp_jtag_coredump.pmap_memory_regions       = (uint64_t) &pmap_memory_regions;
+       kdp_jtag_coredump.pmap_memory_region_count  = (uint64_t) pmap_memory_region_count;
+       kdp_jtag_coredump.pmap_memory_region_t_size = (uint64_t) sizeof(pmap_memory_region_t);
+       kdp_jtag_coredump.physmap_base              = (uint64_t) &physmap_base;
+
+       /* update signature last so that JTAG can trust that structure has valid data */
+       kdp_jtag_coredump.signature                 = (uint64_t) KDP_JTAG_COREDUMP_SIGNATURE;
+}
 
+void
+kdp_machine_init(void)
+{
+       if (debug_boot_arg == 0) {
+               return;
+       }
+
+       kdp_map_debug_pagetable_window();
+       kdp_jtag_coredump_init();
+}