陷阱处理程序到底是什么?

What exactly is a trap handler?

据我所知,陷阱是在特殊事件发生时发生的事情。 在系统调用的情况下,程序执行陷阱指令并跳转到内核模式。然后,陷阱处理程序跳转到所需的处理程序(例如 fork、exec、open)。

当fork、exec、open等执行完毕后,OS调用return-from-trap指令,使程序回到用户态

但是陷阱处理程序到底是什么? (另外,如果可以的话,什么是陷阱 table?)

陷阱处理程序是在触发陷阱时将 运行 的代码。在您的示例中,OS 将安装一个处理程序(即当陷阱发生时,告诉 CPU 代码的内存地址到 运行),并且处理程序将执行系统调用。是NOT跳转到内核模式的程序。程序在触发陷阱后立即中断。使用陷阱处理程序恢复执行。

这样,三层(运行 处于保护模式的程序、运行 处于特权模式的操作系统和 CPU/hardware 强制当前正在执行的代码不能中断退出保护模式)可以在 each-other.

之间切换控制

还要注意 a) 现代 CPU 有专门的系统调用指令——一种比陷阱更有效但概念上工作相同的机制 b) 还有其他 traps/interrupts 使用也出于不同的目的——它们提供了停止顺序程序执行的基本机制,并对某种事件做出 "something else" 反应。

读这个, http://www.cse.iitd.ernet.in/~sbansal/os/lec/l8.html

The x86 processor uses a table known as the interrupt descriptor table (IDT) to determine how to transfer control when a trap occurs. The x86 allows up to 256 different interrupt or exception entry points into the kernel, each with a different interrupt vector. A vector is a number between 0 and 256. An interrupt's vector is determined by the source of the interrupt: different devices, error conditions, and application requests to the kernel generate interrupts with different vectors. The CPU uses the vector as an index into the processor's IDT, which the kernel sets up in kernel-private memory of the kernel's choosing, much like the GDT. From the appropriate entry in this table the processor loads:

你要知道用户态程序不能跳转到内核态,它只是唤醒内核态程序来执行。