kprobes.c 21 KB

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