summaryrefslogtreecommitdiff
path: root/Source/Kernel/MemoryManager
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2009-10-17 18:20:46 +0200
committerAlexis211 <alexis211@gmail.com>2009-10-17 18:20:46 +0200
commitae49f59cc96e4ff3709c857b848132e82e6e0b14 (patch)
tree46c53db51b25c2deea0786d043e53ab09b647ee0 /Source/Kernel/MemoryManager
parent9520a1a101b264abe700c3f7753f5f55bba4c681 (diff)
downloadMelon-ae49f59cc96e4ff3709c857b848132e82e6e0b14.tar.gz
Melon-ae49f59cc96e4ff3709c857b848132e82e6e0b14.zip
User mode code can now run !
We have a sample process (hardcoded in ASM in Task.wtf.asm) that displays a's and sleeps 20ms, but it's on a hidden virtual terminal so it's useless :D
Diffstat (limited to 'Source/Kernel/MemoryManager')
-rw-r--r--Source/Kernel/MemoryManager/GDT.ns.cpp22
-rw-r--r--Source/Kernel/MemoryManager/GDT.ns.h32
-rw-r--r--Source/Kernel/MemoryManager/GDT.wtf.asm6
-rw-r--r--Source/Kernel/MemoryManager/PhysMem.ns.cpp1
4 files changed, 60 insertions, 1 deletions
diff --git a/Source/Kernel/MemoryManager/GDT.ns.cpp b/Source/Kernel/MemoryManager/GDT.ns.cpp
index 0bb606d..f1f5c94 100644
--- a/Source/Kernel/MemoryManager/GDT.ns.cpp
+++ b/Source/Kernel/MemoryManager/GDT.ns.cpp
@@ -1,6 +1,9 @@
#include "GDT.ns.h"
extern "C" void gdt_flush(u32int);
+extern "C" void tss_flush();
+
+using namespace CMem; //For memset
#define GDT_ENTRIES 6
@@ -8,6 +11,7 @@ namespace GDT {
gdt_entry_t gdt_entries[GDT_ENTRIES];
gdt_ptr_t gdt_ptr;
+tss_entry_t tss_entry;
void setGate(s32int num, u32int base, u32int limit, u8int access, u8int gran) {
gdt_entries[num].base_low = (base & 0xFFFF);
@@ -20,6 +24,22 @@ void setGate(s32int num, u32int base, u32int limit, u8int access, u8int gran) {
gdt_entries[num].access = access;
}
+void writeTSS(s32int num, u16int ss0, u32int esp0) {
+ u32int base = (u32int)&tss_entry;
+ u32int limit = base + sizeof(tss_entry);
+
+ setGate(num, base, limit, 0xE9, 0x00);
+
+ memset((u8int*)&tss_entry, 0, sizeof(tss_entry_t));
+
+ tss_entry.ss0 = ss0;
+ tss_entry.esp0 = esp0;
+
+ tss_entry.cs = 0x0B; //0x0B = Kernel code segment + RPL=3 (meaning it is supposed to be called from user mode)
+ //0x13 = Kernel data segment + RPL=3 (meaning to be called from ring3)
+ tss_entry.ss = tss_entry.ds = tss_entry.es = tss_entry.fs = tss_entry.gs = 0x13;
+}
+
void init() {
gdt_ptr.limit = (sizeof(gdt_entry_t) * GDT_ENTRIES) - 1;
gdt_ptr.base = (u32int)&gdt_entries;
@@ -29,8 +49,10 @@ void init() {
setGate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); //Kernel data segment
setGate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); //User code segment
setGate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); //User data segment
+ writeTSS(5, 0x10, 0x0);
gdt_flush((u32int)&gdt_ptr);
+ tss_flush();
}
}
diff --git a/Source/Kernel/MemoryManager/GDT.ns.h b/Source/Kernel/MemoryManager/GDT.ns.h
index 7a9f95f..94306a4 100644
--- a/Source/Kernel/MemoryManager/GDT.ns.h
+++ b/Source/Kernel/MemoryManager/GDT.ns.h
@@ -13,12 +13,44 @@ namespace GDT {
u8int base_high;
} __attribute__((packed));
+ // A struct describing a Task State Segment.
+ struct tss_entry_t {
+ u32int prev_tss; // The previous TSS - if we used hardware task switching this would form a linked list.
+ u32int esp0; // The stack pointer to load when we change to kernel mode.
+ u32int ss0; // The stack segment to load when we change to kernel mode.
+ u32int esp1; // Unused...
+ u32int ss1;
+ u32int esp2;
+ u32int ss2;
+ u32int cr3;
+ u32int eip;
+ u32int eflags;
+ u32int eax;
+ u32int ecx;
+ u32int edx;
+ u32int ebx;
+ u32int esp;
+ u32int ebp;
+ u32int esi;
+ u32int edi;
+ u32int es; // The value to load into ES when we change to kernel mode.
+ u32int cs; // The value to load into CS when we change to kernel mode.
+ u32int ss; // The value to load into SS when we change to kernel mode.
+ u32int ds; // The value to load into DS when we change to kernel mode.
+ u32int fs; // The value to load into FS when we change to kernel mode.
+ u32int gs; // The value to load into GS when we change to kernel mode.
+ u32int ldt; // Unused...
+ u16int trap;
+ u16int iomap_base;
+ } __attribute__((packed));
+
struct gdt_ptr_t {
u16int limit;
u32int base;
} __attribute__((packed));
void init();
+ extern tss_entry_t tss_entry; //Used for setting kernel stack
}
#endif
diff --git a/Source/Kernel/MemoryManager/GDT.wtf.asm b/Source/Kernel/MemoryManager/GDT.wtf.asm
index eb216ed..beb668d 100644
--- a/Source/Kernel/MemoryManager/GDT.wtf.asm
+++ b/Source/Kernel/MemoryManager/GDT.wtf.asm
@@ -15,3 +15,9 @@ gdt_flush:
.flush:
ret
+
+[GLOBAL tss_flush]
+tss_flush:
+ mov ax, 0x2B ;entry 5 = 0x28, with RPL=3
+ ltr ax
+ ret
diff --git a/Source/Kernel/MemoryManager/PhysMem.ns.cpp b/Source/Kernel/MemoryManager/PhysMem.ns.cpp
index 382e8a4..eb6fbf1 100644
--- a/Source/Kernel/MemoryManager/PhysMem.ns.cpp
+++ b/Source/Kernel/MemoryManager/PhysMem.ns.cpp
@@ -28,7 +28,6 @@ void initPaging(u32int mem_size) {
kernelPageDirectory->tables[i] = kernelPageDirectory->tables[768 + i];
}
DEBUG_HEX((u32int)kernelPageDirectory->physicalAddr); DEBUG(" is page dir phys addr.");
- //asm volatile("hlt");
kernelPageDirectory->switchTo();
DEBUG("Paging enabled !");