From patchwork Tue Oct 31 22:25:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 13442291 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0FF50C4167D for ; Tue, 31 Oct 2023 22:25:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8439F6B0337; Tue, 31 Oct 2023 18:25:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7A5256B0338; Tue, 31 Oct 2023 18:25:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 645CC6B0339; Tue, 31 Oct 2023 18:25:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 4A1F06B0337 for ; Tue, 31 Oct 2023 18:25:25 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 1EB4CA04A5 for ; Tue, 31 Oct 2023 22:25:25 +0000 (UTC) X-FDA: 81407189010.27.4AFC336 Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) by imf06.hostedemail.com (Postfix) with ESMTP id 3276818001C for ; Tue, 31 Oct 2023 22:25:22 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=nGk3kWN4; spf=pass (imf06.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.167.176 as permitted sender) smtp.mailfrom=charlie@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698791123; a=rsa-sha256; cv=none; b=kBFMIIf2+0ZYdU2vOReXGpkOZKO75LX2sAIaFjYI3pxt8rjsji22zPTMTlEsb/f3VSom2m iKQcSKxr/Jiw8804S9l51KZX2ZjQ23e3k665vMpOshjuUInVjlczoZ5KiDTi0twnvIJZ9Y 206RBlEjR2dEyjUyhNV74BHNlrK4Evw= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=nGk3kWN4; spf=pass (imf06.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.167.176 as permitted sender) smtp.mailfrom=charlie@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698791123; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=URk3OdUManeok/shpPwBhUNFZ0p313A2y5YhZgjx0S8=; b=Rw8Ok1ePcx0X+nXaUOysL1wkQlC9XwYSDlnN5As0y2pkvNVeFRTt/Bx9cJ7qDaZoD0epa5 /+5FexQz+mte39J1nyiOqOOZBCe8a1gGrVJFF/RiLmuMd18nDlSFKkMw4UokVurHxvamxj no0mA3tNX/AgORqdVa+7h0S25eIm2jo= Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-3b2e22f1937so3345931b6e.1 for ; Tue, 31 Oct 2023 15:25:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1698791122; x=1699395922; darn=kvack.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=URk3OdUManeok/shpPwBhUNFZ0p313A2y5YhZgjx0S8=; b=nGk3kWN423xXT8b+VQA9tN6P85B+ucT+CsKMv1UrbkKiT7jfMrJ3YOXl9PmFH1hWbr WE4NRpWlfa3s4BJ2buPq+dRPSsmxO/dlcjn1JOGwK3dgIm0Rnemx0yYpV4cnl3pkeupP SfR6ut3SlTjvuHb9OM7R9yfe8+XtXK6E1vE62Z88CcLE7R8oz86yrD0zsY6N1UNkTl5Q 8csbtGSkIpatfdWopXG/tsRhCPSSLLPcLPgztVD9uDsYgprj98pGMH/MvCJDFy1R7Bu9 5fuCuXWsNGuz9OZXJZAh8pa8VOF64q/PdEOW31TlHv+MhOPV829MlgX4ukxDDCOgMc09 sBGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698791122; x=1699395922; 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=URk3OdUManeok/shpPwBhUNFZ0p313A2y5YhZgjx0S8=; b=V/Nl9iySq0qXlRWXYnyC5dCRlv6vGT3gRmbWcVKm0C7Z1EWGPT+UFShKCqeNLvz9yo 33271OFLpQd94XPEfI+2DOJF9UjXXVmIx0+ZzWkRpK81syKrj9/0TvZu5kyU/CGAtQZh rWD/C6UIhusjVjoLLru7/gSFWn6Wm5cQ3j0Ue/pl0keDin8eLmqEJ3QYMeYoyCHMGbNS NlACqke5kKLGXzDleZJQWmoNKGdvmpVKmwCHYby2oCvzOjx5nhAIAXX/ZM9dcb8/BNNg hiR7b+zauREiWSVhQ38GTFHoWAIIWjj57GDkCtaTjKcMniuKZ0BCXLyNUDgDMtNe654V lZVw== X-Gm-Message-State: AOJu0Yzw9ub9bJMHUE6uGXI+GaEbnGZ5cl/QF2AjJAOjHTk2mMLA71Af kxCJKKjprt5w7f5SVRVI1tPA6A== X-Google-Smtp-Source: AGHT+IE9y65cA1PqdryYvGzQY7EawoaS7joMLz6aXjbn10i07aam6qkwyfJcf5SQr2zNNJejA76gjA== X-Received: by 2002:a05:6808:1920:b0:3b5:64c9:5146 with SMTP id bf32-20020a056808192000b003b564c95146mr3593901oib.42.1698791122035; Tue, 31 Oct 2023 15:25:22 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id k16-20020a544410000000b003b2e7231faasm42975oiw.28.2023.10.31.15.25.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 15:25:21 -0700 (PDT) From: Charlie Jenkins Date: Tue, 31 Oct 2023 15:25:13 -0700 Subject: [PATCH v8 1/3] riscv: Avoid unaligned access when relocating modules MIME-Version: 1.0 Message-Id: <20231031-module_relocations-v8-1-09b5b720c1fe@rivosinc.com> References: <20231031-module_relocations-v8-0-09b5b720c1fe@rivosinc.com> In-Reply-To: <20231031-module_relocations-v8-0-09b5b720c1fe@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 , Andreas Schwab , Emil Renner Berthing , Samuel Holland , Nelson Chu , Charlie Jenkins , Emil Renner Berthing X-Mailer: b4 0.12.3 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 3276818001C X-Stat-Signature: cis8hfczbm4pqtqm4s5jqnimpij7kaei X-Rspam-User: X-HE-Tag: 1698791122-593121 X-HE-Meta: U2FsdGVkX18BZDb6HMCDXUckRaAo7LCERPA0OPLIgvIvHhtBdsrNIgDHllCv465gc4t93VX8fivfiJfEWLqU8fRxbBMFtHhPEc5elhjXzLMrHtmjsoUt0HV8ndXPiRDBVsCsLIlGKf/J4FUe7BcuXxduSRu79mKZNW4OXTcXI1icprlPgt6Em0jegN0si0nlpt09xrAGjOSvzIhcec68l8G73H34IGT0Lu5T9MB0D/Ri5kyja6J2Y29DEIqnYfKD7QQb26ZiZqT4TvxdHVQPR7TXvr84EvvjvyyGLBNF4qnH7LDkbFar3CzSZ0kJxhs9NBv5w7VJSB+4gULAKcawVFfnfNzeEvl0BlXsNfIi/OyzZR+sHyNac/vk0CDYXFNgKi1f1dnZ+4Ch0jWjmaK3AxbzH3qRE5+UZxQ2YzzktalWM7eRxdD1Asy/YqZ7cByUNhPieHK2voZnYlK2z5pjtqVChSJ1wE4qBXEbNPeHMD/4caagh9RwD5kFYL8++59bIyCF3ytZS+KAUHkIkYh4qCQFgJC4+G8P+N9bE7XxcTGX0chSjj3JtlykVgH7dZl41YvjrwOSD9Il0mWt3uDuxfuW3tPdBMrkQNpOXtzB4a4Ht4zsro+Hm+EpNEbaNYVH29fyzQQ5agnvsEnstd3ziF7IOdr0TWCjPSZIqT3zinzuYZzQiGACgSiwFIA1mGy3rl/uJdKm6UApq74YGOKrDWy6uVh/W/V92/2XbtKB+J8p/03+2ExwklMWK8W0zm8aW4omKlEvPAc1FdFPNjMoaeNA8TsoVW+JNbhRXFyGMWKpo127oDaHigQrbIcAurT5SK9pSiC3WJqCMTF4ni5/QP7S37aAmacqJroNEYvP/URtnIe7q9gX8ePQbtdx8pdnpM5ieifItv3R+lKo9auM32wj/PBcRkUJjfwbVvaDbMtMeeQwnx6fHMSx0ojMCgTsLFKtdHZUBX8q2xRJoA+ mz7d2bwF EZIQoXN9BrkTmRzMmzOi8s66Fb2SefGwyUh4xsdhSOMfmh2XexWKfdyMNTRo/fFHTvItANnlfxI3+UGWLe0DG0Me4xkOtFpWSpVktCOvUHOjqf9U19YQze5mY4VwYaYyvqNFN1jMXqQPFAW79je5vA9AcbcwsJEdq5GMtBuaNNI0B8AjVyRWwIjAMNwmS+IymJRfwTXz8XYwzSZSFvfexnfzjiwh2kbRiehm2NymsBweY+PM34vIcQ/y7m2Teg04yDeTAJt6n+SaLzNx3OH1wIZUsCQeRW2TLonGPTTpmXTcdqxvy73ohiEj2nlk9bPXUBl9UwInQywN4YjvZdfRlh78EDCtkKXNgvJnX+Wn0AjnCXBzRxezSlz0KEvCUvZM+FsIY37uZzcyeX2VwaQY+UmTTNQYTYds9tAKEF5OvxbG74mcq4boPeINQQ40kIIOHTtFdtIZWzw5N0xpk/Xizsig16g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000401, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Emil Renner Berthing 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 little-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. Signed-off-by: Emil Renner Berthing Signed-off-by: Charlie Jenkins --- arch/riscv/kernel/module.c | 153 +++++++++++++++++++++++---------------------- 1 file changed, 77 insertions(+), 76 deletions(-) diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index 7c651d55fcbd..a9e94e939cb5 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -27,68 +27,86 @@ static bool riscv_insn_valid_32bit_offset(ptrdiff_t val) #endif } -static int apply_r_riscv_32_rela(struct module *me, u32 *location, Elf_Addr v) +static int riscv_insn_rmw(void *location, u32 keep, u32 set) +{ + u16 *parcel = location; + u32 insn = (u32)le16_to_cpu(parcel[0]) | (u32)le16_to_cpu(parcel[1]) << 16; + + insn &= keep; + insn |= set; + + parcel[0] = cpu_to_le32(insn); + parcel[1] = cpu_to_le16(insn >> 16); + return 0; +} + +static int riscv_insn_rvc_rmw(void *location, u16 keep, u16 set) +{ + u16 *parcel = location; + + *parcel = cpu_to_le16((le16_to_cpu(*parcel) & keep) | set); + return 0; +} + +static int apply_r_riscv_32_rela(struct module *me, void *location, Elf_Addr v) { if (v != (u32)v) { pr_err("%s: value %016llx out of range for 32-bit field\n", me->name, (long long)v); return -EINVAL; } - *location = v; + *(u32 *)location = v; return 0; } -static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v) +static int apply_r_riscv_64_rela(struct module *me, void *location, Elf_Addr v) { *(u64 *)location = v; return 0; } -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 = (void *)v - (void *)location; + ptrdiff_t offset = (void *)v - location; u32 imm12 = (offset & 0x1000) << (31 - 12); u32 imm11 = (offset & 0x800) >> (11 - 7); u32 imm10_5 = (offset & 0x7e0) << (30 - 10); u32 imm4_1 = (offset & 0x1e) << (11 - 4); - *location = (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1; - return 0; + return riscv_insn_rmw(location, 0x1fff07f, imm12 | imm11 | imm10_5 | imm4_1); } -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 = (void *)v - (void *)location; + ptrdiff_t offset = (void *)v - location; u32 imm20 = (offset & 0x100000) << (31 - 20); u32 imm19_12 = (offset & 0xff000); u32 imm11 = (offset & 0x800) << (20 - 11); u32 imm10_1 = (offset & 0x7fe) << (30 - 10); - *location = (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1; - return 0; + return riscv_insn_rmw(location, 0xfff, imm20 | imm19_12 | imm11 | imm10_1); } -static int apply_r_riscv_rvc_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 = (void *)v - (void *)location; + ptrdiff_t offset = (void *)v - location; u16 imm8 = (offset & 0x100) << (12 - 8); u16 imm7_6 = (offset & 0xc0) >> (6 - 5); u16 imm5 = (offset & 0x20) >> (5 - 2); u16 imm4_3 = (offset & 0x18) << (12 - 5); u16 imm2_1 = (offset & 0x6) << (12 - 10); - *(u16 *)location = (*(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); } -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 = (void *)v - (void *)location; + ptrdiff_t offset = (void *)v - location; u16 imm11 = (offset & 0x800) << (12 - 11); u16 imm10 = (offset & 0x400) >> (10 - 8); u16 imm9_8 = (offset & 0x300) << (12 - 11); @@ -98,16 +116,14 @@ static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location, u16 imm4 = (offset & 0x10) << (12 - 5); u16 imm3_1 = (offset & 0xe) << (12 - 10); - *(u16 *)location = (*(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); } -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 = (void *)v - (void *)location; - s32 hi20; + ptrdiff_t offset = (void *)v - location; if (!riscv_insn_valid_32bit_offset(offset)) { pr_err( @@ -116,23 +132,20 @@ static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location, return -EINVAL; } - hi20 = (offset + 0x800) & 0xfffff000; - *location = (*location & 0xfff) | hi20; - return 0; + return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000); } -static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *location, +static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, void *location, Elf_Addr v) { /* * v is the lo12 value to fill. It is calculated before calling this * handler. */ - *location = (*location & 0xfffff) | ((v & 0xfff) << 20); - return 0; + return riscv_insn_rmw(location, 0xfffff, (v & 0xfff) << 20); } -static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location, +static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, void *location, Elf_Addr v) { /* @@ -142,15 +155,12 @@ static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location, u32 imm11_5 = (v & 0xfe0) << (31 - 11); u32 imm4_0 = (v & 0x1f) << (11 - 4); - *location = (*location & 0x1fff07f) | imm11_5 | imm4_0; - return 0; + return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0); } -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 = %p\n", @@ -158,22 +168,20 @@ static int apply_r_riscv_hi20_rela(struct module *me, u32 *location, return -EINVAL; } - hi20 = ((s32)v + 0x800) & 0xfffff000; - *location = (*location & 0xfff) | hi20; - return 0; + return riscv_insn_rmw(location, 0xfff, ((s32)v + 0x800) & 0xfffff000); } -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 = ((s32)v + 0x800) & 0xfffff000; s32 lo12 = ((s32)v - hi20); - *location = (*location & 0xfffff) | ((lo12 & 0xfff) << 20); - return 0; + + return riscv_insn_rmw(location, 0xfffff, (lo12 & 0xfff) << 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 */ @@ -181,20 +189,18 @@ static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location, s32 lo12 = ((s32)v - hi20); u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11); u32 imm4_0 = (lo12 & 0x1f) << (11 - 4); - *location = (*location & 0x1fff07f) | imm11_5 | imm4_0; - return 0; + + return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0); } -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 = (void *)v - (void *)location; - s32 hi20; + ptrdiff_t offset = (void *)v - location; /* Always emit the got entry */ if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) { - offset = module_emit_got_entry(me, v); - offset = (void *)offset - (void *)location; + offset = (void *)module_emit_got_entry(me, v) - location; } else { pr_err( "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n", @@ -202,22 +208,19 @@ static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location, return -EINVAL; } - hi20 = (offset + 0x800) & 0xfffff000; - *location = (*location & 0xfff) | hi20; - return 0; + return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000); } -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 = (void *)v - (void *)location; + ptrdiff_t offset = (void *)v - location; u32 hi20, lo12; 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 = module_emit_plt_entry(me, v); - offset = (void *)offset - (void *)location; + offset = (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 = %p\n", @@ -228,15 +231,14 @@ static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location, hi20 = (offset + 0x800) & 0xfffff000; lo12 = (offset - hi20) & 0xfff; - *location = (*location & 0xfff) | hi20; - *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20); - return 0; + riscv_insn_rmw(location, 0xfff, hi20); + return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 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 = (void *)v - (void *)location; + ptrdiff_t offset = (void *)v - location; u32 hi20, lo12; if (!riscv_insn_valid_32bit_offset(offset)) { @@ -248,18 +250,17 @@ static int apply_r_riscv_call_rela(struct module *me, u32 *location, hi20 = (offset + 0x800) & 0xfffff000; lo12 = (offset - hi20) & 0xfff; - *location = (*location & 0xfff) | hi20; - *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20); - return 0; + riscv_insn_rmw(location, 0xfff, hi20); + return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 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; } -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( @@ -268,49 +269,49 @@ static int apply_r_riscv_align_rela(struct module *me, u32 *location, return -EINVAL; } -static int apply_r_riscv_add16_rela(struct module *me, u32 *location, +static int apply_r_riscv_add16_rela(struct module *me, void *location, Elf_Addr v) { *(u16 *)location += (u16)v; return 0; } -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 += (u32)v; return 0; } -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 += (u64)v; return 0; } -static int apply_r_riscv_sub16_rela(struct module *me, u32 *location, +static int apply_r_riscv_sub16_rela(struct module *me, void *location, Elf_Addr v) { *(u16 *)location -= (u16)v; return 0; } -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 -= (u32)v; return 0; } -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 -= (u64)v; return 0; } -static int (*reloc_handlers_rela[]) (struct module *me, u32 *location, +static int (*reloc_handlers_rela[]) (struct module *me, void *location, Elf_Addr v) = { [R_RISCV_32] = apply_r_riscv_32_rela, [R_RISCV_64] = apply_r_riscv_64_rela, @@ -342,9 +343,9 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, struct module *me) { Elf_Rela *rel = (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;