[BACK]Return to kvm_aarch64.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libkvm

Annotation of src/lib/libkvm/kvm_aarch64.c, Revision 1.6

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

CVSweb <webmaster@jp.NetBSD.org>