diff cos/kernel/mm.c @ 40:24ce177e01e8

Added more malloc stuff. Added mem usage
author windel
date Tue, 24 Jan 2012 17:54:16 +0100
parents 5c20bd53cccd
children 35cc54e078dd
line wrap: on
line diff
--- a/cos/kernel/mm.c	Mon Jan 16 21:38:55 2012 +0100
+++ b/cos/kernel/mm.c	Tue Jan 24 17:54:16 2012 +0100
@@ -6,13 +6,19 @@
 
 #include "kernel.h"
 
+#define HEAP_START 0xC000000000000000
+#define HEAP_INITIAL_SIZE  0x100000
+
 // Bitmap that keeps track of all the 4 kB memory pages in the system:
 static uint64_t *frames = 0;
 static uint64_t nframes = 0;
 
 memmap_t* kernel_map = 0; // kernel memory mapping
+// TODO: determine if this is required.
 memmap_t* current_map = 0; // The currently active memory mapping
 
+heap_t* kernel_heap = 0;
+
 static void set_frame(uint64_t frame)
 {
    uint64_t idx = frame / 64;
@@ -56,6 +62,24 @@
    return (uint64_t) -1;
 }
 
+void memory_status(void)
+{
+   // Determine amount of used memory.
+   uint64_t i, j;
+   uint64_t inuse = 0;
+   for (i = 0; i < nframes / 64; i++)
+   {
+      for (j = 0; j < 64; j++)
+      {
+         if ((frames[i] & (1UL<<j)) == (1UL<<j))
+         {
+            inuse++;
+         }
+      }
+   }
+   printf("Memory: %d, in use: %d\n", nframes/0x100, inuse/0x100);
+}
+
 /*
  * Initializes the memory manager, allocating a bitmap once.
  */
@@ -63,22 +87,27 @@
 {
    printf("Running with %d MB ram\n", total_mem_size / 1000000);
 
+   // Memory size is from address 0x100000
+   total_mem_size += 0x100000;
+
    // Only here placement malloc is used!
    //
    // Allocate and clear bits to remember which 4KiB-frames are in use:
-   nframes = (total_mem_size / 0x1000); // Calculate number of frames
-   frames = (uint64_t*)kmalloc_int( (nframes / 64) * sizeof(uint64_t) );
-   memset(frames, 0, (nframes / 64) * sizeof(uint64_t));
+   // Calculate number of frames. memory size is indicated from address 0x100000
+   nframes = (total_mem_size) / 0x1000; // Calculate number of frames
+   int numbytes = ((nframes / 64) + 1) * sizeof(uint64_t);
+   frames = (uint64_t*)kmalloc(numbytes);
+   memset(frames, 0, numbytes);
 
    // Create kernel map:
-   kernel_map = (memmap_t*)kmalloc_aligned_int(sizeof(memmap_t));
+   kernel_map = (memmap_t*)kmalloc_a(sizeof(memmap_t));
    memset(kernel_map, 0, sizeof(memmap_t));
 
    // Identity map lower memory and mark as used by the kernel.
    // Mark as used by the kernel:
    uint64_t i;
    i = 0;
-   while ( i <= placement_address)
+   while ( i <= placement_address + 0x100000)
    {
       page_t *page;
 
@@ -98,6 +127,7 @@
    switch_mapping(kernel_map);
 
    // TODO: set the use of placement malloc to invalid after here.
+   // kernel_heap = create_heap(HEAP_START, HEAP_INITIAL_SIZE);
 }
 
 void alloc_frame(page_t *page)
@@ -130,6 +160,7 @@
 {
    current_map = mapping;
 
+   // Load table address:
    asm volatile("mov %0, %%cr3" :: "r"(&mapping->table));
 
    // Enable paging (and flush cache):
@@ -152,7 +183,9 @@
    else
    {
       // Create new table:
-      pdpt = (PDPT_t*)kmalloc_aligned_int(sizeof(PDPT_t));
+   printf("Get page for address %x\n", address);
+      // TODO: make sure that this function calls kmalloc instead of placement malloc.
+      pdpt = (PDPT_t*)kmalloc_a(sizeof(PDPT_t));
       memset(pdpt, 0, sizeof(PDPT_t));
 
       // TODO: get function like virt2phys or something here
@@ -173,7 +206,7 @@
    else
    {
       // Create a new table:
-      pd = (PD_t*)kmalloc_aligned_int(sizeof(PD_t));
+      pd = (PD_t*)kmalloc_a(sizeof(PD_t));
       memset(pd, 0, sizeof(PD_t));
 
       // Enter table into PDPT:
@@ -195,7 +228,7 @@
    else
    {
       // Create table:
-      pt = (PT_t*)kmalloc_aligned_int(sizeof(PD_t));
+      pt = (PT_t*)kmalloc_a(sizeof(PD_t));
       memset(pt, 0, sizeof(PT_t));
 
       // Enter PT into PD: