Skip to contentSkip to conversation

m68k: fix coldfire linux problems

PATCH Superseded [email protected] 2 participants 9 messages Aug 19, 2014 – Jun 19, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover m68k: fix coldfire linux problems

Some small issues are causing problems with running modern versions of Linux on the m68k-system ColdFire targets. These 3 patches fix those problems.

This is a repost of these patches rebased onto the current git tree:

http://lists.nongnu.org/archive/html/qemu-devel/2012-09/msg01954.html
http://lists.nongnu.org/archive/html/qemu-devel/2012-09/msg02099.html
http://lists.nongnu.org/archive/html/qemu-devel/2012-09/msg01956.html

Regards
Greg

--

 hw/m68k/mcf_intc.c      |   14 ++++++++++++++
 target-m68k/op_helper.c |    7 +++----
 target-m68k/translate.c |    8 ++++----
 3 files changed, 21 insertions(+), 8 deletions(-)
#1 lore raw

PATCH 1/3 m68k: implmenent more ColdFire 5208 interrupt controller functionality

From: Greg Ungerer <[email protected]>

Implement the SIMR and CIMR registers of the 5208 interrupt controller. These are used by modern versions of Linux running on ColdFire (not sure of the exact version they were introduced, but they have been in for quite a while now).

Without this change when attempting to run a linux-3.5 kernel you will see:

qemu: hardware error: mcf_intc_write: Bad write offset 28

and execution will stop and dump out.

---
 hw/m68k/mcf_intc.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/m68k/mcf_intc.c +14 -0
index 621423c..37a9de0 100644
@@ -102,6 +102,20 @@ static void mcf_intc_write(void *opaque, hwaddr addr,
     case 0x0c:
         s->imr = (s->imr & 0xffffffff00000000ull) | (uint32_t)val;
         break;
+    case 0x1c:
+        if (val & 0x40) {
+            s->imr = 0xffffffffffffffffull;
+        } else {
+            s->imr |= (0x1ull << (val & 0x3f));
+        }
+        break;
+    case 0x1d:
+        if (val & 0x40) {
+            s->imr = 0ull;
+        } else {
+            s->imr &= ~(0x1ull << (val & 0x3f));
+        }
+        break;
     default:
         hw_error("mcf_intc_write: Bad write offset %d\n", offset);
         break;
Signature · 1 line
1.9.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] m68k: implmenent more ColdFire 5208 interrupt controller functionality

Quoted 24 lines · On Mon, Aug 18, 2014 at 10:37 PM, <[email protected]> wrote:

From: Greg Ungerer <[email protected]>

Implement the SIMR and CIMR registers of the 5208 interrupt controller. These are used by modern versions of Linux running on ColdFire (not sure of the exact version they were introduced, but they have been in for quite a while now).

Without this change when attempting to run a linux-3.5 kernel you will see:

qemu: hardware error: mcf_intc_write: Bad write offset 28

and execution will stop and dump out.

---
 hw/m68k/mcf_intc.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/m68k/mcf_intc.c +0 -0
index 621423c..37a9de0 100644

