Skip to contentSkip to conversation

: sh4 delay slot code update

PATCH In review Magnus Damm 2 participants 6 messages Nov 28, 2007 – Nov 29, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 : sh4 delay slot code update

Hi everyone,

This patch updates the delay slot handling for the sh4 target.

The main feature is that delay slot flags now are stored in the tlb. This is needed for signal emulation and interrupts. There are also some correctness fixes included in this patch - with this patch applied i'm able to run quite a few dynamically linked busybox applets (uclibc). Some of these applets were broken without this fix. I've ran the sh4 binaries included in linux-user-test-0.2 and they work as usual.

Some code and ideas come from the mips target. This is done to make the sh4 code less special and more readable for people coming from that architecture.

 cpu-exec.c             |    4 -
 target-sh4/cpu.h       |   18 +++----
 target-sh4/op.c        |   54 +++++++----------------
 target-sh4/translate.c |  112 ++++++++++++++++++++++++++++++------------------
 translate-all.c        |    5 +-
 5 files changed, 104 insertions(+), 89 deletions(-)

Please apply! Thank you.

/ magnus

cpu-exec.c +2 -2
@@ -202,8 +202,8 @@ static inline TranslationBlock *tb_find_
     cs_base = 0;
     pc = env->pc;
 #elif defined(TARGET_SH4)
-    flags = env->sr & (SR_MD | SR_RB);
-    cs_base = 0;         /* XXXXX */
+    flags = env->flags;
+    cs_base = 0;
     pc = env->pc;
 #elif defined(TARGET_ALPHA)
     flags = env->ps;
target-sh4/cpu.h +9 -9
@@ -46,16 +46,16 @@
 #define FPSCR_SZ (1 << 20)
 #define FPSCR_PR (1 << 19)
 #define FPSCR_DN (1 << 18)
-
-#define DELAY_SLOT             (1 << 0) /* Must be the same as SR_T.  */
-/* This flag is set if the next insn is a delay slot for a conditional jump.
-   The dynamic value of the DELAY_SLOT determines whether the jup is taken. */
+#define DELAY_SLOT             (1 << 0)
 #define DELAY_SLOT_CONDITIONAL (1 << 1)
