Skip to contentSkip to conversation

target/openrisc: Set EPCR to next PC on FPE exceptions

PATCH Superseded Stafford Horne 2 participants 4 messages Jul 29, 2023 – Jul 31, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/openrisc: Set EPCR to next PC on FPE exceptions

The architecture specification calls for the EPCR to be set to "Address of next not executed instruction" when there is a floating point exception (FPE). This was not being done, so fix it by using the same method as syscall. Note, this may need a lot more work if we start seeing floating point operations in delay slots which exceptions enabled.

Without this patch FPU exceptions will loop, as the exception hanlding will always return back to the failed floating point instruction.

This was not noticed in earlier testing because:

1. The compiler usually generates code which clobbers the input operand

    such as:

      lf.div.s r19,r17,r19

2. The target will store the operation output before to the register

    before handling the exception.  So an operation such as:

      float a = 100.0f;
      float b = 0.0f;
      float c = a / b;    /* lf.div.s r19,r17,r19 */

    Will first execute:

      100 / 0    -> Store inf to c (r19)
                 -> triggering divide by zero exception
                 -> handle and return

    Then it will exectute:

      100 / inf  -> Store 0 to c  (no exception)

To confirm the looping behavoid and the fix I used the following:

    float fpu_div(float a, float b) {
	float c;
	asm volatile("lf.div.s %0, %1, %2"
		      : "+r" (c)
		      : "r" (a), "r" (b));
	return c;
    }
---
 target/openrisc/interrupt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/openrisc/interrupt.c +1 -1
index 3887812810..9b14b8a2c6 100644
@@ -34,7 +34,7 @@ void openrisc_cpu_do_interrupt(CPUState *cs)
     int exception = cs->exception_index;
 
     env->epcr = env->pc;
