@@ -562,6 +562,29 @@ static int tdx_mem_page_aug(struct kvm *kvm, gfn_t gfn,
562
562
return 0 ;
563
563
}
564
564
565
+ /*
566
+ * KVM_TDX_INIT_MEM_REGION calls kvm_gmem_populate() to map guest pages; the
567
+ * callback tdx_gmem_post_populate() then maps pages into private memory.
568
+ * through the a seamcall TDH.MEM.PAGE.ADD(). The SEAMCALL also requires the
569
+ * private EPT structures for the page to have been built before, which is
570
+ * done via kvm_tdp_map_page(). nr_premapped counts the number of pages that
571
+ * were added to the EPT structures but not added with TDH.MEM.PAGE.ADD().
572
+ * The counter has to be zero on KVM_TDX_FINALIZE_VM, to ensure that there
573
+ * are no half-initialized shared EPT pages.
574
+ */
575
+ static int tdx_mem_page_record_premap_cnt (struct kvm * kvm , gfn_t gfn ,
576
+ enum pg_level level , kvm_pfn_t pfn )
577
+ {
578
+ struct kvm_tdx * kvm_tdx = to_kvm_tdx (kvm );
579
+
580
+ if (KVM_BUG_ON (kvm -> arch .pre_fault_allowed , kvm ))
581
+ return - EINVAL ;
582
+
583
+ /* nr_premapped will be decreased when tdh_mem_page_add() is called. */
584
+ atomic64_inc (& kvm_tdx -> nr_premapped );
585
+ return 0 ;
586
+ }
587
+
565
588
int tdx_sept_set_private_spte (struct kvm * kvm , gfn_t gfn ,
566
589
enum pg_level level , kvm_pfn_t pfn )
567
590
{
@@ -582,14 +605,15 @@ int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
582
605
*/
583
606
get_page (page );
584
607
608
+ /*
609
+ * Read 'pre_fault_allowed' before 'kvm_tdx->state'; see matching
610
+ * barrier in tdx_td_finalize().
611
+ */
612
+ smp_rmb ();
585
613
if (likely (kvm_tdx -> state == TD_STATE_RUNNABLE ))
586
614
return tdx_mem_page_aug (kvm , gfn , level , page );
587
615
588
- /*
589
- * TODO: KVM_TDX_INIT_MEM_REGION support to populate before finalize
590
- * comes here for the initial memory.
591
- */
592
- return - EOPNOTSUPP ;
616
+ return tdx_mem_page_record_premap_cnt (kvm , gfn , level , pfn );
593
617
}
594
618
595
619
static int tdx_sept_drop_private_spte (struct kvm * kvm , gfn_t gfn ,
@@ -620,10 +644,12 @@ static int tdx_sept_drop_private_spte(struct kvm *kvm, gfn_t gfn,
620
644
if (unlikely (kvm_tdx -> state != TD_STATE_RUNNABLE &&
621
645
err == (TDX_EPT_WALK_FAILED | TDX_OPERAND_ID_RCX ))) {
622
646
/*
623
- * This page was mapped with KVM_MAP_MEMORY , but
624
- * KVM_TDX_INIT_MEM_REGION is not issued yet .
647
+ * Page is mapped by KVM_TDX_INIT_MEM_REGION , but hasn't called
648
+ * tdh_mem_page_add() .
625
649
*/
626
- if (!is_last_spte (entry , level ) || !(entry & VMX_EPT_RWX_MASK )) {
650
+ if ((!is_last_spte (entry , level ) || !(entry & VMX_EPT_RWX_MASK )) &&
651
+ !KVM_BUG_ON (!atomic64_read (& kvm_tdx -> nr_premapped ), kvm )) {
652
+ atomic64_dec (& kvm_tdx -> nr_premapped );
627
653
tdx_unpin (kvm , page );
628
654
return 0 ;
629
655
}
@@ -1371,6 +1397,36 @@ void tdx_flush_tlb_all(struct kvm_vcpu *vcpu)
1371
1397
ept_sync_global ();
1372
1398
}
1373
1399
1400
+ static int tdx_td_finalize (struct kvm * kvm , struct kvm_tdx_cmd * cmd )
1401
+ {
1402
+ struct kvm_tdx * kvm_tdx = to_kvm_tdx (kvm );
1403
+
1404
+ guard (mutex )(& kvm -> slots_lock );
1405
+
1406
+ if (!is_hkid_assigned (kvm_tdx ) || kvm_tdx -> state == TD_STATE_RUNNABLE )
1407
+ return - EINVAL ;
1408
+ /*
1409
+ * Pages are pending for KVM_TDX_INIT_MEM_REGION to issue
1410
+ * TDH.MEM.PAGE.ADD().
1411
+ */
1412
+ if (atomic64_read (& kvm_tdx -> nr_premapped ))
1413
+ return - EINVAL ;
1414
+
1415
+ cmd -> hw_error = tdh_mr_finalize (& kvm_tdx -> td );
1416
+ if (tdx_operand_busy (cmd -> hw_error ))
1417
+ return - EBUSY ;
1418
+ if (KVM_BUG_ON (cmd -> hw_error , kvm )) {
1419
+ pr_tdx_error (TDH_MR_FINALIZE , cmd -> hw_error );
1420
+ return - EIO ;
1421
+ }
1422
+
1423
+ kvm_tdx -> state = TD_STATE_RUNNABLE ;
1424
+ /* TD_STATE_RUNNABLE must be set before 'pre_fault_allowed' */
1425
+ smp_wmb ();
1426
+ kvm -> arch .pre_fault_allowed = true;
1427
+ return 0 ;
1428
+ }
1429
+
1374
1430
int tdx_vm_ioctl (struct kvm * kvm , void __user * argp )
1375
1431
{
1376
1432
struct kvm_tdx_cmd tdx_cmd ;
@@ -1395,6 +1451,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)
1395
1451
case KVM_TDX_INIT_VM :
1396
1452
r = tdx_td_init (kvm , & tdx_cmd );
1397
1453
break ;
1454
+ case KVM_TDX_FINALIZE_VM :
1455
+ r = tdx_td_finalize (kvm , & tdx_cmd );
1456
+ break ;
1398
1457
default :
1399
1458
r = - EINVAL ;
1400
1459
goto out ;
@@ -1662,6 +1721,9 @@ static int tdx_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,
1662
1721
goto out ;
1663
1722
}
1664
1723
1724
+ if (!KVM_BUG_ON (!atomic64_read (& kvm_tdx -> nr_premapped ), kvm ))
1725
+ atomic64_dec (& kvm_tdx -> nr_premapped );
1726
+
1665
1727
if (arg -> flags & KVM_TDX_MEASURE_MEMORY_REGION ) {
1666
1728
for (i = 0 ; i < PAGE_SIZE ; i += TDX_EXTENDMR_CHUNKSIZE ) {
1667
1729
err = tdh_mr_extend (& kvm_tdx -> td , gpa + i , & entry ,
0 commit comments