I am studying System call in the linux. I know that is using SVC exception handler.
the related codes are as follow
====================exception handler codes===================================
ENTRY(vector_swi)
sub sp, sp, #S_FRAME_SIZE
stmia sp, {r0 - r12} @ Calling r0 - r12
add r8, sp, #S_PC
stmdb r8, {sp, lr}^ @ Calling sp, lr
mrs r8, spsr @ called from non-FIQ mode, so ok.
str lr, [sp, #S_PC] @ Save calling PC
str r8, [sp, #S_PSR] @ Save CPSR
str r0, [sp, #S_OLD_R0] @ Save OLD_R0
zero_fp
==========================================================================
I've read some documents when the svc exception occurs, there are som copy operation which is
that the lr_usr is written into lr_svc and cpsr is written into spsr_svc to resore user-mode's context.
I think that the codes above is all about saving user-mode process context.
First Question is:
the first line is that "sub sp, sp, #S_FRAME_SIZE".
I want to know the sp above code means.
is it sp_usr or sp_svc? i guess this is the sp_svc because we are in the svc exception handler. so we should use the banked-sp register
is that right?
Second Question is:
if it were sp_svc. i want to know where the svc stack is initialized. i could not have found the code related to initialing it in the linux(version 2.6.21)
is it process's stack?
the linux kernel starts with SVC mode, and initialize the ilde stack (PID = 0) whose variable name is "init_thread_union".
i think this is the first stack initialization about svc mode. then sp means this stack?
could somebody help me about this?
thanks
I've read some documents when the svc exception occurs, there are som copy operation which isthat the lr_usr is written into lr_svc and cpsr is written into spsr_svc to resore user-mode's context.
The processor will set lr_svc to the return address for the exception (the next unexecuted instruction in program order).
First Question is:the first line is that "sub sp, sp, #S_FRAME_SIZE".I want to know the sp above code means.is it sp_usr or sp_svc? i guess this is the sp_svc because we are in the svc exception handler. so we should use the banked-sp register
Yes, it will sp_svc (the stack pointer for the mode the processor is currently in).
Second Question is:if it were sp_svc. i want to know where the svc stack is initialized. i could not have found the code related to initialing it in the linux(version 2.6.21)is it process's stack?the linux kernel starts with SVC mode, and initialize the ilde stack (PID = 0) whose variable name is "init_thread_union".i think this is the first stack initialization about svc mode. then sp means this stack?
I'm afraid that I'm not a Linux expert, so can't help you with that one.
Note: Your kernel is fairly old. You might want to look at using something more modern.
First, Thanks for your commet.
i think it was helpful with me
I have been struggling with this my question for a week.
so I like to summarize my questions like below
About my first question,
In the exception handler of surpervisor mode(system call handler), the sp is the stack pointer of the svc mode. it is the banked register of the svc mode.
the following instruction makes an error in the GCC compiler
instruction :"mov r0, sp_svc"
Error Message: immediate expression requires a # prefix -- `mov r0,sp_svc'
the programmer can not use that mnemonic of "sp_svc" because the ARM core can only changes the sp_usr register into the banked register sp-svc.
the sp means sp_usr in the user mode or sp_svc in the svc mode.
About My Second Question,
the sp_svc register is first initialized by "init_thread_union" in the linux. that is the stack of the idle task(pid=0, swapper).
this idle task creates the init process. and the init process creates many child processes (the task is the same as the process in the linux.).
the kernel stack is allocated in memory when the child process is created. this stack is initialized into the child process context(thread_info.cpu_context.sp)
every user-process(user-mode process) has two stack. one is user-mode stack and the other is kernel-stack(svc-mode stack). this user-process uses the user-mode stack executing in the user-mode and uses the kernel-stack executing in the svc-mode( this svc-mode is entered by system call).
the linux scheduler(__switch_to) changes this sp_svc register when the context-switch occurs. the kernel scheduler does not know the user-mode stack. It is saved in the kernel stack when the user-process entered into the svc-mode using system call. every kernel stack has a user-mode process context including user-mode stack in the top 80bytes area which is referred to "pt_regs". the "ret_to_user" can restore this user process context including the user-mode stack pointer into user-mode from svc mode.