-    if (exception == EXCP_SYSCALL) {
+    if (exception == EXCP_SYSCALL || exception == EXCP_FPE) {
         env->epcr += 4;
     }
     /* When we have an illegal instruction the error effective address
Signature · 1 line
2.39.1
#1 lore raw
Quoted 6 lines · Stafford Horne

The architecture specification calls for the EPCR to be set to "Address of next not executed instruction" when there is a floating point exception (FPE). This was not being done, so fix it by using the same method as syscall. Note, this may need a lot more work if we start seeing floating point operations in delay slots which exceptions enabled.

It should be "with exceptions enabled."

Quoted 2 lines

Without this patch FPU exceptions will loop, as the exception hanlding

"handling"

Quoted 26 lines

will always return back to the failed floating point instruction.

This was not noticed in earlier testing because:

1. The compiler usually generates code which clobbers the input operand

    such as:

      lf.div.s r19,r17,r19

2. The target will store the operation output before to the register

    before handling the exception.  So an operation such as:

      float a = 100.0f;
      float b = 0.0f;
      float c = a / b;    /* lf.div.s r19,r17,r19 */

    Will first execute:

      100 / 0    -> Store inf to c (r19)
                 -> triggering divide by zero exception
                 -> handle and return

    Then it will exectute:

      100 / inf  -> Store 0 to c  (no exception)
Quoted 1 line

To confirm the looping behavoid and the fix I used the following:

"behavior"

Quoted 9 lines
    float fpu_div(float a, float b) {
	float c;
	asm volatile("lf.div.s %0, %1, %2"
		      : "+r" (c)
		      : "r" (a), "r" (b));
	return c;
    }

-Stafford

Quoted 20 lines
---
 target/openrisc/interrupt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/openrisc/interrupt.c +1 -1
index 3887812810..9b14b8a2c6 100644
@@ -34,7 +34,7 @@ void openrisc_cpu_do_interrupt(CPUState *cs)
     int exception = cs->exception_index;
 
     env->epcr = env->pc;
-    if (exception == EXCP_SYSCALL) {
+    if (exception == EXCP_SYSCALL || exception == EXCP_FPE) {
         env->epcr += 4;
     }
     /* When we have an illegal instruction the error effective address
Signature · 1 line
2.39.1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Stafford Horne
Quoted 54 lines · Stafford Horne

The architecture specification calls for the EPCR to be set to "Address of next not executed instruction" when there is a floating point exception (FPE). This was not being done, so fix it by using the same method as syscall. Note, this may need a lot more work if we start seeing floating point operations in delay slots which exceptions enabled.

Without this patch FPU exceptions will loop, as the exception hanlding will always return back to the failed floating point instruction.

This was not noticed in earlier testing because:

1. The compiler usually generates code which clobbers the input operand

     such as:

       lf.div.s r19,r17,r19

2. The target will store the operation output before to the register

     before handling the exception.  So an operation such as:

       float a = 100.0f;
       float b = 0.0f;
       float c = a / b;    /* lf.div.s r19,r17,r19 */

     Will first execute:

       100 / 0    -> Store inf to c (r19)
                  -> triggering divide by zero exception
                  -> handle and return

     Then it will exectute:

       100 / inf  -> Store 0 to c  (no exception)

To confirm the looping behavoid and the fix I used the following:

     float fpu_div(float a, float b) {
	float c;
	asm volatile("lf.div.s %0, %1, %2"
		      : "+r" (c)
		      : "r" (a), "r" (b));
	return c;
     }
---
  target/openrisc/interrupt.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
target/openrisc/interrupt.c +0 -0
index 3887812810..9b14b8a2c6 100644

target/openrisc/interrupt.c void openrisc_cpu_do_interrupt(CPUState *cs) · line 40

Quoted 8 lines
target/openrisc/interrupt.c +1 -1
@@ -34,7 +34,7 @@ void openrisc_cpu_do_interrupt(CPUState *cs)
      int exception = cs->exception_index;
 
      env->epcr = env->pc;
-    if (exception == EXCP_SYSCALL) {
+    if (exception == EXCP_SYSCALL || exception == EXCP_FPE) {
          env->epcr += 4;
      }
      /* When we have an illegal instruction the error effective address

According to Table 6-3, when in a delay slot the EPCR should be the address of the jump, for both syscall and fpe. This whole block should be moved down...

Quoted 8 lines
    /* Set/clear dsx to indicate if we are in a delay slot exception.  */
    if (env->dflag) {
        env->dflag = 0;
        env->sr |= SR_DSX;
        env->epcr -= 4;
    } else {
        env->sr &= ~SR_DSX;
    }

... into the else.

With that,

r~

#3 lore raw
Quoted 76 lines · Richard Henderson
Quoted 54 lines · Stafford Horne

The architecture specification calls for the EPCR to be set to "Address of next not executed instruction" when there is a floating point exception (FPE). This was not being done, so fix it by using the same method as syscall. Note, this may need a lot more work if we start seeing floating point operations in delay slots which exceptions enabled.

Without this patch FPU exceptions will loop, as the exception hanlding will always return back to the failed floating point instruction.

This was not noticed in earlier testing because:

1. The compiler usually generates code which clobbers the input operand

     such as:

       lf.div.s r19,r17,r19

2. The target will store the operation output before to the register

     before handling the exception.  So an operation such as:

       float a = 100.0f;
       float b = 0.0f;
       float c = a / b;    /* lf.div.s r19,r17,r19 */

     Will first execute:

       100 / 0    -> Store inf to c (r19)
                  -> triggering divide by zero exception
                  -> handle and return

     Then it will exectute:

       100 / inf  -> Store 0 to c  (no exception)

To confirm the looping behavoid and the fix I used the following:

     float fpu_div(float a, float b) {
	float c;
	asm volatile("lf.div.s %0, %1, %2"
		      : "+r" (c)
		      : "r" (a), "r" (b));
	return c;
     }
---
  target/openrisc/interrupt.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
target/openrisc/interrupt.c +0 -0
index 3887812810..9b14b8a2c6 100644

target/openrisc/interrupt.c void openrisc_cpu_do_interrupt(CPUState *cs) · line 39

Quoted 7 lines
target/openrisc/interrupt.c +1 -1
@@ -34,7 +34,7 @@ void openrisc_cpu_do_interrupt(CPUState *cs)
      int exception = cs->exception_index;
      env->epcr = env->pc;
-    if (exception == EXCP_SYSCALL) {
+    if (exception == EXCP_SYSCALL || exception == EXCP_FPE) {
          env->epcr += 4;
      }
      /* When we have an illegal instruction the error effective address

According to Table 6-3, when in a delay slot the EPCR should be the address of the jump, for both syscall and fpe. This whole block should be moved down...

Quoted 8 lines
target/openrisc/interrupt.c +0 -0
    /* Set/clear dsx to indicate if we are in a delay slot exception.  */
    if (env->dflag) {
        env->dflag = 0;
        env->sr |= SR_DSX;
        env->epcr -= 4;
    } else {
        env->sr &= ~SR_DSX;
    }
Quoted 1 line

... into the else.

Thanks for looking at this. This is correct. I will fix it this way, though this points to an issue in the architecture (in the manual as "Particular delay slot issues"). If we have instructions causing exceptions in delay slots like l.sys it will cause looping.

Quoted 2 lines

With that,

Thank you.

-Stafford