kvm_amd64.c revision 1603
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/*
431603Srgrimes * 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>
511602Srgrimes#include <unistd.h>
521602Srgrimes#include <nlist.h>
531602Srgrimes#include <kvm.h>
541602Srgrimes
551602Srgrimes#include <vm/vm.h>
561602Srgrimes#include <vm/vm_param.h>
571602Srgrimes
581602Srgrimes#include <limits.h>
591602Srgrimes#include <db.h>
601602Srgrimes
611602Srgrimes#include "kvm_private.h"
621602Srgrimes
631602Srgrimes#ifndef btop
641603Srgrimes#define	btop(x)		(i386_btop(x))
651603Srgrimes#define	ptob(x)		(i386_ptob(x))
661602Srgrimes#endif
671602Srgrimes
681602Srgrimesstruct vmstate {
691603Srgrimes	struct pde	**IdlePTD;
701603Srgrimes	struct pde	*PTD;
711602Srgrimes};
721602Srgrimes
731602Srgrimes#define KREAD(kd, addr, p)\
741602Srgrimes	(kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
751602Srgrimes
761602Srgrimesvoid
771603Srgrimes_kvm_freevtop(kvm_t *kd) {
781603Srgrimes	if (kd->vmst->PTD) {
791603Srgrimes		free(kd->vmst->PTD);
801603Srgrimes	}
811603Srgrimes	if (kd->vmst != 0) {
821602Srgrimes		free(kd->vmst);
831603Srgrimes	}
841602Srgrimes}
851602Srgrimes
861602Srgrimesint
871603Srgrimes_kvm_initvtop(kvm_t *kd) {
881602Srgrimes	struct vmstate *vm;
891603Srgrimes	struct nlist nlist[2];
901602Srgrimes
911602Srgrimes	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
921603Srgrimes	if (vm == 0) {
931603Srgrimes		_kvm_err(kd, kd->program, "cannot allocate vm");
941602Srgrimes		return (-1);
951603Srgrimes	}
961602Srgrimes	kd->vmst = vm;
971602Srgrimes
981603Srgrimes	nlist[0].n_name = "_IdlePTD";
991603Srgrimes	nlist[1].n_name = 0;
1001602Srgrimes
1011602Srgrimes	if (kvm_nlist(kd, nlist) != 0) {
1021602Srgrimes		_kvm_err(kd, kd->program, "bad namelist");
1031602Srgrimes		return (-1);
1041602Srgrimes	}
1051603Srgrimes	vm->IdlePTD = 0;
1061603Srgrimes	if (KREAD(kd, (u_long)nlist[0].n_value, &vm->IdlePTD)) {
1071603Srgrimes		_kvm_err(kd, kd->program, "cannot read IdlePTD");
1081602Srgrimes		return (-1);
1091602Srgrimes	}
1101603Srgrimes	if ((vm->PTD = _kvm_malloc(kd, NBPG /*sizeof(struct pde)*/)) != 0) {
1111603Srgrimes		_kvm_err(kd, kd->program, "cannot allocate vm->PTD");
1121602Srgrimes	}
1131603Srgrimes	if (KREAD(kd, (u_long)nlist[1].n_value, &vm->PTD)) {
1141603Srgrimes		_kvm_err(kd, kd->program, "cannot read PTD");
1151602Srgrimes		return (-1);
1161602Srgrimes	}
1171602Srgrimes	return (0);
1181602Srgrimes}
1191602Srgrimes
1201602Srgrimesstatic int
1211603Srgrimes_kvm_vatop(kvm_t *kd, u_long va, u_long *pa) {
1221602Srgrimes
1231602Srgrimes	if (ISALIVE(kd)) {
1241602Srgrimes		_kvm_err(kd, 0, "vatop called in live kernel!");
1251602Srgrimes		return((off_t)0);
1261602Srgrimes	}
1271602Srgrimes	_kvm_err(kd, 0, "invalid address (%x)", va);
1281603Srgrimes	return ((off_t)0);
1291602Srgrimes}
1301602Srgrimes
1311602Srgrimesint
1321603Srgrimes_kvm_kvatop(kvm_t *kd, u_long va, u_long *pa) {
1331603Srgrimes	return (_kvm_vatop(kd, va, pa));
1341602Srgrimes}
1351602Srgrimes
1361602Srgrimes/*
1371602Srgrimes * Translate a user virtual address to a physical address.
1381602Srgrimes */
1391602Srgrimesint
1401603Srgrimes_kvm_uvatop(kvm_t *kd, const struct proc *p, u_long va, u_long *pa) {
1411602Srgrimes	if (ISALIVE(kd)) {
1421603Srgrimes		/* Not done yet */
1431603Srgrimes	} else {
1441603Srgrimes		/* Not done yet */
1451602Srgrimes	}
1461603Srgrimes	return ((off_t)(0));
1471602Srgrimes}
148