Re: [RFC v2 05/32] x86/tdx: Add __tdcall() and __tdvmcall() helper functions
From: Dave Hansen
Date: Mon Apr 26 2021 - 19:17:39 EST
On 4/26/21 3:31 PM, Kuppuswamy, Sathyanarayanan wrote:
>>> +#define tdcall .byte 0x66,0x0f,0x01,0xcc
>>> +
>>> +/*
>>> + * __tdcall() - Used to communicate with the TDX module
>>
>> Why is this function here? What does it do? Why do we need it?
>
> __tdcall() function is used to request services from the TDX Module.
> Example use cases are, TDREPORT, VEINFO, TDINFO, etc.
I think there might be some misinterpretation of my question. What you
are describing is what *TDCALL* does. Why do we need a wrapper
function? What purpose does this wrapper function serve? Why do we
need this wrapper function?
>>> + * NOTE: This function should only used for non TDVMCALL
>>> + * use cases
>>> + */
>>> +SYM_FUNC_START(__tdcall)
>>> + FRAME_BEGIN
>>> +
>>> + /* Save non-volatile GPRs that are exposed to the VMM. */
>>> + push %r15
>>> + push %r14
>>> + push %r13
>>> + push %r12
>>
>> Why do we have to save these? Because they might be clobbered? If so,
>> let's say *THAT* instead of just "exposed". "Exposed" could mean "VMM
>> can read".
>>
>> Also, this just told me that this function can't be used to talk to the
>> VMM. Why is this talking about exposure to the VMM?
>
> Although __tdcall() is only used to communicate with the TDX module and the
> TDX module is not supposed to touch these registers, just to be on the safe
> side, I have tried to save the context of registers R12-R15. Anyway cycles
> used by instructions are less compared to tdcall.
Why are you talking about the VMM if this is a call to the SEAM module?
Let's say someone is reading the TDCALL architecture spec. It will say
something like, "blah blah, in this case TDCALL will not modify
%r12->%r15". Then someone goes and looks at this code that basically
says (or implies) "save these before the SEAM module modifies them".
What is a coder to do?
Please remove the ambiguity, either by removing this superfluous
(according to the spec) code, or documenting why it is not superfluous.
>>> + /* Move TDCALL Leaf ID to RAX */
>>> + mov %rdi, %rax
>>> + /* Move output pointer to R12 */
>>> + mov %r9, %r12
>>
>> I thought 'struct tdcall_output' was a purely software construct. Why
>> are we passing a pointer to it into TDCALL?
>
> Its used to store the TDCALL result (RCX, RDX, R8-R11). As far as this
> function is concerned, its just a block of memory (accessed using
> base address + TDCALL_r* offsets).
Is 'struct tdcall_output' a hardware architectural structure or a
software structure?
If it's a software structure, then why are we passing a pointer to a
software structure into a hardware ABI?
If it's a hardware architecture structure, where is the documentation
for it?
>>> + tdcall
>>> +
>>> + /* Check for TDCALL success: 0 - Successful, otherwise failed */
>>> + test %rax, %rax
>>> + jnz 1f
>>> +
>>> + /* Check for a TDCALL output struct */
>>> + test %r12, %r12
>>> + jz 1f
>>
>> Does some universal status come back in r12? Aren't we dealing with a
>> VMM/SEAM-controlled register here? Isn't this dangerous?
>
> R12 is the temporary register we have used to store the address of user
> passed output pointer. We just check for NULL condition here. R12 will
> not be used by the TDX module.
OK, so how do you know this? Could you share your logic, please?
> If you prefer, we can just push the output pointer to stack and get it
> after we make the tdcall.
I prefer that the code be understandable and be written for a clear
purpose. If you're using r12 for temporary storage, I expect to see at
least one reference *SOMEWHERE* to its use as temporary storage. Right
now.... nothing.
>>> + /* Copy TDCALL result registers to output struct: */
>>> + movq %rcx, TDCALL_rcx(%r12)
>>> + movq %rdx, TDCALL_rdx(%r12)
>>> + movq %r8, TDCALL_r8(%r12)
>>> + movq %r9, TDCALL_r9(%r12)
>>> + movq %r10, TDCALL_r10(%r12)
>>> + movq %r11, TDCALL_r11(%r12)
>>> +1:
>>> + /* Zero out registers exposed to the TDX Module. */
>>> + xor %rcx, %rcx
>>> + xor %rdx, %rdx
>>> + xor %r8d, %r8d
>>> + xor %r9d, %r9d
>>> + xor %r10d, %r10d
>>> + xor %r11d, %r11d
>>
>> ... why?
>
> These registers are used by the TDX Module. Why pass the stale values
> back to the user? So we clear them here.
Please go look at some other assembly code in the kernel called from C.
Do those functions do this? Why? Why not? Do they care about
"passing stale values back up"?
>>> +SYM_CODE_START_LOCAL(do_tdvmcall)
>>> + FRAME_BEGIN
>>> +
>>> + /* Save non-volatile GPRs that are exposed to the VMM. */
>>> + push %r15
>>> + push %r14
>>> + push %r13
>>> + push %r12
>>> +
>>> + /* Set TDCALL leaf ID to TDVMCALL (0) in RAX */
>>
>> I think there needs to be some discussion of what TDCALL and TDVMCALL
>> are. They are named too similarly not to do so.
>
> TDVMCALL is the sub function of TDCALL (selected by setting RAX register
> to 0). TDVMCALL is used to request services from VMM.
Actually, I think these functions are horribly misnamed.
I think we should make them
__tdx_seam_call()
or __tdx_module_call()
and
__tdx_hypercall()
__tdcall()
and
__tdvmcall()
are really nonsensical in this context, especially since TDVMCALL is
implemented with the TDCALL instruction, but not the __tdcall() function.
>>> +/* Helper function for standard type of TDVMCALL */
>>> +SYM_FUNC_START(__tdvmcall)
>>> + /* Set TDVMCALL type info (0 - Standard, > 0 - vendor) in R10 */
>>> + xor %r10, %r10
>>> + call do_tdvmcall
>>> + retq
>>> +SYM_FUNC_END(__tdvmcall)
>>
>> Why do we need this helper? Why does it need to be in assembly?
>
> Its simpler to do it in assembly. Also, grouping all register updates
> in the same file will make it easier for us to read or debug issues.
> Another
> reason is, we also call do_tdvmcall() from in/out instruction use case.
Sathya, I seem to have to reverse-engineer what you are doing for all
this stuff. Your answers to my questions are almost entirely orthogonal
to the things I really want to know. I guess I need to be more precise
with the questions I'm asking. But, this is yet another case where I
think the burden for this series continues to fall on the reviewer
rather than the submitter. Not the way I think it is best.
So, trying to reverse-engineer what you are doing here... it seems that
you can't *practically* call do_tdvmcall() directly because %r10 would
be garbage. That makes this (or a wrapper like it) required for every
practical call to do_tdvmcall().
But, even if that's the case, you need to *DOCUMENT* that up in
do_tdvmcall(): Hey, this function is worthless without something that
sets up %r10 before calling it.
I'm also not *SURE* this is simpler to do in assembly.
>>> diff --git a/arch/x86/kernel/tdx.c b/arch/x86/kernel/tdx.c
>>> index 6a7193fead08..29c52128b9c0 100644
>>> --- a/arch/x86/kernel/tdx.c
>>> +++ b/arch/x86/kernel/tdx.c
>>> @@ -1,8 +1,44 @@
>>> // SPDX-License-Identifier: GPL-2.0
>>> /* Copyright (C) 2020 Intel Corporation */
>>> +#define pr_fmt(fmt) "TDX: " fmt
>>> +
>>> #include <asm/tdx.h>
>>> +/*
>>> + * Wrapper for use case that checks for error code and print warning
>>> message.
>>> + */
>>
>> This comment isn't very useful. I can see the error check and warning
>> by reading the code.
>
> Its just a helper function that covers common case of checking for error
> and print the warning message. If this comment is superfluous, I can remove
> it.
I'd prefer that you actually write a comment about what the function is
doing, maybe:
/*
* Wrapper for simple hypercalls that only return a success/error code.
*/
... or *SOMETHING* that tells what its purpose in life is.
>>> +static inline u64 tdvmcall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15)
>>> +{
>>> + u64 err;
>>> +
>>> + err = __tdvmcall(fn, r12, r13, r14, r15, NULL);
>>> +
>>> + if (err)
>>> + pr_warn_ratelimited("TDVMCALL fn:%llx failed with err:%llx\n",
>>> + fn, err);
>>> +
>>> + return err;
>>> +}
>>> +
>>> +/*
>>> + * Wrapper for the semi-common case where we need single output
>>> value (R11).
>>> + */
>>> +static inline u64 tdvmcall_out_r11(u64 fn, u64 r12, u64 r13, u64
>>> r14, u64 r15)
>>> +{
>>> +
>>> + struct tdvmcall_output out = {0};
>>> + u64 err;
>>> +
>>> + err = __tdvmcall(fn, r12, r13, r14, r15, &out);
>>> +
>>> + if (err)
>>> + pr_warn_ratelimited("TDVMCALL fn:%llx failed with err:%llx\n",
>>> + fn, err);
>>> +
>>> + return out.r11;
>>> +}
>>
>> How do callers check for errors? Is the error value superfluously
>> returned in r11 and another output register?
>
> We already check for error in this helper function. User of this function
> only cares about output value (R11). Mainly for in/out use case.
That's pretty valuable information.