陷阱和中断有什么区别?
陷阱和中断有什么区别?
如果不同系统的术语不同,那么它们在 x86 上意味着什么?
What is the difference between Trap and Interrupt?
If the terminology is different for different systems, then what do they mean on x86?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
陷阱是用户进程中的异常。这是由除以零或无效的内存访问引起的。这也是调用内核例程(系统调用)的常用方法,因为它们以更高的运行频率运行。优先于用户代码。处理是同步的(因此用户代码被挂起并随后继续)。从某种意义上说,它们是“活跃的”——大多数时候,代码预计陷阱会发生并依赖于这一事实。
中断是由硬件(例如硬盘等设备)生成的、显卡、I/O 端口等)。这些是异步的(即它们不会发生在用户代码中可预测的位置)或“被动”,因为中断处理程序必须等待它们最终发生。
您还可以将陷阱视为一种 CPU 内部中断,因为陷阱处理程序的处理程序看起来像中断处理程序(保存寄存器和堆栈指针,有上下文切换,在某些情况下可以从中断处恢复执行) 。
A trap is an exception in a user process. It's caused by division by zero or invalid memory access. It's also the usual way to invoke a kernel routine (a system call) because those run with a higher priority than user code. Handling is synchronous (so the user code is suspended and continues afterwards). In a sense they are "active" - most of the time, the code expects the trap to happen and relies on this fact.
An interrupt is something generated by the hardware (devices like the hard disk, graphics card, I/O ports, etc). These are asynchronous (i.e. they don't happen at predictable places in the user code) or "passive" since the interrupt handler has to wait for them to happen eventually.
You can also see a trap as a kind of CPU-internal interrupt since the handler for trap handler looks like an interrupt handler (registers and stack pointers are saved, there is a context switch, execution can resume in some cases where it left off).
陷阱和中断密切相关。陷阱是一种异常,异常类似于中断。
Intel x86 定义了两个重叠的类别,向量事件(中断与异常)和异常类(故障与陷阱)与中止)。
本文中的所有引用均来自 2016 年 4 月版的 英特尔软件开发人员手册。对于(明确且复杂的)x86 视角,我建议阅读 SDM 有关中断和异常处理的章节。
向量事件
向量事件(中断和异常)会导致处理器在保存大部分处理器状态后跳转到中断处理程序(足以使执行稍后可以从该点继续) )。
异常和中断有一个 ID,称为向量,它确定处理器跳转到哪个中断处理程序。中断处理程序在中断描述符表中描述。
中断
例外情况
异常分类
摘要:陷阱会增加指令指针,故障则不会,而中止会“爆炸”。
陷阱
过错
示例:页面错误通常是可以恢复的。应用程序地址空间的一部分可能已从 RAM 换出到磁盘。当应用程序尝试访问已换出的内存时,将触发页面错误。内核可以将该内存从磁盘拉到内存,并将控制权交还给应用程序。应用程序将从中断处继续(在访问换出内存的错误指令处),但这次内存访问应该成功且不会出现错误。
模拟浮点或其他缺失指令的非法指令故障处理程序在查看故障指令是否是它可以处理的指令之后,必须手动增加返回地址以获得所需的类似陷阱的行为。 x86
#UD
是“错误”,而不是“陷阱”。 程序需要一个指向错误指令的指针来确定它是哪条指令。)(处理
边缘情况
软件调用的中断(由 INT 指令触发)以类似陷阱的方式运行。该指令在处理器保存其状态并跳转到中断处理程序之前完成。
Traps and interrupts are closely related. Traps are a type of exception, and exceptions are similar to interrupts.
Intel x86 defines two overlapping categories, vectored events (interrupts vs exceptions), and exception classes (faults vs traps vs aborts).
All of the quotes in this post are from the April 2016 version of the Intel Software Developer Manual. For the (definitive and complex) x86 perspective, I recommend reading the SDM's chapter on Interrupt and Exception handling.
Vectored Events
Vectored Events (interrupts and exceptions) cause the processor to jump into an interrupt handler after saving much of the processor's state (enough such that execution can continue from that point later).
Exceptions and interrupts have an ID, called a vector, that determines which interrupt handler the processor jumps to. Interrupt handlers are described within the Interrupt Descriptor Table.
Interrupts
Exceptions
Exception Classifications
Summary: traps increment the instruction pointer, faults do not, and aborts 'explode'.
Trap
Fault
Example: A page fault is often recoverable. A piece of an application's address space may have been swapped out to disk from ram. The application will trigger a page fault when it tries to access memory that was swapped out. The kernel can pull that memory from disk to ram, and hand control back to the application. The application will continue where it left off (at the faulting instruction that was accessing swapped out memory), but this time the memory access should succeed without faulting.
An illegal-instruction fault handler that emulates floating-point or other missing instructions would have to manually increment the return address to get the trap-like behaviour it needs, after seeing if the faulting instruction was one it could handle. x86
#UD
is a "fault", not a "trap". (The handler would need a pointer to the faulting instruction to figure out which instruction it was.)Abort
Edge Cases
Software invoked interrupts (triggered by the INT instruction) behave in a trap-like manner. The instruction completes before the processor saves its state and jumps to the interrupt handler.
陷阱是一种特殊的中断,通常称为软件中断。 中断是一个更通用的术语,涵盖硬件中断(来自硬件设备的中断)和软件中断(来自软件的中断,例如< em>陷阱)。
A trap is a special kind of interrupt which is commonly referred to as a software interrupt. An interrupt is a more general term which covers both hardware interrupts (interrupts from hardware devices) and software interrupts (interrupts from software, such as traps).
一般来说,异常、故障、中止、陷阱和中断等术语都具有相同的含义,统称为“中断”。
来看看陷阱和中断之间的区别:
(例如
Generally speaking, terms like exceptions, faults, aborts, Traps, and Interrupts all mean the same thing and are called "Interrupts".
Coming to the difference between Trap and Interrupt:
Where as
陷阱由类似程序的代码调用,并用于例如调用操作系统例程(即通常同步)。
中断由事件(很多时候是硬件,例如接收到数据的网卡或 CPU 定时器)调用,并且 - 顾名思义 - 中断正常的控制流,因为 CPU 必须切换到驱动程序例程来处理事件。
A trap is called by code like programs and used e. g. to call OS routines (i. e. normally synchronous).
An interrupt is called by events (many times hardware, like the network card having received data, or the CPU timer), and - as the name suggests - interrupts the normal control flow, as the CPU has to switch to the driver routine to handle the event.
我认为陷阱是由当前指令的执行引起的,因此它们被称为同步事件。其中中断是由处理器中运行的独立指令引起的,该指令与外部事件相关,因此被称为异步中断。
I think Traps are caused by the execution of current instruction and thus they are called as synchronous events. where as interrupts are caused by an independent instruction that is running in the processor which are related to external events and thus are known as asynchronous ones.
中断是硬件中断,而陷阱是软件调用的中断。硬件中断的发生通常会禁用其他硬件中断,但对于陷阱则不然。如果您需要在陷阱被服务之前不允许硬件中断,则需要显式清除中断标志。通常计算机上的中断标志影响(硬件)中断而不是陷阱。这意味着清除该标志不会阻止陷阱。与陷阱不同,中断应保留 CPU 先前的状态。
Interrupts are hardware interrupts, while traps are software-invoked interrupts. Occurrences of hardware interrupts usually disable other hardware interrupts, but this is not true for traps. If you need to disallow hardware interrupts until a trap is served, you need to explicitly clear the interrupt flag. And usually the interrupt flag on the computer affects (hardware) interrupts as opposed to traps. This means that clearing this flag will not prevent traps. Unlike traps, interrupts should preserve the previous state of the CPU.
陷阱是一种软件中断。如果您编写一个程序,其中声明一个除以零值的变量,那么它将被视为陷阱。每当您运行该程序时,它都会同时抛出相同的错误。系统调用是一个陷阱的特殊版本,其中程序向操作系统请求其所需的服务。
如果出现中断(硬件中断的通称),例如 I/O 错误,CPU 会随机中断,当然这不是我们程序员的错。而是硬件导致了它们。
A trap is a software interrupt.If you write a program in which you declare a variable having divide by zero value then it is treated as a trap.Whenever you run this program it will throw same error at the same time.System call is a special version of trap in which a program asks os for its required service.
In case of interrupt(a general word for hardware interrupts)like an i/o error,the cpu is interrupted at random time and off course it is not the fault of our programmers.It is the hardware that brings them up.
中断是系统内由硬件生成的流程变更。一个中断
调用handler来处理中断原因;然后控制权返回到
中断上下文和指令。陷阱是软件生成的中断。一个中断可以
用于指示 I/O 完成,以避免设备轮询的需要。陷阱可以是
用于调用操作系统例程或捕获算术错误。
An interrupt is a hardware-generated change-of-flow within the system. An interrupt
handler is summoned to deal with the cause of the interrupt; control is then returned to the
interrupted context and instruction. A trap is a software-generated interrupt. An interrupt can
be used to signal the completion of an I/O to obviate the need for device polling. A trap can be
used to call operating system routines or to catch arithmetic errors.
陷阱可以被识别为由程序员发起的控制转移。术语“陷阱”与术语“异常”(异常是自动发生的软件中断)可互换使用。但有些人可能会认为陷阱只是一个特殊的子例程调用。因此它们属于软件调用中断的范畴。例如,在80×86机器中,程序员可以使用int指令来启动陷阱。由于陷阱始终是无条件的,因此控制将始终转移到与陷阱关联的子例程。调用处理陷阱的例程的确切指令很容易识别,因为使用显式指令来指定陷阱。 陷阱与中断
A Trap can be identified as a transfer of control, which is initiated by the programmer. The term Trap is used interchangeably with the term Exception (which is an automatically occurring software interrupt). But some may argue that a trap is simply a special subroutine call. So they fall in to the category of software-invoked interrupts. For example, in 80×86 machines, a programmer can use the int instruction to initiate a trap. Because a trap is always unconditional the control will always be transferred to the subroutine associated with the trap. The exact instruction, which invokes the routine for handling the trap is easily identified because an explicit instruction is used to specify a trap. Trap Vs Interrupt