linux/tools/testing/selftests/kvm/x86_64/vmx_set_nested_state_test.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * vmx_set_nested_state_test
   4 *
   5 * Copyright (C) 2019, Google LLC.
   6 *
   7 * This test verifies the integrity of calling the ioctl KVM_SET_NESTED_STATE.
   8 */
   9
  10#include "test_util.h"
  11#include "kvm_util.h"
  12#include "processor.h"
  13#include "vmx.h"
  14
  15#include <errno.h>
  16#include <linux/kvm.h>
  17#include <string.h>
  18#include <sys/ioctl.h>
  19#include <unistd.h>
  20
  21/*
  22 * Mirror of VMCS12_REVISION in arch/x86/kvm/vmx/vmcs12.h. If that value
  23 * changes this should be updated.
  24 */
  25#define VMCS12_REVISION 0x11e57ed0
  26#define VCPU_ID 5
  27
  28void test_nested_state(struct kvm_vm *vm, struct kvm_nested_state *state)
  29{
  30        volatile struct kvm_run *run;
  31
  32        vcpu_nested_state_set(vm, VCPU_ID, state, false);
  33        run = vcpu_state(vm, VCPU_ID);
  34        vcpu_run(vm, VCPU_ID);
  35        TEST_ASSERT(run->exit_reason == KVM_EXIT_SHUTDOWN,
  36                "Got exit_reason other than KVM_EXIT_SHUTDOWN: %u (%s),\n",
  37                run->exit_reason,
  38                exit_reason_str(run->exit_reason));
  39}
  40
  41void test_nested_state_expect_errno(struct kvm_vm *vm,
  42                                    struct kvm_nested_state *state,
  43                                    int expected_errno)
  44{
  45        volatile struct kvm_run *run;
  46        int rv;
  47
  48        rv = vcpu_nested_state_set(vm, VCPU_ID, state, true);
  49        TEST_ASSERT(rv == -1 && errno == expected_errno,
  50                "Expected %s (%d) from vcpu_nested_state_set but got rv: %i errno: %s (%d)",
  51                strerror(expected_errno), expected_errno, rv, strerror(errno),
  52                errno);
  53        run = vcpu_state(vm, VCPU_ID);
  54        vcpu_run(vm, VCPU_ID);
  55        TEST_ASSERT(run->exit_reason == KVM_EXIT_SHUTDOWN,
  56                "Got exit_reason other than KVM_EXIT_SHUTDOWN: %u (%s),\n",
  57                run->exit_reason,
  58                exit_reason_str(run->exit_reason));
  59}
  60
  61void test_nested_state_expect_einval(struct kvm_vm *vm,
  62                                     struct kvm_nested_state *state)
  63{
  64        test_nested_state_expect_errno(vm, state, EINVAL);
  65}
  66
  67void test_nested_state_expect_efault(struct kvm_vm *vm,
  68                                     struct kvm_nested_state *state)
  69{
  70        test_nested_state_expect_errno(vm, state, EFAULT);
  71}
  72
  73void set_revision_id_for_vmcs12(struct kvm_nested_state *state,
  74                                u32 vmcs12_revision)
  75{
  76        /* Set revision_id in vmcs12 to vmcs12_revision. */
  77        memcpy(&state->data, &vmcs12_revision, sizeof(u32));
  78}
  79
  80void set_default_state(struct kvm_nested_state *state)
  81{
  82        memset(state, 0, sizeof(*state));
  83        state->flags = KVM_STATE_NESTED_RUN_PENDING |
  84                       KVM_STATE_NESTED_GUEST_MODE;
  85        state->format = 0;
  86        state->size = sizeof(*state);
  87}
  88
  89void set_default_vmx_state(struct kvm_nested_state *state, int size)
  90{
  91        memset(state, 0, size);
  92        state->flags = KVM_STATE_NESTED_GUEST_MODE  |
  93                        KVM_STATE_NESTED_RUN_PENDING |
  94                        KVM_STATE_NESTED_EVMCS;
  95        state->format = 0;
  96        state->size = size;
  97        state->hdr.vmx.vmxon_pa = 0x1000;
  98        state->hdr.vmx.vmcs12_pa = 0x2000;
  99        state->hdr.vmx.smm.flags = 0;
 100        set_revision_id_for_vmcs12(state, VMCS12_REVISION);
 101}
 102
 103void test_vmx_nested_state(struct kvm_vm *vm)
 104{
 105        /* Add a page for VMCS12. */
 106        const int state_sz = sizeof(struct kvm_nested_state) + getpagesize();
 107        struct kvm_nested_state *state =
 108                (struct kvm_nested_state *)malloc(state_sz);
 109
 110        /* The format must be set to 0. 0 for VMX, 1 for SVM. */
 111        set_default_vmx_state(state, state_sz);
 112        state->format = 1;
 113        test_nested_state_expect_einval(vm, state);
 114
 115        /*
 116         * We cannot virtualize anything if the guest does not have VMX
 117         * enabled.
 118         */
 119        set_default_vmx_state(state, state_sz);
 120        test_nested_state_expect_einval(vm, state);
 121
 122        /*
 123         * We cannot virtualize anything if the guest does not have VMX
 124         * enabled.  We expect KVM_SET_NESTED_STATE to return 0 if vmxon_pa
 125         * is set to -1ull, but the flags must be zero.
 126         */
 127        set_default_vmx_state(state, state_sz);
 128        state->hdr.vmx.vmxon_pa = -1ull;
 129        test_nested_state_expect_einval(vm, state);
 130
 131        state->hdr.vmx.vmcs12_pa = -1ull;
 132        state->flags = KVM_STATE_NESTED_EVMCS;
 133        test_nested_state_expect_einval(vm, state);
 134
 135        state->flags = 0;
 136        test_nested_state(vm, state);
 137
 138        /* Enable VMX in the guest CPUID. */
 139        vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
 140
 141        /*
 142         * Setting vmxon_pa == -1ull and vmcs_pa == -1ull exits early without
 143         * setting the nested state but flags other than eVMCS must be clear.
 144         */
 145        set_default_vmx_state(state, state_sz);
 146        state->hdr.vmx.vmxon_pa = -1ull;
 147        state->hdr.vmx.vmcs12_pa = -1ull;
 148        test_nested_state_expect_einval(vm, state);
 149
 150        state->flags = KVM_STATE_NESTED_EVMCS;
 151        test_nested_state(vm, state);
 152
 153        /* It is invalid to have vmxon_pa == -1ull and SMM flags non-zero. */
 154        state->hdr.vmx.smm.flags = 1;
 155        test_nested_state_expect_einval(vm, state);
 156
 157        /* It is invalid to have vmxon_pa == -1ull and vmcs_pa != -1ull. */
 158        set_default_vmx_state(state, state_sz);
 159        state->hdr.vmx.vmxon_pa = -1ull;
 160        state->flags = 0;
 161        test_nested_state_expect_einval(vm, state);
 162
 163        /* It is invalid to have vmxon_pa set to a non-page aligned address. */
 164        set_default_vmx_state(state, state_sz);
 165        state->hdr.vmx.vmxon_pa = 1;
 166        test_nested_state_expect_einval(vm, state);
 167
 168        /*
 169         * It is invalid to have KVM_STATE_NESTED_SMM_GUEST_MODE and
 170         * KVM_STATE_NESTED_GUEST_MODE set together.
 171         */
 172        set_default_vmx_state(state, state_sz);
 173        state->flags = KVM_STATE_NESTED_GUEST_MODE  |
 174                      KVM_STATE_NESTED_RUN_PENDING;
 175        state->hdr.vmx.smm.flags = KVM_STATE_NESTED_SMM_GUEST_MODE;
 176        test_nested_state_expect_einval(vm, state);
 177
 178        /*
 179         * It is invalid to have any of the SMM flags set besides:
 180         *      KVM_STATE_NESTED_SMM_GUEST_MODE
 181         *      KVM_STATE_NESTED_SMM_VMXON
 182         */
 183        set_default_vmx_state(state, state_sz);
 184        state->hdr.vmx.smm.flags = ~(KVM_STATE_NESTED_SMM_GUEST_MODE |
 185                                KVM_STATE_NESTED_SMM_VMXON);
 186        test_nested_state_expect_einval(vm, state);
 187
 188        /* Outside SMM, SMM flags must be zero. */
 189        set_default_vmx_state(state, state_sz);
 190        state->flags = 0;
 191        state->hdr.vmx.smm.flags = KVM_STATE_NESTED_SMM_GUEST_MODE;
 192        test_nested_state_expect_einval(vm, state);
 193
 194        /* Size must be large enough to fit kvm_nested_state and vmcs12. */
 195        set_default_vmx_state(state, state_sz);
 196        state->size = sizeof(*state);
 197        test_nested_state(vm, state);
 198
 199        /* vmxon_pa cannot be the same address as vmcs_pa. */
 200        set_default_vmx_state(state, state_sz);
 201        state->hdr.vmx.vmxon_pa = 0;
 202        state->hdr.vmx.vmcs12_pa = 0;
 203        test_nested_state_expect_einval(vm, state);
 204
 205        /* The revision id for vmcs12 must be VMCS12_REVISION. */
 206        set_default_vmx_state(state, state_sz);
 207        set_revision_id_for_vmcs12(state, 0);
 208        test_nested_state_expect_einval(vm, state);
 209
 210        /*
 211         * Test that if we leave nesting the state reflects that when we get
 212         * it again.
 213         */
 214        set_default_vmx_state(state, state_sz);
 215        state->hdr.vmx.vmxon_pa = -1ull;
 216        state->hdr.vmx.vmcs12_pa = -1ull;
 217        state->flags = 0;
 218        test_nested_state(vm, state);
 219        vcpu_nested_state_get(vm, VCPU_ID, state);
 220        TEST_ASSERT(state->size >= sizeof(*state) && state->size <= state_sz,
 221                    "Size must be between %d and %d.  The size returned was %d.",
 222                    sizeof(*state), state_sz, state->size);
 223        TEST_ASSERT(state->hdr.vmx.vmxon_pa == -1ull, "vmxon_pa must be -1ull.");
 224        TEST_ASSERT(state->hdr.vmx.vmcs12_pa == -1ull, "vmcs_pa must be -1ull.");
 225
 226        free(state);
 227}
 228
 229int main(int argc, char *argv[])
 230{
 231        struct kvm_vm *vm;
 232        struct kvm_nested_state state;
 233        struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
 234
 235        if (!kvm_check_cap(KVM_CAP_NESTED_STATE)) {
 236                printf("KVM_CAP_NESTED_STATE not available, skipping test\n");
 237                exit(KSFT_SKIP);
 238        }
 239
 240        /*
 241         * AMD currently does not implement set_nested_state, so for now we
 242         * just early out.
 243         */
 244        if (!(entry->ecx & CPUID_VMX)) {
 245                fprintf(stderr, "nested VMX not enabled, skipping test\n");
 246                exit(KSFT_SKIP);
 247        }
 248
 249        vm = vm_create_default(VCPU_ID, 0, 0);
 250
 251        /* Passing a NULL kvm_nested_state causes a EFAULT. */
 252        test_nested_state_expect_efault(vm, NULL);
 253
 254        /* 'size' cannot be smaller than sizeof(kvm_nested_state). */
 255        set_default_state(&state);
 256        state.size = 0;
 257        test_nested_state_expect_einval(vm, &state);
 258
 259        /*
 260         * Setting the flags 0xf fails the flags check.  The only flags that
 261         * can be used are:
 262         *     KVM_STATE_NESTED_GUEST_MODE
 263         *     KVM_STATE_NESTED_RUN_PENDING
 264         *     KVM_STATE_NESTED_EVMCS
 265         */
 266        set_default_state(&state);
 267        state.flags = 0xf;
 268        test_nested_state_expect_einval(vm, &state);
 269
 270        /*
 271         * If KVM_STATE_NESTED_RUN_PENDING is set then
 272         * KVM_STATE_NESTED_GUEST_MODE has to be set as well.
 273         */
 274        set_default_state(&state);
 275        state.flags = KVM_STATE_NESTED_RUN_PENDING;
 276        test_nested_state_expect_einval(vm, &state);
 277
 278        /*
 279         * TODO: When SVM support is added for KVM_SET_NESTED_STATE
 280         *       add tests here to support it like VMX.
 281         */
 282        if (entry->ecx & CPUID_VMX)
 283                test_vmx_nested_state(vm);
 284
 285        kvm_vm_free(vm);
 286        return 0;
 287}
 288