xref: /openbsd-src/lib/libkvm/kvm_mips64.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /*	$OpenBSD: kvm_mips64.c,v 1.8 2009/06/20 20:20:43 millert 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 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)kvm_mips.c	8.1 (Berkeley) 6/4/93";
40 #else
41 static char *rcsid = "$OpenBSD: kvm_mips64.c,v 1.8 2009/06/20 20:20:43 millert Exp $";
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44 
45 /*
46  * MIPS machine dependent routines for kvm.  Hopefully, the forthcoming
47  * vm code will one day obsolete this module.
48  */
49 
50 #include <sys/param.h>
51 #include <sys/user.h>
52 #include <sys/proc.h>
53 #include <sys/stat.h>
54 #include <unistd.h>
55 #include <stdlib.h>
56 #include <nlist.h>
57 #include <kvm.h>
58 
59 #include <limits.h>
60 #include <db.h>
61 
62 #include "kvm_private.h"
63 
64 #include <machine/cpu.h>
65 #include <machine/pte.h>
66 #include <machine/pmap.h>
67 
68 struct vmstate {
69 	pt_entry_t	*Sysmap;
70 	u_int		Sysmapsize;
71 };
72 
73 void
74 _kvm_freevtop(kvm_t *kd)
75 {
76 	if (kd->vmst != 0)
77 		free(kd->vmst);
78 }
79 
80 int
81 _kvm_initvtop(kvm_t *kd)
82 {
83 	struct vmstate *vm;
84 	struct nlist nlist[3];
85 
86 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
87 	if (vm == 0)
88 		return (-1);
89 	kd->vmst = vm;
90 
91 	nlist[0].n_name = "Sysmap";
92 	nlist[1].n_name = "Sysmapsize";
93 	nlist[2].n_name = 0;
94 
95 	if (kvm_nlist(kd, nlist) != 0) {
96 		_kvm_err(kd, kd->program, "bad namelist");
97 		return (-1);
98 	}
99 	if (KREAD(kd, (u_long)nlist[0].n_value, &vm->Sysmap)) {
100 		_kvm_err(kd, kd->program, "cannot read Sysmap");
101 		return (-1);
102 	}
103 	if (KREAD(kd, (u_long)nlist[1].n_value, &vm->Sysmapsize)) {
104 		_kvm_err(kd, kd->program, "cannot read Sysmapsize");
105 		return (-1);
106 	}
107 	return (0);
108 }
109 
110 /*
111  * Translate a kernel virtual address to a physical address.
112  */
113 int
114 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
115 {
116 	struct vmstate *vm;
117 	pt_entry_t pte;
118 	u_long idx, addr, offset;
119 
120 	if (ISALIVE(kd)) {
121 		_kvm_err(kd, 0, "vatop called in live kernel!");
122 		return((off_t)0);
123 	}
124 	vm = kd->vmst;
125 	offset = va & PAGE_MASK;
126 	/*
127 	 * If we are initializing (kernel segment table pointer not yet set)
128 	 * then return pa == va to avoid infinite recursion.
129 	 */
130 	if (vm->Sysmap == 0) {
131 		*pa = va;
132 		return (int)(PAGE_SIZE - offset);
133 	}
134 	/*
135 	 * Check for direct-mapped segments
136 	 */
137 	if (IS_XKPHYS(va)) {
138 		*pa = XKPHYS_TO_PHYS(va);
139 		return (int)(PAGE_SIZE - offset);
140 	}
141 	if (va >= (vaddr_t)KSEG0_BASE && va < (vaddr_t)KSSEG_BASE) {
142 		*pa = KSEG0_TO_PHYS(va);
143 		return (int)(PAGE_SIZE - offset);
144 	}
145 	if (va < VM_MIN_KERNEL_ADDRESS)
146 		goto invalid;
147 	idx = (va - VM_MIN_KERNEL_ADDRESS) >> PGSHIFT;
148 	if (idx >= vm->Sysmapsize)
149 		goto invalid;
150 	addr = (u_long)(vm->Sysmap + idx);
151 	/*
152 	 * Can't use KREAD to read kernel segment table entries.
153 	 * Fortunately it is 1-to-1 mapped so we don't have to.
154 	 */
155 	if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte),
156 	    (off_t)addr) < 0)
157 		goto invalid;
158 	if (!(pte & PG_V))
159 		goto invalid;
160 	*pa = (pte & PG_FRAME) | offset;
161 	return (int)(PAGE_SIZE - offset);
162 
163 invalid:
164 	_kvm_err(kd, 0, "invalid address (%lx)", va);
165 	return (0);
166 }
167 
168 off_t
169 _kvm_pa2off(kvm_t *kd, paddr_t pa)
170 {
171 	_kvm_err(kd, 0, "pa2off going to be implemented!");
172 	return 0;
173 }
174