xref: /freebsd/lib/libkvm/kvm_i386.c (revision 51295a4d3e4c551df85249433c490208dc7fd23d)
1f95a0250SRodney W. Grimes /*-
2f95a0250SRodney W. Grimes  * Copyright (c) 1989, 1992, 1993
3f95a0250SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4f95a0250SRodney W. Grimes  *
5f95a0250SRodney W. Grimes  * This code is derived from software developed by the Computer Systems
6f95a0250SRodney W. Grimes  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7f95a0250SRodney W. Grimes  * BG 91-66 and contributed to Berkeley.
8f95a0250SRodney W. Grimes  *
9f95a0250SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
10f95a0250SRodney W. Grimes  * modification, are permitted provided that the following conditions
11f95a0250SRodney W. Grimes  * are met:
12f95a0250SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
13f95a0250SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
14f95a0250SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
15f95a0250SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
16f95a0250SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
17f95a0250SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
18f95a0250SRodney W. Grimes  *    must display the following acknowledgement:
19f95a0250SRodney W. Grimes  *	This product includes software developed by the University of
20f95a0250SRodney W. Grimes  *	California, Berkeley and its contributors.
21f95a0250SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
22f95a0250SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23f95a0250SRodney W. Grimes  *    without specific prior written permission.
24f95a0250SRodney W. Grimes  *
25f95a0250SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26f95a0250SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27f95a0250SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28f95a0250SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29f95a0250SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30f95a0250SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31f95a0250SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32f95a0250SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33f95a0250SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34f95a0250SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35f95a0250SRodney W. Grimes  * SUCH DAMAGE.
36f95a0250SRodney W. Grimes  */
37f95a0250SRodney W. Grimes 
38f95a0250SRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
39f95a0250SRodney W. Grimes static char sccsid[] = "@(#)kvm_hp300.c	8.1 (Berkeley) 6/4/93";
40f95a0250SRodney W. Grimes #endif /* LIBC_SCCS and not lint */
41f95a0250SRodney W. Grimes 
42f95a0250SRodney W. Grimes /*
4321d54b07SRodney W. Grimes  * i386 machine dependent routines for kvm.  Hopefully, the forthcoming
44f95a0250SRodney W. Grimes  * vm code will one day obsolete this module.
45f95a0250SRodney W. Grimes  */
46f95a0250SRodney W. Grimes 
47f95a0250SRodney W. Grimes #include <sys/param.h>
48f95a0250SRodney W. Grimes #include <sys/user.h>
49f95a0250SRodney W. Grimes #include <sys/proc.h>
50f95a0250SRodney W. Grimes #include <sys/stat.h>
5151295a4dSJordan K. Hubbard #include <stdlib.h>
52f95a0250SRodney W. Grimes #include <unistd.h>
53f95a0250SRodney W. Grimes #include <nlist.h>
54f95a0250SRodney W. Grimes #include <kvm.h>
55f95a0250SRodney W. Grimes 
56f95a0250SRodney W. Grimes #include <vm/vm.h>
57f95a0250SRodney W. Grimes #include <vm/vm_param.h>
58f95a0250SRodney W. Grimes 
59f95a0250SRodney W. Grimes #include <limits.h>
60f95a0250SRodney W. Grimes #include <db.h>
61f95a0250SRodney W. Grimes 
62f95a0250SRodney W. Grimes #include "kvm_private.h"
63f95a0250SRodney W. Grimes 
64f95a0250SRodney W. Grimes #ifndef btop
6521d54b07SRodney W. Grimes #define	btop(x)		(i386_btop(x))
6621d54b07SRodney W. Grimes #define	ptob(x)		(i386_ptob(x))
67f95a0250SRodney W. Grimes #endif
68f95a0250SRodney W. Grimes 
69f95a0250SRodney W. Grimes struct vmstate {
7021d54b07SRodney W. Grimes 	struct pde	**IdlePTD;
7121d54b07SRodney W. Grimes 	struct pde	*PTD;
72f95a0250SRodney W. Grimes };
73f95a0250SRodney W. Grimes 
74f95a0250SRodney W. Grimes #define KREAD(kd, addr, p)\
75f95a0250SRodney W. Grimes 	(kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
76f95a0250SRodney W. Grimes 
77f95a0250SRodney W. Grimes void
7821d54b07SRodney W. Grimes _kvm_freevtop(kvm_t *kd) {
7921d54b07SRodney W. Grimes 	if (kd->vmst->PTD) {
8021d54b07SRodney W. Grimes 		free(kd->vmst->PTD);
8121d54b07SRodney W. Grimes 	}
8221d54b07SRodney W. Grimes 	if (kd->vmst != 0) {
83f95a0250SRodney W. Grimes 		free(kd->vmst);
84f95a0250SRodney W. Grimes 	}
8521d54b07SRodney W. Grimes }
86f95a0250SRodney W. Grimes 
87f95a0250SRodney W. Grimes int
8821d54b07SRodney W. Grimes _kvm_initvtop(kvm_t *kd) {
89f95a0250SRodney W. Grimes 	struct vmstate *vm;
9021d54b07SRodney W. Grimes 	struct nlist nlist[2];
91f95a0250SRodney W. Grimes 
92f95a0250SRodney W. Grimes 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
9321d54b07SRodney W. Grimes 	if (vm == 0) {
9421d54b07SRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot allocate vm");
95f95a0250SRodney W. Grimes 		return (-1);
9621d54b07SRodney W. Grimes 	}
97f95a0250SRodney W. Grimes 	kd->vmst = vm;
98f95a0250SRodney W. Grimes 
9921d54b07SRodney W. Grimes 	nlist[0].n_name = "_IdlePTD";
10021d54b07SRodney W. Grimes 	nlist[1].n_name = 0;
101f95a0250SRodney W. Grimes 
102f95a0250SRodney W. Grimes 	if (kvm_nlist(kd, nlist) != 0) {
103f95a0250SRodney W. Grimes 		_kvm_err(kd, kd->program, "bad namelist");
104f95a0250SRodney W. Grimes 		return (-1);
105f95a0250SRodney W. Grimes 	}
1063f318480SPoul-Henning Kamp 	vm->PTD = 0;
10721d54b07SRodney W. Grimes 	vm->IdlePTD = 0;
10821d54b07SRodney W. Grimes 	if (KREAD(kd, (u_long)nlist[0].n_value, &vm->IdlePTD)) {
10921d54b07SRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read IdlePTD");
110f95a0250SRodney W. Grimes 		return (-1);
111f95a0250SRodney W. Grimes 	}
1122572133eSPoul-Henning Kamp 	if ((vm->PTD = _kvm_malloc(kd, PAGE_SIZE /*sizeof(struct pde)*/)) != 0) {
11321d54b07SRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot allocate vm->PTD");
114f95a0250SRodney W. Grimes 	}
11521d54b07SRodney W. Grimes 	if (KREAD(kd, (u_long)nlist[1].n_value, &vm->PTD)) {
11621d54b07SRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read PTD");
117f95a0250SRodney W. Grimes 		return (-1);
118f95a0250SRodney W. Grimes 	}
119f95a0250SRodney W. Grimes 	return (0);
120f95a0250SRodney W. Grimes }
121f95a0250SRodney W. Grimes 
122f95a0250SRodney W. Grimes static int
12321d54b07SRodney W. Grimes _kvm_vatop(kvm_t *kd, u_long va, u_long *pa) {
124f95a0250SRodney W. Grimes 
125f95a0250SRodney W. Grimes 	if (ISALIVE(kd)) {
126f95a0250SRodney W. Grimes 		_kvm_err(kd, 0, "vatop called in live kernel!");
127f95a0250SRodney W. Grimes 		return((off_t)0);
128f95a0250SRodney W. Grimes 	}
129f95a0250SRodney W. Grimes 	_kvm_err(kd, 0, "invalid address (%x)", va);
13021d54b07SRodney W. Grimes 	return ((off_t)0);
131f95a0250SRodney W. Grimes }
132f95a0250SRodney W. Grimes 
133f95a0250SRodney W. Grimes int
13421d54b07SRodney W. Grimes _kvm_kvatop(kvm_t *kd, u_long va, u_long *pa) {
13521d54b07SRodney W. Grimes 	return (_kvm_vatop(kd, va, pa));
136f95a0250SRodney W. Grimes }
137