2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2008, Juniper Networks, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/endian.h>
36 #include <sys/kerneldump.h>
45 #include "kvm_private.h"
56 valid_elf_header(Elf32_Ehdr *eh)
61 if (eh->e_ident[EI_CLASS] != ELFCLASS32)
63 if (eh->e_ident[EI_DATA] != ELFDATA2MSB)
65 if (eh->e_ident[EI_VERSION] != EV_CURRENT)
67 if (eh->e_ident[EI_OSABI] != ELFOSABI_STANDALONE)
69 if (be16toh(eh->e_type) != ET_CORE)
71 if (be16toh(eh->e_machine) != EM_PPC)
73 /* Can't think of anything else to check... */
78 dump_header_size(struct kerneldumpheader *dh)
81 if (strcmp(dh->magic, KERNELDUMPMAGIC) != 0)
83 if (strcmp(dh->architecture, "powerpc") != 0)
85 /* That should do it... */
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
95 powerpc_maphdrs(kvm_t *kd)
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");
110 if (!valid_elf_header(vm->eh)) {
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...
117 vm->dmphdrsz = dump_header_size(vm->map);
118 if (vm->dmphdrsz == 0)
120 vm->eh = (void *)((uintptr_t)vm->map + vm->dmphdrsz);
121 if (!valid_elf_header(vm->eh))
124 mapsz = be16toh(vm->eh->e_phentsize) * be16toh(vm->eh->e_phnum) +
125 be32toh(vm->eh->e_phoff);
126 munmap(vm->map, vm->mapsz);
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");
135 vm->eh = (void *)((uintptr_t)vm->map + vm->dmphdrsz);
136 vm->ph = (void *)((uintptr_t)vm->eh + be32toh(vm->eh->e_phoff));
140 _kvm_err(kd, kd->program, "invalid corefile");
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.
150 powerpc_va2off(kvm_t *kd, kvaddr_t va, off_t *ofs)
152 struct vmstate *vm = kd->vmst;
157 nph = be16toh(vm->eh->e_phnum);
158 while (nph && (va < be32toh(ph->p_vaddr) ||
159 va >= be32toh(ph->p_vaddr) + be32toh(ph->p_memsz))) {
161 ph = (void *)((uintptr_t)ph + be16toh(vm->eh->e_phentsize));
166 /* Segment found. Return file offset and range. */
167 *ofs = vm->dmphdrsz + be32toh(ph->p_offset) +
168 (va - be32toh(ph->p_vaddr));
169 return (be32toh(ph->p_memsz) - (va - be32toh(ph->p_vaddr)));
173 _powerpc_freevtop(kvm_t *kd)
175 struct vmstate *vm = kd->vmst;
177 if (vm->eh != MAP_FAILED)
178 munmap(vm->eh, vm->mapsz);
184 _powerpc_probe(kvm_t *kd)
187 return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_PPC) &&
188 kd->nlehdr.e_ident[EI_DATA] == ELFDATA2MSB);
192 _powerpc_initvtop(kvm_t *kd)
195 kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst));
196 if (kd->vmst == NULL)
199 if (powerpc_maphdrs(kd) == -1)
206 _powerpc_kvatop(kvm_t *kd, kvaddr_t va, off_t *ofs)
211 if (be32toh(vm->ph->p_paddr) == 0xffffffff)
212 return ((int)powerpc_va2off(kd, va, ofs));
214 _kvm_err(kd, kd->program, "Raw corefile not supported");
219 _powerpc_native(kvm_t *kd __unused)
222 #if defined(__powerpc__) && !defined(__powerpc64__)
229 static struct kvm_arch kvm_powerpc = {
230 .ka_probe = _powerpc_probe,
231 .ka_initvtop = _powerpc_initvtop,
232 .ka_freevtop = _powerpc_freevtop,
233 .ka_kvatop = _powerpc_kvatop,
234 .ka_native = _powerpc_native,
237 KVM_ARCH(kvm_powerpc);