kprobes.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Kernel Probes (KProbes)
  3. * arch/ia64/kernel/kprobes.c
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. *
  19. * Copyright (C) IBM Corporation, 2002, 2004
  20. * Copyright (C) Intel Corporation, 2005
  21. *
  22. * 2005-Apr Rusty Lynch <rusty.lynch@intel.com> and Anil S Keshavamurthy
  23. * <anil.s.keshavamurthy@intel.com> adapted from i386
  24. */
  25. #include <linux/config.h>
  26. #include <linux/kprobes.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/string.h>
  30. #include <linux/slab.h>
  31. #include <linux/preempt.h>
  32. #include <linux/moduleloader.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/kdebug.h>
  35. extern void jprobe_inst_return(void);
  36. /* kprobe_status settings */
  37. #define KPROBE_HIT_ACTIVE 0x00000001
  38. #define KPROBE_HIT_SS 0x00000002
  39. static struct kprobe *current_kprobe;
  40. static unsigned long kprobe_status;
  41. static struct pt_regs jprobe_saved_regs;
  42. enum instruction_type {A, I, M, F, B, L, X, u};
  43. static enum instruction_type bundle_encoding[32][3] = {
  44. { M, I, I }, /* 00 */
  45. { M, I, I }, /* 01 */
  46. { M, I, I }, /* 02 */
  47. { M, I, I }, /* 03 */
  48. { M, L, X }, /* 04 */
  49. { M, L, X }, /* 05 */
  50. { u, u, u }, /* 06 */
  51. { u, u, u }, /* 07 */
  52. { M, M, I }, /* 08 */
  53. { M, M, I }, /* 09 */
  54. { M, M, I }, /* 0A */
  55. { M, M, I }, /* 0B */
  56. { M, F, I }, /* 0C */
  57. { M, F, I }, /* 0D */
  58. { M, M, F }, /* 0E */
  59. { M, M, F }, /* 0F */
  60. { M, I, B }, /* 10 */
  61. { M, I, B }, /* 11 */
  62. { M, B, B }, /* 12 */
  63. { M, B, B }, /* 13 */
  64. { u, u, u }, /* 14 */
  65. { u, u, u }, /* 15 */
  66. { B, B, B }, /* 16 */
  67. { B, B, B }, /* 17 */
  68. { M, M, B }, /* 18 */
  69. { M, M, B }, /* 19 */
  70. { u, u, u }, /* 1A */
  71. { u, u, u }, /* 1B */
  72. { M, F, B }, /* 1C */
  73. { M, F, B }, /* 1D */
  74. { u, u, u }, /* 1E */
  75. { u, u, u }, /* 1F */
  76. };
  77. int arch_prepare_kprobe(struct kprobe *p)
  78. {
  79. unsigned long addr = (unsigned long) p->addr;
  80. unsigned long bundle_addr = addr & ~0xFULL;
  81. unsigned long slot = addr & 0xf;
  82. bundle_t bundle;
  83. unsigned long template;
  84. /*
  85. * TODO: Verify that a probe is not being inserted
  86. * in sensitive regions of code
  87. * TODO: Verify that the memory holding the probe is rwx
  88. * TODO: verify this is a kernel address
  89. */
  90. memcpy(&bundle, (unsigned long *)bundle_addr, sizeof(bundle_t));
  91. template = bundle.quad0.template;
  92. if (((bundle_encoding[template][1] == L) && slot > 1) || (slot > 2)) {
  93. printk(KERN_WARNING "Attempting to insert unaligned kprobe at 0x%lx\n", addr);
  94. return -EINVAL;
  95. }
  96. return 0;
  97. }
  98. void arch_copy_kprobe(struct kprobe *p)
  99. {
  100. unsigned long addr = (unsigned long)p->addr;
  101. unsigned long bundle_addr = addr & ~0xFULL;
  102. memcpy(&p->ainsn.insn.bundle, (unsigned long *)bundle_addr,
  103. sizeof(bundle_t));
  104. memcpy(&p->opcode.bundle, &p->ainsn.insn.bundle, sizeof(bundle_t));
  105. }
  106. void arch_arm_kprobe(struct kprobe *p)
  107. {
  108. unsigned long addr = (unsigned long)p->addr;
  109. unsigned long arm_addr = addr & ~0xFULL;
  110. unsigned long slot = addr & 0xf;
  111. unsigned long template;
  112. unsigned long major_opcode = 0;
  113. unsigned long lx_type_inst = 0;
  114. unsigned long kprobe_inst = 0;
  115. bundle_t bundle;
  116. p->ainsn.inst_flag = 0;
  117. p->ainsn.target_br_reg = 0;
  118. memcpy(&bundle, &p->ainsn.insn.bundle, sizeof(bundle_t));
  119. template = bundle.quad0.template;
  120. if (slot == 1 && bundle_encoding[template][1] == L) {
  121. lx_type_inst = 1;
  122. slot = 2;
  123. }
  124. switch (slot) {
  125. case 0:
  126. major_opcode = (bundle.quad0.slot0 >> SLOT0_OPCODE_SHIFT);
  127. kprobe_inst = bundle.quad0.slot0;
  128. bundle.quad0.slot0 = BREAK_INST;
  129. break;
  130. case 1:
  131. major_opcode = (bundle.quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
  132. kprobe_inst = (bundle.quad0.slot1_p0 |
  133. (bundle.quad1.slot1_p1 << (64-46)));
  134. bundle.quad0.slot1_p0 = BREAK_INST;
  135. bundle.quad1.slot1_p1 = (BREAK_INST >> (64-46));
  136. break;
  137. case 2:
  138. major_opcode = (bundle.quad1.slot2 >> SLOT2_OPCODE_SHIFT);
  139. kprobe_inst = bundle.quad1.slot2;
  140. bundle.quad1.slot2 = BREAK_INST;
  141. break;
  142. }
  143. /*
  144. * Look for IP relative Branches, IP relative call or
  145. * IP relative predicate instructions
  146. */
  147. if (bundle_encoding[template][slot] == B) {
  148. switch (major_opcode) {
  149. case INDIRECT_CALL_OPCODE:
  150. p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
  151. p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
  152. break;
  153. case IP_RELATIVE_PREDICT_OPCODE:
  154. case IP_RELATIVE_BRANCH_OPCODE:
  155. p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
  156. break;
  157. case IP_RELATIVE_CALL_OPCODE:
  158. p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
  159. p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
  160. p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
  161. break;
  162. default:
  163. /* Do nothing */
  164. break;
  165. }
  166. } else if (lx_type_inst) {
  167. switch (major_opcode) {
  168. case LONG_CALL_OPCODE:
  169. p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
  170. p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
  171. break;
  172. default:
  173. /* Do nothing */
  174. break;
  175. }
  176. }
  177. /* Flush icache for the instruction at the emulated address */
  178. flush_icache_range((unsigned long)&p->ainsn.insn.bundle,
  179. (unsigned long)&p->ainsn.insn.bundle +
  180. sizeof(bundle_t));
  181. /*
  182. * Patch the original instruction with the probe instruction
  183. * and flush the instruction cache
  184. */
  185. memcpy((char *) arm_addr, (char *) &bundle, sizeof(bundle_t));
  186. flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
  187. }
  188. void arch_disarm_kprobe(struct kprobe *p)
  189. {
  190. unsigned long addr = (unsigned long)p->addr;
  191. unsigned long arm_addr = addr & ~0xFULL;
  192. /* p->opcode contains the original unaltered bundle */
  193. memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t));
  194. flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
  195. }
  196. void arch_remove_kprobe(struct kprobe *p)
  197. {
  198. }
  199. /*
  200. * We are resuming execution after a single step fault, so the pt_regs
  201. * structure reflects the register state after we executed the instruction
  202. * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
  203. * the ip to point back to the original stack address. To set the IP address
  204. * to original stack address, handle the case where we need to fixup the
  205. * relative IP address and/or fixup branch register.
  206. */
  207. static void resume_execution(struct kprobe *p, struct pt_regs *regs)
  208. {
  209. unsigned long bundle_addr = ((unsigned long) (&p->ainsn.insn.bundle)) & ~0xFULL;
  210. unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
  211. unsigned long template;
  212. int slot = ((unsigned long)p->addr & 0xf);
  213. template = p->opcode.bundle.quad0.template;
  214. if (slot == 1 && bundle_encoding[template][1] == L)
  215. slot = 2;
  216. if (p->ainsn.inst_flag) {
  217. if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
  218. /* Fix relative IP address */
  219. regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr;
  220. }
  221. if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
  222. /*
  223. * Fix target branch register, software convention is
  224. * to use either b0 or b6 or b7, so just checking
  225. * only those registers
  226. */
  227. switch (p->ainsn.target_br_reg) {
  228. case 0:
  229. if ((regs->b0 == bundle_addr) ||
  230. (regs->b0 == bundle_addr + 0x10)) {
  231. regs->b0 = (regs->b0 - bundle_addr) +
  232. resume_addr;
  233. }
  234. break;
  235. case 6:
  236. if ((regs->b6 == bundle_addr) ||
  237. (regs->b6 == bundle_addr + 0x10)) {
  238. regs->b6 = (regs->b6 - bundle_addr) +
  239. resume_addr;
  240. }
  241. break;
  242. case 7:
  243. if ((regs->b7 == bundle_addr) ||
  244. (regs->b7 == bundle_addr + 0x10)) {
  245. regs->b7 = (regs->b7 - bundle_addr) +
  246. resume_addr;
  247. }
  248. break;
  249. } /* end switch */
  250. }
  251. goto turn_ss_off;
  252. }
  253. if (slot == 2) {
  254. if (regs->cr_iip == bundle_addr + 0x10) {
  255. regs->cr_iip = resume_addr + 0x10;
  256. }
  257. } else {
  258. if (regs->cr_iip == bundle_addr) {
  259. regs->cr_iip = resume_addr;
  260. }
  261. }
  262. turn_ss_off:
  263. /* Turn off Single Step bit */
  264. ia64_psr(regs)->ss = 0;
  265. }
  266. static void prepare_ss(struct kprobe *p, struct pt_regs *regs)
  267. {
  268. unsigned long bundle_addr = (unsigned long) &p->ainsn.insn.bundle;
  269. unsigned long slot = (unsigned long)p->addr & 0xf;
  270. /* Update instruction pointer (IIP) and slot number (IPSR.ri) */
  271. regs->cr_iip = bundle_addr & ~0xFULL;
  272. if (slot > 2)
  273. slot = 0;
  274. ia64_psr(regs)->ri = slot;
  275. /* turn on single stepping */
  276. ia64_psr(regs)->ss = 1;
  277. }
  278. static int pre_kprobes_handler(struct pt_regs *regs)
  279. {
  280. struct kprobe *p;
  281. int ret = 0;
  282. kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
  283. preempt_disable();
  284. /* Handle recursion cases */
  285. if (kprobe_running()) {
  286. p = get_kprobe(addr);
  287. if (p) {
  288. if (kprobe_status == KPROBE_HIT_SS) {
  289. unlock_kprobes();
  290. goto no_kprobe;
  291. }
  292. arch_disarm_kprobe(p);
  293. ret = 1;
  294. } else {
  295. /*
  296. * jprobe instrumented function just completed
  297. */
  298. p = current_kprobe;
  299. if (p->break_handler && p->break_handler(p, regs)) {
  300. goto ss_probe;
  301. }
  302. }
  303. }
  304. lock_kprobes();
  305. p = get_kprobe(addr);
  306. if (!p) {
  307. unlock_kprobes();
  308. goto no_kprobe;
  309. }
  310. kprobe_status = KPROBE_HIT_ACTIVE;
  311. current_kprobe = p;
  312. if (p->pre_handler && p->pre_handler(p, regs))
  313. /*
  314. * Our pre-handler is specifically requesting that we just
  315. * do a return. This is handling the case where the
  316. * pre-handler is really our special jprobe pre-handler.
  317. */
  318. return 1;
  319. ss_probe:
  320. prepare_ss(p, regs);
  321. kprobe_status = KPROBE_HIT_SS;
  322. return 1;
  323. no_kprobe:
  324. preempt_enable_no_resched();
  325. return ret;
  326. }
  327. static int post_kprobes_handler(struct pt_regs *regs)
  328. {
  329. if (!kprobe_running())
  330. return 0;
  331. if (current_kprobe->post_handler)
  332. current_kprobe->post_handler(current_kprobe, regs, 0);
  333. resume_execution(current_kprobe, regs);
  334. unlock_kprobes();
  335. preempt_enable_no_resched();
  336. return 1;
  337. }
  338. static int kprobes_fault_handler(struct pt_regs *regs, int trapnr)
  339. {
  340. if (!kprobe_running())
  341. return 0;
  342. if (current_kprobe->fault_handler &&
  343. current_kprobe->fault_handler(current_kprobe, regs, trapnr))
  344. return 1;
  345. if (kprobe_status & KPROBE_HIT_SS) {
  346. resume_execution(current_kprobe, regs);
  347. unlock_kprobes();
  348. preempt_enable_no_resched();
  349. }
  350. return 0;
  351. }
  352. int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
  353. void *data)
  354. {
  355. struct die_args *args = (struct die_args *)data;
  356. switch(val) {
  357. case DIE_BREAK:
  358. if (pre_kprobes_handler(args->regs))
  359. return NOTIFY_STOP;
  360. break;
  361. case DIE_SS:
  362. if (post_kprobes_handler(args->regs))
  363. return NOTIFY_STOP;
  364. break;
  365. case DIE_PAGE_FAULT:
  366. if (kprobes_fault_handler(args->regs, args->trapnr))
  367. return NOTIFY_STOP;
  368. default:
  369. break;
  370. }
  371. return NOTIFY_DONE;
  372. }
  373. int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  374. {
  375. struct jprobe *jp = container_of(p, struct jprobe, kp);
  376. unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
  377. /* save architectural state */
  378. jprobe_saved_regs = *regs;
  379. /* after rfi, execute the jprobe instrumented function */
  380. regs->cr_iip = addr & ~0xFULL;
  381. ia64_psr(regs)->ri = addr & 0xf;
  382. regs->r1 = ((struct fnptr *)(jp->entry))->gp;
  383. /*
  384. * fix the return address to our jprobe_inst_return() function
  385. * in the jprobes.S file
  386. */
  387. regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
  388. return 1;
  389. }
  390. int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  391. {
  392. *regs = jprobe_saved_regs;
  393. return 1;
  394. }