accessing page table of a process

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
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

accessing page table of a process

Post by vjain20 »

Hi,

I am trying to implement multi-tasking in my kernel by following the JamesM tutorial.
The tutorial implements fork() which calls a function - clone_directory() to create
a copy of the address space of the parent process. The clone_directory() function creates new page tables
for the child and copies the data from the parent process to the child process. Now the problem is how to
access the page tables mapped to the page directories of the processes. The code in the tutorial maintains
virtual addresses of the page tables along with their physical addresses in the page directory structure.
However I am not maintaining the virtual addresses of the page tables. Please suggest some
elegant way to access the page tables of a process given its page directory. Presently I just map the
physical address of a page table obtained its from page directory entry to some random virtual address to access the page table.

-Thanks
Vaibhav Jain
- Thanks
Vaibhav jain
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: accessing page table of a process

Post by bluemoon »

Try to avoid accessing page table of other process in the first place, for example, signal the target process to 'do it by itself'.
If it is unavoidable(in case of process cleanup), you may map it and access it with recursive page directory method.
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: accessing page table of a process

Post by vjain20 »

Try to avoid accessing page table of other process in the first place, for example, signal the target process to 'do it by itself'.
Could you please explain the phrase 'do it by itself'? What would be the control flow ?
The tutorial has the methods fork() and clone_directory() in kernel space and the processes are also created in kernel space.
Besides, I haven't implemented signal mechanism in my kernel yet.

Thanks
Vaibhav Jain
- Thanks
Vaibhav jain
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: accessing page table of a process

Post by bluemoon »

For example, when process A create new process B, instead of A adding page into B's table, it's more natural for A to setup a minimal process layout B, and let B to allocate page itself when it is scheduled.

For IPC involve sending pages, instead of injecting pages to remote process, you may just fetch/map pages when the target process is scheduled.

PS. I'm unfamiliar with the tutorials so the method I describes may not fit into your model.
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: accessing page table of a process

Post by vjain20 »

For example, when process A create new process B, instead of A adding page into B's table, it's more natural for A to setup a minimal process layout B, and let B to allocate page itself when it is scheduled.
I am confused here and it might be that I am not clear with the concepts. How can a process create its own pages and access its page tables?
I was thinking that this should be done by the kernel when the fork is called. So there would be a system call for fork which will create page tables
and pages for the child process. Please help me understand.

Thanks
Vaibhav Jain
- Thanks
Vaibhav jain
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: accessing page table of a process

Post by gerryg400 »

vjain20 wrote:
For example, when process A create new process B, instead of A adding page into B's table, it's more natural for A to setup a minimal process layout B, and let B to allocate page itself when it is scheduled.
I am confused here and it might be that I am not clear with the concepts. How can a process create its own pages and access its page tables?
I was thinking that this should be done by the kernel when the fork is called. So there would be a system call for fork which will create page tables
and pages for the child process. Please help me understand.

Thanks
Vaibhav Jain
It is done by the kernel but it's done in the memory context of the child.
If a trainstation is where trains stop, what is a workstation ?
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: accessing page table of a process

Post by iansjack »

vjain20 wrote:
For example, when process A create new process B, instead of A adding page into B's table, it's more natural for A to setup a minimal process layout B, and let B to allocate page itself when it is scheduled.
I am confused here and it might be that I am not clear with the concepts. How can a process create its own pages and access its page tables?
I was thinking that this should be done by the kernel when the fork is called. So there would be a system call for fork which will create page tables
and pages for the child process. Please help me understand.

Thanks
Vaibhav Jain
The fork requires a pretty minimal Page Table (basically a copy of the parent task's). But then a processes will generally execve another program and it may well need more (or less) pages; it will be the responsibility of the new task (calling upon kernel resources) to do this.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: accessing page table of a process

Post by bluemoon »

The kernel space(full or critical part) containing the new process stub will be shared by duplicating the top level page directory(ie. one 4K block), any subsequent allocation, which may involve adding PDPT/PD/PT and the usable memory can be done on behalf of the new process.
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: accessing page table of a process

Post by vjain20 »

Here is what I have :-
A fork() method which creates a copy of the page directory of the parent process such that some page tables (mapped to kernel) are
shared while others (such as for stack) are created and data is copied from the parent process. It then creates a PCB for the child and adds it to
the ready queue. The newly created page directory is pointed to by the PCB.

A switch_task() method which which is called by the timer interrupt handler. It switches to the next task in the ready queue loading the registers
and page directory address.

Here is what I understand from this conversation :-
When the fork is called by a process it enters kernel mode but the page directory remains the same. So the parent process
(executing in kernel mode) creates a copy of the address space of the parent like I have described above.

Is my understanding correct ?

Here is what I don't understand:-

If the child does not do an exec is there a need of anything else other than what I have mentioned ?

Does 'minimal page table' means the same what I have mentioned above ?
For example, when process A create new process B, instead of A adding page into B's table, it's more natural for A to setup a minimal process layout B, and let B to allocate page itself when it is scheduled.
I don't understand what pages need to be allocated once A has created the page tables and copied the data ?
Also, how is B going to do this when it starts in user mode and continues in user mode.
Last edited by vjain20 on Sat Jun 02, 2012 9:34 am, edited 1 time in total.
- Thanks
Vaibhav jain
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: accessing page table of a process

Post by Combuster »

vjain20 wrote:
For example, when process A create new process B, instead of A adding page into B's table, it's more natural for A to setup a minimal process layout B, and let B to allocate page itself when it is scheduled.
I don't understand what pages need to be allocated once A has created the page tables and copied the data ?
Also, how is B going to do this when it starts in user mode and continues in user mode.
It is done by the kernel but it's done in the memory context of the child.
I.e. the suggestion was not to start new processes in usermode, but rather in kernel mode and have it configure the process in its own time.

The problem in this case is that it becomes difficult to do it right in the case of fork(), because you have two conflicting issues:
- You want the copy to be of exactly that moment when fork() is called, and not sometime later
- You don't actually want to physically copy the data because fork() is typically followed by exec() and everything copied up to then would have been a waste of time.
"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 ]
Post Reply