Page 1 of 1

Parsing Memory Card, which gave GRUB.

Posted: Fri Oct 02, 2015 1:32 am
by kailot2
Hello, I for the first time on your forum, and I have a terrible English =)
I want to know why my code does not work, which must be considered a common memory map provided by GRUB

Code: Select all


typedef struct 
{
	udword size;
	uqword base_addr;
	uqword lengthl;
	udword type;
} MMAP_UNIT;
MMAP_UNIT *mmap_unit;
udword k_mem_size;
int k_mem_init (MI_INF_STRUCT *MI) //MI - pointer to MultiBoot Information structure 
{
	mmap_unit =  MI->mmap_addr;
	while (mmap_unit->type)
	{
		k_mem_size += ((mmap_unit->lengthl)-(mmap_unit->base_addr)) ;
		mmap_unit += ((mmap_unit->size) + 4);		
	}

}

Re: Parsing Memory Card, which gave GRUB.

Posted: Fri Oct 02, 2015 7:40 am
by Icee
Because pointer arithmetic. Consult a C book. Also, you are missing an __attribute__((packed)). Also, the expression "(mmap_unit->lengthl)-(mmap_unit->base_addr)" is not what you wanted there.

Re: Parsing Memory Card, which gave GRUB.

Posted: Fri Oct 02, 2015 7:47 am
by kailot2
I have realized my mistake, I do not have programming experience in C, before I wrote in asm and affect habits. the size of the page table entry is not necessarily equal to 20 bytes, so I can not understand how I should increase the value of the pointer

Re: Parsing Memory Card, which gave GRUB.

Posted: Fri Oct 02, 2015 7:52 am
by kailot2
Icee wrote: Also, you are missing an __attribute__((packed)).
can you more about this?

Re: Parsing Memory Card, which gave GRUB.

Posted: Fri Oct 02, 2015 8:06 am
by Icee
kailot2 wrote:I can not understand how I should increase the value of the pointer
Something like this:

Code: Select all

ptr = (const memmap_item *) (((uintptr_t) ptr) + ptr->size + 4);
kailot2 wrote:
Icee wrote: Also, you are missing an __attribute__((packed)).
can you more about this?
By default, fields are aligned so that accesses are efficient (and on some platforms misaligned access is not available at all). If you come from x86 assembly background, consider MOVDQU vs MOVDQA. In your case, there must be no extra padding between the fields, so an attribute is necessary to indicate that to the compiler:

Code: Select all

struct MyStruct { ... } __attribute__((packed));

Re: Parsing Memory Card, which gave GRUB.

Posted: Fri Oct 02, 2015 8:14 am
by kailot2
Thanks for answer,i will understand =)