^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2020 Emil Renner Berthing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Based on arch/arm64/kernel/jump_label.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/jump_label.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/patch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define RISCV_INSN_NOP 0x00000013U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define RISCV_INSN_JAL 0x0000006fU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) void arch_jump_label_transform(struct jump_entry *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) enum jump_label_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) void *addr = (void *)jump_entry_code(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) u32 insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (type == JUMP_LABEL_JMP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) long offset = jump_entry_target(entry) - jump_entry_code(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (WARN_ON(offset & 1 || offset < -524288 || offset >= 524288))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) insn = RISCV_INSN_JAL |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) (((u32)offset & GENMASK(19, 12)) << (12 - 12)) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) (((u32)offset & GENMASK(11, 11)) << (20 - 11)) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) (((u32)offset & GENMASK(10, 1)) << (21 - 1)) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) (((u32)offset & GENMASK(20, 20)) << (31 - 20));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) insn = RISCV_INSN_NOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) mutex_lock(&text_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) patch_text_nosync(addr, &insn, sizeof(insn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) mutex_unlock(&text_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) void arch_jump_label_transform_static(struct jump_entry *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) enum jump_label_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * We use the same instructions in the arch_static_branch and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * arch_static_branch_jump inline functions, so there's no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * need to patch them up here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * The core will call arch_jump_label_transform when those
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * instructions need to be replaced.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }