test_msrs_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 #include <intrinsics/msrs_x64.h>
24 
25 using namespace x64;
26 
27 std::map<msrs::field_type, msrs::value_type> g_msrs;
28 
29 extern "C" uint64_t
31 { return g_msrs[addr]; }
32 
33 extern "C" void
34 __write_msr(uint32_t addr, uint64_t val) noexcept
35 { g_msrs[addr] = val; }
36 
37 void
38 intrinsics_ut::test_ia32_pat()
39 {
40  msrs::ia32_pat::set(0xFFFFFFFFFFFFFFFFUL);
41  this->expect_true(msrs::ia32_pat::get() == 0xFFFFFFFFFFFFFFFFUL);
42 
44 
45  msrs::ia32_pat::set(0x0U);
46  this->expect_true(msrs::ia32_pat::get() == 0x0U);
47 }
48 
49 void
50 intrinsics_ut::test_ia32_pat_pa0()
51 {
53  this->expect_true(msrs::ia32_pat::pa0::get() == 6UL);
54  this->expect_true(msrs::ia32_pat::pa0::get(0x0000000000000006UL) == 6UL);
55 
57  this->expect_true(msrs::ia32_pat::pa0::get() == 4UL);
58  this->expect_true(msrs::ia32_pat::pa0::get(0x0000000000000004UL) == 4UL);
59 }
60 
61 void
62 intrinsics_ut::test_ia32_pat_pa1()
63 {
65  this->expect_true(msrs::ia32_pat::pa1::get() == 6UL);
66  this->expect_true(msrs::ia32_pat::pa1::get(0x0000000000000600UL) == 6UL);
67 
69  this->expect_true(msrs::ia32_pat::pa1::get() == 4UL);
70  this->expect_true(msrs::ia32_pat::pa1::get(0x0000000000000400UL) == 4UL);
71 }
72 
73 void
74 intrinsics_ut::test_ia32_pat_pa2()
75 {
77  this->expect_true(msrs::ia32_pat::pa2::get() == 6UL);
78  this->expect_true(msrs::ia32_pat::pa2::get(0x0000000000060000UL) == 6UL);
79 
81  this->expect_true(msrs::ia32_pat::pa2::get() == 4UL);
82  this->expect_true(msrs::ia32_pat::pa2::get(0x0000000000040000UL) == 4UL);
83 }
84 
85 void
86 intrinsics_ut::test_ia32_pat_pa3()
87 {
89  this->expect_true(msrs::ia32_pat::pa3::get() == 6UL);
90  this->expect_true(msrs::ia32_pat::pa3::get(0x0000000006000000UL) == 6UL);
91 
93  this->expect_true(msrs::ia32_pat::pa3::get() == 4UL);
94  this->expect_true(msrs::ia32_pat::pa3::get(0x0000000004000000UL) == 4UL);
95 }
96 
97 void
98 intrinsics_ut::test_ia32_pat_pa4()
99 {
101  this->expect_true(msrs::ia32_pat::pa4::get() == 6UL);
102  this->expect_true(msrs::ia32_pat::pa4::get(0x0000000600000000UL) == 6UL);
103 
105  this->expect_true(msrs::ia32_pat::pa4::get() == 4UL);
106  this->expect_true(msrs::ia32_pat::pa4::get(0x0000000400000000UL) == 4UL);
107 }
108 
109 void
110 intrinsics_ut::test_ia32_pat_pa5()
111 {
113  this->expect_true(msrs::ia32_pat::pa5::get() == 6UL);
114  this->expect_true(msrs::ia32_pat::pa5::get(0x0000060000000000UL) == 6UL);
115 
117  this->expect_true(msrs::ia32_pat::pa5::get() == 4UL);
118  this->expect_true(msrs::ia32_pat::pa5::get(0x0000040000000000UL) == 4UL);
119 }
120 
121 void
122 intrinsics_ut::test_ia32_pat_pa6()
123 {
125  this->expect_true(msrs::ia32_pat::pa6::get() == 6UL);
126  this->expect_true(msrs::ia32_pat::pa6::get(0x0006000000000000UL) == 6UL);
127 
129  this->expect_true(msrs::ia32_pat::pa6::get() == 4UL);
130  this->expect_true(msrs::ia32_pat::pa6::get(0x0004000000000000UL) == 4UL);
131 }
132 
133 void
134 intrinsics_ut::test_ia32_pat_pa7()
135 {
137  this->expect_true(msrs::ia32_pat::pa7::get() == 6UL);
138  this->expect_true(msrs::ia32_pat::pa7::get(0x0600000000000000UL) == 6UL);
139 
141  this->expect_true(msrs::ia32_pat::pa7::get() == 4UL);
142  this->expect_true(msrs::ia32_pat::pa7::get(0x0400000000000000UL) == 4UL);
143 }
144 
145 void
146 intrinsics_ut::test_ia32_pat_pa()
147 {
156 
157  this->expect_true(msrs::ia32_pat::pa(0UL) == 0UL);
158  this->expect_true(msrs::ia32_pat::pa(1UL) == 1UL);
159  this->expect_true(msrs::ia32_pat::pa(2UL) == 2UL);
160  this->expect_true(msrs::ia32_pat::pa(3UL) == 3UL);
161  this->expect_true(msrs::ia32_pat::pa(4UL) == 4UL);
162  this->expect_true(msrs::ia32_pat::pa(5UL) == 5UL);
163  this->expect_true(msrs::ia32_pat::pa(6UL) == 6UL);
164  this->expect_true(msrs::ia32_pat::pa(7UL) == 7UL);
165 
166  this->expect_true(msrs::ia32_pat::pa(0x0000000000000000UL, 0UL) == 0UL);
167  this->expect_true(msrs::ia32_pat::pa(0x0000000000000100UL, 1UL) == 1UL);
168  this->expect_true(msrs::ia32_pat::pa(0x0000000000020000UL, 2UL) == 2UL);
169  this->expect_true(msrs::ia32_pat::pa(0x0000000003000000UL, 3UL) == 3UL);
170  this->expect_true(msrs::ia32_pat::pa(0x0000000400000000UL, 4UL) == 4UL);
171  this->expect_true(msrs::ia32_pat::pa(0x0000050000000000UL, 5UL) == 5UL);
172  this->expect_true(msrs::ia32_pat::pa(0x0006000000000000UL, 6UL) == 6UL);
173  this->expect_true(msrs::ia32_pat::pa(0x0700000000000000UL, 7UL) == 7UL);
174 
175  this->expect_exception([&] { msrs::ia32_pat::pa(10UL); }, ""_ut_ree);
176  this->expect_exception([&] { msrs::ia32_pat::pa(0x0000000000000000UL, 10UL); }, ""_ut_ree);
177 }
#define expect_exception(f, e)
Definition: unittest.h:162
auto get() noexcept
Definition: msrs_x64.h:53
void set(T val) noexcept
Definition: msrs_x64.h:90
void set(T val) noexcept
Definition: msrs_x64.h:141
void set(T val) noexcept
Definition: msrs_x64.h:107
void set(T val) noexcept
Definition: msrs_x64.h:192
auto get() noexcept
Definition: msrs_x64.h:133
void set(T val) noexcept
Definition: msrs_x64.h:57
auto get() noexcept
Definition: msrs_x64.h:65
void uint64_t uint64_t uint64_t *rdx noexcept
constexpr const auto addr
Definition: cpuid_x64.h:80
void __write_msr(uint32_t addr, uint64_t val) noexcept
auto get() noexcept
Definition: msrs_x64.h:82
auto get() noexcept
Definition: msrs_x64.h:167
auto get() noexcept
Definition: msrs_x64.h:99
void set(T val) noexcept
Definition: msrs_x64.h:175
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
uint64_t __read_msr(uint32_t addr) noexcept
void set(T val) noexcept
Definition: msrs_x64.h:73
void dump() noexcept
Definition: msrs_x64.h:195
auto get() noexcept
Definition: msrs_x64.h:150
void set(T val) noexcept
Definition: msrs_x64.h:124
void set(T val) noexcept
Definition: msrs_x64.h:158
Definition: cache_x64.h:31
std::map< msrs::field_type, msrs::value_type > g_msrs
#define expect_true(a)