[RFC PATCH v4 07/28] x86: Provide general kernel support for memory encryption
Adding general kernel support for memory encryption includes: - Modify and create some page table macros to include the Secure Memory Encryption (SME) memory encryption mask - Modify and create some macros for calculating physical and virtual memory addresses - Provide an SME initialization routine to update the protection map with the memory encryption mask so that it is used by default - #undef CONFIG_AMD_MEM_ENCRYPT in the compressed boot path Signed-off-by: Tom Lendacky --- arch/x86/boot/compressed/pagetable.c |7 + arch/x86/include/asm/fixmap.h|7 + arch/x86/include/asm/mem_encrypt.h | 14 +++ arch/x86/include/asm/page.h |4 ++- arch/x86/include/asm/pgtable.h | 26 ++-- arch/x86/include/asm/pgtable_types.h | 45 ++ arch/x86/include/asm/processor.h |3 ++ arch/x86/kernel/espfix_64.c |2 +- arch/x86/kernel/head64.c | 12 - arch/x86/kernel/head_64.S| 18 +++--- arch/x86/mm/kasan_init_64.c |4 ++- arch/x86/mm/mem_encrypt.c| 20 +++ arch/x86/mm/pageattr.c |3 ++ include/asm-generic/pgtable.h|8 ++ 14 files changed, 133 insertions(+), 40 deletions(-) diff --git a/arch/x86/boot/compressed/pagetable.c b/arch/x86/boot/compressed/pagetable.c index 56589d0..411c443 100644 --- a/arch/x86/boot/compressed/pagetable.c +++ b/arch/x86/boot/compressed/pagetable.c @@ -15,6 +15,13 @@ #define __pa(x) ((unsigned long)(x)) #define __va(x) ((void *)((unsigned long)(x))) +/* + * The pgtable.h and mm/ident_map.c includes make use of the SME related + * information which is not used in the compressed image support. Un-define + * the SME support to avoid any compile and link errors. + */ +#undef CONFIG_AMD_MEM_ENCRYPT + #include "misc.h" /* These actually do the work of building the kernel identity maps. */ diff --git a/arch/x86/include/asm/fixmap.h b/arch/x86/include/asm/fixmap.h index 8554f96..83e91f0 100644 --- a/arch/x86/include/asm/fixmap.h +++ b/arch/x86/include/asm/fixmap.h @@ -153,6 +153,13 @@ static inline void __set_fixmap(enum fixed_addresses idx, } #endif +/* + * Fixmap settings used with memory encryption + * - FIXMAP_PAGE_NOCACHE is used for MMIO so make sure the memory + * encryption mask is not part of the page attributes + */ +#define FIXMAP_PAGE_NOCACHE PAGE_KERNEL_IO_NOCACHE + #include #define __late_set_fixmap(idx, phys, flags) __set_fixmap(idx, phys, flags) diff --git a/arch/x86/include/asm/mem_encrypt.h b/arch/x86/include/asm/mem_encrypt.h index ccc53b0..547989d 100644 --- a/arch/x86/include/asm/mem_encrypt.h +++ b/arch/x86/include/asm/mem_encrypt.h @@ -15,6 +15,8 @@ #ifndef __ASSEMBLY__ +#include + #ifdef CONFIG_AMD_MEM_ENCRYPT extern unsigned long sme_me_mask; @@ -24,6 +26,11 @@ static inline bool sme_active(void) return (sme_me_mask) ? true : false; } +void __init sme_early_init(void); + +#define __sme_pa(x)(__pa((x)) | sme_me_mask) +#define __sme_pa_nodebug(x)(__pa_nodebug((x)) | sme_me_mask) + #else /* !CONFIG_AMD_MEM_ENCRYPT */ #ifndef sme_me_mask @@ -35,6 +42,13 @@ static inline bool sme_active(void) } #endif +static inline void __init sme_early_init(void) +{ +} + +#define __sme_pa __pa +#define __sme_pa_nodebug __pa_nodebug + #endif /* CONFIG_AMD_MEM_ENCRYPT */ #endif /* __ASSEMBLY__ */ diff --git a/arch/x86/include/asm/page.h b/arch/x86/include/asm/page.h index cf8f619..b1f7bf6 100644 --- a/arch/x86/include/asm/page.h +++ b/arch/x86/include/asm/page.h @@ -15,6 +15,8 @@ #ifndef __ASSEMBLY__ +#include + struct page; #include @@ -55,7 +57,7 @@ static inline void copy_user_page(void *to, void *from, unsigned long vaddr, __phys_addr_symbol(__phys_reloc_hide((unsigned long)(x))) #ifndef __va -#define __va(x)((void *)((unsigned long)(x)+PAGE_OFFSET)) +#define __va(x)((void *)(((unsigned long)(x) & ~sme_me_mask) + PAGE_OFFSET)) #endif #define __boot_va(x) __va(x) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 2d81161..b41caab 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -3,6 +3,7 @@ #include #include +#include /* * Macro to mark a page protection value as UC- @@ -13,6 +14,12 @@ cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS))) \ : (prot)) +/* + * Macros to add or remove encryption attribute + */ +#define pgprot_encrypted(prot) __pgprot(pgprot_val(prot) | sme_me_mask) +#define pgprot_decrypted(prot) __pgprot(pgprot_val(prot) & ~sme_me_mask) + #ifndef __ASSEMBLY__ #include @@ -153,17 +160,22 @@ static inline int pte_special(pte_t pte) static inline unsigned long pte_pfn(pte_t pte) { - return (pte_val(pte) & PTE_PFN_MASK) >> PAGE_SHIFT; + r
Re: [RFC PATCH v4 07/28] x86: Provide general kernel support for memory encryption
On Thu, Feb 16, 2017 at 09:43:32AM -0600, Tom Lendacky wrote: > Adding general kernel support for memory encryption includes: > - Modify and create some page table macros to include the Secure Memory > Encryption (SME) memory encryption mask Let's not write it like some technical document: "Secure Memory Encryption (SME) mask" is perfectly fine. > - Modify and create some macros for calculating physical and virtual > memory addresses > - Provide an SME initialization routine to update the protection map with > the memory encryption mask so that it is used by default > - #undef CONFIG_AMD_MEM_ENCRYPT in the compressed boot path These bulletpoints talk about the "what" this patch does but they should talk about the "why". For example, it doesn't say why we're using _KERNPG_TABLE_NOENC when building the initial pagetable and that would be an interesting piece of information. > Signed-off-by: Tom Lendacky > --- > arch/x86/boot/compressed/pagetable.c |7 + > arch/x86/include/asm/fixmap.h|7 + > arch/x86/include/asm/mem_encrypt.h | 14 +++ > arch/x86/include/asm/page.h |4 ++- > arch/x86/include/asm/pgtable.h | 26 ++-- > arch/x86/include/asm/pgtable_types.h | 45 > ++ > arch/x86/include/asm/processor.h |3 ++ > arch/x86/kernel/espfix_64.c |2 +- > arch/x86/kernel/head64.c | 12 - > arch/x86/kernel/head_64.S| 18 +++--- > arch/x86/mm/kasan_init_64.c |4 ++- > arch/x86/mm/mem_encrypt.c| 20 +++ > arch/x86/mm/pageattr.c |3 ++ > include/asm-generic/pgtable.h|8 ++ > 14 files changed, 133 insertions(+), 40 deletions(-) > > diff --git a/arch/x86/boot/compressed/pagetable.c > b/arch/x86/boot/compressed/pagetable.c > index 56589d0..411c443 100644 > --- a/arch/x86/boot/compressed/pagetable.c > +++ b/arch/x86/boot/compressed/pagetable.c > @@ -15,6 +15,13 @@ > #define __pa(x) ((unsigned long)(x)) > #define __va(x) ((void *)((unsigned long)(x))) > > +/* > + * The pgtable.h and mm/ident_map.c includes make use of the SME related > + * information which is not used in the compressed image support. Un-define > + * the SME support to avoid any compile and link errors. > + */ > +#undef CONFIG_AMD_MEM_ENCRYPT > + > #include "misc.h" > > /* These actually do the work of building the kernel identity maps. */ > diff --git a/arch/x86/include/asm/fixmap.h b/arch/x86/include/asm/fixmap.h > index 8554f96..83e91f0 100644 > --- a/arch/x86/include/asm/fixmap.h > +++ b/arch/x86/include/asm/fixmap.h > @@ -153,6 +153,13 @@ static inline void __set_fixmap(enum fixed_addresses idx, > } > #endif > > +/* > + * Fixmap settings used with memory encryption > + * - FIXMAP_PAGE_NOCACHE is used for MMIO so make sure the memory > + * encryption mask is not part of the page attributes Make that a regular sentence. > + */ > +#define FIXMAP_PAGE_NOCACHE PAGE_KERNEL_IO_NOCACHE > + > #include > > #define __late_set_fixmap(idx, phys, flags) __set_fixmap(idx, phys, flags) > diff --git a/arch/x86/include/asm/mem_encrypt.h > b/arch/x86/include/asm/mem_encrypt.h > index ccc53b0..547989d 100644 > --- a/arch/x86/include/asm/mem_encrypt.h > +++ b/arch/x86/include/asm/mem_encrypt.h > @@ -15,6 +15,8 @@ > > #ifndef __ASSEMBLY__ > > +#include > + > #ifdef CONFIG_AMD_MEM_ENCRYPT > > extern unsigned long sme_me_mask; > @@ -24,6 +26,11 @@ static inline bool sme_active(void) > return (sme_me_mask) ? true : false; > } > > +void __init sme_early_init(void); > + > +#define __sme_pa(x) (__pa((x)) | sme_me_mask) > +#define __sme_pa_nodebug(x) (__pa_nodebug((x)) | sme_me_mask) Right, I know we did talk about those but in looking more into the future, you'd have to go educate people to use the __sme_pa* variants. Otherwise, we'd have to go and fix up code on AMD SME machines because someone used __pa_* variants where someone should have been using the __sma_pa_* variants. IOW, should we simply put sme_me_mask in the actual __pa* macro definitions? Or are we saying that the __sme_pa* versions you have above are the special ones and we need them only in a handful of places like load_cr3(), for example...? And the __pa_* ones should return the physical address without the SME mask because callers don't need it? > + > #else/* !CONFIG_AMD_MEM_ENCRYPT */ > > #ifndef sme_me_mask > @@ -35,6 +42,13 @@ static inline bool sme_active(void) > } > #endif > > +static inline void __init sme_early_init(void) > +{ > +} > + > +#define __sme_pa __pa > +#define __sme_pa_nodebug __pa_nodebug > + > #endif /* CONFIG_AMD_MEM_ENCRYPT */ > > #endif /* __ASSEMBLY__ */ > diff --git a/arch/x86/include/asm/page.h b/arch/x86/include/asm/page.h > index cf8f619..b1f7bf6 100644 > --- a/arch/x86/include/asm/page.h > +++ b/arch/x86/include/asm/page.h >
Re: [RFC PATCH v4 07/28] x86: Provide general kernel support for memory encryption
On Thu, Feb 16, 2017 at 09:43:32AM -0600, Tom Lendacky wrote: > Adding general kernel support for memory encryption includes: > - Modify and create some page table macros to include the Secure Memory > Encryption (SME) memory encryption mask > - Modify and create some macros for calculating physical and virtual > memory addresses > - Provide an SME initialization routine to update the protection map with > the memory encryption mask so that it is used by default > - #undef CONFIG_AMD_MEM_ENCRYPT in the compressed boot path > > Signed-off-by: Tom Lendacky ... > +#define __sme_pa(x) (__pa((x)) | sme_me_mask) > +#define __sme_pa_nodebug(x) (__pa_nodebug((x)) | sme_me_mask) > + > #else/* !CONFIG_AMD_MEM_ENCRYPT */ > > #ifndef sme_me_mask > @@ -35,6 +42,13 @@ static inline bool sme_active(void) > } > #endif > > +static inline void __init sme_early_init(void) > +{ > +} > + > +#define __sme_pa __pa > +#define __sme_pa_nodebug __pa_nodebug One more thing - in the !CONFIG_AMD_MEM_ENCRYPT case, sme_me_mask is 0 so you don't need to define __sme_pa* again. -- Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply. -- To unsubscribe from this list: send the line "unsubscribe linux-doc" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [RFC PATCH v4 07/28] x86: Provide general kernel support for memory encryption
On 2/20/2017 9:21 AM, Borislav Petkov wrote: On Thu, Feb 16, 2017 at 09:43:32AM -0600, Tom Lendacky wrote: Adding general kernel support for memory encryption includes: - Modify and create some page table macros to include the Secure Memory Encryption (SME) memory encryption mask Let's not write it like some technical document: "Secure Memory Encryption (SME) mask" is perfectly fine. Ok. - Modify and create some macros for calculating physical and virtual memory addresses - Provide an SME initialization routine to update the protection map with the memory encryption mask so that it is used by default - #undef CONFIG_AMD_MEM_ENCRYPT in the compressed boot path These bulletpoints talk about the "what" this patch does but they should talk about the "why". For example, it doesn't say why we're using _KERNPG_TABLE_NOENC when building the initial pagetable and that would be an interesting piece of information. I'll work on re-wording this to give a better understanding of the patch changes. Signed-off-by: Tom Lendacky --- arch/x86/boot/compressed/pagetable.c |7 + arch/x86/include/asm/fixmap.h|7 + arch/x86/include/asm/mem_encrypt.h | 14 +++ arch/x86/include/asm/page.h |4 ++- arch/x86/include/asm/pgtable.h | 26 ++-- arch/x86/include/asm/pgtable_types.h | 45 ++ arch/x86/include/asm/processor.h |3 ++ arch/x86/kernel/espfix_64.c |2 +- arch/x86/kernel/head64.c | 12 - arch/x86/kernel/head_64.S| 18 +++--- arch/x86/mm/kasan_init_64.c |4 ++- arch/x86/mm/mem_encrypt.c| 20 +++ arch/x86/mm/pageattr.c |3 ++ include/asm-generic/pgtable.h|8 ++ 14 files changed, 133 insertions(+), 40 deletions(-) diff --git a/arch/x86/boot/compressed/pagetable.c b/arch/x86/boot/compressed/pagetable.c index 56589d0..411c443 100644 --- a/arch/x86/boot/compressed/pagetable.c +++ b/arch/x86/boot/compressed/pagetable.c @@ -15,6 +15,13 @@ #define __pa(x) ((unsigned long)(x)) #define __va(x) ((void *)((unsigned long)(x))) +/* + * The pgtable.h and mm/ident_map.c includes make use of the SME related + * information which is not used in the compressed image support. Un-define + * the SME support to avoid any compile and link errors. + */ +#undef CONFIG_AMD_MEM_ENCRYPT + #include "misc.h" /* These actually do the work of building the kernel identity maps. */ diff --git a/arch/x86/include/asm/fixmap.h b/arch/x86/include/asm/fixmap.h index 8554f96..83e91f0 100644 --- a/arch/x86/include/asm/fixmap.h +++ b/arch/x86/include/asm/fixmap.h @@ -153,6 +153,13 @@ static inline void __set_fixmap(enum fixed_addresses idx, } #endif +/* + * Fixmap settings used with memory encryption + * - FIXMAP_PAGE_NOCACHE is used for MMIO so make sure the memory + * encryption mask is not part of the page attributes Make that a regular sentence. Ok. + */ +#define FIXMAP_PAGE_NOCACHE PAGE_KERNEL_IO_NOCACHE + #include #define __late_set_fixmap(idx, phys, flags) __set_fixmap(idx, phys, flags) diff --git a/arch/x86/include/asm/mem_encrypt.h b/arch/x86/include/asm/mem_encrypt.h index ccc53b0..547989d 100644 --- a/arch/x86/include/asm/mem_encrypt.h +++ b/arch/x86/include/asm/mem_encrypt.h @@ -15,6 +15,8 @@ #ifndef __ASSEMBLY__ +#include + #ifdef CONFIG_AMD_MEM_ENCRYPT extern unsigned long sme_me_mask; @@ -24,6 +26,11 @@ static inline bool sme_active(void) return (sme_me_mask) ? true : false; } +void __init sme_early_init(void); + +#define __sme_pa(x)(__pa((x)) | sme_me_mask) +#define __sme_pa_nodebug(x)(__pa_nodebug((x)) | sme_me_mask) Right, I know we did talk about those but in looking more into the future, you'd have to go educate people to use the __sme_pa* variants. Otherwise, we'd have to go and fix up code on AMD SME machines because someone used __pa_* variants where someone should have been using the __sma_pa_* variants. IOW, should we simply put sme_me_mask in the actual __pa* macro definitions? Or are we saying that the __sme_pa* versions you have above are the special ones and we need them only in a handful of places like load_cr3(), for example...? And the __pa_* ones should return the physical address without the SME mask because callers don't need it? It's the latter. It's really only used for working with values that will either be written to or read from cr3. I'll add some comments around the macros as well as expand on it in the commit message. + #else /* !CONFIG_AMD_MEM_ENCRYPT */ #ifndef sme_me_mask @@ -35,6 +42,13 @@ static inline bool sme_active(void) } #endif +static inline void __init sme_early_init(void) +{ +} + +#define __sme_pa __pa +#define __sme_pa_nodebug __pa_nodebug + #endif /* CONFIG_AMD_MEM_ENCRYPT */ #endif /* __ASSEMBLY__ */ diff --git a/arch/x86/include/asm/page.h b
Re: [RFC PATCH v4 07/28] x86: Provide general kernel support for memory encryption
On Tue, Feb 21, 2017 at 11:18:08AM -0600, Tom Lendacky wrote: > It's the latter. It's really only used for working with values that > will either be written to or read from cr3. I'll add some comments > around the macros as well as expand on it in the commit message. Ok, that makes sense. Normally we will have the mask in the lower levels of the pagetable hierarchy but we need to add it to the CR3 value by hand. Yap. > Ok, I'll try and come up with something... maybe __sme_rm or > __sme_clear (__sme_clr). __sme_clr looks nice to me :) Thanks. -- Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply. -- To unsubscribe from this list: send the line "unsubscribe linux-doc" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [RFC PATCH v4 07/28] x86: Provide general kernel support for memory encryption
On 2/20/2017 12:38 PM, Borislav Petkov wrote: On Thu, Feb 16, 2017 at 09:43:32AM -0600, Tom Lendacky wrote: Adding general kernel support for memory encryption includes: - Modify and create some page table macros to include the Secure Memory Encryption (SME) memory encryption mask - Modify and create some macros for calculating physical and virtual memory addresses - Provide an SME initialization routine to update the protection map with the memory encryption mask so that it is used by default - #undef CONFIG_AMD_MEM_ENCRYPT in the compressed boot path Signed-off-by: Tom Lendacky ... +#define __sme_pa(x)(__pa((x)) | sme_me_mask) +#define __sme_pa_nodebug(x)(__pa_nodebug((x)) | sme_me_mask) + #else /* !CONFIG_AMD_MEM_ENCRYPT */ #ifndef sme_me_mask @@ -35,6 +42,13 @@ static inline bool sme_active(void) } #endif +static inline void __init sme_early_init(void) +{ +} + +#define __sme_pa __pa +#define __sme_pa_nodebug __pa_nodebug One more thing - in the !CONFIG_AMD_MEM_ENCRYPT case, sme_me_mask is 0 so you don't need to define __sme_pa* again. Makes sense. I'll move those macros outside the #ifdef (I'll do the same for the new __sme_clr() and __sme_set() macros, too). Thanks, Tom -- To unsubscribe from this list: send the line "unsubscribe linux-doc" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [RFC PATCH v4 07/28] x86: Provide general kernel support for memory encryption
On 02/16/2017 07:43 AM, Tom Lendacky wrote: > static inline unsigned long pte_pfn(pte_t pte) > { > - return (pte_val(pte) & PTE_PFN_MASK) >> PAGE_SHIFT; > + return (pte_val(pte) & ~sme_me_mask & PTE_PFN_MASK) >> PAGE_SHIFT; > } > > static inline unsigned long pmd_pfn(pmd_t pmd) > { > - return (pmd_val(pmd) & pmd_pfn_mask(pmd)) >> PAGE_SHIFT; > + return (pmd_val(pmd) & ~sme_me_mask & pmd_pfn_mask(pmd)) >> PAGE_SHIFT; > } Could you talk a bit about why you chose to do the "~sme_me_mask" bit in here instead of making it a part of PTE_PFN_MASK / pmd_pfn_mask(pmd)? It might not matter, but I'd be worried that this ends up breaking direct users of PTE_PFN_MASK / pmd_pfn_mask(pmd) since they now no longer mask the PFN out of a PTE. -- To unsubscribe from this list: send the line "unsubscribe linux-doc" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [RFC PATCH v4 07/28] x86: Provide general kernel support for memory encryption
On 02/16/2017 07:43 AM, Tom Lendacky wrote: > ) > @@ -673,7 +683,7 @@ static inline unsigned long pgd_page_vaddr(pgd_t pgd) > * Currently stuck as a macro due to indirect forward reference to > * linux/mmzone.h's __section_mem_map_addr() definition: > */ > -#define pgd_page(pgd)pfn_to_page(pgd_val(pgd) >> PAGE_SHIFT) > +#define pgd_page(pgd)pfn_to_page(pgd_pfn(pgd)) FWIW, these seem like good cleanups that can go in separately from the rest of your series. -- To unsubscribe from this list: send the line "unsubscribe linux-doc" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [RFC PATCH v4 07/28] x86: Provide general kernel support for memory encryption
On 2/22/2017 12:13 PM, Dave Hansen wrote: On 02/16/2017 07:43 AM, Tom Lendacky wrote: static inline unsigned long pte_pfn(pte_t pte) { - return (pte_val(pte) & PTE_PFN_MASK) >> PAGE_SHIFT; + return (pte_val(pte) & ~sme_me_mask & PTE_PFN_MASK) >> PAGE_SHIFT; } static inline unsigned long pmd_pfn(pmd_t pmd) { - return (pmd_val(pmd) & pmd_pfn_mask(pmd)) >> PAGE_SHIFT; + return (pmd_val(pmd) & ~sme_me_mask & pmd_pfn_mask(pmd)) >> PAGE_SHIFT; } Could you talk a bit about why you chose to do the "~sme_me_mask" bit in here instead of making it a part of PTE_PFN_MASK / pmd_pfn_mask(pmd)? I think that's a good catch. Let me look at it, but I believe that it should be possible to do and avoid what you're worried about below. Thanks, Tom It might not matter, but I'd be worried that this ends up breaking direct users of PTE_PFN_MASK / pmd_pfn_mask(pmd) since they now no longer mask the PFN out of a PTE. -- To unsubscribe from this list: send the line "unsubscribe linux-doc" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html