2004-10-12 06:52:08

by suthambhara nagaraj

[permalink] [raw]
Subject: Re: Kernel stack

Hi,
The problem is each process does not have a TSS of its own.Only one
TSSper processor is present and the process dependant features (Like
esp) are stored
in another structure( struct thread_struct ).A kernel stack of size 8k
(By default)
is actully shared by processes running on a processor. There is a func named
load_tss (or something similiar) which loads the TSS from the
thread_struct structure during task switch .

A Process does not have an SS entry in its thread_struct but only an
esp (and esp0) entry. This made me believe that the stack base is the
same.
Correct me

Regards


On Tue, 12 Oct 2004 11:55:24 +0530, Dhiman, Gaurav <[email protected]> wrote:
>
> > I have not understood how the common kernel stack in the
> > init_thread_union(2.6 ,init_task_union in case of 2.4) works for all
> > the processes which run on the same processor
>
> As far as I know, Kernel do not have any common stack for all the
> processes running over it. Whenever we enter the kernel mode thru system
> calls, we go thru system gate or descriptor (0x80 entry) in IDT. This
> entry contains the index of the descriptor in GDT (normally it points to
> Kernel CS Segment Descriptor in GDT) and the offset (pointer) to the
> code to be executed in kernel mode (which is system_call() function in
> Kernel).
>
> Now the descriptor entry in GDT pointed out by the system gate entry in
> IDT, contains 2 bit field known as DPL (Desired Privelege Level). If
> this DPL is less than the CPL (Current Prevelege Level) of CPU then CPU
> switches to the process specific kernel stack segement by refferring the
> TSS of current running process. This stack switch is automatic by CPUand
> there is no assembly intruction required for it.
>
> This stack switch is done at the time when we enter from user space to
> the kernel space, this is done because we can not trust and share the
> user process stack (stack used by user process in user mode). That is
> why every process has atleast two and can even have four stacks. In each
> process, stack for every CPU level (ring level) is defined. So whenever
> the process runs in user mode (ring 3), its user mode stack is used, but
> when it enters the kernel mode (ring 0) its stack is switched to the
> kernel stack of that process. All the stacks of a process for different
> levels of CPU are tracked thru TSS defined for that process.
>
> To read more on IDT, GDT, TSS and System Calls invocation, refer to the
> Intels System Programmer's Guide. Her is the Link:
> ftp://download.intel.com/design/PentiumII/manuals/24319202.pdf
>
> Correct me if I am wrong somewhere.
>
> Cheers !!
> Gaurav
>
>
>
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of suthambhara
> nagaraj
> Sent: Tuesday, October 12, 2004 10:31 AM
> To: kernel
> Subject: Kernel stack
>
> Hi all,
>
> I have not understood how the common kernel stack in the
> init_thread_union(2.6 ,init_task_union in case of 2.4) works for all
> the processes which run on the same processor. The scheduling is round
> robin and yet the things on the stack (saved during SAVE_ALL) have to
> be maintained after a switch without them getting erased. I am
> familiar with only the i386 arch implementation.
>
> Please help
>
> regards,
> Suthambhara
>
> --
> Kernelnewbies: Help each other learn about the Linux kernel.
> Archive: http://mail.nl.linux.org/kernelnewbies/
> FAQ: http://kernelnewbies.org/faq/
>
>


2004-10-12 09:42:46

by Jan Hudec

[permalink] [raw]
Subject: Re: Kernel stack

On Tue, Oct 12, 2004 at 12:21:37 +0530, suthambhara nagaraj wrote:
> Hi,
> The problem is each process does not have a TSS of its own.Only one
> TSSper processor is present and the process dependant features (Like
> esp) are stored
> in another structure( struct thread_struct ).A kernel stack of size 8k
> (By default)
> is actully shared by processes running on a processor. There is a func named
> load_tss (or something similiar) which loads the TSS from the
> thread_struct structure during task switch .

Yes. Thus each process has it's own TSS. It is just stored differently
when the process is not scheduled.

It comes out of that, that the stack is NOT shared among different
processes, because it is replaced whenever a different process is
scheduled on a CPU.

