Send interrupt to CPU like keyboard?

Is it possible to somehow simulate hardware interrupts from a user program? I have seen this question repeatedly but never answered.

I want to know about low-level interrupts (for example, simulate a situation when a key is pressed on the keyboard so that the keyboard driver will interrupt the interrupt).

High level events and APIs are out of scope and the question is more theoretical than practical (to prevent "why" discussions :)

+3


source to share


1 answer


Yes and no.

On an x86 processor (for example) there is an instruction int

that generates an interrupt. Once an interrupt is generated, the CPU will not necessarily 1 distinguish between an interrupt generated by hardware and one generated by software. For example, in the original BIOS of your computer, IBM has selected an interrupt that will execute a print screen command. The interrupt they selected (interrupt 5) was one that was not in use, but which Intel said was reserved for future use. Intel ultimately replaced this interrupt - in 286 they added an instruction bound

that checks that the value is within bounds and generates an interrupt if it is not. Instructionsbound

essentially never used because it generates interrupt 5 if the value is out of range. This means (if you are using something like MS-DOS that allows) executing a command bound

with a value that will print the screen out of bounds.

However, in modern OS this is usually not allowed. All interrupt generation and handling happens in the kernel. The hardware had 4 layers of protection ("rings") and support for specifying a ring in which an instruction could be executed int

. If you try to execute it from code running on Ring 3, it won't execute directly - instead, execution will switch to the OS kernel, which can handle it as it sees fit.

This allows (for example) Windows to emulate MS-DOS, so MS-DOS programs (that use an instruction int

) can run in a virtual machine with virtualized input and output, so even if they "think" "they are working directly with the keyboard and display hardware. they actually use emulations of them provided by the software.



However, for "native" programs, the use of most instructions int

(ie, any but a small number of interrupts intended for communication with the kernel) will simply cause the program to terminate.

So bottom line: yes, hardware supports it, but hardware also prohibits it, and almost every modern OS does just that, at least for most programs outside of the OS kernel.


  • Although with typical hardware, an interrupt handler can read data from a programmable interrupt controller (PIC) chip, which will tell it if an interrupt occurred via the PIC (i.e. a hardware interrupt) or not (a software interrupt). Most hardware also supports at least a few interrupts that can only be generated by hardware such as the x86 NMI. They are usually reserved for rather narrow purposes (for example, NMI on a PC is usually used for things like memory parity errors).
+5


source







All Articles