kprobes.c 30 KB

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