linux内核设计与实现读书笔记 第六章

什么是中断

A better solution is to provide a mechanism for the hardware to signal to the kernel when attention is needed.This mechanism is called an interrupt.

中断实施的物理过程

An interrupt is physically produced by electronic signals originating from hardware devices and directed into input pins on an interrupt controller, a simple chip that multi-plexes multiple interrupt lines into a single line to the processor. Upon receiving an inter- rupt, the interrupt controller sends a signal to the processor.The processor detects this sig- nal and interrupts its current execution to handle the interrupt.The processor can then notify the operating system that an interrupt has occurred, and the operating system can handle the interrupt appropriately.

 Different devices can be associated with different interrupts by means of a unique value associated with each interrupt. In turn, the operating system can service each interrupt with its corresponding handler.

异常

In OS texts, exceptions are often discussed at the same time as interrupts. Unlike inter- rupts, exceptions occur synchronously with respect to the processor clock. Indeed, they are often called synchronous interrupts. Exceptions are produced by the processor while execut- ing instructions either in response to a programming error (for example, divide by zero) or abnormal conditions that must be handled by the kernel (for example, a page fault). Because many processor architectures handle exceptions in a similar manner to interrupts, the kernel infrastructure for handling the two is similar. Much of the discussion of interrupts (asynchronous interrupts generated by hardware) in this chapter also pertains to exceptions (synchronous interrupts generated by the processor).

You are already familiar with one exception: In the previous chapter, you saw how system calls on the x86 architecture are implemented by the issuance of a software interrupt, which traps into the kernel and causes execution of a special system call handler. Inter- rupts work in a similar way, you will see, except hardware—not software—issues interrupts.

Interrupt Handlers

The function the kernel runs in response to a specific interrupt is called an interrupt handler or interrupt service routine (ISR).

What differentiates interrupt handlers from other kernel func- tions is that the kernel invokes them in response to interrupts and that they run in a spe- cial context (discussed later in this chapter) called interrupt context.This special context is occasionally called atomic context because, as we shall see, code executing in this context is unable to block.

At the very least, an interrupt handler’s job is to acknowledge the interrupt’s receipt to the hardware. Often, however, interrupt han- dlers have a large amount of work to perform.

Top Halves Versus Bottom Halves

These two goals—that an interrupt handler execute quickly and perform a large amount of work—clearly conflict with one another.

The interrupt handler is the top half.

Work that can be performed later is deferred until the bottom half.

Registering an Interrupt Handler

int request_irq(unsigned int irq,  -- 中断编号,唯一性

irq_handler_t handler,  -- 回调函数

unsigned long flags, -- (是否允许其他中断同时发生、是否对随机值贡献、是否公用同一个中断编号)

const char *name,  -- 设备名称(交互时使用)

void *dev)  -- 用于区分公用中断信号的设备识别

Writing an Interrupt Handler

中断处理函数

static irqreturn_t intr_handler(int irq, void *dev)

The role of the interrupt handler depends entirely on the device and its reasons for issuing the interrupt. At a minimum, most interrupt handlers need to provide acknowl- edgment to the device that they received the interrupt. Devices that are more complex need to additionally send and receive data and perform extended work in the interrupt handler. As mentioned, the extended work is pushed as much as possible into the bottom half handler, which is discussed in the next chapter.

一次完成的中断处理流程

linux内核设计与实现读书笔记 第六章_第1张图片

中断控制

These interfaces enable you to disable the interrupt system for the current processor or mask out an interrupt line for the entire machine.

你可能感兴趣的:(操作系统)