test_vmcs_intel_x64_state.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 // Author: Connor Davis <davisc@ainfosec.com>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 
23 #include <test.h>
25 #include <intrinsics/x64.h>
26 
27 using namespace x64;
28 
29 void
30 vmcs_ut::test_state()
31 {
32  this->expect_no_exception([&] { vmcs_intel_x64_state state{}; });
33 }
34 
35 void
36 vmcs_ut::test_state_segment_registers()
37 {
38  this->expect_no_exception([&]
39  {
40  vmcs_intel_x64_state state{};
41 
42  this->expect_true(state.es() == 0U);
43  this->expect_true(state.cs() == 0U);
44  this->expect_true(state.ss() == 0U);
45  this->expect_true(state.ds() == 0U);
46  this->expect_true(state.fs() == 0U);
47  this->expect_true(state.gs() == 0U);
48  this->expect_true(state.tr() == 0U);
49  this->expect_true(state.ldtr() == 0U);
50  this->expect_no_exception([&]{ state.set_es(42U); });
51  this->expect_no_exception([&]{ state.set_cs(42U); });
52  this->expect_no_exception([&]{ state.set_ss(42U); });
53  this->expect_no_exception([&]{ state.set_ds(42U); });
54  this->expect_no_exception([&]{ state.set_fs(42U); });
55  this->expect_no_exception([&]{ state.set_gs(42U); });
56  this->expect_no_exception([&]{ state.set_tr(42U); });
57  this->expect_no_exception([&]{ state.set_ldtr(42U); });
58  });
59 }
60 
61 void
62 vmcs_ut::test_state_control_registers()
63 {
64  this->expect_no_exception([&]
65  {
66  vmcs_intel_x64_state state{};
67 
68  this->expect_true(state.cr0() == 0U);
69  this->expect_true(state.cr3() == 0U);
70  this->expect_true(state.cr4() == 0U);
71  this->expect_no_exception([&]{ state.set_cr0(42U); });
72  this->expect_no_exception([&]{ state.set_cr3(42U); });
73  this->expect_no_exception([&]{ state.set_cr4(42U); });
74  });
75 }
76 
77 void
78 vmcs_ut::test_state_debug_registers()
79 {
80  this->expect_no_exception([&]
81  {
82  vmcs_intel_x64_state state{};
83 
84  this->expect_true(state.dr7() == 0U);
85  this->expect_no_exception([&]{ state.set_dr7(42U); });
86  });
87 }
88 
89 void
90 vmcs_ut::test_state_rflags()
91 {
92  this->expect_no_exception([&]
93  {
94  vmcs_intel_x64_state state{};
95 
96  this->expect_true(state.rflags() == 0U);
97  this->expect_no_exception([&]{ state.set_rflags(42U); });
98  });
99 }
100 
101 void
102 vmcs_ut::test_state_gdt_base()
103 {
104  this->expect_no_exception([&]
105  {
106  vmcs_intel_x64_state state{};
107 
108  this->expect_true(state.gdt_base() == 0U);
109  this->expect_no_exception([&]{ state.set_gdt_base(42U); });
110  });
111 }
112 
113 void
114 vmcs_ut::test_state_idt_base()
115 {
116  this->expect_no_exception([&]
117  {
118  vmcs_intel_x64_state state{};
119 
120  this->expect_true(state.idt_base() == 0U);
121  this->expect_no_exception([&]{ state.set_idt_base(42U); });
122  });
123 }
124 
125 void
126 vmcs_ut::test_state_gdt_limit()
127 {
128  this->expect_no_exception([&]
129  {
130  vmcs_intel_x64_state state{};
131 
132  this->expect_true(state.gdt_limit() == 0U);
133  this->expect_no_exception([&]{ state.set_gdt_limit(42U); });
134  });
135 }
136 
137 void
138 vmcs_ut::test_state_idt_limit()
139 {
140  this->expect_no_exception([&]
141  {
142  vmcs_intel_x64_state state{};
143 
144  this->expect_true(state.idt_limit() == 0U);
145  this->expect_no_exception([&]{ state.set_idt_limit(42U); });
146  });
147 }
148 
149 void
150 vmcs_ut::test_state_segment_registers_limit()
151 {
152  this->expect_no_exception([&]
153  {
154  vmcs_intel_x64_state state{};
155 
156  this->expect_true(state.es_limit() == 0U);
157  this->expect_true(state.cs_limit() == 0U);
158  this->expect_true(state.ss_limit() == 0U);
159  this->expect_true(state.ds_limit() == 0U);
160  this->expect_true(state.fs_limit() == 0U);
161  this->expect_true(state.gs_limit() == 0U);
162  this->expect_true(state.tr_limit() == 0U);
163  this->expect_true(state.ldtr_limit() == 0U);
164  this->expect_no_exception([&]{ state.set_es_limit(42U); });
165  this->expect_no_exception([&]{ state.set_cs_limit(42U); });
166  this->expect_no_exception([&]{ state.set_ss_limit(42U); });
167  this->expect_no_exception([&]{ state.set_ds_limit(42U); });
168  this->expect_no_exception([&]{ state.set_fs_limit(42U); });
169  this->expect_no_exception([&]{ state.set_gs_limit(42U); });
170  this->expect_no_exception([&]{ state.set_tr_limit(42U); });
171  this->expect_no_exception([&]{ state.set_ldtr_limit(42U); });
172  });
173 
174 }
175 
176 void
177 vmcs_ut::test_state_segment_registers_access_rights()
178 {
179  this->expect_no_exception([&]
180  {
181  vmcs_intel_x64_state state{};
182 
183  this->expect_true(state.es_access_rights() == access_rights::unusable);
184  this->expect_true(state.cs_access_rights() == access_rights::unusable);
185  this->expect_true(state.ss_access_rights() == access_rights::unusable);
186  this->expect_true(state.ds_access_rights() == access_rights::unusable);
187  this->expect_true(state.fs_access_rights() == access_rights::unusable);
188  this->expect_true(state.gs_access_rights() == access_rights::unusable);
189  this->expect_true(state.tr_access_rights() == access_rights::unusable);
190  this->expect_true(state.ldtr_access_rights() == access_rights::unusable);
191  this->expect_no_exception([&]{ state.set_es_access_rights(42U); });
192  this->expect_no_exception([&]{ state.set_cs_access_rights(42U); });
193  this->expect_no_exception([&]{ state.set_ss_access_rights(42U); });
194  this->expect_no_exception([&]{ state.set_ds_access_rights(42U); });
195  this->expect_no_exception([&]{ state.set_fs_access_rights(42U); });
196  this->expect_no_exception([&]{ state.set_gs_access_rights(42U); });
197  this->expect_no_exception([&]{ state.set_tr_access_rights(42U); });
198  this->expect_no_exception([&]{ state.set_ldtr_access_rights(42U); });
199  });
200 }
201 
202 void
203 vmcs_ut::test_state_segment_register_base()
204 {
205  this->expect_no_exception([&]
206  {
207  vmcs_intel_x64_state state{};
208 
209  this->expect_true(state.es_base() == 0U);
210  this->expect_true(state.cs_base() == 0U);
211  this->expect_true(state.ss_base() == 0U);
212  this->expect_true(state.ds_base() == 0U);
213  this->expect_true(state.fs_base() == 0U);
214  this->expect_true(state.gs_base() == 0U);
215  this->expect_true(state.tr_base() == 0U);
216  this->expect_true(state.ldtr_base() == 0U);
217  this->expect_no_exception([&]{ state.set_es_base(42U); });
218  this->expect_no_exception([&]{ state.set_cs_base(42U); });
219  this->expect_no_exception([&]{ state.set_ss_base(42U); });
220  this->expect_no_exception([&]{ state.set_ds_base(42U); });
221  this->expect_no_exception([&]{ state.set_fs_base(42U); });
222  this->expect_no_exception([&]{ state.set_gs_base(42U); });
223  this->expect_no_exception([&]{ state.set_tr_base(42U); });
224  this->expect_no_exception([&]{ state.set_ldtr_base(42U); });
225  });
226 }
227 
228 void
229 vmcs_ut::test_state_msrs()
230 {
231  this->expect_no_exception([&]
232  {
233  vmcs_intel_x64_state state{};
234 
235  this->expect_true(state.ia32_debugctl_msr() == 0U);
236  this->expect_true(state.ia32_pat_msr() == 0U);
237  this->expect_true(state.ia32_efer_msr() == 0U);
238  this->expect_true(state.ia32_perf_global_ctrl_msr() == 0U);
239  this->expect_true(state.ia32_sysenter_cs_msr() == 0U);
240  this->expect_true(state.ia32_sysenter_esp_msr() == 0U);
241  this->expect_true(state.ia32_sysenter_eip_msr() == 0U);
242  this->expect_true(state.ia32_fs_base_msr() == 0U);
243  this->expect_true(state.ia32_gs_base_msr() == 0U);
244  this->expect_no_exception([&]{ state.set_ia32_debugctl_msr(42U); });
245  this->expect_no_exception([&]{ state.set_ia32_pat_msr(42U); });
246  this->expect_no_exception([&]{ state.set_ia32_efer_msr(42U); });
247  this->expect_no_exception([&]{ state.set_ia32_perf_global_ctrl_msr(42U); });
248  this->expect_no_exception([&]{ state.set_ia32_sysenter_cs_msr(42U); });
249  this->expect_no_exception([&]{ state.set_ia32_sysenter_esp_msr(42U); });
250  this->expect_no_exception([&]{ state.set_ia32_sysenter_eip_msr(42U); });
251  this->expect_no_exception([&]{ state.set_ia32_fs_base_msr(42U); });
252  this->expect_no_exception([&]{ state.set_ia32_gs_base_msr(42U); });
253  });
254 }
255 
256 void
257 vmcs_ut::test_state_is_guest()
258 {
259  this->expect_no_exception([&]
260  {
261  vmcs_intel_x64_state state{};
262  this->expect_false(state.is_guest());
263  });
264 }
265 
266 void
267 vmcs_ut::test_state_dump()
268 {
269  this->expect_no_exception([&]
270  {
271  vmcs_intel_x64_state state{};
272  this->expect_no_exception([&] { state.dump(); });
273  });
274 }
#define expect_no_exception(f)
Definition: unittest.h:198
constexpr const auto unusable
Definition: x64.h:90
#define expect_false(a)
Definition: cache_x64.h:31
#define expect_true(a)