2024-03-09 21:02:56

by Kirill A. Shutemov

[permalink] [raw]
Subject: [PATCH 1/4] x86/tdx: Introduce tdg_vm_wr()

Add a helper to write to a TD-scope metadata field and use it to set
NOTIFY_ENABLES.

Signed-off-by: Kirill A. Shutemov <[email protected]>
---
arch/x86/coco/tdx/tdx.c | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c
index cc67f7380055..5ffe5ef99536 100644
--- a/arch/x86/coco/tdx/tdx.c
+++ b/arch/x86/coco/tdx/tdx.c
@@ -77,6 +77,20 @@ static inline void tdcall(u64 fn, struct tdx_module_args *args)
panic("TDCALL %lld failed (Buggy TDX module!)\n", fn);
}

+static inline u64 tdg_vm_wr(u64 field, u64 value, u64 mask)
+{
+ struct tdx_module_args args = {
+ .rdx = field,
+ .r8 = value,
+ .r9 = mask,
+ };
+
+ tdcall(TDG_VM_WR, &args);
+
+ /* Old value */
+ return args.r8;
+}
+
/**
* tdx_mcall_get_report0() - Wrapper to get TDREPORT0 (a.k.a. TDREPORT
* subtype 0) using TDG.MR.REPORT TDCALL.
@@ -902,10 +916,6 @@ static void tdx_kexec_unshare_mem(void)

void __init tdx_early_init(void)
{
- struct tdx_module_args args = {
- .rdx = TDCS_NOTIFY_ENABLES,
- .r9 = -1ULL,
- };
u64 cc_mask;
u32 eax, sig[3];

@@ -924,7 +934,7 @@ void __init tdx_early_init(void)
cc_set_mask(cc_mask);

/* Kernel does not use NOTIFY_ENABLES and does not need random #VEs */
- tdcall(TDG_VM_WR, &args);
+ tdg_vm_wr(TDCS_NOTIFY_ENABLES, 0, -1ULL);

/*
* All bits above GPA width are reserved and kernel treats shared bit
--
2.43.0



Subject: Re: [PATCH 1/4] x86/tdx: Introduce tdg_vm_wr()

On Sat, Mar 9, 2024 at 1:02 PM Kirill A. Shutemov
<[email protected]> wrote:
>
> Add a helper to write to a TD-scope metadata field and use it to set
> NOTIFY_ENABLES.
>

Add a note about why you create this helper function. I think the
intention is to reuse it
to update other TD-scope fields.

> Signed-off-by: Kirill A. Shutemov <[email protected]>
> ---
> arch/x86/coco/tdx/tdx.c | 20 +++++++++++++++-----
> 1 file changed, 15 insertions(+), 5 deletions(-)
>
> diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c
> index cc67f7380055..5ffe5ef99536 100644
> --- a/arch/x86/coco/tdx/tdx.c
> +++ b/arch/x86/coco/tdx/tdx.c
> @@ -77,6 +77,20 @@ static inline void tdcall(u64 fn, struct tdx_module_args *args)
> panic("TDCALL %lld failed (Buggy TDX module!)\n", fn);
> }
>
> +static inline u64 tdg_vm_wr(u64 field, u64 value, u64 mask)
> +{
> + struct tdx_module_args args = {
> + .rdx = field,
> + .r8 = value,
> + .r9 = mask,
> + };
> +
> + tdcall(TDG_VM_WR, &args);
> +
> + /* Old value */
> + return args.r8;

Since the update failure will panic, any use for returning the old value?

> +}
> +
> /**
> * tdx_mcall_get_report0() - Wrapper to get TDREPORT0 (a.k.a. TDREPORT
> * subtype 0) using TDG.MR.REPORT TDCALL.
> @@ -902,10 +916,6 @@ static void tdx_kexec_unshare_mem(void)
>
> void __init tdx_early_init(void)
> {
> - struct tdx_module_args args = {
> - .rdx = TDCS_NOTIFY_ENABLES,
> - .r9 = -1ULL,
> - };
> u64 cc_mask;
> u32 eax, sig[3];
>
> @@ -924,7 +934,7 @@ void __init tdx_early_init(void)
> cc_set_mask(cc_mask);
>
> /* Kernel does not use NOTIFY_ENABLES and does not need random #VEs */
> - tdcall(TDG_VM_WR, &args);
> + tdg_vm_wr(TDCS_NOTIFY_ENABLES, 0, -1ULL);
>
> /*
> * All bits above GPA width are reserved and kernel treats shared bit
> --
> 2.43.0
>

2024-03-11 12:00:31

by Kirill A. Shutemov

[permalink] [raw]
Subject: Re: [PATCH 1/4] x86/tdx: Introduce tdg_vm_wr()

On Sat, Mar 09, 2024 at 04:02:58PM -0800, Kuppuswamy Sathyanarayanan wrote:
> > + /* Old value */
> > + return args.r8;
>
> Since the update failure will panic, any use for returning the old value?

I don't have any immediate users for the old value, but I can imagine
situation when you need to do something else if your write result in
actual change of setting.

--
Kiryl Shutsemau / Kirill A. Shutemov