public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* kernel stack
@ 2004-10-12  6:15 suthambhara nagaraj
  2004-10-12 11:09 ` Neil Horman
  0 siblings, 1 reply; 22+ messages in thread
From: suthambhara nagaraj @ 2004-10-12  6:15 UTC (permalink / raw)
  To: main kernel

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

^ permalink raw reply	[flat|nested] 22+ messages in thread
* RE: Kernel stack
@ 2004-10-13  5:03 Thekkedath, Gopakumar
  0 siblings, 0 replies; 22+ messages in thread
From: Thekkedath, Gopakumar @ 2004-10-13  5:03 UTC (permalink / raw)
  To: 'Dhiman, Gaurav', Jan Hudec, aq
  Cc: suthambhara nagaraj, main kernel, kernel





>You discussed that kernel do not keep track of SS for process specific
>kernel stack as it always starts from fixed offset of task_struct, but
>does that mean, linux kernel do not make use of SS element in the TSS of
>process. I think the kernel can not by-pass the rules defined by
>processor. Processor expects the SS and SP elements to be right at the
>time of stack switching, so we need to initialize them to the right
>values while forking a process.

	Yes, the kernel cannot bypass the TSS entirely.

>I think kernel definitely keep tracks of SS and SP for all CPU levels
>including kernel mode also (ring 0), so that when we are switching form
>user space to kernel space the processor can switch the stacks
>automatically. At stack switching time CPU expects the SS and SP
>elements of TSS to be right, it's just going to copy those values in SS
>and SP registers of CPU, so that now we can push and pop the things on
>the kernel stack.
	In Linxu , the SS and DS normally holds the same value. Mostly I
have seen that,\
 when in kernel mode, DS=SS=0x18 and when in user mode DS=SS=0x28 
(this is what i remember !). 

As Linux does not believe in x86 processor recommended task switching (ie
storing/retrieving all the
necessary register values in a TSS) it uses the process's stack to hold the
required registers. 
But, it cannot get away from the fact that, when a switch happens to kernel
mode,
 the processor expects to find the SS and ESP values for that privilege
level in the current TSS
 (the one which i pointed to be the TS register if i am correct!).

In Linux, we have only one TSS per processor, and when a process is
scheduled most 
probably the kernel sets the  TSS's SS0 (SS corresponding to ring 0) as 0x18
and ESP0 
will be current process's task_struct + 8K (in 2.4). Again, I have not
really gone through
 that piece of code in the kernel, so this is my assumption. Correct me if I
am wrong.


^ permalink raw reply	[flat|nested] 22+ messages in thread
* RE: Kernel stack
@ 2004-10-13  4:35 Dhiman, Gaurav
  2004-10-14 19:31 ` Denis Vlasenko
  0 siblings, 1 reply; 22+ messages in thread
From: Dhiman, Gaurav @ 2004-10-13  4:35 UTC (permalink / raw)
  To: Jan Hudec, aq; +Cc: suthambhara nagaraj, main kernel, kernel


You discussed that kernel do not keep track of SS for process specific
kernel stack as it always starts from fixed offset of task_struct, but
does that mean, linux kernel do not make use of SS element in the TSS of
process. I think the kernel can not by-pass the rules defined by
processor. Processor expects the SS and SP elements to be right at the
time of stack switching, so we need to initialize them to the right
values while forking a process.

I think kernel definitely keep tracks of SS and SP for all CPU levels
including kernel mode also (ring 0), so that when we are switching form
user space to kernel space the processor can switch the stacks
automatically. At stack switching time CPU expects the SS and SP
elements of TSS to be right, it's just going to copy those values in SS
and SP registers of CPU, so that now we can push and pop the things on
the kernel stack.

Yes it definitely can happen (and I think this is the way to do it) that
SS in process's TSS is initialized to the memory address just next to
task_struct (as the kernel stack can grow downwards till this limit)
while the forking of a process and also sets the SP to the fixed offset
from task_struct from where the kernel stack starts growing downwards
(as you mentioned kernel stack starts from there). Now whenever this
process will be scheduled by the scheduler or a process enters the
kernel mode, CPU's SS and SP registers are re-set to the SS and SP
elements in TSS of that process.

