Skip to content

Commit ad27ce1

Browse files
codomaniabonzini
authored andcommitted
KVM: SEV: Add KVM_SEV_SNP_LAUNCH_FINISH command
Add a KVM_SEV_SNP_LAUNCH_FINISH command to finalize the cryptographic launch digest which stores the measurement of the guest at launch time. Also extend the existing SNP firmware data structures to support disabling the use of Versioned Chip Endorsement Keys (VCEK) by guests as part of this command. While finalizing the launch flow, the code also issues the LAUNCH_UPDATE SNP firmware commands to encrypt/measure the initial VMSA pages for each configured vCPU, which requires setting the RMP entries for those pages to private, so also add handling to clean up the RMP entries for these pages whening freeing vCPUs during shutdown. Signed-off-by: Brijesh Singh <brijesh.singh@amd.com> Co-developed-by: Michael Roth <michael.roth@amd.com> Signed-off-by: Michael Roth <michael.roth@amd.com> Signed-off-by: Harald Hoyer <harald@profian.com> Signed-off-by: Ashish Kalra <ashish.kalra@amd.com> Message-ID: <20240501085210.2213060-8-michael.roth@amd.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent dee5a47 commit ad27ce1

4 files changed

Lines changed: 175 additions & 1 deletion

File tree

Documentation/virt/kvm/x86/amd-memory-encryption.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,34 @@ where the allowed values for page_type are #define'd as::
544544
See the SEV-SNP spec [snp-fw-abi]_ for further details on how each page type is
545545
used/measured.
546546

547+
20. KVM_SEV_SNP_LAUNCH_FINISH
548+
-----------------------------
549+
550+
After completion of the SNP guest launch flow, the KVM_SEV_SNP_LAUNCH_FINISH
551+
command can be issued to make the guest ready for execution.
552+
553+
Parameters (in): struct kvm_sev_snp_launch_finish
554+
555+
Returns: 0 on success, -negative on error
556+
557+
::
558+
559+
struct kvm_sev_snp_launch_finish {
560+
__u64 id_block_uaddr;
561+
__u64 id_auth_uaddr;
562+
__u8 id_block_en;
563+
__u8 auth_key_en;
564+
__u8 vcek_disabled;
565+
__u8 host_data[32];
566+
__u8 pad0[3];
567+
__u16 flags; /* Must be zero */
568+
__u64 pad1[4];
569+
};
570+
571+
572+
See SNP_LAUNCH_FINISH in the SEV-SNP specification [snp-fw-abi]_ for further
573+
details on the input parameters in ``struct kvm_sev_snp_launch_finish``.
574+
547575
Device attribute API
548576
====================
549577

arch/x86/include/uapi/asm/kvm.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ enum sev_cmd_id {
700700
/* SNP-specific commands */
701701
KVM_SEV_SNP_LAUNCH_START = 100,
702702
KVM_SEV_SNP_LAUNCH_UPDATE,
703+
KVM_SEV_SNP_LAUNCH_FINISH,
703704

704705
KVM_SEV_NR_MAX,
705706
};
@@ -854,6 +855,22 @@ struct kvm_sev_snp_launch_update {
854855
__u64 pad2[4];
855856
};
856857

858+
#define KVM_SEV_SNP_ID_BLOCK_SIZE 96
859+
#define KVM_SEV_SNP_ID_AUTH_SIZE 4096
860+
#define KVM_SEV_SNP_FINISH_DATA_SIZE 32
861+
862+
struct kvm_sev_snp_launch_finish {
863+
__u64 id_block_uaddr;
864+
__u64 id_auth_uaddr;
865+
__u8 id_block_en;
866+
__u8 auth_key_en;
867+
__u8 vcek_disabled;
868+
__u8 host_data[KVM_SEV_SNP_FINISH_DATA_SIZE];
869+
__u8 pad0[3];
870+
__u16 flags;
871+
__u64 pad1[4];
872+
};
873+
857874
#define KVM_X2APIC_API_USE_32BIT_IDS (1ULL << 0)
858875
#define KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK (1ULL << 1)
859876

arch/x86/kvm/svm/sev.c

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ static u64 sev_supported_vmsa_features;
7575
SNP_POLICY_MASK_DEBUG | \
7676
SNP_POLICY_MASK_SINGLE_SOCKET)
7777

78+
#define INITIAL_VMSA_GPA 0xFFFFFFFFF000
79+
7880
static u8 sev_enc_bit;
7981
static DECLARE_RWSEM(sev_deactivate_lock);
8082
static DEFINE_MUTEX(sev_bitmap_lock);
@@ -2348,6 +2350,115 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)
23482350
return ret;
23492351
}
23502352

