From nobody Sun Nov 24 23:00:40 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1720004895; cv=none; d=zohomail.com; s=zohoarc; b=krIjQfSq3Fsvfc43fZeYY0T5TQ6y+bxiM4epT1k6vDaFH4TFgZXm5omVFkHt5RS9DmyY807Lp170B7Jn5m6Frf1i+SLFg042EXMTn/DwG/jCA+XElg5B8ukDb//CWXBhRWZdJr6erwqjCtE4qbEfAj4Wk4UHNYgEaWieXlvFj3Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720004895; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=peKWevRqVwNKYl8G8+2qCslgTKG04g3K56KP506h7hA=; b=er+Gpp9wcMUTqiGSd5P6HARr3e4DYxf4uOOcf9tNYzYOpJQRZg7BgNTvHbN+DDUKQGM7JKiQ2UJa645wAmBv4NSjLp1+QV15VAjiYB16UgfVHP7Kfc3IECMTxOUgnb6YCngofj8Lu7SMwD4z0qy9TzkQFP0IUbHVagJuWAxWfeY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1720004895544227.86371360614146; Wed, 3 Jul 2024 04:08:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sOxp7-0004xF-5H; Wed, 03 Jul 2024 07:06:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sOxov-0004sb-OB for qemu-devel@nongnu.org; Wed, 03 Jul 2024 07:06:41 -0400 Received: from smtp-out1.suse.de ([2a07:de40:b251:101:10:150:64:1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sOxop-0006Xc-6Q for qemu-devel@nongnu.org; Wed, 03 Jul 2024 07:06:41 -0400 Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id AD82221C01; Wed, 3 Jul 2024 11:06:25 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id EF72213974; Wed, 3 Jul 2024 11:06:24 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id iK+KOLAwhWZ6cgAAD6G6ig (envelope-from ); Wed, 03 Jul 2024 11:06:24 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1720004785; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=peKWevRqVwNKYl8G8+2qCslgTKG04g3K56KP506h7hA=; b=MrXSSyMkj3543ErPfLFLmJ0Brdx+G1NCoc+AvZIXGrMk9ntJ4ZFXBNcrNU4AotefZsEHvU H4BFk6gy1sa5hnJd7rSwTYof6ihR3ZCZL6SShHP+SvClzarYsSmbjhdY0zylWSM6YGknJH w/M3I9L78moIi1HSAzfH8KI6heZCuJg= Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=MrXSSyMk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1720004785; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=peKWevRqVwNKYl8G8+2qCslgTKG04g3K56KP506h7hA=; b=MrXSSyMkj3543ErPfLFLmJ0Brdx+G1NCoc+AvZIXGrMk9ntJ4ZFXBNcrNU4AotefZsEHvU H4BFk6gy1sa5hnJd7rSwTYof6ihR3ZCZL6SShHP+SvClzarYsSmbjhdY0zylWSM6YGknJH w/M3I9L78moIi1HSAzfH8KI6heZCuJg= From: Roy Hopkins To: qemu-devel@nongnu.org Cc: Roy Hopkins , Paolo Bonzini , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Stefano Garzarella , Marcelo Tosatti , "Michael S . Tsirkin" , Cornelia Huck , Marcel Apfelbaum , Sergio Lopez , Eduardo Habkost , Alistair Francis , Peter Xu , David Hildenbrand , Igor Mammedov , Tom Lendacky , Michael Roth , Ani Sinha , =?UTF-8?q?J=C3=B6rg=20Roedel?= Subject: [PATCH v4 10/17] i386/sev: Implement ConfidentialGuestSupport functions for SEV Date: Wed, 3 Jul 2024 12:05:48 +0100 Message-ID: <51933aeb1006616107e912d585dfe8fe0631fdd5.1720004383.git.roy.hopkins@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: AD82221C01 X-Spam-Score: -1.51 X-Spamd-Result: default: False [-1.51 / 50.00]; BAYES_HAM(-3.00)[100.00%]; SUSPICIOUS_RECIPS(1.50)[]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; ARC_NA(0.00)[]; RCPT_COUNT_TWELVE(0.00)[19]; MIME_TRACE(0.00)[0:+]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_TLS_ALL(0.00)[]; TO_DN_SOME(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:email,suse.com:dkim,imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; FREEMAIL_CC(0.00)[suse.com,redhat.com,gmail.com,habkost.net,alistair23.me,amd.com]; DNSWL_BLOCKED(0.00)[2a07:de40:b281:104:10:150:64:97:from,2a07:de40:b281:106:10:150:64:167:received]; RCVD_VIA_SMTP_AUTH(0.00)[]; TAGGED_RCPT(0.00)[]; DKIM_TRACE(0.00)[suse.com:+]; R_RATELIMIT(0.00)[to_ip_from(RLgjcjk3igk5en59wt86eb8xw3)]; FREEMAIL_ENVRCPT(0.00)[gmail.com] X-Rspamd-Action: no action X-Rspamd-Server: rspamd1.dmz-prg2.suse.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a07:de40:b251:101:10:150:64:1; envelope-from=roy.hopkins@suse.com; helo=smtp-out1.suse.de X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1720004897723100003 Content-Type: text/plain; charset="utf-8" The ConfidentialGuestSupport object defines a number of virtual functions that are called during processing of IGVM directives to query or configure initial guest state. In order to support processing of IGVM files, these functions need to be implemented by relevant isolation hardware support code such as SEV. This commit implements the required functions for SEV-ES and adds support for processing IGVM files for configuring the guest. Signed-off-by: Roy Hopkins --- target/i386/sev.h | 2 + target/i386/sev.c | 250 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 242 insertions(+), 10 deletions(-) diff --git a/target/i386/sev.h b/target/i386/sev.h index 167dd154d6..2ccd6fe1e8 100644 --- a/target/i386/sev.h +++ b/target/i386/sev.h @@ -34,6 +34,8 @@ #define SEV_SNP_POLICY_SMT 0x10000 #define SEV_SNP_POLICY_DBG 0x80000 =20 +#define SVM_SEV_FEAT_SNP_ACTIVE 1 + typedef struct SevKernelLoaderContext { char *setup_data; size_t setup_size; diff --git a/target/i386/sev.c b/target/i386/sev.c index 7487971344..9a05b971c8 100644 --- a/target/i386/sev.c +++ b/target/i386/sev.c @@ -39,8 +39,10 @@ #include "qapi/qapi-commands-misc-target.h" #include "confidential-guest.h" #include "hw/i386/pc.h" +#include "hw/i386/e820_memory_layout.h" #include "exec/address-spaces.h" #include "qemu/queue.h" +#include "qemu/cutils.h" =20 OBJECT_DECLARE_TYPE(SevCommonState, SevCommonStateClass, SEV_COMMON) OBJECT_DECLARE_TYPE(SevGuestState, SevCommonStateClass, SEV_GUEST) @@ -49,6 +51,9 @@ OBJECT_DECLARE_TYPE(SevSnpGuestState, SevCommonStateClass= , SEV_SNP_GUEST) /* hard code sha256 digest size */ #define HASH_SIZE 32 =20 +/* Hard coded GPA that KVM uses for the VMSA */ +#define KVM_VMSA_GPA 0xFFFFFFFFF000 + /* Convert between SEV-ES VMSA and SegmentCache flags/attributes */ #define FLAGS_VMSA_TO_SEGCACHE(flags) \ ((((flags) & 0xff00) << 12) | (((flags) & 0xff) << 8)) @@ -487,6 +492,103 @@ static void sev_apply_cpu_context(CPUState *cpu) } } =20 +static int check_vmsa_supported(hwaddr gpa, const struct sev_es_save_area = *vmsa, + Error **errp) +{ + struct sev_es_save_area vmsa_check; + + /* + * KVM always populates the VMSA at a fixed GPA which cannot be modifi= ed + * from userspace. Specifying a different GPA will not prevent the gue= st + * from starting but will cause the launch measurement to be different + * from expected. Therefore check that the provided GPA matches the KVM + * hardcoded value. + */ + if (gpa !=3D KVM_VMSA_GPA) { + error_setg(errp, + "%s: The VMSA GPA must be %lX but is specified as %lX", + __func__, KVM_VMSA_GPA, gpa); + return -1; + } + + /* + * Clear all supported fields so we can then check the entire structure + * is zero. + */ + memcpy(&vmsa_check, vmsa, sizeof(struct sev_es_save_area)); + memset(&vmsa_check.es, 0, sizeof(vmsa_check.es)); + memset(&vmsa_check.cs, 0, sizeof(vmsa_check.cs)); + memset(&vmsa_check.ss, 0, sizeof(vmsa_check.ss)); + memset(&vmsa_check.ds, 0, sizeof(vmsa_check.ds)); + memset(&vmsa_check.fs, 0, sizeof(vmsa_check.fs)); + memset(&vmsa_check.gs, 0, sizeof(vmsa_check.gs)); + memset(&vmsa_check.gdtr, 0, sizeof(vmsa_check.gdtr)); + memset(&vmsa_check.idtr, 0, sizeof(vmsa_check.idtr)); + memset(&vmsa_check.ldtr, 0, sizeof(vmsa_check.ldtr)); + memset(&vmsa_check.tr, 0, sizeof(vmsa_check.tr)); + vmsa_check.efer =3D 0; + vmsa_check.cr0 =3D 0; + vmsa_check.cr3 =3D 0; + vmsa_check.cr4 =3D 0; + vmsa_check.xcr0 =3D 0; + vmsa_check.dr6 =3D 0; + vmsa_check.dr7 =3D 0; + vmsa_check.rax =3D 0; + vmsa_check.rcx =3D 0; + vmsa_check.rdx =3D 0; + vmsa_check.rbx =3D 0; + vmsa_check.rsp =3D 0; + vmsa_check.rbp =3D 0; + vmsa_check.rsi =3D 0; + vmsa_check.rdi =3D 0; + vmsa_check.r8 =3D 0; + vmsa_check.r9 =3D 0; + vmsa_check.r10 =3D 0; + vmsa_check.r11 =3D 0; + vmsa_check.r12 =3D 0; + vmsa_check.r13 =3D 0; + vmsa_check.r14 =3D 0; + vmsa_check.r15 =3D 0; + vmsa_check.rip =3D 0; + vmsa_check.rflags =3D 0; + + vmsa_check.g_pat =3D 0; + vmsa_check.xcr0 =3D 0; + + vmsa_check.x87_fcw =3D 0; + vmsa_check.mxcsr =3D 0; + + if (sev_snp_enabled()) { + if (vmsa_check.sev_features !=3D SVM_SEV_FEAT_SNP_ACTIVE) { + error_setg(errp, + "%s: sev_features in the VMSA contains an unsupport= ed " + "value. For SEV-SNP, sev_features must be set to %x= .", + __func__, SVM_SEV_FEAT_SNP_ACTIVE); + return -1; + } + vmsa_check.sev_features =3D 0; + } else { + if (vmsa_check.sev_features !=3D 0) { + error_setg(errp, + "%s: sev_features in the VMSA contains an unsupport= ed " + "value. For SEV-ES and SEV, sev_features must be " + "set to 0.", __func__); + return -1; + } + } + + if (!buffer_is_zero(&vmsa_check, sizeof(vmsa_check))) { + error_setg(errp, + "%s: The VMSA contains fields that are not " + "synchronized with KVM. Continuing would result in " + "either unpredictable guest behavior, or a " + "mismatched launch measurement.", + __func__); + return -1; + } + return 0; +} + static int sev_set_cpu_context(uint16_t cpu_index, const void *ctx, uint32_t ctx_len, hwaddr gpa, Error **errp) { @@ -1470,18 +1572,26 @@ sev_snp_launch_finish(SevCommonState *sev_common) struct kvm_sev_snp_launch_finish *finish =3D &sev_snp->kvm_finish_conf; =20 /* - * To boot the SNP guest, the hypervisor is required to populate the C= PUID - * and Secrets page before finalizing the launch flow. The location of - * the secrets and CPUID page is available through the OVMF metadata G= UID. + * Populate all the metadata pages if not using an IGVM file. In the c= ase + * where an IGVM file is provided it will be used to configure the met= adata + * pages directly. */ - metadata =3D pc_system_get_ovmf_sev_metadata_ptr(); - if (metadata =3D=3D NULL) { - error_report("%s: Failed to locate SEV metadata header", __func__); - exit(1); - } + if (!X86_MACHINE(qdev_get_machine())->igvm) { + /* + * To boot the SNP guest, the hypervisor is required to populate t= he + * CPUID and Secrets page before finalizing the launch flow. The + * location of the secrets and CPUID page is available through the + * OVMF metadata GUID. + */ + metadata =3D pc_system_get_ovmf_sev_metadata_ptr(); + if (metadata =3D=3D NULL) { + error_report("%s: Failed to locate SEV metadata header", __fun= c__); + exit(1); + } =20 - /* Populate all the metadata pages */ - snp_populate_metadata_pages(sev_snp, metadata); + /* Populate all the metadata pages */ + snp_populate_metadata_pages(sev_snp, metadata); + } =20 QTAILQ_FOREACH(data, &launch_update, next) { ret =3D sev_snp_launch_update(sev_snp, data); @@ -2222,6 +2332,120 @@ static void sev_common_set_kernel_hashes(Object *ob= j, bool value, Error **errp) SEV_COMMON(obj)->kernel_hashes =3D value; } =20 +static int cgs_check_support(ConfidentialGuestPlatformType platform, + uint16_t platform_version, uint8_t highest_vt= l, + uint64_t shared_gpa_boundary) +{ + return (((platform =3D=3D CGS_PLATFORM_SEV_SNP) && sev_snp_enabled()) = || + ((platform =3D=3D CGS_PLATFORM_SEV_ES) && sev_es_enabled()) || + ((platform =3D=3D CGS_PLATFORM_SEV) && sev_enabled())) ? 1 : 0; +} + +static int cgs_set_guest_state(hwaddr gpa, uint8_t *ptr, uint64_t len, + ConfidentialGuestPageType memory_type, + uint16_t cpu_index, Error **errp) +{ + SevCommonState *sev_common =3D SEV_COMMON(MACHINE(qdev_get_machine())-= >cgs); + SevCommonStateClass *klass =3D SEV_COMMON_GET_CLASS(sev_common); + + if (!sev_enabled()) { + error_setg(errp, "%s: attempt to configure guest memory, but SEV " + "is not enabled", __func__); + return -1; + } + + switch (memory_type) { + case CGS_PAGE_TYPE_NORMAL: + case CGS_PAGE_TYPE_ZERO: + return klass->launch_update_data(sev_common, gpa, ptr, len, errp); + + case CGS_PAGE_TYPE_VMSA: + if (!sev_es_enabled()) { + error_setg(errp, + "%s: attempt to configure initial VMSA, but SEV-ES " + "is not supported", + __func__); + return -1; + } + if (check_vmsa_supported(gpa, (const struct sev_es_save_area *)ptr, + errp) < 0) { + return -1; + } + return sev_set_cpu_context(cpu_index, ptr, len, gpa, errp); + + case CGS_PAGE_TYPE_UNMEASURED: + if (sev_snp_enabled()) { + return snp_launch_update_data( + gpa, ptr, len, KVM_SEV_SNP_PAGE_TYPE_UNMEASURED, errp); + } + /* No action required if not SEV-SNP */ + return 0; + + case CGS_PAGE_TYPE_SECRETS: + if (!sev_snp_enabled()) { + error_setg(errp, + "%s: attempt to configure secrets page, but SEV-SNP= " + "is not supported", + __func__); + return -1; + } + return snp_launch_update_data(gpa, ptr, len, + KVM_SEV_SNP_PAGE_TYPE_SECRETS, errp); + + case CGS_PAGE_TYPE_REQUIRED_MEMORY: + if (kvm_convert_memory(gpa, len, true) < 0) { + error_setg( + errp, + "%s: failed to configure required memory. gpa: %lX, type: = %d", + __func__, gpa, memory_type); + return -1; + } + return 0; + + case CGS_PAGE_TYPE_CPUID: + if (!sev_snp_enabled()) { + error_setg(errp, + "%s: attempt to configure CPUID page, but SEV-SNP " + "is not supported", + __func__); + return -1; + } + return snp_launch_update_cpuid(gpa, ptr, len, errp); + } + error_setg(errp, "%s: failed to update guest. gpa: %lX, type: %d", __f= unc__, + gpa, memory_type); + return -1; +} + +static int cgs_get_mem_map_entry(int index, + ConfidentialGuestMemoryMapEntry *entry, + Error **errp) +{ + if ((index < 0) || (index >=3D e820_get_num_entries())) { + return 1; + } + entry->gpa =3D e820_table[index].address; + entry->size =3D e820_table[index].length; + switch (e820_table[index].type) { + case E820_RAM: + entry->type =3D CGS_MEM_RAM; + break; + case E820_RESERVED: + entry->type =3D CGS_MEM_RESERVED; + break; + case E820_ACPI: + entry->type =3D CGS_MEM_ACPI; + break; + case E820_NVS: + entry->type =3D CGS_MEM_NVS; + break; + case E820_UNUSABLE: + entry->type =3D CGS_MEM_UNUSABLE; + break; + } + return 0; +} + static void sev_common_class_init(ObjectClass *oc, void *data) { @@ -2245,6 +2469,8 @@ static void sev_common_instance_init(Object *obj) { SevCommonState *sev_common =3D SEV_COMMON(obj); + ConfidentialGuestSupportClass *cgs =3D + CONFIDENTIAL_GUEST_SUPPORT_GET_CLASS(obj); =20 sev_common->kvm_type =3D -1; =20 @@ -2255,6 +2481,10 @@ sev_common_instance_init(Object *obj) object_property_add_uint32_ptr(obj, "reduced-phys-bits", &sev_common->reduced_phys_bits, OBJ_PROP_FLAG_READWRITE); + cgs->check_support =3D cgs_check_support; + cgs->set_guest_state =3D cgs_set_guest_state; + cgs->get_mem_map_entry =3D cgs_get_mem_map_entry; + QTAILQ_INIT(&sev_common->launch_vmsa); } =20 --=20 2.43.0