Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp36556293rwd; Tue, 11 Jul 2023 02:34:30 -0700 (PDT) X-Google-Smtp-Source: APBJJlHpGUTyVWmLF/HFGjsSmI0LgGzStKyK9c4biFaU8Y7d3f56EfFykx0q0Z4saq8gVCBRUPbV X-Received: by 2002:a05:6a00:179b:b0:667:d0ff:6a0f with SMTP id s27-20020a056a00179b00b00667d0ff6a0fmr21545071pfg.5.1689068070285; Tue, 11 Jul 2023 02:34:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689068070; cv=none; d=google.com; s=arc-20160816; b=e93Bshc6cXFLfDLQWgSzPp0LPVm+x3071elOdqTdTH1unXSn9LUfwMoytSdUX5kGck l9TAm86lnUe2luIO2Iz+suZtgx3KO1d2ha6XaSRaD1kuEo0qW4nllwKB3wono5oyqwbK CppfUu4i8tueUi5GAW9LyL91E5OzFu/EfVRt9kfh44Wj25gguC0hoPBD0W+Er6yaA1+Z cnL5D5imDXUHzj5efxKlMF3RD5cHQt30AdFvcVK42FRhiTIq2kHV9lgj+sc//9rQ0BFb HdhCU/2c9dp5Tu05GsMiJ58IgW5Oz681/jI5do81mwAyeeG0Iyy/YG+sw5hnv+AHWJU6 xWRw== 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 :dkim-signature; bh=SdcGMtW4O88GwOhSHz+dTafrQpAhx1lYzkl4aavvAfY=; fh=UoPnrMfWVKGOqsML3Cwqzr4dYAyNRBpPmY/ITnBmPi0=; b=d2LtFcBYEBc4L76xmUVAqIBRowZxYqJz+w2CkODf/dvCTYCHk7R95SseJiU6jNbtav 56jmaEknJS0BkhnDepI8aDWkQaBnShk5EfJSAUbjnw6NZtTd/V0TUpZI+rgs9XUjriGQ nuhTFRmW58iR5ouDjXDjGQtn3s2pl3+TmyDMB50mBiX8jLW78mIej/zNG8AboAetMFMV ny9trwOxzZ6vrr+z4MPvWb+LaRuKQEGfCCuE4BeaKD6c/XemLfPlx0T3KVoiJiExjUgD 6V8OzOytBgAf21kcA/FQUwSI1+H1iVjnyk+GM13Sk5ruFSekP14y/KDJADo2uKAQ06mV q3tA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=N5gTkJgT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z16-20020a634c10000000b0055c66f42ee0si19788pga.740.2023.07.11.02.34.17; Tue, 11 Jul 2023 02:34:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=N5gTkJgT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231719AbjGKJPL (ORCPT + 99 others); Tue, 11 Jul 2023 05:15:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231620AbjGKJPJ (ORCPT ); Tue, 11 Jul 2023 05:15:09 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58B5BB7; Tue, 11 Jul 2023 02:15:08 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E1C4461418; Tue, 11 Jul 2023 09:15:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 796F8C433CB; Tue, 11 Jul 2023 09:15:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689066907; bh=LsonN6EFYWNHTyh2VqSp3Tgm/usNg6vNJvM1yGPTkDk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=N5gTkJgTcZyBE6WO2hR9BtcFv3Wxra0x1YA5agVtnim2pm174Jv6cvgqidixim+hb 2Nu3A86ydag0JiG5NgkJZDkeAcuW4KJv2Rwr39XwaG71CPod/IdaJuJoH389/kTtxb niOGduStgdf9qftLpp+1A/wG366GeBsaOgAk2U3ONXMGGrzPO5kZRjG767fexC1GvJ EHz9qIx3tINKe9o95O4Giaz8fQSzj6+pU/UOfK/1bnKI4yCyxoEkXjc47n8tT3Ha3+ 5DowCpDRKShTJRzbZZxcs8oDkl8xx5OF/KafjFgqnc5RJsS0ez7JgM30lk5roIuZHx 3GUGGCrSlhYtg== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v6 01/21] x86/head_64: Store boot_params pointer in callee save register Date: Tue, 11 Jul 2023 11:14:33 +0200 Message-Id: <20230711091453.2543622-2-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230711091453.2543622-1-ardb@kernel.org> References: <20230711091453.2543622-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3709; i=ardb@kernel.org; h=from:subject; bh=LsonN6EFYWNHTyh2VqSp3Tgm/usNg6vNJvM1yGPTkDk=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIWWtbLnWtPwJbkICW6cf2GB7KMm3Sdl+mnDfnZq+vtN3l qq9jb/eUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACaidY6R4eMbn50Btb+kFi8z 3PogQGHfomXJ7ZN7Ip02p3x/X75rHS8jw12bCerVzg+k2/vW5U2/9N8krqDFxShjA7t3nszKMyF LmAA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Instead of pushing/popping %RSI to/from the stack every time a function is called from startup_64(), store it in callee preserved register R15 and grab it from there when its value is actually needed. Secondary boot does not and should not care about the value of %RSI at entry, and all the C handlers except the primary boot C entrypoint all ignore the function arguments entirely. Avoid inadvertent surprises by zeroing R15 on the secondary boot path explicitly. Signed-off-by: Ard Biesheuvel --- arch/x86/kernel/head_64.S | 23 +++++--------------- 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index c5b9289837dcbad2..dbc80fe098f86860 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -62,6 +62,7 @@ SYM_CODE_START_NOALIGN(startup_64) * compiled to run at we first fixup the physical addresses in our page * tables and then reload them. */ + mov %rsi, %r15 /* Preserve boot_params pointer */ /* Set up the stack for verify_cpu() */ leaq (__end_init_task - PTREGS_SIZE)(%rip), %rsp @@ -75,9 +76,7 @@ SYM_CODE_START_NOALIGN(startup_64) shrq $32, %rdx wrmsr - pushq %rsi call startup_64_setup_env - popq %rsi /* Now switch to __KERNEL_CS so IRET works reliably */ pushq $__KERNEL_CS @@ -95,10 +94,8 @@ SYM_CODE_START_NOALIGN(startup_64) * which needs to be done before any CPUID instructions are executed in * subsequent code. */ - movq %rsi, %rdi - pushq %rsi + movq %r15, %rdi /* Pass boot_params pointer */ call sme_enable - popq %rsi #endif /* Sanitize CPU configuration */ @@ -111,9 +108,7 @@ SYM_CODE_START_NOALIGN(startup_64) * programmed into CR3. */ leaq _text(%rip), %rdi - pushq %rsi call __startup_64 - popq %rsi /* Form the CR3 value being sure to include the CR3 modifier */ addq $(early_top_pgt - __START_KERNEL_map), %rax @@ -127,8 +122,6 @@ SYM_CODE_START(secondary_startup_64) * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 0, * and someone has loaded a mapped page table. * - * %rsi holds a physical pointer to real_mode_data. - * * We come here either from startup_64 (using physical addresses) * or from trampoline.S (using virtual addresses). * @@ -153,6 +146,8 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) UNWIND_HINT_END_OF_STACK ANNOTATE_NOENDBR + xorq %r15, %r15 /* Clear boot_params pointer */ + /* * Retrieve the modifier (SME encryption mask if SME is active) to be * added to the initial pgdir entry that will be programmed into CR3. @@ -199,13 +194,9 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) * hypervisor could lie about the C-bit position to perform a ROP * attack on the guest by writing to the unencrypted stack and wait for * the next RET instruction. - * %rsi carries pointer to realmode data and is callee-clobbered. Save - * and restore it. */ - pushq %rsi movq %rax, %rdi call sev_verify_cbit - popq %rsi /* * Switch to new page-table @@ -365,9 +356,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) wrmsr /* Setup and Load IDT */ - pushq %rsi call early_setup_idt - popq %rsi /* Check if nx is implemented */ movl $0x80000001, %eax @@ -403,9 +392,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) pushq $0 popfq - /* rsi is pointer to real mode structure with interesting info. - pass it to C */ - movq %rsi, %rdi + movq %r15, %rdi /* Pass boot_params pointer */ .Ljump_to_C_code: /* -- 2.39.2