kprobes.c 21 KB

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