Linux设备驱动程序学习笔记12:中断调用流程

通过轮询的方式去查询各个硬件的状态显得有的低效,一种更好的机制是当硬件状态准备好之后能够主动地报告给CPU。中断就是这样的一种机制,它允许硬件发信号给CPU。

下面就先分析一下当一个中断发生时,Linux系统的处理流程。

首CPU在收到中断线上的中断信号后,会跳到某个固定的地址去执行代码,这个地址被称为中断向量。这部分代码是同体系结构相关,主要是汇编代码,其中的主要工作有保存中断现场,切换到中断上下文中,获取中断号,调用Linux内核中断子系统来对中断做进一步的响应。

经过上面的同体系结构相关的一些处理之后,会调用asm_do_IRQ()函数,进入这个函数就进入了C语言的代码中了,我们可以把它做Linux处理中断的入口函数:

//irq.c
/*
 * do_IRQ handles all hardware IRQ's.  Decoded IRQs should not
 * come via this function.  Instead, they should provide their
 * own 'handler'
 */
asmlinkage void __exception asm_do_IRQ(unsigned int irq, struct pt_regs *regs)
{
	struct pt_regs *old_regs = set_irq_regs(regs);

	irq_enter();

	/*
	 * Some hardware gives randomly wrong interrupts.  Rather
	 * than crashing, do something sensible.
	 */
	if (unlikely(irq >= NR_IRQS)) {
		if (printk_ratelimit())
			printk(KERN_WARNING "Bad IRQ%u\n", irq);
		ack_bad_irq(irq);
	} else {
		generic_handle_irq(irq);
	}

	/* AT91 specific workaround */
	irq_finish(irq);

	irq_exit();
	set_irq_regs(old_regs);
}

在asm_do_IRQ()中经过下面的一系列中间调用generic_handle_irq(irq))-> __do_IRQ(irq) 最终会调用到handle_IRQ_event() 函数:

/**
 * handle_IRQ_event - irq action chain handler
 * @irq:	the interrupt number
 * @action:	the interrupt action chain for this irq
 *
 * Handles the action chain of an irq event
 */
irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
{
	irqreturn_t ret, retval = IRQ_NONE;
	unsigned int flags = 0;

	if (!(action->flags & IRQF_DISABLED))
		local_irq_enable_in_hardirq();

	do {
		trace_irq_handler_entry(irq, action);
		ret = action->handler(irq, action->dev_id);
		trace_irq_handler_exit(irq, action, ret);

		switch (ret) {
		case IRQ_WAKE_THREAD:
			/*
			 * Set result to handled so the spurious check
			 * does not trigger.
			 */
			ret = IRQ_HANDLED;

			/*
			 * Catch drivers which return WAKE_THREAD but
			 * did not set up a thread function
			 */
			if (unlikely(!action->thread_fn)) {
				warn_no_thread(irq, action);
				break;
			}

			/*
			 * Wake up the handler thread for this
			 * action. In case the thread crashed and was
			 * killed we just pretend that we handled the
			 * interrupt. The hardirq handler above has
			 * disabled the device interrupt, so no irq
			 * storm is lurking.
			 */
			if (likely(!test_bit(IRQTF_DIED,
					     &action->thread_flags))) {
				set_bit(IRQTF_RUNTHREAD, &action->thread_flags);
				wake_up_process(action->thread);
			}

			/* Fall through to add to randomness */
		case IRQ_HANDLED:
			flags |= action->flags;
			break;

		default:
			break;
		}

		retval |= ret;
		action = action->next;
	} while (action);

	add_interrupt_randomness(irq, flags);
	local_irq_disable();

	return retval;
}

而在handle_IRQ_event函数中时通过下面的语句来调用我们在驱动程序中注册的中断处理函数的:

		ret = action->handler(irq, action->dev_id);

中断的注册流程,我们会在后面的文章中介绍。





你可能感兴趣的:(Linux)