Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752506AbdLGXeL (ORCPT ); Thu, 7 Dec 2017 18:34:11 -0500 Received: from mail-bl2nam02on0045.outbound.protection.outlook.com ([104.47.38.45]:26384 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752436AbdLGXeI (ORCPT ); Thu, 7 Dec 2017 18:34:08 -0500 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Thomas.Lendacky@amd.com; From: Tom Lendacky Subject: [PATCH v1 2/3] x86/mm: Prepare sme_encrypt_kernel() for PAGE aligned encryption To: x86@kernel.org Cc: Brijesh Singh , linux-kernel@vger.kernel.org, Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Thomas Gleixner Date: Thu, 07 Dec 2017 17:34:02 -0600 Message-ID: <20171207233402.29646.77306.stgit@tlendack-t1.amdoffice.net> In-Reply-To: <20171207233342.29646.12858.stgit@tlendack-t1.amdoffice.net> References: <20171207233342.29646.12858.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: DM5PR1101CA0011.namprd11.prod.outlook.com (10.174.246.21) To BN6PR12MB1140.namprd12.prod.outlook.com (10.168.226.142) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: b1a28a65-df81-4605-3100-08d53dcb016d X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(48565401081)(5600026)(4604075)(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(2017052603307);SRVR:BN6PR12MB1140; X-Microsoft-Exchange-Diagnostics: 1;BN6PR12MB1140;3:h0HzkuZX374ZrB/s9R76zZVlw+zCjjLa2GmC6atUFE+bM54C01lZ4/PuZWQBNowEePsa/M3PrPtqsORQo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qYATXi2you/XnanE/K5wv6CyQuxawZSm0r3qX1fZgf1HKu7Ah+K8BPs9LHrd2rj8NVONwXZBoo6twfY5lAnp4Zpju5ccKb0z4mEPub3Tyx5Uz6UNUVhY/XuDONW6DmZKSmkzFscdOa39QNoU+SEdos8sCopqMyzFUVzpZlL9Pp9tyYY+mMkC/7nHahg8oOEhG59nUX3+vZHhyZkj8JglBvUDqZ46FMkdS4XnG2mxiMQoJjK57TGeAx/35288BzfT3Q2Qzi6tr;4:/MmaL0GTkFkvjw9P8vdV4WYiaYP2nbFKN5m1Tw/J9EPDaeoX/hajSWNJ0u6389e7ZDW4cVD2mCrPphm3IkZGhFpXB6ORwskEAgL36fCD5+WAK+vPTQuMSU1NYU2mekK7N0x9BLS2He77n6SVwWaf6b2pbQjP5Po7x+wloUA486pXoBqPI4xqxKW1gvtsI/EL24TIf6jbd4Cfh8PMTFWe75mLJqJipbP1bNpkx9u95Gi5gVWBcK0DOekkfP3W9VcgHs4QILvkl7nJjDIV2SIXIA287aDpgXthaGPeR40hlyOy89Dt4PsLuvu55c38dpm3 X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(2401047)(8121501046)(5005006)(3231022)(10201501046)(3002001)(93006095)(93001095)(6055026)(6041248)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123555025)(20161123560025)(20161123564025)(20161123562025)(20161123558100)(6072148)(201708071742011);SRVR:BN6PR12MB1140;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:BN6PR12MB1140; X-Forefront-PRVS: 05143A8241 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(366004)(346002)(376002)(39860400002)(189003)(199004)(72206003)(54906003)(69596002)(68736007)(50466002)(23676004)(316002)(97736004)(2906002)(575784001)(86362001)(7696005)(6506006)(25786009)(52116002)(76176011)(4326008)(2486003)(83506002)(53936002)(105586002)(97746001)(3846002)(47776003)(305945005)(33646002)(9686003)(81156014)(81166006)(230700001)(8936002)(8676002)(55016002)(6116002)(1076002)(2351001)(2950100002)(5660300001)(16526018)(106356001)(53416004)(103116003)(58126008)(478600001)(6916009)(7736002)(2361001)(66066001);DIR:OUT;SFP:1101;SCL:1;SRVR:BN6PR12MB1140;H:tlendack-t1.amdoffice.net;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtCTjZQUjEyTUIxMTQwOzIzOlQvc2ZxQy9RVjA1SkMrZTVQMTFGWjhpcW16?= =?utf-8?B?aWVQQWtyeG1mWElPM2UxcTl0K3lEbnJzSXVyRFlrQzFPY21hU3Rhd3BHekQ1?= =?utf-8?B?VlA2TGNoc0hzTjJDNVF5SG5Sek95V1N3aGhiUk5ma2Ywcm5rNi93SE5BUGkw?= =?utf-8?B?WDFxMVFUNEl2RCtsN3hPSTBacXJRekxXK2ZKbjlyOHVhZ3hycERNUHlTUWRj?= =?utf-8?B?WndOaHZEejRnVFE2azl2ZVA3TlVaMW50UmFLTHBEa2dMVTdzRThhc3BKbXpo?= =?utf-8?B?bkw4OFZnOVFqZm1YR1d3V3lXelBKK24vSFJ6T3EyZERna0xYUUpId2h1RXZt?= =?utf-8?B?cWVpTGhjWXVRa3Y4QlMwQ3c5SDBsbmFPUVpvZVVyZmhXZ1lXSkZxSkV2WGpm?= =?utf-8?B?YjVkanRqaENGc253UTd3c05QOGdOSUs4NzlST0UxTHViQkUyd0JSdy9yUGVP?= =?utf-8?B?WXJ4OWEwYnY0OXpvb2l2Umo2U2cwQng3eFJ1ajRVci9mMDhLbE1oSWVFWTNT?= =?utf-8?B?bjBPWXBnbjhtZHhJZlVvdGZ6ai80SnBTeHRESWxtTm5FMDVJNktScFRuRTZB?= =?utf-8?B?OElONkpmWTlFUk9EOUJVNGs3VnJtbkcwR1lVam5NWUxld3p5ZzhCRmZEMnBr?= =?utf-8?B?cjJLaWs5Y0l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wirNcLpFDbWYeLdhOwL/jgu SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;BN6PR12MB1140;20:poeLUV9S9/Nfr2BX1MTw/Y6m9TKYnMUcrsUiLf6wO1CFkgBX9HBHXbahomoibL5HkzlOpOUYq3F1Oe8FhmA61vuv6HMwMFpIQ3QkJ2fqJqsyYAlBnU3hChcl3bznA6VQ+bmOFu08Ql3uvXWPoYSqsqt/wFSGrfSD2o7nQ9L3bAo1IJVacVtMRohmq4WB8adnk1zJ5+iolg1UgFp7/gO2I/7qZ1KalwZkC9LO9eKXSMy3qGKUbDwv3HEFLOeJKo66 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Dec 2017 23:34:04.7663 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b1a28a65-df81-4605-3100-08d53dcb016d X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR12MB1140 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 8549 Lines: 264 In preparation for encrypting more than just the kernel, the encryption support in sme_encrypt_kernel() needs to support 4KB page aligned encryption instead of just 2MB large page aligned encryption. Update the routines that populate the PGD to support non-2MB aligned addresses. This is done by creating PTE page tables for the start and end portion of the address range that fall outside of the 2MB alignment. This results in, at most, two extra pages to hold the PTE entries for each mapping of a range. Signed-off-by: Tom Lendacky --- arch/x86/mm/mem_encrypt.c | 115 ++++++++++++++++++++++++++++++++++------ arch/x86/mm/mem_encrypt_boot.S | 20 +++++-- 2 files changed, 114 insertions(+), 21 deletions(-) diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 2d8404b..1f0efb8 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -486,6 +486,7 @@ static void __init sme_clear_pgd(pgd_t *pgd_base, unsigned long start, #define PGD_FLAGS _KERNPG_TABLE_NOENC #define P4D_FLAGS _KERNPG_TABLE_NOENC #define PUD_FLAGS _KERNPG_TABLE_NOENC +#define PMD_FLAGS _KERNPG_TABLE_NOENC #define PMD_FLAGS_LARGE (__PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL) @@ -494,8 +495,14 @@ static void __init sme_clear_pgd(pgd_t *pgd_base, unsigned long start, (_PAGE_PAT | _PAGE_PWT)) #define PMD_FLAGS_ENC (PMD_FLAGS_LARGE | _PAGE_ENC) -static void __init sme_populate_pgd(pgd_t *pgd_base, unsigned long vaddr, - unsigned long paddr, pmdval_t pmd_flags) +#define PTE_FLAGS (__PAGE_KERNEL_EXEC & ~_PAGE_GLOBAL) + +#define PTE_FLAGS_DEC PTE_FLAGS +#define PTE_FLAGS_DEC_WP ((PTE_FLAGS_DEC & ~_PAGE_CACHE_MASK) | \ + (_PAGE_PAT | _PAGE_PWT)) +#define PTE_FLAGS_ENC (PTE_FLAGS | _PAGE_ENC) + +static pmd_t __init *sme_prepare_pgd(pgd_t *pgd_base, unsigned long vaddr) { pgd_t *pgd_p; p4d_t *p4d_p; @@ -546,7 +553,7 @@ static void __init sme_populate_pgd(pgd_t *pgd_base, unsigned long vaddr, pud_p += pud_index(vaddr); if (native_pud_val(*pud_p)) { if (native_pud_val(*pud_p) & _PAGE_PSE) - return; + return NULL; pmd_p = (pmd_t *)(native_pud_val(*pud_p) & ~PTE_FLAGS_MASK); } else { @@ -560,21 +567,90 @@ static void __init sme_populate_pgd(pgd_t *pgd_base, unsigned long vaddr, native_set_pud(pud_p, pud); } + return pmd_p; +} + +static void __init sme_populate_pgd_large(pgd_t *pgd, unsigned long vaddr, + unsigned long paddr, + pmdval_t pmd_flags) +{ + pmd_t *pmd_p; + + pmd_p = sme_prepare_pgd(pgd, vaddr); + if (!pmd_p) + return; + pmd_p += pmd_index(vaddr); if (!native_pmd_val(*pmd_p) || !(native_pmd_val(*pmd_p) & _PAGE_PSE)) native_set_pmd(pmd_p, native_make_pmd(paddr | pmd_flags)); } +static void __init sme_populate_pgd(pgd_t *pgd, unsigned long vaddr, + unsigned long paddr, + pteval_t pte_flags) +{ + pmd_t *pmd_p; + pte_t *pte_p; + + pmd_p = sme_prepare_pgd(pgd, vaddr); + if (!pmd_p) + return; + + pmd_p += pmd_index(vaddr); + if (native_pmd_val(*pmd_p)) { + if (native_pmd_val(*pmd_p) & _PAGE_PSE) + return; + + pte_p = (pte_t *)(native_pmd_val(*pmd_p) & ~PTE_FLAGS_MASK); + } else { + pmd_t pmd; + + pte_p = pgtable_area; + memset(pte_p, 0, sizeof(*pte_p) * PTRS_PER_PTE); + pgtable_area += sizeof(*pte_p) * PTRS_PER_PTE; + + pmd = native_make_pmd((pteval_t)pte_p + PMD_FLAGS); + native_set_pmd(pmd_p, pmd); + } + + pte_p += pte_index(vaddr); + if (!native_pte_val(*pte_p)) + native_set_pte(pte_p, native_make_pte(paddr | pte_flags)); +} + static void __init __sme_map_range(pgd_t *pgd, unsigned long vaddr, unsigned long vaddr_end, - unsigned long paddr, pmdval_t pmd_flags) + unsigned long paddr, + pmdval_t pmd_flags, pteval_t pte_flags) { - while (vaddr < vaddr_end) { - sme_populate_pgd(pgd, vaddr, paddr, pmd_flags); + if (vaddr & ~PMD_PAGE_MASK) { + /* Start is not 2MB aligned, create PTE entries */ + unsigned long pmd_start = ALIGN(vaddr, PMD_PAGE_SIZE); + + while (vaddr < pmd_start) { + sme_populate_pgd(pgd, vaddr, paddr, pte_flags); + + vaddr += PAGE_SIZE; + paddr += PAGE_SIZE; + } + } + + while (vaddr < (vaddr_end & PMD_PAGE_MASK)) { + sme_populate_pgd_large(pgd, vaddr, paddr, pmd_flags); vaddr += PMD_PAGE_SIZE; paddr += PMD_PAGE_SIZE; } + + if (vaddr_end & ~PMD_PAGE_MASK) { + /* End is not 2MB aligned, create PTE entries */ + while (vaddr < vaddr_end) { + sme_populate_pgd(pgd, vaddr, paddr, pte_flags); + + vaddr += PAGE_SIZE; + paddr += PAGE_SIZE; + } + } } static void __init sme_map_range_encrypted(pgd_t *pgd, @@ -582,7 +658,8 @@ static void __init sme_map_range_encrypted(pgd_t *pgd, unsigned long vaddr_end, unsigned long paddr) { - __sme_map_range(pgd, vaddr, vaddr_end, paddr, PMD_FLAGS_ENC); + __sme_map_range(pgd, vaddr, vaddr_end, paddr, + PMD_FLAGS_ENC, PTE_FLAGS_ENC); } static void __init sme_map_range_decrypted(pgd_t *pgd, @@ -590,7 +667,8 @@ static void __init sme_map_range_decrypted(pgd_t *pgd, unsigned long vaddr_end, unsigned long paddr) { - __sme_map_range(pgd, vaddr, vaddr_end, paddr, PMD_FLAGS_DEC); + __sme_map_range(pgd, vaddr, vaddr_end, paddr, + PMD_FLAGS_DEC, PTE_FLAGS_DEC); } static void __init sme_map_range_decrypted_wp(pgd_t *pgd, @@ -598,19 +676,20 @@ static void __init sme_map_range_decrypted_wp(pgd_t *pgd, unsigned long vaddr_end, unsigned long paddr) { - __sme_map_range(pgd, vaddr, vaddr_end, paddr, PMD_FLAGS_DEC_WP); + __sme_map_range(pgd, vaddr, vaddr_end, paddr, + PMD_FLAGS_DEC_WP, PTE_FLAGS_DEC_WP); } static unsigned long __init sme_pgtable_calc(unsigned long len) { - unsigned long p4d_size, pud_size, pmd_size; + unsigned long p4d_size, pud_size, pmd_size, pte_size; unsigned long total; /* * Perform a relatively simplistic calculation of the pagetable - * entries that are needed. That mappings will be covered by 2MB - * PMD entries so we can conservatively calculate the required - * number of P4D, PUD and PMD structures needed to perform the + * entries that are needed. That mappings will be covered mostly + * by 2MB PMD entries so we can conservatively calculate the required + * number of P4D, PUD, PMD and PTE structures needed to perform the * mappings. Incrementing the count for each covers the case where * the addresses cross entries. */ @@ -626,8 +705,9 @@ static unsigned long __init sme_pgtable_calc(unsigned long len) } pmd_size = (ALIGN(len, PUD_SIZE) / PUD_SIZE) + 1; pmd_size *= sizeof(pmd_t) * PTRS_PER_PMD; + pte_size = 2 * sizeof(pte_t) * PTRS_PER_PTE; - total = p4d_size + pud_size + pmd_size; + total = p4d_size + pud_size + pmd_size + pte_size; /* * Now calculate the added pagetable structures needed to populate @@ -710,10 +790,13 @@ void __init sme_encrypt_kernel(void) /* * The total workarea includes the executable encryption area and - * the pagetable area. + * the pagetable area. The start of the workarea is already 2MB + * aligned, align the end of the workarea on a 2MB boundary so that + * we don't try to create/allocate PTE entries from the workarea + * before it is mapped. */ workarea_len = execute_len + pgtable_area_len; - workarea_end = workarea_start + workarea_len; + workarea_end = ALIGN(workarea_start + workarea_len, PMD_PAGE_SIZE); /* * Set the address to the start of where newly created pagetable diff --git a/arch/x86/mm/mem_encrypt_boot.S b/arch/x86/mm/mem_encrypt_boot.S index 730e6d5..20cca86 100644 --- a/arch/x86/mm/mem_encrypt_boot.S +++ b/arch/x86/mm/mem_encrypt_boot.S @@ -120,23 +120,33 @@ ENTRY(__enc_copy) wbinvd /* Invalidate any cache entries */ + push %r12 + /* Copy/encrypt 2MB at a time */ + movq $PMD_PAGE_SIZE, %r12 1: + cmpq %r12, %r9 + jnb 2f + movq %r9, %r12 + +2: movq %r11, %rsi /* Source - decrypted kernel */ movq %r8, %rdi /* Dest - intermediate copy buffer */ - movq $PMD_PAGE_SIZE, %rcx /* 2MB length */ + movq %r12, %rcx rep movsb movq %r8, %rsi /* Source - intermediate copy buffer */ movq %r10, %rdi /* Dest - encrypted kernel */ - movq $PMD_PAGE_SIZE, %rcx /* 2MB length */ + movq %r12, %rcx rep movsb - addq $PMD_PAGE_SIZE, %r11 - addq $PMD_PAGE_SIZE, %r10 - subq $PMD_PAGE_SIZE, %r9 /* Kernel length decrement */ + addq %r12, %r11 + addq %r12, %r10 + subq %r12, %r9 /* Kernel length decrement */ jnz 1b /* Kernel length not zero? */ + pop %r12 + /* Restore PAT register */ push %rdx /* Save original PAT value */ movl $MSR_IA32_CR_PAT, %ecx