[PATCH 42/62] x86/sev-es: Handle DR7 read/write events

From: Joerg Roedel
Date: Tue Feb 11 2020 - 08:55:29 EST


From: Tom Lendacky <thomas.lendacky@xxxxxxx>

Add code to handle #VC exceptions on DR7 register reads and writes.
This is needed early because show_regs() reads DR7 to print it out.

Signed-off-by: Tom Lendacky <thomas.lendacky@xxxxxxx>
[ jroedel@xxxxxxx: - Adapt to #VC handling framework
- Support early usage ]
Co-developed-by: Joerg Roedel <jroedel@xxxxxxx>
Signed-off-by: Joerg Roedel <jroedel@xxxxxxx>
---
arch/x86/kernel/sev-es.c | 69 +++++++++++++++++++++++++++++++++++++---
1 file changed, 65 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kernel/sev-es.c b/arch/x86/kernel/sev-es.c
index b27d5b0a8ae1..fcd67ab04d2d 100644
--- a/arch/x86/kernel/sev-es.c
+++ b/arch/x86/kernel/sev-es.c
@@ -22,6 +22,8 @@
#include <asm/traps.h>
#include <asm/svm.h>

+#define DR7_RESET_VALUE 0x400
+
/* For early boot hypervisor communication in SEV-ES enabled guests */
struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);

@@ -30,6 +32,9 @@ struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);
* cleared
*/
struct ghcb __initdata *boot_ghcb;
+static DEFINE_PER_CPU(unsigned long, cached_dr7) = DR7_RESET_VALUE;
+/* Needed before per-cpu access is set up */
+static unsigned long early_dr7 = DR7_RESET_VALUE;

/* Runtime GHCBs */
static DEFINE_PER_CPU_DECRYPTED(struct ghcb, ghcb_page) __aligned(PAGE_SIZE);
@@ -212,13 +217,69 @@ static void __init early_forward_exception(struct es_em_ctxt *ctxt)
early_exception(ctxt->regs, trapnr);
}

+static enum es_result handle_dr7_write(struct ghcb *ghcb,
+ struct es_em_ctxt *ctxt,
+ bool early)
+{
+ u8 rm = X86_MODRM_RM(ctxt->insn.modrm.value);
+ unsigned long *reg;
+ enum es_result ret;
+
+ if (ctxt->insn.rex_prefix.nbytes &&
+ X86_REX_B(ctxt->insn.rex_prefix.value))
+ rm |= 0x8;
+
+ reg = register_from_idx(ctxt->regs, rm);
+
+ /* Using a value of 0 for ExitInfo1 means RAX holds the value */
+ ghcb_set_rax(ghcb, *reg);
+ ret = ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WRITE_DR7, 0, 0);
+ if (ret != ES_OK)
+ return ret;
+
+ if (early)
+ early_dr7 = *reg;
+ else
+ this_cpu_write(cached_dr7, *reg);
+
+ return ES_OK;
+}
+
+static enum es_result handle_dr7_read(struct ghcb *ghcb,
+ struct es_em_ctxt *ctxt,
+ bool early)
+{
+ u8 rm = X86_MODRM_RM(ctxt->insn.modrm.value);
+ unsigned long *reg;
+
+ if (ctxt->insn.rex_prefix.nbytes &&
+ X86_REX_B(ctxt->insn.rex_prefix.value))
+ rm |= 0x8;
+
+ reg = register_from_idx(ctxt->regs, rm);
+
+ if (early)
+ *reg = early_dr7;
+ else
+ *reg = this_cpu_read(cached_dr7);
+
+ return ES_OK;
+}
+
static enum es_result handle_vc_exception(struct es_em_ctxt *ctxt,
- struct ghcb *ghcb,
- unsigned long exit_code)
+ struct ghcb *ghcb,
+ unsigned long exit_code,
+ bool early)
{
enum es_result result;

switch (exit_code) {
+ case SVM_EXIT_READ_DR7:
+ result = handle_dr7_read(ghcb, ctxt, early);
+ break;
+ case SVM_EXIT_WRITE_DR7:
+ result = handle_dr7_write(ghcb, ctxt, early);
+ break;
case SVM_EXIT_CPUID:
result = handle_cpuid(ghcb, ctxt);
break;
@@ -302,7 +363,7 @@ dotraplinkage void do_vmm_communication(struct pt_regs *regs, unsigned long exit
result = context_filter(regs, exit_code);

if (result == ES_OK)
- result = handle_vc_exception(&ctxt, ghcb, exit_code);
+ result = handle_vc_exception(&ctxt, ghcb, exit_code, false);

/* Done - now check the result */
switch (result) {
@@ -368,7 +429,7 @@ int __init boot_vc_exception(struct pt_regs *regs)
result = init_em_ctxt(&ctxt, regs, exit_code);

if (result == ES_OK)
- result = handle_vc_exception(&ctxt, boot_ghcb, exit_code);
+ result = handle_vc_exception(&ctxt, boot_ghcb, exit_code, true);

/* Done - now check the result */
switch (result) {
--
2.17.1