Re: [PATCH v8 09/11] x86/tdx: Add MSR support for TDX guest
From: Kuppuswamy, Sathyanarayanan
Date:  Thu Oct 07 2021 - 22:16:16 EST
On 10/6/21 12:49 PM, Josh Poimboeuf wrote:
On Mon, Oct 04, 2021 at 07:52:03PM -0700, Kuppuswamy Sathyanarayanan wrote:
diff --git a/arch/x86/kernel/tdx.c b/arch/x86/kernel/tdx.c
index 3d0416515506..062ac4720434 100644
--- a/arch/x86/kernel/tdx.c
+++ b/arch/x86/kernel/tdx.c
@@ -110,6 +110,41 @@ static __cpuidle void tdx_safe_halt(void)
  	_tdx_halt(irq_disabled, do_sti);
  }
  
+static u64 tdx_read_msr_safe(unsigned int msr, int *err)
Here the kernel convention would probably be to return the error and
make 'val' an argument:
static int tdx_read_msr_safe(unsigned int msr, u64 *val)
Agree. I will fix this in next version.
+{
+	struct tdx_hypercall_output out = {0};
+	u64 ret;
+
+	/*
+	 * Emulate the MSR read via hypercall. More info about ABI
+	 * can be found in TDX Guest-Host-Communication Interface
+	 * (GHCI), sec titled "TDG.VP.VMCALL<Instruction.RDMSR>".
+	 */
+	ret = _tdx_hypercall(EXIT_REASON_MSR_READ, msr, 0, 0, 0, &out);
+
+	*err = ret ? -EIO : 0;
+
+	return out.r11;
+}
+
+static int tdx_write_msr_safe(unsigned int msr, unsigned int low,
+			      unsigned int high)
+{
+	u64 ret;
+
+	WARN_ON_ONCE(tdx_is_context_switched_msr(msr));
This fails to build, tdx_is_context_switched_msr() is missing.
@@ -136,19 +171,33 @@ unsigned long tdx_get_ve_info(struct ve_info *ve)
  int tdx_handle_virtualization_exception(struct pt_regs *regs,
  					struct ve_info *ve)
  {
+	unsigned long val;
+	int ret = 0;
+
  	switch (ve->exit_reason) {
  	case EXIT_REASON_HLT:
  		tdx_halt();
  		break;
+	case EXIT_REASON_MSR_READ:
+		val = tdx_read_msr_safe(regs->cx, (unsigned int *)&ret);
Why the 'unsigned int *' cast?
Also, 'val' should have the same type as the one returned by
tdx_read_msr_safe().  (Though it technically doesn't matter here.)
I will change val type to u64. It will not need any casting.
--
Sathyanarayanan Kuppuswamy
Linux Kernel Developer