Page 1 of 1

Moving on to PMode

Posted: Tue Mar 24, 2009 11:09 am
by Firestryke31
Hello all! I've decided to move Socks on from Real Mode. I was just wondering what would be a good idea to do before making the switch to PMode in the loader. I know that A20 is practically a must, switching the video mode is a good idea, and getting the memory map stored somewhere is pretty nice too, but beyond that I haven't really had much inspiration. So I was wondering, what do you do in your bootloader before the switch? I already know how to make the switch itself (CR0, GDT and whatnot) and just want to know the setup stuff.

This could also be a nice, central place for ideas regarding the topic. Keeping info in a central place is always good.

Edit: I've already looked in the wiki, and couldn't really find anything regarding setup. Just the actual switch and what to do once there.

Re: Moving on to PMode

Posted: Tue Mar 24, 2009 11:16 am
by AJ
Hi,

Although it's not a "must", my boot loader sets up paging too, meaning that the kernel can be located anywhere without the need for the GDT trick.

I also set up a kernel stack and push any arguments I want passed to the kernel in a traditional 'argc, argv' way. This means that my kernel launches directly in to the main routine with no asm stub. It also means that both my 32 and 64 bit kernel launch directly in to the architecture-independent portion and it allows me to get some nice C++ runtime stuff up fairly soon in to the kernel launch process. My loader currently even provides the page frame allocation structure for the kernel.

There are several reasons why you wouldn't want to do the above, but I like doing it that way :)

Cheers,
Adam

Re: Moving on to PMode

Posted: Tue Mar 24, 2009 2:05 pm
by neon
So I was wondering, what do you do in your bootloader before the switch?
Disable interrupts? ;)

In my system, the very first thing in its startup program is to switch to protected mode and enable A20. Everything else is done through a real mode<>pmode interface so that I can call the bios through the interface running in protected mode. Because of this, there is nothing that I need to do before the switch as I can just do it from pmode without any problems.

A little slow this way, but works great :)

Re: Moving on to PMode

Posted: Tue Mar 24, 2009 2:37 pm
by quok
neon wrote:
So I was wondering, what do you do in your bootloader before the switch?
Disable interrupts? ;)

In my system, the very first thing in its startup program is to switch to protected mode and enable A20. Everything else is done through a real mode<>pmode interface so that I can call the bios through the interface running in protected mode. Because of this, there is nothing that I need to do before the switch as I can just do it from pmode without any problems.

A little slow this way, but works great :)
I do the same thing, but with one small difference; I even able A20 after switching to protected mode. :) I find this way allows me to accomplish anything I want rather easily since my C is a lot better than my ASM. Even video mode switching is easy as pie.

Re: Moving on to PMode

Posted: Tue Mar 24, 2009 2:43 pm
by Firestryke31
If you set up the ISRs and the IDT, do you even need to disable interrupts? Or am I missing something stupidly obvious (I get the feeling I am)?

Edit: and should I change the title to the slightly more descriptive "What to do before leaving Real Mode" for future searchers?

Re: Moving on to PMode

Posted: Wed Mar 25, 2009 4:31 am
by jal
Firestryke31 wrote:If you set up the ISRs and the IDT, do you even need to disable interrupts?
The thing is, you do not need to enable interrupts: there's absolutely no use for them at this stage, so setting up an IDT is useless. One of the first things your OS (any OS, for that matter) is going to do is set up one itself.


JAL

Re: Moving on to PMode

Posted: Wed Mar 25, 2009 8:15 am
by quok
jal wrote:
Firestryke31 wrote:If you set up the ISRs and the IDT, do you even need to disable interrupts?
The thing is, you do not need to enable interrupts: there's absolutely no use for them at this stage, so setting up an IDT is useless. One of the first things your OS (any OS, for that matter) is going to do is set up one itself.

JAL
In my case, I hit protected mode as soon as I can, and switch back and forth between real mode and protected mode to use any BIOS functions I may need. I do that by calling interrupts, so I DO need an IDT. There's other ways I could have done this, but I found this one to be pretty easy.

For the little bit that my bootloader runs in real mode, I load the BIOS IVT and enable interrupts there so BIOS can handle anything that occurs. Before the switch back to protected mode, I disable interrupts and after the pmode switch I reload my pmode IDT. I tried running with interrupts disabled all the time (and never bothered to make sure interrupts were still disabled after using the BIOS) only to find that some BIOS functions do enable interrupts and leave them that way.

So, you may need an IDT depending on how you handle any protected mode to real mode switching. As jal said, there's really no need to enable interrupts. Just make sure that interrupts are still disabled after using the BIOS and you should be okay.

Re: Moving on to PMode

Posted: Wed Mar 25, 2009 8:47 am
by Troy Martin
My 32-bit bootloader loads only, no switch. The switch and A20 and GDT and IDT and all things that go bump in the night are handled in the 16-bit header of the kernel.

Re: Moving on to PMode

Posted: Wed Mar 25, 2009 11:23 am
by neon
quok wrote:In my case, I hit protected mode as soon as I can, and switch back and forth between real mode and protected mode to use any BIOS functions I may need. I do that by calling interrupts, so I DO need an IDT. There's other ways I could have done this, but I found this one to be pretty easy.
Looks like your system and mine is a little different :) My interface is a single routine that provides input/output services. It follows the format of the old dos.h int86 routine and works the same way. ie, a call such as io_services (0x10, &in, &out); in turn calls bios int 0x10 in real mode.

(Although it does still set up an IDT, but its only for exception trapping.)

Re: Moving on to PMode

Posted: Wed Apr 01, 2009 2:13 pm
by Firestryke31
Okay, so I have getting the memory map and setting the video mode, and am going to get paging set up, but I had one (or three) more question(s):

How do you organize data before going to Pmode? Do you have a real-mode memory manager, or do you just dump the data at a few specific points and go? If the latter, where do you typically put what data?