xref: /openbsd/lib/libkvm/kvm_alpha.c (revision 09467b48)
1 /*	$OpenBSD: kvm_alpha.c,v 1.16 2015/12/19 18:40:30 mmcc Exp $	*/
2 /*	$NetBSD: kvm_alpha.c,v 1.5 1996/10/01 21:12:05 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1994, 1995 Carnegie-Mellon University.
6  * All rights reserved.
7  *
8  * Author: Chris G. Demetriou
9  *
10  * Permission to use, copy, modify and distribute this software and
11  * its documentation is hereby granted, provided that both the copyright
12  * notice and this permission notice appear in all copies of the
13  * software, derivative works or modified versions, and any portions
14  * thereof, and that both notices appear in supporting documentation.
15  *
16  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
18  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19  *
20  * Carnegie Mellon requests users of this software to return to
21  *
22  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
23  *  School of Computer Science
24  *  Carnegie Mellon University
25  *  Pittsburgh PA 15213-3890
26  *
27  * any improvements or extensions that they make and grant Carnegie the
28  * rights to redistribute these changes.
29  */
30 
31 #define __KVM_ALPHA_PRIVATE	     /* see <machine/pte.h> */
32 
33 #include <sys/param.h>
34 #include <sys/proc.h>
35 #include <sys/stat.h>
36 #include <sys/kcore.h>
37 #include <machine/kcore.h>
38 #include <unistd.h>
39 #include <stdlib.h>
40 #include <nlist.h>
41 #include <kvm.h>
42 
43 #include <uvm/uvm_extern.h>
44 #include <machine/vmparam.h>
45 #include <machine/pmap.h>
46 
47 #include <limits.h>
48 #include <db.h>
49 
50 #include "kvm_private.h"
51 
52 struct vmstate {
53 	vsize_t	page_shift;
54 };
55 
56 void
57 _kvm_freevtop(kvm_t *kd)
58 {
59 
60 	/* Not actually used for anything right now, but safe. */
61 	free(kd->vmst);
62 	kd->vmst = NULL;
63 }
64 
65 int
66 _kvm_initvtop(kvm_t *kd)
67 {
68 	cpu_kcore_hdr_t *cpu_kh;
69 	struct vmstate *vm;
70 
71 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
72 	if (vm == NULL)
73 		return (-1);
74 
75 	cpu_kh = kd->cpu_data;
76 
77 	/* Compute page_shift. */
78 	for (vm->page_shift = 0; (1L << vm->page_shift) < cpu_kh->page_size;
79 	    vm->page_shift++)
80 		/* nothing */ ;
81 	if ((1L << vm->page_shift) != cpu_kh->page_size) {
82 		free(vm);
83 		return (-1);
84 	}
85 
86 	kd->vmst = vm;
87 	return (0);
88 }
89 
90 int
91 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
92 {
93 	cpu_kcore_hdr_t *cpu_kh;
94 	struct vmstate *vm;
95 	int rv, page_off;
96 	alpha_pt_entry_t pte;
97 	off_t pteoff;
98 
99 	if (!kd->vmst) {
100 		_kvm_err(kd, 0, "vatop called before initvtop");
101 		return (0);
102 	}
103 
104 	if (ISALIVE(kd)) {
105 		_kvm_err(kd, 0, "vatop called in live kernel!");
106 		return (0);
107 	}
108 
109 	cpu_kh = kd->cpu_data;
110 	vm = kd->vmst;
111 	page_off = va & (cpu_kh->page_size - 1);
112 
113 #ifndef PAGE_SHIFT
114 #define	PAGE_SHIFT      vm->page_shift
115 #endif
116 
117 	if (va >= ALPHA_K0SEG_BASE && va <= ALPHA_K0SEG_END) {
118 		/*
119 		 * Direct-mapped address: just convert it.
120 		 */
121 
122 		*pa = ALPHA_K0SEG_TO_PHYS(va);
123 		rv = cpu_kh->page_size - page_off;
124 	} else if (va >= ALPHA_K1SEG_BASE && va <= ALPHA_K1SEG_END) {
125 		/*
126 		 * Real kernel virtual address: do the translation.
127 		 */
128 
129 		/* Find and read the L1 PTE. */
130 		pteoff = cpu_kh->lev1map_pa +
131 		    l1pte_index(va) * sizeof(alpha_pt_entry_t);
132 		if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte),
133 		    (off_t)_kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
134 			_kvm_syserr(kd, 0, "could not read L1 PTE");
135 			goto lose;
136 		}
137 
138 		/* Find and read the L2 PTE. */
139 		if ((pte & ALPHA_PTE_VALID) == 0) {
140 			_kvm_err(kd, 0, "invalid translation (invalid L1 PTE)");
141 			goto lose;
142 		}
143 		pteoff = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size +
144 		    l2pte_index(va) * sizeof(alpha_pt_entry_t);
145 		if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte),
146 		    (off_t)_kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
147 			_kvm_syserr(kd, 0, "could not read L2 PTE");
148 			goto lose;
149 		}
150 
151 		/* Find and read the L3 PTE. */
152 		if ((pte & ALPHA_PTE_VALID) == 0) {
153 			_kvm_err(kd, 0, "invalid translation (invalid L2 PTE)");
154 			goto lose;
155 		}
156 		pteoff = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size +
157 		    l3pte_index(va) * sizeof(alpha_pt_entry_t);
158 		if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte),
159 		    (off_t)_kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
160 			_kvm_syserr(kd, 0, "could not read L3 PTE");
161 			goto lose;
162 		}
163 
164 		/* Fill in the PA. */
165 		if ((pte & ALPHA_PTE_VALID) == 0) {
166 			_kvm_err(kd, 0, "invalid translation (invalid L3 PTE)");
167 			goto lose;
168 		}
169 		*pa = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size + page_off;
170 		rv = cpu_kh->page_size - page_off;
171 	} else {
172 		/*
173 		 * Bogus address (not in KV space): punt.
174 		 */
175 
176 		_kvm_err(kd, 0, "invalid kernel virtual address");
177 lose:
178 		*pa = -1;
179 		rv = 0;
180 	}
181 
182 	return (rv);
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 = (phys_ram_seg_t *)((char *)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 	return (kd->dump_off + off);
209 }
210