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