Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753616AbcDZW57 (ORCPT ); Tue, 26 Apr 2016 18:57:59 -0400 Received: from mail-bn1bon0098.outbound.protection.outlook.com ([157.56.111.98]:48726 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753578AbcDZW5y (ORCPT ); Tue, 26 Apr 2016 18:57:54 -0400 X-Greylist: delayed 753 seconds by postgrey-1.27 at vger.kernel.org; Tue, 26 Apr 2016 18:57:53 EDT Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none;vger.kernel.org; dmarc=none action=none header.from=amd.com; From: Tom Lendacky Subject: [RFC PATCH v1 10/18] x86/efi: Access EFI related tables in the clear To: , , , , , , , , CC: Radim =?utf-8?b?S3LEjW3DocWZ?= , Arnd Bergmann , Jonathan Corbet , Matt Fleming , Joerg Roedel , "Konrad Rzeszutek Wilk" , Paolo Bonzini , "Ingo Molnar" , Borislav Petkov , "H. Peter Anvin" , Andrey Ryabinin , "Alexander Potapenko" , Thomas Gleixner , "Dmitry Vyukov" Date: Tue, 26 Apr 2016 17:57:40 -0500 Message-ID: <20160426225740.13567.85438.stgit@tlendack-t1.amdoffice.net> In-Reply-To: <20160426225553.13567.19459.stgit@tlendack-t1.amdoffice.net> References: <20160426225553.13567.19459.stgit@tlendack-t1.amdoffice.net> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SN1PR0701CA0010.namprd07.prod.outlook.com (10.162.96.20) To DM3PR1201MB1119.namprd12.prod.outlook.com (10.164.198.19) X-MS-Office365-Filtering-Correlation-Id: 51ad1601-0a00-4afc-15ae-08d36e262d98 X-Microsoft-Exchange-Diagnostics: 1;DM3PR1201MB1119;2:bm9429uIwyyl9mrhZN8eplEFvryeivUoCtBNN72vgtCa486vjD7CKUXd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D5bxbNHM6XlR52F23pfKwQHMJr7RP1dy6WFDUEsChQw8dUqoHaTFcaZgpdVzkVvB3hh+/UqvZEfdGIWPh7EmLRCqDrpTQDk1O6oQD3o+eGtyO0MGUxdL7JHjoQLhQaqwIIKgq86ra4QNVfcs7Qu89Ta9EGPAmvtV9I2XceTxG2dlSmusLClxg/chbbAiTICaS6zf5nHmJYpgh/DnO+zTioMJHRhCog5bdRqZGMLZYpr5eKpFaUJhqxu0kg33cSHoWdpkOM5DxVNgmn3+krSnV8z1LXt9zBMekCwkEf1sSCsdHPnuTrEV44e8bFTV3ltdQvfk0ei5a149aZd41gCx3O0q9/whEwJm44guEHfWAyph9xqC6AfB15VpemmXhsUyOg+ZfvqqXB781hHj0BWDGTW3PCPzmY1k+TolLandGA6gWo3ySZijj2YkCVBXHL0wMEbIbIPgpBNR588i0PppFMO0cXcqMzgSELehBJ3JmkUCS6Lbh8uXmohP/+9i6;4:E/7yLPwfaUa0wvl4b5oDxW/nvEClCWHmdAPh0EDhtdH6Vm8KMar/wxaqYZrRog0Fkq0IMSKmRshYqWQSFxCNVHR2mtj75zQlVkQyLTSLK03OFpH1MhR2FJ8SOK80LivCLWpziV+JKqpJHNIwdTLb1q0agHaDtO1ofu6y9qN5XO65C2vGCjT+H6BB727RX3Rru08GhIFZ8vLx7h+HQqzhdDpXUnA+IjWXjMCNY8hzS61CD3E6Jn00O8x0cvZxpI2GrsWPyKMW3dhfSm9qxxy33EMrNgP6aYzpgoM+zKDXyOPRiBjyCpkVm42CQbWZUhSfBUK1H/yZyIUMeeCsh00hmmNXDMAHukoG6B0e/EqU58OBe4pMHNUiKd6Iw9JBQQTYgBiv3d5fYXPBU/xJsDiTerVQzJehp76Oqst3M3To5+A= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(9101521072)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001)(6055026);SRVR:DM3PR1201MB1119;BCL:0;PCL:0;RULEID:;SRVR:DM3PR1201MB1119; X-Forefront-PRVS: 0924C6A0D5 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(66066001)(47776003)(4001350100001)(33646002)(97746001)(50466002)(229853001)(19580395003)(6116002)(19580405001)(2950100001)(5004730100002)(2201001)(586003)(1076002)(1096002)(5001770100001)(189998001)(53416004)(23676002)(86362001)(81166005)(9686002)(2906002)(42186005)(92566002)(77096005)(76176999)(50986999)(54356999)(5008740100001)(230700001)(103116003)(4326007)(71626007)(21314002)(217873001);DIR:OUT;SFP:1101;SCL:1;SRVR:DM3PR1201MB1119;H:tlendack-t1.amdoffice.net;FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtETTNQUjEyMDFNQjExMTk7MjM6VFJwSlpMSUl6eVRwS0dFTWxrc1R5em1R?= =?utf-8?B?anp5bTR3cTBqK0pULzhiYVFTRUw2VGgydUYxZjBnM1gveEU3ckVKS3JMUzRW?= =?utf-8?B?MkJrVjZqY1pqWDdLVDBhZ3ZYWHJpY3lKeG9PUmYxUGtUM1h0WnR6WGJlN3Ni?= =?utf-8?B?cVhvekdTc1NTcDlicGZNNXFTTEtGM2NFemN0SDJyZnVFZlRlZ1ZrcmdYV0hr?= =?utf-8?B?Q016NDQ4enNNdjRTN2dRL0hLbVNxUmJjeWdBVFVvdlEvcnFWTVBGQ0ZHaDJs?= =?utf-8?B?M1Jxc21nTFBYUE1pQ3d4TWQwUldQM2xUWDZlQXpZbUNOQ1dZcDUvUDZacm5p?= =?utf-8?B?RWlPTHdoYzBwVDBFdnBZcm44SnFHVmxYalBkaGM4NGNaN2tHTXFkNTlKME5a?= =?utf-8?B?Y2FBTUpFYm9XOFg1WFh5V2NpRFJ2eW42cG1EeC95STVoTjFnczR4STU3V243?= =?utf-8?B?QmVsdkN0UlprbkRBOHZ1dkxhdEFjOStWMStzVmUrL0lJRDFpdTRnU01Vajcx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051THZablMwRm9qdDlWZHk3TjVXa01SS3lF?= =?utf-8?Q?mDjiAIGA=3D?= X-Microsoft-Exchange-Diagnostics: 1;DM3PR1201MB1119;5:nla6ud3sfml83BDqG3UmkVl76/BPP5Or10FGsHCXn08HkoGu1K6vII5yMieZBYbBMSE//B2fl5qA0VLaPKfFsQjuF8nlr85c/odTSDoO7QfCd39n1VSzcXpBlMqNCMAGfIJH7j8K3Ql2FoATKkzlUw==;24:sobVoZXIUza33xmZvKdn1dtk+zYI5cAqybDmSvSsNKqbM61nN1keOfm9iNe5opmXrjdKiQeMztcaAlZwiZbnBK54Ox9GZ/UhqdQ15NXQWl8=;7:m42g+zOdYJvm7A9DvGN+uJUo0fZn/0cTIuwrvFZQFjOHunZATDa1KSqPmDZQllYHO77mNTSwmmjYb85Whw5Ql7gMyhTF65OLt6mC0dOjDJRflpiTNba6EInC+J/VlslVtBE0H/HMif8PK8SIrJN5i6bhnHRK1+FHZlq72eRItiI=;20:SddXnYYaWei0S/ZdBDsRkiGB9Gid58dPUx2v0bA3fF4AQ/s/jT56DJoU00avw+u+W0RucgbI/83whlFFB758vUlLKOXnT+YEUnFYE+DCMMEPjdT2ctxWwkLKRu+vz3L+xDvoGLhulQ5ZmJBWLUcLZXsuzCSNNFvNxG6qNGQL+2uxDpxMdErF2wbKWQGzRC+NiNAbUgcDkRjvF9GSCgANQUF15mSGGKA3qU6Wjzx5n8rK8dPZalSxQbsq0/llbrte X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Apr 2016 22:57:43.1344 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM3PR1201MB1119 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 17554 Lines: 563 The EFI tables are not encrypted and need to be accessed as such. Be sure to memmap them without the encryption attribute set. For EFI support that lives outside of the arch/x86 tree, create a routine that uses the __weak attribute so that it can be overridden by an architecture specific routine. When freeing boot services related memory, since it has been mapped as un-encrypted, be sure to change the mapping to encrypted for future use. Signed-off-by: Tom Lendacky --- arch/x86/include/asm/cacheflush.h | 3 + arch/x86/include/asm/mem_encrypt.h | 22 +++++++++++ arch/x86/kernel/setup.c | 6 +-- arch/x86/mm/mem_encrypt.c | 56 +++++++++++++++++++++++++++ arch/x86/mm/pageattr.c | 75 ++++++++++++++++++++++++++++++++++++ arch/x86/platform/efi/efi.c | 26 +++++++----- arch/x86/platform/efi/efi_64.c | 9 +++- arch/x86/platform/efi/quirks.c | 12 +++++- drivers/firmware/efi/efi.c | 18 +++++++-- drivers/firmware/efi/esrt.c | 12 +++--- include/linux/efi.h | 3 + 11 files changed, 212 insertions(+), 30 deletions(-) diff --git a/arch/x86/include/asm/cacheflush.h b/arch/x86/include/asm/cacheflush.h index 61518cf..bfb08e5 100644 --- a/arch/x86/include/asm/cacheflush.h +++ b/arch/x86/include/asm/cacheflush.h @@ -13,6 +13,7 @@ * Executability : eXeutable, NoteXecutable * Read/Write : ReadOnly, ReadWrite * Presence : NotPresent + * Encryption : ENCrypted, DECrypted * * Within a category, the attributes are mutually exclusive. * @@ -48,6 +49,8 @@ int set_memory_ro(unsigned long addr, int numpages); int set_memory_rw(unsigned long addr, int numpages); int set_memory_np(unsigned long addr, int numpages); int set_memory_4k(unsigned long addr, int numpages); +int set_memory_enc(unsigned long addr, int numpages); +int set_memory_dec(unsigned long addr, int numpages); int set_memory_array_uc(unsigned long *addr, int addrinarray); int set_memory_array_wc(unsigned long *addr, int addrinarray); diff --git a/arch/x86/include/asm/mem_encrypt.h b/arch/x86/include/asm/mem_encrypt.h index 2785493..42868f5 100644 --- a/arch/x86/include/asm/mem_encrypt.h +++ b/arch/x86/include/asm/mem_encrypt.h @@ -23,13 +23,23 @@ extern unsigned long sme_me_mask; u8 sme_get_me_loss(void); +int sme_set_mem_enc(void *vaddr, unsigned long size); +int sme_set_mem_dec(void *vaddr, unsigned long size); + void __init sme_early_mem_enc(resource_size_t paddr, unsigned long size); void __init sme_early_mem_dec(resource_size_t paddr, unsigned long size); +void __init *sme_early_memremap(resource_size_t paddr, + unsigned long size); + void __init sme_early_init(void); +/* Architecture __weak replacement functions */ +void __init *efi_me_early_memremap(resource_size_t paddr, + unsigned long size); + #define __sme_pa(x) (__pa((x)) | sme_me_mask) #define __sme_pa_nodebug(x) (__pa_nodebug((x)) | sme_me_mask) @@ -44,6 +54,16 @@ static inline u8 sme_get_me_loss(void) return 0; } +static inline int sme_set_mem_enc(void *vaddr, unsigned long size) +{ + return 0; +} + +static inline int sme_set_mem_dec(void *vaddr, unsigned long size) +{ + return 0; +} + static inline void __init sme_early_mem_enc(resource_size_t paddr, unsigned long size) { @@ -63,6 +83,8 @@ static inline void __init sme_early_init(void) #define __sme_va __va +#define sme_early_memremap early_memremap + #endif /* CONFIG_AMD_MEM_ENCRYPT */ #endif /* __ASSEMBLY__ */ diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index 1d29cf9..2e460fb 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -424,7 +424,7 @@ static void __init parse_setup_data(void) while (pa_data) { u32 data_len, data_type; - data = early_memremap(pa_data, sizeof(*data)); + data = sme_early_memremap(pa_data, sizeof(*data)); data_len = data->len + sizeof(struct setup_data); data_type = data->type; pa_next = data->next; @@ -457,7 +457,7 @@ static void __init e820_reserve_setup_data(void) return; while (pa_data) { - data = early_memremap(pa_data, sizeof(*data)); + data = sme_early_memremap(pa_data, sizeof(*data)); e820_update_range(pa_data, sizeof(*data)+data->len, E820_RAM, E820_RESERVED_KERN); pa_data = data->next; @@ -477,7 +477,7 @@ static void __init memblock_x86_reserve_range_setup_data(void) pa_data = boot_params.hdr.setup_data; while (pa_data) { - data = early_memremap(pa_data, sizeof(*data)); + data = sme_early_memremap(pa_data, sizeof(*data)); memblock_reserve(pa_data, sizeof(*data) + data->len); pa_data = data->next; early_memunmap(data, sizeof(*data)); diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 5f19ede..7d56d1b 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -14,12 +14,55 @@ #include #include +#include #include #include /* Buffer used for early in-place encryption by BSP, no locking needed */ static char me_early_buffer[PAGE_SIZE] __aligned(PAGE_SIZE); +int sme_set_mem_enc(void *vaddr, unsigned long size) +{ + unsigned long addr, numpages; + + if (!sme_me_mask) + return 0; + + addr = (unsigned long)vaddr & PAGE_MASK; + numpages = PAGE_ALIGN(size) >> PAGE_SHIFT; + + /* + * The set_memory_xxx functions take an integer for numpages, make + * sure it doesn't exceed that. + */ + if (numpages > INT_MAX) + return -EINVAL; + + return set_memory_enc(addr, numpages); +} +EXPORT_SYMBOL_GPL(sme_set_mem_enc); + +int sme_set_mem_dec(void *vaddr, unsigned long size) +{ + unsigned long addr, numpages; + + if (!sme_me_mask) + return 0; + + addr = (unsigned long)vaddr & PAGE_MASK; + numpages = PAGE_ALIGN(size) >> PAGE_SHIFT; + + /* + * The set_memory_xxx functions take an integer for numpages, make + * sure it doesn't exceed that. + */ + if (numpages > INT_MAX) + return -EINVAL; + + return set_memory_dec(addr, numpages); +} +EXPORT_SYMBOL_GPL(sme_set_mem_dec); + void __init sme_early_mem_enc(resource_size_t paddr, unsigned long size) { void *src, *dst; @@ -104,6 +147,12 @@ void __init sme_early_mem_dec(resource_size_t paddr, unsigned long size) } } +void __init *sme_early_memremap(resource_size_t paddr, + unsigned long size) +{ + return early_memremap_dec(paddr, size); +} + void __init sme_early_init(void) { unsigned int i; @@ -117,3 +166,10 @@ void __init sme_early_init(void) for (i = 0; i < ARRAY_SIZE(protection_map); i++) protection_map[i] = __pgprot(pgprot_val(protection_map[i]) | sme_me_mask); } + +/* Architecture __weak replacement functions */ +void __init *efi_me_early_memremap(resource_size_t paddr, + unsigned long size) +{ + return sme_early_memremap(paddr, size); +} diff --git a/arch/x86/mm/pageattr.c b/arch/x86/mm/pageattr.c index c055302..0384fb3 100644 --- a/arch/x86/mm/pageattr.c +++ b/arch/x86/mm/pageattr.c @@ -1731,6 +1731,81 @@ int set_memory_4k(unsigned long addr, int numpages) __pgprot(0), 1, 0, NULL); } +static int __set_memory_enc_dec(struct cpa_data *cpa) +{ + unsigned long addr; + int numpages; + int ret; + + if (*cpa->vaddr & ~PAGE_MASK) { + *cpa->vaddr &= PAGE_MASK; + + /* People should not be passing in unaligned addresses */ + WARN_ON_ONCE(1); + } + + addr = *cpa->vaddr; + numpages = cpa->numpages; + + /* Must avoid aliasing mappings in the highmem code */ + kmap_flush_unused(); + vm_unmap_aliases(); + + ret = __change_page_attr_set_clr(cpa, 1); + + /* Check whether we really changed something */ + if (!(cpa->flags & CPA_FLUSHTLB)) + goto out; + + /* + * On success we use CLFLUSH, when the CPU supports it to + * avoid the WBINVD. + */ + if (!ret && static_cpu_has(X86_FEATURE_CLFLUSH)) + cpa_flush_range(addr, numpages, 1); + else + cpa_flush_all(1); + +out: + return ret; +} + +int set_memory_enc(unsigned long addr, int numpages) +{ + struct cpa_data cpa; + + if (!sme_me_mask) + return 0; + + memset(&cpa, 0, sizeof(cpa)); + cpa.vaddr = &addr; + cpa.numpages = numpages; + cpa.mask_set = __pgprot(_PAGE_ENC); + cpa.mask_clr = __pgprot(0); + cpa.pgd = init_mm.pgd; + + return __set_memory_enc_dec(&cpa); +} +EXPORT_SYMBOL(set_memory_enc); + +int set_memory_dec(unsigned long addr, int numpages) +{ + struct cpa_data cpa; + + if (!sme_me_mask) + return 0; + + memset(&cpa, 0, sizeof(cpa)); + cpa.vaddr = &addr; + cpa.numpages = numpages; + cpa.mask_set = __pgprot(0); + cpa.mask_clr = __pgprot(_PAGE_ENC); + cpa.pgd = init_mm.pgd; + + return __set_memory_enc_dec(&cpa); +} +EXPORT_SYMBOL(set_memory_dec); + int set_pages_uc(struct page *page, int numpages) { unsigned long addr = (unsigned long)page_address(page); diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index 994a7df8..871b213 100644 --- a/arch/x86/platform/efi/efi.c +++ b/arch/x86/platform/efi/efi.c @@ -53,6 +53,7 @@ #include #include #include +#include #define EFI_DEBUG @@ -261,12 +262,12 @@ static int __init efi_systab_init(void *phys) u64 tmp = 0; if (efi_setup) { - data = early_memremap(efi_setup, sizeof(*data)); + data = sme_early_memremap(efi_setup, sizeof(*data)); if (!data) return -ENOMEM; } - systab64 = early_memremap((unsigned long)phys, - sizeof(*systab64)); + systab64 = sme_early_memremap((unsigned long)phys, + sizeof(*systab64)); if (systab64 == NULL) { pr_err("Couldn't map the system table!\n"); if (data) @@ -314,8 +315,8 @@ static int __init efi_systab_init(void *phys) } else { efi_system_table_32_t *systab32; - systab32 = early_memremap((unsigned long)phys, - sizeof(*systab32)); + systab32 = sme_early_memremap((unsigned long)phys, + sizeof(*systab32)); if (systab32 == NULL) { pr_err("Couldn't map the system table!\n"); return -ENOMEM; @@ -361,8 +362,8 @@ static int __init efi_runtime_init32(void) { efi_runtime_services_32_t *runtime; - runtime = early_memremap((unsigned long)efi.systab->runtime, - sizeof(efi_runtime_services_32_t)); + runtime = sme_early_memremap((unsigned long)efi.systab->runtime, + sizeof(efi_runtime_services_32_t)); if (!runtime) { pr_err("Could not map the runtime service table!\n"); return -ENOMEM; @@ -385,8 +386,8 @@ static int __init efi_runtime_init64(void) { efi_runtime_services_64_t *runtime; - runtime = early_memremap((unsigned long)efi.systab->runtime, - sizeof(efi_runtime_services_64_t)); + runtime = sme_early_memremap((unsigned long)efi.systab->runtime, + sizeof(efi_runtime_services_64_t)); if (!runtime) { pr_err("Could not map the runtime service table!\n"); return -ENOMEM; @@ -444,8 +445,8 @@ static int __init efi_memmap_init(void) return 0; /* Map the EFI memory map */ - memmap.map = early_memremap((unsigned long)memmap.phys_map, - memmap.nr_map * memmap.desc_size); + memmap.map = sme_early_memremap((unsigned long)memmap.phys_map, + memmap.nr_map * memmap.desc_size); if (memmap.map == NULL) { pr_err("Could not map the memory map!\n"); return -ENOMEM; @@ -490,7 +491,7 @@ void __init efi_init(void) /* * Show what we know for posterity */ - c16 = tmp = early_memremap(efi.systab->fw_vendor, 2); + c16 = tmp = sme_early_memremap(efi.systab->fw_vendor, 2); if (c16) { for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i) vendor[i] = *c16++; @@ -690,6 +691,7 @@ static void *realloc_pages(void *old_memmap, int old_shift) ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1); if (!ret) goto out; + sme_set_mem_dec(ret, PAGE_SIZE << (old_shift + 1)); /* * A first-time allocation doesn't have anything to copy. diff --git a/arch/x86/platform/efi/efi_64.c b/arch/x86/platform/efi/efi_64.c index 49e4dd4..834a992 100644 --- a/arch/x86/platform/efi/efi_64.c +++ b/arch/x86/platform/efi/efi_64.c @@ -223,7 +223,7 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages) if (efi_enabled(EFI_OLD_MEMMAP)) return 0; - efi_scratch.efi_pgt = (pgd_t *)__pa(efi_pgd); + efi_scratch.efi_pgt = (pgd_t *)__sme_pa(efi_pgd); pgd = efi_pgd; /* @@ -262,7 +262,8 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages) pfn = md->phys_addr >> PAGE_SHIFT; npages = md->num_pages; - if (kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, npages, _PAGE_RW)) { + if (kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, npages, + _PAGE_RW | _PAGE_ENC)) { pr_err("Failed to map 1:1 memory\n"); return 1; } @@ -272,6 +273,7 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages) if (!page) panic("Unable to allocate EFI runtime stack < 4GB\n"); + sme_set_mem_dec(page_address(page), PAGE_SIZE); efi_scratch.phys_stack = virt_to_phys(page_address(page)); efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */ @@ -279,7 +281,8 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages) text = __pa(_text); pfn = text >> PAGE_SHIFT; - if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, _PAGE_RW)) { + if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, + _PAGE_RW | _PAGE_ENC)) { pr_err("Failed to map kernel text 1:1\n"); return 1; } diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index ab50ada..dde4fb6b 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -13,6 +13,7 @@ #include #include #include +#include #define EFI_MIN_RESERVE 5120 @@ -265,6 +266,13 @@ void __init efi_free_boot_services(void) if (md->attribute & EFI_MEMORY_RUNTIME) continue; + /* + * Change the mapping to encrypted memory before freeing. + * This insures any future allocations of this mapped area + * are used encrypted. + */ + sme_set_mem_enc(__va(start), size); + free_bootmem_late(start, size); } @@ -292,7 +300,7 @@ int __init efi_reuse_config(u64 tables, int nr_tables) if (!efi_enabled(EFI_64BIT)) return 0; - data = early_memremap(efi_setup, sizeof(*data)); + data = sme_early_memremap(efi_setup, sizeof(*data)); if (!data) { ret = -ENOMEM; goto out; @@ -303,7 +311,7 @@ int __init efi_reuse_config(u64 tables, int nr_tables) sz = sizeof(efi_config_table_64_t); - p = tablep = early_memremap(tables, nr_tables * sz); + p = tablep = sme_early_memremap(tables, nr_tables * sz); if (!p) { pr_err("Could not map Configuration table!\n"); ret = -ENOMEM; diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c index 3a69ed5..25010c7 100644 --- a/drivers/firmware/efi/efi.c +++ b/drivers/firmware/efi/efi.c @@ -76,6 +76,16 @@ static int __init parse_efi_cmdline(char *str) } early_param("efi", parse_efi_cmdline); +/* + * If memory encryption is supported, then an override to this function + * will be provided. + */ +void __weak __init *efi_me_early_memremap(resource_size_t phys_addr, + unsigned long size) +{ + return early_memremap(phys_addr, size); +} + struct kobject *efi_kobj; /* @@ -289,9 +299,9 @@ int __init efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md) * So just always get our own virtual map on the CPU. * */ - md = early_memremap(p, sizeof (*md)); + md = efi_me_early_memremap(p, sizeof (*md)); if (!md) { - pr_err_once("early_memremap(%pa, %zu) failed.\n", + pr_err_once("efi_me_early_memremap(%pa, %zu) failed.\n", &p, sizeof (*md)); return -ENOMEM; } @@ -431,8 +441,8 @@ int __init efi_config_init(efi_config_table_type_t *arch_tables) /* * Let's see what config tables the firmware passed to us. */ - config_tables = early_memremap(efi.systab->tables, - efi.systab->nr_tables * sz); + config_tables = efi_me_early_memremap(efi.systab->tables, + efi.systab->nr_tables * sz); if (config_tables == NULL) { pr_err("Could not map Configuration table!\n"); return -ENOMEM; diff --git a/drivers/firmware/efi/esrt.c b/drivers/firmware/efi/esrt.c index 75feb3f..7a96bc6 100644 --- a/drivers/firmware/efi/esrt.c +++ b/drivers/firmware/efi/esrt.c @@ -273,10 +273,10 @@ void __init efi_esrt_init(void) return; } - va = early_memremap(efi.esrt, size); + va = efi_me_early_memremap(efi.esrt, size); if (!va) { - pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt, - size); + pr_err("efi_me_early_memremap(%p, %zu) failed.\n", + (void *)efi.esrt, size); return; } @@ -323,10 +323,10 @@ void __init efi_esrt_init(void) /* remap it with our (plausible) new pages */ early_memunmap(va, size); size += entries_size; - va = early_memremap(efi.esrt, size); + va = efi_me_early_memremap(efi.esrt, size); if (!va) { - pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt, - size); + pr_err("efi_me_early_memremap(%p, %zu) failed.\n", + (void *)efi.esrt, size); return; } diff --git a/include/linux/efi.h b/include/linux/efi.h index 1626474..557c774 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -957,6 +957,9 @@ extern void __init efi_fake_memmap(void); static inline void efi_fake_memmap(void) { } #endif +extern void __weak __init *efi_me_early_memremap(resource_size_t phys_addr, + unsigned long size); + /* Iterate through an efi_memory_map */ #define for_each_efi_memory_desc(m, md) \ for ((md) = (m)->map; \