xref: /openbsd-src/lib/libkvm/kvm_mips64.c (revision daf88648c0e349d5c02e1504293082072c981640)
1 /*	$OpenBSD: kvm_mips64.c,v 1.5 2007/01/08 18:54:12 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 #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.5 2007/01/08 18:54:12 deraadt 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 #define KREAD(kd, addr, p)\
74 	(kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
75 
76 void
77 _kvm_freevtop(kvm_t *kd)
78 {
79 	if (kd->vmst != 0)
80 		free(kd->vmst);
81 }
82 
83 int
84 _kvm_initvtop(kvm_t *kd)
85 {
86 	struct vmstate *vm;
87 	struct nlist nlist[3];
88 
89 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
90 	if (vm == 0)
91 		return (-1);
92 	kd->vmst = vm;
93 
94 	nlist[0].n_name = "Sysmap";
95 	nlist[1].n_name = "Sysmapsize";
96 	nlist[2].n_name = 0;
97 
98 	if (kvm_nlist(kd, nlist) != 0) {
99 		_kvm_err(kd, kd->program, "bad namelist");
100 		return (-1);
101 	}
102 	if (KREAD(kd, (u_long)nlist[0].n_value, &vm->Sysmap)) {
103 		_kvm_err(kd, kd->program, "cannot read Sysmap");
104 		return (-1);
105 	}
106 	if (KREAD(kd, (u_long)nlist[1].n_value, &vm->Sysmapsize)) {
107 		_kvm_err(kd, kd->program, "cannot read Sysmapsize");
108 		return (-1);
109 	}
110 	return (0);
111 }
112 
113 /*
114  * Translate a kernel virtual address to a physical address.
115  */
116 int
117 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
118 {
119 	struct vmstate *vm;
120 	u_long pte, addr, offset;
121 
122 	if (ISALIVE(kd)) {
123 		_kvm_err(kd, 0, "vatop called in live kernel!");
124 		return((off_t)0);
125 	}
126 	vm = kd->vmst;
127 	offset = va & PGOFSET;
128 	/*
129 	 * If we are initializing (kernel segment table pointer not yet set)
130 	 * then return pa == va to avoid infinite recursion.
131 	 */
132 	if (vm->Sysmap == 0) {
133 		*pa = va;
134 		return (NBPG - offset);
135 	}
136 	if (va < KERNBASE ||
137 	    va >= VM_MIN_KERNEL_ADDRESS + vm->Sysmapsize * NBPG)
138 		goto invalid;
139 	if (va < VM_MIN_KERNEL_ADDRESS) {
140 		*pa = KSEG0_TO_PHYS(va);
141 		return (NBPG - offset);
142 	}
143 	addr = (u_long)(vm->Sysmap + ((va - VM_MIN_KERNEL_ADDRESS) >> PGSHIFT));
144 	/*
145 	 * Can't use KREAD to read kernel segment table entries.
146 	 * Fortunately it is 1-to-1 mapped so we don't have to.
147 	 */
148 	if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte), (off_t)addr) < 0)
149 		goto invalid;
150 	if (!(pte & PG_V))
151 		goto invalid;
152 	*pa = (pte & PG_FRAME) | offset;
153 	return (NBPG - 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