Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1262485rbb; Mon, 26 Feb 2024 04:13:52 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXSa6445HrMIqnPVY2AwQMpOxa2+9DLaizunKmdtoRHcz5WNL1p7X1tEc2Du7ppaY9dQMIjkQoUkHEjWaZvUWGLKb7H3W8vzkGwyvV7cQ== X-Google-Smtp-Source: AGHT+IGKNr5CBGiXpf+Yz0Haj9fiNL4xJQDIJ7eJcbuhpywrm+2SzuU3n4BjZJ2gXabxLkgZt/qp X-Received: by 2002:a17:906:1949:b0:a43:3b2:bcf6 with SMTP id b9-20020a170906194900b00a4303b2bcf6mr3563978eje.14.1708949632390; Mon, 26 Feb 2024 04:13:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708949632; cv=pass; d=google.com; s=arc-20160816; b=kTlGFY93XfRK/UiLVz4O/3Wrt+kQCK6EZ5zQ6wtLkqxRNUCkiN8INB4RDPk5pnkVFB 7q0F4ZzFuI6OiegVmEkgVMG9s+PzMOpolFMr9tOBr5HJrEo6ylvSQoT24X1cPz6WnTMK q4yGngbG3f/I8iAF74UMz5WD1Nt6Y0NPp/m48OwamGE0mbHAQzI5F3WUrDSUKUi6wExS N3Fg824OJBzXLgsCGxs8kd7A3oLRmaG1DmPr5Dmpd5h1d0Eufeh13H2Ebwd9UTfzUPuY MNLSqFaqmFe+Pld3V2AbH24GUKBhhRgJxcYNz0+qXmyARVrski+sOHiaO0fN/Ql1e7Cw YjGg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:precedence:robot-unsubscribe:robot-id :message-id:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:in-reply-to:cc:subject:to:reply-to:sender :from:dkim-signature:dkim-signature:date; bh=DyHd/3wqfSy+ep7GRJ7JS+mWHJlTKt3+BrRJvvlF2oU=; fh=pcV3b1K4vaZv3RnwxIeScdhg1QZJF2eW6zPnJr6pdJ8=; b=FX115MZPXE7aPmVO7DELd3dJ4oVJhOjTpOePJ28a1UuylVVP7Z0sJqd6a75Fb/nH5L WWu/mCTVL6wc0ggXkvRQV9gasUwr2ZBypOFr2Ab3lEAXuh3vBoxWQsprLV9l/Ygei0ea 1Kg3TiVNJYF5h/uzfNoOPM8X1iVCsaZYbeRBHJus1I1i5TE7f9dL7DaPAGjPmMd3ZObM 6GNk2Ag1bkLlNFOsRuY2GW4sLvVPS1ainxYAHab+IMdoo1/TlPE1VZyC5kU6WevYj49k O5zBLF0tWXQambm37aiOwTpB0ofuOrtzN+X/mt8SXAmLX1InTw5WcXasejE9+9ee2/Oq Etiw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=FiOwzCq9; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-81345-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-81345-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id u8-20020a170906780800b00a3ece38edb2si2022095ejm.747.2024.02.26.04.13.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 04:13:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-81345-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=FiOwzCq9; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-81345-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-81345-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 007991F25FFB for ; Mon, 26 Feb 2024 12:13:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7D72660B87; Mon, 26 Feb 2024 12:12:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="FiOwzCq9"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="173ewDth" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 81881605B0; Mon, 26 Feb 2024 12:12:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708949572; cv=none; b=iggMLJk2TUkNXJ6TCGHndBlIu0xl2Iy3WIkH7fJ3XRDK4Wq3o7S8GgNwbJ1aLdKT73wz3mpr0rD/PLm2WqTW4kgg7HMsyIqJwMYARwy7uK+PvOySwvYOBIvPmCzj653N8nzt27BUej921cSnSUDlvvyiT10H6EmgMC/qwHJzmkY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708949572; c=relaxed/simple; bh=GHDuWh4lNR66dkekDJ22TuJEX3tomQWv/vy8HfoAH4M=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=Z1OSACqXYwGgcHgw+BKPHPFrR7H7KzC1rx6M7IjPxS6sbVn8fzQQRT181MGgqQGlk32hzM9nLY/H/06Xrc3jSqz1NNMLqw/CLRAoWlWn1UvqtLeM9hSDYOKo3bzf9ceEYnky0vB1dLQHfL2DVQiVMQ3IWEgacqvlUv9iHtn3OcA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=FiOwzCq9; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=173ewDth; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Date: Mon, 26 Feb 2024 12:12:48 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708949568; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DyHd/3wqfSy+ep7GRJ7JS+mWHJlTKt3+BrRJvvlF2oU=; b=FiOwzCq9Wf/aViWg+4WUrTaUxawLRaO+MX1aW/Xoydb5SdV39qtUKMq7nP4OCqCgOCKc5N Jwy64XvBkz6yNxJ4Fj296AiUwYrqkHoNv9DuolelMFnwxGEv/cFlNScJ+0w21+TqY5FkRO xkzSKYU7KPXk01y/oqj6qfl0lvB/d3FwIZvwpDf2s3FhDOjhaknKP7kI6prfce6oYhU/Ii 4vJjZsppI+EqkLP7RldXXmIX3odLbKfK9RtWnDbs7myRlExaluJEvwKREY7RyX9zAdppDv ADfNH4NDk5LjEBIupbjVXTaeFY4+8xuY1iQzIYceoMHB6vffDLR08FlDvBMxXA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708949568; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DyHd/3wqfSy+ep7GRJ7JS+mWHJlTKt3+BrRJvvlF2oU=; b=173ewDth7+UEZPq7FS06uqlFPTe7DEYbgOTKLxDuF3JNHArA6xeA4bHrW9pClIYmjAqX68 qP8qfWvpOakV1VDQ== From: "tip-bot2 for Ard Biesheuvel" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/boot] x86/boot/64: Simplify global variable accesses in GDT/IDT programming Cc: Ard Biesheuvel , "Borislav Petkov (AMD)" , Ingo Molnar , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20240221113506.2565718-19-ardb+git@google.com> References: <20240221113506.2565718-19-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <170894956806.398.1538176163796570341.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit The following commit has been merged into the x86/boot branch of tip: Commit-ID: 5da793671957e8e99fa74423fab2737bf8c772a8 Gitweb: https://git.kernel.org/tip/5da793671957e8e99fa74423fab2737bf8c772a8 Author: Ard Biesheuvel AuthorDate: Wed, 21 Feb 2024 12:35:08 +01:00 Committer: Ingo Molnar CommitterDate: Mon, 26 Feb 2024 12:58:11 +01:00 x86/boot/64: Simplify global variable accesses in GDT/IDT programming There are two code paths in the startup code to program an IDT: one that runs from the 1:1 mapping and one that runs from the virtual kernel mapping. Currently, these are strictly separate because fixup_pointer() is used on the 1:1 path, which will produce the wrong value when used while executing from the virtual kernel mapping. Switch to RIP_REL_REF() so that the two code paths can be merged. Also, move the GDT and IDT descriptors to the stack so that they can be referenced directly, rather than via RIP_REL_REF(). Rename startup_64_setup_env() to startup_64_setup_gdt_idt() while at it, to make the call from assembler self-documenting. Signed-off-by: Ard Biesheuvel Signed-off-by: Borislav Petkov (AMD) Signed-off-by: Ard Biesheuvel Signed-off-by: Ingo Molnar Link: https://lore.kernel.org/r/20240221113506.2565718-19-ardb+git@google.com --- arch/x86/include/asm/setup.h | 2 +- arch/x86/kernel/head64.c | 75 ++++++++++++++--------------------- arch/x86/kernel/head_64.S | 4 +-- 3 files changed, 32 insertions(+), 49 deletions(-) diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h index 5c83729..e61e68d 100644 --- a/arch/x86/include/asm/setup.h +++ b/arch/x86/include/asm/setup.h @@ -48,7 +48,7 @@ extern unsigned long saved_video_mode; extern void reserve_standard_io_resources(void); extern void i386_reserve_resources(void); extern unsigned long __startup_64(unsigned long physaddr, struct boot_params *bp); -extern void startup_64_setup_env(unsigned long physbase); +extern void startup_64_setup_gdt_idt(void); extern void early_setup_idt(void); extern void __init do_early_exception(struct pt_regs *regs, int trapnr); diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c index dc09560..1d6865e 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -76,15 +77,6 @@ static struct desc_struct startup_gdt[GDT_ENTRIES] __initdata = { [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(DESC_DATA64, 0, 0xfffff), }; -/* - * Address needs to be set at runtime because it references the startup_gdt - * while the kernel still uses a direct mapping. - */ -static struct desc_ptr startup_gdt_descr __initdata = { - .size = sizeof(startup_gdt)-1, - .address = 0, -}; - static void __head *fixup_pointer(void *ptr, unsigned long physaddr) { return ptr - (void *)_text + (void *)physaddr; @@ -569,62 +561,52 @@ void __init __noreturn x86_64_start_reservations(char *real_mode_data) */ static gate_desc bringup_idt_table[NUM_EXCEPTION_VECTORS] __page_aligned_data; -static struct desc_ptr bringup_idt_descr = { - .size = (NUM_EXCEPTION_VECTORS * sizeof(gate_desc)) - 1, - .address = 0, /* Set at runtime */ -}; - -static void set_bringup_idt_handler(gate_desc *idt, int n, void *handler) +/* This may run while still in the direct mapping */ +static void __head startup_64_load_idt(void *vc_handler) { -#ifdef CONFIG_AMD_MEM_ENCRYPT + struct desc_ptr desc = { + .address = (unsigned long)&RIP_REL_REF(bringup_idt_table), + .size = sizeof(bringup_idt_table) - 1, + }; struct idt_data data; - gate_desc desc; - - init_idt_data(&data, n, handler); - idt_init_desc(&desc, &data); - native_write_idt_entry(idt, n, &desc); -#endif -} + gate_desc idt_desc; -/* This runs while still in the direct mapping */ -static void __head startup_64_load_idt(unsigned long physbase) -{ - struct desc_ptr *desc = fixup_pointer(&bringup_idt_descr, physbase); - gate_desc *idt = fixup_pointer(bringup_idt_table, physbase); - - - if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) { - void *handler; - - /* VMM Communication Exception */ - handler = fixup_pointer(vc_no_ghcb, physbase); - set_bringup_idt_handler(idt, X86_TRAP_VC, handler); + /* @vc_handler is set only for a VMM Communication Exception */ + if (vc_handler) { + init_idt_data(&data, X86_TRAP_VC, vc_handler); + idt_init_desc(&idt_desc, &data); + native_write_idt_entry((gate_desc *)desc.address, X86_TRAP_VC, &idt_desc); } - desc->address = (unsigned long)idt; - native_load_idt(desc); + native_load_idt(&desc); } /* This is used when running on kernel addresses */ void early_setup_idt(void) { - /* VMM Communication Exception */ + void *handler = NULL; + if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) { setup_ghcb(); - set_bringup_idt_handler(bringup_idt_table, X86_TRAP_VC, vc_boot_ghcb); + handler = vc_boot_ghcb; } - bringup_idt_descr.address = (unsigned long)bringup_idt_table; - native_load_idt(&bringup_idt_descr); + startup_64_load_idt(handler); } /* * Setup boot CPU state needed before kernel switches to virtual addresses. */ -void __head startup_64_setup_env(unsigned long physbase) +void __head startup_64_setup_gdt_idt(void) { + void *handler = NULL; + + struct desc_ptr startup_gdt_descr = { + .address = (unsigned long)&RIP_REL_REF(startup_gdt), + .size = sizeof(startup_gdt) - 1, + }; + /* Load GDT */ - startup_gdt_descr.address = (unsigned long)fixup_pointer(startup_gdt, physbase); native_load_gdt(&startup_gdt_descr); /* New GDT is live - reload data segment registers */ @@ -632,5 +614,8 @@ void __head startup_64_setup_env(unsigned long physbase) "movl %%eax, %%ss\n" "movl %%eax, %%es\n" : : "a"(__KERNEL_DS) : "memory"); - startup_64_load_idt(physbase); + if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) + handler = &RIP_REL_REF(vc_no_ghcb); + + startup_64_load_idt(handler); } diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index bfbac50..e09cf0b 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -68,8 +68,6 @@ SYM_CODE_START_NOALIGN(startup_64) /* Set up the stack for verify_cpu() */ leaq (__end_init_task - PTREGS_SIZE)(%rip), %rsp - leaq _text(%rip), %rdi - /* Setup GSBASE to allow stack canary access for C code */ movl $MSR_GS_BASE, %ecx leaq INIT_PER_CPU_VAR(fixed_percpu_data)(%rip), %rdx @@ -77,7 +75,7 @@ SYM_CODE_START_NOALIGN(startup_64) shrq $32, %rdx wrmsr - call startup_64_setup_env + call startup_64_setup_gdt_idt /* Now switch to __KERNEL_CS so IRET works reliably */ pushq $__KERNEL_CS