Hi list, I would like to know how the thread_info address is calculated? As per the LKD book: struct thread_info is stored on the kernel stack. On x86, current is calculated by masking out the 13 least-significant bits of the stack pointer to obtain the thread_info structure.This is done by the current_thread_info() function.The assembly is shown here: movl $-8192, %eax andl %esp, %eax This assumes that the stack size is 8KB. But in code i found that: /* how to get the thread information struct from C */ static inline struct thread_info *current_thread_info(void) { return (struct thread_info *) (current_stack_pointer & ~(THREAD_SIZE - 1)); } /* how to get the current stack pointer from C */ static inline unsigned long current_stack_pointer(void) { unsigned long sp; asm("mov sp,%0; ":"=r" (sp)); return sp; } Could anyone help me in explaining the current_stack_pointer and (current_stack_pointer & ~(THREAD_SIZE - 1)) calculation? This is not as mentioned in the book. If page size is 4KB and 2 page per process kernel stack is used then THREAD_Size will be 8KB. Thanks, Vijay
It's the same thing as you read: THREAD size is 8kb so the operation looks like the following: current_stack_pointer & ~(8191) == current_stack_pointet & 0xFFFFFE00 (last 13 bits are 0) On Fri, Sep 16, 2011 at 3:38 PM, Vijay Chauhan <kernel.vijay@gmail.com> wrote:
Hi list,
I would like to know how the thread_info address is calculated? As per the LKD book: struct thread_info is stored on the kernel stack. On x86, current is calculated by masking out the 13 least-significant bits of the stack pointer to obtain the thread_info structure.This is done by the current_thread_info() function.The assembly is shown here: movl $-8192, %eax andl %esp, %eax This assumes that the stack size is 8KB.
But in code i found that: /* how to get the thread information struct from C */ static inline struct thread_info *current_thread_info(void) { return (struct thread_info *) (current_stack_pointer & ~(THREAD_SIZE - 1)); }
/* how to get the current stack pointer from C */ static inline unsigned long current_stack_pointer(void) { unsigned long sp; asm("mov sp,%0; ":"=r" (sp)); return sp; }
Could anyone help me in explaining the current_stack_pointer and (current_stack_pointer & ~(THREAD_SIZE - 1)) calculation? This is not as mentioned in the book.
If page size is 4KB and 2 page per process kernel stack is used then THREAD_Size will be 8KB.
Thanks, Vijay
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-- Regards, Denis
It's the same thing as you read: THREAD size is 8kb so the operation looks like the following: current_stack_pointer & ~(8191) == current_stack_pointet & 0xFFFFFE00 (last 13 bits are 0)
Ok. Got it. But how ANDing it with current stack pointer points to the address of thread_info structure. Suppose stack is growing from higher to lower memory address. Thanks, Vijay
Hi Vijay, On Fri, Sep 16, 2011 at 5:16 AM, Vijay Chauhan <kernel.vijay@gmail.com> wrote:
It's the same thing as you read: THREAD size is 8kb so the operation looks like the following: current_stack_pointer & ~(8191) == current_stack_pointet & 0xFFFFFE00 (last 13 bits are 0)
Ok. Got it.
But how ANDing it with current stack pointer points to the address of thread_info structure. Suppose stack is growing from higher to lower memory address.
Since the memory for the stack is allocated using a power-of-2 allocator, when you ask for 8K of memory, it is also 8K aligned. The stack will start at the end of the 8K region and grow towards the beginning. The thread_info will be stored at the beginning. ANDing any valid stack pointer with the mask will return the beginning of the 8K block. -- Dave Hylands Shuswap, BC, Canada http://www.davehylands.com
participants (3)
-
Dave Hylands -
Denis Kirjanov -
Vijay Chauhan