xref: /netbsd/lib/libkvm/kvm_alpha.c (revision 55c1d5c2)
1 /* $NetBSD: kvm_alpha.c,v 1.20 2001/08/05 17:51:40 matt Exp $ */
2 
3 /*
4  * Copyright (c) 1994, 1995 Carnegie-Mellon University.
5  * All rights reserved.
6  *
7  * Author: Chris G. Demetriou
8  *
9  * Permission to use, copy, modify and distribute this software and
10  * its documentation is hereby granted, provided that both the copyright
11  * notice and this permission notice appear in all copies of the
12  * software, derivative works or modified versions, and any portions
13  * thereof, and that both notices appear in supporting documentation.
14  *
15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18  *
19  * Carnegie Mellon requests users of this software to return to
20  *
21  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
22  *  School of Computer Science
23  *  Carnegie Mellon University
24  *  Pittsburgh PA 15213-3890
25  *
26  * any improvements or extensions that they make and grant Carnegie the
27  * rights to redistribute these changes.
28  */
29 
30 #define	__KVM_ALPHA_PRIVATE		/* see <machine/pte.h> */
31 
32 #include <sys/param.h>
33 #include <sys/user.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 <nlist.h>
40 #include <kvm.h>
41 
42 #include <uvm/uvm_extern.h>
43 #include <machine/pmap.h>
44 #include <machine/vmparam.h>
45 
46 #include <limits.h>
47 #include <db.h>
48 #include <stdlib.h>
49 
50 #include "kvm_private.h"
51 
52 struct vmstate {
53 	vsize_t		page_shift;
54 };
55 
56 void
57 _kvm_freevtop(kd)
58 	kvm_t *kd;
59 {
60 
61 	if (kd->vmst != 0)
62 		free(kd->vmst);
63 }
64 
65 int
66 _kvm_initvtop(kd)
67 	kvm_t *kd;
68 {
69 	cpu_kcore_hdr_t *cpu_kh;
70 	struct vmstate *vm;
71 
72 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
73 	if (vm == NULL)
74 		return (-1);
75 
76 	cpu_kh = kd->cpu_data;
77 
78 	/* Compute page_shift. */
79 	for (vm->page_shift = 0; (1L << vm->page_shift) < cpu_kh->page_size;
80 	     vm->page_shift++)
81 		/* nothing */ ;
82 	if ((1L << vm->page_shift) != cpu_kh->page_size) {
83 		free(vm);
84 		return (-1);
85 	}
86 
87 	kd->vmst = vm;
88 	return (0);
89 }
90 
91 int
92 _kvm_kvatop(kd, va, pa)
93 	kvm_t *kd;
94 	u_long va;
95 	u_long *pa;
96 {
97 	cpu_kcore_hdr_t *cpu_kh;
98 	struct vmstate *vm;
99 	alpha_pt_entry_t pte;
100 	u_long pteoff, page_off;
101 	int rv;
102 
103         if (ISALIVE(kd)) {
104                 _kvm_err(kd, 0, "vatop called in live kernel!");
105                 return(0);
106         }
107 
108 	cpu_kh = kd->cpu_data;
109 	vm = kd->vmst;
110 	page_off = va & (cpu_kh->page_size - 1);
111 
112 #define	PAGE_SHIFT	vm->page_shift
113 
114 	if (va >= ALPHA_K0SEG_BASE && va <= ALPHA_K0SEG_END) {
115 		/*
116 		 * Direct-mapped address: just convert it.
117 		 */
118 
119 		*pa = ALPHA_K0SEG_TO_PHYS(va);
120 		rv = cpu_kh->page_size - page_off;
121 	} else if (va >= ALPHA_K1SEG_BASE && va <= ALPHA_K1SEG_END) {
122 		/*
123 		 * Real kernel virtual address: do the translation.
124 		 */
125 
126 		/* Find and read the L1 PTE. */
127 		pteoff = cpu_kh->lev1map_pa +
128 		    l1pte_index(va) * sizeof(alpha_pt_entry_t);
129 		if (pread(kd->pmfd, &pte, sizeof(pte),
130 		    _kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
131 			_kvm_syserr(kd, 0, "could not read L1 PTE");
132 			goto lose;
133 		}
134 
135 		/* Find and read the L2 PTE. */
136 		if ((pte & ALPHA_PTE_VALID) == 0) {
137 			_kvm_err(kd, 0, "invalid translation (invalid L1 PTE)");
138 			goto lose;
139 		}
140 		pteoff = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size +
141 		    l2pte_index(va) * sizeof(alpha_pt_entry_t);
142 		if (pread(kd->pmfd, &pte, sizeof(pte),
143 		    _kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
144 			_kvm_syserr(kd, 0, "could not read L2 PTE");
145 			goto lose;
146 		}
147 
148 		/* Find and read the L3 PTE. */
149 		if ((pte & ALPHA_PTE_VALID) == 0) {
150 			_kvm_err(kd, 0, "invalid translation (invalid L2 PTE)");
151 			goto lose;
152 		}
153 		pteoff = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size +
154 		    l3pte_index(va) * sizeof(alpha_pt_entry_t);
155 		if (pread(kd->pmfd, &pte, sizeof(pte),
156 		    _kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
157 			_kvm_syserr(kd, 0, "could not read L3 PTE");
158 			goto lose;
159 		}
160 
161 		/* Fill in the PA. */
162 		if ((pte & ALPHA_PTE_VALID) == 0) {
163 			_kvm_err(kd, 0, "invalid translation (invalid L3 PTE)");
164 			goto lose;
165 		}
166 		*pa = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size + page_off;
167 		rv = cpu_kh->page_size - page_off;
168 	} else {
169 		/*
170 		 * Bogus address (not in KV space): punt.
171 		 */
172 
173 		_kvm_err(kd, 0, "invalid kernel virtual address");
174 lose:
175 		*pa = -1;
176 		rv = 0;
177 	}
178 
179 #undef PAGE_SHIFT
180 
181 	return (rv);
182 }
183 
184 /*
185  * Translate a physical address to a file-offset in the crash-dump.
186  */
187 off_t
188 _kvm_pa2off(kd, pa)
189 	kvm_t *kd;
190 	u_long 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 = (phys_ram_seg_t *)((char *)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 	return (kd->dump_off + off);
211 }
212 
213 /*
214  * Machine-dependent initialization for ALL open kvm descriptors,
215  * not just those for a kernel crash dump.  Some architectures
216  * have to deal with these NOT being constants!  (i.e. m68k)
217  */
218 int
219 _kvm_mdopen(kd)
220 	kvm_t	*kd;
221 {
222 
223 	kd->usrstack = USRSTACK;
224 	kd->min_uva = VM_MIN_ADDRESS;
225 	kd->max_uva = VM_MAXUSER_ADDRESS;
226 
227 	return (0);
228 }
229