x86/sev: Carve out the SVSM support code
cover x86/sev: Carve out the SVSM support code
From: "Borislav Petkov (AMD)" <[email protected]>
Hi,
I've been meaning to do this for a while now but didn't have a good idea how to do it nicely. Using the internal header makes it almost trivial.
Thx.
Borislav Petkov (AMD) (3):
x86/sev: Move the internal header
x86/sev: Add internal header guards
x86/sev: Carve out the SVSM code into a separate compilation unit
arch/x86/boot/startup/sev-startup.c | 3 +-
arch/x86/coco/sev/Makefile | 2 +-
arch/x86/coco/sev/core.c | 380 +-----------------
.../sev-internal.h => coco/sev/internal.h} | 32 ++
arch/x86/coco/sev/noinstr.c | 3 +-
arch/x86/coco/sev/svsm.c | 362 +++++++++++++++++
arch/x86/coco/sev/vc-handle.c | 3 +-
7 files changed, 403 insertions(+), 382 deletions(-)
rename arch/x86/{include/asm/sev-internal.h => coco/sev/internal.h} (75%)
create mode 100644 arch/x86/coco/sev/svsm.cSignature · 1 line
2.51.0
PATCH 1/3 x86/sev: Move the internal header
From: "Borislav Petkov (AMD)" <[email protected]>
Move the internal header out of the usual include/asm/ include path because having an "internal" header there doesn't really make it so
- quite the opposite.
So move where it belongs and make it really internal.
No functional changes.
---
arch/x86/boot/startup/sev-startup.c | 3 ++-
arch/x86/coco/sev/core.c | 3 ++-
arch/x86/{include/asm/sev-internal.h => coco/sev/internal.h} | 0
arch/x86/coco/sev/noinstr.c | 3 ++-
arch/x86/coco/sev/vc-handle.c | 3 ++-
5 files changed, 8 insertions(+), 4 deletions(-)
rename arch/x86/{include/asm/sev-internal.h => coco/sev/internal.h} (100%)arch/x86/boot/startup/sev-startup.c +2 -1
@@ -27,7 +27,6 @@#include <asm/cpu_entry_area.h> #include <asm/stacktrace.h> #include <asm/sev.h> -#include <asm/sev-internal.h> #include <asm/insn-eval.h> #include <asm/fpu/xcr.h> #include <asm/processor.h>
@@ -41,6 +40,8 @@#include <asm/cpuid/api.h> #include <asm/cmdline.h> +#include "../coco/sev/internal.h" +/* Include code shared with pre-decompression boot stage */ #include "sev-shared.c"
arch/x86/coco/sev/core.c +2 -1
@@ -31,7 +31,6 @@#include <asm/cpu_entry_area.h> #include <asm/stacktrace.h> #include <asm/sev.h> -#include <asm/sev-internal.h> #include <asm/insn-eval.h> #include <asm/fpu/xcr.h> #include <asm/processor.h>
@@ -46,6 +45,8 @@#include <asm/cmdline.h> #include <asm/msr.h> +#include "internal.h" +/* Bitmap of SEV features supported by the hypervisor */ u64 sev_hv_features__ro_after_init ;SYM_PIC_ALIAS (sev_hv_features);
arch/x86/coco/sev/internal.h renamed from arch/x86/include/asm/sev-internal.h +0 -0
arch/x86/coco/sev/noinstr.c +2 -1
@@ -16,7 +16,8 @@#include <asm/msr.h> #include <asm/ptrace.h> #include <asm/sev.h> -#include <asm/sev-internal.h> + +#include "internal.h" static __always_inline bool on_vc_stack(struct pt_regs *regs) {
arch/x86/coco/sev/vc-handle.c +2 -1
@@ -23,7 +23,6 @@#include <asm/init.h> #include <asm/stacktrace.h> #include <asm/sev.h> -#include <asm/sev-internal.h> #include <asm/insn-eval.h> #include <asm/fpu/xcr.h> #include <asm/processor.h>
@@ -35,6 +34,8 @@#include <asm/apic.h> #include <asm/cpuid/api.h> +#include "internal.h" +static enum es_result vc_slow_virt_to_phys (struct ghcb *ghcb,struct es_em_ctxt *ctxt,unsigned long vaddr,phys_addr_t *paddr) {
Signature · 1 line
2.51.0
Re: [PATCH 1/3] x86/sev: Move the internal header
Quoted 33 lines · Borislav Petkov
From: "Borislav Petkov (AMD)" <[email protected]>
Move the internal header out of the usual
include/asm/include path because having an "internal" header there doesn't really make it so
- quite the opposite.So move where it belongs and make it really internal.
No functional changes.
Signed-off-by Borislav Petkov (AMD) <[email protected]>--- arch/x86/boot/startup/sev-startup.c | 3 ++- arch/x86/coco/sev/core.c | 3 ++- arch/x86/{include/asm/sev-internal.h => coco/sev/internal.h} | 0 arch/x86/coco/sev/noinstr.c | 3 ++- arch/x86/coco/sev/vc-handle.c | 3 ++- 5 files changed, 8 insertions(+), 4 deletions(-) rename arch/x86/{include/asm/sev-internal.h => coco/sev/internal.h} (100%)arch/x86/boot/startup/sev-startup.c +0
-1@@ -27,7 +27,6 @@#include <asm/cpu_entry_area.h> #include <asm/stacktrace.h> #include <asm/sev.h> -#include <asm/sev-internal.h> #include <asm/insn-eval.h> #include <asm/fpu/xcr.h> #include <asm/processor.h>
arch/x86/boot/startup/sev-startup.c line 43
Quoted 4 lines
arch/x86/boot/startup/sev-startup.c +1
-0@@ -41,6 +40,8 @@#include <asm/cpuid/api.h> #include <asm/cmdline.h> +#include "../coco/sev/internal.h"
Shouldn't this be "../../coco/sev/internal.h" ?
What is strange is that it works with either.
Thanks,
Tom
Quoted 64 lines
arch/x86/boot/startup/sev-startup.c +1
-0+/* Include code shared with pre-decompression boot stage */ #include "sev-shared.c" arch/x86/coco/sev/core.c +2
-1@@ -31,7 +31,6 @@#include <asm/cpu_entry_area.h> #include <asm/stacktrace.h> #include <asm/sev.h> -#include <asm/sev-internal.h> #include <asm/insn-eval.h> #include <asm/fpu/xcr.h> #include <asm/processor.h> @@ -46,6 +45,8 @@#include <asm/cmdline.h> #include <asm/msr.h> +#include "internal.h" +/* Bitmap of SEV features supported by the hypervisor */ u64 sev_hv_features__ro_after_init ;SYM_PIC_ALIAS (sev_hv_features);arch/x86/coco/sev/internal.h renamed from arch/x86/include/asm/sev-internal.h +0
-0arch/x86/coco/sev/noinstr.c +2
-1@@ -16,7 +16,8 @@#include <asm/msr.h> #include <asm/ptrace.h> #include <asm/sev.h> -#include <asm/sev-internal.h> + +#include "internal.h" static __always_inline bool on_vc_stack(struct pt_regs *regs) {arch/x86/coco/sev/vc-handle.c +2
-1@@ -23,7 +23,6 @@#include <asm/init.h> #include <asm/stacktrace.h> #include <asm/sev.h> -#include <asm/sev-internal.h> #include <asm/insn-eval.h> #include <asm/fpu/xcr.h> #include <asm/processor.h> @@ -35,6 +34,8 @@#include <asm/apic.h> #include <asm/cpuid/api.h> +#include "internal.h" +static enum es_result vc_slow_virt_to_phys (struct ghcb *ghcb,struct es_em_ctxt *ctxt,unsigned long vaddr,phys_addr_t *paddr) {
Borislav Petkov
maintainerRe: [PATCH 1/3] x86/sev: Move the internal header
Quoted 5 lines · Tom Lendacky
Quoted 1 line
+#include "
../coco/sev/internal.h"Shouldn't this be "
../../coco/sev/internal.h" ?What is strange is that it works with either.
Pure luck :-\
gcc cmdline is:
gcc -Wp,-MMD,arch/x86/boot/startup/.sev-startup.o.d -nostdinc -I./arch/x86/include...
so the include path becomes:
# 1 "./arch/x86/include/../coco/sev/internal.h" 1
which is
$ readlink -f arch/x86/include/../coco/sev/internal.h /mnt/kernel/kernel/linux/arch/x86/coco/sev/internal.h
And /mnt/kernel is my kernel-sources-containing SSD :-)
In any case, good catch!
I'll do the "../../" so that it is perfectly clear and future potential include directives changes do not break this.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH 2/3 x86/sev: Add internal header guards
In reply to Borislav PetkovFrom: "Borislav Petkov (AMD)" <[email protected]>
All headers need guards ifdeffery.
--- arch/x86/coco/sev/internal.h | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/coco/sev/internal.h +3 -0
@@ -1,4 +1,6 @@/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __X86_COCO_SEV_INTERNAL_H__ +#define __X86_COCO_SEV_INTERNAL_H__#define DR7_RESET_VALUE 0x400
@@ -85,3 +87,4 @@ enum es_result sev_es_ghcb_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxtu64 get_hv_features (void );const struct snp_cpuid_table *snp_cpuid_get_table (void ); +#endif /* __X86_COCO_SEV_INTERNAL_H__ */
Signature · 1 line
2.51.0
[tip: x86/sev] x86/sev: Add internal header guards
The following commit has been merged into the x86/sev branch of tip:
The following commit has been merged into the x86/sev branch of tip:
Commit-ID: f01c6489ad6ceb8d06eae0c5123fc6cf39276ff1 Gitweb: https://git.kernel.org/tip/f01c6489ad6ceb8d06eae0c5123fc6cf39276ff1 Author: Borislav Petkov (AMD) <[email protected]> AuthorDate: Thu, 04 Dec 2025 13:48:07 +01:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Wed, 31 Dec 2025 13:16:06 +01:00
x86/sev: Add internal header guards
All headers need guards ifdeffery.
--- arch/x86/coco/sev/internal.h | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/coco/sev/internal.h +3 -0
@@ -1,4 +1,6 @@/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __X86_COCO_SEV_INTERNAL_H__ +#define __X86_COCO_SEV_INTERNAL_H__#define DR7_RESET_VALUE 0x400
@@ -85,3 +87,4 @@ enum es_result sev_es_ghcb_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxtu64 get_hv_features (void );const struct snp_cpuid_table *snp_cpuid_get_table (void ); +#endif /* __X86_COCO_SEV_INTERNAL_H__ */
PATCH 3/3 x86/sev: Carve out the SVSM code into a separate compilation unit
In reply to Borislav PetkovFrom: "Borislav Petkov (AMD)" <[email protected]>
Move the SVSM-related machinery into a separate compilation unit in order to keep sev/core.c slim and "on-topic".
No functional changes.
--- arch/x86/coco/sev/Makefile | 2 +- arch/x86/coco/sev/core.c | 377 ----------------------------------- arch/x86/coco/sev/internal.h | 29 +++ arch/x86/coco/sev/svsm.c | 362 +++++++++++++++++++++++++++++++++ 4 files changed, 392 insertions(+), 378 deletions(-) create mode 100644 arch/x86/coco/sev/svsm.c
arch/x86/coco/sev/Makefile +1 -1
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 -obj-y += core.o noinstr.o vc-handle.o +obj-y += core.o noinstr.o vc-handle.o svsm.o # Clang 14 and older may fail to respect __no_sanitize_undefined when inlining UBSAN_SANITIZE_noinstr.o := n
arch/x86/coco/sev/core.c +0 -377
407 lines not on this page · raw message
arch/x86/coco/sev/internal.h +29 -0
@@ -66,6 +66,9 @@ extern u64 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); +void svsm_pval_pages (struct snp_psc_desc *desc); +int svsm_perform_call_protocol (struct svsm_call *call); +bool snp_svsm_vtpm_probe (void );static inline u64 sev_es_rd_ghcb_msr (void ) {
@@ -87,4 +90,30 @@ enum es_result sev_es_ghcb_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxtu64 get_hv_features (void );const struct snp_cpuid_table *snp_cpuid_get_table (void ); + +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; +} + +static inline void __pval_terminate (u64 pfn,bool action,unsigned int page_size, +int ret,u64 svsm_ret) +{ +WARN (1 ,"PVALIDATE failure: pfn: 0x%llx, action: %u, size: %u, ret: %d, svsm_ret: 0x%llx\n" , + pfn, action, page_size, ret, svsm_ret); + +sev_es_terminate (SEV_TERM_SET_LINUX ,GHCB_TERM_PVALIDATE ); +} +#endif /* __X86_COCO_SEV_INTERNAL_H__ */
arch/x86/coco/sev/svsm.c new file +362 -0
362 lines not on this page · raw message
Signature · 1 line
2.51.0
[tip: x86/sev] x86/sev: Carve out the SVSM code into a separate compilation unit
The following commit has been merged into the x86/sev branch of tip:
The following commit has been merged into the x86/sev branch of tip:
Commit-ID: e21279b73ef6c7d27237912c914f2db0c5a74786 Gitweb: https://git.kernel.org/tip/e21279b73ef6c7d27237912c914f2db0c5a74786 Author: Borislav Petkov (AMD) <[email protected]> AuthorDate: Thu, 04 Dec 2025 13:48:08 +01:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Wed, 31 Dec 2025 13:18:23 +01:00
x86/sev: Carve out the SVSM code into a separate compilation unit
Move the SVSM-related machinery into a separate compilation unit in order to keep sev/core.c slim and "on-topic".
No functional changes.
--- arch/x86/coco/sev/Makefile | 2 +- arch/x86/coco/sev/core.c | 377 +---------------------------------- arch/x86/coco/sev/internal.h | 29 +++- arch/x86/coco/sev/svsm.c | 362 +++++++++++++++++++++++++++++++++- 4 files changed, 392 insertions(+), 378 deletions(-) create mode 100644 arch/x86/coco/sev/svsm.c
arch/x86/coco/sev/Makefile +1 -1
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 -obj-y += core.o noinstr.o vc-handle.o +obj-y += core.o noinstr.o vc-handle.o svsm.o # Clang 14 and older may fail to respect __no_sanitize_undefined when inlining UBSAN_SANITIZE_noinstr.o := n
arch/x86/coco/sev/core.c +0 -377
407 lines not on this page · raw message
arch/x86/coco/sev/internal.h +29 -0
@@ -66,6 +66,9 @@ extern u64 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); +void svsm_pval_pages (struct snp_psc_desc *desc); +int svsm_perform_call_protocol (struct svsm_call *call); +bool snp_svsm_vtpm_probe (void );static inline u64 sev_es_rd_ghcb_msr (void ) {
@@ -87,4 +90,30 @@ enum es_result sev_es_ghcb_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxtu64 get_hv_features (void );const struct snp_cpuid_table *snp_cpuid_get_table (void ); + +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; +} + +static inline void __pval_terminate (u64 pfn,bool action,unsigned int page_size, +int ret,u64 svsm_ret) +{ +WARN (1 ,"PVALIDATE failure: pfn: 0x%llx, action: %u, size: %u, ret: %d, svsm_ret: 0x%llx\n" , + pfn, action, page_size, ret, svsm_ret); + +sev_es_terminate (SEV_TERM_SET_LINUX ,GHCB_TERM_PVALIDATE ); +} +#endif /* __X86_COCO_SEV_INTERNAL_H__ */
arch/x86/coco/sev/svsm.c new file +362 -0
362 lines not on this page · raw message
Quoted 6 lines · Borislav Petkov
From: "Borislav Petkov (AMD)" <[email protected]>
Hi,
I've been meaning to do this for a while now but didn't have a good idea how to do it nicely. Using the internal header makes it almost trivial.
Quick testing doesn't show any issues.
Quoted 19 lines
Thx.
Borislav Petkov (AMD) (3):
x86/sev: Move the internal header
x86/sev: Add internal header guards
x86/sev: Carve out the SVSM code into a separate compilation unitarch/x86/boot/startup/sev-startup.c | 3 +- arch/x86/coco/sev/Makefile | 2 +- arch/x86/coco/sev/core.c | 380 +----------------- .../sev-internal.h => coco/sev/internal.h} | 32 ++ arch/x86/coco/sev/noinstr.c | 3 +- arch/x86/coco/sev/svsm.c | 362 +++++++++++++++++ arch/x86/coco/sev/vc-handle.c | 3 +- 7 files changed, 403 insertions(+), 382 deletions(-) rename arch/x86/{include/asm/sev-internal.h => coco/sev/internal.h} (75%) create mode 100644 arch/x86/coco/sev/svsm.c
Borislav Petkov
maintainerQuoted 3 lines · Tom Lendacky
Quick testing doesn't show any issues.
Reviewed-by Tom Lendacky <[email protected]>
Much appreciated, thanks!
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette