Virtual page unmapping problem

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
mrjbom
Member
Member
Posts: 317
Joined: Sun Jul 21, 2019 7:34 am

Virtual page unmapping problem

Post 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?
Octocontrabass
Member
Member
Posts: 5575
Joined: Mon Mar 25, 2013 7:01 pm

Re: Virtual page unmapping problem

Post 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?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Virtual page unmapping problem

Post 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.
User avatar
mrjbom
Member
Member
Posts: 317
Joined: Sun Jul 21, 2019 7:34 am

Re: Virtual page unmapping problem

Post 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.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Virtual page unmapping problem

Post 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?
linguofreak
Member
Member
Posts: 510
Joined: Wed Mar 09, 2011 3:55 am

Re: Virtual page unmapping problem

Post 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.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Virtual page unmapping problem

Post 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.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
Post Reply