/**************************************************************************************
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$
**************************************************************************************/
#include <vmm/paging.h>
#include <vmm/memory.h>
#include <ubixos/idt.h>
#include <ubixos/gdt.h>
#include <ubixos/types.h>
#include <ubixos/schedule.h>
#include <drivers/video.h>
uLong *pageDirectory = 0x0;
uLong memoryStart = 0x100000;
uLong *kernelPageDirectory = 0x0;
/************************************************************************
Function: void initPageSystem()
Description: This Function Will Initialize The Ubix Paging Sytem
Notes:
07/29/02 - I Started The Rewrite Of This Function Hopefully All Goes Well
This Is The Startup Routine To Build The Initial VM Space.
Also Note I May Switch The Page Fault To A Task Gate.
07/30/02 - I Decided That To Save On Performance All Page Tables Will Be
Mapped In At BFC00000 So That Memory Does Not Need To Be Relocated
To Alter Page Tables.
Also Put Page Index At 0x100000
************************************************************************/
void initPagingSystem() {
int i=0,x=0;
uLong *pageTable;
//Allocate A Page For The Kernels VM Space Page Directory
kernelPageDirectory = (uLong *)findFreePage(_current->id);
//Make Sure The Page Directory Is Clean
for (i=0;i<pageEntries;i++) {
kernelPageDirectory[i] = (uLong)0x0;
}
//Allocate A Page For The First 4MB Of Memory
pageTable = (uLong *)findFreePage(_current->id);
kernelPageDirectory[0] = (uLong)((uLong)(pageTable) | pageDefault);
//Make Sure The Page Table Is Clean
for (i=0;i<pageEntries;i++) {
pageTable[i] = (uLong)0x0;
}
//Map The First 1MB Of Memory To The Kernel VM Space
for (i=0;i<(pageEntries/4);i++) {
pageTable[i] = (uLong)((i*0x1000) | pageDefault);
}
//Create Page Tables For The Top 1GB Of VM Space This Is To Be Shared With All VM Spaces
for (i=768;i<pageEntries;i++) {
pageTable = (uLong *)findFreePage(-1);
//Make Sure The Page Table Is Clean
for (x=0;x<pageEntries;x++) {
pageTable[x] = (uLong)0x0;
}
//Map In The Page Directory
kernelPageDirectory[i] = (uLong)((uLong)(pageTable) | pageDefault);
}
//Set Up Memory To Be All The Allocated Page Directories
pageTable = (uLong *)findFreePage(-1);
kernelPageDirectory[767] = ((uLong)pageTable | pageDefault);
for (i=0;i<pageEntries;i++) {
pageTable[i] = kernelPageDirectory[i];
}
//Also Set Up Page Directory To Be The The First Page In 0xE0400000
pageTable = (uLong *)(kernelPageDirectory[0] & 0xFFFFF000);
pageTable[256] = (uLong)((uLong)(kernelPageDirectory) | pageDefault);
//Now Lets Turn On Paging With This Initial Page Table
asm(
"movl %0,%%eax \n"
"movl %%eax,%%cr3 \n"
"movl %%cr0,%%eax \n"
"orl $0x80010000,%%eax \n" //Flags To Enable Paging With Protection
"movl %%eax,%%cr0 \n"
: : "d" ((uLong *)(kernelPageDirectory))
);
//Now Add The IDT Entry For Page Faults
setVector(_pageFault,14,dPresent + dInt + dDpl1);
//Remap The Memory List
for (i=0x101000;i<=(0x101000+(numPages*sizeof(mMap)));i+=0x1000) {
remapPage(i,(0xE6667000+(i-0x101000)));
}
memoryMap = (mMap *)0xE6667000;
}
/************************************************************************
Function: void clearVirtualPage(uLong pageAddr);
Description: This Will Null Out A Page Of Memory
Notes:
************************************************************************/
void clearVirtualPage(uLong pageAddr) {
uLong *src = 0x0;
int counter = 0x0;
//Set Source Pointer To Virtual Page Address
src = (uLong *)pageAddr;
//Clear Out The Page
for (counter=0;counter<pageEntries;counter++) {
(uLong)src[counter] = (uLong)0x0;
}
//Return
return;
}
/************************************************************************
Function: void *getPhysicalAddr();
Description: Returns The Physical Address Of The Virtual Page
Notes:
************************************************************************/
void *getPhysicalAddr(uLong pageAddr) {
int pageDirectoryIndex=0,pageTableIndex=0;
uLong *pageTable = 0x0;
//Get The Index To The Page Directory
pageDirectoryIndex = (pageAddr/(1024*4096));
//Get The Index To The Page Table
pageTableIndex = ((pageAddr-(pageDirectoryIndex*(1024*4096)))/4096);
//Set pageTable To The Virtual Address Of Table
pageTable = (uLong *)(tablesBaseAddress + (4096 * pageDirectoryIndex));
//Return The Physical Address Of The Page
return((void *)(pageTable[pageTableIndex] & 0xFFFFF000));
}
/************************************************************************
Function: void freePage(uLong pageAddr);
Description: This Function Marks The Page As Free
Notes:
07/30/02 - This Was Moved Out Of memory.c Into Here
************************************************************************/
void freePage(uLong pageAddr) {
int pageIndex = 0x0;
//Find The Page Index To The Memory Map
pageIndex = (pageAddr/4096);
//Check If Page COW Is Greater Then 0 If It Is Dec It If Not Free It
if (memoryMap[pageIndex].cowCounter == 0) {
//Set Page As Avail So It Can Be Used Again
/*
Here Is The Problem Area I Need To Do Some Extencive Testing
As To Why Im Freeing A Page That Shouldnt Be Freed
*/
//memoryMap[pageIndex].status = memAvail;
memoryMap[pageIndex].cowCounter = 0x0;
memoryMap[pageIndex].pid = -2;
freePages++;
}
else {
//Adjust The COW Counter
adjustCowCounter(((uLong)memoryMap[pageIndex].pageAddr),-1);
}
//Return
return;
}
/************************************************************************
Function: void unmapPage(uLong pageAddr,int flags);
Description: This Function Will Unmap A Page From The Kernel VM Space
The Flags Variable Decides If Its To Free The Page Or Not
A Flag Of 0 Will Free It And A Flag Of 1 Will Keep It
Notes:
07/30/02 - I Have Decided That This Should Free The Physical Page There
Is No Reason To Keep It Marked As Not Available
07/30/02 - Ok A Found A Reason To Keep It Marked As Available I Admit
Even I Am Not Perfect Ok The Case Where You Wouldn't Want To
Free It Would Be On Something Like Where I Allocated A Page
To Create A New Virtual Space So Now It Has A Flag
************************************************************************/
void unmapPage(uLong pageAddr,int flags) {
int pageDirectoryIndex=0,pageTableIndex=0;
uLong *pageTable = 0x0;
//Get The Index To The Page Directory
pageDirectoryIndex = (pageAddr/(1024*4096));
//Get The Index To The Page Table
pageTableIndex = ((pageAddr-(pageDirectoryIndex*(1024*4096)))/4096);
//Set pageTable To The Virtual Address Of Table
pageTable = (uLong *)(tablesBaseAddress + (4096 * pageDirectoryIndex));
//Free The Physical Page If Flags Is 0
if (flags == 0) {
clearVirtualPage(pageAddr);
freePage((uLong)(pageTable[pageTableIndex] & 0xFFFFF000));
}
//Unmap The Page
pageTable[pageTableIndex] = 0x0;
//Rehash The Page Directory
asm(
"movl %cr3,%eax\n"
"movl %eax,%cr3\n"
);
//Return
return;
}
/************************************************************************
Function: void *createVirtualSpace(pid_t);
Description: Creates A Virtual Space For A New Task
Notes:
07/30/02 - This Is Going To Create A New VM Space However Its Going To
Share The Same Top 1GB Space With The Kernels VM And Lower
1MB Of VM Space With The Kernel
07/30/02 - Note This Is Going To Get The Top 1Gig And Lower 1MB Region
From The Currently Loaded Page Directory This Is Safe Because
All VM Spaces Will Share These Regions
07/30/02 - Note I Realized A Mistake The First Page Table Will Need To Be
A Copy But The Page Tables For The Top 1GB Will Not Reason For
This Is That We Just Share The First 1MB In The First Page Table
So We Will Just Share Physical Pages.
08/02/02 - Added Passing Of pid_t pid For Better Tracking Of Who Has Which
Set Of Pages
************************************************************************/
void *createVirtualSpace(pid_t pid) {
void *newPageDirectoryAddress = 0x0;
uLong *parentPageDirectory = 0x0,*newPageDirectory = 0x0;
uLong *parentPageTable = 0x0,*newPageTable = 0x0;
int x = 0;
//Set Address Of Parent Page Directory
parentPageDirectory = (uLong *)parentPageDirAddr;
//Allocate A New Page For The New Page Directory
newPageDirectory = (uLong *)getFreePage(pid);
//Set newPageDirectoryAddress To The Newly Created Page Directories Page
newPageDirectoryAddress = getPhysicalAddr((uLong)newPageDirectory);
//First Set Up A Flushed Page Directory
for (x=0;x<pageEntries;x++) {
newPageDirectory[x] = (uLong)0x0;
}
//Map The Top 1GB Region Of The VM Space
for (x=768;x<pageEntries;x++) {
newPageDirectory[x] = parentPageDirectory[x];
}
/*
Allocate A New Page For The The First Page Table Where We Will Map The
Lower Region
*/
newPageTable = (uLong *)getFreePage(pid);
//Flush The Page From Garbage In Memory
for (x=0;x<pageEntries;x++) {
newPageTable[x] = (uLong)0x0;
}
//Map This Into The Page Directory
newPageDirectory[0] = ((uLong)getPhysicalAddr((uLong)newPageTable) | pageDefault);
//Set Address Of Parents Page Table
parentPageTable = (uLong *)tablesBaseAddress;
//Map The First 1MB Worth Of Pages
for (x=0;x<(pageEntries/4);x++) {
newPageTable[x] = parentPageTable[x];
}
//Set Virtual Mapping For Page Directory
newPageTable[256] = ((uLong)getPhysicalAddr((uLong)newPageDirectory) | pageDefault);
/*
Now The Fun Stuff Build The Initial Virtual Page Space So We Don't
Have To Worry About Mapping Them In Later How Ever I'm Concerned This
May Become A Security Issue
*/
//First Lets Unmap The Previously Allocated Page Table
unmapPage((uLong)newPageTable,1);
//Allocate A New Page Table
newPageTable = (uLong *)getFreePage(pid);
//First Set Our Page Directory To Contain This
newPageDirectory[767] = (uLong)getPhysicalAddr((uLong)newPageTable) | pageDefault;
//Now Lets Build The Page Table
for (x=0;x<pageEntries;x++) {
newPageTable[x] = newPageDirectory[x];
}
//Now We Are Done So Lets Unmap This Page
unmapPage((uLong)newPageTable,1);
//Now We Are Done With The Page Directory So Lets Unmap That Too
unmapPage((uLong)newPageDirectory,1);
//Return Physical Address Of Page Directory
return(newPageDirectoryAddress);
}
/************************************************************************
Function: void *copyVirtualSpace(pid_t pid);
Description: Creates A Copy Of A Virtual Space And Set All NON Kernel
Space To COW For A Fork This Will Also Alter The Parents
VM Space To Make That COW As Well
Notes:
08/02/02 - Added Passing Of pid_t pid So We Can Better Keep Track Of
Which Task Has Which Physical Pages
************************************************************************/
void *copyVirtualSpace(pid_t pid) {
void *newPageDirectoryAddress = 0x0;
uLong *parentPageDirectory = 0x0,*newPageDirectory = 0x0;
uLong *parentPageTable = 0x0,*newPageTable = 0x0;
uLong *parentStackPage = 0x0,*newStackPage = 0x0;
int x = 0,i = 0,s = 0;
//Set Address Of Parent Page Directory
parentPageDirectory = (uLong *)parentPageDirAddr;
//Allocate A New Page For The New Page Directory
newPageDirectory = (uLong *)getFreePage(pid);
//Set newPageDirectoryAddress To The Newly Created Page Directories Page
newPageDirectoryAddress = getPhysicalAddr((uLong)newPageDirectory);
//First Set Up A Flushed Page Directory
for (x=0;x<pageEntries;x++) {
newPageDirectory[x] = (uLong)0x0;
}
//Map The Top 1GB Region Of The VM Space
for (x=768;x<pageEntries;x++) {
newPageDirectory[x] = parentPageDirectory[x];
}
/*
Now For The Fun Stuff For Page Tables 1-766 We Must Map These And Set
The Permissions On Every Mapped Pages To COW This Will Conserve
Memory Because The Two VM Spaces Will Be Sharing Some Pages
*/
for (x=1;x<=766;x++) {
//If Page Table Exists Map It
if (parentPageDirectory[x] != 0) {
//Set Parent To Propper Page Table
parentPageTable = (uLong *)(tablesBaseAddress + (4096 * x));
//Allocate A New Page Table
newPageTable = (uLong *)getFreePage(pid);
//Set Parent And New Pages To COW
for (i=0;i<pageEntries;i++) {
//If Page Is Mapped
if (parentPageTable[i] != 0) {
//Check To See If Its A Stack Page
if (((uLong)parentPageTable[i] & pageStack) == pageStack) {
//Alloc A New Page For This Stack Page
newStackPage = (uLong *)getFreePage(pid);
//Set Pointer To Parents Stack Page
parentStackPage = (uLong *) (((1024*4096)*x)+(4096*i));
//Copy The Tack Byte For Byte (I Should Find A Faster Way)
for (s=0;s<pageEntries;s++) {
newStackPage[s] = parentStackPage[s];
}
//Insert New Stack Into Page Table
newPageTable[i] = ((uLong)getPhysicalAddr((uLong)newStackPage) | pageDefault | pageStack);
//Unmap From Kernel Space
unmapPage((uLong)newStackPage,1);
}
else {
//Set Page To COW In Parent And Child Space
newPageTable[i] = (((uLong)parentPageTable[i] & 0xFFFFF000) | (pagePresent | pageCow));
//Increment The COW Counter For This Page
if (((uLong)parentPageTable[i] & pageCow) == pageCow) {
adjustCowCounter(((uLong)parentPageTable[i] & 0xFFFFF000),1);
}
else {
adjustCowCounter(((uLong)parentPageTable[i] & 0xFFFFF000),2);
parentPageTable[i] = newPageTable[i];
}
}
}
else {
newPageTable[i] = (uLong)0x0;
}
}
//Put New Page Table Into New Page Directory
newPageDirectory[x] = ((uLong)getPhysicalAddr((uLong)newPageTable) | pageDefault);
//Unmap Page From Kernel Space But Keep It Marked As Not Avail
unmapPage((uLong)newPageTable,1);
}
else {
newPageDirectory[x] = (uLong)0x0;
}
}
/*
Allocate A New Page For The The First Page Table Where We Will Map The
Lower Region
*/
newPageTable = (uLong *)getFreePage(pid);
//Flush The Page From Garbage In Memory
for (x=0;x<pageEntries;x++) {
newPageTable[x] = (uLong)0x0;
}
//Map This Into The Page Directory
newPageDirectory[0] = ((uLong)getPhysicalAddr((uLong)newPageTable) | pageDefault);
//Set Address Of Parents Page Table
parentPageTable = (uLong *)tablesBaseAddress;
//Map The First 1MB Worth Of Pages
for (x=0;x<(pageEntries/4);x++) {
newPageTable[x] = parentPageTable[x];
}
//Map The Next 3MB Worth Of Pages But Make Them COW
for (x=(pageEntries/4)+1;x<pageEntries;x++) {
//If Page Is Avaiable Map It
if (parentPageTable[x] != 0) {
//Set Pages To COW
newPageTable[x] = (((uLong)parentPageTable[x] & 0xFFFFF000) | (pagePresent | pageCow));
//Increment The COW Counter For This Page
if (((uLong)parentPageTable[x] & pageCow) == pageCow) {
adjustCowCounter(((uLong)parentPageTable[x] & 0xFFFFF000),1);
}
else {
adjustCowCounter(((uLong)parentPageTable[x] & 0xFFFFF000),2);
parentPageTable[x] = newPageTable[x];
}
}
else {
newPageTable[x] = (uLong)0x0;
}
}
//Set Virtual Mapping For Page Directory
newPageTable[256] = ((uLong)getPhysicalAddr((uLong)newPageDirectory) | pageDefault);
/*
Now The Fun Stuff Build The Initial Virtual Page Space So We Don't
Have To Worry About Mapping Them In Later How Ever I'm Concerned This
May Become A Security Issue
*/
//First Lets Unmap The Previously Allocated Page Table
unmapPage((uLong)newPageTable,1);
//Allocate A New Page Table
newPageTable = (uLong *)getFreePage(pid);
//First Set Our Page Directory To Contain This
newPageDirectory[767] = (uLong)getPhysicalAddr((uLong)newPageTable) | pageDefault;
//Now Lets Build The Page Table
for (x=0;x<pageEntries;x++) {
newPageTable[x] = newPageDirectory[x];
}
//Now We Are Done So Lets Unmap This Page
unmapPage((uLong)newPageTable,1);
//Now We Are Done With The Page Directory So Lets Unmap That Too
unmapPage((uLong)newPageDirectory,1);
//kprintf("Test: [%i]\n",pid);
//Return Physical Address Of Page Directory
return(newPageDirectoryAddress);
}
/************************************************************************
Function: void setPageAttributes(uLong pageAddr,int attributes;
Description: This Function Will Set The Page Attributes Such As
A Read Only Page, Stack Page, COW Page, ETC.
Notes:
************************************************************************/
void setPageAttribute(uLong pageAddr,int attributes) {
int directoryIndex,tableIndex;
uLong *pageTable;
//Get Index To Page Directory
directoryIndex = (pageAddr/(1024*4096));
//Get Index To Page Table
tableIndex = ((pageAddr-(directoryIndex*(1024*4096)))/4096);
//Set Table Pointer
pageTable = (uLong *)(tablesBaseAddress + (4096 * directoryIndex));
//Set Attribute If Page Is Mapped
if (pageTable[tableIndex] != 0) {
pageTable[tableIndex] = ((pageTable[tableIndex] & 0xFFFFF000) | attributes);
}
//Reload The Page Table;
asm(
"movl %cr3,%eax\n"
"movl %eax,%cr3\n"
);
//Return
return;
}
/************************************************************************
Function: void *getFreePage(pid_t pid);
Description: Returns A Free Page Mapped To The VM Space
Notes:
07/30/02 - This Returns A Free Page In The Top 1GB For The Kernel
************************************************************************/
void *getFreePage(pid_t pid) {
int x=0,y=0;
uLong *pageTableSrc = 0x0;
//Lets Search For A Free Page
for (x=768;x<1024;x++) {
//Set Page Table Address
pageTableSrc = (uLong *)(tablesBaseAddress + (4096*x));
for (y=0;y<1024;y++) {
//Loop Through The Page Table Find An UnAllocated Page
if ((uLong)pageTableSrc[y] == (uLong)0x0) {
//Map A Physical Page To The Virtual Page
remapPage(findFreePage(pid),((x*(1024*4096))+(y*4096)));
//Clear This Page So No Garbage Is There
clearVirtualPage((uLong)((x*(1024*4096))+(y*4096)));
//Return The Address Of The Newly Allocate Page
return((void *)((x*(1024*4096))+(y*4096)));
}
}
}
//If No Free Page Was Found Return NULL
return(0x0);
}
/************************************************************************
Function: void *getFreeVirtualPage(pid_t pid);
Description: Returns A Free Page Mapped To The VM Space
Notes:
08/11/02 - This Will Return Next Avilable Free Page Of Tasks VM Space
************************************************************************/
void *getFreeVirtualPage(pid_t pid) {
int x=0,y=0;
uLong *pageTableSrc = 0x0;
//Lets Search For A Free Page
for (x=(_current->vmStart/(1024*4096));x<1024;x++) {
//Set Page Table Address
pageTableSrc = (uLong *)(tablesBaseAddress + (4096*x));
for (y=0;y<1024;y++) {
//Loop Through The Page Table Find An UnAllocated Page
if ((uLong)pageTableSrc[y] == (uLong)0x0) {
//Map A Physical Page To The Virtual Page
remapPage(findFreePage(pid),((x*(1024*4096))+(y*4096)));
//Clear This Page So No Garbage Is There
clearVirtualPage((uLong)((x*(1024*4096))+(y*4096)));
//Return The Address Of The Newly Allocate Page
return((void *)((x*(1024*4096))+(y*4096)));
}
}
}
//If No Free Page Was Found Return NULL
return(0x0);
}
/************************************************************************
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] == 0) {
//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] > 0) {
//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;
}
/************************************************************************
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][%i]\n",memAddr,_current->id);
freeProcessPages(_current->id);
_current->status = EMPTY;
if (_current->id < -1) {
panic();
}
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][%i]\n",memAddr,_current->id);
freeProcessPages(_current->id);
_current->status = EMPTY;
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"
);