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