我们由Linux Kernel中断子系统来龙去脉浅析中可以知道Linux Kernel在处理完一个interrupt后就可能会检查是否有sortirq,如果有,且不在interrupt context中,那么就执行softirq,这也是我们在第5章中断子系统中分析中断的处理全过程的时候未能完成分析的部分,本章将继往开来,将softirq的来龙去脉分析清楚。我们现在再此全面看看asm_do_IRQ到此做了什么:
1. 调用irq_enter处理开始处理该中断的准备工作,其核心的部分在于irq_enter=>__irq_enter=> add_preempt_count(HARDIRQ_OFFSET);,简单来说就是记录下Linux Kernel已经在处理interrupt中。
2. asm_do_IRQàdesc_handle_irq=>desc->handle_irq(irq, desc);真正处理中断。这里handle_irq是handle_level_irq或handle_edge_irq,但是无论是哪一个最后都要调用handle_IRQ_event来处理中断,handle_IRQ_event在处理完中断后退出前会调用local_irq_disable();关闭ARM global interrupt
3. 调用irq_exit做完成中断处理的善后工作,通过sub_preempt_count(IRQ_EXIT_OFFSET);(注:IRQ_EXIT_OFFSET等于HARDIRQ_OFFSET),这是前面的反向操作。此外,我们需要重点关注的是:
if (!in_interrupt() && local_softirq_pending())
invoke_softirq();
就是说如果我们不是在interrtupt处理中或是softirq的处理中,且有softirq pending,那么我们就是开始处理softirq。
我们通常将softirq叫做interrupt handling的bottom half,之前的interrupt handling、也就是我们在Linux Kernel中断子系统来龙去脉浅析中断子系统中详细分析的叫做top half。
本章节我们的任务首先就是从invoke_softirq出发搞清楚softirq和tasklet的来龙去脉,并且讨论在设计某个具体的device driver时如何使用softirq以及相关的注意事项。但是再分析这个之前我们有必要先仔细解释一下上面我们看到的如:add_preempt_count,sub_preempt_count,in_interrupt是什么意思,否则我们无法理解asm_do_IRQ这段code。
Linux Kernel通过struct task_struct管理每个process相关information、状态等,而其中的一些low level的process information记录在struct thread_info中,从struct thread_info下的task pointer可以找到该process的struct task_struct。从struct task_struct中的stack(stack pointer register)也可以找到该struct thread_info。
每个process运行在Kernel Mode时,其stack有8K bytes,the bottom of stack是这个8K bytes的高地址,the top of the stack存储了该process的struct thread_info,所以Kernel Mode下每个process的stack最大只能是8K bytes- sizeof(struct thread_info),详见如下:
union thread_union {
struct thread_info thread_info;
unsigned long stack[THREAD_SIZE/sizeof(long)];
};
在arm linux中(include/asm-arm/thread_info.h):#define THREAD_SIZE 8192
1.1.1 当前process:current macro
这个是我们设计device driver时经常使用到的,但是有没有分析过其current的来龙去脉呢?
include/asm-arm/current.h:
static inline struct task_struct *get_current(void) __attribute_const__;
static inline struct task_struct *get_current(void)
{
return current_thread_info()->task;
}
#define current (get_current())
include/asm-arm/thread_info.h:
static inline struct thread_info *current_thread_info(void)
{
register unsigned long sp asm ("sp"); //sp就是stack pointer register。
return (struct thread_info *)(sp & ~(THREAD_SIZE - 1));
}
1.1.3 preempt_count以及add_preempt_count和sub_preempt_count
include/linux/preempt.h:
#define add_preempt_count(val) do { preempt_count() += (val); } while (0)
#define sub_preempt_count(val) do { preempt_count() -= (val); } while (0)
#define preempt_count() (current_thread_info()->preempt_count)