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