Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[dragonfly.git] / lib / libkvm / kvm_sparc.c
1 /*-
2  * Copyright (c) 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software developed by the Computer Systems
6  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7  * BG 91-66 and contributed to Berkeley.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)kvm_sparc.c      8.1 (Berkeley) 6/4/93
34  * $FreeBSD: src/lib/libkvm/kvm_sparc.c,v 1.3 1999/12/27 07:14:58 peter Exp $
35  */
36
37 /*
38  * Sparc machine dependent routines for kvm.  Hopefully, the forthcoming
39  * vm code will one day obsolete this module.
40  */
41
42 #include <sys/user.h>   /* MUST BE FIRST */
43 #include <sys/param.h>
44 #include <sys/proc.h>
45 #include <sys/stat.h>
46 #include <unistd.h>
47 #include <nlist.h>
48 #include <kvm.h>
49
50 #include <vm/vm.h>
51 #include <vm/vm_param.h>
52
53 #include <limits.h>
54
55 #include "kvm_private.h"
56
57 #define NPMEG 128
58
59 /* XXX from sparc/pmap.c */
60 #define MAXMEM  (128 * 1024 * 1024)     /* no more than 128 MB phys mem */
61 #define NPGBANK 16                      /* 2^4 pages per bank (64K / bank) */
62 #define BSHIFT  4                       /* log2(NPGBANK) */
63 #define BOFFSET (NPGBANK - 1)
64 #define BTSIZE  (MAXMEM / NBPG / NPGBANK)
65 #define HWTOSW(pmap_stod, pg) (pmap_stod[(pg) >> BSHIFT] | ((pg) & BOFFSET))
66
67 struct vmstate {
68         pmeg_t segmap[NKSEG];
69         int pmeg[NPMEG][NPTESG];
70         int pmap_stod[BTSIZE];              /* dense to sparse */
71 };
72
73 void
74 _kvm_freevtop(kvm_t *kd)
75 {
76         if (kd->vmst != 0)
77                 free(kd->vmst);
78 }
79
80 int
81 _kvm_initvtop(kvm_t *kd)
82 {
83         int i;
84         int off;
85         struct vmstate *vm;
86         struct stat st;
87         struct nlist nlist[2];
88
89         vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
90         if (vm == NULL)
91                 return (-1);
92
93         kd->vmst = vm;
94
95         if (fstat(kd->pmfd, &st) < 0)
96                 return (-1);
97         /*
98          * Read segment table.
99          */
100         off = st.st_size - ctob(btoc(sizeof(vm->segmap)));
101         errno = 0;
102         if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
103             read(kd->pmfd, (char *)vm->segmap, sizeof(vm->segmap)) < 0) {
104                 _kvm_err(kd, kd->program, "cannot read segment map");
105                 return (-1);
106         }
107         /*
108          * Read PMEGs.
109          */
110         off = st.st_size - ctob(btoc(sizeof(vm->pmeg)) +
111             btoc(sizeof(vm->segmap)));
112         errno = 0;
113         if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
114             read(kd->pmfd, (char *)vm->pmeg, sizeof(vm->pmeg)) < 0) {
115                 _kvm_err(kd, kd->program, "cannot read PMEG table");
116                 return (-1);
117         }
118         /*
119          * Make pmap_stod be an identity map so we can bootstrap it in.
120          * We assume it's in the first contiguous chunk of physical memory.
121          */
122         for (i = 0; i < BTSIZE; ++i)
123                 vm->pmap_stod[i] = i << 4;
124
125         /*
126          * It's okay to do this nlist separately from the one kvm_getprocs()
127          * does, since the only time we could gain anything by combining
128          * them is if we do a kvm_getprocs() on a dead kernel, which is
129          * not too common.
130          */
131         nlist[0].n_name = "_pmap_stod";
132         nlist[1].n_name = 0;
133         if (kvm_nlist(kd, nlist) != 0) {
134                 _kvm_err(kd, kd->program, "pmap_stod: no such symbol");
135                 return (-1);
136         }
137         if (kvm_read(kd, (u_long)nlist[0].n_value,
138                      (char *)vm->pmap_stod, sizeof(vm->pmap_stod))
139             != sizeof(vm->pmap_stod)) {
140                 _kvm_err(kd, kd->program, "cannot read pmap_stod");
141                 return (-1);
142         }
143         return (0);
144 }
145
146 #define VA_OFF(va) (va & (NBPG - 1))
147
148 /*
149  * Translate a user virtual address to a physical address.
150  */
151 int
152 _kvm_uvatop(kvm_t *kd, const struct proc *p, u_long va, u_long *pa)
153 {
154         int kva, pte;
155         int off, frame;
156         struct vmspace *vms = p->p_vmspace;
157
158         if ((u_long)vms < KERNBASE) {
159                 _kvm_err(kd, kd->program, "_kvm_uvatop: corrupt proc");
160                 return (0);
161         }
162         if (va >= KERNBASE)
163                 return (0);
164         /*
165          * Get the PTE.  This takes two steps.  We read the
166          * base address of the table, then we index it.
167          * Note that the index pte table is indexed by
168          * virtual segment rather than physical segment.
169          */
170         kva = (u_long)&vms->vm_pmap.pm_rpte[VA_VSEG(va)];
171         if (kvm_read(kd, kva, (char *)&kva, 4) != 4 || kva == 0)
172                 goto invalid;
173         kva += sizeof(vms->vm_pmap.pm_rpte[0]) * VA_VPG(va);
174         if (kvm_read(kd, kva, (char *)&pte, 4) == 4 && (pte & PG_V)) {
175                 off = VA_OFF(va);
176                 /*
177                  * /dev/mem adheres to the hardware model of physical memory
178                  * (with holes in the address space), while crashdumps
179                  * adhere to the contiguous software model.
180                  */
181                 if (kvm_ishost(kd))
182                         frame = pte & PG_PFNUM;
183                 else
184                         frame = HWTOSW(kd->vmst->pmap_stod, pte & PG_PFNUM);
185                 *pa = (frame << PGSHIFT) | off;
186                 return (NBPG - off);
187         }
188 invalid:
189         _kvm_err(kd, 0, "invalid address (%x)", va);
190         return (0);
191 }
192
193 /*
194  * Translate a kernel virtual address to a physical address using the
195  * mapping information in kd->vm.  Returns the result in pa, and returns
196  * the number of bytes that are contiguously available from this
197  * physical address.  This routine is used only for crashdumps.
198  */
199 int
200 _kvm_kvatop(kvm_t *kd, u_long va, u_long *pa)
201 {
202         struct vmstate *vm;
203         int s;
204         int pte;
205         int off;
206
207         if (va >= KERNBASE) {
208                 vm = kd->vmst;
209                 s = vm->segmap[VA_VSEG(va) - NUSEG];
210                 pte = vm->pmeg[s][VA_VPG(va)];
211                 if ((pte & PG_V) != 0) {
212                         off = VA_OFF(va);
213                         *pa = (HWTOSW(vm->pmap_stod, pte & PG_PFNUM)
214                                << PGSHIFT) | off;
215
216                         return (NBPG - off);
217                 }
218         }
219         _kvm_err(kd, 0, "invalid address (%x)", va);
220         return (0);
221 }