1*d415763eSderaadt /* $OpenBSD: kvm_riscv64.c,v 1.3 2021/12/01 21:45:19 deraadt Exp $ */
296ef40f8Sdrahn /*
396ef40f8Sdrahn * Copyright (c) 2006 Miodrag Vallat.
496ef40f8Sdrahn *
596ef40f8Sdrahn * Permission to use, copy, modify, and distribute this software for any
696ef40f8Sdrahn * purpose with or without fee is hereby granted, provided that the above
796ef40f8Sdrahn * copyright notice, this permission notice, and the disclaimer below
896ef40f8Sdrahn * appear in all copies.
996ef40f8Sdrahn *
1096ef40f8Sdrahn * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1196ef40f8Sdrahn * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1296ef40f8Sdrahn * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1396ef40f8Sdrahn * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1496ef40f8Sdrahn * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1596ef40f8Sdrahn * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1696ef40f8Sdrahn * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1796ef40f8Sdrahn */
1896ef40f8Sdrahn /*-
1996ef40f8Sdrahn * Copyright (C) 1996 Wolfgang Solfrank.
2096ef40f8Sdrahn * Copyright (C) 1996 TooLs GmbH.
2196ef40f8Sdrahn * All rights reserved.
2296ef40f8Sdrahn *
2396ef40f8Sdrahn * Redistribution and use in source and binary forms, with or without
2496ef40f8Sdrahn * modification, are permitted provided that the following conditions
2596ef40f8Sdrahn * are met:
2696ef40f8Sdrahn * 1. Redistributions of source code must retain the above copyright
2796ef40f8Sdrahn * notice, this list of conditions and the following disclaimer.
2896ef40f8Sdrahn * 2. Redistributions in binary form must reproduce the above copyright
2996ef40f8Sdrahn * notice, this list of conditions and the following disclaimer in the
3096ef40f8Sdrahn * documentation and/or other materials provided with the distribution.
3196ef40f8Sdrahn * 3. All advertising materials mentioning features or use of this software
3296ef40f8Sdrahn * must display the following acknowledgement:
3396ef40f8Sdrahn * This product includes software developed by TooLs GmbH.
3496ef40f8Sdrahn * 4. The name of TooLs GmbH may not be used to endorse or promote products
3596ef40f8Sdrahn * derived from this software without specific prior written permission.
3696ef40f8Sdrahn *
3796ef40f8Sdrahn * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
3896ef40f8Sdrahn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
3996ef40f8Sdrahn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4096ef40f8Sdrahn * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4196ef40f8Sdrahn * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
4296ef40f8Sdrahn * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
4396ef40f8Sdrahn * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
4496ef40f8Sdrahn * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
4596ef40f8Sdrahn * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
4696ef40f8Sdrahn * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4796ef40f8Sdrahn */
4896ef40f8Sdrahn
4996ef40f8Sdrahn /*
5096ef40f8Sdrahn * RISCV machine dependent routines for kvm.
5196ef40f8Sdrahn */
5296ef40f8Sdrahn
530e64ee4cSderaadt #include <sys/types.h>
5496ef40f8Sdrahn #include <sys/kcore.h>
5596ef40f8Sdrahn
5696ef40f8Sdrahn #include <unistd.h>
5796ef40f8Sdrahn #include <stdlib.h>
5896ef40f8Sdrahn #include <nlist.h>
5996ef40f8Sdrahn #include <kvm.h>
6096ef40f8Sdrahn
6196ef40f8Sdrahn #include <db.h>
6296ef40f8Sdrahn
6396ef40f8Sdrahn #include "kvm_private.h"
6496ef40f8Sdrahn
6596ef40f8Sdrahn #include <machine/kcore.h>
6696ef40f8Sdrahn
6796ef40f8Sdrahn void
_kvm_freevtop(kvm_t * kd)6896ef40f8Sdrahn _kvm_freevtop(kvm_t *kd)
6996ef40f8Sdrahn {
7096ef40f8Sdrahn free(kd->vmst);
7196ef40f8Sdrahn kd->vmst = NULL;
7296ef40f8Sdrahn }
7396ef40f8Sdrahn
7496ef40f8Sdrahn int
_kvm_initvtop(kvm_t * kd)7596ef40f8Sdrahn _kvm_initvtop(kvm_t *kd)
7696ef40f8Sdrahn {
7796ef40f8Sdrahn return (0);
7896ef40f8Sdrahn }
7996ef40f8Sdrahn
8096ef40f8Sdrahn /*
8196ef40f8Sdrahn * Translate a kernel virtual address to a physical address by walking
8296ef40f8Sdrahn * the kernels page table.
8396ef40f8Sdrahn */
8496ef40f8Sdrahn
8596ef40f8Sdrahn int
_kvm_kvatop(kvm_t * kd,u_long va,paddr_t * pa)8696ef40f8Sdrahn _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
8796ef40f8Sdrahn {
8896ef40f8Sdrahn cpu_kcore_hdr_t *cpup = kd->cpu_data;
8996ef40f8Sdrahn
9096ef40f8Sdrahn if (ISALIVE(kd)) {
9196ef40f8Sdrahn _kvm_err(kd, 0, "vatop called in live kernel!");
9296ef40f8Sdrahn return (0);
9396ef40f8Sdrahn }
9496ef40f8Sdrahn
9596ef40f8Sdrahn /*
9696ef40f8Sdrahn * This relies upon the kernel text and data being contiguous
9796ef40f8Sdrahn * in the first memory segment.
9896ef40f8Sdrahn * Other virtual addresses are not reachable yet.
9996ef40f8Sdrahn */
10096ef40f8Sdrahn
10196ef40f8Sdrahn if (va >= cpup->kernelbase + cpup->kerneloffs &&
10296ef40f8Sdrahn va < cpup->kernelbase + cpup->kerneloffs + cpup->staticsize) {
10396ef40f8Sdrahn *pa = (va - cpup->kernelbase) +
10496ef40f8Sdrahn (paddr_t)cpup->ram_segs[0].start;
10596ef40f8Sdrahn return (int)(kd->nbpg - (va & (kd->nbpg - 1)));
10696ef40f8Sdrahn }
10796ef40f8Sdrahn
10896ef40f8Sdrahn _kvm_err(kd, 0, "kvm_vatop: va %lx unreachable", va);
10996ef40f8Sdrahn return (0);
11096ef40f8Sdrahn }
11196ef40f8Sdrahn
11296ef40f8Sdrahn off_t
_kvm_pa2off(kvm_t * kd,paddr_t pa)11396ef40f8Sdrahn _kvm_pa2off(kvm_t *kd, paddr_t pa)
11496ef40f8Sdrahn {
11596ef40f8Sdrahn cpu_kcore_hdr_t *cpup = kd->cpu_data;
11696ef40f8Sdrahn phys_ram_seg_t *mp = cpup->ram_segs;
11796ef40f8Sdrahn off_t off = 0;
11896ef40f8Sdrahn int block;
11996ef40f8Sdrahn
12096ef40f8Sdrahn for (block = 0; block < NPHYS_RAM_SEGS; block++, mp++) {
12196ef40f8Sdrahn if (pa >= mp->start && pa < mp->start + mp->size)
12296ef40f8Sdrahn return (kd->dump_off + off +
12396ef40f8Sdrahn (off_t)(pa - (paddr_t)mp->start));
12496ef40f8Sdrahn off += (off_t)mp->size;
12596ef40f8Sdrahn }
12696ef40f8Sdrahn
12796ef40f8Sdrahn _kvm_err(kd, 0, "not a physical address: %lx", pa);
12896ef40f8Sdrahn return (-1);
12996ef40f8Sdrahn }
130