kprobes.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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. #include <asm/uaccess.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 void __kprobes 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 int __kprobes 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 void __kprobes 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 void __kprobes 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 void __kprobes 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 flags, orig_ret_address = 0;
  313. unsigned long trampoline_address =
  314. ((struct fnptr *)kretprobe_trampoline)->ip;
  315. spin_lock_irqsave(&kretprobe_lock, flags);
  316. head = kretprobe_inst_table_head(current);
  317. /*
  318. * It is possible to have multiple instances associated with a given
  319. * task either because an multiple functions in the call path
  320. * have a return probe installed on them, and/or more then one return
  321. * return probe was registered for a target function.
  322. *
  323. * We can handle this because:
  324. * - instances are always inserted at the head of the list
  325. * - when multiple return probes are registered for the same
  326. * function, the first instance's ret_addr will point to the
  327. * real return address, and all the rest will point to
  328. * kretprobe_trampoline
  329. */
  330. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  331. if (ri->task != current)
  332. /* another task is sharing our hash bucket */
  333. continue;
  334. if (ri->rp && ri->rp->handler)
  335. ri->rp->handler(ri, regs);
  336. orig_ret_address = (unsigned long)ri->ret_addr;
  337. recycle_rp_inst(ri);
  338. if (orig_ret_address != trampoline_address)
  339. /*
  340. * This is the real return address. Any other
  341. * instances associated with this task are for
  342. * other calls deeper on the call stack
  343. */
  344. break;
  345. }
  346. BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
  347. regs->cr_iip = orig_ret_address;
  348. reset_current_kprobe();
  349. spin_unlock_irqrestore(&kretprobe_lock, flags);
  350. preempt_enable_no_resched();
  351. /*
  352. * By returning a non-zero value, we are telling
  353. * kprobe_handler() that we don't want the post_handler
  354. * to run (and have re-enabled preemption)
  355. */
  356. return 1;
  357. }
  358. /* Called with kretprobe_lock held */
  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. /*
  412. * We are resuming execution after a single step fault, so the pt_regs
  413. * structure reflects the register state after we executed the instruction
  414. * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
  415. * the ip to point back to the original stack address. To set the IP address
  416. * to original stack address, handle the case where we need to fixup the
  417. * relative IP address and/or fixup branch register.
  418. */
  419. static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
  420. {
  421. unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL;
  422. unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
  423. unsigned long template;
  424. int slot = ((unsigned long)p->addr & 0xf);
  425. template = p->opcode.bundle.quad0.template;
  426. if (slot == 1 && bundle_encoding[template][1] == L)
  427. slot = 2;
  428. if (p->ainsn.inst_flag) {
  429. if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
  430. /* Fix relative IP address */
  431. regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr;
  432. }
  433. if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
  434. /*
  435. * Fix target branch register, software convention is
  436. * to use either b0 or b6 or b7, so just checking
  437. * only those registers
  438. */
  439. switch (p->ainsn.target_br_reg) {
  440. case 0:
  441. if ((regs->b0 == bundle_addr) ||
  442. (regs->b0 == bundle_addr + 0x10)) {
  443. regs->b0 = (regs->b0 - bundle_addr) +
  444. resume_addr;
  445. }
  446. break;
  447. case 6:
  448. if ((regs->b6 == bundle_addr) ||
  449. (regs->b6 == bundle_addr + 0x10)) {
  450. regs->b6 = (regs->b6 - bundle_addr) +
  451. resume_addr;
  452. }
  453. break;
  454. case 7:
  455. if ((regs->b7 == bundle_addr) ||
  456. (regs->b7 == bundle_addr + 0x10)) {
  457. regs->b7 = (regs->b7 - bundle_addr) +
  458. resume_addr;
  459. }
  460. break;
  461. } /* end switch */
  462. }
  463. goto turn_ss_off;
  464. }
  465. if (slot == 2) {
  466. if (regs->cr_iip == bundle_addr + 0x10) {
  467. regs->cr_iip = resume_addr + 0x10;
  468. }
  469. } else {
  470. if (regs->cr_iip == bundle_addr) {
  471. regs->cr_iip = resume_addr;
  472. }
  473. }
  474. turn_ss_off:
  475. /* Turn off Single Step bit */
  476. ia64_psr(regs)->ss = 0;
  477. }
  478. static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs)
  479. {
  480. unsigned long bundle_addr = (unsigned long) &p->opcode.bundle;
  481. unsigned long slot = (unsigned long)p->addr & 0xf;
  482. /* single step inline if break instruction */
  483. if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)
  484. regs->cr_iip = (unsigned long)p->addr & ~0xFULL;
  485. else
  486. regs->cr_iip = bundle_addr & ~0xFULL;
  487. if (slot > 2)
  488. slot = 0;
  489. ia64_psr(regs)->ri = slot;
  490. /* turn on single stepping */
  491. ia64_psr(regs)->ss = 1;
  492. }
  493. static int __kprobes is_ia64_break_inst(struct pt_regs *regs)
  494. {
  495. unsigned int slot = ia64_psr(regs)->ri;
  496. unsigned int template, major_opcode;
  497. unsigned long kprobe_inst;
  498. unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip;
  499. bundle_t bundle;
  500. memcpy(&bundle, kprobe_addr, sizeof(bundle_t));
  501. template = bundle.quad0.template;
  502. /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
  503. if (slot == 1 && bundle_encoding[template][1] == L)
  504. slot++;
  505. /* Get Kprobe probe instruction at given slot*/
  506. get_kprobe_inst(&bundle, slot, &kprobe_inst, &major_opcode);
  507. /* For break instruction,
  508. * Bits 37:40 Major opcode to be zero
  509. * Bits 27:32 X6 to be zero
  510. * Bits 32:35 X3 to be zero
  511. */
  512. if (major_opcode || ((kprobe_inst >> 27) & 0x1FF) ) {
  513. /* Not a break instruction */
  514. return 0;
  515. }
  516. /* Is a break instruction */
  517. return 1;
  518. }
  519. static int __kprobes pre_kprobes_handler(struct die_args *args)
  520. {
  521. struct kprobe *p;
  522. int ret = 0;
  523. struct pt_regs *regs = args->regs;
  524. kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
  525. struct kprobe_ctlblk *kcb;
  526. /*
  527. * We don't want to be preempted for the entire
  528. * duration of kprobe processing
  529. */
  530. preempt_disable();
  531. kcb = get_kprobe_ctlblk();
  532. /* Handle recursion cases */
  533. if (kprobe_running()) {
  534. p = get_kprobe(addr);
  535. if (p) {
  536. if ((kcb->kprobe_status == KPROBE_HIT_SS) &&
  537. (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) {
  538. ia64_psr(regs)->ss = 0;
  539. goto no_kprobe;
  540. }
  541. /* We have reentered the pre_kprobe_handler(), since
  542. * another probe was hit while within the handler.
  543. * We here save the original kprobes variables and
  544. * just single step on the instruction of the new probe
  545. * without calling any user handlers.
  546. */
  547. save_previous_kprobe(kcb);
  548. set_current_kprobe(p, kcb);
  549. kprobes_inc_nmissed_count(p);
  550. prepare_ss(p, regs);
  551. kcb->kprobe_status = KPROBE_REENTER;
  552. return 1;
  553. } else if (args->err == __IA64_BREAK_JPROBE) {
  554. /*
  555. * jprobe instrumented function just completed
  556. */
  557. p = __get_cpu_var(current_kprobe);
  558. if (p->break_handler && p->break_handler(p, regs)) {
  559. goto ss_probe;
  560. }
  561. } else if (!is_ia64_break_inst(regs)) {
  562. /* The breakpoint instruction was removed by
  563. * another cpu right after we hit, no further
  564. * handling of this interrupt is appropriate
  565. */
  566. ret = 1;
  567. goto no_kprobe;
  568. } else {
  569. /* Not our break */
  570. goto no_kprobe;
  571. }
  572. }
  573. p = get_kprobe(addr);
  574. if (!p) {
  575. if (!is_ia64_break_inst(regs)) {
  576. /*
  577. * The breakpoint instruction was removed right
  578. * after we hit it. Another cpu has removed
  579. * either a probepoint or a debugger breakpoint
  580. * at this address. In either case, no further
  581. * handling of this interrupt is appropriate.
  582. */
  583. ret = 1;
  584. }
  585. /* Not one of our break, let kernel handle it */
  586. goto no_kprobe;
  587. }
  588. set_current_kprobe(p, kcb);
  589. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  590. if (p->pre_handler && p->pre_handler(p, regs))
  591. /*
  592. * Our pre-handler is specifically requesting that we just
  593. * do a return. This is used for both the jprobe pre-handler
  594. * and the kretprobe trampoline
  595. */
  596. return 1;
  597. ss_probe:
  598. prepare_ss(p, regs);
  599. kcb->kprobe_status = KPROBE_HIT_SS;
  600. return 1;
  601. no_kprobe:
  602. preempt_enable_no_resched();
  603. return ret;
  604. }
  605. static int __kprobes post_kprobes_handler(struct pt_regs *regs)
  606. {
  607. struct kprobe *cur = kprobe_running();
  608. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  609. if (!cur)
  610. return 0;
  611. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  612. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  613. cur->post_handler(cur, regs, 0);
  614. }
  615. resume_execution(cur, regs);
  616. /*Restore back the original saved kprobes variables and continue. */
  617. if (kcb->kprobe_status == KPROBE_REENTER) {
  618. restore_previous_kprobe(kcb);
  619. goto out;
  620. }
  621. reset_current_kprobe();
  622. out:
  623. preempt_enable_no_resched();
  624. return 1;
  625. }
  626. static int __kprobes kprobes_fault_handler(struct pt_regs *regs, int trapnr)
  627. {
  628. struct kprobe *cur = kprobe_running();
  629. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  630. switch(kcb->kprobe_status) {
  631. case KPROBE_HIT_SS:
  632. case KPROBE_REENTER:
  633. /*
  634. * We are here because the instruction being single
  635. * stepped caused a page fault. We reset the current
  636. * kprobe and the instruction pointer points back to
  637. * the probe address and allow the page fault handler
  638. * to continue as a normal page fault.
  639. */
  640. regs->cr_iip = ((unsigned long)cur->addr) & ~0xFULL;
  641. ia64_psr(regs)->ri = ((unsigned long)cur->addr) & 0xf;
  642. if (kcb->kprobe_status == KPROBE_REENTER)
  643. restore_previous_kprobe(kcb);
  644. else
  645. reset_current_kprobe();
  646. preempt_enable_no_resched();
  647. break;
  648. case KPROBE_HIT_ACTIVE:
  649. case KPROBE_HIT_SSDONE:
  650. /*
  651. * We increment the nmissed count for accounting,
  652. * we can also use npre/npostfault count for accouting
  653. * these specific fault cases.
  654. */
  655. kprobes_inc_nmissed_count(cur);
  656. /*
  657. * We come here because instructions in the pre/post
  658. * handler caused the page_fault, this could happen
  659. * if handler tries to access user space by
  660. * copy_from_user(), get_user() etc. Let the
  661. * user-specified handler try to fix it first.
  662. */
  663. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  664. return 1;
  665. /*
  666. * Let ia64_do_page_fault() fix it.
  667. */
  668. break;
  669. default:
  670. break;
  671. }
  672. return 0;
  673. }
  674. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  675. unsigned long val, void *data)
  676. {
  677. struct die_args *args = (struct die_args *)data;
  678. int ret = NOTIFY_DONE;
  679. if (args->regs && user_mode(args->regs))
  680. return ret;
  681. switch(val) {
  682. case DIE_BREAK:
  683. /* err is break number from ia64_bad_break() */
  684. if (args->err == 0x80200 || args->err == 0x80300 || args->err == 0)
  685. if (pre_kprobes_handler(args))
  686. ret = NOTIFY_STOP;
  687. break;
  688. case DIE_FAULT:
  689. /* err is vector number from ia64_fault() */
  690. if (args->err == 36)
  691. if (post_kprobes_handler(args->regs))
  692. ret = NOTIFY_STOP;
  693. break;
  694. case DIE_PAGE_FAULT:
  695. /* kprobe_running() needs smp_processor_id() */
  696. preempt_disable();
  697. if (kprobe_running() &&
  698. kprobes_fault_handler(args->regs, args->trapnr))
  699. ret = NOTIFY_STOP;
  700. preempt_enable();
  701. default:
  702. break;
  703. }
  704. return ret;
  705. }
  706. struct param_bsp_cfm {
  707. unsigned long ip;
  708. unsigned long *bsp;
  709. unsigned long cfm;
  710. };
  711. static void ia64_get_bsp_cfm(struct unw_frame_info *info, void *arg)
  712. {
  713. unsigned long ip;
  714. struct param_bsp_cfm *lp = arg;
  715. do {
  716. unw_get_ip(info, &ip);
  717. if (ip == 0)
  718. break;
  719. if (ip == lp->ip) {
  720. unw_get_bsp(info, (unsigned long*)&lp->bsp);
  721. unw_get_cfm(info, (unsigned long*)&lp->cfm);
  722. return;
  723. }
  724. } while (unw_unwind(info) >= 0);
  725. lp->bsp = 0;
  726. lp->cfm = 0;
  727. return;
  728. }
  729. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  730. {
  731. struct jprobe *jp = container_of(p, struct jprobe, kp);
  732. unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
  733. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  734. struct param_bsp_cfm pa;
  735. int bytes;
  736. /*
  737. * Callee owns the argument space and could overwrite it, eg
  738. * tail call optimization. So to be absolutely safe
  739. * we save the argument space before transfering the control
  740. * to instrumented jprobe function which runs in
  741. * the process context
  742. */
  743. pa.ip = regs->cr_iip;
  744. unw_init_running(ia64_get_bsp_cfm, &pa);
  745. bytes = (char *)ia64_rse_skip_regs(pa.bsp, pa.cfm & 0x3f)
  746. - (char *)pa.bsp;
  747. memcpy( kcb->jprobes_saved_stacked_regs,
  748. pa.bsp,
  749. bytes );
  750. kcb->bsp = pa.bsp;
  751. kcb->cfm = pa.cfm;
  752. /* save architectural state */
  753. kcb->jprobe_saved_regs = *regs;
  754. /* after rfi, execute the jprobe instrumented function */
  755. regs->cr_iip = addr & ~0xFULL;
  756. ia64_psr(regs)->ri = addr & 0xf;
  757. regs->r1 = ((struct fnptr *)(jp->entry))->gp;
  758. /*
  759. * fix the return address to our jprobe_inst_return() function
  760. * in the jprobes.S file
  761. */
  762. regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
  763. return 1;
  764. }
  765. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  766. {
  767. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  768. int bytes;
  769. /* restoring architectural state */
  770. *regs = kcb->jprobe_saved_regs;
  771. /* restoring the original argument space */
  772. flush_register_stack();
  773. bytes = (char *)ia64_rse_skip_regs(kcb->bsp, kcb->cfm & 0x3f)
  774. - (char *)kcb->bsp;
  775. memcpy( kcb->bsp,
  776. kcb->jprobes_saved_stacked_regs,
  777. bytes );
  778. invalidate_stacked_regs();
  779. preempt_enable_no_resched();
  780. return 1;
  781. }
  782. static struct kprobe trampoline_p = {
  783. .pre_handler = trampoline_probe_handler
  784. };
  785. int __init arch_init_kprobes(void)
  786. {
  787. trampoline_p.addr =
  788. (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
  789. return register_kprobe(&trampoline_p);
  790. }