2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2005 Olivier Houchard
5 * Copyright (c) 1989, 1992, 1993
6 * The Regents of the University of California. All rights reserved.
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * ARM machine dependent routines for kvm.
40 #include <sys/param.h>
41 #include <sys/endian.h>
49 #include <machine/vmparam.h>
52 #include "kvm_private.h"
62 * Translate a physical memory address to a file-offset in the crash-dump.
65 _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs, size_t pgsz)
67 struct vmstate *vm = kd->vmst;
73 while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz))
78 *ofs = (pa - p->p_paddr) + p->p_offset;
80 return (p->p_memsz - (pa - p->p_paddr));
81 return (pgsz - ((size_t)pa & (pgsz - 1)));
85 _arm_freevtop(kvm_t *kd)
87 struct vmstate *vm = kd->vmst;
98 return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_ARM) &&
99 !_kvm_is_minidump(kd));
103 _arm_initvtop(kvm_t *kd)
106 struct kvm_nlist nl[2];
108 arm_physaddr_t physaddr, pa;
109 arm_pd_entry_t *l1pt;
114 _kvm_err(kd, kd->program, "raw dumps not supported on arm");
118 vm = _kvm_malloc(kd, sizeof(*vm));
120 _kvm_err(kd, kd->program, "cannot allocate vm");
126 if (_kvm_read_core_phdrs(kd, &vm->phnum, &vm->phdr) == -1)
130 for (i = 0; i < vm->phnum; i++) {
131 if (vm->phdr[i].p_type == PT_DUMP_DELTA) {
132 kernbase = vm->phdr[i].p_vaddr;
133 physaddr = vm->phdr[i].p_paddr;
141 nl[0].n_name = "kernbase";
142 if (kvm_nlist2(kd, nl) != 0) {
146 _kvm_err(kd, kd->program, "cannot resolve kernbase");
150 kernbase = nl[0].n_value;
152 nl[0].n_name = "physaddr";
153 if (kvm_nlist2(kd, nl) != 0) {
154 _kvm_err(kd, kd->program, "couldn't get phys addr");
157 physaddr = nl[0].n_value;
159 nl[0].n_name = "kernel_l1pa";
160 if (kvm_nlist2(kd, nl) != 0) {
161 _kvm_err(kd, kd->program, "bad namelist");
164 if (kvm_read2(kd, (nl[0].n_value - kernbase + physaddr), &pa,
165 sizeof(pa)) != sizeof(pa)) {
166 _kvm_err(kd, kd->program, "cannot read kernel_l1pa");
169 l1pt = _kvm_malloc(kd, ARM_L1_TABLE_SIZE);
171 _kvm_err(kd, kd->program, "cannot allocate l1pt");
174 if (kvm_read2(kd, pa, l1pt, ARM_L1_TABLE_SIZE) != ARM_L1_TABLE_SIZE) {
175 _kvm_err(kd, kd->program, "cannot read l1pt");
183 /* from arm/pmap.c */
184 #define ARM_L1_IDX(va) ((va) >> ARM_L1_S_SHIFT)
186 #define l1pte_section_p(pde) (((pde) & ARM_L1_TYPE_MASK) == ARM_L1_TYPE_S)
187 #define l1pte_valid(pde) ((pde) != 0)
188 #define l2pte_valid(pte) ((pte) != 0)
189 #define l2pte_index(v) (((v) & ARM_L1_S_OFFSET) >> ARM_L2_S_SHIFT)
193 _arm_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
195 struct vmstate *vm = kd->vmst;
198 arm_physaddr_t pte_pa;
201 if (vm->l1pt == NULL)
202 return (_kvm_pa2off(kd, va, pa, ARM_PAGE_SIZE));
203 pd = _kvm32toh(kd, vm->l1pt[ARM_L1_IDX(va)]);
204 if (!l1pte_valid(pd))
206 if (l1pte_section_p(pd)) {
207 /* 1MB section mapping. */
208 *pa = (pd & ARM_L1_S_ADDR_MASK) + (va & ARM_L1_S_OFFSET);
209 return (_kvm_pa2off(kd, *pa, pa, ARM_L1_S_SIZE));
211 pte_pa = (pd & ARM_L1_C_ADDR_MASK) + l2pte_index(va) * sizeof(pte);
212 _kvm_pa2off(kd, pte_pa, &pte_off, ARM_L1_S_SIZE);
213 if (pread(kd->pmfd, &pte, sizeof(pte), pte_off) != sizeof(pte)) {
214 _kvm_syserr(kd, kd->program, "_arm_kvatop: pread");
217 pte = _kvm32toh(kd, pte);
218 if (!l2pte_valid(pte)) {
221 if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_L) {
222 *pa = (pte & ARM_L2_L_FRAME) | (va & ARM_L2_L_OFFSET);
223 return (_kvm_pa2off(kd, *pa, pa, ARM_L2_L_SIZE));
225 *pa = (pte & ARM_L2_S_FRAME) | (va & ARM_L2_S_OFFSET);
226 return (_kvm_pa2off(kd, *pa, pa, ARM_PAGE_SIZE));
228 _kvm_err(kd, 0, "Invalid address (%jx)", (uintmax_t)va);
233 * Machine-dependent initialization for ALL open kvm descriptors,
234 * not just those for a kernel crash dump. Some architectures
235 * have to deal with these NOT being constants! (i.e. m68k)
239 _kvm_mdopen(kvm_t *kd)
242 kd->usrstack = USRSTACK;
243 kd->min_uva = VM_MIN_ADDRESS;
244 kd->max_uva = VM_MAXUSER_ADDRESS;
252 _arm_native(kvm_t *kd)
254 _arm_native(kvm_t *kd __unused)
259 #if _BYTE_ORDER == _LITTLE_ENDIAN
260 return (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2LSB);
262 return (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2MSB);
269 static struct kvm_arch kvm_arm = {
270 .ka_probe = _arm_probe,
271 .ka_initvtop = _arm_initvtop,
272 .ka_freevtop = _arm_freevtop,
273 .ka_kvatop = _arm_kvatop,
274 .ka_native = _arm_native,