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 __SCCSID("@(#)kvm_hp300.c 8.1 (Berkeley) 6/4/93"); 38 39 /* 40 * AMD64 machine dependent routines for kvm. Hopefully, the forthcoming 41 * vm code will one day obsolete this module. 42 */ 43 44 #include <sys/param.h> 45 #include <sys/endian.h> 46 #include <stdint.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 #include <vm/vm.h> 51 #include <kvm.h> 52 53 #include <limits.h> 54 55 #include "kvm_private.h" 56 #include "kvm_amd64.h" 57 58 struct vmstate { 59 size_t phnum; 60 GElf_Phdr *phdr; 61 amd64_pml4e_t *PML4; 62 }; 63 64 /* 65 * Translate a physical memory address to a file-offset in the crash-dump. 66 */ 67 static size_t 68 _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs) 69 { 70 struct vmstate *vm = kd->vmst; 71 GElf_Phdr *p; 72 size_t n; 73 74 if (kd->rawdump) { 75 *ofs = pa; 76 return (AMD64_PAGE_SIZE - (pa & AMD64_PAGE_MASK)); 77 } 78 79 p = vm->phdr; 80 n = vm->phnum; 81 while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz)) 82 p++, n--; 83 if (n == 0) 84 return (0); 85 *ofs = (pa - p->p_paddr) + p->p_offset; 86 return (AMD64_PAGE_SIZE - (pa & AMD64_PAGE_MASK)); 87 } 88 89 static void 90 _amd64_freevtop(kvm_t *kd) 91 { 92 struct vmstate *vm = kd->vmst; 93 94 if (vm->PML4) 95 free(vm->PML4); 96 free(vm->phdr); 97 free(vm); 98 kd->vmst = NULL; 99 } 100 101 static int 102 _amd64_probe(kvm_t *kd) 103 { 104 105 return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_X86_64) && 106 !_kvm_is_minidump(kd)); 107 } 108 109 static int 110 _amd64_initvtop(kvm_t *kd) 111 { 112 struct kvm_nlist nl[2]; 113 amd64_physaddr_t pa; 114 kvaddr_t kernbase; 115 amd64_pml4e_t *PML4; 116 117 kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst)); 118 if (kd->vmst == NULL) { 119 _kvm_err(kd, kd->program, "cannot allocate vm"); 120 return (-1); 121 } 122 kd->vmst->PML4 = 0; 123 124 if (kd->rawdump == 0) { 125 if (_kvm_read_core_phdrs(kd, &kd->vmst->phnum, 126 &kd->vmst->phdr) == -1) 127 return (-1); 128 } 129 130 nl[0].n_name = "kernbase"; 131 nl[1].n_name = 0; 132 133 if (kvm_nlist2(kd, nl) != 0) { 134 _kvm_err(kd, kd->program, "bad namelist - no kernbase"); 135 return (-1); 136 } 137 kernbase = nl[0].n_value; 138 139 nl[0].n_name = "KPML4phys"; 140 nl[1].n_name = 0; 141 142 if (kvm_nlist2(kd, nl) != 0) { 143 _kvm_err(kd, kd->program, "bad namelist - no KPML4phys"); 144 return (-1); 145 } 146 if (kvm_read2(kd, (nl[0].n_value - kernbase), &pa, sizeof(pa)) != 147 sizeof(pa)) { 148 _kvm_err(kd, kd->program, "cannot read KPML4phys"); 149 return (-1); 150 } 151 pa = le64toh(pa); 152 PML4 = _kvm_malloc(kd, AMD64_PAGE_SIZE); 153 if (PML4 == NULL) { 154 _kvm_err(kd, kd->program, "cannot allocate PML4"); 155 return (-1); 156 } 157 if (kvm_read2(kd, pa, PML4, AMD64_PAGE_SIZE) != AMD64_PAGE_SIZE) { 158 _kvm_err(kd, kd->program, "cannot read KPML4phys"); 159 free(PML4); 160 return (-1); 161 } 162 kd->vmst->PML4 = PML4; 163 return (0); 164 } 165 166 static int 167 _amd64_vatop(kvm_t *kd, kvaddr_t va, off_t *pa) 168 { 169 struct vmstate *vm; 170 amd64_physaddr_t offset; 171 amd64_physaddr_t pdpe_pa; 172 amd64_physaddr_t pde_pa; 173 amd64_physaddr_t pte_pa; 174 amd64_pml4e_t pml4e; 175 amd64_pdpe_t pdpe; 176 amd64_pde_t pde; 177 amd64_pte_t pte; 178 kvaddr_t pml4eindex; 179 kvaddr_t pdpeindex; 180 kvaddr_t pdeindex; 181 kvaddr_t pteindex; 182 amd64_physaddr_t a; 183 off_t ofs; 184 size_t s; 185 186 vm = kd->vmst; 187 offset = va & AMD64_PAGE_MASK; 188 189 /* 190 * If we are initializing (kernel page table descriptor pointer 191 * not yet set) then return pa == va to avoid infinite recursion. 192 */ 193 if (vm->PML4 == NULL) { 194 s = _kvm_pa2off(kd, va, pa); 195 if (s == 0) { 196 _kvm_err(kd, kd->program, 197 "_amd64_vatop: bootstrap data not in dump"); 198 goto invalid; 199 } else 200 return (AMD64_PAGE_SIZE - offset); 201 } 202 203 pml4eindex = (va >> AMD64_PML4SHIFT) & (AMD64_NPML4EPG - 1); 204 pml4e = le64toh(vm->PML4[pml4eindex]); 205 if ((pml4e & AMD64_PG_V) == 0) { 206 _kvm_err(kd, kd->program, "_amd64_vatop: pml4e not valid"); 207 goto invalid; 208 } 209 210 pdpeindex = (va >> AMD64_PDPSHIFT) & (AMD64_NPDPEPG - 1); 211 pdpe_pa = (pml4e & AMD64_PG_FRAME) + (pdpeindex * sizeof(amd64_pdpe_t)); 212 213 s = _kvm_pa2off(kd, pdpe_pa, &ofs); 214 if (s < sizeof(pdpe)) { 215 _kvm_err(kd, kd->program, "_amd64_vatop: pdpe_pa not found"); 216 goto invalid; 217 } 218 if (pread(kd->pmfd, &pdpe, sizeof(pdpe), ofs) != sizeof(pdpe)) { 219 _kvm_syserr(kd, kd->program, "_amd64_vatop: read pdpe"); 220 goto invalid; 221 } 222 pdpe = le64toh(pdpe); 223 if ((pdpe & AMD64_PG_V) == 0) { 224 _kvm_err(kd, kd->program, "_amd64_vatop: pdpe not valid"); 225 goto invalid; 226 } 227 228 if (pdpe & AMD64_PG_PS) { 229 /* 230 * No next-level page table; pdpe describes one 1GB page. 231 */ 232 a = (pdpe & AMD64_PG_1GB_FRAME) + (va & AMD64_PDPMASK); 233 s = _kvm_pa2off(kd, a, pa); 234 if (s == 0) { 235 _kvm_err(kd, kd->program, 236 "_amd64_vatop: 1GB page address not in dump"); 237 goto invalid; 238 } else 239 return (AMD64_NBPDP - (va & AMD64_PDPMASK)); 240 } 241 242 pdeindex = (va >> AMD64_PDRSHIFT) & (AMD64_NPDEPG - 1); 243 pde_pa = (pdpe & AMD64_PG_FRAME) + (pdeindex * sizeof(amd64_pde_t)); 244 245 s = _kvm_pa2off(kd, pde_pa, &ofs); 246 if (s < sizeof(pde)) { 247 _kvm_syserr(kd, kd->program, "_amd64_vatop: pde_pa not found"); 248 goto invalid; 249 } 250 if (pread(kd->pmfd, &pde, sizeof(pde), ofs) != sizeof(pde)) { 251 _kvm_syserr(kd, kd->program, "_amd64_vatop: read pde"); 252 goto invalid; 253 } 254 pde = le64toh(pde); 255 if ((pde & AMD64_PG_V) == 0) { 256 _kvm_err(kd, kd->program, "_amd64_vatop: pde not valid"); 257 goto invalid; 258 } 259 260 if (pde & AMD64_PG_PS) { 261 /* 262 * No final-level page table; pde describes one 2MB page. 263 */ 264 a = (pde & AMD64_PG_PS_FRAME) + (va & AMD64_PDRMASK); 265 s = _kvm_pa2off(kd, a, pa); 266 if (s == 0) { 267 _kvm_err(kd, kd->program, 268 "_amd64_vatop: 2MB page address not in dump"); 269 goto invalid; 270 } else 271 return (AMD64_NBPDR - (va & AMD64_PDRMASK)); 272 } 273 274 pteindex = (va >> AMD64_PAGE_SHIFT) & (AMD64_NPTEPG - 1); 275 pte_pa = (pde & AMD64_PG_FRAME) + (pteindex * sizeof(amd64_pte_t)); 276 277 s = _kvm_pa2off(kd, pte_pa, &ofs); 278 if (s < sizeof(pte)) { 279 _kvm_err(kd, kd->program, "_amd64_vatop: pte_pa not found"); 280 goto invalid; 281 } 282 if (pread(kd->pmfd, &pte, sizeof(pte), ofs) != sizeof(pte)) { 283 _kvm_syserr(kd, kd->program, "_amd64_vatop: read"); 284 goto invalid; 285 } 286 if ((pte & AMD64_PG_V) == 0) { 287 _kvm_err(kd, kd->program, "_amd64_vatop: pte not valid"); 288 goto invalid; 289 } 290 291 a = (pte & AMD64_PG_FRAME) + offset; 292 s = _kvm_pa2off(kd, a, pa); 293 if (s == 0) { 294 _kvm_err(kd, kd->program, "_amd64_vatop: address not in dump"); 295 goto invalid; 296 } else 297 return (AMD64_PAGE_SIZE - offset); 298 299 invalid: 300 _kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va); 301 return (0); 302 } 303 304 static int 305 _amd64_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa) 306 { 307 308 if (ISALIVE(kd)) { 309 _kvm_err(kd, 0, "kvm_kvatop called in live kernel!"); 310 return (0); 311 } 312 return (_amd64_vatop(kd, va, pa)); 313 } 314 315 int 316 _amd64_native(kvm_t *kd __unused) 317 { 318 319 #ifdef __amd64__ 320 return (1); 321 #else 322 return (0); 323 #endif 324 } 325 326 static struct kvm_arch kvm_amd64 = { 327 .ka_probe = _amd64_probe, 328 .ka_initvtop = _amd64_initvtop, 329 .ka_freevtop = _amd64_freevtop, 330 .ka_kvatop = _amd64_kvatop, 331 .ka_native = _amd64_native, 332 }; 333 334 KVM_ARCH(kvm_amd64); 335