xref: /freebsd/lib/libkvm/kvm_arm.c (revision 9960ac47e9b6e0a7e5239ebc87dd3990f3daa130)
18cffa1b4SOlivier Houchard /*	$NetBSD: kvm_powerpc.c,v 1.4 1998/02/03 06:50:07 mycroft Exp $	*/
28cffa1b4SOlivier Houchard 
38cffa1b4SOlivier Houchard /*-
48cffa1b4SOlivier Houchard  * Copyright (C) 1996 Wolfgang Solfrank.
58cffa1b4SOlivier Houchard  * Copyright (C) 1996 TooLs GmbH.
68cffa1b4SOlivier Houchard  * All rights reserved.
78cffa1b4SOlivier Houchard  *
88cffa1b4SOlivier Houchard  * Redistribution and use in source and binary forms, with or without
98cffa1b4SOlivier Houchard  * modification, are permitted provided that the following conditions
108cffa1b4SOlivier Houchard  * are met:
118cffa1b4SOlivier Houchard  * 1. Redistributions of source code must retain the above copyright
128cffa1b4SOlivier Houchard  *    notice, this list of conditions and the following disclaimer.
138cffa1b4SOlivier Houchard  * 2. Redistributions in binary form must reproduce the above copyright
148cffa1b4SOlivier Houchard  *    notice, this list of conditions and the following disclaimer in the
158cffa1b4SOlivier Houchard  *    documentation and/or other materials provided with the distribution.
168cffa1b4SOlivier Houchard  * 3. All advertising materials mentioning features or use of this software
178cffa1b4SOlivier Houchard  *    must display the following acknowledgement:
188cffa1b4SOlivier Houchard  *	This product includes software developed by TooLs GmbH.
198cffa1b4SOlivier Houchard  * 4. The name of TooLs GmbH may not be used to endorse or promote products
208cffa1b4SOlivier Houchard  *    derived from this software 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>
558cffa1b4SOlivier Houchard 
568cffa1b4SOlivier Houchard #include "kvm_private.h"
578cffa1b4SOlivier Houchard 
589960ac47SOlivier Houchard struct vmstate {
599960ac47SOlivier Houchard 	pd_entry_t *l1pt;
609960ac47SOlivier Houchard 	void *mmapbase;
619960ac47SOlivier Houchard 	size_t mmapsize;
629960ac47SOlivier Houchard };
639960ac47SOlivier Houchard 
649960ac47SOlivier Houchard static int
659960ac47SOlivier Houchard _kvm_maphdrs(kvm_t *kd, size_t sz)
669960ac47SOlivier Houchard {
679960ac47SOlivier Houchard 	struct vmstate *vm = kd->vmst;
689960ac47SOlivier Houchard 
699960ac47SOlivier Houchard 	/* munmap() previous mmap(). */
709960ac47SOlivier Houchard 	if (vm->mmapbase != NULL) {
719960ac47SOlivier Houchard 		munmap(vm->mmapbase, vm->mmapsize);
729960ac47SOlivier Houchard 		vm->mmapbase = NULL;
739960ac47SOlivier Houchard 	}
749960ac47SOlivier Houchard 
759960ac47SOlivier Houchard 	vm->mmapsize = sz;
769960ac47SOlivier Houchard 	vm->mmapbase = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
779960ac47SOlivier Houchard 	if (vm->mmapbase == MAP_FAILED) {
789960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "cannot mmap corefile");
799960ac47SOlivier Houchard 		return (-1);
809960ac47SOlivier Houchard 	}
819960ac47SOlivier Houchard 
829960ac47SOlivier Houchard 	return (0);
839960ac47SOlivier Houchard }
849960ac47SOlivier Houchard 
859960ac47SOlivier Houchard /*
869960ac47SOlivier Houchard  * Translate a physical memory address to a file-offset in the crash-dump.
879960ac47SOlivier Houchard  */
889960ac47SOlivier Houchard static size_t
899960ac47SOlivier Houchard _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs, size_t pgsz)
909960ac47SOlivier Houchard {
919960ac47SOlivier Houchard 	Elf32_Ehdr *e = kd->vmst->mmapbase;
929960ac47SOlivier Houchard 	Elf32_Phdr *p = (Elf32_Phdr*)((char*)e + e->e_phoff);
939960ac47SOlivier Houchard 	int n = e->e_phnum;
949960ac47SOlivier Houchard 
959960ac47SOlivier Houchard 	while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz))
969960ac47SOlivier Houchard 		p++, n--;
979960ac47SOlivier Houchard 	if (n == 0)
989960ac47SOlivier Houchard 		return (0);
999960ac47SOlivier Houchard 
1009960ac47SOlivier Houchard 	*ofs = (pa - p->p_paddr) + p->p_offset;
1019960ac47SOlivier Houchard 	if (pgsz == 0)
1029960ac47SOlivier Houchard 		return (p->p_memsz - (pa - p->p_paddr));
1039960ac47SOlivier Houchard 	return (pgsz - ((size_t)pa & (pgsz - 1)));
1049960ac47SOlivier Houchard }
1059960ac47SOlivier Houchard 
1068cffa1b4SOlivier Houchard void
1079960ac47SOlivier Houchard _kvm_freevtop(kvm_t *kd)
1088cffa1b4SOlivier Houchard {
1099960ac47SOlivier Houchard 	if (kd->vmst != 0) {
1109960ac47SOlivier Houchard 		if (kd->vmst->mmapbase != NULL)
1119960ac47SOlivier Houchard 			munmap(kd->vmst->mmapbase, kd->vmst->mmapsize);
1128cffa1b4SOlivier Houchard 		free(kd->vmst);
1139960ac47SOlivier Houchard 		kd->vmst = NULL;
1149960ac47SOlivier Houchard 	}
1158cffa1b4SOlivier Houchard }
1168cffa1b4SOlivier Houchard 
1178cffa1b4SOlivier Houchard int
1189960ac47SOlivier Houchard _kvm_initvtop(kvm_t *kd)
1198cffa1b4SOlivier Houchard {
1209960ac47SOlivier Houchard 	struct vmstate *vm = _kvm_malloc(kd, sizeof(*vm));
1219960ac47SOlivier Houchard 	struct nlist nlist[2];
1229960ac47SOlivier Houchard 	u_long kernbase, physaddr, pa;
1239960ac47SOlivier Houchard 	pd_entry_t *l1pt;
1249960ac47SOlivier Houchard 	Elf32_Ehdr *ehdr;
1259960ac47SOlivier Houchard 	size_t hdrsz;
1269960ac47SOlivier Houchard 
1279960ac47SOlivier Houchard 	if (vm == 0) {
1289960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "cannot allocate vm");
1299960ac47SOlivier Houchard 		return (-1);
1309960ac47SOlivier Houchard 	}
1319960ac47SOlivier Houchard 	kd->vmst = vm;
1329960ac47SOlivier Houchard 	vm->l1pt = NULL;
1339960ac47SOlivier Houchard 	if (_kvm_maphdrs(kd, sizeof(Elf32_Ehdr)) == -1)
1349960ac47SOlivier Houchard 		return (-1);
1359960ac47SOlivier Houchard 	ehdr = kd->vmst->mmapbase;
1369960ac47SOlivier Houchard 	hdrsz = ehdr->e_phoff + ehdr->e_phentsize * ehdr->e_phnum;
1379960ac47SOlivier Houchard 	if (_kvm_maphdrs(kd, hdrsz) == -1)
1389960ac47SOlivier Houchard 		return (-1);
1399960ac47SOlivier Houchard 	nlist[0].n_name = "kernbase";
1409960ac47SOlivier Houchard 	nlist[1].n_name = NULL;
1419960ac47SOlivier Houchard 	if (kvm_nlist(kd, nlist) != 0)
1429960ac47SOlivier Houchard 		kernbase = KERNBASE;
1439960ac47SOlivier Houchard 	else
1449960ac47SOlivier Houchard 		kernbase = nlist[0].n_value;
1459960ac47SOlivier Houchard 
1469960ac47SOlivier Houchard 	nlist[0].n_name = "physaddr";
1479960ac47SOlivier Houchard 	if (kvm_nlist(kd, nlist) != 0) {
1489960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "couldn't get phys addr");
1499960ac47SOlivier Houchard 		return (-1);
1509960ac47SOlivier Houchard 	}
1519960ac47SOlivier Houchard 	physaddr = nlist[0].n_value;
1529960ac47SOlivier Houchard 	nlist[0].n_name = "kernel_l1pa";
1539960ac47SOlivier Houchard 	if (kvm_nlist(kd, nlist) != 0) {
1549960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "bad namelist");
1559960ac47SOlivier Houchard 		return (-1);
1569960ac47SOlivier Houchard 	}
1579960ac47SOlivier Houchard 	if (kvm_read(kd, (nlist[0].n_value - kernbase + physaddr), &pa,
1589960ac47SOlivier Houchard 	    sizeof(pa)) != sizeof(pa)) {
1599960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "cannot read kernel_l1pa");
1609960ac47SOlivier Houchard 		return (-1);
1619960ac47SOlivier Houchard 	}
1629960ac47SOlivier Houchard 	l1pt = _kvm_malloc(kd, L1_TABLE_SIZE);
1639960ac47SOlivier Houchard 	if (kvm_read(kd, pa, l1pt, L1_TABLE_SIZE) != L1_TABLE_SIZE) {
1649960ac47SOlivier Houchard 		_kvm_err(kd, kd->program, "cannot read l1pt");
1659960ac47SOlivier Houchard 		free(l1pt);
1669960ac47SOlivier Houchard 		return (-1);
1679960ac47SOlivier Houchard 	}
1689960ac47SOlivier Houchard 	vm->l1pt = l1pt;
1698cffa1b4SOlivier Houchard 	return 0;
1708cffa1b4SOlivier Houchard }
1718cffa1b4SOlivier Houchard 
1729960ac47SOlivier Houchard /* from arm/pmap.c */
1739960ac47SOlivier Houchard #define	L1_IDX(va)		(((vm_offset_t)(va)) >> L1_S_SHIFT)
1749960ac47SOlivier Houchard /* from arm/pmap.h */
1759960ac47SOlivier Houchard #define	L1_TYPE_INV	0x00		/* Invalid (fault) */
1769960ac47SOlivier Houchard #define	L1_TYPE_C	0x01		/* Coarse L2 */
1779960ac47SOlivier Houchard #define	L1_TYPE_S	0x02		/* Section */
1789960ac47SOlivier Houchard #define	L1_TYPE_F	0x03		/* Fine L2 */
1799960ac47SOlivier Houchard #define	L1_TYPE_MASK	0x03		/* mask of type bits */
1809960ac47SOlivier Houchard 
1819960ac47SOlivier Houchard #define	l1pte_section_p(pde)	(((pde) & L1_TYPE_MASK) == L1_TYPE_S)
1829960ac47SOlivier Houchard #define	l1pte_valid(pde)	((pde) != 0)
1839960ac47SOlivier Houchard #define	l2pte_valid(pte)	((pte) != 0)
1849960ac47SOlivier Houchard #define l2pte_index(v)		(((v) & L2_ADDR_BITS) >> L2_S_SHIFT)
1859960ac47SOlivier Houchard 
1869960ac47SOlivier Houchard 
1878cffa1b4SOlivier Houchard int
1889960ac47SOlivier Houchard _kvm_kvatop(kvm_t *kd, u_long va, off_t *pa)
1898cffa1b4SOlivier Houchard {
1909960ac47SOlivier Houchard 	u_long offset = va & (PAGE_SIZE - 1);
1919960ac47SOlivier Houchard 	struct vmstate *vm = kd->vmst;
1929960ac47SOlivier Houchard 	pd_entry_t pd;
1939960ac47SOlivier Houchard 	pt_entry_t pte;
1949960ac47SOlivier Houchard 	u_long pte_pa;
1958cffa1b4SOlivier Houchard 
1969960ac47SOlivier Houchard 	if (vm->l1pt == NULL)
1979960ac47SOlivier Houchard 		return (_kvm_pa2off(kd, va, pa, PAGE_SIZE));
1989960ac47SOlivier Houchard 	pd = vm->l1pt[L1_IDX(va)];
1999960ac47SOlivier Houchard 	if (!l1pte_valid(pd))
2009960ac47SOlivier Houchard 		goto invalid;
2019960ac47SOlivier Houchard 	if (l1pte_section_p(pd)) {
2029960ac47SOlivier Houchard 		/* 1MB section mapping. */
2039960ac47SOlivier Houchard 		*pa = ((u_long)pd & L1_S_ADDR_MASK) + (va & L1_S_OFFSET);
2049960ac47SOlivier Houchard 		return  (_kvm_pa2off(kd, *pa, pa, L1_S_SIZE));
2059960ac47SOlivier Houchard 	}
2069960ac47SOlivier Houchard 	pte_pa = (pd & L1_ADDR_MASK) + l2pte_index(va) * sizeof(pte);
2079960ac47SOlivier Houchard 	_kvm_pa2off(kd, pte_pa, (off_t *)&pte_pa, L1_S_SIZE);
2089960ac47SOlivier Houchard 	if (lseek(kd->pmfd, pte_pa, 0) == -1) {
2099960ac47SOlivier Houchard 		_kvm_syserr(kd, kd->program, "_kvm_kvatop: lseek");
2109960ac47SOlivier Houchard 		goto invalid;
2119960ac47SOlivier Houchard 	}
2129960ac47SOlivier Houchard 	if (read(kd->pmfd, &pte, sizeof(pte)) != sizeof (pte)) {
2139960ac47SOlivier Houchard 		_kvm_syserr(kd, kd->program, "_kvm_kvatop: read");
2149960ac47SOlivier Houchard 		goto invalid;
2159960ac47SOlivier Houchard 	}
2169960ac47SOlivier Houchard 	if (!l2pte_valid(pte)) {
2179960ac47SOlivier Houchard 		goto invalid;
2189960ac47SOlivier Houchard 	}
2199960ac47SOlivier Houchard 	if ((pte & L2_TYPE_MASK) == L2_TYPE_L) {
2209960ac47SOlivier Houchard 		*pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
2219960ac47SOlivier Houchard 		return (_kvm_pa2off(kd, *pa, pa, L2_L_SIZE));
2229960ac47SOlivier Houchard 	}
2239960ac47SOlivier Houchard 	*pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
2249960ac47SOlivier Houchard 	return (_kvm_pa2off(kd, *pa, pa, PAGE_SIZE));
2259960ac47SOlivier Houchard invalid:
2269960ac47SOlivier Houchard 	_kvm_err(kd, 0, "Invalid address (%x)", va);
2278cffa1b4SOlivier Houchard 	return 0;
2288cffa1b4SOlivier Houchard }
2298cffa1b4SOlivier Houchard 
2308cffa1b4SOlivier Houchard /*
2318cffa1b4SOlivier Houchard  * Machine-dependent initialization for ALL open kvm descriptors,
2328cffa1b4SOlivier Houchard  * not just those for a kernel crash dump.  Some architectures
2338cffa1b4SOlivier Houchard  * have to deal with these NOT being constants!  (i.e. m68k)
2348cffa1b4SOlivier Houchard  */
2358cffa1b4SOlivier Houchard int
2368cffa1b4SOlivier Houchard _kvm_mdopen(kd)
2378cffa1b4SOlivier Houchard 	kvm_t	*kd;
2388cffa1b4SOlivier Houchard {
2398cffa1b4SOlivier Houchard 
2408cffa1b4SOlivier Houchard #ifdef FBSD_NOT_YET
2418cffa1b4SOlivier Houchard 	kd->usrstack = USRSTACK;
2428cffa1b4SOlivier Houchard 	kd->min_uva = VM_MIN_ADDRESS;
2438cffa1b4SOlivier Houchard 	kd->max_uva = VM_MAXUSER_ADDRESS;
2448cffa1b4SOlivier Houchard #endif
2458cffa1b4SOlivier Houchard 
2468cffa1b4SOlivier Houchard 	return (0);
2478cffa1b4SOlivier Houchard }
248