At the time of scheduling, scheduler also change the TSS descriptor for
that CPU on which the process is going to be scheduled. It changes this
TSS descriptor in GDT to point to the TSS of the selected process.

Correct me if I am wrong.

Cheers !!
Gaurav


-----Original Message-----
From: Jan Hudec [mailto:bulb@vagabond.light.src] On Behalf Of Jan Hudec
Sent: Tuesday, October 12, 2004 6:42 PM
To: aq
Cc: suthambhara nagaraj; Dhiman, Gaurav; main kernel; kernel
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
<bulb@ucw.cz>

^ permalink raw reply	[flat|nested] 22+ messages in thread
* RE: Kernel stack
@ 2004-10-12  7:51 Thekkedath, Gopakumar
  0 siblings, 0 replies; 22+ messages in thread
From: Thekkedath, Gopakumar @ 2004-10-12  7:51 UTC (permalink / raw)
  To: 'suthambhara nagaraj', Dhiman, Gaurav; +Cc: main kernel, kernel




-----Original Message-----
From: suthambhara nagaraj [mailto:suthambhara@gmail.com]
Sent: Tuesday, October 12, 2004 12:22 PM
To: Dhiman, Gaurav
Cc: main kernel; kernel
Subject: Re: Kernel stack


>in another structure( struct thread_struct ).A kernel stack of size 8k
>(By default)
>is actully shared by processes running on a processor. 
	Each process has its own kernel mode stack, in fact it starts from
the process's task_struct start address + 8K (atleast in 2.4). (This also
helps in the implementation of 'current')

>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


	Again, in linux implementaton, for each process, SS is the same ,
but ESP differs and hence the stack


On Tue, 12 Oct 2004 11:55:24 +0530, Dhiman, Gaurav <gaurav.dhiman@ca.com>
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: kernelnewbies-bounce@nl.linux.org
> [mailto:kernelnewbies-bounce@nl.linux.org] 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/
> 
>

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/

^ permalink raw reply	[flat|nested] 22+ messages in thread
[parent not found: <577528CFDFEFA643B3324B88812B57FE3055B9@inhyms21.ca.com>]
* Re: Kernel stack....
@ 2001-09-11 15:53 Richard J Moore
  0 siblings, 0 replies; 22+ messages in thread
From: Richard J Moore @ 2001-09-11 15:53 UTC (permalink / raw)
  To: Emmanuel Varagnat-AEV010; +Cc: Kernel Mailing List


If you have an interrupt stack, and that's not strictly necessary in OS
design, then you either need one per processor or to handle interrupts on
only one processor. If you use the task time kernel stack, which under IA32
you will do as soon as the CPU processes the interrupt gate for that IRQ,
then the interrupt stack frame wil appear on the task time stack. Things in
theory can continue this was. Eventually the stack will unwind with the
interrupt frame being finally removed with an IRET. No it is possible to
switch to a separate stack but that has to be engineered by the system
interrupt handler. I don't recall whether Linux does this. If it does
you'll see it happening in the /arch code for interrupt handling.

Richard Moore -  RAS Project Lead - Linux Technology Centre (ATS-PIC).
http://oss.software.ibm.com/developerworks/opensource/linux
Office: (+44) (0)1962-817072, Mobile: (+44) (0)7768-298183
IBM UK Ltd,  MP135 Galileo Centre, Hursley Park, Winchester, SO21 2JN, UK


                                                                                                                                   
                    Emmanuel Varagnat                                                                                              
                    <Emmanuel_Varagnat-AEV010@emai       To:     Richard J Moore/UK/IBM@IBMGB                                      
                    l.mot.com>                           cc:                                                                       
                                                         Subject:     Re: Kernel stack....                                         
                    11/09/2001 16:26                                                                                               
                    Please respond to Emmanuel                                                                                     
                    Varagnat-AEV010                                                                                                
                                                                                                                                   
                                                                                                                                   




