Memory Manager

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
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

Memory Manager

Post by System123 »

I am starting to build a memory manager. I was just wondering should I build the MMU first or the Paging unit?
Gizmic OS
Currently - Busy with FAT12 driver and VFS
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Memory Manager

Post by jal »

System123 wrote:I am starting to build a memory manager. I was just wondering should I build the MMU first or the Paging unit?
I'm really at loss what you mean. The MMU is built into the CPU, it's not something you write in software. Also, why should paging be a 'unit'?


JAL
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Memory Manager

Post by AJ »

Hi,

What do you mean bu "the mmu"? As far as I know, this is the Memory Management Unit of the CPU...

There are basically 3 areas you need:

1) Physical Page Frame Allocator - traditionally a bitmap or stack which assigns physical pages on memory boundaries.
2) Heap Allocator - allocates virtual memory linearly (and compacted - no page alignment by default, like your traditional malloc()/free()).
3) A paging module which will tie it all together.

I would personally write a physical page allocation / freeing set of routines first, then add paging. Add the virtual memory manager on top of this - a part of its function will be to let the paging module know when a page can be paged in (and with what privileges), and when you have an unhandled PFE situation.

You could in theory write a virtual manager first and enable paging later, but I prefer working from the ground up.

Cheers,
Adam
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Memory Manager

Post by AJ »

I was a bit rushed when I posted the above, but have some more time, so let me expand:

Firstly, have a look at James Molloy's Tutorials which cover paging and memory management in detail. I am also hoping to write a bit of a design guide which is in early draft stage at the moment, but you may like to see it here.

Cheers,
Adam
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

Re: Memory Manager

Post by System123 »

I think I confused you by writing MMU, what I meant is that I want to create a Memory Manager, ie: allocMem and freemem procedures, set up paging etc.
Gizmic OS
Currently - Busy with FAT12 driver and VFS
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Memory Manager

Post by jal »

System123 wrote:I think I confused you by writing MMU, what I meant is that I want to create a Memory Manager, ie: allocMem and freemem procedures, set up paging etc.
Well, AJ gave you all the answers you need.


JAL
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

Re: Memory Manager

Post by System123 »

Ok now that I have done extreme amounts of theory I just have one last question to be answered. Lets take for example my kernel ends at the 1Mb mark, ie: 0x10000. Do I then setup a page directory and then on the next free memory I set up my page table? The other thing is does the page directory point to the page table for that piece of memory??
Gizmic OS
Currently - Busy with FAT12 driver and VFS
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Memory Manager

Post by AJ »

System123 wrote:Lets take for example my kernel ends at the 1Mb mark, ie: 0x10000
[pedant]0x100000[/pedant]

Do I then setup a page directory and then on the next free memory I set up my page table?
Entirely up to you where your PD and PT's go.
The other thing is does the page directory point to the page table for that piece of memory??
I strongly suggest you get the Intel Manuals. In conventional 32 bit paging, one PDE points to one PT representing a 4MiB range. Each PTE represents a 4Kib range. There are variations of this, though PSE, PAE, Long Mode...

Cheers,
Adam
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

Re: Memory Manager

Post by System123 »

I just realised i am going to have to port my os to free pascal as borland pascal does not allow me to work in 32bit protected mode. How would i create the page table in pascal?
Would i use longint data types or create a record with different fields? Or should i use assembler to create and write to an array of bits(ie using db) rather?
Gizmic OS
Currently - Busy with FAT12 driver and VFS
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Memory Manager

Post by AJ »

Hi,
How would i create the page table in pascal?
On of the major prerequisites for OS Development is knowing your chosen language (and being able to read the many examples you will see in C/ASM and convert those algorithms to your chosen language). You need to create a 4KiB page-aligned table, with 32 bit entries. These entries point to a physical page frame (with the bottom 12 bits used as flags). You should already know how to do that in your chosen language. But if you try to implement it and end up triple-faulting, please come back and ask a specific question with relevantt code snippets. Whether you use bit fields or a 32 bit data type is entirely up to you - I prefer using bitwise operations with a 32 bit data type, personally.

If you have your own boot loader, you can choose to place the PT wherever you already know there is a free memory space. If using GRUB, you will already have a nice memory map to go from.

With regards to your OP, do you already have a page frame allocator? That would perhaps be the first step. Have you got the Intel Manuals? They will be a big help for your first steps in paging.

You may like to start by creating a single PD and mapping the whole of memory space using large, 4MiB pages - then gradually add in page tables. Remember that although you will be creating your first PD and PT's (semi-)statically, you will later be creating PT's dynamically, so whatever system you go for needs to take this in to account.

Cheers,
Adam
leledumbo
Member
Member
Posts: 103
Joined: Wed Apr 23, 2008 8:46 pm

Re: Memory Manager

Post by leledumbo »

How would i create the page table in pascal?
It's basically the same either in C or Pascal. Check mine if you need any reference.
NEWS: I just made Page Frame & Heap Allocator working!
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

Re: Memory Manager

Post by System123 »

I am familiar wit the language i am using. I have coded delphi for 3years and pascal is very similar. Prehaps i need to get some tutorials on c so that converting code from c - pascal is easier.
Gizmic OS
Currently - Busy with FAT12 driver and VFS
Post Reply