1209881Snwhitehorn/*-
2209881Snwhitehorn * Copyright (c) 2008, Juniper Networks, Inc.
3209881Snwhitehorn * All rights reserved.
4209881Snwhitehorn *
5209881Snwhitehorn * Redistribution and use in source and binary forms, with or without
6209881Snwhitehorn * modification, are permitted provided that the following conditions
7209881Snwhitehorn * are met:
8209881Snwhitehorn * 1. Redistributions of source code must retain the above copyright
9209881Snwhitehorn *    notice, this list of conditions and the following disclaimer.
10209881Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
11209881Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
12209881Snwhitehorn *    documentation and/or other materials provided with the distribution.
13209881Snwhitehorn * 3. Neither the name of the author nor the names of any co-contributors
14209881Snwhitehorn *    may be used to endorse or promote products derived from this software
15209881Snwhitehorn *    without specific prior written permission.
16209881Snwhitehorn *
17209881Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18209881Snwhitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19209881Snwhitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20209881Snwhitehorn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21209881Snwhitehorn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22209881Snwhitehorn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23209881Snwhitehorn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24209881Snwhitehorn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25209881Snwhitehorn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26209881Snwhitehorn * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27209881Snwhitehorn */
28209881Snwhitehorn
29209881Snwhitehorn#include <sys/cdefs.h>
30209881Snwhitehorn__FBSDID("$FreeBSD$");
31209881Snwhitehorn
32209881Snwhitehorn#include <sys/param.h>
33209881Snwhitehorn#include <sys/endian.h>
34209881Snwhitehorn#include <sys/kerneldump.h>
35209881Snwhitehorn#include <sys/mman.h>
36209881Snwhitehorn
37209881Snwhitehorn#include <vm/vm.h>
38209881Snwhitehorn
39209881Snwhitehorn#include <db.h>
40209881Snwhitehorn#include <elf.h>
41209881Snwhitehorn#include <limits.h>
42209881Snwhitehorn#include <kvm.h>
43209881Snwhitehorn#include <stdlib.h>
44217777Suqs#include <string.h>
45209881Snwhitehorn
46209881Snwhitehorn#include "kvm_private.h"
47209881Snwhitehorn
48209881Snwhitehornstruct vmstate {
49209881Snwhitehorn	void		*map;
50209881Snwhitehorn	size_t		mapsz;
51209881Snwhitehorn	size_t		dmphdrsz;
52209881Snwhitehorn	Elf64_Ehdr	*eh;
53209881Snwhitehorn	Elf64_Phdr	*ph;
54209881Snwhitehorn};
55209881Snwhitehorn
56209881Snwhitehornstatic int
57209881Snwhitehornvalid_elf_header(Elf64_Ehdr *eh)
58209881Snwhitehorn{
59209881Snwhitehorn
60209881Snwhitehorn	if (!IS_ELF(*eh))
61209881Snwhitehorn		return (0);
62209881Snwhitehorn	if (eh->e_ident[EI_CLASS] != ELFCLASS64)
63209881Snwhitehorn		return (0);
64209881Snwhitehorn	if (eh->e_ident[EI_DATA] != ELFDATA2MSB)
65209881Snwhitehorn		return (0);
66209881Snwhitehorn	if (eh->e_ident[EI_VERSION] != EV_CURRENT)
67209881Snwhitehorn		return (0);
68209881Snwhitehorn	if (eh->e_ident[EI_OSABI] != ELFOSABI_STANDALONE)
69209881Snwhitehorn		return (0);
70209881Snwhitehorn	if (be16toh(eh->e_type) != ET_CORE)
71209881Snwhitehorn		return (0);
72209881Snwhitehorn	if (be16toh(eh->e_machine) != EM_PPC64)
73209881Snwhitehorn		return (0);
74209881Snwhitehorn	/* Can't think of anything else to check... */
75209881Snwhitehorn	return (1);
76209881Snwhitehorn}
77209881Snwhitehorn
78209881Snwhitehornstatic size_t
79209881Snwhitehorndump_header_size(struct kerneldumpheader *dh)
80209881Snwhitehorn{
81209881Snwhitehorn
82209881Snwhitehorn	if (strcmp(dh->magic, KERNELDUMPMAGIC) != 0)
83209881Snwhitehorn		return (0);
84209881Snwhitehorn	if (strcmp(dh->architecture, "powerpc64") != 0)
85209881Snwhitehorn		return (0);
86209881Snwhitehorn	/* That should do it... */
87209881Snwhitehorn	return (sizeof(*dh));
88209881Snwhitehorn}
89209881Snwhitehorn
90209881Snwhitehorn/*
91209881Snwhitehorn * Map the ELF headers into the process' address space. We do this in two
92209881Snwhitehorn * steps: first the ELF header itself and using that information the whole
93209881Snwhitehorn * set of headers.
94209881Snwhitehorn */
95209881Snwhitehornstatic int
96209881Snwhitehornpowerpc_maphdrs(kvm_t *kd)
97209881Snwhitehorn{
98209881Snwhitehorn	struct vmstate *vm;
99209881Snwhitehorn	size_t mapsz;
100209881Snwhitehorn
101209881Snwhitehorn	vm = kd->vmst;
102209881Snwhitehorn
103209881Snwhitehorn	vm->mapsz = PAGE_SIZE;
104209881Snwhitehorn	vm->map = mmap(NULL, vm->mapsz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
105209881Snwhitehorn	if (vm->map == MAP_FAILED) {
106209881Snwhitehorn		_kvm_err(kd, kd->program, "cannot map corefile");
107209881Snwhitehorn		return (-1);
108209881Snwhitehorn	}
109209881Snwhitehorn	vm->dmphdrsz = 0;
110209881Snwhitehorn	vm->eh = vm->map;
111209881Snwhitehorn	if (!valid_elf_header(vm->eh)) {
112209881Snwhitehorn		/*
113209881Snwhitehorn		 * Hmmm, no ELF header. Maybe we still have a dump header.
114209881Snwhitehorn		 * This is normal when the core file wasn't created by
115209881Snwhitehorn		 * savecore(8), but instead was dumped over TFTP. We can
116209881Snwhitehorn		 * easily skip the dump header...
117209881Snwhitehorn		 */
118209881Snwhitehorn		vm->dmphdrsz = dump_header_size(vm->map);
119209881Snwhitehorn		if (vm->dmphdrsz == 0)
120209881Snwhitehorn			goto inval;
121209881Snwhitehorn		vm->eh = (void *)((uintptr_t)vm->map + vm->dmphdrsz);
122209881Snwhitehorn		if (!valid_elf_header(vm->eh))
123209881Snwhitehorn			goto inval;
124209881Snwhitehorn	}
125209881Snwhitehorn	mapsz = be16toh(vm->eh->e_phentsize) * be16toh(vm->eh->e_phnum) +
126209881Snwhitehorn	    be64toh(vm->eh->e_phoff);
127209881Snwhitehorn	munmap(vm->map, vm->mapsz);
128209881Snwhitehorn
129209881Snwhitehorn	/* Map all headers. */
130209881Snwhitehorn	vm->mapsz = vm->dmphdrsz + mapsz;
131209881Snwhitehorn	vm->map = mmap(NULL, vm->mapsz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
132209881Snwhitehorn	if (vm->map == MAP_FAILED) {
133209881Snwhitehorn		_kvm_err(kd, kd->program, "cannot map corefle headers");
134209881Snwhitehorn		return (-1);
135209881Snwhitehorn	}
136209881Snwhitehorn	vm->eh = (void *)((uintptr_t)vm->map + vm->dmphdrsz);
137209881Snwhitehorn	vm->ph = (void *)((uintptr_t)vm->eh + be64toh(vm->eh->e_phoff));
138209881Snwhitehorn	return (0);
139209881Snwhitehorn
140209881Snwhitehorn inval:
141209881Snwhitehorn	munmap(vm->map, vm->mapsz);
142209881Snwhitehorn	vm->map = MAP_FAILED;
143209881Snwhitehorn	_kvm_err(kd, kd->program, "invalid corefile");
144209881Snwhitehorn	return (-1);
145209881Snwhitehorn}
146209881Snwhitehorn
147209881Snwhitehorn/*
148209881Snwhitehorn * Determine the offset within the corefile corresponding the virtual
149209881Snwhitehorn * address. Return the number of contiguous bytes in the corefile or
150209881Snwhitehorn * 0 when the virtual address is invalid.
151209881Snwhitehorn */
152209881Snwhitehornstatic size_t
153209881Snwhitehornpowerpc64_va2off(kvm_t *kd, u_long va, off_t *ofs)
154209881Snwhitehorn{
155209881Snwhitehorn	struct vmstate *vm = kd->vmst;
156209881Snwhitehorn	Elf64_Phdr *ph;
157209881Snwhitehorn	int nph;
158209881Snwhitehorn
159209881Snwhitehorn	ph = vm->ph;
160209881Snwhitehorn	nph = be16toh(vm->eh->e_phnum);
161209881Snwhitehorn	while (nph && (va < be64toh(ph->p_vaddr) ||
162209881Snwhitehorn	    va >= be64toh(ph->p_vaddr) + be64toh(ph->p_memsz))) {
163209881Snwhitehorn		nph--;
164209881Snwhitehorn		ph = (void *)((uintptr_t)ph + be16toh(vm->eh->e_phentsize));
165209881Snwhitehorn	}
166209881Snwhitehorn	if (nph == 0)
167209881Snwhitehorn		return (0);
168209881Snwhitehorn
169209881Snwhitehorn	/* Segment found. Return file offset and range. */
170209881Snwhitehorn	*ofs = vm->dmphdrsz + be64toh(ph->p_offset) +
171209881Snwhitehorn	    (va - be64toh(ph->p_vaddr));
172209881Snwhitehorn	return (be64toh(ph->p_memsz) - (va - be64toh(ph->p_vaddr)));
173209881Snwhitehorn}
174209881Snwhitehorn
175209881Snwhitehornvoid
176209881Snwhitehorn_kvm_freevtop(kvm_t *kd)
177209881Snwhitehorn{
178209881Snwhitehorn	struct vmstate *vm = kd->vmst;
179209881Snwhitehorn
180209881Snwhitehorn	if (vm == NULL)
181209881Snwhitehorn		return;
182209881Snwhitehorn
183209881Snwhitehorn	if (vm->eh != MAP_FAILED) {
184209881Snwhitehorn		munmap(vm->eh, vm->mapsz);
185209881Snwhitehorn		vm->eh = MAP_FAILED;
186209881Snwhitehorn	}
187209881Snwhitehorn	free(vm);
188209881Snwhitehorn	kd->vmst = NULL;
189209881Snwhitehorn}
190209881Snwhitehorn
191209881Snwhitehornint
192209881Snwhitehorn_kvm_initvtop(kvm_t *kd)
193209881Snwhitehorn{
194209881Snwhitehorn
195209881Snwhitehorn	kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst));
196209881Snwhitehorn	if (kd->vmst == NULL) {
197209881Snwhitehorn		_kvm_err(kd, kd->program, "out of virtual memory");
198209881Snwhitehorn		return (-1);
199209881Snwhitehorn	}
200209881Snwhitehorn	if (powerpc_maphdrs(kd) == -1) {
201209881Snwhitehorn		free(kd->vmst);
202209881Snwhitehorn		kd->vmst = NULL;
203209881Snwhitehorn		return (-1);
204209881Snwhitehorn	}
205209881Snwhitehorn	return (0);
206209881Snwhitehorn}
207209881Snwhitehorn
208209881Snwhitehornint
209209881Snwhitehorn_kvm_kvatop(kvm_t *kd, u_long va, off_t *ofs)
210209881Snwhitehorn{
211209881Snwhitehorn	struct vmstate *vm;
212209881Snwhitehorn
213209881Snwhitehorn	vm = kd->vmst;
214209881Snwhitehorn	if (vm->ph->p_paddr == ~0UL)
215209881Snwhitehorn		return ((int)powerpc64_va2off(kd, va, ofs));
216209881Snwhitehorn
217209881Snwhitehorn	_kvm_err(kd, kd->program, "Raw corefile not supported");
218209881Snwhitehorn	return (0);
219209881Snwhitehorn}
220