kprobes.c 18 KB

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