Memory mapping

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.
Rahman

Memory mapping

Post by Rahman »

what parameters should i pass to the function that maps the virtual memory to the physical memory.

any help appreciated.
Tim

Re:Memory mapping

Post 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.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Memory mapping

Post by Neo »

Tim Could you please specify the steps to be followed by a mapping function?
Only Human
Tim

Re:Memory mapping

Post by Tim »

User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Memory mapping

Post 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?
Only Human
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Memory mapping

Post 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...
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Memory mapping

Post 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.
Only Human
Tim

Re:Memory mapping

Post 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))
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Memory mapping

Post 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?
Only Human
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:Memory mapping

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 9:27 pm, edited 1 time in total.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Memory mapping

Post 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 ...
Slasher

Re:Memory mapping

Post by Slasher »

I just reload the CR3 register with the address of the page directory after making any changes to it. ;D
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Memory mapping

Post 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. :-)
Only Human
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Memory mapping

Post 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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Memory mapping

Post 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
Post Reply