Home | History | Annotate | Line # | Download | only in libkvm
kvm_aarch64.c revision 1.3
      1  1.3   ryo /* $NetBSD: kvm_aarch64.c,v 1.3 2018/04/01 04:35:02 ryo Exp $ */
      2  1.1  matt 
      3  1.1  matt /*-
      4  1.1  matt  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  1.1  matt  * All rights reserved.
      6  1.1  matt  *
      7  1.1  matt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  matt  * by Matt Thomas of 3am Software Foundry.
      9  1.1  matt  *
     10  1.1  matt  * Redistribution and use in source and binary forms, with or without
     11  1.1  matt  * modification, are permitted provided that the following conditions
     12  1.1  matt  * are met:
     13  1.1  matt  * 1. Redistributions of source code must retain the above copyright
     14  1.1  matt  *    notice, this list of conditions and the following disclaimer.
     15  1.1  matt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  matt  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  matt  *    documentation and/or other materials provided with the distribution.
     18  1.1  matt  *
     19  1.1  matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  matt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  matt  */
     31  1.1  matt 
     32  1.1  matt #include <sys/param.h>
     33  1.1  matt #include <sys/proc.h>
     34  1.1  matt #include <sys/stat.h>
     35  1.1  matt #include <sys/kcore.h>
     36  1.1  matt #include <sys/types.h>
     37  1.1  matt #include <unistd.h>
     38  1.1  matt #include <nlist.h>
     39  1.1  matt #include <kvm.h>
     40  1.1  matt 
     41  1.1  matt #include <machine/kcore.h>
     42  1.1  matt #include <machine/pte.h>
     43  1.1  matt #include <machine/vmparam.h>
     44  1.1  matt 
     45  1.1  matt #include <limits.h>
     46  1.1  matt #include <db.h>
     47  1.1  matt #include <stdlib.h>
     48  1.1  matt 
     49  1.1  matt #include "kvm_private.h"
     50  1.1  matt 
     51  1.3   ryo __RCSID("$NetBSD: kvm_aarch64.c,v 1.3 2018/04/01 04:35:02 ryo Exp $");
     52  1.1  matt 
     53  1.1  matt /*ARGSUSED*/
     54  1.1  matt void
     55  1.1  matt _kvm_freevtop(kvm_t *kd)
     56  1.1  matt {
     57  1.1  matt 	return;
     58  1.1  matt }
     59  1.1  matt 
     60  1.1  matt /*ARGSUSED*/
     61  1.1  matt int
     62  1.1  matt _kvm_initvtop(kvm_t *kd)
     63  1.1  matt {
     64  1.1  matt 	return (0);
     65  1.1  matt }
     66  1.1  matt 
     67  1.1  matt int
     68  1.1  matt _kvm_kvatop(kvm_t *kd, vaddr_t va, paddr_t *pa)
     69  1.1  matt {
     70  1.1  matt         if (ISALIVE(kd)) {
     71  1.1  matt                 _kvm_err(kd, 0, "vatop called in live kernel!");
     72  1.1  matt                 return(0);
     73  1.1  matt         }
     74  1.1  matt 
     75  1.1  matt 	if ((va & AARCH64_KSEG_MASK) != AARCH64_KSEG_START) {
     76  1.1  matt 		/*
     77  1.1  matt 		 * Bogus address (not in KV space): punt.
     78  1.1  matt 		 */
     79  1.1  matt 		_kvm_err(kd, 0, "invalid kernel virtual address");
     80  1.1  matt lose:
     81  1.1  matt 		*pa = -1;
     82  1.1  matt 		return 0;
     83  1.1  matt 	}
     84  1.1  matt 
     85  1.1  matt 	const cpu_kcore_hdr_t * const cpu_kh = kd->cpu_data;
     86  1.1  matt 	const u_int tg1 =__SHIFTOUT(cpu_kh->kh_tcr1, TCR_TG1);
     87  1.1  matt 	const u_int t1siz = __SHIFTOUT(cpu_kh->kh_tcr1, TCR_T1SZ);
     88  1.1  matt 
     89  1.1  matt 	/*
     90  1.1  matt 	 * Real kernel virtual address: do the translation.
     91  1.1  matt 	 */
     92  1.1  matt 
     93  1.1  matt 	u_int va_bits;
     94  1.1  matt 	u_int page_shift;
     95  1.1  matt 
     96  1.1  matt 	switch (tg1) {
     97  1.3   ryo 	case TCR_TG1_4KB:
     98  1.1  matt 		va_bits = t1siz + 36;
     99  1.1  matt 		page_shift = 12;
    100  1.1  matt 		break;
    101  1.3   ryo 	case TCR_TG1_16KB:
    102  1.1  matt 		va_bits = 48;
    103  1.1  matt 		page_shift = 14;
    104  1.1  matt 		break;
    105  1.3   ryo 	case TCR_TG1_64KB:
    106  1.1  matt 		va_bits = t1siz + 38;
    107  1.1  matt 		page_shift = 16;
    108  1.1  matt 		break;
    109  1.1  matt 	default:
    110  1.1  matt 		goto lose;
    111  1.1  matt 	}
    112  1.1  matt 
    113  1.1  matt 	const size_t page_size = 1 << page_shift;
    114  1.1  matt 	const uint64_t page_mask = (page_size - 1);
    115  1.1  matt 	const uint64_t page_addr = __BITS(47, 0) & ~page_mask;
    116  1.1  matt 	const uint64_t pte_mask = page_mask >> 3;
    117  1.1  matt 	const u_int pte_shift = page_shift - 3;
    118  1.1  matt 
    119  1.1  matt 	/* how many level of page tables do we have? */
    120  1.1  matt 	u_int level = (48 + page_shift - 1) / page_shift;
    121  1.1  matt 
    122  1.1  matt 	/* restrict va to the valid VA bits */
    123  1.1  matt 	va &= (1LL << va_bits) - 1;
    124  1.1  matt 
    125  1.1  matt 	u_int addr_shift = page_shift + (level - 1) * pte_shift;
    126  1.1  matt 
    127  1.1  matt 	/* clear out the unused low bits of the table address */
    128  1.1  matt 	paddr_t pte_addr = (cpu_kh->kh_ttbr1 & TTBR_BADDR);
    129  1.1  matt 	pte_addr &= ~((8L << (va_bits - addr_shift)) - 1);
    130  1.1  matt 
    131  1.1  matt 	for (;;) {
    132  1.1  matt 		pt_entry_t pte;
    133  1.1  matt 
    134  1.1  matt 		/* now index into the pte table */
    135  1.1  matt 		pte_addr += 8 * ((va >> addr_shift) & pte_mask);
    136  1.1  matt 
    137  1.1  matt 		/* Find and read the PTE. */
    138  1.1  matt 		if (_kvm_pread(kd, kd->pmfd, &pte, sizeof(pte),
    139  1.1  matt 		    _kvm_pa2off(kd, pte_addr)) != sizeof(pte)) {
    140  1.1  matt 			_kvm_syserr(kd, 0, "could not read pte");
    141  1.1  matt 			goto lose;
    142  1.1  matt 		}
    143  1.1  matt 
    144  1.1  matt 		/* Find and read the L2 PTE. */
    145  1.1  matt 		if ((pte & LX_VALID) == 0) {
    146  1.1  matt 			_kvm_err(kd, 0, "invalid translation (invalid pte)");
    147  1.1  matt 			goto lose;
    148  1.1  matt 		}
    149  1.1  matt 
    150  1.1  matt 		if ((pte & LX_TYPE) == LX_TYPE_BLK) {
    151  1.1  matt 			const paddr_t blk_mask = ((1L << addr_shift) - 1);
    152  1.1  matt 
    153  1.1  matt 			*pa = (pte & page_addr & ~blk_mask) | (va & blk_mask);
    154  1.1  matt 			return 0;
    155  1.1  matt 		}
    156  1.1  matt 
    157  1.1  matt 		if (level == page_shift) {
    158  1.1  matt 			*pa = (pte & page_addr) | (va & page_mask);
    159  1.1  matt 			return 0;
    160  1.1  matt 		}
    161  1.1  matt 
    162  1.1  matt 		/*
    163  1.1  matt 		 * Read next level of page table
    164  1.1  matt 		 */
    165  1.1  matt 
    166  1.1  matt 		pte_addr = pte & page_addr;
    167  1.1  matt 		addr_shift -= pte_shift;
    168  1.1  matt 	}
    169  1.1  matt }
    170  1.1  matt 
    171  1.1  matt /*
    172  1.1  matt  * Translate a physical address to a file-offset in the crash dump.
    173  1.1  matt  */
    174  1.1  matt off_t
    175  1.1  matt _kvm_pa2off(kvm_t *kd, paddr_t pa)
    176  1.1  matt {
    177  1.1  matt 	const cpu_kcore_hdr_t * const cpu_kh = kd->cpu_data;
    178  1.1  matt 	off_t off = 0;
    179  1.1  matt 
    180  1.1  matt 	for (const phys_ram_seg_t *ramsegs = cpu_kh->kh_ramsegs;
    181  1.1  matt 	     ramsegs->size != 0; ramsegs++) {
    182  1.1  matt 		if (pa >= ramsegs->start
    183  1.1  matt 		    && pa < ramsegs->start + ramsegs->size) {
    184  1.1  matt 			off += pa - ramsegs->start;
    185  1.1  matt 			break;
    186  1.1  matt 		}
    187  1.1  matt 		off += ramsegs->size;
    188  1.1  matt 	}
    189  1.1  matt 
    190  1.1  matt 	return kd->dump_off + off;
    191  1.1  matt }
    192  1.1  matt 
    193  1.1  matt /*
    194  1.1  matt  * Machine-dependent initialization for ALL open kvm descriptors,
    195  1.1  matt  * not just those for a kernel crash dump.  Some architectures
    196  1.1  matt  * have to deal with these NOT being constants!  (i.e. m68k)
    197  1.1  matt  */
    198  1.1  matt int
    199  1.1  matt _kvm_mdopen(kvm_t *kd)
    200  1.1  matt {
    201  1.1  matt 
    202  1.1  matt 	kd->usrstack = USRSTACK;
    203  1.1  matt 	kd->min_uva = VM_MIN_ADDRESS;
    204  1.1  matt 	kd->max_uva = VM_MAXUSER_ADDRESS;
    205  1.1  matt 
    206  1.1  matt 	return (0);
    207  1.1  matt }
    208