Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[demo] Add clock ioctls to the KVM interface #84

Open
wants to merge 12 commits into
base: mk-demo
Choose a base branch
from
4 changes: 2 additions & 2 deletions hypercall/include/mv_cpuid_flag_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ extern "C"
* <!-- description -->
* @brief Defines CPUID flags
*/
enum mv_cpuid_flag : int32_t
enum mv_cpuid_flag_t : int32_t
#else
/**
* <!-- description -->
* @brief Defines CPUID flags
*/
enum mv_cpuid_flag
enum mv_cpuid_flag_t
#endif
{
/** @brief reserved */
Expand Down
4 changes: 4 additions & 0 deletions hypercall/include/mv_rdl_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
#include <mv_rdl_entry_t.h> // IWYU pragma: export
#include <stdint.h>

#ifdef __clang__
#pragma clang diagnostic ignored "-Wold-style-cast"
#endif

#ifdef __cplusplus
extern "C"
{
Expand Down
26 changes: 26 additions & 0 deletions hypercall/mocks/mv_hypercall.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <mv_exit_io_t.h>
#include <mv_exit_reason_t.h>
#include <mv_mp_state_t.h>
#include <mv_cdl_t.h>
#include <mv_rdl_t.h>
#include <mv_reg_t.h>
#include <mv_translation_t.h>
Expand Down Expand Up @@ -246,14 +247,39 @@ extern "C"
NODISCARD static inline mv_status_t
mv_pp_op_cpuid_get_supported_list(uint64_t const hndl) NOEXCEPT
{
uint64_t mut_i;
struct mv_cdl_entry_t mut_cdl_entry;
#ifdef __cplusplus
bsl::expects(MV_INVALID_HANDLE != hndl);
bsl::expects(hndl > ((uint64_t)0));
#else
platform_expects(MV_INVALID_HANDLE != hndl);
platform_expects(hndl > ((uint64_t)0));
#endif
struct mv_cdl_t *const pmut_cdl = (struct mv_cdl_t *)g_mut_shared_pages[0];

#ifdef __cplusplus
bsl::expects(nullptr != pmut_cdl);
bsl::expects(pmut_cdl->num_entries < MV_CDL_MAX_ENTRIES);
#else
platform_expects(NULL != pmut_cdl);
platform_expects(pmut_cdl->num_entries < MV_CDL_MAX_ENTRIES);
#endif
if (MV_STATUS_FAILURE_CORRUPT_NUM_ENTRIES == g_mut_mv_pp_op_msr_get_supported_list) {
pmut_cdl->num_entries = GARBAGE;
return MV_STATUS_SUCCESS;
}

pmut_cdl->num_entries = g_mut_val;
for (mut_i = ((uint64_t)0); mut_i < pmut_cdl->num_entries; ++mut_i) {
mut_cdl_entry.fun = ((uint32_t)g_mut_val);
mut_cdl_entry.idx = ((uint32_t)g_mut_val);
mut_cdl_entry.eax = ((uint32_t)g_mut_val);
mut_cdl_entry.ebx = ((uint32_t)g_mut_val);
mut_cdl_entry.ecx = ((uint32_t)g_mut_val);
mut_cdl_entry.edx = ((uint32_t)g_mut_val);
pmut_cdl->entries[mut_i] = mut_cdl_entry;
}
return g_mut_mv_pp_op_cpuid_get_supported_list;
}

Expand Down
3 changes: 3 additions & 0 deletions hypercall/tests/mocks/mv_hypercall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ namespace shim
bsl::ut_given{} = [&]() noexcept {
constexpr auto hypercall{&mv_pp_op_cpuid_get_supported_list};
constexpr auto expected{42_u64};
mv_cdl_t mut_cdl{};
bsl::ut_when{} = [&]() noexcept {
mut_cdl.num_entries = bsl::safe_u64::magic_2().get();
g_mut_shared_pages[0] = &mut_cdl;
g_mut_mv_pp_op_cpuid_get_supported_list = expected.get();
bsl::ut_then{} = [&]() noexcept {
bsl::ut_check(expected == hypercall(hndl));
Expand Down
4 changes: 2 additions & 2 deletions shim/include/handle_system_kvm_get_msrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ extern "C"
* @brief Handles the execution of kvm_check_extension.
*
* <!-- inputs/outputs -->
* @param pmut_ioctl_args the arguments provided by userspace
* @param pmut_userargs the arguments provided by userspace
* @return SHIM_SUCCESS on success, SHIM_FAILURE on failure.
*/
NODISCARD int64_t handle_system_kvm_get_msrs(struct kvm_msrs *const pmut_ioctl_args) NOEXCEPT;
NODISCARD int64_t handle_system_kvm_get_msrs(struct kvm_msrs *const pmut_userargs) NOEXCEPT;

#ifdef __cplusplus
}
Expand Down
7 changes: 5 additions & 2 deletions shim/include/handle_vcpu_kvm_get_msrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <kvm_msrs.h>
#include <mv_types.h>
#include <shim_vcpu_t.h>

#ifdef __cplusplus
extern "C"
Expand All @@ -40,10 +41,12 @@ extern "C"
* @brief Handles the execution of kvm_get_msrs.
*
* <!-- inputs/outputs -->
* @param pmut_ioctl_args the arguments provided by userspace
* @param vcpu to get vsid to pass to hypercall
* @param pmut_args the arguments provided by userspace
* @return SHIM_SUCCESS on success, SHIM_FAILURE on failure.
*/
NODISCARD int64_t handle_vcpu_kvm_get_msrs(struct kvm_msrs *const pmut_ioctl_args) NOEXCEPT;
NODISCARD int64_t handle_vcpu_kvm_get_msrs(
struct shim_vcpu_t const *const vcpu, struct kvm_msrs *const pmut_args) NOEXCEPT;

#ifdef __cplusplus
}
Expand Down
7 changes: 5 additions & 2 deletions shim/include/handle_vcpu_kvm_set_msrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <kvm_msrs.h>
#include <mv_types.h>
#include <shim_vcpu_t.h>

#ifdef __cplusplus
extern "C"
Expand All @@ -40,10 +41,12 @@ extern "C"
* @brief Handles the execution of kvm_set_msrs.
*
* <!-- inputs/outputs -->
* @param pmut_ioctl_args the arguments provided by userspace
* @param vcpu arguments received from private data
* @param args the arguments provided by userspace
* @return SHIM_SUCCESS on success, SHIM_FAILURE on failure.
*/
NODISCARD int64_t handle_vcpu_kvm_set_msrs(struct kvm_msrs *const pmut_ioctl_args) NOEXCEPT;
NODISCARD int64_t handle_vcpu_kvm_set_msrs(
struct shim_vcpu_t const *const vcpu, struct kvm_msrs const *const args) NOEXCEPT;

#ifdef __cplusplus
}
Expand Down
5 changes: 0 additions & 5 deletions shim/include/handle_vm_kvm_set_user_memory_region.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ extern "C"
{
#endif

/** @brief TBD */
#define KVM_MEM_LOG_DIRTY_PAGES (((uint64_t)1) << ((uint64_t)0))
/** @brief allows a slot to be read-only */
#define KVM_MEM_READONLY (((uint64_t)1) << ((uint64_t)1))

/**
* <!-- description -->
* @brief Handles the execution of kvm_set_user_memory_region.
Expand Down
12 changes: 10 additions & 2 deletions shim/include/kvm_clock_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ extern "C"
{
#endif

#define KVM_CLOCK_TSC_STABLE 2

#pragma pack(push, 1)

/**
Expand All @@ -44,8 +46,14 @@ extern "C"
*/
struct kvm_clock_data
{
/** @brief replace me with contents from KVM API */
int32_t dummy;
/** @brief value of the clock */
uint64_t clock;

/** @brief clock flags */
uint32_t flags;

/** @brief unused padding for alignment */
uint32_t pad[9];
};

#pragma pack(pop)
Expand Down
60 changes: 60 additions & 0 deletions shim/include/kvm_clock_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @copyright
* Copyright (C) 2020 Assured Information Security, Inc.
*
* @copyright
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* @copyright
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* @copyright
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef KVM_CLOCK_DATA_HPP
#define KVM_CLOCK_DATA_HPP

#include <bsl/array.hpp>
#include <bsl/convert.hpp>
#include <bsl/safe_integral.hpp>

constexpr auto CLOCK_TSC_STABLE{2_u32};
constexpr auto PADDING{9_u32};

namespace shim
{
#pragma pack(push, 1)

/**
* @struct kvm_clock_data
*
* <!-- description -->
* @brief see /include/uapi/linux/kvm.h in Linux for more details.
*/
struct kvm_clock_data
{
/** @brief value of the clock data */
bsl::uint64 clock;
/** @brief clock flags */
bsl::uint32 flags;
/** @brief CPUID entries */
bsl::array<bsl::uint32, PADDING.get()> pad;
};
}

#pragma pack(pop)

#endif
11 changes: 9 additions & 2 deletions shim/include/kvm_cpuid2.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@
#ifndef KVM_CPUID2_H
#define KVM_CPUID2_H

#include <kvm_cpuid_entry2.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C"
{
#endif

#define CPUID2_MAX_ENTRIES 40

#pragma pack(push, 1)

/**
Expand All @@ -44,8 +47,12 @@ extern "C"
*/
struct kvm_cpuid2
{
/** @brief replace me with contents from KVM API */
int32_t dummy;
/** @brief number of entries */
uint32_t nent;
/** @brief padding for alignment */
uint32_t padding;
/** @brief CPUID entries */
struct kvm_cpuid_entry2 entries[CPUID2_MAX_ENTRIES];
};

#pragma pack(pop)
Expand Down
61 changes: 61 additions & 0 deletions shim/include/kvm_cpuid2.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @copyright
* Copyright (C) 2020 Assured Information Security, Inc.
*
* @copyright
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* @copyright
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* @copyright
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef KVM_CPUID2_HPP
#define KVM_CPUID2_HPP

#include <kvm_cpuid_entry2.hpp>

#include <bsl/array.hpp>
#include <bsl/convert.hpp>
#include <bsl/safe_integral.hpp>

constexpr auto CPUID2_MAX_ENTRIES{40_u32};

namespace shim
{
#pragma pack(push, 1)

/**
* @struct kvm_cpuid2
*
* <!-- description -->
* @brief see /include/uapi/linux/kvm.h in Linux for more details.
*/
struct kvm_cpuid2
{
/** @brief number of entries */
bsl::uint32 nent;
/** @brief padding for alignment */
bsl::uint32 padding;
/** @brief CPUID entries */
bsl::array<shim::kvm_cpuid_entry2, CPUID2_MAX_ENTRIES.get()> entries;
};
}

#pragma pack(pop)

#endif
Loading