kvm_amd64.c revision 28318
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 { 7018798Speter pd_entry_t *PTD; 711602Srgrimes}; 721602Srgrimes 731602Srgrimesvoid 7418798Speter_kvm_freevtop(kvm_t *kd) 7518798Speter{ 761603Srgrimes if (kd->vmst != 0) { 7718798Speter if (kd->vmst->PTD) { 7818798Speter free(kd->vmst->PTD); 7918798Speter } 801602Srgrimes free(kd->vmst); 811603Srgrimes } 821602Srgrimes} 831602Srgrimes 841602Srgrimesint 8518798Speter_kvm_initvtop(kvm_t *kd) 8618798Speter{ 871602Srgrimes struct vmstate *vm; 881603Srgrimes struct nlist nlist[2]; 8918798Speter u_long pa; 9018798Speter pd_entry_t *PTD; 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; 9818798Speter vm->PTD = 0; 991602Srgrimes 1001603Srgrimes nlist[0].n_name = "_IdlePTD"; 1011603Srgrimes nlist[1].n_name = 0; 1021602Srgrimes 1031602Srgrimes if (kvm_nlist(kd, nlist) != 0) { 1041602Srgrimes _kvm_err(kd, kd->program, "bad namelist"); 1051602Srgrimes return (-1); 1061602Srgrimes } 10718798Speter if (kvm_read(kd, (nlist[0].n_value - KERNBASE), &pa, sizeof(pa)) != sizeof(pa)) { 1081603Srgrimes _kvm_err(kd, kd->program, "cannot read IdlePTD"); 1091602Srgrimes return (-1); 1101602Srgrimes } 11118798Speter PTD = _kvm_malloc(kd, PAGE_SIZE); 11218798Speter if (kvm_read(kd, pa, PTD, PAGE_SIZE) != PAGE_SIZE) { 1131603Srgrimes _kvm_err(kd, kd->program, "cannot read PTD"); 1141602Srgrimes return (-1); 1151602Srgrimes } 11618798Speter vm->PTD = PTD; 1171602Srgrimes return (0); 1181602Srgrimes} 1191602Srgrimes 1201602Srgrimesstatic int 12118798Speter_kvm_vatop(kvm_t *kd, u_long va, u_long *pa) 12218798Speter{ 12318798Speter struct vmstate *vm; 12418798Speter u_long offset; 12518798Speter u_long pte_pa; 12618798Speter pd_entry_t pde; 12718798Speter pt_entry_t pte; 12818798Speter u_long pdeindex; 12918798Speter u_long pteindex; 13018798Speter int i; 1311602Srgrimes 1321602Srgrimes if (ISALIVE(kd)) { 1331602Srgrimes _kvm_err(kd, 0, "vatop called in live kernel!"); 1341602Srgrimes return((off_t)0); 1351602Srgrimes } 13618798Speter 13718798Speter vm = kd->vmst; 13818798Speter offset = va & (PAGE_SIZE - 1); 13918798Speter 14018798Speter /* 14118798Speter * If we are initializing (kernel page table descriptor pointer 14218798Speter * not yet set) then return pa == va to avoid infinite recursion. 14318798Speter */ 14418798Speter if (vm->PTD == 0) { 14518798Speter *pa = va; 14618798Speter return (PAGE_SIZE - offset); 14718798Speter } 14818798Speter 14918798Speter pdeindex = va >> PDRSHIFT; 15018798Speter pde = vm->PTD[pdeindex]; 15118798Speter if (((u_long)pde & PG_V) == 0) 15218798Speter goto invalid; 15318798Speter 15428318Stegge if ((u_long)pde & PG_PS) { 15528318Stegge /* 15628318Stegge * No second-level page table; ptd describes one 4MB page. 15728318Stegge * (We assume that the kernel wouldn't set PG_PS without enabling 15828318Stegge * it cr0, and that the kernel doesn't support 36-bit physical 15928318Stegge * addresses). 16028318Stegge */ 16128318Stegge#define PAGE4M_MASK (NBPDR - 1) 16228318Stegge#define PG_FRAME4M (~PAGE4M_MASK) 16328318Stegge *pa = ((u_long)pde & PG_FRAME4M) + (va & PAGE4M_MASK); 16428318Stegge return (NBPDR - (va & PAGE4M_MASK)); 16528318Stegge } 16628318Stegge 16718798Speter pteindex = (va >> PAGE_SHIFT) & (NPTEPG-1); 16818798Speter pte_pa = ((u_long)pde & PG_FRAME) + (pteindex * sizeof(pt_entry_t)); 16918798Speter 17018798Speter /* XXX This has to be a physical address read, kvm_read is virtual */ 17118798Speter if (lseek(kd->pmfd, pte_pa, 0) == -1) { 17218798Speter _kvm_syserr(kd, kd->program, "_kvm_vatop: lseek"); 17318798Speter goto invalid; 17418798Speter } 17518798Speter if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) { 17618798Speter _kvm_syserr(kd, kd->program, "_kvm_vatop: read"); 17718798Speter goto invalid; 17818798Speter } 17918798Speter if (((u_long)pte & PG_V) == 0) 18018798Speter goto invalid; 18118798Speter 18218798Speter *pa = ((u_long)pte & PG_FRAME) + offset; 18318798Speter return (PAGE_SIZE - offset); 18418798Speter 18518798Speterinvalid: 1861602Srgrimes _kvm_err(kd, 0, "invalid address (%x)", va); 18718798Speter return (0); 1881602Srgrimes} 1891602Srgrimes 1901602Srgrimesint 19118798Speter_kvm_kvatop(kvm_t *kd, u_long va, u_long *pa) 19218798Speter{ 1931603Srgrimes return (_kvm_vatop(kd, va, pa)); 1941602Srgrimes} 195