kprobes.c 19 KB

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