Newer
Older
ubixos / src / sys / vmm / pagefault.c
/*****************************************************************************************
 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$

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

#include <vmm/vmm.h>
#include <ubixos/sched.h>
#include <ubixos/kpanic.h>
#include <ubixos/spinlock.h>
#include <lib/kprintf.h>

static spinLock_t pageFaultSpinLock = SPIN_LOCK_INITIALIZER;

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

Function: void vmmPageFault();
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 vmm_pageFault(uInt32 memAddr,uInt32 eip) {
  uInt32 i = 0, pageTableIndex = 0,pageDirectoryIndex = 0;
  uInt32 *pageDir = 0x0,*pageTable = 0x0;
  uInt32 *src = 0x0,*dst = 0x0;
  
  spinLock(&pageFaultSpinLock);
  
  pageDir = (uInt32 *)parentPageDirAddr;
  
  //Calculate The Page Directory Index
  pageDirectoryIndex = (memAddr >> 22);
  
  //Calculate The Page Table Index
  pageTableIndex = ((memAddr >> 12) & 0x3FF);

  if (pageDir[pageDirectoryIndex] == 0x0) {
    //Creat A Routine For Non Mapped Memory
    kprintf("Segfault At Address: [0x%X][0x%X][%i][0x%X], Not A Valid Page Table\n",memAddr,_current->tss.esp,_current->id,eip);
    spinUnlock(&pageFaultSpinLock);
    endTask(_current->id);
    }
  else {
    //Set pageTable To Point To Virtual Address Of Page Table
    pageTable = (uInt32 *)(tablesBaseAddress + (0x1000 * pageDirectoryIndex));
    if (((uInt32)pageTable[pageTableIndex] & pageCow) == pageCow) {
      //Set Src To Base Address Of Page To Copy
      src = memAddr & 0xFFFFF000;
      //Allocate A Free Page For Destination
      dst = (uInt32 *) vmmGetFreePage(_current->id);
      //Copy Memory
      for (i=0;i<pageEntries;i++) {
        dst[i] = src[i];
        }
      //Adjust The COW Counter For Physical Page
      adjustCowCounter(((uInt32)pageTable[pageTableIndex] & 0xFFFFF000),-1);
      //Remap In New Page
      pageTable[pageTableIndex] = (uInt32)((uInt32)vmmGetPhysicalAddr((uInt32)dst)|pageDefault);
      //Unlink From Memory Map Allocated Page
      vmmUnmapPage((uInt32)dst,1);
      }
    else if (pageTable[pageTableIndex] != 0x0) {
      kprintf("pageTable: [0x%X]\n",pageTable[pageTableIndex]);
      }
    else {
      spinUnlock(&pageFaultSpinLock);
      //Need To Create A Routine For Attempting To Access Non Mapped Memory
      kprintf("Segfault At Address: [0x%X][0x%X][%i][0x%X] Non Mapped\n",memAddr,_current->tss.esp,_current->id,eip);
      endTask(_current->id);
      //_current->state = DEAD;
      //sched();
      }
    }
  asm volatile(
    "movl %cr3,%eax\n"
    "movl %eax,%cr3\n"
    );
  spinUnlock(&pageFaultSpinLock);
  }
  
/***
 $Log$
 Revision 1.5  2004/07/24 20:00:51  reddawg
 Lots of changes to the vmm subsystem.... Page faults have been adjust to now be blocking on a per thread basis not system wide. This has resulted in no more deadlocks.. also the addition of per thread locking has removed segfaults as a result of COW in which two tasks fault the same COW page and try to modify it.

 Revision 1.4  2004/07/24 17:47:28  reddawg
 vmm_pageFault: deadlock resolved thanks to a propper solution suggested by geist

 Revision 1.3  2004/07/19 02:05:26  reddawg
 vmmPageFault: had a potential memory leak here for one page it was still using sysID on certain COW scenarios

 Revision 1.2  2004/06/10 22:23:56  reddawg
 Volatiles

 Revision 1.1.1.1  2004/04/15 12:06:52  reddawg
 UbixOS v1.0

 Revision 1.4  2004/04/13 16:36:34  reddawg
 Changed our copyright, it is all now under a BSD-Style license

 END
 ***/