patch.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include <asm/cacheflush.h>
  7. #include <asm/fixmap.h>
  8. #include <asm/smp_plat.h>
  9. #include <asm/opcodes.h>
  10. #include <asm/patch.h>
  11. struct patch {
  12. void *addr;
  13. unsigned int insn;
  14. };
  15. static DEFINE_SPINLOCK(patch_lock);
  16. static void __kprobes *patch_map(void *addr, int fixmap, unsigned long *flags)
  17. __acquires(&patch_lock)
  18. {
  19. unsigned int uintaddr = (uintptr_t) addr;
  20. bool module = !core_kernel_text(uintaddr);
  21. struct page *page;
  22. if (module && IS_ENABLED(CONFIG_DEBUG_SET_MODULE_RONX))
  23. page = vmalloc_to_page(addr);
  24. else if (!module && IS_ENABLED(CONFIG_DEBUG_RODATA))
  25. page = virt_to_page(addr);
  26. else
  27. return addr;
  28. if (flags)
  29. spin_lock_irqsave(&patch_lock, *flags);
  30. else
  31. __acquire(&patch_lock);
  32. set_fixmap(fixmap, page_to_phys(page));
  33. return (void *) (__fix_to_virt(fixmap) + (uintaddr & ~PAGE_MASK));
  34. }
  35. static void __kprobes patch_unmap(int fixmap, unsigned long *flags)
  36. __releases(&patch_lock)
  37. {
  38. clear_fixmap(fixmap);
  39. if (flags)
  40. spin_unlock_irqrestore(&patch_lock, *flags);
  41. else
  42. __release(&patch_lock);
  43. }
  44. void __kprobes __patch_text_real(void *addr, unsigned int insn, bool remap)
  45. {
  46. bool thumb2 = IS_ENABLED(CONFIG_THUMB2_KERNEL);
  47. unsigned int uintaddr = (uintptr_t) addr;
  48. bool twopage = false;
  49. unsigned long flags;
  50. void *waddr = addr;
  51. int size;
  52. if (remap)
  53. waddr = patch_map(addr, FIX_TEXT_POKE0, &flags);
  54. else
  55. __acquire(&patch_lock);
  56. if (thumb2 && __opcode_is_thumb16(insn)) {
  57. *(u16 *)waddr = __opcode_to_mem_thumb16(insn);
  58. size = sizeof(u16);
  59. } else if (thumb2 && (uintaddr & 2)) {
  60. u16 first = __opcode_thumb32_first(insn);
  61. u16 second = __opcode_thumb32_second(insn);
  62. u16 *addrh0 = waddr;
  63. u16 *addrh1 = waddr + 2;
  64. twopage = (uintaddr & ~PAGE_MASK) == PAGE_SIZE - 2;
  65. if (twopage && remap)
  66. addrh1 = patch_map(addr + 2, FIX_TEXT_POKE1, NULL);
  67. *addrh0 = __opcode_to_mem_thumb16(first);
  68. *addrh1 = __opcode_to_mem_thumb16(second);
  69. if (twopage && addrh1 != addr + 2) {
  70. flush_kernel_vmap_range(addrh1, 2);
  71. patch_unmap(FIX_TEXT_POKE1, NULL);
  72. }
  73. size = sizeof(u32);
  74. } else {
  75. if (thumb2)
  76. insn = __opcode_to_mem_thumb32(insn);
  77. else
  78. insn = __opcode_to_mem_arm(insn);
  79. *(u32 *)waddr = insn;
  80. size = sizeof(u32);
  81. }
  82. if (waddr != addr) {
  83. flush_kernel_vmap_range(waddr, twopage ? size / 2 : size);
  84. patch_unmap(FIX_TEXT_POKE0, &flags);
  85. } else
  86. __release(&patch_lock);
  87. flush_icache_range((uintptr_t)(addr),
  88. (uintptr_t)(addr) + size);
  89. }
  90. static int __kprobes patch_text_stop_machine(void *data)
  91. {
  92. struct patch *patch = data;
  93. __patch_text(patch->addr, patch->insn);
  94. return 0;
  95. }
  96. void __kprobes patch_text(void *addr, unsigned int insn)
  97. {
  98. struct patch patch = {
  99. .addr = addr,
  100. .insn = insn,
  101. };
  102. stop_machine(patch_text_stop_machine, &patch, NULL);
  103. }