1 /*- 2 * Copyright (c) 1989, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software developed by the Computer Systems 6 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract 7 * BG 91-66 and contributed to Berkeley. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #if defined(LIBC_SCCS) && !defined(lint) 38 #if 0 39 static char sccsid[] = "@(#)kvm_hp300.c 8.1 (Berkeley) 6/4/93"; 40 #endif 41 #endif /* LIBC_SCCS and not lint */ 42 43 /* 44 * AMD64 machine dependent routines for kvm. Hopefully, the forthcoming 45 * vm code will one day obsolete this module. 46 */ 47 48 #include <sys/param.h> 49 #include <sys/endian.h> 50 #include <stdint.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <unistd.h> 54 #include <kvm.h> 55 56 #include <limits.h> 57 58 #include "kvm_private.h" 59 #include "kvm_amd64.h" 60 61 struct vmstate { 62 size_t phnum; 63 GElf_Phdr *phdr; 64 amd64_pml4e_t *PML4; 65 }; 66 67 /* 68 * Translate a physical memory address to a file-offset in the crash-dump. 69 */ 70 static size_t 71 _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs) 72 { 73 struct vmstate *vm = kd->vmst; 74 GElf_Phdr *p; 75 size_t n; 76 77 if (kd->rawdump) { 78 *ofs = pa; 79 return (AMD64_PAGE_SIZE - (pa & AMD64_PAGE_MASK)); 80 } 81 82 p = vm->phdr; 83 n = vm->phnum; 84 while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz)) 85 p++, n--; 86 if (n == 0) 87 return (0); 88 *ofs = (pa - p->p_paddr) + p->p_offset; 89 return (AMD64_PAGE_SIZE - (pa & AMD64_PAGE_MASK)); 90 } 91 92 static void 93 _amd64_freevtop(kvm_t *kd) 94 { 95 struct vmstate *vm = kd->vmst; 96 97 if (vm->PML4) 98 free(vm->PML4); 99 free(vm->phdr); 100 free(vm); 101 kd->vmst = NULL; 102 } 103 104 static int 105 _amd64_probe(kvm_t *kd) 106 { 107 108 return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_X86_64) && 109 !_kvm_is_minidump(kd)); 110 } 111 112 static int 113 _amd64_initvtop(kvm_t *kd) 114 { 115 struct kvm_nlist nl[2]; 116 amd64_physaddr_t pa; 117 kvaddr_t kernbase; 118 amd64_pml4e_t *PML4; 119 120 kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst)); 121 if (kd->vmst == NULL) { 122 _kvm_err(kd, kd->program, "cannot allocate vm"); 123 return (-1); 124 } 125 kd->vmst->PML4 = 0; 126 127 if (kd->rawdump == 0) { 128 if (_kvm_read_core_phdrs(kd, &kd->vmst->phnum, 129 &kd->vmst->phdr) == -1) 130 return (-1); 131 } 132 133 nl[0].n_name = "kernbase"; 134 nl[1].n_name = 0; 135 136 if (kvm_nlist2(kd, nl) != 0) { 137 _kvm_err(kd, kd->program, "bad namelist - no kernbase"); 138 return (-1); 139 } 140 kernbase = nl[0].n_value; 141 142 nl[0].n_name = "KPML4phys"; 143 nl[1].n_name = 0; 144 145 if (kvm_nlist2(kd, nl) != 0) { 146 _kvm_err(kd, kd->program, "bad namelist - no KPML4phys"); 147 return (-1); 148 } 149 if (kvm_read2(kd, (nl[0].n_value - kernbase), &pa, sizeof(pa)) != 150 sizeof(pa)) { 151 _kvm_err(kd, kd->program, "cannot read KPML4phys"); 152 return (-1); 153 } 154 pa = le64toh(pa); 155 PML4 = _kvm_malloc(kd, AMD64_PAGE_SIZE); 156 if (PML4 == NULL) { 157 _kvm_err(kd, kd->program, "cannot allocate PML4"); 158 return (-1); 159 } 160 if (kvm_read2(kd, pa, PML4, AMD64_PAGE_SIZE) != AMD64_PAGE_SIZE) { 161 _kvm_err(kd, kd->program, "cannot read KPML4phys"); 162 return (-1); 163 } 164 kd->vmst->PML4 = PML4; 165 return (0); 166 } 167 168 static int 169 _amd64_vatop(kvm_t *kd, kvaddr_t va, off_t *pa) 170 { 171 struct vmstate *vm; 172 amd64_physaddr_t offset; 173 amd64_physaddr_t pdpe_pa; 174 amd64_physaddr_t pde_pa; 175 amd64_physaddr_t pte_pa; 176 amd64_pml4e_t pml4e; 177 amd64_pdpe_t pdpe; 178 amd64_pde_t pde; 179 amd64_pte_t pte; 180 kvaddr_t pml4eindex; 181 kvaddr_t pdpeindex; 182 kvaddr_t pdeindex; 183 kvaddr_t pteindex; 184 amd64_physaddr_t a; 185 off_t ofs; 186 size_t s; 187 188 vm = kd->vmst; 189 offset = va & AMD64_PAGE_MASK; 190 191 /* 192 * If we are initializing (kernel page table descriptor pointer 193 * not yet set) then return pa == va to avoid infinite recursion. 194 */ 195 if (vm->PML4 == NULL) { 196 s = _kvm_pa2off(kd, va, pa); 197 if (s == 0) { 198 _kvm_err(kd, kd->program, 199 "_amd64_vatop: bootstrap data not in dump"); 200 goto invalid; 201 } else 202 return (AMD64_PAGE_SIZE - offset); 203 } 204 205 pml4eindex = (va >> AMD64_PML4SHIFT) & (AMD64_NPML4EPG - 1); 206 pml4e = le64toh(vm->PML4[pml4eindex]); 207 if ((pml4e & AMD64_PG_V) == 0) { 208 _kvm_err(kd, kd->program, "_amd64_vatop: pml4e not valid"); 209 goto invalid; 210 } 211 212 pdpeindex = (va >> AMD64_PDPSHIFT) & (AMD64_NPDPEPG - 1); 213 pdpe_pa = (pml4e & AMD64_PG_FRAME) + (pdpeindex * sizeof(amd64_pdpe_t)); 214 215 s = _kvm_pa2off(kd, pdpe_pa, &ofs); 216 if (s < sizeof(pdpe)) { 217 _kvm_err(kd, kd->program, "_amd64_vatop: pdpe_pa not found"); 218 goto invalid; 219 } 220 if (pread(kd->pmfd, &pdpe, sizeof(pdpe), ofs) != sizeof(pdpe)) { 221 _kvm_syserr(kd, kd->program, "_amd64_vatop: read pdpe"); 222 goto invalid; 223 } 224 pdpe = le64toh(pdpe); 225 if ((pdpe & AMD64_PG_V) == 0) { 226 _kvm_err(kd, kd->program, "_amd64_vatop: pdpe not valid"); 227 goto invalid; 228 } 229 230 if (pdpe & AMD64_PG_PS) { 231 /* 232 * No next-level page table; pdpe describes one 1GB page. 233 */ 234 a = (pdpe & AMD64_PG_1GB_FRAME) + (va & AMD64_PDPMASK); 235 s = _kvm_pa2off(kd, a, pa); 236 if (s == 0) { 237 _kvm_err(kd, kd->program, 238 "_amd64_vatop: 1GB page address not in dump"); 239 goto invalid; 240 } else 241 return (AMD64_NBPDP - (va & AMD64_PDPMASK)); 242 } 243 244 pdeindex = (va >> AMD64_PDRSHIFT) & (AMD64_NPDEPG - 1); 245 pde_pa = (pdpe & AMD64_PG_FRAME) + (pdeindex * sizeof(amd64_pde_t)); 246 247 s = _kvm_pa2off(kd, pde_pa, &ofs); 248 if (s < sizeof(pde)) { 249 _kvm_syserr(kd, kd->program, "_amd64_vatop: pde_pa not found"); 250 goto invalid; 251 } 252 if (pread(kd->pmfd, &pde, sizeof(pde), ofs) != sizeof(pde)) { 253 _kvm_syserr(kd, kd->program, "_amd64_vatop: read pde"); 254 goto invalid; 255 } 256 pde = le64toh(pde); 257 if ((pde & AMD64_PG_V) == 0) { 258 _kvm_err(kd, kd->program, "_amd64_vatop: pde not valid"); 259 goto invalid; 260 } 261 262 if (pde & AMD64_PG_PS) { 263 /* 264 * No final-level page table; pde describes one 2MB page. 265 */ 266 a = (pde & AMD64_PG_PS_FRAME) + (va & AMD64_PDRMASK); 267 s = _kvm_pa2off(kd, a, pa); 268 if (s == 0) { 269 _kvm_err(kd, kd->program, 270 "_amd64_vatop: 2MB page address not in dump"); 271 goto invalid; 272 } else 273 return (AMD64_NBPDR - (va & AMD64_PDRMASK)); 274 } 275 276 pteindex = (va >> AMD64_PAGE_SHIFT) & (AMD64_NPTEPG - 1); 277 pte_pa = (pde & AMD64_PG_FRAME) + (pteindex * sizeof(amd64_pte_t)); 278 279 s = _kvm_pa2off(kd, pte_pa, &ofs); 280 if (s < sizeof(pte)) { 281 _kvm_err(kd, kd->program, "_amd64_vatop: pte_pa not found"); 282 goto invalid; 283 } 284 if (pread(kd->pmfd, &pte, sizeof(pte), ofs) != sizeof(pte)) { 285 _kvm_syserr(kd, kd->program, "_amd64_vatop: read"); 286 goto invalid; 287 } 288 if ((pte & AMD64_PG_V) == 0) { 289 _kvm_err(kd, kd->program, "_amd64_vatop: pte not valid"); 290 goto invalid; 291 } 292 293 a = (pte & AMD64_PG_FRAME) + offset; 294 s = _kvm_pa2off(kd, a, pa); 295 if (s == 0) { 296 _kvm_err(kd, kd->program, "_amd64_vatop: address not in dump"); 297 goto invalid; 298 } else 299 return (AMD64_PAGE_SIZE - offset); 300 301 invalid: 302 _kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va); 303 return (0); 304 } 305 306 static int 307 _amd64_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa) 308 { 309 310 if (ISALIVE(kd)) { 311 _kvm_err(kd, 0, "kvm_kvatop called in live kernel!"); 312 return (0); 313 } 314 return (_amd64_vatop(kd, va, pa)); 315 } 316 317 int 318 _amd64_native(kvm_t *kd) 319 { 320 321 #ifdef __amd64__ 322 return (1); 323 #else 324 return (0); 325 #endif 326 } 327 328 struct kvm_arch kvm_amd64 = { 329 .ka_probe = _amd64_probe, 330 .ka_initvtop = _amd64_initvtop, 331 .ka_freevtop = _amd64_freevtop, 332 .ka_kvatop = _amd64_kvatop, 333 .ka_native = _amd64_native, 334 }; 335 336 KVM_ARCH(kvm_amd64); 337