hw/m68k/mcf_intc.c static void mcf_intc_write(void *opaque, hwaddr addr, · line 107

Quoted 6 lines
hw/m68k/mcf_intc.c +3 -0
@@ -102,6 +102,20 @@ static void mcf_intc_write(void *opaque, hwaddr addr,
     case 0x0c:
         s->imr = (s->imr & 0xffffffff00000000ull) | (uint32_t)val;
         break;
+    case 0x1c:
+        if (val & 0x40) {
+            s->imr = 0xffffffffffffffffull;

~0ull.

Otherwise,

This introduces magic numbers which is generally discouraged, by this device has no macrofication at all so I guess it should be cleaned up at some stage.

Regards,
Peter

Quoted 18 lines
hw/m68k/mcf_intc.c +11 -1
+        } else {
+            s->imr |= (0x1ull << (val & 0x3f));
+        }
+        break;
+    case 0x1d:
+        if (val & 0x40) {
+            s->imr = 0ull;
+        } else {
+            s->imr &= ~(0x1ull << (val & 0x3f));
+        }
+        break;
     default:
         hw_error("mcf_intc_write: Bad write offset %d\n", offset);
         break;
--

1.9.1

#3 lore raw

Re: [Qemu-devel] [PATCH 1/3] m68k: implmenent more ColdFire 5208 interrupt controller functionality

Hi Peter,

Quoted 34 lines · Peter Crosthwaite
Quoted 24 lines · On Mon, Aug 18, 2014 at 10:37 PM, <[email protected]> wrote:

From: Greg Ungerer <[email protected]>

Implement the SIMR and CIMR registers of the 5208 interrupt controller. These are used by modern versions of Linux running on ColdFire (not sure of the exact version they were introduced, but they have been in for quite a while now).

Without this change when attempting to run a linux-3.5 kernel you will see:

qemu: hardware error: mcf_intc_write: Bad write offset 28

and execution will stop and dump out.

---
 hw/m68k/mcf_intc.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/m68k/mcf_intc.c +0 -0
index 621423c..37a9de0 100644

hw/m68k/mcf_intc.c static void mcf_intc_write(void *opaque, hwaddr addr, · line 107

Quoted 6 lines
hw/m68k/mcf_intc.c +3 -0
@@ -102,6 +102,20 @@ static void mcf_intc_write(void *opaque, hwaddr addr,
     case 0x0c:
         s->imr = (s->imr & 0xffffffff00000000ull) | (uint32_t)val;
         break;
+    case 0x1c:
+        if (val & 0x40) {
+            s->imr = 0xffffffffffffffffull;

~0ull.

Quoted 3 lines

Otherwise,

Thanks, I'll change that and add the reviewed-by.

Quoted 3 lines

This introduces magic numbers which is generally discouraged, by this device has no macrofication at all so I guess it should be cleaned up at some stage.

Agreed. I stuck to the existing style in this case.

Regards
Greg

Quoted 19 lines
Quoted 18 lines
hw/m68k/mcf_intc.c +11 -1
+        } else {
+            s->imr |= (0x1ull << (val & 0x3f));
+        }
+        break;
+    case 0x1d:
+        if (val & 0x40) {
+            s->imr = 0ull;
+        } else {
+            s->imr &= ~(0x1ull << (val & 0x3f));
+        }
+        break;
     default:
         hw_error("mcf_intc_write: Bad write offset %d\n", offset);
         break;
--

1.9.1

#4 lore raw

PATCH 2/3 m68k: implement move to/from usp register instruction

In reply to [email protected]

From: Greg Ungerer <[email protected]>

Fill out the code support for the move to/from usp instructions. They are being decoded, but there is no code to support there actions. So add it.

Current versions of Linux running on the ColdFire 5208 use these instructions.

---
 target-m68k/translate.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
target-m68k/translate.c +4 -4
index efd4cfc..5a6666a 100644
@@ -1995,8 +1995,8 @@ DISAS_INSN(move_from_usp)
         gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
         return;
     }
-    /* TODO: Implement USP.  */
-    gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
+    tcg_gen_ld_i32(AREG(insn, 0), cpu_env,
+                   offsetof(CPUM68KState, sp[M68K_USP]));
 }
 
 DISAS_INSN(move_to_usp)
@@ -2005,8 +2005,8 @@ DISAS_INSN(move_to_usp)
         gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
         return;
     }
-    /* TODO: Implement USP.  */
-    gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
+    tcg_gen_st_i32(AREG(insn, 0), cpu_env,
+                   offsetof(CPUM68KState, sp[M68K_USP]));
 }
 
 DISAS_INSN(halt)
Signature · 1 line
1.9.1
#5 lore raw

PATCH 3/3 m68k: fix usp processing on interrupt entry and exception exit

In reply to [email protected]

From: Greg Ungerer <[email protected]>

The action to potentially switch sp register is not occurring at the correct point in the interrupt entry or exception exit sequences.

For the interrupt entry case the sp on entry is used to create the stack exception frame - but this may well be the user stack pointer, since we haven't done the switch yet. Re-order the flow to switch the sp regs then use the current sp to create the exception frame.

For the return from exception case the code is unwinding the sp after switching sp registers. But it should always unwind the supervisor sp first, then carry out any required sp switch.