-/* Those are used in contexts only */
-#define BRANCH                 (1 << 2)
-#define BRANCH_CONDITIONAL     (1 << 3)
-#define MODE_CHANGE            (1 << 4)	/* Potential MD|RB change */
-#define BRANCH_EXCEPTION       (1 << 5)	/* Branch after exception */
+#define DELAY_SLOT_TRUE        (1 << 2)
+#define DELAY_SLOT_CLEARME     (1 << 3)
+/* The dynamic value of the DELAY_SLOT_TRUE flag determines whether the jump
+ * after the delay slot should be taken or not. It is calculated from SR_T.
+ *
+ * It is unclear if it is permitted to modify the SR_T flag in a delay slot.
+ * The use of DELAY_SLOT_TRUE flag makes us accept such SR_T modification.
+ */
 
 /* XXXXX The structure could be made more compact */
 typedef struct tlb_t {
target-sh4/op.c +17 -37

126 lines not on this page · raw message

target-sh4/translate.c +72 -40

231 lines not on this page · raw message

translate-all.c +4 -1
@@ -53,7 +53,7 @@ uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
 #elif defined(TARGET_SPARC)
 target_ulong gen_opc_npc[OPC_BUF_SIZE];
 target_ulong gen_opc_jump_pc[2];
-#elif defined(TARGET_MIPS)
+#elif defined(TARGET_MIPS) || defined(TARGET_SH4)
 uint32_t gen_opc_hflags[OPC_BUF_SIZE];
 #endif
 
@@ -298,6 +298,9 @@ int cpu_restore_state(TranslationBlock *
     env->hflags |= gen_opc_hflags[j];
 #elif defined(TARGET_ALPHA)
     env->pc = gen_opc_pc[j];
+#elif defined(TARGET_SH4)
+    env->pc = gen_opc_pc[j];
+    env->flags = gen_opc_hflags[j];
 #endif
     return 0;
 }
#1 lore raw

target-sh4/cpu.h line 58

Quoted 8 lines · Magnus Damm
target-sh4/cpu.h +8 -0
+#define DELAY_SLOT_TRUE        (1 << 2)
+#define DELAY_SLOT_CLEARME     (1 << 3)
+/* The dynamic value of the DELAY_SLOT_TRUE flag determines whether the jump
+ * after the delay slot should be taken or not. It is calculated from SR_T.
+ *
+ * It is unclear if it is permitted to modify the SR_T flag in a delay slot.
+ * The use of DELAY_SLOT_TRUE flag makes us accept such SR_T modification.
+ */

Nesting a 'tst' in a delay slot is certainly valid, and GCC correctly treats it as a slottable instruction. If you're in doubt as to whether an opcode can be placed in a delay slot or not, the machine descriptor is a good way of sorting things out. The only restrictions I know of things that cause changes to PC, most of the system instructions (like trapa and ldtlb), and so on. There are of course cases where an instruction itself is slottable which may perform illegal behaviour via PC modification or so on, and we do have an exception for trapping that sort of abuse.

You can see an example in arch/sh/kernel/entry-common.S:

syscall_exit_work:
! r0: current_thread_info->flags
! r8: current_thread_info
tst #_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP | _TIF_SYSCALL_AUDIT, r0
bt/s work_pending
tst #_TIF_NEED_RESCHED, r0

....
work_pending:
! r0: current_thread_info->flags
! r8: current_thread_info
! t: result of "tst #_TIF_NEED_RESCHED, r0"
bf/s work_resched
tst #(_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK), r0

....

This sort of access is not a particularly rare workload. Presumably you'd hit this under system emulation at the very least.

#2 lore raw

Hi Paul,

Thanks for your comments.

Quoted 10 lines · Paul Mundt

target-sh4/cpu.h line 58

Quoted 8 lines · Magnus Damm
target-sh4/cpu.h +8 -0
+#define DELAY_SLOT_TRUE        (1 << 2)
+#define DELAY_SLOT_CLEARME     (1 << 3)
+/* The dynamic value of the DELAY_SLOT_TRUE flag determines whether the jump
+ * after the delay slot should be taken or not. It is calculated from SR_T.
+ *
+ * It is unclear if it is permitted to modify the SR_T flag in a delay slot.
+ * The use of DELAY_SLOT_TRUE flag makes us accept such SR_T modification.
+ */
Quoted 8 lines

Nesting a 'tst' in a delay slot is certainly valid, and GCC correctly treats it as a slottable instruction. If you're in doubt as to whether an opcode can be placed in a delay slot or not, the machine descriptor is a good way of sorting things out. The only restrictions I know of things that cause changes to PC, most of the system instructions (like trapa and ldtlb), and so on. There are of course cases where an instruction itself is slottable which may perform illegal behaviour via PC modification or so on, and we do have an exception for trapping that sort of abuse.

I was mainly wondering if I really needed to save the state of SR_T, but I assumed so. So the code should be correct. And yes, I'm sure there are quite a few slottable instructions with interesting side effects, but that's a separate issue.

Quoted 17 lines

You can see an example in arch/sh/kernel/entry-common.S:

syscall_exit_work:
! r0: current_thread_info->flags
! r8: current_thread_info
tst #_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP | _TIF_SYSCALL_AUDIT, r0
bt/s work_pending
tst #_TIF_NEED_RESCHED, r0

....
work_pending:
! r0: current_thread_info->flags
! r8: current_thread_info
! t: result of "tst #_TIF_NEED_RESCHED, r0"
bf/s work_resched
tst #(_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK), r0

Quoted 4 lines

....

This sort of access is not a particularly rare workload. Presumably you'd hit this under system emulation at the very least.

Yeah, that's a pretty good example that shows that I need to save the SR_T state before executing the delay slot instruction. Thanks for pointing out that code.

/ magnus

#3 lore raw
Quoted 6 lines · Magnus Damm

On Nov 28, 2007 9:49 PM, Paul Mundt <[email protected]> wrote:
I was mainly wondering if I really needed to save the state of SR_T, but I assumed so. So the code should be correct. And yes, I'm sure there are quite a few slottable instructions with interesting side effects, but that's a separate issue.

The only thing to be careful of is the ordering semantics for the T-bit state, which is what I tried to convey in the code snippet.

Quoted 9 lines
Quoted 9 lines

You can see an example in arch/sh/kernel/entry-common.S:

syscall_exit_work:
! r0: current_thread_info->flags
! r8: current_thread_info
tst #_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP | _TIF_SYSCALL_AUDIT, r0
bt/s work_pending
tst #_TIF_NEED_RESCHED, r0

Quoted 8 lines
Quoted 8 lines

....
work_pending:
! r0: current_thread_info->flags
! r8: current_thread_info
! t: result of "tst #_TIF_NEED_RESCHED, r0"
bf/s work_resched
tst #(_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK), r0

So while bt/s is conditional on the first flag test, the T-state is relative to the second test by the time the branch happens.

The T-state check for bt/bf happens _prior_ to execution of the delay slot instruction, while any delay-slot resident T-bit modifier is executed by the time we enter the branch. I don't know if your code handles that or not, but figured it's probably good to make that explicit. T-bit modifiers are always a bit hairy..

#4 lore raw
Quoted 20 lines · Paul Mundt

The only thing to be careful of is the ordering semantics for the T-bit state, which is what I tried to convey in the code snippet.

Quoted 9 lines
Quoted 9 lines

You can see an example in arch/sh/kernel/entry-common.S:

syscall_exit_work:
! r0: current_thread_info->flags
! r8: current_thread_info
tst #_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP | _TIF_SYSCALL_AUDIT, r0
bt/s work_pending
tst #_TIF_NEED_RESCHED, r0

Quoted 8 lines
Quoted 8 lines

....
work_pending:
! r0: current_thread_info->flags
! r8: current_thread_info
! t: result of "tst #_TIF_NEED_RESCHED, r0"
bf/s work_resched
tst #(_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK), r0

Quoted 2 lines

So while bt/s is conditional on the first flag test, the T-state is relative to the second test by the time the branch happens.

Exactly.

Quoted 5 lines

The T-state check for bt/bf happens _prior_ to execution of the delay slot instruction, while any delay-slot resident T-bit modifier is executed by the time we enter the branch. I don't know if your code handles that or not, but figured it's probably good to make that explicit. T-bit modifiers are always a bit hairy..

Yeah, the T flag is indeed a bit hairy. The patch should handle the T bit exactly as you describe it. As part of the bt/bf instruction the T flag is evaluated and if true the the DELAY_SLOT_TRUE bit is set. This bit is later used to decide if we should jump or not after the delay slot instruction.

Thank you,

/ magnus

#5 lore raw
Quoted 7 lines · Magnus Damm
Quoted 5 lines · Paul Mundt

The T-state check for bt/bf happens _prior_ to execution of the delay slot instruction, while any delay-slot resident T-bit modifier is executed by the time we enter the branch. I don't know if your code handles that or not, but figured it's probably good to make that explicit. T-bit modifiers are always a bit hairy..

Quoted 6 lines

Yeah, the T flag is indeed a bit hairy. The patch should handle the T bit exactly as you describe it. As part of the bt/bf instruction the T flag is evaluated and if true the the DELAY_SLOT_TRUE bit is set. This bit is later used to decide if we should jump or not after the delay slot instruction.

Ok, I have no objection to these changes then.