Skip to content

Commit 747bd75

Browse files
authored
Merge pull request #117 from softickllc/setgetlapic
shim: Initial support for KVM_{GET,SET}_LAPIC
2 parents 9322e38 + 6920556 commit 747bd75

13 files changed

+308
-28
lines changed

shim/include/handle_vcpu_kvm_get_lapic.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <kvm_lapic_state.h>
3131
#include <mv_types.h>
32+
#include <shim_vcpu_t.h>
3233

3334
#ifdef __cplusplus
3435
extern "C"
@@ -40,11 +41,13 @@ extern "C"
4041
* @brief Handles the execution of kvm_get_lapic.
4142
*
4243
* <!-- inputs/outputs -->
43-
* @param pmut_ioctl_args the arguments provided by userspace
44+
* @param vcpu arguments received from private data
45+
* @param pmut_ioctl_args returns the virtual lapic args
4446
* @return SHIM_SUCCESS on success, SHIM_FAILURE on failure.
4547
*/
46-
NODISCARD int64_t
47-
handle_vcpu_kvm_get_lapic(struct kvm_lapic_state *const pmut_ioctl_args) NOEXCEPT;
48+
NODISCARD int64_t handle_vcpu_kvm_get_lapic(
49+
struct shim_vcpu_t const *const vcpu,
50+
struct kvm_lapic_state *const pmut_ioctl_args) NOEXCEPT;
4851

4952
#ifdef __cplusplus
5053
}

shim/include/handle_vcpu_kvm_set_lapic.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <kvm_lapic_state.h>
3131
#include <mv_types.h>
32+
#include <shim_vcpu_t.h>
3233

3334
#ifdef __cplusplus
3435
extern "C"
@@ -40,11 +41,13 @@ extern "C"
4041
* @brief Handles the execution of kvm_set_lapic.
4142
*
4243
* <!-- inputs/outputs -->
44+
* @param vcpu arguments received from private data
4345
* @param pmut_ioctl_args the arguments provided by userspace
4446
* @return SHIM_SUCCESS on success, SHIM_FAILURE on failure.
4547
*/
46-
NODISCARD int64_t
47-
handle_vcpu_kvm_set_lapic(struct kvm_lapic_state *const pmut_ioctl_args) NOEXCEPT;
48+
NODISCARD int64_t handle_vcpu_kvm_set_lapic(
49+
struct shim_vcpu_t const *const vcpu,
50+
struct kvm_lapic_state *const pmut_ioctl_args) NOEXCEPT;
4851

4952
#ifdef __cplusplus
5053
}

shim/include/kvm_lapic_state.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#ifndef KVM_LAPIC_STATE_H
2828
#define KVM_LAPIC_STATE_H
2929

30+
#define KVM_APIC_REG_SIZE 1024
3031
#include <stdint.h>
3132

3233
#ifdef __cplusplus
@@ -44,8 +45,9 @@ extern "C"
4445
*/
4546
struct kvm_lapic_state
4647
{
47-
/** @brief replace me with contents from KVM API */
48-
int32_t dummy;
48+
49+
/** @brief the registers of lapic to set or get */
50+
char regs[KVM_APIC_REG_SIZE];
4951
};
5052

5153
#pragma pack(pop)

shim/include/kvm_lapic_state.hpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @copyright
3+
* Copyright (C) 2020 Assured Information Security, Inc.
4+
*
5+
* @copyright
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* @copyright
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* @copyright
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
27+
#ifndef KVM_LAPIC_STATE_HPP
28+
#define KVM_LAPIC_STATE_HPP
29+
30+
#include <bsl/array.hpp>
31+
#include <bsl/convert.hpp>
32+
#include <bsl/safe_integral.hpp>
33+
34+
#pragma pack(push, 1)
35+
36+
namespace shim
37+
{
38+
/// @brief defines the size of the padding3 field
39+
constexpr auto KVM_APIC_REG_SIZE{1024_umx};
40+
/// @struct kvm_lapic_state
41+
///
42+
/// <!-- description -->
43+
/// @brief see /include/uapi/linux/kvm.h in Linux for more details.
44+
///
45+
struct kvm_lapic_state final
46+
{
47+
/// @brief TODO
48+
bsl::array<bsl::uint8, KVM_APIC_REG_SIZE.get()> regs;
49+
};
50+
51+
}
52+
53+
#pragma pack(pop)
54+
55+
#endif

