xref: /openbsd-src/lib/libkvm/kvm_alpha.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: kvm_alpha.c,v 1.7 2001/05/18 09:08:37 art Exp $	*/
2 /*	$NetBSD: kvm_alpha.c,v 1.5 1996/10/01 21:12:05 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1994, 1995 Carnegie-Mellon University.
6  * All rights reserved.
7  *
8  * Author: Chris G. Demetriou
9  *
10  * Permission to use, copy, modify and distribute this software and
11  * its documentation is hereby granted, provided that both the copyright
12  * notice and this permission notice appear in all copies of the
13  * software, derivative works or modified versions, and any portions
14  * thereof, and that both notices appear in supporting documentation.
15  *
16  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
18  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19  *
20  * Carnegie Mellon requests users of this software to return to
21  *
22  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
23  *  School of Computer Science
24  *  Carnegie Mellon University
25  *  Pittsburgh PA 15213-3890
26  *
27  * any improvements or extensions that they make and grant Carnegie the
28  * rights to redistribute these changes.
29  */
30 
31 #define __KVM_ALPHA_PRIVATE             /* see <machine/pte.h> */
32 
33 #include <sys/param.h>
34 #include <sys/user.h>
35 #include <sys/proc.h>
36 #include <sys/stat.h>
37 #include <sys/kcore.h>
38 #include <machine/kcore.h>
39 #include <unistd.h>
40 #include <nlist.h>
41 #include <kvm.h>
42 
43 #include <vm/vm.h>
44 #include <vm/vm_param.h>
45 
46 #include <limits.h>
47 #include <db.h>
48 
49 #include "kvm_private.h"
50 
51 struct vmstate {
52 	vsize_t	page_shift;
53 };
54 
55 void
56 _kvm_freevtop(kd)
57 	kvm_t *kd;
58 {
59 
60 	/* Not actually used for anything right now, but safe. */
61 	if (kd->vmst != 0)
62 		free(kd->vmst);
63 }
64 
65 int
66 _kvm_initvtop(kd)
67 	kvm_t *kd;
68 {
69         cpu_kcore_hdr_t *cpu_kh;
70         struct vmstate *vm;
71 
72         vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
73         if (vm == NULL)
74                 return (-1);
75 
76         cpu_kh = kd->cpu_data;
77 
78         /* Compute page_shift. */
79         for (vm->page_shift = 0; (1L << vm->page_shift) < cpu_kh->page_size;
80              vm->page_shift++)
81                 /* nothing */ ;
82         if ((1L << vm->page_shift) != cpu_kh->page_size) {
83                 free(vm);
84                 return (-1);
85         }
86 
87         kd->vmst = vm;
88 	return (0);
89 }
90 
91 int
92 _kvm_kvatop(kd, va, pa)
93 	kvm_t *kd;
94 	u_long va;
95 	u_long *pa;
96 {
97 	cpu_kcore_hdr_t *cpu_kh;
98 	struct vmstate *vm;
99 	int rv, page_off;
100 	alpha_pt_entry_t pte;
101 	off_t pteoff;
102 
103         if (ISALIVE(kd)) {
104                 _kvm_err(kd, 0, "vatop called in live kernel!");
105                 return(0);
106         }
107 
108 	cpu_kh = kd->cpu_data;
109 	vm = kd->vmst;
110 	page_off = va & (cpu_kh->page_size - 1);
111 
112 #ifndef PAGE_SHIFT
113 #define        PAGE_SHIFT      vm->page_shift
114 #endif
115 
116 	if (va >= ALPHA_K0SEG_BASE && va <= ALPHA_K0SEG_END) {
117 		/*
118 		 * Direct-mapped address: just convert it.
119 		 */
120 
121 		*pa = ALPHA_K0SEG_TO_PHYS(va);
122 		rv = cpu_kh->page_size - page_off;
123 	} else if (va >= ALPHA_K1SEG_BASE && va <= ALPHA_K1SEG_END) {
124 		/*
125 		 * Real kernel virtual address: do the translation.
126 		 */
127 
128 		/* Find and read the L1 PTE. */
129 		pteoff = cpu_kh->lev1map_pa +
130 		    l1pte_index(va) * sizeof(alpha_pt_entry_t);
131 		if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte), (off_t)_kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
132 			_kvm_syserr(kd, 0, "could not read L1 PTE");
133 			goto lose;
134 		}
135 
136 		/* Find and read the L2 PTE. */
137 		if ((pte & ALPHA_PTE_VALID) == 0) {
138 			_kvm_err(kd, 0, "invalid translation (invalid L1 PTE)");
139 			goto lose;
140 		}
141 		pteoff = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size +
142 		    l2pte_index(va) * sizeof(alpha_pt_entry_t);
143 		if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte), (off_t)_kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
144 			_kvm_syserr(kd, 0, "could not read L2 PTE");
145 			goto lose;
146 		}
147 
148 		/* Find and read the L3 PTE. */
149 		if ((pte & ALPHA_PTE_VALID) == 0) {
150 			_kvm_err(kd, 0, "invalid translation (invalid L2 PTE)");
151 			goto lose;
152 		}
153 		pteoff = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size +
154 		    l3pte_index(va) * sizeof(alpha_pt_entry_t);
155 		if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte), (off_t)_kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
156 			_kvm_syserr(kd, 0, "could not read L3 PTE");
157 			goto lose;
158 		}
159 
160 		/* Fill in the PA. */
161 		if ((pte & ALPHA_PTE_VALID) == 0) {
162 			_kvm_err(kd, 0, "invalid translation (invalid L3 PTE)");
163 			goto lose;
164 		}
165 		*pa = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size + page_off;
166 		rv = cpu_kh->page_size - page_off;
167 	} else {
168 		/*
169 		 * Bogus address (not in KV space): punt.
170 		 */
171 
172 		_kvm_err(kd, 0, "invalid kernel virtual address");
173 lose:
174 		*pa = -1;
175 		rv = 0;
176 	}
177 
178 	return (rv);
179 }
180 
181 /*
182  * Translate a physical address to a file-offset in the crash-dump.
183  */
184 off_t
185 _kvm_pa2off(kd, pa)
186 	kvm_t *kd;
187 	u_long pa;
188 {
189 	cpu_kcore_hdr_t *cpu_kh;
190 	phys_ram_seg_t *ramsegs;
191 	off_t off;
192 	int i;
193 
194 	cpu_kh = kd->cpu_data;
195 	ramsegs = (phys_ram_seg_t *)((char *)cpu_kh + ALIGN(sizeof *cpu_kh));
196 
197 	off = 0;
198 	for (i = 0; i < cpu_kh->nmemsegs; i++) {
199 		if (pa >= ramsegs[i].start &&
200 		   (pa - ramsegs[i].start) < ramsegs[i].size) {
201 			off += (pa - ramsegs[i].start);
202 			break;
203 		}
204 		off += ramsegs[i].size;
205 	}
206 	return (kd->dump_off + off);
207 }
208