xref: /openbsd-src/lib/libkvm/kvm_alpha.c (revision b725ae7711052a2233e31a66fefb8a752c388d7a)
1 /*	$OpenBSD: kvm_alpha.c,v 1.10 2001/12/05 02:23:11 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 <uvm/uvm_extern.h>
44 #include <machine/vmparam.h>
45 #include <machine/pmap.h>
46 
47 #include <limits.h>
48 #include <db.h>
49 
50 #include "kvm_private.h"
51 
52 struct vmstate {
53 	vsize_t	page_shift;
54 };
55 
56 void
57 _kvm_freevtop(kd)
58 	kvm_t *kd;
59 {
60 
61 	/* Not actually used for anything right now, but safe. */
62 	if (kd->vmst != 0)
63 		free(kd->vmst);
64 }
65 
66 int
67 _kvm_initvtop(kd)
68 	kvm_t *kd;
69 {
70         cpu_kcore_hdr_t *cpu_kh;
71         struct vmstate *vm;
72 
73         vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
74         if (vm == NULL)
75                 return (-1);
76 
77         cpu_kh = kd->cpu_data;
78 
79         /* Compute page_shift. */
80         for (vm->page_shift = 0; (1L << vm->page_shift) < cpu_kh->page_size;
81              vm->page_shift++)
82                 /* nothing */ ;
83         if ((1L << vm->page_shift) != cpu_kh->page_size) {
84                 free(vm);
85                 return (-1);
86         }
87 
88         kd->vmst = vm;
89 	return (0);
90 }
91 
92 int
93 _kvm_kvatop(kd, va, pa)
94 	kvm_t *kd;
95 	u_long va;
96 	u_long *pa;
97 {
98 	cpu_kcore_hdr_t *cpu_kh;
99 	struct vmstate *vm;
100 	int rv, page_off;
101 	alpha_pt_entry_t pte;
102 	off_t pteoff;
103 
104         if (ISALIVE(kd)) {
105                 _kvm_err(kd, 0, "vatop called in live kernel!");
106                 return(0);
107         }
108 
109 	cpu_kh = kd->cpu_data;
110 	vm = kd->vmst;
111 	page_off = va & (cpu_kh->page_size - 1);
112 
113 #ifndef PAGE_SHIFT
114 #define        PAGE_SHIFT      vm->page_shift
115 #endif
116 
117 	if (va >= ALPHA_K0SEG_BASE && va <= ALPHA_K0SEG_END) {
118 		/*
119 		 * Direct-mapped address: just convert it.
120 		 */
121 
122 		*pa = ALPHA_K0SEG_TO_PHYS(va);
123 		rv = cpu_kh->page_size - page_off;
124 	} else if (va >= ALPHA_K1SEG_BASE && va <= ALPHA_K1SEG_END) {
125 		/*
126 		 * Real kernel virtual address: do the translation.
127 		 */
128 
129 		/* Find and read the L1 PTE. */
130 		pteoff = cpu_kh->lev1map_pa +
131 		    l1pte_index(va) * sizeof(alpha_pt_entry_t);
132 		if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte), (off_t)_kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
133 			_kvm_syserr(kd, 0, "could not read L1 PTE");
134 			goto lose;
135 		}
136 
137 		/* Find and read the L2 PTE. */
138 		if ((pte & ALPHA_PTE_VALID) == 0) {
139 			_kvm_err(kd, 0, "invalid translation (invalid L1 PTE)");
140 			goto lose;
141 		}
142 		pteoff = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size +
143 		    l2pte_index(va) * sizeof(alpha_pt_entry_t);
144 		if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte), (off_t)_kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
145 			_kvm_syserr(kd, 0, "could not read L2 PTE");
146 			goto lose;
147 		}
148 
149 		/* Find and read the L3 PTE. */
150 		if ((pte & ALPHA_PTE_VALID) == 0) {
151 			_kvm_err(kd, 0, "invalid translation (invalid L2 PTE)");
152 			goto lose;
153 		}
154 		pteoff = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size +
155 		    l3pte_index(va) * sizeof(alpha_pt_entry_t);
156 		if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte), (off_t)_kvm_pa2off(kd, pteoff)) != sizeof(pte)) {
157 			_kvm_syserr(kd, 0, "could not read L3 PTE");
158 			goto lose;
159 		}
160 
161 		/* Fill in the PA. */
162 		if ((pte & ALPHA_PTE_VALID) == 0) {
163 			_kvm_err(kd, 0, "invalid translation (invalid L3 PTE)");
164 			goto lose;
165 		}
166 		*pa = ALPHA_PTE_TO_PFN(pte) * cpu_kh->page_size + page_off;
167 		rv = cpu_kh->page_size - page_off;
168 	} else {
169 		/*
170 		 * Bogus address (not in KV space): punt.
171 		 */
172 
173 		_kvm_err(kd, 0, "invalid kernel virtual address");
174 lose:
175 		*pa = -1;
176 		rv = 0;
177 	}
178 
179 	return (rv);
180 }
181 
182 /*
183  * Translate a physical address to a file-offset in the crash-dump.
184  */
185 off_t
186 _kvm_pa2off(kd, pa)
187 	kvm_t *kd;
188 	u_long pa;
189 {
190 	cpu_kcore_hdr_t *cpu_kh;
191 	phys_ram_seg_t *ramsegs;
192 	off_t off;
193 	int i;
194 
195 	cpu_kh = kd->cpu_data;
196 	ramsegs = (phys_ram_seg_t *)((char *)cpu_kh + ALIGN(sizeof *cpu_kh));
197 
198 	off = 0;
199 	for (i = 0; i < cpu_kh->nmemsegs; i++) {
200 		if (pa >= ramsegs[i].start &&
201 		   (pa - ramsegs[i].start) < ramsegs[i].size) {
202 			off += (pa - ramsegs[i].start);
203 			break;
204 		}
205 		off += ramsegs[i].size;
206 	}
207 	return (kd->dump_off + off);
208 }
209