kprobes.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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. int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  379. {
  380. regs->cr_iip = __kretprobe_trampoline_handler(regs, kretprobe_trampoline, NULL);
  381. /*
  382. * By returning a non-zero value, we are telling
  383. * kprobe_handler() that we don't want the post_handler
  384. * to run (and have re-enabled preemption)
  385. */
  386. return 1;
  387. }
  388. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  389. struct pt_regs *regs)
  390. {
  391. ri->ret_addr = (kprobe_opcode_t *)regs->b0;
  392. ri->fp = NULL;
  393. /* Replace the return addr with trampoline addr */
  394. regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip;
  395. }
  396. /* Check the instruction in the slot is break */
  397. static int __kprobes __is_ia64_break_inst(bundle_t *bundle, uint slot)
  398. {
  399. unsigned int major_opcode;
  400. unsigned int template = bundle->quad0.template;
  401. unsigned long kprobe_inst;
  402. /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
  403. if (slot == 1 && bundle_encoding[template][1] == L)
  404. slot++;
  405. /* Get Kprobe probe instruction at given slot*/
  406. get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
  407. /* For break instruction,
  408. * Bits 37:40 Major opcode to be zero
  409. * Bits 27:32 X6 to be zero
  410. * Bits 32:35 X3 to be zero
  411. */
  412. if (major_opcode || ((kprobe_inst >> 27) & 0x1FF)) {
  413. /* Not a break instruction */
  414. return 0;
  415. }
  416. /* Is a break instruction */
  417. return 1;
  418. }
  419. /*
  420. * In this function, we check whether the target bundle modifies IP or
  421. * it triggers an exception. If so, it cannot be boostable.
  422. */
  423. static int __kprobes can_boost(bundle_t *bundle, uint slot,
  424. unsigned long bundle_addr)
  425. {
  426. unsigned int template = bundle->quad0.template;
  427. do {
  428. if (search_exception_tables(bundle_addr + slot) ||
  429. __is_ia64_break_inst(bundle, slot))
  430. return 0; /* exception may occur in this bundle*/
  431. } while ((++slot) < 3);
  432. template &= 0x1e;
  433. if (template >= 0x10 /* including B unit */ ||
  434. template == 0x04 /* including X unit */ ||
  435. template == 0x06) /* undefined */
  436. return 0;
  437. return 1;
  438. }
  439. /* Prepare long jump bundle and disables other boosters if need */
  440. static void __kprobes prepare_booster(struct kprobe *p)
  441. {
  442. unsigned long addr = (unsigned long)p->addr & ~0xFULL;
  443. unsigned int slot = (unsigned long)p->addr & 0xf;
  444. struct kprobe *other_kp;
  445. if (can_boost(&p->ainsn.insn[0].bundle, slot, addr)) {
  446. set_brl_inst(&p->ainsn.insn[1].bundle, (bundle_t *)addr + 1);
  447. p->ainsn.inst_flag |= INST_FLAG_BOOSTABLE;
  448. }
  449. /* disables boosters in previous slots */
  450. for (; addr < (unsigned long)p->addr; addr++) {
  451. other_kp = get_kprobe((void *)addr);
  452. if (other_kp)
  453. other_kp->ainsn.inst_flag &= ~INST_FLAG_BOOSTABLE;
  454. }
  455. }
  456. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  457. {
  458. unsigned long addr = (unsigned long) p->addr;
  459. unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
  460. unsigned long kprobe_inst=0;
  461. unsigned int slot = addr & 0xf, template, major_opcode = 0;
  462. bundle_t *bundle;
  463. int qp;
  464. bundle = &((kprobe_opcode_t *)kprobe_addr)->bundle;
  465. template = bundle->quad0.template;
  466. if(valid_kprobe_addr(template, slot, addr))
  467. return -EINVAL;
  468. /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
  469. if (slot == 1 && bundle_encoding[template][1] == L)
  470. slot++;
  471. /* Get kprobe_inst and major_opcode from the bundle */
  472. get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
  473. qp = unsupported_inst(template, slot, major_opcode, kprobe_inst, addr);
  474. if (qp < 0)
  475. return -EINVAL;
  476. p->ainsn.insn = get_insn_slot();
  477. if (!p->ainsn.insn)
  478. return -ENOMEM;
  479. memcpy(&p->opcode, kprobe_addr, sizeof(kprobe_opcode_t));
  480. memcpy(p->ainsn.insn, kprobe_addr, sizeof(kprobe_opcode_t));
  481. prepare_break_inst(template, slot, major_opcode, kprobe_inst, p, qp);
  482. prepare_booster(p);
  483. return 0;
  484. }
  485. void __kprobes arch_arm_kprobe(struct kprobe *p)
  486. {
  487. unsigned long arm_addr;
  488. bundle_t *src, *dest;
  489. arm_addr = ((unsigned long)p->addr) & ~0xFUL;
  490. dest = &((kprobe_opcode_t *)arm_addr)->bundle;
  491. src = &p->opcode.bundle;
  492. flush_icache_range((unsigned long)p->ainsn.insn,
  493. (unsigned long)p->ainsn.insn +
  494. sizeof(kprobe_opcode_t) * MAX_INSN_SIZE);
  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_disarm_kprobe(struct kprobe *p)
  509. {
  510. unsigned long arm_addr;
  511. bundle_t *src, *dest;
  512. arm_addr = ((unsigned long)p->addr) & ~0xFUL;
  513. dest = &((kprobe_opcode_t *)arm_addr)->bundle;
  514. /* p->ainsn.insn contains the original unaltered kprobe_opcode_t */
  515. src = &p->ainsn.insn->bundle;
  516. switch (p->ainsn.slot) {
  517. case 0:
  518. dest->quad0.slot0 = src->quad0.slot0;
  519. break;
  520. case 1:
  521. dest->quad1.slot1_p1 = src->quad1.slot1_p1;
  522. break;
  523. case 2:
  524. dest->quad1.slot2 = src->quad1.slot2;
  525. break;
  526. }
  527. flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
  528. }
  529. void __kprobes arch_remove_kprobe(struct kprobe *p)
  530. {
  531. if (p->ainsn.insn) {
  532. free_insn_slot(p->ainsn.insn,
  533. p->ainsn.inst_flag & INST_FLAG_BOOSTABLE);
  534. p->ainsn.insn = NULL;
  535. }
  536. }
  537. /*
  538. * We are resuming execution after a single step fault, so the pt_regs
  539. * structure reflects the register state after we executed the instruction
  540. * located in the kprobe (p->ainsn.insn->bundle). We still need to adjust
  541. * the ip to point back to the original stack address. To set the IP address
  542. * to original stack address, handle the case where we need to fixup the
  543. * relative IP address and/or fixup branch register.
  544. */
  545. static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
  546. {
  547. unsigned long bundle_addr = (unsigned long) (&p->ainsn.insn->bundle);
  548. unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
  549. unsigned long template;
  550. int slot = ((unsigned long)p->addr & 0xf);
  551. template = p->ainsn.insn->bundle.quad0.template;
  552. if (slot == 1 && bundle_encoding[template][1] == L)
  553. slot = 2;
  554. if (p->ainsn.inst_flag & ~INST_FLAG_BOOSTABLE) {
  555. if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
  556. /* Fix relative IP address */
  557. regs->cr_iip = (regs->cr_iip - bundle_addr) +
  558. resume_addr;
  559. }
  560. if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
  561. /*
  562. * Fix target branch register, software convention is
  563. * to use either b0 or b6 or b7, so just checking
  564. * only those registers
  565. */
  566. switch (p->ainsn.target_br_reg) {
  567. case 0:
  568. if ((regs->b0 == bundle_addr) ||
  569. (regs->b0 == bundle_addr + 0x10)) {
  570. regs->b0 = (regs->b0 - bundle_addr) +
  571. resume_addr;
  572. }
  573. break;
  574. case 6:
  575. if ((regs->b6 == bundle_addr) ||
  576. (regs->b6 == bundle_addr + 0x10)) {
  577. regs->b6 = (regs->b6 - bundle_addr) +
  578. resume_addr;
  579. }
  580. break;
  581. case 7:
  582. if ((regs->b7 == bundle_addr) ||
  583. (regs->b7 == bundle_addr + 0x10)) {
  584. regs->b7 = (regs->b7 - bundle_addr) +
  585. resume_addr;
  586. }
  587. break;
  588. } /* end switch */
  589. }
  590. goto turn_ss_off;
  591. }
  592. if (slot == 2) {
  593. if (regs->cr_iip == bundle_addr + 0x10) {
  594. regs->cr_iip = resume_addr + 0x10;
  595. }
  596. } else {
  597. if (regs->cr_iip == bundle_addr) {
  598. regs->cr_iip = resume_addr;
  599. }
  600. }
  601. turn_ss_off:
  602. /* Turn off Single Step bit */
  603. ia64_psr(regs)->ss = 0;
  604. }
  605. static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs)
  606. {
  607. unsigned long bundle_addr = (unsigned long) &p->ainsn.insn->bundle;
  608. unsigned long slot = (unsigned long)p->addr & 0xf;
  609. /* single step inline if break instruction */
  610. if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)
  611. regs->cr_iip = (unsigned long)p->addr & ~0xFULL;
  612. else
  613. regs->cr_iip = bundle_addr & ~0xFULL;
  614. if (slot > 2)
  615. slot = 0;
  616. ia64_psr(regs)->ri = slot;
  617. /* turn on single stepping */
  618. ia64_psr(regs)->ss = 1;
  619. }
  620. static int __kprobes is_ia64_break_inst(struct pt_regs *regs)
  621. {
  622. unsigned int slot = ia64_psr(regs)->ri;
  623. unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip;
  624. bundle_t bundle;
  625. memcpy(&bundle, kprobe_addr, sizeof(bundle_t));
  626. return __is_ia64_break_inst(&bundle, slot);
  627. }
  628. static int __kprobes pre_kprobes_handler(struct die_args *args)
  629. {
  630. struct kprobe *p;
  631. int ret = 0;
  632. struct pt_regs *regs = args->regs;
  633. kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
  634. struct kprobe_ctlblk *kcb;
  635. /*
  636. * We don't want to be preempted for the entire
  637. * duration of kprobe processing
  638. */
  639. preempt_disable();
  640. kcb = get_kprobe_ctlblk();
  641. /* Handle recursion cases */
  642. if (kprobe_running()) {
  643. p = get_kprobe(addr);
  644. if (p) {
  645. if ((kcb->kprobe_status == KPROBE_HIT_SS) &&
  646. (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) {
  647. ia64_psr(regs)->ss = 0;
  648. goto no_kprobe;
  649. }
  650. /* We have reentered the pre_kprobe_handler(), since
  651. * another probe was hit while within the handler.
  652. * We here save the original kprobes variables and
  653. * just single step on the instruction of the new probe
  654. * without calling any user handlers.
  655. */
  656. save_previous_kprobe(kcb);
  657. set_current_kprobe(p, kcb);
  658. kprobes_inc_nmissed_count(p);
  659. prepare_ss(p, regs);
  660. kcb->kprobe_status = KPROBE_REENTER;
  661. return 1;
  662. } else if (!is_ia64_break_inst(regs)) {
  663. /* The breakpoint instruction was removed by
  664. * another cpu right after we hit, no further
  665. * handling of this interrupt is appropriate
  666. */
  667. ret = 1;
  668. goto no_kprobe;
  669. } else {
  670. /* Not our break */
  671. goto no_kprobe;
  672. }
  673. }
  674. p = get_kprobe(addr);
  675. if (!p) {
  676. if (!is_ia64_break_inst(regs)) {
  677. /*
  678. * The breakpoint instruction was removed right
  679. * after we hit it. Another cpu has removed
  680. * either a probepoint or a debugger breakpoint
  681. * at this address. In either case, no further
  682. * handling of this interrupt is appropriate.
  683. */
  684. ret = 1;
  685. }
  686. /* Not one of our break, let kernel handle it */
  687. goto no_kprobe;
  688. }
  689. set_current_kprobe(p, kcb);
  690. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  691. if (p->pre_handler && p->pre_handler(p, regs)) {
  692. reset_current_kprobe();
  693. preempt_enable_no_resched();
  694. return 1;
  695. }
  696. #if !defined(CONFIG_PREEMPT)
  697. if (p->ainsn.inst_flag == INST_FLAG_BOOSTABLE && !p->post_handler) {
  698. /* Boost up -- we can execute copied instructions directly */
  699. ia64_psr(regs)->ri = p->ainsn.slot;
  700. regs->cr_iip = (unsigned long)&p->ainsn.insn->bundle & ~0xFULL;
  701. /* turn single stepping off */
  702. ia64_psr(regs)->ss = 0;
  703. reset_current_kprobe();
  704. preempt_enable_no_resched();
  705. return 1;
  706. }
  707. #endif
  708. prepare_ss(p, regs);
  709. kcb->kprobe_status = KPROBE_HIT_SS;
  710. return 1;
  711. no_kprobe:
  712. preempt_enable_no_resched();
  713. return ret;
  714. }
  715. static int __kprobes post_kprobes_handler(struct pt_regs *regs)
  716. {
  717. struct kprobe *cur = kprobe_running();
  718. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  719. if (!cur)
  720. return 0;
  721. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  722. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  723. cur->post_handler(cur, regs, 0);
  724. }
  725. resume_execution(cur, regs);
  726. /*Restore back the original saved kprobes variables and continue. */
  727. if (kcb->kprobe_status == KPROBE_REENTER) {
  728. restore_previous_kprobe(kcb);
  729. goto out;
  730. }
  731. reset_current_kprobe();
  732. out:
  733. preempt_enable_no_resched();
  734. return 1;
  735. }
  736. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  737. {
  738. struct kprobe *cur = kprobe_running();
  739. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  740. switch(kcb->kprobe_status) {
  741. case KPROBE_HIT_SS:
  742. case KPROBE_REENTER:
  743. /*
  744. * We are here because the instruction being single
  745. * stepped caused a page fault. We reset the current
  746. * kprobe and the instruction pointer points back to
  747. * the probe address and allow the page fault handler
  748. * to continue as a normal page fault.
  749. */
  750. regs->cr_iip = ((unsigned long)cur->addr) & ~0xFULL;
  751. ia64_psr(regs)->ri = ((unsigned long)cur->addr) & 0xf;
  752. if (kcb->kprobe_status == KPROBE_REENTER)
  753. restore_previous_kprobe(kcb);
  754. else
  755. reset_current_kprobe();
  756. preempt_enable_no_resched();
  757. break;
  758. case KPROBE_HIT_ACTIVE:
  759. case KPROBE_HIT_SSDONE:
  760. /*
  761. * We increment the nmissed count for accounting,
  762. * we can also use npre/npostfault count for accounting
  763. * these specific fault cases.
  764. */
  765. kprobes_inc_nmissed_count(cur);
  766. /*
  767. * We come here because instructions in the pre/post
  768. * handler caused the page_fault, this could happen
  769. * if handler tries to access user space by
  770. * copy_from_user(), get_user() etc. Let the
  771. * user-specified handler try to fix it first.
  772. */
  773. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  774. return 1;
  775. /*
  776. * In case the user-specified fault handler returned
  777. * zero, try to fix up.
  778. */
  779. if (ia64_done_with_exception(regs))
  780. return 1;
  781. /*
  782. * Let ia64_do_page_fault() fix it.
  783. */
  784. break;
  785. default:
  786. break;
  787. }
  788. return 0;
  789. }
  790. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  791. unsigned long val, void *data)
  792. {
  793. struct die_args *args = (struct die_args *)data;
  794. int ret = NOTIFY_DONE;
  795. if (args->regs && user_mode(args->regs))
  796. return ret;
  797. switch(val) {
  798. case DIE_BREAK:
  799. /* err is break number from ia64_bad_break() */
  800. if ((args->err >> 12) == (__IA64_BREAK_KPROBE >> 12)
  801. || args->err == 0)
  802. if (pre_kprobes_handler(args))
  803. ret = NOTIFY_STOP;
  804. break;
  805. case DIE_FAULT:
  806. /* err is vector number from ia64_fault() */
  807. if (args->err == 36)
  808. if (post_kprobes_handler(args->regs))
  809. ret = NOTIFY_STOP;
  810. break;
  811. default:
  812. break;
  813. }
  814. return ret;
  815. }
  816. struct param_bsp_cfm {
  817. unsigned long ip;
  818. unsigned long *bsp;
  819. unsigned long cfm;
  820. };
  821. static void ia64_get_bsp_cfm(struct unw_frame_info *info, void *arg)
  822. {
  823. unsigned long ip;
  824. struct param_bsp_cfm *lp = arg;
  825. do {
  826. unw_get_ip(info, &ip);
  827. if (ip == 0)
  828. break;
  829. if (ip == lp->ip) {
  830. unw_get_bsp(info, (unsigned long*)&lp->bsp);
  831. unw_get_cfm(info, (unsigned long*)&lp->cfm);
  832. return;
  833. }
  834. } while (unw_unwind(info) >= 0);
  835. lp->bsp = NULL;
  836. lp->cfm = 0;
  837. return;
  838. }
  839. unsigned long arch_deref_entry_point(void *entry)
  840. {
  841. return ((struct fnptr *)entry)->ip;
  842. }
  843. static struct kprobe trampoline_p = {
  844. .pre_handler = trampoline_probe_handler
  845. };
  846. int __init arch_init_kprobes(void)
  847. {
  848. trampoline_p.addr =
  849. (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
  850. return register_kprobe(&trampoline_p);
  851. }
  852. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  853. {
  854. if (p->addr ==
  855. (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip)
  856. return 1;
  857. return 0;
  858. }