2 * Copyright (c) 1989, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
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.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
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 $
42 * x86_64 machine dependent routines for kvm. Hopefully, the forthcoming
43 * vm code will one day obsolete this module.
46 #include <sys/user.h> /* MUST BE FIRST */
47 #include <sys/param.h>
56 #include <vm/vm_param.h>
60 #include "kvm_private.h"
63 #define btop(x) (x86_64_btop(x))
64 #define ptob(x) (x86_64_ptob(x))
72 _kvm_freevtop(kvm_t
*kd
)
83 _kvm_initvtop(kvm_t
*kd
)
86 struct nlist nlist
[2];
91 vm
= (struct vmstate
*)_kvm_malloc(kd
, sizeof(*vm
));
93 _kvm_err(kd
, kd
->program
, "cannot allocate vm");
99 nlist
[0].n_name
= "kernbase";
102 if (kvm_nlist(kd
, nlist
) != 0) {
103 _kvm_err(kd
, kd
->program
, "bad namelist - no kernbase");
106 kernbase
= nlist
[0].n_value
;
108 nlist
[0].n_name
= "KPML4phys";
111 if (kvm_nlist(kd
, nlist
) != 0) {
112 _kvm_err(kd
, kd
->program
, "bad namelist - no KPML4phys");
115 if (kvm_read(kd
, (nlist
[0].n_value
- kernbase
), &pa
, sizeof(pa
)) !=
117 _kvm_err(kd
, kd
->program
, "cannot read KPML4phys");
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");
130 _kvm_vatop(kvm_t
*kd
, u_long va
, u_long
*pa
)
146 if (kvm_ishost(kd
)) {
147 _kvm_err(kd
, 0, "kvm_vatop called in live kernel!");
152 offset
= va
& (PAGE_SIZE
- 1);
155 * If we are initializing (kernel page table descriptor pointer
156 * not yet set) then return pa == va to avoid infinite recursion.
160 return (PAGE_SIZE
- offset
);
163 pml4eindex
= (va
>> PML4SHIFT
) & (NPML4EPG
- 1);
164 pml4e
= vm
->PML4
[pml4eindex
];
165 if (((u_long
)pml4e
& PG_V
) == 0)
168 pdpeindex
= (va
>> PDPSHIFT
) & (NPDPEPG
-1);
169 pdpe_pa
= ((u_long
)pml4e
& PG_FRAME
) + (pdpeindex
* sizeof(pdp_entry_t
));
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");
176 if (read(kd
->pmfd
, &pdpe
, sizeof pdpe
) != sizeof pdpe
) {
177 _kvm_syserr(kd
, kd
->program
, "_kvm_vatop: read pdpe");
180 if (((u_long
)pdpe
& PG_V
) == 0)
184 pdeindex
= (va
>> PDRSHIFT
) & (NPDEPG
-1);
185 pde_pa
= ((u_long
)pdpe
& PG_FRAME
) + (pdeindex
* sizeof(pd_entry_t
));
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");
192 if (read(kd
->pmfd
, &pde
, sizeof pde
) != sizeof pde
) {
193 _kvm_syserr(kd
, kd
->program
, "_kvm_vatop: read pde");
196 if (((u_long
)pde
& PG_V
) == 0)
199 if ((u_long
)pde
& PG_PS
) {
201 * No final-level page table; ptd describes one 2MB page.
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
));
209 pteindex
= (va
>> PAGE_SHIFT
) & (NPTEPG
-1);
210 pte_pa
= ((u_long
)pde
& PG_FRAME
) + (pteindex
* sizeof(pt_entry_t
));
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");
217 if (read(kd
->pmfd
, &pte
, sizeof pte
) != sizeof pte
) {
218 _kvm_syserr(kd
, kd
->program
, "_kvm_vatop: read");
221 if (((u_long
)pte
& PG_V
) == 0)
224 *pa
= ((u_long
)pte
& PG_FRAME
) + offset
;
225 return (PAGE_SIZE
- offset
);
228 _kvm_err(kd
, 0, "invalid address (%jx)", (intmax_t)va
);
233 _kvm_kvatop(kvm_t
*kd
, u_long va
, u_long
*pa
)
235 return (_kvm_vatop(kd
, va
, pa
));