Linux内核 2.4
和
2.6
的进程内核堆栈和
task
描述符存储不太一样,这儿总结一下。
在内核2.4 中堆栈是这么定义的:
union task_union {
struct task_struct task;
unsigned long stack[INIT_TASK_SIZE/sizeof(long)];
};
而 INIT_TASK_SIZE 只能是 8K 。

内核为每个进程分配一个 task_struct 结构时,实际上分配两个连续的物理页面 (8192 字 节 ) ,如图所示。 底部用作 task_struct 结构 ( 大小约为 1K 字节 ) ,结构的上面用作内核堆栈 ( 大小约为 7K 字节 ) 。 访问进程自身的 task_struct 结构,使用宏操作 current, 在 2.4 中定义如下:
#define current get_current()
static inline struct task_struct * get_current(void)
{
struct task_struct *current;
__asm__("andl %%esp,%0; ":"=r" (current) : "" (~8191UL));
return current;
}
~8191UL 表示最低 13 位为 0, 其余位全为 1 。 %esp 指向内核堆栈中,当屏蔽掉 %esp 的最低 13 后,就得到这个 ” 两个连续的物理页面 ” 的开头,而这个开头正好是 task_struct 的开始,从而得到了指向 task_struct 的指针。
在内核2.6中堆栈这么定义:
union thread_union {
struct thread_info thread_info;
unsigned long stack[THREAD_SIZE/sizeof(long)];
};
根据内核的配置, THREAD_SIZE 既可以是 4K 字节 (1 个页面 ) 也可以是 8K 字节 (2 个页面 ) 。 thread_info 是 52 个字节长。
下图是当设为 8KB 时候的内核堆栈: Thread_info 在这个内存区的开始处,内核堆栈从末端向下增长。进程描述符不是在这个内存区中,而分别通过 task 与 thread_info 指针使 thread_info 与进程描述符互联。所以获得当前进程描述符的 current 定义如下 :

#define current get_current()
static inline struct task_struct * get_current(void)
{
return current_thread_info()->task;
}
static inline struct thread_info *current_thread_info(void)
{
struct thread_info *ti;
__asm__("andl %%esp,%0; ":"=r" (ti) : "" (~(THREAD_SIZE - 1)));
return ti;
}
根据 THREAD_SIZE 大小,分别屏蔽掉内核栈的 12-bit LSB(4K) 或 13-bit LSB(8K) ,从而获得内核栈的起始位置。
struct thread_info {
struct task_struct *task; /* main task structure */
struct exec_domain *exec_domain; /* execution domain */
unsigned long flags; /* low level flags */
unsigned long status; /* thread-synchronous flags */
... ..
}
参考:
1. http://hi.baidu.com/zqfazqq/blog/item/12db349980343b0b6f068c5d.html
2. Linux 内核源代码情景分析 ( 上册 , Page267)
3. 深入理解 Linux 内核 ( 第 3 版 , Page90, Page164)
在内核2.4 中堆栈是这么定义的:
union task_union {
struct task_struct task;
unsigned long stack[INIT_TASK_SIZE/sizeof(long)];
};
而 INIT_TASK_SIZE 只能是 8K 。

内核为每个进程分配一个 task_struct 结构时,实际上分配两个连续的物理页面 (8192 字 节 ) ,如图所示。 底部用作 task_struct 结构 ( 大小约为 1K 字节 ) ,结构的上面用作内核堆栈 ( 大小约为 7K 字节 ) 。 访问进程自身的 task_struct 结构,使用宏操作 current, 在 2.4 中定义如下:
#define current get_current()
static inline struct task_struct * get_current(void)
{
struct task_struct *current;
__asm__("andl %%esp,%0; ":"=r" (current) : "" (~8191UL));
return current;
}
~8191UL 表示最低 13 位为 0, 其余位全为 1 。 %esp 指向内核堆栈中,当屏蔽掉 %esp 的最低 13 后,就得到这个 ” 两个连续的物理页面 ” 的开头,而这个开头正好是 task_struct 的开始,从而得到了指向 task_struct 的指针。
在内核2.6中堆栈这么定义:
union thread_union {
struct thread_info thread_info;
unsigned long stack[THREAD_SIZE/sizeof(long)];
};
根据内核的配置, THREAD_SIZE 既可以是 4K 字节 (1 个页面 ) 也可以是 8K 字节 (2 个页面 ) 。 thread_info 是 52 个字节长。
下图是当设为 8KB 时候的内核堆栈: Thread_info 在这个内存区的开始处,内核堆栈从末端向下增长。进程描述符不是在这个内存区中,而分别通过 task 与 thread_info 指针使 thread_info 与进程描述符互联。所以获得当前进程描述符的 current 定义如下 :

#define current get_current()
static inline struct task_struct * get_current(void)
{
return current_thread_info()->task;
}
static inline struct thread_info *current_thread_info(void)
{
struct thread_info *ti;
__asm__("andl %%esp,%0; ":"=r" (ti) : "" (~(THREAD_SIZE - 1)));
return ti;
}
根据 THREAD_SIZE 大小,分别屏蔽掉内核栈的 12-bit LSB(4K) 或 13-bit LSB(8K) ,从而获得内核栈的起始位置。
struct thread_info {
struct task_struct *task; /* main task structure */
struct exec_domain *exec_domain; /* execution domain */
unsigned long flags; /* low level flags */
unsigned long status; /* thread-synchronous flags */
... ..
}
参考:
1. http://hi.baidu.com/zqfazqq/blog/item/12db349980343b0b6f068c5d.html
2. Linux 内核源代码情景分析 ( 上册 , Page267)
3. 深入理解 Linux 内核 ( 第 3 版 , Page90, Page164)