Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1704269ybh; Tue, 14 Jul 2020 05:17:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzp2mQK/SA2ITdqzccJUmAvcQZtZQCzV3SEA9DX4y9XfYiNuqDJ5pXfxXO5Yq6KK9RJO4QI X-Received: by 2002:aa7:da8a:: with SMTP id q10mr4236812eds.139.1594729052573; Tue, 14 Jul 2020 05:17:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594729052; cv=none; d=google.com; s=arc-20160816; b=TL7wtzacWmrOdqx8CwqTelNj3aL1KqW0mHF16VFDQ39Juj6TXpawZRcgIHprM4Rmhp 3fOj5mOwSGByrncl64p9m5N9XwdhexYA4q0P/iMNwh+CBIkdCO0rio9wVmBz1n7+FONS twu+l8vONxDnCW8+6wwne0sHFc7R0N7TN4zi/VzJIiJFfRcb7wjVr6B2WuGVv0NrCeVx poARv1G1fCJl4sMz4tU3bgtPLb1hktiY6+S++SXzhd6zvM3O3zs5u0n9liIyHs6INusC tZAZPjEl/sclnTc9anl8HU+5HfLc9okhjCGVrLFl07fCHZUdhMug7eBZeiuXaIHjPR5W l2jA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=DSjF02qUxW4DLfc6d28z46UMnLfo9Yq1L57eUQX/dxw=; b=axfE7JOIAzwJWnrUMFd+6CxKxrZf5YMuMNduMqRp8GsPUSNWU7YG60OsQv6lznlHAA aryg/x5rt5JyHT601rYficsix/HnJlFBK3s097Nx5RoLEgmpBGeRfBKquGFBDXQ3KHMU KLTR0HTeoATkMmsm7oUV+3PKXfmu3QWo7hPTi/2dNZ1xJxfvzAt6E2V+CcuHwttAiWNg afTY0L7RISBypkOu5FwHdN2hcrXpDfJ+QbD/lua/+nWlbDI/bqwA+d+Lc7Q0yMTZgunk x0fkZP3VS3lMy3ElPuRhwoJzAMNiQlaFx86/diHN9pEnHIUVedelli4wZ+8NsXcO7Dh9 EOlQ== 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 dt4si13061543ejc.34.2020.07.14.05.17.08; Tue, 14 Jul 2020 05:17:32 -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 S1728959AbgGNMPd (ORCPT + 99 others); Tue, 14 Jul 2020 08:15:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728308AbgGNMKx (ORCPT ); Tue, 14 Jul 2020 08:10:53 -0400 Received: from theia.8bytes.org (8bytes.org [IPv6:2a01:238:4383:600:38bc:a715:4b6d:a889]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC079C061755; Tue, 14 Jul 2020 05:10:52 -0700 (PDT) Received: from cap.home.8bytes.org (p5b006776.dip0.t-ipconnect.de [91.0.103.118]) (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 2398AF19; Tue, 14 Jul 2020 14:10:49 +0200 (CEST) From: Joerg Roedel To: x86@kernel.org Cc: Joerg Roedel , Joerg Roedel , hpa@zytor.com, Andy Lutomirski , Dave Hansen , Peter Zijlstra , Jiri Slaby , Dan Williams , Tom Lendacky , Juergen Gross , Kees Cook , David Rientjes , Cfir Cohen , Erdem Aktas , Masami Hiramatsu , Mike Stunes , Sean Christopherson , Martin Radev , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: [PATCH v4 30/75] x86/head/64: Install boot GDT Date: Tue, 14 Jul 2020 14:08:32 +0200 Message-Id: <20200714120917.11253-31-joro@8bytes.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200714120917.11253-1-joro@8bytes.org> References: <20200714120917.11253-1-joro@8bytes.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel Handling exceptions during boot requires a working GDT. The kernel GDT is not yet ready for use, so install a temporary boot GDT. Signed-off-by: Joerg Roedel --- arch/x86/kernel/head64.c | 19 +++++++++++++++++++ arch/x86/kernel/head_64.S | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c index cbb71c1b574f..51059bfd4b99 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -61,6 +61,25 @@ unsigned long vmemmap_base __ro_after_init = __VMEMMAP_BASE_L4; EXPORT_SYMBOL(vmemmap_base); #endif +/* + * GDT used before %gs is set up and the kernel can use gdt_page. Needed for + * early exception handling. + */ +struct desc_struct boot_gdt[GDT_ENTRIES] = { + [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff), + [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff), + [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff), +}; + +/* + * Address needs to be set at runtime because it references the boot_gdt while + * the kernel still uses a direct mapping. + */ +struct desc_ptr boot_gdt_descr = { + .size = sizeof(boot_gdt), + .address = 0, +}; + #define __head __section(.head.text) static void __head *fixup_pointer(void *ptr, unsigned long physaddr) diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index 16da4ac01597..473e60a12e2f 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -73,6 +73,26 @@ SYM_CODE_START_NOALIGN(startup_64) /* Set up the stack for verify_cpu(), similar to initial_stack below */ leaq (__end_init_task - SIZEOF_PTREGS)(%rip), %rsp + /* Setup boot GDT descriptor and load boot GDT */ + leaq boot_gdt(%rip), %rax + movq %rax, boot_gdt_descr+2(%rip) + lgdt boot_gdt_descr(%rip) + + /* New GDT is live - reload data segment registers */ + movl $__KERNEL_DS, %eax + movl %eax, %ds + movl %eax, %ss + movl %eax, %es + + /* Now switch to __KERNEL_CS so IRET works reliably */ + pushq $__KERNEL_CS + leaq .Lon_kernel_cs(%rip), %rax + pushq %rax + lretq + +.Lon_kernel_cs: + UNWIND_HINT_EMPTY + /* Sanitize CPU configuration */ call verify_cpu -- 2.27.0