x64.h
Go to the documentation of this file.
1 //
2 // Bareflank Hypervisor
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Brendan Kerrigan <kerriganb@ainfosec.com>
7 // Author: Connor Davis <davisc@ainfosec.com>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 
23 #ifndef X64_H
24 #define X64_H
25 
26 #include <gsl/gsl>
27 #include <type_traits>
28 
29 #include <intrinsics/cpuid_x64.h>
30 
31 // *INDENT-OFF*
32 
33 namespace x64
34 {
35  constexpr const auto page_size = 0x1000UL;
36  constexpr const auto page_shift = 12UL;
37  constexpr const auto cache_line_size = 64UL;
38  constexpr const auto cache_line_shift = 6UL;
39 
40  namespace memory_type
41  {
42  constexpr const auto uncacheable = 0x00000000UL;
43  constexpr const auto write_combining = 0x00000001UL;
44  constexpr const auto write_through = 0x00000004UL;
45  constexpr const auto write_protected = 0x00000005UL;
46  constexpr const auto write_back = 0x00000006UL;
47  constexpr const auto uncacheable_minus = 0x00000007UL;
48  }
49 
50  namespace access_rights
51  {
52  namespace type
53  {
54  constexpr const auto tss_busy = 0x0000000BU;
55  constexpr const auto tss_available = 0x00000009U;
56 
57  constexpr const auto read_only = 0x00000000U;
58  constexpr const auto read_only_accessed = 0x00000001U;
59  constexpr const auto read_write = 0x00000002U;
60  constexpr const auto read_write_accessed = 0x00000003U;
61  constexpr const auto read_only_expand_down = 0x00000004U;
62  constexpr const auto read_only_expand_down_accessed = 0x00000005U;
63  constexpr const auto read_write_expand_down = 0x00000006U;
64  constexpr const auto read_write_expand_down_accessed = 0x00000007U;
65 
66  constexpr const auto execute_only = 0x00000008U;
67  constexpr const auto execute_only_accessed = 0x00000009U;
68  constexpr const auto read_execute = 0x0000000AU;
69  constexpr const auto read_execute_accessed = 0x0000000BU;
70  constexpr const auto execute_only_conforming = 0x0000000CU;
71  constexpr const auto execute_only_conforming_accessed = 0x0000000DU;
72  constexpr const auto read_execute_conforming = 0x0000000EU;
73  constexpr const auto read_execute_conforming_accessed = 0x0000000FU;
74  }
75 
76  namespace dpl
77  {
78  constexpr const auto ring0 = 0x00000000U;
79  constexpr const auto ring1 = 0x00000001U;
80  constexpr const auto ring2 = 0x00000002U;
81  constexpr const auto ring3 = 0x00000003U;
82  }
83 
84  constexpr const auto ring0_cs_descriptor = 0x0000A09BU;
85  constexpr const auto ring0_ss_descriptor = 0x0000C093U;
86  constexpr const auto ring0_fs_descriptor = 0x00008093U;
87  constexpr const auto ring0_gs_descriptor = 0x00008093U;
88  constexpr const auto ring0_tr_descriptor = 0x0000008BU;
89 
90  constexpr const auto unusable = 0x00010000U;
91  }
92 
93  namespace interrupt
94  {
95  constexpr const auto divide_error = 0U;
96  constexpr const auto debug_exception = 1U;
97  constexpr const auto nmi_interrupt = 2U;
98  constexpr const auto breakpoint = 3U;
99  constexpr const auto overflow = 4U;
100  constexpr const auto bound_range_exceeded = 5U;
101  constexpr const auto invalid_opcode = 6U;
102  constexpr const auto device_not_available = 7U;
103  constexpr const auto double_fault = 8U;
104  constexpr const auto coprocessor_segment_overrun = 9U;
105  constexpr const auto invalid_tss = 10U;
106  constexpr const auto segment_not_present = 11U;
107  constexpr const auto stack_segment_fault = 12U;
108  constexpr const auto general_protection = 13U;
109  constexpr const auto page_fault = 14U;
110  constexpr const auto floating_point_error = 16U;
111  constexpr const auto alignment_check = 17U;
112  constexpr const auto machine_check = 18U;
113  constexpr const auto simd_floating_point_exception = 19U;
114  constexpr const auto virtualization_exception = 20U;
115  }
116 
117  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
119  { return ((addr <= 0x00007FFFFFFFFFFFUL) || (addr >= 0xFFFF800000000000UL)); }
120 
121  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
123  { return is_address_canonical(addr); }
124 
125  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
127  {
128  auto bits = cpuid::addr_size::phys::get();
129  auto mask = (0xFFFFFFFFFFFFFFFFUL >> bits) << bits;
130 
131  return ((addr & mask) == 0);
132  }
133 }
134 
135 // *INDENT-ON*
136 
137 #endif
constexpr const auto overflow
Definition: x64.h:99
constexpr const auto invalid_opcode
Definition: x64.h:101
constexpr const auto ring0_cs_descriptor
Definition: x64.h:84
constexpr const auto nmi_interrupt
Definition: x64.h:97
constexpr const auto cache_line_size
Definition: x64.h:37
constexpr const auto read_write_expand_down
Definition: x64.h:63
constexpr const auto alignment_check
Definition: x64.h:111
constexpr const auto double_fault
Definition: x64.h:103
constexpr const auto execute_only_conforming
Definition: x64.h:70
constexpr const auto mask
Definition: cpuid_x64.h:85
constexpr const auto coprocessor_segment_overrun
Definition: x64.h:104
auto is_physical_address_valid(T addr)
Definition: x64.h:126
constexpr const auto breakpoint
Definition: x64.h:98
constexpr const auto read_execute_accessed
Definition: x64.h:69
constexpr const auto read_write_expand_down_accessed
Definition: x64.h:64
constexpr const auto read_only_expand_down_accessed
Definition: x64.h:62
auto is_linear_address_valid(T addr)
Definition: x64.h:122
constexpr const auto unusable
Definition: x64.h:90
constexpr const auto device_not_available
Definition: x64.h:102
constexpr const auto ring2
Definition: x64.h:80
constexpr const auto read_write
Definition: x64.h:59
constexpr const auto read_only_accessed
Definition: x64.h:58
constexpr const auto simd_floating_point_exception
Definition: x64.h:113
constexpr const auto read_write_accessed
Definition: x64.h:60
constexpr const auto execute_only_accessed
Definition: x64.h:67
constexpr const auto addr
Definition: cpuid_x64.h:80
constexpr const auto debug_exception
Definition: x64.h:96
constexpr const auto virtualization_exception
Definition: x64.h:114
constexpr const auto write_back
Definition: x64.h:46
constexpr const auto page_shift
Definition: x64.h:36
constexpr const auto read_execute
Definition: x64.h:68
constexpr const auto ring0_tr_descriptor
Definition: x64.h:88
constexpr const auto uncacheable_minus
Definition: x64.h:47
constexpr const auto page_fault
Definition: x64.h:109
constexpr const auto machine_check
Definition: x64.h:112
constexpr const auto ring0
Definition: x64.h:78
constexpr const auto segment_not_present
Definition: x64.h:106
constexpr const auto read_execute_conforming_accessed
Definition: x64.h:73
constexpr const auto floating_point_error
Definition: x64.h:110
constexpr const auto ring0_gs_descriptor
Definition: x64.h:87
constexpr const auto write_protected
Definition: x64.h:45
constexpr const auto page_size
Definition: x64.h:35
constexpr const auto uncacheable
Definition: x64.h:42
constexpr const auto execute_only
Definition: x64.h:66
auto get() noexcept
Definition: cpuid_x64.h:89
constexpr const auto ring1
Definition: x64.h:79
constexpr const auto cache_line_shift
Definition: x64.h:38
constexpr const auto invalid_tss
Definition: x64.h:105
constexpr const auto stack_segment_fault
Definition: x64.h:107
constexpr const auto execute_only_conforming_accessed
Definition: x64.h:71
constexpr const auto divide_error
Definition: x64.h:95
constexpr const auto bound_range_exceeded
Definition: x64.h:100
constexpr const auto ring0_ss_descriptor
Definition: x64.h:85
auto is_address_canonical(T addr)
Definition: x64.h:118
constexpr const auto tss_available
Definition: x64.h:55
constexpr const auto general_protection
Definition: x64.h:108
Definition: cache_x64.h:31
constexpr const auto write_through
Definition: x64.h:44
constexpr const auto ring3
Definition: x64.h:81
constexpr const auto ring0_fs_descriptor
Definition: x64.h:86
constexpr const auto tss_busy
Definition: x64.h:54
constexpr const auto read_execute_conforming
Definition: x64.h:72
constexpr const auto read_only
Definition: x64.h:57
constexpr const auto write_combining
Definition: x64.h:43
constexpr const auto read_only_expand_down
Definition: x64.h:61