2010/11/18 Parmenides <mobile.parmeni...@gmail.com>

> Hi,
>
>   According to ULK 3rd edition, the kernel stack is located in user
> space, such as a linear address of 0x015fa000。


Not Quite. For each process, there are 2 stacks - User Stack & a Kernel
Stack . This Per-Process kernel stack is located in the data segment of the
kernel. It is not located in the user space as you understood. When a
process switches to kernel mode, the hardware segmentation registers cs(code
segment) & ds(data segment) are set to __KERNEL_CS & __KERNEL_DS
respectively. Hence, depending on whether the process is executing in the
user mode or kernel mode, the user stack or kernel stack is found according
to the current value of the DS segmentation register.

As far as this situation
> is concerned, I have several questions.
>
> 1. Now that the kernel stack is used by the kernel code, why isn't it
> allocated in the kernel space?
>
Question is based on a wrong assumption of yours. Kernel stack is in the
kernel data segment.

2. For the kernel code, is it feasible to the use the user stack? Why
> do we bother to switch to the kernel stack?
>
The answer is Yes, you could. But it would be pretty messy & inconvenient.
We just don't do it in the linux kernel atleast on x86. Kernel Data Segment
& User Data Segment is different. I guess you could just map the user space
stack in the kernel address space too & use it. Using two seperate stacks is
just more efficient & convenient.

3. What's the difference between the user space and the kernel space on
> earth?
>
Well, i guess this question should have been at the top. Before you can
understand the difference between user space stack & kernel space stack, you
have to know the difference between user space & kernel space. How could you
ask questions 1 & 2 without knowing 3??  Well, i am not sure about the
"earth" but i can tell you the difference in a computer. :))) .

x86 processor supports multiple privilege levels. The kernel runs in ring 0
(most privilaged) and the user space runs in ring 3(least privileged). There
are certain instructions which you can execute only in the
privileged(kernel) mode. O'wise that instruction will result in a trap.
"cli" is an example of such instruction (used for disabling interrupts). So,
applications programs run in unprivilaged mode & hence only execute
instructions which no other process needs to know about. For ex. if you
concat two strings, nobody else needs to know about it. On the other hand,
if you want to execute any instruction like "cli" or reading from file or
using a device, you have to goto the kernel which acts as the mediator for
all processes. Thats why have Operating systems -  Thats why we have user
mode & kernel mode.

Venkatram Tummala

>
> --
> To unsubscribe from this list: send an email with
> "unsubscribe kernelnewbies" to ecar...@nl.linux.org
> Please read the FAQ at http://kernelnewbies.org/FAQ
>
>

Reply via email to