GRUB Loading / Where Are Static and Global Variables

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Geometrian
Member
Member
Posts: 77
Joined: Tue Nov 20, 2012 4:45 pm
Contact:

GRUB Loading / Where Are Static and Global Variables

Post 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
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: GRUB Loading / Where Are Static and Global Variables

Post 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.
If a trainstation is where trains stop, what is a workstation ?
Geometrian
Member
Member
Posts: 77
Joined: Tue Nov 20, 2012 4:45 pm
Contact:

Re: GRUB Loading / Where Are Static and Global Variables

Post 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.
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: GRUB Loading / Where Are Static and Global Variables

Post by Nessphoro »

Yes, in essence.
Geometrian
Member
Member
Posts: 77
Joined: Tue Nov 20, 2012 4:45 pm
Contact:

Re: GRUB Loading / Where Are Static and Global Variables

Post 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!
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: GRUB Loading / Where Are Static and Global Variables

Post 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
Post Reply