From nobody Sun Sep 7 12:19:02 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A31D8287257 for ; Wed, 16 Jul 2025 03:22:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752636160; cv=none; b=t1z1Zbh+37ZUQW1uhQbStNYWAf7LX15toOLWppOuuxockIHhfVdkxQj9IQAgfvp+C00IOCwtp2k5z0J5mx4i9ry6/mrGmId6IM02Wy/xG4IQUAQHOvT+/j6JXArbbY9Or/XgpbTu603wlRqVHsb8cyBnFrEQGkrtfavwb8vtzR8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752636160; c=relaxed/simple; bh=c6F8U57CNwQO9LBMP2pJv8Ns/R2toG/98czmofLbweE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aCiVs/U6Fk2LKZsqZfdxykA8raOXExzuCSAUV1Ay72q+M2KWAIB31w20I2eAxRf6O+y3ZYbyzcumbXYxjQWJybsK30vaOUxoDx4bjwWEwmrHFnpEyw59ZLX+TNwyBZCW8qUebRiUGkNyRhSGjemFtDhgn3IChUOsEIOrVyv7k4k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=j1yuh360; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="j1yuh360" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-456175dba68so22662655e9.2 for ; Tue, 15 Jul 2025 20:22:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752636157; x=1753240957; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=my9gyPV7GpUhZ0j1D85su1Gd7h1qi/PaVo/yjWf1hSg=; b=j1yuh360uLJkLKerY0MTIVLVui4kUsnlJmu6oVFwHrwbkPtsrr4SyIo+HpbyluBG1D 1g6TjwxUBMDevQGRrjQVWMIhl3YLEYyUFnHmCr3t7y1qDHC6wytA1qYUgwTwQdTSIUx3 tKzrpc65HyqvkWyARsbwr+QdWxntw9n3tc/1bThJmjr5BeGLmCRq6CmOSMQIda2j9NsK EeHV9WbXwBcht9MkBXU3W8PduI8NGGr1MHm9Fp9Z4a2Dn1zBf2g6hHxI1EG527BT6pFx DCv2NCnlx1lVjv9AOEMLDw8tXJktFJ+8ITwA361dnsMwwKtlNJ9jygDCLaiifY9xWz8s JH0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752636157; x=1753240957; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=my9gyPV7GpUhZ0j1D85su1Gd7h1qi/PaVo/yjWf1hSg=; b=CwQkcB5KPCBpJBf/dsMh0gKFfo7jxh7Nn9KlEDYSQpUR6Rp/9f+TmOV3SxBZSl091l u2GwKqbUDg1IRhTeY5aTERNZBX8+AYDEo4RxosAs2eiRp1VbEVUJBz5DztYObKRuiAz2 U7SBrkk9YW2CbvO3vnd0kRM7+5jBTOh909tnR4rrEt7VgI76aUNEjhnssBcN9CWn12RG moODHTkpA6XW9EWJvN5cMZfhqd53qWn1G6iGuj/9fdvhpzh/Z08BYH/bCuwDHGgpZMwE gXb8Dg7eicQxOIQpXGuyOQCAznSHVBWNrk514PTYSG/uyJCcmrom+zkjm1yHGA3HXhXe IDgQ== X-Gm-Message-State: AOJu0YwUdJ3Pk0I2U3hmHpg9FTTbvZgoAl+8lHXcgRoSS5Ajsk/YiHK/ BY2YO/JzyVppkm5Xwf0TBzBLaCUHNGp+BYIGju/9LvYr+tJXooQEsIZHsnWPWi4xRWY1OnxU148 RKhPxUq+Y30GP/IZqOjZdVpdwaUxmODeynr9On4m9ZfAYd3sRcjnnPbTGDeNp9uHh8zaebCQY/M SjVttBfC/47wLxkfqCbIM1iScNdElv6KpWBA== X-Google-Smtp-Source: AGHT+IGslqQ23Wi8fxCMq+Pc6u3PM8vlZFV0M8FN7RcONMrupNbWvmshZEZh+3xpbkolkKhaXNe4Bgof X-Received: from wmrn13.prod.google.com ([2002:a05:600c:500d:b0:456:1d9b:2a5d]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:64ce:b0:455:f59e:fd79 with SMTP id 5b1f17b1804b1-4562e33d64emr8756805e9.11.1752636157081; Tue, 15 Jul 2025 20:22:37 -0700 (PDT) Date: Wed, 16 Jul 2025 05:18:24 +0200 In-Reply-To: <20250716031814.2096113-24-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250716031814.2096113-24-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=5481; i=ardb@kernel.org; h=from:subject; bh=CWmGYplgo2z5qlfvc2ln3Mz6KFj9XYcKFlTVXqEMNjg=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIaNcSvs7Z4JTaMLj76+4J5wQy3utepUlOVFfQd1hT0PJU TdXI/eOUhYGMS4GWTFFFoHZf9/tPD1RqtZ5lizMHFYmkCEMXJwCMBGHSob/Uc8XmW3JXa40wZrl +ZQfpcJ8V2vXJ4u7L7SN/dCclDEzmJHh2gnnOx87JV/wNnH23LO00O02y/rDc79moqX0kh8pF9R 4AA== X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250716031814.2096113-33-ardb+git@google.com> Subject: [PATCH v5 09/22] x86/sev: Use boot SVSM CA for all startup and init code From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , Borislav Petkov , Ingo Molnar , Kevin Loughlin , Tom Lendacky , Josh Poimboeuf , Peter Zijlstra , Nikunj A Dadhania Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel To avoid having to reason about whether or not to use the per-CPU SVSM calling area when running startup and init code on the boot CPU, reuse the boot SVSM calling area as the per-CPU area for CPU #0. This removes the need to make the per-CPU variables and associated state in sev_cfg accessible to the startup code once confined. Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/sev.c | 13 ------ arch/x86/boot/startup/sev-startup.c | 7 +-- arch/x86/coco/sev/core.c | 47 +++++++++----------- arch/x86/include/asm/sev-internal.h | 16 ------- 4 files changed, 24 insertions(+), 59 deletions(-) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index 18b0ccf517eb..4bdf5595ed96 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -37,19 +37,6 @@ struct ghcb *boot_ghcb; =20 #define __BOOT_COMPRESSED =20 -extern u64 boot_svsm_caa_pa; - -struct svsm_ca *svsm_get_caa(void) -{ - /* The decompressor is mapped 1:1 so VA =3D=3D PA */ - return (struct svsm_ca *)boot_svsm_caa_pa; -} - -u64 svsm_get_caa_pa(void) -{ - return boot_svsm_caa_pa; -} - u8 snp_vmpl; =20 /* Include code for early handlers */ diff --git a/arch/x86/boot/startup/sev-startup.c b/arch/x86/boot/startup/se= v-startup.c index 7aabda0b378e..8e804369cc60 100644 --- a/arch/x86/boot/startup/sev-startup.c +++ b/arch/x86/boot/startup/sev-startup.c @@ -50,9 +50,6 @@ u64 sev_secrets_pa __ro_after_init; /* For early boot SVSM communication */ struct svsm_ca boot_svsm_ca_page __aligned(PAGE_SIZE); =20 -DEFINE_PER_CPU(struct svsm_ca *, svsm_caa); -DEFINE_PER_CPU(u64, svsm_caa_pa); - /* * Nothing shall interrupt this code path while holding the per-CPU * GHCB. The backup GHCB is only for NMIs interrupting this path. @@ -167,7 +164,7 @@ void __head early_snp_set_memory_private(unsigned long = vaddr, unsigned long padd * table. */ early_set_pages_state(vaddr, paddr, npages, SNP_PAGE_STATE_PRIVATE, - svsm_get_caa(), svsm_get_caa_pa()); + rip_rel_ptr(&boot_svsm_ca_page), boot_svsm_caa_pa); } =20 void __head early_snp_set_memory_shared(unsigned long vaddr, unsigned long= paddr, @@ -184,7 +181,7 @@ void __head early_snp_set_memory_shared(unsigned long v= addr, unsigned long paddr =20 /* Ask hypervisor to mark the memory pages shared in the RMP table. */ early_set_pages_state(vaddr, paddr, npages, SNP_PAGE_STATE_SHARED, - svsm_get_caa(), svsm_get_caa_pa()); + rip_rel_ptr(&boot_svsm_ca_page), boot_svsm_caa_pa); } =20 /* diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c index 7a86a2fe494d..4fe0928bc0ad 100644 --- a/arch/x86/coco/sev/core.c +++ b/arch/x86/coco/sev/core.c @@ -46,6 +46,25 @@ #include #include =20 +DEFINE_PER_CPU(struct svsm_ca *, svsm_caa); +DEFINE_PER_CPU(u64, svsm_caa_pa); + +static inline struct svsm_ca *svsm_get_caa(void) +{ + if (sev_cfg.use_cas) + return this_cpu_read(svsm_caa); + else + return rip_rel_ptr(&boot_svsm_ca_page); +} + +static inline u64 svsm_get_caa_pa(void) +{ + if (sev_cfg.use_cas) + return this_cpu_read(svsm_caa_pa); + else + return boot_svsm_caa_pa; +} + /* AP INIT values as documented in the APM2 section "Processor Initializa= tion State" */ #define AP_INIT_CS_LIMIT 0xffff #define AP_INIT_DS_LIMIT 0xffff @@ -1287,7 +1306,8 @@ static void __init alloc_runtime_data(int cpu) struct svsm_ca *caa; =20 /* Allocate the SVSM CA page if an SVSM is present */ - caa =3D memblock_alloc_or_panic(sizeof(*caa), PAGE_SIZE); + caa =3D cpu ? memblock_alloc_or_panic(sizeof(*caa), PAGE_SIZE) + : &boot_svsm_ca_page; =20 per_cpu(svsm_caa, cpu) =3D caa; per_cpu(svsm_caa_pa, cpu) =3D __pa(caa); @@ -1341,32 +1361,9 @@ void __init sev_es_init_vc_handling(void) init_ghcb(cpu); } =20 - /* If running under an SVSM, switch to the per-cpu CA */ - if (snp_vmpl) { - struct svsm_call call =3D {}; - unsigned long flags; - int ret; - - local_irq_save(flags); - - /* - * SVSM_CORE_REMAP_CA call: - * RAX =3D 0 (Protocol=3D0, CallID=3D0) - * RCX =3D New CA GPA - */ - call.caa =3D svsm_get_caa(); - call.rax =3D SVSM_CORE_CALL(SVSM_CORE_REMAP_CA); - call.rcx =3D this_cpu_read(svsm_caa_pa); - ret =3D svsm_perform_call_protocol(&call); - if (ret) - panic("Can't remap the SVSM CA, ret=3D%d, rax_out=3D0x%llx\n", - ret, call.rax_out); - + if (snp_vmpl) sev_cfg.use_cas =3D true; =20 - local_irq_restore(flags); - } - sev_es_setup_play_dead(); =20 /* Secondary CPUs use the runtime #VC handler */ diff --git a/arch/x86/include/asm/sev-internal.h b/arch/x86/include/asm/sev= -internal.h index 3b72d8217827..bdfe008120f3 100644 --- a/arch/x86/include/asm/sev-internal.h +++ b/arch/x86/include/asm/sev-internal.h @@ -63,22 +63,6 @@ DECLARE_PER_CPU(u64, svsm_caa_pa); =20 extern u64 boot_svsm_caa_pa; =20 -static __always_inline struct svsm_ca *svsm_get_caa(void) -{ - if (sev_cfg.use_cas) - return this_cpu_read(svsm_caa); - else - return rip_rel_ptr(&boot_svsm_ca_page); -} - -static __always_inline u64 svsm_get_caa_pa(void) -{ - if (sev_cfg.use_cas) - return this_cpu_read(svsm_caa_pa); - else - return boot_svsm_caa_pa; -} - enum es_result verify_exception_info(struct ghcb *ghcb, struct es_em_ctxt = *ctxt); void vc_forward_exception(struct es_em_ctxt *ctxt); =20 --=20 2.50.0.727.gbf7dc18ff4-goog