From cc1193087067c7e1105bd3d788520c034f5cf619 Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Tue, 22 Dec 2009 16:49:53 +0100 Subject: Revert "[noncompiling commit] Started working on a segmented memory model" This reverts commit bfec696da052c8c9320773ca69927422efebe8c7. --- Source/Kernel/Makefile | 2 - .../Kernel/MemoryManager/KernelSegment.class.cpp | 51 ----------------- Source/Kernel/MemoryManager/KernelSegment.class.h | 25 --------- Source/Kernel/MemoryManager/Mem.ns.cpp | 4 +- Source/Kernel/MemoryManager/PageAlloc.ns.cpp | 9 ++- .../Kernel/MemoryManager/PageDirectory.class.cpp | 65 ++++++++++------------ Source/Kernel/MemoryManager/PageDirectory.class.h | 17 ++---- Source/Kernel/MemoryManager/PhysMem.ns.cpp | 37 +++++++----- Source/Kernel/MemoryManager/PhysMem.ns.h | 8 +-- Source/Kernel/MemoryManager/Segment.proto.h | 24 -------- Source/Kernel/MemoryManager/V86Segment.class.cpp | 16 ------ Source/Kernel/MemoryManager/V86Segment.class.h | 12 ---- Source/Kernel/TaskManager/V86/V86.ns.cpp | 7 +++ Source/Kernel/TaskManager/V86/V86.ns.h | 1 + 14 files changed, 75 insertions(+), 203 deletions(-) delete mode 100644 Source/Kernel/MemoryManager/KernelSegment.class.cpp delete mode 100644 Source/Kernel/MemoryManager/KernelSegment.class.h delete mode 100644 Source/Kernel/MemoryManager/Segment.proto.h delete mode 100644 Source/Kernel/MemoryManager/V86Segment.class.cpp delete mode 100644 Source/Kernel/MemoryManager/V86Segment.class.h (limited to 'Source') diff --git a/Source/Kernel/Makefile b/Source/Kernel/Makefile index 233b23b..6a44ecf 100644 --- a/Source/Kernel/Makefile +++ b/Source/Kernel/Makefile @@ -21,8 +21,6 @@ Objects = Core/loader.wtf.o \ MemoryManager/GDT.ns.o \ MemoryManager/PageDirectory.class.o \ MemoryManager/PageAlloc.ns.o \ - MemoryManager/KernelSegment.class.o \ - MemoryManager/V86Segment.class.o \ DeviceManager/Disp.ns.o \ DeviceManager/Dev.ns.o \ DeviceManager/Time.ns.o \ diff --git a/Source/Kernel/MemoryManager/KernelSegment.class.cpp b/Source/Kernel/MemoryManager/KernelSegment.class.cpp deleted file mode 100644 index 154ae46..0000000 --- a/Source/Kernel/MemoryManager/KernelSegment.class.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "KernelSegment.class.h" -#include -#include -#include - -seg_map_t KernelSegment::map(PageDirectory* pd) { - for (u32int i = 0; i < 256; i++) { - pd->tables[i + 768] = tables[i]; - pd->tablesPhysical[i + 768] = tablesPhysical[i]; - } - m_pds.push(pd); - seg_map_t ret = {this, 0xC0000000, 0x40000000}; - return ret; -} - -page_t* KernelSegment::getPage(u32int addr) { - if (addr < 0xC0000000) return 0; - addr /= 0x1000; - u32int tableIdx = addr / 1024; - if (tables[tableIdx - 768] == 0) { - tables[tableIdx - 768] = (page_table_t*)PageAlloc::alloc(tablesPhysical + tableIdx - 768); - CMem::memset((u8int*)tables[tableIdx - 768], 0, 0x1000); - for (u32int i = 0; i < m_pds.size(); i++) { - m_pds[i]->tables[tableIdx] = tables[tableIdx - 768]; - m_pds[i]->tablesPhysical[tableIdx] = tablesPhysical[tableIdx - 768]; - } - } - return &tables[tableIdx - 768]->pages[addr % 1024]; -} - -bool KernelSegment::allocFrame(u32int addr) { - page_t *p = getPage(addr); - if (p == 0) return false; - if (p->frame != 0) return false; - PageDirectory::map(p, PhysMem::getFrame(), false, false); - return true; -} - -void KernelSegment::identityMap(u32int addr) { - page_t *p = getPage(addr); - if (p == 0) return; - PageDirectory::map(p, (addr - 0xC0000000) / 0x1000, false, false); -} - -void KernelSegment::freeFrame(u32int addr) { - if (addr < 0xC0000000) return; - addr /= 0x1000; - u32int tableIdx = addr / 1024; - if (tables[tableIdx - 768] == 0) return; - PageDirectory::unmap(&tables[tableIdx - 768]->pages[addr % 1024], true); -} diff --git a/Source/Kernel/MemoryManager/KernelSegment.class.h b/Source/Kernel/MemoryManager/KernelSegment.class.h deleted file mode 100644 index 4d8205a..0000000 --- a/Source/Kernel/MemoryManager/KernelSegment.class.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef DEF_KERNEL_SEGMENT_CLASS_H -#define DEF_KERNEL_SEGMENT_CLASS_H - -#include -#include - -class KernelSegment : public Segment { - private: - Vector m_pds; - page_table_t *tables[256]; - u32int tablesPhysical[256]; - - page_t* getPage(u32int addr); - - public: - seg_map_t map(PageDirectory *pd); - void unmap(PageDirectory *pd, seg_map_t *mapping) {} - bool handleFault(u32int addr, seg_map_t *mapping) { return false; } - - bool allocFrame(u32int addr); //false when page already was mapped to a frame - void identityMap(u32int addr); //Used only on paging initialisation - void freeFrame(u32int addr); -}; - -#endif diff --git a/Source/Kernel/MemoryManager/Mem.ns.cpp b/Source/Kernel/MemoryManager/Mem.ns.cpp index 712c0f3..a0809b0 100644 --- a/Source/Kernel/MemoryManager/Mem.ns.cpp +++ b/Source/Kernel/MemoryManager/Mem.ns.cpp @@ -20,7 +20,7 @@ void *kallocInternal(size_t sz, bool align) { u32int temp = placementAddress; placementAddress += sz; for (u32int i = temp; i < placementAddress; i += 0x1000) { - if (pagingEnabled) PhysMem::keSeg.allocFrame(i); + if (pagingEnabled) kernelPageDirectory->allocFrame(i, false, false); } return (void*)temp; } @@ -34,7 +34,7 @@ void createHeap() { u32int heapSize = HEAP_MIN_SIZE + heapIndexSize; //Calculate heap size for (u32int i = (placementAddress & 0xFFFFF000); i < heapStart; i += 0x1000) { - PhysMem::keSeg.allocFrame(i); + kernelPageDirectory->allocFrame(i, false, false); } kheap.create(heapStart, heapSize, heapIndexSize, kernelPageDirectory, false, false); diff --git a/Source/Kernel/MemoryManager/PageAlloc.ns.cpp b/Source/Kernel/MemoryManager/PageAlloc.ns.cpp index 1b9a322..d8ede2a 100644 --- a/Source/Kernel/MemoryManager/PageAlloc.ns.cpp +++ b/Source/Kernel/MemoryManager/PageAlloc.ns.cpp @@ -29,10 +29,13 @@ void* alloc(u32int* phys) { next = Mem::alloc(0x1000, true); } else { u32int i = 0xFFFFF000; + page_t *p; while (1) { - if (PhysMem::keSeg.allocFrame(i)) break; + p = kernelPageDirectory->getPage(i, true); + if (p->frame == 0) break; i -= 0x1000; } + PhysMem::allocFrame(p, true, false); next = (void*)i; } freePage[freec] = next; @@ -49,7 +52,7 @@ void* alloc(u32int* phys) { if (p == 0) { //THIS SHOULD NEVER HAPPEN PANIC("Cached free page does not exist."); } else if (p->frame == 0) { - PageDirectory::map(p, PhysMem::getFrame(), true, false); + PhysMem::allocFrame(p, true, false); } else { *phys = (p->frame * 0x1000); } @@ -58,7 +61,7 @@ void* alloc(u32int* phys) { } void free(void *ptr) { - PhysMem::keSeg.freeFrame((u32int)ptr); + kernelPageDirectory->freeFrame((u32int)ptr); return; } diff --git a/Source/Kernel/MemoryManager/PageDirectory.class.cpp b/Source/Kernel/MemoryManager/PageDirectory.class.cpp index 575b14c..064b423 100644 --- a/Source/Kernel/MemoryManager/PageDirectory.class.cpp +++ b/Source/Kernel/MemoryManager/PageDirectory.class.cpp @@ -5,23 +5,6 @@ extern "C" void copy_page_physical(u32int src, u32int dest); -/* FOR MAPPING FRAMES TO PAGES */ -void PageDirectory::map(page_t* p, u32int frame, bool is_user, bool is_writable) { - if (p == 0) return; - p->present = 1; - p->user = (is_user ? 1 : 0); - p->rw = (is_writable ? 1 : 0); - p->frame = (frame); -} - -void PageDirectory::unmap(page_t *p, bool freePhys) { - if (p == 0) return; - if (freePhys) PhysMem::freeFrame(p->frame); - p->present = 0; - p->frame = 0; -} - -/* FOR PAGE DIRECTORES */ PageDirectory::PageDirectory() { tablesPhysical = (u32int*)PageAlloc::alloc(&physicalAddr); for (u32int i = 0; i < 1024; i++) { @@ -41,7 +24,7 @@ PageDirectory::PageDirectory(PageDirectory* other) { if (!(other->tables[i]->pages[j].frame)) continue; if (i == 0 and j < 256) continue; //Frame is below 1M, probably used by some V86 stuff. Ignore it. - map(&tables[i]->pages[j], PhysMem::getFrame(), true, true); + PhysMem::allocFrame(&tables[i]->pages[j], true, true); tables[i]->pages[j].present = other->tables[i]->pages[j].present; tables[i]->pages[j].rw = other->tables[i]->pages[j].rw; tables[i]->pages[j].user = other->tables[i]->pages[j].user; @@ -61,48 +44,58 @@ PageDirectory::PageDirectory(PageDirectory* other) { } PageDirectory::~PageDirectory() { - for (u32int i = 0; i < mappedSegs.size(); i++) { - mappedSegs[i].seg->unmap(this, &mappedSegs[i]); - } for (int i = 0; i < 768; i++) { //Only free addresses below 0xC0000000, upper is kernel space if (tables[i] != 0) { + for (int j = 0; j < 1024; j++) { + if (i == 0 and j < 256) continue; //Frame is below 1M, probably used by some V86 stuff. Ignore it. + PhysMem::freeFrame(&(tables[i]->pages[j])); + } PageAlloc::free((void*)tables[i]); } } PageAlloc::free((void*)tablesPhysical); } -void PageDirectory::map(Segment* seg) { - mappedSegs.push(seg->map(this)); -} - -void PageDirectory::unmap(Segment* seg) { - for (u32int i = 0; i < mappedSegs.size(); i++) { - if (mappedSegs[i].seg == seg) { - seg->unmap(this, &mappedSegs[i]); - mappedSegs[i] = mappedSegs.back(); - return; - } - } -} - page_t *PageDirectory::getPage(u32int address, bool make) { address /= 0x1000; u32int tableIdx = address / 1024; if (tables[tableIdx] != 0) { return &(tables[tableIdx]->pages[address % 1024]); } else if (make) { - if (address >= 0xC0000000) return 0; u32int tmp; tables[tableIdx] = (page_table_t*)PageAlloc::alloc(&tmp); CMem::memset((u8int*)tables[tableIdx], 0, 0x1000); tablesPhysical[tableIdx] = tmp | 0x07; + if (tableIdx >= 768) + Task::allocKernelPageTable(tableIdx, tables[tableIdx], tablesPhysical[tableIdx]); return &(tables[tableIdx]->pages[address % 1024]); } else { return 0; } } +void PageDirectory::map(page_t* p, u32int frame, bool is_user, bool is_writable) { + p->present = 1; + p->user = (is_user ? 1 : 0); + p->rw = (is_writable ? 1 : 0); + p->frame = (frame); +} + +void PageDirectory::allocFrame(u32int address, bool is_user, bool is_writable) { + page_t *p = getPage(address, true); + if (address < 0x100000) { + map(p, address / 0x1000, is_user, is_writable); + } else { + if (p != 0) PhysMem::allocFrame(p, is_user, is_writable); + } +} + +void PageDirectory::freeFrame(u32int address) { + page_t *p = getPage(address, false); + if (p == 0) return; + PhysMem::freeFrame(p); +} + void PageDirectory::switchTo() { asm volatile("mov %0, %%cr3" :: "r"(physicalAddr)); u32int cr0; diff --git a/Source/Kernel/MemoryManager/PageDirectory.class.h b/Source/Kernel/MemoryManager/PageDirectory.class.h index c3403aa..e93524b 100644 --- a/Source/Kernel/MemoryManager/PageDirectory.class.h +++ b/Source/Kernel/MemoryManager/PageDirectory.class.h @@ -2,8 +2,6 @@ #define DEF_PAGEDIRECTORY_CLASS_H #include -#include -#include struct page_t { u32int present : 1; @@ -24,21 +22,14 @@ struct PageDirectory { u32int *tablesPhysical; u32int physicalAddr; - Vector mappedSegs; - - //For mapping a page to a frame - static void map(page_t *p, u32int frame, bool is_user, bool is_writable); - static void unmap(page_t *p, bool physFree = false); - PageDirectory(); PageDirectory(PageDirectory* other); //Clones the other pagedir ~PageDirectory(); - void switchTo(); page_t *getPage(u32int address, bool make); - - //For mapping memory segments to the page directory - void map(Segment* seg); - void unmap(Segment* seg); + void map(page_t *p, u32int frame, bool is_user, bool is_writable); + void allocFrame(u32int address, bool is_user, bool is_writable); + void freeFrame(u32int address); + void switchTo(); private: PageDirectory(const PageDirectory& other); diff --git a/Source/Kernel/MemoryManager/PhysMem.ns.cpp b/Source/Kernel/MemoryManager/PhysMem.ns.cpp index d3ce0b9..54eb4de 100644 --- a/Source/Kernel/MemoryManager/PhysMem.ns.cpp +++ b/Source/Kernel/MemoryManager/PhysMem.ns.cpp @@ -9,21 +9,17 @@ namespace PhysMem { u32int nframes; Bitset *frames; -KernelSegment keSeg; - void initPaging(u32int mem_size) { nframes = mem_size / 0x1000; frames = new Bitset(nframes); - for (u32int i = 0; i < Mem::placementAddress / 0x1000; i++) frames->setBit(i); - kernelPageDirectory = new (Mem::alloc(sizeof(PageDirectory), true)) PageDirectory(); - kernelPageDirectory->map(&keSeg); u32int i = 0xC0000000; while (i < Mem::placementAddress) { - keSeg.identityMap(i); + page_t *p2 = kernelPageDirectory->getPage(i, true); + allocFrame(p2, false, false); i += 0x1000; } //Also map thoses pages at begning of virtual memory @@ -46,15 +42,30 @@ void removeTemporaryPages() { } } -u32int getFrame() { - u32int idx = frames->firstFreeBit(); - if (idx == (u32int) - 1) PANIC("No more free frames !"); - frames->setBit(idx); - return idx; +void allocFrame(page_t *page, bool is_user, bool is_writable) { + if (page->frame != 0) { + return; + } else { + u32int idx = frames->firstFreeBit(); + if (idx == (u32int) - 1) PANIC("No more free frames !"); + frames->setBit(idx); + page->present = 1; + page->user = (is_user ? 1 : 0); + page->rw = (is_writable ? 1 : 0); + page->frame = idx; + } } -void freeFrame(u32int frame) { - frames->clearBit(frame); +void freeFrame(page_t *page) { + if (page->frame == 0) { + return; + } else { + if (page->frame >= 0x100) { //First 1M are reserved (system) + frames->clearBit(page->frame); + } + page->present = 0; + page->frame = 0; + } } u32int free() { diff --git a/Source/Kernel/MemoryManager/PhysMem.ns.h b/Source/Kernel/MemoryManager/PhysMem.ns.h index 3e6ec55..258591a 100644 --- a/Source/Kernel/MemoryManager/PhysMem.ns.h +++ b/Source/Kernel/MemoryManager/PhysMem.ns.h @@ -3,21 +3,17 @@ #include -#include - extern PageDirectory* kernelPageDirectory; namespace PhysMem { void initPaging(u32int mem_size); void removeTemporaryPages(); - u32int getFrame(); - void freeFrame(u32int frame); + void allocFrame(page_t *page, bool is_user, bool is_writable); + void freeFrame(page_t *page); u32int free(); u32int total(); - - extern KernelSegment keSeg; } #endif diff --git a/Source/Kernel/MemoryManager/Segment.proto.h b/Source/Kernel/MemoryManager/Segment.proto.h deleted file mode 100644 index 780cd10..0000000 --- a/Source/Kernel/MemoryManager/Segment.proto.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef DEF_SEGMENT_PROTO_H -#define DEF_SEGMENT_PROTO_H - -#include - -class PageDirectory; -class Segment; - -struct seg_map_t { - Segment* seg; - u32int start; - u32int len; -}; - -class Segment { - public: - virtual seg_map_t map(PageDirectory *pd) = 0; - virtual void unmap(PageDirectory *pd, seg_map_t *mapping) = 0; - virtual bool handleFault(u32int addr, seg_map_t *mapping) = 0; //true = ok, false = app segfaulted - - virtual Segment* clone() { return this; } -}; - -#endif diff --git a/Source/Kernel/MemoryManager/V86Segment.class.cpp b/Source/Kernel/MemoryManager/V86Segment.class.cpp deleted file mode 100644 index e82a94c..0000000 --- a/Source/Kernel/MemoryManager/V86Segment.class.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "V86Segment.class.h" -#include - -seg_map_t V86Segment::map(PageDirectory *pd) { - for (u32int i = 0; i < 0x100000; i += 0x1000) { - PageDirectory::map(pd->getPage(i, true), i / 0x1000, true, true); - } - seg_map_t map = {this, 0, 0x100000}; - return map; -} - -void V86Segment::unmap(PageDirectory* pd, seg_map_t *mapping) { - for (u32int i = 0; i < 0x100000; i += 0x1000) { - PageDirectory::unmap(pd->getPage(i, false)); - } -} diff --git a/Source/Kernel/MemoryManager/V86Segment.class.h b/Source/Kernel/MemoryManager/V86Segment.class.h deleted file mode 100644 index 665506d..0000000 --- a/Source/Kernel/MemoryManager/V86Segment.class.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef DEF_V86SEGMENT_CLASS_H -#define DEF_V86SEGMENT_CLASS_H - -#include - -class V86Segment : public Segment { - seg_map_t map(PageDirectory *pd); - void unmap(PageDirectory* pd, seg_map_t *mapping); - bool handleFault(u32int addr, seg_map_t *mapping) { return false; } -}; - -#endif diff --git a/Source/Kernel/TaskManager/V86/V86.ns.cpp b/Source/Kernel/TaskManager/V86/V86.ns.cpp index f6cc641..f70728c 100644 --- a/Source/Kernel/TaskManager/V86/V86.ns.cpp +++ b/Source/Kernel/TaskManager/V86/V86.ns.cpp @@ -19,6 +19,13 @@ void biosInt(u8int int_no, v86_regs_t ®s) { while (!ret.finished) Task::currThread()->sleep(10); } +void map(Process* p) { + if (p == 0) p = Task::currProcess(); + for (u32int i = 0x00000; i < 0xFFFFF; i += 0x1000) { + p->getPagedir()->allocFrame(i, true, true); + } +} + u16int allocSeg(u16int length) { if (length & 0xF) length = (length & 0xFFFF0) + 0x10; u16int segments = length / 16; diff --git a/Source/Kernel/TaskManager/V86/V86.ns.h b/Source/Kernel/TaskManager/V86/V86.ns.h index d30c08e..ec8e928 100644 --- a/Source/Kernel/TaskManager/V86/V86.ns.h +++ b/Source/Kernel/TaskManager/V86/V86.ns.h @@ -26,6 +26,7 @@ namespace V86 { void run(v86_function_t& entry, v86_regs_t ®s); void biosInt(u8int int_no, v86_regs_t ®s); + void map(Process* p = 0); //Maps lower 1MB of virtual memory to physical memory (lower 1MB as well) u16int allocSeg(u16int size); void* alloc(u16int size); } -- cgit v1.2.3