Should I preserve EFLAGS?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
8infy
Member
Member
Posts: 188
Joined: Sun Apr 05, 2020 1:01 pm

Should I preserve EFLAGS?

Post 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.
User avatar
iansjack
Member
Member
Posts: 4834
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Should I preserve EFLAGS?

Post 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.
8infy
Member
Member
Posts: 188
Joined: Sun Apr 05, 2020 1:01 pm

Re: Should I preserve EFLAGS?

Post 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.
User avatar
iansjack
Member
Member
Posts: 4834
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Should I preserve EFLAGS?

Post 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?
8infy
Member
Member
Posts: 188
Joined: Sun Apr 05, 2020 1:01 pm

Re: Should I preserve EFLAGS?

Post 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.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Should I preserve EFLAGS?

Post 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.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
User avatar
iansjack
Member
Member
Posts: 4834
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Should I preserve EFLAGS?

Post by iansjack »

An irq saves only a few registers.

You are the only one who can know what registers your code uses.
8infy
Member
Member
Posts: 188
Joined: Sun Apr 05, 2020 1:01 pm

Re: Should I preserve EFLAGS?

Post 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
User avatar
iansjack
Member
Member
Posts: 4834
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Should I preserve EFLAGS?

Post 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.
User avatar
Velko
Member
Member
Posts: 153
Joined: Fri Oct 03, 2008 4:13 am
Location: Ogre, Latvia, EU

Re: Should I preserve EFLAGS?

Post 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
If something looks overcomplicated, most likely it is.
8infy
Member
Member
Posts: 188
Joined: Sun Apr 05, 2020 1:01 pm

Re: Should I preserve EFLAGS?

Post by 8infy »

Ok thanks everyone for the answers :D
Post Reply