Re: Kernel Stack

Alexander Viro (viro@math.psu.edu)
Fri, 9 Apr 1999 15:25:06 -0400 (EDT)


On Fri, 9 Apr 1999, Richard B. Johnson wrote:

> On Fri, 9 Apr 1999, Brian Gerst wrote:
>
> > Livia Catarina Soares wrote:
> > > 1- Where is the kernel stack ??
> > > Is it above the address "0xC0000000" in the kernel memory ??
> >
> > There is one kernel stack for every user process, and it is mapped in
> > kernel memory (above PAGE_OFFSET). Note that even in an interrupt
> > context, the kernel stack of whatever user process was running is used.
> > ^^^^^^^^^^^^
>
> The kernel stack is a kernel stack. It is not associated with a
> user process. If this was not true, a user could crash the system.

Yes, it is. There is a separate stack for each process. On the context
switch both ring 3 and ring 0 ESPs are changed. Read the source. When the
processor goes into ring 0 it gets the correspondent kernel (== ring 0)
stack. User *can't* crash the system that way, since user code has no
access to the stack in question.

>
> #include <stdio.h>
>
> int main()
> {
> __asm__(
> "nop \n \
> movl $0,%esp \n"
> );
> for(;;)
> ;
> }
>
> All interrupts are handled on the kernel stack.

Yes. And movl $0, %esp will set ESP to 0. Right. When x86 goes
through the interrupt gate and target ring differs from the current it
takes the values of ESS and ESP from the TSS (4 pairs of DWORDs, one for
each ring). Since interrupt gates do not allow parameter-passing it will
not touch the old stack at all. No problems (until you'll return to user
mode, that is).
Kernel stack is different for each task. It has to be - think of
the situation when process A is in the kernel mode and has the processor,
process B is in the kernel mode and waits for execution, A decides to
sleep (i.e. call schedule()). Control is passed to B. Which expects to see
its stack *and* free space below the ESP.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/