手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux软中断

[日期:2012-04-03] 来源:Linux社区  作者:bullbat [字体:大 中 小]

下面是软中断的执行

  1. /*如果在这样的一个检查点(local_softirq_pending()不为0) 
  2. 检测到挂起的软中断,内核调用下面函数处理*/  
  3. asmlinkage void do_softirq(void)  
  4. {  
  5.     __u32 pending;  
  6.     unsigned long flags;  
  7.   
  8.     if (in_interrupt())  
  9.         return;  
  10.   
  11.     local_irq_save(flags);  
  12.   
  13.     pending = local_softirq_pending();  
  14.   
  15.     if (pending)  
  16.         __do_softirq();  
  17.   
  18.     local_irq_restore(flags);  
  19. }  

具体由__do_softirq函数实现

  1. /*读取本地CPU的软中断掩码并执行与每个设置位 
  2. 相关的可延迟函数,__do_softirq只做固定次数的循环 
  3. 然后就返回。如果还有其余挂起的软中断,那么 
  4. 内核线程ksofirqd将会在预期的时间内处理他们*/  
  5. asmlinkage void __do_softirq(void)  
  6. {  
  7.     struct softirq_action *h;  
  8.     __u32 pending;  
  9.     /*把循环计数器的值初始化为10*/  
  10.     int max_restart = MAX_SOFTIRQ_RESTART;  
  11.     int cpu;  
  12.     /*把本地CPU(被local_softirq_pending选中的)软件中断的 
  13.     位掩码复制到局部变量pending中*/  
  14.     pending = local_softirq_pending();  
  15.     account_system_vtime(current);  
  16.     /*增加软中断计数器的值*/  
  17.     __local_bh_disable((unsigned long)__builtin_return_address(0));  
  18.     lockdep_softirq_enter();  
  19.   
  20.     cpu = smp_processor_id();  
  21. restart:  
  22.     /* Reset the pending bitmask before enabling irqs */  
  23.     set_softirq_pending(0);/*清除本地CPU的软中断位图, 
  24.     以便可以激活新的软中断*/  
  25.   
  26.     /*激活本地中断*/  
  27.     local_irq_enable();  
  28.   
  29.     h = softirq_vec;  
  30.   
  31.     do {/*根据pending每一位的的设置,执行对应的软中断 
  32.         处理函数*/  
  33.         if (pending & 1) {  
  34.             int prev_count = preempt_count();  
  35.             kstat_incr_softirqs_this_cpu(h - softirq_vec);  
  36.   
  37.             trace_softirq_entry(h, softirq_vec);  
  38.             h->action(h);/*执行注册的具体的软中断函数*/  
  39.             trace_softirq_exit(h, softirq_vec);  
  40.             if (unlikely(prev_count != preempt_count())) {  
  41.                 printk(KERN_ERR "huh, entered softirq %td %s %p"  
  42.                        "with preempt_count %08x,"  
  43.                        " exited with %08x?\n", h - softirq_vec,  
  44.                        softirq_to_name[h - softirq_vec],  
  45.                        h->action, prev_count, preempt_count());  
  46.                 preempt_count() = prev_count;  
  47.             }  
  48.   
  49.             rcu_bh_qs(cpu);  
  50.         }  
  51.         h++;  
  52.         pending >>= 1;  
  53.     } while (pending);  
  54.   
  55.     local_irq_disable();  
  56.   
  57.     pending = local_softirq_pending();  
  58.     if (pending && --max_restart)  
  59.         goto restart;  
  60.   
  61.     if (pending)/*如果还有挂起的软中断,唤醒内核线程 
  62.             来处理本地CPU的软中断*/  
  63.         wakeup_softirqd();  
  64.   
  65.     lockdep_softirq_exit();  
  66.   
  67.     account_system_vtime(current);  
  68.     _local_bh_enable();/*软中断计数器-1,因而重新激活可延迟函数*/  
  69. }  

到此,linux内核软中断的大致执行和实现基本上分析完了,中间有很多地方没有注释的,主要是考虑到需要别的实现机制以及有的比较易懂。能够自己看懂。

linux
相关资讯       Linux编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款