Page 1 of 1
GRUB Loading / Where Are Static and Global Variables
Posted: Tue Jul 02, 2013 12:32 pm
by Geometrian
Hi,
My OS boots with GRUB, and I'm ready to start adding in memory management.
It occurs to me, however, that I don't know how GRUB knows how large the kernel is. Since the kernel is an ELF file, perhaps it knows that way?
Secondly, I don't know where static and global variables reside in a kernel. I can dump GRUB's memory map. Does this map take into account the locations of these variables?
Thanks,
-G
Re: GRUB Loading / Where Are Static and Global Variables
Posted: Tue Jul 02, 2013 4:14 pm
by gerryg400
Geometrian wrote:It occurs to me, however, that I don't know how GRUB knows how large the kernel is. Since the kernel is an ELF file, perhaps it knows that way?
Grub loaded your kernel so it knows how big it is and where it is. You can figure out the size of your kernel at runtime by using link 'variables' in your linker script. (e.g. ebss etc.)
Geometrian wrote:Secondly, I don't know where static and global variables reside in a kernel. I can dump GRUB's memory map. Does this map take into account the locations of these variables?
No. The Grub memory map includes the memory that Grub has used to load your kernel and modules. It doesn't tell you which memory it used. The memory map doesn't even tell you which addresses Grub has used to create the tables that it 'gives' you. The upshot of this is that you need to record all that data from those tables inside your kernel image before you write to any memory outside your kernel image or you risk writing over the Grub tables.
Re: GRUB Loading / Where Are Static and Global Variables
Posted: Tue Jul 02, 2013 10:28 pm
by Geometrian
gerryg400 wrote:Geometrian wrote:It occurs to me, however, that I don't know how GRUB knows how large the kernel is. Since the kernel is an ELF file, perhaps it knows that way?
Grub loaded your kernel so it knows how big it is and where it is. You can figure out the size of your kernel at runtime by using link 'variables' in your linker script. (e.g. ebss etc.)
Noted. The linker script also seems to define where GRUB loads the kernel to, correct?
Geometrian wrote:Secondly, I don't know where static and global variables reside in a kernel. I can dump GRUB's memory map. Does this map take into account the locations of these variables?
So . . . it looks like the linker script also tells where the static and global variables are.
Re: GRUB Loading / Where Are Static and Global Variables
Posted: Wed Jul 03, 2013 2:16 am
by Nessphoro
Yes, in essence.
Re: GRUB Loading / Where Are Static and Global Variables
Posted: Wed Jul 03, 2013 2:20 am
by Geometrian
I think I understand it now. I set up the grub configuration and linker scripts so that it loads the kernel to 1MiB, building the stack and all the static/global variables between 1MiB and 2MiB. My memory allocator ensures that GRUB believes that [1MiB,some high value) is free space, and then uses that space starting from 2MiB as dynamic memory. Works great!
Re: GRUB Loading / Where Are Static and Global Variables
Posted: Wed Jul 03, 2013 2:47 am
by AJ
Hi,
You could have a small free area defined statically for early memory allocation, but the most reliable way to determine an area for your 'proper' physical memory allocator is to parse the GRUB memory map. You obviously need to read information from the Multiboot(2) headers before assuming that any "free" memory does not contain modules you will need later.
Cheers,
Adam