OSDev.org
https://forum.osdev.org/

Switching from real mode to long mode directly..
https://forum.osdev.org/viewtopic.php?f=1&t=11093
Page 3 of 3

Author:  rdos [ Mon May 09, 2011 12:56 am ]
Post subject:  Re: Switching from real mode to long mode directly..

I hope mainstream OSes will not adopt this for quite some time more, as Intel & AMD could then start dropping protected mode. :twisted:

Author:  Love4Boobies [ Mon May 09, 2011 12:59 am ]
Post subject:  Re: Switching from real mode to long mode directly..

You think they've kept real mode along for 33 years, 26 of which because of backwards compatibility, and they're going to drop protected mode just like that?

Author:  pierrel5 [ Mon May 09, 2011 10:01 am ]
Post subject:  Re: Switching from real mode to long mode directly..

Quote:
No, you can only switch to paged protected mode. However, switching to long mode works from both real mode and non-paged protected mode.


But when returned in paged protected mode, then it should be possible to disable paging and return to real mode ?

Author:  Brendan [ Mon May 09, 2011 10:19 pm ]
Post subject:  Re: Switching from real mode to long mode directly..

Hi,

pierrel5 wrote:
Is is possible to return to real mode directly from long mode (to call bios in true real mode ?)


I haven't tried it, and it's been a while since I messed with switching to/from long mode, but as far as I can tell there's no reason why you couldn't switch from long mode directly to real mode.

The steps would be something like:
  • Make sure your code is running in identity mapped pages below 1 MiB
  • Load 16-bit "compatibility mode" segments at CPL=0
  • Disable IRQs and either disable NMI or load an IDT with "limit = zero" (so that NMI causes triple fault)
  • Clear both the PG flag and PE flag in CR0 (to disable protected/long mode and paging at the same time)
  • Reload segment registers with real-mode values
  • Load a real mode IDT (base = 0, limit = 1023)
  • Enable IRQs (and enable NMI if you disabled it)

This should de-activate long mode. There's no need to disable long mode (rather than simply de-activating it), but you could mess with the EFER, clear the PAE flag, etc afterwards if you really do want to disable long mode.

I probably should mention that there's only 3 reasons for any OS to switch back to real mode like this. The first reason is that your OS is badly designed and failed to finish using the BIOS before switching to long mode in the first place (and you should correct your boot code instead). The second reason is that your OS is badly designed and isn't using the hardware like it should (including reconfiguring just everything from legacy hardware like PIC, PIT all the way up to each PCI device; so that everything isn't stuck in some silly time-warp mode from 1978). The third reason is that you think the first 2 reasons don't apply to you because you're writing the OS as a learning experience and you want to learn how to make design failures (in this case learning how to do something wrong is probably worse than not learning how to do something at all). :D

pierrel5 wrote:
Is paging can be disabled in long mode ? (to use one single segment ?)


No.


Cheers,

Brendan

Author:  pierrel5 [ Tue May 10, 2011 2:15 am ]
Post subject:  Re: Switching from real mode to long mode directly..

Thank you Brendan for you answer.

I know that returning to real mode is ugly, but my OS is light and must work on usb key on every PC.
So if I don't use int13 to access key, I have to program PCI + USB chips drivers + USB stack, .. very complicated to do something reliable on every PC..

Thank you for your help, I'll try your switch method when have time, actually try to program smp :-)

Cheers,
Pierre

ps: I think than doing something wrong is better than doing noting ;-)

Author:  turdus [ Tue May 10, 2011 12:10 pm ]
Post subject:  Re: Switching from real mode to long mode directly..

pierrel5 wrote:
I know that returning to real mode is ugly, but my OS is light and must work on usb key on every PC.
So if I don't use int13 to access key, I have to program PCI + USB chips drivers + USB stack, .. very complicated to do something reliable on every PC.

It's not so hard, I have already posted an assembly code on this forum (homework: search) that does that: switch to real mode directly from long mode, calls a bios int, and switches back to long mode. No protected mode involved.

Author:  pierrel5 [ Tue May 10, 2011 12:37 pm ]
Post subject:  Re: Switching from real mode to long mode directly..

thank you turdus
Very nice code, exactly what I need :D

Page 3 of 3 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/