xref: /netbsd/lib/libkvm/kvm_alpha.c (revision a7a2d171)
1 /* $NetBSD: kvm_alpha.c,v 1.23 2008/01/15 13:57:41 ad 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 /*ARGSUSED*/
53 void
54 _kvm_freevtop(kd)
55 	kvm_t *kd;
56 {
57 	return;
58 }
59 
60 /*ARGSUSED*/
61 int
62 _kvm_initvtop(kd)
63 	kvm_t *kd;
64 {
65 	return (0);
66 }
67 
68 int
69 _kvm_kvatop(kd, va, pa)
70 	kvm_t *kd;
71 	u_long va;
72 	u_long *pa;
73 {
74 	cpu_kcore_hdr_t *cpu_kh;
75 	alpha_pt_entry_t pte;
76 	u_long pteoff, page_off;
77 	int rv;
78 
79         if (ISALIVE(kd)) {
80                 _kvm_err(kd, 0, "vatop called in live kernel!");
81                 return(0);
82         }
83 
84 	cpu_kh = kd->cpu_data;
85 	page_off = va & (cpu_kh->page_size - 1);
86 
87 	if (va >= ALPHA_K0SEG_BASE && va <= ALPHA_K0SEG_END) {
88 		/*
89 		 * Direct-mapped address: just convert it.
90 		 */
91 
92 		*pa = ALPHA_K0SEG_TO_PHYS(va);
93 		rv = cpu_kh->page_size - page_off;
94 	} else if (va >= ALPHA_K1SEG_BASE && va <= ALPHA_K1SEG_END) {
95 		/*
96 		 * Real kernel virtual address: do the translation.
97 		 */
98 
99 		/* Find and read the L1 PTE. */
100 		pteoff = cpu_kh->lev1map_pa +
101 		    l1pte_index(va) * sizeof(alpha_pt_entry_t);
102 		if (_kvm_pread(kd, kd->pmfd, &pte, sizeof(pte),
103 		    _kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
104 			_kvm_syserr(kd, 0, "could not read L1 PTE");
105 			goto lose;
106 		}
107 
108 		/* Find and read the L2 PTE. */
109 		if ((pte & ALPHA_PTE_VALID) == 0) {
110 			_kvm_err(kd, 0, "invalid translation (invalid L1 PTE)");
111 			goto lose;
112 		}
113 		pteoff = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size +
114 		    l2pte_index(va) * sizeof(alpha_pt_entry_t);
115 		if (_kvm_pread(kd, kd->pmfd, &pte, sizeof(pte),
116 		    _kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
117 			_kvm_syserr(kd, 0, "could not read L2 PTE");
118 			goto lose;
119 		}
120 
121 		/* Find and read the L3 PTE. */
122 		if ((pte & ALPHA_PTE_VALID) == 0) {
123 			_kvm_err(kd, 0, "invalid translation (invalid L2 PTE)");
124 			goto lose;
125 		}
126 		pteoff = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size +
127 		    l3pte_index(va) * sizeof(alpha_pt_entry_t);
128 		if (_kvm_pread(kd, kd->pmfd, &pte, sizeof(pte),
129 		    _kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
130 			_kvm_syserr(kd, 0, "could not read L3 PTE");
131 			goto lose;
132 		}
133 
134 		/* Fill in the PA. */
135 		if ((pte & ALPHA_PTE_VALID) == 0) {
136 			_kvm_err(kd, 0, "invalid translation (invalid L3 PTE)");
137 			goto lose;
138 		}
139 		*pa = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size + page_off;
140 		rv = cpu_kh->page_size - page_off;
141 	} else {
142 		/*
143 		 * Bogus address (not in KV space): punt.
144 		 */
145 
146 		_kvm_err(kd, 0, "invalid kernel virtual address");
147 lose:
148 		*pa = -1;
149 		rv = 0;
150 	}
151 
152 	return (rv);
153 }
154 
155 /*
156  * Translate a physical address to a file-offset in the crash dump.
157  */
158 off_t
159 _kvm_pa2off(kd, pa)
160 	kvm_t *kd;
161 	u_long pa;
162 {
163 	cpu_kcore_hdr_t *cpu_kh;
164 	phys_ram_seg_t *ramsegs;
165 	off_t off;
166 	int i;
167 
168 	cpu_kh = kd->cpu_data;
169 	ramsegs = (phys_ram_seg_t *)((char *)cpu_kh + ALIGN(sizeof *cpu_kh));
170 
171 	off = 0;
172 	for (i = 0; i < cpu_kh->nmemsegs; i++) {
173 		if (pa >= ramsegs[i].start &&
174 		    (pa - ramsegs[i].start) < ramsegs[i].size) {
175 			off += (pa - ramsegs[i].start);
176 			break;
177 		}
178 		off += ramsegs[i].size;
179 	}
180 
181 	return (kd->dump_off + off);
182 }
183 
184 /*
185  * Machine-dependent initialization for ALL open kvm descriptors,
186  * not just those for a kernel crash dump.  Some architectures
187  * have to deal with these NOT being constants!  (i.e. m68k)
188  */
189 int
190 _kvm_mdopen(kd)
191 	kvm_t	*kd;
192 {
193 
194 	kd->usrstack = USRSTACK;
195 	kd->min_uva = VM_MIN_ADDRESS;
196 	kd->max_uva = VM_MAXUSER_ADDRESS;
197 
198 	return (0);
199 }
200