Reputation: 822
Regarding the working of current macro in Linux kernel(I am referring to ARM architecture)
The code for current macro :
return (struct thread_info *)(sp & ~(THREAD_SIZE - 1));
This means that the struct thread_info
is placed at the top of stack ?
This is from linux Kernel development 3rd edition :
struct thread_info
lives at the bottom of the stack (for stacks that grow down) and at the top of the stack (for stacks that grow up).
How is this struct thread_info
prevented by getting overwritten ?
Upvotes: 1
Views: 1659
Reputation: 16441
It is not protected from overrun.
If the stack grows too large (stack overflow), the first thing it overruns is the `struct thread_info, which soon leads to various nasty failures.
So when writing kernel code, use a s little stack space as possible, to avoid overruns.
Upvotes: 1
Reputation: 180080
A pointer to the thread's struct thread_info
is placed at the bottom of the memory that is reserved for the thread's kernel stack.
(Each thread needs its own stack, so the stack pointer's value is guaranteed to be unique for each thread.)
There is no special protection mechanism to prevent overwriting this pointer, except the fact that kernel code does not use much space space (and that interrupts get switched to their own stack).
Upvotes: 0
Reputation: 7486
THREAD_SIZE
is a constant with a power of 2, which gives the amount of memory allocated for the thread's stack.
~(THREAD_SIZE - 1)
then gives a bitmask for getting rid of the actual stack address. Eg. For 8 kB stack, it would be 0xffffff00.By taking a bitwise and with the stack pointer value, we get the lowest address allocated for the stack.
The stack pointer is useful for getting the thread information because each thread always has its own stack.
Upvotes: 2