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

shim: Initial support for KVM_SET_GSI_ROUTING #122

Draft
wants to merge 1 commit into
base: mk-irqchip
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -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
}
Expand Down
108 changes: 106 additions & 2 deletions shim/include/kvm_irq_routing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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)
Expand Down
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
Expand Up @@ -56,3 +56,4 @@ microv_add_shim_integration(kvm_set_irqchip HEADERS)
microv_add_shim_integration(kvm_get_lapic HEADERS)
microv_add_shim_integration(kvm_set_lapic HEADERS)
microv_add_shim_integration(kvm_irqfd 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
Expand Up @@ -31,6 +31,7 @@
#include <kvm_cpuid2.hpp>
#include <kvm_cpuid_entry2.hpp>
#include <kvm_fpu.hpp>
#include <kvm_irq_routing.hpp>
#include <kvm_irqchip.hpp>
#include <kvm_irqfd.hpp>
#include <kvm_mp_state.hpp>
Expand Down Expand Up @@ -216,8 +217,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
Expand Down
23 changes: 20 additions & 3 deletions shim/linux/src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include <handle_vm_kvm_destroy_vcpu.h>
#include <handle_vm_kvm_get_irqchip.h>
#include <handle_vm_kvm_irqfd.h>
#include <handle_vm_kvm_set_gsi_routing.h>
#include <handle_vm_kvm_set_irqchip.h>
#include <handle_vm_kvm_set_user_memory_region.h>
#include <linux/anon_inodes.h>
Expand Down Expand Up @@ -723,10 +724,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
Expand Down Expand Up @@ -952,7 +967,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: {
Expand Down
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
Expand Up @@ -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;
}
Loading