Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp36919137rwd; Tue, 11 Jul 2023 07:18:37 -0700 (PDT) X-Google-Smtp-Source: APBJJlHT79tX7uvZxJJ6R2tV3S+6d5mlJSZRaN0trtCQiBOf5YejNf7hMiX6MR/ZFhrc7Ke4Pf/B X-Received: by 2002:a17:906:2096:b0:993:f90b:e549 with SMTP id 22-20020a170906209600b00993f90be549mr9432687ejq.37.1689085117079; Tue, 11 Jul 2023 07:18:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689085117; cv=none; d=google.com; s=arc-20160816; b=BKC5F7v6XKvn9BC28bwBozhv6iEFYPgTTnTK42LCpDSsQ5d49kh6V6rLswlZLKR8Y0 B+3GmKxM5V/xmlKRNHId25Xk7/Y661CsMLA6izyeQJ0Q/BB3mi96geEKSrbn4gi/SaDp MvtUGJwxUt4Y4SMEnPXL2EqEctFM8PdBWj2y+9wxTA1VtSCiHQAEHpn2KH3IvpsOYaZ/ M8HZic/2gC5l4dnPnSQEqOEC205wqNopIkHRCmfvlh9fXrCZzYv3gjo3xzupK5h+VAta M964bTImAKArEdg7hgaIZW6IGchTLr7ot2NxZLkZY121RfG6DdDgyk80FdqydRuVwVyp 97QQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JykG/UzUnVwFeMJrnBIWl4WMZH6IAo4uTuNPnZHvTzA=; fh=NxQDIo4JBLp57/JgJgcSQwgQkWLQsYyb4wjsMoILEcM=; b=HSexcmghltZw1+t7oXFCRkfCYq8S4Q5fs74ipEQw/tLbu1bsWr+MDUdYwWrvpbnEG1 Cc7P+YNgIV3K0I2let8/toU/7urH3BMWNDC2D+0E4LJGcrZD/R304Q3tpRZq+NjsBuHO viqnq9NlfukfeoM3jpx7MOpkC2/o0bQc3RXQbnVrTvUNsG0+uZBD3GQ/e6XUePY5Bsyx B9oKXGIs1GemRBuDjPyiMdzRLTkyInRoUwsRYf9IZPruJMADif444cPJK+DXjkwN7Eeb YcvugGrgW7n8gCKftG9n0/ylmtqveVaJ9zq9JIEZV5Ds0tTvqRbBmD8B8WB5Wu+TZTJc cs0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="m8vIO/3o"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kk25-20020a170907767900b00993a68a3af6si2101499ejc.511.2023.07.11.07.18.11; Tue, 11 Jul 2023 07:18:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="m8vIO/3o"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232860AbjGKNIy (ORCPT + 99 others); Tue, 11 Jul 2023 09:08:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232752AbjGKNIx (ORCPT ); Tue, 11 Jul 2023 09:08:53 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C24418D for ; Tue, 11 Jul 2023 06:08:52 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B02F2614BD for ; Tue, 11 Jul 2023 13:08:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EEFD1C433C8; Tue, 11 Jul 2023 13:08:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689080931; bh=YRG1SatEXSQvibLkPPPJGs1s5S3gHEmvd7gZSzwoAnU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m8vIO/3oz0iV+JgNzmjwzr+CqcICYWIe7Rog2iDkpGmFwZJYExFefwtunqZdT6VMl EQnVR6hgqYxvFUCn1yz1VxthdhaUjLWAQNQknByoD2ylRfeXIGTkinPU7DRPpEARDZ 7wX0sSRV4gsPE+0pjG7tc7lPTroIyaiNr9krJQaOVEEiaUTnd9kPtjM1i7eDyrWveS QtdstMrnsA2dPoXWxPs7dLSeHSQaNy11g5joZAo4bkYF0MURxHjYoP8QJ1BL2CHNxl dRj4mzhN1/YDtHZnmVaI3l9GfljurrccBjsCIrXpFRYcC9/hqYTui5DrmkZ/0JYoKJ JsEwgA/i1IYjg== From: Greg Ungerer To: linux-riscv@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: palmer@dabbelt.com, viro@zeniv.linux.org.uk, ebiederm@xmission.com, keescook@chromium.org, brauner@kernel.org, Greg Ungerer Subject: [PATCH v2 2/2] riscv: support the elf-fdpic binfmt loader Date: Tue, 11 Jul 2023 23:07:54 +1000 Message-Id: <20230711130754.481209-3-gerg@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230711130754.481209-1-gerg@kernel.org> References: <20230711130754.481209-1-gerg@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add support for enabling and using the binfmt_elf_fdpic program loader on RISC-V platforms. The most important change is to setup registers during program load to pass the mapping addresses to the new process. One of the interesting features of the elf-fdpic loader is that it also allows appropriately compiled ELF format binaries to be loaded on nommu systems. Appropriate being those compiled with -pie. Signed-off-by: Greg Ungerer --- v1->v2: rebase onto linux-6.5-rc1 increment PTRACE_GETFDPIC value to keep it unique arch/riscv/include/asm/elf.h | 11 ++++++++++- arch/riscv/include/asm/mmu.h | 4 ++++ arch/riscv/include/uapi/asm/ptrace.h | 5 +++++ fs/Kconfig.binfmt | 2 +- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/elf.h b/arch/riscv/include/asm/elf.h index c24280774caf..c33fe923ef6d 100644 --- a/arch/riscv/include/asm/elf.h +++ b/arch/riscv/include/asm/elf.h @@ -41,6 +41,7 @@ extern bool compat_elf_check_arch(Elf32_Ehdr *hdr); #define compat_elf_check_arch compat_elf_check_arch #define CORE_DUMP_USE_REGSET +#define ELF_FDPIC_CORE_EFLAGS 0 #define ELF_EXEC_PAGESIZE (PAGE_SIZE) /* @@ -69,6 +70,13 @@ extern bool compat_elf_check_arch(Elf32_Ehdr *hdr); #define ELF_HWCAP riscv_get_elf_hwcap() extern unsigned long elf_hwcap; +#define ELF_FDPIC_PLAT_INIT(_r, _exec_map_addr, _interp_map_addr, dynamic_addr) \ + do { \ + (_r)->a1 = _exec_map_addr; \ + (_r)->a2 = _interp_map_addr; \ + (_r)->a3 = dynamic_addr; \ + } while (0) + /* * This yields a string that ld.so will use to load implementation * specific libraries for optimization. This is more specific in @@ -78,7 +86,6 @@ extern unsigned long elf_hwcap; #define COMPAT_ELF_PLATFORM (NULL) -#ifdef CONFIG_MMU #define ARCH_DLINFO \ do { \ /* \ @@ -115,6 +122,8 @@ do { \ else \ NEW_AUX_ENT(AT_IGNORE, 0); \ } while (0) + +#ifdef CONFIG_MMU #define ARCH_HAS_SETUP_ADDITIONAL_PAGES struct linux_binprm; extern int arch_setup_additional_pages(struct linux_binprm *bprm, diff --git a/arch/riscv/include/asm/mmu.h b/arch/riscv/include/asm/mmu.h index 0099dc116168..355504b37f8e 100644 --- a/arch/riscv/include/asm/mmu.h +++ b/arch/riscv/include/asm/mmu.h @@ -20,6 +20,10 @@ typedef struct { /* A local icache flush is needed before user execution can resume. */ cpumask_t icache_stale_mask; #endif +#ifdef CONFIG_BINFMT_ELF_FDPIC + unsigned long exec_fdpic_loadmap; + unsigned long interp_fdpic_loadmap; +#endif } mm_context_t; void __init create_pgd_mapping(pgd_t *pgdp, uintptr_t va, phys_addr_t pa, diff --git a/arch/riscv/include/uapi/asm/ptrace.h b/arch/riscv/include/uapi/asm/ptrace.h index e17c550986a6..30f6d6537adc 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -10,6 +10,11 @@ #include +#define PTRACE_GETFDPIC 33 + +#define PTRACE_GETFDPIC_EXEC 0 +#define PTRACE_GETFDPIC_INTERP 1 + /* * User-mode register state for core dumps, ptrace, sigcontext * diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt index 93539aac0e5b..f5693164ca9a 100644 --- a/fs/Kconfig.binfmt +++ b/fs/Kconfig.binfmt @@ -58,7 +58,7 @@ config ARCH_USE_GNU_PROPERTY config BINFMT_ELF_FDPIC bool "Kernel support for FDPIC ELF binaries" default y if !BINFMT_ELF - depends on ARM || ((M68K || SUPERH || XTENSA) && !MMU) + depends on ARM || ((M68K || RISCV || SUPERH || XTENSA) && !MMU) select ELFCORE help ELF FDPIC binaries are based on ELF, but allow the individual load -- 2.25.1