1 /* $OpenBSD: kvm_amd64.c,v 1.9 2012/12/05 23:20:02 deraadt Exp $ */ 2 /* $NetBSD: kvm_x86_64.c,v 1.3 2002/06/05 22:01:55 fvdl Exp $ */ 3 4 /*- 5 * Copyright (c) 1989, 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software developed by the Computer Systems 9 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract 10 * BG 91-66 and contributed to Berkeley. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 /* 38 * x86-64 machine dependent routines for kvm. 39 */ 40 41 #include <sys/param.h> 42 #include <sys/user.h> 43 #include <sys/proc.h> 44 #include <sys/stat.h> 45 #include <sys/kcore.h> 46 #include <machine/kcore.h> 47 #include <stdlib.h> 48 #include <unistd.h> 49 #include <nlist.h> 50 #include <kvm.h> 51 52 #include <uvm/uvm_extern.h> 53 54 #include <limits.h> 55 #include <db.h> 56 57 #include "kvm_private.h" 58 59 #include <machine/pmap.h> 60 #include <machine/pte.h> 61 #include <machine/vmparam.h> 62 63 void 64 _kvm_freevtop(kvm_t *kd) 65 { 66 67 /* Not actually used for anything right now, but safe. */ 68 if (kd->vmst != NULL) { 69 free(kd->vmst); 70 kd->vmst = NULL; 71 } 72 } 73 74 /*ARGSUSED*/ 75 int 76 _kvm_initvtop(kvm_t *kd) 77 { 78 79 return (0); 80 } 81 82 /* 83 * Translate a kernel virtual address to a physical address. 84 */ 85 int 86 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa) 87 { 88 cpu_kcore_hdr_t *cpu_kh; 89 paddr_t pde_pa, pte_pa; 90 u_long page_off; 91 pd_entry_t pde; 92 pt_entry_t pte; 93 94 if (ISALIVE(kd)) { 95 _kvm_err(kd, 0, "vatop called in live kernel!"); 96 return (0); 97 } 98 99 page_off = va & PAGE_MASK; 100 101 if (va >= PMAP_DIRECT_BASE && va <= PMAP_DIRECT_END) { 102 *pa = va - PMAP_DIRECT_BASE; 103 return (int)(PAGE_SIZE - page_off); 104 } 105 106 cpu_kh = kd->cpu_data; 107 108 /* 109 * Find and read all entries to get to the pa. 110 */ 111 112 /* 113 * Level 4. 114 */ 115 pde_pa = cpu_kh->ptdpaddr + (pl4_pi(va) * sizeof(pd_entry_t)); 116 if (pread(kd->pmfd, (void *)&pde, sizeof(pde), 117 _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) { 118 _kvm_syserr(kd, 0, "could not read PT level 4 entry"); 119 goto lose; 120 } 121 if ((pde & PG_V) == 0) { 122 _kvm_err(kd, 0, "invalid translation (invalid level 4 PDE)"); 123 goto lose; 124 } 125 126 /* 127 * Level 3. 128 */ 129 pde_pa = (pde & PG_FRAME) + (pl3_pi(va) * sizeof(pd_entry_t)); 130 if (pread(kd->pmfd, (void *)&pde, sizeof(pde), 131 _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) { 132 _kvm_syserr(kd, 0, "could not read PT level 3 entry"); 133 goto lose; 134 } 135 if ((pde & PG_V) == 0) { 136 _kvm_err(kd, 0, "invalid translation (invalid level 3 PDE)"); 137 goto lose; 138 } 139 140 /* 141 * Level 2. 142 */ 143 pde_pa = (pde & PG_FRAME) + (pl2_pi(va) * sizeof(pd_entry_t)); 144 if (pread(kd->pmfd, (void *)&pde, sizeof(pde), 145 _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) { 146 _kvm_syserr(kd, 0, "could not read PT level 2 entry"); 147 goto lose; 148 } 149 if ((pde & PG_V) == 0) { 150 _kvm_err(kd, 0, "invalid translation (invalid level 2 PDE)"); 151 goto lose; 152 } 153 154 /* 155 * Might be a large page. 156 */ 157 if ((pde & PG_PS) != 0) { 158 page_off = va & (NBPD_L2 - 1); 159 *pa = (pde & PG_LGFRAME) | page_off; 160 return (int)(NBPD_L2 - page_off); 161 } 162 163 /* 164 * Level 1. 165 */ 166 pte_pa = (pde & PG_FRAME) + (pl1_pi(va) * sizeof(pt_entry_t)); 167 if (pread(kd->pmfd, (void *) &pte, sizeof(pte), 168 _kvm_pa2off(kd, pte_pa)) != sizeof(pte)) { 169 _kvm_syserr(kd, 0, "could not read PTE"); 170 goto lose; 171 } 172 /* 173 * Validate the PTE and return the physical address. 174 */ 175 if ((pte & PG_V) == 0) { 176 _kvm_err(kd, 0, "invalid translation (invalid PTE)"); 177 goto lose; 178 } 179 *pa = (pte & PG_FRAME) + page_off; 180 return (int)(PAGE_SIZE - page_off); 181 182 lose: 183 *pa = (u_long)~0L; 184 return (0); 185 } 186 187 /* 188 * Translate a physical address to a file-offset in the crash dump. 189 */ 190 off_t 191 _kvm_pa2off(kvm_t *kd, paddr_t pa) 192 { 193 cpu_kcore_hdr_t *cpu_kh; 194 phys_ram_seg_t *ramsegs; 195 off_t off; 196 int i; 197 198 cpu_kh = kd->cpu_data; 199 ramsegs = (void *)((char *)(void *)cpu_kh + ALIGN(sizeof *cpu_kh)); 200 201 off = 0; 202 for (i = 0; i < cpu_kh->nmemsegs; i++) { 203 if (pa >= ramsegs[i].start && 204 (pa - ramsegs[i].start) < ramsegs[i].size) { 205 off += (pa - ramsegs[i].start); 206 break; 207 } 208 off += ramsegs[i].size; 209 } 210 211 if (i == cpu_kh->nmemsegs) 212 _kvm_err(kd, 0, "pa %lx not in dump", pa); 213 214 return (kd->dump_off + off); 215 } 216