ashishkumar4 wrote:
For some reasons related, I want to deliberately try writing to random memory locations (not random :3) ,after paging all the memory which I gonna use, to check wether it causes a page fault or not. I actually just want to map the whole usable memory by testing for page faults by writing and reading after allocating a page to where I am testing. If there is some or the other problem like read only page, protection violation, etc. because of which I just cant use the memory at that moment, It would naturally raise a Page Fault. Now Page Fault is not a problem here, its actually what I want. I want that when a page fault comes, it puts the value of a global var to say 1. I want then that the page fault function returns to the NEXT instruction after the write operation which caused the problem which would check wether the global var is 1 or not. If its 1, the function should put the global var to 0 back and set the memory location in memory map as not available or if not the case then put the memory location as available.
This sounds like you want exception handlers, something along the lines of:
Code:
try
{
EAX = [EBX];
}
catch
{
// Memory access failed
}
If I understand this behavior correctly, this essentially "registers" the "catch" "function" with the application's context in the OS, so that if the OS catches an exception, it can look up the currently registered exception handler routine and jump to it. Normally, these can be nested, so in that case, it would be a stack of handlers, not just one handler. The OS would just jump to the top of the handler on the top of the stack.
Someone may be able to go into more detail, since I've never implemented one of these, myself.
Also, this may be overkill for what you are looking for, but it's basically a more elaborate version of Brendan's suggestion.