Page 1 of 1

Determining the size of the kernel

Posted: Sat Feb 17, 2007 5:58 pm
by deadmopo3
Hello,

Having been involved into OS development for almost a week now, I have finally reached the memory management part )
Parts present (thanks to the well-known tutorial) : GDT, IDT, IRQ0-1, simple shell, bootloader - GRUB.
The question is : how can I locate the start and the end of the kernel?
Linker source:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000; /* 1 mb */
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    . = ALIGN(0x1000);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(0x1000);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(0x1000);
  }
  end = .;
}
Is there a way to pass variables from linker to kernel? Would x=phys+SIZEOF(.text)+SIZEOF(.data)+SIZEOF(.bss) contain the address of the end of the kernel part?

Posted: Sat Feb 17, 2007 6:37 pm
by xsix
just on the last line on kernel.asm add KERNEL_END: and after ORG add KERNEL_START, so just mov eax, KERNEL_START mov ebx, KERNEL_END and sub ebx, eax and EBX contains size of the kernel in bytes

Posted: Sat Feb 17, 2007 6:40 pm
by deadmutex
the start of your kernel is at "code" and the end is at "end"

In C you would get the values like this:

Code: Select all

extern char code[];
extern char end[];
and to access them you would do something like:

Code: Select all

void *kStart = &code;
void *kEnd = &end;
In ASM you would just import "code" and "end"

EDIT: Hmm... not really sure if this will work for a binary...

Posted: Sun Feb 18, 2007 5:53 am
by deadmopo3
Thanks for the replies.
Figured out how to get size: simply pushed 'end' before calling main() so that I could access it as an argument.

Posted: Sun Feb 18, 2007 8:25 am
by os64dev
i hope you keep to the convention/prototype of main int main(int argc, char * argv[]), though it strictly not required it is nicer and deadmutex his method should also do the trick then again if it works it works.