Page 1 of 1

Creating address space for new process?

Posted: Fri Aug 22, 2008 4:29 pm
by Virtlink
Hello all,

While I've been active on the wiki, this is my first post to the forums here. And there is something I can't figure out. #-o

How does one create a new virtual address space (page directory/tables) for a new process? I am in ring 0 with the first 4 MiB identity mapped, but the rest of the kernel's address space is mostly empty. I have a scheduler which can switch between two functions in the one and only address space.

Do I create the page directory and tables anywhere in the current address space? How do you decide where? (Since there is no free-page stack or bitmap for the virtual address space, to get any available page.) And how do you remove the PD and stuff from the address space without freeing the underlying physical pages? (Because the new process needs them.) Then, how do I get my scheduler to switch address spaces?

Thanks in advance for any help or pointers in the right direction! :D

Re: Creating address space for new process?

Posted: Fri Aug 22, 2008 4:50 pm
by cr2
Virtlink wrote:Do I create the page directory and tables anywhere in the current address space? How do you decide where?
Use the heap (or pool as I like to call it).
Virtlink wrote:And how do you remove the PD and stuff from the address space without freeing the underlying physical pages?
Huh?
Virtlink wrote:Then, how do I get my scheduler to switch address spaces?
cr3.

Re: Creating address space for new process?

Posted: Fri Aug 22, 2008 4:56 pm
by Virtlink
cr2 wrote: Use the heap (or pool as I like to call it).
Virtlink wrote:And how do you remove the PD and stuff from the address space without freeing the underlying physical pages?
Huh?
After I've allocated some space on the heap, as you suggest, and filled the structures, and put the physical address in CR3, then the kernel creating the process does not need to keep those structures in it's address space. Only the new process needs those. But when I free() the structures, I'll lose them. When I free the virtual page, the physical page also gets freed, again losing the structures. Or do you keep, say, a hundred PD's in your kernel's virtual address space when you have a hundred processes?

By the way, my malloc heap will almost never give me a page-aligned memory chunk, and malloc in general has no way to specify the alignment of the allocated data.

Re: Creating address space for new process?

Posted: Fri Aug 22, 2008 5:24 pm
by ru2aqare
Virtlink wrote:After I've allocated some space on the heap, as you suggest, and filled the structures, and put the physical address in CR3, then the kernel creating the process does not need to keep those structures in it's address space. Only the new process needs those. But when I free() the structures, I'll lose them. When I free the virtual page, the physical page also gets freed, again losing the structures. Or do you keep, say, a hundred PD's in your kernel's virtual address space when you have a hundred processes?
I think the kernel needs to keep those structures in its address space. However, your can map all these structures to the same virtual address. By doing that, you use less virtual address space, and the kernel (and drivers running in kernel mode) can reach only the structures that belong to the currently running process (which is enough most of the time). You should only free() the structures when the process is terminated.
Virtlink wrote: By the way, my malloc heap will almost never give me a page-aligned memory chunk, and malloc in general has no way to specify the alignment of the allocated data.
That is why usually the page directories and tables are allocated by retrieving a page from the physical allocator.

Re: Creating address space for new process?

Posted: Fri Aug 22, 2008 11:05 pm
by cr2
You can fool around with your pool to make it(malloc4k() or whatever you want to call it) return a 4k aligned address.