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