Note that these problems don't effect operation unless the user sp bit is set in the CACR register. Only a single sp is used in the default power up state. Previously Linux only used this single sp mode. But modern versions of Linux use the user sp mode now, so we need correct behavior for Linux to work.

---
 target-m68k/op_helper.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
target-m68k/op_helper.c +3 -4
index 9dd3e74..cd748b8 100644
@@ -63,8 +63,8 @@ static void do_rte(CPUM68KState *env)
     env->pc = cpu_ldl_kernel(env, sp + 4);
     sp |= (fmt >> 28) & 3;
     env->sr = fmt & 0xffff;
-    m68k_switch_sp(env);
     env->aregs[7] = sp + 8;
+    m68k_switch_sp(env);
 }
 
 static void do_interrupt_all(CPUM68KState *env, int is_hw)
@@ -108,10 +108,7 @@ static void do_interrupt_all(CPUM68KState *env, int is_hw)
 
     vector = cs->exception_index << 2;
 
-    sp = env->aregs[7];
-
     fmt |= 0x40000000;
-    fmt |= (sp & 3) << 28;
     fmt |= vector << 16;
     fmt |= env->sr;
 
@@ -121,6 +118,8 @@ static void do_interrupt_all(CPUM68KState *env, int is_hw)
         env->sr &= ~SR_M;
     }
     m68k_switch_sp(env);
+    sp = env->aregs[7];
+    fmt |= (sp & 3) << 28;
 
     /* ??? This could cause MMU faults.  */
     sp &= ~3;
Signature · 1 line
1.9.1
Reviewed-by on v1 3/3
#6 lore raw

Re: [Qemu-devel] [PATCH 3/3] m68k: fix usp processing on interrupt entry and exception exit

Quoted 10 lines · On Mon, Aug 18, 2014 at 10:37 PM, <[email protected]> wrote:

From: Greg Ungerer <[email protected]>

The action to potentially switch sp register is not occurring at the correct point in the interrupt entry or exception exit sequences.

For the interrupt entry case the sp on entry is used to create the stack exception frame - but this may well be the user stack pointer, since we haven't done the switch yet. Re-order the flow to switch the sp regs then use the current sp to create the exception frame.

So I see the bug. The existing code is definitely bogus, that in both int and ret the, newly switched SP is overwitten by a stale one that has just been used. The actual stack push and pop logic happens after and before the switch for int and ret respectively so it is pretty clear the author intended the stacking to happen on the supervisors stack. Is this mandated by the spec one way or the other or is it up to implementation which stack these values are stored on? I can see it could work both ways.

Quoted 30 lines

For the return from exception case the code is unwinding the sp after switching sp registers. But it should always unwind the supervisor sp first, then carry out any required sp switch.

Note that these problems don't effect operation unless the user sp bit is set in the CACR register. Only a single sp is used in the default power up state. Previously Linux only used this single sp mode. But modern versions of Linux use the user sp mode now, so we need correct behavior for Linux to work.

---
 target-m68k/op_helper.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
target-m68k/op_helper.c +1 -1
index 9dd3e74..cd748b8 100644
@@ -63,8 +63,8 @@ static void do_rte(CPUM68KState *env)
     env->pc = cpu_ldl_kernel(env, sp + 4);
     sp |= (fmt >> 28) & 3;
     env->sr = fmt & 0xffff;
-    m68k_switch_sp(env);
     env->aregs[7] = sp + 8;
+    m68k_switch_sp(env);
 }
 
 static void do_interrupt_all(CPUM68KState *env, int is_hw)

target-m68k/op_helper.c static void do_interrupt_all(CPUM68KState *env, int is_hw) · line 113

Quoted 9 lines
target-m68k/op_helper.c +0 -3
@@ -108,10 +108,7 @@ static void do_interrupt_all(CPUM68KState *env, int is_hw)
 
     vector = cs->exception_index << 2;
 
-    sp = env->aregs[7];
-
     fmt |= 0x40000000;
-    fmt |= (sp & 3) << 28;
     fmt |= vector << 16;
     fmt |= env->sr;

You should move them all to keep them together.

Otherwise,

Regards,
Peter

