msrs_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 MSRS_X64_H
23 #define MSRS_X64_H
24 
25 #include <gsl/gsl>
26 
27 #include <debug.h>
28 #include <bitmanip.h>
29 
30 extern "C" uint64_t __read_msr(uint32_t addr) noexcept;
31 extern "C" void __write_msr(uint32_t addr, uint64_t val) noexcept;
32 
33 // *INDENT-OFF*
34 
35 namespace x64
36 {
37 namespace msrs
38 {
39  using field_type = uint32_t;
40  using value_type = uint64_t;
41 
42  template<class A> inline auto get(A addr) noexcept
43  { return __read_msr(gsl::narrow_cast<field_type>(addr)); }
44 
45  template<class A, class T> void set(A addr, T val) noexcept
46  { __write_msr(gsl::narrow_cast<field_type>(addr), val); }
47 
48  namespace ia32_pat
49  {
50  constexpr const auto addr = 0x00000277U;
51  constexpr const auto name = "ia32_pat";
52 
53  inline auto get() noexcept
54  { return __read_msr(addr); }
55 
56  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
57  void set(T val) noexcept { __write_msr(addr, val); }
58 
59  namespace pa0
60  {
61  constexpr const auto mask = 0x0000000000000007UL;
62  constexpr const auto from = 0;
63  constexpr const auto name = "pa0";
64 
65  inline auto get() noexcept
66  { return get_bits(__read_msr(addr), mask) >> from; }
67 
68  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
69  auto get(T val) noexcept
70  { return get_bits(val, mask) >> from; }
71 
72  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
73  void set(T val) noexcept { __write_msr(addr, set_bits(__read_msr(addr), mask, val << from)); }
74  }
75 
76  namespace pa1
77  {
78  constexpr const auto mask = 0x0000000000000700UL;
79  constexpr const auto from = 8;
80  constexpr const auto name = "pa1";
81 
82  inline auto get() noexcept
83  { return get_bits(__read_msr(addr), mask) >> from; }
84 
85  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
86  auto get(T val) noexcept
87  { return get_bits(val, mask) >> from; }
88 
89  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
90  void set(T val) noexcept { __write_msr(addr, set_bits(__read_msr(addr), mask, val << from)); }
91  }
92 
93  namespace pa2
94  {
95  constexpr const auto mask = 0x0000000000070000UL;
96  constexpr const auto from = 16;
97  constexpr const auto name = "pa2";
98 
99  inline auto get() noexcept
100  { return get_bits(__read_msr(addr), mask) >> from; }
101 
102  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
103  auto get(T val) noexcept
104  { return get_bits(val, 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_msr(addr, set_bits(__read_msr(addr), mask, val << from)); }
108  }
109 
110  namespace pa3
111  {
112  constexpr const auto mask = 0x0000000007000000UL;
113  constexpr const auto from = 24;
114  constexpr const auto name = "pa3";
115 
116  inline auto get() noexcept
117  { return get_bits(__read_msr(addr), mask) >> from; }
118 
119  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
120  auto get(T val) noexcept
121  { return get_bits(val, mask) >> from; }
122 
123  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
124  void set(T val) noexcept { __write_msr(addr, set_bits(__read_msr(addr), mask, val << from)); }
125  }
126 
127  namespace pa4
128  {
129  constexpr const auto mask = 0x0000000700000000UL;
130  constexpr const auto from = 32;
131  constexpr const auto name = "pa4";
132 
133  inline auto get() noexcept
134  { return get_bits(__read_msr(addr), mask) >> from; }
135 
136  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
137  auto get(T val) noexcept
138  { return get_bits(val, mask) >> from; }
139 
140  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
141  void set(T val) noexcept { __write_msr(addr, set_bits(__read_msr(addr), mask, val << from)); }
142  }
143 
144  namespace pa5
145  {
146  constexpr const auto mask = 0x0000070000000000UL;
147  constexpr const auto from = 40;
148  constexpr const auto name = "pa5";
149 
150  inline auto get() noexcept
151  { return get_bits(__read_msr(addr), mask) >> from; }
152 
153  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
154  auto get(T val) noexcept
155  { return get_bits(val, mask) >> from; }
156 
157  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
158  void set(T val) noexcept { __write_msr(addr, set_bits(__read_msr(addr), mask, val << from)); }
159  }
160 
161  namespace pa6
162  {
163  constexpr const auto mask = 0x0007000000000000UL;
164  constexpr const auto from = 48;
165  constexpr const auto name = "pa6";
166 
167  inline auto get() noexcept
168  { return get_bits(__read_msr(addr), mask) >> from; }
169 
170  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
171  auto get(T val) noexcept
172  { return get_bits(val, mask) >> from; }
173 
174  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
175  void set(T val) noexcept { __write_msr(addr, set_bits(__read_msr(addr), mask, val << from)); }
176  }
177 
178  namespace pa7
179  {
180  constexpr const auto mask = 0x0700000000000000UL;
181  constexpr const auto from = 56;
182  constexpr const auto name = "pa7";
183 
184  inline auto get() noexcept
185  { return get_bits(__read_msr(addr), mask) >> from; }
186 
187  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
188  auto get(T val) noexcept
189  { return get_bits(val, mask) >> from; }
190 
191  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
192  void set(T val) noexcept { __write_msr(addr, set_bits(__read_msr(addr), mask, val << from)); }
193  }
194 
195  inline void dump() noexcept
196  {
197  bfdebug << "msrs::ia32_pat fields:" << bfendl;
198 
199  bfdebug << " - " << pa0::name << " = "
200  << view_as_pointer(pa0::get()) << bfendl;
201  bfdebug << " - " << pa1::name << " = "
202  << view_as_pointer(pa1::get()) << bfendl;
203  bfdebug << " - " << pa2::name << " = "
204  << view_as_pointer(pa2::get()) << bfendl;
205  bfdebug << " - " << pa3::name << " = "
206  << view_as_pointer(pa3::get()) << bfendl;
207  bfdebug << " - " << pa4::name << " = "
208  << view_as_pointer(pa4::get()) << bfendl;
209  bfdebug << " - " << pa5::name << " = "
210  << view_as_pointer(pa5::get()) << bfendl;
211  bfdebug << " - " << pa6::name << " = "
212  << view_as_pointer(pa6::get()) << bfendl;
213  bfdebug << " - " << pa7::name << " = "
214  << view_as_pointer(pa7::get()) << bfendl;
215  }
216 
217  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
218  auto pa(T index)
219  {
220  switch (index)
221  {
222  case 0: return pa0::get();
223  case 1: return pa1::get();
224  case 2: return pa2::get();
225  case 3: return pa3::get();
226  case 4: return pa4::get();
227  case 5: return pa5::get();
228  case 6: return pa6::get();
229  case 7: return pa7::get();
230  default:
231  throw std::runtime_error("unknown pat index");
232  };
233  }
234 
235  template<class V, class I,
236  class = typename std::enable_if<std::is_integral<V>::value>::type,
237  class = typename std::enable_if<std::is_integral<I>::value>::type>
238  auto pa(V value, I index)
239  {
240  switch (index)
241  {
242  case 0: return pa0::get(value);
243  case 1: return pa1::get(value);
244  case 2: return pa2::get(value);
245  case 3: return pa3::get(value);
246  case 4: return pa4::get(value);
247  case 5: return pa5::get(value);
248  case 6: return pa6::get(value);
249  case 7: return pa7::get(value);
250  default:
251  throw std::runtime_error("unknown pat index");
252  };
253  }
254  }
255 
256 }
257 }
258 
259 // *INDENT-ON*
260 
261 #endif
constexpr const auto from
Definition: msrs_x64.h:164
auto get() noexcept
Definition: msrs_x64.h:133
constexpr const auto name
Definition: msrs_x64.h:97
constexpr const auto mask
Definition: msrs_x64.h:129
uint32_t field_type
Definition: msrs_x64.h:39
constexpr const auto mask
Definition: msrs_x64.h:112
constexpr const auto mask
Definition: msrs_x64.h:163
auto get() noexcept
Definition: msrs_x64.h:65
constexpr const auto name
Definition: msrs_x64.h:51
constexpr const auto from
Definition: msrs_x64.h:113
constexpr const auto name
Definition: msrs_x64.h:182
constexpr const auto mask
Definition: msrs_x64.h:95
void uint64_t uint64_t uint64_t *rdx noexcept
constexpr const auto name
Definition: msrs_x64.h:131
auto index(const T virt, const F from)
constexpr const auto from
Definition: msrs_x64.h:79
constexpr const auto mask
Definition: msrs_x64.h:180
uint64_t value_type
Definition: msrs_x64.h:40
auto get() noexcept
Definition: msrs_x64.h:82
constexpr const auto mask
Definition: msrs_x64.h:61
auto get() noexcept
Definition: msrs_x64.h:167
constexpr const auto from
Definition: msrs_x64.h:96
auto get() noexcept
Definition: msrs_x64.h:99
constexpr const auto name
Definition: msrs_x64.h:165
constexpr const auto name
Definition: msrs_x64.h:63
const void * view_as_pointer(const T val)
auto get() noexcept
Definition: msrs_x64.h:184
auto get() noexcept
Definition: msrs_x64.h:116
auto pa(T index)
Definition: msrs_x64.h:218
constexpr const auto addr
Definition: msrs_x64.h:50
auto get_bits(T t, M m) noexcept
Definition: bitmanip.h:65
constexpr const auto from
Definition: msrs_x64.h:130
void dump() noexcept
Definition: msrs_x64.h:195
constexpr const auto name
Definition: msrs_x64.h:148
uint64_t __read_msr(uint32_t addr) noexcept
auto get() noexcept
Definition: msrs_x64.h:150
constexpr const auto from
Definition: msrs_x64.h:62
constexpr const auto from
Definition: msrs_x64.h:147
constexpr const auto mask
Definition: msrs_x64.h:78
constexpr const auto name
Definition: msrs_x64.h:80
Definition: cache_x64.h:31
void __write_msr(uint32_t addr, uint64_t val) noexcept
auto set_bits(T t, M m, V v) noexcept
Definition: bitmanip.h:72
constexpr const auto mask
Definition: msrs_x64.h:146
constexpr const auto from
Definition: msrs_x64.h:181
constexpr const auto name
Definition: msrs_x64.h:114