OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 12:20 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Continue from next instruction after a Page Fault
PostPosted: Sat Jan 16, 2016 6:03 am 
Offline
Member
Member

Joined: Wed Dec 23, 2015 10:42 pm
Posts: 73
So this is another stupid query from me :3 don't be angry and as I am a noob here, please explain me the things in an easy way :p

I have been re-implementing the Paging and Memory allocation systems for my hobby OS (used James M's **** paging before :3)
But there is a need for me now. 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.

Now everything works fine except the RETURN FROM PAGE FAULT thing. It returns back to the instruction which caused it . But how to make it proceed to the instruction next? Because otherwise its not fruitful. And also, I don't want to temporary page fault handler I use here to allocate pages for the memory location because if the frame I am referring to already has a read only page or the memory location is simply not available, I would get in trouble. So please help me here. I am other alternatives, But I currently want to do it this way due to reasons :/
If someone can help me, I would get a precise mem map of any memory region. I don't want the mem map from grub itself :3 Please help me in making the Page Fault handler go to next instruction :p
EDIT: THE METHOD FOR MAPPING I USED ABOVE IS REALLY A BAD IDEA. NOT USING IT. BUT STILL I WANT TO KNOW HOW TO RESUME WORK AFTER A PAGE FAULT, FROM THE NEXT LINE OF CODE??
NOTE: I am bad at assembly, Programming in C.

_________________
The best method for accelerating a computer is the one that boosts it by 9.8 m/s2.
My OS : https://github.com/AshishKumar4/Aqeous


Last edited by ashishkumar4 on Sat Jan 16, 2016 8:10 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Continue from next instruction after a Page Fault
PostPosted: Sat Jan 16, 2016 6:16 am 
Offline
Member
Member

Joined: Sat Nov 21, 2009 5:11 pm
Posts: 852
That is not how page faults work at all.


Top
 Profile  
 
 Post subject: Re: Continue from next instruction after a Page Fault
PostPosted: Sat Jan 16, 2016 6:26 am 
Offline
Member
Member

Joined: Wed Dec 23, 2015 10:42 pm
Posts: 73
:/ I was asking a question :/ but according to what I know, Page fault is called when I try to write to a location which has not been paged or which has been paged as read only etc. That's only what I have learnt and read :/ its a interrupt basically? I register my page fault handler to interrupt 14 and it works even :/ but still, I would be glad if you correct my concepts :p

_________________
The best method for accelerating a computer is the one that boosts it by 9.8 m/s2.
My OS : https://github.com/AshishKumar4/Aqeous


Top
 Profile  
 
 Post subject: Re: Continue from next instruction after a Page Fault
PostPosted: Sat Jan 16, 2016 6:34 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5137
ashishkumar4 wrote:
If someone can help me, I would get a precise mem map of any memory region. I don't want the mem map from grub itself :3

MANUALLY PROBING FOR MEMORY CAN DAMAGE YOUR COMPUTER.

If you don't want to use the memory map from GRUB, you can ask the BIOS directly. You have to be in real mode to do that, so it's much better to let your bootloader (GRUB) deal with the BIOS.


Top
 Profile  
 
 Post subject: Re: Continue from next instruction after a Page Fault
PostPosted: Sat Jan 16, 2016 7:06 am 
Offline
Member
Member

Joined: Wed Dec 23, 2015 10:42 pm
Posts: 73
ya I know that. if I do this in physical mem, virtualbox automatically shuts down the system. But I am probing in virtual memory after enabling paging. I guess anything illegal would through a page fault, right? if I am wrong, I still need a way of moving to next inst. after a page fault, for other purposes

_________________
The best method for accelerating a computer is the one that boosts it by 9.8 m/s2.
My OS : https://github.com/AshishKumar4/Aqeous


Top
 Profile  
 
 Post subject: Re: Continue from next instruction after a Page Fault
PostPosted: Sat Jan 16, 2016 7:11 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
Quote:
I guess anything illegal would through a page fault, right?
No. You set up the page tables, so you determine what goes to what physical memory. There is no magic filtering involved by the mere enabling of paging.

Quote:
I still need a way of moving to next inst. after a page fault, for other purposes
To be honest, I don't believe you do once you know how things really work.

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
 Post subject: Re: Continue from next instruction after a Page Fault
PostPosted: Sat Jan 16, 2016 7:19 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

ashishkumar4 wrote:
ya I know that. if I do this in physical mem, virtualbox automatically shuts down the system. But I am probing in virtual memory after enabling paging. I guess anything illegal would through a page fault, right? if I am wrong, I still need a way of moving to next inst. after a page fault, for other purposes


Why?

There are only 3 reasons I can think of:
  • C style "signals" (SIGSEGV)
  • A messy extension to a language's run-time exceptions (e.g. C++ exceptions; like "try { ... } catch (pageFault) { ... }")
  • Unit testing your kernel to make sure it got page permissions correct

Of these, the first 2 need a more general approach (e.g. starting with some sort of "send signal/exception to user space" that can be used for many different things, and code in standard library/ies to support however the kernel implemented it).

The third reason (unit testing) is a special case, not something you'd leave in the OS forever.


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re: Continue from next instruction after a Page Fault
PostPosted: Sat Jan 16, 2016 7:50 am 
Offline
Member
Member

Joined: Wed Dec 23, 2015 10:42 pm
Posts: 73
Thanks Brenden and Combuster. Yeah its the reason 3 :p I am just experimenting. And Combuster, sorry I got confused with that stuff, because u see I tend to do more experimenting before reading much :p that's why I call myself stupid. But I came to that conclusion just because when I was in physical memory, in virtualbox, when I accessed something illegal, the virtualbox just restarted. But when I tried to FIRST make some pages for that location and THEN enable paging (as the JamesM paging, I used earlier, does for first few used memory spaces), and then try to access the same location which crashed the virtual box, it gives me a page fault only but it does not crashes or anything like that. Things I put in the page fault function run uninterrupted.
And I still want to know how to make the page fault handler return and resume work from the NEXT line of code :/

And thanks for reminding, I want something like the try and catch in c++ and c# in c if its possible (anything, anyway) or shall I do this stuff in c++ itself?

_________________
The best method for accelerating a computer is the one that boosts it by 9.8 m/s2.
My OS : https://github.com/AshishKumar4/Aqeous


Top
 Profile  
 
 Post subject: Re: Continue from next instruction after a Page Fault
PostPosted: Sat Jan 16, 2016 8:08 am 
Offline
Member
Member

Joined: Wed Dec 23, 2015 10:42 pm
Posts: 73
Okay I got it, Its definetly not a good and smart way to map memory. Sorry for this, I am not gonna use this method, gonna use the grub.

But still I want to know how to resume work after a page fault

_________________
The best method for accelerating a computer is the one that boosts it by 9.8 m/s2.
My OS : https://github.com/AshishKumar4/Aqeous


Top
 Profile  
 
 Post subject: Re: Continue from next instruction after a Page Fault
PostPosted: Sat Jan 16, 2016 8:16 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

ashishkumar4 wrote:
Thanks Brenden and Combuster. Yeah its the reason 3 :p


In that case; cheat.

You can put the "address page fault handler should return to" in a register, and then have a special/temporary page fault handler that replaces it's "return EIP" (on the stack) with that register. That way you can do something like:

Code:
    mov edi,.faulted
    mov eax,0x12345678]
    clc                      ;No page fault
    ret

.faulted
    stc                      ;Page fault occurred
    ret


ashishkumar4 wrote:
And thanks for reminding, I want something like the try and catch in c++ and c# in c if its possible (anything, anyway) or shall I do this stuff in c++ itself?


If you must; I'd only support signals (e.g. SIGSEGV) in the kernel. If a C++ compiler/library wants to support "CPU exceptions as language exceptions" (which isn't standard for C++) then it can implement it on top of the SIGSEGV signal handler.


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re: Continue from next instruction after a Page Fault
PostPosted: Mon Jan 18, 2016 7:52 pm 
Offline

Joined: Sat Sep 01, 2012 7:17 am
Posts: 10
You could also read the EIP instruction stream and determine what next EIP should be and set it manually :P


Top
 Profile  
 
 Post subject: Re: Continue from next instruction after a Page Fault
PostPosted: Tue Jan 19, 2016 10:38 am 
Offline
Member
Member
User avatar

Joined: Sun Sep 19, 2010 10:05 pm
Posts: 1074
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.

_________________
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 97 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group