Page 1 of 1
Memory Manager
Posted: Fri Jul 25, 2008 8:07 am
by System123
I am starting to build a memory manager. I was just wondering should I build the MMU first or the Paging unit?
Re: Memory Manager
Posted: Fri Jul 25, 2008 8:24 am
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
Re: Memory Manager
Posted: Fri Jul 25, 2008 8:25 am
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
Re: Memory Manager
Posted: Fri Jul 25, 2008 8:57 am
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
Re: Memory Manager
Posted: Fri Jul 25, 2008 12:00 pm
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.
Re: Memory Manager
Posted: Fri Jul 25, 2008 12:39 pm
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
Re: Memory Manager
Posted: Fri Jul 25, 2008 11:40 pm
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??
Re: Memory Manager
Posted: Sun Jul 27, 2008 12:35 pm
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
Re: Memory Manager
Posted: Sun Jul 27, 2008 10:49 pm
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?
Re: Memory Manager
Posted: Mon Jul 28, 2008 1:50 am
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
Re: Memory Manager
Posted: Mon Jul 28, 2008 2:38 am
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!
Re: Memory Manager
Posted: Mon Jul 28, 2008 6:04 am
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.