test_page_table_entry_x64.cpp
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 #include <test.h>
23 
24 #include <bitmanip.h>
26 
28 
29 void
30 memory_manager_ut::test_page_table_entry_x64_present()
31 {
32  pte_type entry = 0;
33  auto &&pte = std::make_unique<page_table_entry_x64>(&entry);
34 
35  pte->set_present(true);
36  this->expect_true(pte->present());
37  this->expect_true(num_bits_set(entry) == 1);
38  this->expect_true(is_bit_set(entry, 0));
39 
40  pte->set_present(false);
41  this->expect_false(pte->present());
42  this->expect_true(num_bits_set(entry) == 0);
43 }
44 
45 void
46 memory_manager_ut::test_page_table_entry_x64_rw()
47 {
48  pte_type entry = 0;
49  auto &&pte = std::make_unique<page_table_entry_x64>(&entry);
50 
51  pte->set_rw(true);
52  this->expect_true(pte->rw());
53  this->expect_true(num_bits_set(entry) == 1);
54  this->expect_true(is_bit_set(entry, 1));
55 
56  pte->set_rw(false);
57  this->expect_false(pte->rw());
58  this->expect_true(num_bits_set(entry) == 0);
59 }
60 
61 void
62 memory_manager_ut::test_page_table_entry_x64_us()
63 {
64  pte_type entry = 0;
65  auto &&pte = std::make_unique<page_table_entry_x64>(&entry);
66 
67  pte->set_us(true);
68  this->expect_true(pte->us());
69  this->expect_true(num_bits_set(entry) == 1);
70  this->expect_true(is_bit_set(entry, 2));
71 
72  pte->set_us(false);
73  this->expect_false(pte->us());
74  this->expect_true(num_bits_set(entry) == 0);
75 }
76 
77 void
78 memory_manager_ut::test_page_table_entry_x64_pwt()
79 {
80  pte_type entry = 0;
81  auto &&pte = std::make_unique<page_table_entry_x64>(&entry);
82 
83  pte->set_pwt(true);
84  this->expect_true(pte->pwt());
85  this->expect_true(num_bits_set(entry) == 1);
86  this->expect_true(is_bit_set(entry, 3));
87 
88  pte->set_pwt(false);
89  this->expect_false(pte->pwt());
90  this->expect_true(num_bits_set(entry) == 0);
91 }
92 
93 void
94 memory_manager_ut::test_page_table_entry_x64_pcd()
95 {
96  pte_type entry = 0;
97  auto &&pte = std::make_unique<page_table_entry_x64>(&entry);
98 
99  pte->set_pcd(true);
100  this->expect_true(pte->pcd());
101  this->expect_true(num_bits_set(entry) == 1);
102  this->expect_true(is_bit_set(entry, 4));
103 
104  pte->set_pcd(false);
105  this->expect_false(pte->pcd());
106  this->expect_true(num_bits_set(entry) == 0);
107 }
108 
109 void
110 memory_manager_ut::test_page_table_entry_x64_accessed()
111 {
112  pte_type entry = 0;
113  auto &&pte = std::make_unique<page_table_entry_x64>(&entry);
114 
115  pte->set_accessed(true);
116  this->expect_true(pte->accessed());
117  this->expect_true(num_bits_set(entry) == 1);
118  this->expect_true(is_bit_set(entry, 5));
119 
120  pte->set_accessed(false);
121  this->expect_false(pte->accessed());
122  this->expect_true(num_bits_set(entry) == 0);
123 }
124 
125 void
126 memory_manager_ut::test_page_table_entry_x64_dirty()
127 {
128  pte_type entry = 0;
129  auto &&pte = std::make_unique<page_table_entry_x64>(&entry);
130 
131  pte->set_dirty(true);
132  this->expect_true(pte->dirty());
133  this->expect_true(num_bits_set(entry) == 1);
134  this->expect_true(is_bit_set(entry, 6));
135 
136  pte->set_dirty(false);
137  this->expect_false(pte->dirty());
138  this->expect_true(num_bits_set(entry) == 0);
139 }
140 
141 void
142 memory_manager_ut::test_page_table_entry_x64_pat()
143 {
144  pte_type entry = 0;
145  auto &&pte = std::make_unique<page_table_entry_x64>(&entry);
146 
147  pte->set_pat_4k(true);
148  this->expect_true(pte->pat_4k());
149  this->expect_true(num_bits_set(entry) == 1);
150  this->expect_true(is_bit_set(entry, 7));
151 
152  pte->set_pat_4k(false);
153  this->expect_false(pte->pat_4k());
154  this->expect_true(num_bits_set(entry) == 0);
155 
156  pte->set_pat_large(true);
157  this->expect_true(pte->pat_large());
158  this->expect_true(num_bits_set(entry) == 1);
159  this->expect_true(is_bit_set(entry, 12));
160 
161  pte->set_pat_large(false);
162  this->expect_false(pte->pat_large());
163  this->expect_true(num_bits_set(entry) == 0);
164 }
165 
166 void
167 memory_manager_ut::test_page_table_entry_x64_ps()
168 {
169  pte_type entry = 0;
170  auto &&pte = std::make_unique<page_table_entry_x64>(&entry);
171 
172  pte->set_ps(true);
173  this->expect_true(pte->ps());
174  this->expect_true(num_bits_set(entry) == 1);
175  this->expect_true(is_bit_set(entry, 7));
176 
177  pte->set_ps(false);
178  this->expect_false(pte->ps());
179  this->expect_true(num_bits_set(entry) == 0);
180 }
181 
182 void
183 memory_manager_ut::test_page_table_entry_x64_global()
184 {
185  pte_type entry = 0;
186  auto &&pte = std::make_unique<page_table_entry_x64>(&entry);
187 
188  pte->set_global(true);
189  this->expect_true(pte->global());
190  this->expect_true(num_bits_set(entry) == 1);
191  this->expect_true(is_bit_set(entry, 8));
192 
193  pte->set_global(false);
194  this->expect_false(pte->global());
195  this->expect_true(num_bits_set(entry) == 0);
196 }
197 
198 void
199 memory_manager_ut::test_page_table_entry_x64_nx()
200 {
201  pte_type entry = 0;
202  auto &&pte = std::make_unique<page_table_entry_x64>(&entry);
203 
204  pte->set_nx(true);
205  this->expect_true(pte->nx());
206  this->expect_true(num_bits_set(entry) == 1);
207  this->expect_true(is_bit_set(entry, 63));
208 
209  pte->set_nx(false);
210  this->expect_false(pte->nx());
211  this->expect_true(num_bits_set(entry) == 0);
212 }
213 
214 void
215 memory_manager_ut::test_page_table_entry_x64_phys_addr()
216 {
217  pte_type entry = 0;
218  auto &&pte = std::make_unique<page_table_entry_x64>(&entry);
219 
220  pte->set_present(true);
221  pte->set_nx(true);
222  pte->set_phys_addr(0x0000ABCDEF123000);
223  this->expect_true(pte->present());
224  this->expect_true(pte->nx());
225  this->expect_true(pte->phys_addr() == 0x0000ABCDEF123000);
226 
227  pte->set_phys_addr(0x0000ABCDEF123010);
228  this->expect_true(pte->phys_addr() == 0x0000ABCDEF123000);
229  this->expect_false(pte->pcd());
230 
231  pte->set_present(true);
232  pte->set_nx(true);
233  pte->set_phys_addr(0x0);
234  this->expect_true(pte->present());
235  this->expect_true(pte->nx());
236  this->expect_true(pte->phys_addr() == 0x0);
237 }
238 
239 void
240 memory_manager_ut::test_page_table_entry_x64_pat_index()
241 {
242  pte_type entry = 0;
243  auto &&pte = std::make_unique<page_table_entry_x64>(&entry);
244 
245  pte->set_pwt(false);
246  pte->set_pcd(false);
247  pte->set_pat_4k(false);
248  this->expect_true(pte->pat_index_4k() == 0);
249 
250  pte->set_pwt(true);
251  pte->set_pcd(false);
252  pte->set_pat_4k(false);
253  this->expect_true(pte->pat_index_4k() == 1);
254 
255  pte->set_pwt(false);
256  pte->set_pcd(true);
257  pte->set_pat_4k(false);
258  this->expect_true(pte->pat_index_4k() == 2);
259 
260  pte->set_pwt(true);
261  pte->set_pcd(true);
262  pte->set_pat_4k(false);
263  this->expect_true(pte->pat_index_4k() == 3);
264 
265  pte->set_pwt(false);
266  pte->set_pcd(false);
267  pte->set_pat_4k(true);
268  this->expect_true(pte->pat_index_4k() == 4);
269 
270  pte->set_pwt(true);
271  pte->set_pcd(false);
272  pte->set_pat_4k(true);
273  this->expect_true(pte->pat_index_4k() == 5);
274 
275  pte->set_pwt(false);
276  pte->set_pcd(true);
277  pte->set_pat_4k(true);
278  this->expect_true(pte->pat_index_4k() == 6);
279 
280  pte->set_pwt(true);
281  pte->set_pcd(true);
282  pte->set_pat_4k(true);
283  this->expect_true(pte->pat_index_4k() == 7);
284 
285  pte->set_pat_index_4k(0);
286  this->expect_false(pte->pwt());
287  this->expect_false(pte->pcd());
288  this->expect_false(pte->pat_4k());
289 
290  pte->set_pat_index_4k(1);
291  this->expect_true(pte->pwt());
292  this->expect_false(pte->pcd());
293  this->expect_false(pte->pat_4k());
294 
295  pte->set_pat_index_4k(2);
296  this->expect_false(pte->pwt());
297  this->expect_true(pte->pcd());
298  this->expect_false(pte->pat_4k());
299 
300  pte->set_pat_index_4k(3);
301  this->expect_true(pte->pwt());
302  this->expect_true(pte->pcd());
303  this->expect_false(pte->pat_4k());
304 
305  pte->set_pat_index_4k(4);
306  this->expect_false(pte->pwt());
307  this->expect_false(pte->pcd());
308  this->expect_true(pte->pat_4k());
309 
310  pte->set_pat_index_4k(5);
311  this->expect_true(pte->pwt());
312  this->expect_false(pte->pcd());
313  this->expect_true(pte->pat_4k());
314 
315  pte->set_pat_index_4k(6);
316  this->expect_false(pte->pwt());
317  this->expect_true(pte->pcd());
318  this->expect_true(pte->pat_4k());
319 
320  pte->set_pat_index_4k(7);
321  this->expect_true(pte->pwt());
322  this->expect_true(pte->pcd());
323  this->expect_true(pte->pat_4k());
324 
325  this->expect_exception([&] { pte->set_pat_index_4k(10); }, ""_ut_ffe);
326 
327  pte->set_pwt(false);
328  pte->set_pcd(false);
329  pte->set_pat_large(false);
330  this->expect_true(pte->pat_index_large() == 0);
331 
332  pte->set_pwt(true);
333  pte->set_pcd(false);
334  pte->set_pat_large(false);
335  this->expect_true(pte->pat_index_large() == 1);
336 
337  pte->set_pwt(false);
338  pte->set_pcd(true);
339  pte->set_pat_large(false);
340  this->expect_true(pte->pat_index_large() == 2);
341 
342  pte->set_pwt(true);
343  pte->set_pcd(true);
344  pte->set_pat_large(false);
345  this->expect_true(pte->pat_index_large() == 3);
346 
347  pte->set_pwt(false);
348  pte->set_pcd(false);
349  pte->set_pat_large(true);
350  this->expect_true(pte->pat_index_large() == 4);
351 
352  pte->set_pwt(true);
353  pte->set_pcd(false);
354  pte->set_pat_large(true);
355  this->expect_true(pte->pat_index_large() == 5);
356 
357  pte->set_pwt(false);
358  pte->set_pcd(true);
359  pte->set_pat_large(true);
360  this->expect_true(pte->pat_index_large() == 6);
361 
362  pte->set_pwt(true);
363  pte->set_pcd(true);
364  pte->set_pat_large(true);
365  this->expect_true(pte->pat_index_large() == 7);
366 
367  pte->set_pat_index_large(0);
368  this->expect_false(pte->pwt());
369  this->expect_false(pte->pcd());
370  this->expect_false(pte->pat_large());
371 
372  pte->set_pat_index_large(1);
373  this->expect_true(pte->pwt());
374  this->expect_false(pte->pcd());
375  this->expect_false(pte->pat_large());
376 
377  pte->set_pat_index_large(2);
378  this->expect_false(pte->pwt());
379  this->expect_true(pte->pcd());
380  this->expect_false(pte->pat_large());
381 
382  pte->set_pat_index_large(3);
383  this->expect_true(pte->pwt());
384  this->expect_true(pte->pcd());
385  this->expect_false(pte->pat_large());
386 
387  pte->set_pat_index_large(4);
388  this->expect_false(pte->pwt());
389  this->expect_false(pte->pcd());
390  this->expect_true(pte->pat_large());
391 
392  pte->set_pat_index_large(5);
393  this->expect_true(pte->pwt());
394  this->expect_false(pte->pcd());
395  this->expect_true(pte->pat_large());
396 
397  pte->set_pat_index_large(6);
398  this->expect_false(pte->pwt());
399  this->expect_true(pte->pcd());
400  this->expect_true(pte->pat_large());
401 
402  pte->set_pat_index_large(7);
403  this->expect_true(pte->pwt());
404  this->expect_true(pte->pcd());
405  this->expect_true(pte->pat_large());
406 
407  this->expect_exception([&] { pte->set_pat_index_large(10); }, ""_ut_ffe);
408 }
409 
410 void
411 memory_manager_ut::test_page_table_entry_x64_clear()
412 {
413  pte_type entry = 0xFFFFFFFFFFFFFFFF;
414  auto &&pte = std::make_unique<page_table_entry_x64>(&entry);
415 
416  pte->clear();
417  this->expect_true(entry == 0);
418 }
#define expect_exception(f, e)
Definition: unittest.h:162
uintptr_t integer_pointer
auto num_bits_set(T t) noexcept
Definition: bitmanip.h:59
page_table_entry_x64::integer_pointer pte_type
#define expect_false(a)
auto is_bit_set(T t, B b) noexcept
Definition: bitmanip.h:48
#define expect_true(a)