Quoted 14 lines
target-m68k/op_helper.c +2 -1
@@ -121,6 +118,8 @@ static void do_interrupt_all(CPUM68KState *env, int is_hw)
         env->sr &= ~SR_M;
     }
     m68k_switch_sp(env);
+    sp = env->aregs[7];
+    fmt |= (sp & 3) << 28;
 
     /* ??? This could cause MMU faults.  */
     sp &= ~3;
--

1.9.1

#7 lore raw

Re: [Qemu-devel] [PATCH 3/3] m68k: fix usp processing on interrupt entry and exception exit

Hi Peter,

Quoted 12 lines · Peter Crosthwaite
Quoted 10 lines · On Mon, Aug 18, 2014 at 10:37 PM, <[email protected]> wrote:

From: Greg Ungerer <[email protected]>

The action to potentially switch sp register is not occurring at the correct point in the interrupt entry or exception exit sequences.

For the interrupt entry case the sp on entry is used to create the stack exception frame - but this may well be the user stack pointer, since we haven't done the switch yet. Re-order the flow to switch the sp regs then use the current sp to create the exception frame.

Quoted 8 lines

So I see the bug. The existing code is definitely bogus, that in both int and ret the, newly switched SP is overwitten by a stale one that has just been used. The actual stack push and pop logic happens after and before the switch for int and ret respectively so it is pretty clear the author intended the stacking to happen on the supervisors stack. Is this mandated by the spec one way or the other or is it up to implementation which stack these values are stored on? I can see it could work both ways.

There is 2 cases to deal with.

1. Early ColdFire parts only had a single stack pointer (a7). There is no notion of swapping to a supervisor stack in this case. All ColdFire parts power up in this mode, only a single stack pointer. This mode works with the existing qemu code.

2. Later model ColdFire's introduced the conventional m68k user and supervisor stack pointers. It is enabled via a mode bit in the CPU CACR register. Once the EUSP bit is enabled then on interrupt and exception the ColdFire reference manual states that "The processor saves the current context by creating an exception stack frame on the system stack". So it must be on the supervisor stack, using the supervisor stack pointer.

Case 2 is what this patch fixes.

Quoted 33 lines
Quoted 33 lines

For the return from exception case the code is unwinding the sp after switching sp registers. But it should always unwind the supervisor sp first, then carry out any required sp switch.

Note that these problems don't effect operation unless the user sp bit is set in the CACR register. Only a single sp is used in the default power up state. Previously Linux only used this single sp mode. But modern versions of Linux use the user sp mode now, so we need correct behavior for Linux to work.

---
 target-m68k/op_helper.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
target-m68k/op_helper.c +1 -1
index 9dd3e74..cd748b8 100644
@@ -63,8 +63,8 @@ static void do_rte(CPUM68KState *env)
     env->pc = cpu_ldl_kernel(env, sp + 4);
     sp |= (fmt >> 28) & 3;
     env->sr = fmt & 0xffff;
-    m68k_switch_sp(env);
     env->aregs[7] = sp + 8;
+    m68k_switch_sp(env);
 }
 
 static void do_interrupt_all(CPUM68KState *env, int is_hw)
@@ -108,10 +108,7 @@ static void do_interrupt_all(CPUM68KState *env, int is_hw)
 
     vector = cs->exception_index << 2;
 
Quoted 8 lines

target-m68k/op_helper.c static void do_interrupt_all(CPUM68KState *env, int is_hw) · line 113

Quoted 6 lines
target-m68k/op_helper.c +0 -3
-    sp = env->aregs[7];
-
     fmt |= 0x40000000;
-    fmt |= (sp & 3) << 28;
     fmt |= vector << 16;
     fmt |= env->sr;

You should move them all to keep them together.

We need to get at least env->sr into fmt here, before we modify it in steps immediately after this. At least if we don't want to save env->sr locally to use later.

Quoted 3 lines

Otherwise,

Thanks for the review.

Regards
Greg

Quoted 14 lines
Quoted 13 lines
target-m68k/op_helper.c +2 -1
@@ -121,6 +118,8 @@ static void do_interrupt_all(CPUM68KState *env, int is_hw)
         env->sr &= ~SR_M;
     }
     m68k_switch_sp(env);
+    sp = env->aregs[7];
+    fmt |= (sp & 3) << 28;
 
     /* ??? This could cause MMU faults.  */
     sp &= ~3;
