single_step.c 22 KB

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