optprobes.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Code for Kernel probes Jump optimization.
  3. *
  4. * Copyright 2017, Anju T, IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kprobes.h>
  12. #include <linux/jump_label.h>
  13. #include <linux/types.h>
  14. #include <linux/slab.h>
  15. #include <linux/list.h>
  16. #include <asm/kprobes.h>
  17. #include <asm/ptrace.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/code-patching.h>
  20. #include <asm/sstep.h>
  21. #include <asm/ppc-opcode.h>
  22. #define TMPL_CALL_HDLR_IDX \
  23. (optprobe_template_call_handler - optprobe_template_entry)
  24. #define TMPL_EMULATE_IDX \
  25. (optprobe_template_call_emulate - optprobe_template_entry)
  26. #define TMPL_RET_IDX \
  27. (optprobe_template_ret - optprobe_template_entry)
  28. #define TMPL_OP_IDX \
  29. (optprobe_template_op_address - optprobe_template_entry)
  30. #define TMPL_INSN_IDX \
  31. (optprobe_template_insn - optprobe_template_entry)
  32. #define TMPL_END_IDX \
  33. (optprobe_template_end - optprobe_template_entry)
  34. DEFINE_INSN_CACHE_OPS(ppc_optinsn);
  35. static bool insn_page_in_use;
  36. static void *__ppc_alloc_insn_page(void)
  37. {
  38. if (insn_page_in_use)
  39. return NULL;
  40. insn_page_in_use = true;
  41. return &optinsn_slot;
  42. }
  43. static void __ppc_free_insn_page(void *page __maybe_unused)
  44. {
  45. insn_page_in_use = false;
  46. }
  47. struct kprobe_insn_cache kprobe_ppc_optinsn_slots = {
  48. .mutex = __MUTEX_INITIALIZER(kprobe_ppc_optinsn_slots.mutex),
  49. .pages = LIST_HEAD_INIT(kprobe_ppc_optinsn_slots.pages),
  50. /* insn_size initialized later */
  51. .alloc = __ppc_alloc_insn_page,
  52. .free = __ppc_free_insn_page,
  53. .nr_garbage = 0,
  54. };
  55. /*
  56. * Check if we can optimize this probe. Returns NIP post-emulation if this can
  57. * be optimized and 0 otherwise.
  58. */
  59. static unsigned long can_optimize(struct kprobe *p)
  60. {
  61. struct pt_regs regs;
  62. struct instruction_op op;
  63. unsigned long nip = 0;
  64. /*
  65. * kprobe placed for kretprobe during boot time
  66. * has a 'nop' instruction, which can be emulated.
  67. * So further checks can be skipped.
  68. */
  69. if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
  70. return (unsigned long)p->addr + sizeof(kprobe_opcode_t);
  71. /*
  72. * We only support optimizing kernel addresses, but not
  73. * module addresses.
  74. *
  75. * FIXME: Optimize kprobes placed in module addresses.
  76. */
  77. if (!is_kernel_addr((unsigned long)p->addr))
  78. return 0;
  79. memset(&regs, 0, sizeof(struct pt_regs));
  80. regs.nip = (unsigned long)p->addr;
  81. regs.trap = 0x0;
  82. regs.msr = MSR_KERNEL;
  83. /*
  84. * Kprobe placed in conditional branch instructions are
  85. * not optimized, as we can't predict the nip prior with
  86. * dummy pt_regs and can not ensure that the return branch
  87. * from detour buffer falls in the range of address (i.e 32MB).
  88. * A branch back from trampoline is set up in the detour buffer
  89. * to the nip returned by the analyse_instr() here.
  90. *
  91. * Ensure that the instruction is not a conditional branch,
  92. * and that can be emulated.
  93. */
  94. if (!is_conditional_branch(*p->ainsn.insn) &&
  95. analyse_instr(&op, &regs, *p->ainsn.insn) == 1) {
  96. emulate_update_regs(&regs, &op);
  97. nip = regs.nip;
  98. }
  99. return nip;
  100. }
  101. static void optimized_callback(struct optimized_kprobe *op,
  102. struct pt_regs *regs)
  103. {
  104. /* This is possible if op is under delayed unoptimizing */
  105. if (kprobe_disabled(&op->kp))
  106. return;
  107. preempt_disable();
  108. if (kprobe_running()) {
  109. kprobes_inc_nmissed_count(&op->kp);
  110. } else {
  111. __this_cpu_write(current_kprobe, &op->kp);
  112. regs->nip = (unsigned long)op->kp.addr;
  113. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  114. opt_pre_handler(&op->kp, regs);
  115. __this_cpu_write(current_kprobe, NULL);
  116. }
  117. preempt_enable_no_resched();
  118. }
  119. NOKPROBE_SYMBOL(optimized_callback);
  120. void arch_remove_optimized_kprobe(struct optimized_kprobe *op)
  121. {
  122. if (op->optinsn.insn) {
  123. free_ppc_optinsn_slot(op->optinsn.insn, 1);
  124. op->optinsn.insn = NULL;
  125. }
  126. }
  127. /*
  128. * emulate_step() requires insn to be emulated as
  129. * second parameter. Load register 'r4' with the
  130. * instruction.
  131. */
  132. void patch_imm32_load_insns(unsigned int val, kprobe_opcode_t *addr)
  133. {
  134. /* addis r4,0,(insn)@h */
  135. patch_instruction(addr, PPC_INST_ADDIS | ___PPC_RT(4) |
  136. ((val >> 16) & 0xffff));
  137. addr++;
  138. /* ori r4,r4,(insn)@l */
  139. patch_instruction(addr, PPC_INST_ORI | ___PPC_RA(4) |
  140. ___PPC_RS(4) | (val & 0xffff));
  141. }
  142. /*
  143. * Generate instructions to load provided immediate 64-bit value
  144. * to register 'r3' and patch these instructions at 'addr'.
  145. */
  146. void patch_imm64_load_insns(unsigned long val, kprobe_opcode_t *addr)
  147. {
  148. /* lis r3,(op)@highest */
  149. patch_instruction(addr, PPC_INST_ADDIS | ___PPC_RT(3) |
  150. ((val >> 48) & 0xffff));
  151. addr++;
  152. /* ori r3,r3,(op)@higher */
  153. patch_instruction(addr, PPC_INST_ORI | ___PPC_RA(3) |
  154. ___PPC_RS(3) | ((val >> 32) & 0xffff));
  155. addr++;
  156. /* rldicr r3,r3,32,31 */
  157. patch_instruction(addr, PPC_INST_RLDICR | ___PPC_RA(3) |
  158. ___PPC_RS(3) | __PPC_SH64(32) | __PPC_ME64(31));
  159. addr++;
  160. /* oris r3,r3,(op)@h */
  161. patch_instruction(addr, PPC_INST_ORIS | ___PPC_RA(3) |
  162. ___PPC_RS(3) | ((val >> 16) & 0xffff));
  163. addr++;
  164. /* ori r3,r3,(op)@l */
  165. patch_instruction(addr, PPC_INST_ORI | ___PPC_RA(3) |
  166. ___PPC_RS(3) | (val & 0xffff));
  167. }
  168. int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
  169. {
  170. kprobe_opcode_t *buff, branch_op_callback, branch_emulate_step;
  171. kprobe_opcode_t *op_callback_addr, *emulate_step_addr;
  172. long b_offset;
  173. unsigned long nip, size;
  174. int rc, i;
  175. kprobe_ppc_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
  176. nip = can_optimize(p);
  177. if (!nip)
  178. return -EILSEQ;
  179. /* Allocate instruction slot for detour buffer */
  180. buff = get_ppc_optinsn_slot();
  181. if (!buff)
  182. return -ENOMEM;
  183. /*
  184. * OPTPROBE uses 'b' instruction to branch to optinsn.insn.
  185. *
  186. * The target address has to be relatively nearby, to permit use
  187. * of branch instruction in powerpc, because the address is specified
  188. * in an immediate field in the instruction opcode itself, ie 24 bits
  189. * in the opcode specify the address. Therefore the address should
  190. * be within 32MB on either side of the current instruction.
  191. */
  192. b_offset = (unsigned long)buff - (unsigned long)p->addr;
  193. if (!is_offset_in_branch_range(b_offset))
  194. goto error;
  195. /* Check if the return address is also within 32MB range */
  196. b_offset = (unsigned long)(buff + TMPL_RET_IDX) -
  197. (unsigned long)nip;
  198. if (!is_offset_in_branch_range(b_offset))
  199. goto error;
  200. /* Setup template */
  201. /* We can optimize this via patch_instruction_window later */
  202. size = (TMPL_END_IDX * sizeof(kprobe_opcode_t)) / sizeof(int);
  203. pr_devel("Copying template to %p, size %lu\n", buff, size);
  204. for (i = 0; i < size; i++) {
  205. rc = patch_instruction(buff + i, *(optprobe_template_entry + i));
  206. if (rc < 0)
  207. goto error;
  208. }
  209. /*
  210. * Fixup the template with instructions to:
  211. * 1. load the address of the actual probepoint
  212. */
  213. patch_imm64_load_insns((unsigned long)op, buff + TMPL_OP_IDX);
  214. /*
  215. * 2. branch to optimized_callback() and emulate_step()
  216. */
  217. op_callback_addr = (kprobe_opcode_t *)ppc_kallsyms_lookup_name("optimized_callback");
  218. emulate_step_addr = (kprobe_opcode_t *)ppc_kallsyms_lookup_name("emulate_step");
  219. if (!op_callback_addr || !emulate_step_addr) {
  220. WARN(1, "Unable to lookup optimized_callback()/emulate_step()\n");
  221. goto error;
  222. }
  223. branch_op_callback = create_branch((unsigned int *)buff + TMPL_CALL_HDLR_IDX,
  224. (unsigned long)op_callback_addr,
  225. BRANCH_SET_LINK);
  226. branch_emulate_step = create_branch((unsigned int *)buff + TMPL_EMULATE_IDX,
  227. (unsigned long)emulate_step_addr,
  228. BRANCH_SET_LINK);
  229. if (!branch_op_callback || !branch_emulate_step)
  230. goto error;
  231. patch_instruction(buff + TMPL_CALL_HDLR_IDX, branch_op_callback);
  232. patch_instruction(buff + TMPL_EMULATE_IDX, branch_emulate_step);
  233. /*
  234. * 3. load instruction to be emulated into relevant register, and
  235. */
  236. patch_imm32_load_insns(*p->ainsn.insn, buff + TMPL_INSN_IDX);
  237. /*
  238. * 4. branch back from trampoline
  239. */
  240. patch_branch(buff + TMPL_RET_IDX, (unsigned long)nip, 0);
  241. flush_icache_range((unsigned long)buff,
  242. (unsigned long)(&buff[TMPL_END_IDX]));
  243. op->optinsn.insn = buff;
  244. return 0;
  245. error:
  246. free_ppc_optinsn_slot(buff, 0);
  247. return -ERANGE;
  248. }
  249. int arch_prepared_optinsn(struct arch_optimized_insn *optinsn)
  250. {
  251. return optinsn->insn != NULL;
  252. }
  253. /*
  254. * On powerpc, Optprobes always replaces one instruction (4 bytes
  255. * aligned and 4 bytes long). It is impossible to encounter another
  256. * kprobe in this address range. So always return 0.
  257. */
  258. int arch_check_optimized_kprobe(struct optimized_kprobe *op)
  259. {
  260. return 0;
  261. }
  262. void arch_optimize_kprobes(struct list_head *oplist)
  263. {
  264. struct optimized_kprobe *op;
  265. struct optimized_kprobe *tmp;
  266. list_for_each_entry_safe(op, tmp, oplist, list) {
  267. /*
  268. * Backup instructions which will be replaced
  269. * by jump address
  270. */
  271. memcpy(op->optinsn.copied_insn, op->kp.addr,
  272. RELATIVEJUMP_SIZE);
  273. patch_instruction(op->kp.addr,
  274. create_branch((unsigned int *)op->kp.addr,
  275. (unsigned long)op->optinsn.insn, 0));
  276. list_del_init(&op->list);
  277. }
  278. }
  279. void arch_unoptimize_kprobe(struct optimized_kprobe *op)
  280. {
  281. arch_arm_kprobe(&op->kp);
  282. }
  283. void arch_unoptimize_kprobes(struct list_head *oplist,
  284. struct list_head *done_list)
  285. {
  286. struct optimized_kprobe *op;
  287. struct optimized_kprobe *tmp;
  288. list_for_each_entry_safe(op, tmp, oplist, list) {
  289. arch_unoptimize_kprobe(op);
  290. list_move(&op->list, done_list);
  291. }
  292. }
  293. int arch_within_optimized_kprobe(struct optimized_kprobe *op,
  294. unsigned long addr)
  295. {
  296. return ((unsigned long)op->kp.addr <= addr &&
  297. (unsigned long)op->kp.addr + RELATIVEJUMP_SIZE > addr);
  298. }