> A Process does not have an SS entry in its thread_struct but only an
> esp (and esp0) entry. This made me believe that the stack base is the
> same.

There is no SS entry, because SS does not specify the stack. It is siply
a segment in which the stack lives. Any segment, that covers all address
space will do! IIRC in kernel SS == DS.

The base of the stack does not have to be stored either, because it is
AT FIXED OFFSET from the task_struct! If you don't believe me, look at
definition of the current macro. It says just (%esp & ~8195) (it says it
in assembly, because you can't directly access registers from C, and it
uses some macros that mean "two pages" instead of 8195).

The kernel stack is allocated together with the task_struct. Two pages
are allocated and task_struct is placed at the start while the stack is
placed at the end and grows down towards the task_struct.

-------------------------------------------------------------------------------
Jan 'Bulb' Hudec <[email protected]>


Attachments:
(No filename) (1.73 kB)
signature.asc (189.00 B)
Digital signature
Download all attachments

2004-10-12 10:06:01

by Nguyen Anh Quynh

[permalink] [raw]
Subject: Re: Kernel stack

> There is no SS entry, because SS does not specify the stack. It is siply
> a segment in which the stack lives. Any segment, that covers all address
> space will do! IIRC in kernel SS == DS.
yes, if I am right, in Linux SS, DS and CS all point to the same base
address ( = 0 ?). To be exact, SS !=DS, since the segment registers in
protected mode point to segment selectors (in GDT ?), and we should
compare the value stored GDT entries, not the value of SS and DS in
this case.

> The kernel stack is allocated together with the task_struct. Two pages
> are allocated and task_struct is placed at the start while the stack is
> placed at the end and grows down towards the task_struct.
2 pages of kernel stack or not is optional. Recently version of kernel
allow you to choose to use 4K or 8K size for kernel stack.

>From what you all discuss, I can say: kernel memory is devided into 2
part, and the upper part are shared between processes. The below part
(the kernel stack, or 8K traditionally) is specifict for each process.

Is that right?

Regards,
AQ

2004-10-12 10:28:02

by Jan Hudec

[permalink] [raw]
Subject: Re: Kernel stack

On Tue, Oct 12, 2004 at 19:05:52 +0900, aq wrote:
> > There is no SS entry, because SS does not specify the stack. It is siply
> > a segment in which the stack lives. Any segment, that covers all address
> > space will do! IIRC in kernel SS == DS.
> yes, if I am right, in Linux SS, DS and CS all point to the same base
> address ( = 0 ?). To be exact, SS !=DS, since the segment registers in
> protected mode point to segment selectors (in GDT ?), and we should
> compare the value stored GDT entries, not the value of SS and DS in
> this case.

Of course. Though since DS and SS need the same parameters, they might
actualy point to the same GDT entry. I don't know if they actualy do,
though.

By the way, C compilers usualy set SS and DS with same base. They would
have to do conversion when taking pointers to local variables otherwise.

> > The kernel stack is allocated together with the task_struct. Two pages
> > are allocated and task_struct is placed at the start while the stack is
> > placed at the end and grows down towards the task_struct.
> 2 pages of kernel stack or not is optional. Recently version of kernel
> allow you to choose to use 4K or 8K size for kernel stack.

Yes, it does. Few people touch the "Kernel Hacking" though...

> >From what you all discuss, I can say: kernel memory is devided into 2
> part, and the upper part are shared between processes. The below part
> (the kernel stack, or 8K traditionally) is specifict for each process.
>
> Is that right?

No, it's not. There is just one kernel memory. In it each process has
it's own task_struct + kernel stack (by default 8K). There is no special
address mapping for these, nor are they allocated from a special area.

When a context of some process is entered, esp is pointed to the top of
it's stack. That's exactly all it takes to exchange stacks.

-------------------------------------------------------------------------------
Jan 'Bulb' Hudec <[email protected]>


Attachments:
(No filename) (1.91 kB)
signature.asc (189.00 B)
Digital signature
Download all attachments

2004-10-12 12:30:58

by Nguyen Anh Quynh

[permalink] [raw]
Subject: Re: Kernel stack

