Skip to contentSkip to conversation

x86/tdx: Generate SIGBUS on userspace MMIO

PATCH In review Kirill A. Shutemov 3 participants 4 messages May 21, 2024 – May 23, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/tdx: Generate SIGBUS on userspace MMIO

Currently attempt to do MMIO from userspace in TDX guest leads to warning about unexpect #VE and SIGSEGV being delivered to the process.

Enlightened userspace might choose to deal with MMIO on their own if kernel doesn't emulate it.

Handle EPT_VIOLATION exit reason for userspace and deliver SIGBUS instead of SIGSEV. SIGBUS is more appropriate for MMIO situation.

---
 arch/x86/coco/tdx/tdx.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
arch/x86/coco/tdx/tdx.c +14 -5
index c1cb90369915..d2aa93cebf5a 100644
@@ -7,6 +7,7 @@
 #include <linux/cpufeature.h>
 #include <linux/export.h>
 #include <linux/io.h>
+#include <linux/sched/signal.h>
 #include <asm/coco.h>
 #include <asm/tdx.h>
 #include <asm/vmx.h>
@@ -630,6 +631,11 @@ void tdx_get_ve_info(struct ve_info *ve)
 	ve->instr_info  = upper_32_bits(args.r10);
 }
 
+static inline bool is_private_gpa(u64 gpa)
+{
+	return gpa == cc_mkenc(gpa);
+}
+
 /*
  * Handle the user initiated #VE.
  *
@@ -641,17 +647,20 @@ static int virt_exception_user(struct pt_regs *regs, struct ve_info *ve)
 	switch (ve->exit_reason) {
 	case EXIT_REASON_CPUID:
 		return handle_cpuid(regs, ve);
+	case EXIT_REASON_EPT_VIOLATION:
+		if (is_private_gpa(ve->gpa))
+			panic("Unexpected EPT-violation on private memory.");
+
+		force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)ve->gla);
+
+		/* Return 0 to avoid incrementing RIP */
+		return 0;
 	default:
 		pr_warn("Unexpected #VE: %lld\n", ve->exit_reason);
 		return -EIO;
 	}
 }
 
-static inline bool is_private_gpa(u64 gpa)
-{
-	return gpa == cc_mkenc(gpa);
-}
-
 /*
  * Handle the kernel #VE.
  *
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Kirill A. Shutemov

Currently attempt to do MMIO from userspace in TDX guest leads to warning about unexpect #VE and SIGSEGV being delivered to the process.

Enlightened userspace might choose to deal with MMIO on their own if kernel doesn't emulate it.

Any specific use cases ? Like who is using it?

Quoted 5 lines

Handle EPT_VIOLATION exit reason for userspace and deliver SIGBUS instead of SIGSEV. SIGBUS is more appropriate for MMIO situation.

---

Code looks good to me.

Quoted 53 lines
 arch/x86/coco/tdx/tdx.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
arch/x86/coco/tdx/tdx.c +14 -5
index c1cb90369915..d2aa93cebf5a 100644
@@ -7,6 +7,7 @@
 #include <linux/cpufeature.h>
 #include <linux/export.h>
 #include <linux/io.h>
+#include <linux/sched/signal.h>
 #include <asm/coco.h>
 #include <asm/tdx.h>
 #include <asm/vmx.h>
@@ -630,6 +631,11 @@ void tdx_get_ve_info(struct ve_info *ve)
 	ve->instr_info  = upper_32_bits(args.r10);
 }
 
+static inline bool is_private_gpa(u64 gpa)
+{
+	return gpa == cc_mkenc(gpa);
+}
+
 /*
  * Handle the user initiated #VE.
  *
@@ -641,17 +647,20 @@ static int virt_exception_user(struct pt_regs *regs, struct ve_info *ve)
 	switch (ve->exit_reason) {
 	case EXIT_REASON_CPUID:
 		return handle_cpuid(regs, ve);
+	case EXIT_REASON_EPT_VIOLATION:
+		if (is_private_gpa(ve->gpa))
+			panic("Unexpected EPT-violation on private memory.");
+
+		force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)ve->gla);
+
+		/* Return 0 to avoid incrementing RIP */
+		return 0;
 	default:
 		pr_warn("Unexpected #VE: %lld\n", ve->exit_reason);
 		return -EIO;
 	}
 }
 
-static inline bool is_private_gpa(u64 gpa)
-{
-	return gpa == cc_mkenc(gpa);
-}
-
 /*
  * Handle the kernel #VE.
  *
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
#2 lore raw
Quoted 9 lines · Kuppuswamy Sathyanarayanan
Quoted 5 lines · Kirill A. Shutemov

Currently attempt to do MMIO from userspace in TDX guest leads to warning about unexpect #VE and SIGSEGV being delivered to the process.

Enlightened userspace might choose to deal with MMIO on their own if kernel doesn't emulate it.

Any specific use cases ? Like who is using it?

Microsoft folks wanted it. Chris, Dexuan, John, any comments?

But it is generally right thing to do. SIGBUS is right signal to deliver.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#3 lore raw

We use this to handle MMIO issued by userspace that the kernel does not handle in a #VE, for devices assigned to a TDX VM.

Chris

-----Original Message-----
From: Kirill A. Shutemov <[email protected]>
Sent: Thursday, May 23, 2024 3:15 AM
To: Kuppuswamy Sathyanarayanan <[email protected]>
Cc: Dave Hansen <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Borislav Petkov <[email protected]>; [email protected]; H. Peter Anvin <[email protected]>; [email protected]; [email protected]; Chris Oo <[email protected]>; Dexuan Cui <[email protected]>; John Starks <[email protected]>
Subject: [EXTERNAL] Re: [PATCH] x86/tdx: Generate SIGBUS on userspace MMIO

Quoted 9 lines · Kuppuswamy Sathyanarayanan
Quoted 5 lines · Kirill A. Shutemov

Currently attempt to do MMIO from userspace in TDX guest leads to warning about unexpect #VE and SIGSEGV being delivered to the process.

Enlightened userspace might choose to deal with MMIO on their own if kernel doesn't emulate it.

Any specific use cases ? Like who is using it?

Microsoft folks wanted it. Chris, Dexuan, John, any comments?

But it is generally right thing to do. SIGBUS is right signal to deliver.

--
Kiryl Shutsemau / Kirill A. Shutemov