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