Page 1 of 1

Kernel size

Posted: Tue Dec 01, 2015 6:10 pm
by cepanamea
Hi,

I've been writing the first steps of a kernel, following tutorials like the bare bones and the like. Everything worked fine for a while, but as the code grew, after the kernel image reached a certain size (12-14k), it started crashing the emulator (QEMU). So if I comment a certain function, the kernel gets smaller and it works. Uncommenting the function (without ever calling it) generates the crash.
I'm probably missing some obvious step, but I'm totally lost for now. If I need to give more specific details and code/config samples, please let me know.

Thanks

Re: Kernel size

Posted: Tue Dec 01, 2015 8:35 pm
by alexfru
Too little info. We don't know if it's because your loader fails to find or load more sectors or you have size/index variables too small somewhere or your address arithmetic loses a carry when done in 16-bit or the code/data grows larger than the segment size or your code/data overwrites some important stuff in memory (including collision with the stack or hard-coded buffers), etc etc.
You should do your best in identifying the last instruction where things are OK / the first instruction where things start going wrong. Debug it.

Re: Kernel size

Posted: Tue Dec 01, 2015 10:43 pm
by thepowersgang
It sounds very much like you're booting using qemu's `-kernel` support and using a multiboot kernel.

This is the same as grub's Error 13 - Your multiboot header has moved outside of the range searched, so qemu fails to load it correctly (and earlier versions would just crash when that happens)

Re: Kernel size

Posted: Wed Dec 02, 2015 3:16 am
by cepanamea
@thepowersgang: yes, this is exactly what I'm doing. I figured something like this is happening, the code not fitting into the memory space. What I don't know is how to put the code where there is enough space.. Or maybe I simply don't understand the concept. The documentation I've read so far hasn't made things clear for a beginner like me.
@alexfru: my kernel is basically a clone of the bare bones tutorial. and it works. then I added some more code, for a printf() or itoa() functions, and it also worked, until the code grew too big. I didn't write any code to manage where the rest of the code gets loaded into memory, just used the classic linker script that's been floating around with these tutorials.

Re: Kernel size

Posted: Wed Dec 02, 2015 4:00 am
by iansjack
You should also consider the possibilities that your stack is overwriting code and/or that you are not saving the complete code to the kerel image. The latter more commonly happens when you write sectors to an image file but don't allow enough sectors. You really shouldn't run into multiboot size problems with a simple kernel such as the one you describe. I'm presuming that you have checked that your code is using the functions that you have defined and is not trying to use GCC's built-in ones.

Re: Kernel size

Posted: Wed Dec 02, 2015 4:34 am
by cepanamea
I'm not using image files, I'm just feeding qemu the binary file generated by the linker. As for GCC, I built a cross compiler, which has no libc whatsoever, probably the only foreign reference
in my code is to a va_list.
I've been looking on how to configure each section, so that my code section has more space, but I can't seem to find the info on how to size each section (or even if this is how it works).
I'll check tonight the docs related to grub error 13. It seems that the linker script should be slightly modified, maybe that's it.

Re: Kernel size

Posted: Wed Dec 02, 2015 12:48 pm
by cepanamea
Ok, problem solved. One must declare the multiboot header in its own section in the asm code and then put that section in the linker script BEFORE the text section but AFTER the 1MB mark. Something like this

Code: Select all

ENTRY(start)
SECTIONS
{
	. = 1M;
 	.multiboot : {
      *(.multiboot)
   	}
   	
    .text :
    {
        code = .; _code = .; __code = .;
        *(.text)
        . = ALIGN(4096);
    }
....
Thanks for the tips

Re: Kernel size

Posted: Wed Dec 02, 2015 12:59 pm
by iansjack
The built-in functions are not provided by libc. But as long as you are using the compiler switch "-fno-builtin" you will be ok.