Accessing physical memory in paging mode

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
User avatar
deadmutex
Member
Member
Posts: 85
Joined: Wed Sep 28, 2005 11:00 pm

Accessing physical memory in paging mode

Post by deadmutex »

I am trying to figure out how to read/write directly to physical memory while in paging mode. For example, if I wanted to create a new address space and modify its page tables while in kernel mode, I would need access to physical mem. I thought about using 1:1 mappings of physical pages to virtual pages, but I ran into problems when large amounts of memory were present(address space wastage, kernel gets written over). Maybe I'm totally missing a simple point?
Da_Maestro
Member
Member
Posts: 144
Joined: Tue Oct 26, 2004 11:00 pm
Location: Australia

Re: Accessing physical memory in paging mode

Post by Da_Maestro »

well the 1:1 method is the only way to do it sonny :-p

You just need to keep track of your page mappings better, and write the code to check that you're not overwriting your kernel

My kernel is in 1:1 mapped memory, the only memmory that I remap is above 1Mb where my processes reside
Last edited by Da_Maestro on Tue Jan 10, 2006 12:00 am, edited 1 time in total.
Two things are infinite: The universe and human stupidity. But I'm not quite sure about the universe.
--- Albert Einstein
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: Accessing physical memory in paging mode

Post by JAAman »

why do you need to write to physical memory? just write it within virtual memory (then you also know where to find it), then check the mappings -- you should have a lookup function, which converts virtual->physical by reading the page tables to find the appropriet physical address, then use the returned value to populate your destination CR3

if you mean modifying the current page tables, then they should always be mapped to a fixed location within each address space (there is a trick that saves space by mapping it to the very top_of_mem -- but i cannot remember it offhand) -- every address space maps its own page tables to the same virtual address, making it simple to modify simply by writing to that address which (unlike the physical address) will always be a constant
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Re: Accessing physical memory in paging mode

Post by carbonBased »

If you assign the page directory as the last entry in itself (ie, it becomes the last page table), then converting from linear to physical addresses is quite easy.

--Jeff
User avatar
deadmutex
Member
Member
Posts: 85
Joined: Wed Sep 28, 2005 11:00 pm

Re: Accessing physical memory in paging mode

Post by deadmutex »

Ok, I reworked my memory design. I mapped the first 8MB of physical mem at 0xF0000000 in all address spaces for the kernel to have access to video RAM, IVT, DMA mem, etc. during syscalls. I mapped the page dir into itself in order for the kernel to modify the PDEs and PTEs, but this trick only seems to work in the current address space.

Is it possible to load a virtual address into CR3??

EDIT:
It seems that CR3 must contain a physical address. If I needed to duplicate the current address space and then modify the duplicate, would I have to 1:1 map a physical page, copy the current pdir into the duplicate, switch to the duplicate pdir, modify entries, and then switch back to the original? This would result in a huge TLB penalty....
Last edited by deadmutex on Tue Jan 10, 2006 12:00 am, edited 2 times in total.
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: Accessing physical memory in paging mode

Post by JAAman »

no you don't, just place the new tables into memory (at any virtual address), then read the current page tables to find what the physical address is for the virtual page you are using, then use that as the physical address to enter into CR3, (and don't forget to unmap the virtual address)
User avatar
deadmutex
Member
Member
Posts: 85
Joined: Wed Sep 28, 2005 11:00 pm

Re: Accessing physical memory in paging mode

Post by deadmutex »

Ok, I understand now. I was confusing the terms: page table and page directory entry(PDE)....
The page directory contains 1024 4-byte PDEs. Each PDE has information about a page table including its phys address. One page table has 1024 4-byte PTEs and the PTE has info about the page.
Post Reply