Linux內核中的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的最高位置。
那麼這個線程信息是如何保證以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這個函數來達到目的的,不過目前還沒有涉及到內存分配與管理這一塊,暫且放一放。

轉至:http://blog.csdn.net/lights_joy/article/details/2436899
發佈了28 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章