Sweep-fix comparing pointers with 0 (and assigning 0 to pointers).
[dragonfly.git] / lib / libkvm / kvm_i386.c
1 /*-
2  * Copyright (c) 1989, 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_hp300.c      8.1 (Berkeley) 6/4/93
38  * $FreeBSD: src/lib/libkvm/kvm_i386.c,v 1.11.2.1 2001/09/21 04:01:51 peter Exp $
39  */
40
41 /*
42  * i386 machine dependent routines for kvm.  Hopefully, the forthcoming
43  * vm code will one day obsolete this module.
44  */
45
46 #include <sys/user.h>   /* MUST BE FIRST */
47 #include <sys/param.h>
48 #include <sys/proc.h>
49 #include <sys/stat.h>
50 #include <sys/mman.h>
51 #include <sys/elf_common.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <nlist.h>
56 #include <kvm.h>
57
58 #include <vm/vm.h>
59 #include <vm/vm_param.h>
60
61 #include <machine/elf.h>
62
63 #include <limits.h>
64
65 #include "kvm_private.h"
66
67 #ifndef btop
68 #define btop(x)         (i386_btop(x))
69 #define ptob(x)         (i386_ptob(x))
70 #endif
71
72 /* minidump must be the first item! */
73 struct vmstate {
74         int             minidump;       /* 1 = minidump mode */
75         void            *mmapbase;
76         size_t          mmapsize;
77         void            *PTD;
78 };
79
80 /*
81  * Map the ELF headers into the process' address space. We do this in two
82  * steps: first the ELF header itself and using that information the whole
83  * set of headers. (Taken from kvm_ia64.c)
84  */
85 static int
86 _kvm_maphdrs(kvm_t *kd, size_t sz)
87 {
88         struct vmstate *vm = kd->vmst;
89
90         if (kd->vmst->minidump) {
91                 _kvm_minidump_freevtop(kd);
92                 return (0);
93         }
94
95         /* munmap() previous mmap(). */
96         if (vm->mmapbase != NULL) {
97                 munmap(vm->mmapbase, vm->mmapsize);
98                 vm->mmapbase = NULL;
99         }
100
101         vm->mmapsize = sz;
102         vm->mmapbase = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
103         if (vm->mmapbase == MAP_FAILED) {
104                 _kvm_err(kd, kd->program, "cannot mmap corefile");
105                 return (-1);
106         }
107         return (0);
108 }
109
110 /*
111  * Translate a physical memory address to a file-offset in the crash-dump.
112  * (Taken from kvm_ia64.c)
113  */
114 static size_t
115 _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs)
116 {
117         Elf_Ehdr *e = kd->vmst->mmapbase;
118         Elf_Phdr *p;
119         int n;
120
121         if (kd->rawdump) {
122                 *ofs = pa;
123                 return (PAGE_SIZE - ((size_t)pa & PAGE_MASK));
124         }
125
126         p = (Elf_Phdr*)((char*)e + e->e_phoff);
127         n = e->e_phnum;
128
129         while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz))
130                 p++, n--;
131         if (n == 0)
132                 return (0);
133         *ofs = (pa - p->p_paddr) + p->p_offset;
134         return (PAGE_SIZE - ((size_t)pa & PAGE_MASK));
135 }
136
137 void
138 _kvm_freevtop(kvm_t *kd)
139 {
140         struct vmstate  *vm = kd->vmst;
141
142         if (kd->vmst->minidump) {
143                 _kvm_minidump_freevtop(kd);
144                 return;
145         }
146
147         if (vm->mmapbase != NULL)
148                 munmap(vm->mmapbase, vm->mmapsize);
149         if (vm->PTD)
150                 free(vm->PTD);
151         free(vm);
152         kd->vmst = NULL;
153 }
154
155 int
156 _kvm_initvtop(kvm_t *kd)
157 {
158         struct nlist nlist[2];
159         u_long pa;
160         u_long kernbase;
161         char            *PTD;
162         Elf_Ehdr        *ehdr;
163         size_t          hdrsz;
164         char            minihdr[8];
165         struct pcb      dumppcb;
166
167         if (pread(kd->pmfd, &minihdr, 8, 0) == 8)
168                 if (memcmp(&minihdr, "minidump", 8) == 0)
169                         return (_kvm_minidump_initvtop(kd));
170
171         kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst));
172         if (kd->vmst == 0) {
173                 _kvm_err(kd, kd->program, "cannot allocate vm");
174                 return (-1);
175         }
176         kd->vmst->PTD = 0;
177
178         if (_kvm_maphdrs(kd, sizeof(Elf_Ehdr)) == -1)
179                 return (-1);
180         /*
181          * Check if this is indeed an ELF header. If not, assume old style dump or
182          * memory layout.
183          */
184         ehdr = kd->vmst->mmapbase;
185         if (!IS_ELF(*ehdr)) {
186                 kd->rawdump = 1;
187                 munmap(kd->vmst->mmapbase, kd->vmst->mmapsize);
188                 kd->vmst->mmapbase = NULL;
189         } else {
190                 hdrsz = ehdr->e_phoff + ehdr->e_phentsize * ehdr->e_phnum;
191                 if (_kvm_maphdrs(kd, hdrsz) == -1)
192                         return (-1);
193         }
194
195         nlist[0].n_name = "_kernbase";
196         nlist[1].n_name = 0;
197
198         if (kvm_nlist(kd, nlist) != 0)
199                 kernbase = KERNBASE;    /* for old kernels */
200         else
201                 kernbase = nlist[0].n_value;
202
203         nlist[0].n_name = "_dumppcb";
204         nlist[1].n_name = 0;
205
206         if (kvm_nlist(kd, nlist) != 0) {
207                 _kvm_err(kd, kd->program, "bad namelist");
208                 return (-1);
209         }
210
211         if (kvm_read(kd, (nlist[0].n_value - kernbase), &dumppcb,
212                      sizeof(dumppcb)) != sizeof(dumppcb)) {
213                 _kvm_err(kd, kd->program, "cannot read dumppcb");
214                 return (-1);
215         }
216         pa = dumppcb.pcb_cr3 & PG_FRAME;
217
218         PTD = _kvm_malloc(kd, PAGE_SIZE);
219         if (kvm_read(kd, pa, PTD, PAGE_SIZE) != PAGE_SIZE) {
220                 _kvm_err(kd, kd->program, "cannot read PTD");
221                 return (-1);
222         }
223         kd->vmst->PTD = PTD;
224         return (0);
225 }
226
227 static int
228 _kvm_vatop(kvm_t *kd, u_long va, off_t *pa)
229 {
230         struct vmstate *vm;
231         u_long offset;
232         u_long pte_pa;
233         u_long pde_pa;
234         pd_entry_t pde;
235         pt_entry_t pte;
236         u_long pdeindex;
237         u_long pteindex;
238         size_t  s;
239         u_long  a;
240         off_t   ofs;
241         uint32_t *PTD;
242
243
244         vm = kd->vmst;
245         PTD = (uint32_t *)vm->PTD;
246         offset = va & (PAGE_SIZE - 1);
247
248         /*
249          * If we are initializing (kernel page table descriptor pointer
250          * not yet set) then return pa == va to avoid infinite recursion.
251          */
252         if (PTD == NULL) {
253                 s = _kvm_pa2off(kd, va, pa);
254                 if (s == 0) {
255                         _kvm_err(kd, kd->program,
256                             "_kvm_vatop: bootstrap data not in dump");
257                         goto invalid;
258                 } else {
259                         return (PAGE_SIZE - offset);
260                 }
261         }
262
263         pdeindex = va >> PDRSHIFT;
264         pde = PTD[pdeindex];
265
266         if (((u_long)pde & PG_V) == 0) {
267                 _kvm_err(kd, kd->program, "_kvm_vatop: pde not valid");
268                 goto invalid;
269         }
270
271         if ((u_long)pde & PG_PS) {
272               /*
273                * No second-level page table; ptd describes one 4MB page.
274                * (We assume that the kernel wouldn't set PG_PS without enabling
275                * it cr0, and that the kernel doesn't support 36-bit physical
276                * addresses).
277                */
278 #define PAGE4M_MASK     (NBPDR - 1)
279 #define PG_FRAME4M      (~PAGE4M_MASK)
280 #if 0
281                 *pa = ((u_long)pde & PG_FRAME4M) + (va & PAGE4M_MASK);
282 #endif
283                 pde_pa = ((u_long)pde & PG_FRAME4M) + (va & PAGE4M_MASK);
284                 s = _kvm_pa2off(kd, pde_pa, &ofs);
285                 if (s < sizeof pde) {
286                         _kvm_syserr(kd, kd->program,
287                             "_kvm_vatop: pde_pa not found");
288                         goto invalid;
289                 }
290                 *pa = ofs;
291
292                 return (NBPDR - (va & PAGE4M_MASK));
293         }
294
295         pteindex = (va >> PAGE_SHIFT) & (NPTEPG-1);
296         pte_pa = ((u_long)pde & PG_FRAME) + (pteindex * sizeof(pde));
297
298         s = _kvm_pa2off(kd, pte_pa, &ofs);
299         if (s < sizeof pte) {
300                 _kvm_err(kd, kd->program, "_kvm_vatop: pdpe_pa not found");
301                 goto invalid;
302         }
303
304         /* XXX This has to be a physical address read, kvm_read is virtual */
305         if (lseek(kd->pmfd, ofs, 0) == -1) {
306                 _kvm_syserr(kd, kd->program, "_kvm_vatop: lseek");
307                 goto invalid;
308         }
309         if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) {
310                 _kvm_syserr(kd, kd->program, "_kvm_vatop: read");
311                 goto invalid;
312         }
313         if (((u_long)pte & PG_V) == 0) {
314                 _kvm_err(kd, kd->program, "_kvm_kvatop: pte not valid");
315                 goto invalid;
316         }
317
318         a = ((u_long)pte & PG_FRAME) + offset;
319         s =_kvm_pa2off(kd, a, pa);
320         if (s == 0) {
321                 _kvm_err(kd, kd->program, "_kvm_vatop: address not in dump");
322                 goto invalid;
323         } else
324                 return (PAGE_SIZE - offset);
325
326 invalid:
327         _kvm_err(kd, 0, "invalid address (0x%lx)", va);
328         return (0);
329 }
330
331 int
332 _kvm_kvatop(kvm_t *kd, u_long va, off_t *pa)
333 {
334         if (kd->vmst->minidump)
335                 return (_kvm_minidump_kvatop(kd, va, pa));
336
337         if (kvm_ishost(kd)) {
338                 _kvm_err(kd, 0, "vatop called in live kernel!");
339                 return (0);
340         }
341
342         return (_kvm_vatop(kd, va, pa));
343 }