OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 5:30 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Incorrect data in case of upper memory detect
PostPosted: Tue Dec 10, 2019 10:22 am 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 293
Hello.

I'm trying using GRUB to get the total RAM size.

Code:
size_t ram_len;

while(memory_map < MBI->mmap_addr + MBI->mmap_length) {
   memory_map = (multiboot_memory_map_t*)((unsigned int)memory_map + memory_map->size + sizeof(memory_map->size));
        ram_len += memory_map->len;
        char* str_result;
        itoa(memory_map->addr, str_result, 16);
        write_serial_str("addr of section = ");
        write_serial_str(str_result);
        write_serial('\n');
    }


As a result, I get incorrect addresses(I expect to see such as in this table) and incorrect memory size.

Here is the output:
Code:
addr of section = 9fc00
addr of section = f0000
addr of section = 100000
addr of section = 7fe0000
addr of section = -40000
addr of section = 0
mem_lower = 639
mem_upper = 129920
ram_len = 133497856


Link to the repository.


Top
 Profile  
 
 Post subject: Re: Incorrect data in case of upper memory detect
PostPosted: Tue Dec 10, 2019 10:41 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
Code:
while(memory_map < MBI->mmap_addr + MBI->mmap_length) {
   memory_map = (multiboot_memory_map_t*)((unsigned int)memory_map + memory_map->size + sizeof(memory_map->size));

You are skipping the first entry of the memory map, and reading past the end of the memory map.

Code:
        char* str_result;

You are declaring a null pointer for your temporary variable. You should declare an array instead. Make sure the array is large enough to store the entire string, including the null terminator.

Code:
        itoa(memory_map->addr, str_result, 16);

Your itoa() function prints signed integers, but the address is an unsigned integer. Your itoa() function handles 32 bits, but the values you want to print are 64 bits.

There may be other problems with your code as well; these are only the ones I spotted after a quick look.


Top
 Profile  
 
 Post subject: Re: Incorrect data in case of upper memory detect
PostPosted: Tue Dec 10, 2019 11:26 am 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 293
Octocontrabass wrote:
Code:
while(memory_map < MBI->mmap_addr + MBI->mmap_length) {
   memory_map = (multiboot_memory_map_t*)((unsigned int)memory_map + memory_map->size + sizeof(memory_map->size));

You are skipping the first entry of the memory map, and reading past the end of the memory map.


How do I fix this? This is the code from the wiki and I didn't think it was a mistake.


Top
 Profile  
 
 Post subject: Re: Incorrect data in case of upper memory detect
PostPosted: Tue Dec 10, 2019 12:00 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
mrjbom wrote:
How do I fix this? This is the code from the wiki and I didn't think it was a mistake.

The code in the wiki uses "..." to indicate where you are supposed to insert your own code.


Top
 Profile  
 
 Post subject: Re: Incorrect data in case of upper memory detect
PostPosted: Tue Dec 10, 2019 12:32 pm 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 293
Octocontrabass wrote:
mrjbom wrote:
How do I fix this? This is the code from the wiki and I didn't think it was a mistake.

The code in the wiki uses "..." to indicate where you are supposed to insert your own code.


Have I forgotten something? What is it?


Top
 Profile  
 
 Post subject: Re: Incorrect data in case of upper memory detect
PostPosted: Tue Dec 10, 2019 12:54 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
I'm not sure how else to explain it. The wiki example code uses "..." to show you where you should insert your code. You did not follow those directions, so the example doesn't work.

Not all entries in the memory map are RAM. You will need to check the entry type to see if it should count towards the total RAM.

Your code doesn't handle odd cases like overlapping memory ranges or ranges that aren't aligned to page boundaries, but those are rare.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 67 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