OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 25, 2024 2:59 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Should I preserve EFLAGS?
PostPosted: Wed Jul 08, 2020 1:19 am 
Offline
Member
Member

Joined: Sun Apr 05, 2020 1:01 pm
Posts: 183
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.


Top
 Profile  
 
 Post subject: Re: Should I preserve EFLAGS?
PostPosted: Wed Jul 08, 2020 2:13 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4597
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: Should I preserve EFLAGS?
PostPosted: Wed Jul 08, 2020 2:28 am 
Offline
Member
Member

Joined: Sun Apr 05, 2020 1:01 pm
Posts: 183
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.


Top
 Profile  
 
 Post subject: Re: Should I preserve EFLAGS?
PostPosted: Wed Jul 08, 2020 4:38 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4597
Location: Chichester, UK
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?


Top
 Profile  
 
 Post subject: Re: Should I preserve EFLAGS?
PostPosted: Wed Jul 08, 2020 4:54 am 
Offline
Member
Member

Joined: Sun Apr 05, 2020 1:01 pm
Posts: 183
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.


Top
 Profile  
 
 Post subject: Re: Should I preserve EFLAGS?
PostPosted: Wed Jul 08, 2020 5:03 am 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
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


Top
 Profile  
 
 Post subject: Re: Should I preserve EFLAGS?
PostPosted: Wed Jul 08, 2020 5:07 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4597
Location: Chichester, UK
An irq saves only a few registers.

You are the only one who can know what registers your code uses.


Top
 Profile  
 
 Post subject: Re: Should I preserve EFLAGS?
PostPosted: Wed Jul 08, 2020 5:20 am 
Offline
Member
Member

Joined: Sun Apr 05, 2020 1:01 pm
Posts: 183
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_Multi-tasking_Tutorial


Top
 Profile  
 
 Post subject: Re: Should I preserve EFLAGS?
PostPosted: Wed Jul 08, 2020 5:42 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4597
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: Should I preserve EFLAGS?
PostPosted: Wed Jul 08, 2020 5:56 am 
Offline
Member
Member
User avatar

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


Top
 Profile  
 
 Post subject: Re: Should I preserve EFLAGS?
PostPosted: Wed Jul 08, 2020 7:02 am 
Offline
Member
Member

Joined: Sun Apr 05, 2020 1:01 pm
Posts: 183
Ok thanks everyone for the answers :D


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Majestic-12 [Bot], SemrushBot [Bot], suspectedowl and 267 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group