曙海教育集团论坛Linux专区Linux驱动开发 → Linux驱动开发必看:详解神秘内核(1)


  共有7619人关注过本帖树形打印

主题:Linux驱动开发必看:详解神秘内核(1)

美女呀,离线,留言给我吧!
wangxinxin
  1楼 个性首页 | 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:青蜂侠 帖子:1393 积分:14038 威望:0 精华:0 注册:2010-11-12 11:08:23
Linux驱动开发必看:详解神秘内核(1)  发帖心情 Post By:2010-11-24 11:27:27

命令行参数将影响启动过程中的代码执行路径。举一个例子,假设某命令行参数为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的处理速度。校准的结果被存储 href="http://storage.it168.com/" 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()时,会打印上述信息。


支持(0中立(0反对(0单帖管理 | 引用 | 回复 回到顶部

返回版面帖子列表

Linux驱动开发必看:详解神秘内核(1)








签名