kprobes.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. #include <asm/sections.h>
  36. extern void jprobe_inst_return(void);
  37. /* kprobe_status settings */
  38. #define KPROBE_HIT_ACTIVE 0x00000001
  39. #define KPROBE_HIT_SS 0x00000002
  40. static struct kprobe *current_kprobe, *kprobe_prev;
  41. static unsigned long kprobe_status, kprobe_status_prev;
  42. static struct pt_regs jprobe_saved_regs;
  43. enum instruction_type {A, I, M, F, B, L, X, u};
  44. static enum instruction_type bundle_encoding[32][3] = {
  45. { M, I, I }, /* 00 */
  46. { M, I, I }, /* 01 */
  47. { M, I, I }, /* 02 */
  48. { M, I, I }, /* 03 */
  49. { M, L, X }, /* 04 */
  50. { M, L, X }, /* 05 */
  51. { u, u, u }, /* 06 */
  52. { u, u, u }, /* 07 */
  53. { M, M, I }, /* 08 */
  54. { M, M, I }, /* 09 */
  55. { M, M, I }, /* 0A */
  56. { M, M, I }, /* 0B */
  57. { M, F, I }, /* 0C */
  58. { M, F, I }, /* 0D */
  59. { M, M, F }, /* 0E */
  60. { M, M, F }, /* 0F */
  61. { M, I, B }, /* 10 */
  62. { M, I, B }, /* 11 */
  63. { M, B, B }, /* 12 */
  64. { M, B, B }, /* 13 */
  65. { u, u, u }, /* 14 */
  66. { u, u, u }, /* 15 */
  67. { B, B, B }, /* 16 */
  68. { B, B, B }, /* 17 */
  69. { M, M, B }, /* 18 */
  70. { M, M, B }, /* 19 */
  71. { u, u, u }, /* 1A */
  72. { u, u, u }, /* 1B */
  73. { M, F, B }, /* 1C */
  74. { M, F, B }, /* 1D */
  75. { u, u, u }, /* 1E */
  76. { u, u, u }, /* 1F */
  77. };
  78. /*
  79. * In this function we check to see if the instruction
  80. * is IP relative instruction and update the kprobe
  81. * inst flag accordingly
  82. */
  83. static void update_kprobe_inst_flag(uint template, uint slot, uint major_opcode,
  84. unsigned long kprobe_inst, struct kprobe *p)
  85. {
  86. p->ainsn.inst_flag = 0;
  87. p->ainsn.target_br_reg = 0;
  88. if (bundle_encoding[template][slot] == B) {
  89. switch (major_opcode) {
  90. case INDIRECT_CALL_OPCODE:
  91. p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
  92. p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
  93. break;
  94. case IP_RELATIVE_PREDICT_OPCODE:
  95. case IP_RELATIVE_BRANCH_OPCODE:
  96. p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
  97. break;
  98. case IP_RELATIVE_CALL_OPCODE:
  99. p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
  100. p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
  101. p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
  102. break;
  103. }
  104. } else if (bundle_encoding[template][slot] == X) {
  105. switch (major_opcode) {
  106. case LONG_CALL_OPCODE:
  107. p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
  108. p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
  109. break;
  110. }
  111. }
  112. return;
  113. }
  114. /*
  115. * In this function we check to see if the instruction
  116. * on which we are inserting kprobe is supported.
  117. * Returns 0 if supported
  118. * Returns -EINVAL if unsupported
  119. */
  120. static int unsupported_inst(uint template, uint slot, uint major_opcode,
  121. unsigned long kprobe_inst, struct kprobe *p)
  122. {
  123. unsigned long addr = (unsigned long)p->addr;
  124. if (bundle_encoding[template][slot] == I) {
  125. switch (major_opcode) {
  126. case 0x0: //I_UNIT_MISC_OPCODE:
  127. /*
  128. * Check for Integer speculation instruction
  129. * - Bit 33-35 to be equal to 0x1
  130. */
  131. if (((kprobe_inst >> 33) & 0x7) == 1) {
  132. printk(KERN_WARNING
  133. "Kprobes on speculation inst at <0x%lx> not supported\n",
  134. addr);
  135. return -EINVAL;
  136. }
  137. /*
  138. * IP relative mov instruction
  139. * - Bit 27-35 to be equal to 0x30
  140. */
  141. if (((kprobe_inst >> 27) & 0x1FF) == 0x30) {
  142. printk(KERN_WARNING
  143. "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
  144. addr);
  145. return -EINVAL;
  146. }
  147. }
  148. }
  149. return 0;
  150. }
  151. /*
  152. * In this function we check to see if the instruction
  153. * (qp) cmpx.crel.ctype p1,p2=r2,r3
  154. * on which we are inserting kprobe is cmp instruction
  155. * with ctype as unc.
  156. */
  157. static uint is_cmp_ctype_unc_inst(uint template, uint slot, uint major_opcode,
  158. unsigned long kprobe_inst)
  159. {
  160. cmp_inst_t cmp_inst;
  161. uint ctype_unc = 0;
  162. if (!((bundle_encoding[template][slot] == I) ||
  163. (bundle_encoding[template][slot] == M)))
  164. goto out;
  165. if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
  166. (major_opcode == 0xE)))
  167. goto out;
  168. cmp_inst.l = kprobe_inst;
  169. if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {
  170. /* Integere compare - Register Register (A6 type)*/
  171. if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)
  172. &&(cmp_inst.f.c == 1))
  173. ctype_unc = 1;
  174. } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {
  175. /* Integere compare - Immediate Register (A8 type)*/
  176. if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))
  177. ctype_unc = 1;
  178. }
  179. out:
  180. return ctype_unc;
  181. }
  182. /*
  183. * In this function we override the bundle with
  184. * the break instruction at the given slot.
  185. */
  186. static void prepare_break_inst(uint template, uint slot, uint major_opcode,
  187. unsigned long kprobe_inst, struct kprobe *p)
  188. {
  189. unsigned long break_inst = BREAK_INST;
  190. bundle_t *bundle = &p->ainsn.insn.bundle;
  191. /*
  192. * Copy the original kprobe_inst qualifying predicate(qp)
  193. * to the break instruction iff !is_cmp_ctype_unc_inst
  194. * because for cmp instruction with ctype equal to unc,
  195. * which is a special instruction always needs to be
  196. * executed regradless of qp
  197. */
  198. if (!is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst))
  199. break_inst |= (0x3f & kprobe_inst);
  200. switch (slot) {
  201. case 0:
  202. bundle->quad0.slot0 = break_inst;
  203. break;
  204. case 1:
  205. bundle->quad0.slot1_p0 = break_inst;
  206. bundle->quad1.slot1_p1 = break_inst >> (64-46);
  207. break;
  208. case 2:
  209. bundle->quad1.slot2 = break_inst;
  210. break;
  211. }
  212. /*
  213. * Update the instruction flag, so that we can
  214. * emulate the instruction properly after we
  215. * single step on original instruction
  216. */
  217. update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
  218. }
  219. static inline void get_kprobe_inst(bundle_t *bundle, uint slot,
  220. unsigned long *kprobe_inst, uint *major_opcode)
  221. {
  222. unsigned long kprobe_inst_p0, kprobe_inst_p1;
  223. unsigned int template;
  224. template = bundle->quad0.template;
  225. switch (slot) {
  226. case 0:
  227. *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
  228. *kprobe_inst = bundle->quad0.slot0;
  229. break;
  230. case 1:
  231. *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
  232. kprobe_inst_p0 = bundle->quad0.slot1_p0;
  233. kprobe_inst_p1 = bundle->quad1.slot1_p1;
  234. *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
  235. break;
  236. case 2:
  237. *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
  238. *kprobe_inst = bundle->quad1.slot2;
  239. break;
  240. }
  241. }
  242. /* Returns non-zero if the addr is in the Interrupt Vector Table */
  243. static inline int in_ivt_functions(unsigned long addr)
  244. {
  245. return (addr >= (unsigned long)__start_ivt_text
  246. && addr < (unsigned long)__end_ivt_text);
  247. }
  248. static int valid_kprobe_addr(int template, int slot, unsigned long addr)
  249. {
  250. if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
  251. printk(KERN_WARNING "Attempting to insert unaligned kprobe "
  252. "at 0x%lx\n", addr);
  253. return -EINVAL;
  254. }
  255. if (in_ivt_functions(addr)) {
  256. printk(KERN_WARNING "Kprobes can't be inserted inside "
  257. "IVT functions at 0x%lx\n", addr);
  258. return -EINVAL;
  259. }
  260. if (slot == 1 && bundle_encoding[template][1] != L) {
  261. printk(KERN_WARNING "Inserting kprobes on slot #1 "
  262. "is not supported\n");
  263. return -EINVAL;
  264. }
  265. return 0;
  266. }
  267. static inline void save_previous_kprobe(void)
  268. {
  269. kprobe_prev = current_kprobe;
  270. kprobe_status_prev = kprobe_status;
  271. }
  272. static inline void restore_previous_kprobe(void)
  273. {
  274. current_kprobe = kprobe_prev;
  275. kprobe_status = kprobe_status_prev;
  276. }
  277. static inline void set_current_kprobe(struct kprobe *p)
  278. {
  279. current_kprobe = p;
  280. }
  281. static void kretprobe_trampoline(void)
  282. {
  283. }
  284. /*
  285. * At this point the target function has been tricked into
  286. * returning into our trampoline. Lookup the associated instance
  287. * and then:
  288. * - call the handler function
  289. * - cleanup by marking the instance as unused
  290. * - long jump back to the original return address
  291. */
  292. int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  293. {
  294. struct kretprobe_instance *ri = NULL;
  295. struct hlist_head *head;
  296. struct hlist_node *node, *tmp;
  297. unsigned long orig_ret_address = 0;
  298. unsigned long trampoline_address =
  299. ((struct fnptr *)kretprobe_trampoline)->ip;
  300. head = kretprobe_inst_table_head(current);
  301. /*
  302. * It is possible to have multiple instances associated with a given
  303. * task either because an multiple functions in the call path
  304. * have a return probe installed on them, and/or more then one return
  305. * return probe was registered for a target function.
  306. *
  307. * We can handle this because:
  308. * - instances are always inserted at the head of the list
  309. * - when multiple return probes are registered for the same
  310. * function, the first instance's ret_addr will point to the
  311. * real return address, and all the rest will point to
  312. * kretprobe_trampoline
  313. */
  314. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  315. if (ri->task != current)
  316. /* another task is sharing our hash bucket */
  317. continue;
  318. if (ri->rp && ri->rp->handler)
  319. ri->rp->handler(ri, regs);
  320. orig_ret_address = (unsigned long)ri->ret_addr;
  321. recycle_rp_inst(ri);
  322. if (orig_ret_address != trampoline_address)
  323. /*
  324. * This is the real return address. Any other
  325. * instances associated with this task are for
  326. * other calls deeper on the call stack
  327. */
  328. break;
  329. }
  330. BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
  331. regs->cr_iip = orig_ret_address;
  332. unlock_kprobes();
  333. preempt_enable_no_resched();
  334. /*
  335. * By returning a non-zero value, we are telling
  336. * kprobe_handler() that we have handled unlocking
  337. * and re-enabling preemption.
  338. */
  339. return 1;
  340. }
  341. void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs)
  342. {
  343. struct kretprobe_instance *ri;
  344. if ((ri = get_free_rp_inst(rp)) != NULL) {
  345. ri->rp = rp;
  346. ri->task = current;
  347. ri->ret_addr = (kprobe_opcode_t *)regs->b0;
  348. /* Replace the return addr with trampoline addr */
  349. regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip;
  350. add_rp_inst(ri);
  351. } else {
  352. rp->nmissed++;
  353. }
  354. }
  355. int arch_prepare_kprobe(struct kprobe *p)
  356. {
  357. unsigned long addr = (unsigned long) p->addr;
  358. unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
  359. unsigned long kprobe_inst=0;
  360. unsigned int slot = addr & 0xf, template, major_opcode = 0;
  361. bundle_t *bundle = &p->ainsn.insn.bundle;
  362. memcpy(&p->opcode.bundle, kprobe_addr, sizeof(bundle_t));
  363. memcpy(&p->ainsn.insn.bundle, kprobe_addr, sizeof(bundle_t));
  364. template = bundle->quad0.template;
  365. if(valid_kprobe_addr(template, slot, addr))
  366. return -EINVAL;
  367. /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
  368. if (slot == 1 && bundle_encoding[template][1] == L)
  369. slot++;
  370. /* Get kprobe_inst and major_opcode from the bundle */
  371. get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
  372. if (unsupported_inst(template, slot, major_opcode, kprobe_inst, p))
  373. return -EINVAL;
  374. prepare_break_inst(template, slot, major_opcode, kprobe_inst, p);
  375. return 0;
  376. }
  377. void arch_arm_kprobe(struct kprobe *p)
  378. {
  379. unsigned long addr = (unsigned long)p->addr;
  380. unsigned long arm_addr = addr & ~0xFULL;
  381. memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t));
  382. flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
  383. }
  384. void arch_disarm_kprobe(struct kprobe *p)
  385. {
  386. unsigned long addr = (unsigned long)p->addr;
  387. unsigned long arm_addr = addr & ~0xFULL;
  388. /* p->opcode contains the original unaltered bundle */
  389. memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t));
  390. flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
  391. }
  392. void arch_remove_kprobe(struct kprobe *p)
  393. {
  394. }
  395. /*
  396. * We are resuming execution after a single step fault, so the pt_regs
  397. * structure reflects the register state after we executed the instruction
  398. * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
  399. * the ip to point back to the original stack address. To set the IP address
  400. * to original stack address, handle the case where we need to fixup the
  401. * relative IP address and/or fixup branch register.
  402. */
  403. static void resume_execution(struct kprobe *p, struct pt_regs *regs)
  404. {
  405. unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL;
  406. unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
  407. unsigned long template;
  408. int slot = ((unsigned long)p->addr & 0xf);
  409. template = p->opcode.bundle.quad0.template;
  410. if (slot == 1 && bundle_encoding[template][1] == L)
  411. slot = 2;
  412. if (p->ainsn.inst_flag) {
  413. if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
  414. /* Fix relative IP address */
  415. regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr;
  416. }
  417. if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
  418. /*
  419. * Fix target branch register, software convention is
  420. * to use either b0 or b6 or b7, so just checking
  421. * only those registers
  422. */
  423. switch (p->ainsn.target_br_reg) {
  424. case 0:
  425. if ((regs->b0 == bundle_addr) ||
  426. (regs->b0 == bundle_addr + 0x10)) {
  427. regs->b0 = (regs->b0 - bundle_addr) +
  428. resume_addr;
  429. }
  430. break;
  431. case 6:
  432. if ((regs->b6 == bundle_addr) ||
  433. (regs->b6 == bundle_addr + 0x10)) {
  434. regs->b6 = (regs->b6 - bundle_addr) +
  435. resume_addr;
  436. }
  437. break;
  438. case 7:
  439. if ((regs->b7 == bundle_addr) ||
  440. (regs->b7 == bundle_addr + 0x10)) {
  441. regs->b7 = (regs->b7 - bundle_addr) +
  442. resume_addr;
  443. }
  444. break;
  445. } /* end switch */
  446. }
  447. goto turn_ss_off;
  448. }
  449. if (slot == 2) {
  450. if (regs->cr_iip == bundle_addr + 0x10) {
  451. regs->cr_iip = resume_addr + 0x10;
  452. }
  453. } else {
  454. if (regs->cr_iip == bundle_addr) {
  455. regs->cr_iip = resume_addr;
  456. }
  457. }
  458. turn_ss_off:
  459. /* Turn off Single Step bit */
  460. ia64_psr(regs)->ss = 0;
  461. }
  462. static void prepare_ss(struct kprobe *p, struct pt_regs *regs)
  463. {
  464. unsigned long bundle_addr = (unsigned long) &p->opcode.bundle;
  465. unsigned long slot = (unsigned long)p->addr & 0xf;
  466. /* Update instruction pointer (IIP) and slot number (IPSR.ri) */
  467. regs->cr_iip = bundle_addr & ~0xFULL;
  468. if (slot > 2)
  469. slot = 0;
  470. ia64_psr(regs)->ri = slot;
  471. /* turn on single stepping */
  472. ia64_psr(regs)->ss = 1;
  473. }
  474. static int pre_kprobes_handler(struct die_args *args)
  475. {
  476. struct kprobe *p;
  477. int ret = 0;
  478. struct pt_regs *regs = args->regs;
  479. kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
  480. preempt_disable();
  481. /* Handle recursion cases */
  482. if (kprobe_running()) {
  483. p = get_kprobe(addr);
  484. if (p) {
  485. if (kprobe_status == KPROBE_HIT_SS) {
  486. unlock_kprobes();
  487. goto no_kprobe;
  488. }
  489. /* We have reentered the pre_kprobe_handler(), since
  490. * another probe was hit while within the handler.
  491. * We here save the original kprobes variables and
  492. * just single step on the instruction of the new probe
  493. * without calling any user handlers.
  494. */
  495. save_previous_kprobe();
  496. set_current_kprobe(p);
  497. p->nmissed++;
  498. prepare_ss(p, regs);
  499. kprobe_status = KPROBE_REENTER;
  500. return 1;
  501. } else if (args->err == __IA64_BREAK_JPROBE) {
  502. /*
  503. * jprobe instrumented function just completed
  504. */
  505. p = current_kprobe;
  506. if (p->break_handler && p->break_handler(p, regs)) {
  507. goto ss_probe;
  508. }
  509. } else {
  510. /* Not our break */
  511. goto no_kprobe;
  512. }
  513. }
  514. lock_kprobes();
  515. p = get_kprobe(addr);
  516. if (!p) {
  517. unlock_kprobes();
  518. goto no_kprobe;
  519. }
  520. kprobe_status = KPROBE_HIT_ACTIVE;
  521. set_current_kprobe(p);
  522. if (p->pre_handler && p->pre_handler(p, regs))
  523. /*
  524. * Our pre-handler is specifically requesting that we just
  525. * do a return. This is used for both the jprobe pre-handler
  526. * and the kretprobe trampoline
  527. */
  528. return 1;
  529. ss_probe:
  530. prepare_ss(p, regs);
  531. kprobe_status = KPROBE_HIT_SS;
  532. return 1;
  533. no_kprobe:
  534. preempt_enable_no_resched();
  535. return ret;
  536. }
  537. static int post_kprobes_handler(struct pt_regs *regs)
  538. {
  539. if (!kprobe_running())
  540. return 0;
  541. if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
  542. kprobe_status = KPROBE_HIT_SSDONE;
  543. current_kprobe->post_handler(current_kprobe, regs, 0);
  544. }
  545. resume_execution(current_kprobe, regs);
  546. /*Restore back the original saved kprobes variables and continue. */
  547. if (kprobe_status == KPROBE_REENTER) {
  548. restore_previous_kprobe();
  549. goto out;
  550. }
  551. unlock_kprobes();
  552. out:
  553. preempt_enable_no_resched();
  554. return 1;
  555. }
  556. static int kprobes_fault_handler(struct pt_regs *regs, int trapnr)
  557. {
  558. if (!kprobe_running())
  559. return 0;
  560. if (current_kprobe->fault_handler &&
  561. current_kprobe->fault_handler(current_kprobe, regs, trapnr))
  562. return 1;
  563. if (kprobe_status & KPROBE_HIT_SS) {
  564. resume_execution(current_kprobe, regs);
  565. unlock_kprobes();
  566. preempt_enable_no_resched();
  567. }
  568. return 0;
  569. }
  570. int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
  571. void *data)
  572. {
  573. struct die_args *args = (struct die_args *)data;
  574. switch(val) {
  575. case DIE_BREAK:
  576. if (pre_kprobes_handler(args))
  577. return NOTIFY_STOP;
  578. break;
  579. case DIE_SS:
  580. if (post_kprobes_handler(args->regs))
  581. return NOTIFY_STOP;
  582. break;
  583. case DIE_PAGE_FAULT:
  584. if (kprobes_fault_handler(args->regs, args->trapnr))
  585. return NOTIFY_STOP;
  586. default:
  587. break;
  588. }
  589. return NOTIFY_DONE;
  590. }
  591. int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  592. {
  593. struct jprobe *jp = container_of(p, struct jprobe, kp);
  594. unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
  595. /* save architectural state */
  596. jprobe_saved_regs = *regs;
  597. /* after rfi, execute the jprobe instrumented function */
  598. regs->cr_iip = addr & ~0xFULL;
  599. ia64_psr(regs)->ri = addr & 0xf;
  600. regs->r1 = ((struct fnptr *)(jp->entry))->gp;
  601. /*
  602. * fix the return address to our jprobe_inst_return() function
  603. * in the jprobes.S file
  604. */
  605. regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
  606. return 1;
  607. }
  608. int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  609. {
  610. *regs = jprobe_saved_regs;
  611. return 1;
  612. }
  613. static struct kprobe trampoline_p = {
  614. .pre_handler = trampoline_probe_handler
  615. };
  616. int __init arch_init(void)
  617. {
  618. trampoline_p.addr =
  619. (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
  620. return register_kprobe(&trampoline_p);
  621. }