srs_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 SRS_X64_H
23 #define SRS_X64_H
24 
25 #include <gsl/gsl>
26 
27 #include <debug.h>
28 #include <bitmanip.h>
29 
30 extern "C" uint16_t __read_es(void) noexcept;
31 extern "C" void __write_es(uint16_t val) noexcept;
32 
33 extern "C" uint16_t __read_cs(void) noexcept;
34 extern "C" void __write_cs(uint16_t val) noexcept;
35 
36 extern "C" uint16_t __read_ss(void) noexcept;
37 extern "C" void __write_ss(uint16_t val) noexcept;
38 
39 extern "C" uint16_t __read_ds(void) noexcept;
40 extern "C" void __write_ds(uint16_t val) noexcept;
41 
42 extern "C" uint16_t __read_fs(void) noexcept;
43 extern "C" void __write_fs(uint16_t val) noexcept;
44 
45 extern "C" uint16_t __read_gs(void) noexcept;
46 extern "C" void __write_gs(uint16_t val) noexcept;
47 
48 extern "C" uint16_t __read_ldtr(void) noexcept;
49 extern "C" void __write_ldtr(uint16_t val) noexcept;
50 
51 extern "C" uint16_t __read_tr(void) noexcept;
52 extern "C" void __write_tr(uint16_t val) noexcept;
53 
54 // *INDENT-OFF*
55 
56 namespace x64
57 {
58 namespace segment_register
59 {
60 
61 using type = uint16_t;
62 
63 namespace es
64 {
65  inline auto get() noexcept
66  { return __read_es(); }
67 
68  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
69  void set(T val) noexcept { __write_es(gsl::narrow_cast<type>(val)); }
70 
71  namespace rpl
72  {
73  constexpr const auto mask = 0x00000003U;
74  constexpr const auto from = 0;
75  constexpr const auto name = "rpl";
76 
77  inline auto get() noexcept
78  { return gsl::narrow_cast<type>(get_bits(__read_es(), mask) >> from); }
79 
80  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
81  void set(T val) noexcept { __write_es(gsl::narrow_cast<type>(set_bits(__read_es(), mask, val << from))); }
82  }
83 
84  namespace ti
85  {
86  constexpr const auto mask = 0x00000004U;
87  constexpr const auto from = 2;
88  constexpr const auto name = "ti";
89 
90  inline auto get() noexcept
91  { return gsl::narrow_cast<type>(get_bit(__read_es(), from)); }
92 
93  inline void set(bool val) noexcept
94  { __write_es(gsl::narrow_cast<type>(val ? set_bit(__read_es(), from) : clear_bit(__read_es(), from))); }
95  }
96 
97  namespace index
98  {
99  constexpr const auto mask = 0x0000FFF8U;
100  constexpr const auto from = 3;
101  constexpr const auto name = "index";
102 
103  inline auto get() noexcept
104  { return gsl::narrow_cast<type>(get_bits(__read_es(), mask) >> from); }
105 
106  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
107  void set(T val) noexcept { __write_es(gsl::narrow_cast<type>(set_bits(__read_es(), mask, val << from))); }
108  }
109 }
110 
111 namespace cs
112 {
113  inline auto get() noexcept
114  { return __read_cs(); }
115 
116  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
117  void set(T val) noexcept { __write_cs(gsl::narrow_cast<type>(val)); }
118 
119  namespace rpl
120  {
121  constexpr const auto mask = 0x00000003U;
122  constexpr const auto from = 0;
123  constexpr const auto name = "rpl";
124 
125  inline auto get() noexcept
126  { return gsl::narrow_cast<type>(get_bits(__read_cs(), mask) >> from); }
127 
128  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
129  void set(T val) noexcept { __write_cs(gsl::narrow_cast<type>(set_bits(__read_cs(), mask, val << from))); }
130  }
131 
132  namespace ti
133  {
134  constexpr const auto mask = 0x00000004U;
135  constexpr const auto from = 2;
136  constexpr const auto name = "ti";
137 
138  inline auto get() noexcept
139  { return gsl::narrow_cast<type>(get_bit(__read_cs(), from)); }
140 
141  inline void set(bool val) noexcept
142  { __write_cs(gsl::narrow_cast<type>(val ? set_bit(__read_cs(), from) : clear_bit(__read_cs(), from))); }
143  }
144 
145  namespace index
146  {
147  constexpr const auto mask = 0x0000FFF8U;
148  constexpr const auto from = 3;
149  constexpr const auto name = "index";
150 
151  inline auto get() noexcept
152  { return gsl::narrow_cast<type>(get_bits(__read_cs(), mask) >> from); }
153 
154  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
155  void set(T val) noexcept { __write_cs(gsl::narrow_cast<type>(set_bits(__read_cs(), mask, val << from))); }
156  }
157 }
158 
159 namespace ss
160 {
161  inline auto get() noexcept
162  { return __read_ss(); }
163 
164  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
165  void set(T val) noexcept { __write_ss(gsl::narrow_cast<type>(val)); }
166 
167  namespace rpl
168  {
169  constexpr const auto mask = 0x00000003U;
170  constexpr const auto from = 0;
171  constexpr const auto name = "rpl";
172 
173  inline auto get() noexcept
174  { return gsl::narrow_cast<type>(get_bits(__read_ss(), mask) >> from); }
175 
176  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
177  void set(T val) noexcept { __write_ss(gsl::narrow_cast<type>(set_bits(__read_ss(), mask, val << from))); }
178  }
179 
180  namespace ti
181  {
182  constexpr const auto mask = 0x00000004U;
183  constexpr const auto from = 2;
184  constexpr const auto name = "ti";
185 
186  inline auto get() noexcept
187  { return gsl::narrow_cast<type>(get_bit(__read_ss(), from)); }
188 
189  inline void set(bool val) noexcept
190  { __write_ss(gsl::narrow_cast<type>(val ? set_bit(__read_ss(), from) : clear_bit(__read_ss(), from))); }
191  }
192 
193  namespace index
194  {
195  constexpr const auto mask = 0x0000FFF8U;
196  constexpr const auto from = 3;
197  constexpr const auto name = "index";
198 
199  inline auto get() noexcept
200  { return gsl::narrow_cast<type>(get_bits(__read_ss(), mask) >> from); }
201 
202  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
203  void set(T val) noexcept { __write_ss(gsl::narrow_cast<type>(set_bits(__read_ss(), mask, val << from))); }
204  }
205 }
206 
207 namespace ds
208 {
209  inline auto get() noexcept
210  { return __read_ds(); }
211 
212  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
213  void set(T val) noexcept { __write_ds(gsl::narrow_cast<type>(val)); }
214 
215  namespace rpl
216  {
217  constexpr const auto mask = 0x00000003U;
218  constexpr const auto from = 0;
219  constexpr const auto name = "rpl";
220 
221  inline auto get() noexcept
222  { return gsl::narrow_cast<type>(get_bits(__read_ds(), mask) >> from); }
223 
224  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
225  void set(T val) noexcept { __write_ds(gsl::narrow_cast<type>(set_bits(__read_ds(), mask, val << from))); }
226  }
227 
228  namespace ti
229  {
230  constexpr const auto mask = 0x00000004U;
231  constexpr const auto from = 2;
232  constexpr const auto name = "ti";
233 
234  inline auto get() noexcept
235  { return gsl::narrow_cast<type>(get_bit(__read_ds(), from)); }
236 
237  inline void set(bool val) noexcept
238  { __write_ds(gsl::narrow_cast<type>(val ? set_bit(__read_ds(), from) : clear_bit(__read_ds(), from))); }
239  }
240 
241  namespace index
242  {
243  constexpr const auto mask = 0x0000FFF8U;
244  constexpr const auto from = 3;
245  constexpr const auto name = "index";
246 
247  inline auto get() noexcept
248  { return gsl::narrow_cast<type>(get_bits(__read_ds(), mask) >> from); }
249 
250  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
251  void set(T val) noexcept { __write_ds(gsl::narrow_cast<type>(set_bits(__read_ds(), mask, val << from))); }
252  }
253 }
254 
255 namespace fs
256 {
257  inline auto get() noexcept
258  { return __read_fs(); }
259 
260  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
261  void set(T val) noexcept { __write_fs(gsl::narrow_cast<type>(val)); }
262 
263  namespace rpl
264  {
265  constexpr const auto mask = 0x00000003U;
266  constexpr const auto from = 0;
267  constexpr const auto name = "rpl";
268 
269  inline auto get() noexcept
270  { return gsl::narrow_cast<type>(get_bits(__read_fs(), mask) >> from); }
271 
272  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
273  void set(T val) noexcept { __write_fs(gsl::narrow_cast<type>(set_bits(__read_fs(), mask, val << from))); }
274  }
275 
276  namespace ti
277  {
278  constexpr const auto mask = 0x00000004U;
279  constexpr const auto from = 2;
280  constexpr const auto name = "ti";
281 
282  inline auto get() noexcept
283  { return gsl::narrow_cast<type>(get_bit(__read_fs(), from)); }
284 
285  inline void set(bool val) noexcept
286  { __write_fs(gsl::narrow_cast<type>(val ? set_bit(__read_fs(), from) : clear_bit(__read_fs(), from))); }
287  }
288 
289  namespace index
290  {
291  constexpr const auto mask = 0x0000FFF8U;
292  constexpr const auto from = 3;
293  constexpr const auto name = "index";
294 
295  inline auto get() noexcept
296  { return gsl::narrow_cast<type>(get_bits(__read_fs(), mask) >> from); }
297 
298  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
299  void set(T val) noexcept { __write_fs(gsl::narrow_cast<type>(set_bits(__read_fs(), mask, val << from))); }
300  }
301 }
302 
303 namespace gs
304 {
305  inline auto get() noexcept
306  { return __read_gs(); }
307 
308  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
309  void set(T val) noexcept { __write_gs(gsl::narrow_cast<type>(val)); }
310 
311  namespace rpl
312  {
313  constexpr const auto mask = 0x00000003U;
314  constexpr const auto from = 0;
315  constexpr const auto name = "rpl";
316 
317  inline auto get() noexcept
318  { return gsl::narrow_cast<type>(get_bits(__read_gs(), mask) >> from); }
319 
320  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
321  void set(T val) noexcept { __write_gs(gsl::narrow_cast<type>(set_bits(__read_gs(), mask, val << from))); }
322  }
323 
324  namespace ti
325  {
326  constexpr const auto mask = 0x00000004U;
327  constexpr const auto from = 2;
328  constexpr const auto name = "ti";
329 
330  inline auto get() noexcept
331  { return gsl::narrow_cast<type>(get_bit(__read_gs(), from)); }
332 
333  inline void set(bool val) noexcept
334  { __write_gs(gsl::narrow_cast<type>(val ? set_bit(__read_gs(), from) : clear_bit(__read_gs(), from))); }
335  }
336 
337  namespace index
338  {
339  constexpr const auto mask = 0x0000FFF8U;
340  constexpr const auto from = 3;
341  constexpr const auto name = "index";
342 
343  inline auto get() noexcept
344  { return gsl::narrow_cast<type>(get_bits(__read_gs(), mask) >> from); }
345 
346  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
347  void set(T val) noexcept { __write_gs(gsl::narrow_cast<type>(set_bits(__read_gs(), mask, val << from))); }
348  }
349 }
350 
351 namespace ldtr
352 {
353  inline auto get() noexcept
354  { return __read_ldtr(); }
355 
356  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
357  void set(T val) noexcept { __write_ldtr(gsl::narrow_cast<type>(val)); }
358 
359  namespace rpl
360  {
361  constexpr const auto mask = 0x00000003U;
362  constexpr const auto from = 0;
363  constexpr const auto name = "rpl";
364 
365  inline auto get() noexcept
366  { return gsl::narrow_cast<type>(get_bits(__read_ldtr(), mask) >> from); }
367 
368  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
369  void set(T val) noexcept { __write_ldtr(gsl::narrow_cast<type>(set_bits(__read_ldtr(), mask, val << from))); }
370  }
371 
372  namespace ti
373  {
374  constexpr const auto mask = 0x00000004U;
375  constexpr const auto from = 2;
376  constexpr const auto name = "ti";
377 
378  inline auto get() noexcept
379  { return gsl::narrow_cast<type>(get_bit(__read_ldtr(), from)); }
380 
381  inline void set(bool val) noexcept
382  { __write_ldtr(gsl::narrow_cast<type>(val ? set_bit(__read_ldtr(), from) : clear_bit(__read_ldtr(), from))); }
383  }
384 
385  namespace index
386  {
387  constexpr const auto mask = 0x0000FFF8U;
388  constexpr const auto from = 3;
389  constexpr const auto name = "index";
390 
391  inline auto get() noexcept
392  { return gsl::narrow_cast<type>(get_bits(__read_ldtr(), mask) >> from); }
393 
394  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
395  void set(T val) noexcept { __write_ldtr(gsl::narrow_cast<type>(set_bits(__read_ldtr(), mask, val << from))); }
396  }
397 }
398 
399 namespace tr
400 {
401  inline auto get() noexcept
402  { return __read_tr(); }
403 
404  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
405  void set(T val) noexcept { __write_tr(gsl::narrow_cast<type>(val)); }
406 
407  namespace rpl
408  {
409  constexpr const auto mask = 0x00000003U;
410  constexpr const auto from = 0;
411  constexpr const auto name = "rpl";
412 
413  inline auto get() noexcept
414  { return gsl::narrow_cast<type>(get_bits(__read_tr(), mask) >> from); }
415 
416  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
417  void set(T val) noexcept { __write_tr(gsl::narrow_cast<type>(set_bits(__read_tr(), mask, val << from))); }
418  }
419 
420  namespace ti
421  {
422  constexpr const auto mask = 0x00000004U;
423  constexpr const auto from = 2;
424  constexpr const auto name = "ti";
425 
426  inline auto get() noexcept
427  { return gsl::narrow_cast<type>(get_bit(__read_tr(), from)); }
428 
429  inline void set(bool val) noexcept
430  { __write_tr(gsl::narrow_cast<type>(val ? set_bit(__read_tr(), from) : clear_bit(__read_tr(), from))); }
431  }
432 
433  namespace index
434  {
435  constexpr const auto mask = 0x0000FFF8U;
436  constexpr const auto from = 3;
437  constexpr const auto name = "index";
438 
439  inline auto get() noexcept
440  { return gsl::narrow_cast<type>(get_bits(__read_tr(), mask) >> from); }
441 
442  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
443  void set(T val) noexcept { __write_tr(gsl::narrow_cast<type>(set_bits(__read_tr(), mask, val << from))); }
444  }
445 }
446 }
447 }
448 
449 // *INDENT-ON*
450 
451 #endif
constexpr const auto name
Definition: srs_x64.h:245
constexpr const auto from
Definition: srs_x64.h:74
constexpr const auto mask
Definition: srs_x64.h:134
uint16_t __read_ds(void) noexcept
constexpr const auto name
Definition: srs_x64.h:88
constexpr const auto from
Definition: srs_x64.h:183
void __write_ldtr(uint16_t val) noexcept
constexpr const auto mask
Definition: srs_x64.h:265
constexpr const auto name
Definition: srs_x64.h:315
constexpr const auto from
Definition: srs_x64.h:340
void __write_tr(uint16_t val) noexcept
constexpr const auto name
Definition: srs_x64.h:197
constexpr const auto from
Definition: srs_x64.h:410
constexpr const auto mask
Definition: srs_x64.h:422
uint16_t __read_ss(void) noexcept
constexpr const auto from
Definition: srs_x64.h:170
void __write_gs(uint16_t val) noexcept
void __write_fs(uint16_t val) noexcept
constexpr const auto mask
Definition: srs_x64.h:374
constexpr const auto mask
Definition: srs_x64.h:230
constexpr const auto mask
Definition: srs_x64.h:435
constexpr const auto mask
Definition: srs_x64.h:169
constexpr const auto name
Definition: srs_x64.h:75
constexpr const auto from
Definition: srs_x64.h:292
constexpr const auto from
Definition: srs_x64.h:266
constexpr const auto mask
Definition: srs_x64.h:73
constexpr const auto from
Definition: srs_x64.h:423
constexpr const auto name
Definition: srs_x64.h:376
constexpr const auto from
Definition: srs_x64.h:327
constexpr const auto name
Definition: srs_x64.h:363
constexpr const auto from
Definition: srs_x64.h:122
void __write_cs(uint16_t val) noexcept
constexpr const auto mask
Definition: srs_x64.h:361
constexpr const auto name
Definition: srs_x64.h:411
void __write_ds(uint16_t val) noexcept
constexpr const auto mask
Definition: srs_x64.h:121
constexpr const auto name
Definition: srs_x64.h:437
constexpr const auto name
Definition: srs_x64.h:136
constexpr const auto mask
Definition: srs_x64.h:147
constexpr const auto name
Definition: srs_x64.h:293
uint16_t __read_cs(void) noexcept
auto get_bit(T t, B b) noexcept
Definition: bitmanip.h:42
void uint64_t uint64_t uint64_t *rdx noexcept
constexpr const auto from
Definition: srs_x64.h:436
constexpr const auto from
Definition: srs_x64.h:87
auto index(const T virt, const F from)
constexpr const auto name
Definition: srs_x64.h:171
constexpr const auto mask
Definition: srs_x64.h:243
uint16_t __read_es(void) noexcept
constexpr const auto mask
Definition: srs_x64.h:409
constexpr const auto name
Definition: srs_x64.h:184
constexpr const auto from
Definition: srs_x64.h:100
uint16_t __read_tr(void) noexcept
constexpr const auto mask
Definition: srs_x64.h:339
constexpr const auto name
Definition: srs_x64.h:389
constexpr const auto name
Definition: srs_x64.h:123
void __write_ss(uint16_t val) noexcept
constexpr const auto mask
Definition: srs_x64.h:195
auto clear_bit(T t, B b) noexcept
Definition: bitmanip.h:36
void __write_es(uint16_t val) noexcept
constexpr const auto from
Definition: srs_x64.h:375
auto set_bit(T t, B b) noexcept
Definition: bitmanip.h:30
auto get_bits(T t, M m) noexcept
Definition: bitmanip.h:65
constexpr const auto mask
Definition: srs_x64.h:182
uint16_t __read_gs(void) noexcept
constexpr const auto mask
Definition: srs_x64.h:291
constexpr const auto name
Definition: srs_x64.h:267
constexpr const auto from
Definition: srs_x64.h:218
constexpr const auto mask
Definition: srs_x64.h:326
constexpr const auto name
Definition: srs_x64.h:280
constexpr const auto name
Definition: srs_x64.h:328
constexpr const auto mask
Definition: srs_x64.h:278
constexpr const auto mask
Definition: srs_x64.h:387
constexpr const auto name
Definition: srs_x64.h:424
constexpr const auto mask
Definition: srs_x64.h:313
constexpr const auto mask
Definition: srs_x64.h:99
constexpr const auto mask
Definition: srs_x64.h:217
constexpr const auto name
Definition: srs_x64.h:232
constexpr const auto from
Definition: srs_x64.h:135
constexpr const auto name
Definition: srs_x64.h:149
uint16_t __read_ldtr(void) noexcept
constexpr const auto from
Definition: srs_x64.h:388
constexpr const auto name
Definition: srs_x64.h:219
constexpr const auto from
Definition: srs_x64.h:231
uint16_t __read_fs(void) noexcept
constexpr const auto from
Definition: srs_x64.h:196
Definition: cache_x64.h:31
constexpr const auto mask
Definition: srs_x64.h:86
constexpr const auto from
Definition: srs_x64.h:148
constexpr const auto from
Definition: srs_x64.h:314
auto set_bits(T t, M m, V v) noexcept
Definition: bitmanip.h:72
constexpr const auto from
Definition: srs_x64.h:279
constexpr const auto name
Definition: srs_x64.h:101
constexpr const auto from
Definition: srs_x64.h:244
constexpr const auto name
Definition: srs_x64.h:341
constexpr const auto from
Definition: srs_x64.h:362