QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1737|回复: 2

Linux内核2.6和2.4中内核堆栈的差异

[复制链接]
发表于 2005-4-25 11:49:23 | 显示全部楼层 |阅读模式
这一阵子在看《Linux kernel development 2nd Edition》发现了在内核
栈的处理上,2.4和2.6的一点小小的差异,顺手记了下来。有问题请指正阿,
我还菜。

在2.6的内核上允许内核栈的大小在4K和8K之间选择,于是,根据内核
的配置,在thread_info.h中,加入了下面的一段代码
#ifdef CONFIG_4KSTACKS
#define THREAD_SIZE            (4096)
#else
#define THREAD_SIZE            (8192)
这个大小的不同会影响内核堆栈的表示方法和current宏的实现,详见下面

首先,看看内核栈在表达方法上的差异
在2.6中
union thread_union {
         struct thread_info thread_info;
         unsigned long stack[THREAD_SIZE/sizeof(long)];
};
这里THREAD_SIZE既可以是4K也可以是8K
在2.4中
union task_union {
         struct task_struct task;
         unsigned long stack[INIT_TASK_SIZE/sizeof(long)];
};
而INIT_TASK_SIZE只能是8K

其次,看看current宏实现的不同
众所周知,我们可以使用current宏来引用当前进程,但是实现方法,在2.4和2.6
中,则截然不同。
在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;
}
屏蔽掉%esp的13-bit LSB后,就得到了内核栈的开头,而这个开头正好是task_struct的开
始,从而得到了指向task_struct的指针。
在2.6中,包装多了一层
#define current get_current()

static inline struct task_struct * get_current(void)
{
         return current_thread_info()->task;
}
继续深入
/* how to get the thread information struct from C */
static inline struct thread_info *current_thread_info(void)
{
        struct thread_info *ti;
        __asm__("andl %%esp,%0; ":"=r" (ti) : "" (~(THREAD_SIZE - 1)));
        return ti;
}
根据CONFIG_4KSTACKS的设置,分别屏蔽掉内核栈的12-bit LSB(4K)或13-bit LSB(8K),
从而获得内核栈的起始位置。

看了thread_info的结构,一切豁然开朗
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 */
         ... ..
}
看到了吧,其实thread_info结构的第一个成员就是一个指向task_struct结构的指针,所以
要用current_thread_info()->task表示task_struct的地址,但是整个过程对用户是完全透
明的,我们还是可以用current表示当前进程。
发表于 2005-4-26 15:58:48 | 显示全部楼层
呵呵,不错!长经验值了:)谢谢楼主!
BTW,在Linux 2.6中thread_info结构主要是做什么用的?看上去好像很多task_struct中的东东都移到它里面去了么?
回复

使用道具 举报

 楼主| 发表于 2005-4-26 20:35:18 | 显示全部楼层
关于2.6的进程管理,我还在学习中,估计5 1后就能回答你了,见谅,哈~~    
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-6-29 08:26 , Processed in 0.047692 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表