2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
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.
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
14 * A code-rewriter that enables instruction single-stepping.
15 * Derived from iLib's single-stepping code.
18 #ifndef __tilegx__ /* Hardware support for single step unavailable. */
20 /* These functions are only used on the TILE platform */
21 #include <linux/slab.h>
22 #include <linux/thread_info.h>
23 #include <linux/uaccess.h>
24 #include <linux/mman.h>
25 #include <linux/types.h>
26 #include <linux/err.h>
27 #include <asm/cacheflush.h>
29 #include <arch/opcode.h>
31 #define signExtend17(val) sign_extend((val), 17)
32 #define TILE_X1_MASK (0xffffffffULL << 31)
36 static int __init
setup_unaligned_printk(char *str
)
39 if (strict_strtol(str
, 0, &val
) != 0)
41 unaligned_printk
= val
;
42 pr_info("Printk for each unaligned data accesses is %s\n",
43 unaligned_printk
? "enabled" : "disabled");
46 __setup("unaligned_printk=", setup_unaligned_printk
);
48 unsigned int unaligned_fixup_count
;
58 static inline tile_bundle_bits
set_BrOff_X1(tile_bundle_bits n
, s32 offset
)
60 tile_bundle_bits result
;
62 /* mask out the old offset */
63 tile_bundle_bits mask
= create_BrOff_X1(-1);
66 /* or in the new offset */
67 result
|= create_BrOff_X1(offset
);
72 static inline tile_bundle_bits
move_X1(tile_bundle_bits n
, int dest
, int src
)
74 tile_bundle_bits result
;
77 result
= n
& (~TILE_X1_MASK
);
79 op
= create_Opcode_X1(SPECIAL_0_OPCODE_X1
) |
80 create_RRROpcodeExtension_X1(OR_SPECIAL_0_OPCODE_X1
) |
81 create_Dest_X1(dest
) |
82 create_SrcB_X1(TREG_ZERO
) |
89 static inline tile_bundle_bits
nop_X1(tile_bundle_bits n
)
91 return move_X1(n
, TREG_ZERO
, TREG_ZERO
);
94 static inline tile_bundle_bits
addi_X1(
95 tile_bundle_bits n
, int dest
, int src
, int imm
)
99 n
|= (create_SrcA_X1(src
) |
100 create_Dest_X1(dest
) |
101 create_Imm8_X1(imm
) |
103 create_Opcode_X1(IMM_0_OPCODE_X1
) |
104 create_ImmOpcodeExtension_X1(ADDI_IMM_0_OPCODE_X1
));
109 static tile_bundle_bits
rewrite_load_store_unaligned(
110 struct single_step_state
*state
,
111 tile_bundle_bits bundle
,
112 struct pt_regs
*regs
,
114 int size
, int sign_ext
)
116 unsigned char __user
*addr
;
117 int val_reg
, addr_reg
, err
, val
;
119 /* Get address and value registers */
120 if (bundle
& TILEPRO_BUNDLE_Y_ENCODING_MASK
) {
121 addr_reg
= get_SrcA_Y2(bundle
);
122 val_reg
= get_SrcBDest_Y2(bundle
);
123 } else if (mem_op
== MEMOP_LOAD
|| mem_op
== MEMOP_LOAD_POSTINCR
) {
124 addr_reg
= get_SrcA_X1(bundle
);
125 val_reg
= get_Dest_X1(bundle
);
127 addr_reg
= get_SrcA_X1(bundle
);
128 val_reg
= get_SrcB_X1(bundle
);
132 * If registers are not GPRs, don't try to handle it.
134 * FIXME: we could handle non-GPR loads by getting the real value
135 * from memory, writing it to the single step buffer, using a
136 * temp_reg to hold a pointer to that memory, then executing that
137 * instruction and resetting temp_reg. For non-GPR stores, it's a
138 * little trickier; we could use the single step buffer for that
139 * too, but we'd have to add some more state bits so that we could
140 * call back in here to copy that value to the real target. For
141 * now, we just handle the simple case.
143 if ((val_reg
>= PTREGS_NR_GPRS
&&
144 (val_reg
!= TREG_ZERO
||
145 mem_op
== MEMOP_LOAD
||
146 mem_op
== MEMOP_LOAD_POSTINCR
)) ||
147 addr_reg
>= PTREGS_NR_GPRS
)
150 /* If it's aligned, don't handle it specially */
151 addr
= (void __user
*)regs
->regs
[addr_reg
];
152 if (((unsigned long)addr
% size
) == 0)
155 #ifndef __LITTLE_ENDIAN
156 # error We assume little-endian representation with copy_xx_user size 2 here
158 /* Handle unaligned load/store */
159 if (mem_op
== MEMOP_LOAD
|| mem_op
== MEMOP_LOAD_POSTINCR
) {
160 unsigned short val_16
;
163 err
= copy_from_user(&val_16
, addr
, sizeof(val_16
));
164 val
= sign_ext
? ((short)val_16
) : val_16
;
167 err
= copy_from_user(&val
, addr
, sizeof(val
));
173 state
->update_reg
= val_reg
;
174 state
->update_value
= val
;
178 val
= (val_reg
== TREG_ZERO
) ? 0 : regs
->regs
[val_reg
];
179 err
= copy_to_user(addr
, &val
, size
);
185 .si_code
= SEGV_MAPERR
,
188 trace_unhandled_signal("segfault", regs
,
189 (unsigned long)addr
, SIGSEGV
);
190 force_sig_info(info
.si_signo
, &info
, current
);
191 return (tile_bundle_bits
) 0;
194 if (unaligned_fixup
== 0) {
197 .si_code
= BUS_ADRALN
,
200 trace_unhandled_signal("unaligned trap", regs
,
201 (unsigned long)addr
, SIGBUS
);
202 force_sig_info(info
.si_signo
, &info
, current
);
203 return (tile_bundle_bits
) 0;
206 if (unaligned_printk
|| unaligned_fixup_count
== 0) {
207 pr_info("Process %d/%s: PC %#lx: Fixup of"
208 " unaligned %s at %#lx.\n",
209 current
->pid
, current
->comm
, regs
->pc
,
210 (mem_op
== MEMOP_LOAD
||
211 mem_op
== MEMOP_LOAD_POSTINCR
) ?
213 (unsigned long)addr
);
214 if (!unaligned_printk
) {
217 P("Unaligned fixups in the kernel will slow your application considerably.\n");
218 P("To find them, write a \"1\" to /proc/sys/tile/unaligned_fixup/printk,\n");
219 P("which requests the kernel show all unaligned fixups, or write a \"0\"\n");
220 P("to /proc/sys/tile/unaligned_fixup/enabled, in which case each unaligned\n");
221 P("access will become a SIGBUS you can debug. No further warnings will be\n");
222 P("shown so as to avoid additional slowdown, but you can track the number\n");
223 P("of fixups performed via /proc/sys/tile/unaligned_fixup/count.\n");
224 P("Use the tile-addr2line command (see \"info addr2line\") to decode PCs.\n");
229 ++unaligned_fixup_count
;
231 if (bundle
& TILEPRO_BUNDLE_Y_ENCODING_MASK
) {
232 /* Convert the Y2 instruction to a prefetch. */
233 bundle
&= ~(create_SrcBDest_Y2(-1) |
234 create_Opcode_Y2(-1));
235 bundle
|= (create_SrcBDest_Y2(TREG_ZERO
) |
236 create_Opcode_Y2(LW_OPCODE_Y2
));
237 /* Replace the load postincr with an addi */
238 } else if (mem_op
== MEMOP_LOAD_POSTINCR
) {
239 bundle
= addi_X1(bundle
, addr_reg
, addr_reg
,
240 get_Imm8_X1(bundle
));
241 /* Replace the store postincr with an addi */
242 } else if (mem_op
== MEMOP_STORE_POSTINCR
) {
243 bundle
= addi_X1(bundle
, addr_reg
, addr_reg
,
244 get_Dest_Imm8_X1(bundle
));
246 /* Convert the X1 instruction to a nop. */
247 bundle
&= ~(create_Opcode_X1(-1) |
248 create_UnShOpcodeExtension_X1(-1) |
249 create_UnOpcodeExtension_X1(-1));
250 bundle
|= (create_Opcode_X1(SHUN_0_OPCODE_X1
) |
251 create_UnShOpcodeExtension_X1(
252 UN_0_SHUN_0_OPCODE_X1
) |
253 create_UnOpcodeExtension_X1(
254 NOP_UN_0_SHUN_0_OPCODE_X1
));
261 * Called after execve() has started the new image. This allows us
262 * to reset the info state. Note that the the mmap'ed memory, if there
263 * was any, has already been unmapped by the exec.
265 void single_step_execve(void)
267 struct thread_info
*ti
= current_thread_info();
268 kfree(ti
->step_state
);
269 ti
->step_state
= NULL
;
273 * single_step_once() - entry point when single stepping has been triggered.
274 * @regs: The machine register state
276 * When we arrive at this routine via a trampoline, the single step
277 * engine copies the executing bundle to the single step buffer.
278 * If the instruction is a condition branch, then the target is
279 * reset to one past the next instruction. If the instruction
280 * sets the lr, then that is noted. If the instruction is a jump
281 * or call, then the new target pc is preserved and the current
282 * bundle instruction set to null.
284 * The necessary post-single-step rewriting information is stored in
285 * single_step_state-> We use data segment values because the
286 * stack will be rewound when we run the rewritten single-stepped
289 void single_step_once(struct pt_regs
*regs
)
291 extern tile_bundle_bits __single_step_ill_insn
;
292 extern tile_bundle_bits __single_step_j_insn
;
293 extern tile_bundle_bits __single_step_addli_insn
;
294 extern tile_bundle_bits __single_step_auli_insn
;
295 struct thread_info
*info
= (void *)current_thread_info();
296 struct single_step_state
*state
= info
->step_state
;
297 int is_single_step
= test_ti_thread_flag(info
, TIF_SINGLESTEP
);
298 tile_bundle_bits __user
*buffer
, *pc
;
299 tile_bundle_bits bundle
;
301 int target_reg
= TREG_LR
;
303 enum mem_op mem_op
= MEMOP_NONE
;
304 int size
= 0, sign_ext
= 0; /* happy compiler */
307 " .pushsection .rodata.single_step\n"
309 " .globl __single_step_ill_insn\n"
310 "__single_step_ill_insn:\n"
312 " .globl __single_step_addli_insn\n"
313 "__single_step_addli_insn:\n"
314 " { nop; addli r0, zero, 0 }\n"
315 " .globl __single_step_auli_insn\n"
316 "__single_step_auli_insn:\n"
317 " { nop; auli r0, r0, 0 }\n"
318 " .globl __single_step_j_insn\n"
319 "__single_step_j_insn:\n"
325 * Enable interrupts here to allow touching userspace and the like.
326 * The callers expect this: do_trap() already has interrupts
327 * enabled, and do_work_pending() handles functions that enable
328 * interrupts internally.
333 /* allocate a page of writable, executable memory */
334 state
= kmalloc(sizeof(struct single_step_state
), GFP_KERNEL
);
336 pr_err("Out of kernel memory trying to single-step\n");
340 /* allocate a cache line of writable, executable memory */
341 down_write(¤t
->mm
->mmap_sem
);
342 buffer
= (void __user
*) do_mmap(NULL
, 0, 64,
343 PROT_EXEC
| PROT_READ
| PROT_WRITE
,
344 MAP_PRIVATE
| MAP_ANONYMOUS
,
346 up_write(¤t
->mm
->mmap_sem
);
348 if (IS_ERR((void __force
*)buffer
)) {
350 pr_err("Out of kernel pages trying to single-step\n");
354 state
->buffer
= buffer
;
355 state
->is_enabled
= 0;
357 info
->step_state
= state
;
359 /* Validate our stored instruction patterns */
360 BUG_ON(get_Opcode_X1(__single_step_addli_insn
) !=
362 BUG_ON(get_Opcode_X1(__single_step_auli_insn
) !=
364 BUG_ON(get_SrcA_X1(__single_step_addli_insn
) != TREG_ZERO
);
365 BUG_ON(get_Dest_X1(__single_step_addli_insn
) != 0);
366 BUG_ON(get_JOffLong_X1(__single_step_j_insn
) != 0);
370 * If we are returning from a syscall, we still haven't hit the
371 * "ill" for the swint1 instruction. So back the PC up to be
372 * pointing at the swint1, but we'll actually return directly
373 * back to the "ill" so we come back in via SIGILL as if we
374 * had "executed" the swint1 without ever being in kernel space.
376 if (regs
->faultnum
== INT_SWINT_1
)
379 pc
= (tile_bundle_bits __user
*)(regs
->pc
);
380 if (get_user(bundle
, pc
) != 0) {
381 pr_err("Couldn't read instruction at %p trying to step\n", pc
);
385 /* We'll follow the instruction with 2 ill op bundles */
386 state
->orig_pc
= (unsigned long)pc
;
387 state
->next_pc
= (unsigned long)(pc
+ 1);
388 state
->branch_next_pc
= 0;
391 if (!(bundle
& TILEPRO_BUNDLE_Y_ENCODING_MASK
)) {
392 /* two wide, check for control flow */
393 int opcode
= get_Opcode_X1(bundle
);
397 case BRANCH_OPCODE_X1
:
399 s32 offset
= signExtend17(get_BrOff_X1(bundle
));
402 * For branches, we use a rewriting trick to let the
403 * hardware evaluate whether the branch is taken or
404 * untaken. We record the target offset and then
405 * rewrite the branch instruction to target 1 insn
406 * ahead if the branch is taken. We then follow the
407 * rewritten branch with two bundles, each containing
408 * an "ill" instruction. The supervisor examines the
409 * pc after the single step code is executed, and if
410 * the pc is the first ill instruction, then the
411 * branch (if any) was not taken. If the pc is the
412 * second ill instruction, then the branch was
413 * taken. The new pc is computed for these cases, and
414 * inserted into the registers for the thread. If
415 * the pc is the start of the single step code, then
416 * an exception or interrupt was taken before the
417 * code started processing, and the same "original"
418 * pc is restored. This change, different from the
419 * original implementation, has the advantage of
420 * executing a single user instruction.
422 state
->branch_next_pc
= (unsigned long)(pc
+ offset
);
424 /* rewrite branch offset to go forward one bundle */
425 bundle
= set_BrOff_X1(bundle
, 2);
434 (unsigned long) (pc
+ get_JOffLong_X1(bundle
));
440 (unsigned long) (pc
+ get_JOffLong_X1(bundle
));
441 bundle
= nop_X1(bundle
);
444 case SPECIAL_0_OPCODE_X1
:
445 switch (get_RRROpcodeExtension_X1(bundle
)) {
447 case JALRP_SPECIAL_0_OPCODE_X1
:
448 case JALR_SPECIAL_0_OPCODE_X1
:
451 regs
->regs
[get_SrcA_X1(bundle
)];
454 case JRP_SPECIAL_0_OPCODE_X1
:
455 case JR_SPECIAL_0_OPCODE_X1
:
457 regs
->regs
[get_SrcA_X1(bundle
)];
458 bundle
= nop_X1(bundle
);
461 case LNK_SPECIAL_0_OPCODE_X1
:
463 target_reg
= get_Dest_X1(bundle
);
467 case SH_SPECIAL_0_OPCODE_X1
:
468 mem_op
= MEMOP_STORE
;
472 case SW_SPECIAL_0_OPCODE_X1
:
473 mem_op
= MEMOP_STORE
;
480 case SHUN_0_OPCODE_X1
:
481 if (get_UnShOpcodeExtension_X1(bundle
) ==
482 UN_0_SHUN_0_OPCODE_X1
) {
483 switch (get_UnOpcodeExtension_X1(bundle
)) {
484 case LH_UN_0_SHUN_0_OPCODE_X1
:
490 case LH_U_UN_0_SHUN_0_OPCODE_X1
:
496 case LW_UN_0_SHUN_0_OPCODE_X1
:
501 case IRET_UN_0_SHUN_0_OPCODE_X1
:
503 unsigned long ex0_0
= __insn_mfspr(
505 unsigned long ex0_1
= __insn_mfspr(
508 * Special-case it if we're iret'ing
509 * to PL0 again. Otherwise just let
510 * it run and it will generate SIGILL.
512 if (EX1_PL(ex0_1
) == USER_PL
) {
513 state
->next_pc
= ex0_0
;
515 bundle
= nop_X1(bundle
);
523 /* postincrement operations */
524 case IMM_0_OPCODE_X1
:
525 switch (get_ImmOpcodeExtension_X1(bundle
)) {
526 case LWADD_IMM_0_OPCODE_X1
:
527 mem_op
= MEMOP_LOAD_POSTINCR
;
531 case LHADD_IMM_0_OPCODE_X1
:
532 mem_op
= MEMOP_LOAD_POSTINCR
;
537 case LHADD_U_IMM_0_OPCODE_X1
:
538 mem_op
= MEMOP_LOAD_POSTINCR
;
543 case SWADD_IMM_0_OPCODE_X1
:
544 mem_op
= MEMOP_STORE_POSTINCR
;
548 case SHADD_IMM_0_OPCODE_X1
:
549 mem_op
= MEMOP_STORE_POSTINCR
;
557 #endif /* CHIP_HAS_WH64() */
562 * Get an available register. We start with a
563 * bitmask with 1's for available registers.
564 * We truncate to the low 32 registers since
565 * we are guaranteed to have set bits in the
566 * low 32 bits, then use ctz to pick the first.
568 u32 mask
= (u32
) ~((1ULL << get_Dest_X0(bundle
)) |
569 (1ULL << get_SrcA_X0(bundle
)) |
570 (1ULL << get_SrcB_X0(bundle
)) |
571 (1ULL << target_reg
));
572 temp_reg
= __builtin_ctz(mask
);
573 state
->update_reg
= temp_reg
;
574 state
->update_value
= regs
->regs
[temp_reg
];
575 regs
->regs
[temp_reg
] = (unsigned long) (pc
+1);
576 regs
->flags
|= PT_FLAGS_RESTORE_REGS
;
577 bundle
= move_X1(bundle
, target_reg
, temp_reg
);
580 int opcode
= get_Opcode_Y2(bundle
);
603 mem_op
= MEMOP_STORE
;
608 mem_op
= MEMOP_STORE
;
615 * Check if we need to rewrite an unaligned load/store.
616 * Returning zero is a special value meaning we need to SIGSEGV.
618 if (mem_op
!= MEMOP_NONE
&& unaligned_fixup
>= 0) {
619 bundle
= rewrite_load_store_unaligned(state
, bundle
, regs
,
620 mem_op
, size
, sign_ext
);
625 /* write the bundle to our execution area */
626 buffer
= state
->buffer
;
627 err
= __put_user(bundle
, buffer
++);
630 * If we're really single-stepping, we take an INT_ILL after.
631 * If we're just handling an unaligned access, we can just
632 * jump directly back to where we were in user code.
634 if (is_single_step
) {
635 err
|= __put_user(__single_step_ill_insn
, buffer
++);
636 err
|= __put_user(__single_step_ill_insn
, buffer
++);
641 /* We have some state to update; do it inline */
643 bundle
= __single_step_addli_insn
;
644 bundle
|= create_Dest_X1(state
->update_reg
);
645 bundle
|= create_Imm16_X1(state
->update_value
);
646 err
|= __put_user(bundle
, buffer
++);
647 bundle
= __single_step_auli_insn
;
648 bundle
|= create_Dest_X1(state
->update_reg
);
649 bundle
|= create_SrcA_X1(state
->update_reg
);
650 ha16
= (state
->update_value
+ 0x8000) >> 16;
651 bundle
|= create_Imm16_X1(ha16
);
652 err
|= __put_user(bundle
, buffer
++);
656 /* End with a jump back to the next instruction */
657 delta
= ((regs
->pc
+ TILE_BUNDLE_SIZE_IN_BYTES
) -
658 (unsigned long)buffer
) >>
659 TILE_LOG2_BUNDLE_ALIGNMENT_IN_BYTES
;
660 bundle
= __single_step_j_insn
;
661 bundle
|= create_JOffLong_X1(delta
);
662 err
|= __put_user(bundle
, buffer
++);
666 pr_err("Fault when writing to single-step buffer\n");
672 * We do a local flush only, since this is a thread-specific buffer.
674 __flush_icache_range((unsigned long)state
->buffer
,
675 (unsigned long)buffer
);
677 /* Indicate enabled */
678 state
->is_enabled
= is_single_step
;
679 regs
->pc
= (unsigned long)state
->buffer
;
681 /* Fault immediately if we are coming back from a syscall. */
682 if (regs
->faultnum
== INT_SWINT_1
)
687 #include <linux/smp.h>
688 #include <linux/ptrace.h>
689 #include <arch/spr_def.h>
691 static DEFINE_PER_CPU(unsigned long, ss_saved_pc
);
695 * Called directly on the occasion of an interrupt.
697 * If the process doesn't have single step set, then we use this as an
698 * opportunity to turn single step off.
700 * It has been mentioned that we could conditionally turn off single stepping
701 * on each entry into the kernel and rely on single_step_once to turn it
702 * on for the processes that matter (as we already do), but this
703 * implementation is somewhat more efficient in that we muck with registers
704 * once on a bum interrupt rather than on every entry into the kernel.
706 * If SINGLE_STEP_CONTROL_K has CANCELED set, then an interrupt occurred,
707 * so we have to run through this process again before we can say that an
708 * instruction has executed.
710 * swint will set CANCELED, but it's a legitimate instruction. Fortunately
711 * it changes the PC. If it hasn't changed, then we know that the interrupt
712 * wasn't generated by swint and we'll need to run this process again before
713 * we can say an instruction has executed.
715 * If either CANCELED == 0 or the PC's changed, we send out SIGTRAPs and get
719 void gx_singlestep_handle(struct pt_regs
*regs
, int fault_num
)
721 unsigned long *ss_pc
= &__get_cpu_var(ss_saved_pc
);
722 struct thread_info
*info
= (void *)current_thread_info();
723 int is_single_step
= test_ti_thread_flag(info
, TIF_SINGLESTEP
);
724 unsigned long control
= __insn_mfspr(SPR_SINGLE_STEP_CONTROL_K
);
726 if (is_single_step
== 0) {
727 __insn_mtspr(SPR_SINGLE_STEP_EN_K_K
, 0);
729 } else if ((*ss_pc
!= regs
->pc
) ||
730 (!(control
& SPR_SINGLE_STEP_CONTROL_1__CANCELED_MASK
))) {
732 ptrace_notify(SIGTRAP
);
733 control
|= SPR_SINGLE_STEP_CONTROL_1__CANCELED_MASK
;
734 control
|= SPR_SINGLE_STEP_CONTROL_1__INHIBIT_MASK
;
735 __insn_mtspr(SPR_SINGLE_STEP_CONTROL_K
, control
);
741 * Called from need_singlestep. Set up the control registers and the enable
742 * register, then return back.
745 void single_step_once(struct pt_regs
*regs
)
747 unsigned long *ss_pc
= &__get_cpu_var(ss_saved_pc
);
748 unsigned long control
= __insn_mfspr(SPR_SINGLE_STEP_CONTROL_K
);
751 control
|= SPR_SINGLE_STEP_CONTROL_1__CANCELED_MASK
;
752 control
|= SPR_SINGLE_STEP_CONTROL_1__INHIBIT_MASK
;
753 __insn_mtspr(SPR_SINGLE_STEP_CONTROL_K
, control
);
754 __insn_mtspr(SPR_SINGLE_STEP_EN_K_K
, 1 << USER_PL
);
757 void single_step_execve(void)
762 #endif /* !__tilegx__ */