xref: /netbsd/lib/libkvm/kvm_riscv.c (revision 62e1e7cd)
1*62e1e7cdSchristos /*	$NetBSD: kvm_riscv.c,v 1.2 2022/01/10 19:51:30 christos Exp $	*/
225195b1eSmatt 
325195b1eSmatt /*-
425195b1eSmatt  * Copyright (c) 2014 The NetBSD Foundation, Inc.
525195b1eSmatt  * All rights reserved.
625195b1eSmatt  *
725195b1eSmatt  * This code is derived from software contributed to The NetBSD Foundation
825195b1eSmatt  * by Matt Thomas of 3am Software Foundry.
925195b1eSmatt  *
1025195b1eSmatt  * Redistribution and use in source and binary forms, with or without
1125195b1eSmatt  * modification, are permitted provided that the following conditions
1225195b1eSmatt  * are met:
1325195b1eSmatt  * 1. Redistributions of source code must retain the above copyright
1425195b1eSmatt  *    notice, this list of conditions and the following disclaimer.
1525195b1eSmatt  * 2. Redistributions in binary form must reproduce the above copyright
1625195b1eSmatt  *    notice, this list of conditions and the following disclaimer in the
1725195b1eSmatt  *    documentation and/or other materials provided with the distribution.
1825195b1eSmatt  *
1925195b1eSmatt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2025195b1eSmatt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2125195b1eSmatt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2225195b1eSmatt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2325195b1eSmatt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2425195b1eSmatt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2525195b1eSmatt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2625195b1eSmatt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2725195b1eSmatt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2825195b1eSmatt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2925195b1eSmatt  * POSSIBILITY OF SUCH DAMAGE.
3025195b1eSmatt  */
3125195b1eSmatt 
3225195b1eSmatt /*
3325195b1eSmatt  * OR1K machine dependent routines for kvm.
3425195b1eSmatt  */
3525195b1eSmatt 
3625195b1eSmatt #include <sys/param.h>
3725195b1eSmatt #include <sys/exec.h>
3825195b1eSmatt #include <sys/types.h>
3925195b1eSmatt 
4025195b1eSmatt #include <uvm/uvm_extern.h>
4125195b1eSmatt 
4225195b1eSmatt #include <db.h>
4325195b1eSmatt #include <limits.h>
4425195b1eSmatt #include <kvm.h>
4525195b1eSmatt #include <stdlib.h>
4625195b1eSmatt #include <unistd.h>
4725195b1eSmatt 
4825195b1eSmatt #include "kvm_private.h"
4925195b1eSmatt 
5025195b1eSmatt #include <sys/kcore.h>
5125195b1eSmatt #include <machine/kcore.h>
5225195b1eSmatt #include <machine/vmparam.h>
5325195b1eSmatt 
54*62e1e7cdSchristos __RCSID("$NetBSD: kvm_riscv.c,v 1.2 2022/01/10 19:51:30 christos Exp $");
5525195b1eSmatt 
5625195b1eSmatt void
_kvm_freevtop(kvm_t * kd)5725195b1eSmatt _kvm_freevtop(kvm_t *kd)
5825195b1eSmatt {
5925195b1eSmatt 	if (kd->vmst != 0)
6025195b1eSmatt 		free(kd->vmst);
6125195b1eSmatt }
6225195b1eSmatt 
6325195b1eSmatt /*ARGSUSED*/
6425195b1eSmatt int
_kvm_initvtop(kvm_t * kd)6525195b1eSmatt _kvm_initvtop(kvm_t *kd)
6625195b1eSmatt {
6725195b1eSmatt 
6825195b1eSmatt 	return 0;
6925195b1eSmatt }
7025195b1eSmatt 
7125195b1eSmatt /*
7225195b1eSmatt  * Translate a KVA to a PA
7325195b1eSmatt  */
7425195b1eSmatt int
_kvm_kvatop(kvm_t * kd,vaddr_t va,paddr_t * pa)7525195b1eSmatt _kvm_kvatop(kvm_t *kd, vaddr_t va, paddr_t *pa)
7625195b1eSmatt {
7725195b1eSmatt //	cpu_kcore_hdr_t	*cpu_kh;
7825195b1eSmatt 
7925195b1eSmatt 	if (ISALIVE(kd)) {
8025195b1eSmatt 		_kvm_err(kd, 0, "vatop called in live kernel!");
8125195b1eSmatt 		return 0;
8225195b1eSmatt 	}
8325195b1eSmatt 
8425195b1eSmatt 	/* No hit -- no translation */
8525195b1eSmatt 	*pa = (u_long)~0UL;
8625195b1eSmatt 	return 0;
8725195b1eSmatt }
8825195b1eSmatt 
8925195b1eSmatt off_t
_kvm_pa2off(kvm_t * kd,paddr_t pa)9025195b1eSmatt _kvm_pa2off(kvm_t *kd, paddr_t pa)
9125195b1eSmatt {
9225195b1eSmatt 	cpu_kcore_hdr_t	*cpu_kh;
9325195b1eSmatt 	phys_ram_seg_t	*ram;
9425195b1eSmatt 	off_t		off;
9525195b1eSmatt 	void		*e;
9625195b1eSmatt 
9725195b1eSmatt 	cpu_kh = kd->cpu_data;
9825195b1eSmatt 	e = (char *) kd->cpu_data + kd->cpu_dsize;
9925195b1eSmatt         ram = (void *)((char *)(void *)cpu_kh + ALIGN(sizeof *cpu_kh));
10025195b1eSmatt 	off = kd->dump_off;
10125195b1eSmatt 	do {
10225195b1eSmatt 		if (pa >= ram->start && (pa - ram->start) < ram->size) {
10325195b1eSmatt 			return off + (pa - ram->start);
10425195b1eSmatt 		}
10525195b1eSmatt 		ram++;
10625195b1eSmatt 		off += ram->size;
10725195b1eSmatt 	} while ((void *) ram < e && ram->size);
10825195b1eSmatt 
10925195b1eSmatt 	_kvm_err(kd, 0, "pa2off failed for pa %#" PRIxPADDR "\n", pa);
11025195b1eSmatt 	return (off_t) -1;
11125195b1eSmatt }
11225195b1eSmatt 
11325195b1eSmatt /*
11425195b1eSmatt  * Machine-dependent initialization for ALL open kvm descriptors,
11525195b1eSmatt  * not just those for a kernel crash dump.  Some architectures
11625195b1eSmatt  * have to deal with these NOT being constants!  (i.e. m68k)
11725195b1eSmatt  */
11825195b1eSmatt int
_kvm_mdopen(kvm_t * kd)11925195b1eSmatt _kvm_mdopen(kvm_t *kd)
12025195b1eSmatt {
12125195b1eSmatt 	kd->min_uva = VM_MIN_ADDRESS;
12225195b1eSmatt 	kd->max_uva = VM_MAXUSER_ADDRESS;
12325195b1eSmatt 
12425195b1eSmatt 	return (0);
12525195b1eSmatt }
126