Skip to content

Commit

Permalink
Merge pull request #117 from softickllc/setgetlapic
Browse files Browse the repository at this point in the history
shim: Initial support for KVM_{GET,SET}_LAPIC
  • Loading branch information
chp-io authored Feb 28, 2022
2 parents 9322e38 + 6920556 commit 747bd75
Show file tree
Hide file tree
Showing 13 changed files with 308 additions and 28 deletions.
9 changes: 6 additions & 3 deletions shim/include/handle_vcpu_kvm_get_lapic.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

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

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

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

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

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

#ifdef __cplusplus
}
Expand Down
6 changes: 4 additions & 2 deletions shim/include/kvm_lapic_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#ifndef KVM_LAPIC_STATE_H
#define KVM_LAPIC_STATE_H

#define KVM_APIC_REG_SIZE 1024
#include <stdint.h>

#ifdef __cplusplus
Expand All @@ -44,8 +45,9 @@ extern "C"
*/
struct kvm_lapic_state
{
/** @brief replace me with contents from KVM API */
int32_t dummy;

/** @brief the registers of lapic to set or get */
char regs[KVM_APIC_REG_SIZE];
};

#pragma pack(pop)
Expand Down
55 changes: 55 additions & 0 deletions shim/include/kvm_lapic_state.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @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_LAPIC_STATE_HPP
#define KVM_LAPIC_STATE_HPP

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

#pragma pack(push, 1)

namespace shim
{
/// @brief defines the size of the padding3 field
constexpr auto KVM_APIC_REG_SIZE{1024_umx};
/// @struct kvm_lapic_state
///
/// <!-- description -->
/// @brief see /include/uapi/linux/kvm.h in Linux for more details.
///
struct kvm_lapic_state final
{
/// @brief TODO
bsl::array<bsl::uint8, KVM_APIC_REG_SIZE.get()> regs;
};

}

#pragma pack(pop)

#endif
2 changes: 2 additions & 0 deletions shim/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ microv_add_shim_integration(kvm_set_msrs HEADERS)
microv_add_shim_integration(kvm_create_irqchip HEADERS)
microv_add_shim_integration(kvm_get_irqchip HEADERS)
microv_add_shim_integration(kvm_set_irqchip HEADERS)
microv_add_shim_integration(kvm_get_lapic HEADERS)
microv_add_shim_integration(kvm_set_lapic HEADERS)
56 changes: 56 additions & 0 deletions shim/integration/kvm_get_lapic.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 lapic{mut_vcpu.send(shim::KVM_GET_LAPIC)};
integration::verify(lapic.is_pos());
integration::verify(lapic >= mut_ret.get());
}
return bsl::exit_success;
}
56 changes: 56 additions & 0 deletions shim/integration/kvm_set_lapic.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 lapic{mut_vcpu.send(shim::KVM_SET_LAPIC)};
integration::verify(lapic.is_pos());
integration::verify(lapic >= mut_ret.get());
}
return bsl::exit_success;
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
// #include <kvm_irq_level.hpp>
// #include <kvm_irq_routing.hpp>
// #include <kvm_irqfd.hpp>
// #include <kvm_lapic_state.hpp>
#include <kvm_lapic_state.hpp>
// #include <kvm_mp_state.hpp>
// #include <kvm_msi.hpp>
// #include <kvm_msrs.hpp>
Expand Down Expand Up @@ -221,10 +221,12 @@ namespace shim
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
constexpr bsl::safe_umx KVM_SET_TSC_KHZ{static_cast<bsl::uintmx>(_IO(SHIMIO.get(), 0xa2))};
// /// @brief defines KVM's KVM_GET_LAPIC IOCTL
// constexpr bsl::safe_umx KVM_GET_LAPIC{static_cast<bsl::uintmx>(_IOR(SHIMIO.get(), 0x8e, struct kvm_lapic_state))};
// /// @brief defines KVM's KVM_SET_LAPIC IOCTL
// constexpr bsl::safe_umx KVM_SET_LAPIC{static_cast<bsl::uintmx>(_IOW(SHIMIO.get(), 0x8f, struct kvm_lapic_state))};
/// @brief defines KVM's KVM_GET_LAPIC IOCTL
constexpr bsl::safe_umx KVM_GET_LAPIC{
static_cast<bsl::uintmx>(_IOR(SHIMIO.get(), 0x8e, struct kvm_lapic_state))};
/// @brief defines KVM's KVM_SET_LAPIC IOCTL
constexpr bsl::safe_umx KVM_SET_LAPIC{
static_cast<bsl::uintmx>(_IOW(SHIMIO.get(), 0x8f, struct kvm_lapic_state))};
// /// @brief defines KVM's KVM_IOEVENTFD IOCTL
// constexpr bsl::safe_umx KVM_IOEVENTFD{static_cast<bsl::uintmx>(_IOW(SHIMIO.get(), 0x79, struct kvm_ioeventfd))};
/// @brief defines KVM's KVM_NMI IOCTL
Expand Down
53 changes: 45 additions & 8 deletions shim/linux/src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@
#include <handle_system_kvm_get_supported_cpuid.h>
#include <handle_system_kvm_get_vcpu_mmap_size.h>
#include <handle_vcpu_kvm_get_fpu.h>
#include <handle_vcpu_kvm_get_lapic.h>
#include <handle_vcpu_kvm_get_mp_state.h>
#include <handle_vcpu_kvm_get_msrs.h>
#include <handle_vcpu_kvm_get_regs.h>
#include <handle_vcpu_kvm_get_sregs.h>
#include <handle_vcpu_kvm_get_tsc_khz.h>
#include <handle_vcpu_kvm_run.h>
#include <handle_vcpu_kvm_set_fpu.h>
#include <handle_vcpu_kvm_set_lapic.h>
#include <handle_vcpu_kvm_set_mp_state.h>
#include <handle_vcpu_kvm_set_msrs.h>
#include <handle_vcpu_kvm_set_regs.h>
Expand Down Expand Up @@ -1029,10 +1031,27 @@ dispatch_vcpu_kvm_get_fpu(
}

