Skip to content

Commit

Permalink
Merge pull request #123 from softickllc/createirqchip
Browse files Browse the repository at this point in the history
KVM_CREATE_IRQCHIP: Add scafolding and initial integration tests
  • Loading branch information
chp-io authored Feb 7, 2022
2 parents 9401ba2 + 3e0f939 commit acbfdf1
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 9 deletions.
8 changes: 6 additions & 2 deletions shim/include/handle_vm_kvm_create_irqchip.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define HANDLE_VM_KVM_CREATE_IRQCHIP_H

#include <mv_types.h>
#include <shim_vm_t.h>

#ifdef __cplusplus
extern "C"
Expand All @@ -36,12 +37,15 @@ extern "C"

/**
* <!-- description -->
* @brief Handles the execution of kvm_create_irqchip.
* @brief Handles the execution of handle_vm_kvm_create_irqchip.
*
* <!-- inputs/outputs -->
* @param pmut_vm the argument to hold vm details of type shim_vm_t
* @return SHIM_SUCCESS on success, SHIM_FAILURE on failure.
*/
NODISCARD int64_t handle_vm_kvm_create_irqchip(void) NOEXCEPT;
NODISCARD int64_t handle_vm_kvm_create_irqchip(struct shim_vm_t *const pmut_vm) NOEXCEPT;
// NODISCARD int64_t handle_vm_kvm_create_irqchip(
// struct shim_vm_t *const pmut_vm, struct shim_virqchip_t **const pmut_virqchip) NOEXCEPT;

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions shim/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ microv_add_shim_integration(kvm_get_msr_index_list HEADERS)
microv_add_shim_integration(kvm_get_supported_cpuid HEADERS)
microv_add_shim_integration(kvm_get_msrs HEADERS)
microv_add_shim_integration(kvm_set_msrs HEADERS)
microv_add_shim_integration(kvm_create_irqchip HEADERS)
56 changes: 56 additions & 0 deletions shim/integration/kvm_create_irqchip.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/// @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.

#include <integration_utils.hpp>
#include <ioctl_t.hpp>
#include <shim_platform_interface.hpp>

#include <bsl/convert.hpp>
#include <bsl/enable_color.hpp>
#include <bsl/exit_code.hpp>
#include <bsl/safe_integral.hpp>

/// <!-- description -->
/// @brief Provides the main entry point for this application.
///
/// <!-- inputs/outputs -->
/// @return bsl::exit_success on success, bsl::exit_failure otherwise.
///
[[nodiscard]] auto
main() noexcept -> bsl::exit_code
{
bsl::enable_color();
integration::ioctl_t mut_system_ctl{shim::DEVICE_NAME};
auto const vmfd{mut_system_ctl.send(shim::KVM_CREATE_VM)};
integration::ioctl_t mut_vm{bsl::to_i32(vmfd)};
auto const vcpufd{mut_vm.send(shim::KVM_CREATE_VCPU)};
integration::ioctl_t mut_vcpu{bsl::to_i32(vcpufd)};
constexpr auto mut_ret{0_i64};
{
auto const irqchip{mut_vcpu.send(shim::KVM_CREATE_IRQCHIP)};
integration::verify(irqchip.is_pos());
integration::verify(irqchip >= mut_ret.get());
}
return bsl::exit_success;
}
11 changes: 8 additions & 3 deletions shim/linux/src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <handle_vcpu_kvm_set_regs.h>
#include <handle_vcpu_kvm_set_sregs.h>
#include <handle_vm_kvm_check_extension.h>
#include <handle_vm_kvm_create_irqchip.h>
#include <handle_vm_kvm_create_vcpu.h>
#include <handle_vm_kvm_destroy_vcpu.h>
#include <handle_vm_kvm_set_user_memory_region.h>
Expand Down Expand Up @@ -506,9 +507,13 @@ dispatch_vm_kvm_create_device(struct kvm_create_device *const ioctl_args)
}

static long
dispatch_kvm_create_irqchip(void)
dispatch_kvm_create_irqchip(struct shim_vm_t *pmut_mut_vm)
{
return -EINVAL;
if (handle_vm_kvm_create_irqchip(pmut_mut_vm)) {
bferror("handle_vm_kvm_create_irqchip failed");
return -EINVAL;
}
return SHIM_SUCCESS;
}

