Skip to content

Commit 44676bb

Browse files
tialaliuw
authored andcommitted
x86/hyperv: Add smp support for SEV-SNP guest
In the AMD SEV-SNP guest, AP needs to be started up via sev es save area and Hyper-V requires to call HVCALL_START_VP hypercall to pass the gpa of sev es save area with AP's vp index and VTL(Virtual trust level) parameters. Override wakeup_secondary_cpu_64 callback with hv_snp_boot_ap. Reviewed-by: Dexuan Cui <decui@microsoft.com> Reviewed-by: Michael Kelley <mikelley@microsoft.com> Signed-off-by: Tianyu Lan <tiala@microsoft.com> Signed-off-by: Wei Liu <wei.liu@kernel.org> Link: https://lore.kernel.org/r/20230818102919.1318039-8-ltykernel@gmail.com
1 parent 45f46b1 commit 44676bb

File tree

4 files changed

+157
-1
lines changed

4 files changed

+157
-1
lines changed

arch/x86/hyperv/ivm.c

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,20 @@
1818
#include <asm/mshyperv.h>
1919
#include <asm/hypervisor.h>
2020
#include <asm/mtrr.h>
21+
#include <asm/coco.h>
22+
#include <asm/io_apic.h>
23+
#include <asm/sev.h>
24+
#include <asm/realmode.h>
25+
#include <asm/e820/api.h>
26+
#include <asm/desc.h>
2127

2228
#ifdef CONFIG_AMD_MEM_ENCRYPT
2329

2430
#define GHCB_USAGE_HYPERV_CALL 1
2531

32+
static u8 ap_start_input_arg[PAGE_SIZE] __bss_decrypted __aligned(PAGE_SIZE);
33+
static u8 ap_start_stack[PAGE_SIZE] __aligned(PAGE_SIZE);
34+
2635
union hv_ghcb {
2736
struct ghcb ghcb;
2837
struct {
@@ -56,6 +65,8 @@ union hv_ghcb {
5665
} hypercall;
5766
} __packed __aligned(HV_HYP_PAGE_SIZE);
5867

68+
static DEFINE_PER_CPU(struct sev_es_save_area *, hv_sev_vmsa);
69+
5970
static u16 hv_ghcb_version __ro_after_init;
6071

6172
u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size)
@@ -357,6 +368,133 @@ static bool hv_is_private_mmio(u64 addr)
357368
return false;
358369
}
359370

