Skip to content

Commit a6fec53

Browse files
pgondabonzini
authored andcommitted
selftests: KVM: sev_migrate_tests: Add mirror command tests
Add tests to confirm mirror vms can only run correct subset of commands. Cc: Paolo Bonzini <[email protected]> Cc: Sean Christopherson <[email protected]> Cc: Marc Orr <[email protected]> Signed-off-by: Peter Gonda <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 427d046 commit a6fec53

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#define NR_LOCK_TESTING_THREADS 3
2222
#define NR_LOCK_TESTING_ITERATIONS 10000
2323

24-
static void sev_ioctl(int vm_fd, int cmd_id, void *data)
24+
static int __sev_ioctl(int vm_fd, int cmd_id, void *data, __u32 *fw_error)
2525
{
2626
struct kvm_sev_cmd cmd = {
2727
.id = cmd_id,
@@ -30,11 +30,20 @@ static void sev_ioctl(int vm_fd, int cmd_id, void *data)
3030
};
3131
int ret;
3232

33-
3433
ret = ioctl(vm_fd, KVM_MEMORY_ENCRYPT_OP, &cmd);
35-
TEST_ASSERT(ret == 0 && cmd.error == SEV_RET_SUCCESS,
34+
*fw_error = cmd.error;
35+
return ret;
36+
}
37+
38+
static void sev_ioctl(int vm_fd, int cmd_id, void *data)
39+
{
40+
int ret;
41+
__u32 fw_error;
42+
43+
ret = __sev_ioctl(vm_fd, cmd_id, data, &fw_error);
44+
TEST_ASSERT(ret == 0 && fw_error == SEV_RET_SUCCESS,
3645
"%d failed: return code: %d, errno: %d, fw error: %d",
37-
cmd_id, ret, errno, cmd.error);
46+
cmd_id, ret, errno, fw_error);
3847
}
3948

4049
static struct kvm_vm *sev_vm_create(bool es)
@@ -226,6 +235,42 @@ static void sev_mirror_create(int dst_fd, int src_fd)
226235
TEST_ASSERT(!ret, "Copying context failed, ret: %d, errno: %d\n", ret, errno);
227236
}
228237

238+
static void verify_mirror_allowed_cmds(int vm_fd)
239+
{
240+
struct kvm_sev_guest_status status;
241+
242+
for (int cmd_id = KVM_SEV_INIT; cmd_id < KVM_SEV_NR_MAX; ++cmd_id) {
243+
int ret;
244+
__u32 fw_error;
245+
246+
/*
247+
* These commands are allowed for mirror VMs, all others are
248+
* not.
249+
*/
250+
switch (cmd_id) {
251+
case KVM_SEV_LAUNCH_UPDATE_VMSA:
252+
case KVM_SEV_GUEST_STATUS:
253+
case KVM_SEV_DBG_DECRYPT:
254+
case KVM_SEV_DBG_ENCRYPT:
255+
continue;
256+
default:
257+
break;
258+
}
259+
260+
/*
261+
* These commands should be disallowed before the data
262+
* parameter is examined so NULL is OK here.
263+
*/
264+
ret = __sev_ioctl(vm_fd, cmd_id, NULL, &fw_error);
265+
TEST_ASSERT(
266+
ret == -1 && errno == EINVAL,
267+
"Should not be able call command: %d. ret: %d, errno: %d\n",
268+
cmd_id, ret, errno);
269+
}
270+
271+
sev_ioctl(vm_fd, KVM_SEV_GUEST_STATUS, &status);
272+
}
273+
229274
static void test_sev_mirror(bool es)
230275
{
231276
struct kvm_vm *src_vm, *dst_vm;
@@ -243,6 +288,8 @@ static void test_sev_mirror(bool es)
243288
if (es)
244289
sev_ioctl(dst_vm->fd, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL);
245290

291+
verify_mirror_allowed_cmds(dst_vm->fd);
292+
246293
kvm_vm_free(src_vm);
247294
kvm_vm_free(dst_vm);
248295
}

0 commit comments

Comments
 (0)