這是一個(gè)連載的博文系列伴澄,我將持續(xù)為大家提供盡可能透徹的Android源碼分析 github連載地址
前言
Android本質(zhì)上就是一個(gè)基于Linux內(nèi)核的操作系統(tǒng)盈滴,與Ubuntu Linux、Fedora Linux類似湘今,我們要講Android拦赠,必定先要了解一些Linux內(nèi)核的知識(shí)巍沙。
Linux內(nèi)核的東西特別多,我也不可能全部講完荷鼠,由于本文主要講解Android系統(tǒng)啟動(dòng)流程,所以這里主要講一些內(nèi)核啟動(dòng)相關(guān)的知識(shí)榔幸。
Linux內(nèi)核啟動(dòng)主要涉及3個(gè)特殊的進(jìn)程允乐,idle進(jìn)程(PID = 0), init進(jìn)程(PID = 1)和kthreadd進(jìn)程(PID = 2),這三個(gè)進(jìn)程是內(nèi)核的基礎(chǔ)削咆。
- idle進(jìn)程是Linux系統(tǒng)第一個(gè)進(jìn)程牍疏,是init進(jìn)程和kthreadd進(jìn)程的父進(jìn)程
- init進(jìn)程是Linux系統(tǒng)第一個(gè)用戶進(jìn)程,是Android系統(tǒng)應(yīng)用程序的始祖拨齐,我們的app都是直接或間接以它為父進(jìn)程
- kthreadd進(jìn)程是Linux系統(tǒng)內(nèi)核管家鳞陨,所有的內(nèi)核線程都是直接或間接以它為父進(jìn)程
本文將以這三個(gè)進(jìn)程為線索,主要講解以下內(nèi)容:
- idle進(jìn)程啟動(dòng)
- kthreadd進(jìn)程啟動(dòng)
- init進(jìn)程啟動(dòng)
本文涉及到的文件
msm/arch/arm64/kernel/head.S
msm/init/main.c
msm/kernel/rcutree.c
msm/kernel/fork.c
msm/mm/mempolicy.c
msm/kernel/kthread.c
msm/include/linux/kthread.h
msm/include/linux/rcupdate.h
msm/kernel/rcupdate.c
msm/kernel/pid.c
msm/include/linux/sched.h
msm/kernel/sched/core.c
msm/kernel/cpu/idle.c
msm/drivers/base/init.c
一瞻惋、idle進(jìn)程啟動(dòng)
很多文章講Android都從init進(jìn)程講起厦滤,它的進(jìn)程號(hào)是1,既然進(jìn)程號(hào)是1歼狼,那么有沒有進(jìn)程號(hào)是0的進(jìn)程呢掏导,其實(shí)是有的。
這個(gè)進(jìn)程名字叫init_task羽峰,后期會(huì)退化為idle趟咆,它是Linux系統(tǒng)的第一個(gè)進(jìn)程(init進(jìn)程是第一個(gè)用戶進(jìn)程)添瓷,也是唯一一個(gè)沒有通過(guò)fork或者kernel_thread產(chǎn)生的進(jìn)程,它在完成初始化操作后值纱,主要負(fù)責(zé)進(jìn)程調(diào)度鳞贷、交換。
idle進(jìn)程的啟動(dòng)是用匯編語(yǔ)言寫的虐唠,對(duì)應(yīng)文件是msm/arch/arm64/kernel/head.S搀愧,因?yàn)槎际怯脜R編語(yǔ)言寫的,我就不多介紹了凿滤,具體可參考 kernel 啟動(dòng)流程之head.S ,這里面有一句比較重要
340 str x22, [x4] // Save processor ID
341 str x21, [x5] // Save FDT pointer
342 str x24, [x6] // Save PHYS_OFFSET
343 mov x29, #0
344 b start_kernel //跳轉(zhuǎn)start_kernel函數(shù)
第344行妈橄,b start_kernel,b 就是跳轉(zhuǎn)的意思翁脆,跳轉(zhuǎn)到start_kernel.h眷蚓,這個(gè)頭文件對(duì)應(yīng)的實(shí)現(xiàn)在msm/init/main.c,start_kernel函數(shù)在最后會(huì)調(diào)用rest_init函數(shù)反番,這個(gè)函數(shù)開啟了init進(jìn)程和kthreadd進(jìn)程沙热,我們著重分析下rest_init函數(shù)。
在講源碼前罢缸,我先說(shuō)明下我分析源碼的寫作風(fēng)格:
- 一般我會(huì)在函數(shù)下面寫明該函數(shù)所在的位置篙贸,比如定義在msm/init/main.c中,這樣大家就可以去項(xiàng)目里找到源文件
- 我會(huì)把源碼相應(yīng)的英文注釋也一并copy進(jìn)來(lái)枫疆,這樣方便英文好的人可以看到原作者的注釋
- 我會(huì)盡可能將函數(shù)中每一行代碼的作用注釋下(一般以//的形式注釋在代碼結(jié)尾)爵川,大家在看源碼的同時(shí)就可以理解這段代碼作用,這也是我花時(shí)間最多的,請(qǐng)大家務(wù)必認(rèn)真看息楔。我也想過(guò)在源碼外部統(tǒng)一通過(guò)行號(hào)來(lái)解釋寝贡,但是感覺這樣需要大家一會(huì)兒看源碼,一會(huì)兒看解釋值依,上下來(lái)回看不方便圃泡,所以干脆寫在一起了
- 在函數(shù)結(jié)尾我盡可能總結(jié)下這個(gè)函數(shù)做了些什么,以及這個(gè)函數(shù)涉及到的一些知識(shí)
- 對(duì)于重要的函數(shù)愿险,我會(huì)將函數(shù)中每一個(gè)調(diào)用的子函數(shù)再單獨(dú)拿出來(lái)講解
- 考慮到大家都是開發(fā)Android的比較多颇蜡,對(duì)C/C++不太了解,在注釋中我也會(huì)講一些C/C++的知識(shí)辆亏,方便大家理解风秤,C語(yǔ)言注釋我一般用/** */的形式注釋在代碼頂頭
- 為了更好的閱讀體驗(yàn),希望大家可以下載一下Source Insight同步看代碼褒链,使用教程 ,可以直接將項(xiàng)目中app/src/main/cpp作為目錄加入到Source Insight中
1.1 rest_init
定義在msm/init/main.c中
/*
* C語(yǔ)言oninline與inline是一對(duì)意義相反的關(guān)鍵字唁情,inline的作用是編譯期間直接替換代碼塊,也就是說(shuō)編譯后就沒有這個(gè)方法了甫匹,而是直接把代碼塊替換調(diào)用這個(gè)函數(shù)的地方甸鸟,oninline就相反惦费,強(qiáng)制不替換,保持原有的函數(shù)
* __init_refok是__init的擴(kuò)展抢韭,__init 定義的初始化函數(shù)會(huì)放入名叫.init.text的輸入段薪贫,當(dāng)內(nèi)核啟動(dòng)完畢后,這個(gè)段中的內(nèi)存會(huì)被釋放掉刻恭,在本文中有講瞧省,關(guān)注3.5 free_initmem。
* 不帶參數(shù)的方法會(huì)加一個(gè)void參數(shù)
*/
static noinline void __init_refok rest_init(void)
{
int pid;
/*
* C語(yǔ)言中const相當(dāng)于Java中的final static鳍贾, 表示常量
* struct是結(jié)構(gòu)體鞍匾,相當(dāng)于Java中定義了一個(gè)實(shí)體類,里面只有一些成員變量骑科,{.sched_priority =1 }相當(dāng)于new橡淑,然后將成員變量sched_priority的值賦為1
*/
const struct sched_param param = { .sched_priority = 1 }; //初始化優(yōu)先級(jí)為1的進(jìn)程調(diào)度策略,取值1~99咆爽,1為最小
rcu_scheduler_starting(); //啟動(dòng)RCU機(jī)制梁棠,這個(gè)與后面的rcu_read_lock和rcu_read_unlock是配套的,用于多核同步
/*
* We need to spawn init first so that it obtains pid 1, however
* the init task will end up wanting to create kthreads, which, if
* we schedule it before we create kthreadd, will OOPS.
*/
/*
* C語(yǔ)言中支持方法傳參斗埂,kernel_thread是函數(shù)符糊,kernel_init也是函數(shù),但是kernel_init卻作為參數(shù)傳遞了過(guò)去呛凶,其實(shí)傳遞過(guò)去的是一個(gè)函數(shù)指針,參考[函數(shù)指針](http://www.cnblogs.com/haore147/p/3647262.html)
* CLONE_FS這種大寫的一般就是常量了男娄,跟Java差不多
*/
kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND); //用kernel_thread方式創(chuàng)建init進(jìn)程,CLONE_FS 子進(jìn)程與父進(jìn)程共享相同的文件系統(tǒng)漾稀,包括root沪伙、當(dāng)前目錄、umask县好,CLONE_SIGHAND 子進(jìn)程與父進(jìn)程共享相同的信號(hào)處理(signal handler)表
numa_default_policy(); // 設(shè)定NUMA系統(tǒng)的默認(rèn)內(nèi)存訪問策略
pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);//用kernel_thread方式創(chuàng)建kthreadd進(jìn)程,CLONE_FILES 子進(jìn)程與父進(jìn)程共享相同的文件描述符(file descriptor)表
rcu_read_lock(); //打開RCU讀取鎖暖混,在此期間無(wú)法進(jìn)行進(jìn)程切換
/*
* C語(yǔ)言中&的作用是獲得變量的內(nèi)存地址缕贡,參考[C指針](http://www.runoob.com/cprogramming/c-pointers.html)
*/
kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);// 獲取kthreadd的進(jìn)程描述符,期間需要檢索進(jìn)程pid的使用鏈表拣播,所以要加鎖
rcu_read_unlock(); //關(guān)閉RCU讀取鎖
sched_setscheduler_nocheck(kthreadd_task, SCHED_FIFO, ¶m); //設(shè)置kthreadd的進(jìn)程調(diào)度策略晾咪,SCHED_FIFO 實(shí)時(shí)調(diào)度策略,即馬上調(diào)用贮配,先到先服務(wù)谍倦,param的優(yōu)先級(jí)之前定義為1
complete(&kthreadd_done); // complete和wait_for_completion是配套的同步機(jī)制,跟java的notify和wait差不多泪勒,之前kernel_init函數(shù)調(diào)用了wait_for_completion(&kthreadd_done)昼蛀,這里調(diào)用complete就是通知kernel_init進(jìn)程kthreadd進(jìn)程已創(chuàng)建完成宴猾,可以繼續(xù)執(zhí)行
/*
* The boot idle thread must execute schedule()
* at least once to get things moving:
*/
init_idle_bootup_task(current);//current表示當(dāng)前進(jìn)程,當(dāng)前0號(hào)進(jìn)程init_task設(shè)置為idle進(jìn)程
schedule_preempt_disabled(); //0號(hào)進(jìn)程主動(dòng)請(qǐng)求調(diào)度叼旋,讓出cpu仇哆,1號(hào)進(jìn)程kernel_init將會(huì)運(yùn)行,并且禁止搶占
/* Call into cpu_idle with preempt disabled */
cpu_startup_entry(CPUHP_ONLINE);// 這個(gè)函數(shù)會(huì)調(diào)用cpu_idle_loop()使得idle進(jìn)程進(jìn)入自己的事件處理循環(huán)
}
rest_init的字面意思是剩余的初始化,但是它卻一點(diǎn)都不剩余夫植,它創(chuàng)建了Linux系統(tǒng)中兩個(gè)重要的進(jìn)程init和kthreadd讹剔,并且將init_task進(jìn)程變?yōu)閕dle進(jìn)程,接下來(lái)我將把rest_init中的方法逐個(gè)解析详民,方便大家理解延欠。
1.2 rcu_scheduler_starting
定義在msm/kernel/rcutree.c
/*
* This function is invoked towards the end of the scheduler's initialization
* process. Before this is called, the idle task might contain
* RCU read-side critical sections (during which time, this idle
* task is booting the system). After this function is called, the
* idle tasks are prohibited from containing RCU read-side critical
* sections. This function also enables RCU lockdep checking.
*/
void rcu_scheduler_starting(void)
{
WARN_ON(num_online_cpus() != 1); //WARN_ON相當(dāng)于警告,會(huì)打印出當(dāng)前棧信息沈跨,不會(huì)重啟由捎, num_online_cpus表示當(dāng)前啟動(dòng)的cpu數(shù)
WARN_ON(nr_context_switches() > 0); // nr_context_switches 進(jìn)行進(jìn)程切換的次數(shù)
rcu_scheduler_active = 1; //啟用rcu機(jī)制
}
1.3 kernel_thread
定義在msm/kernel/fork.c
/*
* Create a kernel thread.
*/
/*
* C語(yǔ)言中 int (*fn)(void *)表示函數(shù)指針的定義,int是返回值谒出,void是函數(shù)的參數(shù)隅俘,fn是名字
* C語(yǔ)言中 * 表示指針,這個(gè)用法很多
* unsigned表示無(wú)符號(hào)笤喳,一般與long,int,char等結(jié)合使用为居,表示范圍只有正數(shù),比如init表示范圍-2147483648~2147483647 杀狡,那unsigned表示范圍0~4294967295蒙畴,足足多了一倍
*/
pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
{
return do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn,
(unsigned long)arg, NULL, NULL);
}
do_fork函數(shù)用于創(chuàng)建進(jìn)程,它首先調(diào)用copy_process()創(chuàng)建新進(jìn)程呜象,然后調(diào)用wake_up_new_task()將進(jìn)程放入運(yùn)行隊(duì)列中并啟動(dòng)新進(jìn)程膳凝。
kernel_thread的第一個(gè)參數(shù)是一個(gè)函數(shù)引用,它相當(dāng)于Java中的構(gòu)造函數(shù)恭陡,會(huì)在創(chuàng)建進(jìn)程后執(zhí)行蹬音,第三個(gè)參數(shù)是創(chuàng)建進(jìn)程的方式,具體如下:
參數(shù)名 | 作用 |
---|---|
CLONE_PARENT | 創(chuàng)建的子進(jìn)程的父進(jìn)程是調(diào)用者的父進(jìn)程休玩,新進(jìn)程與創(chuàng)建它的進(jìn)程成了“兄弟”而不是“父子” |
CLONE_FS | 子進(jìn)程與父進(jìn)程共享相同的文件系統(tǒng)著淆,包括root、當(dāng)前目錄拴疤、umask |
CLONE_FILES | 子進(jìn)程與父進(jìn)程共享相同的文件描述符(file descriptor)表 |
CLONE_NEWNS | 在新的namespace啟動(dòng)子進(jìn)程永部,namespace描述了進(jìn)程的文件hierarchy |
CLONE_SIGHAND | 子進(jìn)程與父進(jìn)程共享相同的信號(hào)處理(signal handler)表 |
CLONE_PTRACE | 若父進(jìn)程被trace,子進(jìn)程也被trace |
CLONE_UNTRACED | 若父進(jìn)程被trace呐矾,子進(jìn)程不被trace |
CLONE_VFORK | 父進(jìn)程被掛起苔埋,直至子進(jìn)程釋放虛擬內(nèi)存資源 |
CLONE_VM | 子進(jìn)程與父進(jìn)程運(yùn)行于相同的內(nèi)存空間 |
CLONE_PID | 子進(jìn)程在創(chuàng)建時(shí)PID與父進(jìn)程一致 |
CLONE_THREAD | Linux 2.4中增加以支持POSIX線程標(biāo)準(zhǔn),子進(jìn)程與父進(jìn)程共享相同的線程群 |
1.4 kernel_init
定義在msm/init/main.c
這個(gè)函數(shù)比較重要蜒犯,負(fù)責(zé)init進(jìn)程的啟動(dòng)组橄,我將放在第三節(jié)重點(diǎn)講荞膘,這個(gè)函數(shù)首先調(diào)用kernel_init_freeable函數(shù)
static noinline void __init kernel_init_freeable(void)
{
/*
* Wait until kthreadd is all set-up.
*/
wait_for_completion(&kthreadd_done);
...
}
wait_for_completion之前講了,與complete是配套的同步機(jī)制晨炕,這里就是等待&kthreadd_done這個(gè)值complete衫画,然后就可以繼續(xù)執(zhí)行
1.5 numa_default_policy
定義在msm/mm/mempolicy.c
/* Reset policy of current process to default */
void numa_default_policy(void)
{
do_set_mempolicy(MPOL_DEFAULT, 0, NULL); //設(shè)定NUMA系統(tǒng)的內(nèi)存訪問策略為MPOL_DEFAULT
}
1.6 kthreadd
定義在msm/kernel/kthread.c中
kthreadd進(jìn)程我將在第二節(jié)中重點(diǎn)講,它是內(nèi)核中重要的進(jìn)程瓮栗,負(fù)責(zé)內(nèi)核線程的調(diào)度和管理削罩,內(nèi)核線程基本都是以它為父進(jìn)程的
1.7 rcu_read_lock & rcu_read_unlock
定義在msm/include/linux/rcupdate.h和msm/kernel/rcupdate.c中
RCU(Read-Copy Update)是數(shù)據(jù)同步的一種方式,在當(dāng)前的Linux內(nèi)核中發(fā)揮著重要的作用费奸。RCU主要針對(duì)的數(shù)據(jù)對(duì)象是鏈表弥激,目的是提高遍歷讀取數(shù)據(jù)的效率,為了達(dá)到目的使用RCU機(jī)制讀取數(shù)據(jù)的時(shí)候不對(duì)鏈表進(jìn)行耗時(shí)的加鎖操作愿阐。這樣在同一時(shí)間可以有多個(gè)線程同時(shí)讀取該鏈表微服,并且允許一個(gè)線程對(duì)鏈表進(jìn)行修改(修改的時(shí)候,需要加鎖)
static inline void rcu_read_lock(void)
{
__rcu_read_lock();
__acquire(RCU);
rcu_lock_acquire(&rcu_lock_map);
rcu_lockdep_assert(!rcu_is_cpu_idle(),
"rcu_read_lock() used illegally while idle");
}
static inline void rcu_read_unlock(void)
{
rcu_lockdep_assert(!rcu_is_cpu_idle(),
"rcu_read_unlock() used illegally while idle");
rcu_lock_release(&rcu_lock_map);
__release(RCU);
__rcu_read_unlock();
}
1.8 find_task_by_pid_ns
定義在msm/kernel/pid.c中
task_struct叫進(jìn)程描述符缨历,這個(gè)結(jié)構(gòu)體包含了一個(gè)進(jìn)程所需的所有信息以蕴,它定義在msm/include/linux/sched.h文件中。
它的結(jié)構(gòu)十分復(fù)雜辛孵,本文就不重點(diǎn)講了丛肮,可以參考Linux進(jìn)程描述符task_struct結(jié)構(gòu)體詳解
/*
* Must be called under rcu_read_lock().
*/
struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns)
{
rcu_lockdep_assert(rcu_read_lock_held(),
"find_task_by_pid_ns() needs rcu_read_lock()"
" protection"); //必須進(jìn)行RCU加鎖
return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID);
}
struct pid *find_pid_ns(int nr, struct pid_namespace *ns)
{
struct upid *pnr;
hlist_for_each_entry_rcu(pnr,
&pid_hash[pid_hashfn(nr, ns)], pid_chain)
/*
* C語(yǔ)言中 -> 用于指向結(jié)構(gòu)體 struct 中的數(shù)據(jù)
*/
if (pnr->nr == nr && pnr->ns == ns)
return container_of(pnr, struct pid,
numbers[ns->level]); //遍歷hash表,找到struct pid
return NULL;
}
struct task_struct *pid_task(struct pid *pid, enum pid_type type)
{
struct task_struct *result = NULL;
if (pid) {
struct hlist_node *first;
first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]),
lockdep_tasklist_lock_is_held());
if (first)
result = hlist_entry(first, struct task_struct, pids[(type)].node); //從hash表中找出struct task_struct
}
return result;
}
find_task_by_pid_ns的作用就是根據(jù)pid魄缚,在hash表中獲得對(duì)應(yīng)pid的task_struct
1.9 sched_setscheduler_nocheck
定義在msm/kernel/sched/core.c中
int sched_setscheduler_nocheck(struct task_struct *p, int policy,
const struct sched_param *param)
{
struct sched_attr attr = {
.sched_policy = policy,
.sched_priority = param->sched_priority
};
return __sched_setscheduler(p, &attr, false); //設(shè)置進(jìn)程調(diào)度策略
}
linux內(nèi)核目前實(shí)現(xiàn)了6種調(diào)度策略(即調(diào)度算法), 用于對(duì)不同類型的進(jìn)程進(jìn)行調(diào)度, 或者支持某些特殊的功能
SCHED_FIFO和SCHED_RR和SCHED_DEADLINE則采用不同的調(diào)度策略調(diào)度實(shí)時(shí)進(jìn)程宝与,優(yōu)先級(jí)最高
SCHED_NORMAL和SCHED_BATCH調(diào)度普通的非實(shí)時(shí)進(jìn)程,優(yōu)先級(jí)普通
SCHED_IDLE則在系統(tǒng)空閑時(shí)調(diào)用idle進(jìn)程冶匹,優(yōu)先級(jí)最低
1.10 init_idle_bootup_task
定義在msm/kernel/sched/core.c中
void __cpuinit init_idle_bootup_task(struct task_struct *idle)
{
idle->sched_class = &idle_sched_class; //設(shè)置進(jìn)程的調(diào)度器類為idle_sched_class
}
Linux依據(jù)其調(diào)度策略的不同實(shí)現(xiàn)了5個(gè)調(diào)度器類, 一個(gè)調(diào)度器類可以用一種種或者多種調(diào)度策略調(diào)度某一類進(jìn)程, 也可以用于特殊情況或者調(diào)度特殊功能的進(jìn)程.
其所屬進(jìn)程的優(yōu)先級(jí)順序?yàn)?/p>
stop_sched_class -> dl_sched_class -> rt_sched_class -> fair_sched_class -> idle_sched_class
可見idle_sched_class的優(yōu)先級(jí)最低习劫,只有系統(tǒng)空閑時(shí)才調(diào)用idle進(jìn)程
1.11 schedule_preempt_disabled
定義在msm/kernel/sched/core.c中
/**
* schedule_preempt_disabled - called with preemption disabled
*
* Returns with preemption disabled. Note: preempt_count must be 1
*/
void __sched schedule_preempt_disabled(void)
{
sched_preempt_enable_no_resched(); //開啟內(nèi)核搶占
schedule(); // 并主動(dòng)請(qǐng)求調(diào)度,讓出cpu
preempt_disable(); // 關(guān)閉內(nèi)核搶占
}
1.9到1.11都涉及到Linux的進(jìn)程調(diào)度問題嚼隘,可以參考 Linux用戶搶占和內(nèi)核搶占詳解
1.12 cpu_startup_entry
定義在msm/kernel/cpu/idle.c中
void cpu_startup_entry(enum cpuhp_state state)
{
/*
* This #ifdef needs to die, but it's too late in the cycle to
* make this generic (arm and sh have never invoked the canary
* init for the non boot cpus!). Will be fixed in 3.11
*/
/*
* C語(yǔ)言中#ifdef和#else诽里、#endif是條件編譯語(yǔ)句,也就是說(shuō)在滿足某些條件的時(shí)候飞蛹,夾在這幾個(gè)關(guān)鍵字中間的代碼才編譯须肆,不滿足就不編譯
* 下面這句話的意思就是如果定義了CONFIG_X86這個(gè)宏,就把boot_init_stack_canary這個(gè)代碼編譯進(jìn)去
*/
#ifdef CONFIG_X86
/*
* If we're the non-boot CPU, nothing set the stack canary up
* for us. The boot CPU already has it initialized but no harm
* in doing it again. This is a good place for updating it, as
* we wont ever return from this function (so the invalid
* canaries already on the stack wont ever trigger).
*/
boot_init_stack_canary();//只有在x86這種non-boot CPU機(jī)器上執(zhí)行桩皿,該函數(shù)主要用于初始化stack_canary的值,用于防止棧溢出
#endif
__current_set_polling(); //設(shè)置本架構(gòu)下面有標(biāo)示輪詢poll的bit位,保證cpu進(jìn)行重新調(diào)度幢炸。
arch_cpu_idle_prepare(); //進(jìn)行idle前的準(zhǔn)備工作泄隔,ARM64中沒有實(shí)現(xiàn)
per_cpu(idle_force_poll, smp_processor_id()) = 0;
cpu_idle_loop(); //進(jìn)入idle進(jìn)程的事件循環(huán)
}
1.13 cpu_idle_loop
定義在msm/kernel/cpu/idle.c中
/*
* Generic idle loop implementation
*/
static void cpu_idle_loop(void)
{
while (1) { //開啟無(wú)限循環(huán),進(jìn)行進(jìn)程調(diào)度
tick_nohz_idle_enter(); //停止周期時(shí)鐘
while (!need_resched()) { //判斷是否有設(shè)置TIF_NEED_RESCHED宛徊,只有系統(tǒng)沒有進(jìn)程需要調(diào)度時(shí)才執(zhí)行while里面操作
check_pgt_cache();
rmb();
local_irq_disable(); //關(guān)閉irq中斷
arch_cpu_idle_enter();
/*
* In poll mode we reenable interrupts and spin.
*
* Also if we detected in the wakeup from idle
* path that the tick broadcast device expired
* for us, we don't want to go deep idle as we
* know that the IPI is going to arrive right
* away
*/
if (cpu_idle_force_poll ||
tick_check_broadcast_expired() ||
__get_cpu_var(idle_force_poll)) {
cpu_idle_poll(); //進(jìn)入 CPU 的poll mode模式佛嬉,避免進(jìn)入深度睡眠逻澳,可以處理 處理器間中斷
} else {
if (!current_clr_polling_and_test()) {
stop_critical_timings();
rcu_idle_enter();
arch_cpu_idle(); //進(jìn)入 CPU 的 idle 模式,省電
WARN_ON_ONCE(irqs_disabled());
rcu_idle_exit();
start_critical_timings();
} else {
local_irq_enable();
}
__current_set_polling();
}
arch_cpu_idle_exit();
}
tick_nohz_idle_exit(); //如果有進(jìn)程需要調(diào)度暖呕,則先開啟周期時(shí)鐘
schedule_preempt_disabled(); //讓出cpu斜做,執(zhí)行調(diào)度
if (cpu_is_offline(smp_processor_id())) //如果當(dāng)前cpu處理offline狀態(tài),關(guān)閉idle進(jìn)程
arch_cpu_idle_dead();
}
}
idle進(jìn)程并不執(zhí)行什么復(fù)雜的工作湾揽,只有在系統(tǒng)沒有其他進(jìn)程調(diào)度的時(shí)候才進(jìn)入idle進(jìn)程瓤逼,而在idle進(jìn)程中盡可能讓cpu空閑下來(lái),連周期時(shí)鐘也關(guān)掉了库物,達(dá)到省電目的霸旗。當(dāng)有其他進(jìn)程需要調(diào)度的時(shí)候,馬上開啟周期時(shí)鐘戚揭,然后讓出cpu诱告。
小結(jié)
idle進(jìn)程是Linux系統(tǒng)的第一個(gè)進(jìn)程,進(jìn)程號(hào)是0民晒,在完成系統(tǒng)環(huán)境初始化工作之后精居,開啟了兩個(gè)重要的進(jìn)程,init進(jìn)程和kthreadd進(jìn)程潜必,執(zhí)行完創(chuàng)建工作之后靴姿,開啟一個(gè)無(wú)限循環(huán),負(fù)責(zé)進(jìn)程的調(diào)度刮便。
二空猜、kthreadd進(jìn)程啟動(dòng)
之前在rest_init函數(shù)中啟動(dòng)了kthreadd進(jìn)程
pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
進(jìn)程創(chuàng)建成功后會(huì)執(zhí)行kthreadd函數(shù)
2.1 kthreadd
定義在msm/kernel/kthread.c中
int kthreadd(void *unused)
{
struct task_struct *tsk = current;
/* Setup a clean context for our children to inherit. */
set_task_comm(tsk, "kthreadd");
ignore_signals(tsk);
set_cpus_allowed_ptr(tsk, cpu_all_mask); // 允許kthreadd在任意CPU上運(yùn)行
set_mems_allowed(node_states[N_MEMORY]);
current->flags |= PF_NOFREEZE;
for (;;) {
set_current_state(TASK_INTERRUPTIBLE); //首先將線程狀態(tài)設(shè)置為 TASK_INTERRUPTIBLE, 如果當(dāng)前沒有要?jiǎng)?chuàng)建的線程則主動(dòng)放棄 CPU 完成調(diào)度.此進(jìn)程變?yōu)樽枞麘B(tài)
if (list_empty(&kthread_create_list)) // 沒有需要?jiǎng)?chuàng)建的內(nèi)核線程
schedule(); // 執(zhí)行一次調(diào)度, 讓出CPU
__set_current_state(TASK_RUNNING);// 運(yùn)行到此表示 kthreadd 線程被喚醒(就是我們當(dāng)前),設(shè)置進(jìn)程運(yùn)行狀態(tài)為 TASK_RUNNING
spin_lock(&kthread_create_lock); //spin_lock和spin_unlock是配套的加鎖機(jī)制,spin_lock是加鎖
while (!list_empty(&kthread_create_list)) {
struct kthread_create_info *create;
create = list_entry(kthread_create_list.next,
struct kthread_create_info, list); //kthread_create_list是一個(gè)鏈表恨旱,從鏈表中取出下一個(gè)要?jiǎng)?chuàng)建的kthread_create_info,即線程創(chuàng)建信息
list_del_init(&create->list); //刪除create中的list
spin_unlock(&kthread_create_lock); //解鎖
create_kthread(create); //創(chuàng)建線程
spin_lock(&kthread_create_lock);
}
spin_unlock(&kthread_create_lock);
}
return 0;
}
kthreadd函數(shù)的作用就是循環(huán)地從kthread_create_list鏈表中取出要?jiǎng)?chuàng)建的線程辈毯,然后執(zhí)行create_kthread函數(shù),直到kthread_create_list為空搜贤,讓出CPU,進(jìn)入睡眠谆沃,我們來(lái)看下create_kthread函數(shù)
2.2 create_kthread
定義在msm/kernel/kthread.c中
static void create_kthread(struct kthread_create_info *create)
{
int pid;
#ifdef CONFIG_NUMA
current->pref_node_fork = create->node;
#endif
/* We want our own signal handler (we take no signals by default). */
pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
if (pid < 0) {
create->result = ERR_PTR(pid);
complete(&create->done);
}
}
其實(shí)這里面就是調(diào)用kernel_thread函數(shù)創(chuàng)建進(jìn)程,然后執(zhí)行kthread函數(shù)仪芒,注意不要搞混了唁影,之前那個(gè)函數(shù)叫kthreadd,接下來(lái)看看kthread函數(shù)
2.3 kthread
定義在msm/kernel/kthread.c中
static int kthread(void *_create)
{
/* Copy data: it's on kthread's stack */
struct kthread_create_info *create = _create; // create 就是之前kthreadd函數(shù)循環(huán)取出的 kthread_create_info
int (*threadfn)(void *data) = create->threadfn; //新線程工作函數(shù)
void *data = create->data;
struct kthread self;
int ret;
self.flags = 0;
self.data = data;
init_completion(&self.exited);
init_completion(&self.parked);
current->vfork_done = &self.exited;
/* OK, tell user we're spawned, wait for stop or wakeup */
__set_current_state(TASK_UNINTERRUPTIBLE);
create->result = current;
complete(&create->done); //表示線程創(chuàng)建完畢
schedule(); //讓出CPU掂名,注意這里并沒有執(zhí)行新線程的threadfn函數(shù)就直接進(jìn)入睡眠了据沈,然后等待線程被手動(dòng)喚醒,然后才執(zhí)行threadfn
ret = -EINTR;
if (!test_bit(KTHREAD_SHOULD_STOP, &self.flags)) {
__kthread_parkme(&self);
ret = threadfn(data);
}
/* we can't just return, we must preserve "self" on stack */
do_exit(ret);
}
2.4 kthread_create & kthread_run
定義在msm/include/linux/kthread.h
kthreadd創(chuàng)建線程是遍歷kthread_create_list列表饺蔑,那kthread_create_list列表中的值是哪兒來(lái)的呢锌介?我們知道Linux創(chuàng)建內(nèi)核線程有兩種方式,kthread_create和kthread_run
#define kthread_create(threadfn, data, namefmt, arg...) \
kthread_create_on_node(threadfn, data, -1, namefmt, ##arg)
#define kthread_run(threadfn, data, namefmt, ...) \
({ \
struct task_struct *__k \
= kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
if (!IS_ERR(__k)) \
wake_up_process(__k); //手動(dòng)喚醒新線程 \
__k; \
})
kthread_create和kthread_run并不是函數(shù),而是宏孔祸,宏相當(dāng)于Java中的final static定義隆敢,在編譯時(shí)會(huì)替換對(duì)應(yīng)代碼,宏的參數(shù)沒有類型定義崔慧,多行宏的定義會(huì)在行末尾加上\
這兩個(gè)宏最終都是調(diào)用kthread_create_on_node函數(shù)拂蝎,只是kthread_run在線程創(chuàng)建完成后會(huì)手動(dòng)喚醒,我們來(lái)看看kthread_create_on_node函數(shù)
2.5 kthread_create_on_node
定義在msm/kernel/kthread.c中
/**
* kthread_create_on_node - create a kthread.
* @threadfn: the function to run until signal_pending(current).
* @data: data ptr for @threadfn.
* @node: memory node number.
* @namefmt: printf-style name for the thread.
*
* Description: This helper function creates and names a kernel
* thread. The thread will be stopped: use wake_up_process() to start
* it. See also kthread_run().
*
* If thread is going to be bound on a particular cpu, give its node
* in @node, to get NUMA affinity for kthread stack, or else give -1.
* When woken, the thread will run @threadfn() with @data as its
* argument. @threadfn() can either call do_exit() directly if it is a
* standalone thread for which no one will call kthread_stop(), or
* return when 'kthread_should_stop()' is true (which means
* kthread_stop() has been called). The return value should be zero
* or a negative error number; it will be passed to kthread_stop().
*
* Returns a task_struct or ERR_PTR(-ENOMEM).
*/
struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
void *data, int node,
const char namefmt[],
...)
{
struct kthread_create_info create;
create.threadfn = threadfn;
create.data = data;
create.node = node;
init_completion(&create.done); //初始化&create.done惶室,之前講過(guò)completion和wait_for_completion同步
spin_lock(&kthread_create_lock); //加鎖温自,之前也講過(guò)
list_add_tail(&create.list, &kthread_create_list); //將要?jiǎng)?chuàng)建的線程加到kthread_create_list鏈表尾部
spin_unlock(&kthread_create_lock);
wake_up_process(kthreadd_task); //喚醒kthreadd進(jìn)程,開啟列表循環(huán)創(chuàng)建線程
wait_for_completion(&create.done); //當(dāng)&create.done complete時(shí)拇涤,會(huì)繼續(xù)往下執(zhí)行
if (!IS_ERR(create.result)) {
static const struct sched_param param = { .sched_priority = 0 };
va_list args; //不定參數(shù)定義捣作,相當(dāng)于Java中的... ,定義多個(gè)數(shù)量不定的參數(shù)
va_start(args, namefmt);
vsnprintf(create.result->comm, sizeof(create.result->comm),
namefmt, args);
va_end(args);
/*
* root may have changed our (kthreadd's) priority or CPU mask.
* The kernel thread should not inherit these properties.
*/
sched_setscheduler_nocheck(create.result, SCHED_NORMAL, ¶m); //create.result類型為task_struct鹅士,該函數(shù)作用是設(shè)置新線程調(diào)度策略券躁,SCHED_NORMAL 普通調(diào)度策略,非實(shí)時(shí)掉盅,優(yōu)先級(jí)低于實(shí)時(shí)調(diào)度策略SCHED_FIFO和SCHED_RR也拜,param的優(yōu)先級(jí)上面定義為0
set_cpus_allowed_ptr(create.result, cpu_all_mask); //允許新線程在任意CPU上運(yùn)行
}
return create.result;
}
kthread_create_on_node主要作用就是在kthread_create_list鏈表尾部加上要?jiǎng)?chuàng)建的線程,然后喚醒kthreadd進(jìn)程進(jìn)行具體創(chuàng)建工作
小結(jié)
kthreadd進(jìn)程由idle通過(guò)kernel_thread創(chuàng)建趾痘,并始終運(yùn)行在內(nèi)核空間, 負(fù)責(zé)所有內(nèi)核線程的調(diào)度和管理慢哈,所有的內(nèi)核線程都是直接或者間接的以kthreadd為父進(jìn)程。
kthreadd進(jìn)程會(huì)執(zhí)行一個(gè)kthreadd的函數(shù)永票,該函數(shù)的作用就是遍歷kthread_create_list鏈表卵贱,從鏈表中取出需要?jiǎng)?chuàng)建的內(nèi)核線程進(jìn)行創(chuàng)建, 創(chuàng)建成功后會(huì)執(zhí)行kthread函數(shù)。
kthread函數(shù)完成一些初始賦值后就讓出CPU侣集,并沒有執(zhí)行新線程的工作函數(shù)键俱,因此需要手工 wake up被喚醒后,新線程才執(zhí)行自己的真正工作函數(shù)世分。
當(dāng)我們調(diào)用kthread_create和kthread_run創(chuàng)建的內(nèi)核線程會(huì)被加入到kthread_create_list鏈表编振,kthread_create不會(huì)手動(dòng)wake up新線程,kthread_run會(huì)手動(dòng)wake up新線程臭埋。
其實(shí)這就是一個(gè)典型的生產(chǎn)者消費(fèi)者模式踪央,kthread_create和kthread_run負(fù)責(zé)生產(chǎn)各種內(nèi)核線程創(chuàng)建需求,kthreadd開啟循環(huán)去消費(fèi)各種內(nèi)核線程創(chuàng)建需求瓢阴。
三畅蹂、init進(jìn)程啟動(dòng)
init進(jìn)程分為前后兩部分,前一部分是在內(nèi)核啟動(dòng)的荣恐,主要是完成創(chuàng)建和內(nèi)核初始化工作魁莉,內(nèi)容都是跟Linux內(nèi)核相關(guān)的;后一部分是在用戶空間啟動(dòng)的,主要完成Android系統(tǒng)的初始化工作。
我這里要講的是前一部分旗唁,后一部分將在下一篇文章中講述。
之前在rest_init函數(shù)中啟動(dòng)了init進(jìn)程
kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);
在創(chuàng)建完init進(jìn)程后痹束,會(huì)調(diào)用kernel_init函數(shù)
3.1 kernel_init
定義在msm/init/main.c中
/*
* __ref 這個(gè)跟之前講的__init作用一樣
*/
static int __ref kernel_init(void *unused)
{
kernel_init_freeable(); //進(jìn)行init進(jìn)程的一些初始化操作
/* need to finish all async __init code before freeing the memory */
async_synchronize_full();// 等待所有異步調(diào)用執(zhí)行完成,检疫,在釋放內(nèi)存前,必須完成所有的異步 __init 代碼
free_initmem();// 釋放所有init.* 段中的內(nèi)存
mark_rodata_ro(); //arm64空實(shí)現(xiàn)
system_state = SYSTEM_RUNNING;// 設(shè)置系統(tǒng)狀態(tài)為運(yùn)行狀態(tài)
numa_default_policy(); // 設(shè)定NUMA系統(tǒng)的默認(rèn)內(nèi)存訪問策略
flush_delayed_fput(); // 釋放所有延時(shí)的struct file結(jié)構(gòu)體
if (ramdisk_execute_command) { //ramdisk_execute_command的值為"/init"
if (!run_init_process(ramdisk_execute_command)) //運(yùn)行根目錄下的init程序
return 0;
pr_err("Failed to execute %s\n", ramdisk_execute_command);
}
/*
* We try each of these until one succeeds.
*
* The Bourne shell can be used instead of init if we are
* trying to recover a really broken machine.
*/
if (execute_command) { //execute_command的值如果有定義就去根目錄下找對(duì)應(yīng)的應(yīng)用程序,然后啟動(dòng)
if (!run_init_process(execute_command))
return 0;
pr_err("Failed to execute %s. Attempting defaults...\n",
execute_command);
}
if (!run_init_process("/sbin/init") || //如果ramdisk_execute_command和execute_command定義的應(yīng)用程序都沒有找到祷嘶,就到根目錄下找 /sbin/init屎媳,/etc/init,/bin/init,/bin/sh 這四個(gè)應(yīng)用程序進(jìn)行啟動(dòng)
!run_init_process("/etc/init") ||
!run_init_process("/bin/init") ||
!run_init_process("/bin/sh"))
return 0;
panic("No init found. Try passing init= option to kernel. "
"See Linux Documentation/init.txt for guidance.");
}
kernel_init主要工作是完成一些init的初始化操作论巍,然后去系統(tǒng)根目錄下依次找ramdisk_execute_command和execute_command設(shè)置的應(yīng)用程序烛谊,如果這兩個(gè)目錄都找不到,就依次去根目錄下找 /sbin/init嘉汰,/etc/init丹禀,/bin/init,/bin/sh 這四個(gè)應(yīng)用程序進(jìn)行啟動(dòng),只要這些應(yīng)用程序有一個(gè)啟動(dòng)了鞋怀,其他就不啟動(dòng)了
ramdisk_execute_command和execute_command的值是通過(guò)bootloader傳遞過(guò)來(lái)的參數(shù)設(shè)置的双泪,ramdisk_execute_command通過(guò)"rdinit"參數(shù)賦值,execute_command通過(guò)"init"參數(shù)賦值
ramdisk_execute_command如果沒有被賦值密似,kernel_init_freeable函數(shù)會(huì)賦一個(gè)初始值"/init"
3.2 kernel_init_freeable
定義在msm/init/main.c中
static noinline void __init kernel_init_freeable(void)
{
/*
* Wait until kthreadd is all set-up.
*/
wait_for_completion(&kthreadd_done); //等待&kthreadd_done這個(gè)值complete,這個(gè)在rest_init方法中有寫焙矛,在ktreadd進(jìn)程啟動(dòng)完成后設(shè)置為complete
/* Now the scheduler is fully set up and can do blocking allocations */
gfp_allowed_mask = __GFP_BITS_MASK;//設(shè)置bitmask, 使得init進(jìn)程可以使用PM并且允許I/O阻塞操作
/*
* init can allocate pages on any node
*/
set_mems_allowed(node_states[N_MEMORY]);//init進(jìn)程可以分配物理頁(yè)面
/*
* init can run on any cpu.
*/
set_cpus_allowed_ptr(current, cpu_all_mask); //init進(jìn)程可以在任意cpu上執(zhí)行
cad_pid = task_pid(current); //設(shè)置到init進(jìn)程的pid號(hào)給cad_pid,cad就是ctrl-alt-del残腌,設(shè)置init進(jìn)程來(lái)處理ctrl-alt-del信號(hào)
smp_prepare_cpus(setup_max_cpus);//設(shè)置smp初始化時(shí)的最大CPU數(shù)量村斟,然后將對(duì)應(yīng)數(shù)量的CPU狀態(tài)設(shè)置為present
do_pre_smp_initcalls();//調(diào)用__initcall_start到__initcall0_start之間的initcall_t函數(shù)指針
lockup_detector_init(); //開啟watchdog_threads,watchdog主要用來(lái)監(jiān)控抛猫、管理CPU的運(yùn)行狀態(tài)
smp_init();//啟動(dòng)cpu0外的其他cpu核
sched_init_smp(); //進(jìn)程調(diào)度域初始化
do_basic_setup();//初始化設(shè)備蟆盹,驅(qū)動(dòng)等,這個(gè)方法比較重要邑滨,將在下面單獨(dú)講
/* Open the /dev/console on the rootfs, this should never fail */
if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0) // 打開/dev/console日缨,文件號(hào)0,作為init進(jìn)程標(biāo)準(zhǔn)輸入
pr_err("Warning: unable to open an initial console.\n");
(void) sys_dup(0);// 標(biāo)準(zhǔn)輸入
(void) sys_dup(0);// 標(biāo)準(zhǔn)輸出
/*
* check if there is an early userspace init. If yes, let it do all
* the work
*/
if (!ramdisk_execute_command) //如果 ramdisk_execute_command 沒有賦值掖看,則賦值為"/init"匣距,之前有講到
ramdisk_execute_command = "/init";
if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) { // 嘗試進(jìn)入ramdisk_execute_command指向的文件,如果失敗則重新掛載根文件系統(tǒng)
ramdisk_execute_command = NULL;
prepare_namespace();
}
/*
* Ok, we have completed the initial bootup, and
* we're essentially up and running. Get rid of the
* initmem segments and start the user-mode stuff..
*/
/* rootfs is available now, try loading default modules */
load_default_modules(); // 加載I/O調(diào)度的電梯算法
}
kernel_init_freeable函數(shù)做了很多重要的事情
- 啟動(dòng)了smp哎壳,smp全稱是Symmetrical Multi-Processing毅待,即對(duì)稱多處理,是指在一個(gè)計(jì)算機(jī)上匯集了一組處理器(多CPU),各CPU之間共享內(nèi)存子系統(tǒng)以及總線結(jié)構(gòu)归榕。
- 初始化設(shè)備和驅(qū)動(dòng)程序
- 打開標(biāo)準(zhǔn)輸入和輸出
- 初始化文件系統(tǒng)
3.3 do_basic_setup
定義在msm/init/main.c中
/*
* Ok, the machine is now initialized. None of the devices
* have been touched yet, but the CPU subsystem is up and
* running, and memory and process management works.
*
* Now we can finally start doing some real work..
*/
static void __init do_basic_setup(void)
{
cpuset_init_smp();//針對(duì)SMP系統(tǒng)尸红,初始化內(nèi)核control group的cpuset子系統(tǒng)。
usermodehelper_init();// 創(chuàng)建khelper單線程工作隊(duì)列,用于協(xié)助新建和運(yùn)行用戶空間程序
shmem_init();// 初始化共享內(nèi)存
driver_init();// 初始化設(shè)備驅(qū)動(dòng)外里,比較重要下面單獨(dú)講
init_irq_proc();//創(chuàng)建/proc/irq目錄, 并初始化系統(tǒng)中所有中斷對(duì)應(yīng)的子目錄
do_ctors();// 執(zhí)行內(nèi)核的構(gòu)造函數(shù)
usermodehelper_enable();// 啟用usermodehelper
do_initcalls();//遍歷initcall_levels數(shù)組怎爵,調(diào)用里面的initcall函數(shù),這里主要是對(duì)設(shè)備盅蝗、驅(qū)動(dòng)鳖链、文件系統(tǒng)進(jìn)行初始化,之所有將函數(shù)封裝到數(shù)組進(jìn)行遍歷墩莫,主要是為了好擴(kuò)展
random_int_secret_init();//初始化隨機(jī)數(shù)生成池
}
3.4 driver_init
定義在msm/drivers/base/init.c中
/**
* driver_init - initialize driver model.
*
* Call the driver model init functions to initialize their
* subsystems. Called early from init/main.c.
*/
void __init driver_init(void)
{
/* These are the core pieces */
devtmpfs_init();// 注冊(cè)devtmpfs文件系統(tǒng)芙委,啟動(dòng)kdevtmpfs進(jìn)程
devices_init();// 初始化驅(qū)動(dòng)模型中的部分子系統(tǒng),kset:devices 和 kobject:dev狂秦、 dev/block灌侣、 dev/char
buses_init();// 初始化驅(qū)動(dòng)模型中的bus子系統(tǒng),kset:bus裂问、devices/system
classes_init();// 初始化驅(qū)動(dòng)模型中的class子系統(tǒng)侧啼,kset:class
firmware_init();// 初始化驅(qū)動(dòng)模型中的firmware子系統(tǒng) ,kobject:firmware
hypervisor_init();// 初始化驅(qū)動(dòng)模型中的hypervisor子系統(tǒng)愕秫,kobject:hypervisor
/* These are also core pieces, but must come after the
* core core pieces.
*/
platform_bus_init();// 初始化驅(qū)動(dòng)模型中的bus/platform子系統(tǒng),這個(gè)節(jié)點(diǎn)是所有platform設(shè)備和驅(qū)動(dòng)的總線類型慨菱,即所有platform設(shè)備和驅(qū)動(dòng)都會(huì)掛載到這個(gè)總線上
cpu_dev_init(); // 初始化驅(qū)動(dòng)模型中的devices/system/cpu子系統(tǒng),該節(jié)點(diǎn)包含CPU相關(guān)的屬性
memory_dev_init();//初始化驅(qū)動(dòng)模型中的/devices/system/memory子系統(tǒng),該節(jié)點(diǎn)包含了內(nèi)存相關(guān)的屬性,如塊大小等
}
這個(gè)函數(shù)完成驅(qū)動(dòng)子系統(tǒng)的構(gòu)建戴甩,實(shí)現(xiàn)了Linux設(shè)備驅(qū)動(dòng)的一個(gè)整體框架符喝,但是它只是建立了目錄結(jié)構(gòu),具體驅(qū)動(dòng)的裝載是在do_initcalls函數(shù)甜孤,之前有講
kernel_init_freeable函數(shù)告一段落了协饲,我們接著講kernel_init中剩余的函數(shù)
3.5 free_initmem
定義在msm/arch/arm64/mm/init.c中中
void free_initmem(void)
{
poison_init_mem(__init_begin, __init_end - __init_begin);
free_initmem_default(0);
}
所有使用__init標(biāo)記過(guò)的函數(shù)和使用__initdata標(biāo)記過(guò)的數(shù)據(jù),在free_initmem函數(shù)執(zhí)行后缴川,都不能使用茉稠,它們?cè)?jīng)獲得的內(nèi)存現(xiàn)在可以重新用于其他目的。
3.6 flush_delayed_fput
定義在msm/arch/arm64/mm/init.c中,它執(zhí)行的是delayed_fput(NULL)
static void delayed_fput(struct work_struct *unused)
{
LIST_HEAD(head);
spin_lock_irq(&delayed_fput_lock);
list_splice_init(&delayed_fput_list, &head);
spin_unlock_irq(&delayed_fput_lock);
while (!list_empty(&head)) {
struct file *f = list_first_entry(&head, struct file, f_u.fu_list);
list_del_init(&f->f_u.fu_list); //刪除fu_list
__fput(f); //釋放struct file
}
}
這個(gè)函數(shù)主要用于釋放&delayed_fput_list這個(gè)鏈表中的struct file把夸,struct file即文件結(jié)構(gòu)體而线,代表一個(gè)打開的文件,系統(tǒng)中的每個(gè)打開的文件在內(nèi)核空間都有一個(gè)關(guān)聯(lián)的 struct file恋日。
3.7 run_init_process
定義在msm/init/main.c中
static int run_init_process(const char *init_filename)
{
argv_init[0] = init_filename;
return do_execve(init_filename,
(const char __user *const __user *)argv_init,
(const char __user *const __user *)envp_init); //do_execve就是執(zhí)行一個(gè)可執(zhí)行文件
}
run_init_process就是運(yùn)行可執(zhí)行文件了膀篮,從kernel_init函數(shù)中可知,系統(tǒng)會(huì)依次去找根目錄下的init岂膳,execute_command誓竿,/sbin/init,/etc/init谈截,/bin/init,/bin/sh這六個(gè)可執(zhí)行文件筷屡,只要找到其中一個(gè)涧偷,其他就不執(zhí)行。
Android系統(tǒng)一般會(huì)在根目錄下放一個(gè)init的可執(zhí)行文件毙死,也就是說(shuō)Linux系統(tǒng)的init進(jìn)程在內(nèi)核初始化完成后燎潮,就直接執(zhí)行init這個(gè)文件,這個(gè)文件的源代碼在platform/system/core/init/init.cpp扼倘,下一篇文章中我將以這個(gè)文件為入口跟啤,講解Android系統(tǒng)的init進(jìn)程。