Page 1 of 1

Virtual page unmapping problem

Posted: Wed Jun 17, 2020 3:35 am
by mrjbom
Hi.
I am trying to unmapping a virtual page, as written in wiki, I set pt[ptindex] to null, but when I try to access this page, the system crashes.
That's why I have to put a virtual address with the present flag there.

Code: Select all

pt[ptindex] = (uint32_t)virtualaddr | PAGE_PRESENT;
Why is it written in the wiki that it is enough to set null?

Re: Virtual page unmapping problem

Posted: Wed Jun 17, 2020 3:40 am
by Octocontrabass
mrjbom wrote:when I try to access this page
Why are you trying to access the unmapped page? What do you expect to happen?

Have you checked the Intel or AMD manuals to see what the CPU will do?

Re: Virtual page unmapping problem

Posted: Wed Jun 17, 2020 3:41 am
by iansjack
mrjbom wrote:I am trying to unmapping a virtual page, as written in wiki, I set pt[ptindex] to null, but when I try to access this page, the system crashes.
What do you expect to happen when you try to access an address that you have unmapped? It's not crashing, it's Page Faulting; if this causes a crash this is a limitation of your operating system.

Re: Virtual page unmapping problem

Posted: Wed Jun 17, 2020 3:44 am
by mrjbom
Oh, I guess I got the job logic wrong. I expected that if the PRESENT flag is not present, then trying to access the page will lead to access to the physical page and I will not have any problems.
Sorry, stupid question.

Re: Virtual page unmapping problem

Posted: Thu Jun 18, 2020 7:45 am
by kzinti
mrjbom wrote:Oh, I guess I got the job logic wrong. I expected that if the PRESENT flag is not present, then trying to access the page will lead to access to the physical page and I will not have any problems.
And which physical page did you expect to be accessed? How would the processor / MMU know which page to access?

Re: Virtual page unmapping problem

Posted: Thu Jun 18, 2020 12:34 pm
by linguofreak
kzinti wrote:
mrjbom wrote:Oh, I guess I got the job logic wrong. I expected that if the PRESENT flag is not present, then trying to access the page will lead to access to the physical page and I will not have any problems.
And which physical page did you expect to be accessed? How would the processor / MMU know which page to access?
I think he was thinking that if the PTE for a given linear address has the present bit set, then the address is translated per the PTE, and if the present bit is not set, then no translation is performed and the linear address is used directly as a physical address.

@mrjbom:

The entire point of the present flag is to allow the OS to detect if a program has attempted to access memory that has not been assigned to it. When a program (or the OS itself) tries to access a page with the present flag unset, an interrupt fires and control is passed to the OS (as long as the OS has set up a handler for the page fault exception). The OS can then decide what action to take. If the program wasn't supposed to have access to that memory at all, the appropriate action is generally to terminate the program. If the program was supposed to have access to that memory, but the system had swapped that page to disk because there wasn't enough RAM, the appropriate action is to load that page back into RAM (potentially swapping something else out to make room, depending on whether the system is still low on memory). If the OS hasn't set up a page fault handler, then a double fault will result when the CPU attempts to do the page fault, and if a double fault handler hasn't been set up, then there will be a triple fault and the system will reboot.

Re: Virtual page unmapping problem

Posted: Thu Jun 18, 2020 12:45 pm
by nexos
The CPU can't access pages marked not present. If it could, that would be very bad indeed. You should read more about paging in the Intel manuals, and there is a (fairly) good tutorial here.