Swapping out paging structures?

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
zerosum
Member
Member
Posts: 63
Joined: Wed Apr 09, 2008 6:57 pm

Swapping out paging structures?

Post by zerosum »

Hi all,

Soon I will be trying to implement my first "real step" towards an OS; memory management.

I'm wondering, how do you all store your paging structures? Do you permit them to be swapped out or do they always reside in physical memory?

I understand that they need to be in physical memory when in use, but otherwise the currently unused paging structures could theoretically be swapped out.

Of course, having to swap these structures back into physical memory every time the tasks are run would incur some overhead, possibly a lot of overhead.

This probably wouldn't be such an issue with a 32-bit OS, but in long mode, with 48-bit addresses, an application could theoretically use up a hell of a lot of virtual memory. If such an application's paging structures were permanently stored in physical memory and not permitted to be swapped out, this could vastly reduce the physical memory available to the system, potentially preventing further processes starting etc.

Realistically, such an application is probably unlikely to exist for the near future, but the potential issue is still there and if nothing else, could be exploited by malicious code.

Thoughts / corrections / suggestions would be appreciated :-)

Cheers,
Lee
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Hi,

At present, I just store all my paging structures in virtual RAM. Each task has its own PML4. When that's not current, all the pages used for that task space also become inactive.

As for swapping - I haven't got there yet (most of my OS probably still runs from the CPU's cache ATM!), but would suggest that your paging structures should be swapped out only as a very last resort. If you have to do that too much, there is probably more running than the PC can sensibly cope with!

Cheers,
Adam
User avatar
zaleschiemilgabriel
Member
Member
Posts: 232
Joined: Mon Feb 04, 2008 3:58 am

Post by zaleschiemilgabriel »

Look at it this way: You haven't even started to write your OS, and you're already thinking about malicious code (viruses ) :? exploiting it's weaknesses? :lol:
Well, the best answer to your question would have to come with another question: Do you have storage drivers for your OS? If so, nothing should stop you from implementing swapping, and it would actually be recommended to do so before moving on with other parts of your OS. If you don't have drivers yet, you probably have no place to swap to, so if you run into "not enough memory" types of situations, just display an error message and halt the CPU. :wink:
As for the paging structures, only the currently used ones need to be in physical memory (the ones whose physical addresses are in the GPTR).
The ones used for user-level processes (ring 3) can be swapped/replaced when the physical memory gets scarce, but I would keep any kernel-level tables in-memory at all times, for safety reasons.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

zaleschiemilgabriel wrote:You haven't even started to write your OS, and you're already thinking about malicious code (viruses ) :? exploiting it's weaknesses? :lol:
Perhaps more people should do the same. I understood that the OP was not necessarily just talking about viruses, but also 'memory hog' applications.

Cheers,
Adam
zerosum
Member
Member
Posts: 63
Joined: Wed Apr 09, 2008 6:57 pm

Post by zerosum »

Thanks guys :-)

The reason I'm asking these questions is because I believe it would be better to plan well in the first instance than to have code which needs modification every two days as you add or extend things.

And no, I do not have storage drivers written yet. Of course this makes an actual swap impossible at this stage. The current plan is to write my memory management stuff, complete to the point where it does everything except an actual swap, where it will then print an error message and halt, until such a time as I have storage drivers written.

Cheers,
Lee
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Post by jal »

zerosum wrote:The reason I'm asking these questions is because I believe it would be better to plan well in the first instance than to have code which needs modification every two days as you add or extend things.
Hear hear!


JAL
Post Reply