1*104ea677Schristos /* $NetBSD: kvm_i386.c,v 1.32 2022/01/10 19:51:30 christos Exp $ */
2346e67f8Sthorpej
30215cc7dScgd /*-
40215cc7dScgd * Copyright (c) 1989, 1992, 1993
50215cc7dScgd * The Regents of the University of California. All rights reserved.
60215cc7dScgd *
70215cc7dScgd * This code is derived from software developed by the Computer Systems
80215cc7dScgd * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
90215cc7dScgd * BG 91-66 and contributed to Berkeley.
100215cc7dScgd *
110215cc7dScgd * Redistribution and use in source and binary forms, with or without
120215cc7dScgd * modification, are permitted provided that the following conditions
130215cc7dScgd * are met:
140215cc7dScgd * 1. Redistributions of source code must retain the above copyright
150215cc7dScgd * notice, this list of conditions and the following disclaimer.
160215cc7dScgd * 2. Redistributions in binary form must reproduce the above copyright
170215cc7dScgd * notice, this list of conditions and the following disclaimer in the
180215cc7dScgd * documentation and/or other materials provided with the distribution.
19eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
200215cc7dScgd * may be used to endorse or promote products derived from this software
210215cc7dScgd * without specific prior written permission.
220215cc7dScgd *
230215cc7dScgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
240215cc7dScgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
250215cc7dScgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
260215cc7dScgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
270215cc7dScgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
280215cc7dScgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
290215cc7dScgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
300215cc7dScgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
310215cc7dScgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
320215cc7dScgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
330215cc7dScgd * SUCH DAMAGE.
340215cc7dScgd */
350215cc7dScgd
36b4119f6bSmikel #include <sys/cdefs.h>
370215cc7dScgd #if defined(LIBC_SCCS) && !defined(lint)
38346e67f8Sthorpej #if 0
39346e67f8Sthorpej static char sccsid[] = "@(#)kvm_hp300.c 8.1 (Berkeley) 6/4/93";
40346e67f8Sthorpej #else
41*104ea677Schristos __RCSID("$NetBSD: kvm_i386.c,v 1.32 2022/01/10 19:51:30 christos Exp $");
42346e67f8Sthorpej #endif
430215cc7dScgd #endif /* LIBC_SCCS and not lint */
440215cc7dScgd
450215cc7dScgd /*
469a9b6113Sthorpej * i386 machine dependent routines for kvm.
470215cc7dScgd */
480215cc7dScgd
490215cc7dScgd #include <sys/param.h>
500215cc7dScgd #include <sys/stat.h>
519a9b6113Sthorpej #include <sys/kcore.h>
52962a341dSjym #include <sys/types.h>
53962a341dSjym
5464b09480Scgd #include <stdlib.h>
550215cc7dScgd #include <unistd.h>
560215cc7dScgd #include <nlist.h>
570215cc7dScgd #include <kvm.h>
580215cc7dScgd
593b8ac18dSmrg #include <uvm/uvm_extern.h>
600215cc7dScgd
610215cc7dScgd #include <limits.h>
620215cc7dScgd #include <db.h>
630215cc7dScgd
640215cc7dScgd #include "kvm_private.h"
650215cc7dScgd
66962a341dSjym #include <i386/kcore.h>
674e9d08baSmrg #include <i386/pmap.h>
684e9d08baSmrg #include <i386/pte.h>
694e9d08baSmrg #include <i386/vmparam.h>
700215cc7dScgd
710215cc7dScgd #ifndef btop
720215cc7dScgd #define btop(x) (((unsigned)(x)) >> PGSHIFT) /* XXX */
730215cc7dScgd #define ptob(x) ((caddr_t)((x) << PGSHIFT)) /* XXX */
740215cc7dScgd #endif
750215cc7dScgd
76f15c6971Sjym /*
77f15c6971Sjym * Indicates whether PAE is in use for the kernel image
78f15c6971Sjym * 0: native i386 memory mappings
79f15c6971Sjym * 1: i386 PAE mappings
80f15c6971Sjym */
81f15c6971Sjym static int i386_use_pae;
82f15c6971Sjym
83f15c6971Sjym int _kvm_kvatop_i386(kvm_t *, vaddr_t, paddr_t *);
84f15c6971Sjym int _kvm_kvatop_i386pae(kvm_t *, vaddr_t, paddr_t *);
85f15c6971Sjym
860215cc7dScgd void
_kvm_freevtop(kvm_t * kd)876dc46b92Sjym _kvm_freevtop(kvm_t *kd)
880215cc7dScgd {
890215cc7dScgd
909a9b6113Sthorpej /* Not actually used for anything right now, but safe. */
919a9b6113Sthorpej if (kd->vmst != 0)
920215cc7dScgd free(kd->vmst);
930215cc7dScgd }
940215cc7dScgd
95cc7ffa0dSchristos /*ARGSUSED*/
960215cc7dScgd int
_kvm_initvtop(kvm_t * kd)976dc46b92Sjym _kvm_initvtop(kvm_t *kd)
980215cc7dScgd {
99f15c6971Sjym cpu_kcore_hdr_t *cpu_kh = kd->cpu_data;
100f15c6971Sjym
101f15c6971Sjym i386_use_pae = 0; /* default: non PAE mode */
102f15c6971Sjym if ((cpu_kh->pdppaddr & I386_KCORE_PAE) == I386_KCORE_PAE)
103f15c6971Sjym i386_use_pae = 1;
104bd267b1bSmycroft
1056dc46b92Sjym return 0;
1060215cc7dScgd }
1070215cc7dScgd
1080215cc7dScgd /*
1090215cc7dScgd * Translate a kernel virtual address to a physical address.
1100215cc7dScgd */
1110215cc7dScgd int
_kvm_kvatop(kvm_t * kd,vaddr_t va,paddr_t * pa)112962a341dSjym _kvm_kvatop(kvm_t *kd, vaddr_t va, paddr_t *pa)
1130215cc7dScgd {
1140215cc7dScgd
1150215cc7dScgd if (ISALIVE(kd)) {
1160215cc7dScgd _kvm_err(kd, 0, "vatop called in live kernel!");
117962a341dSjym return 0;
1180215cc7dScgd }
11964b09480Scgd
120f15c6971Sjym switch (i386_use_pae) {
121f15c6971Sjym default:
122f15c6971Sjym case 0:
123f15c6971Sjym return _kvm_kvatop_i386(kd, va, pa);
124f15c6971Sjym case 1:
125f15c6971Sjym return _kvm_kvatop_i386pae(kd, va, pa);
126f15c6971Sjym }
127f15c6971Sjym
128f15c6971Sjym }
129f15c6971Sjym
130f15c6971Sjym /*
131f15c6971Sjym * Used to translate a virtual address to a physical address for systems
132f15c6971Sjym * with PAE mode disabled. Only two levels of virtual memory pages are
133f15c6971Sjym * dereferenced (L2 PDEs, then L1 PTEs).
134f15c6971Sjym */
135f15c6971Sjym int
_kvm_kvatop_i386(kvm_t * kd,vaddr_t va,paddr_t * pa)136f15c6971Sjym _kvm_kvatop_i386(kvm_t *kd, vaddr_t va, paddr_t *pa)
137f15c6971Sjym {
138f15c6971Sjym cpu_kcore_hdr_t *cpu_kh;
139f15c6971Sjym u_long page_off;
140f15c6971Sjym pd_entry_t pde;
141f15c6971Sjym pt_entry_t pte;
142f15c6971Sjym paddr_t pde_pa, pte_pa;
143f15c6971Sjym
1449a9b6113Sthorpej cpu_kh = kd->cpu_data;
1459a9b6113Sthorpej page_off = va & PGOFSET;
1460215cc7dScgd
14764b09480Scgd /*
1489a9b6113Sthorpej * Find and read the page directory entry.
149f15c6971Sjym * pdppaddr being PAGE_SIZE aligned, we mask the option bits.
15064b09480Scgd */
15126cfc3f1Smaxv pde_pa = (cpu_kh->pdppaddr & PTE_FRAME) + (pl2_pi(va) * sizeof(pde));
152a7a2d171Sad if (_kvm_pread(kd, kd->pmfd, (void *)&pde, sizeof(pde),
153cc7ffa0dSchristos _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) {
1549a9b6113Sthorpej _kvm_syserr(kd, 0, "could not read PDE");
1559a9b6113Sthorpej goto lose;
15664b09480Scgd }
15764b09480Scgd
1589a9b6113Sthorpej /*
1599a9b6113Sthorpej * Find and read the page table entry.
1609a9b6113Sthorpej */
16126cfc3f1Smaxv if ((pde & PTE_P) == 0) {
1629a9b6113Sthorpej _kvm_err(kd, 0, "invalid translation (invalid PDE)");
1639a9b6113Sthorpej goto lose;
1649a9b6113Sthorpej }
16526cfc3f1Smaxv if ((pde & PTE_PS) != 0) {
1661717a7feSjld /*
1671717a7feSjld * This is a 4MB page.
1681717a7feSjld */
16926cfc3f1Smaxv page_off = va & ~PTE_LGFRAME;
17026cfc3f1Smaxv *pa = (pde & PTE_LGFRAME) + page_off;
1711717a7feSjld return (int)(NBPD_L2 - page_off);
1721717a7feSjld }
17326cfc3f1Smaxv pte_pa = (pde & PTE_FRAME) + (pl1_pi(va) * sizeof(pt_entry_t));
174a7a2d171Sad if (_kvm_pread(kd, kd->pmfd, (void *) &pte, sizeof(pte),
175cc7ffa0dSchristos _kvm_pa2off(kd, pte_pa)) != sizeof(pte)) {
1769a9b6113Sthorpej _kvm_syserr(kd, 0, "could not read PTE");
1779a9b6113Sthorpej goto lose;
1789a9b6113Sthorpej }
17964b09480Scgd
1809a9b6113Sthorpej /*
1819a9b6113Sthorpej * Validate the PTE and return the physical address.
1829a9b6113Sthorpej */
18326cfc3f1Smaxv if ((pte & PTE_P) == 0) {
1849a9b6113Sthorpej _kvm_err(kd, 0, "invalid translation (invalid PTE)");
1859a9b6113Sthorpej goto lose;
1869a9b6113Sthorpej }
18726cfc3f1Smaxv *pa = (pte & PTE_FRAME) + page_off;
188cc7ffa0dSchristos return (int)(NBPG - page_off);
1899a9b6113Sthorpej
1909a9b6113Sthorpej lose:
191962a341dSjym *pa = (paddr_t)~0L;
192962a341dSjym return 0;
1930215cc7dScgd }
1947739ce58Sgwr
1957739ce58Sgwr /*
1969a9b6113Sthorpej * Translate a physical address to a file-offset in the crash dump.
1979a9b6113Sthorpej */
1989a9b6113Sthorpej off_t
_kvm_pa2off(kvm_t * kd,paddr_t pa)199962a341dSjym _kvm_pa2off(kvm_t *kd, paddr_t pa)
2009a9b6113Sthorpej {
2019a9b6113Sthorpej cpu_kcore_hdr_t *cpu_kh;
2029a9b6113Sthorpej phys_ram_seg_t *ramsegs;
2039a9b6113Sthorpej off_t off;
2049a9b6113Sthorpej int i;
2059a9b6113Sthorpej
2069a9b6113Sthorpej cpu_kh = kd->cpu_data;
207cc7ffa0dSchristos ramsegs = (void *)((char *)(void *)cpu_kh + ALIGN(sizeof *cpu_kh));
2089a9b6113Sthorpej
2099a9b6113Sthorpej off = 0;
2109a9b6113Sthorpej for (i = 0; i < cpu_kh->nmemsegs; i++) {
2119a9b6113Sthorpej if (pa >= ramsegs[i].start &&
2129a9b6113Sthorpej (pa - ramsegs[i].start) < ramsegs[i].size) {
2139a9b6113Sthorpej off += (pa - ramsegs[i].start);
2149a9b6113Sthorpej break;
2159a9b6113Sthorpej }
2169a9b6113Sthorpej off += ramsegs[i].size;
2179a9b6113Sthorpej }
2189a9b6113Sthorpej
2199a9b6113Sthorpej return (kd->dump_off + off);
2209a9b6113Sthorpej }
2219a9b6113Sthorpej
2229a9b6113Sthorpej /*
2237739ce58Sgwr * Machine-dependent initialization for ALL open kvm descriptors,
2247739ce58Sgwr * not just those for a kernel crash dump. Some architectures
2257739ce58Sgwr * have to deal with these NOT being constants! (i.e. m68k)
2267739ce58Sgwr */
2277739ce58Sgwr int
_kvm_mdopen(kvm_t * kd)2286dc46b92Sjym _kvm_mdopen(kvm_t *kd)
2297739ce58Sgwr {
2307739ce58Sgwr
2317739ce58Sgwr kd->min_uva = VM_MIN_ADDRESS;
2327739ce58Sgwr kd->max_uva = VM_MAXUSER_ADDRESS;
2337739ce58Sgwr
2346dc46b92Sjym return 0;
2357739ce58Sgwr }
236