From nobody Wed Jun 24 01:02:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0DB79C433F5 for ; Thu, 24 Feb 2022 15:25:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236262AbiBXP0R (ORCPT ); Thu, 24 Feb 2022 10:26:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236107AbiBXPZu (ORCPT ); Thu, 24 Feb 2022 10:25:50 -0500 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F6FB22A291 for ; Thu, 24 Feb 2022 07:25:19 -0800 (PST) Received: by mail-ed1-x536.google.com with SMTP id q17so3371308edd.4 for ; Thu, 24 Feb 2022 07:25:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Qw2dAmFiLhG20Fq9WpBqzH0yihPpPi6A4uH3+mZ36Gg=; b=HGOi35okBIpzSVR+vaq3gGg5P8nC+upR2jiiU/1ySrRfO4jz6Bh0+5Nx0G/ULFwmuV 76VUorkyuAotaE+Alg6BOsEm7/+KMF7BScHPGlyQUrEc+XtjV1o3YutiDIPHIO/dDWBr NS4spAnrFG4/IYnvfPUtaHzPwqhIjxw30SyGt9iYXPxZ8i2vZ2ETgjfKaCLMT9dYvdNI CQU8hbXg79vn/0TQSx+rxosiH00VSnteijBPxc7o5UR/kieWjxvaCZ3kuJWgWvDiPoWw HVDZKO1/88Dq/2Rfl2/uuHnVkrcg9PHn2TRX4wgrGjlNPkN3VRjj0jwnPjuESFuKXi3m WqNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=Qw2dAmFiLhG20Fq9WpBqzH0yihPpPi6A4uH3+mZ36Gg=; b=Xm0oaKnqlKkHZso5/RXdOcS/80teXetHZQcPaZWcNXoUMpcYvlouLd3AeYHH/xfNlj vS9VaE93napxmu7cpgrXi8Ims2PdU/+XhqYGP7HSfWXUuzokDtrVAnZaNhGCd0KlQkgA fwoIBdA6PfcAfEdv9QMpxGrXFSdWLguQ3QHH20Y9onH5m0JDg5E7eGo9MvcBywkrzaLb lli2X7Wox/h174evcDZ2Vm6Wnpln00NyUj3EgUeXvvo/6YEKrruKSVGQRI/qnGhxTEbB Px8JJsz4WFZZeic8m6VxSRIqWf6IVxkgWZ58EZ3nHcaKpP9Ph3xWGVeEcYQrQYGZ2n1a Xh/A== X-Gm-Message-State: AOAM533PdHcmjGD+hKwUIIHikmNxZKu6CV/V2jdv7AERnmyccM4REmWS UGsv9/7ljP/SiT+u7jt3zKw= X-Google-Smtp-Source: ABdhPJzKVa200BJ0XQQ/0XNEDYKEMcs/FfB6nZMmexxPzUsW61l9Mlx9uZnLQ1GhkOaeX7Pl5gktnA== X-Received: by 2002:a05:6402:190a:b0:408:9d75:eab6 with SMTP id e10-20020a056402190a00b004089d75eab6mr2752553edz.286.1645716317441; Thu, 24 Feb 2022 07:25:17 -0800 (PST) Received: from stitch.. (80.71.140.73.ipv4.parknet.dk. [80.71.140.73]) by smtp.gmail.com with ESMTPSA id s11sm1509693edt.10.2022.02.24.07.25.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 07:25:17 -0800 (PST) Sender: Emil Renner Berthing From: Emil Renner Berthing To: linux-riscv@lists.infradead.org Cc: Emil Renner Berthing , Steven Rostedt , Ingo Molnar , Paul Walmsley , Palmer Dabbelt , Albert Ou , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Ard Biesheuvel , Jisheng Zhang , Alexandre Ghiti , linux-kernel@vger.kernel.org Subject: [PATCH v3 1/8] riscv: Avoid unaligned access when relocating modules Date: Thu, 24 Feb 2022 16:24:49 +0100 Message-Id: <20220224152456.493365-2-kernel@esmil.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220224152456.493365-1-kernel@esmil.dk> References: <20220224152456.493365-1-kernel@esmil.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" With the C-extension regular 32bit instructions are not necessarily aligned on 4-byte boundaries. RISC-V instructions are in fact an ordered list of 16bit native-endian "parcels", so access the instruction as such. This should also make the code work in case someone builds a big-endian RISC-V machine. Fix rcv -> rvc typo while we're at it. Signed-off-by: Emil Renner Berthing --- arch/riscv/kernel/module.c | 151 +++++++++++++++++++------------------ 1 file changed, 76 insertions(+), 75 deletions(-) diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index 4a48287513c3..8d6a16d74b5b 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -26,68 +26,86 @@ static bool riscv_insn_valid_32bit_offset(ptrdiff_t val) #endif } =20 -static int apply_r_riscv_32_rela(struct module *me, u32 *location, Elf_Add= r v) +static int riscv_insn_rmw(void *location, u32 keep, u32 set) +{ + u16 *parcel =3D location; + u32 insn =3D (u32)parcel[0] | (u32)parcel[1] << 16; + + insn &=3D keep; + insn |=3D set; + + parcel[0] =3D insn; + parcel[1] =3D insn >> 16; + return 0; +} + +static int riscv_insn_rvc_rmw(void *location, u16 keep, u16 set) +{ + u16 *parcel =3D location; + + *parcel =3D (*parcel & keep) | set; + return 0; +} + +static int apply_r_riscv_32_rela(struct module *me, void *location, Elf_Ad= dr v) { if (v !=3D (u32)v) { pr_err("%s: value %016llx out of range for 32-bit field\n", me->name, (long long)v); return -EINVAL; } - *location =3D v; + *(u32 *)location =3D v; return 0; } =20 -static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Add= r v) +static int apply_r_riscv_64_rela(struct module *me, void *location, Elf_Ad= dr v) { *(u64 *)location =3D v; return 0; } =20 -static int apply_r_riscv_branch_rela(struct module *me, u32 *location, +static int apply_r_riscv_branch_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; + ptrdiff_t offset =3D (void *)v - location; u32 imm12 =3D (offset & 0x1000) << (31 - 12); u32 imm11 =3D (offset & 0x800) >> (11 - 7); u32 imm10_5 =3D (offset & 0x7e0) << (30 - 10); u32 imm4_1 =3D (offset & 0x1e) << (11 - 4); =20 - *location =3D (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1; - return 0; + return riscv_insn_rmw(location, 0x1fff07f, imm12 | imm11 | imm10_5 | imm4= _1); } =20 -static int apply_r_riscv_jal_rela(struct module *me, u32 *location, +static int apply_r_riscv_jal_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; + ptrdiff_t offset =3D (void *)v - location; u32 imm20 =3D (offset & 0x100000) << (31 - 20); u32 imm19_12 =3D (offset & 0xff000); u32 imm11 =3D (offset & 0x800) << (20 - 11); u32 imm10_1 =3D (offset & 0x7fe) << (30 - 10); =20 - *location =3D (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1; - return 0; + return riscv_insn_rmw(location, 0xfff, imm20 | imm19_12 | imm11 | imm10_1= ); } =20 -static int apply_r_riscv_rcv_branch_rela(struct module *me, u32 *location, +static int apply_r_riscv_rvc_branch_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; + ptrdiff_t offset =3D (void *)v - location; u16 imm8 =3D (offset & 0x100) << (12 - 8); u16 imm7_6 =3D (offset & 0xc0) >> (6 - 5); u16 imm5 =3D (offset & 0x20) >> (5 - 2); u16 imm4_3 =3D (offset & 0x18) << (12 - 5); u16 imm2_1 =3D (offset & 0x6) << (12 - 10); =20 - *(u16 *)location =3D (*(u16 *)location & 0xe383) | - imm8 | imm7_6 | imm5 | imm4_3 | imm2_1; - return 0; + return riscv_insn_rvc_rmw(location, 0xe383, + imm8 | imm7_6 | imm5 | imm4_3 | imm2_1); } =20 -static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location, +static int apply_r_riscv_rvc_jump_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; + ptrdiff_t offset =3D (void *)v - location; u16 imm11 =3D (offset & 0x800) << (12 - 11); u16 imm10 =3D (offset & 0x400) >> (10 - 8); u16 imm9_8 =3D (offset & 0x300) << (12 - 11); @@ -97,16 +115,14 @@ static int apply_r_riscv_rvc_jump_rela(struct module *= me, u32 *location, u16 imm4 =3D (offset & 0x10) << (12 - 5); u16 imm3_1 =3D (offset & 0xe) << (12 - 10); =20 - *(u16 *)location =3D (*(u16 *)location & 0xe003) | - imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1; - return 0; + return riscv_insn_rvc_rmw(location, 0xe003, + imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1); } =20 -static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location, +static int apply_r_riscv_pcrel_hi20_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; - s32 hi20; + ptrdiff_t offset =3D (void *)v - location; =20 if (!riscv_insn_valid_32bit_offset(offset)) { pr_err( @@ -115,23 +131,20 @@ static int apply_r_riscv_pcrel_hi20_rela(struct modul= e *me, u32 *location, return -EINVAL; } =20 - hi20 =3D (offset + 0x800) & 0xfffff000; - *location =3D (*location & 0xfff) | hi20; - return 0; + return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000); } =20 -static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *locatio= n, +static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, void *locati= on, Elf_Addr v) { /* * v is the lo12 value to fill. It is calculated before calling this * handler. */ - *location =3D (*location & 0xfffff) | ((v & 0xfff) << 20); - return 0; + return riscv_insn_rmw(location, 0xfffff, (v & 0xfff) << 20); } =20 -static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *locatio= n, +static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, void *locati= on, Elf_Addr v) { /* @@ -141,15 +154,12 @@ static int apply_r_riscv_pcrel_lo12_s_rela(struct mod= ule *me, u32 *location, u32 imm11_5 =3D (v & 0xfe0) << (31 - 11); u32 imm4_0 =3D (v & 0x1f) << (11 - 4); =20 - *location =3D (*location & 0x1fff07f) | imm11_5 | imm4_0; - return 0; + return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0); } =20 -static int apply_r_riscv_hi20_rela(struct module *me, u32 *location, +static int apply_r_riscv_hi20_rela(struct module *me, void *location, Elf_Addr v) { - s32 hi20; - if (IS_ENABLED(CONFIG_CMODEL_MEDLOW)) { pr_err( "%s: target %016llx can not be addressed by the 32-bit offset from PC = =3D %p\n", @@ -157,22 +167,20 @@ static int apply_r_riscv_hi20_rela(struct module *me,= u32 *location, return -EINVAL; } =20 - hi20 =3D ((s32)v + 0x800) & 0xfffff000; - *location =3D (*location & 0xfff) | hi20; - return 0; + return riscv_insn_rmw(location, 0xfff, ((s32)v + 0x800) & 0xfffff000); } =20 -static int apply_r_riscv_lo12_i_rela(struct module *me, u32 *location, +static int apply_r_riscv_lo12_i_rela(struct module *me, void *location, Elf_Addr v) { /* Skip medlow checking because of filtering by HI20 already */ s32 hi20 =3D ((s32)v + 0x800) & 0xfffff000; s32 lo12 =3D ((s32)v - hi20); - *location =3D (*location & 0xfffff) | ((lo12 & 0xfff) << 20); - return 0; + + return riscv_insn_rmw(location, 0xfffff, (lo12 & 0xfff) << 20); } =20 -static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location, +static int apply_r_riscv_lo12_s_rela(struct module *me, void *location, Elf_Addr v) { /* Skip medlow checking because of filtering by HI20 already */ @@ -180,20 +188,18 @@ static int apply_r_riscv_lo12_s_rela(struct module *m= e, u32 *location, s32 lo12 =3D ((s32)v - hi20); u32 imm11_5 =3D (lo12 & 0xfe0) << (31 - 11); u32 imm4_0 =3D (lo12 & 0x1f) << (11 - 4); - *location =3D (*location & 0x1fff07f) | imm11_5 | imm4_0; - return 0; + + return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0); } =20 -static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location, +static int apply_r_riscv_got_hi20_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; - s32 hi20; + ptrdiff_t offset =3D (void *)v - location; =20 /* Always emit the got entry */ if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) { - offset =3D module_emit_got_entry(me, v); - offset =3D (void *)offset - (void *)location; + offset =3D (void *)module_emit_got_entry(me, v) - location; } else { pr_err( "%s: can not generate the GOT entry for symbol =3D %016llx from PC =3D= %p\n", @@ -201,22 +207,19 @@ static int apply_r_riscv_got_hi20_rela(struct module = *me, u32 *location, return -EINVAL; } =20 - hi20 =3D (offset + 0x800) & 0xfffff000; - *location =3D (*location & 0xfff) | hi20; - return 0; + return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000); } =20 -static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location, +static int apply_r_riscv_call_plt_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; + ptrdiff_t offset =3D (void *)v - location; u32 hi20, lo12; =20 if (!riscv_insn_valid_32bit_offset(offset)) { /* Only emit the plt entry if offset over 32-bit range */ if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) { - offset =3D module_emit_plt_entry(me, v); - offset =3D (void *)offset - (void *)location; + offset =3D (void *)module_emit_plt_entry(me, v) - location; } else { pr_err( "%s: target %016llx can not be addressed by the 32-bit offset from PC= =3D %p\n", @@ -227,15 +230,14 @@ static int apply_r_riscv_call_plt_rela(struct module = *me, u32 *location, =20 hi20 =3D (offset + 0x800) & 0xfffff000; lo12 =3D (offset - hi20) & 0xfff; - *location =3D (*location & 0xfff) | hi20; - *(location + 1) =3D (*(location + 1) & 0xfffff) | (lo12 << 20); - return 0; + riscv_insn_rmw(location, 0xfff, hi20); + return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20); } =20 -static int apply_r_riscv_call_rela(struct module *me, u32 *location, +static int apply_r_riscv_call_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; + ptrdiff_t offset =3D (void *)v - location; u32 hi20, lo12; =20 if (!riscv_insn_valid_32bit_offset(offset)) { @@ -247,18 +249,17 @@ static int apply_r_riscv_call_rela(struct module *me,= u32 *location, =20 hi20 =3D (offset + 0x800) & 0xfffff000; lo12 =3D (offset - hi20) & 0xfff; - *location =3D (*location & 0xfff) | hi20; - *(location + 1) =3D (*(location + 1) & 0xfffff) | (lo12 << 20); - return 0; + riscv_insn_rmw(location, 0xfff, hi20); + return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20); } =20 -static int apply_r_riscv_relax_rela(struct module *me, u32 *location, +static int apply_r_riscv_relax_rela(struct module *me, void *location, Elf_Addr v) { return 0; } =20 -static int apply_r_riscv_align_rela(struct module *me, u32 *location, +static int apply_r_riscv_align_rela(struct module *me, void *location, Elf_Addr v) { pr_err( @@ -267,41 +268,41 @@ static int apply_r_riscv_align_rela(struct module *me= , u32 *location, return -EINVAL; } =20 -static int apply_r_riscv_add32_rela(struct module *me, u32 *location, +static int apply_r_riscv_add32_rela(struct module *me, void *location, Elf_Addr v) { *(u32 *)location +=3D (u32)v; return 0; } =20 -static int apply_r_riscv_add64_rela(struct module *me, u32 *location, +static int apply_r_riscv_add64_rela(struct module *me, void *location, Elf_Addr v) { *(u64 *)location +=3D (u64)v; return 0; } =20 -static int apply_r_riscv_sub32_rela(struct module *me, u32 *location, +static int apply_r_riscv_sub32_rela(struct module *me, void *location, Elf_Addr v) { *(u32 *)location -=3D (u32)v; return 0; } =20 -static int apply_r_riscv_sub64_rela(struct module *me, u32 *location, +static int apply_r_riscv_sub64_rela(struct module *me, void *location, Elf_Addr v) { *(u64 *)location -=3D (u64)v; return 0; } =20 -static int (*reloc_handlers_rela[]) (struct module *me, u32 *location, +static int (*reloc_handlers_rela[]) (struct module *me, void *location, Elf_Addr v) =3D { [R_RISCV_32] =3D apply_r_riscv_32_rela, [R_RISCV_64] =3D apply_r_riscv_64_rela, [R_RISCV_BRANCH] =3D apply_r_riscv_branch_rela, [R_RISCV_JAL] =3D apply_r_riscv_jal_rela, - [R_RISCV_RVC_BRANCH] =3D apply_r_riscv_rcv_branch_rela, + [R_RISCV_RVC_BRANCH] =3D apply_r_riscv_rvc_branch_rela, [R_RISCV_RVC_JUMP] =3D apply_r_riscv_rvc_jump_rela, [R_RISCV_PCREL_HI20] =3D apply_r_riscv_pcrel_hi20_rela, [R_RISCV_PCREL_LO12_I] =3D apply_r_riscv_pcrel_lo12_i_rela, @@ -325,9 +326,9 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *s= trtab, struct module *me) { Elf_Rela *rel =3D (void *) sechdrs[relsec].sh_addr; - int (*handler)(struct module *me, u32 *location, Elf_Addr v); + int (*handler)(struct module *me, void *location, Elf_Addr v); Elf_Sym *sym; - u32 *location; + void *location; unsigned int i, type; Elf_Addr v; int res; --=20 2.35.1 From nobody Wed Jun 24 01:02:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C275CC433EF for ; Thu, 24 Feb 2022 15:25:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236237AbiBXP0K (ORCPT ); Thu, 24 Feb 2022 10:26:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236178AbiBXPZu (ORCPT ); Thu, 24 Feb 2022 10:25:50 -0500 Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E134A22A292 for ; Thu, 24 Feb 2022 07:25:19 -0800 (PST) Received: by mail-ej1-x62d.google.com with SMTP id qk11so5167102ejb.2 for ; Thu, 24 Feb 2022 07:25:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YCCLljOzkZTWVhp4UcnxVmYpwCgumhAeXuk5FTxJkB0=; b=TiFvJ0dV8oZgGXJzw3d5vMthSlHC7N/ou80D7XIhS6zXizdQQ9fT7xARchhB1K989M sqvbYGdOf26hgiJpp0Mmshu/fPhlntJU1t+XNdqJUGxprWcV6ZKCGfykIl0Scgm3fYXC rP3b4kAIMhOhIQzbVGd3StT+r0HPCyyI7zELHOkPvFUCOmwX6prXOPCKYWYotiq18VMZ MN6C9H9BZaTU1RgKK+J/E4wxu7dvxDDkxc8NO25dyYzV4j6OjvCTgA5/L4Xdr9QnaOng pODMwpsmRd5PV+pl4hChgJNgqe8M8Gseg7MFHnKpScIo1ppmyp9OCME/fwKniob97ArX 18Eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=YCCLljOzkZTWVhp4UcnxVmYpwCgumhAeXuk5FTxJkB0=; b=HTKjslbTRbYnQ9ckCC1XdjQBDJYOmN9dldb0huVg1YH6yxbLSMBNsiYbALcBd98/V1 GIQjDk6BKv7Hx0IvzMJaxgL8WbwPw4DDOiqcDEVrOQpRb8QkgwbDlvnjwO/IsyKOWmrV J7UbFj+NHLsWq8MvrkQs60i7/sMhnn9yAFHYAN/rX6EmHH2TrYj082IK837DFY6CUD1r soHguWJATZ07hfrrfbtmt831yOw5/cWZlsbrRqLMnxGCkwN/D55NDuUMCZZEhpB3aQVy uu0XPvtdhOV59p/lyWkRHikhM8cjothxPXHVQbHyU0pB8mHqGZ1F2Kfe8IVWI1LtyrlX lgpw== X-Gm-Message-State: AOAM532/HBINn7qYN0K6i27fe76diWnKEDLVZIYHK6E78DIl7ZEPWrML v25usJhykqFh+NbBKvDdHpPdzWOr9ULtNl0R X-Google-Smtp-Source: ABdhPJzbc4J9AQJ7JJ5b/LrjFjbCOzAT+PXAgLjfQPliRCa02sLFO4rQw8CytIS537W0EB6TGsW2Hg== X-Received: by 2002:a17:906:aad7:b0:6cc:c9aa:d9ad with SMTP id kt23-20020a170906aad700b006ccc9aad9admr2708800ejb.726.1645716318467; Thu, 24 Feb 2022 07:25:18 -0800 (PST) Received: from stitch.. (80.71.140.73.ipv4.parknet.dk. [80.71.140.73]) by smtp.gmail.com with ESMTPSA id s11sm1509693edt.10.2022.02.24.07.25.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 07:25:18 -0800 (PST) Sender: Emil Renner Berthing From: Emil Renner Berthing To: linux-riscv@lists.infradead.org Cc: Emil Renner Berthing , Steven Rostedt , Ingo Molnar , Paul Walmsley , Palmer Dabbelt , Albert Ou , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Ard Biesheuvel , Jisheng Zhang , Alexandre Ghiti , linux-kernel@vger.kernel.org Subject: [PATCH v3 2/8] riscv: Remove unneeded definitions from asm/module.h Date: Thu, 24 Feb 2022 16:24:50 +0100 Message-Id: <20220224152456.493365-3-kernel@esmil.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220224152456.493365-1-kernel@esmil.dk> References: <20220224152456.493365-1-kernel@esmil.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The inline functions previously defined here are only ever used in kernel/module-sections.c, so there is no need to include them in every user of asm/module.h. Through linux/module.h this is just about every driver. Now that these functions are static in a single file remove the inline marker to allow the compiler to make its own decisions. Signed-off-by: Emil Renner Berthing --- arch/riscv/include/asm/module.h | 87 ---------------------------- arch/riscv/kernel/module-sections.c | 90 +++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 87 deletions(-) diff --git a/arch/riscv/include/asm/module.h b/arch/riscv/include/asm/modul= e.h index 76aa96a9fc08..570cd025f220 100644 --- a/arch/riscv/include/asm/module.h +++ b/arch/riscv/include/asm/module.h @@ -22,93 +22,6 @@ struct mod_arch_specific { struct mod_section plt; struct mod_section got_plt; }; - -struct got_entry { - unsigned long symbol_addr; /* the real variable address */ -}; - -static inline struct got_entry emit_got_entry(unsigned long val) -{ - return (struct got_entry) {val}; -} - -static inline struct got_entry *get_got_entry(unsigned long val, - const struct mod_section *sec) -{ - struct got_entry *got =3D (struct got_entry *)(sec->shdr->sh_addr); - int i; - for (i =3D 0; i < sec->num_entries; i++) { - if (got[i].symbol_addr =3D=3D val) - return &got[i]; - } - return NULL; -} - -struct plt_entry { - /* - * Trampoline code to real target address. The return address - * should be the original (pc+4) before entring plt entry. - */ - u32 insn_auipc; /* auipc t0, 0x0 */ - u32 insn_ld; /* ld t1, 0x10(t0) */ - u32 insn_jr; /* jr t1 */ -}; - -#define OPC_AUIPC 0x0017 -#define OPC_LD 0x3003 -#define OPC_JALR 0x0067 -#define REG_T0 0x5 -#define REG_T1 0x6 - -static inline struct plt_entry emit_plt_entry(unsigned long val, - unsigned long plt, - unsigned long got_plt) -{ - /* - * U-Type encoding: - * +------------+----------+----------+ - * | imm[31:12] | rd[11:7] | opc[6:0] | - * +------------+----------+----------+ - * - * I-Type encoding: - * +------------+------------+--------+----------+----------+ - * | imm[31:20] | rs1[19:15] | funct3 | rd[11:7] | opc[6:0] | - * +------------+------------+--------+----------+----------+ - * - */ - unsigned long offset =3D got_plt - plt; - u32 hi20 =3D (offset + 0x800) & 0xfffff000; - u32 lo12 =3D (offset - hi20); - return (struct plt_entry) { - OPC_AUIPC | (REG_T0 << 7) | hi20, - OPC_LD | (lo12 << 20) | (REG_T0 << 15) | (REG_T1 << 7), - OPC_JALR | (REG_T1 << 15) - }; -} - -static inline int get_got_plt_idx(unsigned long val, const struct mod_sect= ion *sec) -{ - struct got_entry *got_plt =3D (struct got_entry *)sec->shdr->sh_addr; - int i; - for (i =3D 0; i < sec->num_entries; i++) { - if (got_plt[i].symbol_addr =3D=3D val) - return i; - } - return -1; -} - -static inline struct plt_entry *get_plt_entry(unsigned long val, - const struct mod_section *sec_plt, - const struct mod_section *sec_got_plt) -{ - struct plt_entry *plt =3D (struct plt_entry *)sec_plt->shdr->sh_addr; - int got_plt_idx =3D get_got_plt_idx(val, sec_got_plt); - if (got_plt_idx >=3D 0) - return plt + got_plt_idx; - else - return NULL; -} - #endif /* CONFIG_MODULE_SECTIONS */ =20 #endif /* _ASM_RISCV_MODULE_H */ diff --git a/arch/riscv/kernel/module-sections.c b/arch/riscv/kernel/module= -sections.c index e264e59e596e..39d4ac681c2a 100644 --- a/arch/riscv/kernel/module-sections.c +++ b/arch/riscv/kernel/module-sections.c @@ -10,6 +10,28 @@ #include #include =20 +struct got_entry { + unsigned long symbol_addr; /* the real variable address */ +}; + +static struct got_entry emit_got_entry(unsigned long val) +{ + return (struct got_entry) {val}; +} + +static struct got_entry *get_got_entry(unsigned long val, + const struct mod_section *sec) +{ + struct got_entry *got =3D (struct got_entry *)(sec->shdr->sh_addr); + int i; + + for (i =3D 0; i < sec->num_entries; i++) { + if (got[i].symbol_addr =3D=3D val) + return &got[i]; + } + return NULL; +} + unsigned long module_emit_got_entry(struct module *mod, unsigned long val) { struct mod_section *got_sec =3D &mod->arch.got; @@ -29,6 +51,74 @@ unsigned long module_emit_got_entry(struct module *mod, = unsigned long val) return (unsigned long)&got[i]; } =20 +struct plt_entry { + /* + * Trampoline code to real target address. The return address + * should be the original (pc+4) before entring plt entry. + */ + u32 insn_auipc; /* auipc t0, 0x0 */ + u32 insn_ld; /* ld t1, 0x10(t0) */ + u32 insn_jr; /* jr t1 */ +}; + +#define OPC_AUIPC 0x0017 +#define OPC_LD 0x3003 +#define OPC_JALR 0x0067 +#define REG_T0 0x5 +#define REG_T1 0x6 + +static struct plt_entry emit_plt_entry(unsigned long val, + unsigned long plt, + unsigned long got_plt) +{ + /* + * U-Type encoding: + * +------------+----------+----------+ + * | imm[31:12] | rd[11:7] | opc[6:0] | + * +------------+----------+----------+ + * + * I-Type encoding: + * +------------+------------+--------+----------+----------+ + * | imm[31:20] | rs1[19:15] | funct3 | rd[11:7] | opc[6:0] | + * +------------+------------+--------+----------+----------+ + * + */ + unsigned long offset =3D got_plt - plt; + u32 hi20 =3D (offset + 0x800) & 0xfffff000; + u32 lo12 =3D (offset - hi20); + + return (struct plt_entry) { + OPC_AUIPC | (REG_T0 << 7) | hi20, + OPC_LD | (lo12 << 20) | (REG_T0 << 15) | (REG_T1 << 7), + OPC_JALR | (REG_T1 << 15) + }; +} + +static int get_got_plt_idx(unsigned long val, const struct mod_section *se= c) +{ + struct got_entry *got_plt =3D (struct got_entry *)sec->shdr->sh_addr; + int i; + + for (i =3D 0; i < sec->num_entries; i++) { + if (got_plt[i].symbol_addr =3D=3D val) + return i; + } + return -1; +} + +static struct plt_entry *get_plt_entry(unsigned long val, + const struct mod_section *sec_plt, + const struct mod_section *sec_got_plt) +{ + struct plt_entry *plt =3D (struct plt_entry *)sec_plt->shdr->sh_addr; + int got_plt_idx =3D get_got_plt_idx(val, sec_got_plt); + + if (got_plt_idx >=3D 0) + return plt + got_plt_idx; + else + return NULL; +} + unsigned long module_emit_plt_entry(struct module *mod, unsigned long val) { struct mod_section *got_plt_sec =3D &mod->arch.got_plt; --=20 2.35.1 From nobody Wed Jun 24 01:02:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D355C433F5 for ; Thu, 24 Feb 2022 15:25:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236224AbiBXP0O (ORCPT ); Thu, 24 Feb 2022 10:26:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236167AbiBXPZu (ORCPT ); Thu, 24 Feb 2022 10:25:50 -0500 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01D7822B955 for ; Thu, 24 Feb 2022 07:25:20 -0800 (PST) Received: by mail-ej1-x634.google.com with SMTP id d10so5078433eje.10 for ; Thu, 24 Feb 2022 07:25:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=F5OyyqcGBOYroe/L/tNkoUrlGSaTDcsxizXKN09y7yE=; b=Kaew0SH/TCeAiPwYdosspcB1TVRMXC5YKrH07NT6MH1F9SHJMvC24ODuaK0LJk/93z D/SLzfgn9VdYpwV34WRYr62vhZCvqz4jdXFSGzvRyj+ZEMvNiRxS8USUXUp3FF9ByLzz qjNQ3f6uPY5rMezNzpKZMfbgrN8bBmwSyLEmyC7O05AiWfznUZl05TThgSt+xEEdQhrv DuJQm35TJH1TJlir31NOJZ1yNI6C3F2O2TR9tCKkp0dcV9FMyr+fnowbWjF/8VftngSF LaaY7VsxpTJsZ7mUevzKqpX2SpVSWH4kJ1lj3RvJpQkVU+EnlF5mIrmFI/povniM0Urj KiZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=F5OyyqcGBOYroe/L/tNkoUrlGSaTDcsxizXKN09y7yE=; b=kJSiSoDkZpk0/eGc6nkQB6hN2SuuVMiskZ3gnf5CJTniDS9BGOFZLVVdKEgh2jRZFH OL1s2Ab5CRwt7oOn9VaDTdxleD2V+fq2pWK+wq62xl7VuzR+yV5uw4MHHfPYOXFh/wmN nvHcf7M+gJLgugboNbhtcJitCboGQFXnZRiBKv+XV/tQLRvc5egMd1Oa38C1PuBRu3iP hNchoeN5NeDhEglDhjOUdFE21G35Gs037IZpkVQyrTB7ub9Gdh7EdZ/CWyKam+RS9cRg ifEUObFypxs9yFZo+2UvcqHlEA2dXq5s+RcBzHgDB/gbncn+oC1sPJWAfVgL9mchJkt/ EZBA== X-Gm-Message-State: AOAM531atNOZLNl3AMoM/SQnv+NKTuQZioBeD0O/L2sj+4r+b3ZTCWXI Lb027xN68MFEaKodzo5a/pM= X-Google-Smtp-Source: ABdhPJxjOawtmnv3q4ky6UnhfDKTXZ8H/SdiRNzzbQmUMaqQUFmexWJyIZun3SuAUQl6+eDK150nTg== X-Received: by 2002:a17:906:66cb:b0:6cf:e4f7:9504 with SMTP id k11-20020a17090666cb00b006cfe4f79504mr2735299ejp.142.1645716319481; Thu, 24 Feb 2022 07:25:19 -0800 (PST) Received: from stitch.. (80.71.140.73.ipv4.parknet.dk. [80.71.140.73]) by smtp.gmail.com with ESMTPSA id s11sm1509693edt.10.2022.02.24.07.25.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 07:25:19 -0800 (PST) Sender: Emil Renner Berthing From: Emil Renner Berthing To: linux-riscv@lists.infradead.org Cc: Emil Renner Berthing , Steven Rostedt , Ingo Molnar , Paul Walmsley , Palmer Dabbelt , Albert Ou , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Ard Biesheuvel , Jisheng Zhang , Alexandre Ghiti , linux-kernel@vger.kernel.org Subject: [PATCH v3 3/8] riscv: Remove unneeded definitions from asm/ftrace.h Date: Thu, 24 Feb 2022 16:24:51 +0100 Message-Id: <20220224152456.493365-4-kernel@esmil.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220224152456.493365-1-kernel@esmil.dk> References: <20220224152456.493365-1-kernel@esmil.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The macros for generating the auipc + jalr instruction pair is only ever used in kernel/ftrace.c, so move the definitions there. Signed-off-by: Emil Renner Berthing --- arch/riscv/include/asm/ftrace.h | 35 +-------------------------------- arch/riscv/kernel/ftrace.c | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrac= e.h index 04dad3380041..585714993749 100644 --- a/arch/riscv/include/asm/ftrace.h +++ b/arch/riscv/include/asm/ftrace.h @@ -36,41 +36,8 @@ struct dyn_arch_ftrace { #endif =20 #ifdef CONFIG_DYNAMIC_FTRACE -/* - * A general call in RISC-V is a pair of insts: - * 1) auipc: setting high-20 pc-related bits to ra register - * 2) jalr: setting low-12 offset to ra, jump to ra, and set ra to - * return address (original pc + 4) - * - * Dynamic ftrace generates probes to call sites, so we must deal with - * both auipc and jalr at the same time. - */ - -#define MCOUNT_ADDR ((unsigned long)MCOUNT_NAME) -#define JALR_SIGN_MASK (0x00000800) -#define JALR_OFFSET_MASK (0x00000fff) -#define AUIPC_OFFSET_MASK (0xfffff000) -#define AUIPC_PAD (0x00001000) -#define JALR_SHIFT 20 -#define JALR_BASIC (0x000080e7) -#define AUIPC_BASIC (0x00000097) -#define NOP4 (0x00000013) - -#define make_call(caller, callee, call) \ -do { \ - call[0] =3D to_auipc_insn((unsigned int)((unsigned long)callee - \ - (unsigned long)caller)); \ - call[1] =3D to_jalr_insn((unsigned int)((unsigned long)callee - \ - (unsigned long)caller)); \ -} while (0) - -#define to_jalr_insn(offset) \ - (((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_BASIC) =20 -#define to_auipc_insn(offset) \ - ((offset & JALR_SIGN_MASK) ? \ - (((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) | AUIPC_BASIC) : \ - ((offset & AUIPC_OFFSET_MASK) | AUIPC_BASIC)) +#define MCOUNT_ADDR ((unsigned long)MCOUNT_NAME) =20 /* * Let auipc+jalr be the basic *mcount unit*, so we make it 8 bytes here. diff --git a/arch/riscv/kernel/ftrace.c b/arch/riscv/kernel/ftrace.c index 4716f4cdc038..2cc15dc45ce0 100644 --- a/arch/riscv/kernel/ftrace.c +++ b/arch/riscv/kernel/ftrace.c @@ -12,6 +12,41 @@ #include =20 #ifdef CONFIG_DYNAMIC_FTRACE +/* + * A general call in RISC-V is a pair of insts: + * 1) auipc: setting high-20 pc-related bits to ra register + * 2) jalr: setting low-12 offset to ra, jump to ra, and set ra to + * return address (original pc + 4) + * + * Dynamic ftrace generates probes to call sites, so we must deal with + * both auipc and jalr at the same time. + */ + +#define JALR_SIGN_MASK (0x00000800) +#define JALR_OFFSET_MASK (0x00000fff) +#define AUIPC_OFFSET_MASK (0xfffff000) +#define AUIPC_PAD (0x00001000) +#define JALR_SHIFT 20 +#define JALR_BASIC (0x000080e7) +#define AUIPC_BASIC (0x00000097) +#define NOP4 (0x00000013) + +#define make_call(caller, callee, call) \ +do { \ + call[0] =3D to_auipc_insn((unsigned int)((unsigned long)callee - \ + (unsigned long)caller)); \ + call[1] =3D to_jalr_insn((unsigned int)((unsigned long)callee - \ + (unsigned long)caller)); \ +} while (0) + +#define to_jalr_insn(offset) \ + (((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_BASIC) + +#define to_auipc_insn(offset) \ + ((offset & JALR_SIGN_MASK) ? \ + (((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) | AUIPC_BASIC) : \ + ((offset & AUIPC_OFFSET_MASK) | AUIPC_BASIC)) + int ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex) { mutex_lock(&text_mutex); --=20 2.35.1 From nobody Wed Jun 24 01:02:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 32956C433EF for ; Thu, 24 Feb 2022 15:25:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236274AbiBXP0W (ORCPT ); Thu, 24 Feb 2022 10:26:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236181AbiBXPZw (ORCPT ); Thu, 24 Feb 2022 10:25:52 -0500 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E22FA22B95F for ; Thu, 24 Feb 2022 07:25:21 -0800 (PST) Received: by mail-ej1-x62b.google.com with SMTP id qk11so5167288ejb.2 for ; Thu, 24 Feb 2022 07:25:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xb6ZbsBfJcNQTb4eFVxZ+eD4ollYnCaYaEUkZiE9buU=; b=XE4Mjw0oW6cyh7Jz/klql52/xhVYa6R3/iQA8JMo4UcXgDnA7XP/kt9VYeJjcct+TU fk4oaYlJrq1hov7+CB8pLoqAIyKv0UC2H25etYmgwtsr4WVP14vrICEZ5BgFFfFWU29e DRVioE05DadX6IfGCD+P7K9pHHkMrtzANp6NuKOWeHc+2ENGdJaGcli8AuoXXlB1/bpQ ue8hNGTE7Unql/nSYX95SRS1v2QRT9XD728n90NAVUHegHsCrZyyxhad61I3wI52C+m2 rt5/9+PVIa8+J0vi43+bWyU1ZpJ1OzngNS/tZUF7gqylKf4z8jrPmMjCnhY9ny3/zBs/ +ddQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=xb6ZbsBfJcNQTb4eFVxZ+eD4ollYnCaYaEUkZiE9buU=; b=vqqnlYdjUk9dxCUBPm3/bAqPiFnNYj7O1whlLnBwqdnHDDL+I2APRRzLVulhSeIm00 l6+2mLOHCCsSafN4X7/ntbJ8fSVbIqXXXhVLfqzXNLwRw58YnJwaMcKEUmEKOP3OVnX8 b+ypcAcQXJ+IlZzcGVds4l/sk+eGxNedWzv2rc4rbk0xixtYdyqTsAgim2TwsNfP9i8V N0V7J57ZZROw9zRt/FpG2o1NRcxMzQ94Zp2CJA78NfrIIVzEEEgmm+mAofIWanNAAPRZ sHdLG3NO007Pc9dAYpCbeHmN+xIqDh56Bfdtte3knY1qTEiUAiurpAeky/DU+zNq3ckV FAVA== X-Gm-Message-State: AOAM530P6Xd/gA+y59n4PA70Xa1szBc9UcrzayYPsoTcldWT5x6jQp9V Q4bbYBSMsvtRyJ2OFQuHk8E= X-Google-Smtp-Source: ABdhPJyal/+V9nN3rpbn43hnO657ZZ2qZJ/qzGLGOKSpxlqsaAfLg7zoK74dogk+Cxfnd6mQyPpG/Q== X-Received: by 2002:a17:907:3c7:b0:6cd:5399:c1ad with SMTP id su7-20020a17090703c700b006cd5399c1admr2687583ejb.547.1645716320508; Thu, 24 Feb 2022 07:25:20 -0800 (PST) Received: from stitch.. (80.71.140.73.ipv4.parknet.dk. [80.71.140.73]) by smtp.gmail.com with ESMTPSA id s11sm1509693edt.10.2022.02.24.07.25.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 07:25:20 -0800 (PST) Sender: Emil Renner Berthing From: Emil Renner Berthing To: linux-riscv@lists.infradead.org Cc: Emil Renner Berthing , Steven Rostedt , Ingo Molnar , Paul Walmsley , Palmer Dabbelt , Albert Ou , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Ard Biesheuvel , Jisheng Zhang , Alexandre Ghiti , linux-kernel@vger.kernel.org Subject: [PATCH v3 4/8] riscv: Add asm/insn.h header Date: Thu, 24 Feb 2022 16:24:52 +0100 Message-Id: <20220224152456.493365-5-kernel@esmil.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220224152456.493365-1-kernel@esmil.dk> References: <20220224152456.493365-1-kernel@esmil.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add new asm/insn.h header to consolidate RISC-V instruction constants and inline helper functions. Signed-off-by: Emil Renner Berthing --- arch/riscv/include/asm/insn.h | 151 ++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 arch/riscv/include/asm/insn.h diff --git a/arch/riscv/include/asm/insn.h b/arch/riscv/include/asm/insn.h new file mode 100644 index 000000000000..02f387a06ef3 --- /dev/null +++ b/arch/riscv/include/asm/insn.h @@ -0,0 +1,151 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2020 Emil Renner Berthing + */ +#ifndef __ASM_RISCV_INSN_H +#define __ASM_RISCV_INSN_H + +#include + +#define RISCV_INSN_LW _AC(0x00002003, U) +#define RISCV_INSN_LD _AC(0x00003003, U) +#define RISCV_INSN_ADDI _AC(0x00000013, U) +#define RISCV_INSN_NOP RISCV_INSN_ADDI +#define RISCV_INSN_SW _AC(0x00002023, U) +#define RISCV_INSN_SD _AC(0x00003023, U) +#define RISCV_INSN_AUIPC _AC(0x00000017, U) +#define RISCV_INSN_LUI _AC(0x00000037, U) +#define RISCV_INSN_JALR _AC(0x00000067, U) +#define RISCV_INSN_JAL _AC(0x0000006f, U) + +#if __riscv_xlen =3D=3D 64 +#define RISCV_INSN_REG_L RISCV_INSN_LD +#define RISCV_INSN_REG_S RISCV_INSN_SD +#define RISCV_INSN_SZREG 8 +#elif __riscv_xlen =3D=3D 32 +#define RISCV_INSN_REG_L RISCV_INSN_LW +#define RISCV_INSN_REG_S RISCV_INSN_SW +#define RISCV_INSN_SZREG 4 +#else +#error "Unexpected __riscv_xlen" +#endif + +#define RISCV_INSN_RA _AC(0x1, U) +#define RISCV_INSN_SP _AC(0x2, U) +#define RISCV_INSN_T0 _AC(0x5, U) +#define RISCV_INSN_T1 _AC(0x6, U) + +#define RISCV_INSN_RD_POS 7 +#define RISCV_INSN_RD_RA (RISCV_INSN_RA << RISCV_INSN_RD_POS) +#define RISCV_INSN_RD_SP (RISCV_INSN_SP << RISCV_INSN_RD_POS) +#define RISCV_INSN_RD_T0 (RISCV_INSN_T0 << RISCV_INSN_RD_POS) +#define RISCV_INSN_RD_T1 (RISCV_INSN_T1 << RISCV_INSN_RD_POS) + +#define RISCV_INSN_RS1_POS 15 +#define RISCV_INSN_RS1_RA (RISCV_INSN_RA << RISCV_INSN_RS1_POS) +#define RISCV_INSN_RS1_SP (RISCV_INSN_SP << RISCV_INSN_RS1_POS) +#define RISCV_INSN_RS1_T0 (RISCV_INSN_T0 << RISCV_INSN_RS1_POS) +#define RISCV_INSN_RS1_T1 (RISCV_INSN_T1 << RISCV_INSN_RS1_POS) + +#define RISCV_INSN_RS2_POS 20 +#define RISCV_INSN_RS2_RA (RISCV_INSN_RA << RISCV_INSN_RS2_POS) +#define RISCV_INSN_RS2_SP (RISCV_INSN_SP << RISCV_INSN_RS2_POS) +#define RISCV_INSN_RS2_T0 (RISCV_INSN_T0 << RISCV_INSN_RS2_POS) +#define RISCV_INSN_RS2_T1 (RISCV_INSN_T1 << RISCV_INSN_RS2_POS) + +#define RISCV_INSN_I_IMM_MASK _AC(0xfff00000, U) +#define RISCV_INSN_S_IMM_MASK _AC(0xfe000f80, U) +#define RISCV_INSN_B_IMM_MASK _AC(0xfe000f80, U) +#define RISCV_INSN_U_IMM_MASK _AC(0xfffff000, U) +#define RISCV_INSN_J_IMM_MASK _AC(0xfffff000, U) + +#define RISCV_INSN_CI_IMM_MASK _AC(0x107c, U) +#define RISCV_INSN_CSS_IMM_MASK _AC(0x1f80, U) +#define RISCV_INSN_CIW_IMM_MASK _AC(0x1fe0, U) +#define RISCV_INSN_CL_IMM_MASK _AC(0x1c60, U) +#define RISCV_INSN_CS_IMM_MASK _AC(0x1c60, U) +#define RISCV_INSN_CB_IMM_MASK _AC(0x1c7c, U) +#define RISCV_INSN_CJ_IMM_MASK _AC(0x1ffc, U) + +#ifndef __ASSEMBLY__ +#include +#include + +/* + * The J-format jump and link instruction, jal, has a 20bit immediate exce= pt + * bit 0 is always taken to be 0, so it must be even and is interpreted as= a + * signed value. + */ +static inline bool riscv_insn_valid_20bit_offset(ptrdiff_t val) +{ + return !(val & 1) && -(1L << 19) <=3D val && val < (1L << 19); +} + +/* + * The auipc+jalr instruction pair can reach any PC-relative offset + * in the range [-2^31 - 2^11, 2^31 - 2^11) + */ +static inline bool riscv_insn_valid_32bit_offset(ptrdiff_t val) +{ +#if __riscv_xlen =3D=3D 32 + return true; +#else + return (-(1L << 31) - (1L << 11)) <=3D val && val < ((1L << 31) - (1L << = 11)); +#endif +} + +static inline u32 riscv_insn_i_imm(u32 imm) +{ + return (imm & GENMASK(11, 0)) << 20; +} + +static inline u32 riscv_insn_s_imm(u32 imm) +{ + return (imm & GENMASK( 4, 0)) << ( 7 - 0) | + (imm & GENMASK(11, 5)) << (25 - 5); +} + +static inline u32 riscv_insn_b_imm(u32 imm) +{ + return (imm & GENMASK(11, 11)) >> (11 - 7) | + (imm & GENMASK( 4, 1)) << ( 8 - 1) | + (imm & GENMASK(10, 5)) << (25 - 5) | + (imm & GENMASK(12, 12)) << (31 - 12); +} + +static inline u32 riscv_insn_u_imm(u32 imm) +{ + return imm & GENMASK(31, 12); +} + +static inline u32 riscv_insn_j_imm(u32 imm) +{ + return (imm & GENMASK(19, 12)) << (12 - 12) | + (imm & GENMASK(11, 11)) << (20 - 11) | + (imm & GENMASK(10, 1)) << (21 - 1) | + (imm & GENMASK(20, 20)) << (31 - 20); +} + +static inline u16 riscv_insn_rvc_branch_imm(u16 imm) +{ + return (imm & GENMASK(5, 5)) >> ( 5 - 2) | + (imm & GENMASK(2, 1)) << ( 3 - 1) | + (imm & GENMASK(7, 6)) >> ( 6 - 5) | + (imm & GENMASK(4, 3)) << (10 - 3) | + (imm & GENMASK(8, 8)) << (12 - 8); +} + +static inline u16 riscv_insn_rvc_jump_imm(u16 imm) +{ + return (imm & GENMASK( 5, 5)) >> ( 5 - 2) | + (imm & GENMASK( 3, 1)) << ( 3 - 1) | + (imm & GENMASK( 7, 7)) >> ( 7 - 6) | + (imm & GENMASK( 6, 6)) << ( 7 - 6) | + (imm & GENMASK(10, 10)) >> (10 - 8) | + (imm & GENMASK( 9, 8)) << ( 9 - 8) | + (imm & GENMASK( 4, 4)) << (11 - 4) | + (imm & GENMASK(11, 11)) << (12 - 11); +} + +#endif +#endif --=20 2.35.1 From nobody Wed Jun 24 01:02:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CC416C433EF for ; Thu, 24 Feb 2022 15:26:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236212AbiBXP0d (ORCPT ); Thu, 24 Feb 2022 10:26:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236219AbiBXPZ6 (ORCPT ); Thu, 24 Feb 2022 10:25:58 -0500 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0350E22B961 for ; Thu, 24 Feb 2022 07:25:23 -0800 (PST) Received: by mail-ej1-x62f.google.com with SMTP id qk11so5167416ejb.2 for ; Thu, 24 Feb 2022 07:25:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zFC8RrMScueCrfbuzC7oMwjqoiodd5KWPoUO6iBs4zE=; b=P9rZ74f43/Rjcitt8roVzvprR/aM94XNNwRVYeBMvQ/DGIKdO/YRwGvZE5bPjiAGVJ 7wSGKeQObBcYFKHLvZKseUmZ6f65JnRyaulYCnOWQQLTOlU9YC5Cb4S7qL1E5nRaWV5v 1o8QSZETpMDY3DqZeCHG1NWXzHE6nStg3l5bdLPF36mBBPWwD6OA83uchpYqAvV87L5d m1r9vqNnVBX8wHoXeSY4zeU7c+oqEPwDypSJLZYXfarvMu2b0k8LoTjjo6zouhLdPk9t cA0SoEQsYdAKHcJ3vEkC2LvPYcF/UZqydktdfIkCracdHCYjJsq4Pz1qRYYF9hOlSGR1 sSnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=zFC8RrMScueCrfbuzC7oMwjqoiodd5KWPoUO6iBs4zE=; b=Dolicz5xzaCBIF2eNPI/3XJZQA+uM2oOy4txipqJ9EYGQgffn4ad8cyit3UcGl6Cgp zXHol/e8NA1hj4HexOQ7yD0bK5lxymy89GSfVAwdOsknqNjR2uwIjx9Ozg6tEV9/pcL4 QDCfHsD8CG+1JTwQTY9zN/CNJhdHfrnh8EByXfLxVS0g3MMJiJR/yt4egTNxB/58ayug 9EZfV4Xzy8MedEcp/YyjpUjQd8wSppION17VmflrNKCbJJRUvmhTnfX3ou8Rgl+iA6pd i08J1lu/hlfBS3D2hcEZVSeeozBqK4OpfXjMou2JkrIiKUvyX9iSB8FfLgXLPBOi+/xI GRRA== X-Gm-Message-State: AOAM533wHq3BngHC/DqiwcpVQ5Y5HCLgUsN/7iYlphzeM6eLk9Q7DlpE 0MdnJUemrV4Etct4Ekx3vk8= X-Google-Smtp-Source: ABdhPJxUseKMXbJF3jbI1BLU1twwDUjL/41Lrh42NnUGf0qAivjc9hNG77gdcKbTvefH8Hrz4eQKgQ== X-Received: by 2002:a17:906:7852:b0:6ce:88f5:4dc9 with SMTP id p18-20020a170906785200b006ce88f54dc9mr2720606ejm.491.1645716321533; Thu, 24 Feb 2022 07:25:21 -0800 (PST) Received: from stitch.. (80.71.140.73.ipv4.parknet.dk. [80.71.140.73]) by smtp.gmail.com with ESMTPSA id s11sm1509693edt.10.2022.02.24.07.25.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 07:25:21 -0800 (PST) Sender: Emil Renner Berthing From: Emil Renner Berthing To: linux-riscv@lists.infradead.org Cc: Emil Renner Berthing , Steven Rostedt , Ingo Molnar , Paul Walmsley , Palmer Dabbelt , Albert Ou , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Ard Biesheuvel , Jisheng Zhang , Alexandre Ghiti , linux-kernel@vger.kernel.org Subject: [PATCH v3 5/8] riscv: Use asm/insn.h for module relocations Date: Thu, 24 Feb 2022 16:24:53 +0100 Message-Id: <20220224152456.493365-6-kernel@esmil.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220224152456.493365-1-kernel@esmil.dk> References: <20220224152456.493365-1-kernel@esmil.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This converts the module relocations in kernel/module.c to use asm/insn.h for instruction manipulation. Also RISC-V has a number of instruction pairs to generate 32bit immediates or jump/call offsets. Eg.: lui rd, hi20 addi rd, rd, lo12 ..where hi20 is the upper 20bits to load into register rd and lo12 is the lower 12bits. However both immediates are interpreted as two's complement signed values. Hence the old code calculates hi20 and lo12 for 32bit immediates imm like this: hi20 =3D (imm + 0x800) & 0xfffff000; lo12 =3D (imm - hi20) & 0xfff; This patch simplifies it to: hi20 =3D (imm + 0x800) & 0xfffff000; lo12 =3D imm & 0xfff; ..which amounts to the same: imm - hi20 may be become negative/underflow, but it doesn't change the lower 12 bits. Signed-off-by: Emil Renner Berthing --- arch/riscv/kernel/module.c | 139 +++++++++++++++---------------------- 1 file changed, 56 insertions(+), 83 deletions(-) diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index 8d6a16d74b5b..2212d88776e0 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -2,6 +2,7 @@ /* * * Copyright (C) 2017 Zihao Yu + * Copyright (C) 2020 Emil Renner Berthing */ =20 #include @@ -11,39 +12,27 @@ #include #include #include +#include #include =20 -/* - * The auipc+jalr instruction pair can reach any PC-relative offset - * in the range [-2^31 - 2^11, 2^31 - 2^11) - */ -static bool riscv_insn_valid_32bit_offset(ptrdiff_t val) -{ -#ifdef CONFIG_32BIT - return true; -#else - return (-(1L << 31) - (1L << 11)) <=3D val && val < ((1L << 31) - (1L << = 11)); -#endif -} - -static int riscv_insn_rmw(void *location, u32 keep, u32 set) +static int riscv_insn_rmw(void *location, u32 mask, u32 value) { u16 *parcel =3D location; u32 insn =3D (u32)parcel[0] | (u32)parcel[1] << 16; =20 - insn &=3D keep; - insn |=3D set; + insn &=3D ~mask; + insn |=3D value; =20 parcel[0] =3D insn; parcel[1] =3D insn >> 16; return 0; } =20 -static int riscv_insn_rvc_rmw(void *location, u16 keep, u16 set) +static int riscv_insn_rvc_rmw(void *location, u16 mask, u16 value) { u16 *parcel =3D location; =20 - *parcel =3D (*parcel & keep) | set; + *parcel =3D (*parcel & ~mask) | value; return 0; } =20 @@ -68,55 +57,40 @@ static int apply_r_riscv_branch_rela(struct module *me,= void *location, Elf_Addr v) { ptrdiff_t offset =3D (void *)v - location; - u32 imm12 =3D (offset & 0x1000) << (31 - 12); - u32 imm11 =3D (offset & 0x800) >> (11 - 7); - u32 imm10_5 =3D (offset & 0x7e0) << (30 - 10); - u32 imm4_1 =3D (offset & 0x1e) << (11 - 4); =20 - return riscv_insn_rmw(location, 0x1fff07f, imm12 | imm11 | imm10_5 | imm4= _1); + return riscv_insn_rmw(location, + RISCV_INSN_B_IMM_MASK, + riscv_insn_b_imm(offset)); } =20 static int apply_r_riscv_jal_rela(struct module *me, void *location, Elf_Addr v) { ptrdiff_t offset =3D (void *)v - location; - u32 imm20 =3D (offset & 0x100000) << (31 - 20); - u32 imm19_12 =3D (offset & 0xff000); - u32 imm11 =3D (offset & 0x800) << (20 - 11); - u32 imm10_1 =3D (offset & 0x7fe) << (30 - 10); =20 - return riscv_insn_rmw(location, 0xfff, imm20 | imm19_12 | imm11 | imm10_1= ); + return riscv_insn_rmw(location, + RISCV_INSN_J_IMM_MASK, + riscv_insn_j_imm(offset)); } =20 static int apply_r_riscv_rvc_branch_rela(struct module *me, void *location, Elf_Addr v) { ptrdiff_t offset =3D (void *)v - location; - u16 imm8 =3D (offset & 0x100) << (12 - 8); - u16 imm7_6 =3D (offset & 0xc0) >> (6 - 5); - u16 imm5 =3D (offset & 0x20) >> (5 - 2); - u16 imm4_3 =3D (offset & 0x18) << (12 - 5); - u16 imm2_1 =3D (offset & 0x6) << (12 - 10); - - return riscv_insn_rvc_rmw(location, 0xe383, - imm8 | imm7_6 | imm5 | imm4_3 | imm2_1); + + return riscv_insn_rvc_rmw(location, + RISCV_INSN_CB_IMM_MASK, + riscv_insn_rvc_branch_imm(offset)); } =20 static int apply_r_riscv_rvc_jump_rela(struct module *me, void *location, Elf_Addr v) { ptrdiff_t offset =3D (void *)v - location; - u16 imm11 =3D (offset & 0x800) << (12 - 11); - u16 imm10 =3D (offset & 0x400) >> (10 - 8); - u16 imm9_8 =3D (offset & 0x300) << (12 - 11); - u16 imm7 =3D (offset & 0x80) >> (7 - 6); - u16 imm6 =3D (offset & 0x40) << (12 - 11); - u16 imm5 =3D (offset & 0x20) >> (5 - 2); - u16 imm4 =3D (offset & 0x10) << (12 - 5); - u16 imm3_1 =3D (offset & 0xe) << (12 - 10); - - return riscv_insn_rvc_rmw(location, 0xe003, - imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1); + + return riscv_insn_rvc_rmw(location, + RISCV_INSN_CJ_IMM_MASK, + riscv_insn_rvc_jump_imm(offset)); } =20 static int apply_r_riscv_pcrel_hi20_rela(struct module *me, void *location, @@ -131,30 +105,27 @@ static int apply_r_riscv_pcrel_hi20_rela(struct modul= e *me, void *location, return -EINVAL; } =20 - return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000); + return riscv_insn_rmw(location, + RISCV_INSN_U_IMM_MASK, + riscv_insn_u_imm(offset + 0x800)); } =20 static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, void *locati= on, Elf_Addr v) { - /* - * v is the lo12 value to fill. It is calculated before calling this - * handler. - */ - return riscv_insn_rmw(location, 0xfffff, (v & 0xfff) << 20); + /* v is already the relative offset */ + return riscv_insn_rmw(location, + RISCV_INSN_I_IMM_MASK, + riscv_insn_i_imm(v)); } =20 static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, void *locati= on, Elf_Addr v) { - /* - * v is the lo12 value to fill. It is calculated before calling this - * handler. - */ - u32 imm11_5 =3D (v & 0xfe0) << (31 - 11); - u32 imm4_0 =3D (v & 0x1f) << (11 - 4); - - return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0); + /* v is already the relative offset */ + return riscv_insn_rmw(location, + RISCV_INSN_S_IMM_MASK, + riscv_insn_s_imm(v)); } =20 static int apply_r_riscv_hi20_rela(struct module *me, void *location, @@ -167,29 +138,27 @@ static int apply_r_riscv_hi20_rela(struct module *me,= void *location, return -EINVAL; } =20 - return riscv_insn_rmw(location, 0xfff, ((s32)v + 0x800) & 0xfffff000); + return riscv_insn_rmw(location, + RISCV_INSN_U_IMM_MASK, + riscv_insn_u_imm(v + 0x800)); } =20 static int apply_r_riscv_lo12_i_rela(struct module *me, void *location, Elf_Addr v) { /* Skip medlow checking because of filtering by HI20 already */ - s32 hi20 =3D ((s32)v + 0x800) & 0xfffff000; - s32 lo12 =3D ((s32)v - hi20); - - return riscv_insn_rmw(location, 0xfffff, (lo12 & 0xfff) << 20); + return riscv_insn_rmw(location, + RISCV_INSN_I_IMM_MASK, + riscv_insn_i_imm(v)); } =20 static int apply_r_riscv_lo12_s_rela(struct module *me, void *location, Elf_Addr v) { /* Skip medlow checking because of filtering by HI20 already */ - s32 hi20 =3D ((s32)v + 0x800) & 0xfffff000; - s32 lo12 =3D ((s32)v - hi20); - u32 imm11_5 =3D (lo12 & 0xfe0) << (31 - 11); - u32 imm4_0 =3D (lo12 & 0x1f) << (11 - 4); - - return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0); + return riscv_insn_rmw(location, + RISCV_INSN_S_IMM_MASK, + riscv_insn_s_imm(v)); } =20 static int apply_r_riscv_got_hi20_rela(struct module *me, void *location, @@ -207,14 +176,15 @@ static int apply_r_riscv_got_hi20_rela(struct module = *me, void *location, return -EINVAL; } =20 - return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000); + return riscv_insn_rmw(location, + RISCV_INSN_U_IMM_MASK, + riscv_insn_u_imm(offset + 0x800)); } =20 static int apply_r_riscv_call_plt_rela(struct module *me, void *location, Elf_Addr v) { ptrdiff_t offset =3D (void *)v - location; - u32 hi20, lo12; =20 if (!riscv_insn_valid_32bit_offset(offset)) { /* Only emit the plt entry if offset over 32-bit range */ @@ -228,17 +198,18 @@ static int apply_r_riscv_call_plt_rela(struct module = *me, void *location, } } =20 - hi20 =3D (offset + 0x800) & 0xfffff000; - lo12 =3D (offset - hi20) & 0xfff; - riscv_insn_rmw(location, 0xfff, hi20); - return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20); + riscv_insn_rmw(location, + RISCV_INSN_U_IMM_MASK, + riscv_insn_u_imm(offset + 0x800)); + return riscv_insn_rmw(location + 4, + RISCV_INSN_I_IMM_MASK, + riscv_insn_i_imm(offset)); } =20 static int apply_r_riscv_call_rela(struct module *me, void *location, Elf_Addr v) { ptrdiff_t offset =3D (void *)v - location; - u32 hi20, lo12; =20 if (!riscv_insn_valid_32bit_offset(offset)) { pr_err( @@ -247,10 +218,12 @@ static int apply_r_riscv_call_rela(struct module *me,= void *location, return -EINVAL; } =20 - hi20 =3D (offset + 0x800) & 0xfffff000; - lo12 =3D (offset - hi20) & 0xfff; - riscv_insn_rmw(location, 0xfff, hi20); - return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20); + riscv_insn_rmw(location, + RISCV_INSN_U_IMM_MASK, + riscv_insn_u_imm(offset + 0x800)); + return riscv_insn_rmw(location + 4, + RISCV_INSN_I_IMM_MASK, + riscv_insn_i_imm(offset)); } =20 static int apply_r_riscv_relax_rela(struct module *me, void *location, --=20 2.35.1 From nobody Wed Jun 24 01:02:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9D99DC433F5 for ; Thu, 24 Feb 2022 15:26:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233995AbiBXP0m (ORCPT ); Thu, 24 Feb 2022 10:26:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236223AbiBXPZ6 (ORCPT ); Thu, 24 Feb 2022 10:25:58 -0500 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01D4722EDCC for ; Thu, 24 Feb 2022 07:25:24 -0800 (PST) Received: by mail-ej1-x635.google.com with SMTP id qk11so5167492ejb.2 for ; Thu, 24 Feb 2022 07:25:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Uv9kZcj+SYuMKmc2v8qVWQoeoD1xbOxuZhUO9LDOn7M=; b=bX8bQwwicUGsPVy4Y3mCqGccZPF/GGpbluqz317ey+zLTOQ8c1fHnPI/DfzWOV1UYh trfJkNv4XTVJi09MAREd/Uj1wKpzLT7JGQ7Tt+J6DfwVsdakLY+CYarxxiaBsF6EOifX G1LBOc3kKYU754bVeI8/3HceRIVewkj+qWX8iAHHYnfMLLVXVWFzlAFTkPcS705ti4HE WO4bvtl6dq7IvcNWGhEv6DQnxHq9LLmKzfqxpMcaC+SWiiiMWxkgVVA9U2SE5v97t0FH /ZXQPpG8O45IQmob4YkGgegJZnVWJNfRfHwTbLHoNQJfzgLF0l0XizzrW5m8vb7Gi82u +AeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=Uv9kZcj+SYuMKmc2v8qVWQoeoD1xbOxuZhUO9LDOn7M=; b=WJ9mEAkywcg4wAktxrpOhVHUf0Z7mgbl0FNxdA9TeEAb4dsN2aEjWS/2JzxUEv53vk F0ldpUfhD2bpFBi+9OLTBs1+zKSx2yFiH8XX5jdBnc03spMBYJd8dCuThuGW2NVDPLWI o7fhbO2TyNraRVxQjEIc0lm1llxg6ao/EF7ikaV9rUnuxy8RQwjLanUg6rttWl2ue8Hx jj+h4zcNfdgrPImet4G9kA40cbFtxWNP51qLwaecFbnsMJdqe70qnsocbbS90bvX243n BLx6CSY2poPq/KCbsh5pb0jQ2osriYmDcfR8UPSX6ArMt72Ii1YEetQIwT187PltO/8Y BPLg== X-Gm-Message-State: AOAM531bCXMpLWW1dVAxc3Y4aGorrxioPk96kNCaR+B+KqNL6zZUhHY7 zt06cvPgbb2Jd8hNlgCh8TM= X-Google-Smtp-Source: ABdhPJx2BEkVsAOUtvzhxXAulZFuBAavPEG0SaUBdCmd2wQhOtBfUaEYSeBVOeUXEPdh2WkJ1Gu0yg== X-Received: by 2002:a17:906:39da:b0:6cf:7f09:a7bc with SMTP id i26-20020a17090639da00b006cf7f09a7bcmr2769072eje.457.1645716322553; Thu, 24 Feb 2022 07:25:22 -0800 (PST) Received: from stitch.. (80.71.140.73.ipv4.parknet.dk. [80.71.140.73]) by smtp.gmail.com with ESMTPSA id s11sm1509693edt.10.2022.02.24.07.25.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 07:25:22 -0800 (PST) Sender: Emil Renner Berthing From: Emil Renner Berthing To: linux-riscv@lists.infradead.org Cc: Emil Renner Berthing , Steven Rostedt , Ingo Molnar , Paul Walmsley , Palmer Dabbelt , Albert Ou , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Ard Biesheuvel , Jisheng Zhang , Alexandre Ghiti , linux-kernel@vger.kernel.org Subject: [PATCH v3 6/8] riscv: Use asm/insn.h to generate plt entries Date: Thu, 24 Feb 2022 16:24:54 +0100 Message-Id: <20220224152456.493365-7-kernel@esmil.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220224152456.493365-1-kernel@esmil.dk> References: <20220224152456.493365-1-kernel@esmil.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This converts kernel/module-sections.c to use asm/insn.h to generate the instructions in the plt entries. Signed-off-by: Emil Renner Berthing --- arch/riscv/kernel/module-sections.c | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/arch/riscv/kernel/module-sections.c b/arch/riscv/kernel/module= -sections.c index 39d4ac681c2a..cb73399c3603 100644 --- a/arch/riscv/kernel/module-sections.c +++ b/arch/riscv/kernel/module-sections.c @@ -9,6 +9,7 @@ #include #include #include +#include =20 struct got_entry { unsigned long symbol_addr; /* the real variable address */ @@ -61,36 +62,16 @@ struct plt_entry { u32 insn_jr; /* jr t1 */ }; =20 -#define OPC_AUIPC 0x0017 -#define OPC_LD 0x3003 -#define OPC_JALR 0x0067 -#define REG_T0 0x5 -#define REG_T1 0x6 - static struct plt_entry emit_plt_entry(unsigned long val, unsigned long plt, unsigned long got_plt) { - /* - * U-Type encoding: - * +------------+----------+----------+ - * | imm[31:12] | rd[11:7] | opc[6:0] | - * +------------+----------+----------+ - * - * I-Type encoding: - * +------------+------------+--------+----------+----------+ - * | imm[31:20] | rs1[19:15] | funct3 | rd[11:7] | opc[6:0] | - * +------------+------------+--------+----------+----------+ - * - */ unsigned long offset =3D got_plt - plt; - u32 hi20 =3D (offset + 0x800) & 0xfffff000; - u32 lo12 =3D (offset - hi20); =20 return (struct plt_entry) { - OPC_AUIPC | (REG_T0 << 7) | hi20, - OPC_LD | (lo12 << 20) | (REG_T0 << 15) | (REG_T1 << 7), - OPC_JALR | (REG_T1 << 15) + RISCV_INSN_AUIPC | RISCV_INSN_RD_T0 | riscv_insn_u_imm(offset + 0x800), + RISCV_INSN_LD | RISCV_INSN_RD_T1 | RISCV_INSN_RS1_T0 | riscv_insn_i_i= mm(offset), + RISCV_INSN_JALR | RISCV_INSN_RS1_T1, }; } =20 --=20 2.35.1 From nobody Wed Jun 24 01:02:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 791B1C433EF for ; Thu, 24 Feb 2022 15:26:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236308AbiBXP0x (ORCPT ); Thu, 24 Feb 2022 10:26:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236236AbiBXPZ6 (ORCPT ); Thu, 24 Feb 2022 10:25:58 -0500 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBA8522A291 for ; Thu, 24 Feb 2022 07:25:24 -0800 (PST) Received: by mail-ej1-x62b.google.com with SMTP id hw13so5088006ejc.9 for ; Thu, 24 Feb 2022 07:25:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=U2McVQuv53c33dZYwtuytrfr7zhfRHKf5OwpDdiji0Q=; b=FeLVTuzc9wPFQH0ho6dLgoj+hWeW+fYvqEve7lVWA6cpLUDwNU3G4myXMgVZX8wV11 OFiFwl9VXdVCJxputF6RNS9sOj5Rz0iwt+Ccsi4YLAt5xJOMf3Oct4BF7CtYgp07Hx0Z Px1K1NlBlRUjfC2Mf9oijmdM3U+adJmgDX0+emytZDFkGZ7pQQk7dbvzutjnDBh65znN 5b/Qx/uM1lZm3wLRbuoCuqBXesw3IYGN3LBVCpCJDO4LdcN3Ao+RmXaZry2BPOuGUATy jXxmFYAm06C186UwdZme7YcBfIHiZRSc2jmnWWkU7kTp7vht4qvOi5IS+Hrk8gT0PR2u rRrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=U2McVQuv53c33dZYwtuytrfr7zhfRHKf5OwpDdiji0Q=; b=59CK9i67e5WiEkrzmez3LZCls/EsD0n0/upvoxWgGaCKXgbakjeyk4YizyrqEvcnV9 A9vtDgVynvm0TAkfi93lYo7jwrP/uWjkJYkHSXBhoKmR+kEvmeLHU7/u/i5Xc31perx6 Uvv2kFak0eUyjSxLWzHSRGQiGvsD2CKky/fT5yOv4ZeAoU1QuhTTXpg+T3OkTko5zfiP 3w5BnV8yO5o2mpLtkzXtZd+b6gBtwvNzoDSXtAohH+Q10nYGexUnNZLZX4NRo/NDYUgc amsqv3RzhGa3i0Suu29JmFgRBYcEmSl2p5jBx3Smdadbs8druoMLG3aukSDipBzZAYIC b4tg== X-Gm-Message-State: AOAM533xVhHzCGfQM7ZA/XjKPcmYyxkNv9CCdWVOYCSaZu7n9BUub9RB pmS5RYQT/TNC3t1npHQKx0Q= X-Google-Smtp-Source: ABdhPJzmtg5/x9WY3qCGLphHaryu+r7YthG+HP7Rm2wRL6NLXkgnTbKEoS4nS1g0S/n8hO2ecRMJzA== X-Received: by 2002:a17:906:65d5:b0:6ba:3133:dfb9 with SMTP id z21-20020a17090665d500b006ba3133dfb9mr2818930ejn.56.1645716323553; Thu, 24 Feb 2022 07:25:23 -0800 (PST) Received: from stitch.. (80.71.140.73.ipv4.parknet.dk. [80.71.140.73]) by smtp.gmail.com with ESMTPSA id s11sm1509693edt.10.2022.02.24.07.25.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 07:25:23 -0800 (PST) Sender: Emil Renner Berthing From: Emil Renner Berthing To: linux-riscv@lists.infradead.org Cc: Emil Renner Berthing , Steven Rostedt , Ingo Molnar , Paul Walmsley , Palmer Dabbelt , Albert Ou , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Ard Biesheuvel , Jisheng Zhang , Alexandre Ghiti , linux-kernel@vger.kernel.org Subject: [PATCH v3 7/8] riscv: Use asm/insn.h for jump labels Date: Thu, 24 Feb 2022 16:24:55 +0100 Message-Id: <20220224152456.493365-8-kernel@esmil.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220224152456.493365-1-kernel@esmil.dk> References: <20220224152456.493365-1-kernel@esmil.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This converts kernel/jump_label.c to use asm/insn.h to generate the jump/nop instructions. Signed-off-by: Emil Renner Berthing --- arch/riscv/kernel/jump_label.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/arch/riscv/kernel/jump_label.c b/arch/riscv/kernel/jump_label.c index 20e09056d141..b5b4892c3e9e 100644 --- a/arch/riscv/kernel/jump_label.c +++ b/arch/riscv/kernel/jump_label.c @@ -9,11 +9,9 @@ #include #include #include +#include #include =20 -#define RISCV_INSN_NOP 0x00000013U -#define RISCV_INSN_JAL 0x0000006fU - void arch_jump_label_transform(struct jump_entry *entry, enum jump_label_type type) { @@ -23,14 +21,10 @@ void arch_jump_label_transform(struct jump_entry *entry, if (type =3D=3D JUMP_LABEL_JMP) { long offset =3D jump_entry_target(entry) - jump_entry_code(entry); =20 - if (WARN_ON(offset & 1 || offset < -524288 || offset >=3D 524288)) + if (WARN_ON(!riscv_insn_valid_20bit_offset(offset))) return; =20 - insn =3D RISCV_INSN_JAL | - (((u32)offset & GENMASK(19, 12)) << (12 - 12)) | - (((u32)offset & GENMASK(11, 11)) << (20 - 11)) | - (((u32)offset & GENMASK(10, 1)) << (21 - 1)) | - (((u32)offset & GENMASK(20, 20)) << (31 - 20)); + insn =3D RISCV_INSN_JAL | riscv_insn_j_imm(offset); } else { insn =3D RISCV_INSN_NOP; } --=20 2.35.1 From nobody Wed Jun 24 01:02:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 070EAC433EF for ; Thu, 24 Feb 2022 15:26:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236300AbiBXP0t (ORCPT ); Thu, 24 Feb 2022 10:26:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236241AbiBXPZ7 (ORCPT ); Thu, 24 Feb 2022 10:25:59 -0500 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5F1122EDEB for ; Thu, 24 Feb 2022 07:25:27 -0800 (PST) Received: by mail-ed1-x52a.google.com with SMTP id h15so3353609edv.7 for ; Thu, 24 Feb 2022 07:25:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Pg83usMw5uRQIx1/D/J156mChwwgfj7a6QBGSOjo5rI=; b=iFZIadtNxbwHIW9fHYTCppxMigghvTsOZRNML6dHUjyUjy4oLhptJgn8ccs4spGjgB yCvA9Ni3vnDhLaO43nFU3/ciGraWo4FFoZtgdpMDGsP9nA9wXHwEX3ufgDwMIxMkAYi9 6/oIvtib8AZ6kzkzcIBXYYZ+FfMA2q/pL/++3WmCZP1OvCoJBHQaijDYqG3obTSdDtDo 9cB1dqru6ytirRMj5YzyQFuP70K/yrnvOTTjnBdpinpGSoLFnx2WnQsPSkuR1h2vZfmM 7GtIU1UAH6IjZQ0d7f+WWwh+l2mCKzRgRAD/TWskng/2TJcF1kLeCp+hyZ/zNGowles7 FBvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=Pg83usMw5uRQIx1/D/J156mChwwgfj7a6QBGSOjo5rI=; b=k0XC3nRgxiBPmHaV98MHbvJwDO/vutdC0Mmkd2uftyKMo75/sz34DYbALbiRYuaAf2 p0NiVFbZkygK/Fdx3nS2ZtCXY6Spn7pYAGqCTUBLhdMpVQcIKzvvaMAhpfYXpH7+ni+M QqAMImtKmdy508NqalyJhoKUzOIM2boMlm/8iWxRyhD7+UT5dPoO+YtZ3FlD0yuz+iWS U0Duoqvjs4vRJ8rlpa3TlxjFUxa7pCc0mlZuxI1gEzaJ7GDV45Q62jRgLt+rwevpH7X0 K4n3dfVth8s2FTlGHJJ/9riOVZxe39rEsDjyJxiOqqPENGzR6glrfPW0ivHUuJtG8wVy IsiQ== X-Gm-Message-State: AOAM532qSffz4OnbRMOXPUEW8l/+U6dKoCmgpxKBNAITxmWO01URLQaw bYHK7LoO4qrL3Bnzcg7ZKNI= X-Google-Smtp-Source: ABdhPJyIgk54xHRmo7jfYS9ym3DA+LZ7j94fiqCQC3NeW5TeueHYzp8dNzSBavEaZQbEyL///blaiw== X-Received: by 2002:a05:6402:144b:b0:410:b990:a68a with SMTP id d11-20020a056402144b00b00410b990a68amr2800791edx.25.1645716324506; Thu, 24 Feb 2022 07:25:24 -0800 (PST) Received: from stitch.. (80.71.140.73.ipv4.parknet.dk. [80.71.140.73]) by smtp.gmail.com with ESMTPSA id s11sm1509693edt.10.2022.02.24.07.25.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 07:25:24 -0800 (PST) Sender: Emil Renner Berthing From: Emil Renner Berthing To: linux-riscv@lists.infradead.org Cc: Emil Renner Berthing , Steven Rostedt , Ingo Molnar , Paul Walmsley , Palmer Dabbelt , Albert Ou , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Ard Biesheuvel , Jisheng Zhang , Alexandre Ghiti , linux-kernel@vger.kernel.org Subject: [PATCH v3 8/8] riscv: Use asm/insn.h for dynamic ftrace Date: Thu, 24 Feb 2022 16:24:56 +0100 Message-Id: <20220224152456.493365-9-kernel@esmil.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220224152456.493365-1-kernel@esmil.dk> References: <20220224152456.493365-1-kernel@esmil.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This converts kernel/ftrace.c to use asm/insn.h to generate the instructions for dynamic ftrace. This also converts the make_call macro into a regular static function. Signed-off-by: Emil Renner Berthing --- arch/riscv/kernel/ftrace.c | 69 ++++++++++++-------------------------- 1 file changed, 22 insertions(+), 47 deletions(-) diff --git a/arch/riscv/kernel/ftrace.c b/arch/riscv/kernel/ftrace.c index 2cc15dc45ce0..7dd3aafa17aa 100644 --- a/arch/riscv/kernel/ftrace.c +++ b/arch/riscv/kernel/ftrace.c @@ -9,6 +9,7 @@ #include #include #include +#include #include =20 #ifdef CONFIG_DYNAMIC_FTRACE @@ -21,31 +22,13 @@ * Dynamic ftrace generates probes to call sites, so we must deal with * both auipc and jalr at the same time. */ +static void make_call(unsigned long caller, unsigned long callee, unsigned= int call[2]) +{ + u32 offset =3D callee - caller; =20 -#define JALR_SIGN_MASK (0x00000800) -#define JALR_OFFSET_MASK (0x00000fff) -#define AUIPC_OFFSET_MASK (0xfffff000) -#define AUIPC_PAD (0x00001000) -#define JALR_SHIFT 20 -#define JALR_BASIC (0x000080e7) -#define AUIPC_BASIC (0x00000097) -#define NOP4 (0x00000013) - -#define make_call(caller, callee, call) \ -do { \ - call[0] =3D to_auipc_insn((unsigned int)((unsigned long)callee - \ - (unsigned long)caller)); \ - call[1] =3D to_jalr_insn((unsigned int)((unsigned long)callee - \ - (unsigned long)caller)); \ -} while (0) - -#define to_jalr_insn(offset) \ - (((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_BASIC) - -#define to_auipc_insn(offset) \ - ((offset & JALR_SIGN_MASK) ? \ - (((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) | AUIPC_BASIC) : \ - ((offset & AUIPC_OFFSET_MASK) | AUIPC_BASIC)) + call[0] =3D RISCV_INSN_AUIPC | RISCV_INSN_RD_RA | riscv_insn_u_imm(offset= + 0x800); + call[1] =3D RISCV_INSN_JALR | RISCV_INSN_RD_RA | RISCV_INSN_RS1_RA | ris= cv_insn_i_imm(offset); +} =20 int ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex) { @@ -63,7 +46,7 @@ static int ftrace_check_current_call(unsigned long hook_p= os, unsigned int *expected) { unsigned int replaced[2]; - unsigned int nops[2] =3D {NOP4, NOP4}; + unsigned int nops[2] =3D { RISCV_INSN_NOP, RISCV_INSN_NOP }; =20 /* we expect nops at the hook position */ if (!expected) @@ -95,7 +78,7 @@ static int __ftrace_modify_call(unsigned long hook_pos, u= nsigned long target, bool enable) { unsigned int call[2]; - unsigned int nops[2] =3D {NOP4, NOP4}; + unsigned int nops[2] =3D { RISCV_INSN_NOP, RISCV_INSN_NOP }; =20 make_call(hook_pos, target, call); =20 @@ -108,39 +91,31 @@ static int __ftrace_modify_call(unsigned long hook_pos= , unsigned long target, } =20 /* - * Put 5 instructions with 16 bytes at the front of function within + * Put 4 instructions with 16 bytes at the front of function within * patchable function entry nops' area. * * 0: REG_S ra, -SZREG(sp) - * 1: auipc ra, 0x? - * 2: jalr -?(ra) + * 1: auipc ra, ? + * 2: jalr ra, ra, ? * 3: REG_L ra, -SZREG(sp) - * - * So the opcodes is: - * 0: 0xfe113c23 (sd)/0xfe112e23 (sw) - * 1: 0x???????? -> auipc - * 2: 0x???????? -> jalr - * 3: 0xff813083 (ld)/0xffc12083 (lw) */ -#if __riscv_xlen =3D=3D 64 -#define INSN0 0xfe113c23 -#define INSN3 0xff813083 -#elif __riscv_xlen =3D=3D 32 -#define INSN0 0xfe112e23 -#define INSN3 0xffc12083 -#endif - #define FUNC_ENTRY_SIZE 16 #define FUNC_ENTRY_JMP 4 =20 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) { - unsigned int call[4] =3D {INSN0, 0, 0, INSN3}; + unsigned int call[4] =3D { + RISCV_INSN_REG_S | RISCV_INSN_RS2_RA | RISCV_INSN_RS1_SP | + riscv_insn_s_imm(-RISCV_INSN_SZREG), + 0, + 0, + RISCV_INSN_REG_L | RISCV_INSN_RD_RA | RISCV_INSN_RS1_SP | + riscv_insn_i_imm(-RISCV_INSN_SZREG), + }; unsigned long target =3D addr; unsigned long caller =3D rec->ip + FUNC_ENTRY_JMP; =20 - call[1] =3D to_auipc_insn((unsigned int)(target - caller)); - call[2] =3D to_jalr_insn((unsigned int)(target - caller)); + make_call(caller, target, &call[1]); =20 if (patch_text_nosync((void *)rec->ip, call, FUNC_ENTRY_SIZE)) return -EPERM; @@ -151,7 +126,7 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned l= ong addr) int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr) { - unsigned int nops[4] =3D {NOP4, NOP4, NOP4, NOP4}; + unsigned int nops[4] =3D { RISCV_INSN_NOP, RISCV_INSN_NOP, RISCV_INSN_NOP= , RISCV_INSN_NOP }; =20 if (patch_text_nosync((void *)rec->ip, nops, FUNC_ENTRY_SIZE)) return -EPERM; --=20 2.35.1