^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Jump label s390 support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright IBM Corp. 2011
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author(s): Jan Glauber <jang@linux.vnet.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/stop_machine.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/jump_label.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/ipl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct insn {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) u16 opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) s32 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static void jump_label_make_nop(struct jump_entry *entry, struct insn *insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /* brcl 0,offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) insn->opcode = 0xc004;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) insn->offset = (jump_entry_target(entry) - jump_entry_code(entry)) >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static void jump_label_make_branch(struct jump_entry *entry, struct insn *insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /* brcl 15,offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) insn->opcode = 0xc0f4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) insn->offset = (jump_entry_target(entry) - jump_entry_code(entry)) >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static void jump_label_bug(struct jump_entry *entry, struct insn *expected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct insn *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) unsigned char *ipc = (unsigned char *)jump_entry_code(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) unsigned char *ipe = (unsigned char *)expected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned char *ipn = (unsigned char *)new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) pr_emerg("Jump label code mismatch at %pS [%px]\n", ipc, ipc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) pr_emerg("Found: %6ph\n", ipc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) pr_emerg("Expected: %6ph\n", ipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) pr_emerg("New: %6ph\n", ipn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) panic("Corrupted kernel text");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static struct insn orignop = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) .opcode = 0xc004,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) .offset = JUMP_LABEL_NOP_OFFSET >> 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static void __jump_label_transform(struct jump_entry *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) enum jump_label_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) void *code = (void *)jump_entry_code(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct insn old, new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (type == JUMP_LABEL_JMP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) jump_label_make_nop(entry, &old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) jump_label_make_branch(entry, &new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) jump_label_make_branch(entry, &old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) jump_label_make_nop(entry, &new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (init) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (memcmp(code, &orignop, sizeof(orignop)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) jump_label_bug(entry, &orignop, &new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (memcmp(code, &old, sizeof(old)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) jump_label_bug(entry, &old, &new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) s390_kernel_write(code, &new, sizeof(new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static void __jump_label_sync(void *dummy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) void arch_jump_label_transform(struct jump_entry *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) enum jump_label_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) __jump_label_transform(entry, type, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) smp_call_function(__jump_label_sync, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) void arch_jump_label_transform_static(struct jump_entry *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) enum jump_label_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) __jump_label_transform(entry, type, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }