LittleKernel学习笔记(1)

过去的一些文档都比较旧,新的LK已经发生了一些变化,故新写一篇学习笔记。

LK的C语音入口函数是bootable/bootloader/lk/top下面的main.c中的lk_main()

void lk_main(ulong arg0, ulong arg1, ulong arg2, ulong arg3) {
    // save the boot args,传传参数而已,混吃等死
    lk_boot_args[0] = arg0;
    lk_boot_args[1] = arg1;
    lk_boot_args[2] = arg2;
    lk_boot_args[3] = arg3;

    // get us into some sort of thread context
    thread_init_early();//初始化线程相关

    // early arch stuff,鬼晓得干啥子,看名字像是CPU相关
    lk_primary_cpu_init_level(LK_INIT_LEVEL_EARLIEST, LK_INIT_LEVEL_ARCH_EARLY - 1);
    arch_early_init();

    // do any super early platform initialization,平台相关哦
    lk_primary_cpu_init_level(LK_INIT_LEVEL_ARCH_EARLY, LK_INIT_LEVEL_PLATFORM_EARLY - 1);
    platform_early_init();

    // do any super early target initialization,目标初始化哦
    lk_primary_cpu_init_level(LK_INIT_LEVEL_PLATFORM_EARLY, LK_INIT_LEVEL_TARGET_EARLY - 1);
    target_early_init();

#if WITH_SMP
    dprintf(INFO, "\nwelcome to lk/MP\n\n");
#else
    dprintf(INFO, "\nwelcome to lk\n\n");
#endif
    dprintf(INFO, "boot args 0x%lx 0x%lx 0x%lx 0x%lx\n",
            lk_boot_args[0], lk_boot_args[1], lk_boot_args[2], lk_boot_args[3]);

    // bring up the kernel heap,堆
    lk_primary_cpu_init_level(LK_INIT_LEVEL_TARGET_EARLY, LK_INIT_LEVEL_HEAP - 1);
    dprintf(SPEW, "initializing heap\n");
    heap_init();

    // deal with any static constructors,来来来,静态构造
    dprintf(SPEW, "calling constructors\n");
    call_constructors();

    // initialize the kernel,kernel初始化
    lk_primary_cpu_init_level(LK_INIT_LEVEL_HEAP, LK_INIT_LEVEL_KERNEL - 1);
    kernel_init();

    lk_primary_cpu_init_level(LK_INIT_LEVEL_KERNEL, LK_INIT_LEVEL_THREADING - 1);

    // create a thread to complete system initialization,创建线程完成系统初始化
    dprintf(SPEW, "creating bootstrap completion thread\n");
    thread_t *t = thread_create("bootstrap2", &bootstrap2, NULL, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
    thread_set_pinned_cpu(t, 0);
    thread_detach(t);
    thread_resume(t);

    // become the idle thread and enable interrupts to start the scheduler,成为优秀多金有前途的大好青年呗炫耀
    thread_become_idle();
}

 我靠,还得一个一个来看函数,不说废话,直接来源码,吐血也看不懂的就别看了,看了会发困。

 

 1 /**
 2  * @brief  Initialize threading system
 3  *
 4  * This function is called once, from kmain()打了预防针呀,说这玩意只会来一发
 5  */
 6 void thread_init_early(void) {
 7     int i;//很粗鲁呀,很尴尬的程序i变量
 8 
 9     DEBUG_ASSERT(arch_curr_cpu_num() == 0);//哦,还支持ASSERT功能调试
10 
11     /* initialize the run queues *///一个循环,初始化了一个队列而已
12     for (i=0; i < NUM_PRIORITIES; i++)
13         list_initialize(&run_queue[i]);
14 
15     /* initialize the thread list */
16     list_initialize(&thread_list);//初始化了thread_list,这是啥?不知道又不掉肉
17 
18     /* create a thread to cover the current running state */
19     thread_t *t = idle_thread(0);//买了一个新线程玩玩
20     init_thread_struct(t, "bootstrap");//初始化这个线程,取了个名字叫bootstrap,名字很土但是好记呀,Boot
21 
22     /* half construct this thread, since we're already running *//说先构造这个线程一般,不只是是不是不珍惜,曾经有一份外卖放在楼下,我没有拿
23     t->priority = HIGHEST_PRIORITY;//咦,开这么高的权限,难道是高干子弟?
24     t->state = THREAD_RUNNING;
25     t->flags = THREAD_FLAG_DETACHED;
26     thread_set_curr_cpu(t, 0);
27     thread_set_pinned_cpu(t, 0);
28     wait_queue_init(&t->retcode_wait_queue);
29     list_add_head(&thread_list, &t->thread_list_node);
30     set_current_thread(t);
31 }

那就看一下这个初始化函数

static inline void list_initialize(struct list_node *list) {
list->prev = list->next = list;
}

也很简单呀,好了,thread_init_early分析就到这里了



 

posted @ 2021-08-27 11:27  zhougong12  阅读(627)  评论(0)    收藏  举报