Executing C Kernel #101
Posted: Fri Aug 12, 2011 9:43 am
The Bare Bones tutorial is awesome. However I would like to experiment with my own Boot Loader instead of GRUB. Therefore, I have several questions. Sorry if this is going to be long, I will try my best to explain issues.
Alright, so, my 2nd Stage Boot Loader is up. The A20 and Protected Mode are enabled. Lets consider the following snippet from it (we are in PMode already):
Here, within comments, I've just wrote my own vision of how I should start executing Kernel. Am I right?
The kernel:
Now, to start executing this "8:X" address there must be this kmain function. I guess that I can't rely on the compiler/linker to place it right in the beginning of Kernel binary file, can I? So, there are 2 possible scenarios:
1. If I CAN'T rely on it, then as far as I understand here comes "loader.s" in Bare Bones tutorial to solve this problem, because assembly's binary output is much more tunable, right? Therefore, by linking assembly "loader.o" and "kernel.o" I will get Kernel binary file with the first instruction on label "loader:" so jumping to 8:X will 100% execute it?
2. If I CAN rely on it, then it is probably linker, who can put "kmain" at the beginning of Kernel binary file. Since linker script contains "ENTRY(...)" directive. What if I just put "kmain" there? Then after jumping to 8:X will I 100% execute "kmain"?
Now I have several questions about linker. Consider the following linker script:
1. AFAIK it means that program is going to be located at 1 MB in RAM. Remember "X"? My Stage 2 Boot Loader is responsible for placing Kernel program to X address, then why on earth do I have to specify this "0x00100000" or any other "X" for linker?
2. AFAIK this is used to place different sections within 4 KB blocks in RAM (I guess it is related with paging somehow). OK, lets imagine that all my sections (text, rodata, data and bss) are 1 KB long each. Does it mean that Kernel binary file (produced by linker) will look like this:
1 KB of text
3 KB of some garbage (maybe zeroes)
1 KB of rodata
3 KB of some garbage (maybe zeroes)
1 KB of data
3 KB of some garbage (maybe zeroes)
1 KB of bss
3 KB of some garbage (maybe zeroes)
I believe, this is the only way to align. Am I right?
Here goes one more similar question about the .bss section in general. Look:
I've read somewhere that .bss section is VIRTUAL. Like "When you assemble - the output binary file will not contain those STACKSIZE bytes. But when you execute it this section will appear..." - I'm like "Whatta?!" Who on earth is going to create this section for me in runtime?
I think that's all for now. Thanks in advance guys and sorry for long post again.
Alright, so, my 2nd Stage Boot Loader is up. The A20 and Protected Mode are enabled. Lets consider the following snippet from it (we are in PMode already):
Code: Select all
BITS 32
Code32:
MOV AX,16
MOV DS,AX
MOV ES,AX
MOV FS,AX
MOV GS,AX
MOV SS,AX
MOV ESP,0x9FFFF
;Load Kernel binary file from HD to "X" (X is some physical address in RAM)
;JMP 8:X
The kernel:
Code: Select all
void kmain()
{
unsigned char *videoram = (unsigned char *) 0xb8000;
videoram[0] = 65;
videoram[1] = 0x07;
}
1. If I CAN'T rely on it, then as far as I understand here comes "loader.s" in Bare Bones tutorial to solve this problem, because assembly's binary output is much more tunable, right? Therefore, by linking assembly "loader.o" and "kernel.o" I will get Kernel binary file with the first instruction on label "loader:" so jumping to 8:X will 100% execute it?
2. If I CAN rely on it, then it is probably linker, who can put "kmain" at the beginning of Kernel binary file. Since linker script contains "ENTRY(...)" directive. What if I just put "kmain" there? Then after jumping to 8:X will I 100% execute "kmain"?
Now I have several questions about linker. Consider the following linker script:
Code: Select all
SECTIONS{
. = 0x00100000;
.text :{
*(.text)
}
.rodata ALIGN (0x1000) : {
*(.rodata)
}
.data ALIGN (0x1000) : {
*(.data)
}
.bss : {
sbss = .;
*(COMMON)
*(.bss)
ebss = .;
}
}
Code: Select all
. = 0x00100000;
Code: Select all
ALIGN (0x1000)
1 KB of text
3 KB of some garbage (maybe zeroes)
1 KB of rodata
3 KB of some garbage (maybe zeroes)
1 KB of data
3 KB of some garbage (maybe zeroes)
1 KB of bss
3 KB of some garbage (maybe zeroes)
I believe, this is the only way to align. Am I right?
Here goes one more similar question about the .bss section in general. Look:
Code: Select all
section .bss
resb STACKSIZE
I think that's all for now. Thanks in advance guys and sorry for long post again.