summaryrefslogtreecommitdiff
path: root/asm
diff options
context:
space:
mode:
Diffstat (limited to 'asm')
-rw-r--r--asm/GDT.asm3
-rw-r--r--asm/helpers.s43
-rw-r--r--asm/int_irq.asm1
-rw-r--r--asm/multiboot.s47
-rw-r--r--asm/usermode.s35
5 files changed, 85 insertions, 44 deletions
diff --git a/asm/GDT.asm b/asm/GDT.asm
index f271377..444c313 100644
--- a/asm/GDT.asm
+++ b/asm/GDT.asm
@@ -15,6 +15,7 @@
;
global gdt_descriptor
+global gdt_start
gdt_start:
@@ -76,3 +77,5 @@ CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start
CODE16_SEG equ gdt16_code - gdt_start
DATA16_SEG equ gdt16_data - gdt_start
+
+
diff --git a/asm/helpers.s b/asm/helpers.s
new file mode 100644
index 0000000..6e89df6
--- /dev/null
+++ b/asm/helpers.s
@@ -0,0 +1,43 @@
+//http://wiki.osdev.org/GDT_Tutorial
+.global setup_gdt
+
+// call as setup_gdt(GDT,sizeof(GDT))
+setup_gdt:
+
+ // re-fill gdt_descriptor with new GDT location and size
+ movl 4(%esp),%eax
+ movl %eax, gdt_descriptor+2
+
+ movw 8(%esp),%ax
+ movw %ax, gdt_descriptor
+ //
+
+ lgdt gdt_descriptor #load new descriptor table!
+
+ // reload to take effect
+ reloadSegments:
+
+ #Reload CS register containing code selector:
+ jmp $0x08,$reload_CS # 0x08 points at the new code selector
+
+ reload_CS:
+ mov $0x10, %ax #0x10 points at the new data selector
+ mov %ax, %ds
+ mov %ax, %es
+ mov %ax, %fs
+ mov %ax, %gs
+ mov %ax, %ss
+
+ tss_flush:
+
+ movb $0x2B,%ax # Load the index of our TSS structure - The index is
+ # 0x28, as it is the 5th selector and each is 8 bytes
+ # long, but we set the bottom two bits (making 0x2B)
+ # so that it has an RPL of 3, not zero.
+ ltr %ax # Load 0x2B into the task state register.
+
+ ret
+
+
+
+ret
diff --git a/asm/int_irq.asm b/asm/int_irq.asm
index e9864f0..ac8eef8 100644
--- a/asm/int_irq.asm
+++ b/asm/int_irq.asm
@@ -127,7 +127,6 @@ int_irq13:
int_irq14:
cli
- pop eax
call exception_handle_14 ;this will never return due to panic!
jmp $
diff --git a/asm/multiboot.s b/asm/multiboot.s
index 1927c9b..22be3d1 100644
--- a/asm/multiboot.s
+++ b/asm/multiboot.s
@@ -14,6 +14,7 @@
# You don't need to understand all these details as it is just magic values that
# is documented in the multiboot standard. The bootloader will search for this
# magic sequence and recognize us as a multiboot kernel.
+
.section .multiboot
.align 4
.long MAGIC
@@ -49,60 +50,20 @@ stack_top:
.global stack_bottom
.type _start, @function
_start:
- # Welcome to kernel mode! We now have sufficient code for the bootloader to
- # load and run our operating system. It doesn't do anything interesting yet.
- # Perhaps we would like to call printf("Hello, World\n"). You should now
- # realize one of the profound truths about kernel mode: There is nothing
- # there unless you provide it yourself. There is no printf function. There
- # is no <stdio.h> header. If you want a function, you will have to code it
- # yourself. And that is one of the best things about kernel development:
- # you get to make the entire system yourself. You have absolute and complete
- # power over the machine, there are no security restrictions, no safe
- # guards, no debugging mechanisms, there is nothing but what you build.
-
- # By now, you are perhaps tired of assembly language. You realize some
- # things simply cannot be done in C, such as making the multiboot header in
- # the right section and setting up the stack. However, you would like to
- # write the operating system in a higher level language, such as C or C++.
- # To that end, the next task is preparing the processor for execution of
- # such code. C doesn't expect much at this point and we only need to set up
- # a stack. Note that the processor is not fully initialized yet and stuff
- # such as floating point instructions are not available yet.
- lgdt gdt_descriptor #load descriptor table!
# To set up a stack, we simply set the esp register to point to the top of
# our stack (as it grows downwards).
movl $stack_top, %esp
- # We are now ready to actually execute C code. We cannot embed that in an
- # assembly file, so we'll create a kernel.c file in a moment. In that file,
- # we'll create a C entry point called kernel_main and call it here.
-
push %ebx #pass address of the multiboot information data structure
push %eax #pass eax, so kernel can check for magic number
-
-
- reloadSegments:
- #Reload CS register containing code selector:
- jmp $0x08,$reload_CS # 0x08 points at the new code selector
-
- reload_CS:
- mov $0x10, %ax
- mov %ax, %ds
- mov %ax, %es
- mov %ax, %fs
- mov %ax, %gs
- mov %ax, %ss
+
call kernel_main
- # In case the function returns, we'll want to put the computer into an
- # infinite loop. To do that, we use the clear interrupt ('cli') instruction
- # to disable interrupts, the halt instruction ('hlt') to stop the CPU until
- # the next interrupt arrives, and jumping to the halt instruction if it ever
- # continues execution, just to be safe. We will create a local label rather
- # than real symbol and jump to there endlessly.
+ # should never be reached
+
cli
hlt
.Lhang:
diff --git a/asm/usermode.s b/asm/usermode.s
new file mode 100644
index 0000000..acf4b04
--- /dev/null
+++ b/asm/usermode.s
@@ -0,0 +1,35 @@
+.global asm_usermode
+.extern userfunc
+
+# pass address to func to exec (TODO)
+asm_usermode:
+
+ // 0x23 is user data segment (|2 low bits)
+ // 0x1b is user code segment (|2 low bits)
+
+ // set segment registers
+ mov $0x23, %ax
+ mov %ax, %ds
+ mov %ax, %es
+ mov %ax, %fs
+ mov %ax, %gs
+ // ss is handled by iret
+
+ mov %esp, %eax
+
+ pushl $0x23 // user data segment
+ pushl %eax // current stack
+ pushf //
+
+ // http://x86.renejeschke.de/html/file_module_x86_id_145.html
+ //mov $0x200, %eax
+ //push %eax // eflags image
+ pushl $0x1B // return code segment selector
+ push $userfunc // return instruction pointer
+ iret
+
+ jmp . // will never be reached?
+
+
+
+