// vmm.c // Author: Josh Holtrop // Date: 09/30/03 PageDirectory *vmm_PDBR = 0; dword vmm_first_virtual_address = 0; void vmm_init() { if(!(vmm_PDBR = mm_palloc(1, PID_KERNEL))) { printf("ERROR! COULD NOT ALLOCATE PAGE FOR INITIAL PAGE DIRECTORY!!\n"); halt(); } dword address = 0; PageTable *tmp; int pde; int pte; for (pde = 0; pde<1024; pde++) { for (pte = 0; pte<1024; pte++) { if (pte == 0) { if (!(tmp = mm_palloc(1, PID_KERNEL))) { printf("ERROR! Page could not be allocated for PDE %d, PTE %d!\n", pde, pte); halt(); } vmm_PDBR->pageTables[pde] = ((dword)tmp | 0x00000003); //present, read/write, supervisor priviledge } tmp = (PageTable *)((vmm_PDBR->pageTables[pde]) & 0xFFFFF000); //mask out address of page table tmp->page[pte] = (address | 0x00000003); //present, read/write, supervisor priviledge address += 4096; if (address >= mm_totalmem) pte = pde = 2000; } } //we also need to map in the video framebuffer memory: dword framebuffer_end = video_mode.PhysBasePtr + video_mode.BitsPerPixel/8 * video_mode.XResolution * video_mode.YResolution; //framebuffer size in bytes address = video_mode.PhysBasePtr; if (address != 0) //we do have a graphics buffer { pde = (address & 0xFFC00000) >> 22; pte = (address & 0x003FF000) >> 12; if (pte != 0) vmm_PDBR->pageTables[pde] = (dword)mm_palloc(1, PID_KERNEL) | 0x03; //present, read/write, supervisor priviledge for ( ; pde<1024; pde++) //top 10 bits are page directory index { for ( ; pte<1024; pte++) //next 10 bits page table index { if (pte == 0) { if (!(tmp = mm_palloc(1, PID_KERNEL))) { printf("ERROR! Page could not be allocated for PDE %d, PTE %d!\n", pde, pte); halt(); } vmm_PDBR->pageTables[pde] = ((dword)tmp | 0x00000003); //present, read/write, supervisor priviledge } tmp = (PageTable *)((vmm_PDBR->pageTables[pde]) & 0xFFFFF000); //mask out address of page table tmp->page[pte] = (address | 0x00000003); //present, read/write, supervisor priviledge address += 4096; if (address >= framebuffer_end) pte = pde = 2000; } pte = 0; } } } void *malloc(dword bytes) { } int free(void *ptr) { }