Moving on 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
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Moving on to PMode

Post 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.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Moving on to PMode

Post 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
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Moving on to PMode

Post 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 :)
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: Moving on to PMode

Post 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.
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: Moving on to PMode

Post 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?
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Moving on to PMode

Post 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
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: Moving on to PMode

Post 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.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Moving on to PMode

Post 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.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Moving on to PMode

Post 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.)
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: Moving on to PMode

Post 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?
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
Post Reply