Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753537AbcDZW5f (ORCPT ); Tue, 26 Apr 2016 18:57:35 -0400 Received: from mail-bn1on0081.outbound.protection.outlook.com ([157.56.110.81]:52271 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753478AbcDZW5a (ORCPT ); Tue, 26 Apr 2016 18:57:30 -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: [RFC PATCH v1 08/18] x86: Add support for early encryption/decryption of memory 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:18 -0500 Message-ID: <20160426225718.13567.92281.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: BY1PR14CA0039.namprd14.prod.outlook.com (10.161.91.49) To DM3PR1201MB1119.namprd12.prod.outlook.com (10.164.198.19) X-MS-Office365-Filtering-Correlation-Id: 7b439bb9-67b5-45e5-95f8-08d36e2621ae X-Microsoft-Exchange-Diagnostics: 1;DM3PR1201MB1119;2:/FQXs5uUAkx4aw3vF/xhlswOJ6yG/fEoCqvT+f5+8YsXJaUoiUyDfxhBH/4WNSr1jVqDEVh3Baqo6RE+9n1IagxB3o7Z1GfGsBucKXqwKsoR4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rwHkyxdUush4nUcKhvGhbik1avaRup8SshEjf602931NrHhzc3zUfxKCEkCGOAz+OnpYY7UEtt4NVtgCdEkxz+efW5jjhzbPvbVD3LMoulID3w2JmYqjguyd7xtorHDHl83ScZDrG20WK0WIjo3b0/8d6s59IWKQb+yOqF8v3+nysHs/9+F7Ugn/RrRXUr1w+F9TI1oCwnKl2oifJkbLkAD0rbBqVzsaBP1z9aPeXBDw1dzCxHGVhWr4M+eqTxdvHWh9SJ7FXDcjZl1e5RKmR1VqmfVBoIoeeWPBcilodW+R+7NQzdH6Blq+Tf5fsC4Oz8m5BU/TBB+0EIOy9Nxy3K7BH9VZMy1iIL0YY693uSV27K/i+p0zN+8EbNerai4G;4:9Nc2nxFCYIW4yyH7o40e796OkCh+BqSk4xD91dMk57QSfOXZ1IZDhiY8mIp9oM5eSWs1+5ZSVqVe5ygZsTIumyKfOmr+eeb0iB+cMXwGhgAH14Ho4TRLxSJ2lycx/KIVfe8gXCxQA5gePVY6/B58ui0kVL5DeKGRKVU25X/YwUrqeMkNufzQPZKy+tziKIIfS+0cNIGaBlay45l8Q6c4fXNhYJDPIOaW9FuvgPcbeHD4dfbPK62rdFVSxf/PgREJPbsmBF4PDSdOKNkuA7mwFzJKCb6pxghbXJYs4sOH8w7ulnTxDMlIo4xDA6YcVSeMXpSyc48FJ1N/OdiVNOwiex6MUCRSy4qryx7wg/7FkHi8czu1yhtkWvkQ4PhRnHcvy9GOwI/erZX/PIm1ONmLAlkQ6sAopPftXCOMxh80GlI= 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)(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?MTtETTNQUjEyMDFNQjExMTk7MjM6N1VXSi93ei9VRUh3OERkNTBUTG4xQjk4?= =?utf-8?B?c0NoMk41VUErWjlSVU1Sb1JNM2NJYXdCMEl0ZzVJMEJ0cjg1K084OVNQaWdh?= =?utf-8?B?TzB0RVVheXc5bVhKUmdwZytOQW84aWNyRGFmWGZjRHJxK0RwNHlpblpmVTRH?= =?utf-8?B?NmthUWUva3YyWlZDNjFYbXdFM1FyTjltVURla1JyVUxXMXlQUjQ1elVuRW5x?= =?utf-8?B?YzJHaHZTTzYrNld1YWlVOXdycU9nejg1RjUxaTl6Zm04bDgrSFBwWlhFS2Z2?= =?utf-8?B?ZlBUNlRWMlJpSzZ2OC9SQ0I5c2VzSEI2QnBLR1ZqWXF6ekZwbVR0ekVNSmd2?= =?utf-8?B?OWU3V25hWHZaR2JTNFdDOUMzcHhXZ3FBMEY5MWdIUkp6R2JsdnY0bWQxeDIr?= =?utf-8?B?UkZVSG1qd3hsLzUyR1Q1a0EwUzFNRVlBWWhNNXh2bFNVV2dnTnYvUEZTdXUr?= =?utf-8?B?VE5MSFNmcVZna09BVlY3aVptVHMzeVpxd2lxZFBaVXVOQkV4dWdkTktYTEFq?= =?utf-8?B?UE5hNkg0OE5keFNncWk1akdRRGIxbnFRTDN4aGVIbm9TaTNBZDY1MWNydEhY?= =?utf-8?B?QmJHOXpnUlVDMGljWTQ2M2t3Sm51Lyt4UUxuQ0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kSpd2aRnlBTVK/gXlYpIkQHwQrAvUll0hPU3/X12n4d3bOt86wlq3PftMoFw==;24:PdBOJUsL/ODCGuQ1UPEN5FVcwl/ixIWWMdTK3XLl+fO4PG4hEjmquoZUgn4X94D7h/UphXpi4w698i0YoIYIf1VJirYTDtUbXTE+sQmxlyI=;7:aBFjua969kMVuM+0slPiGixNLobERF5EjHnS419QeEPMAMloKaqFFv+tabpXL9i/APU+az8/7YOi+BB7fHTL43YMscyzu3lsC5JRjhS6pqQNLQZxK13KLm7SBzANNDFKRy638hqZdxQ7SAkSd/8Cxwml6MiIK6U4G5EOHXjjOHU=;20:qXImXxEZH2IOgdTvcOSKOeUS3cmtqfETavRt4P5JNORjOxiSp0h+5BFLdsj/0Zwi7+fIxVR4kSfM3bAw9VBrAxOOe6Abubn1nc0Sj2yWyCu5uicbV1Ul8wRziWwWLAvshmuR7ej5kBg9m9i5jv2gkGz/U2Dglmbjit2fKortd8CnddKidcw8NWQ9uPNyMyKz/4R9+GXRMggQfiQAGdMyAr3Xsb7dlumKooPh+WBpL74wIgUus2mQRUUFTtEelMQG X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Apr 2016 22:57:21.9148 (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: 3849 Lines: 147 This adds support to be able to either encrypt or decrypt data 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 un-encrypted 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 | 89 ++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/arch/x86/include/asm/mem_encrypt.h b/arch/x86/include/asm/mem_encrypt.h index 9f3e762..2785493 100644 --- a/arch/x86/include/asm/mem_encrypt.h +++ b/arch/x86/include/asm/mem_encrypt.h @@ -23,6 +23,11 @@ extern unsigned long sme_me_mask; u8 sme_get_me_loss(void); +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_init(void); #define __sme_pa(x) (__pa((x)) | sme_me_mask) @@ -39,6 +44,16 @@ static inline u8 sme_get_me_loss(void) return 0; } +static inline void __init sme_early_mem_enc(resource_size_t paddr, + unsigned long size) +{ +} + +static inline void __init sme_early_mem_dec(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 00eb705..5f19ede 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -14,6 +14,95 @@ #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); + +void __init sme_early_mem_enc(resource_size_t paddr, unsigned long size) +{ + 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(me_early_buffer), size); + + /* Create a mapping for non-encrypted write-protected memory */ + src = early_memremap_dec_wp(paddr, len); + + /* Create a mapping for encrypted memory */ + dst = early_memremap_enc(paddr, len); + + /* + * If a mapping can't be obtained to perform the encryption, + * then encrypted access to that area will end up causing + * a crash. + */ + BUG_ON(!src || !dst); + + memcpy(me_early_buffer, src, len); + memcpy(dst, me_early_buffer, len); + + early_memunmap(dst, len); + early_memunmap(src, len); + + paddr += len; + size -= len; + } +} + +void __init sme_early_mem_dec(resource_size_t paddr, unsigned long size) +{ + 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(me_early_buffer), size); + + /* Create a mapping for encrypted write-protected memory */ + src = early_memremap_enc_wp(paddr, len); + + /* Create a mapping for non-encrypted memory */ + dst = early_memremap_dec(paddr, len); + + /* + * If a mapping can't be obtained to perform the decryption, + * then un-encrypted access to that area will end up causing + * a crash. + */ + BUG_ON(!src || !dst); + + memcpy(me_early_buffer, src, len); + memcpy(dst, me_early_buffer, len); + + early_memunmap(dst, len); + early_memunmap(src, len); + + paddr += len; + size -= len; + } +} void __init sme_early_init(void) {