1010#include "test_util.h"
1111#include "kvm_util.h"
1212#include "processor.h"
13- #include "svm_util .h"
13+ #include "sev .h"
1414#include "kselftest.h"
1515
16- #define SEV_POLICY_ES 0b100
17-
1816#define NR_MIGRATE_TEST_VCPUS 4
1917#define NR_MIGRATE_TEST_VMS 3
2018#define NR_LOCK_TESTING_THREADS 3
2119#define NR_LOCK_TESTING_ITERATIONS 10000
2220
2321bool have_sev_es ;
2422
25- static int __sev_ioctl (int vm_fd , int cmd_id , void * data , __u32 * fw_error )
26- {
27- struct kvm_sev_cmd cmd = {
28- .id = cmd_id ,
29- .data = (uint64_t )data ,
30- .sev_fd = open_sev_dev_path_or_exit (),
31- };
32- int ret ;
33-
34- ret = ioctl (vm_fd , KVM_MEMORY_ENCRYPT_OP , & cmd );
35- * fw_error = cmd .error ;
36- return ret ;
37- }
38-
39- static void sev_ioctl (int vm_fd , int cmd_id , void * data )
40- {
41- int ret ;
42- __u32 fw_error ;
43-
44- ret = __sev_ioctl (vm_fd , cmd_id , data , & fw_error );
45- TEST_ASSERT (ret == 0 && fw_error == SEV_RET_SUCCESS ,
46- "%d failed: return code: %d, errno: %d, fw error: %d" ,
47- cmd_id , ret , errno , fw_error );
48- }
49-
5023static struct kvm_vm * sev_vm_create (bool es )
5124{
5225 struct kvm_vm * vm ;
53- struct kvm_sev_launch_start start = { 0 };
5426 int i ;
5527
5628 vm = vm_create_barebones ();
57- sev_ioctl (vm -> fd , es ? KVM_SEV_ES_INIT : KVM_SEV_INIT , NULL );
29+ if (!es )
30+ sev_vm_init (vm );
31+ else
32+ sev_es_vm_init (vm );
33+
5834 for (i = 0 ; i < NR_MIGRATE_TEST_VCPUS ; ++ i )
5935 __vm_vcpu_add (vm , i );
36+
37+ sev_vm_launch (vm , es ? SEV_POLICY_ES : 0 );
38+
6039 if (es )
61- start .policy |= SEV_POLICY_ES ;
62- sev_ioctl (vm -> fd , KVM_SEV_LAUNCH_START , & start );
63- if (es )
64- sev_ioctl (vm -> fd , KVM_SEV_LAUNCH_UPDATE_VMSA , NULL );
40+ vm_sev_ioctl (vm , KVM_SEV_LAUNCH_UPDATE_VMSA , NULL );
6541 return vm ;
6642}
6743
@@ -181,7 +157,7 @@ static void test_sev_migrate_parameters(void)
181157 sev_vm = sev_vm_create (/* es= */ false);
182158 sev_es_vm = sev_vm_create (/* es= */ true);
183159 sev_es_vm_no_vmsa = vm_create_barebones ();
184- sev_ioctl (sev_es_vm_no_vmsa -> fd , KVM_SEV_ES_INIT , NULL );
160+ sev_es_vm_init (sev_es_vm_no_vmsa );
185161 __vm_vcpu_add (sev_es_vm_no_vmsa , 1 );
186162
187163 ret = __sev_migrate_from (sev_vm , sev_es_vm );
@@ -230,13 +206,13 @@ static void sev_mirror_create(struct kvm_vm *dst, struct kvm_vm *src)
230206 TEST_ASSERT (!ret , "Copying context failed, ret: %d, errno: %d\n" , ret , errno );
231207}
232208
233- static void verify_mirror_allowed_cmds (int vm_fd )
209+ static void verify_mirror_allowed_cmds (struct kvm_vm * vm )
234210{
235211 struct kvm_sev_guest_status status ;
212+ int cmd_id ;
236213
237- for (int cmd_id = KVM_SEV_INIT ; cmd_id < KVM_SEV_NR_MAX ; ++ cmd_id ) {
214+ for (cmd_id = KVM_SEV_INIT ; cmd_id < KVM_SEV_NR_MAX ; ++ cmd_id ) {
238215 int ret ;
239- __u32 fw_error ;
240216
241217 /*
242218 * These commands are allowed for mirror VMs, all others are
@@ -256,14 +232,14 @@ static void verify_mirror_allowed_cmds(int vm_fd)
256232 * These commands should be disallowed before the data
257233 * parameter is examined so NULL is OK here.
258234 */
259- ret = __sev_ioctl ( vm_fd , cmd_id , NULL , & fw_error );
235+ ret = __vm_sev_ioctl ( vm , cmd_id , NULL );
260236 TEST_ASSERT (
261237 ret == -1 && errno == EINVAL ,
262238 "Should not be able call command: %d. ret: %d, errno: %d\n" ,
263239 cmd_id , ret , errno );
264240 }
265241
266- sev_ioctl ( vm_fd , KVM_SEV_GUEST_STATUS , & status );
242+ vm_sev_ioctl ( vm , KVM_SEV_GUEST_STATUS , & status );
267243}
268244
269245static void test_sev_mirror (bool es )
@@ -281,9 +257,9 @@ static void test_sev_mirror(bool es)
281257 __vm_vcpu_add (dst_vm , i );
282258
283259 if (es )
284- sev_ioctl (dst_vm -> fd , KVM_SEV_LAUNCH_UPDATE_VMSA , NULL );
260+ vm_sev_ioctl (dst_vm , KVM_SEV_LAUNCH_UPDATE_VMSA , NULL );
285261
286- verify_mirror_allowed_cmds (dst_vm -> fd );
262+ verify_mirror_allowed_cmds (dst_vm );
287263
288264 kvm_vm_free (src_vm );
289265 kvm_vm_free (dst_vm );
0 commit comments