xref: /openbsd-src/lib/libkvm/kvm_mips64.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: kvm_mips64.c,v 1.13 2013/11/01 15:57:56 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. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 /*
38  * MIPS machine dependent routines for kvm.  Hopefully, the forthcoming
39  * vm code will one day obsolete this module.
40  */
41 
42 #include <sys/param.h>
43 #include <sys/proc.h>
44 #include <sys/stat.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <nlist.h>
48 #include <kvm.h>
49 
50 #include <limits.h>
51 #include <db.h>
52 
53 #include "kvm_private.h"
54 
55 #include <machine/cpu.h>
56 #include <machine/pte.h>
57 #include <machine/pmap.h>
58 
59 #include <uvm/uvm_extern.h>
60 
61 struct vmstate {
62 	pt_entry_t	*Sysmap;
63 	u_int		Sysmapsize;
64 	vaddr_t		Sysmapbase;
65 	int		pagesize;
66 	int		pagemask;
67 	int		pageshift;
68 };
69 
70 void
71 _kvm_freevtop(kvm_t *kd)
72 {
73 	if (kd->vmst != 0)
74 		free(kd->vmst);
75 }
76 
77 int
78 _kvm_initvtop(kvm_t *kd)
79 {
80 	struct vmstate *vm;
81 	struct nlist nl[4];
82 	struct uvmexp uvmexp;
83 
84 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
85 	if (vm == 0)
86 		return (-1);
87 	kd->vmst = vm;
88 
89 	nl[0].n_name = "Sysmap";
90 	nl[1].n_name = "Sysmapsize";
91 	nl[2].n_name = "uvmexp";
92 	nl[3].n_name = 0;
93 
94 	if (kvm_nlist(kd, nl) != 0) {
95 		_kvm_err(kd, kd->program, "bad namelist");
96 		return (-1);
97 	}
98 	if (KREAD(kd, (u_long)nl[0].n_value, &vm->Sysmap)) {
99 		_kvm_err(kd, kd->program, "cannot read Sysmap");
100 		return (-1);
101 	}
102 	if (KREAD(kd, (u_long)nl[1].n_value, &vm->Sysmapsize)) {
103 		_kvm_err(kd, kd->program, "cannot read Sysmapsize");
104 		return (-1);
105 	}
106 	/*
107 	 * We are only interested in the first three fields of struct
108 	 * uvmexp, so do not try to read more than necessary (especially
109 	 * in case the layout changes).
110 	 */
111 	if (kvm_read(kd, (u_long)nl[2].n_value, &uvmexp,
112 	    3 * sizeof(int)) != 3 * sizeof(int)) {
113 		_kvm_err(kd, kd->program, "cannot read uvmexp");
114 		return (-1);
115 	}
116 	vm->pagesize = uvmexp.pagesize;
117 	vm->pagemask = uvmexp.pagemask;
118 	vm->pageshift = uvmexp.pageshift;
119 
120 	/*
121 	 * Older kernels might not have this symbol; in which case
122 	 * we use the value of VM_MIN_KERNE_ADDRESS they must have.
123 	 */
124 
125 	nl[0].n_name = "Sysmapbase";
126 	nl[1].n_name = 0;
127 	if (kvm_nlist(kd, nl) != 0 ||
128 	    KREAD(kd, (u_long)nl[0].n_value, &vm->Sysmapbase))
129 		vm->Sysmapbase = (vaddr_t)CKSSEG_BASE;
130 
131 	return (0);
132 }
133 
134 /*
135  * Translate a kernel virtual address to a physical address.
136  */
137 int
138 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
139 {
140 	struct vmstate *vm;
141 	pt_entry_t pte;
142 	u_long idx, addr;
143 	int offset;
144 
145 	if (ISALIVE(kd)) {
146 		_kvm_err(kd, 0, "vatop called in live kernel!");
147 		return((off_t)0);
148 	}
149 	vm = kd->vmst;
150 	offset = (int)va & vm->pagemask;
151 	/*
152 	 * If we are initializing (kernel segment table pointer not yet set)
153 	 * then return pa == va to avoid infinite recursion.
154 	 */
155 	if (vm->Sysmap == 0) {
156 		*pa = va;
157 		return vm->pagesize - offset;
158 	}
159 	/*
160 	 * Check for direct-mapped segments
161 	 */
162 	if (IS_XKPHYS(va)) {
163 		*pa = XKPHYS_TO_PHYS(va);
164 		return vm->pagesize - offset;
165 	}
166 	if (va >= (vaddr_t)CKSEG0_BASE && va < (vaddr_t)CKSSEG_BASE) {
167 		*pa = CKSEG0_TO_PHYS(va);
168 		return vm->pagesize - offset;
169 	}
170 	if (va < vm->Sysmapbase)
171 		goto invalid;
172 	idx = (va - vm->Sysmapbase) >> vm->pageshift;
173 	if (idx >= vm->Sysmapsize)
174 		goto invalid;
175 	addr = (u_long)vm->Sysmap + idx;
176 	/*
177 	 * Can't use KREAD to read kernel segment table entries.
178 	 * Fortunately it is 1-to-1 mapped so we don't have to.
179 	 */
180 	if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte),
181 	    (off_t)addr) < 0)
182 		goto invalid;
183 	if (!(pte & PG_V))
184 		goto invalid;
185 	*pa = (pte & PG_FRAME) | (paddr_t)offset;
186 	return vm->pagesize - offset;
187 
188 invalid:
189 	_kvm_err(kd, 0, "invalid address (%lx)", va);
190 	return (0);
191 }
192 
193 off_t
194 _kvm_pa2off(kvm_t *kd, paddr_t pa)
195 {
196 	_kvm_err(kd, 0, "pa2off going to be implemented!");
197 	return 0;
198 }
199