免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2296 | 回复: 0
打印 上一主题 下一主题

[硬件及驱动] printk()的实现 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-09-22 23:55 |只看该作者 |倒序浏览
今天看情景分析看到终端设备这节,突然想要看printk是怎么实现,看了下代码,发现控制台都没有初始化就已经用了printk函数,想了一会,无解。在网上搜索了下,发现了网友分析了printk。
网址:http://blog.21ic.com/user1/1066/archives/2006/15092.html

start_kernel(void)
{lock_kernel();
printk(linux_banner);
.....
console_init();
}
上面的代码显示在调用console_init之前,已经使用了printk函数。那么printk函数到底是如何在console上实现呢?可否更改printk函数,重定向输出?本文给出答案。

printk在src/kernel/Printk.c中实现:
int printk(const char *fmt, ...)
在该函数内申请了一块静态内存static char printk_buf[1024];作为输出缓冲区。也就是说,不管console是否存在,printk都可以成功返回。
if (!down_trylock(&console_sem)) {
  /*
   * We own the drivers.  We can drop the spinlock and let
   * release_console_sem() print the text
   */
  spin_unlock_irqrestore(&logbuf_lock, flags);
  console_may_schedule = 0;
  release_console_sem();
printk函数,时时刻刻希望得到当前系统的console,因此只要系统调用了console init函数,printk就可以觉察到。在下次调用printk时,printk会把printk_buf中存储的数据输出到console中。这个过程在release_console_sem();实现:
for ( ; ; ) {
  spin_lock_irqsave(&logbuf_lock, flags);
  must_wake_klogd |= log_start - log_end;
  if (con_start == log_end)
   break;   /* Nothing to print */
  _con_start = con_start;
  _log_end = log_end;
  con_start = log_end;  /* Flush */
  spin_unlock_irqrestore(&logbuf_lock, flags);
  call_console_drivers(_con_start, _log_end);
}
通过调用call_console_drivers,printk输出数据到console!
call_console_drivers再次调用函数:
_call_console_drivers(start_print, cur_index, msg_level);
再次调用更为底层的__call_console_drivers()实现。
正是在__call_console_drivers()中完成了对console驱动中write的调用!
for (con = console_drivers; con; con = con->next) {
  if ((con->flags & CON_ENABLED) && con->write)
   con->write(con, &LOG_BUF(start), end - start);
}

因此,在系统没有调用console_init();之前,printk并没有真正把数据发送到console。更改printk,可以很方便的输出到任何需要的设备上。

评分

参与人数 1可用积分 +6 收起 理由
Godbach + 6 赞一个!

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP