From: Gerd Hoffmann kraxel@redhat.com
[ Upstream commit 7b22e0432981c2fa230f1b493082b7e67112c4aa ]
In case efi_mm is active go use the userspace instruction decoder which supports fetching instructions from active_mm. This is needed to make instruction emulation work for EFI runtime code, so it can use CPUID and RDMSR.
EFI runtime code uses the CPUID instruction to gather information about the environment it is running in, such as SEV being enabled or not, and choose (if needed) the SEV code path for ioport access.
EFI runtime code uses the RDMSR instruction to get the location of the CAA page (see SVSM spec, section 4.2 - "Post Boot").
The big picture behind this is that the kernel needs to be able to properly handle #VC exceptions that come from EFI runtime services. Since EFI runtime services have a special page table mapping for the EFI virtual address space, the efi_mm context must be used when decoding instructions during #VC handling.
[ bp: Massage. ]
Signed-off-by: Gerd Hoffmann kraxel@redhat.com Signed-off-by: Borislav Petkov (AMD) bp@alien8.de Reviewed-by: Pankaj Gupta pankaj.gupta@amd.com Link: https://lore.kernel.org/20250626114014.373748-2-kraxel@redhat.com Signed-off-by: Sasha Levin sashal@kernel.org ---
LLM Generated explanations, may be completely bogus:
Based on my analysis of the commit message and code changes, I can provide my assessment:
**Backport Status: YES**
This commit should be backported to stable kernel trees for the following reasons:
1. **Fixes a real bug affecting users**: The commit fixes instruction emulation for EFI runtime services in SEV-enabled systems. Without this fix, EFI runtime services cannot properly use CPUID and RDMSR instructions, which are essential for: - Determining if SEV is enabled - Getting the location of the CAA page (per SVSM spec) - Choosing the correct code path for I/O port access
2. **Small and contained fix**: The change is minimal - it only modifies the `vc_decode_insn()` function in arch/x86/coco/sev/vc-handle.c by adding a single condition check: `|| mm_is_efi(current->active_mm)`. This additional check ensures that when EFI runtime services are active (using efi_mm), the userspace instruction decoder is used instead of the kernel decoder.
3. **No architectural changes**: The fix doesn't introduce any new features or change the architecture. It simply fixes the existing instruction decoding logic to handle the special case of EFI runtime services.
4. **Critical for SEV functionality**: Systems with AMD SEV (Secure Encrypted Virtualization) enabled need proper #VC exception handling for EFI runtime services to function correctly. Without this fix, these systems may experience failures when EFI runtime services attempt to execute CPUID or RDMSR instructions.
5. **Low regression risk**: The change only affects the code path when `mm_is_efi()` returns true, which is a very specific scenario (EFI runtime services execution). It doesn't modify the existing behavior for normal kernel or userspace instruction decoding.
6. **Clear problem and solution**: The commit message clearly explains the issue (EFI runtime services use special page table mappings that require the userspace decoder) and the solution is straightforward.
The fix is essential for proper operation of SEV-enabled systems that rely on EFI runtime services, making it a good candidate for stable backporting according to stable tree rules.
arch/x86/coco/sev/vc-handle.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/arch/x86/coco/sev/vc-handle.c b/arch/x86/coco/sev/vc-handle.c index 0989d98da130..faf1fce89ed4 100644 --- a/arch/x86/coco/sev/vc-handle.c +++ b/arch/x86/coco/sev/vc-handle.c @@ -17,6 +17,7 @@ #include <linux/mm.h> #include <linux/io.h> #include <linux/psp-sev.h> +#include <linux/efi.h> #include <uapi/linux/sev-guest.h>
#include <asm/init.h> @@ -178,9 +179,15 @@ static enum es_result __vc_decode_kern_insn(struct es_em_ctxt *ctxt) return ES_OK; }
+/* + * User instruction decoding is also required for the EFI runtime. Even though + * the EFI runtime is running in kernel mode, it uses special EFI virtual + * address mappings that require the use of efi_mm to properly address and + * decode. + */ static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt) { - if (user_mode(ctxt->regs)) + if (user_mode(ctxt->regs) || mm_is_efi(current->active_mm)) return __vc_decode_user_insn(ctxt); else return __vc_decode_kern_insn(ctxt);