xref: /openbsd-src/lib/libkvm/kvm_arm64.c (revision d415763e8eca6b33134cd1c48e0bb3880267a548)
1*d415763eSderaadt /*	$OpenBSD: kvm_arm64.c,v 1.3 2021/12/01 21:45:19 deraadt Exp $	*/
23c3e6419Spatrick /*
33c3e6419Spatrick  * Copyright (c) 2006 Miodrag Vallat.
43c3e6419Spatrick  *
53c3e6419Spatrick  * Permission to use, copy, modify, and distribute this software for any
63c3e6419Spatrick  * purpose with or without fee is hereby granted, provided that the above
73c3e6419Spatrick  * copyright notice, this permission notice, and the disclaimer below
83c3e6419Spatrick  * appear in all copies.
93c3e6419Spatrick  *
103c3e6419Spatrick  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
113c3e6419Spatrick  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
123c3e6419Spatrick  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
133c3e6419Spatrick  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
143c3e6419Spatrick  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
153c3e6419Spatrick  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
163c3e6419Spatrick  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
173c3e6419Spatrick  */
183c3e6419Spatrick /*-
193c3e6419Spatrick  * Copyright (C) 1996 Wolfgang Solfrank.
203c3e6419Spatrick  * Copyright (C) 1996 TooLs GmbH.
213c3e6419Spatrick  * All rights reserved.
223c3e6419Spatrick  *
233c3e6419Spatrick  * Redistribution and use in source and binary forms, with or without
243c3e6419Spatrick  * modification, are permitted provided that the following conditions
253c3e6419Spatrick  * are met:
263c3e6419Spatrick  * 1. Redistributions of source code must retain the above copyright
273c3e6419Spatrick  *    notice, this list of conditions and the following disclaimer.
283c3e6419Spatrick  * 2. Redistributions in binary form must reproduce the above copyright
293c3e6419Spatrick  *    notice, this list of conditions and the following disclaimer in the
303c3e6419Spatrick  *    documentation and/or other materials provided with the distribution.
313c3e6419Spatrick  * 3. All advertising materials mentioning features or use of this software
323c3e6419Spatrick  *    must display the following acknowledgement:
333c3e6419Spatrick  *	This product includes software developed by TooLs GmbH.
343c3e6419Spatrick  * 4. The name of TooLs GmbH may not be used to endorse or promote products
353c3e6419Spatrick  *    derived from this software without specific prior written permission.
363c3e6419Spatrick  *
373c3e6419Spatrick  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
383c3e6419Spatrick  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
393c3e6419Spatrick  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
403c3e6419Spatrick  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
413c3e6419Spatrick  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
423c3e6419Spatrick  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
433c3e6419Spatrick  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
443c3e6419Spatrick  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
453c3e6419Spatrick  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
463c3e6419Spatrick  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
473c3e6419Spatrick  */
483c3e6419Spatrick 
493c3e6419Spatrick /*
503c3e6419Spatrick  * ARM64 machine dependent routines for kvm.
513c3e6419Spatrick  */
523c3e6419Spatrick 
530e64ee4cSderaadt #include <sys/types.h>
543c3e6419Spatrick #include <sys/kcore.h>
553c3e6419Spatrick 
563c3e6419Spatrick #include <unistd.h>
573c3e6419Spatrick #include <stdlib.h>
583c3e6419Spatrick #include <nlist.h>
593c3e6419Spatrick #include <kvm.h>
603c3e6419Spatrick 
613c3e6419Spatrick #include <db.h>
623c3e6419Spatrick 
633c3e6419Spatrick #include "kvm_private.h"
643c3e6419Spatrick 
65*d415763eSderaadt #include <machine/kcore.h>
663c3e6419Spatrick 
673c3e6419Spatrick void
_kvm_freevtop(kvm_t * kd)683c3e6419Spatrick _kvm_freevtop(kvm_t *kd)
693c3e6419Spatrick {
703c3e6419Spatrick 	free(kd->vmst);
713c3e6419Spatrick 	kd->vmst = NULL;
723c3e6419Spatrick }
733c3e6419Spatrick 
743c3e6419Spatrick int
_kvm_initvtop(kvm_t * kd)753c3e6419Spatrick _kvm_initvtop(kvm_t *kd)
763c3e6419Spatrick {
773c3e6419Spatrick 	return (0);
783c3e6419Spatrick }
793c3e6419Spatrick 
803c3e6419Spatrick /*
813c3e6419Spatrick  * Translate a kernel virtual address to a physical address by walking
823c3e6419Spatrick  * the kernels page table.
833c3e6419Spatrick  */
843c3e6419Spatrick 
853c3e6419Spatrick int
_kvm_kvatop(kvm_t * kd,u_long va,paddr_t * pa)863c3e6419Spatrick _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
873c3e6419Spatrick {
883c3e6419Spatrick 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
893c3e6419Spatrick 
903c3e6419Spatrick 	if (ISALIVE(kd)) {
913c3e6419Spatrick 		_kvm_err(kd, 0, "vatop called in live kernel!");
923c3e6419Spatrick 		return (0);
933c3e6419Spatrick 	}
943c3e6419Spatrick 
953c3e6419Spatrick 	/*
963c3e6419Spatrick 	 * This relies upon the kernel text and data being contiguous
973c3e6419Spatrick 	 * in the first memory segment.
983c3e6419Spatrick 	 * Other virtual addresses are not reachable yet.
993c3e6419Spatrick 	 */
1003c3e6419Spatrick 
1013c3e6419Spatrick 	if (va >= cpup->kernelbase + cpup->kerneloffs &&
1023c3e6419Spatrick 	    va < cpup->kernelbase + cpup->kerneloffs + cpup->staticsize) {
1033c3e6419Spatrick 		*pa = (va - cpup->kernelbase) +
1043c3e6419Spatrick 		    (paddr_t)cpup->ram_segs[0].start;
1053c3e6419Spatrick 		return (int)(kd->nbpg - (va & (kd->nbpg - 1)));
1063c3e6419Spatrick 	}
1073c3e6419Spatrick 
1083c3e6419Spatrick 	_kvm_err(kd, 0, "kvm_vatop: va %lx unreachable", va);
1093c3e6419Spatrick 	return (0);
1103c3e6419Spatrick }
1113c3e6419Spatrick 
1123c3e6419Spatrick off_t
_kvm_pa2off(kvm_t * kd,paddr_t pa)1133c3e6419Spatrick _kvm_pa2off(kvm_t *kd, paddr_t pa)
1143c3e6419Spatrick {
1153c3e6419Spatrick 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
1163c3e6419Spatrick 	phys_ram_seg_t *mp = cpup->ram_segs;
1173c3e6419Spatrick 	off_t off = 0;
1183c3e6419Spatrick 	int block;
1193c3e6419Spatrick 
1203c3e6419Spatrick 	for (block = 0; block < NPHYS_RAM_SEGS; block++, mp++) {
1213c3e6419Spatrick 		if (pa >= mp->start && pa < mp->start + mp->size)
1223c3e6419Spatrick 			return (kd->dump_off + off +
1233c3e6419Spatrick 			    (off_t)(pa - (paddr_t)mp->start));
1243c3e6419Spatrick 		off += (off_t)mp->size;
1253c3e6419Spatrick 	}
1263c3e6419Spatrick 
1273c3e6419Spatrick 	_kvm_err(kd, 0, "not a physical address: %lx", pa);
1283c3e6419Spatrick 	return (-1);
1293c3e6419Spatrick }
130