打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Linux内核2.6和2.4中内核堆栈的差异
在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表示当前进程。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux内核的thread
Linux进程管理之基础知识(每个进程都是由一个taskstruct结构来进行描述的(进程控制块)就是指taskstruct)
linux驱动current关键词
对Linux的进程内核栈的认识
关于kernel_thread的补充说明
进程(二):进程创建
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服