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