kernel stack and user space stack

后端 未结 3 1037
孤独总比滥情好
孤独总比滥情好 2020-11-28 16:57

What\'s the difference between kernel stack and user stack? Why kernel stack is used? If a local variable is declared in an ISR, where it will be stored? Does each process h

3条回答
  •  旧巷少年郎
    2020-11-28 17:44

    My answer is collected from other SO questions with my stuffs.

    What's the difference between kernel stack and user stack?
    

    As a kernel programmer, you know that the kernel should be restricted from erroneous user programs. Suppose you keep same stack for both kernel & user space, then simple segfault in user application crashes kernel and needs restart.

    There is one "kernel stack" per CPU like ISR Stack and one "kernel stack" per Process. There is one "user stack" for each process, though each thread has its own stack, including both user and kernel threads.

    http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-10/3194.html

    Why kernel stack is used?
    

    So when we are in kernel mode, stack kind of mechanism is necessary for dealing with function calls, local variables similar to user space.

    http://www.kernel.org/doc/Documentation/x86/kernel-stacks

    If a local variable is declared in an ISR, where it will be stored?
    

    It will be stored in ISR stack(IRQSTACKSIZE). The ISR runs on a separate interrupt stack only if the hardware supports it. Otherwise, the ISR stack frames are pushed onto the stack of the interrupted thread.

    The user space does not know and frankly does not care about whether the interrupt is served in current process's kernel stack or a separate ISR stack. As interrupts comes per cpu, therefore ISR stack has to be per cpu.

     Does each process has its own kernel stack ?
    

    Yes. Each process has its own kernel stack.

     Then how the process coordinates between both these stacks?
    

    @FrankH's answer looks great to me.

提交回复
热议问题