summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2009-12-22 16:48:52 +0100
committerAlexis211 <alexis211@gmail.com>2009-12-22 16:48:52 +0100
commitbfec696da052c8c9320773ca69927422efebe8c7 (patch)
tree325efd88744344cd732d6e3d09bfebf3cb5847a4
parent2c23da1cf398aef420130dde1b86e4c474e3cacb (diff)
downloadMelon-bfec696da052c8c9320773ca69927422efebe8c7.tar.gz
Melon-bfec696da052c8c9320773ca69927422efebe8c7.zip
[noncompiling commit] Started working on a segmented memory model
-rw-r--r--Source/Kernel/Makefile2
-rw-r--r--Source/Kernel/MemoryManager/KernelSegment.class.cpp51
-rw-r--r--Source/Kernel/MemoryManager/KernelSegment.class.h25
-rw-r--r--Source/Kernel/MemoryManager/Mem.ns.cpp4
-rw-r--r--Source/Kernel/MemoryManager/PageAlloc.ns.cpp9
-rw-r--r--Source/Kernel/MemoryManager/PageDirectory.class.cpp65
-rw-r--r--Source/Kernel/MemoryManager/PageDirectory.class.h17
-rw-r--r--Source/Kernel/MemoryManager/PhysMem.ns.cpp37
-rw-r--r--Source/Kernel/MemoryManager/PhysMem.ns.h8
-rw-r--r--Source/Kernel/MemoryManager/Segment.proto.h24
-rw-r--r--Source/Kernel/MemoryManager/V86Segment.class.cpp16
-rw-r--r--Source/Kernel/MemoryManager/V86Segment.class.h12
-rw-r--r--Source/Kernel/TaskManager/V86/V86.ns.cpp7
-rw-r--r--Source/Kernel/TaskManager/V86/V86.ns.h1
14 files changed, 203 insertions, 75 deletions
diff --git a/Source/Kernel/Makefile b/Source/Kernel/Makefile
index 6a44ecf..233b23b 100644
--- a/Source/Kernel/Makefile
+++ b/Source/Kernel/Makefile
@@ -21,6 +21,8 @@ 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
new file mode 100644
index 0000000..154ae46
--- /dev/null
+++ b/Source/Kernel/MemoryManager/KernelSegment.class.cpp
@@ -0,0 +1,51 @@
+#include "KernelSegment.class.h"
+#include <MemoryManager/PageAlloc.ns.h>
+#include <MemoryManager/PhysMem.ns.h>
+#include <MemoryManager/PageDirectory.class.h>
+
+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
new file mode 100644
index 0000000..4d8205a
--- /dev/null
+++ b/Source/Kernel/MemoryManager/KernelSegment.class.h
@@ -0,0 +1,25 @@
+#ifndef DEF_KERNEL_SEGMENT_CLASS_H
+#define DEF_KERNEL_SEGMENT_CLASS_H
+
+#include <MemoryManager/Segment.proto.h>
+#include <MemoryManager/PageDirectory.class.h>
+
+class KernelSegment : public Segment {
+ private:
+ Vector<PageDirectory*> 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 a0809b0..712c0f3 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) kernelPageDirectory->allocFrame(i, false, false);
+ if (pagingEnabled) PhysMem::keSeg.allocFrame(i);
}
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) {
- kernelPageDirectory->allocFrame(i, false, false);
+ PhysMem::keSeg.allocFrame(i);
}
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 d8ede2a..1b9a322 100644
--- a/Source/Kernel/MemoryManager/PageAlloc.ns.cpp
+++ b/Source/Kernel/MemoryManager/PageAlloc.ns.cpp
@@ -29,13 +29,10 @@ void* alloc(u32int* phys) {
next = Mem::alloc(0x1000, true);
} else {
u32int i = 0xFFFFF000;
- page_t *p;
while (1) {
- p = kernelPageDirectory->getPage(i, true);
- if (p->frame == 0) break;
+ if (PhysMem::keSeg.allocFrame(i)) break;
i -= 0x1000;
}
- PhysMem::allocFrame(p, true, false);
next = (void*)i;
}
freePage[freec] = next;
@@ -52,7 +49,7 @@ void* alloc(u32int* phys) {
if (p == 0) { //THIS SHOULD NEVER HAPPEN
PANIC("Cached free page does not exist.");
} else if (p->frame == 0) {
- PhysMem::allocFrame(p, true, false);
+ PageDirectory::map(p, PhysMem::getFrame(), true, false);
} else {
*phys = (p->frame * 0x1000);
}
@@ -61,7 +58,7 @@ void* alloc(u32int* phys) {
}
void free(void *ptr) {
- kernelPageDirectory->freeFrame((u32int)ptr);
+ PhysMem::keSeg.freeFrame((u32int)ptr);
return;
}
diff --git a/Source/Kernel/MemoryManager/PageDirectory.class.cpp b/Source/Kernel/MemoryManager/PageDirectory.class.cpp
index 064b423..575b14c 100644
--- a/Source/Kernel/MemoryManager/PageDirectory.class.cpp
+++ b/Source/Kernel/MemoryManager/PageDirectory.class.cpp
@@ -5,6 +5,23 @@
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++) {
@@ -24,7 +41,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.
- PhysMem::allocFrame(&tables[i]->pages[j], true, true);
+ map(&tables[i]->pages[j], PhysMem::getFrame(), 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;
@@ -44,58 +61,48 @@ 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 e93524b..c3403aa 100644
--- a/Source/Kernel/MemoryManager/PageDirectory.class.h
+++ b/Source/Kernel/MemoryManager/PageDirectory.class.h
@@ -2,6 +2,8 @@
#define DEF_PAGEDIRECTORY_CLASS_H
#include <common.h>
+#include <Vector.class.h>
+#include <MemoryManager/Segment.proto.h>
struct page_t {
u32int present : 1;
@@ -22,14 +24,21 @@ struct PageDirectory {
u32int *tablesPhysical;
u32int physicalAddr;
+ Vector<seg_map_t> 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();
- page_t *getPage(u32int address, bool make);
- 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();
+ page_t *getPage(u32int address, bool make);
+
+ //For mapping memory segments to the page directory
+ void map(Segment* seg);
+ void unmap(Segment* seg);
private:
PageDirectory(const PageDirectory& other);
diff --git a/Source/Kernel/MemoryManager/PhysMem.ns.cpp b/Source/Kernel/MemoryManager/PhysMem.ns.cpp
index 54eb4de..d3ce0b9 100644
--- a/Source/Kernel/MemoryManager/PhysMem.ns.cpp
+++ b/Source/Kernel/MemoryManager/PhysMem.ns.cpp
@@ -9,17 +9,21 @@ 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) {
- page_t *p2 = kernelPageDirectory->getPage(i, true);
- allocFrame(p2, false, false);
+ keSeg.identityMap(i);
i += 0x1000;
}
//Also map thoses pages at begning of virtual memory
@@ -42,30 +46,15 @@ void removeTemporaryPages() {
}
}
-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;
- }
+u32int getFrame() {
+ u32int idx = frames->firstFreeBit();
+ if (idx == (u32int) - 1) PANIC("No more free frames !");
+ frames->setBit(idx);
+ return idx;
}
-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;
- }
+void freeFrame(u32int frame) {
+ frames->clearBit(frame);
}
u32int free() {
diff --git a/Source/Kernel/MemoryManager/PhysMem.ns.h b/Source/Kernel/MemoryManager/PhysMem.ns.h
index 258591a..3e6ec55 100644
--- a/Source/Kernel/MemoryManager/PhysMem.ns.h
+++ b/Source/Kernel/MemoryManager/PhysMem.ns.h
@@ -3,17 +3,21 @@
#include <MemoryManager/PageDirectory.class.h>
+#include <MemoryManager/KernelSegment.class.h>
+
extern PageDirectory* kernelPageDirectory;
namespace PhysMem {
void initPaging(u32int mem_size);
void removeTemporaryPages();
- void allocFrame(page_t *page, bool is_user, bool is_writable);
- void freeFrame(page_t *page);
+ u32int getFrame();
+ void freeFrame(u32int frame);
u32int free();
u32int total();
+
+ extern KernelSegment keSeg;
}
#endif
diff --git a/Source/Kernel/MemoryManager/Segment.proto.h b/Source/Kernel/MemoryManager/Segment.proto.h
new file mode 100644
index 0000000..780cd10
--- /dev/null
+++ b/Source/Kernel/MemoryManager/Segment.proto.h
@@ -0,0 +1,24 @@
+#ifndef DEF_SEGMENT_PROTO_H
+#define DEF_SEGMENT_PROTO_H
+
+#include <common.h>
+
+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
new file mode 100644
index 0000000..e82a94c
--- /dev/null
+++ b/Source/Kernel/MemoryManager/V86Segment.class.cpp
@@ -0,0 +1,16 @@
+#include "V86Segment.class.h"
+#include <MemoryManager/PageDirectory.class.h>
+
+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
new file mode 100644
index 0000000..665506d
--- /dev/null
+++ b/Source/Kernel/MemoryManager/V86Segment.class.h
@@ -0,0 +1,12 @@
+#ifndef DEF_V86SEGMENT_CLASS_H
+#define DEF_V86SEGMENT_CLASS_H
+
+#include <MemoryManager/Segment.proto.h>
+
+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 f70728c..f6cc641 100644
--- a/Source/Kernel/TaskManager/V86/V86.ns.cpp
+++ b/Source/Kernel/TaskManager/V86/V86.ns.cpp
@@ -19,13 +19,6 @@ void biosInt(u8int int_no, v86_regs_t &regs) {
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 ec8e928..d30c08e 100644
--- a/Source/Kernel/TaskManager/V86/V86.ns.h
+++ b/Source/Kernel/TaskManager/V86/V86.ns.h
@@ -26,7 +26,6 @@ namespace V86 {
void run(v86_function_t& entry, v86_regs_t &regs);
void biosInt(u8int int_no, v86_regs_t &regs);
- 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);
}