1*8c30b001Schenmiao // SPDX-License-Identifier: GPL-2.0-only
2*8c30b001Schenmiao /*
3*8c30b001Schenmiao * Copyright (C) 2025 Chen Miao
4*8c30b001Schenmiao *
5*8c30b001Schenmiao * Based on arch/arm/kernel/jump_label.c
6*8c30b001Schenmiao */
7*8c30b001Schenmiao #include <linux/jump_label.h>
8*8c30b001Schenmiao #include <linux/kernel.h>
9*8c30b001Schenmiao #include <linux/memory.h>
10*8c30b001Schenmiao #include <asm/bug.h>
11*8c30b001Schenmiao #include <asm/cacheflush.h>
12*8c30b001Schenmiao #include <asm/text-patching.h>
13*8c30b001Schenmiao
arch_jump_label_transform_queue(struct jump_entry * entry,enum jump_label_type type)14*8c30b001Schenmiao bool arch_jump_label_transform_queue(struct jump_entry *entry,
15*8c30b001Schenmiao enum jump_label_type type)
16*8c30b001Schenmiao {
17*8c30b001Schenmiao void *addr = (void *)jump_entry_code(entry);
18*8c30b001Schenmiao u32 insn;
19*8c30b001Schenmiao
20*8c30b001Schenmiao if (type == JUMP_LABEL_JMP) {
21*8c30b001Schenmiao long offset;
22*8c30b001Schenmiao
23*8c30b001Schenmiao offset = jump_entry_target(entry) - jump_entry_code(entry);
24*8c30b001Schenmiao /*
25*8c30b001Schenmiao * The actual maximum range of the l.j instruction's offset is -134,217,728
26*8c30b001Schenmiao * ~ 134,217,724 (sign 26-bit imm).
27*8c30b001Schenmiao * For the original jump range, we need to right-shift N by 2 to obtain the
28*8c30b001Schenmiao * instruction's offset.
29*8c30b001Schenmiao */
30*8c30b001Schenmiao WARN_ON_ONCE(offset < -134217728 || offset > 134217724);
31*8c30b001Schenmiao
32*8c30b001Schenmiao /* 26bit imm mask */
33*8c30b001Schenmiao offset = (offset >> 2) & 0x03ffffff;
34*8c30b001Schenmiao
35*8c30b001Schenmiao insn = offset;
36*8c30b001Schenmiao } else {
37*8c30b001Schenmiao insn = OPENRISC_INSN_NOP;
38*8c30b001Schenmiao }
39*8c30b001Schenmiao
40*8c30b001Schenmiao if (early_boot_irqs_disabled)
41*8c30b001Schenmiao copy_to_kernel_nofault(addr, &insn, sizeof(insn));
42*8c30b001Schenmiao else
43*8c30b001Schenmiao patch_insn_write(addr, insn);
44*8c30b001Schenmiao
45*8c30b001Schenmiao return true;
46*8c30b001Schenmiao }
47*8c30b001Schenmiao
arch_jump_label_transform_apply(void)48*8c30b001Schenmiao void arch_jump_label_transform_apply(void)
49*8c30b001Schenmiao {
50*8c30b001Schenmiao kick_all_cpus_sync();
51*8c30b001Schenmiao }
52