kvm_amd64.c revision 83551
118334Speter/*-
218334Speter * Copyright (c) 1989, 1992, 1993
318334Speter *	The Regents of the University of California.  All rights reserved.
418334Speter *
518334Speter * This code is derived from software developed by the Computer Systems
618334Speter * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
718334Speter * BG 91-66 and contributed to Berkeley.
818334Speter *
918334Speter * Redistribution and use in source and binary forms, with or without
1018334Speter * modification, are permitted provided that the following conditions
1118334Speter * are met:
1218334Speter * 1. Redistributions of source code must retain the above copyright
1318334Speter *    notice, this list of conditions and the following disclaimer.
1418334Speter * 2. Redistributions in binary form must reproduce the above copyright
1518334Speter *    notice, this list of conditions and the following disclaimer in the
1618334Speter *    documentation and/or other materials provided with the distribution.
1718334Speter * 3. All advertising materials mentioning features or use of this software
1818334Speter *    must display the following acknowledgement:
1918334Speter *	This product includes software developed by the University of
2018334Speter *	California, Berkeley and its contributors.
2118334Speter * 4. Neither the name of the University nor the names of its contributors
2218334Speter *    may be used to endorse or promote products derived from this software
2318334Speter *    without specific prior written permission.
2418334Speter *
2518334Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2618334Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2718334Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2818334Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2918334Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3018334Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3118334Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3218334Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3318334Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3418334Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3518334Speter * SUCH DAMAGE.
3618334Speter */
3718334Speter
3818334Speter#include <sys/cdefs.h>
3918334Speter__FBSDID("$FreeBSD: head/lib/libkvm/kvm_amd64.c 83551 2001-09-16 21:35:07Z dillon $");
4018334Speter
4118334Speter#if defined(LIBC_SCCS) && !defined(lint)
4218334Speter#if 0
4318334Speterstatic char sccsid[] = "@(#)kvm_hp300.c	8.1 (Berkeley) 6/4/93";
4418334Speter#endif
4518334Speter#endif /* LIBC_SCCS and not lint */
4618334Speter
4718334Speter/*
4818334Speter * i386 machine dependent routines for kvm.  Hopefully, the forthcoming
4918334Speter * vm code will one day obsolete this module.
5018334Speter */
5118334Speter
5218334Speter#include <sys/param.h>
5318334Speter#include <sys/lock.h>
5418334Speter#include <sys/mutex.h>
5518334Speter#include <sys/user.h>
5618334Speter#include <sys/proc.h>
5750397Sobrien#include <sys/stat.h>
5850397Sobrien#include <stdlib.h>
5950397Sobrien#include <unistd.h>
6018334Speter#include <nlist.h>
6118334Speter#include <kvm.h>
6218334Speter
6318334Speter#include <vm/vm.h>
6418334Speter#include <vm/vm_param.h>
6518334Speter
6618334Speter#include <limits.h>
6718334Speter
6818334Speter#include "kvm_private.h"
6918334Speter
7018334Speter#ifndef btop
7118334Speter#define	btop(x)		(i386_btop(x))
7250397Sobrien#define	ptob(x)		(i386_ptob(x))
7350397Sobrien#endif
7450397Sobrien
7550397Sobrienstruct vmstate {
7650397Sobrien	pd_entry_t	*PTD;
7750397Sobrien};
7850397Sobrien
7918334Spetervoid
8018334Speter_kvm_freevtop(kvm_t *kd)
8118334Speter{
8218334Speter	if (kd->vmst != 0) {
8318334Speter		if (kd->vmst->PTD) {
8450397Sobrien			free(kd->vmst->PTD);
8518334Speter		}
8618334Speter		free(kd->vmst);
87	}
88}
89
90int
91_kvm_initvtop(kvm_t *kd)
92{
93	struct vmstate *vm;
94	struct nlist nlist[2];
95	u_long pa;
96	u_long kernbase;
97	pd_entry_t	*PTD;
98
99	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
100	if (vm == 0) {
101		_kvm_err(kd, kd->program, "cannot allocate vm");
102		return (-1);
103	}
104	kd->vmst = vm;
105	vm->PTD = 0;
106
107	nlist[0].n_name = "kernbase";
108	nlist[1].n_name = 0;
109
110	if (kvm_nlist(kd, nlist) != 0)
111		kernbase = KERNBASE;	/* for old kernels */
112	else
113		kernbase = nlist[0].n_value;
114
115	nlist[0].n_name = "IdlePTD";
116	nlist[1].n_name = 0;
117
118	if (kvm_nlist(kd, nlist) != 0) {
119		_kvm_err(kd, kd->program, "bad namelist");
120		return (-1);
121	}
122	if (kvm_read(kd, (nlist[0].n_value - kernbase), &pa, sizeof(pa)) !=
123	    sizeof(pa)) {
124		_kvm_err(kd, kd->program, "cannot read IdlePTD");
125		return (-1);
126	}
127	PTD = _kvm_malloc(kd, PAGE_SIZE);
128	if (kvm_read(kd, pa, PTD, PAGE_SIZE) != PAGE_SIZE) {
129		_kvm_err(kd, kd->program, "cannot read PTD");
130		return (-1);
131	}
132	vm->PTD = PTD;
133	return (0);
134}
135
136static int
137_kvm_vatop(kvm_t *kd, u_long va, u_long *pa)
138{
139	struct vmstate *vm;
140	u_long offset;
141	u_long pte_pa;
142	pd_entry_t pde;
143	pt_entry_t pte;
144	u_long pdeindex;
145	u_long pteindex;
146	int i;
147
148	if (ISALIVE(kd)) {
149		_kvm_err(kd, 0, "vatop called in live kernel!");
150		return((off_t)0);
151	}
152
153	vm = kd->vmst;
154	offset = va & (PAGE_SIZE - 1);
155
156	/*
157	 * If we are initializing (kernel page table descriptor pointer
158	 * not yet set) then return pa == va to avoid infinite recursion.
159	 */
160	if (vm->PTD == 0) {
161		*pa = va;
162		return (PAGE_SIZE - offset);
163	}
164
165	pdeindex = va >> PDRSHIFT;
166	pde = vm->PTD[pdeindex];
167	if (((u_long)pde & PG_V) == 0)
168		goto invalid;
169
170	if ((u_long)pde & PG_PS) {
171	      /*
172	       * No second-level page table; ptd describes one 4MB page.
173	       * (We assume that the kernel wouldn't set PG_PS without enabling
174	       * it cr0, and that the kernel doesn't support 36-bit physical
175	       * addresses).
176	       */
177#define	PAGE4M_MASK	(NBPDR - 1)
178#define	PG_FRAME4M	(~PAGE4M_MASK)
179		*pa = ((u_long)pde & PG_FRAME4M) + (va & PAGE4M_MASK);
180		return (NBPDR - (va & PAGE4M_MASK));
181	}
182
183	pteindex = (va >> PAGE_SHIFT) & (NPTEPG-1);
184	pte_pa = ((u_long)pde & PG_FRAME) + (pteindex * sizeof(pt_entry_t));
185
186	/* XXX This has to be a physical address read, kvm_read is virtual */
187	if (lseek(kd->pmfd, pte_pa, 0) == -1) {
188		_kvm_syserr(kd, kd->program, "_kvm_vatop: lseek");
189		goto invalid;
190	}
191	if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) {
192		_kvm_syserr(kd, kd->program, "_kvm_vatop: read");
193		goto invalid;
194	}
195	if (((u_long)pte & PG_V) == 0)
196		goto invalid;
197
198	*pa = ((u_long)pte & PG_FRAME) + offset;
199	return (PAGE_SIZE - offset);
200
201invalid:
202	_kvm_err(kd, 0, "invalid address (%x)", va);
203	return (0);
204}
205
206int
207_kvm_kvatop(kvm_t *kd, u_long va, u_long *pa)
208{
209	return (_kvm_vatop(kd, va, pa));
210}
211