rflags_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 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 #ifndef RFLAGS_X64_H
23 #define RFLAGS_X64_H
24 
25 #include <gsl/gsl>
26 
27 #include <debug.h>
28 #include <bitmanip.h>
29 
30 extern "C" uint64_t __read_rflags(void) noexcept;
31 extern "C" void __write_rflags(uint64_t val) noexcept;
32 
33 // *INDENT-OFF*
34 
35 namespace x64
36 {
37 namespace rflags
38 {
39  using value_type = uint64_t;
40 
41  inline auto get() noexcept
42  { return __read_rflags(); }
43 
44  namespace carry_flag
45  {
46  constexpr const auto mask = 0x0000000000000001UL;
47  constexpr const auto from = 0;
48  constexpr const auto name = "carry_flag";
49 
50  inline auto get() noexcept
51  { return get_bit(__read_rflags(), from) != 0; }
52  }
53 
54  namespace parity_flag
55  {
56  constexpr const auto mask = 0x0000000000000004UL;
57  constexpr const auto from = 2;
58  constexpr const auto name = "parity_flag";
59 
60  inline auto get() noexcept
61  { return get_bit(__read_rflags(), from) != 0; }
62  }
63 
64  namespace auxiliary_carry_flag
65  {
66  constexpr const auto mask = 0x0000000000000010UL;
67  constexpr const auto from = 4;
68  constexpr const auto name = "auxiliary_carry_flag";
69 
70  inline auto get() noexcept
71  { return get_bit(__read_rflags(), from) != 0; }
72  }
73 
74  namespace zero_flag
75  {
76  constexpr const auto mask = 0x0000000000000040UL;
77  constexpr const auto from = 6;
78  constexpr const auto name = "zero_flag";
79 
80  inline auto get() noexcept
81  { return get_bit(__read_rflags(), from) != 0; }
82  }
83 
84  namespace sign_flag
85  {
86  constexpr const auto mask = 0x0000000000000080UL;
87  constexpr const auto from = 7;
88  constexpr const auto name = "sign_flag";
89 
90  inline auto get() noexcept
91  { return get_bit(__read_rflags(), from) != 0; }
92  }
93 
94  namespace trap_flag
95  {
96  constexpr const auto mask = 0x0000000000000100UL;
97  constexpr const auto from = 8;
98  constexpr const auto name = "trap_flag";
99 
100  inline auto get() noexcept
101  { return get_bit(__read_rflags(), from) != 0; }
102  }
103 
104  namespace interrupt_enable_flag
105  {
106  constexpr const auto mask = 0x0000000000000200UL;
107  constexpr const auto from = 9;
108  constexpr const auto name = "interrupt_enable_flag";
109 
110  inline auto get() noexcept
111  { return get_bit(__read_rflags(), from) != 0; }
112  }
113 
114  namespace direction_flag
115  {
116  constexpr const auto mask = 0x0000000000000400UL;
117  constexpr const auto from = 10;
118  constexpr const auto name = "direction_flag";
119 
120  inline auto get() noexcept
121  { return get_bit(__read_rflags(), from) != 0; }
122  }
123 
124  namespace overflow_flag
125  {
126  constexpr const auto mask = 0x0000000000000800UL;
127  constexpr const auto from = 11;
128  constexpr const auto name = "overflow_flag";
129 
130  inline auto get() noexcept
131  { return get_bit(__read_rflags(), from) != 0; }
132  }
133 
134  namespace privilege_level
135  {
136  constexpr const auto mask = 0x0000000000003000UL;
137  constexpr const auto from = 12;
138  constexpr const auto name = "privilege_level";
139 
140  inline auto get() noexcept
141  { return get_bits(__read_rflags(), mask) >> from; }
142  }
143 
144  namespace nested_task
145  {
146  constexpr const auto mask = 0x0000000000004000UL;
147  constexpr const auto from = 14;
148  constexpr const auto name = "nested_task";
149 
150  inline auto get() noexcept
151  { return get_bit(__read_rflags(), from) != 0; }
152  }
153 
154  namespace resume_flag
155  {
156  constexpr const auto mask = 0x0000000000010000UL;
157  constexpr const auto from = 16;
158  constexpr const auto name = "resume_flag";
159 
160  inline auto get() noexcept
161  { return get_bit(__read_rflags(), from) != 0; }
162  }
163 
164  namespace virtual_8086_mode
165  {
166  constexpr const auto mask = 0x0000000000020000UL;
167  constexpr const auto from = 17;
168  constexpr const auto name = "virtual_8086_mode";
169 
170  inline auto get() noexcept
171  { return get_bit(__read_rflags(), from) != 0; }
172  }
173 
174  namespace alignment_check_access_control
175  {
176  constexpr const auto mask = 0x0000000000040000UL;
177  constexpr const auto from = 18;
178  constexpr const auto name = "alignment_check_access_control";
179 
180  inline auto get() noexcept
181  { return get_bit(__read_rflags(), from) != 0; }
182  }
183 
184  namespace virtual_interupt_flag
185  {
186  constexpr const auto mask = 0x0000000000080000UL;
187  constexpr const auto from = 19;
188  constexpr const auto name = "virtual_interupt_flag";
189 
190  inline auto get() noexcept
191  { return get_bit(__read_rflags(), from) != 0; }
192  }
193 
194  namespace virtual_interupt_pending
195  {
196  constexpr const auto mask = 0x0000000000100000UL;
197  constexpr const auto from = 20;
198  constexpr const auto name = "virtual_interupt_pending";
199 
200  inline auto get() noexcept
201  { return get_bit(__read_rflags(), from) != 0; }
202  }
203 
204  namespace id_flag
205  {
206  constexpr const auto mask = 0x0000000000200000UL;
207  constexpr const auto from = 21;
208  constexpr const auto name = "id_flag";
209 
210  inline auto get() noexcept
211  { return get_bit(__read_rflags(), from) != 0; }
212  }
213 
214  namespace reserved
215  {
216  constexpr const auto mask = 0xFFFFFFFFFFC08028UL;
217  constexpr const auto from = 0;
218  constexpr const auto name = "reserved";
219 
220  inline auto get() noexcept
221  { return get_bits(__read_rflags(), mask) >> from; }
222  }
223 
224  namespace always_disabled
225  {
226  constexpr const auto mask = 0xFFFFFFFFFFC08028UL;
227  constexpr const auto from = 0;
228  constexpr const auto name = "always_disabled";
229 
230  inline auto get() noexcept
231  { return get_bits(__read_rflags(), mask) >> from; }
232  }
233 
234  namespace always_enabled
235  {
236  constexpr const auto mask = 0x0000000000000002UL;
237  constexpr const auto from = 0;
238  constexpr const auto name = "always_enabled";
239 
240  inline auto get() noexcept
241  { return get_bits(__read_rflags(), mask) >> from; }
242  }
243 
244  inline void dump() noexcept
245  {
246  bfdebug << "rflags enabled flags:" << bfendl;
247 
248  if (carry_flag::get())
249  bfdebug << " - " << carry_flag::name << bfendl;
250  if (parity_flag::get())
251  bfdebug << " - " << parity_flag::name << bfendl;
253  bfdebug << " - " << auxiliary_carry_flag::name << bfendl;
254  if (zero_flag::get())
255  bfdebug << " - " << zero_flag::name << bfendl;
256  if (sign_flag::get())
257  bfdebug << " - " << sign_flag::name << bfendl;
258  if (trap_flag::get())
259  bfdebug << " - " << trap_flag::name << bfendl;
261  bfdebug << " - " << interrupt_enable_flag::name << bfendl;
262  if (direction_flag::get())
263  bfdebug << " - " << direction_flag::name << bfendl;
264  if (overflow_flag::get())
265  bfdebug << " - " << overflow_flag::name << bfendl;
266  if (nested_task::get())
267  bfdebug << " - " << nested_task::name << bfendl;
268  if (resume_flag::get())
269  bfdebug << " - " << resume_flag::name << bfendl;
271  bfdebug << " - " << virtual_8086_mode::name << bfendl;
273  bfdebug << " - " << alignment_check_access_control::name << bfendl;
275  bfdebug << " - " << virtual_interupt_flag::name << bfendl;
277  bfdebug << " - " << virtual_interupt_pending::name << bfendl;
278  if (id_flag::get())
279  bfdebug << " - " << id_flag::name << bfendl;
280 
281  bfdebug << bfendl;
282  bfdebug << "rflags fields:" << bfendl;
283 
284  bfdebug << " - " << privilege_level::name << " = "
285  << view_as_pointer(privilege_level::get()) << bfendl;
286  }
287 }
288 }
289 
290 // *INDENT-ON*
291 
292 #endif
constexpr const auto from
Definition: rflags_x64.h:237
constexpr const auto name
Definition: rflags_x64.h:148
constexpr const auto mask
Definition: rflags_x64.h:96
auto get() noexcept
Definition: rflags_x64.h:160
constexpr const auto mask
Definition: rflags_x64.h:116
constexpr const auto from
Definition: rflags_x64.h:207
auto get() noexcept
Definition: rflags_x64.h:80
constexpr const auto from
Definition: rflags_x64.h:77
constexpr const auto name
Definition: rflags_x64.h:118
constexpr const auto from
Definition: rflags_x64.h:167
auto get() noexcept
Definition: rflags_x64.h:90
auto get() noexcept
Definition: rflags_x64.h:100
constexpr const auto from
Definition: rflags_x64.h:87
constexpr const auto mask
Definition: rflags_x64.h:136
constexpr const auto name
Definition: rflags_x64.h:58
constexpr const auto name
Definition: rflags_x64.h:138
constexpr const auto mask
Definition: rflags_x64.h:236
void dump() noexcept
Definition: rflags_x64.h:244
constexpr const auto mask
Definition: rflags_x64.h:86
constexpr const auto name
Definition: rflags_x64.h:208
auto get() noexcept
Definition: rflags_x64.h:150
constexpr const auto from
Definition: rflags_x64.h:117
auto get() noexcept
Definition: rflags_x64.h:60
auto get_bit(T t, B b) noexcept
Definition: bitmanip.h:42
auto get() noexcept
Definition: rflags_x64.h:210
constexpr const auto from
Definition: rflags_x64.h:67
void uint64_t uint64_t uint64_t *rdx noexcept
constexpr const auto from
Definition: rflags_x64.h:137
constexpr const auto mask
Definition: rflags_x64.h:66
constexpr const auto name
Definition: rflags_x64.h:168
constexpr const auto mask
Definition: rflags_x64.h:156
constexpr const auto mask
Definition: rflags_x64.h:56
auto get() noexcept
Definition: rflags_x64.h:50
constexpr const auto mask
Definition: rflags_x64.h:126
constexpr const auto mask
Definition: rflags_x64.h:166
constexpr const auto from
Definition: rflags_x64.h:147
constexpr const auto name
Definition: rflags_x64.h:218
void __write_rflags(uint64_t val) noexcept
constexpr const auto mask
Definition: rflags_x64.h:76
constexpr const auto name
Definition: rflags_x64.h:128
constexpr const auto reserved
Definition: vcpuid.h:33
const void * view_as_pointer(const T val)
uint64_t value_type
Definition: rflags_x64.h:39
auto get_bits(T t, M m) noexcept
Definition: bitmanip.h:65
constexpr const auto from
Definition: rflags_x64.h:217
constexpr const auto from
Definition: rflags_x64.h:97
constexpr const auto name
Definition: rflags_x64.h:78
constexpr const auto name
Definition: rflags_x64.h:238
constexpr const auto name
Definition: rflags_x64.h:98
constexpr const auto mask
Definition: rflags_x64.h:46
constexpr const auto from
Definition: rflags_x64.h:157
constexpr const auto mask
Definition: rflags_x64.h:206
constexpr const auto mask
Definition: rflags_x64.h:216
constexpr const auto mask
Definition: rflags_x64.h:146
constexpr const auto name
Definition: rflags_x64.h:48
constexpr const auto from
Definition: rflags_x64.h:127
constexpr const auto name
Definition: rflags_x64.h:68
constexpr const auto from
Definition: rflags_x64.h:47
uint64_t __read_rflags(void) noexcept
Definition: cache_x64.h:31
constexpr const auto from
Definition: rflags_x64.h:57
constexpr const auto name
Definition: rflags_x64.h:158
constexpr const auto from
Definition: rflags_x64.h:227
constexpr const auto name
Definition: rflags_x64.h:228
constexpr const auto mask
Definition: rflags_x64.h:226
constexpr const auto name
Definition: rflags_x64.h:88