Mapping memory via Grub taking too long

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
barkingtreefrog
Posts: 2
Joined: Fri Sep 13, 2013 12:13 pm

Mapping memory via Grub taking too long

Post by barkingtreefrog »

I've got to the point where I'm detecting memory, I've decided to use multiboot.h and get my memory map from Grub following the tutorial I'm using this code

Code: Select all

typedef struct multiboot_memory_map {
	unsigned int size;
	unsigned int base_addr_low,base_addr_high;
// You can also use: unsigned long long int base_addr; if supported.
	unsigned int length_low,length_high;
// You can also use: unsigned long long int length; if supported.
	unsigned int type;
} multiboot_memory_map_t;
 
int main(multiboot_info* mbt, unsigned int magic) {
	...
	multiboot_memory_map_t* mmap = mbt->mmap_addr;
	while(mmap < mbt->mmap_addr + mbt->mmap_length) {
		...
		mmap = (multiboot_memory_map_t*) ( (unsigned int)mmap + mmap->size + sizeof(unsigned int) );
	}
	...
}
It works fine but takes over 5 minutes. I'm running my iso in VirtualBox. Now here's the odd part. I wrote to the screen the the loop at one point because I thought my OS had froze. If I write to the screen in that loop then the entire process takes less than 2 seconds. If I don't have anything in my loop by what is above it takes way, way to long. Is there something simple I might be overlooking here?

Also, I only have 128MB allocated for my virtual machine.
Kortath
Member
Member
Posts: 57
Joined: Sat Sep 07, 2013 11:23 am

Re: Mapping memory via Grub taking too long

Post by Kortath »

barkingtreefrog wrote:I've got to the point where I'm detecting memory, I've decided to use multiboot.h and get my memory map from Grub following the tutorial I'm using this code

Code: Select all

typedef struct multiboot_memory_map {
	unsigned int size;
	unsigned int base_addr_low,base_addr_high;
// You can also use: unsigned long long int base_addr; if supported.
	unsigned int length_low,length_high;
// You can also use: unsigned long long int length; if supported.
	unsigned int type;
} multiboot_memory_map_t;
 
int main(multiboot_info* mbt, unsigned int magic) {
	...
	multiboot_memory_map_t* mmap = mbt->mmap_addr;
	while(mmap < mbt->mmap_addr + mbt->mmap_length) {
		...
		mmap = (multiboot_memory_map_t*) ( (unsigned int)mmap + mmap->size + sizeof(unsigned int) );
	}
	...
}
It works fine but takes over 5 minutes. I'm running my iso in VirtualBox. Now here's the odd part. I wrote to the screen the the loop at one point because I thought my OS had froze. If I write to the screen in that loop then the entire process takes less than 2 seconds. If I don't have anything in my loop by what is above it takes way, way to long. Is there something simple I might be overlooking here?

Also, I only have 128MB allocated for my virtual machine.

This may seem a little off topic, and not sure if this helps or not, but I setup my VirtualBox with 256 MB ram and the video with 32 MB of RAM. They seemed to be the "sweet" spot for my projects.
User avatar
darkinsanity
Member
Member
Posts: 45
Joined: Wed Sep 17, 2008 3:59 am
Location: Germany

Re: Mapping memory via Grub taking too long

Post by darkinsanity »

Hm...
Did you make sure that it's the loop that is taking so long? Did you add output-statements directly before and after the loop?
Did you try it in another emulator?
Whats the complete code inside the loop?

Also I recommend that you use the structs defined in multiboot.h instead of creating your own.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Mapping memory via Grub taking too long

Post by Love4Boobies »

The code you've showed us is not sufficient to reproduce the problem. Could you link us to your repository?

Until you do, you should still fix some unrelated problems with your code:
  • As C doesn't require that structures be packed, yours only works by sheer luck and can break as soon as you change the ABI. Either do proper serialization, using an array, or (sigh) use a compiler-specific extension, such as GCC's __attribute__((packed)).
  • Similarly, you should be using fixed-width integer types, as the ones you're using don't have fixed widths or sizes; they're ABI-dependent.
  • Finally, calling conventions also vary from ABI to ABI. It's safest to pass magic and mbt as external objects rather than as parameters.
  • Given that multiboot_memory_map_t is not an opaque type, why do you typedef it?
  • Why does your main function expect to have a return value?
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Kortath
Member
Member
Posts: 57
Joined: Sat Sep 07, 2013 11:23 am

Re: Mapping memory via Grub taking too long

Post by Kortath »

Maybe I am reading this wrong.. but.. what exactly is multiboot_memory_map_t returning ? Because when you call this, your not assigning anything to multiboot_memory_map_t and so nothing is being returned.

Code: Select all

typedef struct multiboot_memory_map {
   unsigned int size;
   unsigned int base_addr_low,base_addr_high;
   unsigned int length_low,length_high;
   unsigned int type;
} multiboot_memory_map_t;
Also....
unless I just don't know C well enough, which is also possible.. ( winks ), in all the books on C that I have read, you return from a function like this ( pseudo code )..

NOTE : I am assuming your coding this in C.

Code: Select all

your_function {
     return  what_you_want_to_return;
}
EXAMPLE :
http://www.mycplus.com/tutorials/c-prog ... functions/
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Mapping memory via Grub taking too long

