OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 12:48 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Grub Multiboot2 Kernel Physical Address
PostPosted: Fri Jan 10, 2020 5:31 am 
Offline

Joined: Mon Nov 25, 2019 3:34 pm
Posts: 23
Hi everyone, I looked around for this information but couldn't find a definitive answer so I will post here.
My question regards the physical location of my ELF-format kernel after being loaded by GRUB2.
As I understand it (please correct me if I'm wrong), since I'm using the ELF format, my kernel gets loaded at the physical address that I specify when linking it (through my linker script).
I played around a little with the script and i was able to get GRUB2 to put the kernel pretty much where I wanted in main memory.
Now my first question is, if I don't specify a location, GRUB2 puts my kernel at physical address 0 (which I thought was not available) and everything works fine. Why does it do that? How does it know that address 0 is a good place for my kernel?
I then printed the memory map from GRUB2 and it looks like the pic in the attachment (Qemu w/ 128MB RAM).
Now i can see that the first 0x09fc00 bytes are marked as available so, all in all, putting the kernel at 0 address is not a bad idea, but all of this knowledge I got after it was actually loaded.
I can, of course, specify where I want it loaded and I fooled around with it putting it in reserved memory places causing a crash which seems normal to me.
Am I wrong in saying that GRUB2 does some kind of 'thinking' and puts my kernel where it can find the first free memory chunk big enough to load the whole thing? If my kernel was bigger than 0x09fc00 and therefore couldn't fit the block of mem that starts at 0, would GRUB2 put it at 0x10000 (the first largest block that can fit my kernel) ?
More in general, can I forget about it and set the kernel loading address at 1MB (0x10000) in my linker script assuming that I have space ranging from there to the end of the free block to play around with?
As a last note, I set up the classic GDT with 1:1 mapping to physical addresses effectively ignoring segmentation.

I hope I was clear and didn't talk much bs,
thank you in advance.

FG


[EDIT]
I posted too soon, I managed to do some test and I answered some of my questions:
If I make the kernel large enough, GRUB2 won't change the loading address, it just loads it at 0 and run out of memory.
So I guess my question now is: can I always assume that starting from 1MB I will have a fairly large chunk of mem where to load my kernel?


Attachments:
File comment: My custom kernel memory map from GRUB2
kernel_memory.png
kernel_memory.png [ 7 KiB | Viewed 994 times ]
Top
 Profile  
 
 Post subject: Re: Grub Multiboot2 Kernel Physical Address
PostPosted: Sat Jan 11, 2020 2:19 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
Lagor wrote:
If I make the kernel large enough, GRUB2 won't change the loading address, it just loads it at 0 and run out of memory.
Yet another reason why not to use Grub :-) Your assumption (if the kernel doesn't fit, put it into the first big enough free block) makes a lot more sense imho.
Lagor wrote:
So I guess my question now is: can I always assume that starting from 1MB I will have a fairly large chunk of mem where to load my kernel?
Most kernels are just loaded to 1M. Considering your machine (or vm) has more memory than your kernel plus 1M, it is a safe assumption you'll have a large enough chunk of free memory there. However if your kernel is guaranteed to be smaller than 640k (a microkernel for example), then address 0 is a better choice imho.

Cheers,
bzt


Top
 Profile  
 
 Post subject: Re: Grub Multiboot2 Kernel Physical Address
PostPosted: Sat Jan 11, 2020 3:07 am 
Online
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
Lagor wrote:
So I guess my question now is: can I always assume that starting from 1MB I will have a fairly large chunk of mem where to load my kernel?

Even on very old computers, there's typically 14MB of RAM starting at 1MB. That should be more than enough for your kernel.

bzt wrote:
Yet another reason why not to use Grub :-) Your assumption (if the kernel doesn't fit, put it into the first big enough free block) makes a lot more sense imho.

GRUB supports relocatable binaries, but you must tell GRUB that your binary is relocatable and you must perform the relocation yourself.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], FrankRay78 and 71 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group