mirror of
https://github.com/AuxXxilium/linux_dsm_epyc7002.git
synced 2025-01-25 22:29:48 +07:00
885689e47d
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
334 lines
6.9 KiB
C
334 lines
6.9 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* AMD Memory Encryption Support
|
|
*
|
|
* Copyright (C) 2019 SUSE
|
|
*
|
|
* Author: Joerg Roedel <jroedel@suse.de>
|
|
*/
|
|
|
|
#include <linux/sched/debug.h> /* For show_regs() */
|
|
#include <linux/percpu-defs.h>
|
|
#include <linux/mem_encrypt.h>
|
|
#include <linux/printk.h>
|
|
#include <linux/mm_types.h>
|
|
#include <linux/set_memory.h>
|
|
#include <linux/memblock.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/mm.h>
|
|
|
|
#include <asm/sev-es.h>
|
|
#include <asm/insn-eval.h>
|
|
#include <asm/fpu/internal.h>
|
|
#include <asm/processor.h>
|
|
#include <asm/trap_pf.h>
|
|
#include <asm/trapnr.h>
|
|
#include <asm/svm.h>
|
|
|
|
/* For early boot hypervisor communication in SEV-ES enabled guests */
|
|
static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);
|
|
|
|
/*
|
|
* Needs to be in the .data section because we need it NULL before bss is
|
|
* cleared
|
|
*/
|
|
static struct ghcb __initdata *boot_ghcb;
|
|
|
|
/* #VC handler runtime per-CPU data */
|
|
struct sev_es_runtime_data {
|
|
struct ghcb ghcb_page;
|
|
};
|
|
|
|
static DEFINE_PER_CPU(struct sev_es_runtime_data*, runtime_data);
|
|
|
|
/* Needed in vc_early_forward_exception */
|
|
void do_early_exception(struct pt_regs *regs, int trapnr);
|
|
|
|
static inline u64 sev_es_rd_ghcb_msr(void)
|
|
{
|
|
return __rdmsr(MSR_AMD64_SEV_ES_GHCB);
|
|
}
|
|
|
|
static inline void sev_es_wr_ghcb_msr(u64 val)
|
|
{
|
|
u32 low, high;
|
|
|
|
low = (u32)(val);
|
|
high = (u32)(val >> 32);
|
|
|
|
native_wrmsr(MSR_AMD64_SEV_ES_GHCB, low, high);
|
|
}
|
|
|
|
static int vc_fetch_insn_kernel(struct es_em_ctxt *ctxt,
|
|
unsigned char *buffer)
|
|
{
|
|
return copy_from_kernel_nofault(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
|
|
}
|
|
|
|
static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
|
|
{
|
|
char buffer[MAX_INSN_SIZE];
|
|
enum es_result ret;
|
|
int res;
|
|
|
|
res = vc_fetch_insn_kernel(ctxt, buffer);
|
|
if (unlikely(res == -EFAULT)) {
|
|
ctxt->fi.vector = X86_TRAP_PF;
|
|
ctxt->fi.error_code = 0;
|
|
ctxt->fi.cr2 = ctxt->regs->ip;
|
|
return ES_EXCEPTION;
|
|
}
|
|
|
|
insn_init(&ctxt->insn, buffer, MAX_INSN_SIZE - res, 1);
|
|
insn_get_length(&ctxt->insn);
|
|
|
|
ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED;
|
|
|
|
return ret;
|
|
}
|
|
|
|
static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
|
|
char *dst, char *buf, size_t size)
|
|
{
|
|
unsigned long error_code = X86_PF_PROT | X86_PF_WRITE;
|
|
char __user *target = (char __user *)dst;
|
|
u64 d8;
|
|
u32 d4;
|
|
u16 d2;
|
|
u8 d1;
|
|
|
|
switch (size) {
|
|
case 1:
|
|
memcpy(&d1, buf, 1);
|
|
if (put_user(d1, target))
|
|
goto fault;
|
|
break;
|
|
case 2:
|
|
memcpy(&d2, buf, 2);
|
|
if (put_user(d2, target))
|
|
goto fault;
|
|
break;
|
|
case 4:
|
|
memcpy(&d4, buf, 4);
|
|
if (put_user(d4, target))
|
|
goto fault;
|
|
break;
|
|
case 8:
|
|
memcpy(&d8, buf, 8);
|
|
if (put_user(d8, target))
|
|
goto fault;
|
|
break;
|
|
default:
|
|
WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
|
|
return ES_UNSUPPORTED;
|
|
}
|
|
|
|
return ES_OK;
|
|
|
|
fault:
|
|
if (user_mode(ctxt->regs))
|
|
error_code |= X86_PF_USER;
|
|
|
|
ctxt->fi.vector = X86_TRAP_PF;
|
|
ctxt->fi.error_code = error_code;
|
|
ctxt->fi.cr2 = (unsigned long)dst;
|
|
|
|
return ES_EXCEPTION;
|
|
}
|
|
|
|
static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
|
|
char *src, char *buf, size_t size)
|
|
{
|
|
unsigned long error_code = X86_PF_PROT;
|
|
char __user *s = (char __user *)src;
|
|
u64 d8;
|
|
u32 d4;
|
|
u16 d2;
|
|
u8 d1;
|
|
|
|
switch (size) {
|
|
case 1:
|
|
if (get_user(d1, s))
|
|
goto fault;
|
|
memcpy(buf, &d1, 1);
|
|
break;
|
|
case 2:
|
|
if (get_user(d2, s))
|
|
goto fault;
|
|
memcpy(buf, &d2, 2);
|
|
break;
|
|
case 4:
|
|
if (get_user(d4, s))
|
|
goto fault;
|
|
memcpy(buf, &d4, 4);
|
|
break;
|
|
case 8:
|
|
if (get_user(d8, s))
|
|
goto fault;
|
|
memcpy(buf, &d8, 8);
|
|
break;
|
|
default:
|
|
WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
|
|
return ES_UNSUPPORTED;
|
|
}
|
|
|
|
return ES_OK;
|
|
|
|
fault:
|
|
if (user_mode(ctxt->regs))
|
|
error_code |= X86_PF_USER;
|
|
|
|
ctxt->fi.vector = X86_TRAP_PF;
|
|
ctxt->fi.error_code = error_code;
|
|
ctxt->fi.cr2 = (unsigned long)src;
|
|
|
|
return ES_EXCEPTION;
|
|
}
|
|
|
|
/* Include code shared with pre-decompression boot stage */
|
|
#include "sev-es-shared.c"
|
|
|
|
/*
|
|
* This function runs on the first #VC exception after the kernel
|
|
* switched to virtual addresses.
|
|
*/
|
|
static bool __init sev_es_setup_ghcb(void)
|
|
{
|
|
/* First make sure the hypervisor talks a supported protocol. */
|
|
if (!sev_es_negotiate_protocol())
|
|
return false;
|
|
|
|
/*
|
|
* Clear the boot_ghcb. The first exception comes in before the bss
|
|
* section is cleared.
|
|
*/
|
|
memset(&boot_ghcb_page, 0, PAGE_SIZE);
|
|
|
|
/* Alright - Make the boot-ghcb public */
|
|
boot_ghcb = &boot_ghcb_page;
|
|
|
|
return true;
|
|
}
|
|
|
|
static void __init alloc_runtime_data(int cpu)
|
|
{
|
|
struct sev_es_runtime_data *data;
|
|
|
|
data = memblock_alloc(sizeof(*data), PAGE_SIZE);
|
|
if (!data)
|
|
panic("Can't allocate SEV-ES runtime data");
|
|
|
|
per_cpu(runtime_data, cpu) = data;
|
|
}
|
|
|
|
static void __init init_ghcb(int cpu)
|
|
{
|
|
struct sev_es_runtime_data *data;
|
|
int err;
|
|
|
|
data = per_cpu(runtime_data, cpu);
|
|
|
|
err = early_set_memory_decrypted((unsigned long)&data->ghcb_page,
|
|
sizeof(data->ghcb_page));
|
|
if (err)
|
|
panic("Can't map GHCBs unencrypted");
|
|
|
|
memset(&data->ghcb_page, 0, sizeof(data->ghcb_page));
|
|
}
|
|
|
|
void __init sev_es_init_vc_handling(void)
|
|
{
|
|
int cpu;
|
|
|
|
BUILD_BUG_ON(offsetof(struct sev_es_runtime_data, ghcb_page) % PAGE_SIZE);
|
|
|
|
if (!sev_es_active())
|
|
return;
|
|
|
|
/* Initialize per-cpu GHCB pages */
|
|
for_each_possible_cpu(cpu) {
|
|
alloc_runtime_data(cpu);
|
|
init_ghcb(cpu);
|
|
}
|
|
}
|
|
|
|
static void __init vc_early_forward_exception(struct es_em_ctxt *ctxt)
|
|
{
|
|
int trapnr = ctxt->fi.vector;
|
|
|
|
if (trapnr == X86_TRAP_PF)
|
|
native_write_cr2(ctxt->fi.cr2);
|
|
|
|
ctxt->regs->orig_ax = ctxt->fi.error_code;
|
|
do_early_exception(ctxt->regs, trapnr);
|
|
}
|
|
|
|
static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
|
|
struct ghcb *ghcb,
|
|
unsigned long exit_code)
|
|
{
|
|
enum es_result result;
|
|
|
|
switch (exit_code) {
|
|
default:
|
|
/*
|
|
* Unexpected #VC exception
|
|
*/
|
|
result = ES_UNSUPPORTED;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
bool __init handle_vc_boot_ghcb(struct pt_regs *regs)
|
|
{
|
|
unsigned long exit_code = regs->orig_ax;
|
|
struct es_em_ctxt ctxt;
|
|
enum es_result result;
|
|
|
|
/* Do initial setup or terminate the guest */
|
|
if (unlikely(boot_ghcb == NULL && !sev_es_setup_ghcb()))
|
|
sev_es_terminate(GHCB_SEV_ES_REASON_GENERAL_REQUEST);
|
|
|
|
vc_ghcb_invalidate(boot_ghcb);
|
|
|
|
result = vc_init_em_ctxt(&ctxt, regs, exit_code);
|
|
if (result == ES_OK)
|
|
result = vc_handle_exitcode(&ctxt, boot_ghcb, exit_code);
|
|
|
|
/* Done - now check the result */
|
|
switch (result) {
|
|
case ES_OK:
|
|
vc_finish_insn(&ctxt);
|
|
break;
|
|
case ES_UNSUPPORTED:
|
|
early_printk("PANIC: Unsupported exit-code 0x%02lx in early #VC exception (IP: 0x%lx)\n",
|
|
exit_code, regs->ip);
|
|
goto fail;
|
|
case ES_VMM_ERROR:
|
|
early_printk("PANIC: Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n",
|
|
exit_code, regs->ip);
|
|
goto fail;
|
|
case ES_DECODE_FAILED:
|
|
early_printk("PANIC: Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n",
|
|
exit_code, regs->ip);
|
|
goto fail;
|
|
case ES_EXCEPTION:
|
|
vc_early_forward_exception(&ctxt);
|
|
break;
|
|
case ES_RETRY:
|
|
/* Nothing to do */
|
|
break;
|
|
default:
|
|
BUG();
|
|
}
|
|
|
|
return true;
|
|
|
|
fail:
|
|
show_regs(regs);
|
|
|
|
while (true)
|
|
halt();
|
|
}
|