- We use the term exception to refer to an unusual condition occurring at run time associated with an instruction in the current RISC-V hart.
- We use the term interrupt to refer to an external asynchronous event that may cause a RISC-V hart to experience an unexpected transfer of control.
- We use the term trap to refer to the transfer of control to a trap handler caused by either an exception or an interrupt;
大部分的异常都会产生trap, except for floating-point exceptions, which, in the standard floating-point extensions, do not cause traps
计算机有两种运行模式:用户态, 内核态。
- 其中操作系统运行在内核态,在内核态中,操作系统具有对所有硬件的完全访问权限,可以使机器运行任何指令;
- 相反,用户程序运行在用户态,在用户态下,软件只能使用少数指令,它们并不具备直接访问硬件的权限。
- 这就出现了问题:
- 所以:用户空间和内核空间的切换通常被称为trap;
- trap的主要应用是在调试中;
异常
异常是由当前正在执行的进程产生。异常包括很多方面,有出错(fault),有陷入(trap),也有可编程异常(programmable exception)。
- 出错(fault)和陷入(trap)最重要的一点区别是他们发生时所保存的EIP值的不同。
- 出错(fault)保存的EIP指向触发异常的那条指令;
- 而陷入(trap)保存的EIP指向触发异常的那条指令的下一条指令。
- 因此,当从异常返回时,出错(fault)会重新执行那条指令;而陷入(trap)就不会重新执行。
中断和陷阱的主要区别:
- 陷阱通常由处理机正在执行的现行指令引起,而中断则是由与现行指令无关的中断源引起的。
- 陷阱处理程序提供的服务为当前进程所用,而中断处理程序提供的服务则不是为了当前进程的。
- CPU 在执行完一条指令之后,下一条指令开始之前响应中断,而在一条指令执行中也可以响应陷阱。