view cos/kernel/kernel.c @ 35:bcb3b68c8147

Added bss end address and load end address to multiboot header
author windel
date Mon, 16 Jan 2012 17:38:00 +0100
parents 8012221dd740
children 91f91ff07ea8
line wrap: on
line source

#include "kernel.h"

static void testMalloc()
{
   char *a, *b;

   printf("Testing malloc\n");
   a = kmalloc(100);
   printf("Got a at %x\n", a);
   a[0] = 'A';
   b = kmalloc(22);
   printf("Got b at %x\n", b);
   b[0] = 'B';
   kfree(a);
}

uint64_t testvar = 1234; // Ends up in data section
uint64_t testvar2 = 0; // Ends up in bss section

multiboot_info_t *multiboot_info = 0; // Set by startup code.

/* This routine initializes the kernel.
 * We are left here in 64-bit long mode with the first 6 MB identity mapped.
 * */
void kmain()
{
   // No kmalloc required here yet:
   init_screen();
   setupIDT();
   keyboard_init();
   timer_init();

   testvar++;
   testvar2++;
   printf("Test variable = %d, testvar2 = %d\n", testvar, testvar2);

   /* Retrieve memory information from multiboot header */
   uint64_t available_memory = 0;
   if ((multiboot_info->flags & (1<<6)) == (1<<6))
   {
      multiboot_memory_map_t *mmap;
      for (mmap = (multiboot_memory_map_t*)(uint64_t)multiboot_info->mmap_addr; 
            (uint64_t)mmap < multiboot_info->mmap_addr + multiboot_info->mmap_length; 
            mmap = (multiboot_memory_map_t*) ( (uint64_t)mmap + mmap->size + sizeof(mmap->size)) )
      {
         /*
         printf("size: %d, start: 0x%x, length=0x%x, type=%d\n", mmap->size, 
            mmap->base, mmap->length, mmap->type);
         */

         if ( (mmap->type == 1) && (mmap->base == 0x100000) )
         {
            available_memory = mmap->length;
         }
      }
   }
   else
   {
      panic("Found no GRUB memory map\n");
   }

   printf("Running with %d MB ram\n", available_memory / 1000000);

   /*
   fs_node_t *fs_root = 0;
   if ( (multiboot_info->flags & (1<<3)) == (1<<3))
   {
      printf("Mod count: %d\n", multiboot_info->mods_count);
      uint64_t i;
      multiboot_module_t *mod;
      for (i=0, mod=(multiboot_module_t*)(uint64_t)multiboot_info->mods_addr; i<multiboot_info->mods_count; i++, mod++)
      {
         printf("Mod start: %x, end: %x\n", mod->mod_start, mod->mod_end);
         if (i == 0)
         {
            uint64_t ramdisk_location = ((uint32_t*)((uint64_t)multiboot_info->mods_addr))[0];
            fs_root = initialize_initrd(ramdisk_location);
         }
         // TODO: Make sure that placement malloc does not overwrite the ramdisk.
         printf("Mod size: %d", mod->mod_end - mod->mod_start);
      }
   }
   
   if (fs_root != 0)
   {
      fs_dirent_t *node = 0;
      int i = 0;
      while ( (node = readdir_fs(fs_root, i)) != 0)
      {

      }
   }
   */

   // Assume first 16MB:
   // TODO: get size from grub
   init_memory(0x1000000);

   // TODO: make below a user space program!
   printf("Welcome!\n");

  while (1) 
  {
    char buffer[70];
    printf(">");
    getline(buffer, 70);
    // TODO: interpret this line with python :)
    printf("\n");
    if (buffer[0] == 'x') 
    {
      printf("System time in ms: %d\n", getTimeMS());
    }
    if (buffer[0] == 't')
    {
        testMalloc();
    }
    if ( strncmp(buffer, "help", 4)) 
    {
      printf("Help\n Try one of these commands:\n");
      printf(" x: print system time in ms\n");
      printf(" r: reboot\n");
      printf(" t: test\n");
      printf(" b: break\n");
    }
    if (strncmp(buffer, "r", 1))
    {
       reboot();
    }
    if (strncmp(buffer, "b", 1))
    {
       magicBochsBreak();
    }
      if (strncmp(buffer, "pf", 2))
      {
      /* Test general protection exception */
      uint64_t *x;
      x = (uint64_t*)0x4000000; // Address that is not mapped
      *x = 0x2; // trigger paging exception
      }

   }
}