Newer
Older
Scratch / lockwasher / src / sys / vmm / pagefault.c
/**************************************************************************************
 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: pagefault.c,v 1.1 2003/04/20 12:11:30 reddawg Exp $
**************************************************************************************/

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

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

Function: void pageFault();
Description: This Function Is The Second Half Of The Page Fault ISR
             Currently It Handles COW However I Need To Prepar It For
             Swapping
Notes:

07/30/02 - Fixed COW However I Need To Think Of A Way To Impliment
           A Paging System Also Start To Add Security Levels

************************************************************************/
void pageFault() {
  uLong memAddr = 0,i = 0, pageTableIndex = 0,pageDirectoryIndex = 0;
  uLong *pageDir,*pageTable;
  uLong *src,*dst;
  pageDir = (uLong *)parentPageDirAddr;
  //Get Memory Address For Violation
  asm(
    "movl %%cr2,%%eax\n"
    "movl %%eax,%0\n"

    : "=g" (memAddr)
    );
  //Calculate The Page Directory Index
  pageDirectoryIndex = (memAddr/(1024*4096));
  //Calculate The Page Table Index
  pageTableIndex = ((memAddr-(pageDirectoryIndex*(1024*4096)))/4096);
  if (pageDir[pageDirectoryIndex] == 0) {
    //Creat A Routine For Non Mapped Memory
    kprintf("Segfault At Address: [0x%X][0x%X][%i]\n",memAddr,_current->tss.esp,_current->id);
    if ((uInt32)_current->tss.cr3 != (uInt32)kernelPageDirectory) {
      freeProcessPages(_current->id);
      }
    setTaskStatus(_current->id,EXITING);
    if (_current->id <= -1) {
      //kPanic("Kernel Crashed.\n");
      }
    schedule();
    while (1);
    }
  else {
    //Set pageTable To Point To Virtual Address Of Page Table
    pageTable = (uLong *)(tablesBaseAddress + (4096 * pageDirectoryIndex));
    if (((uLong)pageTable[pageTableIndex] & pageCow) == pageCow) {
      //Set Src To Base Address Of Page To Copy
      src = (uLong *) ((1024*4096*pageDirectoryIndex) + (4096*pageTableIndex));
      //Allocate A Free Page For Destination
      dst = (uLong *) getFreePage(-1);
      //Copy Memory
      for (i=0;i<pageEntries;i++) {
        dst[i] = src[i];
        }
      //Adjust The COW Counter For Physical Page
      adjustCowCounter(((uLong)pageTable[pageTableIndex] & 0xFFFFF000),-1);
      //Remap In New Page
      pageTable[pageTableIndex] = (uLong)((uLong)getPhysicalAddr((uLong)dst)|pageDefault);
      //Unlink From Memory Map Allocated Page
      unmapPage((uLong)dst,1);
      }
    else {
      //Need To Create A Routine For Attempting To Access Non Mapped Memory
      kprintf("Segfault At Address: [0x%X][0x%X][%i] Non Mapped\n",memAddr,_current->tss.esp,_current->id);
      /*
      kprintf("EAX:    [0x%X],EBX:    [0x%X]\n",_current->tss.eax,_current->tss.ebx);
      kprintf("ECX:    [0x%X],EDX:    [0x%X]\n",_current->tss.ecx,_current->tss.edx);
      kprintf("ESP:    [0x%X],EPB:    [0x%X]\n",_current->tss.esp,_current->tss.ebp);
      kprintf("EIP:    [0x%X],CR2:    [0x%X]\n",_current->tss.eip,memAddr);
      kprintf("CS:     [0x%X],DS:     [0x%X]\n",_current->tss.cs,_current->tss.ds);
      kprintf("SS:     [0x%X],ES:     [0x%X]\n",_current->tss.ss,_current->tss.es);
      kprintf("FS:     [0x%X],GS:     [0x%X]\n",_current->tss.gs,_current->tss.gs);
      kprintf("EFLAGS: [0x%X]         \n",_current->tss.eflags);
      */

      if ((uInt32)_current->tss.cr3 != (uInt32)kernelPageDirectory) {
        freeProcessPages(_current->id);
        }
      setTaskStatus(_current->id,EXITING);
      if (_current->id <= -1) {
        panic();
        }
      schedule();
      while(1);
      }
    }
  asm(
    "movl %cr3,%eax\n"
    "movl %eax,%cr3\n"
    );
  }

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

Function: void _pageFault()
Description: This Is The ASM Code That Calls The pageFault() Function
Notes:

************************************************************************/
asm(
    ".global _pageFault  \n"
    "_pageFault:         \n"
    "xchgl %eax,(%esp)   \n"
    "pushl %ecx          \n"
    "pushl %edx          \n"
    "push %ds            \n"
    "push %es            \n"
    "push %fs            \n"
    "call pageFault      \n"
    "pop %fs             \n"
    "pop %es             \n"
    "pop %ds             \n"
    "popl %edx           \n"
    "popl %ecx           \n"
    "popl %eax           \n"
    "iret                \n"
    );