single_step.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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 unaligned %s at %#lx\n",
  200. current->pid, current->comm, regs->pc,
  201. mem_op == MEMOP_LOAD || mem_op == MEMOP_LOAD_POSTINCR ?
  202. "load" : "store",
  203. (unsigned long)addr);
  204. if (!unaligned_printk) {
  205. #define P pr_info
  206. P("\n");
  207. P("Unaligned fixups in the kernel will slow your application considerably.\n");
  208. P("To find them, write a \"1\" to /proc/sys/tile/unaligned_fixup/printk,\n");
  209. P("which requests the kernel show all unaligned fixups, or write a \"0\"\n");
  210. P("to /proc/sys/tile/unaligned_fixup/enabled, in which case each unaligned\n");
  211. P("access will become a SIGBUS you can debug. No further warnings will be\n");
  212. P("shown so as to avoid additional slowdown, but you can track the number\n");
  213. P("of fixups performed via /proc/sys/tile/unaligned_fixup/count.\n");
  214. P("Use the tile-addr2line command (see \"info addr2line\") to decode PCs.\n");
  215. P("\n");
  216. #undef P
  217. }
  218. }
  219. ++unaligned_fixup_count;
  220. if (bundle & TILEPRO_BUNDLE_Y_ENCODING_MASK) {
  221. /* Convert the Y2 instruction to a prefetch. */
  222. bundle &= ~(create_SrcBDest_Y2(-1) |
  223. create_Opcode_Y2(-1));
  224. bundle |= (create_SrcBDest_Y2(TREG_ZERO) |
  225. create_Opcode_Y2(LW_OPCODE_Y2));
  226. /* Replace the load postincr with an addi */
  227. } else if (mem_op == MEMOP_LOAD_POSTINCR) {
  228. bundle = addi_X1(bundle, addr_reg, addr_reg,
  229. get_Imm8_X1(bundle));
  230. /* Replace the store postincr with an addi */
  231. } else if (mem_op == MEMOP_STORE_POSTINCR) {
  232. bundle = addi_X1(bundle, addr_reg, addr_reg,
  233. get_Dest_Imm8_X1(bundle));
  234. } else {
  235. /* Convert the X1 instruction to a nop. */
  236. bundle &= ~(create_Opcode_X1(-1) |
  237. create_UnShOpcodeExtension_X1(-1) |
  238. create_UnOpcodeExtension_X1(-1));
  239. bundle |= (create_Opcode_X1(SHUN_0_OPCODE_X1) |
  240. create_UnShOpcodeExtension_X1(
  241. UN_0_SHUN_0_OPCODE_X1) |
  242. create_UnOpcodeExtension_X1(
  243. NOP_UN_0_SHUN_0_OPCODE_X1));
  244. }
  245. return bundle;
  246. }
  247. /*
  248. * Called after execve() has started the new image. This allows us
  249. * to reset the info state. Note that the the mmap'ed memory, if there
  250. * was any, has already been unmapped by the exec.
  251. */
  252. void single_step_execve(void)
  253. {
  254. struct thread_info *ti = current_thread_info();
  255. kfree(ti->step_state);
  256. ti->step_state = NULL;
  257. }
  258. /*
  259. * single_step_once() - entry point when single stepping has been triggered.
  260. * @regs: The machine register state
  261. *
  262. * When we arrive at this routine via a trampoline, the single step
  263. * engine copies the executing bundle to the single step buffer.
  264. * If the instruction is a condition branch, then the target is
  265. * reset to one past the next instruction. If the instruction
  266. * sets the lr, then that is noted. If the instruction is a jump
  267. * or call, then the new target pc is preserved and the current
  268. * bundle instruction set to null.
  269. *
  270. * The necessary post-single-step rewriting information is stored in
  271. * single_step_state-> We use data segment values because the
  272. * stack will be rewound when we run the rewritten single-stepped
  273. * instruction.
  274. */
  275. void single_step_once(struct pt_regs *regs)
  276. {
  277. extern tilepro_bundle_bits __single_step_ill_insn;
  278. extern tilepro_bundle_bits __single_step_j_insn;
  279. extern tilepro_bundle_bits __single_step_addli_insn;
  280. extern tilepro_bundle_bits __single_step_auli_insn;
  281. struct thread_info *info = (void *)current_thread_info();
  282. struct single_step_state *state = info->step_state;
  283. int is_single_step = test_ti_thread_flag(info, TIF_SINGLESTEP);
  284. tilepro_bundle_bits __user *buffer, *pc;
  285. tilepro_bundle_bits bundle;
  286. int temp_reg;
  287. int target_reg = TREG_LR;
  288. int err;
  289. enum mem_op mem_op = MEMOP_NONE;
  290. int size = 0, sign_ext = 0; /* happy compiler */
  291. int align_ctl;
  292. align_ctl = unaligned_fixup;
  293. switch (task_thread_info(current)->align_ctl) {
  294. case PR_UNALIGN_NOPRINT:
  295. align_ctl = 1;
  296. break;
  297. case PR_UNALIGN_SIGBUS:
  298. align_ctl = 0;
  299. break;
  300. }
  301. asm(
  302. " .pushsection .rodata.single_step\n"
  303. " .align 8\n"
  304. " .globl __single_step_ill_insn\n"
  305. "__single_step_ill_insn:\n"
  306. " ill\n"
  307. " .globl __single_step_addli_insn\n"
  308. "__single_step_addli_insn:\n"
  309. " { nop; addli r0, zero, 0 }\n"
  310. " .globl __single_step_auli_insn\n"
  311. "__single_step_auli_insn:\n"
  312. " { nop; auli r0, r0, 0 }\n"
  313. " .globl __single_step_j_insn\n"
  314. "__single_step_j_insn:\n"
  315. " j .\n"
  316. " .popsection\n"
  317. );
  318. /*
  319. * Enable interrupts here to allow touching userspace and the like.
  320. * The callers expect this: do_trap() already has interrupts
  321. * enabled, and do_work_pending() handles functions that enable
  322. * interrupts internally.
  323. */
  324. local_irq_enable();
  325. if (state == NULL) {
  326. /* allocate a page of writable, executable memory */
  327. state = kmalloc(sizeof(struct single_step_state), GFP_KERNEL);
  328. if (state == NULL) {
  329. pr_err("Out of kernel memory trying to single-step\n");
  330. return;
  331. }
  332. /* allocate a cache line of writable, executable memory */
  333. buffer = (void __user *) vm_mmap(NULL, 0, 64,
  334. PROT_EXEC | PROT_READ | PROT_WRITE,
  335. MAP_PRIVATE | MAP_ANONYMOUS,
  336. 0);
  337. if (IS_ERR((void __force *)buffer)) {
  338. kfree(state);
  339. pr_err("Out of kernel pages trying to single-step\n");
  340. return;
  341. }
  342. state->buffer = buffer;
  343. state->is_enabled = 0;
  344. info->step_state = state;
  345. /* Validate our stored instruction patterns */
  346. BUG_ON(get_Opcode_X1(__single_step_addli_insn) !=
  347. ADDLI_OPCODE_X1);
  348. BUG_ON(get_Opcode_X1(__single_step_auli_insn) !=
  349. AULI_OPCODE_X1);
  350. BUG_ON(get_SrcA_X1(__single_step_addli_insn) != TREG_ZERO);
  351. BUG_ON(get_Dest_X1(__single_step_addli_insn) != 0);
  352. BUG_ON(get_JOffLong_X1(__single_step_j_insn) != 0);
  353. }
  354. /*
  355. * If we are returning from a syscall, we still haven't hit the
  356. * "ill" for the swint1 instruction. So back the PC up to be
  357. * pointing at the swint1, but we'll actually return directly
  358. * back to the "ill" so we come back in via SIGILL as if we
  359. * had "executed" the swint1 without ever being in kernel space.
  360. */
  361. if (regs->faultnum == INT_SWINT_1)
  362. regs->pc -= 8;
  363. pc = (tilepro_bundle_bits __user *)(regs->pc);
  364. if (get_user(bundle, pc) != 0) {
  365. pr_err("Couldn't read instruction at %p trying to step\n", pc);
  366. return;
  367. }
  368. /* We'll follow the instruction with 2 ill op bundles */
  369. state->orig_pc = (unsigned long)pc;
  370. state->next_pc = (unsigned long)(pc + 1);
  371. state->branch_next_pc = 0;
  372. state->update = 0;
  373. if (!(bundle & TILEPRO_BUNDLE_Y_ENCODING_MASK)) {
  374. /* two wide, check for control flow */
  375. int opcode = get_Opcode_X1(bundle);
  376. switch (opcode) {
  377. /* branches */
  378. case BRANCH_OPCODE_X1:
  379. {
  380. s32 offset = signExtend17(get_BrOff_X1(bundle));
  381. /*
  382. * For branches, we use a rewriting trick to let the
  383. * hardware evaluate whether the branch is taken or
  384. * untaken. We record the target offset and then
  385. * rewrite the branch instruction to target 1 insn
  386. * ahead if the branch is taken. We then follow the
  387. * rewritten branch with two bundles, each containing
  388. * an "ill" instruction. The supervisor examines the
  389. * pc after the single step code is executed, and if
  390. * the pc is the first ill instruction, then the
  391. * branch (if any) was not taken. If the pc is the
  392. * second ill instruction, then the branch was
  393. * taken. The new pc is computed for these cases, and
  394. * inserted into the registers for the thread. If
  395. * the pc is the start of the single step code, then
  396. * an exception or interrupt was taken before the
  397. * code started processing, and the same "original"
  398. * pc is restored. This change, different from the
  399. * original implementation, has the advantage of
  400. * executing a single user instruction.
  401. */
  402. state->branch_next_pc = (unsigned long)(pc + offset);
  403. /* rewrite branch offset to go forward one bundle */
  404. bundle = set_BrOff_X1(bundle, 2);
  405. }
  406. break;
  407. /* jumps */
  408. case JALB_OPCODE_X1:
  409. case JALF_OPCODE_X1:
  410. state->update = 1;
  411. state->next_pc =
  412. (unsigned long) (pc + get_JOffLong_X1(bundle));
  413. break;
  414. case JB_OPCODE_X1:
  415. case JF_OPCODE_X1:
  416. state->next_pc =
  417. (unsigned long) (pc + get_JOffLong_X1(bundle));
  418. bundle = nop_X1(bundle);
  419. break;
  420. case SPECIAL_0_OPCODE_X1:
  421. switch (get_RRROpcodeExtension_X1(bundle)) {
  422. /* jump-register */
  423. case JALRP_SPECIAL_0_OPCODE_X1:
  424. case JALR_SPECIAL_0_OPCODE_X1:
  425. state->update = 1;
  426. state->next_pc =
  427. regs->regs[get_SrcA_X1(bundle)];
  428. break;
  429. case JRP_SPECIAL_0_OPCODE_X1:
  430. case JR_SPECIAL_0_OPCODE_X1:
  431. state->next_pc =
  432. regs->regs[get_SrcA_X1(bundle)];
  433. bundle = nop_X1(bundle);
  434. break;
  435. case LNK_SPECIAL_0_OPCODE_X1:
  436. state->update = 1;
  437. target_reg = get_Dest_X1(bundle);
  438. break;
  439. /* stores */
  440. case SH_SPECIAL_0_OPCODE_X1:
  441. mem_op = MEMOP_STORE;
  442. size = 2;
  443. break;
  444. case SW_SPECIAL_0_OPCODE_X1:
  445. mem_op = MEMOP_STORE;
  446. size = 4;
  447. break;
  448. }
  449. break;
  450. /* loads and iret */
  451. case SHUN_0_OPCODE_X1:
  452. if (get_UnShOpcodeExtension_X1(bundle) ==
  453. UN_0_SHUN_0_OPCODE_X1) {
  454. switch (get_UnOpcodeExtension_X1(bundle)) {
  455. case LH_UN_0_SHUN_0_OPCODE_X1:
  456. mem_op = MEMOP_LOAD;
  457. size = 2;
  458. sign_ext = 1;
  459. break;
  460. case LH_U_UN_0_SHUN_0_OPCODE_X1:
  461. mem_op = MEMOP_LOAD;
  462. size = 2;
  463. sign_ext = 0;
  464. break;
  465. case LW_UN_0_SHUN_0_OPCODE_X1:
  466. mem_op = MEMOP_LOAD;
  467. size = 4;
  468. break;
  469. case IRET_UN_0_SHUN_0_OPCODE_X1:
  470. {
  471. unsigned long ex0_0 = __insn_mfspr(
  472. SPR_EX_CONTEXT_0_0);
  473. unsigned long ex0_1 = __insn_mfspr(
  474. SPR_EX_CONTEXT_0_1);
  475. /*
  476. * Special-case it if we're iret'ing
  477. * to PL0 again. Otherwise just let
  478. * it run and it will generate SIGILL.
  479. */
  480. if (EX1_PL(ex0_1) == USER_PL) {
  481. state->next_pc = ex0_0;
  482. regs->ex1 = ex0_1;
  483. bundle = nop_X1(bundle);
  484. }
  485. }
  486. }
  487. }
  488. break;
  489. /* postincrement operations */
  490. case IMM_0_OPCODE_X1:
  491. switch (get_ImmOpcodeExtension_X1(bundle)) {
  492. case LWADD_IMM_0_OPCODE_X1:
  493. mem_op = MEMOP_LOAD_POSTINCR;
  494. size = 4;
  495. break;
  496. case LHADD_IMM_0_OPCODE_X1:
  497. mem_op = MEMOP_LOAD_POSTINCR;
  498. size = 2;
  499. sign_ext = 1;
  500. break;
  501. case LHADD_U_IMM_0_OPCODE_X1:
  502. mem_op = MEMOP_LOAD_POSTINCR;
  503. size = 2;
  504. sign_ext = 0;
  505. break;
  506. case SWADD_IMM_0_OPCODE_X1:
  507. mem_op = MEMOP_STORE_POSTINCR;
  508. size = 4;
  509. break;
  510. case SHADD_IMM_0_OPCODE_X1:
  511. mem_op = MEMOP_STORE_POSTINCR;
  512. size = 2;
  513. break;
  514. default:
  515. break;
  516. }
  517. break;
  518. }
  519. if (state->update) {
  520. /*
  521. * Get an available register. We start with a
  522. * bitmask with 1's for available registers.
  523. * We truncate to the low 32 registers since
  524. * we are guaranteed to have set bits in the
  525. * low 32 bits, then use ctz to pick the first.
  526. */
  527. u32 mask = (u32) ~((1ULL << get_Dest_X0(bundle)) |
  528. (1ULL << get_SrcA_X0(bundle)) |
  529. (1ULL << get_SrcB_X0(bundle)) |
  530. (1ULL << target_reg));
  531. temp_reg = __builtin_ctz(mask);
  532. state->update_reg = temp_reg;
  533. state->update_value = regs->regs[temp_reg];
  534. regs->regs[temp_reg] = (unsigned long) (pc+1);
  535. regs->flags |= PT_FLAGS_RESTORE_REGS;
  536. bundle = move_X1(bundle, target_reg, temp_reg);
  537. }
  538. } else {
  539. int opcode = get_Opcode_Y2(bundle);
  540. switch (opcode) {
  541. /* loads */
  542. case LH_OPCODE_Y2:
  543. mem_op = MEMOP_LOAD;
  544. size = 2;
  545. sign_ext = 1;
  546. break;
  547. case LH_U_OPCODE_Y2:
  548. mem_op = MEMOP_LOAD;
  549. size = 2;
  550. sign_ext = 0;
  551. break;
  552. case LW_OPCODE_Y2:
  553. mem_op = MEMOP_LOAD;
  554. size = 4;
  555. break;
  556. /* stores */
  557. case SH_OPCODE_Y2:
  558. mem_op = MEMOP_STORE;
  559. size = 2;
  560. break;
  561. case SW_OPCODE_Y2:
  562. mem_op = MEMOP_STORE;
  563. size = 4;
  564. break;
  565. }
  566. }
  567. /*
  568. * Check if we need to rewrite an unaligned load/store.
  569. * Returning zero is a special value meaning we generated a signal.
  570. */
  571. if (mem_op != MEMOP_NONE && align_ctl >= 0) {
  572. bundle = rewrite_load_store_unaligned(state, bundle, regs,
  573. mem_op, size, sign_ext);
  574. if (bundle == 0)
  575. return;
  576. }
  577. /* write the bundle to our execution area */
  578. buffer = state->buffer;
  579. err = __put_user(bundle, buffer++);
  580. /*
  581. * If we're really single-stepping, we take an INT_ILL after.
  582. * If we're just handling an unaligned access, we can just
  583. * jump directly back to where we were in user code.
  584. */
  585. if (is_single_step) {
  586. err |= __put_user(__single_step_ill_insn, buffer++);
  587. err |= __put_user(__single_step_ill_insn, buffer++);
  588. } else {
  589. long delta;
  590. if (state->update) {
  591. /* We have some state to update; do it inline */
  592. int ha16;
  593. bundle = __single_step_addli_insn;
  594. bundle |= create_Dest_X1(state->update_reg);
  595. bundle |= create_Imm16_X1(state->update_value);
  596. err |= __put_user(bundle, buffer++);
  597. bundle = __single_step_auli_insn;
  598. bundle |= create_Dest_X1(state->update_reg);
  599. bundle |= create_SrcA_X1(state->update_reg);
  600. ha16 = (state->update_value + 0x8000) >> 16;
  601. bundle |= create_Imm16_X1(ha16);
  602. err |= __put_user(bundle, buffer++);
  603. state->update = 0;
  604. }
  605. /* End with a jump back to the next instruction */
  606. delta = ((regs->pc + TILEPRO_BUNDLE_SIZE_IN_BYTES) -
  607. (unsigned long)buffer) >>
  608. TILEPRO_LOG2_BUNDLE_ALIGNMENT_IN_BYTES;
  609. bundle = __single_step_j_insn;
  610. bundle |= create_JOffLong_X1(delta);
  611. err |= __put_user(bundle, buffer++);
  612. }
  613. if (err) {
  614. pr_err("Fault when writing to single-step buffer\n");
  615. return;
  616. }
  617. /*
  618. * Flush the buffer.
  619. * We do a local flush only, since this is a thread-specific buffer.
  620. */
  621. __flush_icache_range((unsigned long)state->buffer,
  622. (unsigned long)buffer);
  623. /* Indicate enabled */
  624. state->is_enabled = is_single_step;
  625. regs->pc = (unsigned long)state->buffer;
  626. /* Fault immediately if we are coming back from a syscall. */
  627. if (regs->faultnum == INT_SWINT_1)
  628. regs->pc += 8;
  629. }
  630. #else
  631. static DEFINE_PER_CPU(unsigned long, ss_saved_pc);
  632. /*
  633. * Called directly on the occasion of an interrupt.
  634. *
  635. * If the process doesn't have single step set, then we use this as an
  636. * opportunity to turn single step off.
  637. *
  638. * It has been mentioned that we could conditionally turn off single stepping
  639. * on each entry into the kernel and rely on single_step_once to turn it
  640. * on for the processes that matter (as we already do), but this
  641. * implementation is somewhat more efficient in that we muck with registers
  642. * once on a bum interrupt rather than on every entry into the kernel.
  643. *
  644. * If SINGLE_STEP_CONTROL_K has CANCELED set, then an interrupt occurred,
  645. * so we have to run through this process again before we can say that an
  646. * instruction has executed.
  647. *
  648. * swint will set CANCELED, but it's a legitimate instruction. Fortunately
  649. * it changes the PC. If it hasn't changed, then we know that the interrupt
  650. * wasn't generated by swint and we'll need to run this process again before
  651. * we can say an instruction has executed.
  652. *
  653. * If either CANCELED == 0 or the PC's changed, we send out SIGTRAPs and get
  654. * on with our lives.
  655. */
  656. void gx_singlestep_handle(struct pt_regs *regs, int fault_num)
  657. {
  658. unsigned long *ss_pc = this_cpu_ptr(&ss_saved_pc);
  659. struct thread_info *info = (void *)current_thread_info();
  660. int is_single_step = test_ti_thread_flag(info, TIF_SINGLESTEP);
  661. unsigned long control = __insn_mfspr(SPR_SINGLE_STEP_CONTROL_K);
  662. if (is_single_step == 0) {
  663. __insn_mtspr(SPR_SINGLE_STEP_EN_K_K, 0);
  664. } else if ((*ss_pc != regs->pc) ||
  665. (!(control & SPR_SINGLE_STEP_CONTROL_1__CANCELED_MASK))) {
  666. control |= SPR_SINGLE_STEP_CONTROL_1__CANCELED_MASK;
  667. control |= SPR_SINGLE_STEP_CONTROL_1__INHIBIT_MASK;
  668. __insn_mtspr(SPR_SINGLE_STEP_CONTROL_K, control);
  669. send_sigtrap(current, regs);
  670. }
  671. }
  672. /*
  673. * Called from need_singlestep. Set up the control registers and the enable
  674. * register, then return back.
  675. */
  676. void single_step_once(struct pt_regs *regs)
  677. {
  678. unsigned long *ss_pc = this_cpu_ptr(&ss_saved_pc);
  679. unsigned long control = __insn_mfspr(SPR_SINGLE_STEP_CONTROL_K);
  680. *ss_pc = regs->pc;
  681. control |= SPR_SINGLE_STEP_CONTROL_1__CANCELED_MASK;
  682. control |= SPR_SINGLE_STEP_CONTROL_1__INHIBIT_MASK;
  683. __insn_mtspr(SPR_SINGLE_STEP_CONTROL_K, control);
  684. __insn_mtspr(SPR_SINGLE_STEP_EN_K_K, 1 << USER_PL);
  685. }
  686. void single_step_execve(void)
  687. {
  688. /* Nothing */
  689. }
  690. #endif /* !__tilegx__ */