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

Re: Explanation needed for current macro



On Wednesday 01 December 2004 06:34, Jagadeesh Bhaskar P wrote:
> Hi all,
>  I was going through the explanation of the current macro, and found out
> that it was got by masking 13-bits LSB the esp register. So is it that
> always the process descriptor will start at a location with the last 13
> bits are 0??

No. I am very new to the kernel but I think you missed something.
On x86, current is calculated by masking the 13 least significant bits of the 
stack pointer to get the the thread_info structure. 

First you have:

static inline struct task_struct * get_current(void)
{
         return current_thread_info()->task;
}
#define current get_current()

Then current_thread_info() is defined as the following:

static inline struct thread_info *current_thread_info(void)
 {
         struct thread_info *ti;
         __asm__("andl %%esp,%0; ":"=r" (ti) : "" (~(THREAD_SIZE - 1)));
         return ti;
}

Did you notice the '~' operator in front of (THREAD_SIZE - 1)? I'm not used 
with inline Assebly but the previous code should mean, more or less:

movl $-8192, %eax 
andl %esp, %eax

So you have 13 * '1' in a register, and the "andl" instruction is executed 
between "1 1111 1111 1111" and the stack pointer. We don't have any zeros in 
the register %eax to be andl(ed) with %esp.
The result is written in "struct thread_info *ti" and then, in get_current() 
we have return "current_thread_info()->task;" that is like "ti->task;".

>
> I have read that both the kernel stack and process descriptor of a
> process is stored in together in an 8KB page. Now the offsets in the
> page should start from all bits 0, rite? So then why masking only the 13
> bits LSB?? What is the significance of keeping that length at 13??
>
> Please do help!!
>
> TIA

Ciao,

Fabio De Francesco

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