Skip to content

Commit 69add17

Browse files
joergroedelsuryasaimadhu
authored andcommitted
x86/boot/compressed/64: Unmap GHCB page before booting the kernel
Force a page-fault on any further accesses to the GHCB page when they shouldn't happen anymore. This will catch any bugs where a #VC exception is raised even though none is expected anymore. Signed-off-by: Joerg Roedel <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 597cfe4 commit 69add17

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

arch/x86/boot/compressed/ident_map_64.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ int set_page_encrypted(unsigned long address)
298298
return set_clr_page_flags(&mapping_info, address, _PAGE_ENC, 0);
299299
}
300300

301+
int set_page_non_present(unsigned long address)
302+
{
303+
return set_clr_page_flags(&mapping_info, address, 0, _PAGE_PRESENT);
304+
}
305+
301306
static void do_pf_error(const char *msg, unsigned long error_code,
302307
unsigned long address, unsigned long ip)
303308
{
@@ -316,8 +321,14 @@ static void do_pf_error(const char *msg, unsigned long error_code,
316321

317322
void do_boot_page_fault(struct pt_regs *regs, unsigned long error_code)
318323
{
319-
unsigned long address = native_read_cr2() & PMD_MASK;
320-
unsigned long end = address + PMD_SIZE;
324+
unsigned long address = native_read_cr2();
325+
unsigned long end;
326+
bool ghcb_fault;
327+
328+
ghcb_fault = sev_es_check_ghcb_fault(address);
329+
330+
address &= PMD_MASK;
331+
end = address + PMD_SIZE;
321332

322333
/*
323334
* Check for unexpected error codes. Unexpected are:
@@ -327,6 +338,8 @@ void do_boot_page_fault(struct pt_regs *regs, unsigned long error_code)
327338
*/
328339
if (error_code & (X86_PF_PROT | X86_PF_USER | X86_PF_RSVD))
329340
do_pf_error("Unexpected page-fault:", error_code, address, regs->ip);
341+
else if (ghcb_fault)
342+
do_pf_error("Page-fault on GHCB page:", error_code, address, regs->ip);
330343

331344
/*
332345
* Error code is sane - now identity map the 2M region around

arch/x86/boot/compressed/misc.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ static inline void choose_random_location(unsigned long input,
100100
#ifdef CONFIG_X86_64
101101
extern int set_page_decrypted(unsigned long address);
102102
extern int set_page_encrypted(unsigned long address);
103+
extern int set_page_non_present(unsigned long address);
103104
extern unsigned char _pgtable[];
104105
#endif
105106

@@ -117,8 +118,13 @@ void set_sev_encryption_mask(void);
117118

118119
#ifdef CONFIG_AMD_MEM_ENCRYPT
119120
void sev_es_shutdown_ghcb(void);
121+
extern bool sev_es_check_ghcb_fault(unsigned long address);
120122
#else
121123
static inline void sev_es_shutdown_ghcb(void) { }
124+
static inline bool sev_es_check_ghcb_fault(unsigned long address)
125+
{
126+
return false;
127+
}
122128
#endif
123129

124130
/* acpi.c */

arch/x86/boot/compressed/sev-es.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ void sev_es_shutdown_ghcb(void)
121121
*/
122122
if (set_page_encrypted((unsigned long)&boot_ghcb_page))
123123
error("Can't map GHCB page encrypted");
124+
125+
/*
126+
* GHCB page is mapped encrypted again and flushed from the cache.
127+
* Mark it non-present now to catch bugs when #VC exceptions trigger
128+
* after this point.
129+
*/
130+
if (set_page_non_present((unsigned long)&boot_ghcb_page))
131+
error("Can't unmap GHCB page");
132+
}
133+
134+
bool sev_es_check_ghcb_fault(unsigned long address)
135+
{
136+
/* Check whether the fault was on the GHCB page */
137+
return ((address & PAGE_MASK) == (unsigned long)&boot_ghcb_page);
124138
}
125139

126140
void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)

0 commit comments

Comments
 (0)