> > >From what you all discuss, I can say: kernel memory is devided into 2
> > part, and the upper part are shared between processes. The below part
> > (the kernel stack, or 8K traditionally) is specifict for each process.
> >
> > Is that right?
>
> No, it's not. There is just one kernel memory. In it each process has
> it's own task_struct + kernel stack (by default 8K). There is no special
> address mapping for these, nor are they allocated from a special area.
>
> When a context of some process is entered, esp is pointed to the top of
> it's stack. That's exactly all it takes to exchange stacks.

OK, lets say there are 20 processes running in the system. Then the
kernel must allocate 20 * 8K = 160K just for the stacks of these
processes. All of these 160K always occupy the kernel (kernel memory
is never swapped out). When a process actives, ESP would switch to
point to the corresponding stack (of that process).

The remainding memory of kernel therefore is equally accessible to all
the processes.

Is that correct ?

Thank you,
AQ

2004-10-12 13:12:49

by Jan Hudec

[permalink] [raw]
Subject: Re: Kernel stack

On Tue, Oct 12, 2004 at 21:30:54 +0900, aq wrote:
> > > >From what you all discuss, I can say: kernel memory is devided into 2
> > > part, and the upper part are shared between processes. The below part
> > > (the kernel stack, or 8K traditionally) is specifict for each process.
> > >
> > > Is that right?
> >
> > No, it's not. There is just one kernel memory. In it each process has
> > it's own task_struct + kernel stack (by default 8K). There is no special
> > address mapping for these, nor are they allocated from a special area.
> >
> > When a context of some process is entered, esp is pointed to the top of
> > it's stack. That's exactly all it takes to exchange stacks.
>
> OK, lets say there are 20 processes running in the system. Then the
> kernel must allocate 20 * 8K = 160K just for the stacks of these
> processes. All of these 160K always occupy the kernel (kernel memory
> is never swapped out). When a process actives, ESP would switch to
> point to the corresponding stack (of that process).

This is correct.

> The remainding memory of kernel therefore is equally accessible to all
> the processes.

This is not. There is nothing like "remaining memory". **ALL* kernel
memory is equally accessible to all the processes.

There is noting special about the stacks and task-structs. They are
normal 8K structures somewhere in kernel memory.

> Is that correct ?

-------------------------------------------------------------------------------
Jan 'Bulb' Hudec <[email protected]>


Attachments:
(No filename) (1.47 kB)
signature.asc (189.00 B)
Digital signature
Download all attachments

2004-10-12 14:35:07

by Jan Hudec

[permalink] [raw]
Subject: Re: Kernel stack

On Tue, Oct 12, 2004 at 15:30:34 +0100, Jon Masters wrote:
> On Tue, 12 Oct 2004 11:41:04 +0200, Jan Hudec <[email protected]> wrote:
>
> > The base of the stack does not have to be stored either, because it is
> > AT FIXED OFFSET from the task_struct! If you don't believe me, look at
> > definition of the current macro. It says just (%esp & ~8195) (it says it
> > in assembly, because you can't directly access registers from C, and it
> > uses some macros that mean "two pages" instead of 8195).
>
> The pedant in me wants to point out that 8K is 0-8191 and not 0-8195 :-)

OOPS, a braino ;-).

-------------------------------------------------------------------------------
Jan 'Bulb' Hudec <[email protected]>


Attachments:
(No filename) (713.00 B)
signature.asc (189.00 B)
Digital signature
Download all attachments

2004-10-12 14:52:12

by Jon Masters

[permalink] [raw]
Subject: Re: Kernel stack

On Tue, 12 Oct 2004 11:41:04 +0200, Jan Hudec <[email protected]> wrote:

> The base of the stack does not have to be stored either, because it is
> AT FIXED OFFSET from the task_struct! If you don't believe me, look at
> definition of the current macro. It says just (%esp & ~8195) (it says it
> in assembly, because you can't directly access registers from C, and it
> uses some macros that mean "two pages" instead of 8195).

The pedant in me wants to point out that 8K is 0-8191 and not 0-8195 :-)

Jon.