Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KVM_SET_GSI_ROUTING: Add scaffolding and initial integration tests
Browse files Browse the repository at this point in the history
softickllc committed Feb 10, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent acbfdf1 commit 5f90412
Showing 9 changed files with 284 additions and 14 deletions.
5 changes: 3 additions & 2 deletions shim/include/handle_vm_kvm_set_gsi_routing.h
Original file line number Diff line number Diff line change
@@ -40,11 +40,12 @@ extern "C"
* @brief Handles the execution of kvm_set_gsi_routing.
*
* <!-- inputs/outputs -->
* @param pmut_vm the argumento hold vm details of type shim_vm_t
* @param pmut_ioctl_args the arguments provided by userspace
* @return SHIM_SUCCESS on success, SHIM_FAILURE on failure.
*/
NODISCARD int64_t
handle_vm_kvm_set_gsi_routing(struct kvm_irq_routing *const pmut_ioctl_args) NOEXCEPT;
NODISCARD int64_t handle_vm_kvm_set_gsi_routing(
struct shim_vm_t *const pmut_vm, struct kvm_irq_routing *const pmut_ioctl_args) NOEXCEPT;

#ifdef __cplusplus
}
108 changes: 106 additions & 2 deletions shim/include/kvm_irq_routing.h
Original file line number Diff line number Diff line change
@@ -27,15 +27,115 @@
#ifndef KVM_IRQ_ROUTING_H
#define KVM_IRQ_ROUTING_H

#define KVM_ENTRY ((uint32_t)0)
#define KVM_PAD ((uint32_t)8)

#include <stdint.h>

#define KVM_IRQ_ROUTING_IRQCHIP 1
#define KVM_IRQ_ROUTING_MSI 2
#define KVM_IRQ_ROUTING_S390_ADAPTER 3

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

