Skip to content

Commit 5ce344b

Browse files
AdamCDunlapbp3tk0v
authored andcommitted
x86/apic: Force native_apic_mem_read() to use the MOV instruction
When done from a virtual machine, instructions that touch APIC memory must be emulated. By convention, MMIO accesses are typically performed via io.h helpers such as readl() or writeq() to simplify instruction emulation/decoding (ex: in KVM hosts and SEV guests) [0]. Currently, native_apic_mem_read() does not follow this convention, allowing the compiler to emit instructions other than the MOV instruction generated by readl(). In particular, when the kernel is compiled with clang and run as a SEV-ES or SEV-SNP guest, the compiler would emit a TESTL instruction which is not supported by the SEV-ES emulator, causing a boot failure in that environment. It is likely the same problem would happen in a TDX guest as that uses the same instruction emulator as SEV-ES. To make sure all emulators can emulate APIC memory reads via MOV, use the readl() function in native_apic_mem_read(). It is expected that any emulator would support MOV in any addressing mode as it is the most generic and is what is usually emitted currently. The TESTL instruction is emitted when native_apic_mem_read() is inlined into apic_mem_wait_icr_idle(). The emulator comes from insn_decode_mmio() in arch/x86/lib/insn-eval.c. It's not worth it to extend insn_decode_mmio() to support more instructions since, in theory, the compiler could choose to output nearly any instruction for such reads which would bloat the emulator beyond reason. [0] https://lore.kernel.org/all/[email protected]/ [ bp: Massage commit message, fix typos. ] Signed-off-by: Adam Dunlap <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Reviewed-by: Thomas Gleixner <[email protected]> Reviewed-by: Ard Biesheuvel <[email protected]> Tested-by: Kevin Loughlin <[email protected]> Cc: <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent fec50db commit 5ce344b

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

arch/x86/include/asm/apic.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <asm/mpspec.h>
1414
#include <asm/msr.h>
1515
#include <asm/hardirq.h>
16+
#include <asm/io.h>
1617

1718
#define ARCH_APICTIMER_STOPS_ON_C3 1
1819

@@ -98,7 +99,7 @@ static inline void native_apic_mem_write(u32 reg, u32 v)
9899

99100
static inline u32 native_apic_mem_read(u32 reg)
100101
{
101-
return *((volatile u32 *)(APIC_BASE + reg));
102+
return readl((void __iomem *)(APIC_BASE + reg));
102103
}
103104

104105
static inline void native_apic_mem_eoi(void)

0 commit comments

Comments
 (0)