2 * Copyright (c) 2008, Juniper Networks, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/endian.h>
34 #include <sys/kerneldump.h>
43 #include "kvm_private.h"
54 valid_elf_header(Elf32_Ehdr *eh)
59 if (eh->e_ident[EI_CLASS] != ELFCLASS32)
61 if (eh->e_ident[EI_DATA] != ELFDATA2MSB)
63 if (eh->e_ident[EI_VERSION] != EV_CURRENT)
65 if (eh->e_ident[EI_OSABI] != ELFOSABI_STANDALONE)
67 if (be16toh(eh->e_type) != ET_CORE)
69 if (be16toh(eh->e_machine) != EM_PPC)
71 /* Can't think of anything else to check... */
76 dump_header_size(struct kerneldumpheader *dh)
79 if (strcmp(dh->magic, KERNELDUMPMAGIC) != 0)
81 if (strcmp(dh->architecture, "powerpc") != 0)
83 /* That should do it... */
88 * Map the ELF headers into the process' address space. We do this in two
89 * steps: first the ELF header itself and using that information the whole
93 powerpc_maphdrs(kvm_t *kd)
100 vm->mapsz = sizeof(*vm->eh) + sizeof(struct kerneldumpheader);
101 vm->map = mmap(NULL, vm->mapsz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
102 if (vm->map == MAP_FAILED) {
103 _kvm_err(kd, kd->program, "cannot map corefile");
108 if (!valid_elf_header(vm->eh)) {
110 * Hmmm, no ELF header. Maybe we still have a dump header.
111 * This is normal when the core file wasn't created by
112 * savecore(8), but instead was dumped over TFTP. We can
113 * easily skip the dump header...
115 vm->dmphdrsz = dump_header_size(vm->map);
116 if (vm->dmphdrsz == 0)
118 vm->eh = (void *)((uintptr_t)vm->map + vm->dmphdrsz);
119 if (!valid_elf_header(vm->eh))
122 mapsz = be16toh(vm->eh->e_phentsize) * be16toh(vm->eh->e_phnum) +
123 be32toh(vm->eh->e_phoff);
124 munmap(vm->map, vm->mapsz);
126 /* Map all headers. */
127 vm->mapsz = vm->dmphdrsz + mapsz;
128 vm->map = mmap(NULL, vm->mapsz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
129 if (vm->map == MAP_FAILED) {
130 _kvm_err(kd, kd->program, "cannot map corefile headers");
133 vm->eh = (void *)((uintptr_t)vm->map + vm->dmphdrsz);
134 vm->ph = (void *)((uintptr_t)vm->eh + be32toh(vm->eh->e_phoff));
138 _kvm_err(kd, kd->program, "invalid corefile");
143 * Determine the offset within the corefile corresponding the virtual
144 * address. Return the number of contiguous bytes in the corefile or
145 * 0 when the virtual address is invalid.
148 powerpc_va2off(kvm_t *kd, kvaddr_t va, off_t *ofs)
150 struct vmstate *vm = kd->vmst;
155 nph = be16toh(vm->eh->e_phnum);
156 while (nph && (va < be32toh(ph->p_vaddr) ||
157 va >= be32toh(ph->p_vaddr) + be32toh(ph->p_memsz))) {
159 ph = (void *)((uintptr_t)ph + be16toh(vm->eh->e_phentsize));
164 /* Segment found. Return file offset and range. */
165 *ofs = vm->dmphdrsz + be32toh(ph->p_offset) +
166 (va - be32toh(ph->p_vaddr));
167 return (be32toh(ph->p_memsz) - (va - be32toh(ph->p_vaddr)));
171 _powerpc_freevtop(kvm_t *kd)
173 struct vmstate *vm = kd->vmst;
175 if (vm->eh != MAP_FAILED)
176 munmap(vm->eh, vm->mapsz);
182 _powerpc_probe(kvm_t *kd)
185 return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_PPC) &&
186 kd->nlehdr.e_ident[EI_DATA] == ELFDATA2MSB);
190 _powerpc_initvtop(kvm_t *kd)
193 kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst));
194 if (kd->vmst == NULL)
197 if (powerpc_maphdrs(kd) == -1)
204 _powerpc_kvatop(kvm_t *kd, kvaddr_t va, off_t *ofs)
209 if (be32toh(vm->ph->p_paddr) == 0xffffffff)
210 return ((int)powerpc_va2off(kd, va, ofs));
212 _kvm_err(kd, kd->program, "Raw corefile not supported");
217 _powerpc_native(kvm_t *kd __unused)
220 #if defined(__powerpc__) && !defined(__powerpc64__)
227 static struct kvm_arch kvm_powerpc = {
228 .ka_probe = _powerpc_probe,
229 .ka_initvtop = _powerpc_initvtop,
230 .ka_freevtop = _powerpc_freevtop,
231 .ka_kvatop = _powerpc_kvatop,
232 .ka_native = _powerpc_native,
235 KVM_ARCH(kvm_powerpc);