简体   繁体   中英

Linux kernel : Current macro working

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 ?

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.

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 as little stack space as possible, to avoid overruns.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM