1*0e64ee4cSderaadt /* $OpenBSD: kvm_i386.c,v 1.28 2021/12/01 16:53:28 deraadt Exp $ */
215f0ebb0Sderaadt /* $NetBSD: kvm_i386.c,v 1.9 1996/03/18 22:33:38 thorpej Exp $ */
33fbbad10Sniklas
4df930be7Sderaadt /*-
5df930be7Sderaadt * Copyright (c) 1989, 1992, 1993
6df930be7Sderaadt * The Regents of the University of California. All rights reserved.
7df930be7Sderaadt *
8df930be7Sderaadt * This code is derived from software developed by the Computer Systems
9df930be7Sderaadt * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
10df930be7Sderaadt * BG 91-66 and contributed to Berkeley.
11df930be7Sderaadt *
12df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
13df930be7Sderaadt * modification, are permitted provided that the following conditions
14df930be7Sderaadt * are met:
15df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright
16df930be7Sderaadt * notice, this list of conditions and the following disclaimer.
17df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
18df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the
19df930be7Sderaadt * documentation and/or other materials provided with the distribution.
206580fee3Smillert * 3. Neither the name of the University nor the names of its contributors
21df930be7Sderaadt * may be used to endorse or promote products derived from this software
22df930be7Sderaadt * without specific prior written permission.
23df930be7Sderaadt *
24df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df930be7Sderaadt * SUCH DAMAGE.
35df930be7Sderaadt */
36df930be7Sderaadt
37df930be7Sderaadt /*
38df930be7Sderaadt * i386 machine dependent routines for kvm. Hopefully, the forthcoming
39df930be7Sderaadt * vm code will one day obsolete this module.
40df930be7Sderaadt */
41df930be7Sderaadt
42*0e64ee4cSderaadt #include <sys/types.h>
43*0e64ee4cSderaadt #include <sys/signal.h>
44df930be7Sderaadt #include <sys/proc.h>
45df930be7Sderaadt #include <sys/stat.h>
46df930be7Sderaadt #include <stdlib.h>
47df930be7Sderaadt #include <unistd.h>
48df930be7Sderaadt #include <nlist.h>
49df930be7Sderaadt #include <kvm.h>
50df930be7Sderaadt
5179f7dc0bSart #include <uvm/uvm_extern.h>
52*0e64ee4cSderaadt #include <machine/param.h>
5371007b88Sart #include <machine/vmparam.h>
5471007b88Sart #include <machine/pmap.h>
55df930be7Sderaadt
56df930be7Sderaadt #include <limits.h>
57df930be7Sderaadt #include <db.h>
58df930be7Sderaadt
59df930be7Sderaadt #include "kvm_private.h"
60df930be7Sderaadt
61df930be7Sderaadt #include <machine/pte.h>
62df930be7Sderaadt
63d7c9b95fSmlarkin /*
644d148712Sguenther * We access both normal and PAE entries in 32bit chunks.
654d148712Sguenther * Use a local name to avoid conflicting with the kernel's maybe-public,
664d148712Sguenther * maybe-not p[td]_entry_t typedefs.
674d148712Sguenther */
684d148712Sguenther typedef u_long ptd_entry_t;
694d148712Sguenther
704d148712Sguenther /*
71d7c9b95fSmlarkin * These must match the values in pmap.c/pmapae.c
724d148712Sguenther * First the non-PAE versions
73d7c9b95fSmlarkin */
74d7c9b95fSmlarkin #define PD_MASK 0xffc00000 /* page directory address bits */
75d7c9b95fSmlarkin #define PT_MASK 0x003ff000 /* page table address bits */
76d7c9b95fSmlarkin
774d148712Sguenther /*
784d148712Sguenther * PAE versions
794d148712Sguenther *
804d148712Sguenther * paddr_t is still 32bits, so the top 32bits of PDEs and PTEs only
814d148712Sguenther * matters for the NX bit...which libkvm doesn't care about
824d148712Sguenther */
834d148712Sguenther #define PAE_PDSHIFT 21
844d148712Sguenther #define PAE_PD_MASK 0xffe00000 /* page directory address bits */
854d148712Sguenther #define PAE_PT_MASK 0x001ff000 /* page table address bits */
864d148712Sguenther
874d148712Sguenther #define PG_FRAME 0xfffff000
884d148712Sguenther
894d148712Sguenther static int cpu_pae;
90d7c9b95fSmlarkin
91df930be7Sderaadt struct vmstate {
924d148712Sguenther ptd_entry_t *PTD;
934d148712Sguenther ptd_entry_t PD_mask;
944d148712Sguenther ptd_entry_t PT_mask;
954d148712Sguenther int PD_shift;
964d148712Sguenther int PG_shift;
97df930be7Sderaadt };
98df930be7Sderaadt
994d148712Sguenther #define pdei(vm,VA) (((VA) & (vm)->PD_mask) >> (vm)->PD_shift)
1004d148712Sguenther #define ptei(vm,VA) (((VA) & (vm)->PT_mask) >> PAGE_SHIFT)
1014d148712Sguenther
102df930be7Sderaadt void
_kvm_freevtop(kvm_t * kd)103551fad64Sderaadt _kvm_freevtop(kvm_t *kd)
104df930be7Sderaadt {
105551fad64Sderaadt if (kd->vmst != NULL) {
106df930be7Sderaadt free(kd->vmst->PTD);
107df930be7Sderaadt
108df930be7Sderaadt free(kd->vmst);
109551fad64Sderaadt kd->vmst = NULL;
110df930be7Sderaadt }
111df930be7Sderaadt }
112df930be7Sderaadt
113df930be7Sderaadt int
_kvm_initvtop(kvm_t * kd)114551fad64Sderaadt _kvm_initvtop(kvm_t *kd)
115df930be7Sderaadt {
1164d148712Sguenther struct nlist nl[4];
117551fad64Sderaadt struct vmstate *vm;
1184d148712Sguenther u_long pa, PTDsize;
119df930be7Sderaadt
1204d148712Sguenther vm = _kvm_malloc(kd, sizeof(*vm));
121566036e0Skettenis if (vm == NULL)
122df930be7Sderaadt return (-1);
123df930be7Sderaadt kd->vmst = vm;
124df930be7Sderaadt
125566036e0Skettenis vm->PTD = NULL;
126566036e0Skettenis
12761b60ac2Skettenis nl[0].n_name = "_PTDpaddr";
1284d148712Sguenther nl[1].n_name = "_PTDsize";
1294d148712Sguenther nl[2].n_name = "_cpu_pae";
1304d148712Sguenther nl[3].n_name = NULL;
131df930be7Sderaadt
13261b60ac2Skettenis if (kvm_nlist(kd, nl) != 0) {
133df930be7Sderaadt _kvm_err(kd, kd->program, "bad namelist");
134df930be7Sderaadt return (-1);
135df930be7Sderaadt }
136df930be7Sderaadt
1374d148712Sguenther if (_kvm_pread(kd, kd->pmfd, &cpu_pae, sizeof cpu_pae,
1384d148712Sguenther _kvm_pa2off(kd, nl[2].n_value - KERNBASE)) != sizeof cpu_pae)
1394d148712Sguenther goto invalid;
1404d148712Sguenther
1414d148712Sguenther if (_kvm_pread(kd, kd->pmfd, &PTDsize, sizeof PTDsize,
1424d148712Sguenther _kvm_pa2off(kd, nl[1].n_value - KERNBASE)) != sizeof PTDsize)
1434d148712Sguenther goto invalid;
1444d148712Sguenther
1457af97067Stom if (_kvm_pread(kd, kd->pmfd, &pa, sizeof pa,
1464d148712Sguenther _kvm_pa2off(kd, nl[0].n_value - KERNBASE)) != sizeof pa)
1477af97067Stom goto invalid;
148df930be7Sderaadt
1494d148712Sguenther vm->PTD = _kvm_malloc(kd, PTDsize);
1503fbbad10Sniklas
1514d148712Sguenther if (_kvm_pread(kd, kd->pmfd, vm->PTD, PTDsize,
1524d148712Sguenther _kvm_pa2off(kd, pa)) != PTDsize)
1537af97067Stom goto invalid;
1543fbbad10Sniklas
1554d148712Sguenther if (cpu_pae) {
1564d148712Sguenther vm->PD_mask = PAE_PD_MASK;
1574d148712Sguenther vm->PT_mask = PAE_PT_MASK;
1584d148712Sguenther /* -1 here because entries are twice as large */
1594d148712Sguenther vm->PD_shift = PAE_PDSHIFT - 1;
1604d148712Sguenther vm->PG_shift = PAGE_SHIFT - 1;
1614d148712Sguenther } else {
1624d148712Sguenther vm->PD_mask = PD_MASK;
1634d148712Sguenther vm->PT_mask = PT_MASK;
1644d148712Sguenther vm->PD_shift = PDSHIFT;
1654d148712Sguenther vm->PG_shift = PAGE_SHIFT;
1664d148712Sguenther }
1674d148712Sguenther
1687af97067Stom return (0);
1693fbbad10Sniklas
1707af97067Stom invalid:
1713fbbad10Sniklas free(vm->PTD);
172566036e0Skettenis vm->PTD = NULL;
1737af97067Stom return (-1);
174bb64e08dSmickey }
175bb64e08dSmickey
176df930be7Sderaadt /*
177df930be7Sderaadt * Translate a kernel virtual address to a physical address.
178df930be7Sderaadt */
179df930be7Sderaadt int
_kvm_kvatop(kvm_t * kd,u_long va,paddr_t * pa)180fdd3f45bSmickey _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
181df930be7Sderaadt {
1827af97067Stom u_long offset, pte_pa;
183df930be7Sderaadt struct vmstate *vm;
1844d148712Sguenther ptd_entry_t pte;
185df930be7Sderaadt
1863ad56321Smickey if (!kd->vmst) {
1873ad56321Smickey _kvm_err(kd, 0, "vatop called before initvtop");
1883ad56321Smickey return (0);
1893ad56321Smickey }
1903ad56321Smickey
191df930be7Sderaadt if (ISALIVE(kd)) {
192df930be7Sderaadt _kvm_err(kd, 0, "vatop called in live kernel!");
193df930be7Sderaadt return (0);
194df930be7Sderaadt }
195df930be7Sderaadt
196df930be7Sderaadt vm = kd->vmst;
1977468fac6Sderaadt offset = va & (kd->nbpg - 1);
198df930be7Sderaadt
199df930be7Sderaadt /*
200df930be7Sderaadt * If we are initializing (kernel page table descriptor pointer
201df930be7Sderaadt * not yet set) * then return pa == va to avoid infinite recursion.
202df930be7Sderaadt */
203566036e0Skettenis if (vm->PTD == NULL) {
204df930be7Sderaadt *pa = va;
2057468fac6Sderaadt return (kd->nbpg - (int)offset);
206df930be7Sderaadt }
2074d148712Sguenther if ((vm->PTD[pdei(vm,va)] & PG_V) == 0)
208df930be7Sderaadt goto invalid;
209df930be7Sderaadt
2104d148712Sguenther pte_pa = (vm->PTD[pdei(vm,va)] & PG_FRAME) +
2114d148712Sguenther (ptei(vm,va) * sizeof(ptd_entry_t));
212551fad64Sderaadt
213df930be7Sderaadt /* XXX READ PHYSICAL XXX */
2147af97067Stom if (_kvm_pread(kd, kd->pmfd, &pte, sizeof pte,
2154d148712Sguenther _kvm_pa2off(kd, pte_pa)) != sizeof pte)
216df930be7Sderaadt goto invalid;
217df930be7Sderaadt
2184d148712Sguenther if ((pte & PG_V) == 0)
2194d148712Sguenther goto invalid;
2207af97067Stom *pa = (pte & PG_FRAME) + offset;
2217468fac6Sderaadt return (kd->nbpg - (int)offset);
222df930be7Sderaadt
223df930be7Sderaadt invalid:
22403f48ff6Sniklas _kvm_err(kd, 0, "invalid address (%lx)", va);
225df930be7Sderaadt return (0);
226df930be7Sderaadt }
22758ece82cSmillert
22858ece82cSmillert /*
2298e285992Smickey * Translate a physical address to a file-offset in the crash-dump.
23058ece82cSmillert */
23158ece82cSmillert off_t
_kvm_pa2off(kvm_t * kd,paddr_t pa)232fdd3f45bSmickey _kvm_pa2off(kvm_t *kd, paddr_t pa)
23358ece82cSmillert {
2348e285992Smickey return ((off_t)(kd->dump_off + pa));
23558ece82cSmillert }
236