xref: /freebsd/lib/libkvm/kvm_minidump_arm.c (revision fed1ca4b719c56c930f2259d80663cd34be812bb)
1 /*-
2  * Copyright (c) 2008 Semihalf, Grzegorz Bernacki
3  * Copyright (c) 2006 Peter Wemm
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * From: FreeBSD: src/lib/libkvm/kvm_minidump_i386.c,v 1.2 2006/06/05 08:51:14
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /*
33  * ARM machine dependent routines for kvm and minidumps.
34  */
35 
36 #include <sys/endian.h>
37 #include <sys/param.h>
38 #include <kvm.h>
39 #include <limits.h>
40 #include <stdint.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #include "../../sys/arm/include/minidump.h"
46 
47 #include "kvm_private.h"
48 #include "kvm_arm.h"
49 
50 #define	arm_round_page(x)	roundup2((kvaddr_t)(x), ARM_PAGE_SIZE)
51 
52 struct vmstate {
53 	struct		minidumphdr hdr;
54 	struct		hpt hpt;
55 	void		*ptemap;
56 	unsigned char	ei_data;
57 };
58 
59 static int
60 _arm_minidump_probe(kvm_t *kd)
61 {
62 
63 	return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_ARM) &&
64 	    _kvm_is_minidump(kd));
65 }
66 
67 static void
68 _arm_minidump_freevtop(kvm_t *kd)
69 {
70 	struct vmstate *vm = kd->vmst;
71 
72 	_kvm_hpt_free(&vm->hpt);
73 	if (vm->ptemap)
74 		free(vm->ptemap);
75 	free(vm);
76 	kd->vmst = NULL;
77 }
78 
79 static int
80 _arm_minidump_initvtop(kvm_t *kd)
81 {
82 	struct vmstate *vmst;
83 	uint32_t *bitmap;
84 	off_t off;
85 
86 	vmst = _kvm_malloc(kd, sizeof(*vmst));
87 	if (vmst == NULL) {
88 		_kvm_err(kd, kd->program, "cannot allocate vm");
89 		return (-1);
90 	}
91 
92 	kd->vmst = vmst;
93 
94 	if (pread(kd->pmfd, &vmst->hdr,
95 	    sizeof(vmst->hdr), 0) != sizeof(vmst->hdr)) {
96 		_kvm_err(kd, kd->program, "cannot read dump header");
97 		return (-1);
98 	}
99 
100 	if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic,
101 	    sizeof(vmst->hdr.magic)) != 0) {
102 		_kvm_err(kd, kd->program, "not a minidump for this platform");
103 		return (-1);
104 	}
105 	vmst->hdr.version = _kvm32toh(kd, vmst->hdr.version);
106 	if (vmst->hdr.version != MINIDUMP_VERSION) {
107 		_kvm_err(kd, kd->program, "wrong minidump version. "
108 		    "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version);
109 		return (-1);
110 	}
111 	vmst->hdr.msgbufsize = _kvm32toh(kd, vmst->hdr.msgbufsize);
112 	vmst->hdr.bitmapsize = _kvm32toh(kd, vmst->hdr.bitmapsize);
113 	vmst->hdr.ptesize = _kvm32toh(kd, vmst->hdr.ptesize);
114 	vmst->hdr.kernbase = _kvm32toh(kd, vmst->hdr.kernbase);
115 	vmst->hdr.arch = _kvm32toh(kd, vmst->hdr.arch);
116 	vmst->hdr.mmuformat = _kvm32toh(kd, vmst->hdr.mmuformat);
117 	if (vmst->hdr.mmuformat == MINIDUMP_MMU_FORMAT_UNKNOWN) {
118 		/* This is a safe default as 1K pages are not used. */
119 		vmst->hdr.mmuformat = MINIDUMP_MMU_FORMAT_V6;
120 	}
121 
122 	/* Skip header and msgbuf */
123 	off = ARM_PAGE_SIZE + arm_round_page(vmst->hdr.msgbufsize);
124 
125 	bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize);
126 	if (bitmap == NULL) {
127 		_kvm_err(kd, kd->program, "cannot allocate %d bytes for "
128 		    "bitmap", vmst->hdr.bitmapsize);
129 		return (-1);
130 	}
131 
132 	if (pread(kd->pmfd, bitmap, vmst->hdr.bitmapsize, off) !=
133 	    (ssize_t)vmst->hdr.bitmapsize) {
134 		_kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap",
135 		    vmst->hdr.bitmapsize);
136 		free(bitmap);
137 		return (-1);
138 	}
139 	off += arm_round_page(vmst->hdr.bitmapsize);
140 
141 	vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize);
142 	if (vmst->ptemap == NULL) {
143 		_kvm_err(kd, kd->program, "cannot allocate %d bytes for "
144 		    "ptemap", vmst->hdr.ptesize);
145 		free(bitmap);
146 		return (-1);
147 	}
148 
149 	if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) !=
150 	    (ssize_t)vmst->hdr.ptesize) {
151 		_kvm_err(kd, kd->program, "cannot read %d bytes for ptemap",
152 		    vmst->hdr.ptesize);
153 		free(bitmap);
154 		return (-1);
155 	}
156 
157 	off += vmst->hdr.ptesize;
158 
159 	/* Build physical address hash table for sparse pages */
160 	_kvm_hpt_init(kd, &vmst->hpt, bitmap, vmst->hdr.bitmapsize, off,
161 	    ARM_PAGE_SIZE, sizeof(*bitmap));
162 	free(bitmap);
163 
164 	return (0);
165 }
166 
167 static int
168 _arm_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
169 {
170 	struct vmstate *vm;
171 	arm_pt_entry_t pte;
172 	arm_physaddr_t offset, a;
173 	kvaddr_t pteindex;
174 	off_t ofs;
175 	arm_pt_entry_t *ptemap;
176 
177 	if (ISALIVE(kd)) {
178 		_kvm_err(kd, 0, "_arm_minidump_kvatop called in live kernel!");
179 		return (0);
180 	}
181 
182 	vm = kd->vmst;
183 	ptemap = vm->ptemap;
184 
185 	if (va >= vm->hdr.kernbase) {
186 		pteindex = (va - vm->hdr.kernbase) >> ARM_PAGE_SHIFT;
187 		pte = _kvm32toh(kd, ptemap[pteindex]);
188 		if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_INV) {
189 			_kvm_err(kd, kd->program,
190 			    "_arm_minidump_kvatop: pte not valid");
191 			goto invalid;
192 		}
193 		if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_L) {
194 			/* 64K page -> convert to be like 4K page */
195 			offset = va & ARM_L2_S_OFFSET;
196 			a = (pte & ARM_L2_L_FRAME) +
197 			    (va & ARM_L2_L_OFFSET & ARM_L2_S_FRAME);
198 		} else {
199 			if (kd->vmst->hdr.mmuformat == MINIDUMP_MMU_FORMAT_V4 &&
200 			    (pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_T) {
201 				_kvm_err(kd, kd->program,
202 				    "_arm_minidump_kvatop: pte not supported");
203 				goto invalid;
204 			}
205 			/* 4K page */
206 			offset = va & ARM_L2_S_OFFSET;
207 			a = pte & ARM_L2_S_FRAME;
208 		}
209 
210 		ofs = _kvm_hpt_find(&vm->hpt, a);
211 		if (ofs == -1) {
212 			_kvm_err(kd, kd->program, "_arm_minidump_kvatop: "
213 			    "physical address 0x%jx not in minidump",
214 			    (uintmax_t)a);
215 			goto invalid;
216 		}
217 
218 		*pa = ofs + offset;
219 		return (ARM_PAGE_SIZE - offset);
220 	} else
221 		_kvm_err(kd, kd->program, "_arm_minidump_kvatop: virtual "
222 		    "address 0x%jx not minidumped", (uintmax_t)va);
223 
224 invalid:
225 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
226 	return (0);
227 }
228 
229 struct kvm_arch kvm_arm_minidump = {
230 	.ka_probe = _arm_minidump_probe,
231 	.ka_initvtop = _arm_minidump_initvtop,
232 	.ka_freevtop = _arm_minidump_freevtop,
233 	.ka_kvatop = _arm_minidump_kvatop,
234 	.ka_native = _arm_native,
235 };
236 
237 KVM_ARCH(kvm_arm_minidump);
238