kprobes.c 27 KB

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