kprobes.c 30 KB

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