Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp1367476pxb; Fri, 1 Oct 2021 09:10:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy+Cb5tQZ4OQttSOynkH1HpZDZMg1VWkBFMfmB/+b+JAq0LVSQjVPBl0BeUCXGIjqaBfSzX X-Received: by 2002:a17:907:20f8:: with SMTP id rh24mr6952490ejb.526.1633104608585; Fri, 01 Oct 2021 09:10:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633104608; cv=none; d=google.com; s=arc-20160816; b=fom3IlgcxwrHfbpVQt0/IGUUdhk7T5GAGpXhC5XnpGgg9O8Qyarpn8ZaoRuh3ElpJY s9Z6IOaSyS8BbSsypcVbiT2UPbQfq/gzj4wC6fDFPGO43W2/ZFjMlTQX2trvoQ5CV/Rj MmavUv3pJDUyR/aVvq/DPLYuvIteEeMtbJN0O1LTTa1prtAH6KTxz63x3m3RCwQWKKbQ Il6ftNXYeL0H3bbOcHxIVfM+jvU8N/ncZMb3YL1CR+disZnRFg39JpihjaBm7Z1aZPgh cJOevI85lvRJLnkzg+DUr3tpScDj2vlT8nDl/+omOgYhdX9itoP2iPTKftq/RTBTIadS aw4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=2CaYdDnhcfuFI4pD64iBsyzEjs6JmVJDI9aW6eIae2A=; b=k6nG6DLJsRDWGpWqHqg5Jc5S9LAdHVeJQxf12IxrcCwmVepXJ0nlRTD+fuYtqFR/fY CIYq3c7UtQRjZXa8etYq/A36zG0eoj5XQOunXSPuZjSdaZpkvsXK/1ENs1eXQ7bEgsJm XICBWHum7WrVEZCHIexVPgm9qRCMRzIhKGSuVIkdwntQAsE2elrV7hkXCrGUomUzXLAT CcgTC2kPNvPPPc0NsArdGqOiwyH73oPAuPAGwT4O2UkhLUrziTYTu2G/HuZbL3i/yuJx 4RlgQz2OnnwZgp8sKc/klkDqwvN4oNyuXZU68bRLiRw/LgTjhFBzD6RHq7DYUs2SeU4W k0xw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a17si8329304eju.585.2021.10.01.09.09.42; Fri, 01 Oct 2021 09:10:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355098AbhJAPu3 (ORCPT + 99 others); Fri, 1 Oct 2021 11:50:29 -0400 Received: from 8bytes.org ([81.169.241.247]:43004 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353712AbhJAPuN (ORCPT ); Fri, 1 Oct 2021 11:50:13 -0400 Received: from cap.home.8bytes.org (p4ff2b5b0.dip0.t-ipconnect.de [79.242.181.176]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by theia.8bytes.org (Postfix) with ESMTPSA id A8A804D4; Fri, 1 Oct 2021 17:48:27 +0200 (CEST) From: Joerg Roedel To: x86@kernel.org Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , hpa@zytor.com, Dave Hansen , Andy Lutomirski , Peter Zijlstra , Joerg Roedel , Mike Rapoport , Andrew Morton , Brijesh Singh , linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v3 4/4] x86/64/mm: Map all kernel memory into trampoline_pgd Date: Fri, 1 Oct 2021 17:48:17 +0200 Message-Id: <20211001154817.29225-5-joro@8bytes.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211001154817.29225-1-joro@8bytes.org> References: <20211001154817.29225-1-joro@8bytes.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel The trampoline_pgd only maps the 0xfffffff000000000-0xffffffffffffffff range of kernel memory (with 4-level paging). This range contains the kernels text+data+bss mappings and the module mapping space, but not the direct mapping and the vmalloc area. This is enough to get an application processors out of real-mode, but for code that switches back to real-mode the trampoline_pgd is missing important parts of the address space. For example, consider this code from arch/x86/kernel/reboot.c, function machine_real_restart() for a 64-bit kernel: #ifdef CONFIG_X86_32 load_cr3(initial_page_table); #else write_cr3(real_mode_header->trampoline_pgd); /* Exiting long mode will fail if CR4.PCIDE is set. */ if (boot_cpu_has(X86_FEATURE_PCID)) cr4_clear_bits(X86_CR4_PCIDE); #endif /* Jump to the identity-mapped low memory code */ #ifdef CONFIG_X86_32 asm volatile("jmpl *%0" : : "rm" (real_mode_header->machine_real_restart_asm), "a" (type)); #else asm volatile("ljmpl *%0" : : "m" (real_mode_header->machine_real_restart_asm), "D" (type)); #endif The code switches to the trampoline_pgd, which unmaps the direct mapping and also the kernel stack. The call to cr4_clear_bits() will find no stack and crash the machine. The real_mode_header pointer below points into the direct mapping, and dereferencing it also causes a crash. The reason this does not crash always is only that kernel mappings are global and the CR3 switch does not flush those mappings. But if theses mappings are not in the TLB already, the above code will crash before it can jump to the real-mode stub. Extend the trampoline_pgd to contain all kernel mappings to prevent these crashes and to make code which runs on this page-table more robust. Cc: stable@vger.kernel.org Signed-off-by: Joerg Roedel --- arch/x86/realmode/init.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/arch/x86/realmode/init.c b/arch/x86/realmode/init.c index b9802b18f504..77617cd624fe 100644 --- a/arch/x86/realmode/init.c +++ b/arch/x86/realmode/init.c @@ -95,6 +95,7 @@ static void __init setup_real_mode(void) #ifdef CONFIG_X86_64 u64 *trampoline_pgd; u64 efer; + int i; #endif base = (unsigned char *)real_mode_header; @@ -151,8 +152,17 @@ static void __init setup_real_mode(void) trampoline_header->flags = 0; trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd); + + /* Map the real mode stub as virtual == physical */ trampoline_pgd[0] = trampoline_pgd_entry.pgd; - trampoline_pgd[511] = init_top_pgt[511].pgd; + + /* + * Include the entirety of the kernel mapping into the trampoline + * PGD. This way, all mappings present in the normal kernel page + * tables are usable while running on trampoline_pgd. + */ + for (i = pgd_index(__PAGE_OFFSET); i < PTRS_PER_PGD; i++) + trampoline_pgd[i] = init_top_pgt[i].pgd; #endif sme_sev_setup_real_mode(trampoline_header); -- 2.33.0