18cffa1b4SOlivier Houchard /*-
2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni *
45fa78ac5SWarner Losh * Copyright (c) 2005 Olivier Houchard
55fa78ac5SWarner Losh * Copyright (c) 1989, 1992, 1993
65fa78ac5SWarner Losh * The Regents of the University of California. All rights reserved.
75fa78ac5SWarner Losh *
85fa78ac5SWarner Losh * This code is derived from software developed by the Computer Systems
95fa78ac5SWarner Losh * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
105fa78ac5SWarner Losh * BG 91-66 and contributed to Berkeley.
118cffa1b4SOlivier Houchard *
128cffa1b4SOlivier Houchard * Redistribution and use in source and binary forms, with or without
138cffa1b4SOlivier Houchard * modification, are permitted provided that the following conditions
148cffa1b4SOlivier Houchard * are met:
158cffa1b4SOlivier Houchard * 1. Redistributions of source code must retain the above copyright
168cffa1b4SOlivier Houchard * notice, this list of conditions and the following disclaimer.
178cffa1b4SOlivier Houchard * 2. Redistributions in binary form must reproduce the above copyright
188cffa1b4SOlivier Houchard * notice, this list of conditions and the following disclaimer in the
198cffa1b4SOlivier Houchard * documentation and/or other materials provided with the distribution.
20fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
215fa78ac5SWarner Losh * may be used to endorse or promote products derived from this software
225fa78ac5SWarner Losh * without specific prior written permission.
238cffa1b4SOlivier Houchard *
248cffa1b4SOlivier Houchard * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
258cffa1b4SOlivier Houchard * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
268cffa1b4SOlivier Houchard * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
278cffa1b4SOlivier Houchard * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
288cffa1b4SOlivier Houchard * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
298cffa1b4SOlivier Houchard * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
308cffa1b4SOlivier Houchard * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
318cffa1b4SOlivier Houchard * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
328cffa1b4SOlivier Houchard * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
338cffa1b4SOlivier Houchard * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
348cffa1b4SOlivier Houchard */
358cffa1b4SOlivier Houchard
368cffa1b4SOlivier Houchard /*
378cffa1b4SOlivier Houchard * ARM machine dependent routines for kvm.
388cffa1b4SOlivier Houchard */
398cffa1b4SOlivier Houchard
408cffa1b4SOlivier Houchard #include <sys/param.h>
417f911abeSJohn Baldwin #include <sys/endian.h>
428cffa1b4SOlivier Houchard #include <kvm.h>
437f911abeSJohn Baldwin #include <limits.h>
447f911abeSJohn Baldwin #include <stdint.h>
458cffa1b4SOlivier Houchard #include <stdlib.h>
468e321b79SRafal Jaworowski #include <unistd.h>
478cffa1b4SOlivier Houchard
487f911abeSJohn Baldwin #ifdef __arm__
497f911abeSJohn Baldwin #include <machine/vmparam.h>
507f911abeSJohn Baldwin #endif
517f911abeSJohn Baldwin
528cffa1b4SOlivier Houchard #include "kvm_private.h"
537f911abeSJohn Baldwin #include "kvm_arm.h"
548cffa1b4SOlivier Houchard
559960ac47SOlivier Houchard struct vmstate {
567f911abeSJohn Baldwin arm_pd_entry_t *l1pt;
577f911abeSJohn Baldwin size_t phnum;
587f911abeSJohn Baldwin GElf_Phdr *phdr;
599960ac47SOlivier Houchard };
609960ac47SOlivier Houchard
619960ac47SOlivier Houchard /*
629960ac47SOlivier Houchard * Translate a physical memory address to a file-offset in the crash-dump.
639960ac47SOlivier Houchard */
649960ac47SOlivier Houchard static size_t
_kvm_pa2off(kvm_t * kd,uint64_t pa,off_t * ofs,size_t pgsz)659960ac47SOlivier Houchard _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs, size_t pgsz)
669960ac47SOlivier Houchard {
677f911abeSJohn Baldwin struct vmstate *vm = kd->vmst;
687f911abeSJohn Baldwin GElf_Phdr *p;
697f911abeSJohn Baldwin size_t n;
709960ac47SOlivier Houchard
717f911abeSJohn Baldwin p = vm->phdr;
727f911abeSJohn Baldwin n = vm->phnum;
739960ac47SOlivier Houchard while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz))
749960ac47SOlivier Houchard p++, n--;
759960ac47SOlivier Houchard if (n == 0)
769960ac47SOlivier Houchard return (0);
779960ac47SOlivier Houchard
789960ac47SOlivier Houchard *ofs = (pa - p->p_paddr) + p->p_offset;
799960ac47SOlivier Houchard if (pgsz == 0)
809960ac47SOlivier Houchard return (p->p_memsz - (pa - p->p_paddr));
819960ac47SOlivier Houchard return (pgsz - ((size_t)pa & (pgsz - 1)));
829960ac47SOlivier Houchard }
839960ac47SOlivier Houchard
847f911abeSJohn Baldwin static void
_arm_freevtop(kvm_t * kd)857f911abeSJohn Baldwin _arm_freevtop(kvm_t *kd)
868cffa1b4SOlivier Houchard {
877f911abeSJohn Baldwin struct vmstate *vm = kd->vmst;
887f911abeSJohn Baldwin
897f911abeSJohn Baldwin free(vm->phdr);
907f911abeSJohn Baldwin free(vm);
919960ac47SOlivier Houchard kd->vmst = NULL;
929960ac47SOlivier Houchard }
937f911abeSJohn Baldwin
947f911abeSJohn Baldwin static int
_arm_probe(kvm_t * kd)957f911abeSJohn Baldwin _arm_probe(kvm_t *kd)
967f911abeSJohn Baldwin {
977f911abeSJohn Baldwin
987f911abeSJohn Baldwin return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_ARM) &&
997f911abeSJohn Baldwin !_kvm_is_minidump(kd));
1008cffa1b4SOlivier Houchard }
1018cffa1b4SOlivier Houchard
1027f911abeSJohn Baldwin static int
_arm_initvtop(kvm_t * kd)1037f911abeSJohn Baldwin _arm_initvtop(kvm_t *kd)
1048cffa1b4SOlivier Houchard {
1058e321b79SRafal Jaworowski struct vmstate *vm;
1067f911abeSJohn Baldwin struct kvm_nlist nl[2];
1077f911abeSJohn Baldwin kvaddr_t kernbase;
1087f911abeSJohn Baldwin arm_physaddr_t physaddr, pa;
1097f911abeSJohn Baldwin arm_pd_entry_t *l1pt;
1107f911abeSJohn Baldwin size_t i;
1117f911abeSJohn Baldwin int found;
1129960ac47SOlivier Houchard
1137f911abeSJohn Baldwin if (kd->rawdump) {
1147f911abeSJohn Baldwin _kvm_err(kd, kd->program, "raw dumps not supported on arm");
1158e321b79SRafal Jaworowski return (-1);
1168e321b79SRafal Jaworowski }
1178e321b79SRafal Jaworowski
1188e321b79SRafal Jaworowski vm = _kvm_malloc(kd, sizeof(*vm));
119fb0e1892SEnji Cooper if (vm == NULL) {
1209960ac47SOlivier Houchard _kvm_err(kd, kd->program, "cannot allocate vm");
1219960ac47SOlivier Houchard return (-1);
1229960ac47SOlivier Houchard }
1239960ac47SOlivier Houchard kd->vmst = vm;
1249960ac47SOlivier Houchard vm->l1pt = NULL;
1257f911abeSJohn Baldwin
1267f911abeSJohn Baldwin if (_kvm_read_core_phdrs(kd, &vm->phnum, &vm->phdr) == -1)
1279960ac47SOlivier Houchard return (-1);
12858c47a70SAndrew Turner
12958c47a70SAndrew Turner found = 0;
1307f911abeSJohn Baldwin for (i = 0; i < vm->phnum; i++) {
1317f911abeSJohn Baldwin if (vm->phdr[i].p_type == PT_DUMP_DELTA) {
1327f911abeSJohn Baldwin kernbase = vm->phdr[i].p_vaddr;
1337f911abeSJohn Baldwin physaddr = vm->phdr[i].p_paddr;
13458c47a70SAndrew Turner found = 1;
13558c47a70SAndrew Turner break;
13658c47a70SAndrew Turner }
13758c47a70SAndrew Turner }
13858c47a70SAndrew Turner
139c10970ddSUlrich Spörlein nl[1].n_name = NULL;
14058c47a70SAndrew Turner if (!found) {
14158c47a70SAndrew Turner nl[0].n_name = "kernbase";
1427f911abeSJohn Baldwin if (kvm_nlist2(kd, nl) != 0) {
1437f911abeSJohn Baldwin #ifdef __arm__
1449960ac47SOlivier Houchard kernbase = KERNBASE;
1457f911abeSJohn Baldwin #else
1467f911abeSJohn Baldwin _kvm_err(kd, kd->program, "cannot resolve kernbase");
1477f911abeSJohn Baldwin return (-1);
1487f911abeSJohn Baldwin #endif
1497f911abeSJohn Baldwin } else
150c10970ddSUlrich Spörlein kernbase = nl[0].n_value;
1519960ac47SOlivier Houchard
152c10970ddSUlrich Spörlein nl[0].n_name = "physaddr";
1537f911abeSJohn Baldwin if (kvm_nlist2(kd, nl) != 0) {
1549960ac47SOlivier Houchard _kvm_err(kd, kd->program, "couldn't get phys addr");
1559960ac47SOlivier Houchard return (-1);
1569960ac47SOlivier Houchard }
157c10970ddSUlrich Spörlein physaddr = nl[0].n_value;
15858c47a70SAndrew Turner }
159c10970ddSUlrich Spörlein nl[0].n_name = "kernel_l1pa";
1607f911abeSJohn Baldwin if (kvm_nlist2(kd, nl) != 0) {
1619960ac47SOlivier Houchard _kvm_err(kd, kd->program, "bad namelist");
1629960ac47SOlivier Houchard return (-1);
1639960ac47SOlivier Houchard }
1647f911abeSJohn Baldwin if (kvm_read2(kd, (nl[0].n_value - kernbase + physaddr), &pa,
1659960ac47SOlivier Houchard sizeof(pa)) != sizeof(pa)) {
1669960ac47SOlivier Houchard _kvm_err(kd, kd->program, "cannot read kernel_l1pa");
1679960ac47SOlivier Houchard return (-1);
1689960ac47SOlivier Houchard }
1697f911abeSJohn Baldwin l1pt = _kvm_malloc(kd, ARM_L1_TABLE_SIZE);
170fb0e1892SEnji Cooper if (l1pt == NULL) {
171fb0e1892SEnji Cooper _kvm_err(kd, kd->program, "cannot allocate l1pt");
172fb0e1892SEnji Cooper return (-1);
173fb0e1892SEnji Cooper }
1747f911abeSJohn Baldwin if (kvm_read2(kd, pa, l1pt, ARM_L1_TABLE_SIZE) != ARM_L1_TABLE_SIZE) {
1759960ac47SOlivier Houchard _kvm_err(kd, kd->program, "cannot read l1pt");
1769960ac47SOlivier Houchard free(l1pt);
1779960ac47SOlivier Houchard return (-1);
1789960ac47SOlivier Houchard }
1799960ac47SOlivier Houchard vm->l1pt = l1pt;
1808cffa1b4SOlivier Houchard return 0;
1818cffa1b4SOlivier Houchard }
1828cffa1b4SOlivier Houchard
1839960ac47SOlivier Houchard /* from arm/pmap.c */
1847f911abeSJohn Baldwin #define ARM_L1_IDX(va) ((va) >> ARM_L1_S_SHIFT)
1859960ac47SOlivier Houchard
1867f911abeSJohn Baldwin #define l1pte_section_p(pde) (((pde) & ARM_L1_TYPE_MASK) == ARM_L1_TYPE_S)
1879960ac47SOlivier Houchard #define l1pte_valid(pde) ((pde) != 0)
1889960ac47SOlivier Houchard #define l2pte_valid(pte) ((pte) != 0)
189d97d068fSSvatopluk Kraus #define l2pte_index(v) (((v) & ARM_L1_S_OFFSET) >> ARM_L2_S_SHIFT)
1909960ac47SOlivier Houchard
1919960ac47SOlivier Houchard
1927f911abeSJohn Baldwin static int
_arm_kvatop(kvm_t * kd,kvaddr_t va,off_t * pa)1937f911abeSJohn Baldwin _arm_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
1948cffa1b4SOlivier Houchard {
1959960ac47SOlivier Houchard struct vmstate *vm = kd->vmst;
1967f911abeSJohn Baldwin arm_pd_entry_t pd;
1977f911abeSJohn Baldwin arm_pt_entry_t pte;
1987f911abeSJohn Baldwin arm_physaddr_t pte_pa;
1997f911abeSJohn Baldwin off_t pte_off;
2008e321b79SRafal Jaworowski
2019960ac47SOlivier Houchard if (vm->l1pt == NULL)
2027f911abeSJohn Baldwin return (_kvm_pa2off(kd, va, pa, ARM_PAGE_SIZE));
2037f911abeSJohn Baldwin pd = _kvm32toh(kd, vm->l1pt[ARM_L1_IDX(va)]);
2049960ac47SOlivier Houchard if (!l1pte_valid(pd))
2059960ac47SOlivier Houchard goto invalid;
2069960ac47SOlivier Houchard if (l1pte_section_p(pd)) {
2079960ac47SOlivier Houchard /* 1MB section mapping. */
2087f911abeSJohn Baldwin *pa = (pd & ARM_L1_S_ADDR_MASK) + (va & ARM_L1_S_OFFSET);
2097f911abeSJohn Baldwin return (_kvm_pa2off(kd, *pa, pa, ARM_L1_S_SIZE));
2109960ac47SOlivier Houchard }
2117f911abeSJohn Baldwin pte_pa = (pd & ARM_L1_C_ADDR_MASK) + l2pte_index(va) * sizeof(pte);
2127f911abeSJohn Baldwin _kvm_pa2off(kd, pte_pa, &pte_off, ARM_L1_S_SIZE);
2137f911abeSJohn Baldwin if (pread(kd->pmfd, &pte, sizeof(pte), pte_off) != sizeof(pte)) {
2147f911abeSJohn Baldwin _kvm_syserr(kd, kd->program, "_arm_kvatop: pread");
2159960ac47SOlivier Houchard goto invalid;
2169960ac47SOlivier Houchard }
2177f911abeSJohn Baldwin pte = _kvm32toh(kd, pte);
2189960ac47SOlivier Houchard if (!l2pte_valid(pte)) {
2199960ac47SOlivier Houchard goto invalid;
2209960ac47SOlivier Houchard }
2217f911abeSJohn Baldwin if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_L) {
2227f911abeSJohn Baldwin *pa = (pte & ARM_L2_L_FRAME) | (va & ARM_L2_L_OFFSET);
2237f911abeSJohn Baldwin return (_kvm_pa2off(kd, *pa, pa, ARM_L2_L_SIZE));
2249960ac47SOlivier Houchard }
2257f911abeSJohn Baldwin *pa = (pte & ARM_L2_S_FRAME) | (va & ARM_L2_S_OFFSET);
2267f911abeSJohn Baldwin return (_kvm_pa2off(kd, *pa, pa, ARM_PAGE_SIZE));
2279960ac47SOlivier Houchard invalid:
2287f911abeSJohn Baldwin _kvm_err(kd, 0, "Invalid address (%jx)", (uintmax_t)va);
2298cffa1b4SOlivier Houchard return 0;
2308cffa1b4SOlivier Houchard }
2318cffa1b4SOlivier Houchard
2328cffa1b4SOlivier Houchard /*
2338cffa1b4SOlivier Houchard * Machine-dependent initialization for ALL open kvm descriptors,
2348cffa1b4SOlivier Houchard * not just those for a kernel crash dump. Some architectures
2358cffa1b4SOlivier Houchard * have to deal with these NOT being constants! (i.e. m68k)
2368cffa1b4SOlivier Houchard */
237c10970ddSUlrich Spörlein #ifdef FBSD_NOT_YET
2388cffa1b4SOlivier Houchard int
_kvm_mdopen(kvm_t * kd)239c10970ddSUlrich Spörlein _kvm_mdopen(kvm_t *kd)
2408cffa1b4SOlivier Houchard {
2418cffa1b4SOlivier Houchard
2428cffa1b4SOlivier Houchard kd->usrstack = USRSTACK;
2438cffa1b4SOlivier Houchard kd->min_uva = VM_MIN_ADDRESS;
2448cffa1b4SOlivier Houchard kd->max_uva = VM_MAXUSER_ADDRESS;
2458cffa1b4SOlivier Houchard
2468cffa1b4SOlivier Houchard return (0);
2478cffa1b4SOlivier Houchard }
248c10970ddSUlrich Spörlein #endif
2497f911abeSJohn Baldwin
2507f911abeSJohn Baldwin int
251881b0edbSEnji Cooper #ifdef __arm__
_arm_native(kvm_t * kd)2527f911abeSJohn Baldwin _arm_native(kvm_t *kd)
253881b0edbSEnji Cooper #else
254881b0edbSEnji Cooper _arm_native(kvm_t *kd __unused)
255881b0edbSEnji Cooper #endif
2567f911abeSJohn Baldwin {
2577f911abeSJohn Baldwin
2587f911abeSJohn Baldwin #ifdef __arm__
2597f911abeSJohn Baldwin #if _BYTE_ORDER == _LITTLE_ENDIAN
2607f911abeSJohn Baldwin return (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2LSB);
2617f911abeSJohn Baldwin #else
2627f911abeSJohn Baldwin return (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2MSB);
2637f911abeSJohn Baldwin #endif
2647f911abeSJohn Baldwin #else
2657f911abeSJohn Baldwin return (0);
2667f911abeSJohn Baldwin #endif
2677f911abeSJohn Baldwin }
2687f911abeSJohn Baldwin
269881b0edbSEnji Cooper static struct kvm_arch kvm_arm = {
2707f911abeSJohn Baldwin .ka_probe = _arm_probe,
2717f911abeSJohn Baldwin .ka_initvtop = _arm_initvtop,
2727f911abeSJohn Baldwin .ka_freevtop = _arm_freevtop,
2737f911abeSJohn Baldwin .ka_kvatop = _arm_kvatop,
2747f911abeSJohn Baldwin .ka_native = _arm_native,
2757f911abeSJohn Baldwin };
2767f911abeSJohn Baldwin
2777f911abeSJohn Baldwin KVM_ARCH(kvm_arm);
278