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