Richard J Moore wrote:
>
> Emmanuel Varagnat wrote:
>
> >Yes but there is one stack per processor ?
>
> One per process.

Yes I know, but inside the kernel when an IRQ is handled
the kernel use its own stack. But if there is many processors
many IRQ handler are supposed "turn". And if they share the
same stack, data are melted.

Thanks for your answer.

-Manu





^ permalink raw reply	[flat|nested] 22+ messages in thread
* Kernel stack....
@ 2001-09-10 21:47 Raghava Raju
  2001-09-10 21:57 ` Erik Mouw
  2001-09-11 12:15 ` Richard B. Johnson
  0 siblings, 2 replies; 22+ messages in thread
From: Raghava Raju @ 2001-09-10 21:47 UTC (permalink / raw)
  To: linux-kernel; +Cc: kernelnewbies



Hi,

      1) I want to know what exactly is the structure
of kernel stack. Is it some thing like bss,data,text?

      2) I want to access kernel stack(in kernel
mode). So I am using  kernel stack pointer provided in
thread_struct. So how to access different areas(.i.e 
data,text)  in kernel stack.


       Any pointers will be helpful.

   Please mail me as I did't subscribe.
   vraghava_raju@yahoo.com

     Thank you.
     Raj. 

__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger
http://im.yahoo.com

^ permalink raw reply	[flat|nested] 22+ messages in thread
* Kernel stack...
@ 2001-08-27 22:24 Raghava Raju
  0 siblings, 0 replies; 22+ messages in thread
From: Raghava Raju @ 2001-08-27 22:24 UTC (permalink / raw)
  To: linux-kernel


 Hi 

   Kindly provide me with some basic insights
  into kernel.

 1) I want to know exact structure of kernel stack
  and process stack in memory. It would be helpful
  if description includes approximate memory addresses
  of kernel stack,process stack, and different regions
  in kernel stack and process stack. What happens 
  if there are multiple processes.

 2) What about "current" data structure. Where is it
  located  on memory. If I am not wrong it will be 
  just below the process stack.

 3)Which variable to use to access (I am in kernel
   code) kernel stack. In "task_struct" 
   kernel_stack_page is not defined. Mine is 
   "linux 2.4.2 mvista".

  I am not subscribed to this list, kindly send
  me the reply to vraghava_raju@yahoo.com .

__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2004-10-14 19:36 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-10-12  6:15 kernel stack suthambhara nagaraj
2004-10-12 11:09 ` Neil Horman
2004-10-13  3:29   ` suthambhara nagaraj
2004-10-14  3:15     ` suthambhara nagaraj
  -- strict thread matches above, loose matches on Subject: below --
2004-10-13  5:03 Kernel stack Thekkedath, Gopakumar
2004-10-13  4:35 Dhiman, Gaurav
2004-10-14 19:31 ` Denis Vlasenko
2004-10-12  7:51 Thekkedath, Gopakumar
     [not found] <577528CFDFEFA643B3324B88812B57FE3055B9@inhyms21.ca.com>
2004-10-12  6:51 ` suthambhara nagaraj
2004-10-12  9:41   ` Jan Hudec
2004-10-12 10:05     ` aq
2004-10-12 10:27       ` Jan Hudec
2004-10-12 12:30         ` aq
2004-10-12 13:11           ` Jan Hudec
2004-10-12 14:30     ` Jon Masters
2004-10-12 14:31       ` Jan Hudec
2001-09-11 15:53 Richard J Moore
2001-09-10 21:47 Raghava Raju
2001-09-10 21:57 ` Erik Mouw
2001-09-11 10:31   ` Emmanuel Varagnat
2001-09-11 12:15 ` Richard B. Johnson
2001-08-27 22:24 Raghava Raju

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox