PDT Cloning

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
oib111
Member
Member
Posts: 55
Joined: Fri Sep 04, 2009 12:39 am

PDT Cloning

Post by oib111 »

This is something that's been bothering me for a while. So when I initialize my Virtual Memory Manager I have to setup an initial PDT, and it has 0-1MB and 2-4MB in memory identity mapped, and the kernel is remapped to 0x80000000. So when I clone a directory each task has the kernel mapped at 0x80000000 which is good, but they would also have 0-1MB and 2-4MB of memory being identity mapped which causes obvious problems. Now see, I need that to be identity mapped in the beginning. So my question is how do I get around this? Should I setup a separate PDT that has the kernel remapped and a heap, but nothing identity mapped, and when a process is started I clone that directory?
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: PDT Cloning

Post by neon »

Hello,

Why not just unmap that region of the virtual address space when you are in the kernel? I have to assume that your Memory Manager provides method for unmapping and freeing frames for reuse.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
oib111
Member
Member
Posts: 55
Joined: Fri Sep 04, 2009 12:39 am

Re: PDT Cloning

Post by oib111 »

It does, but why "when you are in the kernel"? Do you mean when I clone a task unmap the pages?
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: PDT Cloning

Post by pcmattman »

Hi,
Do you mean when I clone a task unmap the pages?
Technically when you clone a task you keep the pages from the previous task in the address space. This is the general concept of "forking" a task (of course, there's more to clone than just the address space, but you get the idea). When you create a new executable image (in POSIX, "exec" functions) you clean out the old address space - all the pages below 0x80000000 in your case - and map in the new image into the now-clean address space.
It does, but why "when you are in the kernel"
When you actually kick off your first userspace application from within the kernel, you would clone the kernel address space and use a similar method to your "executable image creation" (ala POSIX "exec") as mentioned above. This way the regions below 0x80000000 get cleaned out and you load an executable image in one fell swoop!

Note: When I talk about POSIX "exec" and "fork" I'm merely using them as a descriptive example as they're functions you should already be familiar with. There is no obligation for you to call these functions "exec*" and "fork" in your kernel, nor do you have to follow POSIX guidelines if you don't want to. It all depends on how your kernel is designed. Referencing the functions is simply the easiest way to show the two different operations that occur.

Cheers,
Matt
oib111
Member
Member
Posts: 55
Joined: Fri Sep 04, 2009 12:39 am

Re: PDT Cloning

Post by oib111 »

Sorry, I phrased that wrong. I meant when I start a new task, I would unmap the identity mapped pages in the kernel PDT and then clone that PDT.
Hangin10
Member
Member
Posts: 162
Joined: Wed Feb 27, 2008 12:40 am

Re: PDT Cloning

Post by Hangin10 »

Why not just unmap it when you're done with it in initialization (ie after your EIP is in the higher half kernel) ?
oib111
Member
Member
Posts: 55
Joined: Fri Sep 04, 2009 12:39 am

Re: PDT Cloning

Post by oib111 »

I don't unmap it after initialization of the VMM because I need it for working with the screen as well as dynamic memory allocation until I setup a heap.
Hangin10
Member
Member
Posts: 162
Joined: Wed Feb 27, 2008 12:40 am

Re: PDT Cloning

Post by Hangin10 »

Then after you've set up your heap why not unmap it.

Ultimately it boils down to why not just unmap it before you get to creating any tasks, then you don't have to worry about it. If you have to create tasks before unmapping the identity-mapped kernel, that would seem to indicate that you are doing things in a wrong (or at least difficult to organize) manner.
oib111
Member
Member
Posts: 55
Joined: Fri Sep 04, 2009 12:39 am

Re: PDT Cloning

Post by oib111 »

I will unmap after I setup the heap, and I'll just remap the first 1MB of memory to 0x80100000 or 0x80200000.
Post Reply