single_step.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. *
  14. * A code-rewriter that enables instruction single-stepping.
  15. */
  16. #include <linux/smp.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/slab.h>
  19. #include <linux/thread_info.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/mman.h>
  22. #include <linux/types.h>
  23. #include <linux/err.h>
  24. #include <linux/prctl.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/traps.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/unaligned.h>
  29. #include <arch/abi.h>
  30. #include <arch/spr_def.h>
  31. #include <arch/opcode.h>
  32. #ifndef __tilegx__ /* Hardware support for single step unavailable. */
  33. #define signExtend17(val) sign_extend((val), 17)
  34. #define TILE_X1_MASK (0xffffffffULL << 31)
  35. enum mem_op {
  36. MEMOP_NONE,
  37. MEMOP_LOAD,
  38. MEMOP_STORE,
  39. MEMOP_LOAD_POSTINCR,
  40. MEMOP_STORE_POSTINCR
  41. };
  42. static inline tilepro_bundle_bits set_BrOff_X1(tilepro_bundle_bits n,
  43. s32 offset)
  44. {
  45. tilepro_bundle_bits result;
  46. /* mask out the old offset */
  47. tilepro_bundle_bits mask = create_BrOff_X1(-1);
  48. result = n & (~mask);
  49. /* or in the new offset */
  50. result |= create_BrOff_X1(offset);
  51. return result;
  52. }
  53. static inline tilepro_bundle_bits move_X1(tilepro_bundle_bits n, int dest,
  54. int src)
  55. {
  56. tilepro_bundle_bits result;
  57. tilepro_bundle_bits op;
  58. result = n & (~TILE_X1_MASK);
  59. op = create_Opcode_X1(SPECIAL_0_OPCODE_X1) |
  60. create_RRROpcodeExtension_X1(OR_SPECIAL_0_OPCODE_X1) |
  61. create_Dest_X1(dest) |
  62. create_SrcB_X1(TREG_ZERO) |
  63. create_SrcA_X1(src) ;
  64. result |= op;
  65. return result;
  66. }
  67. static inline tilepro_bundle_bits nop_X1(tilepro_bundle_bits n)
  68. {
  69. return move_X1(n, TREG_ZERO, TREG_ZERO);
  70. }
  71. static inline tilepro_bundle_bits addi_X1(
  72. tilepro_bundle_bits n, int dest, int src, int imm)
  73. {
  74. n &= ~TILE_X1_MASK;
  75. n |= (create_SrcA_X1(src) |
  76. create_Dest_X1(dest) |
  77. create_Imm8_X1(imm) |
  78. create_S_X1(0) |
  79. create_Opcode_X1(IMM_0_OPCODE_X1) |
  80. create_ImmOpcodeExtension_X1(ADDI_IMM_0_OPCODE_X1));
  81. return n;
  82. }
  83. static tilepro_bundle_bits rewrite_load_store_unaligned(
  84. struct single_step_state *state,
  85. tilepro_bundle_bits bundle,
  86. struct pt_regs *regs,
  87. enum mem_op mem_op,
  88. int size, int sign_ext)
  89. {
  90. unsigned char __user *addr;
  91. int val_reg, addr_reg, err, val;
  92. int align_ctl;
  93. align_ctl = unaligned_fixup;
  94. switch (task_thread_info(current)->align_ctl) {
  95. case PR_UNALIGN_NOPRINT:
  96. align_ctl = 1;
  97. break;
  98. case PR_UNALIGN_SIGBUS:
  99. align_ctl = 0;
  100. break;
  101. }
  102. /* Get address and value registers */
  103. if (bundle & TILEPRO_BUNDLE_Y_ENCODING_MASK) {
  104. addr_reg = get_SrcA_Y2(bundle);
  105. val_reg = get_SrcBDest_Y2(bundle);
  106. } else if (mem_op == MEMOP_LOAD || mem_op == MEMOP_LOAD_POSTINCR) {
  107. addr_reg = get_SrcA_X1(bundle);
  108. val_reg = get_Dest_X1(bundle);
  109. } else {
  110. addr_reg = get_SrcA_X1(bundle);
  111. val_reg = get_SrcB_X1(bundle);
  112. }
  113. /*
  114. * If registers are not GPRs, don't try to handle it.
  115. *
  116. * FIXME: we could handle non-GPR loads by getting the real value
  117. * from memory, writing it to the single step buffer, using a
  118. * temp_reg to hold a pointer to that memory, then executing that
  119. * instruction and resetting temp_reg. For non-GPR stores, it's a
  120. * little trickier; we could use the single step buffer for that
  121. * too, but we'd have to add some more state bits so that we could
  122. * call back in here to copy that value to the real target. For
  123. * now, we just handle the simple case.
  124. */
  125. if ((val_reg >= PTREGS_NR_GPRS &&
  126. (val_reg != TREG_ZERO ||
  127. mem_op == MEMOP_LOAD ||
  128. mem_op == MEMOP_LOAD_POSTINCR)) ||
  129. addr_reg >= PTREGS_NR_GPRS)
  130. return bundle;
  131. /* If it's aligned, don't handle it specially */
  132. addr = (void __user *)regs->regs[addr_reg];
  133. if (((unsigned long)addr % size) == 0)
  134. return bundle;
  135. /*
  136. * Return SIGBUS with the unaligned address, if requested.
  137. * Note that we return SIGBUS even for completely invalid addresses
  138. * as long as they are in fact unaligned; this matches what the
  139. * tilepro hardware would be doing, if it could provide us with the
  140. * actual bad address in an SPR, which it doesn't.
  141. */
  142. if (align_ctl == 0) {
  143. siginfo_t info = {
  144. .si_signo = SIGBUS,
  145. .si_code = BUS_ADRALN,
  146. .si_addr = addr
  147. };
  148. trace_unhandled_signal("unaligned trap", regs,
  149. (unsigned long)addr, SIGBUS);
  150. force_sig_info(info.si_signo, &info, current);
  151. return (tilepro_bundle_bits) 0;
  152. }
  153. /* Handle unaligned load/store */
  154. if (mem_op == MEMOP_LOAD || mem_op == MEMOP_LOAD_POSTINCR) {
  155. unsigned short val_16;
  156. switch (size) {
  157. case 2:
  158. err = copy_from_user(&val_16, addr, sizeof(val_16));
  159. val = sign_ext ? ((short)val_16) : val_16;
  160. break;
  161. case 4:
  162. err = copy_from_user(&val, addr, sizeof(val));
  163. break;
  164. default:
  165. BUG();
  166. }
  167. if (err == 0) {
  168. state->update_reg = val_reg;
  169. state->update_value = val;
  170. state->update = 1;
  171. }
  172. } else {
  173. unsigned short val_16;
  174. val = (val_reg == TREG_ZERO) ? 0 : regs->regs[val_reg];
  175. switch (size) {
  176. case 2:
  177. val_16 = val;
  178. err = copy_to_user(addr, &val_16, sizeof(val_16));
  179. break;
  180. case 4:
  181. err = copy_to_user(addr, &val, sizeof(val));
  182. break;
  183. default:
  184. BUG();
  185. }
  186. }
  187. if (err) {
  188. siginfo_t info = {
  189. .si_signo = SIGBUS,
  190. .si_code = BUS_ADRALN,
  191. .si_addr = addr
  192. };
  193. trace_unhandled_signal("bad address for unaligned fixup", regs,
  194. (unsigned long)addr, SIGBUS);
  195. force_sig_info(info.si_signo, &info, current);
  196. return (tilepro_bundle_bits) 0;
  197. }
  198. if (unaligned_printk || unaligned_fixup_count == 0) {
  199. pr_info("Process %d/%s: PC %#lx: Fixup of"
  200. " unaligned %s at %#lx.\n",
  201. current->pid, current->comm, regs->pc,
  202. (mem_op == MEMOP_LOAD ||
  203. mem_op == MEMOP_LOAD_POSTINCR) ?
  204. "load" : "store",
  205. (unsigned long)addr);
  206. if (!unaligned_printk) {
  207. #define P pr_info
  208. P("\n");
  209. P("Unaligned fixups in the kernel will slow your application considerably.\n");
  210. P("To find them, write a \"1\" to /proc/sys/tile/unaligned_fixup/printk,\n");
  211. P("which requests the kernel show all unaligned fixups, or write a \"0\"\n");
  212. P("to /proc/sys/tile/unaligned_fixup/enabled, in which case each unaligned\n");
  213. P("access will become a SIGBUS you can debug. No further warnings will be\n");
  214. P("shown so as to avoid additional slowdown, but you can track the number\n");
  215. P("of fixups performed via /proc/sys/tile/unaligned_fixup/count.\n");
  216. P("Use the tile-addr2line command (see \"info addr2line\") to decode PCs.\n");
  217. P("\n");
  218. #undef P
  219. }
  220. }
  221. ++unaligned_fixup_count;
  222. if (bundle & TILEPRO_BUNDLE_Y_ENCODING_MASK) {
  223. /* Convert the Y2 instruction to a prefetch. */
  224. bundle &= ~(create_SrcBDest_Y2(-1) |
  225. create_Opcode_Y2(-1));
  226. bundle |= (create_SrcBDest_Y2(TREG_ZERO) |
  227. create_Opcode_Y2(LW_OPCODE_Y2));
  228. /* Replace the load postincr with an addi */
  229. } else if (mem_op == MEMOP_LOAD_POSTINCR) {
  230. bundle = addi_X1(bundle, addr_reg, addr_reg,
  231. get_Imm8_X1(bundle));
  232. /* Replace the store postincr with an addi */
  233. } else if (mem_op == MEMOP_STORE_POSTINCR) {
  234. bundle = addi_X1(bundle, addr_reg, addr_reg,
  235. get_Dest_Imm8_X1(bundle));
  236. } else {
  237. /* Convert the X1 instruction to a nop. */
  238. bundle &= ~(create_Opcode_X1(-1) |
  239. create_UnShOpcodeExtension_X1(-1) |
  240. create_UnOpcodeExtension_X1(-1));
  241. bundle |= (create_Opcode_X1(SHUN_0_OPCODE_X1) |
  242. create_UnShOpcodeExtension_X1(
  243. UN_0_SHUN_0_OPCODE_X1) |
  244. create_UnOpcodeExtension_X1(
  245. NOP_UN_0_SHUN_0_OPCODE_X1));
  246. }
  247. return bundle;
  248. }
  249. /*
  250. * Called after execve() has started the new image. This allows us
  251. * to reset the info state. Note that the the mmap'ed memory, if there
  252. * was any, has already been unmapped by the exec.
  253. */
  254. void single_step_execve(void)
  255. {
  256. struct thread_info *ti = current_thread_info();
  257. kfree(ti->step_state);
  258. ti->step_state = NULL;
  259. }
  260. /*
  261. * single_step_once() - entry point when single stepping has been triggered.
  262. * @regs: The machine register state
  263. *
  264. * When we arrive at this routine via a trampoline, the single step
  265. * engine copies the executing bundle to the single step buffer.
  266. * If the instruction is a condition branch, then the target is
  267. * reset to one past the next instruction. If the instruction
  268. * sets the lr, then that is noted. If the instruction is a jump
  269. * or call, then the new target pc is preserved and the current
  270. * bundle instruction set to null.
  271. *
  272. * The necessary post-single-step rewriting information is stored in
  273. * single_step_state-> We use data segment values because the
  274. * stack will be rewound when we run the rewritten single-stepped
  275. * instruction.
  276. */
  277. void single_step_once(struct pt_regs *regs)
  278. {
  279. extern tilepro_bundle_bits __single_step_ill_insn;
  280. extern tilepro_bundle_bits __single_step_j_insn;
  281. extern tilepro_bundle_bits __single_step_addli_insn;
  282. extern tilepro_bundle_bits __single_step_auli_insn;
  283. struct thread_info *info = (void *)current_thread_info();
  284. struct single_step_state *state = info->step_state;
  285. int is_single_step = test_ti_thread_flag(info, TIF_SINGLESTEP);
  286. tilepro_bundle_bits __user *buffer, *pc;
  287. tilepro_bundle_bits bundle;
  288. int temp_reg;
  289. int target_reg = TREG_LR;
  290. int err;
  291. enum mem_op mem_op = MEMOP_NONE;
  292. int size = 0, sign_ext = 0; /* happy compiler */
  293. int align_ctl;
  294. align_ctl = unaligned_fixup;
  295. switch (task_thread_info(current)->align_ctl) {
  296. case PR_UNALIGN_NOPRINT:
  297. align_ctl = 1;
  298. break;
  299. case PR_UNALIGN_SIGBUS:
  300. align_ctl = 0;
  301. break;
  302. }
  303. asm(
  304. " .pushsection .rodata.single_step\n"
  305. " .align 8\n"
  306. " .globl __single_step_ill_insn\n"
  307. "__single_step_ill_insn:\n"
  308. " ill\n"
  309. " .globl __single_step_addli_insn\n"
  310. "__single_step_addli_insn:\n"
  311. " { nop; addli r0, zero, 0 }\n"
  312. " .globl __single_step_auli_insn\n"
  313. "__single_step_auli_insn:\n"
  314. " { nop; auli r0, r0, 0 }\n"
  315. " .globl __single_step_j_insn\n"
  316. "__single_step_j_insn:\n"
  317. " j .\n"
  318. " .popsection\n"
  319. );
  320. /*
  321. * Enable interrupts here to allow touching userspace and the like.
  322. * The callers expect this: do_trap() already has interrupts
  323. * enabled, and do_work_pending() handles functions that enable
  324. * interrupts internally.
  325. */
  326. local_irq_enable();
  327. if (state == NULL) {
  328. /* allocate a page of writable, executable memory */
  329. state = kmalloc(sizeof(struct single_step_state), GFP_KERNEL);
  330. if (state == NULL) {
  331. pr_err("Out of kernel memory trying to single-step\n");
  332. return;
  333. }
  334. /* allocate a cache line of writable, executable memory */
  335. buffer = (void __user *) vm_mmap(NULL, 0, 64,
  336. PROT_EXEC | PROT_READ | PROT_WRITE,
  337. MAP_PRIVATE | MAP_ANONYMOUS,
  338. 0);
  339. if (IS_ERR((void __force *)buffer)) {
  340. kfree(state);
  341. pr_err("Out of kernel pages trying to single-step\n");
  342. return;
  343. }
  344. state->buffer = buffer;
  345. state->is_enabled = 0;
  346. info->step_state = state;
  347. /* Validate our stored instruction patterns */
  348. BUG_ON(get_Opcode_X1(__single_step_addli_insn) !=
  349. ADDLI_OPCODE_X1);
  350. BUG_ON(get_Opcode_X1(__single_step_auli_insn) !=
  351. AULI_OPCODE_X1);
  352. BUG_ON(get_SrcA_X1(__single_step_addli_insn) != TREG_ZERO);
  353. BUG_ON(get_Dest_X1(__single_step_addli_insn) != 0);
  354. BUG_ON(get_JOffLong_X1(__single_step_j_insn) != 0);
  355. }
  356. /*
  357. * If we are returning from a syscall, we still haven't hit the
  358. * "ill" for the swint1 instruction. So back the PC up to be
  359. * pointing at the swint1, but we'll actually return directly
  360. * back to the "ill" so we come back in via SIGILL as if we
  361. * had "executed" the swint1 without ever being in kernel space.
  362. */
  363. if (regs->faultnum == INT_SWINT_1)
  364. regs->pc -= 8;
  365. pc = (tilepro_bundle_bits __user *)(regs->pc);
  366. if (get_user(bundle, pc) != 0) {
  367. pr_err("Couldn't read instruction at %p trying to step\n", pc);
  368. return;
  369. }
  370. /* We'll follow the instruction with 2 ill op bundles */
  371. state->orig_pc = (unsigned long)pc;
  372. state->next_pc = (unsigned long)(pc + 1);
  373. state->branch_next_pc = 0;
  374. state->update = 0;
  375. if (!(bundle & TILEPRO_BUNDLE_Y_ENCODING_MASK)) {
  376. /* two wide, check for control flow */
  377. int opcode = get_Opcode_X1(bundle);
  378. switch (opcode) {
  379. /* branches */
  380. case BRANCH_OPCODE_X1:
  381. {
  382. s32 offset = signExtend17(get_BrOff_X1(bundle));
  383. /*
  384. * For branches, we use a rewriting trick to let the
  385. * hardware evaluate whether the branch is taken or
  386. * untaken. We record the target offset and then
  387. * rewrite the branch instruction to target 1 insn
  388. * ahead if the branch is taken. We then follow the
  389. * rewritten branch with two bundles, each containing
  390. * an "ill" instruction. The supervisor examines the
  391. * pc after the single step code is executed, and if
  392. * the pc is the first ill instruction, then the
  393. * branch (if any) was not taken. If the pc is the
  394. * second ill instruction, then the branch was
  395. * taken. The new pc is computed for these cases, and
  396. * inserted into the registers for the thread. If
  397. * the pc is the start of the single step code, then
  398. * an exception or interrupt was taken before the
  399. * code started processing, and the same "original"
  400. * pc is restored. This change, different from the
  401. * original implementation, has the advantage of
  402. * executing a single user instruction.
  403. */
  404. state->branch_next_pc = (unsigned long)(pc + offset);
  405. /* rewrite branch offset to go forward one bundle */
  406. bundle = set_BrOff_X1(bundle, 2);
  407. }
  408. break;
  409. /* jumps */
  410. case JALB_OPCODE_X1:
  411. case JALF_OPCODE_X1:
  412. state->update = 1;
  413. state->next_pc =
  414. (unsigned long) (pc + get_JOffLong_X1(bundle));
  415. break;
  416. case JB_OPCODE_X1:
  417. case JF_OPCODE_X1:
  418. state->next_pc =
  419. (unsigned long) (pc + get_JOffLong_X1(bundle));
  420. bundle = nop_X1(bundle);
  421. break;
  422. case SPECIAL_0_OPCODE_X1:
  423. switch (get_RRROpcodeExtension_X1(bundle)) {
  424. /* jump-register */
  425. case JALRP_SPECIAL_0_OPCODE_X1:
  426. case JALR_SPECIAL_0_OPCODE_X1:
  427. state->update = 1;
  428. state->next_pc =
  429. regs->regs[get_SrcA_X1(bundle)];
  430. break;
  431. case JRP_SPECIAL_0_OPCODE_X1:
  432. case JR_SPECIAL_0_OPCODE_X1:
  433. state->next_pc =
  434. regs->regs[get_SrcA_X1(bundle)];
  435. bundle = nop_X1(bundle);
  436. break;
  437. case LNK_SPECIAL_0_OPCODE_X1:
  438. state->update = 1;
  439. target_reg = get_Dest_X1(bundle);
  440. break;
  441. /* stores */
  442. case SH_SPECIAL_0_OPCODE_X1:
  443. mem_op = MEMOP_STORE;
  444. size = 2;
  445. break;
  446. case SW_SPECIAL_0_OPCODE_X1:
  447. mem_op = MEMOP_STORE;
  448. size = 4;
  449. break;
  450. }
  451. break;
  452. /* loads and iret */
  453. case SHUN_0_OPCODE_X1:
  454. if (get_UnShOpcodeExtension_X1(bundle) ==
  455. UN_0_SHUN_0_OPCODE_X1) {
  456. switch (get_UnOpcodeExtension_X1(bundle)) {
  457. case LH_UN_0_SHUN_0_OPCODE_X1:
  458. mem_op = MEMOP_LOAD;
  459. size = 2;
  460. sign_ext = 1;
  461. break;
  462. case LH_U_UN_0_SHUN_0_OPCODE_X1:
  463. mem_op = MEMOP_LOAD;
  464. size = 2;
  465. sign_ext = 0;
  466. break;
  467. case LW_UN_0_SHUN_0_OPCODE_X1:
  468. mem_op = MEMOP_LOAD;
  469. size = 4;
  470. break;
  471. case IRET_UN_0_SHUN_0_OPCODE_X1:
  472. {
  473. unsigned long ex0_0 = __insn_mfspr(
  474. SPR_EX_CONTEXT_0_0);
  475. unsigned long ex0_1 = __insn_mfspr(
  476. SPR_EX_CONTEXT_0_1);
  477. /*
  478. * Special-case it if we're iret'ing
  479. * to PL0 again. Otherwise just let
  480. * it run and it will generate SIGILL.
  481. */
  482. if (EX1_PL(ex0_1) == USER_PL) {
  483. state->next_pc = ex0_0;
  484. regs->ex1 = ex0_1;
  485. bundle = nop_X1(bundle);
  486. }
  487. }
  488. }
  489. }
  490. break;
  491. /* postincrement operations */
  492. case IMM_0_OPCODE_X1:
  493. switch (get_ImmOpcodeExtension_X1(bundle)) {
  494. case LWADD_IMM_0_OPCODE_X1:
  495. mem_op = MEMOP_LOAD_POSTINCR;
  496. size = 4;
  497. break;
  498. case LHADD_IMM_0_OPCODE_X1:
  499. mem_op = MEMOP_LOAD_POSTINCR;
  500. size = 2;
  501. sign_ext = 1;
  502. break;
  503. case LHADD_U_IMM_0_OPCODE_X1:
  504. mem_op = MEMOP_LOAD_POSTINCR;
  505. size = 2;
  506. sign_ext = 0;
  507. break;
  508. case SWADD_IMM_0_OPCODE_X1:
  509. mem_op = MEMOP_STORE_POSTINCR;
  510. size = 4;
  511. break;
  512. case SHADD_IMM_0_OPCODE_X1:
  513. mem_op = MEMOP_STORE_POSTINCR;
  514. size = 2;
  515. break;
  516. default:
  517. break;
  518. }
  519. break;
  520. }
  521. if (state->update) {
  522. /*
  523. * Get an available register. We start with a
  524. * bitmask with 1's for available registers.
  525. * We truncate to the low 32 registers since
  526. * we are guaranteed to have set bits in the
  527. * low 32 bits, then use ctz to pick the first.
  528. */
  529. u32 mask = (u32) ~((1ULL << get_Dest_X0(bundle)) |
  530. (1ULL << get_SrcA_X0(bundle)) |
  531. (1ULL << get_SrcB_X0(bundle)) |
  532. (1ULL << target_reg));
  533. temp_reg = __builtin_ctz(mask);
  534. state->update_reg = temp_reg;
  535. state->update_value = regs->regs[temp_reg];
  536. regs->regs[temp_reg] = (unsigned long) (pc+1);
  537. regs->flags |= PT_FLAGS_RESTORE_REGS;
  538. bundle = move_X1(bundle, target_reg, temp_reg);
  539. }
  540. } else {
  541. int opcode = get_Opcode_Y2(bundle);
  542. switch (opcode) {
  543. /* loads */
  544. case LH_OPCODE_Y2:
  545. mem_op = MEMOP_LOAD;
  546. size = 2;
  547. sign_ext = 1;
  548. break;
  549. case LH_U_OPCODE_Y2:
  550. mem_op = MEMOP_LOAD;
  551. size = 2;
  552. sign_ext = 0;
  553. break;
  554. case LW_OPCODE_Y2:
  555. mem_op = MEMOP_LOAD;
  556. size = 4;
  557. break;
  558. /* stores */
  559. case SH_OPCODE_Y2:
  560. mem_op = MEMOP_STORE;
  561. size = 2;
  562. break;
  563. case SW_OPCODE_Y2:
  564. mem_op = MEMOP_STORE;
  565. size = 4;
  566. break;
  567. }
  568. }
  569. /*
  570. * Check if we need to rewrite an unaligned load/store.
  571. * Returning zero is a special value meaning we generated a signal.
  572. */
  573. if (mem_op != MEMOP_NONE && align_ctl >= 0) {
  574. bundle = rewrite_load_store_unaligned(state, bundle, regs,
  575. mem_op, size, sign_ext);
  576. if (bundle == 0)
  577. return;
  578. }
  579. /* write the bundle to our execution area */
  580. buffer = state->buffer;
  581. err = __put_user(bundle, buffer++);
  582. /*
  583. * If we're really single-stepping, we take an INT_ILL after.
  584. * If we're just handling an unaligned access, we can just
  585. * jump directly back to where we were in user code.
  586. */
  587. if (is_single_step) {
  588. err |= __put_user(__single_step_ill_insn, buffer++);
  589. err |= __put_user(__single_step_ill_insn, buffer++);
  590. } else {
  591. long delta;
  592. if (state->update) {
  593. /* We have some state to update; do it inline */
  594. int ha16;
  595. bundle = __single_step_addli_insn;
  596. bundle |= create_Dest_X1(state->update_reg);
  597. bundle |= create_Imm16_X1(state->update_value);
  598. err |= __put_user(bundle, buffer++);
  599. bundle = __single_step_auli_insn;
  600. bundle |= create_Dest_X1(state->update_reg);
  601. bundle |= create_SrcA_X1(state->update_reg);
  602. ha16 = (state->update_value + 0x8000) >> 16;
  603. bundle |= create_Imm16_X1(ha16);
  604. err |= __put_user(bundle, buffer++);
  605. state->update = 0;
  606. }
  607. /* End with a jump back to the next instruction */
  608. delta = ((regs->pc + TILEPRO_BUNDLE_SIZE_IN_BYTES) -
  609. (unsigned long)buffer) >>
  610. TILEPRO_LOG2_BUNDLE_ALIGNMENT_IN_BYTES;
  611. bundle = __single_step_j_insn;
  612. bundle |= create_JOffLong_X1(delta);
  613. err |= __put_user(bundle, buffer++);
  614. }
  615. if (err) {
  616. pr_err("Fault when writing to single-step buffer\n");
  617. return;
  618. }
  619. /*
  620. * Flush the buffer.
  621. * We do a local flush only, since this is a thread-specific buffer.
  622. */
  623. __flush_icache_range((unsigned long)state->buffer,
  624. (unsigned long)buffer);
  625. /* Indicate enabled */
  626. state->is_enabled = is_single_step;
  627. regs->pc = (unsigned long)state->buffer;
  628. /* Fault immediately if we are coming back from a syscall. */
  629. if (regs->faultnum == INT_SWINT_1)
  630. regs->pc += 8;
  631. }
  632. #else
  633. static DEFINE_PER_CPU(unsigned long, ss_saved_pc);
  634. /*
  635. * Called directly on the occasion of an interrupt.
  636. *
  637. * If the process doesn't have single step set, then we use this as an
  638. * opportunity to turn single step off.
  639. *
  640. * It has been mentioned that we could conditionally turn off single stepping
  641. * on each entry into the kernel and rely on single_step_once to turn it
  642. * on for the processes that matter (as we already do), but this
  643. * implementation is somewhat more efficient in that we muck with registers
  644. * once on a bum interrupt rather than on every entry into the kernel.
  645. *
  646. * If SINGLE_STEP_CONTROL_K has CANCELED set, then an interrupt occurred,
  647. * so we have to run through this process again before we can say that an
  648. * instruction has executed.
  649. *
  650. * swint will set CANCELED, but it's a legitimate instruction. Fortunately
  651. * it changes the PC. If it hasn't changed, then we know that the interrupt
  652. * wasn't generated by swint and we'll need to run this process again before
  653. * we can say an instruction has executed.
  654. *
  655. * If either CANCELED == 0 or the PC's changed, we send out SIGTRAPs and get
  656. * on with our lives.
  657. */
  658. void gx_singlestep_handle(struct pt_regs *regs, int fault_num)
  659. {
  660. unsigned long *ss_pc = &__get_cpu_var(ss_saved_pc);
  661. struct thread_info *info = (void *)current_thread_info();
  662. int is_single_step = test_ti_thread_flag(info, TIF_SINGLESTEP);
  663. unsigned long control = __insn_mfspr(SPR_SINGLE_STEP_CONTROL_K);
  664. if (is_single_step == 0) {
  665. __insn_mtspr(SPR_SINGLE_STEP_EN_K_K, 0);
  666. } else if ((*ss_pc != regs->pc) ||
  667. (!(control & SPR_SINGLE_STEP_CONTROL_1__CANCELED_MASK))) {
  668. control |= SPR_SINGLE_STEP_CONTROL_1__CANCELED_MASK;
  669. control |= SPR_SINGLE_STEP_CONTROL_1__INHIBIT_MASK;
  670. __insn_mtspr(SPR_SINGLE_STEP_CONTROL_K, control);
  671. send_sigtrap(current, regs);
  672. }
  673. }
  674. /*
  675. * Called from need_singlestep. Set up the control registers and the enable
  676. * register, then return back.
  677. */
  678. void single_step_once(struct pt_regs *regs)
  679. {
  680. unsigned long *ss_pc = &__get_cpu_var(ss_saved_pc);
  681. unsigned long control = __insn_mfspr(SPR_SINGLE_STEP_CONTROL_K);
  682. *ss_pc = regs->pc;
  683. control |= SPR_SINGLE_STEP_CONTROL_1__CANCELED_MASK;
  684. control |= SPR_SINGLE_STEP_CONTROL_1__INHIBIT_MASK;
  685. __insn_mtspr(SPR_SINGLE_STEP_CONTROL_K, control);
  686. __insn_mtspr(SPR_SINGLE_STEP_EN_K_K, 1 << USER_PL);
  687. }
  688. void single_step_execve(void)
  689. {
  690. /* Nothing */
  691. }
  692. #endif /* !__tilegx__ */