xref: /netbsd/lib/libkvm/kvm_i386pae.c (revision 0ed2f35a)
1*0ed2f35aSmaxv /* $NetBSD: kvm_i386pae.c,v 1.3 2020/04/25 05:17:16 maxv Exp $ */
2f15c6971Sjym 
3f15c6971Sjym /*
4f15c6971Sjym  * Copyright (c) 2010 Jean-Yves Migeon.
5f15c6971Sjym  *
6f15c6971Sjym  * Redistribution and use in source and binary forms, with or without
7f15c6971Sjym  * modification, are permitted provided that the following conditions
8f15c6971Sjym  * are met:
9f15c6971Sjym  * 1. Redistributions of source code must retain the above copyright
10f15c6971Sjym  *    notice, this list of conditions and the following disclaimer.
11f15c6971Sjym  * 2. Redistributions in binary form must reproduce the above copyright
12f15c6971Sjym  *    notice, this list of conditions and the following disclaimer in the
13f15c6971Sjym  *    documentation and/or other materials provided with the distribution.
14f15c6971Sjym  *
15f15c6971Sjym  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16f15c6971Sjym  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17f15c6971Sjym  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18f15c6971Sjym  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19f15c6971Sjym  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20f15c6971Sjym  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21f15c6971Sjym  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22f15c6971Sjym  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23f15c6971Sjym  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24f15c6971Sjym  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25f15c6971Sjym  * POSSIBILITY OF SUCH DAMAGE.
26f15c6971Sjym  */
27f15c6971Sjym 
28f15c6971Sjym #include <sys/cdefs.h>
29*0ed2f35aSmaxv __RCSID("$NetBSD: kvm_i386pae.c,v 1.3 2020/04/25 05:17:16 maxv Exp $");
30f15c6971Sjym 
31f15c6971Sjym /*
32f15c6971Sjym  * This will expose PAE functions, macros, definitions and constants.
33f15c6971Sjym  * Note: this affects all virtual memory related functions. Only their
34f15c6971Sjym  * PAE versions can be used below.
35f15c6971Sjym  */
36f15c6971Sjym #define PAE
37f15c6971Sjym 
38f15c6971Sjym #include <sys/param.h>
39f15c6971Sjym #include <sys/stat.h>
40f15c6971Sjym #include <sys/kcore.h>
41f15c6971Sjym #include <sys/types.h>
42f15c6971Sjym 
43f15c6971Sjym #include <stdlib.h>
44f15c6971Sjym #include <unistd.h>
45f15c6971Sjym #include <nlist.h>
46f15c6971Sjym #include <kvm.h>
47f15c6971Sjym 
48f15c6971Sjym #include <uvm/uvm_extern.h>
49f15c6971Sjym 
50f15c6971Sjym #include <limits.h>
51f15c6971Sjym #include <db.h>
52f15c6971Sjym 
53f15c6971Sjym #include "kvm_private.h"
54f15c6971Sjym 
55f15c6971Sjym #include <i386/kcore.h>
56f15c6971Sjym #include <i386/pmap.h>
57f15c6971Sjym #include <i386/pte.h>
58f15c6971Sjym #include <i386/vmparam.h>
59f15c6971Sjym 
60f15c6971Sjym int _kvm_kvatop_i386pae(kvm_t *, vaddr_t, paddr_t *);
61f15c6971Sjym 
62f15c6971Sjym /*
63f15c6971Sjym  * Used to translate a virtual address to a physical address for systems
64f15c6971Sjym  * running under PAE mode. Three levels of virtual memory pages are handled
65f15c6971Sjym  * here: the per-CPU L3 page, the 4 L2 PDs and the PTs.
66f15c6971Sjym  */
67f15c6971Sjym int
_kvm_kvatop_i386pae(kvm_t * kd,vaddr_t va,paddr_t * pa)68f15c6971Sjym _kvm_kvatop_i386pae(kvm_t *kd, vaddr_t va, paddr_t *pa)
69f15c6971Sjym {
70f15c6971Sjym 	cpu_kcore_hdr_t *cpu_kh;
71f15c6971Sjym 	u_long page_off;
72f15c6971Sjym 	pd_entry_t pde;
73f15c6971Sjym 	pt_entry_t pte;
74f15c6971Sjym 	paddr_t pde_pa, pte_pa;
75f15c6971Sjym 
76f15c6971Sjym 	cpu_kh = kd->cpu_data;
77f15c6971Sjym 	page_off = va & PGOFSET;
78f15c6971Sjym 
79f15c6971Sjym 	/*
80f15c6971Sjym 	 * Find and read the PDE. Ignore the L3, as it is only a per-CPU
81f15c6971Sjym 	 * page, not needed for kernel VA => PA translations.
82f15c6971Sjym 	 * Remember that the 4 L2 pages are contiguous, so it is safe
83f15c6971Sjym 	 * to increment pdppaddr to compute the address of the PDE.
84f15c6971Sjym 	 * pdppaddr being PAGE_SIZE aligned, we mask the option bits.
85f15c6971Sjym 	 */
86*0ed2f35aSmaxv 	pde_pa = (cpu_kh->pdppaddr & PTE_FRAME) + (pl2_pi(va) * sizeof(pde));
87f15c6971Sjym 	if (_kvm_pread(kd, kd->pmfd, (void *)&pde, sizeof(pde),
88f15c6971Sjym 	    _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) {
89f15c6971Sjym 		_kvm_syserr(kd, 0, "could not read PDE");
90f15c6971Sjym 		goto lose;
91f15c6971Sjym 	}
92f15c6971Sjym 
93f15c6971Sjym 	/*
94f15c6971Sjym 	 * Find and read the page table entry.
95f15c6971Sjym 	 */
96*0ed2f35aSmaxv 	if ((pde & PTE_P) == 0) {
97f15c6971Sjym 		_kvm_err(kd, 0, "invalid translation (invalid PDE)");
98f15c6971Sjym 		goto lose;
99f15c6971Sjym 	}
100*0ed2f35aSmaxv 	if ((pde & PTE_PS) != 0) {
101f15c6971Sjym 		/*
102f15c6971Sjym 		 * This is a 2MB page.
103f15c6971Sjym 		 */
104*0ed2f35aSmaxv 		page_off = va & ((vaddr_t)~PTE_LGFRAME);
105*0ed2f35aSmaxv 		*pa = (pde & PTE_LGFRAME) + page_off;
106f15c6971Sjym 		return (int)(NBPD_L2 - page_off);
107f15c6971Sjym 	}
108f15c6971Sjym 
109*0ed2f35aSmaxv 	pte_pa = (pde & PTE_FRAME) + (pl1_pi(va) * sizeof(pt_entry_t));
110f15c6971Sjym 	if (_kvm_pread(kd, kd->pmfd, (void *) &pte, sizeof(pte),
111f15c6971Sjym 	    _kvm_pa2off(kd, pte_pa)) != sizeof(pte)) {
112f15c6971Sjym 		_kvm_syserr(kd, 0, "could not read PTE");
113f15c6971Sjym 		goto lose;
114f15c6971Sjym 	}
115f15c6971Sjym 
116f15c6971Sjym 	/*
117f15c6971Sjym 	 * Validate the PTE and return the physical address.
118f15c6971Sjym 	 */
119*0ed2f35aSmaxv 	if ((pte & PTE_P) == 0) {
120f15c6971Sjym 		_kvm_err(kd, 0, "invalid translation (invalid PTE)");
121f15c6971Sjym 		goto lose;
122f15c6971Sjym 	}
123*0ed2f35aSmaxv 	*pa = (pte & PTE_FRAME) + page_off;
124f15c6971Sjym 	return (int)(NBPG - page_off);
125f15c6971Sjym 
126f15c6971Sjym lose:
127f15c6971Sjym 	*pa = (paddr_t)~0L;
128f15c6971Sjym 	return 0;
129f15c6971Sjym 
130f15c6971Sjym }
131