命令行參數(shù)將影響啟動(dòng)過(guò)程中的代碼執(zhí)行路徑。舉一個(gè)例子,假設(shè)某命令行參數(shù)為bootmode,如果該參數(shù)被設(shè)置為1,意味著你希望在啟動(dòng)過(guò)程中打印一些調(diào)試信息并在啟動(dòng)結(jié)束時(shí)切換到runlevel的第3級(jí)(初始化進(jìn)程的啟動(dòng)信息打印后就會(huì)了解runlevel的含義);如果bootmode參數(shù)被設(shè)置為0,意味著你希望啟動(dòng)過(guò)程相對(duì)簡(jiǎn)潔,并且設(shè)置runlevel為2。既然已經(jīng)熟悉了init/main.c文件,下面就在該文件中增加如下修改:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->static unsigned int bootmode = 1;
static int __init
is_bootmode_setup(char *str)
{
get_option(&str, &bootmode);
return 1;
}
/* Handle parameter "bootmode=" */
__setup("bootmode=", is_bootmode_setup);
if (bootmode) {
/* Print verbose output */
/* ... */
}
/* ... */
/* If bootmode is 1, choose an init runlevel of 3, else
switch to a run level of 2 */
if (bootmode) {
argv_init[++args] = "3";
} else {
argv_init[++args] = "2";
}
/* ... */
請(qǐng)重新編譯內(nèi)核并嘗試運(yùn)行新的修改。
2.1.4 Calibrating delay...1197.46 BogoMIPS (lpj=2394935)
在啟動(dòng)過(guò)程中,內(nèi)核會(huì)計(jì)算處理器在一個(gè)jiffy時(shí)間內(nèi)運(yùn)行一個(gè)內(nèi)部的延遲循環(huán)的次數(shù)。jiffy的含義是系統(tǒng)定時(shí)器2個(gè)連續(xù)的節(jié)拍之間的間隔。正如所料,該計(jì)算必須被校準(zhǔn)到所用CPU的處理速度。校準(zhǔn)的結(jié)果被存儲(chǔ) target=_blank>存儲(chǔ)在稱(chēng)為loops_per_jiffy的內(nèi)核變量中。使用loops_per_jiffy的一種情況是某設(shè)備驅(qū)動(dòng)程序希望進(jìn)行小的微秒級(jí)別的延遲的時(shí)候。
為了理解延遲—循環(huán)校準(zhǔn)代碼,讓我們看一下定義于init/calibrate.c文件中的calibrate_ delay()函數(shù)。該函數(shù)靈活地使用整型運(yùn)算得到了浮點(diǎn)的精度。如下的代碼片段(有一些注釋)顯示了該函數(shù)的開(kāi)始部分,這部分用于得到一個(gè)loops_per_jiffy的粗略值:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->loops_per_jiffy = (1 << 12); /* Initial approximation = 4096 */
printk(KERN_DEBUG “Calibrating delay loop...“);
while ((loops_per_jiffy <<= 1) != 0) {
ticks = jiffies; /* As you will find out in the section, “Kernel
Timers," the jiffies variable contains the
number of timer ticks since the kernel
started, and is incremented in the timer
interrupt handler */
while (ticks == jiffies); /* Wait until the start of the next jiffy */
ticks = jiffies;
/* Delay */
__delay(loops_per_jiffy);
/* Did the wait outlast the current jiffy? Continue if it didn't */
ticks = jiffies - ticks;
if (ticks) break;
}
loops_per_jiffy >>= 1; /* This fixes the most significant bit and is
the lower-bound of loops_per_jiffy */
上述代碼首先假定loops_per_jiffy大于4096,這可以轉(zhuǎn)化為處理器速度大約為每秒100萬(wàn)條指令,即1 MIPS。接下來(lái),它等待jiffy被刷新(1個(gè)新的節(jié)拍的開(kāi)始),并開(kāi)始運(yùn)行延遲循環(huán)__delay(loops_per_jiffy)。如果這個(gè)延遲循環(huán)持續(xù)了1個(gè)jiffy以上,將使用以前的loops_per_jiffy值(將當(dāng)前值右移1位)修復(fù)當(dāng)前l(fā)oops_per_jiffy的最高位;否則,該函數(shù)繼續(xù)通過(guò)左移loops_per_jiffy值來(lái)探測(cè)出其最高位。在內(nèi)核計(jì)算出最高位后,它開(kāi)始計(jì)算低位并微調(diào)其精度:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->loopbit = loops_per_jiffy;
/* Gradually work on the lower-order bits */
while (lps_precision-- && (loopbit >>= 1)) {
loops_per_jiffy |= loopbit;
ticks = jiffies;
while (ticks == jiffies); /* Wait until the start of the next jiffy */
ticks = jiffies;
/* Delay */
__delay(loops_per_jiffy);
if (jiffies != ticks) /* longer than 1 tick */
loops_per_jiffy &= ~loopbit;
}
上述代碼計(jì)算出了延遲循環(huán)跨越j(luò)iffy邊界時(shí)loops_per_jiffy的低位值。這個(gè)被校準(zhǔn)的值可被用于獲取BogoMIPS(其實(shí)它是一個(gè)并非科學(xué)的處理器速度指標(biāo))。可以使用BogoMIPS作為衡量處理器運(yùn)行速度的相對(duì)尺度。在1.6G Hz 基于Pentium M的筆記本電腦上,根據(jù)前述啟動(dòng)過(guò)程的打印信息,循環(huán)校準(zhǔn)的結(jié)果是:loops_per_jiffy的值為2394935。獲得BogoMIPS的方式如下:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->BogoMIPS = loops_per_jiffy * 1秒內(nèi)的jiffy數(shù)*延遲循環(huán)消耗的指令數(shù)(以百萬(wàn)為單位)
= (2394935 * HZ * 2) / (1000000)
= (2394935 * 250 * 2) / (1000000)
= 1197.46(與啟動(dòng)過(guò)程打印信息中的值一致)
在2.4節(jié)將更深入闡述jiffy、HZ和loops_per_jiffy。
2.1.5 Checking HLT instruction
由于Linux內(nèi)核支持多種硬件平臺(tái),啟動(dòng)代碼會(huì)檢查體系架構(gòu)相關(guān)的bug。其中一項(xiàng)工作就是驗(yàn)證停機(jī)(HLT)指令。
x86處理器的HLT指令會(huì)將CPU置入一種低功耗睡眠模式,直到下一次硬件中斷發(fā)生之前維持不變。當(dāng)內(nèi)核想讓CPU進(jìn)入空閑狀態(tài)時(shí)(查看arch/x86/kernel/process_32.c文件中定義的cpu_idle()函數(shù)),它會(huì)使用HLT指令。對(duì)于有問(wèn)題的CPU而言,命令行參數(shù)no-hlt可以禁止HLT指令。如果no-hlt被設(shè)置,在空閑的時(shí)候,內(nèi)核會(huì)進(jìn)行忙等待而不是通過(guò)HLT給CPU降溫。
當(dāng)init/main.c中的啟動(dòng)代碼調(diào)用include/asm-your-arch/bugs.h中定義的check_bugs()時(shí),會(huì)打印上述信息。