Creating address space for new 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
Virtlink
Member
Member
Posts: 34
Joined: Thu Jun 05, 2008 3:53 pm
Location: The Netherlands
Contact:

Creating address space for new process?

Post 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
User avatar
cr2
Member
Member
Posts: 162
Joined: Fri Jun 27, 2008 8:05 pm
Location: ND, USA

Re: Creating address space for new process?

Post 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.
OS-LUX V0.0
Working on...
Memory management: the Pool
Virtlink
Member
Member
Posts: 34
Joined: Thu Jun 05, 2008 3:53 pm
Location: The Netherlands
Contact:

Re: Creating address space for new process?

Post 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.
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Creating address space for new process?

Post 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.
User avatar
cr2
Member
Member
Posts: 162
Joined: Fri Jun 27, 2008 8:05 pm
Location: ND, USA

Re: Creating address space for new process?

Post 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.
OS-LUX V0.0
Working on...
Memory management: the Pool
Post Reply