xref: /freebsd/lib/libkvm/kvm_powerpc64.c (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
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/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/endian.h>
36 #include <sys/kerneldump.h>
37 #include <sys/mman.h>
38 
39 #include <elf.h>
40 #include <kvm.h>
41 #include <limits.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include "kvm_private.h"
46 
47 struct vmstate {
48 	void		*map;
49 	size_t		mapsz;
50 	size_t		dmphdrsz;
51 	Elf64_Ehdr	*eh;
52 	Elf64_Phdr	*ph;
53 };
54 
55 static int
56 valid_elf_header(Elf64_Ehdr *eh)
57 {
58 
59 	if (!IS_ELF(*eh))
60 		return (0);
61 	if (eh->e_ident[EI_CLASS] != ELFCLASS64)
62 		return (0);
63 	if (eh->e_ident[EI_DATA] != ELFDATA2MSB)
64 		return (0);
65 	if (eh->e_ident[EI_VERSION] != EV_CURRENT)
66 		return (0);
67 	if (eh->e_ident[EI_OSABI] != ELFOSABI_STANDALONE)
68 		return (0);
69 	if (be16toh(eh->e_type) != ET_CORE)
70 		return (0);
71 	if (be16toh(eh->e_machine) != EM_PPC64)
72 		return (0);
73 	/* Can't think of anything else to check... */
74 	return (1);
75 }
76 
77 static size_t
78 dump_header_size(struct kerneldumpheader *dh)
79 {
80 
81 	if (strcmp(dh->magic, KERNELDUMPMAGIC) != 0)
82 		return (0);
83 	if (strcmp(dh->architecture, "powerpc64") != 0)
84 		return (0);
85 	/* That should do it... */
86 	return (sizeof(*dh));
87 }
88 
89 /*
90  * Map the ELF headers into the process' address space. We do this in two
91  * steps: first the ELF header itself and using that information the whole
92  * set of headers.
93  */
94 static int
95 powerpc_maphdrs(kvm_t *kd)
96 {
97 	struct vmstate *vm;
98 	size_t mapsz;
99 
100 	vm = kd->vmst;
101 
102 	vm->mapsz = sizeof(*vm->eh) + sizeof(struct kerneldumpheader);
103 	vm->map = mmap(NULL, vm->mapsz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
104 	if (vm->map == MAP_FAILED) {
105 		_kvm_err(kd, kd->program, "cannot map corefile");
106 		return (-1);
107 	}
108 	vm->dmphdrsz = 0;
109 	vm->eh = vm->map;
110 	if (!valid_elf_header(vm->eh)) {
111 		/*
112 		 * Hmmm, no ELF header. Maybe we still have a dump header.
113 		 * This is normal when the core file wasn't created by
114 		 * savecore(8), but instead was dumped over TFTP. We can
115 		 * easily skip the dump header...
116 		 */
117 		vm->dmphdrsz = dump_header_size(vm->map);
118 		if (vm->dmphdrsz == 0)
119 			goto inval;
120 		vm->eh = (void *)((uintptr_t)vm->map + vm->dmphdrsz);
121 		if (!valid_elf_header(vm->eh))
122 			goto inval;
123 	}
124 	mapsz = be16toh(vm->eh->e_phentsize) * be16toh(vm->eh->e_phnum) +
125 	    be64toh(vm->eh->e_phoff);
126 	munmap(vm->map, vm->mapsz);
127 
128 	/* Map all headers. */
129 	vm->mapsz = vm->dmphdrsz + mapsz;
130 	vm->map = mmap(NULL, vm->mapsz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
131 	if (vm->map == MAP_FAILED) {
132 		_kvm_err(kd, kd->program, "cannot map corefile headers");
133 		return (-1);
134 	}
135 	vm->eh = (void *)((uintptr_t)vm->map + vm->dmphdrsz);
136 	vm->ph = (void *)((uintptr_t)vm->eh +
137 	    (uintptr_t)be64toh(vm->eh->e_phoff));
138 	return (0);
139 
140  inval:
141 	_kvm_err(kd, kd->program, "invalid corefile");
142 	return (-1);
143 }
144 
145 /*
146  * Determine the offset within the corefile corresponding the virtual
147  * address. Return the number of contiguous bytes in the corefile or
148  * 0 when the virtual address is invalid.
149  */
150 static size_t
151 powerpc64_va2off(kvm_t *kd, kvaddr_t va, off_t *ofs)
152 {
153 	struct vmstate *vm = kd->vmst;
154 	Elf64_Phdr *ph;
155 	int nph;
156 
157 	ph = vm->ph;
158 	nph = be16toh(vm->eh->e_phnum);
159 	while (nph && (va < be64toh(ph->p_vaddr) ||
160 	    va >= be64toh(ph->p_vaddr) + be64toh(ph->p_memsz))) {
161 		nph--;
162 		ph = (void *)((uintptr_t)ph + be16toh(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 + be64toh(ph->p_offset) +
169 	    (va - be64toh(ph->p_vaddr));
170 	return (be64toh(ph->p_memsz) - (va - be64toh(ph->p_vaddr)));
171 }
172 
173 static void
174 _powerpc64_freevtop(kvm_t *kd)
175 {
176 	struct vmstate *vm = kd->vmst;
177 
178 	if (vm->eh != MAP_FAILED)
179 		munmap(vm->eh, vm->mapsz);
180 	free(vm);
181 	kd->vmst = NULL;
182 }
183 
184 static int
185 _powerpc64_probe(kvm_t *kd)
186 {
187 
188 	return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_PPC64) &&
189 	    kd->nlehdr.e_ident[EI_DATA] == ELFDATA2MSB);
190 }
191 
192 static int
193 _powerpc64_initvtop(kvm_t *kd)
194 {
195 
196 	kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst));
197 	if (kd->vmst == NULL)
198 		return (-1);
199 
200 	if (powerpc_maphdrs(kd) == -1)
201 		return (-1);
202 
203 	return (0);
204 }
205 
206 static int
207 _powerpc64_kvatop(kvm_t *kd, kvaddr_t va, off_t *ofs)
208 {
209 	struct vmstate *vm;
210 
211 	vm = kd->vmst;
212 	if (be64toh(vm->ph->p_paddr) == 0xffffffffffffffff)
213 		return ((int)powerpc64_va2off(kd, va, ofs));
214 
215 	_kvm_err(kd, kd->program, "Raw corefile not supported");
216 	return (0);
217 }
218 
219 static int
220 _powerpc64_native(kvm_t *kd __unused)
221 {
222 
223 #ifdef __powerpc64__
224 	return (1);
225 #else
226 	return (0);
227 #endif
228 }
229 
230 static struct kvm_arch kvm_powerpc64 = {
231 	.ka_probe = _powerpc64_probe,
232 	.ka_initvtop = _powerpc64_initvtop,
233 	.ka_freevtop = _powerpc64_freevtop,
234 	.ka_kvatop = _powerpc64_kvatop,
235 	.ka_native = _powerpc64_native,
236 };
237 
238 KVM_ARCH(kvm_powerpc64);
239