/*
   MMInit.c, Copyright  (c) by George Fankhauser,
   Swiss Federal Institute of Technology,
   Computer Engineering and Networks Laboratory.
 
   TOPSY -- A Teachable Operating System.
   Implementation of a tiny and simple
   micro kernel for teaching purposes.
 
   For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
   This software is provided under the terms of the GNU General Public Licence.
 
   A full copy of the GNU GPL is provided in the file COPYING found in the 
   development root of Topsy.
 */
/*
  File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/Memory/MMInit.c,v $
  Author(s):             George Fankhauser
  Affiliation:           ETH Zuerich, TIK
  Version:               $Revision: 2922 $
  Creation Date:         
  Last Date of Change:   $Date: 2007-03-19 17:48:52 +0100 (Mon, 19 Mär 2007) $      by: $Author: baderma $
  */

#include "MMInit.h"
#include "Threads.h"

#include "MMHeapMemory.h"
#include "MMVirtualMemory.h"
#include "MMMapping.h"
#include "MMError.h"

#include "vm_bootup.h"

void mmInit(struct vm_segmap* segMapPtr, Address* mmStackPtr, Address* tmStackPtr,
            Address *idStackPtr)
{

#if 0
  vm_print_segmap(segMapPtr);
#endif

  /* memory error handlers need to be installed */
  //-CJ  mmInstallErrorHandlers();

  /* set up page mapping */
  //-CJ  mmInitMemoryMapping(segMapPtr);

  /* build dynamic kernel heap memory in one vm region (which will be
   * created later using heap memory for lists and descriptors)
   * the heap always starts after the static kernel data...
   */

  //-CJ  if (hmInit(segMapPtr->bootalloc[1].addr) != HM_INITOK) /* XXX */
  //-CJ    PANIC("heap init failed");

  /* setup memory region lists. beware: vmregionlist/freelist and first
     region descriptors must use static memory
     vmInit makes regions for the following:
            KERNEL:	boot/exception stack (first page in kernel memory)
            kernel code
            kernel data
            kernel heap (dynamic data)
            stacks for mmStackPtr, tmStackPtr
            free region (what's left in kernel space)
            USER:	user code
            user data
            free region (what's left in user space)
   */
  if (mmVmInit(segMapPtr->text,  segMapPtr->textsize,
               segMapPtr->data<segMapPtr->bss?segMapPtr->data:segMapPtr->bss,
               segMapPtr->datasize + segMapPtr->bsssize, /* XXX this will not work if there is a gap between data and bss */
               (Address)0x1000,    segMapPtr->optsegmentsize,
               segMapPtr->bootalloc[1].virt, segMapPtr->bootalloc[1].size,
               segMapPtr->bootstackbottom, segMapPtr->bootstacksize,
               mmStackPtr, tmStackPtr, idStackPtr) != MM_VMINITOK)
  {
    PANIC("vm init failed");
  }
  //-LR   printf("mmInit about return\n");
  //-LR   CheckHM();

  //-LR asm("hlt");
}
