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