jump_label.c 2.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
J
Jan Glauber 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Jump label s390 support
 *
 * Copyright IBM Corp. 2011
 * Author(s): Jan Glauber <jang@linux.vnet.ibm.com>
 */
#include <linux/uaccess.h>
#include <linux/stop_machine.h>
#include <linux/jump_label.h>
#include <asm/ipl.h>

struct insn {
	u16 opcode;
	s32 offset;
} __packed;

struct insn_args {
19 20
	struct jump_entry *entry;
	enum jump_label_type type;
J
Jan Glauber 已提交
21 22
};

23 24 25 26 27 28 29 30 31 32 33
static void jump_label_make_nop(struct jump_entry *entry, struct insn *insn)
{
	/* brcl 0,0 */
	insn->opcode = 0xc004;
	insn->offset = 0;
}

static void jump_label_make_branch(struct jump_entry *entry, struct insn *insn)
{
	/* brcl 15,offset */
	insn->opcode = 0xc0f4;
34
	insn->offset = (jump_entry_target(entry) - jump_entry_code(entry)) >> 1;
35 36
}

37 38
static void jump_label_bug(struct jump_entry *entry, struct insn *expected,
			   struct insn *new)
39
{
40
	unsigned char *ipc = (unsigned char *)jump_entry_code(entry);
41 42
	unsigned char *ipe = (unsigned char *)expected;
	unsigned char *ipn = (unsigned char *)new;
43 44

	pr_emerg("Jump label code mismatch at %pS [%p]\n", ipc, ipc);
45 46 47
	pr_emerg("Found:    %6ph\n", ipc);
	pr_emerg("Expected: %6ph\n", ipe);
	pr_emerg("New:      %6ph\n", ipn);
48 49 50
	panic("Corrupted kernel text");
}

51 52 53 54 55
static struct insn orignop = {
	.opcode = 0xc004,
	.offset = JUMP_LABEL_NOP_OFFSET >> 1,
};

56
static void __jump_label_transform(struct jump_entry *entry,
57 58
				   enum jump_label_type type,
				   int init)
J
Jan Glauber 已提交
59
{
60
	void *code = (void *)jump_entry_code(entry);
61
	struct insn old, new;
J
Jan Glauber 已提交
62

63
	if (type == JUMP_LABEL_JMP) {
64 65
		jump_label_make_nop(entry, &old);
		jump_label_make_branch(entry, &new);
J
Jan Glauber 已提交
66
	} else {
67
		jump_label_make_branch(entry, &old);
68
		jump_label_make_nop(entry, &new);
J
Jan Glauber 已提交
69
	}
70
	if (init) {
71
		if (memcmp(code, &orignop, sizeof(orignop)))
72
			jump_label_bug(entry, &orignop, &new);
73
	} else {
74
		if (memcmp(code, &old, sizeof(old)))
75
			jump_label_bug(entry, &old, &new);
76
	}
77
	s390_kernel_write(code, &new, sizeof(new));
78
}
J
Jan Glauber 已提交
79

80 81 82 83
static int __sm_arch_jump_label_transform(void *data)
{
	struct insn_args *args = data;

84
	__jump_label_transform(args->entry, args->type, 0);
85 86 87 88 89 90 91 92 93 94 95
	return 0;
}

void arch_jump_label_transform(struct jump_entry *entry,
			       enum jump_label_type type)
{
	struct insn_args args;

	args.entry = entry;
	args.type = type;

96
	stop_machine_cpuslocked(__sm_arch_jump_label_transform, &args, NULL);
97 98 99 100 101
}

void arch_jump_label_transform_static(struct jump_entry *entry,
				      enum jump_label_type type)
{
102
	__jump_label_transform(entry, type, 1);
J
Jan Glauber 已提交
103
}