Skip to content

Commit b577f54

Browse files
kirylsuryasaimadhu
authored andcommitted
x86/coco: Add API to handle encryption mask
AMD SME/SEV uses a bit in the page table entries to indicate that the page is encrypted and not accessible to the VMM. TDX uses a similar approach, but the polarity of the mask is opposite to AMD: if the bit is set the page is accessible to VMM. Provide vendor-neutral API to deal with the mask: cc_mkenc() and cc_mkdec() modify given address to make it encrypted/decrypted. It can be applied to phys_addr_t, pgprotval_t or page table entry value. pgprot_encrypted() and pgprot_decrypted() reimplemented using new helpers. The implementation will be extended to cover TDX. pgprot_decrypted() is used by drivers (i915, virtio_gpu, vfio). cc_mkdec() called by pgprot_decrypted(). Export cc_mkdec(). Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Signed-off-by: Borislav Petkov <bp@suse.de> Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com> Link: https://lore.kernel.org/r/20220222185740.26228-5-kirill.shutemov@linux.intel.com
1 parent 655a0fa commit b577f54

5 files changed

Lines changed: 56 additions & 8 deletions

File tree

arch/x86/coco/core.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <asm/processor.h>
1515

1616
static enum cc_vendor vendor __ro_after_init;
17+
static u64 cc_mask __ro_after_init;
1718

1819
static bool intel_cc_platform_has(enum cc_attr attr)
1920
{
@@ -84,7 +85,33 @@ bool cc_platform_has(enum cc_attr attr)
8485
}
8586
EXPORT_SYMBOL_GPL(cc_platform_has);
8687

88+
u64 cc_mkenc(u64 val)
89+
{
90+
switch (vendor) {
91+
case CC_VENDOR_AMD:
92+
return val | cc_mask;
93+
default:
94+
return val;
95+
}
96+
}
97+
98+
u64 cc_mkdec(u64 val)
99+
{
100+
switch (vendor) {
101+
case CC_VENDOR_AMD:
102+
return val & ~cc_mask;
103+
default:
104+
return val;
105+
}
106+
}
107+
EXPORT_SYMBOL_GPL(cc_mkdec);
108+
87109
__init void cc_set_vendor(enum cc_vendor v)
88110
{
89111
vendor = v;
90112
}
113+
114+
__init void cc_set_mask(u64 mask)
115+
{
116+
cc_mask = mask;
117+
}

arch/x86/include/asm/coco.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#ifndef _ASM_X86_COCO_H
33
#define _ASM_X86_COCO_H
44

5+
#include <asm/types.h>
6+
57
enum cc_vendor {
68
CC_VENDOR_NONE,
79
CC_VENDOR_AMD,
@@ -10,5 +12,21 @@ enum cc_vendor {
1012
};
1113

1214
void cc_set_vendor(enum cc_vendor v);
15+
void cc_set_mask(u64 mask);
16+
17+
#ifdef CONFIG_ARCH_HAS_CC_PLATFORM
18+
u64 cc_mkenc(u64 val);
19+
u64 cc_mkdec(u64 val);
20+
#else
21+
static inline u64 cc_mkenc(u64 val)
22+
{
23+
return val;
24+
}
25+
26+
static inline u64 cc_mkdec(u64 val)
27+
{
28+
return val;
29+
}
30+
#endif
1331

1432
#endif /* _ASM_X86_COCO_H */

arch/x86/include/asm/pgtable.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@
1515
cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS))) \
1616
: (prot))
1717

18-
/*
19-
* Macros to add or remove encryption attribute
20-
*/
21-
#define pgprot_encrypted(prot) __pgprot(__sme_set(pgprot_val(prot)))
22-
#define pgprot_decrypted(prot) __pgprot(__sme_clr(pgprot_val(prot)))
23-
2418
#ifndef __ASSEMBLY__
2519
#include <linux/spinlock.h>
2620
#include <asm/x86_init.h>
2721
#include <asm/pkru.h>
2822
#include <asm/fpu/api.h>
23+
#include <asm/coco.h>
2924
#include <asm-generic/pgtable_uffd.h>
3025
#include <linux/page_table_check.h>
3126

@@ -38,6 +33,12 @@ void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm,
3833
void ptdump_walk_pgd_level_checkwx(void);
3934
void ptdump_walk_user_pgd_level_checkwx(void);
4035

36+
/*
37+
* Macros to add or remove encryption attribute
38+
*/
39+
#define pgprot_encrypted(prot) __pgprot(cc_mkenc(pgprot_val(prot)))
40+
#define pgprot_decrypted(prot) __pgprot(cc_mkdec(pgprot_val(prot)))
41+
4142
#ifdef CONFIG_DEBUG_WX
4243
#define debug_checkwx() ptdump_walk_pgd_level_checkwx()
4344
#define debug_checkwx_user() ptdump_walk_user_pgd_level_checkwx()

arch/x86/mm/mem_encrypt_identity.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,5 +604,6 @@ void __init sme_enable(struct boot_params *bp)
604604
if (sme_me_mask) {
605605
physical_mask &= ~sme_me_mask;
606606
cc_set_vendor(CC_VENDOR_AMD);
607+
cc_set_mask(sme_me_mask);
607608
}
608609
}

arch/x86/mm/pat/set_memory.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,7 @@ int set_memory_global(unsigned long addr, int numpages)
19891989
*/
19901990
static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc)
19911991
{
1992+
pgprot_t empty = __pgprot(0);
19921993
struct cpa_data cpa;
19931994
int ret;
19941995

@@ -1999,8 +2000,8 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc)
19992000
memset(&cpa, 0, sizeof(cpa));
20002001
cpa.vaddr = &addr;
20012002
cpa.numpages = numpages;
2002-
cpa.mask_set = enc ? __pgprot(_PAGE_ENC) : __pgprot(0);
2003-
cpa.mask_clr = enc ? __pgprot(0) : __pgprot(_PAGE_ENC);
2003+
cpa.mask_set = enc ? pgprot_encrypted(empty) : pgprot_decrypted(empty);
2004+
cpa.mask_clr = enc ? pgprot_decrypted(empty) : pgprot_encrypted(empty);
20042005
cpa.pgd = init_mm.pgd;
20052006

20062007
/* Must avoid aliasing mappings in the highmem code */

0 commit comments

Comments
 (0)