kvm_aarch64.c revision 1.1.4.2 1 1.1.4.2 tls /* $NetBSD: kvm_aarch64.c,v 1.1.4.2 2014/08/20 00:02:17 tls Exp $ */
2 1.1.4.2 tls
3 1.1.4.2 tls /*-
4 1.1.4.2 tls * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 1.1.4.2 tls * All rights reserved.
6 1.1.4.2 tls *
7 1.1.4.2 tls * This code is derived from software contributed to The NetBSD Foundation
8 1.1.4.2 tls * by Matt Thomas of 3am Software Foundry.
9 1.1.4.2 tls *
10 1.1.4.2 tls * Redistribution and use in source and binary forms, with or without
11 1.1.4.2 tls * modification, are permitted provided that the following conditions
12 1.1.4.2 tls * are met:
13 1.1.4.2 tls * 1. Redistributions of source code must retain the above copyright
14 1.1.4.2 tls * notice, this list of conditions and the following disclaimer.
15 1.1.4.2 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.4.2 tls * notice, this list of conditions and the following disclaimer in the
17 1.1.4.2 tls * documentation and/or other materials provided with the distribution.
18 1.1.4.2 tls *
19 1.1.4.2 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.4.2 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.4.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.4.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.4.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.4.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.4.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.4.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.4.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.4.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.4.2 tls * POSSIBILITY OF SUCH DAMAGE.
30 1.1.4.2 tls */
31 1.1.4.2 tls
32 1.1.4.2 tls #include <sys/param.h>
33 1.1.4.2 tls #include <sys/user.h>
34 1.1.4.2 tls #include <sys/proc.h>
35 1.1.4.2 tls #include <sys/stat.h>
36 1.1.4.2 tls #include <sys/kcore.h>
37 1.1.4.2 tls #include <sys/types.h>
38 1.1.4.2 tls #include <unistd.h>
39 1.1.4.2 tls #include <nlist.h>
40 1.1.4.2 tls #include <kvm.h>
41 1.1.4.2 tls
42 1.1.4.2 tls #include <machine/kcore.h>
43 1.1.4.2 tls #include <machine/pte.h>
44 1.1.4.2 tls #include <machine/vmparam.h>
45 1.1.4.2 tls
46 1.1.4.2 tls #include <limits.h>
47 1.1.4.2 tls #include <db.h>
48 1.1.4.2 tls #include <stdlib.h>
49 1.1.4.2 tls
50 1.1.4.2 tls #include "kvm_private.h"
51 1.1.4.2 tls
52 1.1.4.2 tls __RCSID("$NetBSD: kvm_aarch64.c,v 1.1.4.2 2014/08/20 00:02:17 tls Exp $");
53 1.1.4.2 tls
54 1.1.4.2 tls /*ARGSUSED*/
55 1.1.4.2 tls void
56 1.1.4.2 tls _kvm_freevtop(kvm_t *kd)
57 1.1.4.2 tls {
58 1.1.4.2 tls return;
59 1.1.4.2 tls }
60 1.1.4.2 tls
61 1.1.4.2 tls /*ARGSUSED*/
62 1.1.4.2 tls int
63 1.1.4.2 tls _kvm_initvtop(kvm_t *kd)
64 1.1.4.2 tls {
65 1.1.4.2 tls return (0);
66 1.1.4.2 tls }
67 1.1.4.2 tls
68 1.1.4.2 tls int
69 1.1.4.2 tls _kvm_kvatop(kvm_t *kd, vaddr_t va, paddr_t *pa)
70 1.1.4.2 tls {
71 1.1.4.2 tls if (ISALIVE(kd)) {
72 1.1.4.2 tls _kvm_err(kd, 0, "vatop called in live kernel!");
73 1.1.4.2 tls return(0);
74 1.1.4.2 tls }
75 1.1.4.2 tls
76 1.1.4.2 tls if ((va & AARCH64_KSEG_MASK) != AARCH64_KSEG_START) {
77 1.1.4.2 tls /*
78 1.1.4.2 tls * Bogus address (not in KV space): punt.
79 1.1.4.2 tls */
80 1.1.4.2 tls _kvm_err(kd, 0, "invalid kernel virtual address");
81 1.1.4.2 tls lose:
82 1.1.4.2 tls *pa = -1;
83 1.1.4.2 tls return 0;
84 1.1.4.2 tls }
85 1.1.4.2 tls
86 1.1.4.2 tls const cpu_kcore_hdr_t * const cpu_kh = kd->cpu_data;
87 1.1.4.2 tls const u_int tg1 =__SHIFTOUT(cpu_kh->kh_tcr1, TCR_TG1);
88 1.1.4.2 tls const u_int t1siz = __SHIFTOUT(cpu_kh->kh_tcr1, TCR_T1SZ);
89 1.1.4.2 tls
90 1.1.4.2 tls /*
91 1.1.4.2 tls * Real kernel virtual address: do the translation.
92 1.1.4.2 tls */
93 1.1.4.2 tls
94 1.1.4.2 tls u_int va_bits;
95 1.1.4.2 tls u_int page_shift;
96 1.1.4.2 tls
97 1.1.4.2 tls switch (tg1) {
98 1.1.4.2 tls case TCR_TG_4KB:
99 1.1.4.2 tls va_bits = t1siz + 36;
100 1.1.4.2 tls page_shift = 12;
101 1.1.4.2 tls break;
102 1.1.4.2 tls case TCR_TG_16KB:
103 1.1.4.2 tls va_bits = 48;
104 1.1.4.2 tls page_shift = 14;
105 1.1.4.2 tls break;
106 1.1.4.2 tls case TCR_TG_64KB:
107 1.1.4.2 tls va_bits = t1siz + 38;
108 1.1.4.2 tls page_shift = 16;
109 1.1.4.2 tls break;
110 1.1.4.2 tls default:
111 1.1.4.2 tls goto lose;
112 1.1.4.2 tls }
113 1.1.4.2 tls
114 1.1.4.2 tls const size_t page_size = 1 << page_shift;
115 1.1.4.2 tls const uint64_t page_mask = (page_size - 1);
116 1.1.4.2 tls const uint64_t page_addr = __BITS(47, 0) & ~page_mask;
117 1.1.4.2 tls const uint64_t pte_mask = page_mask >> 3;
118 1.1.4.2 tls const u_int pte_shift = page_shift - 3;
119 1.1.4.2 tls
120 1.1.4.2 tls /* how many level of page tables do we have? */
121 1.1.4.2 tls u_int level = (48 + page_shift - 1) / page_shift;
122 1.1.4.2 tls
123 1.1.4.2 tls /* restrict va to the valid VA bits */
124 1.1.4.2 tls va &= (1LL << va_bits) - 1;
125 1.1.4.2 tls
126 1.1.4.2 tls u_int addr_shift = page_shift + (level - 1) * pte_shift;
127 1.1.4.2 tls
128 1.1.4.2 tls /* clear out the unused low bits of the table address */
129 1.1.4.2 tls paddr_t pte_addr = (cpu_kh->kh_ttbr1 & TTBR_BADDR);
130 1.1.4.2 tls pte_addr &= ~((8L << (va_bits - addr_shift)) - 1);
131 1.1.4.2 tls
132 1.1.4.2 tls for (;;) {
133 1.1.4.2 tls pt_entry_t pte;
134 1.1.4.2 tls
135 1.1.4.2 tls /* now index into the pte table */
136 1.1.4.2 tls pte_addr += 8 * ((va >> addr_shift) & pte_mask);
137 1.1.4.2 tls
138 1.1.4.2 tls /* Find and read the PTE. */
139 1.1.4.2 tls if (_kvm_pread(kd, kd->pmfd, &pte, sizeof(pte),
140 1.1.4.2 tls _kvm_pa2off(kd, pte_addr)) != sizeof(pte)) {
141 1.1.4.2 tls _kvm_syserr(kd, 0, "could not read pte");
142 1.1.4.2 tls goto lose;
143 1.1.4.2 tls }
144 1.1.4.2 tls
145 1.1.4.2 tls /* Find and read the L2 PTE. */
146 1.1.4.2 tls if ((pte & LX_VALID) == 0) {
147 1.1.4.2 tls _kvm_err(kd, 0, "invalid translation (invalid pte)");
148 1.1.4.2 tls goto lose;
149 1.1.4.2 tls }
150 1.1.4.2 tls
151 1.1.4.2 tls if ((pte & LX_TYPE) == LX_TYPE_BLK) {
152 1.1.4.2 tls const paddr_t blk_mask = ((1L << addr_shift) - 1);
153 1.1.4.2 tls
154 1.1.4.2 tls *pa = (pte & page_addr & ~blk_mask) | (va & blk_mask);
155 1.1.4.2 tls return 0;
156 1.1.4.2 tls }
157 1.1.4.2 tls
158 1.1.4.2 tls if (level == page_shift) {
159 1.1.4.2 tls *pa = (pte & page_addr) | (va & page_mask);
160 1.1.4.2 tls return 0;
161 1.1.4.2 tls }
162 1.1.4.2 tls
163 1.1.4.2 tls /*
164 1.1.4.2 tls * Read next level of page table
165 1.1.4.2 tls */
166 1.1.4.2 tls
167 1.1.4.2 tls pte_addr = pte & page_addr;
168 1.1.4.2 tls addr_shift -= pte_shift;
169 1.1.4.2 tls }
170 1.1.4.2 tls }
171 1.1.4.2 tls
172 1.1.4.2 tls /*
173 1.1.4.2 tls * Translate a physical address to a file-offset in the crash dump.
174 1.1.4.2 tls */
175 1.1.4.2 tls off_t
176 1.1.4.2 tls _kvm_pa2off(kvm_t *kd, paddr_t pa)
177 1.1.4.2 tls {
178 1.1.4.2 tls const cpu_kcore_hdr_t * const cpu_kh = kd->cpu_data;
179 1.1.4.2 tls off_t off = 0;
180 1.1.4.2 tls
181 1.1.4.2 tls for (const phys_ram_seg_t *ramsegs = cpu_kh->kh_ramsegs;
182 1.1.4.2 tls ramsegs->size != 0; ramsegs++) {
183 1.1.4.2 tls if (pa >= ramsegs->start
184 1.1.4.2 tls && pa < ramsegs->start + ramsegs->size) {
185 1.1.4.2 tls off += pa - ramsegs->start;
186 1.1.4.2 tls break;
187 1.1.4.2 tls }
188 1.1.4.2 tls off += ramsegs->size;
189 1.1.4.2 tls }
190 1.1.4.2 tls
191 1.1.4.2 tls return kd->dump_off + off;
192 1.1.4.2 tls }
193 1.1.4.2 tls
194 1.1.4.2 tls /*
195 1.1.4.2 tls * Machine-dependent initialization for ALL open kvm descriptors,
196 1.1.4.2 tls * not just those for a kernel crash dump. Some architectures
197 1.1.4.2 tls * have to deal with these NOT being constants! (i.e. m68k)
198 1.1.4.2 tls */
199 1.1.4.2 tls int
200 1.1.4.2 tls _kvm_mdopen(kvm_t *kd)
201 1.1.4.2 tls {
202 1.1.4.2 tls
203 1.1.4.2 tls kd->usrstack = USRSTACK;
204 1.1.4.2 tls kd->min_uva = VM_MIN_ADDRESS;
205 1.1.4.2 tls kd->max_uva = VM_MAXUSER_ADDRESS;
206 1.1.4.2 tls
207 1.1.4.2 tls return (0);
208 1.1.4.2 tls }
209