|
| 1 | +/// @copyright |
| 2 | +/// Copyright (C) 2020 Assured Information Security, Inc. |
| 3 | +/// |
| 4 | +/// @copyright |
| 5 | +/// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +/// of this software and associated documentation files (the "Software"), to deal |
| 7 | +/// in the Software without restriction, including without limitation the rights |
| 8 | +/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +/// copies of the Software, and to permit persons to whom the Software is |
| 10 | +/// furnished to do so, subject to the following conditions: |
| 11 | +/// |
| 12 | +/// @copyright |
| 13 | +/// The above copyright notice and this permission notice shall be included in |
| 14 | +/// all copies or substantial portions of the Software. |
| 15 | +/// |
| 16 | +/// @copyright |
| 17 | +/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +/// SOFTWARE. |
| 24 | + |
| 25 | +#include <integration_utils.hpp> |
| 26 | +#include <ioctl_t.hpp> |
| 27 | +#include <kvm_msr_list.hpp> |
| 28 | +#include <shim_platform_interface.hpp> |
| 29 | + |
| 30 | +#include <bsl/array.hpp> |
| 31 | +#include <bsl/convert.hpp> |
| 32 | +#include <bsl/enable_color.hpp> |
| 33 | +#include <bsl/exit_code.hpp> |
| 34 | +#include <bsl/safe_idx.hpp> |
| 35 | +#include <bsl/safe_integral.hpp> |
| 36 | + |
| 37 | +/// <!-- description --> |
| 38 | +/// @brief Provides the main entry point for this application. |
| 39 | +/// |
| 40 | +/// <!-- inputs/outputs --> |
| 41 | +/// @return bsl::exit_success on success, bsl::exit_failure otherwise. |
| 42 | +/// |
| 43 | +[[nodiscard]] auto |
| 44 | +main() noexcept -> bsl::exit_code |
| 45 | +{ |
| 46 | + shim::kvm_msr_list mut_msr_list{}; |
| 47 | + constexpr auto star_val{0xC0000081_u32}; |
| 48 | + constexpr auto pat_val{0x00000277_u32}; |
| 49 | + constexpr auto apic_base_val{0x0000001B_u32}; |
| 50 | + constexpr auto init_nmsrs{0x10_u32}; |
| 51 | + |
| 52 | + bsl::enable_color(); |
| 53 | + integration::ioctl_t mut_system_ctl{shim::DEVICE_NAME}; |
| 54 | + bsl::array<bsl::uint32, bsl::uintmx(init_nmsrs.get())> mut_msr_indices{}; |
| 55 | + |
| 56 | + // nmsrs is too big |
| 57 | + { |
| 58 | + mut_msr_list.nmsrs = HYPERVISOR_PAGE_SIZE.get(); |
| 59 | + mut_msr_list.nmsrs++; |
| 60 | + mut_msr_list.indices = mut_msr_indices.front_if(); |
| 61 | + |
| 62 | + integration::verify( |
| 63 | + mut_system_ctl.write(shim::KVM_GET_MSR_INDEX_LIST, &mut_msr_list).is_neg()); |
| 64 | + } |
| 65 | + |
| 66 | + { |
| 67 | + mut_msr_list.nmsrs = init_nmsrs.get(); |
| 68 | + mut_msr_list.indices = mut_msr_indices.front_if(); |
| 69 | + |
| 70 | + integration::verify( |
| 71 | + mut_system_ctl.write(shim::KVM_GET_MSR_INDEX_LIST, &mut_msr_list).is_zero()); |
| 72 | + |
| 73 | + auto mut_nmsrs{bsl::to_u32(mut_msr_list.nmsrs)}; |
| 74 | + integration::verify(mut_nmsrs > bsl::safe_u32::magic_0()); |
| 75 | + } |
| 76 | + |
| 77 | + // Valid registers should be present |
| 78 | + { |
| 79 | + bool mut_found_star{false}; |
| 80 | + bool mut_found_pat{false}; |
| 81 | + bool mut_found_apic_base{false}; |
| 82 | + auto mut_nmsrs{bsl::to_idx(mut_msr_list.nmsrs)}; |
| 83 | + |
| 84 | + for (bsl::safe_idx mut_i{}; mut_i < mut_nmsrs; ++mut_i) { |
| 85 | + if (star_val == mut_msr_list.indices[mut_i.get()]) { |
| 86 | + mut_found_star = true; |
| 87 | + } |
| 88 | + else if (pat_val == mut_msr_list.indices[mut_i.get()]) { |
| 89 | + mut_found_pat = true; |
| 90 | + } |
| 91 | + else if (apic_base_val == mut_msr_list.indices[mut_i.get()]) { |
| 92 | + mut_found_apic_base = true; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + integration::verify(mut_found_star); |
| 97 | + integration::verify(mut_found_pat); |
| 98 | + integration::verify(mut_found_apic_base); |
| 99 | + } |
| 100 | + |
| 101 | + // Try a bunch of times |
| 102 | + { |
| 103 | + constexpr auto num_loops{0x1000_umx}; |
| 104 | + for (bsl::safe_idx mut_i{}; mut_i < num_loops; ++mut_i) { |
| 105 | + integration::verify( |
| 106 | + mut_system_ctl.write(shim::KVM_GET_MSR_INDEX_LIST, &mut_msr_list).is_zero()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return bsl::exit_success; |
| 111 | +} |
0 commit comments