shim/integration/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ microv_add_shim_integration(kvm_set_msrs HEADERS)
5353
microv_add_shim_integration(kvm_create_irqchip HEADERS)
5454
microv_add_shim_integration(kvm_get_irqchip HEADERS)
5555
microv_add_shim_integration(kvm_set_irqchip HEADERS)
56+
microv_add_shim_integration(kvm_get_lapic HEADERS)
57+
microv_add_shim_integration(kvm_set_lapic HEADERS)

shim/integration/kvm_get_lapic.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/// @copyright
2+
/// Copyright (C) 2020 Assured Information Security, Inc.
3+
///
4+
/// @copyright
5+
/// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
/// of this software and associated documentation files (the "Software"), to deal
7+
/// in the Software without restriction, including without limitation the rights
8+
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
/// copies of the Software, and to permit persons to whom the Software is
10+
/// furnished to do so, subject to the following conditions:
11+
///
12+
/// @copyright
13+
/// The above copyright notice and this permission notice shall be included in
14+
/// all copies or substantial portions of the Software.
15+
///
16+
/// @copyright
17+
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
/// SOFTWARE.
24+
25+
#include <integration_utils.hpp>
26+
#include <ioctl_t.hpp>
27+
#include <shim_platform_interface.hpp>
28+
29+
#include <bsl/convert.hpp>
30+
#include <bsl/enable_color.hpp>
31+
#include <bsl/exit_code.hpp>
32+
#include <bsl/safe_integral.hpp>
33+
34+
/// <!-- description -->
35+
/// @brief Provides the main entry point for this application.
36+
///
37+
/// <!-- inputs/outputs -->
38+
/// @return bsl::exit_success on success, bsl::exit_failure otherwise.
39+
///
40+
[[nodiscard]] auto
41+
main() noexcept -> bsl::exit_code
42+
{
43+
bsl::enable_color();
44+
integration::ioctl_t mut_system_ctl{shim::DEVICE_NAME};
45+
auto const vmfd{mut_system_ctl.send(shim::KVM_CREATE_VM)};
46+
integration::ioctl_t mut_vm{bsl::to_i32(vmfd)};
47+
auto const vcpufd{mut_vm.send(shim::KVM_CREATE_VCPU)};
48+
integration::ioctl_t mut_vcpu{bsl::to_i32(vcpufd)};
49+
constexpr auto mut_ret{0_i64};
50+
{
51+
auto const lapic{mut_vcpu.send(shim::KVM_GET_LAPIC)};
52+
integration::verify(lapic.is_pos());
53+
integration::verify(lapic >= mut_ret.get());
54+
}
55+
return bsl::exit_success;
56+
}

