kvm_amd64.c revision 17141
11602Srgrimes/*-
21602Srgrimes * Copyright (c) 1989, 1992, 1993
31602Srgrimes *	The Regents of the University of California.  All rights reserved.
41602Srgrimes *
51602Srgrimes * This code is derived from software developed by the Computer Systems
61602Srgrimes * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
71602Srgrimes * BG 91-66 and contributed to Berkeley.
81602Srgrimes *
91602Srgrimes * Redistribution and use in source and binary forms, with or without
101602Srgrimes * modification, are permitted provided that the following conditions
111602Srgrimes * are met:
121602Srgrimes * 1. Redistributions of source code must retain the above copyright
131602Srgrimes *    notice, this list of conditions and the following disclaimer.
141602Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151602Srgrimes *    notice, this list of conditions and the following disclaimer in the
161602Srgrimes *    documentation and/or other materials provided with the distribution.
171602Srgrimes * 3. All advertising materials mentioning features or use of this software
181602Srgrimes *    must display the following acknowledgement:
191602Srgrimes *	This product includes software developed by the University of
201602Srgrimes *	California, Berkeley and its contributors.
211602Srgrimes * 4. Neither the name of the University nor the names of its contributors
221602Srgrimes *    may be used to endorse or promote products derived from this software
231602Srgrimes *    without specific prior written permission.
241602Srgrimes *
251602Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
261602Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
271602Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281602Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291602Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301602Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311602Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
321602Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
331602Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
341602Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
351602Srgrimes * SUCH DAMAGE.
361602Srgrimes */
371602Srgrimes
381602Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
391602Srgrimesstatic char sccsid[] = "@(#)kvm_hp300.c	8.1 (Berkeley) 6/4/93";
401602Srgrimes#endif /* LIBC_SCCS and not lint */
411602Srgrimes
421602Srgrimes/*
438870Srgrimes * i386 machine dependent routines for kvm.  Hopefully, the forthcoming
441602Srgrimes * vm code will one day obsolete this module.
451602Srgrimes */
461602Srgrimes
471602Srgrimes#include <sys/param.h>
481602Srgrimes#include <sys/user.h>
491602Srgrimes#include <sys/proc.h>
501602Srgrimes#include <sys/stat.h>
5117141Sjkh#include <stdlib.h>
521602Srgrimes#include <unistd.h>
531602Srgrimes#include <nlist.h>
541602Srgrimes#include <kvm.h>
551602Srgrimes
561602Srgrimes#include <vm/vm.h>
571602Srgrimes#include <vm/vm_param.h>
581602Srgrimes
591602Srgrimes#include <limits.h>
601602Srgrimes#include <db.h>
611602Srgrimes
621602Srgrimes#include "kvm_private.h"
631602Srgrimes
641602Srgrimes#ifndef btop
651603Srgrimes#define	btop(x)		(i386_btop(x))
661603Srgrimes#define	ptob(x)		(i386_ptob(x))
671602Srgrimes#endif
681602Srgrimes
691602Srgrimesstruct vmstate {
701603Srgrimes	struct pde	**IdlePTD;
711603Srgrimes	struct pde	*PTD;
721602Srgrimes};
731602Srgrimes
741602Srgrimes#define KREAD(kd, addr, p)\
751602Srgrimes	(kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
761602Srgrimes
771602Srgrimesvoid
781603Srgrimes_kvm_freevtop(kvm_t *kd) {
791603Srgrimes	if (kd->vmst->PTD) {
801603Srgrimes		free(kd->vmst->PTD);
811603Srgrimes	}
821603Srgrimes	if (kd->vmst != 0) {
831602Srgrimes		free(kd->vmst);
841603Srgrimes	}
851602Srgrimes}
861602Srgrimes
871602Srgrimesint
881603Srgrimes_kvm_initvtop(kvm_t *kd) {
891602Srgrimes	struct vmstate *vm;
901603Srgrimes	struct nlist nlist[2];
911602Srgrimes
921602Srgrimes	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
931603Srgrimes	if (vm == 0) {
941603Srgrimes		_kvm_err(kd, kd->program, "cannot allocate vm");
951602Srgrimes		return (-1);
961603Srgrimes	}
971602Srgrimes	kd->vmst = vm;
981602Srgrimes
991603Srgrimes	nlist[0].n_name = "_IdlePTD";
1001603Srgrimes	nlist[1].n_name = 0;
1011602Srgrimes
1021602Srgrimes	if (kvm_nlist(kd, nlist) != 0) {
1031602Srgrimes		_kvm_err(kd, kd->program, "bad namelist");
1041602Srgrimes		return (-1);
1051602Srgrimes	}
1064241Sphk	vm->PTD = 0;
1071603Srgrimes	vm->IdlePTD = 0;
1081603Srgrimes	if (KREAD(kd, (u_long)nlist[0].n_value, &vm->IdlePTD)) {
1091603Srgrimes		_kvm_err(kd, kd->program, "cannot read IdlePTD");
1101602Srgrimes		return (-1);
1111602Srgrimes	}
11215533Sphk	if ((vm->PTD = _kvm_malloc(kd, PAGE_SIZE /*sizeof(struct pde)*/)) != 0) {
1131603Srgrimes		_kvm_err(kd, kd->program, "cannot allocate vm->PTD");
1141602Srgrimes	}
1151603Srgrimes	if (KREAD(kd, (u_long)nlist[1].n_value, &vm->PTD)) {
1161603Srgrimes		_kvm_err(kd, kd->program, "cannot read PTD");
1171602Srgrimes		return (-1);
1181602Srgrimes	}
1191602Srgrimes	return (0);
1201602Srgrimes}
1211602Srgrimes
1221602Srgrimesstatic int
1231603Srgrimes_kvm_vatop(kvm_t *kd, u_long va, u_long *pa) {
1241602Srgrimes
1251602Srgrimes	if (ISALIVE(kd)) {
1261602Srgrimes		_kvm_err(kd, 0, "vatop called in live kernel!");
1271602Srgrimes		return((off_t)0);
1281602Srgrimes	}
1291602Srgrimes	_kvm_err(kd, 0, "invalid address (%x)", va);
1301603Srgrimes	return ((off_t)0);
1311602Srgrimes}
1321602Srgrimes
1331602Srgrimesint
1341603Srgrimes_kvm_kvatop(kvm_t *kd, u_long va, u_long *pa) {
1351603Srgrimes	return (_kvm_vatop(kd, va, pa));
1361602Srgrimes}
137