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