how do the registers get saved when a process gets interrupted?

前提是你 提交于 2019-12-25 01:13:33

问题


this has been bugging me all day. When a program sets itself up to call a function when it receives a certain interrupt, I know that the registers are pushed onto the stack when the program is interrupted, but what I can't figure out is: how do the registers get off the stack? I know that the compiler doesn't know if the function is an interrupt handler, and it can't know how many arguments the interrupt gave to the function. So how on earth does it get the registers off?


回答1:


It depends on the compiler, the OS and the CPU.

For low level embedded stuff, where an ISR may be called directly in response to an interrupt, the compiler will typically have some extension to the language (usually C or C++) that flags a given routine as an ISR, and registers will be saved and restored at the beginning and end of such a routine. [1]

For common desktop/server OSs though there is normally a level of abstraction between interrupts and user code - interrupts are normally handled first by some kernel code before being passed to a user routine, in which case the kernel code takes care of saving and restoring registers, and there is nothing special about the user-supplied ISR.


[1] E.g. Keil 8051 C compiler:

void Some_ISR(void) interrupt 0 // this routine will get called in response to interrupt 0
{
    // compiler generates preamble to save registers

    // ISR code goes here

    // compiler generates code to restore registers and
    // do any other special end-of-ISR stuff
}


来源:https://stackoverflow.com/questions/6332517/how-do-the-registers-get-saved-when-a-process-gets-interrupted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!