xref: /kvm-unit-tests/x86/amd_sev.c (revision b643ae6207dafcad8ad99f0d8a2ca8245c2ce993)
1 /*
2  * AMD SEV test cases
3  *
4  * Copyright (c) 2021, Google Inc
5  *
6  * Authors:
7  *   Hyunwook (Wooky) Baek <baekhw@google.com>
8  *   Zixuan Wang <zixuanwang@google.com>
9  *
10  * SPDX-License-Identifier: LGPL-2.0-or-later
11  */
12 
13 #include "libcflat.h"
14 #include "x86/processor.h"
15 #include "x86/amd_sev.h"
16 #include "msr.h"
17 
18 #define TESTDEV_IO_PORT 0xe0
19 
20 static char st1[] = "abcdefghijklmnop";
21 
22 static void test_sev_es_activation(void)
23 {
24 	if (rdmsr(MSR_SEV_STATUS) & SEV_ES_ENABLED_MASK) {
25 		printf("SEV-ES is enabled.\n");
26 	} else {
27 		printf("SEV-ES is not enabled.\n");
28 	}
29 }
30 
31 static void test_stringio(void)
32 {
33 	int st1_len = sizeof(st1) - 1;
34 	u16 got;
35 
36 	asm volatile("cld \n\t"
37 		     "movw %0, %%dx \n\t"
38 		     "rep outsw \n\t"
39 		     : : "i"((short)TESTDEV_IO_PORT),
40 		         "S"(st1), "c"(st1_len / 2));
41 
42 	asm volatile("inw %1, %0\n\t" : "=a"(got) : "i"((short)TESTDEV_IO_PORT));
43 
44 	report((got & 0xff) == st1[sizeof(st1) - 3], "outsb nearly up");
45 	report((got & 0xff00) >> 8 == st1[sizeof(st1) - 2], "outsb up");
46 }
47 
48 int main(void)
49 {
50 	if (!amd_sev_enabled()) {
51 		report_skip("AMD SEV not enabled\n");
52 		goto out;
53 	}
54 
55 	test_sev_es_activation();
56 	test_stringio();
57 
58 out:
59 	return report_summary();
60 }
61