xref: /openbsd-src/lib/libkvm/kvm_mips64.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /*	$OpenBSD: kvm_mips64.c,v 1.16 2018/05/03 16:42:07 zhuk 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 <sys/sysctl.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <nlist.h>
49 #include <kvm.h>
50 
51 #include <limits.h>
52 #include <db.h>
53 
54 #include "kvm_private.h"
55 
56 #include <machine/cpu.h>
57 #include <machine/pte.h>
58 #include <machine/pmap.h>
59 
60 #include <uvm/uvm_extern.h>
61 
62 struct vmstate {
63 	pt_entry_t	*Sysmap;
64 	u_int		Sysmapsize;
65 	vaddr_t		Sysmapbase;
66 	int		pagesize;
67 	int		pagemask;
68 	int		pageshift;
69 };
70 
71 void
72 _kvm_freevtop(kvm_t *kd)
73 {
74 	free(kd->vmst);
75 	kd->vmst = NULL;
76 }
77 
78 int
79 _kvm_initvtop(kvm_t *kd)
80 {
81 	struct vmstate *vm;
82 	struct nlist nl[4];
83 	struct uvmexp uvmexp;
84 
85 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
86 	if (vm == 0)
87 		return (-1);
88 	kd->vmst = vm;
89 
90 	nl[0].n_name = "Sysmap";
91 	nl[1].n_name = "Sysmapsize";
92 	nl[2].n_name = "uvmexp";
93 	nl[3].n_name = 0;
94 
95 	if (kvm_nlist(kd, nl) != 0) {
96 		_kvm_err(kd, kd->program, "bad namelist");
97 		return (-1);
98 	}
99 	if (KREAD(kd, (u_long)nl[0].n_value, &vm->Sysmap)) {
100 		_kvm_err(kd, kd->program, "cannot read Sysmap");
101 		return (-1);
102 	}
103 	if (KREAD(kd, (u_long)nl[1].n_value, &vm->Sysmapsize)) {
104 		_kvm_err(kd, kd->program, "cannot read Sysmapsize");
105 		return (-1);
106 	}
107 	/*
108 	 * We are only interested in the first three fields of struct
109 	 * uvmexp, so do not try to read more than necessary (especially
110 	 * in case the layout changes).
111 	 */
112 	if (kvm_read(kd, (u_long)nl[2].n_value, &uvmexp,
113 	    3 * sizeof(int)) != 3 * sizeof(int)) {
114 		_kvm_err(kd, kd->program, "cannot read uvmexp");
115 		return (-1);
116 	}
117 	vm->pagesize = uvmexp.pagesize;
118 	vm->pagemask = uvmexp.pagemask;
119 	vm->pageshift = uvmexp.pageshift;
120 
121 	/*
122 	 * Older kernels might not have this symbol; in which case
123 	 * we use the value of VM_MIN_KERNEL_ADDRESS they must have.
124 	 */
125 
126 	nl[0].n_name = "Sysmapbase";
127 	nl[1].n_name = 0;
128 	if (kvm_nlist(kd, nl) != 0 ||
129 	    KREAD(kd, (u_long)nl[0].n_value, &vm->Sysmapbase))
130 		vm->Sysmapbase = (vaddr_t)CKSSEG_BASE;
131 
132 	return (0);
133 }
134 
135 /*
136  * Translate a kernel virtual address to a physical address.
137  */
138 int
139 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
140 {
141 	struct vmstate *vm;
142 	pt_entry_t pte;
143 	u_long idx, addr;
144 	int offset;
145 
146 	if (ISALIVE(kd)) {
147 		_kvm_err(kd, 0, "vatop called in live kernel!");
148 		return((off_t)0);
149 	}
150 	vm = kd->vmst;
151 	offset = (int)va & vm->pagemask;
152 	/*
153 	 * If we are initializing (kernel segment table pointer not yet set)
154 	 * then return pa == va to avoid infinite recursion.
155 	 */
156 	if (vm->Sysmap == 0) {
157 		*pa = va;
158 		return vm->pagesize - offset;
159 	}
160 	/*
161 	 * Check for direct-mapped segments
162 	 */
163 	if (IS_XKPHYS(va)) {
164 		*pa = XKPHYS_TO_PHYS(va);
165 		return vm->pagesize - offset;
166 	}
167 	if (va >= (vaddr_t)CKSEG0_BASE && va < (vaddr_t)CKSSEG_BASE) {
168 		*pa = CKSEG0_TO_PHYS(va);
169 		return vm->pagesize - offset;
170 	}
171 	if (va < vm->Sysmapbase)
172 		goto invalid;
173 	idx = (va - vm->Sysmapbase) >> vm->pageshift;
174 	if (idx >= vm->Sysmapsize)
175 		goto invalid;
176 	addr = (u_long)vm->Sysmap + idx;
177 	/*
178 	 * Can't use KREAD to read kernel segment table entries.
179 	 * Fortunately it is 1-to-1 mapped so we don't have to.
180 	 */
181 	if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte),
182 	    (off_t)addr) < 0)
183 		goto invalid;
184 	if (!(pte & PG_V))
185 		goto invalid;
186 	*pa = (pte & PG_FRAME) | (paddr_t)offset;
187 	return vm->pagesize - offset;
188 
189 invalid:
190 	_kvm_err(kd, 0, "invalid address (%lx)", va);
191 	return (0);
192 }
193 
194 off_t
195 _kvm_pa2off(kvm_t *kd, paddr_t pa)
196 {
197 	_kvm_err(kd, 0, "pa2off going to be implemented!");
198 	return 0;
199 }
200