xref: /freebsd/lib/libkvm/kvm_arm.c (revision 953e4134b5b3948e32f9f0066d2f5cc8d7146c9c)
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>
55953e4134SEd Schouten #include <string.h>
568e321b79SRafal Jaworowski #include <unistd.h>
578cffa1b4SOlivier Houchard 
588cffa1b4SOlivier Houchard #include "kvm_private.h"
598cffa1b4SOlivier Houchard 
608e321b79SRafal Jaworowski /* minidump must be the first item! */
619960ac47SOlivier Houchard struct vmstate {
628e321b79SRafal Jaworowski 	int minidump;		/* 1 = minidump mode */
639960ac47SOlivier Houchard 	pd_entry_t *l1pt;
649960ac47SOlivier Houchard 	void *mmapbase;
659960ac47SOlivier Houchard 	size_t mmapsize;
669960ac47SOlivier Houchard };
679960ac47SOlivier Houchard 
689960ac47SOlivier Houchard static int
699960ac47SOlivier Houchard _kvm_maphdrs(kvm_t *kd, size_t sz)
709960ac47SOlivier Houchard {
719960ac47SOlivier Houchard 	struct vmstate *vm = kd->vmst;
729960ac47SOlivier Houchard 
739960ac47SOlivier Houchard 	/* munmap() previous mmap(). */
749960ac47SOlivier Houchard 	if (vm->mmapbase != NULL) {
759960ac47SOlivier Houchard 		munmap(vm->mmapbase, vm->mmapsize);
769960ac47SOlivier Houchard 		vm->mmapbase = NULL;
779960ac47SOlivier Houchard 	}
789960ac47SOlivier Houchard 
799960ac47SOlivier Houchard 	vm->mmapsize = sz;
809960ac47SOlivier Houchard 	vm->mmapbase = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
819960ac47SOlivier Houchard 	if (vm->mmapbase == MAP_FAILED) {
829960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "cannot mmap corefile");
839960ac47SOlivier Houchard 		return (-1);
849960ac47SOlivier Houchard 	}
859960ac47SOlivier Houchard 
869960ac47SOlivier Houchard 	return (0);
879960ac47SOlivier Houchard }
889960ac47SOlivier Houchard 
899960ac47SOlivier Houchard /*
909960ac47SOlivier Houchard  * Translate a physical memory address to a file-offset in the crash-dump.
919960ac47SOlivier Houchard  */
929960ac47SOlivier Houchard static size_t
939960ac47SOlivier Houchard _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs, size_t pgsz)
949960ac47SOlivier Houchard {
959960ac47SOlivier Houchard 	Elf32_Ehdr *e = kd->vmst->mmapbase;
969960ac47SOlivier Houchard 	Elf32_Phdr *p = (Elf32_Phdr*)((char*)e + e->e_phoff);
979960ac47SOlivier Houchard 	int n = e->e_phnum;
989960ac47SOlivier Houchard 
999960ac47SOlivier Houchard 	while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz))
1009960ac47SOlivier Houchard 		p++, n--;
1019960ac47SOlivier Houchard 	if (n == 0)
1029960ac47SOlivier Houchard 		return (0);
1039960ac47SOlivier Houchard 
1049960ac47SOlivier Houchard 	*ofs = (pa - p->p_paddr) + p->p_offset;
1059960ac47SOlivier Houchard 	if (pgsz == 0)
1069960ac47SOlivier Houchard 		return (p->p_memsz - (pa - p->p_paddr));
1079960ac47SOlivier Houchard 	return (pgsz - ((size_t)pa & (pgsz - 1)));
1089960ac47SOlivier Houchard }
1099960ac47SOlivier Houchard 
1108cffa1b4SOlivier Houchard void
1119960ac47SOlivier Houchard _kvm_freevtop(kvm_t *kd)
1128cffa1b4SOlivier Houchard {
1139960ac47SOlivier Houchard 	if (kd->vmst != 0) {
1148e321b79SRafal Jaworowski 		if (kd->vmst->minidump)
1158e321b79SRafal Jaworowski 			return (_kvm_minidump_freevtop(kd));
1169960ac47SOlivier Houchard 		if (kd->vmst->mmapbase != NULL)
1179960ac47SOlivier Houchard 			munmap(kd->vmst->mmapbase, kd->vmst->mmapsize);
1188cffa1b4SOlivier Houchard 		free(kd->vmst);
1199960ac47SOlivier Houchard 		kd->vmst = NULL;
1209960ac47SOlivier Houchard 	}
1218cffa1b4SOlivier Houchard }
1228cffa1b4SOlivier Houchard 
1238cffa1b4SOlivier Houchard int
1249960ac47SOlivier Houchard _kvm_initvtop(kvm_t *kd)
1258cffa1b4SOlivier Houchard {
1268e321b79SRafal Jaworowski 	struct vmstate *vm;
1279960ac47SOlivier Houchard 	struct nlist nlist[2];
1289960ac47SOlivier Houchard 	u_long kernbase, physaddr, pa;
1299960ac47SOlivier Houchard 	pd_entry_t *l1pt;
1309960ac47SOlivier Houchard 	Elf32_Ehdr *ehdr;
1319960ac47SOlivier Houchard 	size_t hdrsz;
1328e321b79SRafal Jaworowski 	char minihdr[8];
1339960ac47SOlivier Houchard 
1348e321b79SRafal Jaworowski 	if (!kd->rawdump) {
1358e321b79SRafal Jaworowski 		if (pread(kd->pmfd, &minihdr, 8, 0) == 8) {
1368e321b79SRafal Jaworowski 			if (memcmp(&minihdr, "minidump", 8) == 0)
1378e321b79SRafal Jaworowski 				return (_kvm_minidump_initvtop(kd));
1388e321b79SRafal Jaworowski 		} else {
1398e321b79SRafal Jaworowski 			_kvm_err(kd, kd->program, "cannot read header");
1408e321b79SRafal Jaworowski 			return (-1);
1418e321b79SRafal Jaworowski 		}
1428e321b79SRafal Jaworowski 	}
1438e321b79SRafal Jaworowski 
1448e321b79SRafal Jaworowski 	vm = _kvm_malloc(kd, sizeof(*vm));
1459960ac47SOlivier Houchard 	if (vm == 0) {
1469960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "cannot allocate vm");
1479960ac47SOlivier Houchard 		return (-1);
1489960ac47SOlivier Houchard 	}
1499960ac47SOlivier Houchard 	kd->vmst = vm;
1509960ac47SOlivier Houchard 	vm->l1pt = NULL;
1519960ac47SOlivier Houchard 	if (_kvm_maphdrs(kd, sizeof(Elf32_Ehdr)) == -1)
1529960ac47SOlivier Houchard 		return (-1);
1539960ac47SOlivier Houchard 	ehdr = kd->vmst->mmapbase;
1549960ac47SOlivier Houchard 	hdrsz = ehdr->e_phoff + ehdr->e_phentsize * ehdr->e_phnum;
1559960ac47SOlivier Houchard 	if (_kvm_maphdrs(kd, hdrsz) == -1)
1569960ac47SOlivier Houchard 		return (-1);
1579960ac47SOlivier Houchard 	nlist[0].n_name = "kernbase";
1589960ac47SOlivier Houchard 	nlist[1].n_name = NULL;
1599960ac47SOlivier Houchard 	if (kvm_nlist(kd, nlist) != 0)
1609960ac47SOlivier Houchard 		kernbase = KERNBASE;
1619960ac47SOlivier Houchard 	else
1629960ac47SOlivier Houchard 		kernbase = nlist[0].n_value;
1639960ac47SOlivier Houchard 
1649960ac47SOlivier Houchard 	nlist[0].n_name = "physaddr";
1659960ac47SOlivier Houchard 	if (kvm_nlist(kd, nlist) != 0) {
1669960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "couldn't get phys addr");
1679960ac47SOlivier Houchard 		return (-1);
1689960ac47SOlivier Houchard 	}
1699960ac47SOlivier Houchard 	physaddr = nlist[0].n_value;
1709960ac47SOlivier Houchard 	nlist[0].n_name = "kernel_l1pa";
1719960ac47SOlivier Houchard 	if (kvm_nlist(kd, nlist) != 0) {
1729960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "bad namelist");
1739960ac47SOlivier Houchard 		return (-1);
1749960ac47SOlivier Houchard 	}
1759960ac47SOlivier Houchard 	if (kvm_read(kd, (nlist[0].n_value - kernbase + physaddr), &pa,
1769960ac47SOlivier Houchard 	    sizeof(pa)) != sizeof(pa)) {
1779960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "cannot read kernel_l1pa");
1789960ac47SOlivier Houchard 		return (-1);
1799960ac47SOlivier Houchard 	}
1809960ac47SOlivier Houchard 	l1pt = _kvm_malloc(kd, L1_TABLE_SIZE);
1819960ac47SOlivier Houchard 	if (kvm_read(kd, pa, l1pt, L1_TABLE_SIZE) != L1_TABLE_SIZE) {
1829960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "cannot read l1pt");
1839960ac47SOlivier Houchard 		free(l1pt);
1849960ac47SOlivier Houchard 		return (-1);
1859960ac47SOlivier Houchard 	}
1869960ac47SOlivier Houchard 	vm->l1pt = l1pt;
1878cffa1b4SOlivier Houchard 	return 0;
1888cffa1b4SOlivier Houchard }
1898cffa1b4SOlivier Houchard 
1909960ac47SOlivier Houchard /* from arm/pmap.c */
1919960ac47SOlivier Houchard #define	L1_IDX(va)		(((vm_offset_t)(va)) >> L1_S_SHIFT)
1929960ac47SOlivier Houchard /* from arm/pmap.h */
1939960ac47SOlivier Houchard #define	L1_TYPE_INV	0x00		/* Invalid (fault) */
1949960ac47SOlivier Houchard #define	L1_TYPE_C	0x01		/* Coarse L2 */
1959960ac47SOlivier Houchard #define	L1_TYPE_S	0x02		/* Section */
1969960ac47SOlivier Houchard #define	L1_TYPE_F	0x03		/* Fine L2 */
1979960ac47SOlivier Houchard #define	L1_TYPE_MASK	0x03		/* mask of type bits */
1989960ac47SOlivier Houchard 
1999960ac47SOlivier Houchard #define	l1pte_section_p(pde)	(((pde) & L1_TYPE_MASK) == L1_TYPE_S)
2009960ac47SOlivier Houchard #define	l1pte_valid(pde)	((pde) != 0)
2019960ac47SOlivier Houchard #define	l2pte_valid(pte)	((pte) != 0)
2029960ac47SOlivier Houchard #define l2pte_index(v)		(((v) & L2_ADDR_BITS) >> L2_S_SHIFT)
2039960ac47SOlivier Houchard 
2049960ac47SOlivier Houchard 
2058cffa1b4SOlivier Houchard int
2069960ac47SOlivier Houchard _kvm_kvatop(kvm_t *kd, u_long va, off_t *pa)
2078cffa1b4SOlivier Houchard {
2089960ac47SOlivier Houchard 	u_long offset = va & (PAGE_SIZE - 1);
2099960ac47SOlivier Houchard 	struct vmstate *vm = kd->vmst;
2109960ac47SOlivier Houchard 	pd_entry_t pd;
2119960ac47SOlivier Houchard 	pt_entry_t pte;
2129960ac47SOlivier Houchard 	u_long pte_pa;
2138cffa1b4SOlivier Houchard 
2148e321b79SRafal Jaworowski 	if (kd->vmst->minidump)
2158e321b79SRafal Jaworowski 		return (_kvm_minidump_kvatop(kd, va, pa));
2168e321b79SRafal Jaworowski 
2179960ac47SOlivier Houchard 	if (vm->l1pt == NULL)
2189960ac47SOlivier Houchard 		return (_kvm_pa2off(kd, va, pa, PAGE_SIZE));
2199960ac47SOlivier Houchard 	pd = vm->l1pt[L1_IDX(va)];
2209960ac47SOlivier Houchard 	if (!l1pte_valid(pd))
2219960ac47SOlivier Houchard 		goto invalid;
2229960ac47SOlivier Houchard 	if (l1pte_section_p(pd)) {
2239960ac47SOlivier Houchard 		/* 1MB section mapping. */
2249960ac47SOlivier Houchard 		*pa = ((u_long)pd & L1_S_ADDR_MASK) + (va & L1_S_OFFSET);
2259960ac47SOlivier Houchard 		return  (_kvm_pa2off(kd, *pa, pa, L1_S_SIZE));
2269960ac47SOlivier Houchard 	}
2279960ac47SOlivier Houchard 	pte_pa = (pd & L1_ADDR_MASK) + l2pte_index(va) * sizeof(pte);
2289960ac47SOlivier Houchard 	_kvm_pa2off(kd, pte_pa, (off_t *)&pte_pa, L1_S_SIZE);
2299960ac47SOlivier Houchard 	if (lseek(kd->pmfd, pte_pa, 0) == -1) {
2309960ac47SOlivier Houchard 		_kvm_syserr(kd, kd->program, "_kvm_kvatop: lseek");
2319960ac47SOlivier Houchard 		goto invalid;
2329960ac47SOlivier Houchard 	}
2339960ac47SOlivier Houchard 	if (read(kd->pmfd, &pte, sizeof(pte)) != sizeof (pte)) {
2349960ac47SOlivier Houchard 		_kvm_syserr(kd, kd->program, "_kvm_kvatop: read");
2359960ac47SOlivier Houchard 		goto invalid;
2369960ac47SOlivier Houchard 	}
2379960ac47SOlivier Houchard 	if (!l2pte_valid(pte)) {
2389960ac47SOlivier Houchard 		goto invalid;
2399960ac47SOlivier Houchard 	}
2409960ac47SOlivier Houchard 	if ((pte & L2_TYPE_MASK) == L2_TYPE_L) {
2419960ac47SOlivier Houchard 		*pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
2429960ac47SOlivier Houchard 		return (_kvm_pa2off(kd, *pa, pa, L2_L_SIZE));
2439960ac47SOlivier Houchard 	}
2449960ac47SOlivier Houchard 	*pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
2459960ac47SOlivier Houchard 	return (_kvm_pa2off(kd, *pa, pa, PAGE_SIZE));
2469960ac47SOlivier Houchard invalid:
2479960ac47SOlivier Houchard 	_kvm_err(kd, 0, "Invalid address (%x)", va);
2488cffa1b4SOlivier Houchard 	return 0;
2498cffa1b4SOlivier Houchard }
2508cffa1b4SOlivier Houchard 
2518cffa1b4SOlivier Houchard /*
2528cffa1b4SOlivier Houchard  * Machine-dependent initialization for ALL open kvm descriptors,
2538cffa1b4SOlivier Houchard  * not just those for a kernel crash dump.  Some architectures
2548cffa1b4SOlivier Houchard  * have to deal with these NOT being constants!  (i.e. m68k)
2558cffa1b4SOlivier Houchard  */
2568cffa1b4SOlivier Houchard int
2578cffa1b4SOlivier Houchard _kvm_mdopen(kd)
2588cffa1b4SOlivier Houchard 	kvm_t	*kd;
2598cffa1b4SOlivier Houchard {
2608cffa1b4SOlivier Houchard 
2618cffa1b4SOlivier Houchard #ifdef FBSD_NOT_YET
2628cffa1b4SOlivier Houchard 	kd->usrstack = USRSTACK;
2638cffa1b4SOlivier Houchard 	kd->min_uva = VM_MIN_ADDRESS;
2648cffa1b4SOlivier Houchard 	kd->max_uva = VM_MAXUSER_ADDRESS;
2658cffa1b4SOlivier Houchard #endif
2668cffa1b4SOlivier Houchard 
2678cffa1b4SOlivier Houchard 	return (0);
2688cffa1b4SOlivier Houchard }
269