kprobes.c 27 KB

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