single_step.c 21 KB

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