kprobes.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Kernel Probes (KProbes)
  4. * arch/ia64/kernel/kprobes.c
  5. *
  6. * Copyright (C) IBM Corporation, 2002, 2004
  7. * Copyright (C) Intel Corporation, 2005
  8. *
  9. * 2005-Apr Rusty Lynch <[email protected]> and Anil S Keshavamurthy
  10. * <[email protected]> adapted from i386
  11. */
  12. #include <linux/kprobes.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/string.h>
  15. #include <linux/slab.h>
  16. #include <linux/preempt.h>
  17. #include <linux/extable.h>
  18. #include <linux/kdebug.h>
  19. #include <linux/pgtable.h>
  20. #include <asm/sections.h>
  21. #include <asm/exception.h>
  22. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  23. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  24. struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
  25. enum instruction_type {A, I, M, F, B, L, X, u};
  26. static enum instruction_type bundle_encoding[32][3] = {
  27. [0x00] = { M, I, I },
  28. [0x01] = { M, I, I },
  29. [0x02] = { M, I, I },
  30. [0x03] = { M, I, I },
  31. [0x04] = { M, L, X },
  32. [0x05] = { M, L, X },
  33. [0x06] = { u, u, u },
  34. [0x07] = { u, u, u },
  35. [0x08] = { M, M, I },
  36. [0x09] = { M, M, I },
  37. [0x0A] = { M, M, I },
  38. [0x0B] = { M, M, I },
  39. [0x0C] = { M, F, I },
  40. [0x0D] = { M, F, I },
  41. [0x0E] = { M, M, F },
  42. [0x0F] = { M, M, F },
  43. [0x10] = { M, I, B },
  44. [0x11] = { M, I, B },
  45. [0x12] = { M, B, B },
  46. [0x13] = { M, B, B },
  47. [0x14] = { u, u, u },
  48. [0x15] = { u, u, u },
  49. [0x16] = { B, B, B },
  50. [0x17] = { B, B, B },
  51. [0x18] = { M, M, B },
  52. [0x19] = { M, M, B },
  53. [0x1A] = { u, u, u },
  54. [0x1B] = { u, u, u },
  55. [0x1C] = { M, F, B },
  56. [0x1D] = { M, F, B },
  57. [0x1E] = { u, u, u },
  58. [0x1F] = { u, u, u },
  59. };
  60. /* Insert a long branch code */
  61. static void __kprobes set_brl_inst(void *from, void *to)
  62. {
  63. s64 rel = ((s64) to - (s64) from) >> 4;
  64. bundle_t *brl;
  65. brl = (bundle_t *) ((u64) from & ~0xf);
  66. brl->quad0.template = 0x05; /* [MLX](stop) */
  67. brl->quad0.slot0 = NOP_M_INST; /* nop.m 0x0 */
  68. brl->quad0.slot1_p0 = ((rel >> 20) & 0x7fffffffff) << 2;
  69. brl->quad1.slot1_p1 = (((rel >> 20) & 0x7fffffffff) << 2) >> (64 - 46);
  70. /* brl.cond.sptk.many.clr rel<<4 (qp=0) */
  71. brl->quad1.slot2 = BRL_INST(rel >> 59, rel & 0xfffff);
  72. }
  73. /*
  74. * In this function we check to see if the instruction
  75. * is IP relative instruction and update the kprobe
  76. * inst flag accordingly
  77. */
  78. static void __kprobes update_kprobe_inst_flag(uint template, uint slot,
  79. uint major_opcode,
  80. unsigned long kprobe_inst,
  81. struct kprobe *p)
  82. {
  83. p->ainsn.inst_flag = 0;
  84. p->ainsn.target_br_reg = 0;
  85. p->ainsn.slot = slot;
  86. /* Check for Break instruction
  87. * Bits 37:40 Major opcode to be zero
  88. * Bits 27:32 X6 to be zero
  89. * Bits 32:35 X3 to be zero
  90. */
  91. if ((!major_opcode) && (!((kprobe_inst >> 27) & 0x1FF)) ) {
  92. /* is a break instruction */
  93. p->ainsn.inst_flag |= INST_FLAG_BREAK_INST;
  94. return;
  95. }
  96. if (bundle_encoding[template][slot] == B) {
  97. switch (major_opcode) {
  98. case INDIRECT_CALL_OPCODE:
  99. p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
  100. p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
  101. break;
  102. case IP_RELATIVE_PREDICT_OPCODE:
  103. case IP_RELATIVE_BRANCH_OPCODE:
  104. p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
  105. break;
  106. case IP_RELATIVE_CALL_OPCODE:
  107. p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
  108. p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
  109. p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
  110. break;
  111. }
  112. } else if (bundle_encoding[template][slot] == X) {
  113. switch (major_opcode) {
  114. case LONG_CALL_OPCODE:
  115. p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
  116. p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
  117. break;
  118. }
  119. }
  120. return;
  121. }
  122. /*
  123. * In this function we check to see if the instruction
  124. * (qp) cmpx.crel.ctype p1,p2=r2,r3
  125. * on which we are inserting kprobe is cmp instruction
  126. * with ctype as unc.
  127. */
  128. static uint __kprobes is_cmp_ctype_unc_inst(uint template, uint slot,
  129. uint major_opcode,
  130. unsigned long kprobe_inst)
  131. {
  132. cmp_inst_t cmp_inst;
  133. uint ctype_unc = 0;
  134. if (!((bundle_encoding[template][slot] == I) ||
  135. (bundle_encoding[template][slot] == M)))
  136. goto out;
  137. if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
  138. (major_opcode == 0xE)))
  139. goto out;
  140. cmp_inst.l = kprobe_inst;
  141. if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {
  142. /* Integer compare - Register Register (A6 type)*/
  143. if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)
  144. &&(cmp_inst.f.c == 1))
  145. ctype_unc = 1;
  146. } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {
  147. /* Integer compare - Immediate Register (A8 type)*/
  148. if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))
  149. ctype_unc = 1;
  150. }
  151. out:
  152. return ctype_unc;
  153. }
  154. /*
  155. * In this function we check to see if the instruction
  156. * on which we are inserting kprobe is supported.
  157. * Returns qp value if supported
  158. * Returns -EINVAL if unsupported
  159. */
  160. static int __kprobes unsupported_inst(uint template, uint slot,
  161. uint major_opcode,
  162. unsigned long kprobe_inst,
  163. unsigned long addr)
  164. {
  165. int qp;
  166. qp = kprobe_inst & 0x3f;
  167. if (is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst)) {
  168. if (slot == 1 && qp) {
  169. printk(KERN_WARNING "Kprobes on cmp unc "
  170. "instruction on slot 1 at <0x%lx> "
  171. "is not supported\n", addr);
  172. return -EINVAL;
  173. }
  174. qp = 0;
  175. }
  176. else if (bundle_encoding[template][slot] == I) {
  177. if (major_opcode == 0) {
  178. /*
  179. * Check for Integer speculation instruction
  180. * - Bit 33-35 to be equal to 0x1
  181. */
  182. if (((kprobe_inst >> 33) & 0x7) == 1) {
  183. printk(KERN_WARNING
  184. "Kprobes on speculation inst at <0x%lx> not supported\n",
  185. addr);
  186. return -EINVAL;
  187. }
  188. /*
  189. * IP relative mov instruction
  190. * - Bit 27-35 to be equal to 0x30
  191. */
  192. if (((kprobe_inst >> 27) & 0x1FF) == 0x30) {
  193. printk(KERN_WARNING
  194. "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
  195. addr);
  196. return -EINVAL;
  197. }
  198. }
  199. else if ((major_opcode == 5) && !(kprobe_inst & (0xFUl << 33)) &&
  200. (kprobe_inst & (0x1UL << 12))) {
  201. /* test bit instructions, tbit,tnat,tf
  202. * bit 33-36 to be equal to 0
  203. * bit 12 to be equal to 1
  204. */
  205. if (slot == 1 && qp) {
  206. printk(KERN_WARNING "Kprobes on test bit "
  207. "instruction on slot at <0x%lx> "
  208. "is not supported\n", addr);
  209. return -EINVAL;
  210. }
  211. qp = 0;
  212. }
  213. }
  214. else if (bundle_encoding[template][slot] == B) {
  215. if (major_opcode == 7) {
  216. /* IP-Relative Predict major code is 7 */
  217. printk(KERN_WARNING "Kprobes on IP-Relative"
  218. "Predict is not supported\n");
  219. return -EINVAL;
  220. }
  221. else if (major_opcode == 2) {
  222. /* Indirect Predict, major code is 2
  223. * bit 27-32 to be equal to 10 or 11
  224. */
  225. int x6=(kprobe_inst >> 27) & 0x3F;
  226. if ((x6 == 0x10) || (x6 == 0x11)) {
  227. printk(KERN_WARNING "Kprobes on "
  228. "Indirect Predict is not supported\n");
  229. return -EINVAL;
  230. }
  231. }
  232. }
  233. /* kernel does not use float instruction, here for safety kprobe
  234. * will judge whether it is fcmp/flass/float approximation instruction
  235. */
  236. else if (unlikely(bundle_encoding[template][slot] == F)) {
  237. if ((major_opcode == 4 || major_opcode == 5) &&
  238. (kprobe_inst & (0x1 << 12))) {
  239. /* fcmp/fclass unc instruction */
  240. if (slot == 1 && qp) {
  241. printk(KERN_WARNING "Kprobes on fcmp/fclass "
  242. "instruction on slot at <0x%lx> "
  243. "is not supported\n", addr);
  244. return -EINVAL;
  245. }
  246. qp = 0;
  247. }
  248. if ((major_opcode == 0 || major_opcode == 1) &&
  249. (kprobe_inst & (0x1UL << 33))) {
  250. /* float Approximation instruction */
  251. if (slot == 1 && qp) {
  252. printk(KERN_WARNING "Kprobes on float Approx "
  253. "instr at <0x%lx> is not supported\n",
  254. addr);
  255. return -EINVAL;
  256. }
  257. qp = 0;
  258. }
  259. }
  260. return qp;
  261. }
  262. /*
  263. * In this function we override the bundle with
  264. * the break instruction at the given slot.
  265. */
  266. static void __kprobes prepare_break_inst(uint template, uint slot,
  267. uint major_opcode,
  268. unsigned long kprobe_inst,
  269. struct kprobe *p,
  270. int qp)
  271. {
  272. unsigned long break_inst = BREAK_INST;
  273. bundle_t *bundle = &p->opcode.bundle;
  274. /*
  275. * Copy the original kprobe_inst qualifying predicate(qp)
  276. * to the break instruction
  277. */
  278. break_inst |= qp;
  279. switch (slot) {
  280. case 0:
  281. bundle->quad0.slot0 = break_inst;
  282. break;
  283. case 1:
  284. bundle->quad0.slot1_p0 = break_inst;
  285. bundle->quad1.slot1_p1 = break_inst >> (64-46);
  286. break;
  287. case 2:
  288. bundle->quad1.slot2 = break_inst;
  289. break;
  290. }
  291. /*
  292. * Update the instruction flag, so that we can
  293. * emulate the instruction properly after we
  294. * single step on original instruction
  295. */
  296. update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
  297. }
  298. static void __kprobes get_kprobe_inst(bundle_t *bundle, uint slot,
  299. unsigned long *kprobe_inst, uint *major_opcode)
  300. {
  301. unsigned long kprobe_inst_p0, kprobe_inst_p1;
  302. unsigned int template;
  303. template = bundle->quad0.template;
  304. switch (slot) {
  305. case 0:
  306. *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
  307. *kprobe_inst = bundle->quad0.slot0;
  308. break;
  309. case 1:
  310. *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
  311. kprobe_inst_p0 = bundle->quad0.slot1_p0;
  312. kprobe_inst_p1 = bundle->quad1.slot1_p1;
  313. *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
  314. break;
  315. case 2:
  316. *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
  317. *kprobe_inst = bundle->quad1.slot2;
  318. break;
  319. }
  320. }
  321. /* Returns non-zero if the addr is in the Interrupt Vector Table */
  322. static int __kprobes in_ivt_functions(unsigned long addr)
  323. {
  324. return (addr >= (unsigned long)__start_ivt_text
  325. && addr < (unsigned long)__end_ivt_text);
  326. }
  327. static int __kprobes valid_kprobe_addr(int template, int slot,
  328. unsigned long addr)
  329. {
  330. if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
  331. printk(KERN_WARNING "Attempting to insert unaligned kprobe "
  332. "at 0x%lx\n", addr);
  333. return -EINVAL;
  334. }
  335. if (in_ivt_functions(addr)) {
  336. printk(KERN_WARNING "Kprobes can't be inserted inside "
  337. "IVT functions at 0x%lx\n", addr);
  338. return -EINVAL;
  339. }
  340. return 0;
  341. }
  342. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  343. {
  344. unsigned int i;
  345. i = atomic_add_return(1, &kcb->prev_kprobe_index);
  346. kcb->prev_kprobe[i-1].kp = kprobe_running();
  347. kcb->prev_kprobe[i-1].status = kcb->kprobe_status;
  348. }
  349. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  350. {
  351. unsigned int i;
  352. i = atomic_read(&kcb->prev_kprobe_index);
  353. __this_cpu_write(current_kprobe, kcb->prev_kprobe[i-1].kp);
  354. kcb->kprobe_status = kcb->prev_kprobe[i-1].status;
  355. atomic_sub(1, &kcb->prev_kprobe_index);
  356. }
  357. static void __kprobes set_current_kprobe(struct kprobe *p,
  358. struct kprobe_ctlblk *kcb)
  359. {
  360. __this_cpu_write(current_kprobe, p);
  361. }
  362. void __kretprobe_trampoline(void)
  363. {
  364. }
  365. int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  366. {
  367. regs->cr_iip = __kretprobe_trampoline_handler(regs, NULL);
  368. /*
  369. * By returning a non-zero value, we are telling
  370. * kprobe_handler() that we don't want the post_handler
  371. * to run (and have re-enabled preemption)
  372. */
  373. return 1;
  374. }
  375. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  376. struct pt_regs *regs)
  377. {
  378. ri->ret_addr = (kprobe_opcode_t *)regs->b0;
  379. ri->fp = NULL;
  380. /* Replace the return addr with trampoline addr */
  381. regs->b0 = (unsigned long)dereference_function_descriptor(__kretprobe_trampoline);
  382. }
  383. /* Check the instruction in the slot is break */
  384. static int __kprobes __is_ia64_break_inst(bundle_t *bundle, uint slot)
  385. {
  386. unsigned int major_opcode;
  387. unsigned int template = bundle->quad0.template;
  388. unsigned long kprobe_inst;
  389. /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
  390. if (slot == 1 && bundle_encoding[template][1] == L)
  391. slot++;
  392. /* Get Kprobe probe instruction at given slot*/
  393. get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
  394. /* For break instruction,
  395. * Bits 37:40 Major opcode to be zero
  396. * Bits 27:32 X6 to be zero
  397. * Bits 32:35 X3 to be zero
  398. */
  399. if (major_opcode || ((kprobe_inst >> 27) & 0x1FF)) {
  400. /* Not a break instruction */
  401. return 0;
  402. }
  403. /* Is a break instruction */
  404. return 1;
  405. }
  406. /*
  407. * In this function, we check whether the target bundle modifies IP or
  408. * it triggers an exception. If so, it cannot be boostable.
  409. */
  410. static int __kprobes can_boost(bundle_t *bundle, uint slot,
  411. unsigned long bundle_addr)
  412. {
  413. unsigned int template = bundle->quad0.template;
  414. do {
  415. if (search_exception_tables(bundle_addr + slot) ||
  416. __is_ia64_break_inst(bundle, slot))
  417. return 0; /* exception may occur in this bundle*/
  418. } while ((++slot) < 3);
  419. template &= 0x1e;
  420. if (template >= 0x10 /* including B unit */ ||
  421. template == 0x04 /* including X unit */ ||
  422. template == 0x06) /* undefined */
  423. return 0;
  424. return 1;
  425. }
  426. /* Prepare long jump bundle and disables other boosters if need */
  427. static void __kprobes prepare_booster(struct kprobe *p)
  428. {
  429. unsigned long addr = (unsigned long)p->addr & ~0xFULL;
  430. unsigned int slot = (unsigned long)p->addr & 0xf;
  431. struct kprobe *other_kp;
  432. if (can_boost(&p->ainsn.insn[0].bundle, slot, addr)) {
  433. set_brl_inst(&p->ainsn.insn[1].bundle, (bundle_t *)addr + 1);
  434. p->ainsn.inst_flag |= INST_FLAG_BOOSTABLE;
  435. }
  436. /* disables boosters in previous slots */
  437. for (; addr < (unsigned long)p->addr; addr++) {
  438. other_kp = get_kprobe((void *)addr);
  439. if (other_kp)
  440. other_kp->ainsn.inst_flag &= ~INST_FLAG_BOOSTABLE;
  441. }
  442. }
  443. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  444. {
  445. unsigned long addr = (unsigned long) p->addr;
  446. unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
  447. unsigned long kprobe_inst=0;
  448. unsigned int slot = addr & 0xf, template, major_opcode = 0;
  449. bundle_t *bundle;
  450. int qp;
  451. bundle = &((kprobe_opcode_t *)kprobe_addr)->bundle;
  452. template = bundle->quad0.template;
  453. if(valid_kprobe_addr(template, slot, addr))
  454. return -EINVAL;
  455. /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
  456. if (slot == 1 && bundle_encoding[template][1] == L)
  457. slot++;
  458. /* Get kprobe_inst and major_opcode from the bundle */
  459. get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
  460. qp = unsupported_inst(template, slot, major_opcode, kprobe_inst, addr);
  461. if (qp < 0)
  462. return -EINVAL;
  463. p->ainsn.insn = get_insn_slot();
  464. if (!p->ainsn.insn)
  465. return -ENOMEM;
  466. memcpy(&p->opcode, kprobe_addr, sizeof(kprobe_opcode_t));
  467. memcpy(p->ainsn.insn, kprobe_addr, sizeof(kprobe_opcode_t));
  468. prepare_break_inst(template, slot, major_opcode, kprobe_inst, p, qp);
  469. prepare_booster(p);
  470. return 0;
  471. }
  472. void __kprobes arch_arm_kprobe(struct kprobe *p)
  473. {
  474. unsigned long arm_addr;
  475. bundle_t *src, *dest;
  476. arm_addr = ((unsigned long)p->addr) & ~0xFUL;
  477. dest = &((kprobe_opcode_t *)arm_addr)->bundle;
  478. src = &p->opcode.bundle;
  479. flush_icache_range((unsigned long)p->ainsn.insn,
  480. (unsigned long)p->ainsn.insn +
  481. sizeof(kprobe_opcode_t) * MAX_INSN_SIZE);
  482. switch (p->ainsn.slot) {
  483. case 0:
  484. dest->quad0.slot0 = src->quad0.slot0;
  485. break;
  486. case 1:
  487. dest->quad1.slot1_p1 = src->quad1.slot1_p1;
  488. break;
  489. case 2:
  490. dest->quad1.slot2 = src->quad1.slot2;
  491. break;
  492. }
  493. flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
  494. }
  495. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  496. {
  497. unsigned long arm_addr;
  498. bundle_t *src, *dest;
  499. arm_addr = ((unsigned long)p->addr) & ~0xFUL;
  500. dest = &((kprobe_opcode_t *)arm_addr)->bundle;
  501. /* p->ainsn.insn contains the original unaltered kprobe_opcode_t */
  502. src = &p->ainsn.insn->bundle;
  503. switch (p->ainsn.slot) {
  504. case 0:
  505. dest->quad0.slot0 = src->quad0.slot0;
  506. break;
  507. case 1:
  508. dest->quad1.slot1_p1 = src->quad1.slot1_p1;
  509. break;
  510. case 2:
  511. dest->quad1.slot2 = src->quad1.slot2;
  512. break;
  513. }
  514. flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
  515. }
  516. void __kprobes arch_remove_kprobe(struct kprobe *p)
  517. {
  518. if (p->ainsn.insn) {
  519. free_insn_slot(p->ainsn.insn,
  520. p->ainsn.inst_flag & INST_FLAG_BOOSTABLE);
  521. p->ainsn.insn = NULL;
  522. }
  523. }
  524. /*
  525. * We are resuming execution after a single step fault, so the pt_regs
  526. * structure reflects the register state after we executed the instruction
  527. * located in the kprobe (p->ainsn.insn->bundle). We still need to adjust
  528. * the ip to point back to the original stack address. To set the IP address
  529. * to original stack address, handle the case where we need to fixup the
  530. * relative IP address and/or fixup branch register.
  531. */
  532. static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
  533. {
  534. unsigned long bundle_addr = (unsigned long) (&p->ainsn.insn->bundle);
  535. unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
  536. unsigned long template;
  537. int slot = ((unsigned long)p->addr & 0xf);
  538. template = p->ainsn.insn->bundle.quad0.template;
  539. if (slot == 1 && bundle_encoding[template][1] == L)
  540. slot = 2;
  541. if (p->ainsn.inst_flag & ~INST_FLAG_BOOSTABLE) {
  542. if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
  543. /* Fix relative IP address */
  544. regs->cr_iip = (regs->cr_iip - bundle_addr) +
  545. resume_addr;
  546. }
  547. if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
  548. /*
  549. * Fix target branch register, software convention is
  550. * to use either b0 or b6 or b7, so just checking
  551. * only those registers
  552. */
  553. switch (p->ainsn.target_br_reg) {
  554. case 0:
  555. if ((regs->b0 == bundle_addr) ||
  556. (regs->b0 == bundle_addr + 0x10)) {
  557. regs->b0 = (regs->b0 - bundle_addr) +
  558. resume_addr;
  559. }
  560. break;
  561. case 6:
  562. if ((regs->b6 == bundle_addr) ||
  563. (regs->b6 == bundle_addr + 0x10)) {
  564. regs->b6 = (regs->b6 - bundle_addr) +
  565. resume_addr;
  566. }
  567. break;
  568. case 7:
  569. if ((regs->b7 == bundle_addr) ||
  570. (regs->b7 == bundle_addr + 0x10)) {
  571. regs->b7 = (regs->b7 - bundle_addr) +
  572. resume_addr;
  573. }
  574. break;
  575. } /* end switch */
  576. }
  577. goto turn_ss_off;
  578. }
  579. if (slot == 2) {
  580. if (regs->cr_iip == bundle_addr + 0x10) {
  581. regs->cr_iip = resume_addr + 0x10;
  582. }
  583. } else {
  584. if (regs->cr_iip == bundle_addr) {
  585. regs->cr_iip = resume_addr;
  586. }
  587. }
  588. turn_ss_off:
  589. /* Turn off Single Step bit */
  590. ia64_psr(regs)->ss = 0;
  591. }
  592. static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs)
  593. {
  594. unsigned long bundle_addr = (unsigned long) &p->ainsn.insn->bundle;
  595. unsigned long slot = (unsigned long)p->addr & 0xf;
  596. /* single step inline if break instruction */
  597. if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)
  598. regs->cr_iip = (unsigned long)p->addr & ~0xFULL;
  599. else
  600. regs->cr_iip = bundle_addr & ~0xFULL;
  601. if (slot > 2)
  602. slot = 0;
  603. ia64_psr(regs)->ri = slot;
  604. /* turn on single stepping */
  605. ia64_psr(regs)->ss = 1;
  606. }
  607. static int __kprobes is_ia64_break_inst(struct pt_regs *regs)
  608. {
  609. unsigned int slot = ia64_psr(regs)->ri;
  610. unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip;
  611. bundle_t bundle;
  612. memcpy(&bundle, kprobe_addr, sizeof(bundle_t));
  613. return __is_ia64_break_inst(&bundle, slot);
  614. }
  615. static int __kprobes pre_kprobes_handler(struct die_args *args)
  616. {
  617. struct kprobe *p;
  618. int ret = 0;
  619. struct pt_regs *regs = args->regs;
  620. kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
  621. struct kprobe_ctlblk *kcb;
  622. /*
  623. * We don't want to be preempted for the entire
  624. * duration of kprobe processing
  625. */
  626. preempt_disable();
  627. kcb = get_kprobe_ctlblk();
  628. /* Handle recursion cases */
  629. if (kprobe_running()) {
  630. p = get_kprobe(addr);
  631. if (p) {
  632. if ((kcb->kprobe_status == KPROBE_HIT_SS) &&
  633. (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) {
  634. ia64_psr(regs)->ss = 0;
  635. goto no_kprobe;
  636. }
  637. /* We have reentered the pre_kprobe_handler(), since
  638. * another probe was hit while within the handler.
  639. * We here save the original kprobes variables and
  640. * just single step on the instruction of the new probe
  641. * without calling any user handlers.
  642. */
  643. save_previous_kprobe(kcb);
  644. set_current_kprobe(p, kcb);
  645. kprobes_inc_nmissed_count(p);
  646. prepare_ss(p, regs);
  647. kcb->kprobe_status = KPROBE_REENTER;
  648. return 1;
  649. } else if (!is_ia64_break_inst(regs)) {
  650. /* The breakpoint instruction was removed by
  651. * another cpu right after we hit, no further
  652. * handling of this interrupt is appropriate
  653. */
  654. ret = 1;
  655. goto no_kprobe;
  656. } else {
  657. /* Not our break */
  658. goto no_kprobe;
  659. }
  660. }
  661. p = get_kprobe(addr);
  662. if (!p) {
  663. if (!is_ia64_break_inst(regs)) {
  664. /*
  665. * The breakpoint instruction was removed right
  666. * after we hit it. Another cpu has removed
  667. * either a probepoint or a debugger breakpoint
  668. * at this address. In either case, no further
  669. * handling of this interrupt is appropriate.
  670. */
  671. ret = 1;
  672. }
  673. /* Not one of our break, let kernel handle it */
  674. goto no_kprobe;
  675. }
  676. set_current_kprobe(p, kcb);
  677. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  678. if (p->pre_handler && p->pre_handler(p, regs)) {
  679. reset_current_kprobe();
  680. preempt_enable_no_resched();
  681. return 1;
  682. }
  683. #if !defined(CONFIG_PREEMPTION)
  684. if (p->ainsn.inst_flag == INST_FLAG_BOOSTABLE && !p->post_handler) {
  685. /* Boost up -- we can execute copied instructions directly */
  686. ia64_psr(regs)->ri = p->ainsn.slot;
  687. regs->cr_iip = (unsigned long)&p->ainsn.insn->bundle & ~0xFULL;
  688. /* turn single stepping off */
  689. ia64_psr(regs)->ss = 0;
  690. reset_current_kprobe();
  691. preempt_enable_no_resched();
  692. return 1;
  693. }
  694. #endif
  695. prepare_ss(p, regs);
  696. kcb->kprobe_status = KPROBE_HIT_SS;
  697. return 1;
  698. no_kprobe:
  699. preempt_enable_no_resched();
  700. return ret;
  701. }
  702. static int __kprobes post_kprobes_handler(struct pt_regs *regs)
  703. {
  704. struct kprobe *cur = kprobe_running();
  705. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  706. if (!cur)
  707. return 0;
  708. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  709. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  710. cur->post_handler(cur, regs, 0);
  711. }
  712. resume_execution(cur, regs);
  713. /*Restore back the original saved kprobes variables and continue. */
  714. if (kcb->kprobe_status == KPROBE_REENTER) {
  715. restore_previous_kprobe(kcb);
  716. goto out;
  717. }
  718. reset_current_kprobe();
  719. out:
  720. preempt_enable_no_resched();
  721. return 1;
  722. }
  723. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  724. {
  725. struct kprobe *cur = kprobe_running();
  726. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  727. switch(kcb->kprobe_status) {
  728. case KPROBE_HIT_SS:
  729. case KPROBE_REENTER:
  730. /*
  731. * We are here because the instruction being single
  732. * stepped caused a page fault. We reset the current
  733. * kprobe and the instruction pointer points back to
  734. * the probe address and allow the page fault handler
  735. * to continue as a normal page fault.
  736. */
  737. regs->cr_iip = ((unsigned long)cur->addr) & ~0xFULL;
  738. ia64_psr(regs)->ri = ((unsigned long)cur->addr) & 0xf;
  739. if (kcb->kprobe_status == KPROBE_REENTER)
  740. restore_previous_kprobe(kcb);
  741. else
  742. reset_current_kprobe();
  743. preempt_enable_no_resched();
  744. break;
  745. case KPROBE_HIT_ACTIVE:
  746. case KPROBE_HIT_SSDONE:
  747. /*
  748. * In case the user-specified fault handler returned
  749. * zero, try to fix up.
  750. */
  751. if (ia64_done_with_exception(regs))
  752. return 1;
  753. /*
  754. * Let ia64_do_page_fault() fix it.
  755. */
  756. break;
  757. default:
  758. break;
  759. }
  760. return 0;
  761. }
  762. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  763. unsigned long val, void *data)
  764. {
  765. struct die_args *args = (struct die_args *)data;
  766. int ret = NOTIFY_DONE;
  767. if (args->regs && user_mode(args->regs))
  768. return ret;
  769. switch(val) {
  770. case DIE_BREAK:
  771. /* err is break number from ia64_bad_break() */
  772. if ((args->err >> 12) == (__IA64_BREAK_KPROBE >> 12)
  773. || args->err == 0)
  774. if (pre_kprobes_handler(args))
  775. ret = NOTIFY_STOP;
  776. break;
  777. case DIE_FAULT:
  778. /* err is vector number from ia64_fault() */
  779. if (args->err == 36)
  780. if (post_kprobes_handler(args->regs))
  781. ret = NOTIFY_STOP;
  782. break;
  783. default:
  784. break;
  785. }
  786. return ret;
  787. }
  788. static struct kprobe trampoline_p = {
  789. .pre_handler = trampoline_probe_handler
  790. };
  791. int __init arch_init_kprobes(void)
  792. {
  793. trampoline_p.addr =
  794. dereference_function_descriptor(__kretprobe_trampoline);
  795. return register_kprobe(&trampoline_p);
  796. }
  797. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  798. {
  799. if (p->addr ==
  800. dereference_function_descriptor(__kretprobe_trampoline))
  801. return 1;
  802. return 0;
  803. }