1
votes

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 ?

3
I think: (sp & ~(THREAD_SIZE - 1)); check whether odd number of thread id. Code checks last bit is one. - Grijesh Chauhan
@GrijeshChauhan : I am not sure whether it does that I say that because further this code is used like this : current_thread_info()->task - Leo Messi
I myself couldn't understand this but I understand that (sp & ~(THREAD_SIZE - 1) check last bit is one or not. - Grijesh Chauhan
Suppose THREAD_SIZE is a power of two, say 0x100. Then (THREAD_SIZE-1) will be 0xff. ~(THREAD_SIZE-1)` will be the same mask inverted : 0xfffffff00 So, 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) - wildplasser
The only thing that makes this ARM specific is the register used to get the stack pointer. Ie, on the ARM, it is sp whereas the PowerPC, it is R1, etc. The same concept is used on most (all?) architectures. - artless noise

3 Answers

2
votes

THREAD_SIZE is a constant with a power of 2, which gives the amount of memory allocated for the thread's stack.

  • The expression ~(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.

1
votes

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.

0
votes

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