|
| 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 <mv_constants.hpp> |
| 27 | +#include <mv_hypercall_impl.hpp> |
| 28 | +#include <mv_hypercall_t.hpp> |
| 29 | +#include <mv_types.hpp> |
| 30 | + |
| 31 | +#include <bsl/convert.hpp> |
| 32 | +#include <bsl/enable_color.hpp> |
| 33 | +#include <bsl/exit_code.hpp> |
| 34 | +#include <bsl/safe_integral.hpp> |
| 35 | + |
| 36 | +namespace hypercall |
| 37 | +{ |
| 38 | + |
| 39 | + /// <!-- description --> |
| 40 | + /// @brief Always returns bsl::exit_success. If a failure occurs, |
| 41 | + /// this function will exit early. |
| 42 | + /// |
| 43 | + /// <!-- inputs/outputs --> |
| 44 | + /// @return Always returns bsl::exit_success. If a failure occurs, |
| 45 | + /// this function will exit early. |
| 46 | + /// |
| 47 | + [[nodiscard]] constexpr auto |
| 48 | + tests() noexcept -> bsl::exit_code |
| 49 | + { |
| 50 | + constexpr auto msr_pat{0x277_u32}; |
| 51 | + constexpr auto msr_sysenter_cs{0x174_u32}; |
| 52 | + constexpr auto msr_sysenter_esp{0x175_u32}; |
| 53 | + constexpr auto msr_sysenter_eip{0x176_u32}; |
| 54 | + constexpr auto msr_efer{0xC0000080_u32}; |
| 55 | + constexpr auto msr_star{0xC0000081_u32}; |
| 56 | + constexpr auto msr_lstar{0xC0000082_u32}; |
| 57 | + constexpr auto msr_cstar{0xC0000083_u32}; |
| 58 | + constexpr auto msr_fmask{0xC0000084_u32}; |
| 59 | + constexpr auto msr_fs_base{0xC0000100_u32}; |
| 60 | + constexpr auto msr_gs_base{0xC0000101_u32}; |
| 61 | + constexpr auto msr_kernel_gs_base{0xC0000102_u32}; |
| 62 | + constexpr auto msr_apic_base{0x0000001B_u32}; |
| 63 | + |
| 64 | + mv_status_t mut_ret{}; |
| 65 | + bsl::safe_u64 mut_val{}; |
| 66 | + |
| 67 | + integration::initialize_globals(); |
| 68 | + |
| 69 | + // invalid VSID #1 |
| 70 | + mut_ret = |
| 71 | + mv_vs_op_msr_get_impl(hndl.get(), MV_INVALID_ID.get(), msr_star.get(), mut_val.data()); |
| 72 | + integration::verify(mut_ret != MV_STATUS_SUCCESS); |
| 73 | + |
| 74 | + // invalid VSID #2 |
| 75 | + mut_ret = |
| 76 | + mv_vs_op_msr_get_impl(hndl.get(), MV_SELF_ID.get(), msr_star.get(), mut_val.data()); |
| 77 | + integration::verify(mut_ret != MV_STATUS_SUCCESS); |
| 78 | + |
| 79 | + // invalid VSID #3 |
| 80 | + mut_ret = mv_vs_op_msr_get_impl(hndl.get(), vsid0.get(), msr_star.get(), mut_val.data()); |
| 81 | + integration::verify(mut_ret != MV_STATUS_SUCCESS); |
| 82 | + |
| 83 | + // invalid VSID #4 |
| 84 | + mut_ret = mv_vs_op_msr_get_impl(hndl.get(), vsid1.get(), msr_star.get(), mut_val.data()); |
| 85 | + integration::verify(mut_ret != MV_STATUS_SUCCESS); |
| 86 | + |
| 87 | + // VSID out of range |
| 88 | + auto const oor{bsl::to_u16(HYPERVISOR_MAX_VSS + bsl::safe_u64::magic_1()).checked()}; |
| 89 | + mut_ret = mv_vs_op_msr_get_impl(hndl.get(), oor.get(), msr_star.get(), mut_val.data()); |
| 90 | + integration::verify(mut_ret != MV_STATUS_SUCCESS); |
| 91 | + |
| 92 | + // VSID not yet created |
| 93 | + auto const nyc{bsl::to_u16(HYPERVISOR_MAX_VSS - bsl::safe_u64::magic_1()).checked()}; |
| 94 | + mut_ret = mv_vs_op_msr_get_impl(hndl.get(), nyc.get(), msr_star.get(), mut_val.data()); |
| 95 | + integration::verify(mut_ret != MV_STATUS_SUCCESS); |
| 96 | + |
| 97 | + // register unsupported |
| 98 | + constexpr auto ureg{0xFFFFFFFF_u32}; |
| 99 | + mut_ret = mv_vs_op_msr_get_impl(hndl.get(), self.get(), ureg.get(), mut_val.data()); |
| 100 | + integration::verify(mut_ret != MV_STATUS_SUCCESS); |
| 101 | + |
| 102 | + // Verify the general purpose registers |
| 103 | + { |
| 104 | + auto const vmid{mut_hvc.mv_vm_op_create_vm()}; |
| 105 | + auto const vpid{mut_hvc.mv_vp_op_create_vp(vmid)}; |
| 106 | + auto const vsid{mut_hvc.mv_vs_op_create_vs(vpid)}; |
| 107 | + |
| 108 | + integration::verify(vmid.is_valid_and_checked()); |
| 109 | + integration::verify(vpid.is_valid_and_checked()); |
| 110 | + integration::verify(vsid.is_valid_and_checked()); |
| 111 | + |
| 112 | + constexpr auto val{0x87654321_u64}; |
| 113 | + |
| 114 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_pat, val)); |
| 115 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_pat)); |
| 116 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_sysenter_cs, val)); |
| 117 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_sysenter_cs)); |
| 118 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_sysenter_esp, val)); |
| 119 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_sysenter_esp)); |
| 120 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_sysenter_eip, val)); |
| 121 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_sysenter_eip)); |
| 122 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_efer, val)); |
| 123 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_efer)); |
| 124 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_star, val)); |
| 125 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_star)); |
| 126 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_lstar, val)); |
| 127 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_lstar)); |
| 128 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_cstar, val)); |
| 129 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_cstar)); |
| 130 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_fmask, val)); |
| 131 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_fmask)); |
| 132 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_fs_base, val)); |
| 133 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_fs_base)); |
| 134 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_gs_base, val)); |
| 135 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_gs_base)); |
| 136 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_kernel_gs_base, val)); |
| 137 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_kernel_gs_base)); |
| 138 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_apic_base, val)); |
| 139 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_apic_base)); |
| 140 | + |
| 141 | + integration::verify(mut_hvc.mv_vs_op_destroy_vs(vsid)); |
| 142 | + integration::verify(mut_hvc.mv_vp_op_destroy_vp(vpid)); |
| 143 | + integration::verify(mut_hvc.mv_vm_op_destroy_vm(vmid)); |
| 144 | + } |
| 145 | + |
| 146 | + // Verify the general purpose registers |
| 147 | + { |
| 148 | + auto const vmid{mut_hvc.mv_vm_op_create_vm()}; |
| 149 | + auto const vpid{mut_hvc.mv_vp_op_create_vp(vmid)}; |
| 150 | + auto const vsid{mut_hvc.mv_vs_op_create_vs(vpid)}; |
| 151 | + |
| 152 | + integration::verify(vmid.is_valid_and_checked()); |
| 153 | + integration::verify(vpid.is_valid_and_checked()); |
| 154 | + integration::verify(vsid.is_valid_and_checked()); |
| 155 | + |
| 156 | + constexpr auto val{0x87654321_u64}; |
| 157 | + |
| 158 | + integration::set_affinity(core0); |
| 159 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_star, val)); |
| 160 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_star)); |
| 161 | + integration::set_affinity(core1); |
| 162 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_star, val)); |
| 163 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_star)); |
| 164 | + |
| 165 | + integration::set_affinity(core0); |
| 166 | + integration::verify(mut_hvc.mv_vs_op_msr_set(vsid, msr_star, val)); |
| 167 | + integration::set_affinity(core1); |
| 168 | + integration::verify(val == mut_hvc.mv_vs_op_msr_get(vsid, msr_star)); |
| 169 | + integration::set_affinity(core0); |
| 170 | + |
| 171 | + integration::verify(mut_hvc.mv_vs_op_destroy_vs(vsid)); |
| 172 | + integration::verify(mut_hvc.mv_vp_op_destroy_vp(vpid)); |
| 173 | + integration::verify(mut_hvc.mv_vm_op_destroy_vm(vmid)); |
| 174 | + } |
| 175 | + |
| 176 | + return bsl::exit_success; |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +/// <!-- description --> |
| 181 | +/// @brief Provides the main entry point for this application. |
| 182 | +/// |
| 183 | +/// <!-- inputs/outputs --> |
| 184 | +/// @return bsl::exit_success on success, bsl::exit_failure otherwise. |
| 185 | +/// |
| 186 | +[[nodiscard]] auto |
| 187 | +main() noexcept -> bsl::exit_code |
| 188 | +{ |
| 189 | + bsl::enable_color(); |
| 190 | + return hypercall::tests(); |
| 191 | +} |
0 commit comments