问题:
为什么在创建时在内核栈的“底部”保留了 8 个字节?
背景:
我们知道 struct pt_regs
和 thread_info
共享相同的 2 个连续页面(8192 字节),其中 pt_reg
位于较高端, thread_info
在下端。
但是,我注意到这 2 个页面的最高地址保留了 8 个字节:
在 arch/arm/include/asm/threadinfo.h 中
#define THREAD_START_SP (THREAD_SIZE - 8)
最佳答案
这样您就可以通过读取堆栈指针并屏蔽掉THREAD_SIZE
位来访问thread_info
结构(否则SP
最初会在下一个 THREAD_SIZE
block )。
static inline struct thread_info *current_thread_info(void)
{
register unsigned long sp asm ("sp");
return (struct thread_info *)(sp & ~(THREAD_SIZE - 1));
}
8 个字节来自 ARM 调用约定,SP
需要 8 字节对齐。
更新: AAPCS 5.2.1.1 指出:
A process may only access (for reading or writing) the closed interval of the entire stack delimited by [SP, stack-base – 1] (where SP is the value of register r13).
因为栈是全降序的
THREAD_START_SP (THREAD_SIZE - 8)
可能会通过非法访问下一页(段错误)来强制执行此要求。
关于c - 在ARM Linux中,每个线程在内核堆栈的 "bottom"处保留的几个字节的目的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25237607/