2013年11月5日 星期二

process switch for stm32f4discovery (2) - save/restore stack

這個版本在 proc_a 使用了 stack, 用來測試 stack 的儲存與回復。ref: L85 ~ L86

process_2.S
  1 # not process switch, this is context switch
  2 
  3 @.equ STACK_TOP, 0x20000800
  4 .text
  5 .global _start
  6 .code 16
  7 .syntax unified
  8 _start:
  9   .word STACK_TOP, start 
 10   .type start, function @ let lsb to 1
 11 
 12   .word int_isr+1
 13   .word hardf_isr+1
 14   .word mmf_isr+1
 15   .word int_isr+1
 16   .word int_isr+1
 17   .word int_isr+1
 18   .word int_isr+1
 19   .word int_isr+1
 20   .word int_isr+1
 21   .word svc_isr+1 @ svc isr
 22   .word int_isr+1
 23   .word int_isr+1
 24   .word int_isr+1
 25   .word int_isr+1
 26   .word int_isr+1
 27   .word int_isr+1
 28   .word int_isr+1
 29 
 30 start:
 31   @ init a proc
 32   ldr r0, =proc_a_stack
 33 
 34   @ setup proc a psr
 35   mov r1, #0x21000000
 36   str r1, [r0, #(-4)]
 37 
 38   @ setup proc a pc
 39   ldr r1, =proc_a
 40   str r1, [r0, #(-8)]
 41 
 42   @@@@@@@@@@@@@@@@@@@
 43 
 44   @ init b proc
 45   ldr r0, =proc_b_stack
 46 
 47   @ setup proc b psr
 48   mov r1, #0x21000000
 49   str r1, [r0, #(-4)]
 50 
 51   @ setup proc b pc
 52   ldr r1, =proc_b
 53   str r1, [r0, #(-8)]
 54 
 55   @@@@@@@@@@@@@@@@@@@
 56 
 57   ldr r0, =proc_a_sp
 58   ldr r1, =proc_a_stack
 59   sub r1, #(16*4)
 60   str r1, [r0]
 61 
 62   ldr r0, =proc_b_sp
 63   ldr r1, =proc_b_stack
 64   sub r1, #(16*4)
 65   str r1, [r0]
 66 
 67   ldr r0, =cur_proc
 68   mov r1, #0
 69   str r1, [r0]
 70 
 71   ldr r1, =proc_a_sp
 72   ldr r0, [r1]
 73 
 74   @ ref: scmrtos-code/Ports/CortexM3/GCC/OS_Target_asm.S
 75   LDR     r4, [r0, #(4 * 14)]     @ Load process entry point into R4
 76   add     r0, #(4 * 16)           @ emulate context restore
 77   mov sp, r0
 78 @  dsb
 79   isb                             @ Insert a barrier
 80 
 81   bx r4                           @ run proc_a
 82 
 83 .type proc_a, function @ let lsb to 1
 84 proc_a:
 85   movs r0, #0x10
 86   push {r0}
 87   svc 0
 88   nop
 89   b proc_a
 90 
 91 .type proc_b, function @ let lsb to 1
 92 proc_b:
 93   movs r0, #0x20
 94   movs r1, #0x21
 95   movs r2, #0x22
 96   movs r3, #0x23
 97   movs r4, #0x24
 98   movs r5, #0x25
 99   movs r6, #0x26
100   movs r7, #0x27
101   movs r8, #0x28
102   movs r9, #0x29
103   movs r10, #0x2a
104   movs r11, #0x2b
105   movs r12, #0x2c
106   nop
107   svc 0
108   b proc_b
109 
110 
111 .type del_func, function @ let lsb to 1
112 del_func:
113   mov r5, #5
114   b del_func
115 
116 int_isr:
117   mov r0, #67
118   bx lr
119 
120 hardf_isr:
121   mov r0, #0x3b
122   bx lr
123 @ memory manage fault
124 mmf_isr:
125   mov r0, #0x3a
126   bx lr
127 
128 svc_isr:
129   cpsid i @Prevent interruption during context switch
130 
131   push {r4-r11}
132 
133   @ choice another process
134   ldr r1, =cur_proc
135   ldr r0, [r1]
136 
137   cmp r0, #1
138   beq.n 1f
139   @ current proc a, switch to proc b
140   ldr r2, =proc_a_sp
141   str sp, [r2] @ save cur process sp
142 
143   mov r3, #1
144   str r3, [r1]
145 
146   ldr r0, =proc_b_sp
147 
148   b 2f
149 1:
150   @ current proc b, switch to proc a
151   ldr r2, =proc_b_sp
152   str sp, [r2] @ save cur process sp
153 
154   mov r3, #0
155   str r3, [r1]
156 
157   ldr r0, =proc_a_sp
158 
159 2:
160 
161   ldr r0, [r0]
162 
163   mov sp, r0
164   pop {R4-R11}     // Restore r4-11 from new process stack
165 
166   cpsie i
167   bx lr
168 
169 
170 .data
171 
172 proc_a_sp:
173 .word 1
174 proc_b_sp:
175 .word 2
176 
177 proc_a_ptr:
178 .space  0x300, 0
179 proc_a_stack:
180 
181 proc_b_ptr:
182 .space  0x300, 0
183 proc_b_stack:
184 
185 
186 cur_proc:
187 .word 
188 
189 .space  0x300, 0
190 STACK_TOP:

下圖上方的示意圖是第一次 proc a 執行時 stack 變化, 下方的示意圖為第二次執行 proc a 時 stack 變化。

疑!寫完了, 這篇真短。



對於畫出精美的圖示我實在不在行, 不過手工圖彌補了這個缺點, 大家將就著看吧。

沒有留言:

張貼留言

使用 google 的 reCAPTCHA 驗證碼, 總算可以輕鬆留言了。

我實在受不了 spam 了, 又不想讓大家的眼睛花掉, 只好放棄匿名留言。這是沒辦法中的辦法了。留言的朋友需要有 google 帳號。