mirror_ubuntu-kernels/arch/x86/coco/tdx/tdx-shared.c
Kai Huang 90f5ecd37f x86/tdx: Reimplement __tdx_hypercall() using TDX_MODULE_CALL asm
Now the TDX_HYPERCALL asm is basically identical to the TDX_MODULE_CALL
with both '\saved' and '\ret' enabled, with two minor things though:

1) The way to restore the structure pointer is different

The TDX_HYPERCALL uses RCX as spare to restore the structure pointer,
but the TDX_MODULE_CALL assumes no spare register can be used.  In other
words, TDX_MODULE_CALL already covers what TDX_HYPERCALL does.

2) TDX_MODULE_CALL only clears shared registers for TDH.VP.ENTER

For this just need to make that code available for the non-host case.

Thus, remove the TDX_HYPERCALL and reimplement the __tdx_hypercall()
using the TDX_MODULE_CALL.

Extend the TDX_MODULE_CALL to cover "clear shared registers" for
TDG.VP.VMCALL.  Introduce a new __tdcall_saved_ret() to replace the
temporary __tdcall_hypercall().

The __tdcall_saved_ret() can also be used for those new TDCALLs which
require more input/output registers than the basic TDCALLs do.

Suggested-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Kai Huang <kai.huang@intel.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Reviewed-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/all/e68a2473fb6f5bcd78b078cae7510e9d0753b3df.1692096753.git.kai.huang%40intel.com
2023-09-12 16:30:14 -07:00

115 lines
2.5 KiB
C

#include <asm/tdx.h>
#include <asm/pgtable.h>
static unsigned long try_accept_one(phys_addr_t start, unsigned long len,
enum pg_level pg_level)
{
unsigned long accept_size = page_level_size(pg_level);
struct tdx_module_args args = {};
u8 page_size;
if (!IS_ALIGNED(start, accept_size))
return 0;
if (len < accept_size)
return 0;
/*
* Pass the page physical address to the TDX module to accept the
* pending, private page.
*
* Bits 2:0 of RCX encode page size: 0 - 4K, 1 - 2M, 2 - 1G.
*/
switch (pg_level) {
case PG_LEVEL_4K:
page_size = 0;
break;
case PG_LEVEL_2M:
page_size = 1;
break;
case PG_LEVEL_1G:
page_size = 2;
break;
default:
return 0;
}
args.rcx = start | page_size;
if (__tdcall(TDG_MEM_PAGE_ACCEPT, &args))
return 0;
return accept_size;
}
bool tdx_accept_memory(phys_addr_t start, phys_addr_t end)
{
/*
* For shared->private conversion, accept the page using
* TDG_MEM_PAGE_ACCEPT TDX module call.
*/
while (start < end) {
unsigned long len = end - start;
unsigned long accept_size;
/*
* Try larger accepts first. It gives chance to VMM to keep
* 1G/2M Secure EPT entries where possible and speeds up
* process by cutting number of hypercalls (if successful).
*/
accept_size = try_accept_one(start, len, PG_LEVEL_1G);
if (!accept_size)
accept_size = try_accept_one(start, len, PG_LEVEL_2M);
if (!accept_size)
accept_size = try_accept_one(start, len, PG_LEVEL_4K);
if (!accept_size)
return false;
start += accept_size;
}
return true;
}
noinstr u64 __tdx_hypercall(struct tdx_hypercall_args *args)
{
struct tdx_module_args margs = {
.rcx = TDVMCALL_EXPOSE_REGS_MASK,
.rdx = args->rdx,
.r8 = args->r8,
.r9 = args->r9,
.r10 = args->r10,
.r11 = args->r11,
.r12 = args->r12,
.r13 = args->r13,
.r14 = args->r14,
.r15 = args->r15,
.rbx = args->rbx,
.rdi = args->rdi,
.rsi = args->rsi,
};
/*
* Failure of __tdcall_saved_ret() indicates a failure of the TDVMCALL
* mechanism itself and that something has gone horribly wrong with
* the TDX module. __tdx_hypercall_failed() never returns.
*/
if (__tdcall_saved_ret(TDG_VP_VMCALL, &margs))
__tdx_hypercall_failed();
args->r8 = margs.r8;
args->r9 = margs.r9;
args->r10 = margs.r10;
args->r11 = margs.r11;
args->r12 = margs.r12;
args->r13 = margs.r13;
args->r14 = margs.r14;
args->r15 = margs.r15;
args->rdi = margs.rdi;
args->rsi = margs.rsi;
args->rbx = margs.rbx;
args->rdx = margs.rdx;
/* TDVMCALL leaf return code is in R10 */
return args->r10;
}