371+
#define hv_populate_vmcb_seg(seg, gdtr_base) \
372+
do { \
373+
if (seg.selector) { \
374+
seg.base = 0; \
375+
seg.limit = HV_AP_SEGMENT_LIMIT; \
376+
seg.attrib = *(u16 *)(gdtr_base + seg.selector + 5); \
377+
seg.attrib = (seg.attrib & 0xFF) | ((seg.attrib >> 4) & 0xF00); \
378+
} \
379+
} while (0) \
380+
381+
static int snp_set_vmsa(void *va, bool vmsa)
382+
{
383+
u64 attrs;
384+
385+
/*
386+
* Running at VMPL0 allows the kernel to change the VMSA bit for a page
387+
* using the RMPADJUST instruction. However, for the instruction to
388+
* succeed it must target the permissions of a lesser privileged
389+
* (higher numbered) VMPL level, so use VMPL1 (refer to the RMPADJUST
390+
* instruction in the AMD64 APM Volume 3).
391+
*/
392+
attrs = 1;
393+
if (vmsa)
394+
attrs |= RMPADJUST_VMSA_PAGE_BIT;
395+
396+
return rmpadjust((unsigned long)va, RMP_PG_SIZE_4K, attrs);
397+
}
398+
399+
static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa)
400+
{
401+
int err;
402+
403+
err = snp_set_vmsa(vmsa, false);
404+
if (err)
405+
pr_err("clear VMSA page failed (%u), leaking page\n", err);
406+
else
407+
free_page((unsigned long)vmsa);
408+
}
409+
410+
int hv_snp_boot_ap(int cpu, unsigned long start_ip)
411+
{
412+
struct sev_es_save_area *vmsa = (struct sev_es_save_area *)
413+
__get_free_page(GFP_KERNEL | __GFP_ZERO);
414+
struct sev_es_save_area *cur_vmsa;
415+
struct desc_ptr gdtr;
416+
u64 ret, retry = 5;
417+
struct hv_enable_vp_vtl *start_vp_input;
418+
unsigned long flags;
419+
420+
if (!vmsa)
421+
return -ENOMEM;
422+
423+
native_store_gdt(&gdtr);
424+
425+
vmsa->gdtr.base = gdtr.address;
426+
vmsa->gdtr.limit = gdtr.size;
427+
428+
asm volatile("movl %%es, %%eax;" : "=a" (vmsa->es.selector));
429+
hv_populate_vmcb_seg(vmsa->es, vmsa->gdtr.base);
430+
431+
asm volatile("movl %%cs, %%eax;" : "=a" (vmsa->cs.selector));
432+
hv_populate_vmcb_seg(vmsa->cs, vmsa->gdtr.base);
433+
434+
asm volatile("movl %%ss, %%eax;" : "=a" (vmsa->ss.selector));
435+
hv_populate_vmcb_seg(vmsa->ss, vmsa->gdtr.base);
436+
437+
asm volatile("movl %%ds, %%eax;" : "=a" (vmsa->ds.selector));
438+
hv_populate_vmcb_seg(vmsa->ds, vmsa->gdtr.base);
439+
440+
vmsa->efer = native_read_msr(MSR_EFER);
441+
442+
asm volatile("movq %%cr4, %%rax;" : "=a" (vmsa->cr4));
443+
asm volatile("movq %%cr3, %%rax;" : "=a" (vmsa->cr3));
444+
asm volatile("movq %%cr0, %%rax;" : "=a" (vmsa->cr0));
445+
446+
vmsa->xcr0 = 1;
447+
vmsa->g_pat = HV_AP_INIT_GPAT_DEFAULT;
448+
vmsa->rip = (u64)secondary_startup_64_no_verify;
449+
vmsa->rsp = (u64)&ap_start_stack[PAGE_SIZE];
450+
451+
/*
452+
* Set the SNP-specific fields for this VMSA:
453+
* VMPL level
454+
* SEV_FEATURES (matches the SEV STATUS MSR right shifted 2 bits)
455+
*/
456+
vmsa->vmpl = 0;
457+
vmsa->sev_features = sev_status >> 2;
458+
459+
ret = snp_set_vmsa(vmsa, true);
460+
if (!ret) {
461+
pr_err("RMPADJUST(%llx) failed: %llx\n", (u64)vmsa, ret);
462+
free_page((u64)vmsa);
463+
return ret;
464+
}
465+
466+
local_irq_save(flags);
467+
start_vp_input = (struct hv_enable_vp_vtl *)ap_start_input_arg;
468+
memset(start_vp_input, 0, sizeof(*start_vp_input));
469+
start_vp_input->partition_id = -1;
470+
start_vp_input->vp_index = cpu;
471+
start_vp_input->target_vtl.target_vtl = ms_hyperv.vtl;
472+
*(u64 *)&start_vp_input->vp_context = __pa(vmsa) | 1;
473+
474+
do {
475+
ret = hv_do_hypercall(HVCALL_START_VP,
476+
start_vp_input, NULL);
477+
} while (hv_result(ret) == HV_STATUS_TIME_OUT && retry--);
478+
479+
local_irq_restore(flags);
480+
481+
if (!hv_result_success(ret)) {
482+
pr_err("HvCallStartVirtualProcessor failed: %llx\n", ret);
483+
snp_cleanup_vmsa(vmsa);
484+
vmsa = NULL;
485+
}
486+
487+
cur_vmsa = per_cpu(hv_sev_vmsa, cpu);
488+
/* Free up any previous VMSA page */
489+
if (cur_vmsa)
490+
snp_cleanup_vmsa(cur_vmsa);
491+
492+
/* Record the current VMSA page */
493+
per_cpu(hv_sev_vmsa, cpu) = vmsa;
494+
495+
return ret;
496+
}
497+
360498
void __init hv_vtom_init(void)
361499
{
362500
/*

arch/x86/include/asm/mshyperv.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ extern u64 hv_current_partition_id;
4949
extern union hv_ghcb * __percpu *hv_ghcb_pg;
5050

5151
extern bool hv_isolation_type_en_snp(void);
52+
/*
53+
* DEFAULT INIT GPAT and SEGMENT LIMIT value in struct VMSA
54+
* to start AP in enlightened SEV guest.
55+
*/
56+
#define HV_AP_INIT_GPAT_DEFAULT 0x0007040600070406ULL
57+
#define HV_AP_SEGMENT_LIMIT 0xffffffff
5258

5359
int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
5460
int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
@@ -256,12 +262,14 @@ void hv_ghcb_msr_read(u64 msr, u64 *value);
256262
bool hv_ghcb_negotiate_protocol(void);
257263
void __noreturn hv_ghcb_terminate(unsigned int set, unsigned int reason);
258264
void hv_vtom_init(void);
265+
int hv_snp_boot_ap(int cpu, unsigned long start_ip);
259266
#else
260267
static inline void hv_ghcb_msr_write(u64 msr, u64 value) {}
261268
static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {}
262269
static inline bool hv_ghcb_negotiate_protocol(void) { return false; }
263270
static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {}
264271
static inline void hv_vtom_init(void) {}
272+
static int hv_snp_boot_ap(int cpu, unsigned long start_ip) { return 0; }
265273
#endif
266274

267275
extern bool hv_isolation_type_snp(void);

arch/x86/kernel/cpu/mshyperv.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,15 @@ static void __init hv_smp_prepare_cpus(unsigned int max_cpus)
295295

296296
native_smp_prepare_cpus(max_cpus);
297297

298+
/*
299+
* Override wakeup_secondary_cpu_64 callback for SEV-SNP
300+
* enlightened guest.
301+
*/
302+
if (hv_isolation_type_en_snp()) {
303+
apic->wakeup_secondary_cpu_64 = hv_snp_boot_ap;
304+
return;
305+
}
306+
298307
#ifdef CONFIG_X86_64
299308
for_each_present_cpu(i) {
300309
if (i == 0)
@@ -502,7 +511,7 @@ static void __init ms_hyperv_init_platform(void)
502511

503512
# ifdef CONFIG_SMP
504513
smp_ops.smp_prepare_boot_cpu = hv_smp_prepare_boot_cpu;
505-
if (hv_root_partition)
514+
if (hv_root_partition || hv_isolation_type_en_snp())
506515
smp_ops.smp_prepare_cpus = hv_smp_prepare_cpus;
507516
# endif
508517

include/asm-generic/hyperv-tlfs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ enum HV_GENERIC_SET_FORMAT {
223223
#define HV_STATUS_INVALID_PORT_ID 17
224224
#define HV_STATUS_INVALID_CONNECTION_ID 18
225225
#define HV_STATUS_INSUFFICIENT_BUFFERS 19
226+
#define HV_STATUS_TIME_OUT 120
226227
#define HV_STATUS_VTL_ALREADY_ENABLED 134
227228

228229
/*

0 commit comments

Comments
 (0)