/**************************************************************************************
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>
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=767;i<pageEntries;i++) {
pageTable = (uLong *)findFreePage(_current->id);
//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 At E0000000 To Be All The Allocated Page Directories
pageTable = (uLong *)(kernelPageDirectory[767] & 0xFFFFF000);
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 + dDpl3);
//Remap The Memory List
for (i=0x101000;i<=(0x101000+(numPages*8));i+=0x1000) {
remapPage(i,(0xE6667000+(i-0x101000)));
}
memoryMap = (mMap *)0xE6667000;
}
/************************************************************************
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);
//Set Page As Avail So It Can Be Used Again
memoryMap[pageIndex].status = memAvail;
}
/************************************************************************
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) {
freePage((pageTable[pageTableIndex] & 0xFFFFF000));
}
//Unmap The Page
pageTable[pageTableIndex] = 0x0;
//Rehash The Page Directory
asm(
"movl %cr3,%eax\n"
"movl %eax,%cr3\n"
);
}
/************************************************************************
Function: void *createVirtualSpace();
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.
************************************************************************/
void *createVirtualSpace() {
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();
//Set newPageDirectoryAddress To The Newly Created Page Directories Page
newPageDirectoryAddress = getPhysicalAddr((uLong)newPageDirectory);
//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();
//Map This Into The Page Directory
newPageDirectory[0] = ((uLong)newPageTable | pageDefault);
//Set Address Of Parents Page Table
parentPageTable = (uLong *)tablesBaseAddress;
//Map The First 1MB Worth Of Pages
for (x=0;x<256;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();
//First Set Our Page Directory To Contain This
newPageDirectory[767] = (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 *getFreePage();
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() {
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(_current->id),((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);
//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"
);
}
/************************************************************************
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 cr2 = 0,i = 0, page = 0,pi = 0;
uLong *pageDir,*pageTable;
uChar *src,*dst;
if ((_current->id < 0) || (_current->id > numTasks)) {
pageDir = pageDirectory;
}
else {
pageDir = (long *)_current->tss.cr3;
}
asm(
"movl %%cr2,%%eax\n"
"movl %%eax,%0\n"
: "=g" (cr2)
);
pi = cr2/(1024*4096);
page = (cr2-(pi*(1024*4096)))/4096;
if (pageDir[pi] == 0) {
kprintf("Dumb Ass You Forgot To Allocate Memory [%i]\n",cr2);
while (1);
// pageTable = (uLong *)allocPage();
pageDir[pi] = (uLong)pageTable | pageDefault;
for (i=0;i<1024;i++) {
pageTable[i] = 0;
}
// pageTable[page] = allocPage() | pageDefault;
}
else {
pageTable = (uLong *)(pageDir[pi] & 0xFFFFF000);
if (pageTable[page] > 0) {
src = (uChar *) (pageTable[page] & 0xFFFFF000);
pageTable[page] = findFreePage(_current->id) | pageDefault;
dst = (uChar *) (pageTable[page] & 0xFFFFF000);
for (i=0;i<4096;i++) {
dst[i] = src[i];
}
}
else {
pageTable[page] = findFreePage(_current->id) | pageDefault;
}
}
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"
);