bluemoon wrote:
I have not yet implemented CoW but I would do it this way:
When page fault:
1) Check if the page is present, user bit clear, and write error, otherwise it is not CoW, handle it accordingly.
2) To speed up the identification of the cause (ie. CoW), may consider to use the OS specific bit; but see notes below.
3) loop thru' a kernel data structure that record all CoW pages, with counter, process list and their address. This structure may be implemented with one or more map (for parallel access) with physical address as hash key.
4) If a record is found, handle it accordingly (clone page, release reference, possibility remove entry if zero reference, and remap into address)
5) If no record found, it is basically access error.
Note:
Step 1 is pretty quick and if that passed, there are only two possible scenario - writing to a CoW page, or write error.
Step 2 is optional, since at that point it's either CoW or kill process(write error) - we may assume CoW is the usual case here.
The point is, quickly identify if the cause is CoW, so that time consuming task (like finding the structure) is not performed for irrelevant causes.
I would say like this:
1) Check for write-error. If not write error, not relevant for CoW, so leave
2) Check if CoW bit is set. If not set, not relevant for CoW, so generate page fault exception
3) Lock CoW section
4) Allocate a new page and copy contents to new page. Set new page to R/W and clear CoW
5) In the aliased page structure of the other process, make the page R/W, and clear CoW bit
6) Unlock CoW section