static long
Expand Down Expand Up @@ -786,7 +791,7 @@ dev_unlocked_ioctl_vm(
}

case KVM_CREATE_IRQCHIP: {
return dispatch_kvm_create_irqchip();
return dispatch_kvm_create_irqchip(pmut_mut_vm);
}

case KVM_CREATE_PIT2: {
Expand Down
38 changes: 36 additions & 2 deletions shim/src/handle_vm_kvm_create_irqchip.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,51 @@
* SOFTWARE.
*/

#include <debug.h>
#include <detect_hypervisor.h>
#include <mv_types.h>
#include <platform.h>
#include <shim_vm_t.h>

/**
* <!-- description -->
* @brief Handles the execution of kvm_create_irqchip.
*
* <!-- inputs/outputs -->
* @return SHIM_SUCCESS on success, SHIM_FAILURE on failure.
* @param pmut_vm the argument to hold vm details of type shim_vm_t
* @return SHIM_SUCCESS on success, SHIM_FAILURE on failure.
*/
NODISCARD int64_t
handle_vm_kvm_create_irqchip(void) NOEXCEPT
handle_vm_kvm_create_irqchip(struct shim_vm_t *const pmut_vm) NOEXCEPT
{

platform_expects(NULL != pmut_vm);

if (detect_hypervisor()) {
bferror("The shim is not running in a VM. Did you forget to start MicroV?");
return SHIM_FAILURE;
}
//NOTE: Below hypercalls to uncomment when they are implemented

//platform_mutex_lock(&pmut_vm->mutex);
// mv_status_t mut_pic_ret;
// mv_status_t mut_iopic_ret;
/** @brief for x86 create pic and iopic **/

// mut_pic_ret = mv_vm_op_pic(pmut_vm->vmid);
// if (mut_pic_ret) {
// mut_iopic_ret = mv_vm_op_ioapic_init(pmut_vm->vmid);
// if (mut_iopic_ret) {
// platform_mutex_lock(&pmut_vm->slots_lock);
// mv_vm_op_destroy_pic(pmut_vm->vmid);
// platform_mutex_unlock(&pmut_vm->slots_lock);
// goto create_irqchip_unlock;
// }
// } else
// goto create_irqchip_unlock;

// create_irqchip_unlock:
// platform_mutex_unlock(&pmut_vm->mutex);

return SHIM_SUCCESS;
}
21 changes: 19 additions & 2 deletions shim/tests/src/test_handle_vm_kvm_create_irqchip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
/// SOFTWARE.

#include "../../include/handle_vm_kvm_create_irqchip.h"
#include "shim_vm_t.h"

#include <mv_types.h>
#include <helpers.hpp>

#include <bsl/ut.hpp>

Expand All @@ -42,16 +43,32 @@ namespace shim
[[nodiscard]] constexpr auto
tests() noexcept -> bsl::exit_code
{
init_tests();
bsl::ut_scenario{"description"} = []() noexcept {
bsl::ut_given{} = [&]() noexcept {
shim_vm_t mut_vm{};
bsl::ut_when{} = [&]() noexcept {
bsl::ut_then{} = [&]() noexcept {
bsl::ut_check(SHIM_SUCCESS == handle_vm_kvm_create_irqchip());
bsl::ut_check(SHIM_SUCCESS == handle_vm_kvm_create_irqchip(&mut_vm));
};
};
};
};

bsl::ut_scenario{"hypervisor not detected"} = []() noexcept {
bsl::ut_given{} = [&]() noexcept {
shim_vm_t mut_vm{};
bsl::ut_when{} = [&]() noexcept {
g_mut_hypervisor_detected = false;
bsl::ut_then{} = [&]() noexcept {
bsl::ut_check(SHIM_FAILURE == handle_vm_kvm_create_irqchip(&mut_vm));
};
bsl::ut_cleanup{} = [&]() noexcept {
g_mut_hypervisor_detected = true;
};
};
};
};
return bsl::ut_success();
}
}
Expand Down

0 comments on commit acbfdf1

Please sign in to comment.