From patchwork Wed Sep 13 21:04:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 139596 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp357916vqi; Thu, 14 Sep 2023 06:43:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG59W6/OoJkn2ko4e1ma8+uLOYfNmGlop9W57tw2wOPKc9D902OWMbFv6tuJHZW22E5PunZ X-Received: by 2002:a17:902:d4c8:b0:1bb:9c45:130f with SMTP id o8-20020a170902d4c800b001bb9c45130fmr6197861plg.69.1694699019812; Thu, 14 Sep 2023 06:43:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694699019; cv=none; d=google.com; s=arc-20160816; b=SQlO/sgSqu7lqS1fii+pyWkNV2Rp/zqwajEE/Rkvy6NE834g5/S++7GtxCHtiGfXRb Q5Bm0EzkgzUDPRhzxsDHQ+Qyv+TiPcMv4/ZAiAGFDeQo2j7i1LICPfYPHj9F7ndzwiF5 bNtVb/Lxnk+OLfOPPDNTasBg8yFR9fffP8cXjJzWNhTpmnQyss+bkSDRNs8jeVcZR1n9 nsuxeKhPBt6Z9upVS3gfiHZb+HsOVF2OrMpF87tPevqHrisqIrc/81attwZN27D0urTf IMh1dlvNyUYNjiBS4/QdF4KqDRUfAC6SubS5WUsKbWnOnsqPyU5RgMtbguzqFhG3Rjlh EuJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=tv4HEesTHW83aQBsr6P2xoLUTJCnxlrDYoQTHgTGZUs=; fh=KZqRT9ufKlz9AAx/9RG99hMgOts4DBuJCtI1Ondw5wc=; b=YkvFxUbweNuqW/kVl77cv1SSXtOTH7njxISuTlhDgKiXuOOLVkoApZWBOdwoZDqOLD yd/VkMUHclx5PLvv6BOg1gWyrB4roOvsv9cwFRFGlX9Ae8GepFW1pctpOD48sxliAljS rJLdubC1QcCow8qFGbOj1TwkQ1hOCxtqB9M164MbVSvoTIDwWCp5OuWh/T0pr8ZVgMPa zXRCPOtx0cQKjA6inye88G6voqspm/fQsbakRfxLJvmWfcLCgJ8lzdrEilpRU7Ta/d5y 2yqEXEpO7dbWSF/SjzuM4ih0jVbEVh2KZ0OLNSyz1a3hjULnVPJF56N2xT4WGSx/N2Hd ppTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=yMMVHq3w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id o17-20020a170903301100b001bb9d6794d3si1621712pla.72.2023.09.14.06.43.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 06:43:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=yMMVHq3w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 4A5848322714; Wed, 13 Sep 2023 14:05:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232741AbjIMVFr (ORCPT + 34 others); Wed, 13 Sep 2023 17:05:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232757AbjIMVFp (ORCPT ); Wed, 13 Sep 2023 17:05:45 -0400 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9CD81BCE for ; Wed, 13 Sep 2023 14:05:41 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id 98e67ed59e1d1-26b44247123so215837a91.2 for ; Wed, 13 Sep 2023 14:05:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1694639141; x=1695243941; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=tv4HEesTHW83aQBsr6P2xoLUTJCnxlrDYoQTHgTGZUs=; b=yMMVHq3w7jKxBpZjbaflfAxAPqVheqSLwcu/9+Bg8Nfv2TK5wrbljw2K76nukd0WcO Rk+6sMdAjtjn9qrxLwbaIoaVBQIAASWOf3FkLSTc6Bh8wL3ZEWe0sNCoT2CQ67h6wbSQ DhF69jDcYPl/373rbdmMrLBnc9Hg6sFuqHKCmj0dRWZ+mqanvfcf0g32LERHwKmNQzSF +UAZy+is8KTC/0ApbScye4GHNCQHKFsw4mIeQTYlT9VRD/qJ9NY3JhzcCVpMEdfjFcDu ueVOo4D8eVqQuk3SK+pORG4ts2DIRLQRyv7sCpaKsfHD18IDR5RtxmU+d+I2mZ8YJwfz ClDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694639141; x=1695243941; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tv4HEesTHW83aQBsr6P2xoLUTJCnxlrDYoQTHgTGZUs=; b=sOg22YsobcAkw140Xv+Io3WxxSWBwhDAY6CUBLzXMtw4V4Q7a+FHDrDkwdBuGlO3Tq 495r8vGWUifereZhG49+NQf5dEKIbPVQCcftnOJw6RpYwmINnGSgpaSj2nLNR0AqWPr5 bIS+S++ElRVvpbZC+ZC3A3/XVPqixX+Pj1GVgU6xFDv79WvNQQbW93ps3rmVZD6HblRa oXD1MP4jXelyUdk1uAceawKKtO9xaz+MWHfNu2jxV46/NVnNknREU8zIPTLsMMi8T0xn aOGt6xSVWhJaKKJcX4V1utvXyCR5AQ3ieHIRSHIXj548jYTkN70O4zKNbamZ7ItmRP7L VMTQ== X-Gm-Message-State: AOJu0YxBaCautntQcny/PN7sLMszlqDHznjfVRAu3lEEKxoi+/BfZgQC i16M96gJ5Xi2wfZi76P6qMl//w== X-Received: by 2002:a17:90b:4ad0:b0:262:ee7d:2d20 with SMTP id mh16-20020a17090b4ad000b00262ee7d2d20mr3514022pjb.12.1694639141273; Wed, 13 Sep 2023 14:05:41 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id z16-20020a170903019000b001ab2b4105ddsm66323plg.60.2023.09.13.14.05.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 14:05:40 -0700 (PDT) From: Charlie Jenkins Date: Wed, 13 Sep 2023 14:04:49 -0700 Subject: [PATCH 1/2] riscv: Add remaining module relocations MIME-Version: 1.0 Message-Id: <20230913-module_relocations-v1-1-bb3d8467e793@rivosinc.com> References: <20230913-module_relocations-v1-0-bb3d8467e793@rivosinc.com> In-Reply-To: <20230913-module_relocations-v1-0-bb3d8467e793@rivosinc.com> To: linux-riscv@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Eric Biederman , Kees Cook , Paul Walmsley , Palmer Dabbelt , Albert Ou , Charlie Jenkins X-Mailer: b4 0.12.3 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 13 Sep 2023 14:05:51 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777020719391628593 X-GMAIL-MSGID: 1777020719391628593 Add all final module relocations and add error logs explaining the ones that are not supported. Signed-off-by: Charlie Jenkins --- arch/riscv/include/uapi/asm/elf.h | 6 +- arch/riscv/kernel/module.c | 191 +++++++++++++++++++++++++++++++++----- 2 files changed, 171 insertions(+), 26 deletions(-) diff --git a/arch/riscv/include/uapi/asm/elf.h b/arch/riscv/include/uapi/asm/elf.h index d696d6610231..a9307a1c9ceb 100644 --- a/arch/riscv/include/uapi/asm/elf.h +++ b/arch/riscv/include/uapi/asm/elf.h @@ -49,6 +49,7 @@ typedef union __riscv_fp_state elf_fpregset_t; #define R_RISCV_TLS_DTPREL64 9 #define R_RISCV_TLS_TPREL32 10 #define R_RISCV_TLS_TPREL64 11 +#define R_RISCV_IRELATIVE 58 /* Relocation types not used by the dynamic linker */ #define R_RISCV_BRANCH 16 @@ -81,7 +82,7 @@ typedef union __riscv_fp_state elf_fpregset_t; #define R_RISCV_ALIGN 43 #define R_RISCV_RVC_BRANCH 44 #define R_RISCV_RVC_JUMP 45 -#define R_RISCV_LUI 46 +#define R_RISCV_RVC_LUI 46 #define R_RISCV_GPREL_I 47 #define R_RISCV_GPREL_S 48 #define R_RISCV_TPREL_I 49 @@ -93,6 +94,9 @@ typedef union __riscv_fp_state elf_fpregset_t; #define R_RISCV_SET16 55 #define R_RISCV_SET32 56 #define R_RISCV_32_PCREL 57 +#define R_RISCV_PLT32 59 +#define R_RISCV_SET_ULEB128 60 +#define R_RISCV_SUB_ULEB128 61 #endif /* _UAPI_ASM_RISCV_ELF_H */ diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index 7c651d55fcbd..7c0cb03b9035 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -253,6 +253,30 @@ static int apply_r_riscv_call_rela(struct module *me, u32 *location, return 0; } +static int apply_r_riscv_rvc_lui_rela(struct module *me, u32 *location, + Elf_Addr v) +{ + // Get high 6 bits of 18 bit absolute address + s32 imm = ((s32)v + 0x800) >> 12; + + if (v != sign_extend32(v, 6)) { + pr_err("%s: target %016llx can not be addressed by the 6-bit offset from PC = %p\n", + me->name, (long long)v, location); + return -EINVAL; + } + + if (imm == 0) { + // imm = 0 is invalid for c.lui, convert to c.li + *location = (*location & 0x0F83) | 0x4000; + } else { + u16 imm17 = ((((s32)v + 0x800) & 0x20000) >> (17 - 12)); + u16 imm16_12 = ((((s32)v + 0x800) & 0x1f000) >> (12 - 2)); + *location = (*location & 0xef83) | imm17 | imm16_12; + } + + return 0; +} + static int apply_r_riscv_relax_rela(struct module *me, u32 *location, Elf_Addr v) { @@ -268,6 +292,12 @@ static int apply_r_riscv_align_rela(struct module *me, u32 *location, return -EINVAL; } +static int apply_r_riscv_add8_rela(struct module *me, u32 *location, Elf_Addr v) +{ + *(u8 *)location += (u8)v; + return 0; +} + static int apply_r_riscv_add16_rela(struct module *me, u32 *location, Elf_Addr v) { @@ -289,6 +319,12 @@ static int apply_r_riscv_add64_rela(struct module *me, u32 *location, return 0; } +static int apply_r_riscv_sub8_rela(struct module *me, u32 *location, Elf_Addr v) +{ + *(u8 *)location -= (u8)v; + return 0; +} + static int apply_r_riscv_sub16_rela(struct module *me, u32 *location, Elf_Addr v) { @@ -310,31 +346,136 @@ static int apply_r_riscv_sub64_rela(struct module *me, u32 *location, return 0; } -static int (*reloc_handlers_rela[]) (struct module *me, u32 *location, - Elf_Addr v) = { - [R_RISCV_32] = apply_r_riscv_32_rela, - [R_RISCV_64] = apply_r_riscv_64_rela, - [R_RISCV_BRANCH] = apply_r_riscv_branch_rela, - [R_RISCV_JAL] = apply_r_riscv_jal_rela, - [R_RISCV_RVC_BRANCH] = apply_r_riscv_rvc_branch_rela, - [R_RISCV_RVC_JUMP] = apply_r_riscv_rvc_jump_rela, - [R_RISCV_PCREL_HI20] = apply_r_riscv_pcrel_hi20_rela, - [R_RISCV_PCREL_LO12_I] = apply_r_riscv_pcrel_lo12_i_rela, - [R_RISCV_PCREL_LO12_S] = apply_r_riscv_pcrel_lo12_s_rela, - [R_RISCV_HI20] = apply_r_riscv_hi20_rela, - [R_RISCV_LO12_I] = apply_r_riscv_lo12_i_rela, - [R_RISCV_LO12_S] = apply_r_riscv_lo12_s_rela, - [R_RISCV_GOT_HI20] = apply_r_riscv_got_hi20_rela, - [R_RISCV_CALL_PLT] = apply_r_riscv_call_plt_rela, - [R_RISCV_CALL] = apply_r_riscv_call_rela, - [R_RISCV_RELAX] = apply_r_riscv_relax_rela, - [R_RISCV_ALIGN] = apply_r_riscv_align_rela, - [R_RISCV_ADD16] = apply_r_riscv_add16_rela, - [R_RISCV_ADD32] = apply_r_riscv_add32_rela, - [R_RISCV_ADD64] = apply_r_riscv_add64_rela, - [R_RISCV_SUB16] = apply_r_riscv_sub16_rela, - [R_RISCV_SUB32] = apply_r_riscv_sub32_rela, - [R_RISCV_SUB64] = apply_r_riscv_sub64_rela, +static int dynamic_linking_not_supported(struct module *me, u32 *location, + Elf_Addr v) +{ + pr_err("%s: Dynamic linking not supported in kernel modules PC = %p\n", + me->name, location); + return -EINVAL; +} + +static int tls_not_supported(struct module *me, u32 *location, Elf_Addr v) +{ + pr_err("%s: Thread local storage not supported in kernel modules PC = %p\n", + me->name, location); + return -EINVAL; +} + +static int apply_r_riscv_sub6_rela(struct module *me, u32 *location, Elf_Addr v) +{ + *(u8 *)location -= (u8)v & 0x3F; + return 0; +} + +static int apply_r_riscv_set6_rela(struct module *me, u32 *location, Elf_Addr v) +{ + *(u8 *)location = (*(u8 *)location & 0xc0) | ((u8)v & 0x3F); + return 0; +} + +static int apply_r_riscv_set8_rela(struct module *me, u32 *location, Elf_Addr v) +{ + *(u8 *)location = (u8)v; + return 0; +} + +static int apply_r_riscv_set16_rela(struct module *me, u32 *location, + Elf_Addr v) +{ + *(u16 *)location = (u16)v; + return 0; +} + +static int apply_r_riscv_set32_rela(struct module *me, u32 *location, + Elf_Addr v) +{ + *(u32 *)location = (u32)v; + return 0; +} + +static int apply_r_riscv_32_pcrel_rela(struct module *me, u32 *location, + Elf_Addr v) +{ + *(u32 *)location = (u32)v; + return 0; +} + +static int apply_r_riscv_plt32_rela(struct module *me, u32 *location, + Elf_Addr v) +{ + *(u32 *)location = (u32)v; + return 0; +} + +static int uleb128_not_supported(struct module *me, u32 *location, Elf_Addr v) +{ + pr_err("%s: R_RISCV_SET_ULEB128 and R_RISCV_SUB_ULEB128 not supported PC = %p\n", + me->name, location); + return -EINVAL; +} + +/* + * Relocations defined in the riscv-elf-psabi-doc. + * This handles static linking only. + */ +static int (*reloc_handlers_rela[])(struct module *me, u32 *location, + Elf_Addr v) = { + [R_RISCV_32] = apply_r_riscv_32_rela, + [R_RISCV_64] = apply_r_riscv_64_rela, + [R_RISCV_RELATIVE] = dynamic_linking_not_supported, + [R_RISCV_COPY] = dynamic_linking_not_supported, + [R_RISCV_JUMP_SLOT] = dynamic_linking_not_supported, + [R_RISCV_TLS_DTPMOD32] = dynamic_linking_not_supported, + [R_RISCV_TLS_DTPMOD64] = dynamic_linking_not_supported, + [R_RISCV_TLS_DTPREL32] = dynamic_linking_not_supported, + [R_RISCV_TLS_DTPREL64] = dynamic_linking_not_supported, + [R_RISCV_TLS_TPREL32] = dynamic_linking_not_supported, + [R_RISCV_TLS_TPREL64] = dynamic_linking_not_supported, + /* 12-15 undefined */ + [R_RISCV_BRANCH] = apply_r_riscv_branch_rela, + [R_RISCV_JAL] = apply_r_riscv_jal_rela, + [R_RISCV_CALL] = apply_r_riscv_call_rela, + [R_RISCV_CALL_PLT] = apply_r_riscv_call_plt_rela, + [R_RISCV_GOT_HI20] = apply_r_riscv_got_hi20_rela, + [R_RISCV_TLS_GOT_HI20] = tls_not_supported, + [R_RISCV_TLS_GD_HI20] = tls_not_supported, + [R_RISCV_PCREL_HI20] = apply_r_riscv_pcrel_hi20_rela, + [R_RISCV_PCREL_LO12_I] = apply_r_riscv_pcrel_lo12_i_rela, + [R_RISCV_PCREL_LO12_S] = apply_r_riscv_pcrel_lo12_s_rela, + [R_RISCV_HI20] = apply_r_riscv_hi20_rela, + [R_RISCV_LO12_I] = apply_r_riscv_lo12_i_rela, + [R_RISCV_LO12_S] = apply_r_riscv_lo12_s_rela, + [R_RISCV_TPREL_HI20] = tls_not_supported, + [R_RISCV_TPREL_LO12_I] = tls_not_supported, + [R_RISCV_TPREL_LO12_S] = tls_not_supported, + [R_RISCV_TPREL_ADD] = tls_not_supported, + [R_RISCV_ADD8] = apply_r_riscv_add8_rela, + [R_RISCV_ADD16] = apply_r_riscv_add16_rela, + [R_RISCV_ADD32] = apply_r_riscv_add32_rela, + [R_RISCV_ADD64] = apply_r_riscv_add64_rela, + [R_RISCV_SUB8] = apply_r_riscv_sub8_rela, + [R_RISCV_SUB16] = apply_r_riscv_sub16_rela, + [R_RISCV_SUB32] = apply_r_riscv_sub32_rela, + [R_RISCV_SUB64] = apply_r_riscv_sub64_rela, + /* 41-42 reserved for future standard use */ + [R_RISCV_ALIGN] = apply_r_riscv_align_rela, + [R_RISCV_RVC_BRANCH] = apply_r_riscv_rvc_branch_rela, + [R_RISCV_RVC_JUMP] = apply_r_riscv_rvc_jump_rela, + [R_RISCV_RVC_LUI] = apply_r_riscv_rvc_lui_rela, + /* 47-50 reserved for future standard use */ + [R_RISCV_RELAX] = apply_r_riscv_relax_rela, + [R_RISCV_SUB6] = apply_r_riscv_sub6_rela, + [R_RISCV_SET6] = apply_r_riscv_set6_rela, + [R_RISCV_SET8] = apply_r_riscv_set8_rela, + [R_RISCV_SET16] = apply_r_riscv_set16_rela, + [R_RISCV_SET32] = apply_r_riscv_set32_rela, + [R_RISCV_32_PCREL] = apply_r_riscv_32_pcrel_rela, + [R_RISCV_IRELATIVE] = dynamic_linking_not_supported, + [R_RISCV_PLT32] = apply_r_riscv_plt32_rela, + [R_RISCV_SET_ULEB128] = uleb128_not_supported, + [R_RISCV_SUB_ULEB128] = uleb128_not_supported, + /* 62-191 reserved for future standard use */ + /* 192-255 nonstandard ABI extensions */ }; int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,