static long
dispatch_vcpu_kvm_get_lapic(struct kvm_lapic_state *const ioctl_args)
dispatch_vcpu_kvm_get_lapic(
struct shim_vcpu_t const *const vcpu,
struct kvm_lapic_state *const user_args)
{
(void)ioctl_args;
return -EINVAL;
struct kvm_lapic_state mut_args;

if (NULL == user_args) {
bferror("user_args are null");
return -EINVAL;
}

if (handle_vcpu_kvm_get_lapic(vcpu, &mut_args)) {
bferror("handle_vcpu_kvm_get_lapic failed");
return -EINVAL;
}

if (platform_copy_to_user(user_args, &mut_args, sizeof(mut_args))) {
bferror("platform_copy_to_user failed");
return -EINVAL;
}
return 0;
}

static long
Expand Down Expand Up @@ -1283,10 +1302,28 @@ dispatch_vcpu_kvm_set_guest_debug(struct kvm_guest_debug *const ioctl_args)
}

static long
dispatch_vcpu_kvm_set_lapic(struct kvm_lapic_state *const ioctl_args)
dispatch_vcpu_kvm_set_lapic(
struct shim_vcpu_t const *const vcpu,
struct kvm_lapic_state *const user_args)
{
(void)ioctl_args;
return -EINVAL;
struct kvm_lapic_state mut_args;

if (NULL == user_args) {
bferror("user_args are null");
return -EINVAL;
}

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

if (handle_vcpu_kvm_set_lapic(vcpu, &mut_args)) {
bferror("handle_vcpu_kvm_set_lapic failed");
return -EINVAL;
}

return 0;
}

static long
Expand Down Expand Up @@ -1504,7 +1541,7 @@ dev_unlocked_ioctl_vcpu(

case KVM_GET_LAPIC: {
return dispatch_vcpu_kvm_get_lapic(
(struct kvm_lapic_state *)ioctl_args);
pmut_mut_vcpu, (struct kvm_lapic_state *)ioctl_args);
}

case KVM_GET_MP_STATE: {
Expand Down Expand Up @@ -1601,7 +1638,7 @@ dev_unlocked_ioctl_vcpu(

case KVM_SET_LAPIC: {
return dispatch_vcpu_kvm_set_lapic(
(struct kvm_lapic_state *)ioctl_args);
pmut_mut_vcpu, (struct kvm_lapic_state *)ioctl_args);
}

case KVM_SET_MP_STATE: {
Expand Down
Loading

0 comments on commit 747bd75

Please sign in to comment.