From patchwork Wed Jun 26 17:16:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jesse Taube X-Patchwork-Id: 13713203 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 89A9DC27C4F for ; Wed, 26 Jun 2024 17:17:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=Kbn1y8Z8bpwJK9IZAnyvS+tu/PdBVjhChVSR5HDZh0o=; b=g+naGKCCuonpgV iJvtaSYeIfOWoOxc4302l/vvZEyzb5etivQSihhGgUCMuXF0i+lvpFpJJHz3ugc09d1qrUKhiZFn8 I3ikiKNQLEkFgVR7foNRA+ZvG483e4X9cPLuwYPsYnodhhchHRXVteZhh7HNEY0w23jUThz6yzgV9 DmO2SqQso1YrWyd8wCAjEulMIEvIUsqS6TqJYR/N8FG6JzYpaBS6STzkTOcpnbjkwA8pBLNkyywsa 3aG6F7ktiKbQGgarRN/W5i8bWePCv6chvkJwFOU5kYfkpsbEgJBYVOH1P+LT4+VrQDCtExdfcPqhM wOdeCrcXeAEVeogmgZ0A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMWGe-00000007jKH-1S4E; Wed, 26 Jun 2024 17:17:12 +0000 Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMWGc-00000007jJp-01Vx for linux-riscv@lists.infradead.org; Wed, 26 Jun 2024 17:17:11 +0000 Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-713fa1aea36so3453789a12.1 for ; Wed, 26 Jun 2024 10:17:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1719422228; x=1720027028; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=sfc+bItioWOLi2TuWFqieQ4WdP0MPgrB+J5zxVf671w=; b=1d0JvubzGSVr/NOiBrLHIzzmlQPS8GYiCQMq/cUhYr/NsAhD3QL2DJ7/iNztji4ywf EEH8drHgRbtgdYyKVKM8bgjcH6VkOW8wExsa92Bx3TUTkEq6+iqiDI/yEJALxao9skke KjOneVhnXwQBovqu/fXTNAyWYQuYJSH3gWiyAHOFMIUg5Yn/c9qjLBjSKZm/2PQblO5t t98YkbGJ6QcJG/zYtMwH8rR8mMkBz93p/PCs3DXgAUf3wCRhkuLkaqyFl3cgM6Efp8Bf l1RIGahACbQHNuch64j2TXsiAYJb7PcHw9Qj+jfDYbgctgeAoT8ac0M2Mbh5f2usfwlV 5ztA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719422228; x=1720027028; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=sfc+bItioWOLi2TuWFqieQ4WdP0MPgrB+J5zxVf671w=; b=srmY3RCUEy4QqZot9vmjLtda1rmXp5XVF4hGKYM9cymbiFja6vaBaRyLyowT97u6o0 alsoowQhI2Y7SxUsHxsmdmRsvEn0AWYVlmP7GZdPtcofOQgT9CCqKIocej/x9rJMt1qg kUDwNuuRO2Hw+jXpLlZgjoMrX4yw5QbfrgzNM3bIE5nbUsoDf8pWKOqJoo0qF1/ZwSN7 bk/5hyeLugWlK2zE6mTyHUlw3E+SsEAmLjNEaEnRiJpLsateD3ifyn3S8gWUEmf8gAcO W7G20C9RjXwO7HcAmBORrTr27bYpE1OV/HDoRD+VAeam7hn8RGAPx21bQKXosYifsvAG 6rrg== X-Gm-Message-State: AOJu0YzbktEI8m9o6QKCbnp40Zxmv+Bc4ECMOvorugCi+I/qHrUeESs2 KgQLFtZeO3ftA0BppwYBP/P9NPdC4Zm3VTt/JEfrvgu9U9XvAUcLd5Z5KFiGazGTSFRhtTlFUxp c X-Google-Smtp-Source: AGHT+IFRRYAJqPRgI7bs8MXpEZG6vyONCJH7c7jAbKBR0F7SXl7y1eqCXlLYI6Wy4OsF9R8wyxyQng== X-Received: by 2002:a17:90b:b0b:b0:2c8:53be:fa21 with SMTP id 98e67ed59e1d1-2c853befb16mr11452942a91.34.1719422227952; Wed, 26 Jun 2024 10:17:07 -0700 (PDT) Received: from jesse-desktop.. (pool-108-26-179-17.bstnma.fios.verizon.net. [108.26.179.17]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c8f3b5ad36sm83217a91.26.2024.06.26.10.16.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jun 2024 10:17:07 -0700 (PDT) From: Jesse Taube To: linux-riscv@lists.infradead.org Cc: Ard Biesheuvel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Jesse Taube , Alexandre Ghiti , Conor Dooley , Masahiro Yamada , Wende Tan , Christophe JAILLET , Sami Tolvanen , Andrew Morton , Baoquan He , Chen Jiahao , "Mike Rapoport (IBM)" , "Vishal Moola (Oracle)" , linux-kernel@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v2 1/3] RISC-V: pi: Force hidden visibility for all symbol references Date: Wed, 26 Jun 2024 13:16:50 -0400 Message-ID: <20240626171652.366415-1-jesse@rivosinc.com> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240626_101710_090254_BB0EA3FB X-CRM114-Status: UNSURE ( 8.13 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Eliminate all GOT entries in the .pi section, by forcing hidden visibility for all symbol references, which informs the compiler that such references will be resolved at link time without the need for allocating GOT entries. Include linux/hidden.h in Makefile, like arm64, for the hidden visibility attribute. Signed-off-by: Jesse Taube Reviewed-by: Alexandre Ghiti --- arch/riscv/kernel/pi/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/riscv/kernel/pi/Makefile b/arch/riscv/kernel/pi/Makefile index 50bc5ef7dd2f..1ef7584be0c3 100644 --- a/arch/riscv/kernel/pi/Makefile +++ b/arch/riscv/kernel/pi/Makefile @@ -5,6 +5,7 @@ KBUILD_CFLAGS := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) -fpie \ -Os -DDISABLE_BRANCH_PROFILING $(DISABLE_STACKLEAK_PLUGIN) \ $(call cc-option,-mbranch-protection=none) \ -I$(srctree)/scripts/dtc/libfdt -fno-stack-protector \ + -include $(srctree)/include/linux/hidden.h \ -D__DISABLE_EXPORTS -ffreestanding \ -fno-asynchronous-unwind-tables -fno-unwind-tables \ $(call cc-option,-fno-addrsig) From patchwork Wed Jun 26 17:16:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jesse Taube X-Patchwork-Id: 13713204 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 62F66C3064D for ; Wed, 26 Jun 2024 17:17:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=j3KAu5yuCH6TYgvyZu9l1KYxhUrjlYqVCCn3czikbL0=; b=GHceb/UXW1QAIL xOIoJQrBU86+eDaZQu2EdG47yORzDtIVYxe04Ibld4bE8l8a3NerFcdJNpQBdezoDakFxv2bX+ScH Ka256SMSS368eQaWyXZKrpg5jx6NTbLkmD6Ch0QfvqDAbYvsmswgcwMS389wdmLF/V6z7O9fQsBfH i0ZWvLzEh00xnmKYSeoIR+T50LASmvv0HoKs9nH0sBkiDUuark3M0aSe6+Y/lN20VYkbA2Yj4o/ba K0ziCoTUb9SCLSF6isTtlpTdVYLOj0hYXleY+AHbjGypyNT6SZv0tOXyW4CgIy37t4tOkIn5JkGCA gheuLeeSkIzFeMmapjEg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMWGo-00000007jMk-10ut; Wed, 26 Jun 2024 17:17:22 +0000 Received: from mail-pg1-x534.google.com ([2607:f8b0:4864:20::534]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMWGl-00000007jLA-3220 for linux-riscv@lists.infradead.org; Wed, 26 Jun 2024 17:17:21 +0000 Received: by mail-pg1-x534.google.com with SMTP id 41be03b00d2f7-681bc7f50d0so671719a12.0 for ; Wed, 26 Jun 2024 10:17:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1719422239; x=1720027039; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2+lAD5jtfoEc4FrM9h7co7KfTqJwWQk8uXEwNnDDNnA=; b=cd6lpDYh9z026hTum4tdF9OIah+0OxNe4kHhBN4h2LdlHQiwMc1MCZkLA4bYjA5rnR 3xjmJYRyhVPFDizowGSgIdL7RSq2f7xwLjf3ykDln65iCer/mePQoHT+nunRKvc84pdX K6RbVCAlBH7kjh1osjKQ1DSSTuwnSRiOMCkCg2lGuINmYCFHRpCmlyVu+/UmN2XbqjQh b+BZPWwlRaeVJCc2dpaCUd3C3QgfSWRFaJ3x3VXNOK89ivdiJu2tceEI5kQxGEbbBEPt AymxqyUJ6eP3wamIQDIRfW/fzhi2PSHz06+m4Ny7MGJbzz0PvShhSx2WKrkQBcXECWsl kTMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719422239; x=1720027039; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2+lAD5jtfoEc4FrM9h7co7KfTqJwWQk8uXEwNnDDNnA=; b=suU+G84RA/OqbgP3MKFPxMEl3+XGLJX5KnxeBF34eYEOUEuecCQWdo2omfA6y1q46C ixiUDUXpALF7ohi4Q6J6hsaXyu46rJ+9oA8GatyBvylFkWYo0mqoWLttqeUA3TBoxEyg YEbiYpimJ54hgexqxmFr41PasVoCaG7qiHF6hrsdcZJIaRmcaoVDgm8QfED+nUE9uPgw MT3rHDlNZDprkcPu6Hv92igyn2DlT5LrvwOiKFzqiG/Yir1UIjuwbaolKGG1hGedi5lw 02Aaua21YBKE4weX1ZO1+qCgsgxDEY8s1thn5IwqQSAJxQ+7RWsXE5ZvnQ0ciVYcuygI xCXw== X-Gm-Message-State: AOJu0Yy8IGwRQbS7Wgp6+duwDADJMDFXGHoiahfAjsaaSJdbcH9DYi4q nKIKjQfeRcJ381vn8b5Ucb1ifQBFyYM/JPFMvDR2aHGRErZl0AzTGhQgTtWnPEU3q35d13RZYyP W X-Google-Smtp-Source: AGHT+IGG7s6Oe33J/XmHTMtV5FMqVvWxCk+2M3wpvCrsMnzNwrLwsfiUejeLhxTVumdEvPHZXZpSuQ== X-Received: by 2002:a17:90a:4094:b0:2c8:2236:e2c3 with SMTP id 98e67ed59e1d1-2c848a4399bmr15456635a91.17.1719422238599; Wed, 26 Jun 2024 10:17:18 -0700 (PDT) Received: from jesse-desktop.. (pool-108-26-179-17.bstnma.fios.verizon.net. [108.26.179.17]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c8f3b5ad36sm83217a91.26.2024.06.26.10.17.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jun 2024 10:17:18 -0700 (PDT) From: Jesse Taube To: linux-riscv@lists.infradead.org Cc: Ard Biesheuvel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Jesse Taube , Alexandre Ghiti , Conor Dooley , Masahiro Yamada , Wende Tan , Christophe JAILLET , Sami Tolvanen , Andrew Morton , Baoquan He , Chen Jiahao , "Mike Rapoport (IBM)" , "Vishal Moola (Oracle)" , linux-kernel@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v2 2/3] RISC-V: pi: Add kernel/pi/pi.h Date: Wed, 26 Jun 2024 13:16:51 -0400 Message-ID: <20240626171652.366415-2-jesse@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240626171652.366415-1-jesse@rivosinc.com> References: <20240626171652.366415-1-jesse@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240626_101719_784786_E37CEC75 X-CRM114-Status: GOOD ( 13.25 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Add pi.h header for declarations of the kernel/pi prefixed functions and any other related declarations. Signed-off-by: Jesse Taube Reviewed-by: Alexandre Ghiti --- arch/riscv/kernel/pi/cmdline_early.c | 10 ++-------- arch/riscv/kernel/pi/fdt_early.c | 7 +------ arch/riscv/kernel/pi/pi.h | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 arch/riscv/kernel/pi/pi.h diff --git a/arch/riscv/kernel/pi/cmdline_early.c b/arch/riscv/kernel/pi/cmdline_early.c index f6d4dedffb84..fbcdc9e4e143 100644 --- a/arch/riscv/kernel/pi/cmdline_early.c +++ b/arch/riscv/kernel/pi/cmdline_early.c @@ -6,15 +6,9 @@ #include #include -static char early_cmdline[COMMAND_LINE_SIZE]; +#include "pi.h" -/* - * Declare the functions that are exported (but prefixed) here so that LLVM - * does not complain it lacks the 'static' keyword (which, if added, makes - * LLVM complain because the function is actually unused in this file). - */ -u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa); -bool set_nokaslr_from_cmdline(uintptr_t dtb_pa); +static char early_cmdline[COMMAND_LINE_SIZE]; static char *get_early_cmdline(uintptr_t dtb_pa) { diff --git a/arch/riscv/kernel/pi/fdt_early.c b/arch/riscv/kernel/pi/fdt_early.c index 899610e042ab..40ee299702bf 100644 --- a/arch/riscv/kernel/pi/fdt_early.c +++ b/arch/riscv/kernel/pi/fdt_early.c @@ -3,12 +3,7 @@ #include #include -/* - * Declare the functions that are exported (but prefixed) here so that LLVM - * does not complain it lacks the 'static' keyword (which, if added, makes - * LLVM complain because the function is actually unused in this file). - */ -u64 get_kaslr_seed(uintptr_t dtb_pa); +#include "pi.h" u64 get_kaslr_seed(uintptr_t dtb_pa) { diff --git a/arch/riscv/kernel/pi/pi.h b/arch/riscv/kernel/pi/pi.h new file mode 100644 index 000000000000..65da99466baf --- /dev/null +++ b/arch/riscv/kernel/pi/pi.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _RISCV_PI_H_ +#define _RISCV_PI_H_ + +#include + +/* + * The folowing functions are exported (but prefixed) declare them here so + * that LLVM does not complain it lacks the 'static' keyword (which, if + * added, makes LLVM complain because the function is unused). + */ + +u64 get_kaslr_seed(uintptr_t dtb_pa); +bool set_nokaslr_from_cmdline(uintptr_t dtb_pa); +u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa); + +#endif /* _RISCV_PI_H_ */ From patchwork Wed Jun 26 17:16:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jesse Taube X-Patchwork-Id: 13713205 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B0348C27C4F for ; Wed, 26 Jun 2024 17:17:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=1Wf1COs7ZmIaYq9XoJXyxnftEKYEc6Jc/mCFNTOb+us=; b=mt8jF/10yCFOco njiweuxp3mDLaG5OKLFoz4WVoLwJvxt0x6EP15j086OAzCNJG/chWFmuGDWsSr25jBhV++Lnuud4q tKchAqNJrGdRAR1mCWTIHoyCvPevE9D0ippPKs1ecaFVv8efqSbMHdZJYP5Ovi+qz7UhTE/Qay4WS MGBazMh0vS0hhyGzU1WqvW4e/4v8PA1xZf5sniX4I8z/id2BL9EOAXbop7BDQ/S+VzB8ey1s1hu0l g8n6BTTS8nwAx5eLVE8FnP6LdPsFq4H8R04Lg7NwGR8ekCeqskeDk2n8/dXz7yq2EkJu9ztWkIkZy W4vavybyn6m5jXFC8VNA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMWH3-00000007jTB-1GEn; Wed, 26 Jun 2024 17:17:37 +0000 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMWGx-00000007jPh-0dEK for linux-riscv@lists.infradead.org; Wed, 26 Jun 2024 17:17:32 +0000 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-2c7dbdc83bfso5319195a91.1 for ; Wed, 26 Jun 2024 10:17:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1719422250; x=1720027050; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=of2FbNumXWiiWPSU2khL/cXCQvlYVxK+wsi6dw//9/8=; b=D1/JBI6Qzen96qTy15xhFPunNaEf59PZkR2ngXJfkiJBjCNlPphhO9ZGK/9Jdjo1hG mOXwyqJKEBW+GREKZw33Cv8NdK0xfCk0+kI4Fl1VFr31eg2N8mbSvQkz8XZaLCGbplUO iS3GyEovX/NFR1iUZI/tMZxt9zkyaLvwMS/XdyyL7C7VYnkQ5iYnyij/2+4o2N0UjStW vPTd3gqB9uKy7rO7pf71xfk7BH5/B30KfTaRdi5BloVh2d5PHloQPTkpSSlcG9h3xD1l hwBzWF6d4aDYrBrZ7+1h2B8ZypKznr9+3x2/Goo39fPdt5kJgY4XfYe6JAZatJJ8qusJ /f+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719422250; x=1720027050; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=of2FbNumXWiiWPSU2khL/cXCQvlYVxK+wsi6dw//9/8=; b=owowU5UhBkkw7vLD+nG6nbR9PXl8elG1D474NYF3Mcx/8Aghrg1T8dv0WstlfPCJE9 1O2FuDNnTVupp6NJ7sSgHU7neddYrBeZlmAB1ydBsFhbYi4rJzIQwcUukVLX3gYPLW87 9jka9RJXkFsq7edAR2jFt/dDF217jmtpeaD0keGU2PGjUZsdpY4o/pQHesoD809pACYm CKTfyHLQeJoN6Kgl5odEKPBHOSLazHhjw1emfmfO4hagoSLCGU0QZxkITp42sdzdt+F3 7O91cHjEoHSX210VfGN0Ra0txPLWEd3hgDWancLKKn2C2EEE7lobF0enQ64me4NSeFLU 4m0w== X-Gm-Message-State: AOJu0Ywm6tk4pyhoF4oB7GXPpFGHgbhBBlE10+tg0EpO2omubEHg1Kfb 5OvrvhrmnTjq6niOXQSlSVvUiAFE+8LGYdrzDQ0HFEkmFjgcdbL89laL2vlrv8AQTtv/w7Zvy35 s X-Google-Smtp-Source: AGHT+IGyHd+WADwyRkT1gcGbulDJF4qqkfp5LHBXeyGhlm6qCM53u/kZpVYoYyrJHx1rDz4EeAXGkg== X-Received: by 2002:a17:90b:1884:b0:2c2:f6dc:eb12 with SMTP id 98e67ed59e1d1-2c8612c7375mr10774803a91.11.1719422249790; Wed, 26 Jun 2024 10:17:29 -0700 (PDT) Received: from jesse-desktop.. (pool-108-26-179-17.bstnma.fios.verizon.net. [108.26.179.17]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c8f3b5ad36sm83217a91.26.2024.06.26.10.17.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jun 2024 10:17:29 -0700 (PDT) From: Jesse Taube To: linux-riscv@lists.infradead.org Cc: Ard Biesheuvel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Jesse Taube , Alexandre Ghiti , Conor Dooley , Masahiro Yamada , Wende Tan , Christophe JAILLET , Sami Tolvanen , Andrew Morton , Baoquan He , Chen Jiahao , "Mike Rapoport (IBM)" , "Vishal Moola (Oracle)" , linux-kernel@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v2 3/3] RISC-V: Use Zkr to seed KASLR base address Date: Wed, 26 Jun 2024 13:16:52 -0400 Message-ID: <20240626171652.366415-3-jesse@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240626171652.366415-1-jesse@rivosinc.com> References: <20240626171652.366415-1-jesse@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240626_101731_217573_165E8743 X-CRM114-Status: GOOD ( 23.95 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Parse the device tree for Zkr in isa string. If Zkr is present, use it to seed the kernel base address. Signed-off-by: Jesse Taube --- arch/riscv/kernel/pi/Makefile | 2 +- arch/riscv/kernel/pi/archrandom_early.c | 30 ++++++++ arch/riscv/kernel/pi/fdt_early.c | 94 +++++++++++++++++++++++++ arch/riscv/kernel/pi/pi.h | 3 + arch/riscv/mm/init.c | 5 +- 5 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 arch/riscv/kernel/pi/archrandom_early.c diff --git a/arch/riscv/kernel/pi/Makefile b/arch/riscv/kernel/pi/Makefile index 1ef7584be0c3..dba902f2a538 100644 --- a/arch/riscv/kernel/pi/Makefile +++ b/arch/riscv/kernel/pi/Makefile @@ -33,5 +33,5 @@ $(obj)/string.o: $(srctree)/lib/string.c FORCE $(obj)/ctype.o: $(srctree)/lib/ctype.c FORCE $(call if_changed_rule,cc_o_c) -obj-y := cmdline_early.pi.o fdt_early.pi.o string.pi.o ctype.pi.o lib-fdt.pi.o lib-fdt_ro.pi.o +obj-y := cmdline_early.pi.o fdt_early.pi.o string.pi.o ctype.pi.o lib-fdt.pi.o lib-fdt_ro.pi.o archrandom_early.pi.o extra-y := $(patsubst %.pi.o,%.o,$(obj-y)) diff --git a/arch/riscv/kernel/pi/archrandom_early.c b/arch/riscv/kernel/pi/archrandom_early.c new file mode 100644 index 000000000000..c6261165e8a6 --- /dev/null +++ b/arch/riscv/kernel/pi/archrandom_early.c @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include + +#include "pi.h" + +/* + * To avoid rewriting code include asm/archrandom.h and create macros + * for the functions that won't be included. + */ +#undef riscv_has_extension_unlikely +#define riscv_has_extension_likely(...) false +#undef pr_err_once +#define pr_err_once(...) + +#include + +u64 get_kaslr_seed_zkr(const uintptr_t dtb_pa) +{ + unsigned long seed = 0; + + if (!early_isa_str((const void *)dtb_pa, "zkr")) + return 0; + + if (!csr_seed_long(&seed)) + return 0; + + return seed; +} diff --git a/arch/riscv/kernel/pi/fdt_early.c b/arch/riscv/kernel/pi/fdt_early.c index 40ee299702bf..ba76197b44d1 100644 --- a/arch/riscv/kernel/pi/fdt_early.c +++ b/arch/riscv/kernel/pi/fdt_early.c @@ -23,3 +23,97 @@ u64 get_kaslr_seed(uintptr_t dtb_pa) *prop = 0; return ret; } + +/* Based off of fdt_stringlist_contains */ +static int isa_string_contains(const char *strlist, int listlen, const char *str) +{ + int len = strlen(str); + const char *p; + + while (listlen >= len) { + if (strncasecmp(str, strlist, len) == 0) + return 1; + p = memchr(strlist, '_', listlen); + if (!p) + p = memchr(strlist, '\0', listlen); + if (!p) + return 0; /* malformed strlist.. */ + listlen -= (p - strlist) + 1; + strlist = p + 1; + } + + return 0; +} + +/* Based off of fdt_nodename_eq_ */ +static int fdt_node_name_eq(const void *fdt, int offset, + const char *s) +{ + int olen; + int len = strlen(s); + const char *p = fdt_get_name(fdt, offset, &olen); + + if (!p || olen < len) + /* short match */ + return 0; + + if (memcmp(p, s, len) != 0) + return 0; + + if (p[len] == '\0') + return 1; + else if (!memchr(s, '@', len) && (p[len] == '@')) + return 1; + else + return 0; +} + +/* + * Returns true if the extension is in the isa string + * Returns false if the extension is not found + */ +static bool get_ext_named(const void *fdt, int node, const char *name) +{ + const void *prop; + int len; + + prop = fdt_getprop(fdt, node, "riscv,isa-base", &len); + if (prop && isa_string_contains(prop, len, name)) + return true; + + prop = fdt_getprop(fdt, node, "riscv,isa-extensions", &len); + if (prop && isa_string_contains(prop, len, name)) + return true; + + prop = fdt_getprop(fdt, node, "riscv,isa", &len); + if (prop && isa_string_contains(prop, len, name)) + return true; + + return false; +} + +/* + * Returns true if the extension is in the isa string on all cpus + * Returns false if the extension is not found + */ +bool early_isa_str(const void *fdt, const char *ext_name) +{ + int node, parent; + bool ret = false; + + parent = fdt_path_offset(fdt, "/cpus"); + if (parent < 0) + return false; + + fdt_for_each_subnode(node, fdt, parent) { + if (!fdt_node_name_eq(fdt, node, "cpu")) + continue; + + if (!get_ext_named(fdt, node, ext_name)) + return false; + + ret = true; + } + + return ret; +} diff --git a/arch/riscv/kernel/pi/pi.h b/arch/riscv/kernel/pi/pi.h index 65da99466baf..26e7e5f84a30 100644 --- a/arch/riscv/kernel/pi/pi.h +++ b/arch/riscv/kernel/pi/pi.h @@ -4,6 +4,8 @@ #include +bool early_isa_str(const void *fdt, const char *ext_name); + /* * The folowing functions are exported (but prefixed) declare them here so * that LLVM does not complain it lacks the 'static' keyword (which, if @@ -11,6 +13,7 @@ */ u64 get_kaslr_seed(uintptr_t dtb_pa); +u64 get_kaslr_seed_zkr(const uintptr_t dtb_pa); bool set_nokaslr_from_cmdline(uintptr_t dtb_pa); u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa); diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 9940171c79f0..bfb068dc4a64 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -1025,6 +1025,7 @@ static void __init pt_ops_set_late(void) #ifdef CONFIG_RANDOMIZE_BASE extern bool __init __pi_set_nokaslr_from_cmdline(uintptr_t dtb_pa); extern u64 __init __pi_get_kaslr_seed(uintptr_t dtb_pa); +extern u64 __init __pi_get_kaslr_seed_zkr(const uintptr_t dtb_pa); static int __init print_nokaslr(char *p) { @@ -1045,10 +1046,12 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa) #ifdef CONFIG_RANDOMIZE_BASE if (!__pi_set_nokaslr_from_cmdline(dtb_pa)) { - u64 kaslr_seed = __pi_get_kaslr_seed(dtb_pa); + u64 kaslr_seed = __pi_get_kaslr_seed_zkr(dtb_pa); u32 kernel_size = (uintptr_t)(&_end) - (uintptr_t)(&_start); u32 nr_pos; + if (kaslr_seed == 0) + kaslr_seed = __pi_get_kaslr_seed(dtb_pa); /* * Compute the number of positions available: we are limited * by the early page table that only has one PUD and we must