xref: /netbsd/lib/libkvm/kvm_alpha.c (revision 395c20ea)
1 /*	$NetBSD: kvm_alpha.c,v 1.7 1997/10/10 08:45:32 mrg 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 #include <sys/param.h>
31 #include <sys/user.h>
32 #include <sys/proc.h>
33 #include <sys/stat.h>
34 #include <sys/kcore.h>
35 #include <machine/kcore.h>
36 #include <unistd.h>
37 #include <nlist.h>
38 #include <kvm.h>
39 
40 #include <vm/vm.h>
41 #include <vm/vm_param.h>
42 
43 #include <limits.h>
44 #include <db.h>
45 #include <stdlib.h>
46 
47 #include "kvm_private.h"
48 
49 void
50 _kvm_freevtop(kd)
51 	kvm_t *kd;
52 {
53 
54 	/* Not actually used for anything right now, but safe. */
55 	if (kd->vmst != 0)
56 		free(kd->vmst);
57 }
58 
59 int
60 _kvm_initvtop(kd)
61 	kvm_t *kd;
62 {
63 
64 	return (0);
65 }
66 
67 int
68 _kvm_kvatop(kd, va, pa)
69 	kvm_t *kd;
70 	u_long va;
71 	u_long *pa;
72 {
73 	cpu_kcore_hdr_t *cpu_kh;
74 	int rv, page_off;
75 	alpha_pt_entry_t pte;
76 	off_t pteoff;
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 		    kvtol1pte(va) * sizeof(alpha_pt_entry_t);
101 		if (lseek(kd->pmfd, _kvm_pa2off(kd, pteoff), 0) == -1 ||
102 		    read(kd->pmfd, (char *)&pte, sizeof(pte)) != 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 		    vatoste(va) * sizeof(alpha_pt_entry_t);
114 		if (lseek(kd->pmfd, _kvm_pa2off(kd, pteoff), 0) == -1 ||
115 		    read(kd->pmfd, (char *)&pte, sizeof(pte)) != 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 		    vatopte(va) * sizeof(alpha_pt_entry_t);
127 		if (lseek(kd->pmfd, _kvm_pa2off(kd, pteoff), 0) == -1 ||
128 		    read(kd->pmfd, (char *)&pte, sizeof(pte)) != 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 		    vatopte(va) * sizeof(alpha_pt_entry_t);
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 	off_t off;
164 	cpu_kcore_hdr_t *cpu_kh;
165 
166 	cpu_kh = kd->cpu_data;
167 
168 	off = 0;
169 	pa -= cpu_kh->core_seg.start;
170 
171 	return (kd->dump_off + off + pa);
172 }
173 
174 /*
175  * Machine-dependent initialization for ALL open kvm descriptors,
176  * not just those for a kernel crash dump.  Some architectures
177  * have to deal with these NOT being constants!  (i.e. m68k)
178  */
179 int
180 _kvm_mdopen(kd)
181 	kvm_t	*kd;
182 {
183 
184 	kd->usrstack = USRSTACK;
185 	kd->min_uva = VM_MIN_ADDRESS;
186 	kd->max_uva = VM_MAXUSER_ADDRESS;
187 
188 	return (0);
189 }
190