Page 1 of 1

My grub-probed free memory address is off. Why?

Posted: Thu Jan 21, 2010 11:13 am
by netrom
Hello fellow developers.

I've recently started programming my own OS in C/assembler. The Bare Bones article pretty much set me up.

In my current phase I'm trying to get the memory management up and running. So I access the provided multiboot_memory_map structure (from multiboot.h) for those whose 'type' is 1, and thereby not reserved.

My actual problem arises when I try to set up my virtual memory (just using a simple linked list at first). The address provided from the 'addr' field is somewhat off.. In my perception. If I use it as it is then I overwrite my running kernel code at some point. I have interpreted 'addr' as the starting address of the free memory available, which then spans the amount of bytes specified in the 'len' field.

As of now I'm using a hack (multiplying 'addr' by 4, haha I know) but I would like to know why the offset is off?

Thank you very much in advance.

Re: My grub-probed free memory address is off. Why?

Posted: Thu Jan 21, 2010 11:20 am
by gravaera
Hi:

GRUB doesn't map your kernel's executable image as reserved in the memory map. It's just supposed to tell you about which ranges of RAM are usable for the kernel. The kernel itself should handle protecting its physical frames in RAM.

Re: My grub-probed free memory address is off. Why?

Posted: Thu Jan 21, 2010 11:30 am
by netrom
Of course, that makes sense.

What is the best way of deducing the size of the running footprint of the kernel? I mean, before anything has been allocated and so on.

Re: My grub-probed free memory address is off. Why?

Posted: Thu Jan 21, 2010 12:36 pm
by Creature
netrom wrote:Of course, that makes sense.

What is the best way of deducing the size of the running footprint of the kernel? I mean, before anything has been allocated and so on.
If you're referring to getting the size of your executable, I suppose what most of us here do is use the linker script to determine the size. You put a linker script entry at the end (and possibly at the start) and subtract them from each other.

For example:

Code: Select all

SECTIONS
{
   . = 0x100000;

   /* ... */

   KernelEnd = .;
}
Then the size of the kernel executable would be KernelEnd - 0x100000.

Re: My grub-probed free memory address is off. Why?

Posted: Thu Jan 21, 2010 1:15 pm
by netrom
Creature wrote:[...]You put a linker script entry at the end (and possibly at the start) and subtract them from each other.

Then the size of the kernel executable would be KernelEnd - 0x100000.
That sounds like the way to go, but how to I access 'KernelEnd' then? I mean, how I read the value from C (or assembler)?

Re: My grub-probed free memory address is off. Why?

Posted: Thu Jan 21, 2010 1:26 pm
by -m32

Re: My grub-probed free memory address is off. Why?

Posted: Thu Jan 21, 2010 1:38 pm
by gravaera
EDIT: No need. Good luck :)

Re: My grub-probed free memory address is off. Why?

Posted: Thu Jan 21, 2010 5:12 pm
by netrom
I'm sorry.. That was kinda embarrassing as I have used the wiki quite a lot. #-o
Thanks again anyways.