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 ?
(sp & ~(THREAD_SIZE - 1));check whether odd number of thread id. Code checks last bit is one. - Grijesh Chauhan(sp & ~(THREAD_SIZE - 1)check last bit is one or not. - Grijesh ChauhanTHREAD_SIZEis a power of two, say0x100. Then(THREAD_SIZE-1)will be0xff. ~(THREAD_SIZE-1)` will be the same mask inverted :0xfffffff00So, the macro mask off the lowest bits. The current struct is probably located at the lower size of the stack (assuming sp is the stack pointer, or a pointer into an array of thread structures) - wildplasserspwhereas the PowerPC, it isR1, etc. The same concept is used on most (all?) architectures. - artless noise