Linux Audio

Check our new training course

Loading...
v4.6
  1#include <linux/kernel.h>
  2#include <linux/spinlock.h>
  3#include <linux/kprobes.h>
  4#include <linux/mm.h>
  5#include <linux/stop_machine.h>
  6
  7#include <asm/cacheflush.h>
  8#include <asm/fixmap.h>
  9#include <asm/smp_plat.h>
 10#include <asm/opcodes.h>
 11#include <asm/patch.h>
 
 12
 13struct patch {
 14	void *addr;
 15	unsigned int insn;
 16};
 17
 18static DEFINE_SPINLOCK(patch_lock);
 19
 20static void __kprobes *patch_map(void *addr, int fixmap, unsigned long *flags)
 21	__acquires(&patch_lock)
 22{
 23	unsigned int uintaddr = (uintptr_t) addr;
 24	bool module = !core_kernel_text(uintaddr);
 25	struct page *page;
 26
 27	if (module && IS_ENABLED(CONFIG_DEBUG_SET_MODULE_RONX))
 28		page = vmalloc_to_page(addr);
 29	else if (!module && IS_ENABLED(CONFIG_DEBUG_RODATA))
 30		page = virt_to_page(addr);
 31	else
 32		return addr;
 33
 34	if (flags)
 35		spin_lock_irqsave(&patch_lock, *flags);
 36	else
 37		__acquire(&patch_lock);
 38
 39	set_fixmap(fixmap, page_to_phys(page));
 40
 41	return (void *) (__fix_to_virt(fixmap) + (uintaddr & ~PAGE_MASK));
 42}
 43
 44static void __kprobes patch_unmap(int fixmap, unsigned long *flags)
 45	__releases(&patch_lock)
 46{
 47	clear_fixmap(fixmap);
 48
 49	if (flags)
 50		spin_unlock_irqrestore(&patch_lock, *flags);
 51	else
 52		__release(&patch_lock);
 53}
 54
 55void __kprobes __patch_text_real(void *addr, unsigned int insn, bool remap)
 56{
 57	bool thumb2 = IS_ENABLED(CONFIG_THUMB2_KERNEL);
 58	unsigned int uintaddr = (uintptr_t) addr;
 59	bool twopage = false;
 60	unsigned long flags;
 61	void *waddr = addr;
 62	int size;
 63
 64	if (remap)
 65		waddr = patch_map(addr, FIX_TEXT_POKE0, &flags);
 66	else
 67		__acquire(&patch_lock);
 68
 69	if (thumb2 && __opcode_is_thumb16(insn)) {
 70		*(u16 *)waddr = __opcode_to_mem_thumb16(insn);
 71		size = sizeof(u16);
 72	} else if (thumb2 && (uintaddr & 2)) {
 73		u16 first = __opcode_thumb32_first(insn);
 74		u16 second = __opcode_thumb32_second(insn);
 75		u16 *addrh0 = waddr;
 76		u16 *addrh1 = waddr + 2;
 77
 78		twopage = (uintaddr & ~PAGE_MASK) == PAGE_SIZE - 2;
 79		if (twopage && remap)
 80			addrh1 = patch_map(addr + 2, FIX_TEXT_POKE1, NULL);
 81
 82		*addrh0 = __opcode_to_mem_thumb16(first);
 83		*addrh1 = __opcode_to_mem_thumb16(second);
 84
 85		if (twopage && addrh1 != addr + 2) {
 86			flush_kernel_vmap_range(addrh1, 2);
 87			patch_unmap(FIX_TEXT_POKE1, NULL);
 88		}
 89
 90		size = sizeof(u32);
 91	} else {
 92		if (thumb2)
 93			insn = __opcode_to_mem_thumb32(insn);
 94		else
 95			insn = __opcode_to_mem_arm(insn);
 96
 97		*(u32 *)waddr = insn;
 98		size = sizeof(u32);
 99	}
100
101	if (waddr != addr) {
102		flush_kernel_vmap_range(waddr, twopage ? size / 2 : size);
103		patch_unmap(FIX_TEXT_POKE0, &flags);
104	} else
105		__release(&patch_lock);
106
107	flush_icache_range((uintptr_t)(addr),
108			   (uintptr_t)(addr) + size);
109}
110
111static int __kprobes patch_text_stop_machine(void *data)
112{
113	struct patch *patch = data;
114
115	__patch_text(patch->addr, patch->insn);
116
117	return 0;
118}
119
120void __kprobes patch_text(void *addr, unsigned int insn)
121{
122	struct patch patch = {
123		.addr = addr,
124		.insn = insn,
125	};
126
127	stop_machine(patch_text_stop_machine, &patch, NULL);
 
 
 
 
 
 
 
 
 
 
 
128}
v3.5.6
 1#include <linux/kernel.h>
 
 2#include <linux/kprobes.h>
 
 3#include <linux/stop_machine.h>
 4
 5#include <asm/cacheflush.h>
 
 6#include <asm/smp_plat.h>
 7#include <asm/opcodes.h>
 8
 9#include "patch.h"
10
11struct patch {
12	void *addr;
13	unsigned int insn;
14};
15
16void __kprobes __patch_text(void *addr, unsigned int insn)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17{
18	bool thumb2 = IS_ENABLED(CONFIG_THUMB2_KERNEL);
 
 
 
 
19	int size;
20
 
 
 
 
 
21	if (thumb2 && __opcode_is_thumb16(insn)) {
22		*(u16 *)addr = __opcode_to_mem_thumb16(insn);
23		size = sizeof(u16);
24	} else if (thumb2 && ((uintptr_t)addr & 2)) {
25		u16 first = __opcode_thumb32_first(insn);
26		u16 second = __opcode_thumb32_second(insn);
27		u16 *addrh = addr;
 
28
29		addrh[0] = __opcode_to_mem_thumb16(first);
30		addrh[1] = __opcode_to_mem_thumb16(second);
 
 
 
 
 
 
 
 
 
31
32		size = sizeof(u32);
33	} else {
34		if (thumb2)
35			insn = __opcode_to_mem_thumb32(insn);
36		else
37			insn = __opcode_to_mem_arm(insn);
38
39		*(u32 *)addr = insn;
40		size = sizeof(u32);
41	}
42
 
 
 
 
 
 
43	flush_icache_range((uintptr_t)(addr),
44			   (uintptr_t)(addr) + size);
45}
46
47static int __kprobes patch_text_stop_machine(void *data)
48{
49	struct patch *patch = data;
50
51	__patch_text(patch->addr, patch->insn);
52
53	return 0;
54}
55
56void __kprobes patch_text(void *addr, unsigned int insn)
57{
58	struct patch patch = {
59		.addr = addr,
60		.insn = insn,
61	};
62
63	if (cache_ops_need_broadcast()) {
64		stop_machine(patch_text_stop_machine, &patch, cpu_online_mask);
65	} else {
66		bool straddles_word = IS_ENABLED(CONFIG_THUMB2_KERNEL)
67				      && __opcode_is_thumb32(insn)
68				      && ((uintptr_t)addr & 2);
69
70		if (straddles_word)
71			stop_machine(patch_text_stop_machine, &patch, NULL);
72		else
73			__patch_text(addr, insn);
74	}
75}