From nobody Wed Dec 17 23:22:38 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6AF7AC25B5F for ; Wed, 16 Aug 2023 16:00:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344571AbjHPP7p (ORCPT ); Wed, 16 Aug 2023 11:59:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344520AbjHPP7T (ORCPT ); Wed, 16 Aug 2023 11:59:19 -0400 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3ED52D4A; Wed, 16 Aug 2023 08:59:03 -0700 (PDT) Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1bba48b0bd2so45251965ad.3; Wed, 16 Aug 2023 08:59:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692201543; x=1692806343; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=edyxZlMDBy/hWpSdjMir1/zkWMoNQWbVpCyIxteeHYI=; b=YKeAX1vL4TvgQsAac85iqiUqLaffd3zpfGC7TdlWAw/eaIXmp7C/H3+RXTIRL5woN6 0jUJEk48sgGbixnL72/i3iftb7h1V102p2jg53Zcmuy32Tenew7Sze6UY8w4PipyJHtZ mdH4fRJJyWCZ603KVWAsDLB1PMw/oMgT9sLLEH655OgpglcA9fAG1c0CrWPOESXvISm9 dYzv6W1lUdmsko43ghC6vNRANBwmzwqpst41wDUg18d2jrjkgHvA7IFgGnCCGxVnv5OD 2mWXtf8+jKR2+zRdVADy2pXJCtHjHIF9Qi3C4YL+cbNyxDAbTb9g/47+YHJbLOaXTmPA 4HqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692201543; x=1692806343; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=edyxZlMDBy/hWpSdjMir1/zkWMoNQWbVpCyIxteeHYI=; b=BWf/hg76c6htVdMrOZkCWQNkNY0++hLu6EXy3wXh4SAWvBiO1KgHgQ4TQYJCKiN2eD 4xuEBo93Skwwm9HW1/uTyb3wRld9BHoPRamS2is4H1fzlSI0Llq8bvIS9l5SlStqFMKY yJvdkUgPIeUneoZNIP6U42m9zSVWn4C53OCsvbTPmAUweY/BJRiD87h4Y5K1l2sdU9QJ 97EcrgRcY9n+c2BMoyLsBkgiQ9btPUlZ5CfrXP/MFMNZCZ6gvxvPbhmTvUEunJfhUl3I wNsIPQV5V9w1xQrfjr9vIfIJxA2/d4lKN1G63SIExPoY77RPohiChpTBJz5v3WH4IdPp jKgw== X-Gm-Message-State: AOJu0Yz5DswGcYJ1r9USLeYcrFIDMDY1ZpfOY4Krc/TfCIzEh17Nvfy3 bxUTWP/Oivc5yTCcRNyColA= X-Google-Smtp-Source: AGHT+IFWl8O1V8EvLbaSYODw78xAGUkzHlb6dvUUrtcZn2Le04EZ1qs22r5iGZQhpXO5A3fVKJIwpQ== X-Received: by 2002:a17:902:ec8f:b0:1bd:f378:b1a8 with SMTP id x15-20020a170902ec8f00b001bdf378b1a8mr2449973plg.11.1692201543400; Wed, 16 Aug 2023 08:59:03 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:e588:8d80:9ae5:5adc]) by smtp.gmail.com with ESMTPSA id h17-20020a170902f7d100b001bc930d4517sm13366973plw.42.2023.08.16.08.59.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Aug 2023 08:59:03 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, daniel.lezcano@linaro.org, arnd@arndb.de, michael.h.kelley@microsoft.com Cc: Tianyu Lan , linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, vkuznets@redhat.com, Michael Kelley Subject: [PATCH v6 7/8] x86/hyperv: Add smp support for SEV-SNP guest Date: Wed, 16 Aug 2023 11:58:48 -0400 Message-Id: <20230816155850.1216996-8-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230816155850.1216996-1-ltykernel@gmail.com> References: <20230816155850.1216996-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Tianyu Lan In the AMD SEV-SNP guest, AP needs to be started up via sev es save area and Hyper-V requires to call HVCALL_START_VP hypercall to pass the gpa of sev es save area with AP's vp index and VTL(Virtual trust level) parameters. Override wakeup_secondary_cpu_64 callback with hv_snp_boot_ap. Reviewed-by: Michael Kelley Signed-off-by: Tianyu Lan --- arch/x86/hyperv/ivm.c | 138 ++++++++++++++++++++++++++++++ arch/x86/include/asm/mshyperv.h | 9 ++ arch/x86/kernel/cpu/mshyperv.c | 13 ++- include/asm-generic/hyperv-tlfs.h | 1 + 4 files changed, 159 insertions(+), 2 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index b2b5cb19fac9..cbbd3af4c3da 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -18,11 +18,20 @@ #include #include #include +#include +#include +#include +#include +#include +#include =20 #ifdef CONFIG_AMD_MEM_ENCRYPT =20 #define GHCB_USAGE_HYPERV_CALL 1 =20 +static u8 ap_start_input_arg[PAGE_SIZE] __bss_decrypted __aligned(PAGE_SIZ= E); +static u8 ap_start_stack[PAGE_SIZE] __aligned(PAGE_SIZE); + union hv_ghcb { struct ghcb ghcb; struct { @@ -56,6 +65,8 @@ union hv_ghcb { } hypercall; } __packed __aligned(HV_HYP_PAGE_SIZE); =20 +static DEFINE_PER_CPU(struct sev_es_save_area *, hv_sev_vmsa); + static u16 hv_ghcb_version __ro_after_init; =20 u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_si= ze) @@ -357,6 +368,133 @@ static bool hv_is_private_mmio(u64 addr) return false; } =20 +#define hv_populate_vmcb_seg(seg, gdtr_base) \ +do { \ + if (seg.selector) { \ + seg.base =3D 0; \ + seg.limit =3D HV_AP_SEGMENT_LIMIT; \ + seg.attrib =3D *(u16 *)(gdtr_base + seg.selector + 5); \ + seg.attrib =3D (seg.attrib & 0xFF) | ((seg.attrib >> 4) & 0xF00); \ + } \ +} while (0) \ + +static int snp_set_vmsa(void *va, bool vmsa) +{ + u64 attrs; + + /* + * Running at VMPL0 allows the kernel to change the VMSA bit for a page + * using the RMPADJUST instruction. However, for the instruction to + * succeed it must target the permissions of a lesser privileged + * (higher numbered) VMPL level, so use VMPL1 (refer to the RMPADJUST + * instruction in the AMD64 APM Volume 3). + */ + attrs =3D 1; + if (vmsa) + attrs |=3D RMPADJUST_VMSA_PAGE_BIT; + + return rmpadjust((unsigned long)va, RMP_PG_SIZE_4K, attrs); +} + +static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa) +{ + int err; + + err =3D snp_set_vmsa(vmsa, false); + if (err) + pr_err("clear VMSA page failed (%u), leaking page\n", err); + else + free_page((unsigned long)vmsa); +} + +int hv_snp_boot_ap(int cpu, unsigned long start_ip) +{ + struct sev_es_save_area *vmsa =3D (struct sev_es_save_area *) + __get_free_page(GFP_KERNEL | __GFP_ZERO); + struct sev_es_save_area *cur_vmsa; + struct desc_ptr gdtr; + u64 ret, retry =3D 5; + struct hv_enable_vp_vtl *start_vp_input; + unsigned long flags; + + if (!vmsa) + return -ENOMEM; + + native_store_gdt(&gdtr); + + vmsa->gdtr.base =3D gdtr.address; + vmsa->gdtr.limit =3D gdtr.size; + + asm volatile("movl %%es, %%eax;" : "=3Da" (vmsa->es.selector)); + hv_populate_vmcb_seg(vmsa->es, vmsa->gdtr.base); + + asm volatile("movl %%cs, %%eax;" : "=3Da" (vmsa->cs.selector)); + hv_populate_vmcb_seg(vmsa->cs, vmsa->gdtr.base); + + asm volatile("movl %%ss, %%eax;" : "=3Da" (vmsa->ss.selector)); + hv_populate_vmcb_seg(vmsa->ss, vmsa->gdtr.base); + + asm volatile("movl %%ds, %%eax;" : "=3Da" (vmsa->ds.selector)); + hv_populate_vmcb_seg(vmsa->ds, vmsa->gdtr.base); + + vmsa->efer =3D native_read_msr(MSR_EFER); + + asm volatile("movq %%cr4, %%rax;" : "=3Da" (vmsa->cr4)); + asm volatile("movq %%cr3, %%rax;" : "=3Da" (vmsa->cr3)); + asm volatile("movq %%cr0, %%rax;" : "=3Da" (vmsa->cr0)); + + vmsa->xcr0 =3D 1; + vmsa->g_pat =3D HV_AP_INIT_GPAT_DEFAULT; + vmsa->rip =3D (u64)secondary_startup_64_no_verify; + vmsa->rsp =3D (u64)&ap_start_stack[PAGE_SIZE]; + + /* + * Set the SNP-specific fields for this VMSA: + * VMPL level + * SEV_FEATURES (matches the SEV STATUS MSR right shifted 2 bits) + */ + vmsa->vmpl =3D 0; + vmsa->sev_features =3D sev_status >> 2; + + ret =3D snp_set_vmsa(vmsa, true); + if (!ret) { + pr_err("RMPADJUST(%llx) failed: %llx\n", (u64)vmsa, ret); + free_page((u64)vmsa); + return ret; + } + + local_irq_save(flags); + start_vp_input =3D (struct hv_enable_vp_vtl *)ap_start_input_arg; + memset(start_vp_input, 0, sizeof(*start_vp_input)); + start_vp_input->partition_id =3D -1; + start_vp_input->vp_index =3D cpu; + start_vp_input->target_vtl.target_vtl =3D ms_hyperv.vtl; + *(u64 *)&start_vp_input->vp_context =3D __pa(vmsa) | 1; + + do { + ret =3D hv_do_hypercall(HVCALL_START_VP, + start_vp_input, NULL); + } while (hv_result(ret) =3D=3D HV_STATUS_TIME_OUT && retry--); + + local_irq_restore(flags); + + if (!hv_result_success(ret)) { + pr_err("HvCallStartVirtualProcessor failed: %llx\n", ret); + snp_cleanup_vmsa(vmsa); + vmsa =3D NULL; + } + + cur_vmsa =3D per_cpu(hv_sev_vmsa, cpu); + /* Free up any previous VMSA page */ + if (cur_vmsa) + snp_cleanup_vmsa(cur_vmsa); + + /* Record the current VMSA page */ + per_cpu(hv_sev_vmsa, cpu) =3D vmsa; + + return ret; +} + void __init hv_vtom_init(void) { /* diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyper= v.h index 8479626cd7cb..ffc419409db7 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -48,6 +48,13 @@ extern u64 hv_current_partition_id; =20 extern union hv_ghcb * __percpu *hv_ghcb_pg; =20 +/* + * DEFAULT INIT GPAT and SEGMENT LIMIT value in struct VMSA + * to start AP in enlightened SEV guest. + */ +#define HV_AP_INIT_GPAT_DEFAULT 0x0007040600070406ULL +#define HV_AP_SEGMENT_LIMIT 0xffffffff + int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages); int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id); int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags); @@ -253,12 +260,14 @@ void hv_ghcb_msr_read(u64 msr, u64 *value); bool hv_ghcb_negotiate_protocol(void); void __noreturn hv_ghcb_terminate(unsigned int set, unsigned int reason); void hv_vtom_init(void); +int hv_snp_boot_ap(int cpu, unsigned long start_ip); #else static inline void hv_ghcb_msr_write(u64 msr, u64 value) {} static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {} static inline bool hv_ghcb_negotiate_protocol(void) { return false; } static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason= ) {} static inline void hv_vtom_init(void) {} +static int hv_snp_boot_ap(int cpu, unsigned long start_ip) { return 0; } #endif =20 extern bool hv_isolation_type_snp(void); diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c index 5398fb2f4d39..c2ccb49b49c2 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -295,6 +295,16 @@ static void __init hv_smp_prepare_cpus(unsigned int ma= x_cpus) =20 native_smp_prepare_cpus(max_cpus); =20 + /* + * Override wakeup_secondary_cpu_64 callback for SEV-SNP + * enlightened guest. + */ + if (hv_isolation_type_en_snp()) + apic->wakeup_secondary_cpu_64 =3D hv_snp_boot_ap; + + if (!hv_root_partition) + return; + #ifdef CONFIG_X86_64 for_each_present_cpu(i) { if (i =3D=3D 0) @@ -502,8 +512,7 @@ static void __init ms_hyperv_init_platform(void) =20 # ifdef CONFIG_SMP smp_ops.smp_prepare_boot_cpu =3D hv_smp_prepare_boot_cpu; - if (hv_root_partition) - smp_ops.smp_prepare_cpus =3D hv_smp_prepare_cpus; + smp_ops.smp_prepare_cpus =3D hv_smp_prepare_cpus; # endif =20 /* diff --git a/include/asm-generic/hyperv-tlfs.h b/include/asm-generic/hyperv= -tlfs.h index f4e4cc4f965f..fdac4a1714ec 100644 --- a/include/asm-generic/hyperv-tlfs.h +++ b/include/asm-generic/hyperv-tlfs.h @@ -223,6 +223,7 @@ enum HV_GENERIC_SET_FORMAT { #define HV_STATUS_INVALID_PORT_ID 17 #define HV_STATUS_INVALID_CONNECTION_ID 18 #define HV_STATUS_INSUFFICIENT_BUFFERS 19 +#define HV_STATUS_TIME_OUT 120 #define HV_STATUS_VTL_ALREADY_ENABLED 134 =20 /* --=20 2.25.1