Is this a good method?

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
theelitenoob
Posts: 16
Joined: Wed Aug 24, 2011 11:19 am

Is this a good method?

Post by theelitenoob »

So I have a boot-loader and was wondering If i got everything, done right.
This is how it goes, I'm wondering if anything is missing?
  1. First Stage bootloader (loads second)
  2. Second stage bootloader
    • Enable A20 Gate
    • Go to protected mode
    • Perform a long jump to kernel.asm
  3. Kernel.asm goes to kmain in C code.
Do i need anything else? As this seems to work.
Also, can I set up the GDT and paging in C instead of ASM? Finally, i noticed some bootladers have a config.inc, and show stuff for FAT, is that how the filesystem is set-up, or is more complicated?
Snake
Posts: 19
Joined: Wed Mar 21, 2012 7:38 pm

Re: Is this a good method?

Post by Snake »

You can do almost everything that needs doing about gdt and paging in C, but you still need to use lgdt instruction, and read/write cr3 register somehow. So maybe use inline assembly for that.

I'd say don't worry about the bootloader too much. If it can load your os it should be fine. If you want to do more complicated features why not just use grub?
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Is this a good method?

Post by bluemoon »

You need to setup a good-shape environment for C, which requires:
1. proper stack
2. zero ebp/rbp (not really required, but just to make stack trace cleaner)
3. zero'd bss (C global static variables are assumed to be zero)

(1), (2) are usually done in your kernel bootstrap (kernel.asm).
(3) can be done in kernel or boot loader, it's up to you.

theelitenoob wrote:Also, can I set up the GDT and paging in C
I recommend to do it in assembly since it usually require working with different addressing model(physical & logical), which can get messy with C.
But don't worry getting the hand dirty with assembly, it only took me 18 lines to setup paging + gdt and jump to higher-half.
theelitenoob
Posts: 16
Joined: Wed Aug 24, 2011 11:19 am

Re: Is this a good method?

Post by theelitenoob »

bluemoon wrote:You need to setup a good-shape environment for C, which requires:
1. proper stack
2. zero ebp/rbp (not really required, but just to make stack trace cleaner)
3. zero'd bss (C global static variables are assumed to be zero)

(1), (2) are usually done in your kernel bootstrap (kernel.asm).
(3) can be done in kernel or boot loader, it's up to you.

theelitenoob wrote:Also, can I set up the GDT and paging in C
I recommend to do it in assembly since it usually require working with different addressing model(physical & logical), which can get messy with C.
But don't worry getting the hand dirty with assembly, it only took me 18 lines to setup paging + gdt and jump to higher-half.
Ok thanks for the advice, it really helps.
User avatar
turdus
Member
Member
Posts: 496
Joined: Tue Feb 08, 2011 1:58 pm

Re: Is this a good method?

Post by turdus »

You'll probably need to gather some information from the BIOS while you're in real mode. E820 or VBE modes for example. (If you don't want to struggle with calling real mode interrupts later from protmode C).
Rudster816
Member
Member
Posts: 141
Joined: Thu Jun 17, 2010 2:36 am

Re: Is this a good method?

Post by Rudster816 »

bluemoon wrote:You need to setup a good-shape environment for C, which requires:
1. proper stack
2. zero ebp/rbp (not really required, but just to make stack trace cleaner)
3. zero'd bss (C global static variables are assumed to be zero)

(1), (2) are usually done in your kernel bootstrap (kernel.asm).
(3) can be done in kernel or boot loader, it's up to you.

theelitenoob wrote:Also, can I set up the GDT and paging in C
I recommend to do it in assembly since it usually require working with different addressing model(physical & logical), which can get messy with C.
But don't worry getting the hand dirty with assembly, it only took me 18 lines to setup paging + gdt and jump to higher-half.
You also need to make sure you have your segments setup correctly (which requires a GDT obviously).


If you're writing your own bootloader, I would go ahead and enable protected mode\paging at the same time. It will allow you to do a higher half kernel without seperate .text sections or the GDT trick. This will probably be the easiest way in the long run. I ended up writing my own bootloader 64-bit bootloader that runs in real mode (with the exception of temporarily switching to protected to move the kernel image to > 1MB memory) and it really wasn't too hard. You just have to make sure to keep a good program structure, otherwise you'll end up with super buggy spaghetti code, which is very easy to do in assembly, especially with flat\standalone binary.
Post Reply