Page 1 of 1
accessing page table of a process
Posted: Wed May 30, 2012 1:02 am
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
Re: accessing page table of a process
Posted: Wed May 30, 2012 1:24 am
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.
Re: accessing page table of a process
Posted: Wed May 30, 2012 1:47 am
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
Re: accessing page table of a process
Posted: Wed May 30, 2012 2:37 am
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.
Re: accessing page table of a process
Posted: Wed May 30, 2012 3:03 am
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
Re: accessing page table of a process
Posted: Wed May 30, 2012 3:46 am
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.
Re: accessing page table of a process
Posted: Wed May 30, 2012 4:22 am
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.
Re: accessing page table of a process
Posted: Wed May 30, 2012 6:18 am
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.
Re: accessing page table of a process
Posted: Wed May 30, 2012 5:25 pm
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.
Re: accessing page table of a process
Posted: Wed May 30, 2012 11:05 pm
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.