xref: /openbsd-src/lib/libkvm/kvm_amd64.c (revision 824adb5411e4389b29bae28eba5c2c2bbd147f34)
1 /*	$OpenBSD: kvm_amd64.c,v 1.14 2021/09/10 00:02:43 deraadt Exp $	*/
2 /*	$NetBSD: kvm_x86_64.c,v 1.3 2002/06/05 22:01:55 fvdl Exp $	*/
3 
4 /*-
5  * Copyright (c) 1989, 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software developed by the Computer Systems
9  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
10  * BG 91-66 and contributed to Berkeley.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 /*
38  * x86-64 machine dependent routines for kvm.
39  */
40 
41 #include <sys/param.h>	/* MAXCOMLEN ALIGN */
42 #include <sys/types.h>
43 #include <sys/signal.h>
44 #include <sys/proc.h>
45 #include <sys/stat.h>
46 #include <sys/kcore.h>
47 #include <machine/kcore.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <nlist.h>
51 #include <kvm.h>
52 
53 #include <uvm/uvm_extern.h>
54 
55 #include <limits.h>
56 #include <db.h>
57 
58 #include "kvm_private.h"
59 
60 #include <machine/pmap.h>
61 #include <machine/pte.h>
62 #include <machine/vmparam.h>
63 
64 void
65 _kvm_freevtop(kvm_t *kd)
66 {
67 	/* Not actually used for anything right now, but safe. */
68 	free(kd->vmst);
69 	kd->vmst = NULL;
70 }
71 
72 /*ARGSUSED*/
73 int
74 _kvm_initvtop(kvm_t *kd)
75 {
76 
77 	return (0);
78 }
79 
80 /*
81  * Translate a kernel virtual address to a physical address.
82  */
83 int
84 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
85 {
86 	cpu_kcore_hdr_t *cpu_kh;
87 	paddr_t pde_pa, pte_pa;
88 	u_long page_off;
89 	pd_entry_t pde;
90 	pt_entry_t pte;
91 
92 	if (ISALIVE(kd)) {
93 		_kvm_err(kd, 0, "vatop called in live kernel!");
94 		return (0);
95 	}
96 
97 	page_off = va & (kd->nbpg - 1);
98 
99 	if (va >= PMAP_DIRECT_BASE && va <= PMAP_DIRECT_END) {
100 		*pa = va - PMAP_DIRECT_BASE;
101 		return (int)(kd->nbpg - page_off);
102 	}
103 
104 	cpu_kh = kd->cpu_data;
105 
106 	/*
107 	 * Find and read all entries to get to the pa.
108 	 */
109 
110 	/*
111 	 * Level 4.
112 	 */
113 	pde_pa = cpu_kh->ptdpaddr + (pl4_pi(va) * sizeof(pd_entry_t));
114 	if (pread(kd->pmfd, (void *)&pde, sizeof(pde),
115 	    _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) {
116 		_kvm_syserr(kd, 0, "could not read PT level 4 entry");
117 		goto lose;
118 	}
119 	if ((pde & PG_V) == 0) {
120 		_kvm_err(kd, 0, "invalid translation (invalid level 4 PDE)");
121 		goto lose;
122 	}
123 
124 	/*
125 	 * Level 3.
126 	 */
127 	pde_pa = (pde & PG_FRAME) + (pl3_pi(va) * sizeof(pd_entry_t));
128 	if (pread(kd->pmfd, (void *)&pde, sizeof(pde),
129 	    _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) {
130 		_kvm_syserr(kd, 0, "could not read PT level 3 entry");
131 		goto lose;
132 	}
133 	if ((pde & PG_V) == 0) {
134 		_kvm_err(kd, 0, "invalid translation (invalid level 3 PDE)");
135 		goto lose;
136 	}
137 
138 	/*
139 	 * Level 2.
140 	 */
141 	pde_pa = (pde & PG_FRAME) + (pl2_pi(va) * sizeof(pd_entry_t));
142 	if (pread(kd->pmfd, (void *)&pde, sizeof(pde),
143 	    _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) {
144 		_kvm_syserr(kd, 0, "could not read PT level 2 entry");
145 		goto lose;
146 	}
147 	if ((pde & PG_V) == 0) {
148 		_kvm_err(kd, 0, "invalid translation (invalid level 2 PDE)");
149 		goto lose;
150 	}
151 
152 	/*
153 	 * Might be a large page.
154 	 */
155 	if ((pde & PG_PS) != 0) {
156 		page_off = va & (NBPD_L2 - 1);
157 		*pa = (pde & PG_LGFRAME) | page_off;
158 		return (int)(NBPD_L2 - page_off);
159 	}
160 
161 	/*
162 	 * Level 1.
163 	 */
164 	pte_pa = (pde & PG_FRAME) + (pl1_pi(va) * sizeof(pt_entry_t));
165 	if (pread(kd->pmfd, (void *) &pte, sizeof(pte),
166 	    _kvm_pa2off(kd, pte_pa)) != sizeof(pte)) {
167 		_kvm_syserr(kd, 0, "could not read PTE");
168 		goto lose;
169 	}
170 	/*
171 	 * Validate the PTE and return the physical address.
172 	 */
173 	if ((pte & PG_V) == 0) {
174 		_kvm_err(kd, 0, "invalid translation (invalid PTE)");
175 		goto lose;
176 	}
177 	*pa = (pte & PG_FRAME) + page_off;
178 	return (int)(kd->nbpg - page_off);
179 
180  lose:
181 	*pa = (u_long)~0L;
182 	return (0);
183 }
184 
185 /*
186  * Translate a physical address to a file-offset in the crash dump.
187  */
188 off_t
189 _kvm_pa2off(kvm_t *kd, paddr_t pa)
190 {
191 	cpu_kcore_hdr_t *cpu_kh;
192 	phys_ram_seg_t *ramsegs;
193 	off_t off;
194 	int i;
195 
196 	cpu_kh = kd->cpu_data;
197 	ramsegs = (void *)((char *)(void *)cpu_kh + ALIGN(sizeof *cpu_kh));
198 
199 	off = 0;
200 	for (i = 0; i < cpu_kh->nmemsegs; i++) {
201 		if (pa >= ramsegs[i].start &&
202 		    (pa - ramsegs[i].start) < ramsegs[i].size) {
203 			off += (pa - ramsegs[i].start);
204 			break;
205 		}
206 		off += ramsegs[i].size;
207 	}
208 
209 	if (i == cpu_kh->nmemsegs)
210 		_kvm_err(kd, 0, "pa %lx not in dump", pa);
211 
212 	return (kd->dump_off + off);
213 }
214