Linux – Do I need to save flag registers when an interrupt occurs, or when a process is scheduled?

Do I need to save flag registers when an interrupt occurs, or when a process is scheduled?… here is a solution to the problem.

Do I need to save flag registers when an interrupt occurs, or when a process is scheduled?

I

know that all the general-purpose registers are pushed onto the stack when an interrupt occurs, but I don’t see any code that flags the registers are saved. Assembly instructions like setl, which rely on flag registers, can easily produce erroneous results when recovering from interrupts if the flag registers are broken.

Solution

Yes, the (e/r)flags register needs to be saved across context switches like this.

All interrupts (hardware and software, including exceptions) are automatically saved on the stack and recovered by an iret instruction at the end of the ISR.

System calls use the same or similar mechanisms and reserve registers.

Scheduling is triggered by interrupts or system calls. So, everything is covered.

Related Problems and Solutions