之前看過lk但是都是零散,也記不住,這次好好記錄一下,就當自己的隨筆看看
lk幹了哪些事情,我大概寫一下:
1.根據板子的硬體資訊初始化board這個結構體
2.初始化各種時鐘
3.初始化中斷(支援多少中斷)
4.初始化uart(設定時鐘,配置相應的gpio,設定各種狀態)
5.初始化heap
6.初始化線程
7.建立idle 線程
8.panel的初始化還有點亮
9.shell環境的初始化準備
lk的第一個函數
void kmain(void){// get us into some sort of thread contextthread_init_early();// early arch stuffarch_early_init();// do any super early platform initializationplatform_early_init();// do any super early target initializationtarget_early_init();dprintf(INFO, "welcome to lk\n\n");bs_set_timestamp(BS_BL_START);// deal with any static constructorsdprintf(SPEW, "calling constructors\n");call_constructors();// bring up the kernel heapdprintf(SPEW, "initializing heap\n");heap_init();__stack_chk_guard_setup();// initialize the threading systemdprintf(SPEW, "initializing threads\n");thread_init();// initialize the dpc systemdprintf(SPEW, "initializing dpc\n");dpc_init();// initialize kernel timersdprintf(SPEW, "initializing timers\n");timer_init();#if (!ENABLE_NANDWRITE)// create a thread to complete system initializationdprintf(SPEW, "creating bootstrap completion thread\n");thread_resume(thread_create("bootstrap2", &bootstrap2, NULL, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE));// enable interruptsexit_critical_section();// become the idle threadthread_become_idle();#else bootstrap_nandwrite();#endif}
接下來一個一個看
先看第一個
void thread_init_early(void){int i;/* initialize the run queues */for (i=0; i < NUM_PRIORITIES; i++)list_initialize(&run_queue[i]); //static struct list_node run_queue[NUM_PRIORITIES];/*static inline void list_initialize(struct list_node *list){list->prev = list->next = list;}*//* initialize the thread list */list_initialize(&thread_list); //static struct list_node thread_list;/* create a thread to cover the current running state */thread_t *t = &bootstrap_thread; //static thread_t bootstrap_thread;init_thread_struct(t, "bootstrap");/* half construct this thread, since we're already running */t->priority = HIGHEST_PRIORITY;t->state = THREAD_RUNNING;t->saved_critical_section_count = 1;list_add_head(&thread_list, &t->thread_list_node);current_thread = t;}再看看
static void init_thread_struct(thread_t *t, const char *name){memset(t, 0, sizeof(thread_t));t->magic = THREAD_MAGIC;strlcpy(t->name, name, sizeof(t->name)); //把<span style="font-family: Arial, Helvetica, sans-serif;">bootstrap賦值給thread name</span>}