/*
  MMDirectMapping.c, Copyright  (c) by Lukas Ruf, lr@lpr.ch,
  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/ia32/MMDirectMapping.c,v $
 	Author(s):             Lukas Ruf, lr@lpr.ch
 	Affiliation:           ETH Zuerich, TIK
 	Version:               $Revision: 3039 $
 	Creation Date:         
 	Last Date of Change:   $Date: 2007-06-12 21:43:31 +0200 (Die, 12 Jun 2007) $      by: $Author: sryffel $
*/
#include "MMMapping.h"
#include "Threads.h"
#include "Support.h"
#include "cpu.h"

#include "Video.h"

unsigned int bootStackBottom = 0;

Error   mmInitMemoryMapping(
  Address codeAddr, unsigned long int codeSize,
  Address dataAddr, unsigned long int dataSize,
  Address userLoadedInKernelAt)
{

#ifdef NULL
  printf("Moving User Chunk\n");
  printf("CodeAddr: to = 0x%8x, from = 0x%8x, size = %i\n",
         (unsigned int)codeAddr,
         (unsigned int)userLoadedInKernelAt,
         (unsigned int)codeSize);
#endif

  //-CJ  byteCopy(codeAddr, userLoadedInKernelAt, codeSize);

#ifdef NULL

  printf("dataAddr: to = 0x%8x, from = 0x%8x, size = %i\n",
         (unsigned int)dataAddr,
         (unsigned int)( (unsigned long)userLoadedInKernelAt +
                         (unsigned long)dataAddr - (unsigned long)codeAddr),
         (unsigned int)dataSize);
#endif

  //-CJ  byteCopy(dataAddr, (Address)((unsigned long)userLoadedInKernelAt +
  //-CJ    (unsigned long)dataAddr - (unsigned long)codeAddr), dataSize);
  //-CJ XXX THE USER IS ALLREAD MOVED TO KUSEG_BASE IN INIT.C
  return MM_INITMEMORYMAPPINGOK;
}

Error mmMapPages(Page startPage, Page nOfPages, PageStatus pstat)
{
  return MM_MAPPAGESOK;
}


Error mmUnmapPages(Page startPage, Page nOfPages)
{
  return MM_UNMAPPAGESOK;
}


Error mmMovePage(Page from, Page to)
{
  /* direct mapped memory systems can only copy the page */
  byteCopy((Address)(to*LOGICALPAGESIZE), (Address)(from*LOGICALPAGESIZE),
           LOGICALPAGESIZE);
  zeroOut((Address)(from*LOGICALPAGESIZE), LOGICALPAGESIZE);
  return MM_MOVEPAGEOK;
}


Error mmProtectPage(Page page, ProtectionMode pmode)
{
  /* direct mapped memory does nothing */
  return MM_PROTECTPAGEFAILED;
}


/* this Function is called always from KERNEL SPACE, i.e.
 * all addresses are relativ to the Kernel Segment Base, i.e.
 * every segment starts at K0SEG_BASE.
 * The Memory Management to define the segments is handled a layer below.-)
 * Both Kernel and User space range from 0x0 to 4GB. 
 */
Error  mmAddressSpaceRange(AddressSpace space, Address* addressPtr,
                           unsigned long int* sizePtr)
{
#if 0
  if (space == KERNEL)
  {
    //-LR       *addressPtr = (Address)K0SEG_BASE;
    *addressPtr = (Address)0;
    *sizePtr = K0SEG_SIZE; /* 512kB */
  }
  else if (space == USER)
  {
    //-LR       *addressPtr = (Address)KUSEG_BASE; /* zero page invalid */
    *addressPtr = (Address)UADDR_BASE; /* zero page invalid */
    *sizePtr = USERMEM;           /* 256kB */
  }
  else
    return MM_ADDRESSSPACERANGEFAILED;
#endif
  *addressPtr = (Address)0;
  *sizePtr = KDATASEG_SIZE;
  return MM_ADDRESSSPACERANGEOK;
}
