Page 1 of 1
Accessing physical memory in paging mode
Posted: Tue Jan 10, 2006 12:00 am
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?
Re: Accessing physical memory in paging mode
Posted: Tue Jan 10, 2006 12:00 am
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
Re: Accessing physical memory in paging mode
Posted: Tue Jan 10, 2006 12:00 am
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
Re: Accessing physical memory in paging mode
Posted: Tue Jan 10, 2006 12:00 am
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
Re: Accessing physical memory in paging mode
Posted: Tue Jan 10, 2006 12:00 am
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....
Re: Accessing physical memory in paging mode
Posted: Wed Jan 11, 2006 12:00 am
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)
Re: Accessing physical memory in paging mode
Posted: Wed Jan 11, 2006 12:00 am
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.