4
votes

I am able to get the execution of system calls invocation and it's processing in Kernel. But few things are not yet clear to me. Upon entering the swi routine, the Kernel saves the User mode registers on stack. The question is-

  1. Who's stack is it? (As swi handling and the corresponding system call routine needs the stack frame to work upon)

  2. If it is Kernel's own stack, from where will get the stack allocated..? Will it start using the current's stack? If yes, then current can be any process that might be executing at that moment in kernel. Does this not exhaust current's stack?

  3. If it uses the currently executing User process's stack in swi handler, then this will be User address space which kernel will now be accessing. Is this possible? As the kernel addressable memory is within 1GB (if 1:3 Kernel-to-User address space ratio is used in a 4GB RAM memory system).

2

2 Answers

5
votes

Most ARM modes have a separate stack. The stacks are usually set up shortly after reset handler. From arch/arm/kernel/setup.c:

/*
 * setup stacks for re-entrant exception handlers
 */
__asm__ (
"msr    cpsr_c, %1\n\t"
"add    sp, %0, %2\n\t"
"msr    cpsr_c, %3\n\t"
"add    sp, %0, %4\n\t"
"msr    cpsr_c, %5\n\t"
"add    sp, %0, %6\n\t"
"msr    cpsr_c, %7"
    :
    : "r" (stk),
      "I" (PSR_F_BIT | PSR_I_BIT | IRQ_MODE),
      "I" (offsetof(struct stack, irq[0])),
      "I" (PSR_F_BIT | PSR_I_BIT | ABT_MODE),
      "I" (offsetof(struct stack, abt[0])),
      "I" (PSR_F_BIT | PSR_I_BIT | UND_MODE),
      "I" (offsetof(struct stack, und[0])),
      "I" (PSR_F_BIT | PSR_I_BIT | SVC_MODE)
    : "r14");

P.S. SVC is the current name for what was called SWI.

0
votes

It is true that the stack is specific to ARM modes.

This is the fast syscall return path. We do as little as possible here, and this includes saving r0 back into the SVC stack.

The above lines are quoted in entry-common.S. So the stack is SVC stack. (Note: swi is replaced by svc).