xref: /freebsd/lib/libkvm/kvm_powerpc64.c (revision f9bc005b8ef3b507a5ecbc2d7fe4411ef70eff38)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2008, Juniper Networks, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the author nor the names of any co-contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/kerneldump.h>
34 #include <sys/mman.h>
35 
36 #include <elf.h>
37 #include <kvm.h>
38 #include <limits.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include "kvm_private.h"
43 
44 struct vmstate {
45 	void		*map;
46 	size_t		mapsz;
47 	size_t		dmphdrsz;
48 	Elf64_Ehdr	*eh;
49 	Elf64_Phdr	*ph;
50 };
51 
52 static int
53 valid_elf_header(kvm_t *kd, Elf64_Ehdr *eh)
54 {
55 
56 	if (!IS_ELF(*eh))
57 		return (0);
58 	if (eh->e_ident[EI_CLASS] != ELFCLASS64)
59 		return (0);
60 	if (eh->e_ident[EI_DATA] != ELFDATA2MSB &&
61 	    eh->e_ident[EI_DATA] != ELFDATA2LSB)
62 		return (0);
63 	if (eh->e_ident[EI_VERSION] != EV_CURRENT)
64 		return (0);
65 	if (eh->e_ident[EI_OSABI] != ELFOSABI_STANDALONE)
66 		return (0);
67 	if (_kvm16toh(kd, eh->e_type) != ET_CORE)
68 		return (0);
69 	if (_kvm16toh(kd, eh->e_machine) != EM_PPC64)
70 		return (0);
71 	/* Can't think of anything else to check... */
72 	return (1);
73 }
74 
75 static size_t
76 dump_header_size(struct kerneldumpheader *dh)
77 {
78 
79 	if (strcmp(dh->magic, KERNELDUMPMAGIC) != 0)
80 		return (0);
81 	if (strcmp(dh->architecture, "powerpc64") != 0 &&
82 	    strcmp(dh->architecture, "powerpc64le") != 0)
83 		return (0);
84 	/* That should do it... */
85 	return (sizeof(*dh));
86 }
87 
88 /*
89  * Map the ELF headers into the process' address space. We do this in two
90  * steps: first the ELF header itself and using that information the whole
91  * set of headers.
92  */
93 static int
94 powerpc_maphdrs(kvm_t *kd)
95 {
96 	struct vmstate *vm;
97 	size_t mapsz;
98 
99 	vm = kd->vmst;
100 
101 	vm->mapsz = sizeof(*vm->eh) + sizeof(struct kerneldumpheader);
102 	vm->map = mmap(NULL, vm->mapsz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
103 	if (vm->map == MAP_FAILED) {
104 		_kvm_err(kd, kd->program, "cannot map corefile");
105 		return (-1);
106 	}
107 	vm->dmphdrsz = 0;
108 	vm->eh = vm->map;
109 	if (!valid_elf_header(kd, vm->eh)) {
110 		/*
111 		 * Hmmm, no ELF header. Maybe we still have a dump header.
112 		 * This is normal when the core file wasn't created by
113 		 * savecore(8), but instead was dumped over TFTP. We can
114 		 * easily skip the dump header...
115 		 */
116 		vm->dmphdrsz = dump_header_size(vm->map);
117 		if (vm->dmphdrsz == 0)
118 			goto inval;
119 		vm->eh = (void *)((uintptr_t)vm->map + vm->dmphdrsz);
120 		if (!valid_elf_header(kd, vm->eh))
121 			goto inval;
122 	}
123 	mapsz = _kvm16toh(kd, vm->eh->e_phentsize) *
124 	    _kvm16toh(kd, vm->eh->e_phnum) + _kvm64toh(kd, vm->eh->e_phoff);
125 	munmap(vm->map, vm->mapsz);
126 
127 	/* Map all headers. */
128 	vm->mapsz = vm->dmphdrsz + mapsz;
129 	vm->map = mmap(NULL, vm->mapsz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
130 	if (vm->map == MAP_FAILED) {
131 		_kvm_err(kd, kd->program, "cannot map corefile headers");
132 		return (-1);
133 	}
134 	vm->eh = (void *)((uintptr_t)vm->map + vm->dmphdrsz);
135 	vm->ph = (void *)((uintptr_t)vm->eh +
136 	    (uintptr_t)_kvm64toh(kd, vm->eh->e_phoff));
137 	return (0);
138 
139  inval:
140 	_kvm_err(kd, kd->program, "invalid corefile");
141 	return (-1);
142 }
143 
144 /*
145  * Determine the offset within the corefile corresponding the virtual
146  * address. Return the number of contiguous bytes in the corefile or
147  * 0 when the virtual address is invalid.
148  */
149 static size_t
150 powerpc64_va2off(kvm_t *kd, kvaddr_t va, off_t *ofs)
151 {
152 	struct vmstate *vm = kd->vmst;
153 	Elf64_Phdr *ph;
154 	int nph;
155 
156 	ph = vm->ph;
157 	nph = _kvm16toh(kd, vm->eh->e_phnum);
158 	while (nph && (va < _kvm64toh(kd, ph->p_vaddr) ||
159 	    va >= _kvm64toh(kd, ph->p_vaddr) + _kvm64toh(kd, ph->p_memsz))) {
160 		nph--;
161 		ph = (void *)((uintptr_t)ph +
162 		    _kvm16toh(kd, vm->eh->e_phentsize));
163 	}
164 	if (nph == 0)
165 		return (0);
166 
167 	/* Segment found. Return file offset and range. */
168 	*ofs = vm->dmphdrsz + _kvm64toh(kd, ph->p_offset) +
169 	    (va - _kvm64toh(kd, ph->p_vaddr));
170 	return (_kvm64toh(kd, ph->p_memsz) -
171 	    (va - _kvm64toh(kd, ph->p_vaddr)));
172 }
173 
174 static void
175 _powerpc64_freevtop(kvm_t *kd)
176 {
177 	struct vmstate *vm = kd->vmst;
178 
179 	if (vm->eh != MAP_FAILED)
180 		munmap(vm->eh, vm->mapsz);
181 	free(vm);
182 	kd->vmst = NULL;
183 }
184 
185 static int
186 _powerpc64_probe(kvm_t *kd)
187 {
188 
189 	return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_PPC64) &&
190 	    kd->nlehdr.e_ident[EI_DATA] == ELFDATA2MSB &&
191 	    !_kvm_is_minidump(kd));
192 }
193 
194 static int
195 _powerpc64le_probe(kvm_t *kd)
196 {
197 
198 	return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_PPC64) &&
199 	    kd->nlehdr.e_ident[EI_DATA] == ELFDATA2LSB &&
200 	    !_kvm_is_minidump(kd));
201 }
202 
203 static int
204 _powerpc64_initvtop(kvm_t *kd)
205 {
206 
207 	kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst));
208 	if (kd->vmst == NULL)
209 		return (-1);
210 
211 	if (powerpc_maphdrs(kd) == -1)
212 		return (-1);
213 
214 	return (0);
215 }
216 
217 static int
218 _powerpc64_kvatop(kvm_t *kd, kvaddr_t va, off_t *ofs)
219 {
220 	struct vmstate *vm;
221 
222 	vm = kd->vmst;
223 	if (_kvm64toh(kd, vm->ph->p_paddr) == 0xffffffffffffffff)
224 		return ((int)powerpc64_va2off(kd, va, ofs));
225 
226 	_kvm_err(kd, kd->program, "Raw corefile not supported");
227 	return (0);
228 }
229 
230 static int
231 _powerpc64_native(kvm_t *kd __unused)
232 {
233 
234 #if defined(__powerpc64__) && BYTE_ORDER == BIG_ENDIAN
235 	return (1);
236 #else
237 	return (0);
238 #endif
239 }
240 
241 static int
242 _powerpc64le_native(kvm_t *kd __unused)
243 {
244 
245 #if defined(__powerpc64__) && BYTE_ORDER == LITTLE_ENDIAN
246 	return (1);
247 #else
248 	return (0);
249 #endif
250 }
251 
252 static struct kvm_arch kvm_powerpc64 = {
253 	.ka_probe = _powerpc64_probe,
254 	.ka_initvtop = _powerpc64_initvtop,
255 	.ka_freevtop = _powerpc64_freevtop,
256 	.ka_kvatop = _powerpc64_kvatop,
257 	.ka_native = _powerpc64_native,
258 };
259 
260 static struct kvm_arch kvm_powerpc64le = {
261 	.ka_probe = _powerpc64le_probe,
262 	.ka_initvtop = _powerpc64_initvtop,
263 	.ka_freevtop = _powerpc64_freevtop,
264 	.ka_kvatop = _powerpc64_kvatop,
265 	.ka_native = _powerpc64le_native,
266 };
267 
268 KVM_ARCH(kvm_powerpc64);
269 KVM_ARCH(kvm_powerpc64le);
270