简体   繁体   English

Linux内核:当前的宏工作

[英]Linux kernel : Current macro working

Regarding the working of current macro in Linux kernel(I am referring to ARM architecture) 关于Linux内核中当前宏的工作(我指的是ARM体系结构)

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 ? 这意味着struct thread_info位于堆栈的顶部?

This is from linux Kernel development 3rd edition : 这是来自linux Kernel开发的第3版:

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). struct thread_info位于堆栈的底部(对于向下增长的堆栈)和堆栈顶部(对于成长的堆栈)。

How is this struct thread_info prevented by getting overwritten ? 如何通过覆盖来阻止这个struct thread_info

THREAD_SIZE is a constant with a power of 2, which gives the amount of memory allocated for the thread's stack. THREAD_SIZE是一个幂为2的常量,它给出了为线程堆栈分配的内存量。

  • The expression ~(THREAD_SIZE - 1) then gives a bitmask for getting rid of the actual stack address. 表达式~(THREAD_SIZE - 1)然后给出一个位掩码来删除实际的堆栈地址。 Eg. 例如。 For 8 kB stack, it would be 0xffffff00. 对于8 kB堆栈,它将是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. 如果堆栈变得太大(堆栈溢出),它首先超出的是`struct thread_info,这很快会导致各种令人讨厌的失败。

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. 指向线程struct thread_info指针放在内存的底部,该内存是为线程的内核堆栈保留的。 (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). 没有特殊的保护机制来防止覆盖此指针,除了内核代码不占用太多空间(并且中断被切换到它们自己的堆栈)这一事实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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