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 <elf.h> 38 #include <kvm.h> 39 #include <limits.h> 40 #include <stdlib.h> 41 #include <string.h> 42 43 #include "kvm_private.h" 44 45 struct vmstate { 46 void *map; 47 size_t mapsz; 48 size_t dmphdrsz; 49 Elf64_Ehdr *eh; 50 Elf64_Phdr *ph; 51 }; 52 53 static int 54 valid_elf_header(Elf64_Ehdr *eh) 55 { 56 57 if (!IS_ELF(*eh)) 58 return (0); 59 if (eh->e_ident[EI_CLASS] != ELFCLASS64) 60 return (0); 61 if (eh->e_ident[EI_DATA] != ELFDATA2MSB) 62 return (0); 63 if (eh->e_ident[EI_VERSION] != EV_CURRENT) 64 return (0); 65 if (eh->e_ident[EI_OSABI] != ELFOSABI_STANDALONE) 66 return (0); 67 if (be16toh(eh->e_type) != ET_CORE) 68 return (0); 69 if (be16toh(eh->e_machine) != EM_PPC64) 70 return (0); 71 /* Can't think of anything else to check... */ 72 return (1); 73 } 74 75 static size_t 76 dump_header_size(struct kerneldumpheader *dh) 77 { 78 79 if (strcmp(dh->magic, KERNELDUMPMAGIC) != 0) 80 return (0); 81 if (strcmp(dh->architecture, "powerpc64") != 0) 82 return (0); 83 /* That should do it... */ 84 return (sizeof(*dh)); 85 } 86 87 /* 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 90 * set of headers. 91 */ 92 static int 93 powerpc_maphdrs(kvm_t *kd) 94 { 95 struct vmstate *vm; 96 size_t mapsz; 97 98 vm = kd->vmst; 99 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"); 104 return (-1); 105 } 106 vm->dmphdrsz = 0; 107 vm->eh = vm->map; 108 if (!valid_elf_header(vm->eh)) { 109 /* 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... 114 */ 115 vm->dmphdrsz = dump_header_size(vm->map); 116 if (vm->dmphdrsz == 0) 117 goto inval; 118 vm->eh = (void *)((uintptr_t)vm->map + vm->dmphdrsz); 119 if (!valid_elf_header(vm->eh)) 120 goto inval; 121 } 122 mapsz = be16toh(vm->eh->e_phentsize) * be16toh(vm->eh->e_phnum) + 123 be64toh(vm->eh->e_phoff); 124 munmap(vm->map, vm->mapsz); 125 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"); 131 return (-1); 132 } 133 vm->eh = (void *)((uintptr_t)vm->map + vm->dmphdrsz); 134 vm->ph = (void *)((uintptr_t)vm->eh + 135 (uintptr_t)be64toh(vm->eh->e_phoff)); 136 return (0); 137 138 inval: 139 _kvm_err(kd, kd->program, "invalid corefile"); 140 return (-1); 141 } 142 143 /* 144 * Determine the offset within the corefile corresponding the virtual 145 * address. Return the number of contiguous bytes in the corefile or 146 * 0 when the virtual address is invalid. 147 */ 148 static size_t 149 powerpc64_va2off(kvm_t *kd, kvaddr_t va, off_t *ofs) 150 { 151 struct vmstate *vm = kd->vmst; 152 Elf64_Phdr *ph; 153 int nph; 154 155 ph = vm->ph; 156 nph = be16toh(vm->eh->e_phnum); 157 while (nph && (va < be64toh(ph->p_vaddr) || 158 va >= be64toh(ph->p_vaddr) + be64toh(ph->p_memsz))) { 159 nph--; 160 ph = (void *)((uintptr_t)ph + be16toh(vm->eh->e_phentsize)); 161 } 162 if (nph == 0) 163 return (0); 164 165 /* Segment found. Return file offset and range. */ 166 *ofs = vm->dmphdrsz + be64toh(ph->p_offset) + 167 (va - be64toh(ph->p_vaddr)); 168 return (be64toh(ph->p_memsz) - (va - be64toh(ph->p_vaddr))); 169 } 170 171 static void 172 _powerpc64_freevtop(kvm_t *kd) 173 { 174 struct vmstate *vm = kd->vmst; 175 176 if (vm->eh != MAP_FAILED) 177 munmap(vm->eh, vm->mapsz); 178 free(vm); 179 kd->vmst = NULL; 180 } 181 182 static int 183 _powerpc64_probe(kvm_t *kd) 184 { 185 186 return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_PPC64) && 187 kd->nlehdr.e_ident[EI_DATA] == ELFDATA2MSB); 188 } 189 190 static int 191 _powerpc64_initvtop(kvm_t *kd) 192 { 193 194 kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst)); 195 if (kd->vmst == NULL) 196 return (-1); 197 198 if (powerpc_maphdrs(kd) == -1) 199 return (-1); 200 201 return (0); 202 } 203 204 static int 205 _powerpc64_kvatop(kvm_t *kd, kvaddr_t va, off_t *ofs) 206 { 207 struct vmstate *vm; 208 209 vm = kd->vmst; 210 if (be64toh(vm->ph->p_paddr) == 0xffffffffffffffff) 211 return ((int)powerpc64_va2off(kd, va, ofs)); 212 213 _kvm_err(kd, kd->program, "Raw corefile not supported"); 214 return (0); 215 } 216 217 static int 218 _powerpc64_native(kvm_t *kd) 219 { 220 221 #ifdef __powerpc64__ 222 return (1); 223 #else 224 return (0); 225 #endif 226 } 227 228 struct kvm_arch kvm_powerpc64 = { 229 .ka_probe = _powerpc64_probe, 230 .ka_initvtop = _powerpc64_initvtop, 231 .ka_freevtop = _powerpc64_freevtop, 232 .ka_kvatop = _powerpc64_kvatop, 233 .ka_native = _powerpc64_native, 234 }; 235 236 KVM_ARCH(kvm_powerpc64); 237