Heap galore
I’ve just committed to the SVN the latest and greatest version of the kernel. I’m trying to figure out how to place all the bricks in the virtual space in a logical and meaningful way while keeping all the things as small as possible. For the moment I chose a very simple, classic and pretty space-wasting layout:
HEAP 0xf0000000 -> 0xfbffffff KERNEL code (.text, .data, .bss) 0xfc000000 -> 0xffffffff
This means that there are up to 64MB reserved for all the kernel’s almost static things (code, initialized and uninitialized data, stack…) and up to 192MB of pure heap galore. I’m looking forward to keep all the things tight in order to leave the largest possible share of virtual space for the user land. I’ve also set up a structure type called mmap_block that is a linked list node to represent the physical memory map1 divided in available and taken chunks of variable size.
/** * Enumerates the possible mmap_block.available states. * MMAP_INVALID: This doesn't describe a valid block, thus it is available for * describing a new block; * MMAP_FREE: This describes a free memory chunk; * MMAP_ALLOC: This describes an allocated chunk of memory; * MMAP_RESERVED: This describes a special chunk of memory that shouldn't be * touched. */ enum mmap_avl { MMAP_INVALID, MMAP_FREE, MMAP_ALLOC, MMAP_RESERVED }; typedef struct mmap_block_s { #if __BITS__ == 32 u32 base_32; u32 size_32; #else union { struct { u32 low; u32 high; } base_32; u64 base_64; }; union { struct { u32 low; u32 high; } size_32; u64 size_64; }; #endif enum mmap_avl available : 2; // See enum mmap_avl above u32 flags : 30; // Reserved for future expansion u32 pid; // The pid of the owner process struct mmap_block_s *next; struct mmap_block_s *prev; } mmap_block; // 24/32B
This structure is intended to allocate memory by chunks of 4kB, thus requiring at most 24MB to cover the whole addressing space on a 32 bit arch. The initial layout is built inside the kernel’s heap to be both easily available from the code and easily extensible as needed. It is (somehow) based on the memory map built by the lovely GRUB taking into account the things that GRUB just can’t be aware of2.
The very next step will be a low level kernel allocator that can give pointers to free memory and allocate new pages and memory chunks as needed.
tOfuS, the musicians’ operating system.