Skip to content

Commit 4e87ace

Browse files
Sven Schnellehdeller
authored andcommitted
parisc: add support for patching multiple words
add patch_text_multiple() which allows to patch multiple text words in memory. This can be used to copy functions. Signed-off-by: Sven Schnelle <svens@stackframe.org> Signed-off-by: Helge Deller <deller@gmx.de>
1 parent 79c3ba3 commit 4e87ace

2 files changed

Lines changed: 62 additions & 17 deletions

File tree

‎arch/parisc/include/asm/patch.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
/* stop machine and patch kernel text */
66
void patch_text(void *addr, unsigned int insn);
7+
void patch_text_multiple(void *addr, u32 *insn, unsigned int len);
78

89
/* patch kernel text with machine already stopped (e.g. in kgdb) */
9-
void __patch_text(void *addr, unsigned int insn);
10+
void __patch_text(void *addr, u32 insn);
11+
void __patch_text_multiple(void *addr, u32 *insn, unsigned int len);
1012

1113
#endif

‎arch/parisc/kernel/patch.c‎

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,26 @@
1717

1818
struct patch {
1919
void *addr;
20-
unsigned int insn;
20+
u32 *insn;
21+
unsigned int len;
2122
};
2223

23-
static void __kprobes *patch_map(void *addr, int fixmap)
24-
{
24+
static DEFINE_RAW_SPINLOCK(patch_lock);
25+
26+
static void __kprobes *patch_map(void *addr, int fixmap, int *need_unmap)
2527
unsigned long uintaddr = (uintptr_t) addr;
2628
bool module = !core_kernel_text(uintaddr);
2729
struct page *page;
2830

31+
*need_unmap = 0;
2932
if (module && IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
3033
page = vmalloc_to_page(addr);
3134
else if (!module && IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
3235
page = virt_to_page(addr);
3336
else
3437
return addr;
3538

39+
*need_unmap = 1;
3640
set_fixmap(fixmap, page_to_phys(page));
3741

3842
return (void *) (__fix_to_virt(fixmap) + (uintaddr & ~PAGE_MASK));
@@ -43,34 +47,73 @@ static void __kprobes patch_unmap(int fixmap)
4347
clear_fixmap(fixmap);
4448
}
4549

46-
void __kprobes __patch_text(void *addr, unsigned int insn)
50+
void __kprobes __patch_text_multiple(void *addr, u32 *insn, unsigned int len)
51+
{
52+
unsigned long start = (unsigned long)addr;
53+
unsigned long end = (unsigned long)addr + len;
54+
u32 *p, *fixmap;
55+
int mapped;
56+
57+
/* Make sure we don't have any aliases in cache */
58+
flush_kernel_vmap_range(addr, len);
59+
flush_icache_range(start, end);
60+
61+
p = fixmap = patch_map(addr, FIX_TEXT_POKE0, &mapped);
62+
63+
while (len >= 4) {
64+
*p++ = *insn++;
65+
addr += sizeof(u32);
66+
len -= sizeof(u32);
67+
if (len && offset_in_page(addr) == 0) {
68+
/*
69+
* We're crossing a page boundary, so
70+
* need to remap
71+
*/
72+
flush_kernel_vmap_range((void *)fixmap,
73+
(p-fixmap) * sizeof(*p));
74+
if (mapped)
75+
patch_unmap(FIX_TEXT_POKE0);
76+
p = fixmap = patch_map(addr, FIX_TEXT_POKE0, &mapped);
77+
}
78+
}
79+
80+
flush_kernel_vmap_range((void *)fixmap, (p-fixmap) * sizeof(*p));
81+
if (mapped)
82+
patch_unmap(FIX_TEXT_POKE0);
83+
flush_icache_range(start, end);
84+
}
85+
86+
void __kprobes __patch_text(void *addr, u32 insn)
4787
{
48-
void *waddr = addr;
49-
int size;
50-
51-
waddr = patch_map(addr, FIX_TEXT_POKE0);
52-
*(u32 *)waddr = insn;
53-
size = sizeof(u32);
54-
flush_kernel_vmap_range(waddr, size);
55-
patch_unmap(FIX_TEXT_POKE0);
56-
flush_icache_range((uintptr_t)(addr),
57-
(uintptr_t)(addr) + size);
88+
__patch_text_multiple(addr, &insn, sizeof(insn));
5889
}
5990

6091
static int __kprobes patch_text_stop_machine(void *data)
6192
{
6293
struct patch *patch = data;
6394

64-
__patch_text(patch->addr, patch->insn);
65-
95+
__patch_text_multiple(patch->addr, patch->insn, patch->len);
6696
return 0;
6797
}
6898

6999
void __kprobes patch_text(void *addr, unsigned int insn)
70100
{
101+
struct patch patch = {
102+
.addr = addr,
103+
.insn = &insn,
104+
.len = sizeof(insn),
105+
};
106+
107+
stop_machine_cpuslocked(patch_text_stop_machine, &patch, NULL);
108+
}
109+
110+
void __kprobes patch_text_multiple(void *addr, u32 *insn, unsigned int len)
111+
{
112+
71113
struct patch patch = {
72114
.addr = addr,
73115
.insn = insn,
116+
.len = len
74117
};
75118

76119
stop_machine_cpuslocked(patch_text_stop_machine, &patch, NULL);

0 commit comments

Comments
 (0)