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的最高位置。
那么这个线程信息是如何保证以8K对齐的呢?答案在.data.init_task这个段,在LDF文件中,有这样的语句:
.data
{
/* make sure the init_task is aligned to the
* kernel thread size so we can locate the kernel
* stack properly and quickly.
*/
__sdata = .;
INPUT_SECTION_ALIGN(8192)
INPUT_SECTIONS($LIBRARIES_CORE_A(.data.init_task))
…
} > MEM_SDRAM
从而保证了init_thread_union是以8192对齐的。
对于一般的线程信息,uclinux内核是如何做到这一点的呢?在thread_info.h中有一个宏定义:
/* thread information allocation */
#define alloc_thread_info(tsk) ((struct thread_info *) /
__get_free_pages(GFP_KERNEL, 1))
#define free_thread_info(ti) free_pages((unsigned long) (ti), 1)
猜测是通过__get_free_pages这个函数来达到目的的,不过目前还没有涉及到内存分配与管理这一块,暂且放一放。