vmm
vmm
in my os pmm is implemented. For vmm tree data structure seems to be the best choice. how is it possible to allocate memory for tree nodes if kmalloc is not implemented. without virtual memory allocator it is not possible for the malloc function to request for the memory. Though i can have some simple solutions like using fixed memory for malloc then afterwards requesting more memory when needed, i like to know how you peoples are handling the situation. i am also looking the source of mobius kernel and unable to find the required information. Most of you people might be familiar with Mobius. The malloc is borrowed from dlmalloc. So what is he doing to handle this situation?
I used something rather hackish in my OS, and it would seem to work.
(note... I use kalloc as the name of the kernel memory allocation subsystem)
When kalloc is initialized, I know that no memory can possibly have been allocated using it. In fact, if that's not true, then whether kalloc works is of no concern whatsoever, because there are vastly more serious issues to deal with.
So, assuming that your kernel+the allocation data structures fit entirely below the first memory hole (probably a safe assumption), you can just use the first N allocations to hold the allocation tables.
In my design, each page of memory consumes 8 bytes of overhead (though I could probably squeeze that significantly) If I have kalloc managing 16 MB of memory (reasonable for now, for me at least), I end up setting up the allocation tables in the first 8 pages, initializing them to be free, and then marking those 8 pages as "used".
Actually, I don't do any kind of paging; the 4K allocation atoms are simply to make things easier when I get around to it.
So in essence, you just use the allocation, then mark it as used until the full allocator is initialized.
Hope that helped!
(note... I use kalloc as the name of the kernel memory allocation subsystem)
When kalloc is initialized, I know that no memory can possibly have been allocated using it. In fact, if that's not true, then whether kalloc works is of no concern whatsoever, because there are vastly more serious issues to deal with.
So, assuming that your kernel+the allocation data structures fit entirely below the first memory hole (probably a safe assumption), you can just use the first N allocations to hold the allocation tables.
In my design, each page of memory consumes 8 bytes of overhead (though I could probably squeeze that significantly) If I have kalloc managing 16 MB of memory (reasonable for now, for me at least), I end up setting up the allocation tables in the first 8 pages, initializing them to be free, and then marking those 8 pages as "used".
Actually, I don't do any kind of paging; the 4K allocation atoms are simply to make things easier when I get around to it.
So in essence, you just use the allocation, then mark it as used until the full allocator is initialized.
Hope that helped!
I use the solution found in http://www.osdever.net/tutorials/memory1.php?the_id=44 (also see tutorial 2).
In this system, you map your page directory in to itself (for example as the last entry). This makes mapping memory in and out (the creation of page tables) very easy.
When it comes down to it, you will have to initially pick a page that you know is free before you have implemented your version of kmalloc. If you have a memory map provided by something like int 0x15 ax=E820, it is possible to pick a free page of physical RAM and map it in for 'house-keeping' purposes.
Knowing how long the kernel is, I place my memory housekeeping structures directly after the kernel. I know that this space is free in both physical ram (kernel is at 0x100000) and virtual RAM (kernel is at 0xC0000000).
What my 'test kernel' does:
1. Put my page dir somewhere in the first 4MB of RAM (identity mapped).
2. Put a kernel page table just after the page directory and map in to 3GB.
3. Map the last PDE to the address of the page dir itself.
4. You can now un-identity map the first 4MB of RAM (ensure that you know how and where to access video RAM first!).
The page directory can now be accessed at 0xFFFFF000 to add a page table, let your physical allocator find a free page, map that page in to the page directory. Supposing you have a page table for the first 4MB of ram, put it's PDE at 0xFFFFF000.
The page table can then be accessed at 0xFFC00000 --> FFC00FFF. It is now fairly simple to map in individual pages by writing their PTE's to your new page table. You will want to provide a few pages specifically for your memory management structures.
I hope this rambling helps you in some way!
Cheers,
Adam
In this system, you map your page directory in to itself (for example as the last entry). This makes mapping memory in and out (the creation of page tables) very easy.
When it comes down to it, you will have to initially pick a page that you know is free before you have implemented your version of kmalloc. If you have a memory map provided by something like int 0x15 ax=E820, it is possible to pick a free page of physical RAM and map it in for 'house-keeping' purposes.
Knowing how long the kernel is, I place my memory housekeeping structures directly after the kernel. I know that this space is free in both physical ram (kernel is at 0x100000) and virtual RAM (kernel is at 0xC0000000).
What my 'test kernel' does:
1. Put my page dir somewhere in the first 4MB of RAM (identity mapped).
2. Put a kernel page table just after the page directory and map in to 3GB.
3. Map the last PDE to the address of the page dir itself.
4. You can now un-identity map the first 4MB of RAM (ensure that you know how and where to access video RAM first!).
The page directory can now be accessed at 0xFFFFF000 to add a page table, let your physical allocator find a free page, map that page in to the page directory. Supposing you have a page table for the first 4MB of ram, put it's PDE at 0xFFFFF000.
The page table can then be accessed at 0xFFC00000 --> FFC00FFF. It is now fairly simple to map in individual pages by writing their PTE's to your new page table. You will want to provide a few pages specifically for your memory management structures.
I hope this rambling helps you in some way!
Cheers,
Adam
Before going to vmm, i wrote a kernel memory allocator within these short interval of time. Please look thru it and give your suggestions. Free is not yet implemented and code is unoptimized. It works fine, however errors are possible.
- Attachments
-
- kmalloc.c
- (9.18 KiB) Downloaded 63 times