1 /* $OpenBSD: kvm_arm.c,v 1.12 2021/12/01 21:45:19 deraadt Exp $ */
2 /*
3 * Copyright (c) 2006 Miodrag Vallat.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice, this permission notice, and the disclaimer below
8 * appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 /*-
19 * Copyright (C) 1996 Wolfgang Solfrank.
20 * Copyright (C) 1996 TooLs GmbH.
21 * All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * This product includes software developed by TooLs GmbH.
34 * 4. The name of TooLs GmbH may not be used to endorse or promote products
35 * derived from this software without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
38 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
40 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
42 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
43 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
45 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
46 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 */
48
49 /*
50 * ARM machine dependent routines for kvm.
51 */
52
53 #include <sys/types.h>
54 #include <sys/kcore.h>
55
56 #include <unistd.h>
57 #include <stdlib.h>
58 #include <nlist.h>
59 #include <kvm.h>
60
61 #include <db.h>
62
63 #include "kvm_private.h"
64
65 #include <arm/kcore.h>
66
67 void
_kvm_freevtop(kvm_t * kd)68 _kvm_freevtop(kvm_t *kd)
69 {
70 free(kd->vmst);
71 kd->vmst = NULL;
72 }
73
74 int
_kvm_initvtop(kvm_t * kd)75 _kvm_initvtop(kvm_t *kd)
76 {
77 return (0);
78 }
79
80 /*
81 * Translate a kernel virtual address to a physical address by walking
82 * the kernels page table.
83 */
84
85 int
_kvm_kvatop(kvm_t * kd,u_long va,paddr_t * pa)86 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
87 {
88 cpu_kcore_hdr_t *cpup = kd->cpu_data;
89
90 if (ISALIVE(kd)) {
91 _kvm_err(kd, 0, "vatop called in live kernel!");
92 return (0);
93 }
94
95 /*
96 * This relies upon the kernel text and data being contiguous
97 * in the first memory segment.
98 * Other virtual addresses are not reachable yet.
99 */
100
101 if (va >= cpup->kernelbase + cpup->kerneloffs &&
102 va < cpup->kernelbase + cpup->kerneloffs + cpup->staticsize) {
103 *pa = (va - cpup->kernelbase) +
104 (paddr_t)cpup->ram_segs[0].start;
105 return (int)(kd->nbpg - (va & (kd->nbpg - 1)));
106 }
107
108 _kvm_err(kd, 0, "kvm_vatop: va %lx unreachable", va);
109 return (0);
110 }
111
112 off_t
_kvm_pa2off(kvm_t * kd,paddr_t pa)113 _kvm_pa2off(kvm_t *kd, paddr_t pa)
114 {
115 cpu_kcore_hdr_t *cpup = kd->cpu_data;
116 phys_ram_seg_t *mp = cpup->ram_segs;
117 off_t off = 0;
118 int block;
119
120 for (block = 0; block < NPHYS_RAM_SEGS; block++, mp++) {
121 if (pa >= mp->start && pa < mp->start + mp->size)
122 return (kd->dump_off + off +
123 (off_t)(pa - (paddr_t)mp->start));
124 off += (off_t)mp->size;
125 }
126
127 _kvm_err(kd, 0, "not a physical address: %lx", pa);
128 return (-1);
129 }
130