From nobody Mon Feb 9 10:50:27 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15C11EB64DC for ; Wed, 12 Jul 2023 03:32:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232116AbjGLDcm (ORCPT ); Tue, 11 Jul 2023 23:32:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229537AbjGLDcQ (ORCPT ); Tue, 11 Jul 2023 23:32:16 -0400 Received: from out0-207.mail.aliyun.com (out0-207.mail.aliyun.com [140.205.0.207]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 053381BF5 for ; Tue, 11 Jul 2023 20:32:07 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R191e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047207;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=11;SR=0;TI=SMTPD_---.TrdHx8q_1689132719; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.TrdHx8q_1689132719) by smtp.aliyun-inc.com; Wed, 12 Jul 2023 11:32:00 +0800 From: "Hou Wenlong" To: linux-kernel@vger.kernel.org Cc: "Lai Jiangshan" , "Hou Wenlong" , "Dave Hansen" , "Andy Lutomirski" , "Peter Zijlstra" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "=?UTF-8?B?bWFpbnRhaW5lcjpYODYgQVJDSElURUNUVVJFIDMyLUJJVCBBTkQgNjQtQklU?=" , "H. Peter Anvin" Subject: [PATCH RFC 7/7] x86/sme: Build the code in mem_encrypt_identity.c as PIE Date: Wed, 12 Jul 2023 11:30:11 +0800 Message-Id: <80367ca39ed736b6fc839c5de99a006f54182c45.1689130310.git.houwenlong.hwl@antgroup.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Similar to head64.c, all the code in mem_encrypt_identity.c runs in identity address. However, it uses inline assembly to use RIP-relative reference for some globals. Therefore, build the code as PIE to force the compiler to generate RIP-relative reference, allowing for all inline assembly to be removed. Suggested-by: Lai Jiangshan Signed-off-by: Hou Wenlong --- The changes have not been tested on AMD SME. arch/x86/mm/Makefile | 3 +++ arch/x86/mm/mem_encrypt_identity.c | 31 +++++------------------------- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/arch/x86/mm/Makefile b/arch/x86/mm/Makefile index c80febc44cd2..f5d7b22c5f1b 100644 --- a/arch/x86/mm/Makefile +++ b/arch/x86/mm/Makefile @@ -32,6 +32,9 @@ obj-y +=3D pat/ # Make sure __phys_addr has no stackprotector CFLAGS_physaddr.o :=3D -fno-stack-protector CFLAGS_mem_encrypt_identity.o :=3D -fno-stack-protector +CFLAGS_mem_encrypt_identity.o +=3D -fPIE -include $(srctree)/include/linu= x/hidden.h + +CFLAGS_REMOVE_mem_encrypt_identity.o +=3D -mcmodel=3Dkernel CFLAGS_fault.o :=3D -I $(srctree)/$(src)/../include/asm/trace diff --git a/arch/x86/mm/mem_encrypt_identity.c b/arch/x86/mm/mem_encrypt_i= dentity.c index 72aeb0f3dec6..2f292ab4e6a9 100644 --- a/arch/x86/mm/mem_encrypt_identity.c +++ b/arch/x86/mm/mem_encrypt_identity.c @@ -343,13 +343,7 @@ void __head sme_encrypt_kernel(struct boot_params *bp) } #endif - /* - * We're running identity mapped, so we must obtain the address to the - * SME encryption workarea using rip-relative addressing. - */ - asm ("lea sme_workarea(%%rip), %0" - : "=3Dr" (workarea_start) - : "p" (sme_workarea)); + workarea_start =3D (unsigned long)(void *)sme_workarea; /* * Calculate required number of workarea bytes needed: @@ -505,7 +499,7 @@ void __head sme_encrypt_kernel(struct boot_params *bp) void __head sme_enable(struct boot_params *bp) { - const char *cmdline_ptr, *cmdline_arg, *cmdline_on, *cmdline_off; + const char *cmdline_ptr; unsigned int eax, ebx, ecx, edx; unsigned long feature_mask; bool active_by_default; @@ -578,21 +572,6 @@ void __head sme_enable(struct boot_params *bp) goto out; } - /* - * Fixups have not been applied to phys_base yet and we're running - * identity mapped, so we must obtain the address to the SME command - * line argument data using rip-relative addressing. - */ - asm ("lea sme_cmdline_arg(%%rip), %0" - : "=3Dr" (cmdline_arg) - : "p" (sme_cmdline_arg)); - asm ("lea sme_cmdline_on(%%rip), %0" - : "=3Dr" (cmdline_on) - : "p" (sme_cmdline_on)); - asm ("lea sme_cmdline_off(%%rip), %0" - : "=3Dr" (cmdline_off) - : "p" (sme_cmdline_off)); - if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT)) active_by_default =3D true; else @@ -601,12 +580,12 @@ void __head sme_enable(struct boot_params *bp) cmdline_ptr =3D (const char *)((u64)bp->hdr.cmd_line_ptr | ((u64)bp->ext_cmd_line_ptr << 32)); - if (cmdline_find_option(cmdline_ptr, cmdline_arg, buffer, sizeof(buffer))= < 0) + if (cmdline_find_option(cmdline_ptr, sme_cmdline_arg, buffer, sizeof(buff= er)) < 0) return; - if (!strncmp(buffer, cmdline_on, sizeof(buffer))) + if (!strncmp(buffer, sme_cmdline_on, sizeof(buffer))) sme_me_mask =3D me_mask; - else if (!strncmp(buffer, cmdline_off, sizeof(buffer))) + else if (!strncmp(buffer, sme_cmdline_off, sizeof(buffer))) sme_me_mask =3D 0; else sme_me_mask =3D active_by_default ? me_mask : 0; -- 2.31.1