内核如何根据堆栈指针获current_thread_info

current_thread_info的定义在include/asm/thread_info.h中:
/* Given a task stack pointer, you can find it's task structure
 * just by masking it to the 8K boundary.
 */
static inline struct thread_info *current_thread_info(void)
{
     struct thread_info *ti;
      __asm__("%0 = sp;": "=&d"(ti):
     );
     return (struct thread_info *)((long)ti & ~((long)THREAD_SIZE-1));
}
它的作用在于取得运行当前代码的线程的信息,此信息保存在thread_info结构体中。
在上述函数中,THREAD_SIZE定义为:
 
/*
 * Size of kernel stack for each process. This must be a power of 2...
 */
#define THREAD_SIZE         8192 /* 2 pages */
也就是说,上述函数将取SP指针,并将此指针按照8K对齐,即2页对齐,这就是线程信息的指针了。
记得在处理head.s的时候,有一段代码:
       /*
        * load the current thread pointer and stack
        */
       r1.l = _init_thread_union;
       r1.h = _init_thread_union;
 
       r2.l = 0x2000;    // 8192字节
       r2.h = 0x0000;
       r1 = r1 + r2;
       sp = r1;
       usp = sp;
       fp = sp;
其中init_thread_union的定义为:
/*
 * Initial thread structure.
 *
 * We need to make sure that this is 8192-byte aligned due to the
 * way process stacks are handled. This is done by having a special
 * "init_task" linker map entry.
 */
union thread_union init_thread_union
    __attribute__ ((__section__(".data.init_task"))) = {
INIT_THREAD_INFO(init_task)};
thread_union的定义在include/linux/shed.h中:
 
union thread_union {
     struct thread_info thread_info;
     unsigned long stack[THREAD_SIZE/sizeof(long)];
};
这个union的结构体大小为THREAD_SIZE,也就是8K,在初始化的时候,将FP和SP都指向了初始线程stack的最高位置。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值