Page 1 of 1
Is this a good method?
Posted: Thu Mar 29, 2012 11:25 am
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?
- First Stage bootloader (loads second)
- Second stage bootloader
- Enable A20 Gate
- Go to protected mode
- Perform a long jump to kernel.asm
- 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?
Re: Is this a good method?
Posted: Thu Mar 29, 2012 11:47 am
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?
Re: Is this a good method?
Posted: Thu Mar 29, 2012 12:34 pm
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.
Re: Is this a good method?
Posted: Thu Mar 29, 2012 2:16 pm
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.
Re: Is this a good method?
Posted: Thu Mar 29, 2012 3:05 pm
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).
Re: Is this a good method?
Posted: Thu Mar 29, 2012 4:32 pm
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.