[PATCH 1/3] OpenRISC: Account for red-zone (fixup printf bug).

Hesham ALMatary heshamelmatary at gmail.com
Fri Sep 5 15:11:00 UTC 2014


This patch allocate a space in the stack to account for red-zone
that gcc may utilize for the use of leaf functions. Care must be
taken to handle this red-zone from many places:
1- Upon creation of a new thread stack.
2- Once an interrupt entred.

Also moving the enable-thread-dispach() and increment ISR level before
checking if dispatch needed was required.

The previous modifications solved the printf bug which disabled ticker
to output strings after context switches that arise from Thread_Delay_ended.
---
 c/src/lib/libbsp/or1k/or1ksim/start/start.S        |  2 +-
 cpukit/score/cpu/or1k/or1k-context-initialize.c    |  3 +-
 cpukit/score/cpu/or1k/or1k-exception-handler-low.S | 42 +++++++++++-----------
 3 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/c/src/lib/libbsp/or1k/or1ksim/start/start.S b/c/src/lib/libbsp/or1k/or1ksim/start/start.S
index 6942b52..4d911d4 100644
--- a/c/src/lib/libbsp/or1k/or1ksim/start/start.S
+++ b/c/src/lib/libbsp/or1k/or1ksim/start/start.S
@@ -16,7 +16,7 @@
  */
 #define EXCEPTION_SETUP(vector) \
   l.nop   ;\
-  l.addi  r1, r1, -4 ;\
+  l.addi  r1, r1, -200 ;\
   l.sw    0(r1), r3; \
   l.addi  r3, r0, vector; \
   l.j     _ISR_Handler; \
diff --git a/cpukit/score/cpu/or1k/or1k-context-initialize.c b/cpukit/score/cpu/or1k/or1k-context-initialize.c
index 7ac2875..1c9cdd7 100644
--- a/cpukit/score/cpu/or1k/or1k-context-initialize.c
+++ b/cpukit/score/cpu/or1k/or1k-context-initialize.c
@@ -29,7 +29,8 @@ void _CPU_Context_Initialize(
   void *tls_area
 )
 {
-  uint32_t stack = (uint32_t) stack_area_begin;
+  /* Decrement 200 bytes to account for red-zone */
+  uint32_t stack = ((uint32_t) stack_area_begin) - 200;
   uint32_t sr;
 
   sr = _OR1K_mfspr(CPU_OR1K_SPR_SR);
diff --git a/cpukit/score/cpu/or1k/or1k-exception-handler-low.S b/cpukit/score/cpu/or1k/or1k-exception-handler-low.S
index 7108ff6..b18c4a3 100644
--- a/cpukit/score/cpu/or1k/or1k-exception-handler-low.S
+++ b/cpukit/score/cpu/or1k/or1k-exception-handler-low.S
@@ -93,7 +93,7 @@ PUBLIC(_ISR_Handler)
   l.sw    0(r8), r7
 
   /* Save interrupted task stack pointer */
-  l.addi r4, r1, 144
+  l.addi r4, r1, 340
   l.sw   4(r1), r4
 
   /* Save interrupted task r3 (first arg) value */
@@ -118,7 +118,7 @@ PUBLIC(_ISR_Handler)
   /* Do not switch stacks if we are in a nested interrupt. At
    * this point r5 should be holding ISR_NEST_LEVEL value.
    */
-  l.sfgtui r5, 2
+  l.sfgtui r5, 1
   l.bf jump_to_c_handler
   l.nop
 
@@ -134,6 +134,21 @@ jump_to_c_handler:
   /* Switch back to the interrupted task stack */
   l.add r1, r14, r0
 
+  /* Decrement nesting level */
+  l.movhi r6, hi(ISR_NEST_LEVEL)
+  l.ori   r6, r6, lo(ISR_NEST_LEVEL)
+
+  /* Enable multitasking */
+  l.movhi r8, hi(THREAD_DISPATCH_DISABLE_LEVEL)
+  l.ori   r8, r8, lo(THREAD_DISPATCH_DISABLE_LEVEL)
+
+  l.lwz   r5, 0(r6)
+  l.lwz   r7, 0(r8)
+  l.addi  r5, r5, -1
+  l.addi  r7, r7, -1
+  l.sw    0(r6), r5
+  l.sw    0(r8), r7
+  
   /* Check if dispatch needed */
   l.movhi r31, hi(DISPATCH_NEEDED)
   l.ori   r31, r31, lo(DISPATCH_NEEDED)
@@ -163,21 +178,6 @@ jump_to_c_handler:
   l.lwz  r13,  136(r1)
   l.mtspr r0, r13, CPU_OR1K_SPR_ESR0
 
-  /* Increment nesting level */
-  l.movhi r6, hi(ISR_NEST_LEVEL)
-  l.ori   r6, r6, lo(ISR_NEST_LEVEL)
-
-  /* Disable multitasking */
-  l.movhi r8, hi(THREAD_DISPATCH_DISABLE_LEVEL)
-  l.ori   r8, r8, lo(THREAD_DISPATCH_DISABLE_LEVEL)
-
-  l.lwz   r5, 0(r6)
-  l.lwz   r7, 0(r8)
-  l.addi  r5, r5, -1
-  l.addi  r7, r7, -1
-  l.sw    0(r6), r5
-  l.sw    0(r8), r7
-
   l.lwz  r2,  8(r1)
   l.lwz  r3,  12(r1)
   l.lwz  r4,  16(r1)
@@ -208,10 +208,12 @@ jump_to_c_handler:
   l.lwz  r29, 116(r1)
   l.lwz  r30, 120(r1)
   l.lwz  r31, 124(r1)
-
+  
+  /* Unwind exception frame */
   l.addi r1, r1, 140
-
-  l.addi r1, r1, 4
+  
+  /* Red-zone */
+  l.addi r1, r1, 200
 
   l.rfe
   l.nop
-- 
1.9.3



More information about the devel mailing list