kprobes.c 27 KB

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