shim/integration/kvm_set_lapic.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/// @copyright
2+
/// Copyright (C) 2020 Assured Information Security, Inc.
3+
///
4+
/// @copyright
5+
/// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
/// of this software and associated documentation files (the "Software"), to deal
7+
/// in the Software without restriction, including without limitation the rights
8+
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
/// copies of the Software, and to permit persons to whom the Software is
10+
/// furnished to do so, subject to the following conditions:
11+
///
12+
/// @copyright
13+
/// The above copyright notice and this permission notice shall be included in
14+
/// all copies or substantial portions of the Software.
15+
///
16+
/// @copyright
17+
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
/// SOFTWARE.
24+
25+
#include <integration_utils.hpp>
26+
#include <ioctl_t.hpp>
27+
#include <shim_platform_interface.hpp>
28+
29+
#include <bsl/convert.hpp>
30+
#include <bsl/enable_color.hpp>
31+
#include <bsl/exit_code.hpp>
32+
#include <bsl/safe_integral.hpp>
33+
34+
/// <!-- description -->
35+
/// @brief Provides the main entry point for this application.
36+
///
37+
/// <!-- inputs/outputs -->
38+
/// @return bsl::exit_success on success, bsl::exit_failure otherwise.
39+
///
40+
[[nodiscard]] auto
41+
main() noexcept -> bsl::exit_code
42+
{
43+
bsl::enable_color();
44+
integration::ioctl_t mut_system_ctl{shim::DEVICE_NAME};
45+
auto const vmfd{mut_system_ctl.send(shim::KVM_CREATE_VM)};
46+
integration::ioctl_t mut_vm{bsl::to_i32(vmfd)};
47+
auto const vcpufd{mut_vm.send(shim::KVM_CREATE_VCPU)};
48+
integration::ioctl_t mut_vcpu{bsl::to_i32(vcpufd)};
49+
constexpr auto mut_ret{0_i64};
50+
{
51+
auto const lapic{mut_vcpu.send(shim::KVM_SET_LAPIC)};
52+
integration::verify(lapic.is_pos());
53+
integration::verify(lapic >= mut_ret.get());
54+
}
55+
return bsl::exit_success;
56+
}

shim/linux/include/platform_interface/shim_platform_interface.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
// #include <kvm_irq_level.hpp>
6464
// #include <kvm_irq_routing.hpp>
6565
// #include <kvm_irqfd.hpp>
66-
// #include <kvm_lapic_state.hpp>
66+
#include <kvm_lapic_state.hpp>
6767
// #include <kvm_mp_state.hpp>
6868
// #include <kvm_msi.hpp>
6969
// #include <kvm_msrs.hpp>
@@ -221,10 +221,12 @@ namespace shim
221221
constexpr bsl::safe_umx KVM_GET_TSC_KHZ{static_cast<bsl::uintmx>(_IO(SHIMIO.get(), 0xa3))};
222222
/// @brief defines KVM's KVM_SET_TSC_KHZ IOCTL
223223
constexpr bsl::safe_umx KVM_SET_TSC_KHZ{static_cast<bsl::uintmx>(_IO(SHIMIO.get(), 0xa2))};
224-
// /// @brief defines KVM's KVM_GET_LAPIC IOCTL
225-
// constexpr bsl::safe_umx KVM_GET_LAPIC{static_cast<bsl::uintmx>(_IOR(SHIMIO.get(), 0x8e, struct kvm_lapic_state))};
226-
// /// @brief defines KVM's KVM_SET_LAPIC IOCTL
227-
// constexpr bsl::safe_umx KVM_SET_LAPIC{static_cast<bsl::uintmx>(_IOW(SHIMIO.get(), 0x8f, struct kvm_lapic_state))};
224+
/// @brief defines KVM's KVM_GET_LAPIC IOCTL
225+
constexpr bsl::safe_umx KVM_GET_LAPIC{
226+
static_cast<bsl::uintmx>(_IOR(SHIMIO.get(), 0x8e, struct kvm_lapic_state))};
227+
/// @brief defines KVM's KVM_SET_LAPIC IOCTL
228+
constexpr bsl::safe_umx KVM_SET_LAPIC{
229+
static_cast<bsl::uintmx>(_IOW(SHIMIO.get(), 0x8f, struct kvm_lapic_state))};
228230
// /// @brief defines KVM's KVM_IOEVENTFD IOCTL
229231
// constexpr bsl::safe_umx KVM_IOEVENTFD{static_cast<bsl::uintmx>(_IOW(SHIMIO.get(), 0x79, struct kvm_ioeventfd))};
230232
/// @brief defines KVM's KVM_NMI IOCTL

