xref: /openbsd-src/lib/libkvm/kvm_mips64.c (revision 0eea0d082377cb9c3ec583313dc4d52b7b6a4d6d)
1 /*	$OpenBSD: kvm_mips64.c,v 1.1 2004/08/06 22:53:47 deraadt Exp $ */
2 /*	$NetBSD: kvm_mips.c,v 1.3 1996/03/18 22:33:44 thorpej Exp $	*/
3 
4 /*-
5  * Copyright (c) 1989, 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software developed by the Computer Systems
9  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
10  * BG 91-66 and contributed to Berkeley. Modified for MIPS by Ralph Campbell.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 #if defined(LIBC_SCCS) && !defined(lint)
42 #if 0
43 static char sccsid[] = "@(#)kvm_mips.c	8.1 (Berkeley) 6/4/93";
44 #else
45 static char *rcsid = "$OpenBSD: kvm_mips64.c,v 1.1 2004/08/06 22:53:47 deraadt Exp $";
46 #endif
47 #endif /* LIBC_SCCS and not lint */
48 
49 /*
50  * MIPS machine dependent routines for kvm.  Hopefully, the forthcoming
51  * vm code will one day obsolete this module.
52  */
53 
54 #include <sys/param.h>
55 #include <sys/user.h>
56 #include <sys/proc.h>
57 #include <sys/stat.h>
58 #include <unistd.h>
59 #include <nlist.h>
60 #include <kvm.h>
61 
62 #include <limits.h>
63 #include <db.h>
64 
65 #include "kvm_private.h"
66 
67 #include <machine/cpu.h>
68 #include <machine/pte.h>
69 #include <machine/pmap.h>
70 
71 struct vmstate {
72 	pt_entry_t	*Sysmap;
73 	u_int		Sysmapsize;
74 };
75 
76 #define KREAD(kd, addr, p)\
77 	(kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
78 
79 void
80 _kvm_freevtop(kvm_t *kd)
81 {
82 	if (kd->vmst != 0)
83 		free(kd->vmst);
84 }
85 
86 int
87 _kvm_initvtop(kvm_t *kd)
88 {
89 	struct vmstate *vm;
90 	struct nlist nlist[3];
91 
92 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
93 	if (vm == 0)
94 		return (-1);
95 	kd->vmst = vm;
96 
97 	nlist[0].n_name = "Sysmap";
98 	nlist[1].n_name = "Sysmapsize";
99 	nlist[2].n_name = 0;
100 
101 	if (kvm_nlist(kd, nlist) != 0) {
102 		_kvm_err(kd, kd->program, "bad namelist");
103 		return (-1);
104 	}
105 	if (KREAD(kd, (u_long)nlist[0].n_value, &vm->Sysmap)) {
106 		_kvm_err(kd, kd->program, "cannot read Sysmap");
107 		return (-1);
108 	}
109 	if (KREAD(kd, (u_long)nlist[1].n_value, &vm->Sysmapsize)) {
110 		_kvm_err(kd, kd->program, "cannot read mmutype");
111 		return (-1);
112 	}
113 	return (0);
114 }
115 
116 /*
117  * Translate a kernel virtual address to a physical address.
118  */
119 int
120 _kvm_kvatop(kvm_t *kd, u_long va, u_long *pa)
121 {
122 	struct vmstate *vm;
123 	u_long pte, addr, offset;
124 
125 	if (ISALIVE(kd)) {
126 		_kvm_err(kd, 0, "vatop called in live kernel!");
127 		return((off_t)0);
128 	}
129 	vm = kd->vmst;
130 	offset = va & PGOFSET;
131 	/*
132 	 * If we are initializing (kernel segment table pointer not yet set)
133 	 * then return pa == va to avoid infinite recursion.
134 	 */
135 	if (vm->Sysmap == 0) {
136 		*pa = va;
137 		return (NBPG - offset);
138 	}
139 	if (va < KERNBASE ||
140 	    va >= VM_MIN_KERNEL_ADDRESS + vm->Sysmapsize * NBPG)
141 		goto invalid;
142 	if (va < VM_MIN_KERNEL_ADDRESS) {
143 		*pa = KSEG0_TO_PHYS(va);
144 		return (NBPG - offset);
145 	}
146 	addr = (u_long)(vm->Sysmap + ((va - VM_MIN_KERNEL_ADDRESS) >> PGSHIFT));
147 	/*
148 	 * Can't use KREAD to read kernel segment table entries.
149 	 * Fortunately it is 1-to-1 mapped so we don't have to.
150 	 */
151 	if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte), (off_t)addr) < 0)
152 		goto invalid;
153 	if (!(pte & PG_V))
154 		goto invalid;
155 	*pa = (pte & PG_FRAME) | offset;
156 	return (NBPG - offset);
157 
158 invalid:
159 	_kvm_err(kd, 0, "invalid address (%lx)", va);
160 	return (0);
161 }
162 
163 off_t
164 _kvm_pa2off(kvm_t *kd, u_long pa)
165 {
166 	_kvm_err(kd, 0, "pa2off going to be implemented!");
167 	return 0;
168 }
169