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