Post by Love4Boobies »

Is Kortath a troll or just incompetent?
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Kortath
Member
Member
Posts: 57
Joined: Sat Sep 07, 2013 11:23 am

Re: Mapping memory via Grub taking too long

Post by Kortath »

Love4Boobies wrote:Is Kortath a troll or just incompetent?
How does this question help ? All your questioning does is provoke a negative reaction and has no baring on the OP's question.

Now to stay on topic, if i am wrong in my earlier reply to the OP, then please explain why.
User avatar
zhiayang
Member
Member
Posts: 368
Joined: Tue Dec 27, 2011 7:57 am
Libera.chat IRC: zhiayang

Re: Mapping memory via Grub taking too long

Post by zhiayang »

Kortath wrote:Maybe I am reading this wrong.. but.. what exactly is multiboot_memory_map_t returning ? Because when you call this, you're not assigning anything to multiboot_memory_map_t and so nothing is being returned.

Code: Select all

typedef struct multiboot_memory_map {
   unsigned int size;
   unsigned int base_addr_low,base_addr_high;
   unsigned int length_low,length_high;
   unsigned int type;
} multiboot_memory_map_t;
Also....
unless I just don't know C well enough, which is also possible.. ( winks ), in all the books on C that I have read, you return from a function like this ( pseudo code )..

NOTE : I am assuming you're coding this in C.

Code: Select all

your_function {
     return  what_you_want_to_return;
}
EXAMPLE :
http://www.mycplus.com/tutorials/c-prog ... functions/



Look again.
typedef.
struct.
_t.

That is clearly *NOT* a function.
Either stop trolling, or go read up on C again.
There is no direct assignment, nothing to indicate a function call (); or anything of the sort.

Also:
I don't know where you're from, but every god-damned school should allocate at least one hour per week to teach children the difference between

YOUR
and
YOU'RE

in english lesson.
Kortath
Member
Member
Posts: 57
Joined: Sat Sep 07, 2013 11:23 am

Re: Mapping memory via Grub taking too long

Post by Kortath »

requimrar wrote:
Kortath wrote:Maybe I am reading this wrong.. but.. what exactly is multiboot_memory_map_t returning ? Because when you call this, you're not assigning anything to multiboot_memory_map_t and so nothing is being returned.

Code: Select all

typedef struct multiboot_memory_map {
   unsigned int size;
   unsigned int base_addr_low,base_addr_high;
   unsigned int length_low,length_high;
   unsigned int type;
} multiboot_memory_map_t;
Also....
unless I just don't know C well enough, which is also possible.. ( winks ), in all the books on C that I have read, you return from a function like this ( pseudo code )..

NOTE : I am assuming you're coding this in C.

Code: Select all

your_function {
     return  what_you_want_to_return;
}
EXAMPLE :
http://www.mycplus.com/tutorials/c-prog ... functions/



Look again.
typedef.
struct.
_t.

That is clearly *NOT* a function.
Either stop trolling, or go read up on C again.
There is no direct assignment, nothing to indicate a function call (); or anything of the sort.

Also:
I don't know where you're from, but every god-damned school should allocate at least one hour per week to teach children the difference between

YOUR
and
YOU'RE

in english lesson.
Thanks for the info. I clearly misread it as a function.
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Mapping memory via Grub taking too long

Post by dozniak »

requimrar wrote:I don't know where you're from, but every god-damned school should allocate at least one hour per week to teach children the difference between

YOUR
and
YOU'RE

in english lesson.
Five thumbs up. Your right!
Learn to read.
User avatar
zhiayang
Member
Member
Posts: 368
Joined: Tue Dec 27, 2011 7:57 am
Libera.chat IRC: zhiayang

Re: Mapping memory via Grub taking too long

Post by zhiayang »

dozniak wrote:
requimrar wrote:I don't know where you're from, but every god-damned school should allocate at least one hour per week to teach children the difference between

YOUR
and
YOU'RE

in english lesson.
Five thumbs up. Your right!

Hmm? all I see is a wall, there's nothing on my right.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Mapping memory via Grub taking too long

Post by Love4Boobies »

Hmm. There were supposed to be five thumbs there. Who did I send them to then?
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
zhiayang
Member
Member
Posts: 368
Joined: Tue Dec 27, 2011 7:57 am
Libera.chat IRC: zhiayang

Re: Mapping memory via Grub taking too long

Post by zhiayang »

Love4Boobies wrote:Hmm. There were supposed to be five thumbs there. Who did I send them to then?

Oh never mind, I found them in my fridge:

barkingtreefrog
Posts: 2
Joined: Fri Sep 13, 2013 12:13 pm

Re: Mapping memory via Grub taking too long

Post by barkingtreefrog »

darkinsanity wrote:Hm...
Did you make sure that it's the loop that is taking so long? Did you add output-statements directly before and after the loop?
Did you try it in another emulator?
Whats the complete code inside the loop?

Also I recommend that you use the structs defined in multiboot.h instead of creating your own.
I did add output statements before, after, and in the loop. If I place an output statement inside of the loop then the mapping is near instant. Thanks for all of the replies everyone and sorry for the late response. I'll get back to all of you with some more code and/or updates after work tonight. :)
Post Reply