/************************************************************************************** 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 0xC0000000 ************************************************************************/ void initPagingSystem() { int i=0,x=0; uLong *pageTable; //Allocate A Page For The Kernels VM Space Page Directory kernelPageDirectory = (uLong *)allocPage(); //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 *)allocPage(); 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 *)allocPage(); //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[768] & 0xFFFFF000); pageTable[0] = (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=0x100000;i<=(0x100000+(numPages*8));i+=0x1000) { remapPage(i,(0xE6667000+(i-0x100000))); } memoryMap = (mMap *)0xE6667000; } void *getFreePage() { short x=0,y=0; uLong *pageTableSrc = 0x0; for (x=896;x<1024;x++) { pageTableSrc = (uLong *)(pageDirectory[x] & 0xFFFFF000); for (y=0;y<1024;y++) { if (pageTableSrc[y] == 0) { kprintf("found One: [%i][%i], Addr: (%i)\n",x,y,((x*(1024*4096))+(y*4096))); while(1); } } } return(0x0); } unsigned int allocPage() { uLong page; page = findFreepage(_current->id); /* if (memoryStart%4096 != 0) { memoryStart += 4096 - memoryStart%4096; } page = memoryStart; memoryStart += 4096; */ return(page); } /************************************************************************ 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 *)0xC0000000; //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)allocPage() | pageDefault; //Also Add It To Virtual Space So We Can Make Changes Later pageTable = (uLong *)(0xBFC00000 + (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 *)(0xBFC00000 + (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" ); } 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] = allocPage() | pageDefault; dst = (uChar *) (pageTable[page] & 0xFFFFF000); for (i=0;i<4096;i++) { dst[i] = src[i]; } } else { pageTable[page] = allocPage() | pageDefault; } } asm( "movl %cr3,%eax\n" "movl %eax,%cr3\n" ); } 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" );