New grub issue : strange memory map

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
User avatar
Neolander
Member
Member
Posts: 228
Joined: Tue Mar 23, 2010 3:01 pm
Location: Uppsala, Sweden
Contact:

New grub issue : strange memory map

Post by Neolander »

Hello again !

Today, I'm here because while trying to handle the multiboot information structure, I encountered a new puzzling behavior. This time, I was trying to get a memory map.

According to the multiboot doc...
If bit 6 in the `flags' word is set, then the `mmap_*' fields are
valid, and indicate the address and length of a buffer containing a
memory map of the machine provided by the BIOS. `mmap_addr' is the
address, and `mmap_length' is the total size of the buffer. The buffer
consists of one or more of the following size/structure pairs (`size'
is really used for skipping to the next pair):

+-------------------+
-4 | size |
+-------------------+
0 | base_addr_low |
4 | base_addr_high |
8 | length_low |
12 | length_high |
16 | type |
+-------------------+

where `size' is the size of the associated structure in bytes, which
can be greater than the minimum of 20 bytes. `base_addr_low' is the
lower 32 bits of the starting address, and `base_addr_high' is the
upper 32 bits, for a total of a 64-bit starting address. `length_low'
is the lower 32 bits of the size of the memory region in bytes, and
`length_high' is the upper 32 bits, for a total of a 64-bit length.
`type' is the variety of address range represented, where a value of 1
indicates available RAM, and all other values currently indicated a
reserved area.

The map provided is guaranteed to list all standard RAM that should
be available for normal use.
Okay, so after a long puzzled look, I made some code reading the memory map in order to make sure everything is okay.

Code: Select all

void print_mmap(multiboot_info_t* mbd) {
  unsigned int remaining_mmap;
  memory_map_t* current_mmap;
  unsigned long hack_current_mmap;
  int i;
  
  if((mbd->flags & 64) == 64) {
    remaining_mmap = mbd->mmap_length;
    current_mmap = mbd->mmap_addr;
    i = 0;
    dbg_print_str("Map of the memory :\n");
    while(remaining_mmap > 0) {
      //Display contents
      dbg_print_str("* Memory item ");
      dbg_print_int(i);
      dbg_print_str(" from ");
      dbg_print_uint(current_mmap->base_addr_high);
      dbg_print_chr('/');
      dbg_print_uint(current_mmap->base_addr_low);
      dbg_print_str(" of size ");
      dbg_print_uint(current_mmap->length_high);
      dbg_print_chr('/');
      dbg_print_uint(current_mmap->length_low);
      if(current_mmap->type == 1) dbg_print_str(" is available\n");
      else dbg_print_str(" is reserved\n");
      //Move to next mmap item
      hack_current_mmap = (unsigned long) current_mmap;
      hack_current_mmap += current_mmap->size+4;
      remaining_mmap -= current_mmap->size+4;
      current_mmap = (memory_map_t*) hack_current_mmap;
      ++i;
    }
  } else dbg_print_str("No memory map found\n");
}
The results are displayed in the following screen shot :
Memory map
Memory map
It looks like there are some... holes... in that memory map. Item 0 is fine, item 1 sounds good, but item 2 has bad starting address : I'm lacking some memory inbetween !
Item 2->Item 3->Item 4 transitions look fine, but Item 4->Item 5 transition look strange, too.

That's strange, and according to the Multiboot doc,
The map provided is guaranteed to list all standard RAM that should
be available for normal use.
Shouldn't RAM take the form of a contiguous address space ? Is my code wrong ?

(My OS will surely rule the world someday, but in meantime it's very hard to go back to the "noob programmer" state :mrgreen: )
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: New grub issue : strange memory map

Post by Gigasoft »

The memory map is correct. RAM isn't generally contiguous. For example, addresses 0xA0000 - 0xBFFFF are reserved for a display adapter, and the RAM at these addresses is disabled. RAM at 0x1F00000 - 0x1FFFFFF is also sometimes disabled, so that the area can be assigned to ISA devices. In a computer with more than 4GB of RAM, there is also a hole reserved for PCI devices and for the BIOS (and in this case the RAM addresses are usually decoded in a way that doesn't waste any RAM).
User avatar
Neolander
Member
Member
Posts: 228
Joined: Tue Mar 23, 2010 3:01 pm
Location: Uppsala, Sweden
Contact:

Re: New grub issue : strange memory map

Post by Neolander »

Thanks for the explanation. So what GRUB tells me is only about RAM, and I see holes in the memory map because it doesn't takes into account memory-mapped devices and other reserved address ranges which are not RAM (which sounds logical when I think of it). Fine.

One more question : does GRUB take my kernel into account as reserved memory in his memory map ? Or do I have to discover where it's located all by myself, since for some reason GRUB won't give me the ELF headers ?
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: New grub issue : strange memory map

Post by Gigasoft »

No, the memory map is taken from the BIOS and no changes are made to it.

The kernel is always loaded at the same address, which is the one specified in the Multiboot header. Therefore, you already know in advance where the kernel is loaded.
User avatar
Neolander
Member
Member
Posts: 228
Joined: Tue Mar 23, 2010 3:01 pm
Location: Uppsala, Sweden
Contact:

Re: New grub issue : strange memory map

Post by Neolander »

Gigasoft wrote:No, the memory map is taken from the BIOS and no changes are made to it.

The kernel is always loaded at the same address, which is the one specified in the Multiboot header. Therefore, you already know in advance where the kernel is loaded.
I do not specify it at the moment... I thought that grub could find a good place for it all by itself and then tell me where it put it. Guess I'm going to change my way of thinking ;)
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: New grub issue : strange memory map

Post by Gigasoft »

Ah. In the ELF case, the load address taken from the ELF header, but it's still a fixed value. To determine the image size, you probably have to use a linker script.
Post Reply