Jump to content
  • Advertisement
Sign in to follow this  
Mastaba

Question regarding switching an x86 from real mode to protected

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've read the Intel documents ten times over and I've yet to find a definitive answer to one question I have about switching the cpu into protected mode. Since switching into protected mode, via SMSW or MOV to CR0, does not load CS or SS with a new selector, isn't it the case that you must ensure that the CS and SS selectors are valid selectors (or rather would be once the switch is made)? And essentially be identity mappings so far as the descriptors they reference points to the same area of memory with the same address offsets? They do indicate you need to do that in certian situations, but they don't explicitly indicate that switching from real to protected mode is one of those situations.

Share this post


Link to post
Share on other sites
Advertisement
Interesting question. This article, 15. Accessing 4 Gigs of Memory in Real Mode, suggests setting the segments after the switch. Maybe some of the code snips here indicate otherwise.

This article, Embedded X86 Programming: Protected Mode says that after switching into protected mode (via CR0) the segment registers can be used to access subsequent instructions or data even though their contents are unknown at that point. Glossing over the details from the article, the switch from 16 bit to 32 bit is made by loading CS. SS is loaded last followed by ESP.

Share this post


Link to post
Share on other sites
Aha! The magic is with the "hidden" portion of the CS and SS registers. When a value is loaded into a segment register, the cpu properly fills the hidden portion of the segment register (the limit, base and attributes) so that even in real mode, the ram address is calculated the same exact way it is in protected mode, its just that in protected mode, those hidden portions of the segment register come from the segment descriptor table, but only when the selector register is loaded. So when the switch to real mode is made, the CS register remains valid because its not the selector value that's being used in the memory references, its the hidden parts which are used and those remain perfectly valid, until a new selector is loaded.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!