summaryrefslogtreecommitdiffstats
path: root/arch/parisc/kernel/patch.c
blob: 70e9997a3f803b6c0cc9056dd8253167ec15ee49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// SPDX-License-Identifier: GPL-2.0
 /*
  * functions to patch RO kernel text during runtime
  *
  * Copyright (c) 2019 Sven Schnelle <svens@stackframe.org>
  */

#include <linux/kernel.h>
#include <linux/spinlock.h>
#include <linux/kprobes.h>
#include <linux/mm.h>
#include <linux/stop_machine.h>

#include <asm/cacheflush.h>
#include <asm/fixmap.h>
#include <asm/patch.h>

struct patch {
	void *addr;
	u32 *insn;
	unsigned int len;
};

static DEFINE_RAW_SPINLOCK(patch_lock);

static void __kprobes *patch_map(void *addr, int fixmap, int *need_unmap)
	unsigned long uintaddr = (uintptr_t) addr;
	bool module = !core_kernel_text(uintaddr);
	struct page *page;

	*need_unmap = 0;
	if (module && IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
		page = vmalloc_to_page(addr);
	else if (!module && IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
		page = virt_to_page(addr);
	else
		return addr;

	*need_unmap = 1;
	set_fixmap(fixmap, page_to_phys(page));

	return (void *) (__fix_to_virt(fixmap) + (uintaddr & ~PAGE_MASK));
}

static void __kprobes patch_unmap(int fixmap)
{
	clear_fixmap(fixmap);
}

void __kprobes __patch_text_multiple(void *addr, u32 *insn, unsigned int len)
{
	unsigned long start = (unsigned long)addr;
	unsigned long end = (unsigned long)addr + len;
	u32 *p, *fixmap;
	int mapped;

	/* Make sure we don't have any aliases in cache */
	flush_kernel_vmap_range(addr, len);
	flush_icache_range(start, end);

	p = fixmap = patch_map(addr, FIX_TEXT_POKE0, &mapped);

	while (len >= 4) {
		*p++ = *insn++;
		addr += sizeof(u32);
		len -= sizeof(u32);
		if (len && offset_in_page(addr) == 0) {
			/*
			 * We're crossing a page boundary, so
			 * need to remap
			 */
			flush_kernel_vmap_range((void *)fixmap,
						(p-fixmap) * sizeof(*p));
			if (mapped)
				patch_unmap(FIX_TEXT_POKE0);
			p = fixmap = patch_map(addr, FIX_TEXT_POKE0, &mapped);
		}
	}

	flush_kernel_vmap_range((void *)fixmap, (p-fixmap) * sizeof(*p));
	if (mapped)
		patch_unmap(FIX_TEXT_POKE0);
	flush_icache_range(start, end);
}

void __kprobes __patch_text(void *addr, u32 insn)
{
	__patch_text_multiple(addr, &insn, sizeof(insn));
}

static int __kprobes patch_text_stop_machine(void *data)
{
	struct patch *patch = data;

	__patch_text_multiple(patch->addr, patch->insn, patch->len);
	return 0;
}

void __kprobes patch_text(void *addr, unsigned int insn)
{
	struct patch patch = {
		.addr = addr,
		.insn = &insn,
		.len = sizeof(insn),
	};

	stop_machine_cpuslocked(patch_text_stop_machine, &patch, NULL);
}

void __kprobes patch_text_multiple(void *addr, u32 *insn, unsigned int len)
{

	struct patch patch = {
		.addr = addr,
		.insn = insn,
		.len = len
	};

	stop_machine_cpuslocked(patch_text_stop_machine, &patch, NULL);
}