xref: /openbsd-src/lib/libkvm/kvm_amd64.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: kvm_amd64.c,v 1.7 2009/03/30 21:16:14 kettenis 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 #include <sys/cdefs.h>
38 
39 /*
40  * x86-64 machine dependent routines for kvm.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/user.h>
45 #include <sys/proc.h>
46 #include <sys/stat.h>
47 #include <sys/kcore.h>
48 #include <machine/kcore.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <nlist.h>
52 #include <kvm.h>
53 
54 #include <uvm/uvm_extern.h>
55 
56 #include <limits.h>
57 #include <db.h>
58 
59 #include "kvm_private.h"
60 
61 #include <machine/pmap.h>
62 #include <machine/pte.h>
63 #include <machine/vmparam.h>
64 
65 void
66 _kvm_freevtop(kvm_t *kd)
67 {
68 
69 	/* Not actually used for anything right now, but safe. */
70 	if (kd->vmst != NULL) {
71 		free(kd->vmst);
72 		kd->vmst = NULL;
73 	}
74 }
75 
76 /*ARGSUSED*/
77 int
78 _kvm_initvtop(kvm_t *kd)
79 {
80 
81 	return (0);
82 }
83 
84 /*
85  * Translate a kernel virtual address to a physical address.
86  */
87 int
88 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
89 {
90 	cpu_kcore_hdr_t *cpu_kh;
91 	paddr_t pde_pa, pte_pa;
92 	u_long page_off;
93 	pd_entry_t pde;
94 	pt_entry_t pte;
95 
96 	if (ISALIVE(kd)) {
97 		_kvm_err(kd, 0, "vatop called in live kernel!");
98 		return (0);
99 	}
100 
101 	page_off = va & PGOFSET;
102 
103 	if (va >= PMAP_DIRECT_BASE && va <= PMAP_DIRECT_END) {
104 		*pa = va - PMAP_DIRECT_BASE;
105 		return (int)(NBPG - page_off);
106 	}
107 
108 	cpu_kh = kd->cpu_data;
109 
110 	/*
111 	 * Find and read all entries to get to the pa.
112 	 */
113 
114 	/*
115 	 * Level 4.
116 	 */
117 	pde_pa = cpu_kh->ptdpaddr + (pl4_pi(va) * sizeof(pd_entry_t));
118 	if (pread(kd->pmfd, (void *)&pde, sizeof(pde),
119 	    _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) {
120 		_kvm_syserr(kd, 0, "could not read PT level 4 entry");
121 		goto lose;
122 	}
123 	if ((pde & PG_V) == 0) {
124 		_kvm_err(kd, 0, "invalid translation (invalid level 4 PDE)");
125 		goto lose;
126 	}
127 
128 	/*
129 	 * Level 3.
130 	 */
131 	pde_pa = (pde & PG_FRAME) + (pl3_pi(va) * sizeof(pd_entry_t));
132 	if (pread(kd->pmfd, (void *)&pde, sizeof(pde),
133 	    _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) {
134 		_kvm_syserr(kd, 0, "could not read PT level 3 entry");
135 		goto lose;
136 	}
137 	if ((pde & PG_V) == 0) {
138 		_kvm_err(kd, 0, "invalid translation (invalid level 3 PDE)");
139 		goto lose;
140 	}
141 
142 	/*
143 	 * Level 2.
144 	 */
145 	pde_pa = (pde & PG_FRAME) + (pl2_pi(va) * sizeof(pd_entry_t));
146 	if (pread(kd->pmfd, (void *)&pde, sizeof(pde),
147 	    _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) {
148 		_kvm_syserr(kd, 0, "could not read PT level 2 entry");
149 		goto lose;
150 	}
151 	if ((pde & PG_V) == 0) {
152 		_kvm_err(kd, 0, "invalid translation (invalid level 2 PDE)");
153 		goto lose;
154 	}
155 
156 	/*
157 	 * Might be a large page.
158 	 */
159 	if ((pde & PG_PS) != 0) {
160 		page_off = va & (NBPD_L2 - 1);
161 		*pa = (pde & PG_LGFRAME) | page_off;
162 		return (int)(NBPD_L2 - page_off);
163 	}
164 
165 	/*
166 	 * Level 1.
167 	 */
168 	pte_pa = (pde & PG_FRAME) + (pl1_pi(va) * sizeof(pt_entry_t));
169 	if (pread(kd->pmfd, (void *) &pte, sizeof(pte),
170 	    _kvm_pa2off(kd, pte_pa)) != sizeof(pte)) {
171 		_kvm_syserr(kd, 0, "could not read PTE");
172 		goto lose;
173 	}
174 	/*
175 	 * Validate the PTE and return the physical address.
176 	 */
177 	if ((pte & PG_V) == 0) {
178 		_kvm_err(kd, 0, "invalid translation (invalid PTE)");
179 		goto lose;
180 	}
181 	*pa = (pte & PG_FRAME) + page_off;
182 	return (int)(NBPG - page_off);
183 
184  lose:
185 	*pa = (u_long)~0L;
186 	return (0);
187 }
188 
189 /*
190  * Translate a physical address to a file-offset in the crash dump.
191  */
192 off_t
193 _kvm_pa2off(kvm_t *kd, paddr_t pa)
194 {
195 	cpu_kcore_hdr_t *cpu_kh;
196 	phys_ram_seg_t *ramsegs;
197 	off_t off;
198 	int i;
199 
200 	cpu_kh = kd->cpu_data;
201 	ramsegs = (void *)((char *)(void *)cpu_kh + ALIGN(sizeof *cpu_kh));
202 
203 	off = 0;
204 	for (i = 0; i < cpu_kh->nmemsegs; i++) {
205 		if (pa >= ramsegs[i].start &&
206 		    (pa - ramsegs[i].start) < ramsegs[i].size) {
207 			off += (pa - ramsegs[i].start);
208 			break;
209 		}
210 		off += ramsegs[i].size;
211 	}
212 
213 	if (i == cpu_kh->nmemsegs)
214 		_kvm_err(kd, 0, "pa %lx not in dump", pa);
215 
216 	return (kd->dump_off + off);
217 }
218