Interrupts and Exceptions

由于阅读内核时经常遇到各种各样的锁,以前没去太在意它们,终于忍不住了,决心把内核中的锁搞清楚。 为了使并发程度达到最大,不同的情况下需要使用不同的锁,在了解各种锁之前,需要先搞清两个很容易混淆的概念,国内很多书籍对这两个概念是不区分的,其实,在内核中,对它们的处理是有不同的,其中很大一个不同就是运行所对应的handler时中断是否关闭,所以,又由此引出了不同锁的使用。 废话不说,上原文,配合自己写的理解。 以下出自ULK The Intel documentation classifies interrupts and exceptions as follows:(ULK中这部分文字出自Intel的文档,应该算是最官方的了吧) Interrupts:(先来看中断) Maskable interrupts(可屏蔽中断) All Interrupt Requests (IRQs) issued by I/O devices give rise to maskable interrupts . A maskable interrupt can be in two states: masked or unmasked; a masked interrupt is ignored by the control unit as long as it remains masked.(所有的IRQ中断,也就是“issued by I/O devices“的中断,都是可屏蔽的) Nonmaskable interrupts(非可屏蔽中断) Only a few critical events (such as hardware failures) give rise to nonmaskable interrupts . Nonmaskable interrupts are always recognized by the CPU.(仅仅有很少一部分"critical events"是不允许屏蔽的) Exceptions:(再来看异常) Processor-detected exceptions(异常也分两大类,第一种是“processor-detected",也就是直接被cpu侦测到的。) Generated when the CPU detects an anomalous condition while executing an instruction. These are further divided into three groups, depending on the value of the eip register that is saved on the Kernel Mode stack when the CPU control unit raises the exception.(根据处理异常时内核栈中存放的eip指针的不同,这大类异常还可以分为三小类) Faults(Fault,最熟悉不过了) Can generally be corrected; once corrected, the program is allowed to restart with no loss of continuity. The saved value of eip is the address of the instruction that caused the fault, and hence that instruction can be resumed when the exception handler terminates. As we'll see in the section "Page Fault Exception Handler" in Chapter 9, resuming the same instruction is necessary whenever the handler is able to correct the anomalous condition that caused the exception.(在处理这种异常之前,会将当前指令(而不是下一条指令:)的地址压入内核栈,处理完后,重新执行这条指令。最典型的例子就是"page fault exception",所以,国内大部分书籍对此将其解释成“缺页中断”是不准确的,就像毛老师书中所说,应该是“缺页异常”,在产生异常后,处理完后回来会继续执行产生异常的那条指令,而且要注意,微机原理中所讲的,几个不可内部“中断"(如除数为零等)其实不是中断,应该是这里这个fault异常) Traps(陷阱) Reported immediately following the execution of the trapping instruction; after the kernel returns control to the program, it is allowed to continue its execution with no loss of continuity. The saved value of eip is the address of the instruction that should be executed after the one that caused the trap. A trap is triggered only when there is no need to reexecute the instruction that terminated. The main use of traps is for debugging purposes. The role of the interrupt signal in this case is to notify the debugger that a specific instruction has been executed (for instance, a breakpoint has been reached within a program). Once the user has examined the data provided by the debugger, she may ask that execution of the debugged program resume, starting from the next instruction.(这和处理fault时不同,因为处理之前,会将当前指令的下一条指令的地址压入内核栈,处理完后,执行的是下一条指令,果然像一个陷阱,爬出来后走后面的路,而不是再像以前一样,再掉入陷阱一次。文档中说陷阱的主要使用是在debugger中,例如编译器的断点设置) Aborts(这种就更少见了吧,中止) A serious error occurred; the control unit is in trouble, and it may be unable to store in the eip register the precise location of the instruction causing the exception. Aborts are used to report severe errors, such as hardware failures and invalid or inconsistent values in system tables. The interrupt signal sent by the control unit is an emergency signal used to switch control to the corresponding abort exception handler. This handler has no choice but to force the affected process to terminate.(说是发生了serious error,以至于可能根本不能将发生异常的指令地址压入内核栈。发生这种异常后,会产生"an emergency singnal",其handler的处理结果是”has no choice but to force the affected process to terminate",看到这里,想起来了内存越界访问时(比如访问3G以上的虚拟地址,毛老师书中出现过的,会发送一个SIGSEGV信号,对这个信号的处理结果便是结束进程,并打印“segment fault") Programmed exceptions(以上三种异常都属于“processor-detected",这种就不一样了) Occur at the request of the programmer. They are triggered by int or int3 instructions; the into (check for overflow) and bound (check on address bound) instructions also give rise to a programmed exception when the condition they are checking is not true. Programmed exceptions are handled by the control unit as traps; they are often called software interrupts . Such exceptions have two common uses: to implement system calls and to notify a debugger of a specific event (see Chapter 10).(这种异常是编程产生的,也就是经常所说的“软中断”,一般由int 或int3指令触发,在linux中比较常见的就是int80了,进入系统调用时必需用到的指令,查了下,指令在运行级别为3的时候便可以运行,所以用户态程序完全可以调用) Each interrupt or exception is identified by a number ranging from 0 to 255; Intel calls this 8-bit unsigned number a vector. The vectors of nonmaskable interrupts and exceptions are fixed, while those of maskable interrupts can be altered by programming the Interrupt Controller (see the next section).
原文地址:https://www.cnblogs.com/yangce/p/2910085.html