xref: /openbsd-src/lib/libkvm/kvm_mips64.c (revision 11efff7f3ac2b3cfeff0c0cddc14294d9b3aca4f)
1 /*	$OpenBSD: kvm_mips64.c,v 1.2 2004/09/15 19:31:31 miod 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.2 2004/09/15 19:31:31 miod 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 <stdlib.h>
60 #include <nlist.h>
61 #include <kvm.h>
62 
63 #include <limits.h>
64 #include <db.h>
65 
66 #include "kvm_private.h"
67 
68 #include <machine/cpu.h>
69 #include <machine/pte.h>
70 #include <machine/pmap.h>
71 
72 struct vmstate {
73 	pt_entry_t	*Sysmap;
74 	u_int		Sysmapsize;
75 };
76 
77 #define KREAD(kd, addr, p)\
78 	(kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
79 
80 void
81 _kvm_freevtop(kvm_t *kd)
82 {
83 	if (kd->vmst != 0)
84 		free(kd->vmst);
85 }
86 
87 int
88 _kvm_initvtop(kvm_t *kd)
89 {
90 	struct vmstate *vm;
91 	struct nlist nlist[3];
92 
93 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
94 	if (vm == 0)
95 		return (-1);
96 	kd->vmst = vm;
97 
98 	nlist[0].n_name = "Sysmap";
99 	nlist[1].n_name = "Sysmapsize";
100 	nlist[2].n_name = 0;
101 
102 	if (kvm_nlist(kd, nlist) != 0) {
103 		_kvm_err(kd, kd->program, "bad namelist");
104 		return (-1);
105 	}
106 	if (KREAD(kd, (u_long)nlist[0].n_value, &vm->Sysmap)) {
107 		_kvm_err(kd, kd->program, "cannot read Sysmap");
108 		return (-1);
109 	}
110 	if (KREAD(kd, (u_long)nlist[1].n_value, &vm->Sysmapsize)) {
111 		_kvm_err(kd, kd->program, "cannot read mmutype");
112 		return (-1);
113 	}
114 	return (0);
115 }
116 
117 /*
118  * Translate a kernel virtual address to a physical address.
119  */
120 int
121 _kvm_kvatop(kvm_t *kd, u_long va, u_long *pa)
122 {
123 	struct vmstate *vm;
124 	u_long pte, addr, offset;
125 
126 	if (ISALIVE(kd)) {
127 		_kvm_err(kd, 0, "vatop called in live kernel!");
128 		return((off_t)0);
129 	}
130 	vm = kd->vmst;
131 	offset = va & PGOFSET;
132 	/*
133 	 * If we are initializing (kernel segment table pointer not yet set)
134 	 * then return pa == va to avoid infinite recursion.
135 	 */
136 	if (vm->Sysmap == 0) {
137 		*pa = va;
138 		return (NBPG - offset);
139 	}
140 	if (va < KERNBASE ||
141 	    va >= VM_MIN_KERNEL_ADDRESS + vm->Sysmapsize * NBPG)
142 		goto invalid;
143 	if (va < VM_MIN_KERNEL_ADDRESS) {
144 		*pa = KSEG0_TO_PHYS(va);
145 		return (NBPG - offset);
146 	}
147 	addr = (u_long)(vm->Sysmap + ((va - VM_MIN_KERNEL_ADDRESS) >> PGSHIFT));
148 	/*
149 	 * Can't use KREAD to read kernel segment table entries.
150 	 * Fortunately it is 1-to-1 mapped so we don't have to.
151 	 */
152 	if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte), (off_t)addr) < 0)
153 		goto invalid;
154 	if (!(pte & PG_V))
155 		goto invalid;
156 	*pa = (pte & PG_FRAME) | offset;
157 	return (NBPG - offset);
158 
159 invalid:
160 	_kvm_err(kd, 0, "invalid address (%lx)", va);
161 	return (0);
162 }
163 
164 off_t
165 _kvm_pa2off(kvm_t *kd, u_long pa)
166 {
167 	_kvm_err(kd, 0, "pa2off going to be implemented!");
168 	return 0;
169 }
170