xref: /freebsd/lib/libkvm/kvm_powerpc.c (revision f05cddf9)
1 /*-
2  * Copyright (c) 2008, Juniper Networks, Inc.
3  * All rights reserved.
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  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/endian.h>
34 #include <sys/kerneldump.h>
35 #include <sys/mman.h>
36 
37 #include <vm/vm.h>
38 
39 #include <db.h>
40 #include <elf.h>
41 #include <limits.h>
42 #include <kvm.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include "kvm_private.h"
47 
48 struct vmstate {
49 	void		*map;
50 	size_t		mapsz;
51 	size_t		dmphdrsz;
52 	Elf32_Ehdr	*eh;
53 	Elf32_Phdr	*ph;
54 };
55 
56 static int
57 valid_elf_header(Elf32_Ehdr *eh)
58 {
59 
60 	if (!IS_ELF(*eh))
61 		return (0);
62 	if (eh->e_ident[EI_CLASS] != ELFCLASS32)
63 		return (0);
64 	if (eh->e_ident[EI_DATA] != ELFDATA2MSB)
65 		return (0);
66 	if (eh->e_ident[EI_VERSION] != EV_CURRENT)
67 		return (0);
68 	if (eh->e_ident[EI_OSABI] != ELFOSABI_STANDALONE)
69 		return (0);
70 	if (be16toh(eh->e_type) != ET_CORE)
71 		return (0);
72 	if (be16toh(eh->e_machine) != EM_PPC)
73 		return (0);
74 	/* Can't think of anything else to check... */
75 	return (1);
76 }
77 
78 static size_t
79 dump_header_size(struct kerneldumpheader *dh)
80 {
81 
82 	if (strcmp(dh->magic, KERNELDUMPMAGIC) != 0)
83 		return (0);
84 	if (strcmp(dh->architecture, "powerpc") != 0)
85 		return (0);
86 	/* That should do it... */
87 	return (sizeof(*dh));
88 }
89 
90 /*
91  * Map the ELF headers into the process' address space. We do this in two
92  * steps: first the ELF header itself and using that information the whole
93  * set of headers.
94  */
95 static int
96 powerpc_maphdrs(kvm_t *kd)
97 {
98 	struct vmstate *vm;
99 	size_t mapsz;
100 
101 	vm = kd->vmst;
102 
103 	vm->mapsz = PAGE_SIZE;
104 	vm->map = mmap(NULL, vm->mapsz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
105 	if (vm->map == MAP_FAILED) {
106 		_kvm_err(kd, kd->program, "cannot map corefile");
107 		return (-1);
108 	}
109 	vm->dmphdrsz = 0;
110 	vm->eh = vm->map;
111 	if (!valid_elf_header(vm->eh)) {
112 		/*
113 		 * Hmmm, no ELF header. Maybe we still have a dump header.
114 		 * This is normal when the core file wasn't created by
115 		 * savecore(8), but instead was dumped over TFTP. We can
116 		 * easily skip the dump header...
117 		 */
118 		vm->dmphdrsz = dump_header_size(vm->map);
119 		if (vm->dmphdrsz == 0)
120 			goto inval;
121 		vm->eh = (void *)((uintptr_t)vm->map + vm->dmphdrsz);
122 		if (!valid_elf_header(vm->eh))
123 			goto inval;
124 	}
125 	mapsz = be16toh(vm->eh->e_phentsize) * be16toh(vm->eh->e_phnum) +
126 	    be32toh(vm->eh->e_phoff);
127 	munmap(vm->map, vm->mapsz);
128 
129 	/* Map all headers. */
130 	vm->mapsz = vm->dmphdrsz + mapsz;
131 	vm->map = mmap(NULL, vm->mapsz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
132 	if (vm->map == MAP_FAILED) {
133 		_kvm_err(kd, kd->program, "cannot map corefle headers");
134 		return (-1);
135 	}
136 	vm->eh = (void *)((uintptr_t)vm->map + vm->dmphdrsz);
137 	vm->ph = (void *)((uintptr_t)vm->eh + be32toh(vm->eh->e_phoff));
138 	return (0);
139 
140  inval:
141 	munmap(vm->map, vm->mapsz);
142 	vm->map = MAP_FAILED;
143 	_kvm_err(kd, kd->program, "invalid corefile");
144 	return (-1);
145 }
146 
147 /*
148  * Determine the offset within the corefile corresponding the virtual
149  * address. Return the number of contiguous bytes in the corefile or
150  * 0 when the virtual address is invalid.
151  */
152 static size_t
153 powerpc_va2off(kvm_t *kd, u_long va, off_t *ofs)
154 {
155 	struct vmstate *vm = kd->vmst;
156 	Elf32_Phdr *ph;
157 	int nph;
158 
159 	ph = vm->ph;
160 	nph = be16toh(vm->eh->e_phnum);
161 	while (nph && (va < be32toh(ph->p_vaddr) ||
162 	    va >= be32toh(ph->p_vaddr) + be32toh(ph->p_memsz))) {
163 		nph--;
164 		ph = (void *)((uintptr_t)ph + be16toh(vm->eh->e_phentsize));
165 	}
166 	if (nph == 0)
167 		return (0);
168 
169 	/* Segment found. Return file offset and range. */
170 	*ofs = vm->dmphdrsz + be32toh(ph->p_offset) +
171 	    (va - be32toh(ph->p_vaddr));
172 	return (be32toh(ph->p_memsz) - (va - be32toh(ph->p_vaddr)));
173 }
174 
175 void
176 _kvm_freevtop(kvm_t *kd)
177 {
178 	struct vmstate *vm = kd->vmst;
179 
180 	if (vm == NULL)
181 		return;
182 
183 	if (vm->eh != MAP_FAILED) {
184 		munmap(vm->eh, vm->mapsz);
185 		vm->eh = MAP_FAILED;
186 	}
187 	free(vm);
188 	kd->vmst = NULL;
189 }
190 
191 int
192 _kvm_initvtop(kvm_t *kd)
193 {
194 
195 	kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst));
196 	if (kd->vmst == NULL) {
197 		_kvm_err(kd, kd->program, "out of virtual memory");
198 		return (-1);
199 	}
200 	if (powerpc_maphdrs(kd) == -1) {
201 		free(kd->vmst);
202 		kd->vmst = NULL;
203 		return (-1);
204 	}
205 	return (0);
206 }
207 
208 int
209 _kvm_kvatop(kvm_t *kd, u_long va, off_t *ofs)
210 {
211 	struct vmstate *vm;
212 
213 	vm = kd->vmst;
214 	if (vm->ph->p_paddr == ~0U)
215 		return ((int)powerpc_va2off(kd, va, ofs));
216 
217 	_kvm_err(kd, kd->program, "Raw corefile not supported");
218 	return (0);
219 }
220