kprobes.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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. enum instruction_type {A, I, M, F, B, L, X, u};
  39. static enum instruction_type bundle_encoding[32][3] = {
  40. { M, I, I }, /* 00 */
  41. { M, I, I }, /* 01 */
  42. { M, I, I }, /* 02 */
  43. { M, I, I }, /* 03 */
  44. { M, L, X }, /* 04 */
  45. { M, L, X }, /* 05 */
  46. { u, u, u }, /* 06 */
  47. { u, u, u }, /* 07 */
  48. { M, M, I }, /* 08 */
  49. { M, M, I }, /* 09 */
  50. { M, M, I }, /* 0A */
  51. { M, M, I }, /* 0B */
  52. { M, F, I }, /* 0C */
  53. { M, F, I }, /* 0D */
  54. { M, M, F }, /* 0E */
  55. { M, M, F }, /* 0F */
  56. { M, I, B }, /* 10 */
  57. { M, I, B }, /* 11 */
  58. { M, B, B }, /* 12 */
  59. { M, B, B }, /* 13 */
  60. { u, u, u }, /* 14 */
  61. { u, u, u }, /* 15 */
  62. { B, B, B }, /* 16 */
  63. { B, B, B }, /* 17 */
  64. { M, M, B }, /* 18 */
  65. { M, M, B }, /* 19 */
  66. { u, u, u }, /* 1A */
  67. { u, u, u }, /* 1B */
  68. { M, F, B }, /* 1C */
  69. { M, F, B }, /* 1D */
  70. { u, u, u }, /* 1E */
  71. { u, u, u }, /* 1F */
  72. };
  73. /*
  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. /* Integere 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. /* Integere 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. kcb->prev_kprobe.kp = kprobe_running();
  345. kcb->prev_kprobe.status = kcb->kprobe_status;
  346. }
  347. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  348. {
  349. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  350. kcb->kprobe_status = kcb->prev_kprobe.status;
  351. }
  352. static void __kprobes set_current_kprobe(struct kprobe *p,
  353. struct kprobe_ctlblk *kcb)
  354. {
  355. __get_cpu_var(current_kprobe) = p;
  356. }
  357. static void kretprobe_trampoline(void)
  358. {
  359. }
  360. /*
  361. * At this point the target function has been tricked into
  362. * returning into our trampoline. Lookup the associated instance
  363. * and then:
  364. * - call the handler function
  365. * - cleanup by marking the instance as unused
  366. * - long jump back to the original return address
  367. */
  368. int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  369. {
  370. struct kretprobe_instance *ri = NULL;
  371. struct hlist_head *head, empty_rp;
  372. struct hlist_node *node, *tmp;
  373. unsigned long flags, orig_ret_address = 0;
  374. unsigned long trampoline_address =
  375. ((struct fnptr *)kretprobe_trampoline)->ip;
  376. INIT_HLIST_HEAD(&empty_rp);
  377. spin_lock_irqsave(&kretprobe_lock, flags);
  378. head = kretprobe_inst_table_head(current);
  379. /*
  380. * It is possible to have multiple instances associated with a given
  381. * task either because an multiple functions in the call path
  382. * have a return probe installed on them, and/or more then one return
  383. * return probe was registered for a target function.
  384. *
  385. * We can handle this because:
  386. * - instances are always inserted at the head of the list
  387. * - when multiple return probes are registered for the same
  388. * function, the first instance's ret_addr will point to the
  389. * real return address, and all the rest will point to
  390. * kretprobe_trampoline
  391. */
  392. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  393. if (ri->task != current)
  394. /* another task is sharing our hash bucket */
  395. continue;
  396. if (ri->rp && ri->rp->handler)
  397. ri->rp->handler(ri, regs);
  398. orig_ret_address = (unsigned long)ri->ret_addr;
  399. recycle_rp_inst(ri, &empty_rp);
  400. if (orig_ret_address != trampoline_address)
  401. /*
  402. * This is the real return address. Any other
  403. * instances associated with this task are for
  404. * other calls deeper on the call stack
  405. */
  406. break;
  407. }
  408. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  409. regs->cr_iip = orig_ret_address;
  410. reset_current_kprobe();
  411. spin_unlock_irqrestore(&kretprobe_lock, flags);
  412. preempt_enable_no_resched();
  413. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  414. hlist_del(&ri->hlist);
  415. kfree(ri);
  416. }
  417. /*
  418. * By returning a non-zero value, we are telling
  419. * kprobe_handler() that we don't want the post_handler
  420. * to run (and have re-enabled preemption)
  421. */
  422. return 1;
  423. }
  424. /* Called with kretprobe_lock held */
  425. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  426. struct pt_regs *regs)
  427. {
  428. ri->ret_addr = (kprobe_opcode_t *)regs->b0;
  429. /* Replace the return addr with trampoline addr */
  430. regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip;
  431. }
  432. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  433. {
  434. unsigned long addr = (unsigned long) p->addr;
  435. unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
  436. unsigned long kprobe_inst=0;
  437. unsigned int slot = addr & 0xf, template, major_opcode = 0;
  438. bundle_t *bundle;
  439. int qp;
  440. bundle = &((kprobe_opcode_t *)kprobe_addr)->bundle;
  441. template = bundle->quad0.template;
  442. if(valid_kprobe_addr(template, slot, addr))
  443. return -EINVAL;
  444. /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
  445. if (slot == 1 && bundle_encoding[template][1] == L)
  446. slot++;
  447. /* Get kprobe_inst and major_opcode from the bundle */
  448. get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
  449. qp = unsupported_inst(template, slot, major_opcode, kprobe_inst, addr);
  450. if (qp < 0)
  451. return -EINVAL;
  452. p->ainsn.insn = get_insn_slot();
  453. if (!p->ainsn.insn)
  454. return -ENOMEM;
  455. memcpy(&p->opcode, kprobe_addr, sizeof(kprobe_opcode_t));
  456. memcpy(p->ainsn.insn, kprobe_addr, sizeof(kprobe_opcode_t));
  457. prepare_break_inst(template, slot, major_opcode, kprobe_inst, p, qp);
  458. return 0;
  459. }
  460. void __kprobes arch_arm_kprobe(struct kprobe *p)
  461. {
  462. unsigned long arm_addr;
  463. bundle_t *src, *dest;
  464. arm_addr = ((unsigned long)p->addr) & ~0xFUL;
  465. dest = &((kprobe_opcode_t *)arm_addr)->bundle;
  466. src = &p->opcode.bundle;
  467. flush_icache_range((unsigned long)p->ainsn.insn,
  468. (unsigned long)p->ainsn.insn + sizeof(kprobe_opcode_t));
  469. switch (p->ainsn.slot) {
  470. case 0:
  471. dest->quad0.slot0 = src->quad0.slot0;
  472. break;
  473. case 1:
  474. dest->quad1.slot1_p1 = src->quad1.slot1_p1;
  475. break;
  476. case 2:
  477. dest->quad1.slot2 = src->quad1.slot2;
  478. break;
  479. }
  480. flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
  481. }
  482. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  483. {
  484. unsigned long arm_addr;
  485. bundle_t *src, *dest;
  486. arm_addr = ((unsigned long)p->addr) & ~0xFUL;
  487. dest = &((kprobe_opcode_t *)arm_addr)->bundle;
  488. /* p->ainsn.insn contains the original unaltered kprobe_opcode_t */
  489. src = &p->ainsn.insn->bundle;
  490. switch (p->ainsn.slot) {
  491. case 0:
  492. dest->quad0.slot0 = src->quad0.slot0;
  493. break;
  494. case 1:
  495. dest->quad1.slot1_p1 = src->quad1.slot1_p1;
  496. break;
  497. case 2:
  498. dest->quad1.slot2 = src->quad1.slot2;
  499. break;
  500. }
  501. flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
  502. }
  503. void __kprobes arch_remove_kprobe(struct kprobe *p)
  504. {
  505. mutex_lock(&kprobe_mutex);
  506. free_insn_slot(p->ainsn.insn, 0);
  507. mutex_unlock(&kprobe_mutex);
  508. }
  509. /*
  510. * We are resuming execution after a single step fault, so the pt_regs
  511. * structure reflects the register state after we executed the instruction
  512. * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
  513. * the ip to point back to the original stack address. To set the IP address
  514. * to original stack address, handle the case where we need to fixup the
  515. * relative IP address and/or fixup branch register.
  516. */
  517. static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
  518. {
  519. unsigned long bundle_addr = (unsigned long) (&p->ainsn.insn->bundle);
  520. unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
  521. unsigned long template;
  522. int slot = ((unsigned long)p->addr & 0xf);
  523. template = p->ainsn.insn->bundle.quad0.template;
  524. if (slot == 1 && bundle_encoding[template][1] == L)
  525. slot = 2;
  526. if (p->ainsn.inst_flag) {
  527. if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
  528. /* Fix relative IP address */
  529. regs->cr_iip = (regs->cr_iip - bundle_addr) +
  530. resume_addr;
  531. }
  532. if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
  533. /*
  534. * Fix target branch register, software convention is
  535. * to use either b0 or b6 or b7, so just checking
  536. * only those registers
  537. */
  538. switch (p->ainsn.target_br_reg) {
  539. case 0:
  540. if ((regs->b0 == bundle_addr) ||
  541. (regs->b0 == bundle_addr + 0x10)) {
  542. regs->b0 = (regs->b0 - bundle_addr) +
  543. resume_addr;
  544. }
  545. break;
  546. case 6:
  547. if ((regs->b6 == bundle_addr) ||
  548. (regs->b6 == bundle_addr + 0x10)) {
  549. regs->b6 = (regs->b6 - bundle_addr) +
  550. resume_addr;
  551. }
  552. break;
  553. case 7:
  554. if ((regs->b7 == bundle_addr) ||
  555. (regs->b7 == bundle_addr + 0x10)) {
  556. regs->b7 = (regs->b7 - bundle_addr) +
  557. resume_addr;
  558. }
  559. break;
  560. } /* end switch */
  561. }
  562. goto turn_ss_off;
  563. }
  564. if (slot == 2) {
  565. if (regs->cr_iip == bundle_addr + 0x10) {
  566. regs->cr_iip = resume_addr + 0x10;
  567. }
  568. } else {
  569. if (regs->cr_iip == bundle_addr) {
  570. regs->cr_iip = resume_addr;
  571. }
  572. }
  573. turn_ss_off:
  574. /* Turn off Single Step bit */
  575. ia64_psr(regs)->ss = 0;
  576. }
  577. static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs)
  578. {
  579. unsigned long bundle_addr = (unsigned long) &p->ainsn.insn->bundle;
  580. unsigned long slot = (unsigned long)p->addr & 0xf;
  581. /* single step inline if break instruction */
  582. if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)
  583. regs->cr_iip = (unsigned long)p->addr & ~0xFULL;
  584. else
  585. regs->cr_iip = bundle_addr & ~0xFULL;
  586. if (slot > 2)
  587. slot = 0;
  588. ia64_psr(regs)->ri = slot;
  589. /* turn on single stepping */
  590. ia64_psr(regs)->ss = 1;
  591. }
  592. static int __kprobes is_ia64_break_inst(struct pt_regs *regs)
  593. {
  594. unsigned int slot = ia64_psr(regs)->ri;
  595. unsigned int template, major_opcode;
  596. unsigned long kprobe_inst;
  597. unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip;
  598. bundle_t bundle;
  599. memcpy(&bundle, kprobe_addr, sizeof(bundle_t));
  600. template = bundle.quad0.template;
  601. /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
  602. if (slot == 1 && bundle_encoding[template][1] == L)
  603. slot++;
  604. /* Get Kprobe probe instruction at given slot*/
  605. get_kprobe_inst(&bundle, slot, &kprobe_inst, &major_opcode);
  606. /* For break instruction,
  607. * Bits 37:40 Major opcode to be zero
  608. * Bits 27:32 X6 to be zero
  609. * Bits 32:35 X3 to be zero
  610. */
  611. if (major_opcode || ((kprobe_inst >> 27) & 0x1FF) ) {
  612. /* Not a break instruction */
  613. return 0;
  614. }
  615. /* Is a break instruction */
  616. return 1;
  617. }
  618. static int __kprobes pre_kprobes_handler(struct die_args *args)
  619. {
  620. struct kprobe *p;
  621. int ret = 0;
  622. struct pt_regs *regs = args->regs;
  623. kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
  624. struct kprobe_ctlblk *kcb;
  625. /*
  626. * We don't want to be preempted for the entire
  627. * duration of kprobe processing
  628. */
  629. preempt_disable();
  630. kcb = get_kprobe_ctlblk();
  631. /* Handle recursion cases */
  632. if (kprobe_running()) {
  633. p = get_kprobe(addr);
  634. if (p) {
  635. if ((kcb->kprobe_status == KPROBE_HIT_SS) &&
  636. (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) {
  637. ia64_psr(regs)->ss = 0;
  638. goto no_kprobe;
  639. }
  640. /* We have reentered the pre_kprobe_handler(), since
  641. * another probe was hit while within the handler.
  642. * We here save the original kprobes variables and
  643. * just single step on the instruction of the new probe
  644. * without calling any user handlers.
  645. */
  646. save_previous_kprobe(kcb);
  647. set_current_kprobe(p, kcb);
  648. kprobes_inc_nmissed_count(p);
  649. prepare_ss(p, regs);
  650. kcb->kprobe_status = KPROBE_REENTER;
  651. return 1;
  652. } else if (args->err == __IA64_BREAK_JPROBE) {
  653. /*
  654. * jprobe instrumented function just completed
  655. */
  656. p = __get_cpu_var(current_kprobe);
  657. if (p->break_handler && p->break_handler(p, regs)) {
  658. goto ss_probe;
  659. }
  660. } else if (!is_ia64_break_inst(regs)) {
  661. /* The breakpoint instruction was removed by
  662. * another cpu right after we hit, no further
  663. * handling of this interrupt is appropriate
  664. */
  665. ret = 1;
  666. goto no_kprobe;
  667. } else {
  668. /* Not our break */
  669. goto no_kprobe;
  670. }
  671. }
  672. p = get_kprobe(addr);
  673. if (!p) {
  674. if (!is_ia64_break_inst(regs)) {
  675. /*
  676. * The breakpoint instruction was removed right
  677. * after we hit it. Another cpu has removed
  678. * either a probepoint or a debugger breakpoint
  679. * at this address. In either case, no further
  680. * handling of this interrupt is appropriate.
  681. */
  682. ret = 1;
  683. }
  684. /* Not one of our break, let kernel handle it */
  685. goto no_kprobe;
  686. }
  687. set_current_kprobe(p, kcb);
  688. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  689. if (p->pre_handler && p->pre_handler(p, regs))
  690. /*
  691. * Our pre-handler is specifically requesting that we just
  692. * do a return. This is used for both the jprobe pre-handler
  693. * and the kretprobe trampoline
  694. */
  695. return 1;
  696. ss_probe:
  697. prepare_ss(p, regs);
  698. kcb->kprobe_status = KPROBE_HIT_SS;
  699. return 1;
  700. no_kprobe:
  701. preempt_enable_no_resched();
  702. return ret;
  703. }
  704. static int __kprobes post_kprobes_handler(struct pt_regs *regs)
  705. {
  706. struct kprobe *cur = kprobe_running();
  707. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  708. if (!cur)
  709. return 0;
  710. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  711. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  712. cur->post_handler(cur, regs, 0);
  713. }
  714. resume_execution(cur, regs);
  715. /*Restore back the original saved kprobes variables and continue. */
  716. if (kcb->kprobe_status == KPROBE_REENTER) {
  717. restore_previous_kprobe(kcb);
  718. goto out;
  719. }
  720. reset_current_kprobe();
  721. out:
  722. preempt_enable_no_resched();
  723. return 1;
  724. }
  725. static int __kprobes kprobes_fault_handler(struct pt_regs *regs, int trapnr)
  726. {
  727. struct kprobe *cur = kprobe_running();
  728. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  729. switch(kcb->kprobe_status) {
  730. case KPROBE_HIT_SS:
  731. case KPROBE_REENTER:
  732. /*
  733. * We are here because the instruction being single
  734. * stepped caused a page fault. We reset the current
  735. * kprobe and the instruction pointer points back to
  736. * the probe address and allow the page fault handler
  737. * to continue as a normal page fault.
  738. */
  739. regs->cr_iip = ((unsigned long)cur->addr) & ~0xFULL;
  740. ia64_psr(regs)->ri = ((unsigned long)cur->addr) & 0xf;
  741. if (kcb->kprobe_status == KPROBE_REENTER)
  742. restore_previous_kprobe(kcb);
  743. else
  744. reset_current_kprobe();
  745. preempt_enable_no_resched();
  746. break;
  747. case KPROBE_HIT_ACTIVE:
  748. case KPROBE_HIT_SSDONE:
  749. /*
  750. * We increment the nmissed count for accounting,
  751. * we can also use npre/npostfault count for accouting
  752. * these specific fault cases.
  753. */
  754. kprobes_inc_nmissed_count(cur);
  755. /*
  756. * We come here because instructions in the pre/post
  757. * handler caused the page_fault, this could happen
  758. * if handler tries to access user space by
  759. * copy_from_user(), get_user() etc. Let the
  760. * user-specified handler try to fix it first.
  761. */
  762. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  763. return 1;
  764. /*
  765. * In case the user-specified fault handler returned
  766. * zero, try to fix up.
  767. */
  768. if (ia64_done_with_exception(regs))
  769. return 1;
  770. /*
  771. * Let ia64_do_page_fault() fix it.
  772. */
  773. break;
  774. default:
  775. break;
  776. }
  777. return 0;
  778. }
  779. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  780. unsigned long val, void *data)
  781. {
  782. struct die_args *args = (struct die_args *)data;
  783. int ret = NOTIFY_DONE;
  784. if (args->regs && user_mode(args->regs))
  785. return ret;
  786. switch(val) {
  787. case DIE_BREAK:
  788. /* err is break number from ia64_bad_break() */
  789. if ((args->err >> 12) == (__IA64_BREAK_KPROBE >> 12)
  790. || args->err == __IA64_BREAK_JPROBE
  791. || args->err == 0)
  792. if (pre_kprobes_handler(args))
  793. ret = NOTIFY_STOP;
  794. break;
  795. case DIE_FAULT:
  796. /* err is vector number from ia64_fault() */
  797. if (args->err == 36)
  798. if (post_kprobes_handler(args->regs))
  799. ret = NOTIFY_STOP;
  800. break;
  801. case DIE_PAGE_FAULT:
  802. /* kprobe_running() needs smp_processor_id() */
  803. preempt_disable();
  804. if (kprobe_running() &&
  805. kprobes_fault_handler(args->regs, args->trapnr))
  806. ret = NOTIFY_STOP;
  807. preempt_enable();
  808. default:
  809. break;
  810. }
  811. return ret;
  812. }
  813. struct param_bsp_cfm {
  814. unsigned long ip;
  815. unsigned long *bsp;
  816. unsigned long cfm;
  817. };
  818. static void ia64_get_bsp_cfm(struct unw_frame_info *info, void *arg)
  819. {
  820. unsigned long ip;
  821. struct param_bsp_cfm *lp = arg;
  822. do {
  823. unw_get_ip(info, &ip);
  824. if (ip == 0)
  825. break;
  826. if (ip == lp->ip) {
  827. unw_get_bsp(info, (unsigned long*)&lp->bsp);
  828. unw_get_cfm(info, (unsigned long*)&lp->cfm);
  829. return;
  830. }
  831. } while (unw_unwind(info) >= 0);
  832. lp->bsp = NULL;
  833. lp->cfm = 0;
  834. return;
  835. }
  836. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  837. {
  838. struct jprobe *jp = container_of(p, struct jprobe, kp);
  839. unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
  840. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  841. struct param_bsp_cfm pa;
  842. int bytes;
  843. /*
  844. * Callee owns the argument space and could overwrite it, eg
  845. * tail call optimization. So to be absolutely safe
  846. * we save the argument space before transfering the control
  847. * to instrumented jprobe function which runs in
  848. * the process context
  849. */
  850. pa.ip = regs->cr_iip;
  851. unw_init_running(ia64_get_bsp_cfm, &pa);
  852. bytes = (char *)ia64_rse_skip_regs(pa.bsp, pa.cfm & 0x3f)
  853. - (char *)pa.bsp;
  854. memcpy( kcb->jprobes_saved_stacked_regs,
  855. pa.bsp,
  856. bytes );
  857. kcb->bsp = pa.bsp;
  858. kcb->cfm = pa.cfm;
  859. /* save architectural state */
  860. kcb->jprobe_saved_regs = *regs;
  861. /* after rfi, execute the jprobe instrumented function */
  862. regs->cr_iip = addr & ~0xFULL;
  863. ia64_psr(regs)->ri = addr & 0xf;
  864. regs->r1 = ((struct fnptr *)(jp->entry))->gp;
  865. /*
  866. * fix the return address to our jprobe_inst_return() function
  867. * in the jprobes.S file
  868. */
  869. regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
  870. return 1;
  871. }
  872. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  873. {
  874. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  875. int bytes;
  876. /* restoring architectural state */
  877. *regs = kcb->jprobe_saved_regs;
  878. /* restoring the original argument space */
  879. flush_register_stack();
  880. bytes = (char *)ia64_rse_skip_regs(kcb->bsp, kcb->cfm & 0x3f)
  881. - (char *)kcb->bsp;
  882. memcpy( kcb->bsp,
  883. kcb->jprobes_saved_stacked_regs,
  884. bytes );
  885. invalidate_stacked_regs();
  886. preempt_enable_no_resched();
  887. return 1;
  888. }
  889. static struct kprobe trampoline_p = {
  890. .pre_handler = trampoline_probe_handler
  891. };
  892. int __init arch_init_kprobes(void)
  893. {
  894. trampoline_p.addr =
  895. (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
  896. return register_kprobe(&trampoline_p);
  897. }
  898. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  899. {
  900. if (p->addr ==
  901. (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip)
  902. return 1;
  903. return 0;
  904. }