mqueues: Let the world know we support them.
[dragonfly.git] / lib / libkvm / kvm_x86_64.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_amd64.c,v 1.16 2003/04/30 21:05:33 peter Exp $
39  */
40
41 /*
42  * x86_64 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 <stdlib.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 #ifndef btop
63 #define btop(x)         (x86_64_btop(x))
64 #define ptob(x)         (x86_64_ptob(x))
65 #endif
66
67 struct vmstate {
68         pml4_entry_t    *PML4;
69 };
70
71 void
72 _kvm_freevtop(kvm_t *kd)
73 {
74         if (kd->vmst != 0) {
75                 if (kd->vmst->PML4) {
76                         free(kd->vmst->PML4);
77                 }
78                 free(kd->vmst);
79         }
80 }
81
82 int
83 _kvm_initvtop(kvm_t *kd)
84 {
85         struct vmstate *vm;
86         struct nlist nlist[2];
87         u_long pa;
88         u_long kernbase;
89         pml4_entry_t    *PML4;
90
91         vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
92         if (vm == 0) {
93                 _kvm_err(kd, kd->program, "cannot allocate vm");
94                 return (-1);
95         }
96         kd->vmst = vm;
97         vm->PML4 = 0;
98
99         nlist[0].n_name = "kernbase";
100         nlist[1].n_name = 0;
101
102         if (kvm_nlist(kd, nlist) != 0) {
103                 _kvm_err(kd, kd->program, "bad namelist - no kernbase");
104                 return (-1);
105         }
106         kernbase = nlist[0].n_value;
107
108         nlist[0].n_name = "KPML4phys";
109         nlist[1].n_name = 0;
110
111         if (kvm_nlist(kd, nlist) != 0) {
112                 _kvm_err(kd, kd->program, "bad namelist - no KPML4phys");
113                 return (-1);
114         }
115         if (kvm_read(kd, (nlist[0].n_value - kernbase), &pa, sizeof(pa)) !=
116             sizeof(pa)) {
117                 _kvm_err(kd, kd->program, "cannot read KPML4phys");
118                 return (-1);
119         }
120         PML4 = _kvm_malloc(kd, PAGE_SIZE);
121         if (kvm_read(kd, pa, PML4, PAGE_SIZE) != PAGE_SIZE) {
122                 _kvm_err(kd, kd->program, "cannot read KPML4phys");
123                 return (-1);
124         }
125         vm->PML4 = PML4;
126         return (0);
127 }
128
129 static int
130 _kvm_vatop(kvm_t *kd, u_long va, u_long *pa)
131 {
132         struct vmstate *vm;
133         u_long offset;
134         u_long pdpe_pa;
135         u_long pde_pa;
136         u_long pte_pa;
137         pml4_entry_t pml4e;
138         pdp_entry_t pdpe;
139         pd_entry_t pde;
140         pt_entry_t pte;
141         u_long pml4eindex;
142         u_long pdpeindex;
143         u_long pdeindex;
144         u_long pteindex;
145
146         if (kvm_ishost(kd)) {
147                 _kvm_err(kd, 0, "kvm_vatop called in live kernel!");
148                 return((off_t)0);
149         }
150
151         vm = kd->vmst;
152         offset = va & (PAGE_SIZE - 1);
153
154         /*
155          * If we are initializing (kernel page table descriptor pointer
156          * not yet set) then return pa == va to avoid infinite recursion.
157          */
158         if (vm->PML4 == 0) {
159                 *pa = va;
160                 return (PAGE_SIZE - offset);
161         }
162
163         pml4eindex = (va >> PML4SHIFT) & (NPML4EPG - 1);
164         pml4e = vm->PML4[pml4eindex];
165         if (((u_long)pml4e & PG_V) == 0)
166                 goto invalid;
167
168         pdpeindex = (va >> PDPSHIFT) & (NPDPEPG-1);
169         pdpe_pa = ((u_long)pml4e & PG_FRAME) + (pdpeindex * sizeof(pdp_entry_t));
170
171         /* XXX This has to be a physical address read, kvm_read is virtual */
172         if (lseek(kd->pmfd, pdpe_pa, 0) == -1) {
173                 _kvm_syserr(kd, kd->program, "_kvm_vatop: lseek pdpe_pa");
174                 goto invalid;
175         }
176         if (read(kd->pmfd, &pdpe, sizeof pdpe) != sizeof pdpe) {
177                 _kvm_syserr(kd, kd->program, "_kvm_vatop: read pdpe");
178                 goto invalid;
179         }
180         if (((u_long)pdpe & PG_V) == 0)
181                 goto invalid;
182
183
184         pdeindex = (va >> PDRSHIFT) & (NPDEPG-1);
185         pde_pa = ((u_long)pdpe & PG_FRAME) + (pdeindex * sizeof(pd_entry_t));
186
187         /* XXX This has to be a physical address read, kvm_read is virtual */
188         if (lseek(kd->pmfd, pde_pa, 0) == -1) {
189                 _kvm_syserr(kd, kd->program, "_kvm_vatop: lseek pde_pa");
190                 goto invalid;
191         }
192         if (read(kd->pmfd, &pde, sizeof pde) != sizeof pde) {
193                 _kvm_syserr(kd, kd->program, "_kvm_vatop: read pde");
194                 goto invalid;
195         }
196         if (((u_long)pde & PG_V) == 0)
197                 goto invalid;
198
199         if ((u_long)pde & PG_PS) {
200               /*
201                * No final-level page table; ptd describes one 2MB page.
202                */
203 #define PAGE2M_MASK     (NBPDR - 1)
204 #define PG_FRAME2M      (~PAGE2M_MASK)
205                 *pa = ((u_long)pde & PG_FRAME2M) + (va & PAGE2M_MASK);
206                 return (NBPDR - (va & PAGE2M_MASK));
207         }
208
209         pteindex = (va >> PAGE_SHIFT) & (NPTEPG-1);
210         pte_pa = ((u_long)pde & PG_FRAME) + (pteindex * sizeof(pt_entry_t));
211
212         /* XXX This has to be a physical address read, kvm_read is virtual */
213         if (lseek(kd->pmfd, pte_pa, 0) == -1) {
214                 _kvm_syserr(kd, kd->program, "_kvm_vatop: lseek");
215                 goto invalid;
216         }
217         if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) {
218                 _kvm_syserr(kd, kd->program, "_kvm_vatop: read");
219                 goto invalid;
220         }
221         if (((u_long)pte & PG_V) == 0)
222                 goto invalid;
223
224         *pa = ((u_long)pte & PG_FRAME) + offset;
225         return (PAGE_SIZE - offset);
226
227 invalid:
228         _kvm_err(kd, 0, "invalid address (%jx)", (intmax_t)va);
229         return (0);
230 }
231
232 int
233 _kvm_kvatop(kvm_t *kd, u_long va, u_long *pa)
234 {
235         return (_kvm_vatop(kd, va, pa));
236 }