From nobody Fri Nov 29 00:51:14 2024 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F1F63B298 for ; Wed, 25 Sep 2024 15:01:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276516; cv=none; b=p52BB6NT7A2CoP3L+S3Gq1t+skYsjaGfGqBTylBpmVTZzR79Q+yhyRRCVxcDnBO8RpC+pZU8g7xqQ/yonwDwu9WLZnwcWoPnxx/kkIPRn8x1AOtadjhDVktwkPsD+6xpYEYEZYj9m704dhF/qA0jJQcatqiz48YcOIaudH/rA00= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276516; c=relaxed/simple; bh=7J0jWEjwrexzo9oPbBgZ33ZQC1xyFpo8+XmzUWJ/VHg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=B9MJTeGCFvfQVnG5dJlG0rb6FtvC7XBabX7czZ3zkdOC/uwUZ+Z2rO1tSPP/LYCrAJSMDIYUWgpxzPfHMI8o1YEF0jJKV3sCeMIN/3oLniW6EkAczvzcEXhGw/3g7+MMGxQvGlGqbfTV8lVOsXf2cVwMmLuGQFSObn3w4MtKHzA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=yEqqSHTi; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="yEqqSHTi" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-42cb9e14ab6so45722115e9.3 for ; Wed, 25 Sep 2024 08:01:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276512; x=1727881312; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bvHrMBM7osvq1YNoyXkrrTpmrTIb8bNW96uzrIXNQu8=; b=yEqqSHTileV8mRFw/uWhSlHf8HPvgLYHAzC0BoLZiiRPC+JG3TnSCk/wzr0FcVCQ4C u1wy8geYYzk+k4fHyl0oA//q+pNRtKEVT0HoK/m1g7ACiBEhqCqS7nfz/jBUWb10ZUrv gQHVZ3OYRmXq4n1p6xDqyQVOury3bDN40Tm7ZUTsfao/pHE9eY6s3wdLgrHqAipLGuHU HazsPfuHo6iAIwTZzdr3uqUJRMvzHVWSm9Mtyt29amKKtthr4fq4j66MH5FvyNglmzG5 PANRA3J72y8uR8mXzuGElRpueMNhdk3AB8ci0000K0KiLGQ3Pdy3CtmRJ5FM1N7rpZGA SXtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276512; x=1727881312; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bvHrMBM7osvq1YNoyXkrrTpmrTIb8bNW96uzrIXNQu8=; b=G46+nHoKe6/ncqEiCKFG8YBKTkWeK4Ah6xfzNW6bb96PjDRBTlTC4JwEFdRB81ycNE tUU5baPvv2NXpP0eGEkrXpd7Jn/pvOjPk1mR5rc40njBX7A6GOP/IZ+6h8dqqQIyo9IF eR7RB9gzLznqJL36OoyxaMj+NxlwxuRB3fBeYnbblZSBL12KQ9g38s0f+PG8k6alOltO e+3A3l8wLF2iuE+vNhHVviwYxUVdN42jqXKqepegprE/jSlLSteTruONAejJRB2LRVu3 0hbZm/IlPoNnWLZeeKU7MSkh4wXFf5LqgY5VJMUwBtCU6c0wdA2tS8CgSZ6OJzZmrhxH C/BA== X-Gm-Message-State: AOJu0YzB1VLrSowGU/oYwMvgbbXBB+/WhNjMPw+ebmIZfnfmTeznpo/a Ep9zcc5jkpxQaMjoAduAbw11AHWe6bjwDOUV2XglSNMifr8NiYmHYgUd7QQXh72dTgjms02E25U irBxA+9apFWbjjZHWZ4NFyDloRUrtqkY0lVF7wuDkgA0UMGpjQmiVlIdCE+vz7p7yxx1wZ4xGg+ OiLXnQjqPdDi0SOtAfJ4vYTW2O/meyew== X-Google-Smtp-Source: AGHT+IGNEZwim//wtoUQ0IgEwXQAmuWMXuvjVNrkXztiUbXuSVle0sf2azOpfGly/wGUuNh4kSi9g764 X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:600c:4b23:b0:42c:b635:9ba7 with SMTP id 5b1f17b1804b1-42e961449f9mr294105e9.3.1727276511763; Wed, 25 Sep 2024 08:01:51 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:01 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1383; i=ardb@kernel.org; h=from:subject; bh=kkus52hb+9U3rGqLGvcTtWiqi7ULiH1wdI2hpFI7/9I=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6loPG1eTz6/TRZ+ExF55v9yoPVaMz0DJP05TXvNix 3yvWS87SlkYxDgYZMUUWQRm/3238/REqVrnWbIwc1iZQIYwcHEKwER0vzAyLNdk+Hf0DcN93e0c kou2iV0ReKfEPrsn3ORh04fsoM3R+gz/0yZt5ne4cnJFldTMQpGfLor75ZYczDujFLCs+4iee6Q pMwA= X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-31-ardb+git@google.com> Subject: [RFC PATCH 01/28] x86/pvh: Call C code via the kernel virtual mapping From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Calling C code via a different mapping than it was linked at is problematic, because the compiler assumes that RIP-relative and absolute symbol references are interchangeable. GCC in particular may use RIP-relative per-CPU variable references even when not using -fpic. So call xen_prepare_pvh() via its kernel virtual mapping on x86_64, so that those RIP-relative references produce the correct values. This matches the pre-existing behavior for i386, which also invokes xen_prepare_pvh() via the kernel virtual mapping before invoking startup_32 with paging disabled again. Fixes: 7243b93345f7 ("xen/pvh: Bootstrap PVH guest") Signed-off-by: Ard Biesheuvel Reviewed-by: Jason Andryuk Tested-by: Jason Andryuk --- arch/x86/platform/pvh/head.S | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/x86/platform/pvh/head.S b/arch/x86/platform/pvh/head.S index f7235ef87bc3..a308b79a887c 100644 --- a/arch/x86/platform/pvh/head.S +++ b/arch/x86/platform/pvh/head.S @@ -101,7 +101,11 @@ SYM_CODE_START_LOCAL(pvh_start_xen) xor %edx, %edx wrmsr =20 - call xen_prepare_pvh + /* Call xen_prepare_pvh() via the kernel virtual mapping */ + leaq xen_prepare_pvh(%rip), %rax + addq $__START_KERNEL_map, %rax + ANNOTATE_RETPOLINE_SAFE + call *%rax =20 /* startup_64 expects boot_params in %rsi. */ mov $_pa(pvh_bootparams), %rsi --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:14 2024 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 768968175F for ; Wed, 25 Sep 2024 15:01:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276519; cv=none; b=mtFEAOtlZMIYM7kATqvq2Nu+yGOHNB5RB6YkAi2sy/KeGIVP+5XOg3jZlwE6uoMR8mzQmn6Flp3p08CJy4cOMFnvqVFpmQsb9qpY/eMNqVP8EiCXzGl8CDlnKeWv1DiVjVADSsRY5C4DvMOoMDDmBXOb4eJ5091QLEmYN5Df2Pw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276519; c=relaxed/simple; bh=rk1TLausu3D2x4uz05lRhrdTMxiTSLA2fDl1meeIK+I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cfS9a3m37jHpslkOuYNNSpUkSNyLTztB3cwNiwdTPpZiMhQVUsJmfT+qdenYxE28H9zOiHXY5g6DghfjwDVkL8R0Ex0P8WIo3FNHItULhpn1QSZdOPqrnC/DqsykEBcGe8QndMpZDYuPeTtWdFGQFPAHitLqP225X82vMxaY4nM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=VFVz+AlT; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="VFVz+AlT" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-42cb115566eso63191775e9.2 for ; Wed, 25 Sep 2024 08:01:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276515; x=1727881315; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yjSReXj8XxN+J1aHSpg+bgY7XO8zJXJrh2uX1BeFRGQ=; b=VFVz+AlT0fVj8J3pb0uIJQLGxkYCKkAvLDgcAohPDUXmaMuk6UTbw4uq+CHcsEoRZ/ 5x5aSnW20fDUqHmuY3uMAA6I80/9rPDD/5xGciRxrWzTnMT2UtipActHUBezkRFpEjjj sOG059kJtCdOCKA5XBwzrZmIOIa5S/UmblboEdVQKaDLNiR6M6X2DIjGQ4+n7oN0yCPj sb6pNNvyVesjn3JTHhpQAoqB4ylt5fKdSAS0LSqxRycl+oVbZWRIh3MrNoaPcUJI0dRJ +onoD54UsA3PxnhIJe/kNKf8LKnhjzLE+eMh0qFTJDtNpVvQBt0X749C/T9XvKosb9xa ZuuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276515; x=1727881315; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yjSReXj8XxN+J1aHSpg+bgY7XO8zJXJrh2uX1BeFRGQ=; b=nV4s+s7pdpbAghFm6EpX2SpylydyxWY6I4urPs5VqMLuD2buYwA7Yp4Idf7bJ4VDM8 9fuSxNq0AM/SrQ8xtJd5ItwlKPyPXZJ46qzGpvLHieQphpzfL950jkWddzqlStn2M1aW nkeVNlOcvp9rk9VhG9uW489Cq+INvwIHXbkkYXPmpSQyu1EtzeXioI+MeqUnV6qW4NZV 5Htupa4K9bEfrh4P/evLl1DaHjFczS7gz31YDzDSVkRM54v0MHVNPzKaGDH/rrw171Kp qg+SLFEThOr44X9K/Zf/p4rwBF2/3MwuSql3IRti3FfHqYkju4CZlXhGMSw+s7wDrYvH vviQ== X-Gm-Message-State: AOJu0YyJw1ZkmcNQRS906fDfUHv5pz0UD1LlKfeSsq3RkixFD+YjMTJh raV+/K/SA2ZpaPMcHHM395EqOA0f9znhEWAgpzQmtakPo3GNheEjlmAWNR0Z0wlF2E/uL4ji77u gTz5hjBy7ihXdl0Sdh7+AuREBRiwR3v3kRTA/zbD0/zDzh4F34lPQACT21fyCAKeZuIagDd4IC9 SQci8mRSJF0II7Ymw9DcN5S4xTSZQuHw== X-Google-Smtp-Source: AGHT+IGzIZ2o2YM9LwRx7GUBkPXP1nnHd93bOM06M/lDtcRNJJFbQb+/UGpm4f4GZO5kVPvAO8R9y3yP X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:600c:214f:b0:42c:b32e:6ba7 with SMTP id 5b1f17b1804b1-42e9624246bmr34035e9.6.1727276514236; Wed, 25 Sep 2024 08:01:54 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:02 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1869; i=ardb@kernel.org; h=from:subject; bh=Etb53DEUMtJxbS45M94XzvcBugumXuKFZ3M2B/KYBNo=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6jquDX165ub5J178W1mVH21S/GxP0fqUgljlqYpRR x60nEvpKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABPRa2RkeBV2XNd/yacJf4IS mc47ex9ud90aJbN1Uf8PDSuxLfrPTjD8rwhuEVogxe0Xov++IjA4keVg55+fz6JEZeM/B04o0/B nBgA= X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-32-ardb+git@google.com> Subject: [RFC PATCH 02/28] Documentation: Bump minimum GCC version to 8.1 From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Bump the minimum GCC version to 8.1 to gain unconditional support for referring to the per-task stack cookie using a symbol rather than relying on the fixed offset of 40 bytes from %GS, which requires elaborate hacks to support. Signed-off-by: Ard Biesheuvel Acked-by: Arnd Bergmann Acked-by: Mark Rutland Acked-by: Miguel Ojeda --- Documentation/admin-guide/README.rst | 2 +- Documentation/process/changes.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/README.rst b/Documentation/admin-gui= de/README.rst index f2bebff6a733..3dda41923ed6 100644 --- a/Documentation/admin-guide/README.rst +++ b/Documentation/admin-guide/README.rst @@ -259,7 +259,7 @@ Configuring the kernel Compiling the kernel -------------------- =20 - - Make sure you have at least gcc 5.1 available. + - Make sure you have at least gcc 8.1 available. For more information, refer to :ref:`Documentation/process/changes.rst = `. =20 - Do a ``make`` to create a compressed kernel image. It is also possible = to do diff --git a/Documentation/process/changes.rst b/Documentation/process/chan= ges.rst index 00f1ed7c59c3..59b7d3d8a577 100644 --- a/Documentation/process/changes.rst +++ b/Documentation/process/changes.rst @@ -29,7 +29,7 @@ you probably needn't concern yourself with pcmciautils. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D Program Minimal version Command to check the version =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D -GNU C 5.1 gcc --version +GNU C 8.1 gcc --version Clang/LLVM (optional) 13.0.1 clang --version Rust (optional) 1.78.0 rustc --version bindgen (optional) 0.65.1 bindgen --version --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:14 2024 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1569F12C475 for ; Wed, 25 Sep 2024 15:01:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276523; cv=none; b=D6KrEOnPzJa9SoHQ1fbwqjtq7xCeE+4f6iEZxQiLAOBXWCiV+5gjFoGBbj/EmZ3wjab5TOylWD52EYZLqKwsbeHpl7YaNidll+cAMzs71xYkCJpSurTZJgvROEGowtzXaqS9G/O2I0I246hW5+K0xm4MDRU37V/qbK43X4FTzV0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276523; c=relaxed/simple; bh=IaHPxWGMSEwaCkkyiwAlKMa55WaS/Witb8KJx2+NTs0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dCNbIxEkXTFqyXRkx7hf95tedzahy3LdE9Ewq4iAEKPiEOs+/IuaIEekUGhyo+Vq3uvZ286cNgXIjf3q/2YKDSNf+4iMEURmFQpmeE/Rkzomgbq0gITzF+U9kFrR7CiPnhN3RIOTutVoM8/ieszgm9WOSuNCEUQCmpMileDDaUI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=VTSA4YDK; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="VTSA4YDK" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-42cb22d396cso56943065e9.0 for ; Wed, 25 Sep 2024 08:01:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276517; x=1727881317; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=pz/XW8QSYpyjnMJx025a43dGrFFSpzR/yo2KpiqoalU=; b=VTSA4YDKPJq1jicr67xqWdf4nwClspY8JM5dgJH0R0KYTHBFQtSarPIlLjm8RovvY+ /WMs/aqv2AFWG0V251GZWw9pSQ0RH8AIZqSPeWyV1Z3typUzTchSPU2lPWmZcePbsI+u TpFvyzTWgLqj34v4YzyLOYJKEdSzkbTTUT7u5mIhIZND2Eew8i3CNmUmg8NYjjhDAP3s jxv033QZTJfALqXF1tnT5npGZ1q9cqBK8SIvq2Kl+IfPO8W+IVczRgntZcoRujZ7ccaI hMkprLVVGdMQa2gKRcG71lSGCiVmb0a8RcFwqAP22v6Wrz+B0ZZeK1XBf3HdNGvjraJp FrMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276517; x=1727881317; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pz/XW8QSYpyjnMJx025a43dGrFFSpzR/yo2KpiqoalU=; b=v6C+dWkXdc34cF3VeUBRqOSoZZ6ReNpv0bd+bCJsUa+LNnYT1pgiLuMtMmjuzA/wDC 2ETUlVQpsDJVoIz8w7OU8GThsZHg3L9A7UvtYf+hrXGP6yCt47pD/iVMWJbuV5tnm3pV 0g1jTY2JB/MuwAWyKn0ddVPwlhgaq6zKYDd7Nmd7mOKTJioL93ItEC/frSsJBCw5qMRP Z/mdhxf6bgP5HffSROrNZRLyiCAoT+ce/sj8YiqLjq3bTJvq4D4JKJWkflYBDP8Bes50 DzP5+mAprZmzeYTEeogPUUqUT0D92AARCnUPdc+apvwJhku4YaOiycBwNCDuliwyZ7IU Pvpg== X-Gm-Message-State: AOJu0YyDRDNkjvUYlGDxzGq11nlM4rcULXZoE9NCZY8IqtnyU8TJb7XQ UbZM0B/W29+knv/vlSfMqIUhYxIMz/QsVqzVKhrKjUV1eUSHZCaINcO+1B07MBm+fgrUF4/fvcg 4dZNXSE6rL04lD3i5NxEr5CML3hbfoLLTD84jmPInOCMSHw7yD3bKwxvn3kdP+3hLGW6q90Cv2d nmEakzYf8mtuzalecXbxfw3y7zmEPLRw== X-Google-Smtp-Source: AGHT+IEbq9+/MTLELU3OXPlyueW+j2FWZvgN6ZakUbbc2SrTEQmlnuellku1ZjQlO+1N3OhIaNIzAxHq X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:600c:5119:b0:42c:b4ca:768c with SMTP id 5b1f17b1804b1-42e961360edmr149855e9.3.1727276516881; Wed, 25 Sep 2024 08:01:56 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:03 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=10814; i=ardb@kernel.org; h=from:subject; bh=yS/zV/W3xopSyE4a8OCITK3fowdcfQuYvJXmy9tih3c=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6obs5pM9GZf2VnAvYlrokLT1Ituly6LWzTERkUrrD f6oTr3QUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACYSz8/wm0VimXuGXXdnV+M6 5qSfrfdnXGBIb3j5Z7LmafMTK97PMWD4K1PcmbbV2I3veafe8+rETfcetx2VnGN1t1r8BK+RQaQ qCwA= X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-33-ardb+git@google.com> Subject: [RFC PATCH 03/28] x86/tools: Use mmap() to simplify relocs host tool From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Instead of relying on fseek() and fread() to traverse the vmlinux file when processing the ELF relocations, mmap() the whole thing and use memcpy() or direct references where appropriate: - the executable and section headers are byte swabbed before use if the host is big endian, so there, the copy is retained; - the strtab and extended symtab are not byte swabbed so there, the copies are replaced with direct references into the mmap()'ed region. This substantially simplifies the code, and makes it much easier to refer to other file contents directly. This will be used by a subsequent patch to handle GOTPCREL relocations. Signed-off-by: Ard Biesheuvel --- arch/x86/tools/relocs.c | 145 ++++++++------------ arch/x86/tools/relocs.h | 2 + 2 files changed, 62 insertions(+), 85 deletions(-) diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c index c101bed61940..35a73e4aa74d 100644 --- a/arch/x86/tools/relocs.c +++ b/arch/x86/tools/relocs.c @@ -37,15 +37,17 @@ static struct relocs relocs64; #endif =20 struct section { - Elf_Shdr shdr; - struct section *link; - Elf_Sym *symtab; - Elf32_Word *xsymtab; - Elf_Rel *reltab; - char *strtab; + Elf_Shdr shdr; + struct section *link; + Elf_Sym *symtab; + const Elf32_Word *xsymtab; + Elf_Rel *reltab; + const char *strtab; }; static struct section *secs; =20 +static const void *elf_image; + static const char * const sym_regex_kernel[S_NSYMTYPES] =3D { /* * Following symbols have been audited. There values are constant and do @@ -291,7 +293,7 @@ static Elf_Sym *sym_lookup(const char *symname) for (i =3D 0; i < shnum; i++) { struct section *sec =3D &secs[i]; long nsyms; - char *strtab; + const char *strtab; Elf_Sym *symtab; Elf_Sym *sym; =20 @@ -354,7 +356,7 @@ static uint64_t elf64_to_cpu(uint64_t val) static int sym_index(Elf_Sym *sym) { Elf_Sym *symtab =3D secs[shsymtabndx].symtab; - Elf32_Word *xsymtab =3D secs[shxsymtabndx].xsymtab; + const Elf32_Word *xsymtab =3D secs[shxsymtabndx].xsymtab; unsigned long offset; int index; =20 @@ -368,10 +370,9 @@ static int sym_index(Elf_Sym *sym) return elf32_to_cpu(xsymtab[index]); } =20 -static void read_ehdr(FILE *fp) +static void read_ehdr(void) { - if (fread(&ehdr, sizeof(ehdr), 1, fp) !=3D 1) - die("Cannot read ELF header: %s\n", strerror(errno)); + memcpy(&ehdr, elf_image, sizeof(ehdr)); if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) !=3D 0) die("No ELF magic\n"); if (ehdr.e_ident[EI_CLASS] !=3D ELF_CLASS) @@ -414,60 +415,48 @@ static void read_ehdr(FILE *fp) =20 =20 if (shnum =3D=3D SHN_UNDEF || shstrndx =3D=3D SHN_XINDEX) { - Elf_Shdr shdr; - - if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) - die("Seek to %" FMT " failed: %s\n", ehdr.e_shoff, strerror(errno)); - - if (fread(&shdr, sizeof(shdr), 1, fp) !=3D 1) - die("Cannot read initial ELF section header: %s\n", strerror(errno)); + const Elf_Shdr *shdr =3D elf_image + ehdr.e_shoff; =20 if (shnum =3D=3D SHN_UNDEF) - shnum =3D elf_xword_to_cpu(shdr.sh_size); + shnum =3D elf_xword_to_cpu(shdr->sh_size); =20 if (shstrndx =3D=3D SHN_XINDEX) - shstrndx =3D elf_word_to_cpu(shdr.sh_link); + shstrndx =3D elf_word_to_cpu(shdr->sh_link); } =20 if (shstrndx >=3D shnum) die("String table index out of bounds\n"); } =20 -static void read_shdrs(FILE *fp) +static void read_shdrs(void) { + const Elf_Shdr *shdr =3D elf_image + ehdr.e_shoff; int i; - Elf_Shdr shdr; =20 secs =3D calloc(shnum, sizeof(struct section)); if (!secs) die("Unable to allocate %ld section headers\n", shnum); =20 - if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) - die("Seek to %" FMT " failed: %s\n", ehdr.e_shoff, strerror(errno)); - - for (i =3D 0; i < shnum; i++) { + for (i =3D 0; i < shnum; i++, shdr++) { struct section *sec =3D &secs[i]; =20 - if (fread(&shdr, sizeof(shdr), 1, fp) !=3D 1) - die("Cannot read ELF section headers %d/%ld: %s\n", i, shnum, strerror(= errno)); - - sec->shdr.sh_name =3D elf_word_to_cpu(shdr.sh_name); - sec->shdr.sh_type =3D elf_word_to_cpu(shdr.sh_type); - sec->shdr.sh_flags =3D elf_xword_to_cpu(shdr.sh_flags); - sec->shdr.sh_addr =3D elf_addr_to_cpu(shdr.sh_addr); - sec->shdr.sh_offset =3D elf_off_to_cpu(shdr.sh_offset); - sec->shdr.sh_size =3D elf_xword_to_cpu(shdr.sh_size); - sec->shdr.sh_link =3D elf_word_to_cpu(shdr.sh_link); - sec->shdr.sh_info =3D elf_word_to_cpu(shdr.sh_info); - sec->shdr.sh_addralign =3D elf_xword_to_cpu(shdr.sh_addralign); - sec->shdr.sh_entsize =3D elf_xword_to_cpu(shdr.sh_entsize); + sec->shdr.sh_name =3D elf_word_to_cpu(shdr->sh_name); + sec->shdr.sh_type =3D elf_word_to_cpu(shdr->sh_type); + sec->shdr.sh_flags =3D elf_xword_to_cpu(shdr->sh_flags); + sec->shdr.sh_addr =3D elf_addr_to_cpu(shdr->sh_addr); + sec->shdr.sh_offset =3D elf_off_to_cpu(shdr->sh_offset); + sec->shdr.sh_size =3D elf_xword_to_cpu(shdr->sh_size); + sec->shdr.sh_link =3D elf_word_to_cpu(shdr->sh_link); + sec->shdr.sh_info =3D elf_word_to_cpu(shdr->sh_info); + sec->shdr.sh_addralign =3D elf_xword_to_cpu(shdr->sh_addralign); + sec->shdr.sh_entsize =3D elf_xword_to_cpu(shdr->sh_entsize); if (sec->shdr.sh_link < shnum) sec->link =3D &secs[sec->shdr.sh_link]; } =20 } =20 -static void read_strtabs(FILE *fp) +static void read_strtabs(void) { int i; =20 @@ -476,20 +465,11 @@ static void read_strtabs(FILE *fp) =20 if (sec->shdr.sh_type !=3D SHT_STRTAB) continue; - - sec->strtab =3D malloc(sec->shdr.sh_size); - if (!sec->strtab) - die("malloc of %" FMT " bytes for strtab failed\n", sec->shdr.sh_size); - - if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) - die("Seek to %" FMT " failed: %s\n", sec->shdr.sh_offset, strerror(errn= o)); - - if (fread(sec->strtab, 1, sec->shdr.sh_size, fp) !=3D sec->shdr.sh_size) - die("Cannot read symbol table: %s\n", strerror(errno)); + sec->strtab =3D elf_image + sec->shdr.sh_offset; } } =20 -static void read_symtabs(FILE *fp) +static void read_symtabs(void) { int i, j; =20 @@ -499,16 +479,7 @@ static void read_symtabs(FILE *fp) =20 switch (sec->shdr.sh_type) { case SHT_SYMTAB_SHNDX: - sec->xsymtab =3D malloc(sec->shdr.sh_size); - if (!sec->xsymtab) - die("malloc of %" FMT " bytes for xsymtab failed\n", sec->shdr.sh_size= ); - - if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) - die("Seek to %" FMT " failed: %s\n", sec->shdr.sh_offset, strerror(err= no)); - - if (fread(sec->xsymtab, 1, sec->shdr.sh_size, fp) !=3D sec->shdr.sh_siz= e) - die("Cannot read extended symbol table: %s\n", strerror(errno)); - + sec->xsymtab =3D elf_image + sec->shdr.sh_offset; shxsymtabndx =3D i; continue; =20 @@ -519,11 +490,7 @@ static void read_symtabs(FILE *fp) if (!sec->symtab) die("malloc of %" FMT " bytes for symtab failed\n", sec->shdr.sh_size); =20 - if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) - die("Seek to %" FMT " failed: %s\n", sec->shdr.sh_offset, strerror(err= no)); - - if (fread(sec->symtab, 1, sec->shdr.sh_size, fp) !=3D sec->shdr.sh_size) - die("Cannot read symbol table: %s\n", strerror(errno)); + memcpy(sec->symtab, elf_image + sec->shdr.sh_offset, sec->shdr.sh_size); =20 for (j =3D 0; j < num_syms; j++) { Elf_Sym *sym =3D &sec->symtab[j]; @@ -543,12 +510,13 @@ static void read_symtabs(FILE *fp) } =20 =20 -static void read_relocs(FILE *fp) +static void read_relocs(void) { int i, j; =20 for (i =3D 0; i < shnum; i++) { struct section *sec =3D &secs[i]; + const Elf_Rel *reltab =3D elf_image + sec->shdr.sh_offset; =20 if (sec->shdr.sh_type !=3D SHT_REL_TYPE) continue; @@ -557,19 +525,12 @@ static void read_relocs(FILE *fp) if (!sec->reltab) die("malloc of %" FMT " bytes for relocs failed\n", sec->shdr.sh_size); =20 - if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) - die("Seek to %" FMT " failed: %s\n", sec->shdr.sh_offset, strerror(errn= o)); - - if (fread(sec->reltab, 1, sec->shdr.sh_size, fp) !=3D sec->shdr.sh_size) - die("Cannot read symbol table: %s\n", strerror(errno)); - for (j =3D 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) { Elf_Rel *rel =3D &sec->reltab[j]; - - rel->r_offset =3D elf_addr_to_cpu(rel->r_offset); - rel->r_info =3D elf_xword_to_cpu(rel->r_info); + rel->r_offset =3D elf_addr_to_cpu(reltab[j].r_offset); + rel->r_info =3D elf_xword_to_cpu(reltab[j].r_info); #if (SHT_REL_TYPE =3D=3D SHT_RELA) - rel->r_addend =3D elf_xword_to_cpu(rel->r_addend); + rel->r_addend =3D elf_xword_to_cpu(reltab[j].r_addend); #endif } } @@ -591,7 +552,7 @@ static void print_absolute_symbols(void) =20 for (i =3D 0; i < shnum; i++) { struct section *sec =3D &secs[i]; - char *sym_strtab; + const char *sym_strtab; int j; =20 if (sec->shdr.sh_type !=3D SHT_SYMTAB) @@ -633,7 +594,7 @@ static void print_absolute_relocs(void) for (i =3D 0; i < shnum; i++) { struct section *sec =3D &secs[i]; struct section *sec_applies, *sec_symtab; - char *sym_strtab; + const char *sym_strtab; Elf_Sym *sh_symtab; int j; =20 @@ -725,7 +686,7 @@ static void walk_relocs(int (*process)(struct section *= sec, Elf_Rel *rel, =20 /* Walk through the relocations */ for (i =3D 0; i < shnum; i++) { - char *sym_strtab; + const char *sym_strtab; Elf_Sym *sh_symtab; struct section *sec_applies, *sec_symtab; int j; @@ -1177,12 +1138,24 @@ void process(FILE *fp, int use_real_mode, int as_te= xt, int show_absolute_syms, int show_absolute_relocs, int show_reloc_info) { + int fd =3D fileno(fp); + struct stat sb; + void *p; + + if (fstat(fd, &sb)) + die("fstat() failed\n"); + + elf_image =3D p =3D mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + if (p =3D=3D MAP_FAILED) + die("mmap() failed\n"); + regex_init(use_real_mode); - read_ehdr(fp); - read_shdrs(fp); - read_strtabs(fp); - read_symtabs(fp); - read_relocs(fp); + + read_ehdr(); + read_shdrs(); + read_strtabs(); + read_symtabs(); + read_relocs(); =20 if (ELF_BITS =3D=3D 64) percpu_init(); @@ -1203,4 +1176,6 @@ void process(FILE *fp, int use_real_mode, int as_text, } =20 emit_relocs(as_text, use_real_mode); + + munmap(p, sb.st_size); } diff --git a/arch/x86/tools/relocs.h b/arch/x86/tools/relocs.h index 4c49c82446eb..7a509604ff92 100644 --- a/arch/x86/tools/relocs.h +++ b/arch/x86/tools/relocs.h @@ -16,6 +16,8 @@ #include #include #include +#include +#include =20 __attribute__((__format__(printf, 1, 2))) void die(char *fmt, ...) __attribute__((noreturn)); --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:14 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7599417A938 for ; Wed, 25 Sep 2024 15:02:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276524; cv=none; b=KXyv9Erz0NWTH/1KyTz0n9vh2roB84iFH9cHrC5SQFuTauYWQ00fXWoo7NrIsXf1bWi8qVNFamby4wIp3k69p7R6ToEmR3XNgnZzcEt/xDPt12SjTa8iRyHnSUk1fLpztutmeJzcCtMoKXNhXJDC5AuC52piJSCexJZzjmhAaSQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276524; c=relaxed/simple; bh=9IeQ5D389OP3YsIvppVdXgRHsSH2R2gh2yLL82bpmwU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bAVQaI3iWfEhjrCLFBMlw/FtcDt01ZT+FkVsZxch4OIry9y09iLJT9sLGA6CjE6SWz5Kpt7G1igAxwqijwRFdYiMdY8BmmelVz0qN75rS9NB2ceb03I3vUSkXYVD3QhD4CHR8eZPEZuSWPzA7irswb5XcVRcSiL05cbeh/oyPEc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=fD083jGZ; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fD083jGZ" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6d4f52a4069so118665157b3.3 for ; Wed, 25 Sep 2024 08:02:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276520; x=1727881320; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=DA1xJ7QlyinRe20ykWBw1TGfZPAZZ2L4ow9I41mAh9Y=; b=fD083jGZz5kclsqEGn0PeXYqDSpGLWOYSTCBb8UAAsRPYt9jF3ZHXRYkabS7r5p0PA zvrYXsm1ABv3UhphJZ73GbBTQBMwKVmgv/xaOswaVzaJTp74idKtxPJPUhWKfkdVD1V4 5H3BVHpyBN2A2YOhjdjT5bTCZaHh2WTossr029pVLDkZoqCc5HoWGkkG9/UqdRcwQCAP Q4nvwkc+Yk9pRZO9zVsotNFXOCQsvyRQykfBSqsjR6lMT/WpX0ZyIiiD7H1qshH3klIC h91KxOdk5sAPFbd3CDMEvK76txrNKLWUkhQlfQsgCEfv6NPfOuy+mJHCnxs9POxEdn3c 83OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276520; x=1727881320; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DA1xJ7QlyinRe20ykWBw1TGfZPAZZ2L4ow9I41mAh9Y=; b=ihC8x/NZdgpu//uAxzVBUWJvtrl6qgra868bBfGY+b7ZBnQdGohvj4aeOSls1Og+AP PcEpfZ4AVSdRzSzrhzskFMJIcUI3d5UA1LJLnhsIIVIDJwQCy++wLnMBGrqJeCJa8rBk LD/CLMh9YM7LjkhROwMNwQ+8L4NEPi2LvG2jMWvWVjMIds6MggNwPEA/F0rLOG2IOQJ6 4dmugIhZZpyxmEuOTqfTil6XKwi6G0XmbHBlbd0GnkW7xlMeIWD8InxCjW/pA+oD/Bad KH51315+4WR/UnNcwGbAySphINaPIVX9AUg8lSLfoJhQdheO/9ZbE2RfF60dN6OHzSRm +20A== X-Gm-Message-State: AOJu0YwI+7qw7XtEZQx/h7eKVcVPhG0766MhLrUUMDZT+r2WnH0/QVwO 5ZaObI2ISucM71sIQBlxZT6AB/7Zreurg8ECshjIPGQ8XDNSGXBgRyizLmRf0TgG1giJqxlHRI9 Hb/Ibi2VuQbI/aIp9hmskvX7dGQ9hHD+dATDpAW0v6cVFI8nttKHSFeGLyBqepCydffRTdIN0dQ fk2qr0jXD/nFP2WxcLQqUsCmQZpUYFsw== X-Google-Smtp-Source: AGHT+IEVIf0RcL61TjfNJbSKcMrFRGT1L5gz2hG55UguzadiqCTtHCs2NniTQT5w6cgRQkWXw9cPp9k2 X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a81:77c4:0:b0:61c:89a4:dd5f with SMTP id 00721157ae682-6e21d0e8b05mr227547b3.0.1727276519796; Wed, 25 Sep 2024 08:01:59 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:04 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=6485; i=ardb@kernel.org; h=from:subject; bh=xSyMofrsI8W/QeJfcidSH2VUWxFZ3VEJOcPkUtiBlzY=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6sYKk4m5xZLyVelenm+8zuTMnXPA0faQXMTl3XV33 iYnS2/oKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABMRz2b4p2z7TbK64+yiCfEm LE6n5/75udhiw87M0k896bfk2oo/BTL8U20uvRe5bfeVZc9kGMJfb1kvfTNqJu/W1Zs+sddu+hR pywYA X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-34-ardb+git@google.com> Subject: [RFC PATCH 04/28] x86/boot: Permit GOTPCREL relocations for x86_64 builds From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Some of the early x86_64 startup code is written in C, and executes in the early 1:1 mapping of the kernel, which is not the address it was linked at, and this requires special care when accessing global variables. This is currently being dealt with on an ad-hoc basis, primarily in head64.c, using explicit pointer fixups, but it would be better to rely on the compiler for this, by using -fPIE to generate code that can run at any address, and uses RIP-relative accesses to refer to global variables. While it is possible to avoid most GOT based symbol references that the compiler typically emits when running in -fPIE mode, by using 'hidden' visibility, there are cases where the compiler will always rely on the GOT, for instance, for weak external references (which may remain unsatisfied at link time). This means the build may produce a small number of GOT entries nonetheless. So update the reloc processing host tool to add support for this, and place the GOT in the .text section rather than discard it. Note that multiple GOT based references to the same symbol will share a single GOT entry, and so naively emitting a relocation for the GOT entry each time a reference to it is encountered could result in duplicates. Work around this by relying on the fact that the relocation lists are sorted, and deduplicate 64-bit relocations as they are emitted by comparing each entry with the previous one. Signed-off-by: Ard Biesheuvel --- arch/x86/Makefile | 4 +++ arch/x86/kernel/vmlinux.lds.S | 5 +++ arch/x86/tools/relocs.c | 33 ++++++++++++++++++-- include/asm-generic/vmlinux.lds.h | 7 +++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/arch/x86/Makefile b/arch/x86/Makefile index 801fd85c3ef6..6b3fe6e2aadd 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -192,6 +192,10 @@ else KBUILD_CFLAGS +=3D -mcmodel=3Dkernel KBUILD_RUSTFLAGS +=3D -Cno-redzone=3Dy KBUILD_RUSTFLAGS +=3D -Ccode-model=3Dkernel + + # Don't emit relaxable GOTPCREL relocations + KBUILD_AFLAGS_KERNEL +=3D -Wa,-mrelax-relocations=3Dno + KBUILD_CFLAGS_KERNEL +=3D -Wa,-mrelax-relocations=3Dno endif =20 # diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S index 6e73403e874f..7f060d873f75 100644 --- a/arch/x86/kernel/vmlinux.lds.S +++ b/arch/x86/kernel/vmlinux.lds.S @@ -20,6 +20,9 @@ #define RUNTIME_DISCARD_EXIT #define EMITS_PT_NOTE #define RO_EXCEPTION_TABLE_ALIGN 16 +#ifdef CONFIG_X86_64 +#define GOT_IN_RODATA +#endif =20 #include #include @@ -464,10 +467,12 @@ SECTIONS * Sections that should stay zero sized, which is safer to * explicitly check instead of blindly discarding. */ +#ifdef CONFIG_X86_32 .got : { *(.got) *(.igot.*) } ASSERT(SIZEOF(.got) =3D=3D 0, "Unexpected GOT entries detected!") +#endif =20 .plt : { *(.plt) *(.plt.*) *(.iplt) diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c index 35a73e4aa74d..880f0f2e465e 100644 --- a/arch/x86/tools/relocs.c +++ b/arch/x86/tools/relocs.c @@ -223,6 +223,8 @@ static const char *rel_type(unsigned type) REL_TYPE(R_X86_64_JUMP_SLOT), REL_TYPE(R_X86_64_RELATIVE), REL_TYPE(R_X86_64_GOTPCREL), + REL_TYPE(R_X86_64_GOTPCRELX), + REL_TYPE(R_X86_64_REX_GOTPCRELX), REL_TYPE(R_X86_64_32), REL_TYPE(R_X86_64_32S), REL_TYPE(R_X86_64_16), @@ -843,6 +845,7 @@ static int do_reloc64(struct section *sec, Elf_Rel *rel= , ElfW(Sym) *sym, case R_X86_64_32: case R_X86_64_32S: case R_X86_64_64: + case R_X86_64_GOTPCREL: /* * References to the percpu area don't need to be adjusted. */ @@ -861,6 +864,31 @@ static int do_reloc64(struct section *sec, Elf_Rel *re= l, ElfW(Sym) *sym, break; } =20 + if (r_type =3D=3D R_X86_64_GOTPCREL) { + Elf_Shdr *s =3D &secs[sec->shdr.sh_info].shdr; + unsigned file_off =3D offset - s->sh_addr + s->sh_offset; + + /* + * GOTPCREL relocations refer to instructions that load + * a 64-bit address via a 32-bit relative reference to + * the GOT. In this case, it is the GOT entry that + * needs to be fixed up, not the immediate offset in + * the opcode. Note that the linker will have applied an + * addend of -4 to compensate for the delta between the + * relocation offset and the value of RIP when the + * instruction executes, and this needs to be backed out + * again. (Addends other than -4 are permitted in + * principle, but make no sense in practice so they are + * not supported.) + */ + if (rel->r_addend !=3D -4) { + die("invalid addend (%ld) for %s relocation: %s\n", + rel->r_addend, rel_type(r_type), symname); + break; + } + offset +=3D 4 + (int32_t)get_unaligned_le32(elf_image + file_off); + } + /* * Relocation offsets for 64 bit kernels are output * as 32 bits and sign extended back to 64 bits when @@ -870,7 +898,7 @@ static int do_reloc64(struct section *sec, Elf_Rel *rel= , ElfW(Sym) *sym, if ((int32_t)offset !=3D (int64_t)offset) die("Relocation offset doesn't fit in 32 bits\n"); =20 - if (r_type =3D=3D R_X86_64_64) + if (r_type =3D=3D R_X86_64_64 || r_type =3D=3D R_X86_64_GOTPCREL) add_reloc(&relocs64, offset); else add_reloc(&relocs32, offset); @@ -1085,7 +1113,8 @@ static void emit_relocs(int as_text, int use_real_mod= e) =20 /* Now print each relocation */ for (i =3D 0; i < relocs64.count; i++) - write_reloc(relocs64.offset[i], stdout); + if (!i || relocs64.offset[i] !=3D relocs64.offset[i - 1]) + write_reloc(relocs64.offset[i], stdout); =20 /* Print a stop */ write_reloc(0, stdout); diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinu= x.lds.h index 19ec49a9179b..cc14d780c70d 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -443,6 +443,12 @@ #endif #endif =20 +#ifdef GOT_IN_RODATA +#define GOT_RODATA *(.got .igot*) +#else +#define GOT_RODATA +#endif + /* * Read only Data */ @@ -454,6 +460,7 @@ SCHED_DATA \ RO_AFTER_INIT_DATA /* Read only after init */ \ . =3D ALIGN(8); \ + GOT_RODATA \ BOUNDED_SECTION_BY(__tracepoints_ptrs, ___tracepoints_ptrs) \ *(__tracepoints_strings)/* Tracepoints: strings */ \ } \ --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:14 2024 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0CBFF1885A5 for ; Wed, 25 Sep 2024 15:02:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276526; cv=none; b=mpcHC+WlaKsVb7Fs0BswaU7TzeCTKIqDRea0yhBLWPDxLq/jSfofspFgw5CHvOR0LfgUEb+YXkLonJ2/ZCnaaxwAuQMxmf3QsMBcdDjH3yBSbnDIS4wuOmDUEFVqJtZbEzJvqbmi6MzQdAvGmoy9lyqHOwDCAqW5ng+C134bV64= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276526; c=relaxed/simple; bh=E/ekk1rehkbBpUh1L7h/NGwV62IOjSTKuZGPf/08C+4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OfJhHnin7uEAyBuOQbvwQ4GgkDgVq202YwRmJzLFGS//ee9DjRLp7OLSeVlbRK3QoDLwVC56AVedRL+F38lolRwNnIh4fSkHePu87I9IiwSx1gAqLJNZa98RgbLGzXHPlIWXl7mz6wXXsWrp0skwIalxJkdqoCYTkrFhlMrQTrY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sj/q/3WK; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sj/q/3WK" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e1cfb9d655eso9909389276.0 for ; Wed, 25 Sep 2024 08:02:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276523; x=1727881323; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=hcBJaG4CjYcevMfAnFyKwMdAr3oBLta0TuXom4JmDi8=; b=sj/q/3WKh2z2J3OB4jdZX0BVnn07XN/OKZUh9e/CEaTgBKQ0h4A5YWJO7wFI3Mkry9 btCnBHsNz2FwarAYEB1zI+QH4SsROutSKZNu+iSzTXAfFhI63/UbtTPOwichv7wSjshu 14S4PVuxcjXMWPQ22OO0oQS26x5GyEWUFeUUSjSbTP8uKcI/9B/UKlqOC5h0MqjRvB2m xWMj3Npds1z0wVij2q5OsQQRkkZX0gBFV7d8JeSpjT+XpZ5Ua7PhW3pYrj3HHyuH7oK2 mGWb/jZ/wbOPFKhfvZsLewnYXhaH/yE4fUQVkkT0lgpp502ijjlh69ldK+vEYZH3cd2W UdEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276523; x=1727881323; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hcBJaG4CjYcevMfAnFyKwMdAr3oBLta0TuXom4JmDi8=; b=ZaH52+Lq13GGG1gD3MwlmNXIHWubldkICFE+DLKpea3ih15AFaycICtEOQgOSP+2jI Jk2Tfg1V9hogltJv1TF8/9a7GcY21J6/1LFMQMBYnWD3uAOfgnlb2wKKPjUXTEZGowDn D28W3J6pXOUPUvPUSB/CfSd46EUTjGmQEpmNkn6XkRG/UOMtOjcroC2YnJzIRZ0+krCh 7Bi0jshUKgCUyJ5+sGiempxnfb8ZdxBmSDvN9DWy9KSbZpI4vE6MoKmbhIo8eCJSVdro o9rIDMMergzN/oPvROdbOJtnJYtRMkYKH4noGbzyP5UGMnPQEFmf2rbyqiuTyv1eOji8 BBzQ== X-Gm-Message-State: AOJu0Yzm/pidlh4GVs/yMxAMchC/OiWoe+q6M2+A3mG14l3vksHP+Fbg Nq3xqnZ4W0e3IAudG1YKEwWturdd5V/AxrNllGaOg+Lt/LFCaQgfKErniwXzfIohmYdweRED0W9 5iW3HVe69S/sfST2lUGJbUD1w98Nrzo0KrISnhzZcSsYBclTHJxG4Jex0ekiUe2pyhGWJ8hRRRp en0lcvnV1OVbQSz8cVJ3q9/EX+mC4hzQ== X-Google-Smtp-Source: AGHT+IHVPP/hq0VdoG8mz8zZDpKENYQvppd12tQOBs96+ky0ZuGKlW9zxaw1GBiDRtGEwfqJMJeN7cfB X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a25:d695:0:b0:e1a:70ed:6ec9 with SMTP id 3f1490d57ef6-e24d7352785mr29529276.2.1727276522332; Wed, 25 Sep 2024 08:02:02 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:05 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=4340; i=ardb@kernel.org; h=from:subject; bh=I7lWAHThqqqqnrpiqIQsu32XeIcDrKE8EBFU/14ypJ4=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6manxvx6ho28axaYV4atC9wvG5Igfbri2+G3gWYqn maGuz52lLIwiHEwyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgIl4nmVkaA1czF6Uafzbsi92 efGhdw7lev3pD99ze/Rc9uE4U+u6lZHhb2nzhL8Z7seME5YcX8rFdHZX6uJJe7jF87S7K1vP1Mh zAQA= X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-35-ardb+git@google.com> Subject: [RFC PATCH 05/28] x86: Define the stack protector guard symbol explicitly From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Specify the guard symbol for the stack cookie explicitly, rather than positioning it exactly 40 bytes into the per-CPU area. Doing so removes the need for the per-CPU region to be absolute rather than relative to the placement of the per-CPU template region in the kernel image, and this allows the special handling for absolute per-CPU symbols to be removed entirely. This is a worthwhile cleanup in itself, but it is also a prerequisite for PIE codegen and PIE linking, which can replace our bespoke and rather clunky runtime relocation handling. Signed-off-by: Ard Biesheuvel --- arch/x86/Makefile | 4 ++++ arch/x86/include/asm/init.h | 2 +- arch/x86/include/asm/processor.h | 11 +++-------- arch/x86/include/asm/stackprotector.h | 4 ---- tools/perf/util/annotate.c | 4 ++-- 5 files changed, 10 insertions(+), 15 deletions(-) diff --git a/arch/x86/Makefile b/arch/x86/Makefile index 6b3fe6e2aadd..b78b7623a4a9 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -193,6 +193,10 @@ else KBUILD_RUSTFLAGS +=3D -Cno-redzone=3Dy KBUILD_RUSTFLAGS +=3D -Ccode-model=3Dkernel =20 + ifeq ($(CONFIG_STACKPROTECTOR),y) + KBUILD_CFLAGS +=3D -mstack-protector-guard-symbol=3Dfixed_= percpu_data + endif + # Don't emit relaxable GOTPCREL relocations KBUILD_AFLAGS_KERNEL +=3D -Wa,-mrelax-relocations=3Dno KBUILD_CFLAGS_KERNEL +=3D -Wa,-mrelax-relocations=3Dno diff --git a/arch/x86/include/asm/init.h b/arch/x86/include/asm/init.h index 14d72727d7ee..3ed0e8ec973f 100644 --- a/arch/x86/include/asm/init.h +++ b/arch/x86/include/asm/init.h @@ -2,7 +2,7 @@ #ifndef _ASM_X86_INIT_H #define _ASM_X86_INIT_H =20 -#define __head __section(".head.text") +#define __head __section(".head.text") __no_stack_protector =20 struct x86_mapping_info { void *(*alloc_pgt_page)(void *); /* allocate buf for page table */ diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/proces= sor.h index 4a686f0e5dbf..56bc36116814 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -402,14 +402,9 @@ struct irq_stack { #ifdef CONFIG_X86_64 struct fixed_percpu_data { /* - * GCC hardcodes the stack canary as %gs:40. Since the - * irq_stack is the object at %gs:0, we reserve the bottom - * 48 bytes of the irq stack for the canary. - * - * Once we are willing to require -mstack-protector-guard-symbol=3D - * support for x86_64 stackprotector, we can get rid of this. + * Since the irq_stack is the object at %gs:0, the bottom 8 bytes of + * the irq stack are reserved for the canary. */ - char gs_base[40]; unsigned long stack_canary; }; =20 @@ -418,7 +413,7 @@ DECLARE_INIT_PER_CPU(fixed_percpu_data); =20 static inline unsigned long cpu_kernelmode_gs_base(int cpu) { - return (unsigned long)per_cpu(fixed_percpu_data.gs_base, cpu); + return (unsigned long)&per_cpu(fixed_percpu_data, cpu); } =20 extern asmlinkage void entry_SYSCALL32_ignore(void); diff --git a/arch/x86/include/asm/stackprotector.h b/arch/x86/include/asm/s= tackprotector.h index 00473a650f51..d1dcd22a0a4c 100644 --- a/arch/x86/include/asm/stackprotector.h +++ b/arch/x86/include/asm/stackprotector.h @@ -51,10 +51,6 @@ static __always_inline void boot_init_stack_canary(void) { unsigned long canary =3D get_random_canary(); =20 -#ifdef CONFIG_X86_64 - BUILD_BUG_ON(offsetof(struct fixed_percpu_data, stack_canary) !=3D 40); -#endif - current->stack_canary =3D canary; #ifdef CONFIG_X86_64 this_cpu_write(fixed_percpu_data.stack_canary, canary); diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 37ce43c4eb8f..7ecfedf5edb9 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -2485,10 +2485,10 @@ static bool is_stack_operation(struct arch *arch, s= truct disasm_line *dl) =20 static bool is_stack_canary(struct arch *arch, struct annotated_op_loc *lo= c) { - /* On x86_64, %gs:40 is used for stack canary */ + /* On x86_64, %gs:0 is used for stack canary */ if (arch__is(arch, "x86")) { if (loc->segment =3D=3D INSN_SEG_X86_GS && loc->imm && - loc->offset =3D=3D 40) + loc->offset =3D=3D 0) return true; } =20 --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:14 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD89818A6C3 for ; Wed, 25 Sep 2024 15:02:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276529; cv=none; b=WQUOQtEklNml1X/IOfWUfCqxd9FEPqnGn+hoy44pN+wCcpl3qXGg7bVwZmSXyPQFgZjN0M1vIs52hM+cvWCU8xbgZPHDgsv3rLAVODW0x5GQt+GVQu1eh1ZUIimqkF9Vk0ZD53VdVwzuvY448YtZCkI/J/Z/PQoUSXKPKXiYang= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276529; c=relaxed/simple; bh=J79MxP7lqCLqptP8R42gKw+nj8+FT+gfetDjKcKkNS8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WIJIyvwdJ963vSWUdWKLGxqQk72UDzG3KD0if9VTKThPyhokcuifXdFBwk25ILfcAVPFliGP0relMM3n2bDdVUs4R67ZniZgN+UhZ/KzsMB0EwKo1Bt/EanfhiHlQ+LF7B4FPyxhngIznjXViejRVJGjaANQlVkCiXLRwpDHLrs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=f6dMyRxK; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="f6dMyRxK" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e1d46cee0b0so10267021276.2 for ; Wed, 25 Sep 2024 08:02:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276525; x=1727881325; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=eDSZAaST0F7o2pedlrN6lklBaOasvLDlFvV5LwnRCP8=; b=f6dMyRxKEh/badlNRLcmlg18jSpjvrDmYWxQ7EYbyWsfM9GAk/9cJfeqtthFBSO2QE tmr4Y8dW+RZ7AI2xlbBYV8GQJ0M5ZCm/1FXm9nQl+xGG38Gk72HRI81G0Vx6QxYBg522 cZt8fvb3jQBfwNVQaPg0Lw9QLYK7r0CUkbgNNCVhv2U5L5o1t8Lto/ATXHiV4KdnTizz 7CFRUdGaQKLn6Q+RoVRdtx4B8nRG1c80c7oVtSc+3pvLZHWdkR+HrdPBusieBrnsltZE WkbzMUMLLLSx0FLzA7ZkhwfzxmUWJuCTHzkzMUXD2IaA1o6jwe5GYsOKg2AIFgU5sD2t FVrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276525; x=1727881325; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eDSZAaST0F7o2pedlrN6lklBaOasvLDlFvV5LwnRCP8=; b=vil7p6lD4QqIbEFbVKXxf6D9fRdAbtYXMvIxIDAGoW0qcrCE2O3h7q1mK1juR5mHjX HYvoj1LH4e9N92M0tBUNlEpsSrRHyyeyXRz5W37t4NTmas815DqUHSiCjkOjOTij5sPL iKQyfvISN8BwQaWxZ+1jQPCXKWCK9WBWfSDvvF+4aWt/x3RgRoKC89OLC8QOgNCnkbrh mOoAcd+fexFTsxZLQH9nVUW5O8cww9GBsZ8UkLdFKQyX5lKvco7PEMrhP1MLoTDnB6pN l94OtorNXVPZRzCsopvnpB3UkpLNfilp9iPguzjiuZPlPu8q7NBOLqr6MXWr1jHvCwnJ LW2Q== X-Gm-Message-State: AOJu0YysFoyCLcZ2Gh2ngDnJyvASf5HOGadMAg0MpzeEbyvyUkyydnWb ntmxAuNAS0HkqBeDrsEjj8gDsxDYHgyRW4M0kZOND87w1ZM0uBzp+CrcdcUBKR1JpuHLzX39Dxs N4RaswbhYkJSVDxD2hjpacNzO1oGmJ3p4B7KP0PwMd2Qkdd1dFPW2V3lFLuVJrU3tqT7A9TMq2M VbiYmb0D7xLmO1Udo87ZYV86xQJN4HVA== X-Google-Smtp-Source: AGHT+IFVWEnfWJexNr1qxNhJJA6KdWYKxMoVe8Dw+8wG1wSOr4X5mOwe9MyKjjmZekWIuFcjH9KvW3oI X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:6902:1782:b0:e0b:958a:3344 with SMTP id 3f1490d57ef6-e24da39b0c3mr17932276.10.1727276524782; Wed, 25 Sep 2024 08:02:04 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:06 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=12230; i=ardb@kernel.org; h=from:subject; bh=d8zrHLLRsaJb8rjIFgQ7EHO+u22pN8JK1vGiiuXj4lQ=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6pZHteaXk89IXLoXMatcXipz+8w88ZqbTc8vrDt5e 99Nu9mbO0pZGMQ4GGTFFFkEZv99t/P0RKla51myMHNYmUCGMHBxCsBE3LsZ/oo9TvHP2+umfelZ ulBlzIGvqg9nBs08XVmsmMPN+vzurvOMDNcdmH9HcnIw6q7iylwgUHny+rJVkzYJbJk791Zkl/v XRQwA X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-36-ardb+git@google.com> Subject: [RFC PATCH 06/28] x86/percpu: Get rid of absolute per-CPU variable placement From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel For historic reasons, per-CPU symbols on x86_64 are emitted in an address space that is disjoint from the ordinary kernel VA space, starting at address 0x0. This splits a per-CPU symbol reference into a base plus offset, where the base is programmed into the GS segment register. This deviates from the usual approach adopted by other SMP architectures, where the base is a reference to the variable in the kernel image's per-CPU template area, and the offset is a per-CPU value accounting for the displacement of that particular CPU's per-CPU region with respect to the template area. This gives per-CPU variable references a range that is identical to ordinary references, and requires no special handling for the startup code, as the offset will simply be 0x0 up until the point where per-CPU variables are initialized properly. The x86_64 approach was needed to accommodate per-task stack protector cookies, which used to live at a fixed offset of GS+40, requiring GS to be treated as a base register. This is no longer the case, though, and so GS can be repurposed as a true per-CPU offset, adopting the same strategy as other architectures. This also removes the need for linker tricks to emit the per-CPU ELF segment at a different virtual address. It also means RIP-relative per-CPU variables no longer need to be relocated in the opposite direction when KASLR is applied, which was necessary because the 0x0 based per-CPU region remains in place even when the kernel is moved around. Signed-off-by: Ard Biesheuvel Reviewed-by: Christoph Lameter --- arch/x86/include/asm/desc.h | 1 - arch/x86/include/asm/percpu.h | 22 -------------- arch/x86/include/asm/processor.h | 5 ++-- arch/x86/kernel/head64.c | 2 +- arch/x86/kernel/head_64.S | 12 ++------ arch/x86/kernel/irq_64.c | 1 - arch/x86/kernel/setup_percpu.c | 9 +----- arch/x86/kernel/vmlinux.lds.S | 30 -------------------- arch/x86/platform/pvh/head.S | 6 ++-- arch/x86/tools/relocs.c | 8 +----- arch/x86/xen/xen-head.S | 10 ++----- init/Kconfig | 1 - 12 files changed, 13 insertions(+), 94 deletions(-) diff --git a/arch/x86/include/asm/desc.h b/arch/x86/include/asm/desc.h index 62dc9f59ea76..ec95fe44fa3a 100644 --- a/arch/x86/include/asm/desc.h +++ b/arch/x86/include/asm/desc.h @@ -46,7 +46,6 @@ struct gdt_page { } __attribute__((aligned(PAGE_SIZE))); =20 DECLARE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page); -DECLARE_INIT_PER_CPU(gdt_page); =20 /* Provide the original GDT */ static inline struct desc_struct *get_cpu_gdt_rw(unsigned int cpu) diff --git a/arch/x86/include/asm/percpu.h b/arch/x86/include/asm/percpu.h index c55a79d5feae..1ded1207528d 100644 --- a/arch/x86/include/asm/percpu.h +++ b/arch/x86/include/asm/percpu.h @@ -20,12 +20,6 @@ =20 #define PER_CPU_VAR(var) __percpu(var)__percpu_rel =20 -#ifdef CONFIG_X86_64_SMP -# define INIT_PER_CPU_VAR(var) init_per_cpu__##var -#else -# define INIT_PER_CPU_VAR(var) var -#endif - #else /* !__ASSEMBLY__: */ =20 #include @@ -97,22 +91,6 @@ #define __percpu_arg(x) __percpu_prefix "%" #x #define __force_percpu_arg(x) __force_percpu_prefix "%" #x =20 -/* - * Initialized pointers to per-CPU variables needed for the boot - * processor need to use these macros to get the proper address - * offset from __per_cpu_load on SMP. - * - * There also must be an entry in vmlinux_64.lds.S - */ -#define DECLARE_INIT_PER_CPU(var) \ - extern typeof(var) init_per_cpu_var(var) - -#ifdef CONFIG_X86_64_SMP -# define init_per_cpu_var(var) init_per_cpu__##var -#else -# define init_per_cpu_var(var) var -#endif - /* * For arch-specific code, we can use direct single-insn ops (they * don't give an lvalue though). diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/proces= sor.h index 56bc36116814..d7219e149f24 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -409,11 +409,12 @@ struct fixed_percpu_data { }; =20 DECLARE_PER_CPU_FIRST(struct fixed_percpu_data, fixed_percpu_data) __visib= le; -DECLARE_INIT_PER_CPU(fixed_percpu_data); =20 static inline unsigned long cpu_kernelmode_gs_base(int cpu) { - return (unsigned long)&per_cpu(fixed_percpu_data, cpu); + extern unsigned long __per_cpu_offset[]; + + return IS_ENABLED(CONFIG_SMP) ? __per_cpu_offset[cpu] : 0; } =20 extern asmlinkage void entry_SYSCALL32_ignore(void); diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c index 4b9d4557fc94..d4398261ad81 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -559,7 +559,7 @@ void early_setup_idt(void) */ void __head startup_64_setup_gdt_idt(void) { - struct desc_struct *gdt =3D (void *)(__force unsigned long)init_per_cpu_v= ar(gdt_page.gdt); + struct desc_struct *gdt =3D (void *)(__force unsigned long)gdt_page.gdt; void *handler =3D NULL; =20 struct desc_ptr startup_gdt_descr =3D { diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index 330922b328bf..ab6ccee81493 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -68,11 +68,10 @@ SYM_CODE_START_NOALIGN(startup_64) /* Set up the stack for verify_cpu() */ leaq __top_init_kernel_stack(%rip), %rsp =20 - /* Setup GSBASE to allow stack canary access for C code */ + /* Clear %gs so early per-CPU references target the per-CPU load area */ movl $MSR_GS_BASE, %ecx - leaq INIT_PER_CPU_VAR(fixed_percpu_data)(%rip), %rdx - movl %edx, %eax - shrq $32, %rdx + xorl %eax, %eax + cdq wrmsr =20 call startup_64_setup_gdt_idt @@ -361,15 +360,10 @@ SYM_INNER_LABEL(common_startup_64, SYM_L_LOCAL) =20 /* Set up %gs. * - * The base of %gs always points to fixed_percpu_data. If the - * stack protector canary is enabled, it is located at %gs:40. * Note that, on SMP, the boot cpu uses init data section until * the per cpu areas are set up. */ movl $MSR_GS_BASE,%ecx -#ifndef CONFIG_SMP - leaq INIT_PER_CPU_VAR(fixed_percpu_data)(%rip), %rdx -#endif movl %edx, %eax shrq $32, %rdx wrmsr diff --git a/arch/x86/kernel/irq_64.c b/arch/x86/kernel/irq_64.c index ade0043ce56e..56bdeecd8ee0 100644 --- a/arch/x86/kernel/irq_64.c +++ b/arch/x86/kernel/irq_64.c @@ -27,7 +27,6 @@ #include =20 DEFINE_PER_CPU_PAGE_ALIGNED(struct irq_stack, irq_stack_backing_store) __v= isible; -DECLARE_INIT_PER_CPU(irq_stack_backing_store); =20 #ifdef CONFIG_VMAP_STACK /* diff --git a/arch/x86/kernel/setup_percpu.c b/arch/x86/kernel/setup_percpu.c index b30d6e180df7..57482420ff42 100644 --- a/arch/x86/kernel/setup_percpu.c +++ b/arch/x86/kernel/setup_percpu.c @@ -23,17 +23,10 @@ #include #include =20 -#ifdef CONFIG_X86_64 -#define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load) -#else -#define BOOT_PERCPU_OFFSET 0 -#endif - -DEFINE_PER_CPU_READ_MOSTLY(unsigned long, this_cpu_off) =3D BOOT_PERCPU_OF= FSET; +DEFINE_PER_CPU_READ_MOSTLY(unsigned long, this_cpu_off) =3D 0; EXPORT_PER_CPU_SYMBOL(this_cpu_off); =20 unsigned long __per_cpu_offset[NR_CPUS] __ro_after_init =3D { - [0 ... NR_CPUS-1] =3D BOOT_PERCPU_OFFSET, }; EXPORT_SYMBOL(__per_cpu_offset); =20 diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S index 7f060d873f75..00f82db7b3e1 100644 --- a/arch/x86/kernel/vmlinux.lds.S +++ b/arch/x86/kernel/vmlinux.lds.S @@ -103,9 +103,6 @@ PHDRS { text PT_LOAD FLAGS(5); /* R_E */ data PT_LOAD FLAGS(6); /* RW_ */ #ifdef CONFIG_X86_64 -#ifdef CONFIG_SMP - percpu PT_LOAD FLAGS(6); /* RW_ */ -#endif init PT_LOAD FLAGS(7); /* RWE */ #endif note PT_NOTE FLAGS(0); /* ___ */ @@ -225,17 +222,6 @@ SECTIONS __init_begin =3D .; /* paired with __init_end */ } =20 -#if defined(CONFIG_X86_64) && defined(CONFIG_SMP) - /* - * percpu offsets are zero-based on SMP. PERCPU_VADDR() changes the - * output PHDR, so the next output section - .init.text - should - * start another segment - init. - */ - PERCPU_VADDR(INTERNODE_CACHE_BYTES, 0, :percpu) - ASSERT(SIZEOF(.data..percpu) < CONFIG_PHYSICAL_START, - "per-CPU data too large - increase CONFIG_PHYSICAL_START") -#endif - INIT_TEXT_SECTION(PAGE_SIZE) #ifdef CONFIG_X86_64 :init @@ -356,9 +342,7 @@ SECTIONS EXIT_DATA } =20 -#if !defined(CONFIG_X86_64) || !defined(CONFIG_SMP) PERCPU_SECTION(INTERNODE_CACHE_BYTES) -#endif =20 RUNTIME_CONST(shift, d_hash_shift) RUNTIME_CONST(ptr, dentry_hashtable) @@ -497,20 +481,6 @@ SECTIONS "kernel image bigger than KERNEL_IMAGE_SIZE"); =20 #ifdef CONFIG_X86_64 -/* - * Per-cpu symbols which need to be offset from __per_cpu_load - * for the boot processor. - */ -#define INIT_PER_CPU(x) init_per_cpu__##x =3D ABSOLUTE(x) + __per_cpu_load -INIT_PER_CPU(gdt_page); -INIT_PER_CPU(fixed_percpu_data); -INIT_PER_CPU(irq_stack_backing_store); - -#ifdef CONFIG_SMP -. =3D ASSERT((fixed_percpu_data =3D=3D 0), - "fixed_percpu_data is not at start of per-cpu area"); -#endif - #ifdef CONFIG_MITIGATION_UNRET_ENTRY . =3D ASSERT((retbleed_return_thunk & 0x3f) =3D=3D 0, "retbleed_return_thu= nk not cacheline-aligned"); #endif diff --git a/arch/x86/platform/pvh/head.S b/arch/x86/platform/pvh/head.S index a308b79a887c..11245ecdc08d 100644 --- a/arch/x86/platform/pvh/head.S +++ b/arch/x86/platform/pvh/head.S @@ -95,9 +95,9 @@ SYM_CODE_START_LOCAL(pvh_start_xen) /* 64-bit entry point. */ .code64 1: - /* Set base address in stack canary descriptor. */ + /* Clear %gs so early per-CPU references target the per-CPU load area */ mov $MSR_GS_BASE,%ecx - mov $_pa(canary), %eax + xor %eax, %eax xor %edx, %edx wrmsr =20 @@ -161,8 +161,6 @@ SYM_DATA_START_LOCAL(gdt_start) SYM_DATA_END_LABEL(gdt_start, SYM_L_LOCAL, gdt_end) =20 .balign 16 -SYM_DATA_LOCAL(canary, .fill 48, 1, 0) - SYM_DATA_START_LOCAL(early_stack) .fill BOOT_STACK_SIZE, 1, 0 SYM_DATA_END_LABEL(early_stack, SYM_L_LOCAL, early_stack_end) diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c index 880f0f2e465e..10add45b99f1 100644 --- a/arch/x86/tools/relocs.c +++ b/arch/x86/tools/relocs.c @@ -88,7 +88,6 @@ static const char * const sym_regex_kernel[S_NSYMTYPES] = =3D { "(jiffies|jiffies_64)|" #if ELF_BITS =3D=3D 64 "__per_cpu_load|" - "init_per_cpu__.*|" "__end_rodata_hpage_align|" #endif "__vvar_page|" @@ -785,10 +784,6 @@ static void percpu_init(void) * The GNU linker incorrectly associates: * __init_begin * __per_cpu_load - * - * The "gold" linker incorrectly associates: - * init_per_cpu__fixed_percpu_data - * init_per_cpu__gdt_page */ static int is_percpu_sym(ElfW(Sym) *sym, const char *symname) { @@ -796,8 +791,7 @@ static int is_percpu_sym(ElfW(Sym) *sym, const char *sy= mname) =20 return (shndx =3D=3D per_cpu_shndx) && strcmp(symname, "__init_begin") && - strcmp(symname, "__per_cpu_load") && - strncmp(symname, "init_per_cpu_", 13); + strcmp(symname, "__per_cpu_load"); } =20 =20 diff --git a/arch/x86/xen/xen-head.S b/arch/x86/xen/xen-head.S index 758bcd47b72d..faadac7c29e6 100644 --- a/arch/x86/xen/xen-head.S +++ b/arch/x86/xen/xen-head.S @@ -51,15 +51,9 @@ SYM_CODE_START(startup_xen) =20 leaq __top_init_kernel_stack(%rip), %rsp =20 - /* Set up %gs. - * - * The base of %gs always points to fixed_percpu_data. If the - * stack protector canary is enabled, it is located at %gs:40. - * Note that, on SMP, the boot cpu uses init data section until - * the per cpu areas are set up. - */ + /* Clear %gs so early per-CPU references target the per-CPU load area */ movl $MSR_GS_BASE,%ecx - movq $INIT_PER_CPU_VAR(fixed_percpu_data),%rax + xorl %eax, %eax cdq wrmsr =20 diff --git a/init/Kconfig b/init/Kconfig index b05467014041..be8a9a786d3c 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1838,7 +1838,6 @@ config KALLSYMS_ALL config KALLSYMS_ABSOLUTE_PERCPU bool depends on KALLSYMS - default X86_64 && SMP =20 # end of the "standard kernel features (expert users)" menu =20 --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:14 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0136817E005 for ; Wed, 25 Sep 2024 15:02:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276531; cv=none; b=BZxA4O2uZwxLNF9t+N6WahLhkIj3CuA6mLU8iw+DJQnvw02uElNlBH+7TRVyKDGFc3UgTnvQZU2zBHGsIERcN/KO6avpc/DTbdTnDbOGfYf9K7W6m7ZtWRTL3Fuj815uvmY7p2fEDRc6Ll2l5gv2av7VvOEBBYldV0eZogfUHs4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276531; c=relaxed/simple; bh=rvAMLnpCc/KpfZ5R41lgUDfd/m3KQZyhOktFD8tg98U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tgPhtuXbRqeZDbX+dfdzinV+6MsYQskNeJa+rdMrHE/u2wLe11CJO4fv2emV7Ie3cH6+jD6A38qC21QW3GQPCXM25VTccG10EWrhe4PpeBjxCw1XCm2Mpumy7IeTFMxcncQUs3VTOD7jJfSfGY+OYF6i/GlKz94ZzXnXbJM6Nkk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=1NrzdrvG; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1NrzdrvG" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e211e439a3so27376697b3.3 for ; Wed, 25 Sep 2024 08:02:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276527; x=1727881327; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=rTUv2yGZRT/iYzBSyilbN8CQ+H5OcaG9Xif73xCYTUU=; b=1NrzdrvGdFOoEJwA4NtSHJQuS2Au5gVzJYv/mFcE0Np8PSGy7tvIwAJ8IcidVu1OW3 HfbC0gEqMOMFnsGAuqFKZWHsJSghaCD97sOZA9LtJjQfIEeTk7cUyfOynR9O2FeJexmD HDQ2qSS07oE3ZH4eiub+QmUsjHVbeLaHC9HiAMjrX3gnNK/BtsA0rbdLBoAH8Qg8ZmCb aJ9bqILDvZJGWyBFmkdEpl8U6mudNXXh2Pqf3gnFpJ2o0qdm/k6ueQ9BbPJVTubyusW3 LnpWqnDYqttPTKuPaCsel2Xhjy5DspsKnryXSvBWpE0b8KIaiPHQoBfe/upLfjB9qW08 HZXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276527; x=1727881327; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rTUv2yGZRT/iYzBSyilbN8CQ+H5OcaG9Xif73xCYTUU=; b=P2D2xVuMgSEc6f3oyOw73sAV+JpZJwkJIO1zQS0TGsJXFvO+qpBTGTff7L5uLZXkBs yLBItBFPkRHZmn8O1otL9LwV5Ip5OemzbFMrc9wusCB7o2GrPSP4zw0M8Q9OxkoNaggl znf0r0TwBxm+cymssHDouzmKyhu66Z2fKm1H/vYFuIp6CPjrXwb32SGvfUtqdljBDjr5 K9jai6uCLm3ikRrOCUgSgq2GjW3GZUusr8JD2avDZiPqf3MbA7WQh8hwlbxjLyvvijGm oGVHcw+ySlxdOludm/qZe/4jEivEG0cHAegdZzIBffb0xBaNkTynazvW2ARBDOb1oCCk cnyg== X-Gm-Message-State: AOJu0YwUmaYucxCvHSpcWV1Dr68Qybmhc6aANr6XO3TUSeBl/PGk7VZL bThHtsCKe7R2DA7HJKmSRjF+UEn40zEkMk0Gcv1QByUd9wwYkietIPXE8WWzdu6N1aE3c9IFO1P X+VR5XztFijQhy4w6vakHj+vU2CyYy8o36QmWV6TCDJnClJk+n7c2nD7vaVPrhQ15mwrAwsMD1z TcnqdFzbW/1J+gbOpqbEw0c24UeagMjQ== X-Google-Smtp-Source: AGHT+IGLT7yTqD1HRbp0g+dVaksf1xzveaZLP+yFkMN9Z3pXXXvCU4M/LA5dyeTTzQHsrcQFQ2z7Ogbs X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:690c:3583:b0:6b0:57ec:c5f9 with SMTP id 00721157ae682-6e21d0dc0d2mr147787b3.0.1727276527085; Wed, 25 Sep 2024 08:02:07 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:07 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=882; i=ardb@kernel.org; h=from:subject; bh=h9wNxRa08o4eoAqCSyYKOZyZKBTJUImThQPxe9qE/+Y=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6taH/xoldmidSK479WfKnGWr8jq4V3v+610hdCLdd HLxEtXHHaUsDGIcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAiB3cwMjS+ttry3/KZyAZx s7TOmpSVs1htspZOu2OitnHlgdL1srsY/goaVByePZOfkbc43zeoadWW9TLmExXTXvxNfTl7XfX dejYA X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-37-ardb+git@google.com> Subject: [RFC PATCH 07/28] scripts/kallsyms: Avoid 0x0 as the relative base From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel In some cases, LLVM's lld linker may emit the following symbol into the symbol table 0000000000000000 ? _GLOBAL_OFFSET_TABLE_ and its presence throws off the relative base logic in kallsyms. Since 0x0 is never a valid relative base, just ignore it. Signed-off-by: Ard Biesheuvel --- scripts/kallsyms.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 03852da3d249..09757d300a05 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -747,7 +747,7 @@ static void record_relative_base(void) unsigned int i; =20 for (i =3D 0; i < table_cnt; i++) - if (!symbol_absolute(table[i])) { + if (table[i]->addr && !symbol_absolute(table[i])) { /* * The table is sorted by address. * Take the first non-absolute symbol value. --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:14 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 31F4818B47B for ; Wed, 25 Sep 2024 15:02:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276534; cv=none; b=Xip7LmexPjcabatQUvSnbYdIgR6/hGbpr0qCrX+4DlzPonSrDKi8fC7c/fLRrR/N1K3+ttUQqDvz+VfPGGIIELy20/Q3YNXg2khNc+Ww87XEt3Ayv3ehqUOotlQhMM7KEkJHmqLAuNKHSHGVznfV1I+ed7S9qW8OGFTRvtQwBEg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276534; c=relaxed/simple; bh=uNZCRc4hsOOw5KhJ4tKlrDfm3UOmHFTj64isJZp8vsM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=X09OuDCWFAlCR/D9HyW1R4y3sDDH3kqR3iJbkkOyaTRJ/661jGGhNJ1B3KvH80ycsFRhXXItF52k9eiYtQsSYufcXK+4cP2VwgVOs0jDgHTGyEh327u24jiScFQy7c/qmdj4/kKQuupIG4e2WeUO2d13naXKewAAdJ+nUdgEVFM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=nxIGCpWp; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nxIGCpWp" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-690404fd230so104536047b3.3 for ; Wed, 25 Sep 2024 08:02:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276531; x=1727881331; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=lvBkJLcu5Sh3tnaE09NAZ6RZ9jqQeWc/oMvpiTXt3OQ=; b=nxIGCpWpXQtkzmZiT2xfm0JG0meclpO7fqtIS0vMPy6/xl9EzqqYQnCgItcc+41Ho1 sjXfycWXVDoJqWLb11VdvwsyfdYc4/6Y1LVocPC7AgWPDt4c93eMeMO7WlVPc81rLKyt Bof+HPEac3AeEo0dMHaQKjQDsT/bmtj2U4oWOQT9lh06ucxFU6fD62ramLaHiEgddcM4 j00atvuxOQf6JP2FD1ZlGGDRufRCCfOh34vPUrqcs4CXHdtvl4MKLtis3FK36myPwdoO uYjpx76fjwEgiSQdW4U0m4Zx0tXJCKo4tcc4NfZPtiTe5M8Utxlotp7ZlWdIJveDdAhg 08aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276531; x=1727881331; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lvBkJLcu5Sh3tnaE09NAZ6RZ9jqQeWc/oMvpiTXt3OQ=; b=u/TdXX8FMGHnU5zno2vGqcIdZFrReo/Isg4TCkW2NpBi8XhpuGwLqjlNotEpXfWu5U f3affI+vYGRixPESlOCeRn7HOyK6SRcDsw9McV8Qdm4YXb6c2EwVbP1zUe8tGHecr9Vg xClrHGWHzLVboMg9j/TGG9QbXnA0iac0zK+ZdPbaFXYQ42ue/Djj/VuwYgXdIeWMDo6o GhaaRD+TsgP69nM5q70z2aWZWhXCXw3UnXL6fikwOGxiDcFtPRr++IGS018W0TwHGFbq yfhVBKQrnkCblzLrATecKxidavZejQD1jvEIbWj77S4TPJ1GykbuX7LQJQEG9AXWYe1z nu9w== X-Gm-Message-State: AOJu0YwVIlqvRENqA2p/bijGijlQ/apI3xbFBZrjzZpHeuYBo+PSvCDb MBRdZBF/dknjeK5eexXHp3N5KNoAIM6TS07oO31h92TxWN5MoGXERnO1l7Ux2869aw/lLcgOdKU teAh8HXhfJSi5nsNlfwLHLub2ZutvT9FVh9BsPBwOyUqQ3Qm8OTRQcDZCEfWeUCnbuu+KuMILgN S1fynkUjAv4ULLUuftBOLS4Um+p7huqQ== X-Google-Smtp-Source: AGHT+IENPFDDE4RBTdISjbf4G4pk2mGOjzo8FmXYzCFJ7WDt9ZFnIbiw80yVnuCNeltTBlMkQAqf4tvt X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:690c:c9c:b0:6b0:d571:3540 with SMTP id 00721157ae682-6e21da796e6mr255067b3.6.1727276529787; Wed, 25 Sep 2024 08:02:09 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:08 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=6334; i=ardb@kernel.org; h=from:subject; bh=rOkV7b1W8gqAvLsLoQ2i9zRn35wAefNGZsd9tIotAWg=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6vYGW5mupTcMu0+/lmvg/R+5wEt4Wbk5z8ydQtZqX U8bN7/vKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABO5Lc3wP3L7HcnzmyvvZR1M rS85onToQ4R+5Y7wJefSbJaqGNxXYmVkaHXklA3NNHruJrHYtjJMvdzeSUx0wZXnutnTlvEcNXD iBAA= X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-38-ardb+git@google.com> Subject: [RFC PATCH 08/28] scripts/kallsyms: Remove support for absolute per-CPU variables From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel SMP on x86_64 no longer needs absolute per-CPU variables, so this support can be dropped from kallsyms as well, as no other architectures rely on this functionality. Signed-off-by: Ard Biesheuvel --- init/Kconfig | 4 -- kernel/kallsyms.c | 12 +---- scripts/kallsyms.c | 51 +++----------------- scripts/link-vmlinux.sh | 4 -- 4 files changed, 9 insertions(+), 62 deletions(-) diff --git a/init/Kconfig b/init/Kconfig index be8a9a786d3c..f6eeba81282d 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1835,10 +1835,6 @@ config KALLSYMS_ALL =20 Say N unless you really need all symbols, or kernel live patching. =20 -config KALLSYMS_ABSOLUTE_PERCPU - bool - depends on KALLSYMS - # end of the "standard kernel features (expert users)" menu =20 config ARCH_HAS_MEMBARRIER_CALLBACKS diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index a9a0ca605d4a..4198f30aac3c 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -148,16 +148,8 @@ static unsigned int get_symbol_offset(unsigned long po= s) =20 unsigned long kallsyms_sym_address(int idx) { - /* values are unsigned offsets if --absolute-percpu is not in effect */ - if (!IS_ENABLED(CONFIG_KALLSYMS_ABSOLUTE_PERCPU)) - return kallsyms_relative_base + (u32)kallsyms_offsets[idx]; - - /* ...otherwise, positive offsets are absolute values */ - if (kallsyms_offsets[idx] >=3D 0) - return kallsyms_offsets[idx]; - - /* ...and negative offsets are relative to kallsyms_relative_base - 1 */ - return kallsyms_relative_base - 1 - kallsyms_offsets[idx]; + /* values are unsigned offsets */ + return kallsyms_relative_base + (u32)kallsyms_offsets[idx]; } =20 static unsigned int get_symbol_seq(int index) diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 09757d300a05..9c34b9397872 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -5,7 +5,7 @@ * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. * - * Usage: kallsyms [--all-symbols] [--absolute-percpu] in.map > out.S + * Usage: kallsyms [--all-symbols] in.map > out.S * * Table compression uses all the unused char codes on the symbols and * maps these to the most used substrings (tokens). For instance, it might @@ -37,7 +37,6 @@ struct sym_entry { unsigned long long addr; unsigned int len; unsigned int seq; - bool percpu_absolute; unsigned char sym[]; }; =20 @@ -62,7 +61,6 @@ static struct addr_range percpu_range =3D { static struct sym_entry **table; static unsigned int table_size, table_cnt; static int all_symbols; -static int absolute_percpu; =20 static int token_profit[0x10000]; =20 @@ -73,7 +71,7 @@ static unsigned char best_table_len[256]; =20 static void usage(void) { - fprintf(stderr, "Usage: kallsyms [--all-symbols] [--absolute-percpu] in.m= ap > out.S\n"); + fprintf(stderr, "Usage: kallsyms [--all-symbols] in.map > out.S\n"); exit(1); } =20 @@ -175,7 +173,6 @@ static struct sym_entry *read_symbol(FILE *in, char **b= uf, size_t *buf_len) sym->len =3D len; sym->sym[0] =3D type; strcpy(sym_name(sym), name); - sym->percpu_absolute =3D false; =20 return sym; } @@ -319,11 +316,6 @@ static int expand_symbol(const unsigned char *data, in= t len, char *result) return total; } =20 -static bool symbol_absolute(const struct sym_entry *s) -{ - return s->percpu_absolute; -} - static int compare_names(const void *a, const void *b) { int ret; @@ -457,20 +449,10 @@ static void write_src(void) long long offset; bool overflow; =20 - if (!absolute_percpu) { - offset =3D table[i]->addr - relative_base; - overflow =3D offset < 0 || offset > UINT_MAX; - } else if (symbol_absolute(table[i])) { - offset =3D table[i]->addr; - overflow =3D offset < 0 || offset > INT_MAX; - } else { - offset =3D relative_base - table[i]->addr - 1; - overflow =3D offset < INT_MIN || offset >=3D 0; - } + offset =3D table[i]->addr - relative_base; + overflow =3D (offset < 0 || offset > UINT_MAX); if (overflow) { - fprintf(stderr, "kallsyms failure: " - "%s symbol value %#llx out of range in relative mode\n", - symbol_absolute(table[i]) ? "absolute" : "relative", + fprintf(stderr, "kallsyms failure: symbol value %#llx out of range\n", table[i]->addr); exit(EXIT_FAILURE); } @@ -725,32 +707,16 @@ static void sort_symbols(void) qsort(table, table_cnt, sizeof(table[0]), compare_symbols); } =20 -static void make_percpus_absolute(void) -{ - unsigned int i; - - for (i =3D 0; i < table_cnt; i++) - if (symbol_in_range(table[i], &percpu_range, 1)) { - /* - * Keep the 'A' override for percpu symbols to - * ensure consistent behavior compared to older - * versions of this tool. - */ - table[i]->sym[0] =3D 'A'; - table[i]->percpu_absolute =3D true; - } -} - /* find the minimum non-absolute symbol address */ static void record_relative_base(void) { unsigned int i; =20 for (i =3D 0; i < table_cnt; i++) - if (table[i]->addr && !symbol_absolute(table[i])) { + if (table[i]->addr) { /* * The table is sorted by address. - * Take the first non-absolute symbol value. + * Take the first non-zero symbol value. */ relative_base =3D table[i]->addr; return; @@ -762,7 +728,6 @@ int main(int argc, char **argv) while (1) { static const struct option long_options[] =3D { {"all-symbols", no_argument, &all_symbols, 1}, - {"absolute-percpu", no_argument, &absolute_percpu, 1}, {}, }; =20 @@ -779,8 +744,6 @@ int main(int argc, char **argv) =20 read_map(argv[optind]); shrink_table(); - if (absolute_percpu) - make_percpus_absolute(); sort_symbols(); record_relative_base(); optimize_token_table(); diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index a9b3f34a78d2..df5f3fbb46f3 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -140,10 +140,6 @@ kallsyms() kallsymopt=3D"${kallsymopt} --all-symbols" fi =20 - if is_enabled CONFIG_KALLSYMS_ABSOLUTE_PERCPU; then - kallsymopt=3D"${kallsymopt} --absolute-percpu" - fi - info KSYMS "${2}.S" scripts/kallsyms ${kallsymopt} "${1}" > "${2}.S" =20 --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:14 2024 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7BF2818C358 for ; Wed, 25 Sep 2024 15:02:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276538; cv=none; b=iPY5jJ3OJt8j1MTLdbdoWSMUpXm9GcIJgwP2Yi8A4bcsmxhsiWWaWQg+ftw3YglGxTtDzMgnFkpovzCan0H7a5zz++NxsaYI8uybq/3VOTzSFNHcS6q8DwvMRuBfv+DGEojko4bH6Uh/X4tmcC5D4Z8hMsxFS/5Je7bL6DEVDjk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276538; c=relaxed/simple; bh=YEhtCMnlAL7XzGF/XPf7I5bMm2iUu7a6ID53CAvxp54=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sFInatoePElTFOUy3nMRSqiDwnnXa4SX0B/aRjont7/VQprAHMhdySy+a3IvOAioSlQCzaZ88KIA1N2vrlDoTOa8E/r8RITpj20qii/+KIQfGpn4vK/Hv+nxe+UGRyz44eEXo+0+Uhv88RFfp48NZ9lam1ez8ZPI8cnTVF6/tmU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Gj5dJdjV; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Gj5dJdjV" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-42ac185e26cso52454055e9.3 for ; Wed, 25 Sep 2024 08:02:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276533; x=1727881333; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=lbdYNYYjAy1sBuzi5nxjXTZJl29neWIaYfW2XPgVAg0=; b=Gj5dJdjVsPjXxyOtWycvi8g71uQt/m9D48QHQP9xImYEkU3ClzH3UEAjj1k4wAZ3Ut 2W1hhqYUsGvPkCUiigrOhNz5HSo6qSnzJQ0nDKGtAmUob6SpDEMh9Z//MfzuL4iNTDIk d6flNQoX/2WMTm3Lvn7x7djDhT4r+jQia3PjRbtC+BFoxPrdLIx4pIeYBUNV3vFhiJ0E TBGLVwaG7Vb1lBu+ZOXzoGxyA+ZpZeKDwt3/RhToS981eJputQUogR69DbmrtNYVbN+a umFvtrqbCgnJrItX0sDaQ2Oz617qud9Inv9aG6kfDoKyWGK4g2M+KRBn52+WvQFqECdV 3NbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276533; x=1727881333; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lbdYNYYjAy1sBuzi5nxjXTZJl29neWIaYfW2XPgVAg0=; b=atmvP3WbF2aqFHEsvyVXiBdp7oIZIVYnTlORp9LoYofwJfnv89IA3Ec0uTC2RU8+GO XA6P39+Qa5ULyFKbG4A7b4CbQJtRk/SvuJsuRtncriXKMzrlMey+ddVS3pEwx1D0QXfM jzKPgWWcSKC0Tie+0xh8+CBBQC6ZJ4rApDdgJnAEIAMrQC6XWyQK3Hqskdp6ErqBDDVq AzgrzANPO7evRGCxVHiohWbqmyWRR1Y5T9cs/Wy8tXtzVJatumcbw3O+cY79EjiqOsl0 5YO5dp9RAy+9zxOh5JeslaPmgB7LpXRc0/A4Y26m7OXlcNaPC2HZnwIc8HnNH8omSoMe slWg== X-Gm-Message-State: AOJu0YwNFdsGbQiRjFbBXfFte/eu8LvLo4qpR+5eZBUejHCvK7E74y8X phhX4qye50Js6j3C59bkD/bmX7YH6ll/BatkfEH2rcfpM9xdFydsVHYm/HQcx7NZrW8ltrVKi23 GhWDiz+f/zk5p0B5ym0FSCyQotz7nYQKBbizs7yt41a8ItII1bVVCPT885LOuVmGxo4YFTTfAaR uBb+L8Aj9Q82DzKL0WVKgsfSsVWzlX8A== X-Google-Smtp-Source: AGHT+IFnkqBxjgA6y6VoEKdvu8v/U0MfKMiOBhmPl2wzAg6akAfpHBzng9kRstyeb7SA5yNN5ghxNG0E X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:600c:5709:b0:42c:a879:3d0f with SMTP id 5b1f17b1804b1-42e960af3c0mr226155e9.0.1727276532377; Wed, 25 Sep 2024 08:02:12 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:09 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=8267; i=ardb@kernel.org; h=from:subject; bh=shvywZQQiQz7Xv2EgZXJycFhGuSPaFO7HVAh5zMbRYk=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6o42nf3njrQ8av3JMd/3S06Rasbd1hMzGC0+MS4UY uxSiOjvKGVhEONgkBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABP5Jcbwz0iltm/h1Ke2pf/a nXTOnbRVCO637fRRuzetQ4Xl5cn2bIb/Lutep90WfOBcvpZFhO10i1thpKiD2UTd0wxh9l82pHa zAAA= X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-39-ardb+git@google.com> Subject: [RFC PATCH 09/28] x86/tools: Remove special relocation handling for per-CPU variables From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Due to the placement of per-CPU variables in a special, 0x0 based disjoint memory segment in the ELF binary, the KASLR relocation tool needed to perform special processing for references to such variables, as they were not affected by KASLR displacement. This meant that absolute references could be ignored, and RIP-relative references had to be compensated for KASLR, by applying the same offset but negated. None of this is necessary any longer, so remove this handling from the relocation host tool. Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/misc.c | 14 +-- arch/x86/tools/relocs.c | 130 +------------------- 2 files changed, 2 insertions(+), 142 deletions(-) diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/mis= c.c index 04a35b2c26e9..89f01375cdb7 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -235,7 +235,7 @@ static void handle_relocations(void *output, unsigned l= ong output_len, =20 /* * Process relocations: 32 bit relocations first then 64 bit after. - * Three sets of binary relocations are added to the end of the kernel + * Two sets of binary relocations are added to the end of the kernel * before compression. Each relocation table entry is the kernel * address of the location which needs to be updated stored as a * 32-bit value which is sign extended to 64 bits. @@ -245,8 +245,6 @@ static void handle_relocations(void *output, unsigned l= ong output_len, * kernel bits... * 0 - zero terminator for 64 bit relocations * 64 bit relocation repeated - * 0 - zero terminator for inverse 32 bit relocations - * 32 bit inverse relocation repeated * 0 - zero terminator for 32 bit relocations * 32 bit relocation repeated * @@ -267,16 +265,6 @@ static void handle_relocations(void *output, unsigned = long output_len, long extended =3D *reloc; extended +=3D map; =20 - ptr =3D (unsigned long)extended; - if (ptr < min_addr || ptr > max_addr) - error("inverse 32-bit relocation outside of kernel!\n"); - - *(int32_t *)ptr -=3D delta; - } - for (reloc--; *reloc; reloc--) { - long extended =3D *reloc; - extended +=3D map; - ptr =3D (unsigned long)extended; if (ptr < min_addr || ptr > max_addr) error("64-bit relocation outside of kernel!\n"); diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c index 10add45b99f1..942c029a5067 100644 --- a/arch/x86/tools/relocs.c +++ b/arch/x86/tools/relocs.c @@ -29,7 +29,6 @@ static struct relocs relocs16; static struct relocs relocs32; =20 #if ELF_BITS =3D=3D 64 -static struct relocs relocs32neg; static struct relocs relocs64; # define FMT PRIu64 #else @@ -287,34 +286,6 @@ static const char *sym_name(const char *sym_strtab, El= f_Sym *sym) return name; } =20 -static Elf_Sym *sym_lookup(const char *symname) -{ - int i; - - for (i =3D 0; i < shnum; i++) { - struct section *sec =3D &secs[i]; - long nsyms; - const char *strtab; - Elf_Sym *symtab; - Elf_Sym *sym; - - if (sec->shdr.sh_type !=3D SHT_SYMTAB) - continue; - - nsyms =3D sec->shdr.sh_size/sizeof(Elf_Sym); - symtab =3D sec->symtab; - strtab =3D sec->link->strtab; - - for (sym =3D symtab; --nsyms >=3D 0; sym++) { - if (!sym->st_name) - continue; - if (strcmp(symname, strtab + sym->st_name) =3D=3D 0) - return sym; - } - } - return 0; -} - #if BYTE_ORDER =3D=3D LITTLE_ENDIAN # define le16_to_cpu(val) (val) # define le32_to_cpu(val) (val) @@ -722,79 +693,8 @@ static void walk_relocs(int (*process)(struct section = *sec, Elf_Rel *rel, } } =20 -/* - * The .data..percpu section is a special case for x86_64 SMP kernels. - * It is used to initialize the actual per_cpu areas and to provide - * definitions for the per_cpu variables that correspond to their offsets - * within the percpu area. Since the values of all of the symbols need - * to be offsets from the start of the per_cpu area the virtual address - * (sh_addr) of .data..percpu is 0 in SMP kernels. - * - * This means that: - * - * Relocations that reference symbols in the per_cpu area do not - * need further relocation (since the value is an offset relative - * to the start of the per_cpu area that does not change). - * - * Relocations that apply to the per_cpu area need to have their - * offset adjusted by by the value of __per_cpu_load to make them - * point to the correct place in the loaded image (because the - * virtual address of .data..percpu is 0). - * - * For non SMP kernels .data..percpu is linked as part of the normal - * kernel data and does not require special treatment. - * - */ -static int per_cpu_shndx =3D -1; -static Elf_Addr per_cpu_load_addr; - -static void percpu_init(void) -{ - int i; - - for (i =3D 0; i < shnum; i++) { - ElfW(Sym) *sym; - - if (strcmp(sec_name(i), ".data..percpu")) - continue; - - if (secs[i].shdr.sh_addr !=3D 0) /* non SMP kernel */ - return; - - sym =3D sym_lookup("__per_cpu_load"); - if (!sym) - die("can't find __per_cpu_load\n"); - - per_cpu_shndx =3D i; - per_cpu_load_addr =3D sym->st_value; - - return; - } -} - #if ELF_BITS =3D=3D 64 =20 -/* - * Check to see if a symbol lies in the .data..percpu section. - * - * The linker incorrectly associates some symbols with the - * .data..percpu section so we also need to check the symbol - * name to make sure that we classify the symbol correctly. - * - * The GNU linker incorrectly associates: - * __init_begin - * __per_cpu_load - */ -static int is_percpu_sym(ElfW(Sym) *sym, const char *symname) -{ - int shndx =3D sym_index(sym); - - return (shndx =3D=3D per_cpu_shndx) && - strcmp(symname, "__init_begin") && - strcmp(symname, "__per_cpu_load"); -} - - static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, const char *symname) { @@ -805,12 +705,6 @@ static int do_reloc64(struct section *sec, Elf_Rel *re= l, ElfW(Sym) *sym, if (sym->st_shndx =3D=3D SHN_UNDEF) return 0; =20 - /* - * Adjust the offset if this reloc applies to the percpu section. - */ - if (sec->shdr.sh_info =3D=3D per_cpu_shndx) - offset +=3D per_cpu_load_addr; - switch (r_type) { case R_X86_64_NONE: /* NONE can be ignored. */ @@ -819,33 +713,22 @@ static int do_reloc64(struct section *sec, Elf_Rel *r= el, ElfW(Sym) *sym, case R_X86_64_PC32: case R_X86_64_PLT32: /* - * PC relative relocations don't need to be adjusted unless - * referencing a percpu symbol. + * PC relative relocations don't need to be adjusted. * * NB: R_X86_64_PLT32 can be treated as R_X86_64_PC32. */ - if (is_percpu_sym(sym, symname)) - add_reloc(&relocs32neg, offset); break; =20 case R_X86_64_PC64: /* * Only used by jump labels */ - if (is_percpu_sym(sym, symname)) - die("Invalid R_X86_64_PC64 relocation against per-CPU symbol %s\n", sym= name); break; =20 case R_X86_64_32: case R_X86_64_32S: case R_X86_64_64: case R_X86_64_GOTPCREL: - /* - * References to the percpu area don't need to be adjusted. - */ - if (is_percpu_sym(sym, symname)) - break; - if (shn_abs) { /* * Whitelisted absolute symbols do not require @@ -1076,7 +959,6 @@ static void emit_relocs(int as_text, int use_real_mode) /* Order the relocations for more efficient processing */ sort_relocs(&relocs32); #if ELF_BITS =3D=3D 64 - sort_relocs(&relocs32neg); sort_relocs(&relocs64); #else sort_relocs(&relocs16); @@ -1109,13 +991,6 @@ static void emit_relocs(int as_text, int use_real_mod= e) for (i =3D 0; i < relocs64.count; i++) if (!i || relocs64.offset[i] !=3D relocs64.offset[i - 1]) write_reloc(relocs64.offset[i], stdout); - - /* Print a stop */ - write_reloc(0, stdout); - - /* Now print each inverse 32-bit relocation */ - for (i =3D 0; i < relocs32neg.count; i++) - write_reloc(relocs32neg.offset[i], stdout); #endif =20 /* Print a stop */ @@ -1180,9 +1055,6 @@ void process(FILE *fp, int use_real_mode, int as_text, read_symtabs(); read_relocs(); =20 - if (ELF_BITS =3D=3D 64) - percpu_init(); - if (show_absolute_syms) { print_absolute_symbols(); return; --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:14 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3588518C933 for ; Wed, 25 Sep 2024 15:02:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276539; cv=none; b=rilu9lWhAAKXx4S+dlRRgIzDUkptBxhWbLX30U0yR7wX+HzvaLXk+ct4LvR2JE+3a4fJpKV+km8Vayysjo/Q8xIEFSad2qoMLi/PXwrok9aTsXF1rS1/sVKNilc2Yk3ZCJMfq6kCBdrLUW9fbhok/aGbJxc/V71/IvRyJ2wintQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276539; c=relaxed/simple; bh=UKT9SQcfQYmFYYWRpiFhP5glZL5JX0TJEX6TTbNQ3aU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qblysZJ3f17D3qu6McxDP6JBP0jpIxH9KI/um1dly8Il5hhzhWpeIbX2EC+GKdbzrIopo7DX3BjpIlSlYO/J0ELPGb7Y8XexrOsMqW/wNlAbtXzjL1a4izT7s+6jaB3gukltUxZftHHeRAzqL5PW3Ohmc4G6MlGUPZjcBd9DA2k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qkQ5tXmz; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qkQ5tXmz" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6de0b23f4c5so13720337b3.1 for ; Wed, 25 Sep 2024 08:02:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276536; x=1727881336; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=mwy8kTVjZ/08dn4MFhxsJ/CPkFSWb32MIOuFSl+DFsU=; b=qkQ5tXmzAYQIg6PgEygsuVkL1h0sO488dkeiRXYe2buZSMlodM0XND7O6FqDx4j1+6 csYCUf27co0WlgabTvJwDpqMd48V2w9Aok5h1wyV6FsdRFJN0HPuHCUuKfPvF6OvWXZg p+ymE/zAMzfW8t8PNQ3TcsOz4BsXZg2K236iUuDl6wANXGoLhRZ/clO+OtT4BDWdAxRh TRsd8sAaaI96lP9vEVLnMVlLJjcSks+HlR0YfLw3I11eJhkqjvvpY+vQ5X4WyPhqw/iS FIVWX/9ttsSBMuT9K1UYdGO7OFSl2UpDCFKSe0j3FfgXeuqHM/gtKnyLAmvVfxV9nNJO JqCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276536; x=1727881336; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mwy8kTVjZ/08dn4MFhxsJ/CPkFSWb32MIOuFSl+DFsU=; b=qIvDHi3Ic7gRwW3hmsXTgzGBlbaDI9eX7E2Sh8W7JsOMd6FB1stNZrzAgsxBf5Ih2i aTEmvzVuhOyMMsrAWe3JUkJ4j7ZIB7ee6C6ZD9gRhMtRL1btl4Q/XeTbxBFQjvx7aAu7 kmWOqVyETtWB5RLq3/XBVLUh9vnZ9CvfdY4FcVMVkJzUhfQDCy+bCWkMbG+N111AsKJl y8UblkNomPJqZsrjPBT6AJ4TJUO1NxJgwAYlW/UqW1DHkZEiJpOcdopa7NcDbCCpYS0l d2q3IQ6z4p6qM5ba+kcZxDRVUxVDUXLbt1pPJ0NlMmzGdx3/fdhgl6tjzgZYdTJTbqUC ENtQ== X-Gm-Message-State: AOJu0Ywh0V9Bbu8tHIp/ZxHNbRQ4wVk7TCAo4i0QAup6CXXGmLBMBhad ETx6bkj4QYXwuU48hg9KhRgL4JS4ctTwDj9KvrT29+35HTCI6Qhya2QL+4+1/nPqdgS5SEAD+3q sumoQLljcsD62oku+KRtj9XTJGx+on5StZsB03J3ybB1EWdh7EM+sKyx1TweLkwdF3Wj86DZGuy BlulILIJIoqfQpOPZc/rMqe7DvCSdIZQ== X-Google-Smtp-Source: AGHT+IEEpXdESu+AVZ5xAH4ocoULrkWfhbmfoqTWPQarKhwafrufRJLTuhlZhXvWoZ56ZcFdzDjPCpOs X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:690c:6a0d:b0:6da:3596:21b8 with SMTP id 00721157ae682-6e2089c8234mr1848507b3.4.1727276535155; Wed, 25 Sep 2024 08:02:15 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:10 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=4217; i=ardb@kernel.org; h=from:subject; bh=umOxUEgpm+XSdzq7sqW2wzWT3DKWlT715fLzy00ZrIc=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6q4zMe0fH8zn/6yon+W25vvljx/znOb8fBedfXMvk 4PJy+hrHaUsDGIcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAiC5MZGX675CxMX+/71dxB RTLX6OaHWScZikUU3t9k3TfnWIl7YijDP8vjN+r/hng3npecsefHgZqi2WFz5zPUNTIeVXy24tT y6awA X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-40-ardb+git@google.com> Subject: [RFC PATCH 10/28] x86/xen: Avoid relocatable quantities in Xen ELF notes From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Xen puts virtual and physical addresses into ELF notes that are treated by the linker as relocatable by default. Doing so is not only pointless, given that the ELF notes are only intended for consumption by Xen before the kernel boots. It is also a KASLR leak, given that the kernel's ELF notes are exposed via the world readable /sys/kernel/notes. So emit these constants in a way that prevents the linker from marking them as relocatable. This involves place-relative relocations (which subtract their own virtual address from the symbol value) and linker provided absolute symbols that add the address of the place to the desired value. Signed-off-by: Ard Biesheuvel --- arch/x86/kernel/vmlinux.lds.S | 13 +++++++++++++ arch/x86/platform/pvh/head.S | 6 +++--- arch/x86/tools/relocs.c | 1 + arch/x86/xen/xen-head.S | 6 ++++-- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S index 00f82db7b3e1..52b8db931d0f 100644 --- a/arch/x86/kernel/vmlinux.lds.S +++ b/arch/x86/kernel/vmlinux.lds.S @@ -111,6 +111,19 @@ PHDRS { SECTIONS { . =3D __START_KERNEL; + +#ifdef CONFIG_XEN_PV +xen_elfnote_entry_offset =3D + ABSOLUTE(xen_elfnote_entry) + ABSOLUTE(startup_xen); +xen_elfnote_hypercall_page_offset =3D + ABSOLUTE(xen_elfnote_hypercall_page) + ABSOLUTE(hypercall_page); +#endif + +#ifdef CONFIG_PVH +xen_elfnote_phys32_entry_offset =3D + ABSOLUTE(xen_elfnote_phys32_entry) + ABSOLUTE(pvh_start_xen - LOAD_OFFSET= ); +#endif + #ifdef CONFIG_X86_32 phys_startup_32 =3D ABSOLUTE(startup_32 - LOAD_OFFSET); #else diff --git a/arch/x86/platform/pvh/head.S b/arch/x86/platform/pvh/head.S index 11245ecdc08d..adbf57e83e4e 100644 --- a/arch/x86/platform/pvh/head.S +++ b/arch/x86/platform/pvh/head.S @@ -50,7 +50,7 @@ #define PVH_CS_SEL (PVH_GDT_ENTRY_CS * 8) #define PVH_DS_SEL (PVH_GDT_ENTRY_DS * 8) =20 -SYM_CODE_START_LOCAL(pvh_start_xen) +SYM_CODE_START(pvh_start_xen) UNWIND_HINT_END_OF_STACK cld =20 @@ -165,5 +165,5 @@ SYM_DATA_START_LOCAL(early_stack) .fill BOOT_STACK_SIZE, 1, 0 SYM_DATA_END_LABEL(early_stack, SYM_L_LOCAL, early_stack_end) =20 - ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, - _ASM_PTR (pvh_start_xen - __START_KERNEL_map)) + ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, .global xen_elfnote_phys32_entry; + xen_elfnote_phys32_entry: _ASM_PTR xen_elfnote_phys32_entry_offset - .) diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c index 942c029a5067..22c2d3f07a57 100644 --- a/arch/x86/tools/relocs.c +++ b/arch/x86/tools/relocs.c @@ -57,6 +57,7 @@ static const char * const sym_regex_kernel[S_NSYMTYPES] = =3D { [S_ABS] =3D "^(xen_irq_disable_direct_reloc$|" "xen_save_fl_direct_reloc$|" + "xen_elfnote_.+_offset$|" "VDSO|" "__kcfi_typeid_|" "__crc_)", diff --git a/arch/x86/xen/xen-head.S b/arch/x86/xen/xen-head.S index faadac7c29e6..4d246a48a85f 100644 --- a/arch/x86/xen/xen-head.S +++ b/arch/x86/xen/xen-head.S @@ -88,7 +88,8 @@ SYM_CODE_END(xen_cpu_bringup_again) ELFNOTE(Xen, XEN_ELFNOTE_VIRT_BASE, _ASM_PTR __START_KERNEL_map) /* Map the p2m table to a 512GB-aligned user address. */ ELFNOTE(Xen, XEN_ELFNOTE_INIT_P2M, .quad (PUD_SIZE * PTRS_PER_PUD)) - ELFNOTE(Xen, XEN_ELFNOTE_ENTRY, _ASM_PTR startup_xen) + ELFNOTE(Xen, XEN_ELFNOTE_ENTRY, .globl xen_elfnote_entry; + xen_elfnote_entry: _ASM_PTR xen_elfnote_entry_offset - .) ELFNOTE(Xen, XEN_ELFNOTE_FEATURES, .ascii "!writable_page_tables") ELFNOTE(Xen, XEN_ELFNOTE_PAE_MODE, .asciz "yes") ELFNOTE(Xen, XEN_ELFNOTE_L1_MFN_VALID, @@ -109,7 +110,8 @@ SYM_CODE_END(xen_cpu_bringup_again) #else # define FEATURES_DOM0 0 #endif - ELFNOTE(Xen, XEN_ELFNOTE_HYPERCALL_PAGE, _ASM_PTR hypercall_page) + ELFNOTE(Xen, XEN_ELFNOTE_HYPERCALL_PAGE, .globl xen_elfnote_hypercall_pag= e; + xen_elfnote_hypercall_page: _ASM_PTR xen_elfnote_hypercall_page_offset -= .) ELFNOTE(Xen, XEN_ELFNOTE_SUPPORTED_FEATURES, .long FEATURES_PV | FEATURES_PVH | FEATURES_DOM0) ELFNOTE(Xen, XEN_ELFNOTE_LOADER, .asciz "generic") --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:14 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5AD5218C920 for ; Wed, 25 Sep 2024 15:02:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276541; cv=none; b=lVhegDNQuZHlE/okac15dNy8541Bw6cTaVqG2qAO8KEMHqdnP+tQMcyBa9Gb7vM73kt+0xO/I41mLcxoetCRMAFScf5Y8EbrauTztd60mCpAaS/tmQUEqKTxDn/JFILKQ04MOPXnbIPPaixqhVqse8f1cGoidjUN6tuvO0j14n8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276541; c=relaxed/simple; bh=JTcZMRf52oQJjvsrQbaaa26wNEschMPTichZxc3ZyTY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RveKIH22mLgZJUXx71FosIo9pmXrEgbR1F+sZ/IPwxYMP3ZnJY0H+ng+FiyEWLUMF/r9SMJTXekQJUhuxBrK8gckANCgCAp1Jgt+1CI6dxQH5fG/1HQF+tOzy35wE5+HJrGQ2krB8bzlAfnwtKmKxNJpjeEE0B3OszbCJVqTlwY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=CjZEe0yN; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="CjZEe0yN" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ddbcc96a5fso96281457b3.0 for ; Wed, 25 Sep 2024 08:02:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276538; x=1727881338; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=WebA59vxPkU4u7rhQajyH4BvEeqGZRhskbQXw1lowfs=; b=CjZEe0yNT4EA/GcKFh/6Jssm8m05B25tkcW0KFhhtewe6SRdV2pEBLGa8yx/CpnYXJ MfntbYWoR1EhxjADX4+aOngkHAyjORnocT0BwOjgiFWm6L9HcXyB25UtRC5WVEKXYQ8/ OBJp+RzevUhpU5cFWOu9jzr/vhzsqrDj6GDAUkaYRak0E/z6D3Yf/fm8f5JZrtMWcMnl N2qyh7vfxeYL30RgFZtnhXpuFyZSZTj5isYZ0+Nn1pn+I+DNbcSSB5DAZw7YyVlBXAoM 1qDDv2bg/JAR0EqiibTG7Li9wPtqRa1ga4t+tABXo/xjQIYYy2wNNwNCHH8jY+xfdOcT OWug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276538; x=1727881338; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WebA59vxPkU4u7rhQajyH4BvEeqGZRhskbQXw1lowfs=; b=A5r9YSgjoKcshPGExRYJBPwhii9a2doC84NKBpEyL3o2hGdJcSNc9+5rfQSavU3lef McASlquWJyD57YnUMvzbmcm0IaLyJHwPtrRPR2cfg8jGPAjH1wIlyJErhxFppUf0m+BG IQwXHdorCPaT9ik+9F2vEyTChAnzi6FhpxPDstjv2/ovJdijFm6Ih3FlwMXstr8bM0Qp Y3FenMfzqkk/GJX2rO4FSAXmg60i2tnG2zYpmDKiMabtSUkgDFEcXs/GZ0opXOt7U/Ph 0D68wXSfMwuBig67F1xBP0nYnAb1/rj/7i2HL+5WEKfrgzwlgY37U3t9pcTDqFDRuNOn 99zA== X-Gm-Message-State: AOJu0YwSao45TXPmrPki4l00OlsmKVMxJDm15xFJ1WjHV0ueepeY5Xqz 1ebYaf7PA9rFkWtvqmMzZsZG3ItmdY2xDQq9BxS6uWI9yqdtJLKpVgOvCcoO/JIGrfcP3Q4+fp7 mLpte+C1IvEczjPQ6nDYmcDNZpZDjPDXWlkddHZsidh9r2wW/R5LV900M4x4EsEzrJ746Q8dXu/ zRPrB6PKjGqf6h0csmXD5T3jAoHQzybQ== X-Google-Smtp-Source: AGHT+IG1eaRHJ+k+oWi0MEewfWnHRvzWiyuPrZa2XUixnuMGDlz9gyzzCn4pjg7USYksDYWFG76nQbXB X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a81:7c46:0:b0:673:b39a:92ce with SMTP id 00721157ae682-6e21da5ea7bmr151347b3.3.1727276537527; Wed, 25 Sep 2024 08:02:17 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:11 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=3674; i=ardb@kernel.org; h=from:subject; bh=R6ohiPGa3ul2ikQd2A7YE6wpEPvXq0bd6W2LGMrzZ/k=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6m4L/Wkc7n0aPQzsYULiEQZPMlgefsmsLHv4K8KPe c6T1d0dpSwMYhwMsmKKLAKz/77beXqiVK3zLFmYOaxMIEMYuDgFYCLF8YwMhwNCO7WWnu4+bHJ/ R+KN+e/7+Z5GTZy4J83mRy0Xx+UbAowMLwuTAsXUc+SfqAVdK+gtnHdHT+jRjZtrv3D/ms77oD2 BCwA= X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-41-ardb+git@google.com> Subject: [RFC PATCH 11/28] x86/pvh: Avoid absolute symbol references in .head.text From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel The .head.text section contains code that may execute from a different address than it was linked at. This is fragile, given that the x86 ABI can refer to global symbols via absolute or relative references, and the toolchain assumes that these are interchangeable, which they are not in this particular case. In the case of the PVH code, there are some additional complications: - the absolute references are in 32-bit code, which get emitted with R_X86_64_32 relocations, and these are not permitted in PIE code; - the code in question is not actually relocatable: it can only run correctly from the physical load address specified in the ELF note. So rewrite the code to only rely on relative symbol references: these are always 32-bits wide, even in 64-bit code, and are resolved by the linker at build time. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/pvh/head.S | 39 ++++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/arch/x86/platform/pvh/head.S b/arch/x86/platform/pvh/head.S index adbf57e83e4e..e6cb7da40e09 100644 --- a/arch/x86/platform/pvh/head.S +++ b/arch/x86/platform/pvh/head.S @@ -54,7 +54,20 @@ SYM_CODE_START(pvh_start_xen) UNWIND_HINT_END_OF_STACK cld =20 - lgdt (_pa(gdt)) + /* + * This is position dependent code that can only execute correctly from + * the physical address that the kernel was linked to run at. Use the + * symbols emitted for the ELF note to construct the build time physical + * address of pvh_start_xen(), without relying on absolute 32-bit ELF + * relocations, as these are not supported by the linker when running in + * -pie mode, and should be avoided in .head.text in general. + */ +0: mov $xen_elfnote_phys32_entry_offset - 0b, %ebp + sub $xen_elfnote_phys32_entry - 0b, %ebp + + lea (gdt - pvh_start_xen)(%ebp), %eax + add %eax, 2(%eax) + lgdt (%eax) =20 mov $PVH_DS_SEL,%eax mov %eax,%ds @@ -62,14 +75,14 @@ SYM_CODE_START(pvh_start_xen) mov %eax,%ss =20 /* Stash hvm_start_info. */ - mov $_pa(pvh_start_info), %edi + lea (pvh_start_info - pvh_start_xen)(%ebp), %edi mov %ebx, %esi - mov _pa(pvh_start_info_sz), %ecx + mov (pvh_start_info_sz - pvh_start_xen)(%ebp), %ecx shr $2,%ecx rep movsl =20 - mov $_pa(early_stack_end), %esp + lea (early_stack_end - pvh_start_xen)(%ebp), %esp =20 /* Enable PAE mode. */ mov %cr4, %eax @@ -84,17 +97,21 @@ SYM_CODE_START(pvh_start_xen) wrmsr =20 /* Enable pre-constructed page tables. */ - mov $_pa(init_top_pgt), %eax + lea (init_top_pgt - pvh_start_xen)(%ebp), %eax mov %eax, %cr3 mov $(X86_CR0_PG | X86_CR0_PE), %eax mov %eax, %cr0 =20 /* Jump to 64-bit mode. */ - ljmp $PVH_CS_SEL, $_pa(1f) + lea (1f - pvh_start_xen)(%ebp), %eax + push $PVH_CS_SEL + push %eax + lret =20 /* 64-bit entry point. */ .code64 1: + UNWIND_HINT_END_OF_STACK /* Clear %gs so early per-CPU references target the per-CPU load area */ mov $MSR_GS_BASE,%ecx xor %eax, %eax @@ -108,10 +125,8 @@ SYM_CODE_START(pvh_start_xen) call *%rax =20 /* startup_64 expects boot_params in %rsi. */ - mov $_pa(pvh_bootparams), %rsi - mov $_pa(startup_64), %rax - ANNOTATE_RETPOLINE_SAFE - jmp *%rax + lea pvh_bootparams(%rip), %rsi + jmp startup_64 =20 #else /* CONFIG_X86_64 */ =20 @@ -146,8 +161,8 @@ SYM_CODE_END(pvh_start_xen) .section ".init.data","aw" .balign 8 SYM_DATA_START_LOCAL(gdt) - .word gdt_end - gdt_start - .long _pa(gdt_start) + .word gdt_end - gdt_start - 1 + .long gdt_start - gdt .word 0 SYM_DATA_END(gdt) SYM_DATA_START_LOCAL(gdt_start) --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:14 2024 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C50CC18E021 for ; Wed, 25 Sep 2024 15:02:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276544; cv=none; b=poEZ7nDywbun6C7eJjui/8k79CImn+kiNPiZ5hDTViF2a4vF8N4jXR6edVnDkk2Sl1v+vHCMadTiMq9HQ8XtYuzi7c6NSutyZJj/51fk0fSwEMRCiRhmvwQ2SjZas4RNdSNIMq1M9UndUKp9ETDKarTawHwNYc7D0s/AIACoQNc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276544; c=relaxed/simple; bh=5QDZ8e6v38oe4vdIzJiUkWBfZtQ8UHIt+vUykdg9FgY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kG4PR6lqqUz1+bhdrYB/Fgw9Qv8WWrH9DOwIlOccPGXp04hE9BPHdkRRUKMBxK8ay9Of7UYsluwEu0UIGlT87FDdGRS1Pgvgg1aPooGFA2GGBzyefZgU9rhrVmFqgaa6D8U8DT2frBf6lw6rruFaIA5IKLYaT5RPpvwhaPxMb/M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=LeXPBBTs; arc=none smtp.client-ip=209.85.218.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LeXPBBTs" Received: by mail-ej1-f73.google.com with SMTP id a640c23a62f3a-a8a7463c3d0so447911966b.2 for ; Wed, 25 Sep 2024 08:02:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276541; x=1727881341; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=sMLI1adUSxUIp8UXfDcqdD38QxXI+e+F/+Ag4yCxL7E=; b=LeXPBBTsdUX0tgcobuyp0O/6x6z2M5lVDEdbOYH34+fi2/CYM93dEc402cMrFtvM+F CkJx94uMVdCwK7Xc97aEqwdfpJNBKO2xEg7vZldZWC55yQpmfJdjM/wsBayNID9D3qlx P1TsBUMydJjkA5gRJJs8M7gY02k1UvKjmTGyfyzSrE7txWaMXnE94lcDM+iaFZuH4duK /3T2QoUxHNE9G/Aef3gP8mIJ6FEaHR2IEDPCLGLt8Ef/RMihVG1nayEs1fW9xkZ6pdmI M/1LnAjWDiYtcu+/2Qxjjxg/Okoi/YybMwm16A4DUKBrZSlODzKYpyXRIT3F6S6iDa07 xNAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276541; x=1727881341; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sMLI1adUSxUIp8UXfDcqdD38QxXI+e+F/+Ag4yCxL7E=; b=X58s44iTSd0bPcM2HXbdn+R3cmU/tMbJOsFCyFpThmaHvG9zIgGQ04CBW2o96C+snu 4OjlbTIFf4/INL2tRZ6FPFqZl8E6YJStI5lq/8O6D+hANxW/39DKnS3Z6X+xPbt0ABUG GULf8d8SRmLm6nCUC2oqQb2DFM6XQF9Fbey9pb+s3z3/j83unKVSMe0/VSy0BBaqme9N lSKVEAVBCJfhhKDqxKSiA0Wm3wMXhcsxUl/zcQ2h6Rn7jZcKiNNgM3U/KvRYAJEhGgO5 sKNLTY+JVFZo+Kp2wp3mM4UJTu16k1cLg7ON0mzAGx3pKv6azXrKCnicW5wfFgtXdKND Y0PA== X-Gm-Message-State: AOJu0Yy9wB0A3Q8LeiuFoPPHnmYTzEtMbxTYin4f+OnzQG57T6pmp5YU VQijqjfJftSlXyqPzXe+NdDsO3R6habeHLCc/kNcxClytgzsKH7kCE66iAgHVbSnEWZrdQnV/uS SR0YYnaK4C5EvkBk77U9KpNXdcdwvVc/Ncq1cHl2uBlDarsjcfUAFp+5OpTppRVAf9kzE0ARIco DRGHettgFRgXRBu15Z/HNfE+eyKiQTgA== X-Google-Smtp-Source: AGHT+IHqdrbnTURYSpf/5afh17e1i3rk5opqYbKAlLurQBauppiTWqSh5+YV6ICpl369+Ha35/G5jy3r X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a17:906:6d8:b0:a8a:76fd:ae67 with SMTP id a640c23a62f3a-a93a061ba72mr129666b.10.1727276539991; Wed, 25 Sep 2024 08:02:19 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:12 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2056; i=ardb@kernel.org; h=from:subject; bh=VKZKKQxE7f774DDxhaNjhf6gM8y+4CHtkhz4ITFKHVU=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6l6HjphM91K+tMbfPlum+q87xXbcaBq/plqB6J0rH Clz7dQ6SlkYxDgYZMUUWQRm/3238/REqVrnWbIwc1iZQIYwcHEKwESez2L4w3XU9mNkQveSSXqf bkfpTfktlPbu753X3m4//t4RvX456gUjw9ItwQrWWxY9aPiwtX3xVeeW5YpKNdGO1l4qU7KmnF4 VzwoA X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-42-ardb+git@google.com> Subject: [RFC PATCH 12/28] x86/pm-trace: Use RIP-relative accesses for .tracedata From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Use RIP-relative accesses and 32-bit offsets for .tracedata, to avoid the need for relocation fixups at boot time. Signed-off-by: Ard Biesheuvel --- arch/x86/include/asm/pm-trace.h | 4 ++-- drivers/base/power/trace.c | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/pm-trace.h b/arch/x86/include/asm/pm-trac= e.h index bfa32aa428e5..123faf978473 100644 --- a/arch/x86/include/asm/pm-trace.h +++ b/arch/x86/include/asm/pm-trace.h @@ -8,10 +8,10 @@ do { \ if (pm_trace_enabled) { \ const void *tracedata; \ - asm volatile(_ASM_MOV " $1f,%0\n" \ + asm volatile("lea " _ASM_RIP(1f) ", %0\n" \ ".section .tracedata,\"a\"\n" \ "1:\t.word %c1\n\t" \ - _ASM_PTR " %c2\n" \ + ".long %c2 - .\n" \ ".previous" \ :"=3Dr" (tracedata) \ : "i" (__LINE__), "i" (__FILE__)); \ diff --git a/drivers/base/power/trace.c b/drivers/base/power/trace.c index cd6e559648b2..686a0276ccfc 100644 --- a/drivers/base/power/trace.c +++ b/drivers/base/power/trace.c @@ -167,7 +167,7 @@ EXPORT_SYMBOL(set_trace_device); void generate_pm_trace(const void *tracedata, unsigned int user) { unsigned short lineno =3D *(unsigned short *)tracedata; - const char *file =3D *(const char **)(tracedata + 2); + const char *file =3D offset_to_ptr((int *)(tracedata + 2)); unsigned int user_hash_value, file_hash_value; =20 if (!x86_platform.legacy.rtc) @@ -187,9 +187,9 @@ static int show_file_hash(unsigned int value) =20 match =3D 0; for (tracedata =3D __tracedata_start ; tracedata < __tracedata_end ; - tracedata +=3D 2 + sizeof(unsigned long)) { + tracedata +=3D 2 + sizeof(int)) { unsigned short lineno =3D *(unsigned short *)tracedata; - const char *file =3D *(const char **)(tracedata + 2); + const char *file =3D offset_to_ptr((int *)(tracedata + 2)); unsigned int hash =3D hash_string(lineno, file, FILEHASH); if (hash !=3D value) continue; --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:14 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CBFAB18F2F7 for ; Wed, 25 Sep 2024 15:02:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276546; cv=none; b=OoUPcIr5hzQI2iOxbB6epFE+MDWYsFTgLK/h1pAMSnOq9Z9hW2zCxgqhhIdARLCKtlynH1QhyRuxFEbdd77WOZE5PFPOc4pKzqNzfVrVUOCio0BRv9EYOYosi2w6gIlL6Jtb9H4duLfRpd0zXhiClVdL+LBw2VQ6vloSQQwYrmQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276546; c=relaxed/simple; bh=P2FcDMrWaViJAVOVZOgFhqVr7ndECoGt9aNdX1NbL6A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aYq+QTY0t6oTld6IUqa4KIVPESwxrOtIw9uiowsp/VShg5O5FZve5UikQ/FNJA6glrqyDCRQdT6Rb68Bhs/8VG5TMKDQO+yUlpsfFYgcKs5yGZrxeyVVn3aCJOwE74tJCU25yn/0scFXE/uIQJ50BJSht/0xZeEeExg0rBys+C8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=lQtn/1YJ; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="lQtn/1YJ" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e1d46cee0b0so10267435276.2 for ; Wed, 25 Sep 2024 08:02:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276543; x=1727881343; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vu/ZbROFphQGBAR+273iXbk1tlXuWo8crbz4dEomRj0=; b=lQtn/1YJn8ofwp00YPqh/7ezdca6i/dHyOqwIKn9NYR/t34JCf/JnPUsE/CTB64V03 XZQA9DoCVY0P4KsZmS6kF6A3RLqm2zeGzo6VDPM6iwiemCFUih2Dg0OCxkYSE/SMg1Us cW7HeP21m6KnShkftmJzo9cbUKesSxLQSeKXdqNy6YyZxhbp0xOCN+it/zMTpFMhQ21A 5K4QzdOBDM237nQcdJz6PAF+hW1p8zl8uML/NoXt6W8agRnzoo/bdG273F4gG0tLvBj4 KUtgPm9oCEZZYT+PQrU4BKNIjVqOkVLcWKePKSU60RBD5kDm8Gt0DAAzS+wIlRIB6Dta 8nfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276543; x=1727881343; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vu/ZbROFphQGBAR+273iXbk1tlXuWo8crbz4dEomRj0=; b=Lh6zkiSR9zN39bhMyDCqxM12Ko33tbRHgGzSa1xg6x2R3kkyTJM4MRQEhudFhRQd1E qwF8/CWrMEkgV3d4fJsdZWbxM+xTVG5cdy+X2U/G/IazK/qxJb/1QHpdg01C14kIgBou ACJ+YS9eM6ryEqEx4pXuCzi0gvkxSZbOQwvETTdh1k0qv/sBeOMvJ4eZX7vHAhf+5vWj ORdA8E0O4ms74bwSpmCROoRXAAacDramygKGR8C1lPg+ICv6rIuT7Bq6EqJNG6oJfjsc JN7cX4xz+ojPasuKmy4P0RzPJ12wqgKhtIk7h9gf1XHYOE4VjZVzccXYQvavUwkTDSi7 diEw== X-Gm-Message-State: AOJu0YzSdZxA2kepT0YbJxsvAifXDZYdm+rVMKH0KJbLTgV6yjhnwnu6 t8fRKGhtPf/zLpnm7lbok6jmLsU6BRZQ/DEOrtlfrY/VALfe4EnSGH50LXdP5DdlGYOO4f10QHV t5CKsjIgPYboirsyXyYY4qxbOPBT5yHGv6POlpW9IoAC/C5GIUTq31pCRzM0a09jdkiWyIN4BOc Z1TLbvFwp9BJKlpv7gWkZzfgtnGaGsHg== X-Google-Smtp-Source: AGHT+IEyaWBDeaj2o4LzPpSYVvpQbis55gp/KI/SuWbxXUep7wBTAaa7DqSfUrTGsylfjBz2VoxVb7Nf X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:6902:1782:b0:e0b:958a:3344 with SMTP id 3f1490d57ef6-e24da39b0c3mr17936276.10.1727276543053; Wed, 25 Sep 2024 08:02:23 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:13 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1397; i=ardb@kernel.org; h=from:subject; bh=OCB6ogDK8zWwsvV7jOJ7CFiQBF2ggvTbOxK6mQJaX9A=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6r76WO61FcfVRPyObM7vdxPl3nB63j7e+T6H1Wu+r TX46mXYUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACYyaR4jwzOv6S8kr9nwfXyX 9iPzW9q5kzeEqzTXKbA29B6+VB8ddZORYZns0cK9u93/3DfuTd/m9X/T7v+32d81/dIy2BMjHfO RgwMA X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-43-ardb+git@google.com> Subject: [RFC PATCH 13/28] x86/kvm: Use RIP-relative addressing From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Avoid absolute references in code, which require fixing up at boot time, and replace them with RIP-relative ones. In this particular case, due to the register pressure, they cannot be avoided entirely, so one absolute reference is retained but the resulting reference via the GOT is compatible with running the linker in PIE mode. Signed-off-by: Ard Biesheuvel --- arch/x86/kernel/kvm.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c index 263f8aed4e2c..8eac209a31aa 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -800,9 +800,11 @@ extern bool __raw_callee_save___kvm_vcpu_is_preempted(= long); * Hand-optimize version for x86-64 to avoid 8 64-bit register saving and * restoring to/from the stack. */ -#define PV_VCPU_PREEMPTED_ASM \ - "movq __per_cpu_offset(,%rdi,8), %rax\n\t" \ - "cmpb $0, " __stringify(KVM_STEAL_TIME_preempted) "+steal_time(%rax)\n\= t" \ +#define PV_VCPU_PREEMPTED_ASM \ + "leaq __per_cpu_offset(%rip), %rax \n\t" \ + "movq (%rax,%rdi,8), %rax \n\t" \ + "addq steal_time@GOTPCREL(%rip), %rax \n\t" \ + "cmpb $0, " __stringify(KVM_STEAL_TIME_preempted) "(%rax) \n\t" \ "setne %al\n\t" =20 DEFINE_ASM_FUNC(__raw_callee_save___kvm_vcpu_is_preempted, --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:14 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72314190063 for ; Wed, 25 Sep 2024 15:02:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276549; cv=none; b=sfhR69VvV6cIXPm9POpTkzNWYs3wtGoUu0EkpxKzPZz+nz/wq0WivryroiZGoK+s/9ZEbaF7h0l53EGgHNvozshpDI4R/ZGNzPnFX8r7iYxXm6X4OCokehvjU5Ffs08XqRlD23gmq42QltYqeTM1ewcpcipXdX5Zdc7ThvpMnMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276549; c=relaxed/simple; bh=vuy2srbwlGWCNhjJ429H+XQTUepQW5BfwFX52st1yXg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GX5lReelAU479DFGsUqc6Ehge2TzBkNewI8ls4s4DtPwfzVxhAtTe3zRIA+3HxXtK6n8Issi8JCNs/9Hs+SxoGIgnoKNnOHouaRp22CFVZz2hqpo81EERqq2BBoOIqLtqF+ttLCkxfW9pyQMHkNRadKroy/kGgN1FDLvWHhmZSY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=P1KxEfU7; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="P1KxEfU7" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e2261adfdeso8791877b3.2 for ; Wed, 25 Sep 2024 08:02:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276546; x=1727881346; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=TEh1/zkt2ebfm60sAnflL74oR8UqJJcIMQFWRvgC+dE=; b=P1KxEfU7NFI1FLa68htOn3FIemF2YoJ6ZngfHUhsKoFBRiXxiwVBOTYecj3iV5fXut Kp5pAy4MyYpTuIwIh1ul53pcejuye0feuUW/N2sSiiV23IoHpesKme5QnUN3QIRK9Scm jOJSpxVOtjp+R4vV2H1BXWolNHvOaltnkFQZO4b8HH+yxzEJYon4nNKIV9abmiJ3Qr6z MB45/dPAX38qoLlqeKxr2UzmTg6b3zQjOj2t0/4aKMhnPp9H0u99nESwyciFmWtxtP7X WlwMG8GRS226I1RCGZJjImxo/4LOik3jTeV5QqHXPPvoYIyhU83s5SKOqD4zhyJaMGyV AqrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276546; x=1727881346; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TEh1/zkt2ebfm60sAnflL74oR8UqJJcIMQFWRvgC+dE=; b=NyLFgwhgMDAj18KgD9VKqUWdUsCMY9+i2U6fI4BVCgSATpX1i7klLrv4TM/jaOueuN 8SAde/qmR1xAxmUd0ljQOqf1ooXczFVllIt9Ty2wz+i53GkAwdkewKhfYCw1v2XfGKOZ zEkBOESrgBoTrb4P1sn+s3oUpTXHm3ZRehh5ycKJ2xsepLZBRNFTKUbLnJYp+c42RmUI vZomnJP4jnURRD2a3SwSFEk0jssrPzSjdkUewUqUGaHTcGE0GhdvbWkRll27TKqRIV2B tHCfKTPASVtM9kN5QyzDq7AkgO3L3iM7h98FtZ3TA14XZ/DFvoIJuFccESsyiXXoQEYt ED+A== X-Gm-Message-State: AOJu0Yw8ilnwwiJrgVpqIr6PWSIDngU0as9mCFhitz1deFk+Lcd+Wpqm betVgnzHdnNWx7jzAfUvuc6nxV3u1sq1Gx27ygWP3aZ3tq7xrFniMMZbMrepMl75LaSnMPZpo/r XgacxGr6b1SAbruU63mYKhs1dVFjSotA44ufGfgLDX9VdNDKhFjbmJFS/+SnxUcNZHqeVXzFv9w qKJjAwk7mYRllbRf4WltPRrkf6It3yHA== X-Google-Smtp-Source: AGHT+IGW5+6XYu5evov+Dvb+HROZj8E6YTyxVVP1a7XWDg5djajQJxqHbA/Whb8+5/ewCu11GBBmm/SO X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:690c:438d:b0:6b1:8b74:978a with SMTP id 00721157ae682-6e21d9d4ae9mr51697b3.4.1727276545723; Wed, 25 Sep 2024 08:02:25 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:14 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=934; i=ardb@kernel.org; h=from:subject; bh=DJZDFnSYvzXqQFYRPN/gizuDfobz3ccWU866DGFdU/U=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6v7XV9cI3q9cJcJR8+Vf2+Fz2Wk/Dh/Y/X1ToOXyt a351cxsHaUsDGIcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAiXdsZGZaErou6cPCEcv22 Prcpp+bqHWznepHA8X6Z9Zn/83etFNBlZOg9+zlgLqMlq/AX9n2rM647K2+Mmvyn9GzNS4f81qf fJ/EAAA== X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-44-ardb+git@google.com> Subject: [RFC PATCH 14/28] x86/rethook: Use RIP-relative reference for return address From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Instead of pushing an immediate absolute address, which is incompatible with PIE codegen or linking, use a LEA instruction to take the address into a register. Signed-off-by: Ard Biesheuvel --- arch/x86/kernel/rethook.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/rethook.c b/arch/x86/kernel/rethook.c index 8a1c0111ae79..3b3c17ba3cd5 100644 --- a/arch/x86/kernel/rethook.c +++ b/arch/x86/kernel/rethook.c @@ -27,7 +27,8 @@ asm( #ifdef CONFIG_X86_64 ANNOTATE_NOENDBR /* This is only jumped from ret instruction */ /* Push a fake return address to tell the unwinder it's a rethook. */ - " pushq $arch_rethook_trampoline\n" + " leaq arch_rethook_trampoline(%rip), %rdi\n" + " pushq %rdi\n" UNWIND_HINT_FUNC " pushq $" __stringify(__KERNEL_DS) "\n" /* Save the 'sp - 16', this will be fixed later. */ --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:15 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B52A518FDDA for ; Wed, 25 Sep 2024 15:02:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276551; cv=none; b=q6r1TuE1qgY4g28ZdMUySHwWCA+9Nhh1vE8GNA7eaJnWs1v1neu09Q7YryiNeWeywBYWDjtk9kgvQCJowSBg+3BoKcWY/7VHIsqN6akYagoZIUFixqk54p/tnpvggMuMwJo3oGc1vvRGQf9H7aKhqditwdbcj/ObtVLb7lGymV4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276551; c=relaxed/simple; bh=tOVKR1lo4TSaVSSD57+XbfuDxZ/gh43zpiGViV/8Jbk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IZSggig7/hi/ZWoHDIUJB5xiIXiZrJU+5UqEJwI11BhxYsxBQgEZ5iiMrKX9rzBB6Du7AsxDZMjQK2fJXA1ku8dSuRpLu2ti1atN0DAFWGsaaci4Q/8o4+yRBKFZHtvYOQHr+QDOFV47fQSSN9QGZ9kcRvnfK7Bb2NQbzjyId90= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GnkKjhtp; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GnkKjhtp" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6d4f52a4069so118678527b3.3 for ; Wed, 25 Sep 2024 08:02:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276548; x=1727881348; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ApY23d/9zEIgxeEREKcGvRSADQZAXD1f0/BrlQ4mdDw=; b=GnkKjhtp9zSMZYTexidLqwKc++D8bRxlmP6vfNPPiswSuKsY7b5gjmgIVcyTHNJmXV R7cILxiDBk7XWsrqChfpEINGugxqUdT9I5FgI4htYuOm1ROc5WGR59a0jlhTFRO/tjum CWVybAqQtX2snhNPb+A1mhvliI+J7VlZ0xZ3USnRGgmL1vakWwE8brOdDD8vlqavUm12 lanq2ZcCA7IIW/USsG0YE1Oa6h++SWt4alvAWiXjABfnGQRDtvpHHc+Nrb+8CBRvciCq nkpjydToaKAfAY4HeApliRfcLdyZvFqwlsJLIYZPyN43lymbKggonsBTZKs6FZRRm5WP HXJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276548; x=1727881348; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ApY23d/9zEIgxeEREKcGvRSADQZAXD1f0/BrlQ4mdDw=; b=TuHemGDHIdQYB6/j78QmTF7VhfblftPqVv0lKP7ZUmWRfmZllkf7ayz3jyAjJlqzMM wZCLgCdeSjK9bodoA1ip5G0mU4DE17NxiaEPHCX8X0CQQ85C+JaavICz1O8avFqgtPWm V0iW0gP+uLfyxZybXn0DoKPydy9/CDveNaB6dYFbFTLnEpEYmO3u53JZ3wrXJXCE1hg/ JybUyVnS3FVcsPG1WkgBGwkLtxgnoWpbO8M27Fy/EXqUi2WuLh0RiKz/U+l3ySw0ALJ1 hW2/ICBG03RkInDLN2wVjfwN5RRngdP0RpHluzDKBjM8DbuIl75A5PeT4kk4xFiWnVcR tM0g== X-Gm-Message-State: AOJu0Yx7eIVoUdNnlvZNCkI3kDxMr+AlOlbzjR7L5liZzBTctg5hOfj9 cn+O1cRa5CNUlKv8hBjQHHISUemY8KjI72A7WN+XbewdxabeEyDuhvPBwLQBwJPafwbzhMzXce4 makbP2sze9MUltfH87c/kXoSAUE33jImZIjRS1SBmAEIspFr8+vvXZQjozrK50RY8CZWLk7sEFe 9+TaqsT1hwUWD0sCj11nBu7/c3lyM7MA== X-Google-Smtp-Source: AGHT+IG0+ryHhOjgZiIz9yF/upguc+H1WcXc9rbc3LtAhjefmA+cSn7SHizdSFamvUM2J9fCM9A9Vr3/ X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:690c:d85:b0:6db:e107:75a2 with SMTP id 00721157ae682-6e21da5c761mr429007b3.4.1727276548084; Wed, 25 Sep 2024 08:02:28 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:15 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=715; i=ardb@kernel.org; h=from:subject; bh=MI3z3hAzDWNLGP+bB6WD9qNMTMv7yb8jTXTlUIfHWCE=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6sHwPz3fHPWC8oK2RzHe5nszs/XmM4NX7P1ae3YFv bWd+fJLRykLgxgHg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZjI/9UMfwVfxZ9hrm+9xvze 6zfvw/cKoUe5Kv5w1pmKsYfxH/f4do3hf+75iD2ndC64zYjxsFPZpbpKIrxBk/fzstDCxW8eO9h OYgAA X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-45-ardb+git@google.com> Subject: [RFC PATCH 15/28] x86/sync_core: Use RIP-relative addressing From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Use RIP-relative accesses and avoid fixups at runtime. Signed-off-by: Ard Biesheuvel --- arch/x86/include/asm/sync_core.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/sync_core.h b/arch/x86/include/asm/sync_c= ore.h index ab7382f92aff..cfd2f3bca83b 100644 --- a/arch/x86/include/asm/sync_core.h +++ b/arch/x86/include/asm/sync_core.h @@ -31,7 +31,8 @@ static inline void iret_to_self(void) "pushfq\n\t" "mov %%cs, %0\n\t" "pushq %q0\n\t" - "pushq $1f\n\t" + "leaq 1f(%%rip), %q0\n\t" + "pushq %q0\n\t" "iretq\n\t" "1:" : "=3D&r" (tmp), ASM_CALL_CONSTRAINT : : "cc", "memory"); --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:15 2024 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 514C3191F62 for ; Wed, 25 Sep 2024 15:02:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276555; cv=none; b=Dg02nrSfUITkz5A5zAQEvthj6EDWQWw8opSw4IioqyxMAGcPXO4gOC2ELr6Z/CGC7K6E9Uh/ELGjsBphQeagASh8bX/N9w1nvgXUbN1WSYAfHopxCMrRA+Rz3UC6x4BygJgfwYmtkeDBM5jq/wLP5fK592LzoE3ZMblDnViFzHY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276555; c=relaxed/simple; bh=w/P+2/7/i0/j2dzTpIfGv0L554wt2D6+DkL221lAYlA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iMsTD3911bM9uceQFrmEZOFeymwlyty249rUpRoZHdpq2AGTPyPA3guZdEPpmeygHkkuJpuVB38yvSVt0HpU7tF1Vd2ibl4TDyV0QRJeG0xbaETtPCxvdx2bwo2CERc8VsQWzmD7dRI+Dv+3vyLdPn98VyE+Flpl8WHJn/v39gI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Qrok5dzK; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Qrok5dzK" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-42cb374f0cdso5460665e9.0 for ; Wed, 25 Sep 2024 08:02:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276551; x=1727881351; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vAC8l3Y8WvFUyajZb+pirsZCUpFvZ3r8+BwrpQuj/SE=; b=Qrok5dzKpXydUx5M9TW32zg2YJHKEXJ61ctOadUBeAGB0KBhnYmDeES2ItYtzlQ2Iz 83RXWxCohJTrwzWXBDmOBVxKe6VqAo2t8LYfZT/bLYT9u83sHWixLK1XVitYJRNU/4Cs bb77HlZaFm1QRsB7nJfWZ0p3iGuBnLTvkB9/lQp6uDm+ON3kaCp8gsqNFk3Y9/CGKqIE mBsiU79wsuaLlW3wl+quKZDfLd/CzlCAAq9B6bQ6OaoulHkoF0c/xiBT0Ptq3TE5N54G ykdnMzV1y3bDyPswuWKLS6zWQ7VCEkG5IeV+AzWbZ4q26npScA3CSzFd4kTH4K4/ONO0 7t0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276551; x=1727881351; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vAC8l3Y8WvFUyajZb+pirsZCUpFvZ3r8+BwrpQuj/SE=; b=H0nVFyZ/SY1rb1z8JHcGW2Yu6f+bW8rtOQE48E15vKP/h8qZk+wQ0IDD3U87jESOdU liVs55gea7VXeQk7db5clA93lO2d/yymIubbmUg96qMFSfY8ms+gK3w3x5ppFGIj/K5g Ym+Qo1FwhQ3XMBhGGWXSr61K8PJVMUm+RqdUju9NvyHgwXHN1kTZpoxbY9VVWza18mLH 37Trarsud8ffeEXnCqUYsmgTYnR/p/PeEr3V50SbUnnlj4mdJaUMFwS2sog9mUOTzce9 ju5K5rQRSJgOcc0nJITnczQJbrI/Kl35GO7+P6eF/NaMQTc3jfeQycSJ18a5/hNPWq7p d25A== X-Gm-Message-State: AOJu0Yx5OHikM/BHMhTzWNo16+ny/Qe3qth2H8vuUw4ONUY6A8G1umLN ik4YKL8l3hLmIj+FVDUmP1Hd/nljM8XY57fTlb3sxwQQM0vjG2SGF+jvJlGAB9zcxatkjBsZwQr /uOLxRmSalzJfoaXSz5DlL5bYhdwC4GR9SZ8Gde2IH3BYCMaSRrOJRvm7FX/LGbrzX127U/Ymt2 /afXmBNQxuH40m/I/U9slwyI2XR7yTfQ== X-Google-Smtp-Source: AGHT+IGsHQZbBz7v7rjB2VogUNbzT3V2zpNGtAWZsXzYUPDihu4mWF0GF+yn2VIMs1ZNTkRqyJuf4PXN X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:600c:4ca2:b0:42c:acd5:c641 with SMTP id 5b1f17b1804b1-42e96037975mr279665e9.2.1727276550412; Wed, 25 Sep 2024 08:02:30 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:16 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2723; i=ardb@kernel.org; h=from:subject; bh=tMElkct6gfSM6gp7MMWL/mRF1vGpc7RrYvn7ljZWXyE=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6iGZPaJMr8LDnd//b/RW8SszXOfdmXXq3NuftZV75 oQv+lfaUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACZy4w/DHw7GS21Or9/PeKL6 7ZX8zCMs6WX7p7QFXu6zKz6oZDhn3S9GhnkFPcEfOSfJKv9IYrKOXfBu+/X/Xl4zqlnSnqz7v02 4kwsA X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-46-ardb+git@google.com> Subject: [RFC PATCH 16/28] x86/entry_64: Use RIP-relative addressing From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Fix up a couple of occurrences in the x86_64 entry code where we take the absolute address of a symbol while we could use RIP-relative addressing just the same. This avoids relocation fixups at boot for these quantities. Signed-off-by: Ard Biesheuvel --- arch/x86/entry/calling.h | 9 +++++---- arch/x86/entry/entry_64.S | 12 +++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h index ea81770629ee..099da5aaf929 100644 --- a/arch/x86/entry/calling.h +++ b/arch/x86/entry/calling.h @@ -375,8 +375,8 @@ For 32-bit we have the following conventions - kernel i= s built with .endm =20 .macro SAVE_AND_SET_GSBASE scratch_reg:req save_reg:req + GET_PERCPU_BASE \scratch_reg \save_reg rdgsbase \save_reg - GET_PERCPU_BASE \scratch_reg wrgsbase \scratch_reg .endm =20 @@ -412,15 +412,16 @@ For 32-bit we have the following conventions - kernel= is built with * Thus the kernel would consume a guest's TSC_AUX if an NMI arrives * while running KVM's run loop. */ -.macro GET_PERCPU_BASE reg:req +.macro GET_PERCPU_BASE reg:req scratch:req LOAD_CPU_AND_NODE_SEG_LIMIT \reg andq $VDSO_CPUNODE_MASK, \reg - movq __per_cpu_offset(, \reg, 8), \reg + leaq __per_cpu_offset(%rip), \scratch + movq (\scratch, \reg, 8), \reg .endm =20 #else =20 -.macro GET_PERCPU_BASE reg:req +.macro GET_PERCPU_BASE reg:req scratch:req movq pcpu_unit_offsets(%rip), \reg .endm =20 diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S index 1b5be07f8669..6509e12b6329 100644 --- a/arch/x86/entry/entry_64.S +++ b/arch/x86/entry/entry_64.S @@ -1038,7 +1038,8 @@ SYM_CODE_START(error_entry) movl %ecx, %eax /* zero extend */ cmpq %rax, RIP+8(%rsp) je .Lbstep_iret - cmpq $.Lgs_change, RIP+8(%rsp) + leaq .Lgs_change(%rip), %rcx + cmpq %rcx, RIP+8(%rsp) jne .Lerror_entry_done_lfence =20 /* @@ -1250,10 +1251,10 @@ SYM_CODE_START(asm_exc_nmi) * the outer NMI. */ =20 - movq $repeat_nmi, %rdx + leaq repeat_nmi(%rip), %rdx cmpq 8(%rsp), %rdx ja 1f - movq $end_repeat_nmi, %rdx + leaq end_repeat_nmi(%rip), %rdx cmpq 8(%rsp), %rdx ja nested_nmi_out 1: @@ -1307,7 +1308,8 @@ nested_nmi: pushq %rdx pushfq pushq $__KERNEL_CS - pushq $repeat_nmi + leaq repeat_nmi(%rip), %rdx + pushq %rdx =20 /* Put stack back */ addq $(6*8), %rsp @@ -1346,7 +1348,7 @@ first_nmi: addq $8, (%rsp) /* Fix up RSP */ pushfq /* RFLAGS */ pushq $__KERNEL_CS /* CS */ - pushq $1f /* RIP */ + pushq 1f@GOTPCREL(%rip) /* RIP */ iretq /* continues at repeat_nmi below */ UNWIND_HINT_IRET_REGS 1: --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:15 2024 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25A3C191F86 for ; Wed, 25 Sep 2024 15:02:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276558; cv=none; b=RdqcW3rwRhCx9qkdem/QtUKNdavn6Y1XmTpPMD0aX4hmttz0gfdMDbYWAysyDgh9t3yYVJuAsbtk0lTq9bc9NvlSnNjXurtosxYTUhpWoTw6RUSnltg7DV31gcB7i2bQPbp8XIC0kvdrUu/1jf1ZqcFe6Y3/y3FCR+GulOrihGY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276558; c=relaxed/simple; bh=CMurddgdDXZCJtwTh7sicisjqXrlKeRuIZ1TFq/8oX8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Bv28IHvvKJwVeuWkn4XJP06ryGFgurAP+/+rDr3PDwyuFcqc32KnlUicJ3Cs0yOyFzm44cEktCOQfeF60fkhj1qQESYwiN7ri71ojdEJpFx+XzlPDYGrXReAOgsfsipoaOsHns5bWzyqOYA/005A0BNb82UYoFo3ZQVuiaPjY/c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QheHwjD5; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QheHwjD5" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-374b35856aeso2100950f8f.3 for ; Wed, 25 Sep 2024 08:02:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276553; x=1727881353; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GQ1cTLWN/qm1UuyAwDsDiC37ecAWh5CqzUoGN/3jT3Q=; b=QheHwjD5gcp3pQg1btz606WQxjCEVHcr1ocQck8aZdG3CSc99pWkXkDgo0M34zSU0I dtwwyONfaMKeysdhicSDnH3SgJC/XbwybTtvx43xSJPonNlHzlXLd01XPNTmd8BGYbJG 1kE6Nv5tPc/5Z0Y+DESalkSnexq3RW330glSb5cKl38X9bGlfHo7gzPgOoyTLbmLCj4/ zFo4liVFFN1L0oEl2QIwtVmZNdsZn5WijD/a20yAXU8VO4mg9O8c3GMQc6OVRtfiYf3v niyirVOUBznJP8RSQBAHP8REttly+HIqe6ErgmXiFM4hq27L91W2t8t6hxUkCVEAs23F JjrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276553; x=1727881353; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GQ1cTLWN/qm1UuyAwDsDiC37ecAWh5CqzUoGN/3jT3Q=; b=vXS3HIOIJ1Unet5c3bwe1OQ8Pl3XRxAnNIm9bcZhwMGs0NzzHkCJgq7V1h+rmO3yUC I8Rh4UWUL1Kz2807U81wZMhooWu6d5zufEXlZn1oj4vUQyjohbmEMHKvUQNA/s59cAfy Dnf5m6uFVnsnJspCtCw2sZV+5098XEMKxdGNfEhsZgEaLCYI2JN05LVTVuPqLAAwkOM4 3k5/3OEepYjebo7Hcr7xoxsiT3Jpj/yF+C6+jtk0vBfMw6GyrDSBFlhJxdFBrhdm1Eec tO+ZXzF4UQY0MkqVToB7ZeAef2zhf9Q7wTqdvTSLawFtA1HuKQwU+KcsHuJ8r4rR8OXK NRYw== X-Gm-Message-State: AOJu0YyPqTfIF9EejtNRoYrbg3yIayWNFIYAkoRtlJk/h6JS6icE9IS3 kl+iTYdsVveu7rcHGn99j4o24uVcp/+RqUx9Q1lw4Cw+umcxjJJpW2XgKVEkmzKViohZ8ZFlBPP l9FK+P4jtmlJOrMy5WHp1H5e2peDanDym+Fyr7byMQONUm7/X27hSFkjc3m0jI2w4E7DOH2ZIKe X6/c0zvg+6uDUljKI4uh2Qsn4wksiLQA== X-Google-Smtp-Source: AGHT+IEbuxsanqyADq8Deat2AYN/hrx0yHWhBh+K99NJ5NNzbtyXWGf28ibYOumK1/7kTAhssaq/BKmU X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:6000:1948:b0:371:8d08:6302 with SMTP id ffacd0b85a97d-37cc2466282mr1755f8f.2.1727276552813; Wed, 25 Sep 2024 08:02:32 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:17 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1089; i=ardb@kernel.org; h=from:subject; bh=Gk3vl5lcNVlUG5WaXx6+kW7e4vi5Wc78FqbNjCEkOO8=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6pHFHOXq05/1nFnPcudQmJ7wpr7dLDufJIa/O8C5x Txd0oW5o5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAExENobhf+k7pXk1KeYh8xfa vazOipusdTSTNTQtS+jqxsxgKQvlywz/y0srlXSWbHHluXU+4voFET+GHMONjSy3U2vjQqX5RWI YAQ== X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-47-ardb+git@google.com> Subject: [RFC PATCH 17/28] x86/hibernate: Prefer RIP-relative accesses From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Replace some absolute symbol references with RIP-relative ones, so we don't need to fix them up at boot. Signed-off-by: Ard Biesheuvel --- arch/x86/power/hibernate_asm_64.S | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/power/hibernate_asm_64.S b/arch/x86/power/hibernate_a= sm_64.S index 0a0539e1cc81..1d96a119d29d 100644 --- a/arch/x86/power/hibernate_asm_64.S +++ b/arch/x86/power/hibernate_asm_64.S @@ -39,7 +39,7 @@ SYM_FUNC_START(restore_registers) movq %rax, %cr4; # turn PGE back on =20 /* We don't restore %rax, it must be 0 anyway */ - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq pt_regs_sp(%rax), %rsp movq pt_regs_bp(%rax), %rbp movq pt_regs_si(%rax), %rsi @@ -70,7 +70,7 @@ SYM_FUNC_START(restore_registers) SYM_FUNC_END(restore_registers) =20 SYM_FUNC_START(swsusp_arch_suspend) - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq %rsp, pt_regs_sp(%rax) movq %rbp, pt_regs_bp(%rax) movq %rsi, pt_regs_si(%rax) --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:15 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35FD2192580 for ; Wed, 25 Sep 2024 15:02:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276560; cv=none; b=RxoobmmXM+39K9H8LfrmhgA7jc4ziiBDfTFiK8zSTNogVkUQanCRlL5HmbnXwzG9DgSiMpKSoht8FMCbeI/Af3ZMqRru+GoGdGHUZkn/bKNxS98Q3UhSiGLUq1kWJGGeOpcNs3A+sroocAsF1FRq9gRy+vzDZgVFXH1Ope3kIx8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276560; c=relaxed/simple; bh=i/QYg4imAjIe1bwTsD55fhiHAflGFWUqPqRf587cmXw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IgzoE777BNWqdW7LPPolRJtmsZ0uanPUnrWUOEGXp4or/E1HjhwjcdE7fWon5gVgnMH4SR+qDKNSaqeWzn77ds6U3fybSmtbNz4xgEjZfCslSF5r3xl2eKuYJ7G9Nbyqtnmbcu+2Qc4XWEI2Xady1IGDbYLrOCKg85RVxBWz10o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=t4Rppe0o; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="t4Rppe0o" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e1d46cee0b0so10267739276.2 for ; Wed, 25 Sep 2024 08:02:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276556; x=1727881356; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yDR79FvO/p7pnSynPl14YMv6NBkHhQBDE9t/OHYGGd4=; b=t4Rppe0oOZrM8rrVvSNlggls1ZRJPpECq26qFnMncuOtFgzJr46mGusEymy3qWdvjG OkmwarU5hNsJqz/Wh5t61KCFoWUz+AuEgMqrOJ1TazD5+6aIFjevSDXJbKGmDSqTX7Ew CCDANtXnw+qfm76PgKJlBQWSB6BVvF38zmAyKZeZifaZ3dAQZc/naSXMoWSrWh54iiZu 19+JCAkmeYxbqwZHm1oMglO4xPvB/zNYVQddPlc5M9ZsGsOYzlPE3GC8tDYY4nhgyeBS TJ5ONJAIqyf9vEuBbIouLLhXZfht7FqE/E141vxkUC/oXnxa2811TyADIndgQmoWmG7P kKUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276556; x=1727881356; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yDR79FvO/p7pnSynPl14YMv6NBkHhQBDE9t/OHYGGd4=; b=pH9tfNHkdz+0f1fm3x37yXNOPM86LUWFVO59rXb5tiZqljy2RzDrvgH6jo7Xs8PEXf ZZYOs2sY081J7H5MUzi5oDGlb17HvNK3NenBMhWXk7yaPpx0t6KsWMqC1lYtWcsvIA5O aTU0TSxYfaPFkiT+vkOWD/+SXAGwU/KTlXcG2q6gp6DmVxfHcEFhqvDszvCzPHLX3lxB bDWrjErT4CS9WLIqjkpb03YzuKaxXzDqOIJ6k9QpH5ZvkeYnNralqAHixmuiiIYLnP2t CDZBaqDFI5r+6DRP4QRTC1JnEwdIlgQh1g73Rdoz0im43NlEhz80JjhQevoAIh9jAVqc Hibw== X-Gm-Message-State: AOJu0Yz6bPd8tldSMqEfKCo5QTkEp3ofjSdwkd32URFc2Xj9OwxJzCuG m664FFK0QU00p+j74GgPQ3StM85lVm1IDKSxnI2P0EsObX4jN9ArIAoLHaQqKCQi0yb/BV1SEBU cq2Eng8z2IWpemoSsoP0WeEzVD6t13tq6V8obZVh0bMt9EY8vqhp2vnto3Ubp3wD82Gqx2EsXHf 1uv+NVF70hBHGc248SOfNVEgv4X+Uzrg== X-Google-Smtp-Source: AGHT+IHZ4KYfHMEhV3OEcung24V1cc8XfcFAbHD7O3r+Hgg2lidAI5AomtBdq038iHk4HR3+YtV8+eXL X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a5b:ec8:0:b0:e0b:f93:fe8c with SMTP id 3f1490d57ef6-e24d47abf13mr23895276.0.1727276555668; Wed, 25 Sep 2024 08:02:35 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:18 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=4435; i=ardb@kernel.org; h=from:subject; bh=GHJcalcWc5Qb+zlhfpCjetJZ0xMQ4h6Eadch5mSu/kI=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6tGDolHZO8I/hR3x3e/2aucUz1XW9pJNyVMcgt6LK d6perupo5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAExkjgkjw6k/n413NQiwSlie DnVk14nq3/xeMf2gfMyRHcx1Ja3nuRj+h2Vr7vj9abJ2U31nzqv2/TMymqLWPwla++d7/1o/hhv mDAA= X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-48-ardb+git@google.com> Subject: [RFC PATCH 18/28] x86/boot/64: Determine VA/PA offset before entering C code From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Implicit absolute symbol references (e.g., taking the address of a global variable) must be avoided in the C code that runs from the early 1:1 mapping of the kernel, given that this is a practice that violates assumptions on the part of the toolchain. I.e., RIP-relative and absolute references are expected to produce the same values, and so the compiler is free to choose either. However, the code currently assumes that RIP-relative references are never emitted here. So an explicit virtual-to-physical offset needs to be used instead to derive the kernel virtual addresses of _text and _end, instead of simply taking the addresses and assuming that the compiler will not choose to use a RIP-relative references in this particular case. Currently, phys_base is already used to perform such calculations, but it is derived from the kernel virtual address of _text, which is taken using an implicit absolute symbol reference. So instead, derive this VA-to-PA offset in asm code, using the kernel VA of common_startup_64 (which we already keep in a global variable for other reasons), and pass it to the C startup code. Signed-off-by: Ard Biesheuvel --- arch/x86/include/asm/setup.h | 2 +- arch/x86/kernel/head64.c | 8 +++++--- arch/x86/kernel/head_64.S | 9 ++++++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h index 0667b2a88614..85f4fde3515c 100644 --- a/arch/x86/include/asm/setup.h +++ b/arch/x86/include/asm/setup.h @@ -49,7 +49,7 @@ extern unsigned long saved_video_mode; =20 extern void reserve_standard_io_resources(void); extern void i386_reserve_resources(void); -extern unsigned long __startup_64(unsigned long physaddr, struct boot_para= ms *bp); +extern unsigned long __startup_64(unsigned long p2v_offset, struct boot_pa= rams *bp); 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 d4398261ad81..de33ac34773c 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -138,12 +138,14 @@ static unsigned long __head sme_postprocess_startup(s= truct boot_params *bp, pmdv * doesn't have to generate PC-relative relocations when accessing globals= from * that function. Clang actually does not generate them, which leads to * boot-time crashes. To work around this problem, every global pointer mu= st - * be accessed using RIP_REL_REF(). + * be accessed using RIP_REL_REF(). Kernel virtual addresses can be determ= ined + * by subtracting p2v_offset from the RIP-relative address. */ -unsigned long __head __startup_64(unsigned long physaddr, +unsigned long __head __startup_64(unsigned long p2v_offset, struct boot_params *bp) { pmd_t (*early_pgts)[PTRS_PER_PMD] =3D RIP_REL_REF(early_dynamic_pgts); + unsigned long physaddr =3D (unsigned long)&RIP_REL_REF(_text); unsigned long pgtable_flags; unsigned long load_delta; pgdval_t *pgd; @@ -163,7 +165,7 @@ unsigned long __head __startup_64(unsigned long physadd= r, * Compute the delta between the address I am compiled to run at * and the address I am actually running at. */ - load_delta =3D physaddr - (unsigned long)(_text - __START_KERNEL_map); + load_delta =3D __START_KERNEL_map + p2v_offset; RIP_REL_REF(phys_base) =3D load_delta; =20 /* Is the address not 2M aligned? */ diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index ab6ccee81493..db71cf64204b 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -99,13 +99,20 @@ SYM_CODE_START_NOALIGN(startup_64) /* Sanitize CPU configuration */ call verify_cpu =20 + /* + * Use the 1:1 physical and kernel virtual addresses of + * common_startup_64 to determine the physical-to-virtual offset, and + * pass it as the first argument to __startup_64(). + */ + leaq common_startup_64(%rip), %rdi + subq 0f(%rip), %rdi + /* * Perform pagetable fixups. Additionally, if SME is active, encrypt * the kernel and retrieve the modifier (SME encryption mask if SME * is active) to be added to the initial pgdir entry that will be * programmed into CR3. */ - leaq _text(%rip), %rdi movq %r15, %rsi call __startup_64 =20 --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:15 2024 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F9D0192D6C for ; Wed, 25 Sep 2024 15:02:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276563; cv=none; b=l/+QmgNpefEYMqur5fz6Ck8/7xEQ2tZtgpt9IdN4AZ45iNss5+FeYegaZ/84BYJXTy3Vp1WvwZFw6BvIAEehVKtLrJFjVrJz5+M1IvCf6Hk7GvpABg46M0f+uKyUxcOP6GZU327GCKJ1NiIXXdfpmqej8aNlHyYUfYIFKUH2QDU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276563; c=relaxed/simple; bh=jP/irMOJlRaYkkcw0c6zTUJt0fVtAuXqgia734+CBv4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Zzt5DxLMaaFVldR2W6gwgUfRXm/6RkMUCvamZN6XAHb6Fy/+/Nu7u2++75F+S95XWu0tZdyakyKpg0XiXXIdLbmyV5sa7c21Z7CxjqaH8zBaqL1rv8o8mj4o41PdYRcNQCGfTLXyn7rcwyJDX6PqxZPZIFuuoe9rCr4Amsc4PBo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qQ12S8eo; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qQ12S8eo" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-42cb998fd32so47875715e9.1 for ; Wed, 25 Sep 2024 08:02:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276559; x=1727881359; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=If2fzGYCGwBnqYyJZ9PtA7MpTD5EDRXIER0Mmi+qoTw=; b=qQ12S8eoZaogA1c8tkiG0EpWCS4bxYbKY8Tx2/5Ts8MHhBUI0HW8HHCfVqQqw/7KBF TMOntsNtK+tPDD+ylHDH8YJCJh2SP+eIt8alJLR1Eu1HTEKH36uj+T4/PdNILctxiT3c 72KgAdXLONM0LAQBDyib80WfvMIt4AdP0FPxX4tZrRRo1utRmFZ22o6W/IaZZ77M+Gz9 8nONMuwp259keoy5GY30NBylMbZgJNv9LfmqFhAoAHRfjlaHVAVIB32PIN3P5ZjCLnlI xbd29eCVlNiWn1knCLh6RQv/fe3RPIWGP5NrImE40+Dxzi4TaUiOP0sgwbKPjy1i1DTq uRQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276559; x=1727881359; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=If2fzGYCGwBnqYyJZ9PtA7MpTD5EDRXIER0Mmi+qoTw=; b=nLKKNbGuDpSIu2WV245Khicv0+In2uc5FzZ+yN3pKPCIaY8nA4zMHbKzLqC9rL+ZOT CD7ONPAM34k90CWLNmNqMmDZLP8KRfLX7Di6fIXh5U4WeQbxMiNfj6/lkygej2z1sP+k NTfWYTZ3vL9g+nxsPq/548kqsENpLc+7bFRVMQhjg86T7ACFHikrKPRRFiDP7wyrbqom r6N/cQVQoC08gkWyaG9OLgahsAGD3jx/CKt2zT22mTL23acRnrRN/bXMazW6Drjwyj0b BX9BuEe21or9BCmMvzEmoL9GE3V1AQuBbLoIAR57Z/8S4xA5CKuTqPZ+ZRuQDo2sdqDO AZgg== X-Gm-Message-State: AOJu0Yya1PTKrLhSZQWj9EVWrC6VDMkN0WKUMEhbjHWXB8WWiQFlvihH UMv3cfpxe2NRp2k6rujQemCOHIJWzENXqvNY0nqPfkp1IgUXL5mAWAxc27r70xdR88ZvW16d9Bu 1IAYlv8Q1U86H+B0BxOd18fv5JzDpHupwIhO3j7cXmcW3QUytAoooef9WLSmHgc5fZW+Vazpwiu ZA5DWjLZCoLlQzS3od+IlICQRQ5WYD8g== X-Google-Smtp-Source: AGHT+IGkF5zmfUWW4O6+9JFmmh7xBD2vdGsjZEUVC3p8TPU+fzBQ5QlHTxnNWj2zBWn7gVrNU3EsN2Rx X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a5d:5547:0:b0:378:89be:1826 with SMTP id ffacd0b85a97d-37cc2473cc2mr1714f8f.4.1727276557999; Wed, 25 Sep 2024 08:02:37 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:19 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=4681; i=ardb@kernel.org; h=from:subject; bh=1WKyFOMOSweUVoFxWBVIeZQGadqaqXWeT1GGGyj8jAA=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6jGPaQaG+1hu3Z0QvW9R+99QHb2J68ydL88WzUpdH Hf1+4blHaUsDGIcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAiex8y/GZ5P9H2XQc705Yd pyrK5txZNU1B4+0LjklBfTe9r2ed6mlhZNiZU/rz52b9K88lGacbreAU2ufVkfLX5bhxnpO8ivb mR0wA X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-49-ardb+git@google.com> Subject: [RFC PATCH 19/28] x86/boot/64: Avoid intentional absolute symbol references in .head.text From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel The code in .head.text executes from a 1:1 mapping and cannot generally refer to global variables using their kernel virtual addresses. However, there are some occurrences of such references that are valid: the kernel virtual addresses of _text and _end are needed to populate the page tables correctly, and some other section markers are used in a similar way. To avoid the need for making exceptions to the rule that .head.text must not contain any absolute symbol references, derive these addresses from the RIP-relative 1:1 mapped physical addresses, which can be safely determined using RIP_REL_REF(). Signed-off-by: Ard Biesheuvel --- arch/x86/kernel/head64.c | 30 ++++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c index de33ac34773c..49e8ba1c0d34 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -91,9 +91,11 @@ static inline bool check_la57_support(void) return true; } =20 -static unsigned long __head sme_postprocess_startup(struct boot_params *bp= , pmdval_t *pmd) +static unsigned long __head sme_postprocess_startup(struct boot_params *bp, + pmdval_t *pmd, + unsigned long p2v_offset) { - unsigned long vaddr, vaddr_end; + unsigned long paddr, paddr_end; int i; =20 /* Encrypt the kernel and related (if SME is active) */ @@ -106,10 +108,10 @@ static unsigned long __head sme_postprocess_startup(s= truct boot_params *bp, pmdv * attribute. */ if (sme_get_me_mask()) { - vaddr =3D (unsigned long)__start_bss_decrypted; - vaddr_end =3D (unsigned long)__end_bss_decrypted; + paddr =3D (unsigned long)&RIP_REL_REF(__start_bss_decrypted); + paddr_end =3D (unsigned long)&RIP_REL_REF(__end_bss_decrypted); =20 - for (; vaddr < vaddr_end; vaddr +=3D PMD_SIZE) { + for (; paddr < paddr_end; paddr +=3D PMD_SIZE) { /* * On SNP, transition the page to shared in the RMP table so that * it is consistent with the page table attribute change. @@ -118,11 +120,11 @@ static unsigned long __head sme_postprocess_startup(s= truct boot_params *bp, pmdv * mapping (kernel .text). PVALIDATE, by way of * early_snp_set_memory_shared(), requires a valid virtual * address but the kernel is currently running off of the identity - * mapping so use __pa() to get a *currently* valid virtual address. + * mapping so use the PA to get a *currently* valid virtual address. */ - early_snp_set_memory_shared(__pa(vaddr), __pa(vaddr), PTRS_PER_PMD); + early_snp_set_memory_shared(paddr, paddr, PTRS_PER_PMD); =20 - i =3D pmd_index(vaddr); + i =3D pmd_index(paddr - p2v_offset); pmd[i] -=3D sme_get_me_mask(); } } @@ -146,6 +148,7 @@ unsigned long __head __startup_64(unsigned long p2v_off= set, { pmd_t (*early_pgts)[PTRS_PER_PMD] =3D RIP_REL_REF(early_dynamic_pgts); unsigned long physaddr =3D (unsigned long)&RIP_REL_REF(_text); + unsigned long va_text, va_end; unsigned long pgtable_flags; unsigned long load_delta; pgdval_t *pgd; @@ -172,6 +175,9 @@ unsigned long __head __startup_64(unsigned long p2v_off= set, if (load_delta & ~PMD_MASK) for (;;); =20 + va_text =3D physaddr - p2v_offset; + va_end =3D (unsigned long)&RIP_REL_REF(_end) - p2v_offset; + /* Include the SME encryption mask in the fixup value */ load_delta +=3D sme_get_me_mask(); =20 @@ -232,7 +238,7 @@ unsigned long __head __startup_64(unsigned long p2v_off= set, pmd_entry +=3D sme_get_me_mask(); pmd_entry +=3D physaddr; =20 - for (i =3D 0; i < DIV_ROUND_UP(_end - _text, PMD_SIZE); i++) { + for (i =3D 0; i < DIV_ROUND_UP(va_end - va_text, PMD_SIZE); i++) { int idx =3D i + (physaddr >> PMD_SHIFT); =20 pmd[idx % PTRS_PER_PMD] =3D pmd_entry + i * PMD_SIZE; @@ -257,11 +263,11 @@ unsigned long __head __startup_64(unsigned long p2v_o= ffset, pmd =3D &RIP_REL_REF(level2_kernel_pgt)->pmd; =20 /* invalidate pages before the kernel image */ - for (i =3D 0; i < pmd_index((unsigned long)_text); i++) + for (i =3D 0; i < pmd_index(va_text); i++) pmd[i] &=3D ~_PAGE_PRESENT; =20 /* fixup pages that are part of the kernel image */ - for (; i <=3D pmd_index((unsigned long)_end); i++) + for (; i <=3D pmd_index(va_end); i++) if (pmd[i] & _PAGE_PRESENT) pmd[i] +=3D load_delta; =20 @@ -269,7 +275,7 @@ unsigned long __head __startup_64(unsigned long p2v_off= set, for (; i < PTRS_PER_PMD; i++) pmd[i] &=3D ~_PAGE_PRESENT; =20 - return sme_postprocess_startup(bp, pmd); + return sme_postprocess_startup(bp, pmd, p2v_offset); } =20 /* Wipe all early page tables except for the kernel symbol map */ --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:15 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E1848193062 for ; Wed, 25 Sep 2024 15:02:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276565; cv=none; b=qQgLTvPK9REQAftorEOqpj/jZN0SaTRGx7I1Mj1cyrqFfOSggUtISjjpD36dcqewHlP4x6dkboh0sPF+xdUnNCPdAy68aOGu+Cp9GiPlfEZG60+arzMyeDbO9ummryfa55zeSnfgCRw3VY12vhLPYfNljKnjz18T2ZZ7dRD/K9k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276565; c=relaxed/simple; bh=9UAQuGK2mWhKEqX7oxUH9zhtKGPJ6CK9xsJA2g/Xjq0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MCpxlXoUW2yE6mFA2m5MYNqavdSIOhpg95rOS1pEmRmCulZ7w5/pqble8YNBXDl1RcCSXiCc1yvzIFVZwqwvPNzsAciXJmrTFF1L5sF4HmNK6Xc6Qm4hymEhuQuqTidzdJNToepjGhnmwo9mKQbjRYqa9+kPqVqomA5vq7kOh1I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=r++YG/pQ; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="r++YG/pQ" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6db7a8c6831so109995817b3.3 for ; Wed, 25 Sep 2024 08:02:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276562; x=1727881362; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=FlbORA03gt7VD7U3AKlte04ifRsDRozPK1w/WollpSI=; b=r++YG/pQlTOwXp9f1MnH7vTGQLeQr5cQaejWllulTN1n4LXJIpELvkvQZR5I4t0Jgw yS8YxYNmG+tCO3iprEn2zRX4KABA1nrOu7OorZ0caCZN5h52Oq1KB6LVv4RWSv0XLOTy mLnFb0YYsQ3Nh//iLrpL+xDtidxbAipOUHNd13y9OOvXSwDd6dX2mHnmaxQEEaQ274wo fJ5t1gHpnord3C5N2cPrvS+p+rS7cYl/f5yp7+JgHMOOLiwXg1M4YSIoy+MOTrruHRW/ 825Mzn7ewNtj0f6kEFj4ZoMoI+ieqmi/aXoCPE8VsmmEmlnV97dz2qm3cxvTVctsmLV4 wOtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276562; x=1727881362; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FlbORA03gt7VD7U3AKlte04ifRsDRozPK1w/WollpSI=; b=kOl0s7BvWye+Mr9r/RrHZp3s+cwet3WSqPJAAJAQrazliStkSDhsA0AdMGZ7IriW/7 6jmIOTgCA9kXSqSMnfpGHzei9BzpZAgHptsp2h62YD1DS85t2MowootKn1shSIsWnFUI yv8WzY+SEkcZmwSq/msT5Zo5m56XVCM3RkDnsG80GlLXsX6AYmFfk7g/86YDcK4l8bav DJvCIdQwgLk7HuQh4m6FJT4K1NeAC+VuIfwpqiwADAG2wEwbz6s7rr/Qt2RmUOGD1FCx d1fxM4Ig9LcdpJok6A0OKU5I+qU3mbAfFHOK3YVkXSpY957B4h7W3YQm0T7k7gKBlL4b E6mA== X-Gm-Message-State: AOJu0Yx0eIKfMbVaTrJWgMqm/6zt5s41feabyz8dgOMOEBq2rseo0v0B SLibe9sUDOyUbv8RogjCJHhctk1u6IC+PZmb1s7Ke3mz781aLlTJos2zVJ99/ZX8TubnRRzuQJS GnzhI6Bg8saL3qL2JAG2rpnV8/yTy4tKLP0thFlicY+sm+SrWPxF4bGeQtpkE6nZDTsBprXxzys SVMLCyBuclP7mEdbwsnHN39co61QV5JA== X-Google-Smtp-Source: AGHT+IH4yCJxpgJpRghyt05ffZ0KMtbzY3622CltTm7OPK0ZYLkhBwwkH9bUzMfV7VeGOafl80KA41By X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:690c:4289:b0:6e0:1ad:b197 with SMTP id 00721157ae682-6e21d9eb35cmr74667b3.3.1727276561223; Wed, 25 Sep 2024 08:02:41 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:20 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2120; i=ardb@kernel.org; h=from:subject; bh=udZt3Ybebg1JaBid31YzntWp5JIa5pAk0HVUlAzZyZ0=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6gljFsPIOfpfC+4mTz0SPOfTfNeM53artXe5vlz2r m2tZ4xiRykLgxgHg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZjI/0qG/z53N1inv844/EXP gLPr2aVVD8T/nDB5/WSxtlzL53WrfboZGX7d3NY04aKrkHqeft+Obu4DWbvCTcvYF6zT0yzuZn0 pwQAA X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-50-ardb+git@google.com> Subject: [RFC PATCH 20/28] x64/acpi: Use PIC-compatible references in wakeup_64.S From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Use ordinary RIP-relative references to make the code compatible with running the linker in PIE mode. Note that wakeup_long64() runs in the kernel's ordinary virtual mapping so there is no need to record the address of .Lresume_point in a global variable. And fix the comment while at it. Signed-off-by: Ard Biesheuvel --- arch/x86/kernel/acpi/wakeup_64.S | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/arch/x86/kernel/acpi/wakeup_64.S b/arch/x86/kernel/acpi/wakeup= _64.S index 94ff83f3d3fe..af2f2ed57658 100644 --- a/arch/x86/kernel/acpi/wakeup_64.S +++ b/arch/x86/kernel/acpi/wakeup_64.S @@ -14,7 +14,7 @@ =20 .code64 /* - * Hooray, we are in Long 64-bit mode (but still running in low memory) + * Hooray, we are in Long 64-bit mode */ SYM_FUNC_START(wakeup_long64) movq saved_magic(%rip), %rax @@ -40,7 +40,7 @@ SYM_FUNC_START(wakeup_long64) movq saved_rsi(%rip), %rsi movq saved_rbp(%rip), %rbp =20 - movq saved_rip(%rip), %rax + leaq .Lresume_point(%rip), %rax ANNOTATE_RETPOLINE_SAFE jmp *%rax SYM_FUNC_END(wakeup_long64) @@ -51,7 +51,7 @@ SYM_FUNC_START(do_suspend_lowlevel) xorl %eax, %eax call save_processor_state =20 - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq %rsp, pt_regs_sp(%rax) movq %rbp, pt_regs_bp(%rax) movq %rsi, pt_regs_si(%rax) @@ -70,8 +70,6 @@ SYM_FUNC_START(do_suspend_lowlevel) pushfq popq pt_regs_flags(%rax) =20 - movq $.Lresume_point, saved_rip(%rip) - movq %rsp, saved_rsp(%rip) movq %rbp, saved_rbp(%rip) movq %rbx, saved_rbx(%rip) @@ -88,7 +86,7 @@ SYM_FUNC_START(do_suspend_lowlevel) .align 4 .Lresume_point: /* We don't restore %rax, it must be 0 anyway */ - movq $saved_context, %rax + leaq saved_context(%rip), %rax movq saved_context_cr4(%rax), %rbx movq %rbx, %cr4 movq saved_context_cr3(%rax), %rbx @@ -137,7 +135,6 @@ saved_rsi: .quad 0 saved_rdi: .quad 0 saved_rbx: .quad 0 =20 -saved_rip: .quad 0 saved_rsp: .quad 0 =20 SYM_DATA(saved_magic, .quad 0) --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:15 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F2358192D98 for ; Wed, 25 Sep 2024 15:02:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276568; cv=none; b=ekp6z63v6ziZALJOjtu8aC3dbDz1Lz6PXqeYtC9omtzwtVIcHiF1Kb7oT5NVpeUIN0t4w3y0AndRbUSx707zurpdDDydikbd9tecerb9p7y10op1YcjdebT9jUlWdU6D5mDKR2C4OcSU1gJdOBnizTVCDCDyV5vW+NvI7x49M80= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276568; c=relaxed/simple; bh=3JGT0Gp6Mz6RRG9jsRwcVhBESThd+LENzWfNaIpsWgo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=n5nqr8rXolvN14idCeQwlAYxIhG8alR7VLTcT4qzykVpbB5ypIYo04AqjaRoJ1OStfxLxgtlP6PDvNXcRUk26BeE8KQhrhQ6yKVPZoqanUNmgMuRZVlVJLTVhCZGrK614T5/+wcGKgKlCWWfCTFBnCMETAHHE1BqcYRVGDAm5hQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TsAdLtYA; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TsAdLtYA" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e1159fb161fso9928679276.1 for ; Wed, 25 Sep 2024 08:02:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276564; x=1727881364; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=EvnpcmFQxcsGZvvXdDVNifJH/U/gKUWuX4k9X0rnjWY=; b=TsAdLtYAaeJGU3uxeCnFRgjYC3zywHtu6kW+6MWpOyRNzrSw08zUCHdh6CU0oPm8gr 54tOTeHyOuanpEbdNRoeq8LiHt0Ly0sdjAsUk0tK4vAtiLDvGVLI4ChjS1/H1NP68DS/ wSfZ51QMisCY3gLGmgurQEDRdSja9nk83ak8rOou2KXBBOUOMoZvo6HyUrCUqmKGFclq X79FiB7c9maDv32TWuRLLqa01GBzHCXym8JqphMQrvk/vyTCKLoJG/bfAtxBkbCui/JW prbpIXqx2mZ0S0rjKfDQ90v+oF35A3ySYc1z/4V/6oMFpYVBD7CNN2WPxnCQcpMGvSPl uaDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276564; x=1727881364; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EvnpcmFQxcsGZvvXdDVNifJH/U/gKUWuX4k9X0rnjWY=; b=wBb2sNT7M2D8SmYYO+mBWxJ3c4qJ7fmLEKsnPCYA25v8dsEcp6LE+yHvTBl9L8Uw54 LY9dA4F1R/ctZqbX8fDl9GzGvxim/ZVSKwDDBOcrRAjsChvoqDzMmYe5C44DPjh/Nj6i Itqh5bcGfjPRY+pQrmR306AdhdRUfQotzEqsSZYHNN1qN5UGbE3EVk4LnSGWBqtRtVOB 5KCHmoqMRU9HoCZtEGU478j0Rw6SLKLDmH6cLkxNXlQMsoEeCdjJnra/IMOnaO4LbXz/ mClG49nmIFVdsy9wruZyegcT/+q7qvYmit0r2A5+YLiXNOPYXwsaVvZgC4Z0chrKpQcX MH9A== X-Gm-Message-State: AOJu0Yw3b50dpqvkwZ39QM/fkdVsBV7z8wVYI5aL3yDQ42GbNsYt17h6 UaPMak+24Bv5u0197/FMOUYda8Gytydb/X9MQKxOi2eBu7g1t10X5OWVXaUb3/57uUWsfiWNbtq 5crwS8EXeA08wjHGF0tBa9BhdViXkUympXsmAa/0XFCyR/ForprGehjCB3qWGVX4yekON3hbpxx mqoQQxBqorulAvR1VldoBwDzuvId3kSA== X-Google-Smtp-Source: AGHT+IG8l7xPrwiQoCJNQ/ba3EMloi9V99YK0VEhh/KbnKIqVODzSQX4+kTe9p6XHA3xEE581cvXY/7/ X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a25:fc20:0:b0:e25:17cb:352e with SMTP id 3f1490d57ef6-e2517cb37ebmr1573276.9.1727276563646; Wed, 25 Sep 2024 08:02:43 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:21 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2665; i=ardb@kernel.org; h=from:subject; bh=aFznGPhrIJfcYbFW/zJm7PxtyN4XPqpBJvJpRLs7DOQ=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6smrOU/sUlwy2LTW237rV4pUONJ8T3XtdLO2DQ3bj 80/F1nfUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACYyyZHhn8LWprti0g+tdKf9 /bStUjGJXy/+qc4H120n962eP03h1yKG/wHHj7aK75zgynumw7x0V1PPmh2muXNy98WcvvZ74cO kJi4A X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-51-ardb+git@google.com> Subject: [RFC PATCH 21/28] x86/head: Use PIC-compatible symbol references in startup code From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Use RIP-relative symbol references to make them compatible with running the linker in PIE mode. Signed-off-by: Ard Biesheuvel --- arch/x86/kernel/head_64.S | 14 +++++++++----- arch/x86/kernel/relocate_kernel_64.S | 6 ++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index db71cf64204b..cc2fec3de4b7 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -182,8 +182,9 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_G= LOBAL) xorl %r15d, %r15d =20 /* Derive the runtime physical address of init_top_pgt[] */ - movq phys_base(%rip), %rax - addq $(init_top_pgt - __START_KERNEL_map), %rax + leaq init_top_pgt(%rip), %rax + subq $__START_KERNEL_map, %rax + addq phys_base(%rip), %rax =20 /* * Retrieve the modifier (SME encryption mask if SME is active) to be @@ -314,7 +315,8 @@ SYM_INNER_LABEL(common_startup_64, SYM_L_LOCAL) =20 .Lsetup_cpu: /* Get the per cpu offset for the given CPU# which is in ECX */ - movq __per_cpu_offset(,%rcx,8), %rdx + leaq __per_cpu_offset(%rip), %rdx + movq (%rdx,%rcx,8), %rdx #else xorl %edx, %edx /* zero-extended to clear all of RDX */ #endif /* CONFIG_SMP */ @@ -325,7 +327,8 @@ SYM_INNER_LABEL(common_startup_64, SYM_L_LOCAL) * * RDX contains the per-cpu offset */ - movq pcpu_hot + X86_current_task(%rdx), %rax + leaq pcpu_hot + X86_current_task(%rip), %rax + movq (%rax,%rdx), %rax movq TASK_threadsp(%rax), %rsp =20 /* @@ -346,7 +349,8 @@ SYM_INNER_LABEL(common_startup_64, SYM_L_LOCAL) */ subq $16, %rsp movw $(GDT_SIZE-1), (%rsp) - leaq gdt_page(%rdx), %rax + leaq gdt_page(%rip), %rax + addq %rdx, %rax movq %rax, 2(%rsp) lgdt (%rsp) addq $16, %rsp diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index e9e88c342f75..cbfd0227ea3e 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -106,6 +106,9 @@ SYM_CODE_START_NOALIGN(relocate_kernel) /* setup a new stack at the end of the physical control page */ lea PAGE_SIZE(%r8), %rsp =20 + /* take the virtual address of virtual_mapped() before jumping */ + leaq virtual_mapped(%rip), %r14 + /* jump to identity mapped page */ addq $(identity_mapped - relocate_kernel), %r8 pushq %r8 @@ -225,8 +228,7 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) movq %rax, %cr3 lea PAGE_SIZE(%r8), %rsp call swap_pages - movq $virtual_mapped, %rax - pushq %rax + pushq %r14 ANNOTATE_UNRET_SAFE ret int3 --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:15 2024 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B9F581991D8 for ; Wed, 25 Sep 2024 15:02:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276572; cv=none; b=GCc7xQW/zvJsi6fdwab/MCeFp3oPAYjQtBI6ohu549Uh6q4ksU/QSFYm/QlrfqSpgzy/TUNWkbSyr9zpVZSSZhO8/SbLZImXU1QZYmll10G55osV6hDBn6QuDZsuH2Wj5ygiiPGjGsDxy6oxiW4mzcCghnhgTPYM+uLNaDi44xQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276572; c=relaxed/simple; bh=4t4Zd7d88HUB4IQR7hyWqg3RF4QpMaNzLYFQ0q/SbWM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nFwBiMC7sNSCRc6i5Hf5mfZ/ZLx0a/5XeBjm7ptJdGK3HHJC1zlKu/NqmNwgkKa6gXNw8ILi15FmeQvBLcLhccmT3PHtbHHy6vgspHkNVO+5LKUIwFne3NHkA4sY+GKWcFUZVF8e3Pdapn6AeBpD16M+95GijZQxytfLPM9Pe/Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=xpdc7b0r; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xpdc7b0r" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-374bfc57e2aso3602173f8f.3 for ; Wed, 25 Sep 2024 08:02:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276567; x=1727881367; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=pNx8CgL2JjB1FACn8gZOCDgH3XlG/cZ7BMhQN82yM0M=; b=xpdc7b0reBbwnnjkdvySuBhqxCUup4ROen4arCBet5tBUn/jAPZrevZ0XydaQWVEdR NptfvC2h5L3r3+buanOYePlkab/2vDQbYR/I2i9wuWm4TvkCnKkf9vNjvdgt80uJ8+SW iLr4Wbpv+HxRref707wqaz2SoYCW0MfU9uJo8GCadxjy+7NiRAnESsNrx2pnflAkCsms o7NuB+N+ESfhIaXEtIqiTN+f95AS/6k7Spad2NqYoV//cdCFL0JwjorjV7Fhr1bPSoQ6 6GKDsf/GGwmOOxvTmd0CsCY8/y/kVK0718CGugnH/HehdzmKjcWznuNpm26kPgWMkSJ9 d2Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276567; x=1727881367; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pNx8CgL2JjB1FACn8gZOCDgH3XlG/cZ7BMhQN82yM0M=; b=b8a3f9WBqyrr5ZnezBigxhdmpO5L7g4iCIBh/A+OluAoGiVkbc7alLUBobEYueqpXs EPYrMLoP3+DgkHYF0+Q25Dj7ZZULd/3t8/gUu7iD9M52WwDa/gwBQIWL+92gSCb3eXuz WhrKNGjgJcrXwCBmaNVWEC2RLmSNDyGtLaSG/dsbJWN0HtD7D03vkL5XDv/K+lxpMldT PAalrgzPMImLNEVs0jE04NrtozEpFvzX9V/pbJABmfHrhV0t4GXJupIt7jkUhQPyiYQd +ezUiF14qH0NREH9U9yUbPUS0Osdqs5//EjKl5Yz24N1oX7OaAm5sfBVi/uZOWSDe9Vd dzDA== X-Gm-Message-State: AOJu0Yy6YUQkbklHSjsRcQHQTGy69aii9IRdKrSzUzUHKyq3IUh7G5hw qTseOSwvYaLg+3/MHYBh3ftDZ9AIzkKAySSHu8aeYsVANqeAX7+dcPNqHZTg4IXuKWEOvpaCvpf CJwpaj8EL3Gd3x7flD3sdmPApUsbIoquwWdESwdCc9FKdM9+kmq3s7zqpah89bUkCBfVk8kcIWB Bnj/T0ysTZpqb/qt/ls4TKt0Z+DD062A== X-Google-Smtp-Source: AGHT+IHtZA65C9eNefhgBWqmYh0kGm6u7UMHidCQ+DZrqbEOemMqvMt90MEtVjIphkQcIFg1qUNObHvF X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a5d:5e04:0:b0:37c:c51b:d07f with SMTP id ffacd0b85a97d-37cc51bd43bmr945f8f.9.1727276566478; Wed, 25 Sep 2024 08:02:46 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:22 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=4300; i=ardb@kernel.org; h=from:subject; bh=LZda20wnQiwQiTXt6tiHm30A1wODb0KBnxm/teE+Lu4=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6unSO+7/NsayfzpQ7uW+6/sf035bm8SjMXouItt6v vsqNLN3lLIwiHEwyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgIlUFzIyLClfembn6Wlfb79Z nLiyrmmade3VvmNH2xq0FwTP8FnOwsHwmyXhPONLDfu9MQfFQ+MyCsJWuy/VFOpa8t9x77FzLsv /8AAA X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-52-ardb+git@google.com> Subject: [RFC PATCH 22/28] asm-generic: Treat PIC .data.rel.ro sections as .rodata From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel When running the compiler in PIC/PIE mode, it will emit data objects that are 'const' in the context of the program into the .data.rel.ro section if they contain absolute addresses of statically allocated global objects. This helps the dynamic loader distinguish between objects that are truly const from objects that will need to be fixed up by the loader before starting the program. This is not a concern for the kernel, but it does mean those .data.rel.ro input sections need to be handled. So treat them as .rodata. It also means some explicit uses of .rodata for global structures containing absolute addresses need to be changed to .data.rel.ro to prevent the linker from warning about incompatible section flags. Signed-off-by: Ard Biesheuvel --- include/asm-generic/vmlinux.lds.h | 2 +- include/linux/compiler.h | 2 +- scripts/kallsyms.c | 2 +- tools/objtool/check.c | 11 ++++++----- tools/objtool/include/objtool/special.h | 2 +- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinu= x.lds.h index cc14d780c70d..2b079f73820f 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -456,7 +456,7 @@ . =3D ALIGN((align)); \ .rodata : AT(ADDR(.rodata) - LOAD_OFFSET) { \ __start_rodata =3D .; \ - *(.rodata) *(.rodata.*) \ + *(.rodata .rodata.* .data.rel.ro*) \ SCHED_DATA \ RO_AFTER_INIT_DATA /* Read only after init */ \ . =3D ALIGN(8); \ diff --git a/include/linux/compiler.h b/include/linux/compiler.h index ec55bcce4146..f7c48b7c0a6b 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -133,7 +133,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f,= int val, #define annotate_unreachable() __annotate_unreachable(__COUNTER__) =20 /* Annotate a C jump table to allow objtool to follow the code flow */ -#define __annotate_jump_table __section(".rodata..c_jump_table") +#define __annotate_jump_table __section(".data.rel.ro.c_jump_table") =20 #else /* !CONFIG_OBJTOOL */ #define annotate_reachable() diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 9c34b9397872..1700e97400aa 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -357,7 +357,7 @@ static void write_src(void) printf("#define ALGN .balign 4\n"); printf("#endif\n"); =20 - printf("\t.section .rodata, \"a\"\n"); + printf("\t.section .data.rel.ro, \"a\"\n"); =20 output_label("kallsyms_num_syms"); printf("\t.long\t%u\n", table_cnt); diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 01237d167223..04725bd83232 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -2575,15 +2575,16 @@ static void mark_rodata(struct objtool_file *file) * Search for the following rodata sections, each of which can * potentially contain jump tables: * - * - .rodata: can contain GCC switch tables - * - .rodata.: same, if -fdata-sections is being used - * - .rodata..c_jump_table: contains C annotated jump tables + * - .rodata .data.rel.ro : can contain GCC switch tables + * - .rodata. .data.rel.ro. : same, if -fdata-sections is bei= ng used + * - .data.rel.ro.c_jump_table : contains C annotated jump tables * * .rodata.str1.* sections are ignored; they don't contain jump tables. */ for_each_sec(file, sec) { - if (!strncmp(sec->name, ".rodata", 7) && - !strstr(sec->name, ".str1.")) { + if ((!strncmp(sec->name, ".rodata", 7) && + !strstr(sec->name, ".str1.")) || + !strncmp(sec->name, ".data.rel.ro", 12)) { sec->rodata =3D true; found =3D true; } diff --git a/tools/objtool/include/objtool/special.h b/tools/objtool/includ= e/objtool/special.h index 86d4af9c5aa9..89ee12b1a138 100644 --- a/tools/objtool/include/objtool/special.h +++ b/tools/objtool/include/objtool/special.h @@ -10,7 +10,7 @@ #include #include =20 -#define C_JUMP_TABLE_SECTION ".rodata..c_jump_table" +#define C_JUMP_TABLE_SECTION ".data.rel.ro.c_jump_table" =20 struct special_alt { struct list_head list; --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:15 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0B32D199E9F for ; Wed, 25 Sep 2024 15:02:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276573; cv=none; b=S68EdSgx2xS63gGXE+a1mgg/hXsoIwg+cJkX9ywboUlwJZNnzG/CMqyyL39JeQUB4Rf+6XFORHB4aW975xaUe9thHWw0G5cxXJvq/ykbVoZe9a+YBqerFcZ3oIvptjFzpJ3freWImpUTXaut4g08LmHfdHG/vQ0qe8tIUVCF5aw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276573; c=relaxed/simple; bh=EsUvuG/+LSwzxafWhuDBMbAgVDelZ/qjsxvBGmyQnpk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dzrsHH+Y/hC18GaUgRM3y8Lsp0hgcObcai53NhPyCZSRXhT3ipLAhlM+W6BQ0PgL0ahwwLVFcIH6wLCJDFK6ek1eW+p0NmZpmDR5Ztwt+cS1xuIVd+/t464qtSKz5YYCauU16WVu5o7zaVYZ5/SEAKKe5RuHcNkPClb4ohEkjrc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=P/ZioVwW; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="P/ZioVwW" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e03b3f48c65so9629186276.0 for ; Wed, 25 Sep 2024 08:02:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276569; x=1727881369; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=CRXmTW/MqFQ5H7gO5V6pXIYv4aezSxVZvDN4lDSH+kA=; b=P/ZioVwWWRNp9V7vflmCR+7le0HghWzxdDRiNOyUhKbgLsS3ekVJ3ImyQZX5WNmMac YOeG6qEKLLn0LIXg5bJsLosIOTxe9z5y4Sc/SsSyqYAFgZ3k6ljmlUa2MDTBX0CARbEf zke3q9XHmDmMgwQCBj1brJkMIT8Lmyj5RrM2gqAMWAfbTXFs8vZidD1mQdK2h+pPM+RZ MWtGEXkm+KTjRhK3w3cYOvhqT8U7zURD9goRiJg6KJmplj65HwOuVJdkgI7ZyAWCvmd+ UUvICkekTsTFylSmBxQdCWbt4uD/ZW73TRgAvQ3wzeNfJ4+6DDaqMMaWwgAGQuM3I25T L04A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276569; x=1727881369; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CRXmTW/MqFQ5H7gO5V6pXIYv4aezSxVZvDN4lDSH+kA=; b=gfdoyEnGBF6YDtuZUgOXAvePEqRzA1Oo5F0Eqo0983E17/g55GTYIzuNY9TOub+7e2 eI/opCCZyOpDYIs5sHg1e+nqszj/Gpz2W6JjvZImTvX1krS7zSCUiTMIo7mrPL5yV5Z2 e/aUTRCI7T6snsz3pzQYEHpg3cBnRHS3mv9aI6WsyWg19QMkMUxdeOhy5hv5GcmnWhmg 2z/+PGRTpddP8NBlFj4emnOme0Wi7zu8Go290RezuEL9/pEyka4MXv8/xDA305Ohjbp5 nR4DlvICT7u9Sk/xOt2FeFS4a76kEzw92U15W1WsusK2Q80KvC8jDfYpcjG1zMlxV+47 E3BQ== X-Gm-Message-State: AOJu0YwmLTGIPQz0cyvJ+1VB++FrNt9o7QsIaf9JiRcd6j4p4K5nnIGB vjqV9fP09Eg4tspUJmS6dwmqk8ElYj46bzcNNBpKK2H2w8tmw6yIROIO27oz8ceoIuQiH1d7jdo s7e7fhUwZayS6lyCrNd+WHDN/6JGfj4qmKXTnhbdvTk8/eWtcBYXaW+HeBaJeUOJr0tGR9A42GB d1R5cFYFnxwldAwaMW2KnaIElScbijtw== X-Google-Smtp-Source: AGHT+IEC1dPqUcAfKXMWGhZtWcpNQB40zyIE7CTDx8E05n1FmrMxkCCUsqBlLo74/YEl/S1/swIJOPiD X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a5b:841:0:b0:e1d:7ce:4844 with SMTP id 3f1490d57ef6-e24d7ee7f12mr17133276.4.1727276569059; Wed, 25 Sep 2024 08:02:49 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:23 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1309; i=ardb@kernel.org; h=from:subject; bh=AFLlY9eKyU6HJQ3jUUf7ewU02HHQByNyZjoPWXcfJAc=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6pkD1l1SMr9PdplxF72UMD0WHhu/+uT9iz8dfy0Km CzCpZrbUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACbCVcjIsG+u8YbnfWsvLOy8 8P/8g7N6wi1XH/y+rLV/v86EltfmLq8ZGR6dT3mnbCtQs96/+mLXk3mrOl5/49b4OFvBlCnX4bD oQm4A X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-53-ardb+git@google.com> Subject: [RFC PATCH 23/28] tools/objtool: Mark generated sections as writable From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel objtool generates ELF sections such as __mcount_loc, which carry absolute symbol references that need to be fixed up at boot time, based on the actual virtual placement of the kernel binary. This involves writing to the section at boot time, and in some cases (e.g., when using --pie and -z text), the lld linker is more pedantic about this, and complains about absolute relocations operating on read-only sections. None of this actually matters for vmlinux, which manages its own mapping permissions, and so we can just set the SHF_WRITE flag on those sections to make the linker happy. Signed-off-by: Ard Biesheuvel --- tools/objtool/elf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 3d27983dc908..26a39b010c92 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -1142,7 +1142,7 @@ struct section *elf_create_section(struct elf *elf, c= onst char *name, sec->sh.sh_entsize =3D entsize; sec->sh.sh_type =3D SHT_PROGBITS; sec->sh.sh_addralign =3D 1; - sec->sh.sh_flags =3D SHF_ALLOC; + sec->sh.sh_flags =3D SHF_ALLOC | SHF_WRITE; =20 /* Add section name to .shstrtab (or .strtab for Clang) */ shstrtab =3D find_section_by_name(elf, ".shstrtab"); --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:15 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 853AC19AA53 for ; Wed, 25 Sep 2024 15:02:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276576; cv=none; b=CKikvgMrtoEzrDwBoHRLJsq83NZ7+/TYGES6Vqrf4nO60zulRh0EAhIxowNdU970s/DUcZFstCAqz9L6P93+ivtQ6BcaTIa0TOflLH0rEOHLsJNITRghD5/RdBtncmgcUGB2Abasqy+7PEHkoXqpBaCkReeQx3Ge8Z+Ha3du4BQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276576; c=relaxed/simple; bh=31w9JHpb/FOSX9EXfChPlaKIxGcZxzD1C7F5EPjfIx4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lQvCH0ICzWkigG5ksTwyB9fNejh557FWusHMV5NKgtog6WynCu3b6Y7aZFFXb1yhsM538p7Dr8xh7qxj70DhYemz0dfuR/U6Nx8ILhEgsfoCaU4vchxme7/KKCvZXWtRPxrEYhfqR0eLeWGILhIR7fHVFHzL6Op9fltpJnM+zlg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=w+eQ9FA0; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="w+eQ9FA0" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6db7a8c6910so102668607b3.0 for ; Wed, 25 Sep 2024 08:02:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276572; x=1727881372; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=q7Siui3EQaHZbOP2Zf5u6bj1Z6F9xVc5htz8ViId7Yw=; b=w+eQ9FA0GHYJxNAkHx1mPkBCi8HnHJsS0BO2cqHqm9CxqwrOyv8yy9KCeHX9MPqrzD WS9F8awA4YQB1ioGO1mN4KYKqar9hOhf4KVwKcEFsPXO8r29khQ8Kl/6T2gjIfM6OK5l 1/jgqXc8G0q+vd2TZlBys/SWH1S8HQ2PZt1bpq5Xj+ieKrqR2i0WVtg6QZrv9g1LiRgg 0vyGmM5+XXz2LIHNn9bFbVtRBbGDoxksdqaBIkM9gVoomZ3RnD71aqDyQGosKbiNy579 lFYPlUJHdYzzUkbfGXvLJHP5uDGhkX6PWPQqK4zmsL96SVNwW49cfY7JJV8dcVrPCmzA MY0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276572; x=1727881372; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=q7Siui3EQaHZbOP2Zf5u6bj1Z6F9xVc5htz8ViId7Yw=; b=LA+BYi20xA1seUmpZWI913EaeZaZ1lzGL7QAuZJuoCLaqzKcOk99CvwsEwNWSGFtSr WJLjJs92I7BeAf0OSMaZsjYmR2NJa2Mu1ubX05+3wo7My7C+wHpimHm+X9ByiG7wFn/Z q9n4MvQkCIT+PbE7q4sDFYycLvBNgZzT8hhoJTfxGzMJLiHOY5Ra5AqVdREGlhoSRrL6 DYH+ZaAUbN0k106eH+jM/Fl05GmEKS7GvX2nvMMRV06utkm6bnXVC4MX+q8FKdPaQ+Ow nrV4Jhu+r5x+lV/bVYtAEk8vrZ8w3pM4JhBTbfWCl0CQ7FksXiNDbfBQzYIZhFoMWuxx 9ARg== X-Gm-Message-State: AOJu0YwxP5rSbKe3/UMZovhOWquui6Yg0iQesLpfTnqn7l6BEzUz5rKf kR6WhBj7LkfuxDTsQaju7IdRbjO5/RSDxk7cJwislXyu+2mDqJqn4NWjlL+LvimCkeanJfLCAqJ pOO4egAVxwFaMg5ojk+gpNYHO4wkr4AMgDkNB/2f9sEHM0tgW8xWItmjD+WUvi/7at9QlZ4I54c hW5s/WqEnDZrUdIYBTtUg+hurKHjVm2Q== X-Google-Smtp-Source: AGHT+IGUGK/IbClqwN6y6QvmI8hCfQn6s2hVSiVHB1d+dhN6jVJxxhzTiRxXUiYwgEqTcxKFspPDWKFo X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:690c:f90:b0:6c1:298e:5a7 with SMTP id 00721157ae682-6e21d9f2676mr100587b3.5.1727276571410; Wed, 25 Sep 2024 08:02:51 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:24 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2378; i=ardb@kernel.org; h=from:subject; bh=s5eg4POBUMz0+xutAa57cnb7wChxapKHTEsRfaj5xkM=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6rmrbx/K9pd0n9poZ7rKarM71/pb64Mu3OE7WFCew +trFZHXUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACYiIMfIsHzp8ptxntWap0qz /54LMvVhTo1RvbxcftLGA6VFgfY+Jxn+B/cyx9dHTT/wO+rQQaXszxPMFk1geHr9aqrVTv/NPxI dmAE= X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-54-ardb+git@google.com> Subject: [RFC PATCH 24/28] tools/objtool: Treat indirect ftrace calls as direct calls From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel In some cases, the compiler may rely on indirect calls using GOT slots as memory operands to emit function calls. This leaves it up to the linker to relax the call to a direct call if possible, i.e., if the destination address is known at link time and in range, which may not be the case when building shared libraries for user space. On x86, this may happen when building in PIC mode with ftrace enabled, and given that vmlinux is a fully linked binary, this relaxation is always possible, and therefore mandatory per the x86_64 psABI. This means that the indirect calls to __fentry__ that are observeable in vmlinux.o will have been converted to direct calls in vmlinux, and can be treated as such by objtool. Signed-off-by: Ard Biesheuvel --- tools/objtool/check.c | 32 ++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 04725bd83232..94a56099e22d 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -1696,11 +1696,39 @@ static int add_call_destinations(struct objtool_fil= e *file) struct reloc *reloc; =20 for_each_insn(file, insn) { - if (insn->type !=3D INSN_CALL) + if (insn->type !=3D INSN_CALL && + insn->type !=3D INSN_CALL_DYNAMIC) continue; =20 reloc =3D insn_reloc(file, insn); - if (!reloc) { + if (insn->type =3D=3D INSN_CALL_DYNAMIC) { + if (!reloc) + continue; + + /* + * GCC 13 and older on x86 will always emit the call to + * __fentry__ using a relaxable GOT-based symbol + * reference when operating in PIC mode, i.e., + * + * call *0x0(%rip) + * R_X86_64_GOTPCRELX __fentry__-0x4 + * + * where it is left up to the linker to relax this into + * + * call __fentry__ + * nop + * + * if __fentry__ turns out to be DSO local, which is + * always the case for vmlinux. Given that this + * relaxation is mandatory per the x86_64 psABI, these + * calls can simply be treated as direct calls. + */ + if (arch_ftrace_match(reloc->sym->name)) { + insn->type =3D INSN_CALL; + add_call_dest(file, insn, reloc->sym, false); + } + + } else if (!reloc) { dest_off =3D arch_jump_destination(insn); dest =3D find_call_destination(insn->sec, dest_off); =20 --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:15 2024 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B1BF4199958 for ; Wed, 25 Sep 2024 15:02:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276579; cv=none; b=kixYM258kBGE5RyzJNC/wkRgzkV8a5af7cVPuvSf0rqGNlLjN7TF1n5UWjn/MWg799FHcAITx4FwjzvZOzG6FUIoX8pc4ZBM5RtJ9VUCWf78COCoXt2RS8y+0Fj5I3Ny/4vCm3NY7MkryXVM3OjAj9diP+mDwQxd1Gyh/Xk1HEw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276579; c=relaxed/simple; bh=Ojp+f8Ba/j3ywSUpNkHMxJUaSZwEN3iyyriDyS6ns1Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Dsj/5FjQbD2OpLiWrQTMbQrR22exFiVMaEpMaDqK/PCk50w/vJeeXWX0LsnEwcmRZ1oeQBe4oycIUymeCf2T0V8XOVuSa0By1BD5GaTvEegcvCvcLcbpQWGrRdf0rCU4qL+oS7vUhNAbP5lsTraB5pFEs7Y10ZClYq+ISNd8omA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=y3T3x26s; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="y3T3x26s" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-42cb998fd32so47878575e9.1 for ; Wed, 25 Sep 2024 08:02:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276574; x=1727881374; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=O/+lA7axzWVX738v6MDlUUydsjXBcrFQn3zDJoV9Zlw=; b=y3T3x26sz6jhlZueXjpSSXL3amdsXx8Mbpd1yomI+UJdyfjbVZ3wxXIZu2eFjNJyEa mz+UPaOC2MS77QuK67CtQ5Ghwzedq/bAumcKNVhhTn5ZpFdC5dp8EDQ03RLeUZ0grXOy KY3BxHG2sAY3nwi4+XO4ywe7lyqJIYoly693zeMzurddsEafa5E2cPy88tCerp9YJVdi 6Y3CyMU3Q0DBNUkQVq63MZ5CVm7Wp/FdNAHI7XmYd7QbqrQcJPUcoAmiXiEa/WaJIyYp /AdSxodOvEYDXTWJ/4FTOoB8osxnYELNuHCDU3NUTwyEyIpVyXxbTS5RqNlxXpr3CMMW CbmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276574; x=1727881374; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=O/+lA7axzWVX738v6MDlUUydsjXBcrFQn3zDJoV9Zlw=; b=RoFaFri1c07zD1Fs4IVFMVTFC6BjA9dAtefc3YM7ijShaafIslyPxlJeQgG84YAOOs QnSbzij6X70MbiloRvJV+a0d+hahKtuIv+0+ZOpQSBKaMO94AQdZfJoBkIP9Mkp68Qy8 s4lkpcY/G2KE51eGlHVxhDOv95e9tTobOv7YgL+/Xlj0RY+40rYpV9El0lqK4Whz8FuR ZrCNymDSiYY4EUy1HM6XL/9g9xoiPkBt7IcpJSMGOXeljDb7b3gXtr4zwQhxXgEOSguQ a8J3f8a8FBP6jh7Cyx5I7Hd1SRLAm0VZcwYSwf6UIGLieccNpAZcnmi5tCHo/yQ6x9Np lfgg== X-Gm-Message-State: AOJu0YxcrS9rh01I9KraMAEZWwAv6BxXpTQy9Bu23+S8aOXv+p/0ez4/ dttm5Efeul4J7fidgJgojS0IA05vI7Chve6GLuOiIg66Q+Qq9xye+MpB+7+pq7mBsGf5+jQaK9T Z7PUw+oAaAatidLyMJpFysQG88S8a7Cwr3ZPke9n52oY+S+5MrHhTZuBE2A7zNdAw5aPYv2B/5h TkeGOiQQXsQ7LKh5EKMSoGS2/A9oOwbg== X-Google-Smtp-Source: AGHT+IFNuvMvba4376xFzsyJ1BUDwRQ77lLDg4NDppuY0o3Frk8EdeKG+tmu68nrB2OUMQBq6qGzN/uY X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a5d:54cc:0:b0:374:c4ab:a87 with SMTP id ffacd0b85a97d-37cc24daa31mr1689f8f.10.1727276573709; Wed, 25 Sep 2024 08:02:53 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:25 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=4435; i=ardb@kernel.org; h=from:subject; bh=kANZ4hMlm4/UDKey8x5hppB+h4SVBsDN4bipEdg9xMY=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6vknrr8ZmOQrG7s3Nc2KdXJ4vibh1dcKsQf8Readb AEJHvkdpSwMYhwMsmKKLAKz/77beXqiVK3zLFmYOaxMIEMYuDgFYCKL/Rj+Z1Y035g4dcnETV4f AzeGy0dnuYj7Gs68XGu9U01C7fDJWEaGptn9D/zPzyrzzArfXiITGJrvprug4muz0/qrb5bc6L/ LCQA= X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-55-ardb+git@google.com> Subject: [RFC PATCH 25/28] x86: Use PIE codegen for the core kernel From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel As an intermediate step towards enabling PIE linking for the 64-bit x86 kernel, enable PIE codegen for all objects that are linked into the kernel proper. This substantially reduces the number of relocations that need to be processed when booting a relocatable KASLR kernel. Before (size in bytes of the reloc table): 797372 arch/x86/boot/compressed/vmlinux.relocs After: 400252 arch/x86/boot/compressed/vmlinux.relocs Signed-off-by: Ard Biesheuvel --- arch/x86/Makefile | 11 ++++++++++- arch/x86/boot/Makefile | 1 + arch/x86/boot/compressed/Makefile | 2 +- arch/x86/entry/vdso/Makefile | 1 + arch/x86/realmode/rm/Makefile | 1 + include/asm-generic/vmlinux.lds.h | 1 + 6 files changed, 15 insertions(+), 2 deletions(-) diff --git a/arch/x86/Makefile b/arch/x86/Makefile index b78b7623a4a9..83d20f402535 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -193,13 +193,22 @@ else KBUILD_RUSTFLAGS +=3D -Cno-redzone=3Dy KBUILD_RUSTFLAGS +=3D -Ccode-model=3Dkernel =20 + PIE_CFLAGS-y :=3D -fpie -mcmodel=3Dsmall \ + -include $(srctree)/include/linux/hidden.h + + PIE_CFLAGS-$(CONFIG_CC_IS_GCC) +=3D $(call cc-option.-mdirect-exte= rn-access) + PIE_CFLAGS-$(CONFIG_CC_IS_CLANG) +=3D -fdirect-access-external-data + ifeq ($(CONFIG_STACKPROTECTOR),y) KBUILD_CFLAGS +=3D -mstack-protector-guard-symbol=3Dfixed_= percpu_data + + # the 'small' C model defaults to %fs + PIE_CFLAGS-$(CONFIG_SMP) +=3D -mstack-protector-guard-reg= =3Dgs endif =20 # Don't emit relaxable GOTPCREL relocations KBUILD_AFLAGS_KERNEL +=3D -Wa,-mrelax-relocations=3Dno - KBUILD_CFLAGS_KERNEL +=3D -Wa,-mrelax-relocations=3Dno + KBUILD_CFLAGS_KERNEL +=3D -Wa,-mrelax-relocations=3Dno $(PIE_CFLAG= S-y) endif =20 # diff --git a/arch/x86/boot/Makefile b/arch/x86/boot/Makefile index 9cc0ff6e9067..4d3ba35cb619 100644 --- a/arch/x86/boot/Makefile +++ b/arch/x86/boot/Makefile @@ -57,6 +57,7 @@ KBUILD_AFLAGS :=3D $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS +=3D $(call cc-option,-fmacro-prefix-map=3D$(srctree)/=3D) KBUILD_CFLAGS +=3D -fno-asynchronous-unwind-tables KBUILD_CFLAGS +=3D $(CONFIG_CC_IMPLICIT_FALLTHROUGH) +KBUILD_CFLAGS_KERNEL :=3D =20 $(obj)/bzImage: asflags-y :=3D $(SVGA_MODE) =20 diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/M= akefile index f2051644de94..c362d36b5b69 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -73,7 +73,7 @@ LDFLAGS_vmlinux +=3D -T hostprogs :=3D mkpiggy HOST_EXTRACFLAGS +=3D -I$(srctree)/tools/include =20 -sed-voffset :=3D -e 's/^\([0-9a-fA-F]*\) [ABCDGRSTVW] \(_text\|__start_rod= ata\|__bss_start\|_end\)$$/\#define VO_\2 _AC(0x\1,UL)/p' +sed-voffset :=3D -e 's/^\([0-9a-fA-F]*\) [ABbCDdGRSTtVW] \(_text\|__start_= rodata\|__bss_start\|_end\)$$/\#define VO_\2 _AC(0x\1,UL)/p' =20 quiet_cmd_voffset =3D VOFFSET $@ cmd_voffset =3D $(NM) $< | sed -n $(sed-voffset) > $@ diff --git a/arch/x86/entry/vdso/Makefile b/arch/x86/entry/vdso/Makefile index c9216ac4fb1e..7af9fecf9abb 100644 --- a/arch/x86/entry/vdso/Makefile +++ b/arch/x86/entry/vdso/Makefile @@ -141,6 +141,7 @@ endif endif =20 $(obj)/vdso32.so.dbg: KBUILD_CFLAGS =3D $(KBUILD_CFLAGS_32) +$(obj)/vdso32.so.dbg: KBUILD_CFLAGS_KERNEL :=3D =20 $(obj)/vdso32.so.dbg: $(obj)/vdso32/vdso32.lds $(vobjs32) FORCE $(call if_changed,vdso_and_check) diff --git a/arch/x86/realmode/rm/Makefile b/arch/x86/realmode/rm/Makefile index a0fb39abc5c8..70bf0a26da91 100644 --- a/arch/x86/realmode/rm/Makefile +++ b/arch/x86/realmode/rm/Makefile @@ -67,3 +67,4 @@ KBUILD_CFLAGS :=3D $(REALMODE_CFLAGS) -D_SETUP -D_WAKEUP \ -I$(srctree)/arch/x86/boot KBUILD_AFLAGS :=3D $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS +=3D -fno-asynchronous-unwind-tables +KBUILD_CFLAGS_KERNEL :=3D diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinu= x.lds.h index 2b079f73820f..3a084ac77109 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -349,6 +349,7 @@ *(DATA_MAIN) \ *(.data..decrypted) \ *(.ref.data) \ + *(.data.rel*) \ *(.data..shared_aligned) /* percpu related */ \ *(.data.unlikely) \ __start_once =3D .; \ --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:15 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A5C4519CC36 for ; Wed, 25 Sep 2024 15:02:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276580; cv=none; b=q1hsMDMXZzPDxmNsY3gYJNULv+BYzr04uZ2sGerVhUHznvVggYYqU928EwmnQWZKWgHh0UJPIDE+2JujyH34vDSNxRntUvXP7ifZldEdWX2C7C1yCOzQ8JdE6PCJz0BzOtr6JBW1hU+vpv/vSJ5rLdOFda9ebl28IrpMydQhyZw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276580; c=relaxed/simple; bh=nfca0ivNA/FjKhvJ9VN6/KPVpcFIyJuoOSLlvvE4PM8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UvvFofUDMKAG8Vd+93fnApjmfWSz7NO6Pru9ZheWhkwZtbhOHoAs3W8w5qBbJI8NotzsoeEVN3MSIZvnHx1W3exWoV0wHnAkSyS9DOtCizwspuyXjtymqRzLVHvB9MN89Y8nG1CqHEg2jtmJ5R5Zm7lAxsxDqEV/mfJtyAXJAXU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eNTQyg2l; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eNTQyg2l" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6886cd07673so105563037b3.3 for ; Wed, 25 Sep 2024 08:02:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276577; x=1727881377; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NyUlP50R7ff/vN5G1h18De2z2GbJesVTam/AShMXJgw=; b=eNTQyg2lIQxS1jUH1eGnrfN/53xutQ7YWcqhzpwTNuqtU1W6LnL7QYmH5g/3fSqAz9 VqNQvnuvtV5an5EBUer6j5lb3qwDEJYNzur/gSh0bHFu0lQvuI+TfkmKW++fKwwLoE6H v0912UP4k6+vtap43A1zTat4omG2RZhyxNRGv5KNSyIRCMcS38YUfTZin4SYIMcoYleM AcEtnfFh64077WQVcT+14hPoQ1X21WT/pkAbk041Zav9D21y9K3a6JQeLVMLrhruzYeM oqwUzIcx7q5JXlESKOUEMPEkbqntST7KjxgDvXWV2Ry+805T5T/lZmAjWhnYnTIAM+dF H4Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276577; x=1727881377; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NyUlP50R7ff/vN5G1h18De2z2GbJesVTam/AShMXJgw=; b=n2StYYpS4u76Wfl2LIrU1c02CmhIQpFd6RPY1mqxdKvKojikzcuh+G2Ba2ba4oie3L /lp+1X8ns+QKDoTLJzkXhSglBe24uyP3wK+wiSbI/HmsT58ujtjkvweqsNwZjbj6+Y3v zU5dP4Dd1QA0GV78F5RuyzDQug1c49FmSQqgdiytJJzVufp/N0YOvDdrOX3WiKO7WYlT vNjw3DW9nqmF4Y3kIoqShdeDiuNfVMBmmx3NuYCfPqBbfJ0R3XlsGDeWUPM/hFLMS0Zq PT1ny83PcchAyYhDZZaUUjpIbzhoOkDbvJdacg3Cugm48pKE2C2JwGq51quM7XfvFt6Q k1RA== X-Gm-Message-State: AOJu0YyZ/fcWlD2p7xoB3SedJXr6zxbuYW5kKPx6H84uIGt9Et7h5SpY fojHVIdt3r3IxYQ7W8wqs40z1qXxP9ZgYYnyJQaAvLe5BGNVvkWyTmqLCmr/lEVHetTM/bE/5Bj FNyDp3QgRe1kNuKYJKnuJtBExcFj4hZuBKRmXHGI4Ag0a9BjAFwIP269NcyktWi4Syh0QE3qfkL 1DkTZPwun+g6DH9nc3kCqBbtwO4tJluA== X-Google-Smtp-Source: AGHT+IF9cT+1HnKGMG9rCL4XBpFAiRqL2rxQrsa7AeT9AU3GHDLsQi/G2t9Fg7OjYNkaCpx/AYMRBr5P X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:690c:3149:b0:6de:19f:34d7 with SMTP id 00721157ae682-6e21d81e811mr94787b3.2.1727276576540; Wed, 25 Sep 2024 08:02:56 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:26 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=7472; i=ardb@kernel.org; h=from:subject; bh=ZjnajWIKcY5DrOh/WpJS8LufZBk9grHctEdumwSdFZ4=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6oXaCAm2n4qhvN+cWPhMgzpaNBxMv/te55wX/7Z/7 fXGH00dpSwMYhwMsmKKLAKz/77beXqiVK3zLFmYOaxMIEMYuDgFYCJp0owMs7Y8P9MQNOWv4+0U 10OXF7hWP92jueKnm9Tq9WavZtsdSmX4Z/Uk0GTGvv61f15k371aLH96espP9/0Kyz6kZF4+IJv 9hR8A X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-56-ardb+git@google.com> Subject: [RFC PATCH 26/28] x86/boot: Implement support for ELF RELA/RELR relocations From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Add support for standard dynamic ELF relocations to perform the virtual relocation of the core kernel at boot. The RELR format results in a 10x reduction in memory footprint of the relocation data, and can be generated by the linker directly. This removes the need for a) a host tool 'relocs' and a bespoke, clunky relocation table format where the table is simply concatenated to the vmlinux payload when building the decompressor; b) dependence on the --emit-relocs linker switch, which dumps static, intermediate build time relocations into the ELF binary, to be subsequently used as runtime relocations. The latter is especially problematic, as linkers may apply relaxations that result in the code going out of sync with the static relocation that annotated it in the input. This requires additional work on the part of the linker to update the static relocation, which is not even possible in all cases. Therefore, it is much better to consume a runtime, dynamic relocation format in the way it was intended. This will require switching to linking vmlinux in PIE mode - this is implemented in a subsequent patch. Signed-off-by: Ard Biesheuvel --- Documentation/arch/x86/zero-page.rst | 3 +- arch/x86/Kconfig | 1 + arch/x86/include/asm/setup.h | 1 + arch/x86/include/uapi/asm/bootparam.h | 2 +- arch/x86/kernel/head64.c | 36 ++++++++++++++++++++ arch/x86/kernel/head_64.S | 5 +++ arch/x86/kernel/vmlinux.lds.S | 24 +++++++++---- 7 files changed, 64 insertions(+), 8 deletions(-) diff --git a/Documentation/arch/x86/zero-page.rst b/Documentation/arch/x86/= zero-page.rst index 45aa9cceb4f1..fd18b77113e2 100644 --- a/Documentation/arch/x86/zero-page.rst +++ b/Documentation/arch/x86/zero-page.rst @@ -3,7 +3,7 @@ =3D=3D=3D=3D=3D=3D=3D=3D=3D Zero Page =3D=3D=3D=3D=3D=3D=3D=3D=3D -The additional fields in struct boot_params as a part of 32-bit boot +The additional fields in struct boot_params as a part of 32/64-bit boot protocol of kernel. These should be filled by bootloader or 16-bit real-mode setup code of the kernel. References/settings to it mainly are in:: @@ -20,6 +20,7 @@ Offset/Size Proto Name Meaning 060/010 ALL ist_info Intel SpeedStep (IST) BIOS support information (struct ist_info) 070/008 ALL acpi_rsdp_addr Physical address of ACPI RSDP table +078/008 64-bit kaslr_va_shift Virtual kASLR displacement of the core ker= nel 080/010 ALL hd0_info hd0 disk parameter, OBSOLETE!! 090/010 ALL hd1_info hd1 disk parameter, OBSOLETE!! 0A0/010 ALL sys_desc_table System description table (struct sys_desc_tab= le), diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 2852fcd82cbd..54cb1f14218b 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -26,6 +26,7 @@ config X86_64 depends on 64BIT # Options that are inherently 64-bit kernel only: select ARCH_HAS_GIGANTIC_PAGE + select ARCH_HAS_RELR select ARCH_SUPPORTS_INT128 if CC_HAS_INT128 select ARCH_SUPPORTS_PER_VMA_LOCK select ARCH_SUPPORTS_HUGE_PFNMAP if TRANSPARENT_HUGEPAGE diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h index 85f4fde3515c..a4d7dd81f773 100644 --- a/arch/x86/include/asm/setup.h +++ b/arch/x86/include/asm/setup.h @@ -51,6 +51,7 @@ extern void reserve_standard_io_resources(void); extern void i386_reserve_resources(void); extern unsigned long __startup_64(unsigned long p2v_offset, struct boot_pa= rams *bp); extern void startup_64_setup_gdt_idt(void); +extern void startup_64_apply_relocations(struct boot_params *bp); extern void early_setup_idt(void); extern void __init do_early_exception(struct pt_regs *regs, int trapnr); =20 diff --git a/arch/x86/include/uapi/asm/bootparam.h b/arch/x86/include/uapi/= asm/bootparam.h index 9b82eebd7add..3389b1be234c 100644 --- a/arch/x86/include/uapi/asm/bootparam.h +++ b/arch/x86/include/uapi/asm/bootparam.h @@ -120,7 +120,7 @@ struct boot_params { __u64 tboot_addr; /* 0x058 */ struct ist_info ist_info; /* 0x060 */ __u64 acpi_rsdp_addr; /* 0x070 */ - __u8 _pad3[8]; /* 0x078 */ + __u64 kaslr_va_shift; /* 0x078 */ __u8 hd0_info[16]; /* obsolete! */ /* 0x080 */ __u8 hd1_info[16]; /* obsolete! */ /* 0x090 */ struct sys_desc_table sys_desc_table; /* obsolete! */ /* 0x0a0 */ diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c index 49e8ba1c0d34..6609e1012f2f 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -20,6 +20,7 @@ #include #include #include +#include #include =20 #include @@ -588,3 +589,38 @@ void __head startup_64_setup_gdt_idt(void) =20 startup_64_load_idt(handler); } + +#ifdef CONFIG_RELOCATABLE +void __head startup_64_apply_relocations(struct boot_params *bp) +{ + extern const Elf64_Rela __rela_start[], __rela_end[]; + extern const u64 __relr_start[], __relr_end[]; + u64 va_offset =3D (u64)RIP_REL_REF(_text) - __START_KERNEL; + u64 va_shift =3D bp->kaslr_va_shift; + u64 *place =3D NULL; + + if (!va_shift) + return; + + for (const Elf64_Rela *r =3D __rela_start; r < __rela_end; r++) { + if (ELF64_R_TYPE(r->r_info) !=3D R_X86_64_RELATIVE) + continue; + + place =3D (u64 *)(r->r_offset + va_offset); + *place +=3D va_shift; + } + + for (const u64 *rel =3D __relr_start; rel < __relr_end; rel++) { + if ((*rel & 1) =3D=3D 0) { + place =3D (u64 *)(*rel + va_offset); + *place++ +=3D va_shift; + continue; + } + + for (u64 *p =3D place, r =3D *rel >> 1; r; p++, r >>=3D 1) + if (r & 1) + *p +=3D va_shift; + place +=3D 63; + } +} +#endif diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index cc2fec3de4b7..88cdc5a0c7a3 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -74,6 +74,11 @@ SYM_CODE_START_NOALIGN(startup_64) cdq wrmsr =20 +#ifdef CONFIG_RELOCATABLE + movq %r15, %rdi + call startup_64_apply_relocations +#endif + call startup_64_setup_gdt_idt =20 /* Now switch to __KERNEL_CS so IRET works reliably */ diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S index 52b8db931d0f..f7e832c2ac61 100644 --- a/arch/x86/kernel/vmlinux.lds.S +++ b/arch/x86/kernel/vmlinux.lds.S @@ -240,6 +240,18 @@ xen_elfnote_phys32_entry_offset =3D :init #endif =20 + .init.rela : { + __rela_start =3D .; + *(.rela.*) *(.rela_*) + __rela_end =3D .; + } + + .init.relr : { + __relr_start =3D .; + *(.relr.*) + __relr_end =3D .; + } + /* * Section for code used exclusively before alternatives are run. All * references to such code must be patched out by alternatives, normally @@ -469,12 +481,6 @@ xen_elfnote_phys32_entry_offset =3D *(.got) *(.igot.*) } ASSERT(SIZEOF(.got) =3D=3D 0, "Unexpected GOT entries detected!") -#endif - - .plt : { - *(.plt) *(.plt.*) *(.iplt) - } - ASSERT(SIZEOF(.plt) =3D=3D 0, "Unexpected run-time procedure linkages det= ected!") =20 .rel.dyn : { *(.rel.*) *(.rel_*) @@ -485,6 +491,12 @@ xen_elfnote_phys32_entry_offset =3D *(.rela.*) *(.rela_*) } ASSERT(SIZEOF(.rela.dyn) =3D=3D 0, "Unexpected run-time relocations (.rel= a) detected!") +#endif + + .plt : { + *(.plt) *(.plt.*) *(.iplt) + } + ASSERT(SIZEOF(.plt) =3D=3D 0, "Unexpected run-time procedure linkages det= ected!") } =20 /* --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:15 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA4C919D084 for ; Wed, 25 Sep 2024 15:03:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276583; cv=none; b=TWAm2kAWEeA9zucYi0kUnetX+EaOJM7+k+6OasLM7f8YAyjgWMfLZJNhmCZn0LJEha7zMimynRXGJLuOZsefbwbXkFCqz3r3yqcOj49+PUDIQVWdrC6wOp08AAqqPdOfXc/k+mJLUFLAtbZHy2cy3CU7fNcblqKlwk8CIZKYqMo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276583; c=relaxed/simple; bh=uKiTAUlG9Mzz44mB3boSUKqgle05lQ6M72q35xSoV8U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UHohg/QfBxEOCrIpTUDuJ1gs2UTHSNRy85EE45IGEjJFlfNqfUV1TURSAUIuM9dFdRF8GRFhM4ptBlbNP+Bbp97YVtJmNnbsjeB2R7fhRFAESrM/q4OXhQU46lbYYzlufmEk7Z89ONSb1qPcZQ/Kf9aQ3rkIhCo3qcMw3bUqea8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=0NPhHKb/; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0NPhHKb/" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e035949cc4eso10636172276.1 for ; Wed, 25 Sep 2024 08:03:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276579; x=1727881379; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7IM+8uvbpUOTrNblc27FtujIjdoMKE18YT3ThvhmVkQ=; b=0NPhHKb/cB/kfABblGRV87mUbtOZiQ23xWBDVA4HKp3uVw/kISmKVdEXt12DJP8dql ut/iLHxjOOdNKfnlTVxEX5MLgMxy5HYPkt3YCbypmOPElZsYPLhriYBBnUdBnGxOBEO5 1oAoWwxxAE2VIhc/CbQQ+GEasKU102YSFcTts6c/dTqRKr+dxqDcOTdnYLkU4sKTVeVo y+Sr/Tc0sbK7/dKsfaelzVw9bAg383vIObPIgncNdAnX+C0dZIILFIYQHFI+2v+8egWi YR7Cup7w/rNQGSyVrAEd6v5/sAVknsYO8d3RTlxQxOQ0SnAW85zzNVb9wt5CwXgx0tOY J3Ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276579; x=1727881379; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7IM+8uvbpUOTrNblc27FtujIjdoMKE18YT3ThvhmVkQ=; b=uNExfxgDsjFKnlp6teV3I69cVgjKhysO+wpgVec7dV+HyGDK3v/LdyTTRPuTNtCaoZ UrByx4n+pgjl4IUC5pIK5fyvEQoNQ/kIFV5ZcIty+mwD5b8CYolNW+nVp11q1A+l+y3A o6ou9+ASPVwH17d1fNQYwR3czy3oMsKyJPaJJ+xFxOv2DWs8VVbZQ8OCGvAo4O++Xu9Y L1JBo7lR93oh50LWkSzaASg21Ldn5KZL/soD0F8ecRLG7KhpS8ElMRQT48IeTkv7wFUp 4FOGpU5s97SJnJ71Pp9X1dnJ88uVvt96Y5a2ulbH28qIOACVuZ7Awzbbo4KrMOycnNnP yjEA== X-Gm-Message-State: AOJu0YzrcBLEvZGMUV9+tpMKVN6dCNLm43tYJfEZ/2DNOumvohPFYbHi bEMm4RuQ/d1KTZVvGmJ5DfG8yRlgUtyfLtmDNjt+RHVL6gMyaWB8M1SO9J+Iw5hwujjjFIx5a7E FHj7sxRruXHUgDb/8/Gr1gsq+T2+bxcKsSOWzkZ2JX4/lEMDk9OR9g8WzlI+0CT4vPHUOQ/RXj2 vMgpSeXmRl/Wn7kG/lKRxWxN0+nV/yzA== X-Google-Smtp-Source: AGHT+IHCtqA+oNLQfmXqpM//kOUUkVuRiayEu9UAQA8X933VnnJ2JGMGIKXjp5QscVvSLG685e3KtDKL X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:6902:1782:b0:e0b:958a:3344 with SMTP id 3f1490d57ef6-e24da39b0c3mr17940276.10.1727276578847; Wed, 25 Sep 2024 08:02:58 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:27 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=4280; i=ardb@kernel.org; h=from:subject; bh=Qfoxr/CYN1brA02vZLn1djUOVE7sfU2DhAxM/gB47SE=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6qUIX51znF3nJyzZfmoB13+eT2d+37/IY95y8cys2 5n+JYzJHaUsDGIcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAifvYMv5jZZ0me+ya2VVnU Ozku0cJESLKOW+VHtMSKvW8vdx2dKMfIsJzzq0hJkqFDXdOunnmqwir812MtQuf0H13yi9n0fe8 DLgA= X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-57-ardb+git@google.com> Subject: [RFC PATCH 27/28] x86/kernel: Switch to PIE linking for the core kernel From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Build the kernel as a Position Independent Executable (PIE). This results in more efficient relocation processing for the virtual displacement of the kernel (for KASLR). More importantly, it instructs the linker to generate what is actually needed (a program that can be moved around in memory before execution), which is better than having to rely on the linker to create a position dependent binary that happens to tolerate being moved around after poking it in exactly the right manner. Note that this means that all codegen should be compatible with PIE, including Rust objects, so this needs to switch to the small code model with the PIE relocation model as well. Signed-off-by: Ard Biesheuvel --- arch/x86/Kconfig | 2 +- arch/x86/Makefile | 11 +++++++---- arch/x86/boot/compressed/misc.c | 2 ++ arch/x86/kernel/vmlinux.lds.S | 5 +++++ drivers/firmware/efi/libstub/x86-stub.c | 2 ++ 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 54cb1f14218b..dbb4d284b0e1 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2187,7 +2187,7 @@ config RANDOMIZE_BASE # Relocation on x86 needs some additional build support config X86_NEED_RELOCS def_bool y - depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE) + depends on X86_32 && RELOCATABLE =20 config PHYSICAL_ALIGN hex "Alignment value to which kernel should be aligned" diff --git a/arch/x86/Makefile b/arch/x86/Makefile index 83d20f402535..c1dcff444bc8 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -206,9 +206,8 @@ else PIE_CFLAGS-$(CONFIG_SMP) +=3D -mstack-protector-guard-reg= =3Dgs endif =20 - # Don't emit relaxable GOTPCREL relocations - KBUILD_AFLAGS_KERNEL +=3D -Wa,-mrelax-relocations=3Dno - KBUILD_CFLAGS_KERNEL +=3D -Wa,-mrelax-relocations=3Dno $(PIE_CFLAG= S-y) + KBUILD_CFLAGS_KERNEL +=3D $(PIE_CFLAGS-y) + KBUILD_RUSTFLAGS_KERNEL +=3D -Ccode-model=3Dsmall -Crelocation-mod= el=3Dpie endif =20 # @@ -264,12 +263,16 @@ else LDFLAGS_vmlinux :=3D endif =20 +ifdef CONFIG_X86_64 +ldflags-pie-$(CONFIG_LD_IS_LLD) :=3D --apply-dynamic-relocs +ldflags-pie-$(CONFIG_LD_IS_BFD) :=3D -z call-nop=3Dsuffix-nop +LDFLAGS_vmlinux +=3D --pie -z text $(ldflags-pie-y) + # # The 64-bit kernel must be aligned to 2MB. Pass -z max-page-size=3D0x200= 000 to # the linker to force 2MB page size regardless of the default page size us= ed # by the linker. # -ifdef CONFIG_X86_64 LDFLAGS_vmlinux +=3D -z max-page-size=3D0x200000 endif =20 diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/mis= c.c index 89f01375cdb7..79e3ffe16f61 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -495,6 +495,8 @@ asmlinkage __visible void *extract_kernel(void *rmode, = unsigned char *output) error("Destination virtual address changed when not relocatable"); #endif =20 + boot_params_ptr->kaslr_va_shift =3D virt_addr - LOAD_PHYSICAL_ADDR; + debug_putstr("\nDecompressing Linux... "); =20 if (init_unaccepted_memory()) { diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S index f7e832c2ac61..d172e6e8eaaf 100644 --- a/arch/x86/kernel/vmlinux.lds.S +++ b/arch/x86/kernel/vmlinux.lds.S @@ -459,6 +459,11 @@ xen_elfnote_phys32_entry_offset =3D =20 DISCARDS =20 + /DISCARD/ : { + *(.dynsym .gnu.hash .hash .dynamic .dynstr) + *(.interp .dynbss .eh_frame .sframe) + } + /* * Make sure that the .got.plt is either completely empty or it * contains only the lazy dispatch entries. diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi= /libstub/x86-stub.c index f8e465da344d..5c03954924fe 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -912,6 +912,8 @@ static efi_status_t efi_decompress_kernel(unsigned long= *kernel_entry) if (status !=3D EFI_SUCCESS) return status; =20 + boot_params_ptr->kaslr_va_shift =3D virt_addr - LOAD_PHYSICAL_ADDR; + entry =3D decompress_kernel((void *)addr, virt_addr, error); if (entry =3D=3D ULONG_MAX) { efi_free(alloc_size, addr); --=20 2.46.0.792.g87dc391469-goog From nobody Fri Nov 29 00:51:15 2024 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 177F219FA76 for ; Wed, 25 Sep 2024 15:03:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276587; cv=none; b=oPQxp/TTTdmCyVSC2Zgc/wy3wcQQdMYjCq763FUJrEolJGrunVYtdM9lEP25ou94f6gUyKSA0TlsML2S3yaE2c9caQamaFIh4+05K+reWNO1nyHCdn7/mBTA9fFf+1iMyw9G+V34ugbJYjYbHWPARMXdspXajxbZ51p9lhXzFzU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727276587; c=relaxed/simple; bh=YvM9TRdamHMMVDaQnr/PY1VL/3cpzPBrLHBshWo7LWw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CWT9BBi+nFZLqmVAaW2opJ8nfiJS9wXD1vBM4gJm5U5ExW+tN7UPLdyewOdWlM6nceURx7EDlwGpftHmVzqsjHafyILi4pRhvZHFyEBer/E3KXwqirS/xa2Bj1C8Cp4e5HX0rxPp4ohbj9eJ9+X7gJ1fbrgPxCwoW1Y5TAPEp+A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ongWkgeN; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ongWkgeN" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-42cbcf60722so52510645e9.1 for ; Wed, 25 Sep 2024 08:03:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727276581; x=1727881381; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=H/fxT1DdHMwMyeW8np4BBTWW8phQCD6XQ4SETP482c8=; b=ongWkgeNEUsXO/yFix7MO/NjZ2xvYjoDtIUAZporkb9MuWeyH9GspZlyxNuptzMS9z 27Wcx+k6toKj75Lqn7eTnYkTBw2H2B/ooJKtEmb4PCjaA7tabdO0KaZ0MASZ2VwCe6tF dCZWRxhB0Df0qIrum8EqqV+z13+lw9hhfUWtEGwpL0BsUWnrImwdQkmrlVuWFiGfgQHl eZS6XOrp4ju9m/p48G2u5gOy3ga2/DqLQRrgaaVl4yJsa7a2bT7kHX5MYA4LyuWo4zSD gRzCcLC+XGFxdW9bdOm28v1dXeZRvGrlwwA3ODwA2n6vokwpU4qtgvNJ+5kk0Dpe76FB qzQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727276581; x=1727881381; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=H/fxT1DdHMwMyeW8np4BBTWW8phQCD6XQ4SETP482c8=; b=TBZt1UfndPrVyk+YXC7p+hQ+phTJvKHh3Uh9Bqshc3s9TymdnqooPXjA87eBUIKZcf X6/JC3GZIeZUZlA5fAuRfWJ73pdHhfWGzY66h/rK4bEgZuXuKIYBT7gAkkZXDKjcmMYX cYwJeYobNFa3VmzmghXUp84HOSC+Spjz1czaSB8CEj9xryS9+TaR1MERbdX8JJM9eOAv Lyy5pDzcEBNajybDNFSkaG0MJs+K1RVW7Fl6nrbdbFYbyN1ZAtgFPbovuSdzbvhEu0b5 5evtc4+Sr2QRvvtVLGkueZ0IVTpBBEbKEzU68t4UHTAkDhRZvVTXNd1pqiqksFPZcEA7 ROrQ== X-Gm-Message-State: AOJu0YyJZQpwyS7IdIyH+R4rgsZ1yynW6X6hgr1xmI+87E8uv0E1iSAc I6yNZk+kXJG9nRRMVF4KmAcZde07LohLs35Ato8y7xKfsVzM6Q/XtoOP79jD7omhISo+sPPrM5o vLUVXH75k4CyFsnUzRQMpqc9A3xXboHY2JNLsCBSSDvJOmNw0IPnGaBEA2Pd9GqruUnonbP8aCd Gq2AMefdF8NzDxKN9gFePMif7xncawyw== X-Google-Smtp-Source: AGHT+IH1WYrW/UKZ6ngnkIpIsR2dgkaJ0zFY0ft3XgXQ0QWdCfqsR4NIFZKbE/zMO6K3p2uoWgwYBDA5 X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:7b:198d:ac11:8138]) (user=ardb job=sendgmr) by 2002:a05:600c:5709:b0:42c:a879:3d0f with SMTP id 5b1f17b1804b1-42e960af3c0mr226185e9.0.1727276581262; Wed, 25 Sep 2024 08:03:01 -0700 (PDT) Date: Wed, 25 Sep 2024 17:01:28 +0200 In-Reply-To: <20240925150059.3955569-30-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925150059.3955569-30-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=11373; i=ardb@kernel.org; h=from:subject; bh=vOsRO/wusTOgSIMrTbPQkuvbTEvedgqm5gFKQyyQ6LE=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIe2L6uU7KcXh52pbo2UXaeZq6U587fAt6o/qk6KOindLu xmSJDk6SlkYxDgYZMUUWQRm/3238/REqVrnWbIwc1iZQIYwcHEKwES8vjP8D6r8/Whqj0Ue89/g jLlev1knyS9cOWlNtI5Y6rLiT9GiiYwMF5pPLt85df5ck61Kskef/1wi37sr+6Xjrf+r3xWdTEj U5wUA X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240925150059.3955569-58-ardb+git@google.com> Subject: [RFC PATCH 28/28] x86/tools: Drop x86_64 support from 'relocs' tool From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Uros Bizjak , Dennis Zhou , Tejun Heo , Christoph Lameter , Mathieu Desnoyers , Paolo Bonzini , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , Greg Kroah-Hartman , Arnd Bergmann , Masahiro Yamada , Kees Cook , Nathan Chancellor , Keith Packard , Justin Stitt , Josh Poimboeuf , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-doc@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-efi@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-perf-users@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel The relocs tool is no longer used on vmlinux, which is the only 64-bit ELF executable that it used to operate on in the 64-bit build. (It is still used for parts of the decompressor) So drop the 64-bit handling - it is dead code now. Signed-off-by: Ard Biesheuvel --- arch/x86/tools/Makefile | 2 +- arch/x86/tools/relocs.c | 178 +------------------- arch/x86/tools/relocs.h | 9 +- arch/x86/tools/relocs_64.c | 18 -- arch/x86/tools/relocs_common.c | 11 +- 5 files changed, 9 insertions(+), 209 deletions(-) diff --git a/arch/x86/tools/Makefile b/arch/x86/tools/Makefile index 7278e2545c35..f7d12a9dccfc 100644 --- a/arch/x86/tools/Makefile +++ b/arch/x86/tools/Makefile @@ -40,7 +40,7 @@ $(obj)/insn_sanity.o: $(srctree)/tools/arch/x86/lib/insn.= c $(srctree)/tools/arch =20 HOST_EXTRACFLAGS +=3D -I$(srctree)/tools/include hostprogs +=3D relocs -relocs-objs :=3D relocs_32.o relocs_64.o relocs_common.o +relocs-objs :=3D relocs_32.o relocs_common.o PHONY +=3D relocs relocs: $(obj)/relocs @: diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c index 22c2d3f07a57..ff5578e63ff8 100644 --- a/arch/x86/tools/relocs.c +++ b/arch/x86/tools/relocs.c @@ -28,12 +28,7 @@ struct relocs { static struct relocs relocs16; static struct relocs relocs32; =20 -#if ELF_BITS =3D=3D 64 -static struct relocs relocs64; -# define FMT PRIu64 -#else # define FMT PRIu32 -#endif =20 struct section { Elf_Shdr shdr; @@ -86,10 +81,6 @@ static const char * const sym_regex_kernel[S_NSYMTYPES] = =3D { "__end_rodata_aligned|" "__initramfs_start|" "(jiffies|jiffies_64)|" -#if ELF_BITS =3D=3D 64 - "__per_cpu_load|" - "__end_rodata_hpage_align|" -#endif "__vvar_page|" "_end)$" }; @@ -210,27 +201,6 @@ static const char *rel_type(unsigned type) { static const char *type_name[] =3D { #define REL_TYPE(X) [X] =3D #X -#if ELF_BITS =3D=3D 64 - REL_TYPE(R_X86_64_NONE), - REL_TYPE(R_X86_64_64), - REL_TYPE(R_X86_64_PC64), - REL_TYPE(R_X86_64_PC32), - REL_TYPE(R_X86_64_GOT32), - REL_TYPE(R_X86_64_PLT32), - REL_TYPE(R_X86_64_COPY), - REL_TYPE(R_X86_64_GLOB_DAT), - REL_TYPE(R_X86_64_JUMP_SLOT), - REL_TYPE(R_X86_64_RELATIVE), - REL_TYPE(R_X86_64_GOTPCREL), - REL_TYPE(R_X86_64_GOTPCRELX), - REL_TYPE(R_X86_64_REX_GOTPCRELX), - REL_TYPE(R_X86_64_32), - REL_TYPE(R_X86_64_32S), - REL_TYPE(R_X86_64_16), - REL_TYPE(R_X86_64_PC16), - REL_TYPE(R_X86_64_8), - REL_TYPE(R_X86_64_PC8), -#else REL_TYPE(R_386_NONE), REL_TYPE(R_386_32), REL_TYPE(R_386_PC32), @@ -246,7 +216,6 @@ static const char *rel_type(unsigned type) REL_TYPE(R_386_PC8), REL_TYPE(R_386_16), REL_TYPE(R_386_PC16), -#endif #undef REL_TYPE }; const char *name =3D "unknown type rel type name"; @@ -312,19 +281,9 @@ static uint32_t elf32_to_cpu(uint32_t val) #define elf_half_to_cpu(x) elf16_to_cpu(x) #define elf_word_to_cpu(x) elf32_to_cpu(x) =20 -#if ELF_BITS =3D=3D 64 -static uint64_t elf64_to_cpu(uint64_t val) -{ - return le64_to_cpu(val); -} -# define elf_addr_to_cpu(x) elf64_to_cpu(x) -# define elf_off_to_cpu(x) elf64_to_cpu(x) -# define elf_xword_to_cpu(x) elf64_to_cpu(x) -#else # define elf_addr_to_cpu(x) elf32_to_cpu(x) # define elf_off_to_cpu(x) elf32_to_cpu(x) # define elf_xword_to_cpu(x) elf32_to_cpu(x) -#endif =20 static int sym_index(Elf_Sym *sym) { @@ -515,10 +474,7 @@ static void print_absolute_symbols(void) int i; const char *format; =20 - if (ELF_BITS =3D=3D 64) - format =3D "%5d %016"PRIx64" %5"PRId64" %10s %10s %12s %s\n"; - else - format =3D "%5d %08"PRIx32" %5"PRId32" %10s %10s %12s %s\n"; + format =3D "%5d %08"PRIx32" %5"PRId32" %10s %10s %12s %s\n"; =20 printf("Absolute symbols\n"); printf(" Num: Value Size Type Bind Visibility Name\n"); @@ -559,10 +515,7 @@ static void print_absolute_relocs(void) int i, printed =3D 0; const char *format; =20 - if (ELF_BITS =3D=3D 64) - format =3D "%016"PRIx64" %016"PRIx64" %10s %016"PRIx64" %s\n"; - else - format =3D "%08"PRIx32" %08"PRIx32" %10s %08"PRIx32" %s\n"; + format =3D "%08"PRIx32" %08"PRIx32" %10s %08"PRIx32" %s\n"; =20 for (i =3D 0; i < shnum; i++) { struct section *sec =3D &secs[i]; @@ -694,104 +647,6 @@ static void walk_relocs(int (*process)(struct section= *sec, Elf_Rel *rel, } } =20 -#if ELF_BITS =3D=3D 64 - -static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, - const char *symname) -{ - unsigned r_type =3D ELF64_R_TYPE(rel->r_info); - ElfW(Addr) offset =3D rel->r_offset; - int shn_abs =3D (sym->st_shndx =3D=3D SHN_ABS) && !is_reloc(S_REL, symnam= e); - - if (sym->st_shndx =3D=3D SHN_UNDEF) - return 0; - - switch (r_type) { - case R_X86_64_NONE: - /* NONE can be ignored. */ - break; - - case R_X86_64_PC32: - case R_X86_64_PLT32: - /* - * PC relative relocations don't need to be adjusted. - * - * NB: R_X86_64_PLT32 can be treated as R_X86_64_PC32. - */ - break; - - case R_X86_64_PC64: - /* - * Only used by jump labels - */ - break; - - case R_X86_64_32: - case R_X86_64_32S: - case R_X86_64_64: - case R_X86_64_GOTPCREL: - if (shn_abs) { - /* - * Whitelisted absolute symbols do not require - * relocation. - */ - if (is_reloc(S_ABS, symname)) - break; - - die("Invalid absolute %s relocation: %s\n", rel_type(r_type), symname); - break; - } - - if (r_type =3D=3D R_X86_64_GOTPCREL) { - Elf_Shdr *s =3D &secs[sec->shdr.sh_info].shdr; - unsigned file_off =3D offset - s->sh_addr + s->sh_offset; - - /* - * GOTPCREL relocations refer to instructions that load - * a 64-bit address via a 32-bit relative reference to - * the GOT. In this case, it is the GOT entry that - * needs to be fixed up, not the immediate offset in - * the opcode. Note that the linker will have applied an - * addend of -4 to compensate for the delta between the - * relocation offset and the value of RIP when the - * instruction executes, and this needs to be backed out - * again. (Addends other than -4 are permitted in - * principle, but make no sense in practice so they are - * not supported.) - */ - if (rel->r_addend !=3D -4) { - die("invalid addend (%ld) for %s relocation: %s\n", - rel->r_addend, rel_type(r_type), symname); - break; - } - offset +=3D 4 + (int32_t)get_unaligned_le32(elf_image + file_off); - } - - /* - * Relocation offsets for 64 bit kernels are output - * as 32 bits and sign extended back to 64 bits when - * the relocations are processed. - * Make sure that the offset will fit. - */ - if ((int32_t)offset !=3D (int64_t)offset) - die("Relocation offset doesn't fit in 32 bits\n"); - - if (r_type =3D=3D R_X86_64_64 || r_type =3D=3D R_X86_64_GOTPCREL) - add_reloc(&relocs64, offset); - else - add_reloc(&relocs32, offset); - break; - - default: - die("Unsupported relocation type: %s (%d)\n", rel_type(r_type), r_type); - break; - } - - return 0; -} - -#else - static int do_reloc32(struct section *sec, Elf_Rel *rel, Elf_Sym *sym, const char *symname) { @@ -902,8 +757,6 @@ static int do_reloc_real(struct section *sec, Elf_Rel *= rel, Elf_Sym *sym, const return 0; } =20 -#endif - static int cmp_relocs(const void *va, const void *vb) { const uint32_t *a, *b; @@ -939,17 +792,10 @@ static void emit_relocs(int as_text, int use_real_mod= e) int (*write_reloc)(uint32_t, FILE *) =3D write32; int (*do_reloc)(struct section *sec, Elf_Rel *rel, Elf_Sym *sym, const ch= ar *symname); =20 -#if ELF_BITS =3D=3D 64 - if (!use_real_mode) - do_reloc =3D do_reloc64; - else - die("--realmode not valid for a 64-bit ELF file"); -#else if (!use_real_mode) do_reloc =3D do_reloc32; else do_reloc =3D do_reloc_real; -#endif =20 /* Collect up the relocations */ walk_relocs(do_reloc); @@ -959,11 +805,7 @@ static void emit_relocs(int as_text, int use_real_mode) =20 /* Order the relocations for more efficient processing */ sort_relocs(&relocs32); -#if ELF_BITS =3D=3D 64 - sort_relocs(&relocs64); -#else sort_relocs(&relocs16); -#endif =20 /* Print the relocations */ if (as_text) { @@ -984,16 +826,6 @@ static void emit_relocs(int as_text, int use_real_mode) for (i =3D 0; i < relocs32.count; i++) write_reloc(relocs32.offset[i], stdout); } else { -#if ELF_BITS =3D=3D 64 - /* Print a stop */ - write_reloc(0, stdout); - - /* Now print each relocation */ - for (i =3D 0; i < relocs64.count; i++) - if (!i || relocs64.offset[i] !=3D relocs64.offset[i - 1]) - write_reloc(relocs64.offset[i], stdout); -#endif - /* Print a stop */ write_reloc(0, stdout); =20 @@ -1027,12 +859,6 @@ static void print_reloc_info(void) walk_relocs(do_reloc_info); } =20 -#if ELF_BITS =3D=3D 64 -# define process process_64 -#else -# define process process_32 -#endif - void process(FILE *fp, int use_real_mode, int as_text, int show_absolute_syms, int show_absolute_relocs, int show_reloc_info) diff --git a/arch/x86/tools/relocs.h b/arch/x86/tools/relocs.h index 7a509604ff92..ef9eec96bd62 100644 --- a/arch/x86/tools/relocs.h +++ b/arch/x86/tools/relocs.h @@ -32,10 +32,7 @@ enum symtype { S_NSYMTYPES }; =20 -void process_32(FILE *fp, int use_real_mode, int as_text, - int show_absolute_syms, int show_absolute_relocs, - int show_reloc_info); -void process_64(FILE *fp, int use_real_mode, int as_text, - int show_absolute_syms, int show_absolute_relocs, - int show_reloc_info); +void process(FILE *fp, int use_real_mode, int as_text, + int show_absolute_syms, int show_absolute_relocs, + int show_reloc_info); #endif /* RELOCS_H */ diff --git a/arch/x86/tools/relocs_64.c b/arch/x86/tools/relocs_64.c deleted file mode 100644 index 9029cb619cb1..000000000000 --- a/arch/x86/tools/relocs_64.c +++ /dev/null @@ -1,18 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -#include "relocs.h" - -#define ELF_BITS 64 - -#define ELF_MACHINE EM_X86_64 -#define ELF_MACHINE_NAME "x86_64" -#define SHT_REL_TYPE SHT_RELA -#define Elf_Rel Elf64_Rela - -#define ELF_CLASS ELFCLASS64 -#define ELF_R_SYM(val) ELF64_R_SYM(val) -#define ELF_R_TYPE(val) ELF64_R_TYPE(val) -#define ELF_ST_TYPE(o) ELF64_ST_TYPE(o) -#define ELF_ST_BIND(o) ELF64_ST_BIND(o) -#define ELF_ST_VISIBILITY(o) ELF64_ST_VISIBILITY(o) - -#include "relocs.c" diff --git a/arch/x86/tools/relocs_common.c b/arch/x86/tools/relocs_common.c index 6634352a20bc..167985ecd544 100644 --- a/arch/x86/tools/relocs_common.c +++ b/arch/x86/tools/relocs_common.c @@ -72,14 +72,9 @@ int main(int argc, char **argv) die("Cannot read %s: %s", fname, strerror(errno)); } rewind(fp); - if (e_ident[EI_CLASS] =3D=3D ELFCLASS64) - process_64(fp, use_real_mode, as_text, - show_absolute_syms, show_absolute_relocs, - show_reloc_info); - else - process_32(fp, use_real_mode, as_text, - show_absolute_syms, show_absolute_relocs, - show_reloc_info); + process(fp, use_real_mode, as_text, + show_absolute_syms, show_absolute_relocs, + show_reloc_info); fclose(fp); return 0; } --=20 2.46.0.792.g87dc391469-goog