陷阱和中断有什么区别?

发布于 2024-09-07 06:32:12 字数 56 浏览 7 评论 0原文

陷阱和中断有什么区别?

如果不同系统的术语不同,那么它们在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

刘备忘录 2024-09-14 06:32:12

陷阱是用户进程中的异常。这是由除以零或无效的内存访问引起的。这也是调用内核例程(系统调用)的常用方法,因为它们以更高的运行频率运行。优先于用户代码。处理是同步的(因此用户代码被挂起并随后继续)。从某种意义上说,它们是“活跃的”——大多数时候,代码预计陷阱会发生并依赖于这一事实。

中断是由硬件(例如硬盘等设备)生成的、显卡、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).

错爱 2024-09-14 06:32:12

陷阱中断密切相关。陷阱是一种异常,异常类似于中断。

Intel x86 定义了两个重叠的类别,向量事件(中断异常)和异常类(故障陷阱)与中止)。

本文中的所有引用均来自 2016 年 4 月版的 英特尔软件开发人员手册。对于(明确且复杂的)x86 视角,我建议阅读 SDM 有关中断和异常处理的章节。

向量事件

向量事件(中断异常)会导致处理器在保存大部分处理器状态后跳转到中断处理程序(足以使执行稍后可以从该点继续) )。

异常和中断有一个 ID,称为向量,它确定处理器跳转到哪个中断处理程序。中断处理程序在中断描述符表中描述。

中断

中断在程序执行期间随机发生,以响应来自硬件的信号。系统硬件用途
中断来处理处理器外部的事件,例如
请求服务外围设备。软件还可以生成
通过执行 INT n 指令来中断。

例外情况

异常在处理器检测到错误情况时发生
执行指令,例如除以零。处理器
检测各种错误情况,包括保护违规,
页面错误和内部机器错误。

异常分类

异常分为故障陷阱中止,具体取决于报告方式以及是否指令
导致异常的可以重新启动而不会丢失程序或
任务连续性。

摘要:陷阱会增加指令指针,故障则不会,而中止会“爆炸”。

陷阱

陷阱是紧随其后报告的异常
执行捕获指令。陷阱允许执行
计划或任务在不丧失计划连续性的情况下继续进行。
陷阱处理程序的返回地址指向指令
在捕获指令之后执行。

过错

故障是一种通常可以纠正的异常,并且,
一旦更正,允许程序重新启动而不会丢失
连续性。当报告故障时,处理器恢复
机器状态到开始执行之前的状态
错误的指令。返回地址(CS和保存的内容
EIP 寄存器)用于故障处理程序指向故障
指令,而不是故障后的指令
说明。

示例:页面错误通常是可以恢复的。应用程序地址空间的一部分可能已从 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

Interrupts occur at random times during the execution of a program, in response to signals from hardware. System hardware uses
interrupts to handle events external to the processor, such as
requests to service peripheral devices. Software can also generate
interrupts by executing the INT n instruction.

Exceptions

Exceptions occur when the processor detects an error condition while
executing an instruction, such as division by zero. The processor
detects a variety of error conditions including protection violations,
page faults, and internal machine faults.

Exception Classifications

Exceptions are classified as faults, traps, or aborts depending on the way they are reported and whether the instruction
that caused the exception can be restarted without loss of program or
task continuity.

Summary: traps increment the instruction pointer, faults do not, and aborts 'explode'.

Trap

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.

Fault

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 to the instruction following the faulting
instruction.

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

An abort is an exception that does not always report the precise
location of the instruction causing the exception and does not allow a
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.

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.

乖乖兔^ω^ 2024-09-14 06:32:12

陷阱是一种特殊的中断,通常称为软件中断中断是一个更通用的术语,涵盖硬件中断(来自硬件设备的中断)和软件中断(来自软件的中断,例如< 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).

逐鹿 2024-09-14 06:32:12

一般来说,异常、故障、中止、陷阱中断等术语都具有相同的含义,统称为“中断”。

来看看陷阱和中断之间的区别:

陷阱:是程序员发起并期望将控制权转移到特殊处理程序例程。 (例如:80x86 INT 指令就是一个很好的例子)

(例如

中断(硬件):是基于CPU外部的外部硬件事件的程序控制中断(例如:按下键盘上的按键或定时器超时
芯片)

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:

Trap: Is a programmer initiated and expected transfer of control to a special handler routine. (For ex: 80x86 INT instruction is a good example)

Where as

Interrupt(Hardware): Is a program control interruption based on an external hardware event external to the CPU (For ex: Pressing a key on the keyboard or a time out on a timer
chip)

清音悠歌 2024-09-14 06:32:12

陷阱由类似程序的代码调用,并用于例如调用操作系统例程(即通常同步)。
中断由事件(很多时候是硬件,例如接收到数据的网卡或 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.

〆一缕阳光ご 2024-09-14 06:32:12

我认为陷阱是由当前指令的执行引起的,因此它们被称为同步事件。其中中断是由处理器中运行的独立指令引起的,该指令与外部事件相关,因此被称为异步中断。

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.

多像笑话 2024-09-14 06:32:12

中断是硬件中断,而陷阱是软件调用的中断。硬件中断的发生通常会禁用其他硬件中断,但对于陷阱则不然。如果您需要在陷阱被服务之前不允许硬件中断,则需要显式清除中断标志。通常计算机上的中断标志影响(硬件)中断而不是陷阱。这意味着清除该标志不会阻止陷阱。与陷阱不同,中断应保留 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.

永言不败 2024-09-14 06:32:12

陷阱是一种软件中断。如果您编写一个程序,其中声明一个除以零值的变量,那么它将被视为陷阱。每当您运行该程序时,它都会同时抛出相同的错误。系统调用是一个陷阱的特殊版本,其中程序向操作系统请求其所需的服务。
如果出现中断(硬件中断的通称),例如 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.

苏璃陌 2024-09-14 06:32:12

中断是系统内由硬件生成的流程变更。一个中断
调用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.

伴我心暖 2024-09-14 06:32:12

陷阱可以被识别为由程序员发起的控制转移。术语“陷阱”与术语“异常”(异常是自动发生的软件中断)可互换使用。但有些人可能会认为陷阱只是一个特殊的子例程调用。因此它们属于软件调用中断的范畴。例如,在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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文