2353+
static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
2354+
{
2355+
struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2356+
struct sev_data_snp_launch_update data = {};
2357+
struct kvm_vcpu *vcpu;
2358+
unsigned long i;
2359+
int ret;
2360+
2361+
data.gctx_paddr = __psp_pa(sev->snp_context);
2362+
data.page_type = SNP_PAGE_TYPE_VMSA;
2363+
2364+
kvm_for_each_vcpu(i, vcpu, kvm) {
2365+
struct vcpu_svm *svm = to_svm(vcpu);
2366+
u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT;
2367+
2368+
ret = sev_es_sync_vmsa(svm);
2369+
if (ret)
2370+
return ret;
2371+
2372+
/* Transition the VMSA page to a firmware state. */
2373+
ret = rmp_make_private(pfn, INITIAL_VMSA_GPA, PG_LEVEL_4K, sev->asid, true);
2374+
if (ret)
2375+
return ret;
2376+
2377+
/* Issue the SNP command to encrypt the VMSA */
2378+
data.address = __sme_pa(svm->sev_es.vmsa);
2379+
ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE,
2380+
&data, &argp->error);
2381+
if (ret) {
2382+
if (!snp_page_reclaim(pfn))
2383+
host_rmp_make_shared(pfn, PG_LEVEL_4K);
2384+
2385+
return ret;
2386+
}
2387+
2388+
svm->vcpu.arch.guest_state_protected = true;
2389+
}
2390+
2391+
return 0;
2392+
}
2393+
2394+
static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
2395+
{
2396+
struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
2397+
struct kvm_sev_snp_launch_finish params;
2398+
struct sev_data_snp_launch_finish *data;
2399+
void *id_block = NULL, *id_auth = NULL;
2400+
int ret;
2401+
2402+
if (!sev_snp_guest(kvm))
2403+
return -ENOTTY;
2404+
2405+
if (!sev->snp_context)
2406+
return -EINVAL;
2407+
2408+
if (copy_from_user(&params, u64_to_user_ptr(argp->data), sizeof(params)))
2409+
return -EFAULT;
2410+
2411+
if (params.flags)
2412+
return -EINVAL;
2413+
2414+
/* Measure all vCPUs using LAUNCH_UPDATE before finalizing the launch flow. */
2415+
ret = snp_launch_update_vmsa(kvm, argp);
2416+
if (ret)
2417+
return ret;
2418+
2419+
data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
2420+
if (!data)
2421+
return -ENOMEM;
2422+
2423+
if (params.id_block_en) {
2424+
id_block = psp_copy_user_blob(params.id_block_uaddr, KVM_SEV_SNP_ID_BLOCK_SIZE);
2425+
if (IS_ERR(id_block)) {
2426+
ret = PTR_ERR(id_block);
2427+
goto e_free;
2428+
}
2429+
2430+
data->id_block_en = 1;
2431+
data->id_block_paddr = __sme_pa(id_block);
2432+
2433+
id_auth = psp_copy_user_blob(params.id_auth_uaddr, KVM_SEV_SNP_ID_AUTH_SIZE);
2434+
if (IS_ERR(id_auth)) {
2435+
ret = PTR_ERR(id_auth);
2436+
goto e_free_id_block;
2437+
}
2438+
2439+
data->id_auth_paddr = __sme_pa(id_auth);
2440+
2441+
if (params.auth_key_en)
2442+
data->auth_key_en = 1;
2443+
}
2444+
2445+
data->vcek_disabled = params.vcek_disabled;
2446+
2447+
memcpy(data->host_data, params.host_data, KVM_SEV_SNP_FINISH_DATA_SIZE);
2448+
data->gctx_paddr = __psp_pa(sev->snp_context);
2449+
ret = sev_issue_cmd(kvm, SEV_CMD_SNP_LAUNCH_FINISH, data, &argp->error);
2450+
2451+
kfree(id_auth);
2452+
2453+
e_free_id_block:
2454+
kfree(id_block);
2455+
2456+
e_free:
2457+
kfree(data);
2458+
2459+
return ret;
2460+
}
2461+
23512462
int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
23522463
{
23532464
struct kvm_sev_cmd sev_cmd;
@@ -2450,6 +2561,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
24502561
case KVM_SEV_SNP_LAUNCH_UPDATE:
24512562
r = snp_launch_update(kvm, &sev_cmd);
24522563
break;
2564+
case KVM_SEV_SNP_LAUNCH_FINISH:
2565+
r = snp_launch_finish(kvm, &sev_cmd);
2566+
break;
24532567
default:
24542568
r = -EINVAL;
24552569
goto out;
@@ -2940,11 +3054,24 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu)
29403054

29413055
svm = to_svm(vcpu);
29423056

3057+
/*
3058+
* If it's an SNP guest, then the VMSA was marked in the RMP table as
3059+
* a guest-owned page. Transition the page to hypervisor state before
3060+
* releasing it back to the system.
3061+
*/
3062+
if (sev_snp_guest(vcpu->kvm)) {
3063+
u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT;
3064+
3065+
if (host_rmp_make_shared(pfn, PG_LEVEL_4K))
3066+
goto skip_vmsa_free;
3067+
}
3068+
29433069
if (vcpu->arch.guest_state_protected)
29443070
sev_flush_encrypted_page(vcpu, svm->sev_es.vmsa);
29453071

29463072
__free_page(virt_to_page(svm->sev_es.vmsa));
29473073

3074+
skip_vmsa_free:
29483075
if (svm->sev_es.ghcb_sa_free)
29493076
kvfree(svm->sev_es.ghcb_sa);
29503077
}

include/linux/psp-sev.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ struct sev_data_snp_launch_update {
658658
* @id_auth_paddr: system physical address of ID block authentication structure
659659
* @id_block_en: indicates whether ID block is present
660660
* @auth_key_en: indicates whether author key is present in authentication structure
661+
* @vcek_disabled: indicates whether use of VCEK is allowed for attestation reports
661662
* @rsvd: reserved
662663
* @host_data: host-supplied data for guest, not interpreted by firmware
663664
*/
@@ -667,7 +668,8 @@ struct sev_data_snp_launch_finish {
667668
u64 id_auth_paddr;
668669
u8 id_block_en:1;
669670
u8 auth_key_en:1;
670-
u64 rsvd:62;
671+
u8 vcek_disabled:1;
672+
u64 rsvd:61;
671673
u8 host_data[32];
672674
} __packed;
673675

0 commit comments

Comments
 (0)