Do we need 2 Memory managers?

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
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Do we need 2 Memory managers?

Post by Raven »

Hello Geeks

Well after i learnt from James how to solve chicken and egg problem, by using placement address for allocating new available address until.....
UNTIL i have set bitmap ,Kernel directory and frame allocation and free properly, i am confused about this thing:

If i have already a mechanism to allocate frames and will develop a routine to map actual frames to pages for user processes (on the fly depending upon size of programs compiled to some virtual address say 0x100000) then my virtual memory manager is done [-o<
I think i am correct?

Secondly for example neither my kernel nor my user programs need dynamic allocation i mean all using static way like
char x[256];
int y[512];
then there is not need of heap, please correct me.

If above mentioned are correct then certainly no need of malloc() in user processes or malloc() will return address of newly allocated page.....
yeah wastage of memory i mean for allocation of a byte i will allocate 4kb....now i am trying to find a way to reuse that free space within allocated page, that is, each process will have its own mechanism to keep track of what part of last page allocated is free .... can be done using placement address mechanism.


Too long, i think i am not able to put it correctly.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Do we need 2 Memory managers?

Post by AJ »

Hi,
Raven wrote:If i have already a mechanism to allocate frames and will develop a routine to map actual frames to pages for user processes...then my virtual memory manager is done [-o<
I think i am correct?
The page frame allocator normally relates to physical memory, with code (often in your page fault exception handler) which calls on the page frame allocator when it needs to page in. This is your physical memory manager and the paging mechanism - not the virtual memory manager.
Secondly for example neither my kernel nor my user programs need dynamic allocation i mean all using static way like
char x[256];
int y[512];
then there is not need of heap, please correct me.
Correct. Limiting, but correct.
i am trying to find a way to reuse that free space within allocated page
erm...That sounds like what malloc() is meant to do (as the high level interface to the heap manager). The only problem with the system that you describe is that there is no way to reuse redundant space within the heap (which is where free() comes in).

Cheers,
Adam
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Re: Do we need 2 Memory managers?

Post by Raven »

Thanks a lot for helping me to come out of confusion
Now i am at 50-50 situation.
Actually, I don't have any mechanism right now to use swap partition or pagefile.sys to.
What i want is to know is that if suppose my kernel_directory is identity mapped upto size of kernel and now i load my user program say 8 KB in size by creating a user_directory which is mapped according to address to which i have compiled it say 0x100000, will this work :

1. Create two PT entries.
2. Call frame_allocator and map 0x100000 to this frame in first entry.
3. Call frame_allocator and map 0x100000+4kb to this frame in second entry.
4. set not present bit in rest of 1022 enteries.
5. do_task_switch() taking care of reloading cr3 and other things.
6. If my process tries to access beyond 8kb page_fault (without using malloc() )will terminate it.
7. If my process uses malloc(), it will execute a syscall to allocate a page same way as in step 2,3,4,5 (lets forget about how heap will be maintained)

In this case, i fail to understand what else we need?

Thanks for pointing out free problem in this system.
Once more thanks
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Do we need 2 Memory managers?

Post by AJ »

Hi,

Yes, typically when you create a user task, you create a new Page Directory, with an exact replica of the kernel space mapped in:
  • Create a new page directory - zeroed up to the end of user space, and kernel space copied exactly from your current PD.
  • Create the Process Control Block so your scheduler knows about the new task.
  • Switch to the new PD (load CR3) - whether explicitly, or just wait for the task to be scheduled by calling your equivalent of yield().
  • Create the new task's stack and page in somewhere to load user code. Say 0x100000.
  • Switch to ring 3 and execute in user mode.
Now, when a page fault exception happens, your PFE handler needs to know the location of valid user space. Typically, you have a system call called sbrk(bytes), which returns the top of the user heap - in your case this will initially be 0x101000.

Now, when initialising your user space malloc, call sbrk with the parameter 0x1000 and your user heap will extend to 0x102000. malloc() now knows that it has a heap of 4k from 0x101000 - 0x102000. When it has no more space to allocate, it calls sbrk again, which moves the heap breakpoint up to 0x103000 and so on... If enough is freed that the heap can contract, call sbrk with a negative parameter.

When your PFE handler fires, it checks the current heap break point from the last sbrk call. If the PF address is from 0x100000 - 0x103000, you can safely page in and return control to the user process. If the PF address is from 0x103000 - 0xBFFFFFFF, something has gone wrong and the process could be terminated by the kernel.

Of course, the above makes some assumptions - your program code is only 4k long and so on, but hopefully you get the idea. If any of this is unclear, do ask more - but have a play around with it first. It took me a while initially to understand the boundaries between my Physical Memory Manager, Heap Manager and Paging System.

Cheers,
Adam

[Edit: Oh - and you don't need to explicitly clear the Present bit on unused entries - whenever you create a new PD or PT, simply zero it. The reason for doing it this way is that eventually you can make use of optimisations - such as clearing 64 bits per iteration]
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Re: Do we need 2 Memory managers?

Post by Raven »

Thanks Pal but too technical.

Let me put it like this
I have kernel loaded at 0x100000 (1 MB) and have identity mapped kernel_directory from 00H to kernel_size+1MB.
(Suppose no dynamic allocations are done for the sake of simplicity, i am afraid of heap .)

When I create a user task i will create exact copy of kernel_directory as you said and will create extra page-table entries in it (say 2 in case program size is 8 KB) and will call frame_allocator to allocate 2 free frames and will map then in this directory.
Next i will load that program into those allocated frames BUT before that will disable paging so that kernel will have full access to whole RAM. Now i think rest is logic pertaining to MultiTasking.

So Now I donot have heap Allocator ( as i donot need it ), I have a physical-memory manager (frame_alloc and frame_free) who work on bitmap but where is my virtual memory manager?
I mean which part is my virtual memory manager? Please help?

Yeah, forgot about it . In this scenario i will have a page fault handler to just terminate a user process.
I mean

Suppose a User-Task

Code: Select all

void main()
{
int *ptr;
ptr=0xa000000;//not mapped
*ptr=1;// PFE handler terminates it
}
I know i will get it!
Thanks a lot
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: Do we need 2 Memory managers?

Post by Combuster »

i am afraid of heap
In that case, get some therapy. Avoiding that like the plague will not do you good in the long run (and probably not in the short run either).
where is my virtual memory manager?
What pieces of code are writing to any of the paging structures?
"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 ]
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Re: Do we need 2 Memory managers?

Post by Raven »

Combuster wrote:In that case, get some therapy. Avoiding that like the plague will not do you good in the long run (and probably not in the short run either).
Yeah, that i know that is why i said
Raven wrote:(Suppose no dynamic allocations are done for the sake of simplicity, i am afraid of heap .)
Combuster wrote:What pieces of code are writing to any of the paging structures?
Do you mean this?
Raven wrote:If i have already a mechanism to allocate frames and will develop a routine to map actual frames to pages for user processes (on the fly depending upon size of programs compiled to some virtual address say 0x100000) then my virtual memory manager is done
Post Reply