/**************************************************************************************
 Copyright (c) 2002 The UbixOS Project
 All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions, the following disclaimer and the list of authors.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions, the following disclaimer and the list of authors
in the documentation and/or other materials provided with the distribution. Neither the name of the UbixOS Project nor the names of its
contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 $Id: memory.c,v 1.1 2003/01/10 18:33:01 reddawg Exp $

**************************************************************************************/

#include <vmm/memory.h>
#include <ubixos/io.h>
#include <ubixos/vitals.h>
//#include <lib/kstdio.h>

struct memMap *memoryMap = (struct memMap *)0x101000;

/************************************************************************

Function: uInt32 countMemory();
Description: This Function Returns Amount Of Memory In Pages
Notes:

************************************************************************/
uInt32 countMemory() {
  register unsigned long *mem;
  uInt32 memCount = -0x1,tempMemory = 0x0;
  uInt16 memKb = 0x0;
  uInt8  irq1State = 0x0,irq2State = 0x0;
  uInt32 cr0 = 0x0;
  //Save States For IRQ's 1 And 2
  irq1State = inportByte(0x21);
  irq2State = inportByte(0xA1);
  //Turn Off Both IRQ's 1 And 2
  outportByte(0x21, 0xFF);
  outportByte(0xA1, 0xFF);
  //Save State Of CRO
  asm(
    "movl %%cr0, %%ebx\n"
    : "=a"(cr0)
    :
    : "ebx"
    );
  asm("wbinvd");
  asm(
    "movl %%ebx, %%cr0\n"
    :
    : "a" (cr0 | 0x00000001 | 0x40000000 | 0x20000000)
    : "ebx"
    );

  while(memKb<4096 && memCount!=0) {
    memKb++;
    if (memCount == -1) memCount = 0;
    memCount+=1024*1024;
    mem=(unsigned long *)memCount;
    tempMemory=*mem;
    *mem=0x55AA55AA;
    asm("":::"memory");
    if (*mem!=0x55AA55AA) {
      memCount=0;
      }
    else {
      *mem=0xAA55AA55;
      asm("":::"memory");
      if (*mem!=0xAA55AA55) {
        memCount=0;
        }
      }
    asm("":::"memory");
    *mem=tempMemory;
    }
  asm(
    "movl %%ebx, %%cr0\n"
    :
    : "a" (cr0)
    : "ebx"
    );
  //Restore States For IRQ's 1 And 2
  outportByte(0x21, irq1State);
  outportByte(0xA1, irq2State);
  return((memKb*1024*1024)/4096);
  }


/************************************************************************

Function: void initMemoryMap();
Description: This Function Initializes The Systems Memory Map
Notes:

************************************************************************/
void initMemoryMap() {
  uInt32 i = 0x0;
  //kprintf("Initializing Memory Map.........    ");
  systemVitals->numPages = countMemory();
  for (i = 0x0;i<systemVitals->numPages;i++) {
    memoryMap[i].pageAddr   = (uInt32)(i * 4096);
    memoryMap[i].cowCounter = 0x0;
    memoryMap[i].pid        = -1;
    memoryMap[i].status     = memoryAvailable;
    }
  for (i=0x0;i<=((0x101000+(sizeof(struct memMap)*systemVitals->numPages))/4096);i++) {
    memoryMap[i].status = memoryNotAvailable;
    }
  //kprintf("INITIALIZED\n");
  return;
  }

/************************************************************************

Function: void *findFreePage();
Description: This Function Returns A Free Physical Page
Notes:

************************************************************************/  
void *findFreePage(pidType pid) {
  uInt32 i = 0x0;
  for (i=0x0;i<systemVitals->numPages;i++) {
    //Check For An Available Page
    if (memoryMap[i].status == memoryAvailable) {
      memoryMap[i].status = memoryNotAvailable;
      return((void *)memoryMap[i].pageAddr);
      }
    }
  //Return NULL If No Free Page Is Found
  return(NULL);
  }

/************************************************************************

Function: void freePage(uInt32 pageAddr);
Description: This Function Will Free A Page
Notes:

************************************************************************/
void freePage(uInt32 pageAddr) {
  int mapIndex = (pageAddr/4096);
  memoryMap[mapIndex].cowCounter = 0x0;
  memoryMap[mapIndex].pid        = -1;
  memoryMap[mapIndex].status     = memoryAvailable;
  return;
  }