summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--kernel/usermode.c32
-rw-r--r--kernel/usermode.h46
2 files changed, 78 insertions, 0 deletions
diff --git a/kernel/usermode.c b/kernel/usermode.c
new file mode 100644
index 0000000..afad9df
--- /dev/null
+++ b/kernel/usermode.c
@@ -0,0 +1,32 @@
+#include "usermode.h"
+
+
+void install_tss(int cpu_no){
+ // now fill each value
+ // set values necessary
+ sys_tss.ss0 = 0x10;
+ // now set the IO bitmap (not necessary, so set above limit)
+ sys_tss.iomap = ( unsigned short ) sizeof( tss_struct );
+}
+
+void switch_to_user_mode() {
+ // Set up a stack structure for switching to user mode.
+ asm volatile(" \
+ cli; \
+ mov $0x23, %ax; \
+ mov %ax, %ds; \
+ mov %ax, %es; \
+ mov %ax, %fs; \
+ mov %ax, %gs; \
+ \
+ mov %esp, %eax; \
+ pushl $0x23; \
+ pushl %eax; \
+ pushf; \
+ mov $0x200, %eax; \
+ push %eax; \
+ pushl $0x1B; \
+ push $1f; \
+ iret; \ 1: \
+ ");
+}
diff --git a/kernel/usermode.h b/kernel/usermode.h
new file mode 100644
index 0000000..e9195e1
--- /dev/null
+++ b/kernel/usermode.h
@@ -0,0 +1,46 @@
+// https://xarnze.com/posts/post/Tutorial:%20Entering%20User%20mode
+// http://wiki.osdev.org/TSS
+// http://wiki.osdev.org/Global_Descriptor_Table
+
+typedef volatile struct strtss{
+ unsigned short link;
+ unsigned short link_h;
+ unsigned long esp0;
+ unsigned short ss0;
+ unsigned short ss0_h;
+ unsigned long esp1;
+ unsigned short ss1;
+ unsigned short ss1_h;
+ unsigned long esp2;
+ unsigned short ss2;
+ unsigned short ss2_h;
+ unsigned long cr3;
+ unsigned long eip;
+ unsigned long eflags;
+ unsigned long eax;
+ unsigned long ecx;
+ unsigned long edx;
+ unsigned long ebx;
+ unsigned long esp;
+ unsigned long ebp;
+ unsigned long esi;
+ unsigned long edi;
+ unsigned short es;
+ unsigned short es_h;
+ unsigned short cs;
+ unsigned short cs_h;
+ unsigned short ss;
+ unsigned short ss_h;
+ unsigned short ds;
+ unsigned short ds_h;
+ unsigned short fs;
+ unsigned short fs_h;
+ unsigned short gs;
+ unsigned short gs_h;
+ unsigned short ldt;
+ unsigned short ldt_h;
+ unsigned short trap;
+ unsigned short iomap;
+}__attribute__((packed)) tss_struct;
+
+tss_struct sys_tss; //Define the TSS as a global structure