以文本方式查看主題 - 曙海教育集團論壇 (http://www.xinguifushi.cn/bbs/index.asp) -- Linux驅動開發 (http://www.xinguifushi.cn/bbs/list.asp?boardid=33) ---- Linux驅動開發必看:詳解神秘內核(1) (http://www.xinguifushi.cn/bbs/dispbbs.asp?boardid=33&id=1731) |
-- 作者:wangxinxin -- 發布時間:2010-11-24 11:27:27 -- Linux驅動開發必看:詳解神秘內核(1) 命令行參數將影響啟動過程中的代碼執行路徑。舉一個例子,假設某命令行參數為bootmode,如果該參數被設置為1,意味著你希望在啟動過程中打印一些調試信息并在啟動結束時切換到runlevel的第3級(初始化進程的啟動信息打印后就會了解runlevel的含義);如果bootmode參數被設置為0,意味著你希望啟動過程相對簡潔,并且設置runlevel為2。既然已經熟悉了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"; } /* ... */ 請重新編譯內核并嘗試運行新的修改。 2.1.4 Calibrating delay...1197.46 BogoMIPS (lpj=2394935) 在啟動過程中,內核會計算處理器在一個jiffy時間內運行一個內部的延遲循環的次數。jiffy的含義是系統定時器2個連續的節拍之間的間隔。正如所料,該計算必須被校準到所用CPU的處理速度。校準的結果被存儲 target=_blank>存儲在稱為loops_per_jiffy的內核變量中。使用loops_per_jiffy的一種情況是某設備驅動程序希望進行小的微秒級別的延遲的時候。 為了理解延遲—循環校準代碼,讓我們看一下定義于init/calibrate.c文件中的calibrate_ delay()函數。該函數靈活地使用整型運算得到了浮點的精度。如下的代碼片段(有一些注釋)顯示了該函數的開始部分,這部分用于得到一個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,這可以轉化為處理器速度大約為每秒100萬條指令,即1 MIPS。接下來,它等待jiffy被刷新(1個新的節拍的開始),并開始運行延遲循環__delay(loops_per_jiffy)。如果這個延遲循環持續了1個jiffy以上,將使用以前的loops_per_jiffy值(將當前值右移1位)修復當前loops_per_jiffy的最高位;否則,該函數繼續通過左移loops_per_jiffy值來探測出其最高位。在內核計算出最高位后,它開始計算低位并微調其精度: <!-- 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; } 上述代碼計算出了延遲循環跨越jiffy邊界時loops_per_jiffy的低位值。這個被校準的值可被用于獲取BogoMIPS(其實它是一個并非科學的處理器速度指標)。可以使用BogoMIPS作為衡量處理器運行速度的相對尺度。在1.6G Hz 基于Pentium M的筆記本電腦上,根據前述啟動過程的打印信息,循環校準的結果是:loops_per_jiffy的值為2394935。獲得BogoMIPS的方式如下: <!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->BogoMIPS = loops_per_jiffy * 1秒內的jiffy數*延遲循環消耗的指令數(以百萬為單位) = (2394935 * HZ * 2) / (1000000) = (2394935 * 250 * 2) / (1000000) = 1197.46(與啟動過程打印信息中的值一致) 在2.4節將更深入闡述jiffy、HZ和loops_per_jiffy。 2.1.5 Checking HLT instruction 由于Linux內核支持多種硬件平臺,啟動代碼會檢查體系架構相關的bug。其中一項工作就是驗證停機(HLT)指令。 x86處理器的HLT指令會將CPU置入一種低功耗睡眠模式,直到下一次硬件中斷發生之前維持不變。當內核想讓CPU進入空閑狀態時(查看arch/x86/kernel/process_32.c文件中定義的cpu_idle()函數),它會使用HLT指令。對于有問題的CPU而言,命令行參數no-hlt可以禁止HLT指令。如果no-hlt被設置,在空閑的時候,內核會進行忙等待而不是通過HLT給CPU降溫。 當init/main.c中的啟動代碼調用include/asm-your-arch/bugs.h中定義的check_bugs()時,會打印上述信息。 |