jserv / mini-arm-os

Build a minimal multi-tasking OS kernel for ARM from scratch
Other
1.04k stars 242 forks source link

Revise comment for PendSV handler #22

Closed lecopzer closed 6 years ago

lecopzer commented 6 years ago

resolve #19

PendSV is not only an interrupt handler but where the context switch happends r7 is usually used to preserve system call number (isr number).

Without attribute, it looks like:

80005c8: b480 push {r7} 80005ca: af00 add r7, sp, #0 / Save the old task's context / asm volatile("mrs r0, psp\n" 80005cc: f3ef 8009 mrs r0, PSP But our pendsv_handler has context switch code at end of

asm volatile("mov r0, %0\n" : : "r" (tasks[lastTask].stack)); 40 / Restore the new task's context and jump to the task / 41 asm volatile("ldmia r0!, {r4-r11, lr}\n" 42 "msr psp, r0\n" 43 "bx lr\n"); The core register was changed here and we even have our own return code (line 41 to 43) that the compiler will never know this.

Compiler don't know we handle core register and stack in C function in our own and will never assume user "corrupt" calling convention. Compiler is doing its jobs performing regular push r7 and pop r7 Even in 7.2.1 arm-eabi compiler, there is still push r7 in pendsv_handler

So it's reasonable to have naked attribute where we do the context switch or have our own register/stack handling in C function and should not be a FIXME.