中断和异常

来源:互联网 发布:vba连接oracle数据库 编辑:程序博客网 时间:2024/04/30 14:57

1.中断和异常的概念

以上定义摘自Intel官方文档80x86编程手册第三卷第五章Interupt and Exception Handling。从中可以看出中断来自外围设备(peripheral devices,这里指除了CPU)的通知(signals from hardware),而异常则是CPU在执行指令时遇到错误的条件(error condition, 我认为这里的错误的条件实际上就是错误的操作数(error operand)),所以说中断是由外围设备产生的,而异常是由CPU自身产生的(在执行指令时)。

注意:上文中有这样一句话:Software can also generate interrupts by executing the INT n instruction.表明软件也可以生成中断,也就是说我们的编写的程序也可以通过INT n指令来生成中断,事实上 CPU 将一些常用的功能以中断处理器(handler)形式提供给我们,作为我们的程序与 CPU 某些特殊功能的调用接口(在高级语言编程中,我们的程序直接调用API函数,从而使用系统提供给我们的功能,这和中断处理器类似),所以当我们在程序中使用中断时,就可以认为该中断就是一次系统的函数调用(实际上是CPU内部功能的调用)。

 

2.中断和异常的通用处理机制

从以上的描述可以看出processor对于中断和异常的处理过程基本一致,分为以下三个过程

a. processor挂起当前运行的过程或任务

b. processor执行中断或异常的处理器(handler)

c. handler执行完毕之后,processor唤醒要执行的过程或任务

注意截图中红色划线的句子,这句话说明了步骤c应该如何决断:如果唤醒被挂起的过程或任务之后不破坏程序的连续性(程序逻辑的正确性),则processor应该将被挂起的过程或任务唤醒,否则处理器(handler)被执行以后异常就不应该被恢复,而中断就应该将当前运行的程序终止。

3.中断和异常产生的来源

3.1中断的来源:

3.2异常的来源:

4.异常的分类

Exceptions are classified asfaults, traps, or aborts depending on the way they are reported and

whether the instruction that caused the exception can be restarted with no loss of program or task

continuity.

 

Faults A fault is an exception that can generally be corrected and that, once corrected,

allows the program to be restarted with no loss of continuity. When a fault is

reported, the processor restores the machine state to the state prior to the beginning

of execution of the faulting instruction. The return address (saved contents

of the CS and EIP registers) for the fault handler points to the faulting instruction,

rather than the instruction following the faulting instruction.

Note: There are a small subset of exceptions that are normally reported as

faults, but under architectural corner cases, they are not restartable and some

processor context will be lost. An example of these cases is the execution of the

POPAD instruction where the stack frame crosses over the the end of the stack

segment. The exception handler will see that the CS:EIP has been restored as

if the POPAD instruction had not executed however internal processor state

(general purpose registers) will have been modified. These corner cases are

considered programming errors and an application causeing this class of

exceptions will likely be terminated by the operating system.

 

Traps A trap is an exception that is reported immediately following the execution of

the trapping instruction. Traps allow execution of a program or task to be

continued without loss of program continuity. The return address for the trap

handler points to the instruction to be executed after the trapping instruction.

 

Aborts An abort is an exception that does not always report the precise location of the

instruction causing the exception and does not allow restart of the program or

task that caused the exception. Aborts are used to report severe errors, such as

hardware errors and inconsistent or illegal values in system tables

我们在操作系统的内存管理中常常看到的术语page fault,应该就是属于这三种异常中的第一种:Fault。从红色的语句中我们可以看出,page fault是可以恢复的,事实上,对于操作系统的内存管理来说page fault必须是可以恢复的,不然,操作系统的虚拟地址空间机制就没有办法实现了。

5.中断和异常的处理之后(执行了handler)的指令执行策略

For fault-class exceptions,the return instruction pointer that the processor saves when it generates

the exception points to the faulting instruction. So, when a program or task is restarted

following the handling of a fault, the faulting instruction is restarted (re-executed). Restarting

the faulting instruction is commonly used to handle exceptions that are generated when access

to an operand is blocked. The most common example of a fault is a page-fault exception (#PF)

that occurs when a program or task references an operand in a page that is not in memory. When

a page-fault exception occurs, the exception handler can load the page into memory and resume

execution of the program or task by restarting the faulting instruction. To insure that this instruction

restart is handled transparently to the currently executing program or task, the processor

saves the necessary registers and stack pointers to allow it to restore itself to its state prior to the

execution of the faulting instruction.

 

For trap-class exceptions,the return instruction pointer points to the instruction following the

trapping instruction. If a trap is detected during an instruction which transfers execution, the

return instruction pointer reflects the transfer. For example, if a trap is detected while executing

a JMP instruction, the return instruction pointer points to the destination of the JMP instruction,

not to the next address past the JMP instruction. All trap exceptions allow program or task restart

with no loss of continuity. For example, the overflow exception is a trapping exception. Here,

the return instruction pointer points to the instruction following the INTO instruction that tested

the OF (overflow) flag in the EFLAGS register. The trap handler for this exception resolves the

overflow condition. Upon return from the trap handler, program or task execution continues at

the next instruction following the INTO instruction.

 

The abort-class exceptions do not support reliable restarting of the program or task. Abort

handlers generally are designed to collect diagnostic information about the state of the processor

when the abort exception occurred and then shut down the application and system as gracefully

as possible.

Interrupts rigorously(严格的) support restarting of interrupted programs and tasks without loss of continuity.

The return instruction pointer saved for an interrupt points to the next instruction to be

executed at the instruction boundary where the processor took the interrupt. If the instruction

just executed has a repeat prefix, the interrupt is taken at the end of the current iteration with the

registers set to execute the next iteration.

上文中的红色语句说明了执行处理器(handler)之后,代码该如何执行。其中abort-class exception 的异常不支持restart,所以发生该类异常时program or task一般就会退出或终止。

 

6.异常和中断是可以被processor通过设置EFLAGS标志寄存器来屏蔽的。

MOV SS, AX

MOV ESP, StackTop

例如以上指令在执行期间是不应该被中断的,所以在执行MOV SS, AX时要屏蔽中断和异常。

 

7.中断和异常是有预定义的优先级的,所以当出现并发的中断和异常时,processor通常根据其优先级来执行相应的处理器(handler)

 

8.IDTR寄存器和IDT(中断向量表)

9.汇编语言中的中断调用,以及CPU中与DOS,BIOS,以及外设(如:输入设备:鼠标,键盘,输出设备:显示器,打印机等)的交互(功能调用)都是通过中断机制来实现的。

原创粉丝点击