/**************************************************************************************
 Copyright (c) 2002
      The UbixOS Project

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: remappage.c,v 1.1 2003/04/20 12:11:30 reddawg Exp $
**************************************************************************************/

#include <vmm/paging.h>
#include <vmm/memory.h>
#include <ubixos/schedule.h>

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

Function: void remapPage(Physical Source,Virtual Destination)
Description: This Function Will Remap A Physical Page Into Virtual Space
Notes:

07/29/02 - Rewrote This To Work With Our New Paging System

07/30/02 - Changed Address Of Page Tables And Page Directory

************************************************************************/
void remapPage(uLong source,uLong dest) {
  uShort destPageDirectoryIndex=0,destPageTableIndex=0;
  uLong *pageDir,*pageTable;
  //Set Pointer pageDirectory To Point To The Virtual Mapping Of The Page Directory
  pageDir = (uLong *)parentPageDirAddr;
  //Check To See If Page Table Exists
  destPageDirectoryIndex = (dest/(1024*4096));
  if ((pageDir[destPageDirectoryIndex] & pagePresent) != pagePresent) {
    //If Page Table Is Non Existant Then Set It Up
    pageDir[destPageDirectoryIndex] = (uLong)findFreePage(_current->id) | pageDefault;
    //Also Add It To Virtual Space So We Can Make Changes Later
    pageTable = (uLong *)(tablesBaseAddress + (4096 * 767));
    pageTable[destPageDirectoryIndex] = pageDir[destPageDirectoryIndex];
    //Reload Page Directory
    asm(
      "movl %cr3,%eax\n"
      "movl %eax,%cr3\n"
      );
    }
  //Set Address To Page Table
  pageTable = (uLong *)(tablesBaseAddress + (4096 * destPageDirectoryIndex));
  //Get The Index To The Page Table
  destPageTableIndex = ((dest-(destPageDirectoryIndex*(1024*4096)))/4096);
  //If The Page Is Mapped In Free It Before We Remap
  if ((pageTable[destPageTableIndex] & pagePresent) == pagePresent) {
    //Clear The Page First For Security Reasons
    freePage(((uLong)pageTable[destPageTableIndex] & 0xFFFFF000));
    }
  //Set The Source Address In The Destination
  pageTable[destPageTableIndex] = (uLong)(source | pageDefault);
  //Reload The Page Table;
  asm(
    "movl %cr3,%eax\n"
    "movl %eax,%cr3\n"
    );
  //Return
  return;
  }
