What address does GRUB load my kernel to?

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
chris95219
Posts: 5
Joined: Sat Apr 11, 2009 12:56 am

What address does GRUB load my kernel to?

Post by chris95219 »

Hello everyone. I'm back with another question. I am stuck in memory management because I can't find out how large the kernel image is, where it ends and where it begins in memory. Is it possible to find out 1) the size of the kernel dynamically, 2) the beginning address of where GRUB loads your kernel, and 3) (if not 1) where the kernel "image" ends in memory? There is a "load_end_addr" member of the multiboot header specification, but that is for the header - GRUB doesn't pass that to the kernel.

Any help would be appreciated, thanks.
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Re: What address does GRUB load my kernel to?

Post by salil_bhagurkar »

The header that your kernel uses, is what is used by GRUB to load your kernel. So it includes the information about your kernel embedded into the header by the linker at compile time. So you should use it.
chris95219
Posts: 5
Joined: Sat Apr 11, 2009 12:56 am

Re: What address does GRUB load my kernel to?

Post by chris95219 »

salil_bhagurkar wrote:The header that your kernel uses, is what is used by GRUB to load your kernel. So it includes the information about your kernel embedded into the header by the linker at compile time. So you should use it.
So what are safe values to use for this header? I mean, how can I find the length of my kernel? (and can this be done dynamically?) Am I supposed to choose am arbitrary number (such as 1MB) that I know for sure my kernel would fit in?
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: What address does GRUB load my kernel to?

Post by xenos »

The easiest way to determine the size and location of your kernel is to define them as symbols in your linker script. For example, your linker script might look like this:

Code: Select all

// other stuff
. = 0x100000;
kernelStart = .;
// put your kernel sections here, i.e. code, data and bss
kernelEnd = .;
// more other stuff
To access these symbols from your C(++) code, you can do something like this:

Code: Select all

extern long kernelStart;
extern long kernelEnd;

printf("Kernel area: %x - %x, size: %d\n", &kernelStart, &kernelEnd, (unsigned long)&kernelEnd - (unsigned long)&kernelStart);
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
kop99
Member
Member
Posts: 120
Joined: Fri May 15, 2009 2:58 am

Re: What address does GRUB load my kernel to?

Post by kop99 »

You could get some reference about linker script from following.
http://www.nacad.ufrj.br/sgi/860-0247-0 ... ripts.html
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: What address does GRUB load my kernel to?

Post by JamesM »

It is fairly standard to get GRUB to load your kernel at 0x100000 - 1MB.
chris95219
Posts: 5
Joined: Sat Apr 11, 2009 12:56 am

Re: What address does GRUB load my kernel to?

Post by chris95219 »

XenOS wrote:The easiest way to determine the size and location of your kernel is to define them as symbols in your linker script. For example, your linker script might look like this:
Thank you very much!
Post Reply