summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksa Vučković <aleksav013@gmail.com>2021-10-09 02:34:21 +0200
committerAleksa Vučković <aleksav013@gmail.com>2021-10-09 02:34:21 +0200
commit3a9ccbd8e762477f75d8b164a1d99383a01414ae (patch)
tree8088a02f8013bf5b73e078ac421a1cf6b93c4072
parent1dd9c366b885725a3081726b05732a2b81a8e6c7 (diff)
Adding gdt
-rw-r--r--Makefile2
-rw-r--r--src/boot.s22
-rw-r--r--src/gdt.c49
-rw-r--r--src/kernel.c6
4 files changed, 70 insertions, 9 deletions
diff --git a/Makefile b/Makefile
index 86585c5..b427918 100644
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,7 @@ ISO_DIR=isodir
TARGET=myos
-OBJ_FILES=boot.o kernel.o
+OBJ_FILES=boot.o kernel.o gdt.o
CRTBEGIN_OBJ=$(shell $(CC) -print-file-name=crtbegin.o)
CRTEND_OBJ=$(shell $(CC) -print-file-name=crtend.o)
OBJ=$(BUILD_DIR)/crti.o $(CRTBEGIN_OBJ) $(patsubst %,$(BUILD_DIR)/%,$(OBJ_FILES)) $(CRTEND_OBJ) $(BUILD_DIR)/crtn.o
diff --git a/src/boot.s b/src/boot.s
index 385ee70..a0f0c2e 100644
--- a/src/boot.s
+++ b/src/boot.s
@@ -9,7 +9,15 @@
.long MAGIC
.long FLAGS
.long CHECKSUM
-
+
+.global _start
+.global load_gdt
+
+load_gdt:
+ movl 4(%esp), %edx
+ lgdt (%edx)
+ ret
+
.section .bss
.align 16
stack_bottom:
@@ -17,15 +25,19 @@ stack_bottom:
stack_top:
.section .text
-.global _start
.type _start, @function
_start:
+ call init_gdt_table
+ mov 0x10, %ax
+ mov %ds, %ax
+ mov %es, %ax
+ mov %fs, %ax
+ mov %gs, %ax
+ mov %ss, %ax
mov $stack_top, %esp
-
+ cli
call _init
call kernel_main
-
- cli
1: hlt
jmp 1b
diff --git a/src/gdt.c b/src/gdt.c
new file mode 100644
index 0000000..9f87032
--- /dev/null
+++ b/src/gdt.c
@@ -0,0 +1,49 @@
+#include<stdbool.h>
+#include<stddef.h>
+#include<stdint.h>
+
+struct gdt_entry
+{
+ uint16_t limit;
+ uint16_t base1;
+ uint8_t base2;
+ uint8_t access;
+ uint8_t limit_flags;
+ uint8_t base3;
+} __attribute__((packed));
+
+struct gdt_pointer
+{
+ uint16_t size;
+ uint32_t offset;
+} __attribute__((packed));
+
+// asm function
+extern void load_gdt(struct gdt_pointer *gdtp);
+
+struct gdt_entry gdt[3];
+struct gdt_pointer gdtp;
+
+void init_gdt_entry(size_t num, uint32_t limit, uint32_t base1, uint32_t base2, uint8_t access, uint8_t limit_flags, uint8_t base3)
+{
+ gdt[num].limit=limit;
+ gdt[num].base1=base1;
+ gdt[num].access=access;
+ gdt[num].limit_flags=limit_flags;
+ gdt[num].base3=base3;
+}
+
+void init_gdt_table()
+{
+ gdtp.size=sizeof(gdt)-1;
+ gdtp.offset=(uint32_t)&gdt;
+
+ //null
+ init_gdt_entry(0,0,0,0,0,0,0);
+ //code
+ init_gdt_entry(1,0xffff,0x0000,0x00,0b10011010,0b11001111,0x00);
+ //data
+ init_gdt_entry(2,0xffff,0x0000,0x00,0b10010010,0b11001111,0x00);
+
+ load_gdt(&gdtp);
+}
diff --git a/src/kernel.c b/src/kernel.c
index 90f34bd..c6d1045 100644
--- a/src/kernel.c
+++ b/src/kernel.c
@@ -1,6 +1,6 @@
-#include <stdbool.h>
-#include <stddef.h>
-#include <stdint.h>
+#include<stdbool.h>
+#include<stddef.h>
+#include<stdint.h>
#include"vga.h"
static const size_t VGA_WIDTH = 80;