--

1.9.1

#8 lore raw

Re: [Qemu-devel] [PATCH 3/3] m68k: fix usp processing on interrupt entry and exception exit

Quoted 84 lines · Greg Ungerer

Hi Peter,

Quoted 12 lines · Peter Crosthwaite
Quoted 10 lines · On Mon, Aug 18, 2014 at 10:37 PM, <[email protected]> wrote:

From: Greg Ungerer <[email protected]>

The action to potentially switch sp register is not occurring at the correct point in the interrupt entry or exception exit sequences.

For the interrupt entry case the sp on entry is used to create the stack exception frame - but this may well be the user stack pointer, since we haven't done the switch yet. Re-order the flow to switch the sp regs then use the current sp to create the exception frame.

Quoted 8 lines

So I see the bug. The existing code is definitely bogus, that in both int and ret the, newly switched SP is overwitten by a stale one that has just been used. The actual stack push and pop logic happens after and before the switch for int and ret respectively so it is pretty clear the author intended the stacking to happen on the supervisors stack. Is this mandated by the spec one way or the other or is it up to implementation which stack these values are stored on? I can see it could work both ways.

There is 2 cases to deal with.

1. Early ColdFire parts only had a single stack pointer (a7). There is no notion of swapping to a supervisor stack in this case. All ColdFire parts power up in this mode, only a single stack pointer. This mode works with the existing qemu code.

2. Later model ColdFire's introduced the conventional m68k user and supervisor stack pointers. It is enabled via a mode bit in the CPU CACR register. Once the EUSP bit is enabled then on interrupt and exception the ColdFire reference manual states that "The processor saves the current context by creating an exception stack frame on the system stack". So it must be on the supervisor stack, using the supervisor stack pointer.

Case 2 is what this patch fixes.

Quoted 33 lines
Quoted 33 lines

For the return from exception case the code is unwinding the sp after switching sp registers. But it should always unwind the supervisor sp first, then carry out any required sp switch.

Note that these problems don't effect operation unless the user sp bit is set in the CACR register. Only a single sp is used in the default power up state. Previously Linux only used this single sp mode. But modern versions of Linux use the user sp mode now, so we need correct behavior for Linux to work.

---
 target-m68k/op_helper.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
target-m68k/op_helper.c +1 -1
index 9dd3e74..cd748b8 100644
@@ -63,8 +63,8 @@ static void do_rte(CPUM68KState *env)
     env->pc = cpu_ldl_kernel(env, sp + 4);
     sp |= (fmt >> 28) & 3;
     env->sr = fmt & 0xffff;
-    m68k_switch_sp(env);
     env->aregs[7] = sp + 8;
+    m68k_switch_sp(env);
 }
 
 static void do_interrupt_all(CPUM68KState *env, int is_hw)
@@ -108,10 +108,7 @@ static void do_interrupt_all(CPUM68KState *env, int is_hw)
 
     vector = cs->exception_index << 2;
 
Quoted 8 lines

target-m68k/op_helper.c static void do_interrupt_all(CPUM68KState *env, int is_hw) · line 113

Quoted 6 lines
target-m68k/op_helper.c +0 -3
-    sp = env->aregs[7];
-
     fmt |= 0x40000000;
-    fmt |= (sp & 3) << 28;
     fmt |= vector << 16;
     fmt |= env->sr;

You should move them all to keep them together.

Quoted 4 lines

We need to get at least env->sr into fmt here, before we modify it in steps immediately after this. At least if we don't want to save env->sr locally to use later.

Ahh yes I see. No change needed then.

Regards,
Peter

Quoted 28 lines
Quoted 4 lines

Otherwise,

Thanks for the review.

Regards
Greg

Quoted 14 lines
Quoted 13 lines
target-m68k/op_helper.c +2 -1
@@ -121,6 +118,8 @@ static void do_interrupt_all(CPUM68KState *env, int is_hw)
         env->sr &= ~SR_M;
     }
     m68k_switch_sp(env);
+    sp = env->aregs[7];
+    fmt |= (sp & 3) << 28;
 
     /* ??? This could cause MMU faults.  */
     sp &= ~3;
--

1.9.1