Page 1 of 1
Should I preserve EFLAGS?
Posted: Wed Jul 08, 2020 1:19 am
by 8infy
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.
Re: Should I preserve EFLAGS?
Posted: Wed Jul 08, 2020 2:13 am
by iansjack
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?
Posted: Wed Jul 08, 2020 2:28 am
by 8infy
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.
Yes, like I said, if it switches in the middle (because of a timer IRQ), the entire state gets saved automatically by the IRQ.
Re: Should I preserve EFLAGS?
Posted: Wed Jul 08, 2020 4:38 am
by iansjack
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?
Posted: Wed Jul 08, 2020 4:54 am
by 8infy
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?
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.
Re: Should I preserve EFLAGS?
Posted: Wed Jul 08, 2020 5:03 am
by nexos
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?
Posted: Wed Jul 08, 2020 5:07 am
by iansjack
An irq saves only a few registers.
You are the only one who can know what registers your code uses.
Re: Should I preserve EFLAGS?
Posted: Wed Jul 08, 2020 5:20 am
by 8infy
iansjack wrote:An irq saves only a few registers.
You are the only one who can know what registers your code uses.
My IRQ handlers save literally all registers, aside from the FPU and SIMD stuff.
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?
Posted: Wed Jul 08, 2020 5:42 am
by iansjack
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?
Posted: Wed Jul 08, 2020 5:56 am
by Velko
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
Re: Should I preserve EFLAGS?
Posted: Wed Jul 08, 2020 7:02 am
by 8infy
Ok thanks everyone for the answers