#ifdef __cplusplus
extern "C"
{
#endif

#pragma pack(push, 1)

/**
* @struct kvm_irq_routing_irqchip
*
* <!-- description -->
* @brief see /include/uapi/linux/kvm.h in Linux for more details.
*/
//NOLINTNEXTLINE
struct kvm_irq_routing_irqchip
{
/** @brief TODO */
uint32_t irqchip;
/** @brief TODO */
uint32_t pin;
};

/**
* @struct kvm_irq_routing_msi
*
* <!-- description -->
* @brief see /include/uapi/linux/kvm.h in Linux for more details.
*/
//NOLINTNEXTLINE
struct kvm_irq_routing_msi
{
/** @brief TODO */
uint32_t address_lo;
/** @brief TODO */
uint32_t address_hi;
/** @brief TODO */
uint32_t data;
/** @brief TODO */
uint32_t pad;
};

/**
* @struct kvm_irq_routing_s390_adapter
*
* <!-- description -->
* @brief see /include/uapi/linux/kvm.h in Linux for more details.
*/
//NOLINTNEXTLINE
struct kvm_irq_routing_s390_adapter
{
/** @brief TODO */
uint64_t ind_addr;
/** @brief TODO */
uint64_t summary_addr;
/** @brief TODO */
uint64_t ind_offset;
/** @brief TODO */
uint32_t summary_offset;
/** @brief TODO */
uint32_t adapter_id;
};

/**
* @struct kvm_irq_routing_entry
*
* <!-- description -->
* @brief see /include/uapi/linux/kvm.h in Linux for more details.
*/
struct kvm_irq_routing_entry //NOLINT
{
/** @brief TODO */
uint32_t gsi;
/** @brief TODO */
uint32_t type;
/** @brief TODO */
uint32_t flags;
/** @brief TODO */
uint32_t pad;
/** @brief TODO */
//NOLINTNEXTLINE
union
{
/** @brief TODO */
struct kvm_irq_routing_irqchip irqchip;
/** @brief TODO */
struct kvm_irq_routing_msi msi;
/** @brief TODO */
struct kvm_irq_routing_s390_adapter adapter;
/** @brief TODO */
uint32_t pad[KVM_PAD];
} /** @brief TODO */ u;
};
/** TODO: REMOVE ABOVE STRUCTURE: The above structures are part of other branches as of now,
* they will be available to include here once respective branches are merged with master. */

/**
* @struct kvm_irq_routing
*
@@ -44,8 +144,12 @@ extern "C"
*/
struct kvm_irq_routing
{
/** @brief replace me with contents from KVM API */
int32_t dummy;
/** @brief TODO */
uint32_t nr;
/** @brief TODO */
uint32_t flags;
/** @brief TODO */
struct kvm_irq_routing_entry entries[KVM_ENTRY];
};

#pragma pack(pop)
56 changes: 56 additions & 0 deletions shim/include/kvm_irq_routing.hpp
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.

#ifndef KVM_IRQ_ROUTING_HPP
#define KVM_IRQ_ROUTING_HPP

#include <bsl/safe_integral.hpp>

#pragma pack(push, 1)

namespace shim
{
/// @brief defines the size of the padding field
/// @struct kvm_irq_routing
///
/// <!-- description -->
/// @brief see /include/uapi/linux/kvm.h in Linux for more details.
///

struct kvm_irq_routing final
{

/** @brief TODO */
bsl::uint32 nr;
/** @brief TODO */
bsl::uint32 flags;
/** @brief TODO */
// TODO: Below structure to be uncommented when all other branches of IOCTL are merged with master.
//struct kvm_irq_routing_entry entries[KVM_ENTRY];
};
}

#pragma pack(pop)

#endif
1 change: 1 addition & 0 deletions shim/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -50,3 +50,4 @@ 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)
microv_add_shim_integration(kvm_irq_routing HEADERS)
56 changes: 56 additions & 0 deletions shim/integration/kvm_irq_routing.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 irqrouting{mut_vcpu.send(shim::KVM_SET_GSI_ROUTING)};
integration::verify(irqrouting.is_pos());
integration::verify(irqrouting >= mut_ret.get());
}
return bsl::exit_success;
}
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@
#include <kvm_cpuid2.hpp>
#include <kvm_cpuid_entry2.hpp>
#include <kvm_fpu.hpp>
#include <kvm_irq_routing.hpp>
#include <kvm_mp_state.hpp>
#include <kvm_msr_entry.hpp>
#include <kvm_msr_list.hpp>
@@ -212,8 +213,9 @@ namespace shim
/// @brief defines KVM's KVM_GET_SUPPORTED_CPUID IOCTL
constexpr bsl::safe_umx KVM_GET_SUPPORTED_CPUID{static_cast<bsl::uintmx>(_IOWR_LIST(
SHIMIO.get(), 0x05, struct kvm_cpuid2, struct kvm_cpuid_entry2[CPUID2_MAX_ENTRIES.get()]))};
// /// @brief defines KVM's KVM_SET_GSI_ROUTING IOCTL
// constexpr bsl::safe_umx KVM_SET_GSI_ROUTING{static_cast<bsl::uintmx>(_IOW(SHIMIO.get(), 0x6a, struct kvm_irq_routing))};
/// @brief defines KVM's KVM_SET_GSI_ROUTING IOCTL
constexpr bsl::safe_umx KVM_SET_GSI_ROUTING{
static_cast<bsl::uintmx>(_IOW(SHIMIO.get(), 0x6a, struct kvm_irq_routing))};
/// @brief defines KVM's KVM_GET_TSC_KHZ IOCTL
constexpr bsl::safe_umx KVM_GET_TSC_KHZ{static_cast<bsl::uintmx>(_IO(SHIMIO.get(), 0xa3))};
/// @brief defines KVM's KVM_SET_TSC_KHZ IOCTL
23 changes: 20 additions & 3 deletions shim/linux/src/entry.c
Original file line number Diff line number Diff line change
@@ -51,6 +51,7 @@
#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_gsi_routing.h>
#include <handle_vm_kvm_set_user_memory_region.h>
#include <linux/anon_inodes.h>
#include <linux/kernel.h>
@@ -676,10 +677,24 @@ dispatch_vm_kvm_set_device_attr(struct kvm_device_attr *const ioctl_args)
}

