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的最高位置。