xref: /netbsd/lib/libkvm/kvm_alpha.c (revision cce919e0)
1 /* $NetBSD: kvm_alpha.c,v 1.19 2001/08/05 03:33:15 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 
45 #include <limits.h>
46 #include <db.h>
47 #include <stdlib.h>
48 
49 #include "kvm_private.h"
50 
51 struct vmstate {
52 	vsize_t		page_shift;
53 };
54 
55 void
56 _kvm_freevtop(kd)
57 	kvm_t *kd;
58 {
59 
60 	if (kd->vmst != 0)
61 		free(kd->vmst);
62 }
63 
64 int
65 _kvm_initvtop(kd)
66 	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(kd, va, pa)
92 	kvm_t *kd;
93 	u_long va;
94 	u_long *pa;
95 {
96 	cpu_kcore_hdr_t *cpu_kh;
97 	struct vmstate *vm;
98 	alpha_pt_entry_t pte;
99 	u_long pteoff, page_off;
100 	int rv;
101 
102         if (ISALIVE(kd)) {
103                 _kvm_err(kd, 0, "vatop called in live kernel!");
104                 return(0);
105         }
106 
107 	cpu_kh = kd->cpu_data;
108 	vm = kd->vmst;
109 	page_off = va & (cpu_kh->page_size - 1);
110 
111 #define	PAGE_SHIFT	vm->page_shift
112 
113 	if (va >= ALPHA_K0SEG_BASE && va <= ALPHA_K0SEG_END) {
114 		/*
115 		 * Direct-mapped address: just convert it.
116 		 */
117 
118 		*pa = ALPHA_K0SEG_TO_PHYS(va);
119 		rv = cpu_kh->page_size - page_off;
120 	} else if (va >= ALPHA_K1SEG_BASE && va <= ALPHA_K1SEG_END) {
121 		/*
122 		 * Real kernel virtual address: do the translation.
123 		 */
124 
125 		/* Find and read the L1 PTE. */
126 		pteoff = cpu_kh->lev1map_pa +
127 		    l1pte_index(va) * sizeof(alpha_pt_entry_t);
128 		if (pread(kd->pmfd, &pte, sizeof(pte),
129 		    _kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
130 			_kvm_syserr(kd, 0, "could not read L1 PTE");
131 			goto lose;
132 		}
133 
134 		/* Find and read the L2 PTE. */
135 		if ((pte & ALPHA_PTE_VALID) == 0) {
136 			_kvm_err(kd, 0, "invalid translation (invalid L1 PTE)");
137 			goto lose;
138 		}
139 		pteoff = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size +
140 		    l2pte_index(va) * sizeof(alpha_pt_entry_t);
141 		if (pread(kd->pmfd, &pte, sizeof(pte),
142 		    _kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
143 			_kvm_syserr(kd, 0, "could not read L2 PTE");
144 			goto lose;
145 		}
146 
147 		/* Find and read the L3 PTE. */
148 		if ((pte & ALPHA_PTE_VALID) == 0) {
149 			_kvm_err(kd, 0, "invalid translation (invalid L2 PTE)");
150 			goto lose;
151 		}
152 		pteoff = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size +
153 		    l3pte_index(va) * sizeof(alpha_pt_entry_t);
154 		if (pread(kd->pmfd, &pte, sizeof(pte),
155 		    _kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
156 			_kvm_syserr(kd, 0, "could not read L3 PTE");
157 			goto lose;
158 		}
159 
160 		/* Fill in the PA. */
161 		if ((pte & ALPHA_PTE_VALID) == 0) {
162 			_kvm_err(kd, 0, "invalid translation (invalid L3 PTE)");
163 			goto lose;
164 		}
165 		*pa = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size + page_off;
166 		rv = cpu_kh->page_size - page_off;
167 	} else {
168 		/*
169 		 * Bogus address (not in KV space): punt.
170 		 */
171 
172 		_kvm_err(kd, 0, "invalid kernel virtual address");
173 lose:
174 		*pa = -1;
175 		rv = 0;
176 	}
177 
178 #undef PAGE_SHIFT
179 
180 	return (rv);
181 }
182 
183 /*
184  * Translate a physical address to a file-offset in the crash-dump.
185  */
186 off_t
187 _kvm_pa2off(kd, pa)
188 	kvm_t *kd;
189 	u_long 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 
209 	return (kd->dump_off + off);
210 }
211 
212 /*
213  * Machine-dependent initialization for ALL open kvm descriptors,
214  * not just those for a kernel crash dump.  Some architectures
215  * have to deal with these NOT being constants!  (i.e. m68k)
216  */
217 int
218 _kvm_mdopen(kd)
219 	kvm_t	*kd;
220 {
221 
222 	kd->usrstack = USRSTACK;
223 	kd->min_uva = VM_MIN_ADDRESS;
224 	kd->max_uva = VM_MAXUSER_ADDRESS;
225 
226 	return (0);
227 }
228