c - 在ARM Linux中,每个线程在内核堆栈的 "bottom"处保留的几个字节的目的是什么

标签 c linux-kernel arm

问题:

为什么在创建时在内核栈的“底部”保留​​了 8 个字节?

背景:

我们知道 struct pt_regsthread_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/

相关文章:

c - 声明 "char (* ( *f())[])();"是什么意思?

linux - Linux 是如何记住它的内核堆栈指针的?

linux - 在 Firefly RK-3288 板上设置显示分辨率的问题

linux - 与 I/O 地址端口相比,设备 mmap 如何工作?

android - 我应该使用哪些基于 ARM 的开发板?

c - GDB 位位置位值

c - 我正在尝试编写一个 C 代码来对 String 进行排序,但第 13 行总是显示错误消息

c - 如何从完整路径中提取文件名

java - Android SDK跨平台

c++ - 调试 Win32 API 应用程序是否存在内存泄漏