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