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