Page 1 of 2

I cant compile the Kernel :(

Posted: Tue Feb 24, 2004 4:14 pm
by MeshMan
Hi all,

Ive been scanning the web for a week now about how to compile a Kernel. I'll first explain what im doing and my problem.

I made a simple boot-strap, compiled using NASM, loads a KERNEL.BIN from the FAT file system on a floppy disk.
The kernel simply displays "Hello World" then reboots. I made the kernel is ASM, compiled with NASM (outputs about 83bytes) and it loads fine.

Now, the problem is, i want to write the kernel using C (since im a C++ coder for years, seems logical). Im using Windows XP so i got my hands on DJGPP. I've read EVERYWHERE how easy its supposed to be to compile a kernel with C BUT!
I compile using the following cmd line:
gcc -v -c main.c -ffreestanding -nostdlib -fno-builtin -fno-rtti -fno-exceptions

and link with the following:

ld -T link.ld -o kernel.bin main.o

But the output KERNEL.BIN is 4KB! What im trying to do is compile a complete flat raw kernel image file that i can load like my ASM version. Lots of people on IRC have tried to help but its too tricky. Im really feeling discouraged that only linux users have success in compiling C kernels. :/

Help would be so highly appreciated as i've beat my brains out all week over this.

Thanks all.

Re:I cant compile the Kernel :(

Posted: Tue Feb 24, 2004 4:20 pm
by Therx
Can we see the link.ld? The 4 kb size is normal due to alignments of the sections.

Pete

Re:I cant compile the Kernel :(

Posted: Tue Feb 24, 2004 4:26 pm
by MeshMan2
/* Link.ld */
OUTPUT_FORMAT("binary")
ENTRY(start)
SECTIONS
{
.text 0x100000 :
{
code = .; _code = .; __code = .;
*(.text)
. = ALIGN(4096);
}

.data :
{
data = .; _data = .; __data = .;
*(.data)
. = ALIGN(4096);
}

.bss :
{
bss = .; _bss = .; __bss = .;
*(.bss)
. = ALIGN(4096);
}

end = .; _end = .; __end = .;
}

I copied this from some project i found hoping it would work the same. I've no idea what it does :X

(I also cant login, the forums keeps logging me out when i Reply)

Re:I cant compile the Kernel :(

Posted: Tue Feb 24, 2004 4:30 pm
by Therx
What is the ORG line you have at the top of your assembly kernel (i.e. the start address where the bootsector loads it)

Pete

Re:I cant compile the Kernel :(

Posted: Tue Feb 24, 2004 4:32 pm
by MeshMan
(0100:0000) is where i load my image to.

My kernel source (main.c) is EXACTLY as follows:

int main(void)
{
   int a;
   a = 10;
   return 0;
}

Im just testing for now to see if i can load the image like i did the ASM one.

Re:I cant compile the Kernel :(

Posted: Tue Feb 24, 2004 4:34 pm
by Therx
In that case everything seems right. The reason its 4kb is because the linker script aligns for data and bss sections which will be used for global variables.

Pete

EDIT : What makes you think its not working?

Re:I cant compile the Kernel :(

Posted: Tue Feb 24, 2004 4:37 pm
by MeshMan
So i can load this 4KB file like i did my ASM version and execute the kernel at that address? I was told about EXE headers and such, will these pose a problem? Or does the instructions inside the C compiled kernel start like the ASM one does.

Re:I cant compile the Kernel :(

Posted: Tue Feb 24, 2004 4:40 pm
by Therx
No the OUTPUT_FORAMT("binary") sorts that. Try something like:-

Code: Select all

char *vidmem = 0xB8000;

*vidmem = 'H';
And see if you get an H the top left

Pete

EDIT : Sorry forgot '=' in char *.... line

Re:I cant compile the Kernel :(

Posted: Tue Feb 24, 2004 4:43 pm
by MeshMan
Gimme 2 mins and ill let you know.

Re:I cant compile the Kernel :(

Posted: Tue Feb 24, 2004 4:52 pm
by MeshMan
No, i do not get a 'H' in the corner. But i just remembered, my boot-strap hasn't initialized 32-bit PM Mode yet, maybe this is why? Heres the code when i ran it through debug:

C:\DOCUME~1\Martin\Desktop>debug kernel.bin
-u
0C9D:0100 55 PUSH BP
0C9D:0101 89E5 MOV BP,SP
0C9D:0103 83EC08 SUB SP,+08
0C9D:0106 83E4F0 AND SP,-10
0C9D:0109 B80000 MOV AX,0000
0C9D:010C 0000 ADD [BX+SI],AL
0C9D:010E 29C4 SUB SP,AX
0C9D:0110 C745FC0080 MOV WORD PTR [DI-04],8000
0C9D:0115 0B00 OR AX,[BX+SI]
0C9D:0117 8B45FC MOV AX,[DI-04]
0C9D:011A C60048 MOV BYTE PTR [BX+SI],48
0C9D:011D EBFE JMP 011D
0C9D:011F 90 NOP

Will this work when i've switched to 32-PM Mode? I cant try it yet, no time, wil have to wait for tommorow. But, from what you can see, is this ok?

Re:I cant compile the Kernel :(

Posted: Tue Feb 24, 2004 4:56 pm
by Therx
I think it should work in real mode but I don't really know and that I can't understand that disassembly (doesn't mean its wrong).

Also it might be pmode specific as gcc generates 32bit code only. Could someone clarify this?

Dam, just realised, change the B000 to B8000. Wrong address! That should work.

Pete

Re:I cant compile the Kernel :(

Posted: Wed Feb 25, 2004 5:16 am
by Adek336

Code: Select all

char *vmem = (char*) 0xb8000;
vmem is a pmode pointer. remember, under rmode pointers work a bit differently. vmem probably is a short (uint16) pointer. The long pointers need a segment and an offset value:

Code: Select all

char far *vmem = 0xb8000000;
or something like that, to set up segment 0xb800 and offset 0.

BTW, gcc creates 32bit code which will most likely not work correctly under 16bit rmode/pmode. How about using grub?::)

Re:I cant compile the Kernel :(

Posted: Wed Feb 25, 2004 7:03 am
by bubach
return 0; is used for returning the errorcode 0 (in other words that it exited without problem) but you are _not_ in dos.. :-)
use something like: for(;;); to hang the computer instead of return 0;

/ Christoffer

Re:I cant compile the Kernel :(

Posted: Wed Feb 25, 2004 7:06 am
by MeshMan
(return 0) Ah, didn't think of that, ill do that ;)

Im thinking this:

My boot-strap is 16-bit r-mode that could load a NASM compiled stage 2 process which goes into PMODE, then load the kernel.

Or, just get the boot-strap to go into P-MODE and then load the 32-bit kernel?

Re:I cant compile the Kernel :(

Posted: Wed Feb 25, 2004 7:11 am
by bubach
i am using a 16 bit "kernel loader" that lies on the fat12 disc (beeing loaded by my fat12 bootsect) that enters pmode and sets the A20-gate before (loading and) jumping to the kernel.. i think that?s the best way if you want to do it yourself.. otherwise you could use grub.

/ Christoffer