Skip to content

Commit 885689e

Browse files
tlendackysuryasaimadhu
authored andcommitted
x86/sev-es: Setup per-CPU GHCBs for the runtime handler
The runtime handler needs one GHCB per-CPU. Set them up and map them unencrypted. [ bp: Touchups and simplification. ] Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com> Signed-off-by: Joerg Roedel <jroedel@suse.de> Signed-off-by: Borislav Petkov <bp@suse.de> Link: https://lkml.kernel.org/r/20200907131613.12703-42-joro@8bytes.org
1 parent 1aa9aa8 commit 885689e

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

arch/x86/include/asm/mem_encrypt.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void __init mem_encrypt_free_decrypted_mem(void);
4949
/* Architecture __weak replacement functions */
5050
void __init mem_encrypt_init(void);
5151

52+
void __init sev_es_init_vc_handling(void);
5253
bool sme_active(void);
5354
bool sev_active(void);
5455
bool sev_es_active(void);
@@ -72,6 +73,7 @@ static inline void __init sme_early_init(void) { }
7273
static inline void __init sme_encrypt_kernel(struct boot_params *bp) { }
7374
static inline void __init sme_enable(struct boot_params *bp) { }
7475

76+
static inline void sev_es_init_vc_handling(void) { }
7577
static inline bool sme_active(void) { return false; }
7678
static inline bool sev_active(void) { return false; }
7779
static inline bool sev_es_active(void) { return false; }

arch/x86/kernel/sev-es.c

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
*/
99

1010
#include <linux/sched/debug.h> /* For show_regs() */
11-
#include <linux/kernel.h>
11+
#include <linux/percpu-defs.h>
12+
#include <linux/mem_encrypt.h>
1213
#include <linux/printk.h>
14+
#include <linux/mm_types.h>
15+
#include <linux/set_memory.h>
16+
#include <linux/memblock.h>
17+
#include <linux/kernel.h>
1318
#include <linux/mm.h>
1419

1520
#include <asm/sev-es.h>
@@ -29,6 +34,13 @@ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);
2934
*/
3035
static struct ghcb __initdata *boot_ghcb;
3136

37+
/* #VC handler runtime per-CPU data */
38+
struct sev_es_runtime_data {
39+
struct ghcb ghcb_page;
40+
};
41+
42+
static DEFINE_PER_CPU(struct sev_es_runtime_data*, runtime_data);
43+
3244
/* Needed in vc_early_forward_exception */
3345
void do_early_exception(struct pt_regs *regs, int trapnr);
3446

@@ -198,6 +210,48 @@ static bool __init sev_es_setup_ghcb(void)
198210
return true;
199211
}
200212

213+
static void __init alloc_runtime_data(int cpu)
214+
{
215+
struct sev_es_runtime_data *data;
216+
217+
data = memblock_alloc(sizeof(*data), PAGE_SIZE);
218+
if (!data)
219+
panic("Can't allocate SEV-ES runtime data");
220+
221+
per_cpu(runtime_data, cpu) = data;
222+
}
223+
224+
static void __init init_ghcb(int cpu)
225+
{
226+
struct sev_es_runtime_data *data;
227+
int err;
228+
229+
data = per_cpu(runtime_data, cpu);
230+
231+
err = early_set_memory_decrypted((unsigned long)&data->ghcb_page,
232+
sizeof(data->ghcb_page));
233+
if (err)
234+
panic("Can't map GHCBs unencrypted");
235+
236+
memset(&data->ghcb_page, 0, sizeof(data->ghcb_page));
237+
}
238+
239+
void __init sev_es_init_vc_handling(void)
240+
{
241+
int cpu;
242+
243+
BUILD_BUG_ON(offsetof(struct sev_es_runtime_data, ghcb_page) % PAGE_SIZE);
244+
245+
if (!sev_es_active())
246+
return;
247+
248+
/* Initialize per-cpu GHCB pages */
249+
for_each_possible_cpu(cpu) {
250+
alloc_runtime_data(cpu);
251+
init_ghcb(cpu);
252+
}
253+
}
254+
201255
static void __init vc_early_forward_exception(struct es_em_ctxt *ctxt)
202256
{
203257
int trapnr = ctxt->fi.vector;

arch/x86/kernel/traps.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,9 @@ void __init trap_init(void)
10741074
/* Init cpu_entry_area before IST entries are set up */
10751075
setup_cpu_entry_areas();
10761076

1077+
/* Init GHCB memory pages when running as an SEV-ES guest */
1078+
sev_es_init_vc_handling();
1079+
10771080
idt_setup_traps();
10781081

10791082
/*

0 commit comments

Comments
 (0)