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/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/endian.h>
52 #include <machine/vmparam.h>
55 #include "kvm_private.h"
65 * Translate a physical memory address to a file-offset in the crash-dump.
68 _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs, size_t pgsz)
70 struct vmstate *vm = kd->vmst;
76 while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz))
81 *ofs = (pa - p->p_paddr) + p->p_offset;
83 return (p->p_memsz - (pa - p->p_paddr));
84 return (pgsz - ((size_t)pa & (pgsz - 1)));
88 _arm_freevtop(kvm_t *kd)
90 struct vmstate *vm = kd->vmst;
101 return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_ARM) &&
102 !_kvm_is_minidump(kd));
106 _arm_initvtop(kvm_t *kd)
109 struct kvm_nlist nl[2];
111 arm_physaddr_t physaddr, pa;
112 arm_pd_entry_t *l1pt;
117 _kvm_err(kd, kd->program, "raw dumps not supported on arm");
121 vm = _kvm_malloc(kd, sizeof(*vm));
123 _kvm_err(kd, kd->program, "cannot allocate vm");
129 if (_kvm_read_core_phdrs(kd, &vm->phnum, &vm->phdr) == -1)
133 for (i = 0; i < vm->phnum; i++) {
134 if (vm->phdr[i].p_type == PT_DUMP_DELTA) {
135 kernbase = vm->phdr[i].p_vaddr;
136 physaddr = vm->phdr[i].p_paddr;
144 nl[0].n_name = "kernbase";
145 if (kvm_nlist2(kd, nl) != 0) {
149 _kvm_err(kd, kd->program, "cannot resolve kernbase");
153 kernbase = nl[0].n_value;
155 nl[0].n_name = "physaddr";
156 if (kvm_nlist2(kd, nl) != 0) {
157 _kvm_err(kd, kd->program, "couldn't get phys addr");
160 physaddr = nl[0].n_value;
162 nl[0].n_name = "kernel_l1pa";
163 if (kvm_nlist2(kd, nl) != 0) {
164 _kvm_err(kd, kd->program, "bad namelist");
167 if (kvm_read2(kd, (nl[0].n_value - kernbase + physaddr), &pa,
168 sizeof(pa)) != sizeof(pa)) {
169 _kvm_err(kd, kd->program, "cannot read kernel_l1pa");
172 l1pt = _kvm_malloc(kd, ARM_L1_TABLE_SIZE);
174 _kvm_err(kd, kd->program, "cannot allocate l1pt");
177 if (kvm_read2(kd, pa, l1pt, ARM_L1_TABLE_SIZE) != ARM_L1_TABLE_SIZE) {
178 _kvm_err(kd, kd->program, "cannot read l1pt");
186 /* from arm/pmap.c */
187 #define ARM_L1_IDX(va) ((va) >> ARM_L1_S_SHIFT)
189 #define l1pte_section_p(pde) (((pde) & ARM_L1_TYPE_MASK) == ARM_L1_TYPE_S)
190 #define l1pte_valid(pde) ((pde) != 0)
191 #define l2pte_valid(pte) ((pte) != 0)
192 #define l2pte_index(v) (((v) & ARM_L1_S_OFFSET) >> ARM_L2_S_SHIFT)
196 _arm_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
198 struct vmstate *vm = kd->vmst;
201 arm_physaddr_t pte_pa;
204 if (vm->l1pt == NULL)
205 return (_kvm_pa2off(kd, va, pa, ARM_PAGE_SIZE));
206 pd = _kvm32toh(kd, vm->l1pt[ARM_L1_IDX(va)]);
207 if (!l1pte_valid(pd))
209 if (l1pte_section_p(pd)) {
210 /* 1MB section mapping. */
211 *pa = (pd & ARM_L1_S_ADDR_MASK) + (va & ARM_L1_S_OFFSET);
212 return (_kvm_pa2off(kd, *pa, pa, ARM_L1_S_SIZE));
214 pte_pa = (pd & ARM_L1_C_ADDR_MASK) + l2pte_index(va) * sizeof(pte);
215 _kvm_pa2off(kd, pte_pa, &pte_off, ARM_L1_S_SIZE);
216 if (pread(kd->pmfd, &pte, sizeof(pte), pte_off) != sizeof(pte)) {
217 _kvm_syserr(kd, kd->program, "_arm_kvatop: pread");
220 pte = _kvm32toh(kd, pte);
221 if (!l2pte_valid(pte)) {
224 if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_L) {
225 *pa = (pte & ARM_L2_L_FRAME) | (va & ARM_L2_L_OFFSET);
226 return (_kvm_pa2off(kd, *pa, pa, ARM_L2_L_SIZE));
228 *pa = (pte & ARM_L2_S_FRAME) | (va & ARM_L2_S_OFFSET);
229 return (_kvm_pa2off(kd, *pa, pa, ARM_PAGE_SIZE));
231 _kvm_err(kd, 0, "Invalid address (%jx)", (uintmax_t)va);
236 * Machine-dependent initialization for ALL open kvm descriptors,
237 * not just those for a kernel crash dump. Some architectures
238 * have to deal with these NOT being constants! (i.e. m68k)
242 _kvm_mdopen(kvm_t *kd)
245 kd->usrstack = USRSTACK;
246 kd->min_uva = VM_MIN_ADDRESS;
247 kd->max_uva = VM_MAXUSER_ADDRESS;
255 _arm_native(kvm_t *kd)
257 _arm_native(kvm_t *kd __unused)
262 #if _BYTE_ORDER == _LITTLE_ENDIAN
263 return (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2LSB);
265 return (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2MSB);
272 static struct kvm_arch kvm_arm = {
273 .ka_probe = _arm_probe,
274 .ka_initvtop = _arm_initvtop,
275 .ka_freevtop = _arm_freevtop,
276 .ka_kvatop = _arm_kvatop,
277 .ka_native = _arm_native,