Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / platform / pc64 / vmm / ept.h
1 /*
2  * Copyright (c) 2003-2013 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Mihai Carabas <mihai.carabas@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #ifndef _VMM_EPT_H_
36 #define _VMM_EPT_H_
37
38 #include <vm/vm.h>
39
40 #include <machine/pmap.h>
41
42 /* EPT defines */
43 #define EPT_PWL4(cap)                   ((cap) & (1ULL << 6))
44 #define EPT_MEMORY_TYPE_WB(cap)         ((cap) & (1UL << 14))
45 #define EPT_AD_BITS_SUPPORTED(cap)      ((cap) & (1ULL << 21))
46 #define EPT_PG_READ                     (0x1ULL << 0)
47 #define EPT_PG_WRITE                    (0x1ULL << 1)
48 #define EPT_PG_EXECUTE                  (0x1ULL << 2)
49 #define EPT_IGNORE_PAT                  (0x1ULL << 6)
50 #define EPT_PG_PS                       (0x1ULL << 7)
51 #define EPT_PG_A                        (0x1ULL << 8)
52 #define EPT_PG_M                        (0x1ULL << 9)
53 #define EPT_PG_AVAIL1                   (0x1ULL << 10)
54 #define EPT_PG_AVAIL2                   (0x1ULL << 11)
55 #define EPT_PG_AVAIL3                   (0x1ULL << 52)
56 #define EPT_PWLEVELS                    (4)     /* page walk levels */
57
58 #define EPTP_CACHE(x)                   (x)
59 #define EPTP_PWLEN(x)                   ((x) << 3)
60 #define EPTP_AD_ENABLE                  (0x1ULL << 6)
61
62 #define EPT_MEM_TYPE_SHIFT              (0x3)
63 #define EPT_MEM_TYPE_UC                 (0x0ULL << EPT_MEM_TYPE_SHIFT)
64 #define EPT_MEM_TYPE_WC                 (0x1ULL << EPT_MEM_TYPE_SHIFT)
65 #define EPT_MEM_TYPE_WT                 (0x4ULL << EPT_MEM_TYPE_SHIFT)
66 #define EPT_MEM_TYPE_WP                 (0x5ULL << EPT_MEM_TYPE_SHIFT)
67 #define EPT_MEM_TYPE_WB                 (0x6ULL << EPT_MEM_TYPE_SHIFT)
68 #define EPT_MEM_TYPE_MASK               (0x7ULL << EPT_MEM_TYPE_SHIFT)
69
70 #define EPT_VIOLATION_READ              (1ULL << 0)
71 #define EPT_VIOLATION_WRITE             (1ULL << 1)
72 #define EPT_VIOLATION_INST_FETCH        (1ULL << 2)
73 #define EPT_VIOLATION_GPA_READABLE      (1ULL << 3)
74 #define EPT_VIOLATION_GPA_WRITEABLE     (1ULL << 4)
75 #define EPT_VIOLATION_GPA_EXECUTABLE    (1ULL << 5)
76
77 #define INVEPT_TYPE_SINGLE_CONTEXT      1UL
78 #define INVEPT_TYPE_ALL_CONTEXTS        2UL
79
80 struct invept_desc {
81         uint64_t        eptp;
82         uint64_t        _res;
83 };
84 typedef struct invept_desc invept_desc_t;
85
86 CTASSERT(sizeof(struct invept_desc) == 16);
87
88 int vmx_ept_init(void);
89 void vmx_ept_pmap_pinit(pmap_t pmap);
90 uint64_t vmx_eptp(uint64_t ept_address);
91
92 static __inline int
93 vmx_ept_fault_type(uint64_t qualification)
94 {
95         if (qualification & EPT_VIOLATION_WRITE)
96                 return VM_PROT_WRITE;
97         else if (qualification & EPT_VIOLATION_INST_FETCH)
98                 return VM_PROT_EXECUTE;
99         else
100                 return VM_PROT_READ;
101 }
102
103 static __inline int
104 vmx_ept_gpa_prot(uint64_t qualification)
105 {
106         int prot = 0;
107
108         if (qualification & EPT_VIOLATION_GPA_READABLE)
109                 prot |= VM_PROT_READ;
110
111         if (qualification & EPT_VIOLATION_GPA_WRITEABLE)
112                 prot |= VM_PROT_WRITE;
113
114         if (qualification & EPT_VIOLATION_GPA_EXECUTABLE)
115                 prot |= VM_PROT_EXECUTE;
116
117         return prot;
118 }
119
120 #endif