static long
dispatch_vm_kvm_set_gsi_routing(struct kvm_irq_routing *const ioctl_args)
dispatch_vm_kvm_set_gsi_routing(
struct shim_vm_t *const pmut_vm,
struct kvm_irq_routing *const pmut_ioctl_args)
{
(void)ioctl_args;
return -EINVAL;
struct kvm_irq_routing mut_args;
uint64_t const size = sizeof(mut_args);

if (platform_copy_from_user(&mut_args, pmut_ioctl_args, size)) {
bferror("platform_copy_from_user failed");
return -EINVAL;
}

if (handle_vm_kvm_set_gsi_routing(pmut_vm, &mut_args)) {
bferror("handle_vm_kvm_set_gsi_routing failed");
return -EINVAL;
}

return 0;
}

static long
@@ -886,7 +901,9 @@ dev_unlocked_ioctl_vm(

case KVM_SET_GSI_ROUTING: {
return dispatch_vm_kvm_set_gsi_routing(
(struct shim_vm_t *)pmut_mut_vm,
(struct kvm_irq_routing *)ioctl_args);
;
}

case KVM_SET_IDENTITY_MAP_ADDR: {
17 changes: 15 additions & 2 deletions shim/src/handle_vm_kvm_set_gsi_routing.c
Original file line number Diff line number Diff line change
@@ -24,20 +24,33 @@
* SOFTWARE.
*/

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

/**
* <!-- description -->
* @brief Handles the execution of kvm_set_gsi_routing.
*
* <!-- inputs/outputs -->
* @param pmut_vm the argumento hold vm details of type shim_vm_t
* @param pmut_ioctl_args the arguments provided by userspace
* @return SHIM_SUCCESS on success, SHIM_FAILURE on failure.
*/
NODISCARD int64_t
handle_vm_kvm_set_gsi_routing(struct kvm_irq_routing *const pmut_ioctl_args) NOEXCEPT
handle_vm_kvm_set_gsi_routing(
struct shim_vm_t *const pmut_vm, struct kvm_irq_routing *const pmut_ioctl_args) NOEXCEPT
{
(void)pmut_ioctl_args;
platform_expects(NULL != pmut_vm);
platform_expects(NULL != pmut_ioctl_args);

if (detect_hypervisor()) {
bferror("The shim is not running in a VM. Did you forget to start MicroV?");
return SHIM_FAILURE;
}
//TODO: Cally the hypercall here after its implementation
return SHIM_SUCCESS;
}
26 changes: 23 additions & 3 deletions shim/tests/src/test_handle_vm_kvm_set_gsi_routing.cpp
Original file line number Diff line number Diff line change
@@ -23,12 +23,12 @@
/// SOFTWARE.

#include "../../include/handle_vm_kvm_set_gsi_routing.h"
#include "shim_vm_t.h"

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

#include <bsl/ut.hpp>

namespace shim
{
/// <!-- description -->
@@ -43,12 +43,32 @@ namespace shim
[[nodiscard]] constexpr auto
tests() noexcept -> bsl::exit_code
{
init_tests();
bsl::ut_scenario{"description"} = []() noexcept {
bsl::ut_given{} = [&]() noexcept {
kvm_irq_routing mut_args{};
shim_vm_t mut_vm{};
bsl::ut_when{} = [&]() noexcept {
bsl::ut_then{} = [&]() noexcept {
bsl::ut_check(SHIM_SUCCESS == handle_vm_kvm_set_gsi_routing(&mut_args));
bsl::ut_check(
SHIM_SUCCESS == handle_vm_kvm_set_gsi_routing(&mut_vm, &mut_args));
};
};
};
};

bsl::ut_scenario{"hypervisor not detected"} = []() noexcept {
bsl::ut_given{} = [&]() noexcept {
kvm_irq_routing mut_args{};
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_set_gsi_routing(&mut_vm, &mut_args));
};
bsl::ut_cleanup{} = [&]() noexcept {
g_mut_hypervisor_detected = true;
};
};
};

0 comments on commit 5f90412

Please sign in to comment.