kprobes.c 27 KB

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