xref: /freebsd/lib/libkvm/kvm_arm.c (revision 8e321b79437e9e70ff57785e8508c1d2734d8a7f)
18cffa1b4SOlivier Houchard /*-
25fa78ac5SWarner Losh  * Copyright (c) 2005 Olivier Houchard
35fa78ac5SWarner Losh  * Copyright (c) 1989, 1992, 1993
45fa78ac5SWarner Losh  *	The Regents of the University of California.  All rights reserved.
55fa78ac5SWarner Losh  *
65fa78ac5SWarner Losh  * This code is derived from software developed by the Computer Systems
75fa78ac5SWarner Losh  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
85fa78ac5SWarner Losh  * BG 91-66 and contributed to Berkeley.
98cffa1b4SOlivier Houchard  *
108cffa1b4SOlivier Houchard  * Redistribution and use in source and binary forms, with or without
118cffa1b4SOlivier Houchard  * modification, are permitted provided that the following conditions
128cffa1b4SOlivier Houchard  * are met:
138cffa1b4SOlivier Houchard  * 1. Redistributions of source code must retain the above copyright
148cffa1b4SOlivier Houchard  *    notice, this list of conditions and the following disclaimer.
158cffa1b4SOlivier Houchard  * 2. Redistributions in binary form must reproduce the above copyright
168cffa1b4SOlivier Houchard  *    notice, this list of conditions and the following disclaimer in the
178cffa1b4SOlivier Houchard  *    documentation and/or other materials provided with the distribution.
185fa78ac5SWarner Losh  * 4. Neither the name of the University nor the names of its contributors
195fa78ac5SWarner Losh  *    may be used to endorse or promote products derived from this software
205fa78ac5SWarner Losh  *    without specific prior written permission.
218cffa1b4SOlivier Houchard  *
228cffa1b4SOlivier Houchard  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
238cffa1b4SOlivier Houchard  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
248cffa1b4SOlivier Houchard  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
258cffa1b4SOlivier Houchard  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
268cffa1b4SOlivier Houchard  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
278cffa1b4SOlivier Houchard  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
288cffa1b4SOlivier Houchard  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
298cffa1b4SOlivier Houchard  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
308cffa1b4SOlivier Houchard  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
318cffa1b4SOlivier Houchard  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
328cffa1b4SOlivier Houchard  */
338cffa1b4SOlivier Houchard 
348cffa1b4SOlivier Houchard /*
358cffa1b4SOlivier Houchard  * ARM machine dependent routines for kvm.
368cffa1b4SOlivier Houchard  */
378cffa1b4SOlivier Houchard 
388cffa1b4SOlivier Houchard #include <sys/cdefs.h>
398cffa1b4SOlivier Houchard __FBSDID("$FreeBSD$");
408cffa1b4SOlivier Houchard 
418cffa1b4SOlivier Houchard #include <sys/param.h>
429960ac47SOlivier Houchard #include <sys/elf32.h>
439960ac47SOlivier Houchard #include <sys/mman.h>
448cffa1b4SOlivier Houchard 
458cffa1b4SOlivier Houchard #include <vm/vm.h>
469960ac47SOlivier Houchard #include <vm/vm_param.h>
479960ac47SOlivier Houchard #include <vm/pmap.h>
489960ac47SOlivier Houchard 
499960ac47SOlivier Houchard #include <machine/pmap.h>
508cffa1b4SOlivier Houchard 
518cffa1b4SOlivier Houchard #include <db.h>
528cffa1b4SOlivier Houchard #include <limits.h>
538cffa1b4SOlivier Houchard #include <kvm.h>
548cffa1b4SOlivier Houchard #include <stdlib.h>
558e321b79SRafal Jaworowski #include <unistd.h>
568cffa1b4SOlivier Houchard 
578cffa1b4SOlivier Houchard #include "kvm_private.h"
588cffa1b4SOlivier Houchard 
598e321b79SRafal Jaworowski /* minidump must be the first item! */
609960ac47SOlivier Houchard struct vmstate {
618e321b79SRafal Jaworowski 	int minidump;		/* 1 = minidump mode */
629960ac47SOlivier Houchard 	pd_entry_t *l1pt;
639960ac47SOlivier Houchard 	void *mmapbase;
649960ac47SOlivier Houchard 	size_t mmapsize;
659960ac47SOlivier Houchard };
669960ac47SOlivier Houchard 
679960ac47SOlivier Houchard static int
689960ac47SOlivier Houchard _kvm_maphdrs(kvm_t *kd, size_t sz)
699960ac47SOlivier Houchard {
709960ac47SOlivier Houchard 	struct vmstate *vm = kd->vmst;
719960ac47SOlivier Houchard 
729960ac47SOlivier Houchard 	/* munmap() previous mmap(). */
739960ac47SOlivier Houchard 	if (vm->mmapbase != NULL) {
749960ac47SOlivier Houchard 		munmap(vm->mmapbase, vm->mmapsize);
759960ac47SOlivier Houchard 		vm->mmapbase = NULL;
769960ac47SOlivier Houchard 	}
779960ac47SOlivier Houchard 
789960ac47SOlivier Houchard 	vm->mmapsize = sz;
799960ac47SOlivier Houchard 	vm->mmapbase = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
809960ac47SOlivier Houchard 	if (vm->mmapbase == MAP_FAILED) {
819960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "cannot mmap corefile");
829960ac47SOlivier Houchard 		return (-1);
839960ac47SOlivier Houchard 	}
849960ac47SOlivier Houchard 
859960ac47SOlivier Houchard 	return (0);
869960ac47SOlivier Houchard }
879960ac47SOlivier Houchard 
889960ac47SOlivier Houchard /*
899960ac47SOlivier Houchard  * Translate a physical memory address to a file-offset in the crash-dump.
909960ac47SOlivier Houchard  */
919960ac47SOlivier Houchard static size_t
929960ac47SOlivier Houchard _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs, size_t pgsz)
939960ac47SOlivier Houchard {
949960ac47SOlivier Houchard 	Elf32_Ehdr *e = kd->vmst->mmapbase;
959960ac47SOlivier Houchard 	Elf32_Phdr *p = (Elf32_Phdr*)((char*)e + e->e_phoff);
969960ac47SOlivier Houchard 	int n = e->e_phnum;
979960ac47SOlivier Houchard 
989960ac47SOlivier Houchard 	while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz))
999960ac47SOlivier Houchard 		p++, n--;
1009960ac47SOlivier Houchard 	if (n == 0)
1019960ac47SOlivier Houchard 		return (0);
1029960ac47SOlivier Houchard 
1039960ac47SOlivier Houchard 	*ofs = (pa - p->p_paddr) + p->p_offset;
1049960ac47SOlivier Houchard 	if (pgsz == 0)
1059960ac47SOlivier Houchard 		return (p->p_memsz - (pa - p->p_paddr));
1069960ac47SOlivier Houchard 	return (pgsz - ((size_t)pa & (pgsz - 1)));
1079960ac47SOlivier Houchard }
1089960ac47SOlivier Houchard 
1098cffa1b4SOlivier Houchard void
1109960ac47SOlivier Houchard _kvm_freevtop(kvm_t *kd)
1118cffa1b4SOlivier Houchard {
1129960ac47SOlivier Houchard 	if (kd->vmst != 0) {
1138e321b79SRafal Jaworowski 		if (kd->vmst->minidump)
1148e321b79SRafal Jaworowski 			return (_kvm_minidump_freevtop(kd));
1159960ac47SOlivier Houchard 		if (kd->vmst->mmapbase != NULL)
1169960ac47SOlivier Houchard 			munmap(kd->vmst->mmapbase, kd->vmst->mmapsize);
1178cffa1b4SOlivier Houchard 		free(kd->vmst);
1189960ac47SOlivier Houchard 		kd->vmst = NULL;
1199960ac47SOlivier Houchard 	}
1208cffa1b4SOlivier Houchard }
1218cffa1b4SOlivier Houchard 
1228cffa1b4SOlivier Houchard int
1239960ac47SOlivier Houchard _kvm_initvtop(kvm_t *kd)
1248cffa1b4SOlivier Houchard {
1258e321b79SRafal Jaworowski 	struct vmstate *vm;
1269960ac47SOlivier Houchard 	struct nlist nlist[2];
1279960ac47SOlivier Houchard 	u_long kernbase, physaddr, pa;
1289960ac47SOlivier Houchard 	pd_entry_t *l1pt;
1299960ac47SOlivier Houchard 	Elf32_Ehdr *ehdr;
1309960ac47SOlivier Houchard 	size_t hdrsz;
1318e321b79SRafal Jaworowski 	char minihdr[8];
1329960ac47SOlivier Houchard 
1338e321b79SRafal Jaworowski 	if (!kd->rawdump) {
1348e321b79SRafal Jaworowski 		if (pread(kd->pmfd, &minihdr, 8, 0) == 8) {
1358e321b79SRafal Jaworowski 			if (memcmp(&minihdr, "minidump", 8) == 0)
1368e321b79SRafal Jaworowski 				return (_kvm_minidump_initvtop(kd));
1378e321b79SRafal Jaworowski 		} else {
1388e321b79SRafal Jaworowski 			_kvm_err(kd, kd->program, "cannot read header");
1398e321b79SRafal Jaworowski 			return (-1);
1408e321b79SRafal Jaworowski 		}
1418e321b79SRafal Jaworowski 	}
1428e321b79SRafal Jaworowski 
1438e321b79SRafal Jaworowski 	vm = _kvm_malloc(kd, sizeof(*vm));
1449960ac47SOlivier Houchard 	if (vm == 0) {
1459960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "cannot allocate vm");
1469960ac47SOlivier Houchard 		return (-1);
1479960ac47SOlivier Houchard 	}
1489960ac47SOlivier Houchard 	kd->vmst = vm;
1499960ac47SOlivier Houchard 	vm->l1pt = NULL;
1509960ac47SOlivier Houchard 	if (_kvm_maphdrs(kd, sizeof(Elf32_Ehdr)) == -1)
1519960ac47SOlivier Houchard 		return (-1);
1529960ac47SOlivier Houchard 	ehdr = kd->vmst->mmapbase;
1539960ac47SOlivier Houchard 	hdrsz = ehdr->e_phoff + ehdr->e_phentsize * ehdr->e_phnum;
1549960ac47SOlivier Houchard 	if (_kvm_maphdrs(kd, hdrsz) == -1)
1559960ac47SOlivier Houchard 		return (-1);
1569960ac47SOlivier Houchard 	nlist[0].n_name = "kernbase";
1579960ac47SOlivier Houchard 	nlist[1].n_name = NULL;
1589960ac47SOlivier Houchard 	if (kvm_nlist(kd, nlist) != 0)
1599960ac47SOlivier Houchard 		kernbase = KERNBASE;
1609960ac47SOlivier Houchard 	else
1619960ac47SOlivier Houchard 		kernbase = nlist[0].n_value;
1629960ac47SOlivier Houchard 
1639960ac47SOlivier Houchard 	nlist[0].n_name = "physaddr";
1649960ac47SOlivier Houchard 	if (kvm_nlist(kd, nlist) != 0) {
1659960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "couldn't get phys addr");
1669960ac47SOlivier Houchard 		return (-1);
1679960ac47SOlivier Houchard 	}
1689960ac47SOlivier Houchard 	physaddr = nlist[0].n_value;
1699960ac47SOlivier Houchard 	nlist[0].n_name = "kernel_l1pa";
1709960ac47SOlivier Houchard 	if (kvm_nlist(kd, nlist) != 0) {
1719960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "bad namelist");
1729960ac47SOlivier Houchard 		return (-1);
1739960ac47SOlivier Houchard 	}
1749960ac47SOlivier Houchard 	if (kvm_read(kd, (nlist[0].n_value - kernbase + physaddr), &pa,
1759960ac47SOlivier Houchard 	    sizeof(pa)) != sizeof(pa)) {
1769960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "cannot read kernel_l1pa");
1779960ac47SOlivier Houchard 		return (-1);
1789960ac47SOlivier Houchard 	}
1799960ac47SOlivier Houchard 	l1pt = _kvm_malloc(kd, L1_TABLE_SIZE);
1809960ac47SOlivier Houchard 	if (kvm_read(kd, pa, l1pt, L1_TABLE_SIZE) != L1_TABLE_SIZE) {
1819960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "cannot read l1pt");
1829960ac47SOlivier Houchard 		free(l1pt);
1839960ac47SOlivier Houchard 		return (-1);
1849960ac47SOlivier Houchard 	}
1859960ac47SOlivier Houchard 	vm->l1pt = l1pt;
1868cffa1b4SOlivier Houchard 	return 0;
1878cffa1b4SOlivier Houchard }
1888cffa1b4SOlivier Houchard 
1899960ac47SOlivier Houchard /* from arm/pmap.c */
1909960ac47SOlivier Houchard #define	L1_IDX(va)		(((vm_offset_t)(va)) >> L1_S_SHIFT)
1919960ac47SOlivier Houchard /* from arm/pmap.h */
1929960ac47SOlivier Houchard #define	L1_TYPE_INV	0x00		/* Invalid (fault) */
1939960ac47SOlivier Houchard #define	L1_TYPE_C	0x01		/* Coarse L2 */
1949960ac47SOlivier Houchard #define	L1_TYPE_S	0x02		/* Section */
1959960ac47SOlivier Houchard #define	L1_TYPE_F	0x03		/* Fine L2 */
1969960ac47SOlivier Houchard #define	L1_TYPE_MASK	0x03		/* mask of type bits */
1979960ac47SOlivier Houchard 
1989960ac47SOlivier Houchard #define	l1pte_section_p(pde)	(((pde) & L1_TYPE_MASK) == L1_TYPE_S)
1999960ac47SOlivier Houchard #define	l1pte_valid(pde)	((pde) != 0)
2009960ac47SOlivier Houchard #define	l2pte_valid(pte)	((pte) != 0)
2019960ac47SOlivier Houchard #define l2pte_index(v)		(((v) & L2_ADDR_BITS) >> L2_S_SHIFT)
2029960ac47SOlivier Houchard 
2039960ac47SOlivier Houchard 
2048cffa1b4SOlivier Houchard int
2059960ac47SOlivier Houchard _kvm_kvatop(kvm_t *kd, u_long va, off_t *pa)
2068cffa1b4SOlivier Houchard {
2079960ac47SOlivier Houchard 	u_long offset = va & (PAGE_SIZE - 1);
2089960ac47SOlivier Houchard 	struct vmstate *vm = kd->vmst;
2099960ac47SOlivier Houchard 	pd_entry_t pd;
2109960ac47SOlivier Houchard 	pt_entry_t pte;
2119960ac47SOlivier Houchard 	u_long pte_pa;
2128cffa1b4SOlivier Houchard 
2138e321b79SRafal Jaworowski 	if (kd->vmst->minidump)
2148e321b79SRafal Jaworowski 		return (_kvm_minidump_kvatop(kd, va, pa));
2158e321b79SRafal Jaworowski 
2169960ac47SOlivier Houchard 	if (vm->l1pt == NULL)
2179960ac47SOlivier Houchard 		return (_kvm_pa2off(kd, va, pa, PAGE_SIZE));
2189960ac47SOlivier Houchard 	pd = vm->l1pt[L1_IDX(va)];
2199960ac47SOlivier Houchard 	if (!l1pte_valid(pd))
2209960ac47SOlivier Houchard 		goto invalid;
2219960ac47SOlivier Houchard 	if (l1pte_section_p(pd)) {
2229960ac47SOlivier Houchard 		/* 1MB section mapping. */
2239960ac47SOlivier Houchard 		*pa = ((u_long)pd & L1_S_ADDR_MASK) + (va & L1_S_OFFSET);
2249960ac47SOlivier Houchard 		return  (_kvm_pa2off(kd, *pa, pa, L1_S_SIZE));
2259960ac47SOlivier Houchard 	}
2269960ac47SOlivier Houchard 	pte_pa = (pd & L1_ADDR_MASK) + l2pte_index(va) * sizeof(pte);
2279960ac47SOlivier Houchard 	_kvm_pa2off(kd, pte_pa, (off_t *)&pte_pa, L1_S_SIZE);
2289960ac47SOlivier Houchard 	if (lseek(kd->pmfd, pte_pa, 0) == -1) {
2299960ac47SOlivier Houchard 		_kvm_syserr(kd, kd->program, "_kvm_kvatop: lseek");
2309960ac47SOlivier Houchard 		goto invalid;
2319960ac47SOlivier Houchard 	}
2329960ac47SOlivier Houchard 	if (read(kd->pmfd, &pte, sizeof(pte)) != sizeof (pte)) {
2339960ac47SOlivier Houchard 		_kvm_syserr(kd, kd->program, "_kvm_kvatop: read");
2349960ac47SOlivier Houchard 		goto invalid;
2359960ac47SOlivier Houchard 	}
2369960ac47SOlivier Houchard 	if (!l2pte_valid(pte)) {
2379960ac47SOlivier Houchard 		goto invalid;
2389960ac47SOlivier Houchard 	}
2399960ac47SOlivier Houchard 	if ((pte & L2_TYPE_MASK) == L2_TYPE_L) {
2409960ac47SOlivier Houchard 		*pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
2419960ac47SOlivier Houchard 		return (_kvm_pa2off(kd, *pa, pa, L2_L_SIZE));
2429960ac47SOlivier Houchard 	}
2439960ac47SOlivier Houchard 	*pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
2449960ac47SOlivier Houchard 	return (_kvm_pa2off(kd, *pa, pa, PAGE_SIZE));
2459960ac47SOlivier Houchard invalid:
2469960ac47SOlivier Houchard 	_kvm_err(kd, 0, "Invalid address (%x)", va);
2478cffa1b4SOlivier Houchard 	return 0;
2488cffa1b4SOlivier Houchard }
2498cffa1b4SOlivier Houchard 
2508cffa1b4SOlivier Houchard /*
2518cffa1b4SOlivier Houchard  * Machine-dependent initialization for ALL open kvm descriptors,
2528cffa1b4SOlivier Houchard  * not just those for a kernel crash dump.  Some architectures
2538cffa1b4SOlivier Houchard  * have to deal with these NOT being constants!  (i.e. m68k)
2548cffa1b4SOlivier Houchard  */
2558cffa1b4SOlivier Houchard int
2568cffa1b4SOlivier Houchard _kvm_mdopen(kd)
2578cffa1b4SOlivier Houchard 	kvm_t	*kd;
2588cffa1b4SOlivier Houchard {
2598cffa1b4SOlivier Houchard 
2608cffa1b4SOlivier Houchard #ifdef FBSD_NOT_YET
2618cffa1b4SOlivier Houchard 	kd->usrstack = USRSTACK;
2628cffa1b4SOlivier Houchard 	kd->min_uva = VM_MIN_ADDRESS;
2638cffa1b4SOlivier Houchard 	kd->max_uva = VM_MAXUSER_ADDRESS;
2648cffa1b4SOlivier Houchard #endif
2658cffa1b4SOlivier Houchard 
2668cffa1b4SOlivier Houchard 	return (0);
2678cffa1b4SOlivier Houchard }
268