Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752466AbdFGTPn (ORCPT ); Wed, 7 Jun 2017 15:15:43 -0400 Received: from mail-cys01nam02on0083.outbound.protection.outlook.com ([104.47.37.83]:17312 "EHLO NAM02-CY1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752445AbdFGTPg (ORCPT ); Wed, 7 Jun 2017 15:15:36 -0400 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: [PATCH v6 13/34] x86/mm: Add support for early encrypt/decrypt of memory To: linux-arch@vger.kernel.org, linux-efi@vger.kernel.org, kvm@vger.kernel.org, linux-doc@vger.kernel.org, x86@kernel.org, kexec@lists.infradead.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, iommu@lists.linux-foundation.org Cc: Rik van Riel , Radim =?utf-8?b?S3LEjW3DocWZ?= , Toshimitsu Kani , Arnd Bergmann , Jonathan Corbet , Matt Fleming , "Michael S. Tsirkin" , Joerg Roedel , Konrad Rzeszutek Wilk , Paolo Bonzini , Larry Woodman , Brijesh Singh , Ingo Molnar , Borislav Petkov , Andy Lutomirski , "H. Peter Anvin" , Andrey Ryabinin , Alexander Potapenko , Dave Young , Thomas Gleixner , Dmitry Vyukov Date: Wed, 07 Jun 2017 14:15:27 -0500 Message-ID: <20170607191527.28645.84593.stgit@tlendack-t1.amdoffice.net> In-Reply-To: <20170607191309.28645.15241.stgit@tlendack-t1.amdoffice.net> References: <20170607191309.28645.15241.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: BN6PR03CA0068.namprd03.prod.outlook.com (10.173.137.30) To DM5PR12MB1146.namprd12.prod.outlook.com (10.168.236.141) X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM5PR12MB1146: X-MS-Office365-Filtering-Correlation-Id: b9c79d9d-b040-4c8e-093c-08d4add9925f X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(48565401081)(201703131423075)(201703031133081);SRVR:DM5PR12MB1146; X-Microsoft-Exchange-Diagnostics: 1;DM5PR12MB1146;3:746ADObVfr+F0Kt1tjFT02NuYMt4DaqNFQ21ZEUteffL5yD1Zwv/SHKrQ+d9jdh5yX2eN6DyE8ubgAuv1tKWgjTKb2+THjPX/AKIgY2MCKHvVs98h0m+0SQRAXkewN55PlaECsZrekPbvsQx+sDypAeaB7uh+IhBxyXADNIqvzeewWTmNqwosWYzwVwFcFDXqDc32fd0xawMzpTP1DtO+ViQXYgp9HMZ95qKWcCCa6UOHS9xOi6Ql369lZbYd3q+PrccsHlL8LAt0I/8aHQRgh+OoF2KJxiqNpuDKmUdPgpqWNz6yH2xubix33U0RHZPAdhiJm7HOvCtvEttEx7X4MpLW/s2eDYNN9KCSZd6f10=;25:cdfUyuuJ4Y4BuVqL2rj3aBZMehjC3Pesvvc+YMrUF4NEyOShLrkR1Kyb8Vxc4TD3WXzGB8/CIv9Y5jpWj3W9nwDg3hIdZn3pjupfsBHJVRoIjogXso8wMHWlhsdyXh2VnqEUxA1BTPkmyJ+pnvqcCTaIUfm6uVcQR6qvKINP7iIM//rjbsefceB2G5/BqUWfT6swpxdZMc7bfZD1nq9r12oYNrjr5/LMRTBHzcPpX+w0ZbEEjhsF2Y1RMt5YNmQOeGgainqnc66mMd3cg3C2W2BNlmeVviXM0avrsHaLBIxwT9rwye7B9kTaXGoHqWWrg8a2A1/N02svXnpSrgkZQFPiLcbHpfEHEy97vmRyHL4oZYzAFI4FfPhJKwsgLgusK0OMJDSCl75O0Oq1p199Nf1RDgH7up8evHS6n9G1kw3CQStuEldtmOIXs2f6F1skBB6kbcoTCiyvR0UpmAn4kAekCdqtfKR2PGV+HOGLlSc= X-Microsoft-Exchange-Diagnostics: 1;DM5PR12MB1146;31:CrLPqclxjHOjCtVSG378qZ4/FtsqPa5UJ2O9P1oePf2rhsJTaQQgHXPrur6N+Wy0d/4j9Li7na5V57bTEdLqF+uPf5w6u68FHsyXGcbYC5Hyk2QFja7woHEuXB9kvjN3AtG7YhpIKJFWTtElxvrIAxDZerhifV3+F34OjCTJeGhPRiZmOK7ySHM4VJhSUkNzDOpYlgt6b7gmDCghhuO69gruHyhCzacvfGOGkcCbRC8=;20:yBKwYXjX8lPlTza08u3NE7dc7sNya0jTPphVvPZ1XV3dtW+Eo+DQFBVFgJ7kByqrL+HGRo0o+elirxBAmTw2CcnIZ8mXEeuy8EzO6tN8Wak6xHWa/bJGv53L9yb4tShx3MF2ajcnsWs7Wn03mKFJ+AvmI6v75CAxMv25KSAwLA+4EhJj2OCNgqHoJ8EX5e9V8Vy0C4Gw94li3uavrZCHHJUMMqSWSrwFt7XY//yLfoD6c5nOxLPRA9/f75u5D6EKJ015NLggV1Q7GMT5ZBxKo0UneM/8JojmZv+q/4Q8zBSgmYLeAnMpwXd868KrqDkbI8tblyXTUSOSM4nV7HgzSEdchGPpbaWNq5HG3zhJUT1Ln483P//JNMw//ImU1fJ8jmtVrLSk7GJSxBheGKru3rEm1rlo5oAwHqFgR8R4D4rFhto65PMjmIo/ZVLnspPTRQ5PhjsOuQZKSBOdHSz7MIUlj5aORv8o0dceWNaozsiimNLbSymiVkPL7W4+5+Nz X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(8121501046)(5005006)(100000703101)(100105400095)(10201501046)(3002001)(93006095)(93001095)(6055026)(6041248)(20161123562025)(20161123560025)(20161123564025)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123558100)(6072148)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:DM5PR12MB1146;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:DM5PR12MB1146; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtETTVQUjEyTUIxMTQ2OzQ6dkdkV3ZsRkRjT09oTkdvcjFINTFRVXJFQVhG?= =?utf-8?B?ZnJ2cjY0VHgwRzhQRktyaHhPSGxXOXp0WGRxQ2JxcGZ3LzJGVU93ZEJNM2Zs?= =?utf-8?B?UC9iWnhXNnIyRktZZWdaK2VYcElVWXN4VmE5eGhFaG5SK0dCV3E1UHNqOWtt?= =?utf-8?B?WlV2Rnc2MEhoUDJvaGdjditXN1hHTXNoTzE0VzlLcTBNZHdtYnc4ZXUvS1g1?= =?utf-8?B?S2ZEZGsyZFZKKzBPT3JLK0ZQdno3Y1REeU9Bd3dSb2xYeVdKdmk3dGRMT3lx?= =?utf-8?B?cU1pMU80YktMNW94OGZHdnRza3JTRThYTkJRTzA0V1dGOTNFTmczcEU1eSs2?= =?utf-8?B?bDJ0VWRIOC9NTm5kVU1mNTU4Q1FzYUxvV0lzZmZuRlBGenM2VlJtL2wreS9S?= =?utf-8?B?Q0R2VUFIMjkyT0lQRFpuWllwTldYVVdJanZWSExHTFB5UHB5NmxNMmtkdzZn?= =?utf-8?B?NDJGTG1lc1I4RGZLWjJyd0kxQnpyVW9FaXhIWnFtZnZValBTN1dOU01XZ3hK?= =?utf-8?B?K2s1RzRVd1FtTVR0NmhueG4yM3ZIUzEwYktoWjl5clJzYzg0K3Z1M2pmeDhV?= =?utf-8?B?NXZoS3QvNlFpSHpqU0swWGljSnpmVnhxakR6b21tNkpJTGx5UXdZU0R0Q0xj?= =?utf-8?B?eW9kSHgrelJnbVZLaXkyaE56eXNXMjQvSlF1YS9pSWc1NCtiQlN6WU0reGds?= =?utf-8?B?WnRKVnR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= =?utf-8?Q?4ZimWRHNN9uYeVLJONG?= X-Forefront-PRVS: 03319F6FEF X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(39410400002)(39860400002)(39850400002)(39400400002)(39840400002)(39450400003)(47776003)(7406005)(7416002)(9686003)(50986999)(76176999)(50466002)(5660300001)(54356999)(305945005)(81166006)(7736002)(97746001)(86362001)(53416004)(23676002)(54906002)(42186005)(8676002)(55016002)(53936002)(33646002)(38730400002)(3846002)(6116002)(478600001)(103116003)(1076002)(110136004)(230700001)(6506006)(72206003)(66066001)(4001350100001)(25786009)(189998001)(4326008)(83506001)(6666003)(2950100002)(2906002)(921003)(1121003);DIR:OUT;SFP:1101;SCL:1;SRVR:DM5PR12MB1146;H:tlendack-t1.amdoffice.net;FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtETTVQUjEyTUIxMTQ2OzIzOmRmK3BicTJZYjcxRzRCZ1Ywc3IwWE1aRkhK?= =?utf-8?B?V3ExTUFUbTZjY3pubHh5VWhPbzdNaVRYTmF0T3ZtalZBWmlEWXhkeGtMclNX?= =?utf-8?B?OHZlb0k1U1hsek42cU5LRlhHcThseHZVNlNQakFsaGNJM0pzUWtCYUdBdmFa?= =?utf-8?B?RDVVMko3MFVTSnl6cEtXTXNqTkJWQktteVFpOXpGcXV0QmhTUGxiVjIzcHJi?= =?utf-8?B?VXZDSkxCVzB5T29zaVdDa1AxVEgzbmdaTzI0UzYwdGt5Qkc1WUR1VXpWUUVB?= =?utf-8?B?S0tUVXhQdjMxRmNjT0xUL0FMb21tOEhNT0VXdURoZVhKcFhDb1FDZ3o0b01M?= =?utf-8?B?dG54dmVxbm9MVUE0aDQ2QjBRUVhZdkUyb2FrQ2RsL0NuY3VkeFNNSk92Kzds?= =?utf-8?B?eE5UelpaZk5lNlFCSXFhanpmSC8wSzYxL3loeWRpWlNacjBVdUlNdGxINWc2?= =?utf-8?B?ZTE4R2FZNVl3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6add54l22EFDviYHyFbRUsEdGubnfuCwLoRGb7IlmYZ1t1HUjbI5fJVH1/m47ojA6MfIte1FQ34ghpodwBpTPlTdWKxrM+peb6oETGDiXfem1Fdpg8kJ2jllzO0KY/zFib8L3o7WTtMqJE1jCOs9j3aOub5uRgInp X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jun 2017 19:15:31.3649 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1146 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4071 Lines: 139 Add support to be able to either encrypt or decrypt data in place during the early stages of booting the kernel. This does not change the memory encryption attribute - it is used for ensuring that data present in either an encrypted or decrypted memory area is in the proper state (for example the initrd will have been loaded by the boot loader and will not be encrypted, but the memory that it resides in is marked as encrypted). Signed-off-by: Tom Lendacky --- arch/x86/include/asm/mem_encrypt.h | 15 +++++++ arch/x86/mm/mem_encrypt.c | 76 ++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/arch/x86/include/asm/mem_encrypt.h b/arch/x86/include/asm/mem_encrypt.h index f1c4c29..7c395cf 100644 --- a/arch/x86/include/asm/mem_encrypt.h +++ b/arch/x86/include/asm/mem_encrypt.h @@ -21,12 +21,27 @@ extern unsigned long sme_me_mask; +void __init sme_early_encrypt(resource_size_t paddr, + unsigned long size); +void __init sme_early_decrypt(resource_size_t paddr, + unsigned long size); + void __init sme_early_init(void); #else /* !CONFIG_AMD_MEM_ENCRYPT */ #define sme_me_mask 0UL +static inline void __init sme_early_encrypt(resource_size_t paddr, + unsigned long size) +{ +} + +static inline void __init sme_early_decrypt(resource_size_t paddr, + unsigned long size) +{ +} + static inline void __init sme_early_init(void) { } diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 8ca93e5..18c0887 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -17,6 +17,9 @@ #include +#include +#include + /* * Since SME related variables are set early in the boot process they must * reside in the .data section so as not to be zeroed out when the .bss @@ -25,6 +28,79 @@ unsigned long sme_me_mask __section(.data) = 0; EXPORT_SYMBOL_GPL(sme_me_mask); +/* Buffer used for early in-place encryption by BSP, no locking needed */ +static char sme_early_buffer[PAGE_SIZE] __aligned(PAGE_SIZE); + +/* + * This routine does not change the underlying encryption setting of the + * page(s) that map this memory. It assumes that eventually the memory is + * meant to be accessed as either encrypted or decrypted but the contents + * are currently not in the desired state. + * + * This routine follows the steps outlined in the AMD64 Architecture + * Programmer's Manual Volume 2, Section 7.10.8 Encrypt-in-Place. + */ +static void __init __sme_early_enc_dec(resource_size_t paddr, + unsigned long size, bool enc) +{ + void *src, *dst; + size_t len; + + if (!sme_me_mask) + return; + + local_flush_tlb(); + wbinvd(); + + /* + * There are limited number of early mapping slots, so map (at most) + * one page at time. + */ + while (size) { + len = min_t(size_t, sizeof(sme_early_buffer), size); + + /* + * Create mappings for the current and desired format of + * the memory. Use a write-protected mapping for the source. + */ + src = enc ? early_memremap_decrypted_wp(paddr, len) : + early_memremap_encrypted_wp(paddr, len); + + dst = enc ? early_memremap_encrypted(paddr, len) : + early_memremap_decrypted(paddr, len); + + /* + * If a mapping can't be obtained to perform the operation, + * then eventual access of that area in the desired mode + * will cause a crash. + */ + BUG_ON(!src || !dst); + + /* + * Use a temporary buffer, of cache-line multiple size, to + * avoid data corruption as documented in the APM. + */ + memcpy(sme_early_buffer, src, len); + memcpy(dst, sme_early_buffer, len); + + early_memunmap(dst, len); + early_memunmap(src, len); + + paddr += len; + size -= len; + } +} + +void __init sme_early_encrypt(resource_size_t paddr, unsigned long size) +{ + __sme_early_enc_dec(paddr, size, true); +} + +void __init sme_early_decrypt(resource_size_t paddr, unsigned long size) +{ + __sme_early_enc_dec(paddr, size, false); +} + void __init sme_early_init(void) { unsigned int i;