Re: Kernel stack....

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

Raghava Raju wrote:

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

(I describe all platform specific things the way they are on i386 - other platforms may implement things differently):

The kernel stack is a (per process) area of 8KBytes. In the lowest part of that area you find the process descriptor (that's why the following definition can be used for
the current macro:          __asm__("andl %%esp,%0; ":"=r" (current) : "" (~8191UL)); /* esp is the stack pointer,  i.e. we just mask out the lowest 13 bits of the stack
pointer to get a pointer to the current process's descriptor).

The top of this area is used as actual stack (i.e. kernel functions invoked in the context of the corresponding process store e.g. local variables and the return address
there). The stack is accessed through a 'stack pointer'; each time an item is 'pushed' on the stack, the stack pointer is decremented by the size of the item, each time an
item is 'popped' off the stack, the stack pointer is incremented by the size of the item (note that items can only be popped off the stack in a last in first out manner).
Note that the stack is filled form top to bottom, that's why the stack pointer gets *decremented* when items are added (but that's done by the processor anyway).

Each time a process is scheduled, that process's stack pointer is written into the processor's stack register (esp), thus the process (running in kernel mode right after
being scheduled), finds 'it's' stack and has access to the local variables and the return addresses of kernel functions it was about executing before it left the CPU, thus
the stack of a process just beeing re-scheduled might look something like that (note that this is just a conceptional view, I didn't check the details - especially there
might be some more items on the stack, than the ones I mention):

local variable 1 of 1st kernel function called by the process
local variable 2 of 1st kernel function called by the process
local variable 3 of 1st kernel function called by the process
return address of 2nd kernel function called by the process (address within the code of the 1st function)
local variable 1 of 2nd kernel function called by the process
local variable 2 of 2nd kernel function called by the process
local variable 3 of 2nd kernel function called by the process
local variable 3 of 2nd kernel function called by the process
...
return address of last kernel function called by the process
local variable 1 of last kernel function called by the process
local variable 2 of last kernel function called by the process
local variable 3 of last kernel function called by the process
local variable 3 of last kernel function called by the process
/* note that the last kernel function called by the process before leaving the CPU is most often (or even always(?)) schedule() */
...
...
...
...
task descriptor of the process
[end of stack]



>
>
>       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.
>

You don't find text and data in the kernel stack. Note that the kernel stack is process specific, whereas the kernel text and data are shared by all processes, and accessed
by accessing kernel virtual addresses.

Regards
Martin

--
Supercomputing System AG          email: maletinsky@scs.ch
Martin Maletinsky                 phone: +41 (0)1 445 16 05
Technoparkstrasse 1               fax:   +41 (0)1 445 16 10
CH-8005 Zurich


-
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
IRC Channel:   irc.openprojects.net / #kernelnewbies
Web Page:      http://www.kernelnewbies.org/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux