From patchwork Thu Mar 8 08:00:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 130975 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp6175510lja; Thu, 8 Mar 2018 00:02:51 -0800 (PST) X-Google-Smtp-Source: AG47ELs7teBbk5KarhJH8BtE/PbYgbqzQknMlxkfxIxv0jObIhMvZ+bUymUCRUhBONvL2j6YYR3j X-Received: by 10.99.120.205 with SMTP id t196mr19832591pgc.392.1520496171578; Thu, 08 Mar 2018 00:02:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520496171; cv=none; d=google.com; s=arc-20160816; b=0iwNKuOxmUJdWKhMPXQ0MkXP+rqdk8bmM2pGyOxwFJNgy/Pwzdb0tD18x+gMn/qsz3 RY6oLEkE4z4AWlX/mSaJVRC2Lt8lTEtbUOif/1YLzAtl5iZ/O6aAKgoYn4ivWc3qVKmG 63qUW+zrxTGB2Ux3sBuCLzS5n3WJWfCDlDtsaKcl0FptDVWrDY8TJFpfaDMCMpcOv9z3 dTCOnf29CsUtaqyQPaF1FEaik85TazPrM6zLO0QW4w5/JC6Q6kI7yWm3Zvz6NI2u40Cj z6kQuS94C6nArNf8L7hAUxp03fy10PyPSNaEs8gDH+l8MXGmKEDW1YlPTWVoxm7LIHmp EFpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=UDxa9kNHeQ0iRFqRaV94BS3QESCu7DCrVxxTOZHjGMU=; b=jjcNErV6FpbpYdN1y3fGnaZdquyJPIc5dDoypA2MUGo9Y9yYQinecP+PNYSungCM7L WosmXmsT7Zp0Y9AXdJz6/3yfqUIuALHUhXL1BwVjIpAiJJlrn7RboskBkYLZa3Gs6fBP 9zN5UMSNTP02MmbW3W3dXIF8Ykutprt7HT6n0WcKZYLcliWmB8bpu12NA4QZgkCQZLTb A9t7pKThyb4lyP4EfAs79Lki0TZ0VhnamGO11N/ZSjkVgBcb1b7m/NvCEz6gNyl+FFAd nEsFp7IJ7OpTyQzvXqtQZZYWZnnyCzn12fBHyhTODjBiZNsAvp4MjYU4QNNqBK59is2F 803Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Z5fr8d/m; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n22si12729781pgd.564.2018.03.08.00.02.51; Thu, 08 Mar 2018 00:02:51 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Z5fr8d/m; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965607AbeCHICU (ORCPT + 28 others); Thu, 8 Mar 2018 03:02:20 -0500 Received: from mail-wm0-f66.google.com ([74.125.82.66]:54759 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965106AbeCHICQ (ORCPT ); Thu, 8 Mar 2018 03:02:16 -0500 Received: by mail-wm0-f66.google.com with SMTP id z81so9457326wmb.4 for ; Thu, 08 Mar 2018 00:02:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=UDxa9kNHeQ0iRFqRaV94BS3QESCu7DCrVxxTOZHjGMU=; b=Z5fr8d/m8OoknU2058GS+iJI1hHw+YbnPmgtuKXugPzUS58aGZjswmpvyj8i7KoJ5K URGG2OOchPvjUIhS/at6sN2VkgAbf/vlDteYp5AcdneQabFG7B4RrmHhTX0Zx4K2drof jchQKkrTk9DXm3NYZ5ijRUqp2VfZUaiJd3FHk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=UDxa9kNHeQ0iRFqRaV94BS3QESCu7DCrVxxTOZHjGMU=; b=QwPYMdtgPWxxbLvgQ/EbeZrBsff+gUmFP7xbQcg2+045jb+volwlX7WLRue6JOh91F NLxp8/cYvl7d08VYXZ9euKzwqOo7ysKn832sS1sofNUkLoIlCctgCxjRC7BlKi9dYdtr bSb+RulZlGuLiYR5g5b1Z75gMehr1UsWDWVRh4x9LkH+Gw4cEI/AokMNjd+dQ1TT6KZ9 XrsM7v1XuEylhs2nvqKzv12zKbehIcbIkqjercjlhI05SpRn12p0uPVtAybbSp/85OTj 3uv0u9h1L+uzc5Xo3FVK2GUXAr+xDdVgrqVf7FcB1pG5CM7lRw33EBvJSNaB0azj5ATw 1L9A== X-Gm-Message-State: AElRT7GRJNTb4u0YTu8ORVkq4xfrvtxtadMngoBuWfFri1QL7cSq2cYR WZ0buv3AicN95ipVaQnzkWVSnw== X-Received: by 10.28.7.13 with SMTP id 13mr15564749wmh.9.1520496135354; Thu, 08 Mar 2018 00:02:15 -0800 (PST) Received: from localhost.localdomain ([160.89.73.46]) by smtp.gmail.com with ESMTPSA id e191sm11223361wmg.12.2018.03.08.00.02.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Mar 2018 00:02:14 -0800 (PST) From: Ard Biesheuvel To: linux-efi@vger.kernel.org, Ingo Molnar , Thomas Gleixner Cc: Sai Praneeth , Ard Biesheuvel , linux-kernel@vger.kernel.org, "Lee, Chun-Yi" , Borislav Petkov , Tony Luck , Andy Lutomirski , "Michael S . Tsirkin" , Bhupesh Sharma , Ricardo Neri , Ravi Shankar Subject: [PATCH 09/12] x86/efi: Use efi_switch_mm() rather than manually twiddling with %cr3 Date: Thu, 8 Mar 2018 08:00:17 +0000 Message-Id: <20180308080020.22828-10-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180308080020.22828-1-ard.biesheuvel@linaro.org> References: <20180308080020.22828-1-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sai Praneeth Use helper function (efi_switch_mm()) to switch to/from efi_mm. We switch to efi_mm before calling 1. efi_set_virtual_address_map() and 2. Invoking any efi_runtime_service() Likewise, we need to switch back to previous mm (mm context stolen by efi_mm) after the above calls return successfully. We can use efi_switch_mm() helper function only with x86_64 kernel and "efi=old_map" disabled because, x86_32 and efi=old_map doesn't use efi_pgd, rather they use swapper_pg_dir. Signed-off-by: Sai Praneeth Prakhya Cc: "Lee, Chun-Yi" Cc: Borislav Petkov Cc: Tony Luck Cc: Andy Lutomirski Cc: Michael S. Tsirkin Cc: Bhupesh Sharma Cc: Ricardo Neri Cc: Ravi Shankar Tested-by: Bhupesh Sharma Reviewed-by: Matt Fleming Signed-off-by: Ard Biesheuvel --- arch/x86/include/asm/efi.h | 25 +++++++++------------- arch/x86/platform/efi/efi_64.c | 40 +++++++++++++++++++----------------- arch/x86/platform/efi/efi_thunk_64.S | 2 +- 3 files changed, 32 insertions(+), 35 deletions(-) -- 2.15.1 diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h index 00f977ddd718..cda9940bed7a 100644 --- a/arch/x86/include/asm/efi.h +++ b/arch/x86/include/asm/efi.h @@ -62,14 +62,13 @@ extern asmlinkage u64 efi_call(void *fp, ...); #define efi_call_phys(f, args...) efi_call((f), args) /* - * Scratch space used for switching the pagetable in the EFI stub + * struct efi_scratch - Scratch space used while switching to/from efi_mm + * @phys_stack: stack used during EFI Mixed Mode + * @prev_mm: store/restore stolen mm_struct while switching to/from efi_mm */ struct efi_scratch { - u64 r15; - u64 prev_cr3; - pgd_t *efi_pgt; - bool use_pgd; - u64 phys_stack; + u64 phys_stack; + struct mm_struct *prev_mm; } __packed; #define arch_efi_call_virt_setup() \ @@ -78,11 +77,8 @@ struct efi_scratch { preempt_disable(); \ __kernel_fpu_begin(); \ \ - if (efi_scratch.use_pgd) { \ - efi_scratch.prev_cr3 = __read_cr3(); \ - write_cr3((unsigned long)efi_scratch.efi_pgt); \ - __flush_tlb_all(); \ - } \ + if (!efi_enabled(EFI_OLD_MEMMAP)) \ + efi_switch_mm(&efi_mm); \ }) #define arch_efi_call_virt(p, f, args...) \ @@ -90,10 +86,8 @@ struct efi_scratch { #define arch_efi_call_virt_teardown() \ ({ \ - if (efi_scratch.use_pgd) { \ - write_cr3(efi_scratch.prev_cr3); \ - __flush_tlb_all(); \ - } \ + if (!efi_enabled(EFI_OLD_MEMMAP)) \ + efi_switch_mm(efi_scratch.prev_mm); \ \ __kernel_fpu_end(); \ preempt_enable(); \ @@ -135,6 +129,7 @@ extern void __init efi_dump_pagetable(void); extern void __init efi_apply_memmap_quirks(void); extern int __init efi_reuse_config(u64 tables, int nr_tables); extern void efi_delete_dummy_variable(void); +extern void efi_switch_mm(struct mm_struct *mm); struct efi_setup_data { u64 fw_vendor; diff --git a/arch/x86/platform/efi/efi_64.c b/arch/x86/platform/efi/efi_64.c index 8881e601c32d..55c3f623ac49 100644 --- a/arch/x86/platform/efi/efi_64.c +++ b/arch/x86/platform/efi/efi_64.c @@ -81,9 +81,8 @@ pgd_t * __init efi_call_phys_prolog(void) int n_pgds, i, j; if (!efi_enabled(EFI_OLD_MEMMAP)) { - save_pgd = (pgd_t *)__read_cr3(); - write_cr3((unsigned long)efi_scratch.efi_pgt); - goto out; + efi_switch_mm(&efi_mm); + return NULL; } early_code_mapping_set_exec(1); @@ -155,8 +154,7 @@ void __init efi_call_phys_epilog(pgd_t *save_pgd) pud_t *pud; if (!efi_enabled(EFI_OLD_MEMMAP)) { - write_cr3((unsigned long)save_pgd); - __flush_tlb_all(); + efi_switch_mm(efi_scratch.prev_mm); return; } @@ -344,13 +342,6 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages) if (efi_enabled(EFI_OLD_MEMMAP)) return 0; - /* - * Since the PGD is encrypted, set the encryption mask so that when - * this value is loaded into cr3 the PGD will be decrypted during - * the pagetable walk. - */ - efi_scratch.efi_pgt = (pgd_t *)__sme_pa(pgd); - /* * It can happen that the physical address of new_memmap lands in memory * which is not mapped in the EFI page table. Therefore we need to go @@ -364,8 +355,6 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages) return 1; } - efi_scratch.use_pgd = true; - /* * Certain firmware versions are way too sentimential and still believe * they are exclusive and unquestionable owners of the first physical page, @@ -624,6 +613,22 @@ void __init efi_dump_pagetable(void) #endif } +/* + * Makes the calling thread switch to/from efi_mm context. Can be used + * for SetVirtualAddressMap() i.e. current->active_mm == init_mm as well + * as during efi runtime calls i.e current->active_mm == current_mm. + * We are not mm_dropping()/mm_grabbing() any mm, because we are not + * losing/creating any references. + */ +void efi_switch_mm(struct mm_struct *mm) +{ + task_lock(current); + efi_scratch.prev_mm = current->active_mm; + current->active_mm = mm; + switch_mm(efi_scratch.prev_mm, mm, NULL); + task_unlock(current); +} + #ifdef CONFIG_EFI_MIXED extern efi_status_t efi64_thunk(u32, ...); @@ -677,16 +682,13 @@ efi_status_t efi_thunk_set_virtual_address_map( efi_sync_low_kernel_mappings(); local_irq_save(flags); - efi_scratch.prev_cr3 = __read_cr3(); - write_cr3((unsigned long)efi_scratch.efi_pgt); - __flush_tlb_all(); + efi_switch_mm(&efi_mm); func = (u32)(unsigned long)phys_set_virtual_address_map; status = efi64_thunk(func, memory_map_size, descriptor_size, descriptor_version, virtual_map); - write_cr3(efi_scratch.prev_cr3); - __flush_tlb_all(); + efi_switch_mm(efi_scratch.prev_mm); local_irq_restore(flags); return status; diff --git a/arch/x86/platform/efi/efi_thunk_64.S b/arch/x86/platform/efi/efi_thunk_64.S index 189b218da87c..46c58b08739c 100644 --- a/arch/x86/platform/efi/efi_thunk_64.S +++ b/arch/x86/platform/efi/efi_thunk_64.S @@ -33,7 +33,7 @@ ENTRY(efi64_thunk) * Switch to 1:1 mapped 32-bit stack pointer. */ movq %rsp, efi_saved_sp(%rip) - movq efi_scratch+25(%rip), %rsp + movq efi_scratch(%rip), %rsp /* * Calculate the physical address of the kernel text.