内核源码--------boot_cpu_init();

继续回到start_kernel,来到函数boot_cpu_init();定位到该函数

static void __init boot_cpu_init(void)
{
int cpu = smp_processor_id();
/* Mark the boot cpu "present", "online" etc for SMP and UP case */
set_cpu_online(cpu, true);
set_cpu_active(cpu, true);
set_cpu_present(cpu, true);
set_cpu_possible(cpu, true);
}

首先来看函数smp_processor_id(),定位到该函数

#ifdef CONFIG_DEBUG_PREEMPT
  extern unsigned int debug_smp_processor_id(void);
# define smp_processor_id() debug_smp_processor_id()
#else
# define smp_processor_id() raw_smp_processor_id()
#endif

由于没有定义宏CONFIG_DEBUG_PREEMPT,所以最终调用的就是 raw_smp_processor_id(),定位到该函数

#define raw_smp_processor_id() (current_thread_info()->cpu)

可以看得出该函数的值是current_thread_info()结构体的cpu的值,定位到current_thread_info()

static inline struct thread_info *current_thread_info(void)
{
struct thread_info *ti;


__asm__ __volatile__ ("and.d $sp, %0" : "=r" (ti) : "0" (~8191UL));
return ti;
}

该函数中,汇编部分不是太明白,细节的后面在研究,ti是hread_info类型的结构体,该结构体如下

struct thread_info {
struct task_struct *task; /* main task structure */
struct exec_domain *exec_domain;/* execution domain */
unsigned long flags; /* thread_info flags (see TIF_*) */
mm_segment_t addr_limit; /* user-level address space limit */
__u32 cpu; /* current CPU */
int preempt_count; /* 0=premptable, <0=BUG; will also serve as bh-counter */
struct restart_block restart_block;
};

这样函数smp_processor_id()就得到了当前CPU的ID,下面的几个函数是设置cpu相关的变量的值,不再细细研究了。

你可能感兴趣的:(内核源码阅读笔记)