kprobes.c 23 KB

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