Add a new process-level prctl option to enable/disable user-level
hypercalls when running in a confidential VM. Add support for
checking this flag on VMCALL #VE for TDX and transfer control to
a hypervisor vendor-specific handler.

Signed-off-by: Tim Merrifield <tim.merrifi...@broadcom.com>
---
 arch/x86/coco/tdx/tdx.c           | 23 +++++++++++++++++++++++
 arch/x86/include/asm/mmu.h        |  2 ++
 arch/x86/include/asm/x86_init.h   |  1 +
 arch/x86/include/uapi/asm/prctl.h |  3 +++
 arch/x86/kernel/process.c         | 22 ++++++++++++++++++++++
 5 files changed, 51 insertions(+)

diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c
index 078e2bac2553..02580fcf6157 100644
--- a/arch/x86/coco/tdx/tdx.c
+++ b/arch/x86/coco/tdx/tdx.c
@@ -238,6 +238,7 @@ static int ve_instr_len(struct ve_info *ve)
        case EXIT_REASON_MSR_WRITE:
        case EXIT_REASON_CPUID:
        case EXIT_REASON_IO_INSTRUCTION:
+       case EXIT_REASON_VMCALL:
                /* It is safe to use ve->instr_len for #VE due instructions */
                return ve->instr_len;
        case EXIT_REASON_EPT_VIOLATION:
@@ -634,6 +635,26 @@ void tdx_get_ve_info(struct ve_info *ve)
        ve->instr_info  = upper_32_bits(args.r10);
 }
 
+/*
+ * Handle user-initiated, hypervisor-specific VMCALLs.
+ */
+static int handle_user_vmcall(struct pt_regs *regs, struct ve_info *ve)
+{
+       int err;
+
+       if (!x86_platform.hyper.tdx_hcall)
+               return -EOPNOTSUPP;
+
+       if (!test_bit(MM_CONTEXT_COCO_USER_HCALL, &current->mm->context.flags))
+               return -EOPNOTSUPP;
+
+       err = x86_platform.hyper.tdx_hcall(regs);
+       if (err)
+               return err;
+
+       return ve_instr_len(ve);
+}
+
 /*
  * Handle the user initiated #VE.
  *
@@ -645,6 +666,8 @@ 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_VMCALL:
+               return handle_user_vmcall(regs, ve);
        default:
                pr_warn("Unexpected #VE: %lld\n", ve->exit_reason);
                return -EIO;
diff --git a/arch/x86/include/asm/mmu.h b/arch/x86/include/asm/mmu.h
index ce4677b8b735..626ab327e34c 100644
--- a/arch/x86/include/asm/mmu.h
+++ b/arch/x86/include/asm/mmu.h
@@ -16,6 +16,8 @@
 #define MM_CONTEXT_LOCK_LAM            2
 /* Allow LAM and SVA coexisting */
 #define MM_CONTEXT_FORCE_TAGGED_SVA    3
+/* Allow COCO user-level hypercalls. */
+#define MM_CONTEXT_COCO_USER_HCALL     4
 
 /*
  * x86 has arch-specific MMU state beyond what lives in mm_struct.
diff --git a/arch/x86/include/asm/x86_init.h b/arch/x86/include/asm/x86_init.h
index 213cf5379a5a..04d43b91d32a 100644
--- a/arch/x86/include/asm/x86_init.h
+++ b/arch/x86/include/asm/x86_init.h
@@ -282,6 +282,7 @@ struct x86_hyper_runtime {
        void (*sev_es_hcall_prepare)(struct ghcb *ghcb, struct pt_regs *regs);
        bool (*sev_es_hcall_finish)(struct ghcb *ghcb, struct pt_regs *regs);
        bool (*is_private_mmio)(u64 addr);
+       int  (*tdx_hcall)(struct pt_regs *regs);
 };
 
 /**
diff --git a/arch/x86/include/uapi/asm/prctl.h 
b/arch/x86/include/uapi/asm/prctl.h
index 384e2cc6ac19..37d154e503a3 100644
--- a/arch/x86/include/uapi/asm/prctl.h
+++ b/arch/x86/include/uapi/asm/prctl.h
@@ -16,6 +16,9 @@
 #define ARCH_GET_XCOMP_GUEST_PERM      0x1024
 #define ARCH_REQ_XCOMP_GUEST_PERM      0x1025
 
+#define ARCH_GET_COCO_USER_HCALL       0x1030
+#define ARCH_SET_COCO_USER_HCALL       0x1031
+
 #define ARCH_XCOMP_TILECFG             17
 #define ARCH_XCOMP_TILEDATA            18
 
diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index f63f8fd00a91..198431919fd2 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -1042,6 +1042,24 @@ unsigned long __get_wchan(struct task_struct *p)
        return addr;
 }
 
+static int get_coco_user_hcall_mode(void)
+{
+       return !test_bit(MM_CONTEXT_COCO_USER_HCALL,
+                       &current->mm->context.flags);
+}
+
+static int set_coco_user_hcall_mode(unsigned long enabled)
+{
+       if (enabled)
+               set_bit(MM_CONTEXT_COCO_USER_HCALL,
+                       &current->mm->context.flags);
+       else
+               clear_bit(MM_CONTEXT_COCO_USER_HCALL,
+                         &current->mm->context.flags);
+
+       return 0;
+}
+
 long do_arch_prctl_common(int option, unsigned long arg2)
 {
        switch (option) {
@@ -1055,6 +1073,10 @@ long do_arch_prctl_common(int option, unsigned long arg2)
        case ARCH_GET_XCOMP_GUEST_PERM:
        case ARCH_REQ_XCOMP_GUEST_PERM:
                return fpu_xstate_prctl(option, arg2);
+       case ARCH_GET_COCO_USER_HCALL:
+               return get_coco_user_hcall_mode();
+       case ARCH_SET_COCO_USER_HCALL:
+               return set_coco_user_hcall_mode(arg2);
        }
 
        return -EINVAL;
-- 
2.40.1


Reply via email to