kprobes.c 25 KB

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