kprobes.c 27 KB

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