Linux内核2.6和2.4中内核堆栈的比较
2011-12-16 09:38
423 查看
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。
![](http://feizf.blogbus.com/files/12052472000.jpg)
内核为每个进程分配一个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定义如下:
![](http://feizf.blogbus.com/files/12052472001.jpg)
#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 */
... ..
}
/*************************************************************************************************************/
task_struct结构体里的void * stack ,在#include<linux/sched.h>里边有这个:
#define task_thread_info(task) ((struct thread_info *)(task)->stack)
将task_struct中的void* stack指向strcut thread_info。
在内核2.4中堆栈是这么定义的:
union task_union {
struct task_struct task;
unsigned long stack[INIT_TASK_SIZE/sizeof(long)];
};
而INIT_TASK_SIZE只能是8K。
![](http://feizf.blogbus.com/files/12052472000.jpg)
内核为每个进程分配一个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定义如下:
![](http://feizf.blogbus.com/files/12052472001.jpg)
#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 */
... ..
}
/*************************************************************************************************************/
task_struct结构体里的void * stack ,在#include<linux/sched.h>里边有这个:
#define task_thread_info(task) ((struct thread_info *)(task)->stack)
将task_struct中的void* stack指向strcut thread_info。
相关文章推荐
- Linux内核2.6和2.4中内核堆栈的比较
- Linux内核2.6和2.4中内核堆栈的比较
- Linux内核2.6和2.4内核堆栈的比较----转载
- Linux内核2.6和2.4中内核堆栈的比较
- Linux内核2.6和2.4中内核堆栈的比较
- Linux操作系统上2.4和2.6内核Web服务比较
- 从 2.4 到 2.6:Linux 内核可装载模块机制的改变对设备驱动的影响(二)
- linux2.4到linux2.6内核调度(7)
- 为2.4内核的linux操作系统增加2.6内核
- Linux2.6内核驱动与2.4的区别
- linux 内核2.6和linux内核2.4 驱动程序makefile的编写的区别!!!!
- linux2.4到linux2.6内核调度(9)
- 从 2.4 到 2.6:Linux 内核可装载模块机制的改变对设备驱动的影响
- 从 2.4 到 2.6:Linux 内核可装载模块机制的改变对设备驱动的影响 -zt
- linux 2.4 和2.6 内核区别
- 从 2.4 到 2.6:Linux 内核可装载模块机制的改变对设备驱动的影响
- 【转载】从 2.4 到 2.6:Linux 内核可装载模块机制的改变对设备驱动的影响
- 从 2.4 到 2.6:Linux 内核可装载模块机制的改变对设备驱动的影响
- 从2.4 到2.6:Linux 内核可装载模块机制的改变对设备驱动的影响
- linux2.4到linux2.6内核调度(11)