Page 1 of 2

Memory mapping

Posted: Mon Dec 22, 2003 8:19 am
by Rahman
what parameters should i pass to the function that maps the virtual memory to the physical memory.

any help appreciated.

Re:Memory mapping

Posted: Mon Dec 22, 2003 8:48 am
by Tim
Whatever it needs. Typically your virtual-to-physical mapping function might look like this:

Code: Select all

void map_virtual_to_physical(const void *virtual, addr_t physical, uint16_t flags);
If your design lets you access any page directory from anywhere, you might want to add a page directory address parameter there too.

PS: I use addr_t for physical addresses and void* for virtual ones. The reason for this is that physical addresses can be longer than virtual addresses; for instance, 36 bits with PAE enabled.

Re:Memory mapping

Posted: Mon Dec 22, 2003 11:24 am
by Neo
Tim Could you please specify the steps to be followed by a mapping function?

Re:Memory mapping

Posted: Mon Dec 22, 2003 11:42 am
by Tim

Re:Memory mapping

Posted: Tue Dec 23, 2003 2:18 am
by Neo
what is wrong with this function which maps any given virtual address to video memory

Code: Select all

void map2v(DWORD virt)
{
  unsigned long a=0,*PD=0xFFFFF000,*PT=0xFFC00000; // self-mapped
  a=((0xB8000>>12)>>2);
  PD[((virt>>22)>>2)] = PT[a]|3;
  PT[a]= 0xB8000|3;
}
I use a PD mapped to itself. And call the function like this

Code: Select all

unsigned long *temp=0xE03FF001;
map2v(*temp); // map2v(0xE03FF001) also does'nt work
*temp='A';
after this i expect to get 'A' displayed on the top left but nothing happens not even a page fault :-X. What is wrong with my function?

Re:Memory mapping

Posted: Tue Dec 23, 2003 8:11 am
by Pype.Clicker
after having modified the page tables, you (almost) always need to do a pginvld instruction to commit the change to the MMU...

Re:Memory mapping

Posted: Tue Dec 23, 2003 11:21 am
by Neo
i'm not very proficient in inline GCC assembly so can someone tell me if this is correct

Code: Select all

__asm__ __volatile__("invlpg %0"::"m"(virt));
where 'virt' is the virtual address passed to the function as argument. (This still does not work but i dont know if it is due to wrong GCC asm or something else).Please help.

Re:Memory mapping

Posted: Tue Dec 23, 2003 11:43 am
by Tim
No -- I made the same mistake originally. "m" should be an l-value, such as a dereferenced pointer. The code you have there invalidates the page that contains the 'virt' variable, which probably isn't what you want.

Instead, do this:

Code: Select all

__asm__ __volatile__("invlpg %0" : : "m" (*(char*) virt))

Re:Memory mapping

Posted: Tue Dec 23, 2003 11:52 am
by Neo
i tried your code out but to no avail. Maybe something is wrong elsewhere? ??? btw is the function doing what i think its doing?

Re:Memory mapping

Posted: Tue Dec 23, 2003 4:18 pm
by Perica
..

Re:Memory mapping

Posted: Tue Dec 23, 2003 4:24 pm
by Pype.Clicker
no. afaik, invlpg requires a memory argument and will use the effective address of its argument as the virtual address to be discarded.

For instance

Code: Select all

   invlpg [0x1000]
invalidates the page that maps DS.base+0x1000 ...

Re:Memory mapping

Posted: Tue Dec 23, 2003 6:55 pm
by Slasher
I just reload the CR3 register with the address of the page directory after making any changes to it. ;D

Re:Memory mapping

Posted: Wed Dec 24, 2003 1:32 am
by Neo
I'm having a tough time with my memory mapping function so could one of you *please* tell me if this is right.
I want to map address 0xD0CF_F001(binary 1101-0000-1100-1111-1111-0000-0000-0001) to video mem(0xB8000).
If i have a PageDir(PD) at 0xC000 and a PageTable(PT) at 0xE000, is the following correct
The physical address 0xC340(0xC000+(0xD0*4)) should have the value 0xE000|3 (i.e. start of PT) and the physical address 0xE0FF(0xE000+(0x3F*4)) should have the value 0xB8000|3 (i.e. video mem address).
Is this correct? if not what is wrong?
(I hope i'm clear)

WISHING EVERYONE HERE A MERRY CHRISTMAS. Take a well deserved break for a day. :-)

Re:Memory mapping

Posted: Wed Dec 24, 2003 2:37 am
by Candy

Code: Select all

_invlpg:
push ebp
mov ebp, esp

mov eax, DWORD [ebp + 8]
invlpg eax

leave
ret
Well, if you'd turn the eax reference into a memory reference, yes, probably. ModRM encodings 0 through BF are allowed, and 0 is amongst those, so [eax] should work.

Re:Memory mapping

Posted: Wed Dec 24, 2003 3:33 am
by Pype.Clicker
Neo wrote: I'm having a tough time with my memory mapping function so could one of you *please* tell me if this is right.
I want to map address 0xD0CF_F001 (binary 1101-0000-11 00-1111-1111 -0000-0000-0001) to video mem(0xB8000).
You'll be unable to do that. 0xD0CF_F001 may be mapped to 0xB8001, but not to 0xB8000.
If i have a PageDir(PD) at 0xC000 and a PageTable(PT) at 0xE000,
Are these logical or physical addresses ?

From your target address, you should be in entry 0x343 (xx11-0100-0011) of your page directory and in entry 0x0FF (xx00-1111-1111) of your page table.

so phys [0xC000 + 0xD0C] should be 0xE003
and phys [0xE000 + 0x3FC] should be 0xB8003