shim/linux/src/entry.c

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@
3636
#include <handle_system_kvm_get_supported_cpuid.h>
3737
#include <handle_system_kvm_get_vcpu_mmap_size.h>
3838
#include <handle_vcpu_kvm_get_fpu.h>
39+
#include <handle_vcpu_kvm_get_lapic.h>
3940
#include <handle_vcpu_kvm_get_mp_state.h>
4041
#include <handle_vcpu_kvm_get_msrs.h>
4142
#include <handle_vcpu_kvm_get_regs.h>
4243
#include <handle_vcpu_kvm_get_sregs.h>
4344
#include <handle_vcpu_kvm_get_tsc_khz.h>
4445
#include <handle_vcpu_kvm_run.h>
4546
#include <handle_vcpu_kvm_set_fpu.h>
47+
#include <handle_vcpu_kvm_set_lapic.h>
4648
#include <handle_vcpu_kvm_set_mp_state.h>
4749
#include <handle_vcpu_kvm_set_msrs.h>
4850
#include <handle_vcpu_kvm_set_regs.h>
@@ -1029,10 +1031,27 @@ dispatch_vcpu_kvm_get_fpu(
10291031
}
10301032

10311033
static long
1032-
dispatch_vcpu_kvm_get_lapic(struct kvm_lapic_state *const ioctl_args)
1034+
dispatch_vcpu_kvm_get_lapic(
1035+
struct shim_vcpu_t const *const vcpu,
1036+
struct kvm_lapic_state *const user_args)
10331037
{
1034-
(void)ioctl_args;
1035-
return -EINVAL;
1038+
struct kvm_lapic_state mut_args;
1039+
1040+
if (NULL == user_args) {
1041+
bferror("user_args are null");
1042+
return -EINVAL;
1043+
}
1044+
1045+
if (handle_vcpu_kvm_get_lapic(vcpu, &mut_args)) {
1046+
bferror("handle_vcpu_kvm_get_lapic failed");
1047+
return -EINVAL;
1048+
}
1049+
1050+
if (platform_copy_to_user(user_args, &mut_args, sizeof(mut_args))) {
1051+
bferror("platform_copy_to_user failed");
1052+
return -EINVAL;
1053+
}
1054+
return 0;
10361055
}
10371056

10381057
static long
@@ -1283,10 +1302,28 @@ dispatch_vcpu_kvm_set_guest_debug(struct kvm_guest_debug *const ioctl_args)
12831302
}
12841303

12851304
static long
1286-
dispatch_vcpu_kvm_set_lapic(struct kvm_lapic_state *const ioctl_args)
1305+
dispatch_vcpu_kvm_set_lapic(
1306+
struct shim_vcpu_t const *const vcpu,
1307+
struct kvm_lapic_state *const user_args)
12871308
{
1288-
(void)ioctl_args;
1289-
return -EINVAL;
1309+
struct kvm_lapic_state mut_args;
1310+
1311+
if (NULL == user_args) {
1312+
bferror("user_args are null");
1313+
return -EINVAL;
1314+
}
1315+
1316+
if (platform_copy_from_user(&mut_args, user_args, sizeof(mut_args))) {
1317+
bferror("platform_copy_from_user failed");
1318+
return -EINVAL;
1319+
}
1320+
1321+
if (handle_vcpu_kvm_set_lapic(vcpu, &mut_args)) {
1322+
bferror("handle_vcpu_kvm_set_lapic failed");
1323+
return -EINVAL;
1324+
}
1325+
1326+
return 0;
12901327
}
12911328

12921329
static long
@@ -1504,7 +1541,7 @@ dev_unlocked_ioctl_vcpu(
15041541

15051542
case KVM_GET_LAPIC: {
15061543
return dispatch_vcpu_kvm_get_lapic(
1507-
(struct kvm_lapic_state *)ioctl_args);
1544+
pmut_mut_vcpu, (struct kvm_lapic_state *)ioctl_args);
15081545
}
15091546

15101547
case KVM_GET_MP_STATE: {
@@ -1601,7 +1638,7 @@ dev_unlocked_ioctl_vcpu(
16011638

16021639
case KVM_SET_LAPIC: {
16031640
return dispatch_vcpu_kvm_set_lapic(
1604-
(struct kvm_lapic_state *)ioctl_args);
1641+
pmut_mut_vcpu, (struct kvm_lapic_state *)ioctl_args);
16051642
}
16061643

16071644
case KVM_SET_MP_STATE: {

0 commit comments

Comments
 (0)