1/*-
2 * Copyright (c) 2015 John H. Baldwin <jhb@FreeBSD.org>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#ifndef __KVM_AMD64_H__
27#define	__KVM_AMD64_H__
28
29#ifdef __amd64__
30#include <vm/vm.h>
31#include <vm/pmap.h>
32#endif
33
34typedef uint64_t	amd64_physaddr_t;
35typedef uint64_t	amd64_pte_t;
36typedef uint64_t	amd64_pde_t;
37typedef uint64_t	amd64_pdpe_t;
38typedef	uint64_t	amd64_pml4e_t;
39
40#define	AMD64_NPTEPG		(AMD64_PAGE_SIZE / sizeof(amd64_pte_t))
41#define	AMD64_PAGE_SHIFT	12
42#define	AMD64_PAGE_SIZE		(1 << AMD64_PAGE_SHIFT)
43#define	AMD64_PAGE_MASK		(AMD64_PAGE_SIZE - 1)
44#define	AMD64_NPDEPG		(AMD64_PAGE_SIZE / sizeof(amd64_pde_t))
45#define	AMD64_PDRSHIFT		21
46#define	AMD64_NBPDR		(1 << AMD64_PDRSHIFT)
47#define	AMD64_PDRMASK		(AMD64_NBPDR - 1)
48#define	AMD64_NPDPEPG		(AMD64_PAGE_SIZE / sizeof(amd64_pdpe_t))
49#define	AMD64_PDPSHIFT		30
50#define	AMD64_NBPDP		(1 << AMD64_PDPSHIFT)
51#define	AMD64_PDPMASK		(AMD64_NBPDP - 1)
52#define	AMD64_NPML4EPG		(AMD64_PAGE_SIZE / sizeof(amd64_pml4e_t))
53#define	AMD64_PML4SHIFT		39
54
55#define	AMD64_PG_NX		(1ULL << 63)
56#define	AMD64_PG_V		0x001
57#define	AMD64_PG_RW		0x002
58#define	AMD64_PG_PS		0x080
59#define	AMD64_PG_FRAME		(0x000ffffffffff000)
60#define	AMD64_PG_PS_FRAME	(0x000fffffffe00000)
61#define	AMD64_PG_1GB_FRAME	(0x000fffffc0000000)
62
63#ifdef __amd64__
64_Static_assert(NPTEPG == AMD64_NPTEPG, "NPTEPG mismatch");
65_Static_assert(PAGE_SHIFT == AMD64_PAGE_SHIFT, "PAGE_SHIFT mismatch");
66_Static_assert(PAGE_SIZE == AMD64_PAGE_SIZE, "PAGE_SIZE mismatch");
67_Static_assert(PAGE_MASK == AMD64_PAGE_MASK, "PAGE_MASK mismatch");
68_Static_assert(NPDEPG == AMD64_NPDEPG, "NPDEPG mismatch");
69_Static_assert(PDRSHIFT == AMD64_PDRSHIFT, "PDRSHIFT mismatch");
70_Static_assert(NBPDR == AMD64_NBPDR, "NBPDR mismatch");
71_Static_assert(PDRMASK == AMD64_PDRMASK, "PDRMASK mismatch");
72_Static_assert(NPDPEPG == AMD64_NPDPEPG, "NPDPEPG mismatch");
73_Static_assert(PDPSHIFT == AMD64_PDPSHIFT, "PDPSHIFT mismatch");
74_Static_assert(NBPDP == AMD64_NBPDP, "NBPDP mismatch");
75_Static_assert(PDPMASK == AMD64_PDPMASK, "PDPMASK mismatch");
76_Static_assert(NPML4EPG == AMD64_NPML4EPG, "NPML4EPG mismatch");
77_Static_assert(PML4SHIFT == AMD64_PML4SHIFT, "PML4SHIFT mismatch");
78
79_Static_assert(PG_V == AMD64_PG_V, "PG_V mismatch");
80_Static_assert(PG_PS == AMD64_PG_PS, "PG_PS mismatch");
81_Static_assert(PG_FRAME == AMD64_PG_FRAME, "PG_FRAME mismatch");
82_Static_assert(PG_PS_FRAME == AMD64_PG_PS_FRAME, "PG_PS_FRAME mismatch");
83#endif
84
85int	_amd64_native(kvm_t *);
86
87#endif /* !__KVM_AMD64_H__ */
88