Should I preserve EFLAGS?
Should I preserve EFLAGS?
Hi, in my task switcher code I only save a few general purpose registers, like ebx esi edi and ebp, as well as esp. This works fine, especially since most of the task switch happens on timer IRQ where pretty much all registers are automatically saved. However, when a task yields voluntarily only those registers above get saved. I'm kinda worried about the fact that if a task voluntarily yields when it gets executed again it retains the previous task's EFLAGS, how critical is this? Are there any other registers that I might wanna save manually as well?
Thanks.
Thanks.
Re: Should I preserve EFLAGS?
You want to save all registers that might be used in your task code. This means, at a minimum, all general purpose registers, eflags, ebp and esp. If your code uses floating-point or multimedia registers (are you absolutely sure it doesn't?) then you'll need to save these as well. When you return to a task you need exactly the same environment as when you last left it.
Think about what happens if your task switches in the middle of a conditional expression. If you don't save and restore eflags you might change the condition.
Think about what happens if your task switches in the middle of a conditional expression. If you don't save and restore eflags you might change the condition.
Re: Should I preserve EFLAGS?
Yes, like I said, if it switches in the middle (because of a timer IRQ), the entire state gets saved automatically by the IRQ.iansjack wrote:You want to save all registers that might be used in your task code. This means, at a minimum, all general purpose registers, eflags, ebp and esp. If your code uses floating-point or multimedia registers (are you absolutely sure it doesn't?) then you'll need to save these as well. When you return to a task you need exactly the same environment as when you last left it.
Think about what happens if your task switches in the middle of a conditional expression. If you don't save and restore eflags you might change the condition.
Re: Should I preserve EFLAGS?
Can you guarantee that every voluntary switch will occur in code where eflags is unimportant? And can you guarantee that any high-level compiler that you may use to produce programs for your OS uses no registers other than those that you save?
Re: Should I preserve EFLAGS?
That's what I'm asking. I could of course save literally everything in the task switcher but is that a sane thing to do? So it would be (most of the time) saved by the IRQ and then by my task switcher as well.iansjack wrote:Can you guarantee that every voluntary switch will occur in code where eflags is unimportant? And can you guarantee that any high-level compiler that you may use to produce programs for your OS uses no registers other than those that you save?
Re: Should I preserve EFLAGS?
Save every register that exists. If you don't, you will run into some crazy bugs. To save the SSE, MMX, and FPU registers, just use the fxsave and fxstor instuctions.
Re: Should I preserve EFLAGS?
An irq saves only a few registers.
You are the only one who can know what registers your code uses.
You are the only one who can know what registers your code uses.
Re: Should I preserve EFLAGS?
My IRQ handlers save literally all registers, aside from the FPU and SIMD stuff.iansjack wrote:An irq saves only a few registers.
You are the only one who can know what registers your code uses.
For example in this tutorial Brendan only saves a few registers in the task switcher as well, is this bad? https://wiki.osdev.org/Brendan%27s_Mult ... g_Tutorial
Re: Should I preserve EFLAGS?
You seem to have made up your mind to save only a minimal set of registers. That's fine. If it doesn't cause you any problems then go with it.
Re: Should I preserve EFLAGS?
When task is preempted by IRQ, you have to preserve all registers, including EFLAGS. That is because it may happen at any place at any time.
But if task yields voluntarily (by issuing appropriate syscall) and ALL code conforms to single ABI, technically there's no need to preserve registers beyond what is expected by the ABI during regular function calls.
Then for "typical" i386 SysV ABI you need to preserve only EBX, ESI, EDI, EBP, (obviously) ESP, and probably some FPU registers.
This case is a likely scenario in early stages of hobby OS, but you never know how it will end up. So the safe bet remains to preserve everything you can.
But you can use shortcuts like this in a controlled environment. For example: in-kernel thread switches.
Edit: slow typing
But if task yields voluntarily (by issuing appropriate syscall) and ALL code conforms to single ABI, technically there's no need to preserve registers beyond what is expected by the ABI during regular function calls.
Then for "typical" i386 SysV ABI you need to preserve only EBX, ESI, EDI, EBP, (obviously) ESP, and probably some FPU registers.
This case is a likely scenario in early stages of hobby OS, but you never know how it will end up. So the safe bet remains to preserve everything you can.
But you can use shortcuts like this in a controlled environment. For example: in-kernel thread switches.
Edit: slow typing
If something looks overcomplicated, most likely it is.
Re: Should I preserve EFLAGS?
Ok thanks everyone for the answers 
