Re: [PATCHv2 22/29] x86/tdx: Make pages shared in ioremap()

Kirill A. Shutemov
2022-02-16T15:49:58Z

On Wed, Feb 16, 2022 at 10:58:34AM +0100, Borislav Petkov wrote:
> On Tue, Feb 15, 2022 at 08:33:21PM +0300, Kirill A. Shutemov wrote:
> > Like cc_mkencrypted()/cc_mkdecrypted()? I donno. Looks strange.
>
> cc_mkenc/cc_mkdec probably.

Okay, what about this:

static u64 cc_mask;

pgprotval_t cc_mkenc(pgprotval_t protval)
{
if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST))
return protval & ~cc_mask;
else
return protval | cc_mask;
}

pgprotval_t cc_mkdec(pgprotval_t protval)
{
if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST))
return protval | cc_mask;
else
return protval & ~cc_mask;
}
EXPORT_SYMBOL_GPL(cc_mkdec);

__init void cc_init(void)
{
if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST))
cc_mask = tdx_shared_mask();
else
cc_mask = sme_me_mask;
}

I did not introduce explicit vendor variable but opted for
X86_FEATURE_TDX_GUEST to check vendor. There's no X86_FEATURE counter part
on AMD side presumably because it get used too early to be functional.

TDX needs cc_platform_has() later and X86_FEATURE infrastructure is
already functional there (and we can benefit from static branch).

cc_init() got called from sme_enable() for AMD and from tdx_early_init()
for TDX.

I also reworked cc_platform_has() to use combination of
X86_FEATURE_TDX_GUEST and cc_mask to route to right helper:

bool cc_platform_has(enum cc_attr attr)
{
if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST))
return intel_cc_platform_has(attr);
else if (cc_mask)
return amd_cc_platform_has(attr);
else if (hv_is_isolation_supported())
return hyperv_cc_platform_has(attr);
else
return false;
}

Any opinions?

--
Kirill A. Shutemov