kprobes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. /*
  78. * In this function we check to see if the instruction
  79. * is IP relative instruction and update the kprobe
  80. * inst flag accordingly
  81. */
  82. static void update_kprobe_inst_flag(uint template, uint slot, uint major_opcode,
  83. unsigned long kprobe_inst, struct kprobe *p)
  84. {
  85. p->ainsn.inst_flag = 0;
  86. p->ainsn.target_br_reg = 0;
  87. if (bundle_encoding[template][slot] == B) {
  88. switch (major_opcode) {
  89. case INDIRECT_CALL_OPCODE:
  90. p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
  91. p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
  92. break;
  93. case IP_RELATIVE_PREDICT_OPCODE:
  94. case IP_RELATIVE_BRANCH_OPCODE:
  95. p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
  96. break;
  97. case IP_RELATIVE_CALL_OPCODE:
  98. p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
  99. p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
  100. p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
  101. break;
  102. }
  103. } else if (bundle_encoding[template][slot] == X) {
  104. switch (major_opcode) {
  105. case LONG_CALL_OPCODE:
  106. p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
  107. p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
  108. break;
  109. }
  110. }
  111. return;
  112. }
  113. /*
  114. * In this function we check to see if the instruction
  115. * (qp) cmpx.crel.ctype p1,p2=r2,r3
  116. * on which we are inserting kprobe is cmp instruction
  117. * with ctype as unc.
  118. */
  119. static uint is_cmp_ctype_unc_inst(uint template, uint slot, uint major_opcode,
  120. unsigned long kprobe_inst)
  121. {
  122. cmp_inst_t cmp_inst;
  123. uint ctype_unc = 0;
  124. if (!((bundle_encoding[template][slot] == I) ||
  125. (bundle_encoding[template][slot] == M)))
  126. goto out;
  127. if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
  128. (major_opcode == 0xE)))
  129. goto out;
  130. cmp_inst.l = kprobe_inst;
  131. if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {
  132. /* Integere compare - Register Register (A6 type)*/
  133. if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)
  134. &&(cmp_inst.f.c == 1))
  135. ctype_unc = 1;
  136. } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {
  137. /* Integere compare - Immediate Register (A8 type)*/
  138. if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))
  139. ctype_unc = 1;
  140. }
  141. out:
  142. return ctype_unc;
  143. }
  144. /*
  145. * In this function we override the bundle with
  146. * the break instruction at the given slot.
  147. */
  148. static void prepare_break_inst(uint template, uint slot, uint major_opcode,
  149. unsigned long kprobe_inst, struct kprobe *p)
  150. {
  151. unsigned long break_inst = BREAK_INST;
  152. bundle_t *bundle = &p->ainsn.insn.bundle;
  153. /*
  154. * Copy the original kprobe_inst qualifying predicate(qp)
  155. * to the break instruction iff !is_cmp_ctype_unc_inst
  156. * because for cmp instruction with ctype equal to unc,
  157. * which is a special instruction always needs to be
  158. * executed regradless of qp
  159. */
  160. if (!is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst))
  161. break_inst |= (0x3f & kprobe_inst);
  162. switch (slot) {
  163. case 0:
  164. bundle->quad0.slot0 = break_inst;
  165. break;
  166. case 1:
  167. bundle->quad0.slot1_p0 = break_inst;
  168. bundle->quad1.slot1_p1 = break_inst >> (64-46);
  169. break;
  170. case 2:
  171. bundle->quad1.slot2 = break_inst;
  172. break;
  173. }
  174. /*
  175. * Update the instruction flag, so that we can
  176. * emulate the instruction properly after we
  177. * single step on original instruction
  178. */
  179. update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
  180. }
  181. static inline void get_kprobe_inst(bundle_t *bundle, uint slot,
  182. unsigned long *kprobe_inst, uint *major_opcode)
  183. {
  184. unsigned long kprobe_inst_p0, kprobe_inst_p1;
  185. unsigned int template;
  186. template = bundle->quad0.template;
  187. switch (slot) {
  188. case 0:
  189. *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
  190. *kprobe_inst = bundle->quad0.slot0;
  191. break;
  192. case 1:
  193. *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
  194. kprobe_inst_p0 = bundle->quad0.slot1_p0;
  195. kprobe_inst_p1 = bundle->quad1.slot1_p1;
  196. *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
  197. break;
  198. case 2:
  199. *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
  200. *kprobe_inst = bundle->quad1.slot2;
  201. break;
  202. }
  203. }
  204. static int valid_kprobe_addr(int template, int slot, unsigned long addr)
  205. {
  206. if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
  207. printk(KERN_WARNING "Attempting to insert unaligned kprobe at 0x%lx\n",
  208. addr);
  209. return -EINVAL;
  210. }
  211. return 0;
  212. }
  213. int arch_prepare_kprobe(struct kprobe *p)
  214. {
  215. unsigned long addr = (unsigned long) p->addr;
  216. unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
  217. unsigned long kprobe_inst=0;
  218. unsigned int slot = addr & 0xf, template, major_opcode = 0;
  219. bundle_t *bundle = &p->ainsn.insn.bundle;
  220. memcpy(&p->opcode.bundle, kprobe_addr, sizeof(bundle_t));
  221. memcpy(&p->ainsn.insn.bundle, kprobe_addr, sizeof(bundle_t));
  222. template = bundle->quad0.template;
  223. if(valid_kprobe_addr(template, slot, addr))
  224. return -EINVAL;
  225. /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
  226. if (slot == 1 && bundle_encoding[template][1] == L)
  227. slot++;
  228. /* Get kprobe_inst and major_opcode from the bundle */
  229. get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
  230. prepare_break_inst(template, slot, major_opcode, kprobe_inst, p);
  231. return 0;
  232. }
  233. void arch_arm_kprobe(struct kprobe *p)
  234. {
  235. unsigned long addr = (unsigned long)p->addr;
  236. unsigned long arm_addr = addr & ~0xFULL;
  237. memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t));
  238. flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
  239. }
  240. void arch_disarm_kprobe(struct kprobe *p)
  241. {
  242. unsigned long addr = (unsigned long)p->addr;
  243. unsigned long arm_addr = addr & ~0xFULL;
  244. /* p->opcode contains the original unaltered bundle */
  245. memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t));
  246. flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
  247. }
  248. void arch_remove_kprobe(struct kprobe *p)
  249. {
  250. }
  251. /*
  252. * We are resuming execution after a single step fault, so the pt_regs
  253. * structure reflects the register state after we executed the instruction
  254. * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
  255. * the ip to point back to the original stack address. To set the IP address
  256. * to original stack address, handle the case where we need to fixup the
  257. * relative IP address and/or fixup branch register.
  258. */
  259. static void resume_execution(struct kprobe *p, struct pt_regs *regs)
  260. {
  261. unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL;
  262. unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
  263. unsigned long template;
  264. int slot = ((unsigned long)p->addr & 0xf);
  265. template = p->opcode.bundle.quad0.template;
  266. if (slot == 1 && bundle_encoding[template][1] == L)
  267. slot = 2;
  268. if (p->ainsn.inst_flag) {
  269. if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
  270. /* Fix relative IP address */
  271. regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr;
  272. }
  273. if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
  274. /*
  275. * Fix target branch register, software convention is
  276. * to use either b0 or b6 or b7, so just checking
  277. * only those registers
  278. */
  279. switch (p->ainsn.target_br_reg) {
  280. case 0:
  281. if ((regs->b0 == bundle_addr) ||
  282. (regs->b0 == bundle_addr + 0x10)) {
  283. regs->b0 = (regs->b0 - bundle_addr) +
  284. resume_addr;
  285. }
  286. break;
  287. case 6:
  288. if ((regs->b6 == bundle_addr) ||
  289. (regs->b6 == bundle_addr + 0x10)) {
  290. regs->b6 = (regs->b6 - bundle_addr) +
  291. resume_addr;
  292. }
  293. break;
  294. case 7:
  295. if ((regs->b7 == bundle_addr) ||
  296. (regs->b7 == bundle_addr + 0x10)) {
  297. regs->b7 = (regs->b7 - bundle_addr) +
  298. resume_addr;
  299. }
  300. break;
  301. } /* end switch */
  302. }
  303. goto turn_ss_off;
  304. }
  305. if (slot == 2) {
  306. if (regs->cr_iip == bundle_addr + 0x10) {
  307. regs->cr_iip = resume_addr + 0x10;
  308. }
  309. } else {
  310. if (regs->cr_iip == bundle_addr) {
  311. regs->cr_iip = resume_addr;
  312. }
  313. }
  314. turn_ss_off:
  315. /* Turn off Single Step bit */
  316. ia64_psr(regs)->ss = 0;
  317. }
  318. static void prepare_ss(struct kprobe *p, struct pt_regs *regs)
  319. {
  320. unsigned long bundle_addr = (unsigned long) &p->opcode.bundle;
  321. unsigned long slot = (unsigned long)p->addr & 0xf;
  322. /* Update instruction pointer (IIP) and slot number (IPSR.ri) */
  323. regs->cr_iip = bundle_addr & ~0xFULL;
  324. if (slot > 2)
  325. slot = 0;
  326. ia64_psr(regs)->ri = slot;
  327. /* turn on single stepping */
  328. ia64_psr(regs)->ss = 1;
  329. }
  330. static int pre_kprobes_handler(struct pt_regs *regs)
  331. {
  332. struct kprobe *p;
  333. int ret = 0;
  334. kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
  335. preempt_disable();
  336. /* Handle recursion cases */
  337. if (kprobe_running()) {
  338. p = get_kprobe(addr);
  339. if (p) {
  340. if (kprobe_status == KPROBE_HIT_SS) {
  341. unlock_kprobes();
  342. goto no_kprobe;
  343. }
  344. arch_disarm_kprobe(p);
  345. ret = 1;
  346. } else {
  347. /*
  348. * jprobe instrumented function just completed
  349. */
  350. p = current_kprobe;
  351. if (p->break_handler && p->break_handler(p, regs)) {
  352. goto ss_probe;
  353. }
  354. }
  355. }
  356. lock_kprobes();
  357. p = get_kprobe(addr);
  358. if (!p) {
  359. unlock_kprobes();
  360. goto no_kprobe;
  361. }
  362. kprobe_status = KPROBE_HIT_ACTIVE;
  363. current_kprobe = p;
  364. if (p->pre_handler && p->pre_handler(p, regs))
  365. /*
  366. * Our pre-handler is specifically requesting that we just
  367. * do a return. This is handling the case where the
  368. * pre-handler is really our special jprobe pre-handler.
  369. */
  370. return 1;
  371. ss_probe:
  372. prepare_ss(p, regs);
  373. kprobe_status = KPROBE_HIT_SS;
  374. return 1;
  375. no_kprobe:
  376. preempt_enable_no_resched();
  377. return ret;
  378. }
  379. static int post_kprobes_handler(struct pt_regs *regs)
  380. {
  381. if (!kprobe_running())
  382. return 0;
  383. if (current_kprobe->post_handler)
  384. current_kprobe->post_handler(current_kprobe, regs, 0);
  385. resume_execution(current_kprobe, regs);
  386. unlock_kprobes();
  387. preempt_enable_no_resched();
  388. return 1;
  389. }
  390. static int kprobes_fault_handler(struct pt_regs *regs, int trapnr)
  391. {
  392. if (!kprobe_running())
  393. return 0;
  394. if (current_kprobe->fault_handler &&
  395. current_kprobe->fault_handler(current_kprobe, regs, trapnr))
  396. return 1;
  397. if (kprobe_status & KPROBE_HIT_SS) {
  398. resume_execution(current_kprobe, regs);
  399. unlock_kprobes();
  400. preempt_enable_no_resched();
  401. }
  402. return 0;
  403. }
  404. int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
  405. void *data)
  406. {
  407. struct die_args *args = (struct die_args *)data;
  408. switch(val) {
  409. case DIE_BREAK:
  410. if (pre_kprobes_handler(args->regs))
  411. return NOTIFY_STOP;
  412. break;
  413. case DIE_SS:
  414. if (post_kprobes_handler(args->regs))
  415. return NOTIFY_STOP;
  416. break;
  417. case DIE_PAGE_FAULT:
  418. if (kprobes_fault_handler(args->regs, args->trapnr))
  419. return NOTIFY_STOP;
  420. default:
  421. break;
  422. }
  423. return NOTIFY_DONE;
  424. }
  425. int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  426. {
  427. struct jprobe *jp = container_of(p, struct jprobe, kp);
  428. unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
  429. /* save architectural state */
  430. jprobe_saved_regs = *regs;
  431. /* after rfi, execute the jprobe instrumented function */
  432. regs->cr_iip = addr & ~0xFULL;
  433. ia64_psr(regs)->ri = addr & 0xf;
  434. regs->r1 = ((struct fnptr *)(jp->entry))->gp;
  435. /*
  436. * fix the return address to our jprobe_inst_return() function
  437. * in the jprobes.S file
  438. */
  439. regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
  440. return 1;
  441. }
  442. int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  443. {
  444. *regs = jprobe_saved_regs;
  445. return 1;
  446. }