Entering Unreal mode and back to pmode

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
User avatar
balthasar
Member
Member
Posts: 30
Joined: Mon Mar 31, 2008 8:39 pm
Contact:

Entering Unreal mode and back to pmode

Post by balthasar »

I need to know how to get into Unreal mode do some real mode commands and pop back into pmode. I read about Unreal mode and im a bit confused on how to enter it and to go back to Pmode

Thanks
User avatar
Telgin
Member
Member
Posts: 72
Joined: Thu Dec 20, 2007 1:45 pm

Re: Entering Unreal mode and back to pmode

Post by Telgin »

It's fairly simple actually. Pardon the terminology, it may be a little off in some places, but the idea is there.

To get into unreal mode, you load a GDT entry that spans as much memory as you want. Generally that means that it starts at 0 and ends at 4GB (for 32-bit anyway). After you do that, you enter protected mode by setting the appropriate bit (bit 0 in cr0 I think, oring whatever is in there sets it). Next you load a segment register with the selector, then change back to real mode by clearing the protected mode bit (bit x0 in cr0 I think, anding whatever is in there with 0xFE clears it).

After doing this, you can use 32-bit offsets in real mode. It will treat accesses to those segments as starting at 0 though if I understand how it works correctly, so don't try to set the segment as starting at any value or else it might screw up 32-bit memory access. Just use a 32-bit address or offset. Generally real mode will behave exactly the same otherwise, and BIOS interrupts are available, although it is possible that some might screw up the segment selector entry and undo the work you did getting into unreal mode.

To get back into protected mode, just set the protected mode bit again, assuming that you've set up the segment selector, then you don't need to do anything else as far as I know.

Code that demonstrates this is available on the wiki, http://wiki.osdev.org/Unreal_Mode
User avatar
cr2
Member
Member
Posts: 162
Joined: Fri Jun 27, 2008 8:05 pm
Location: ND, USA

Re: Entering Unreal mode and back to pmode

Post by cr2 »

Telgin wrote:bit 0 in cr0 I think
It is.

Code: Select all

; enters pmode
mov eax, cr0
or  eax, 1
mov cr0, eax

; leave pmode
mov eax, cr0
and al, 0FEh
mov cr0, eax
EDIT 1: forgot comma on "leave pmode"
EDIT 2: accidently typed "and cr0, 0FEh" instead of "and al, 0FEh" (thanks, Combuster)
Last edited by cr2 on Sun Aug 17, 2008 9:36 am, edited 3 times in total.
OS-LUX V0.0
Working on...
Memory management: the Pool
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Entering Unreal mode and back to pmode

Post by Combuster »

- and cr0, 0FEh
+ and AL, 0FEh

:wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply