首页 文章

上下文切换功能与中断调用? [关闭]

提问于
浏览
3

我理解从SE问题下面的函数调用和中断(ISR)跳转之间的基本区别 .

difference between function call & ISR

但我仍然不清楚,在这两种情况下,寄存器将被推送/弹出/从堆栈中弹出什么?在这两种情况下如何进行上下文切换?由于我们不知道何时会发生中断,我们需要在进入ISR之前保存(变量,PC,标志(PSW),寄存器,上下文)?

如何在没有任何数据丢失的情况下恢复原始上下文与多线程环境 .

2 回答

  • 1

    我试图谷歌它,我从这里找到了所需的信息:

    谢谢@Drew McGowen

    总而言之,中断的一般顺序如下:

    Foreground code is running, interrupts are enabled
    Interrupt event sends an interrupt request to the CPU
    After completing the current instruction(s), the CPU begins the interrupt response
    automatically saves current program counter
    automatically saves some status (depending on CPU)
    jump to correct interrupt service routine for this request
    ISR code saves any registers and flags it will modify
    ISR services the interrupt and re-arms it if necessary
    ISR code restores any saved registers and flags
    ISR executes a return-from-interrupt instruction or sequence
    return-from-interrupt instruction restores automatically-saved status
    return-from-interrupt instruction recovers saved program counter
    Foreground code continues to run from the point it responded to the interrupt
    

    像往常一样,此过程的细节将取决于CPU设计 . 许多设备将硬件堆栈用于所有已保存的数据,但RISC设计通常将PC保存在寄存器(链接寄存器)中 . 许多设计还具有单独的重复寄存器,可用于中断处理,从而减少必须保存和恢复的状态数据量 .

    请注意,出于效率的原因,保存和恢复前台代码状态通常是两步过程 . 对中断的硬件响应自动保存最重要的状态,但ISR代码的第一行通常专用于保存附加状态(如果没有由硬件保存,通常以保存条件标志的形式,同时保存额外的寄存器) . 使用这个两步过程是因为每个ISR对所需的寄存器数量有不同的要求,因此每个ISR可能需要保存不同的寄存器和不同数量的寄存器,以确保保存所有适当的状态数据而不浪费时间不必要地保存寄存器(即,保存未在ISR中修改的寄存器,因此不需要保存) . 一个非常简单的ISR可能不需要使用任何寄存器,另一个ISR可能只需要使用一个或两个寄存器,而更复杂的ISR可能需要使用大量寄存器 . 在每种情况下,ISR应该只保存和恢复它实际使用的寄存器 .

  • 2

    我'm sure there are different implementations based on the CPU you'重新使用 . 在一般情况下,函数调用将输入参数存储在SPARC上的给定寄存器(%o0-%o9)内,并且在calle函数的(%i0-%i9)寄存器中可用 . 然后,被调用函数将返回值放在%i0寄存器中,以便在调用函数的%o0寄存器中可用 . 根据Sparc Manual,每个中断是:

    Accompanied by data, referred to as an “interrupt packet”. 
    An interrupt packet is 64 bytes long, consisting of eight 64-bit doublewords.
    

    根据this source,当前执行线程中的数据是:

    Saved either on a stack (PDP-11, VAX, MIPS, x86_64) 
       or in a single set of dedicated registers (ARM, PowerPC)
    

    上述消息来源提到了如何在几种不同的体系结构上处理中断 .

    请让我知道,如果你有任何问题!

相关问题