summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksa Vučković <aleksav013@gmail.com>2021-10-27 11:44:12 +0200
committerAleksa Vučković <aleksav013@gmail.com>2021-10-27 11:44:12 +0200
commit89f7f1b114c1bbea3ad62808bf3653e1d2337d1f (patch)
treea4617a721ebe2e9456a99b1b8fc0c208b767ac15
parent818a6fea63d5427e43becc0d4a5279a631a7f1a6 (diff)
Added paging
-rw-r--r--Makefile23
-rw-r--r--src/as/Makefile2
-rw-r--r--src/as/paging.s21
-rw-r--r--src/c/Makefile2
-rw-r--r--src/c/irq.c (renamed from src/c/irq_handler.c)0
-rw-r--r--src/c/kernel.c2
-rw-r--r--src/c/paging.c51
-rw-r--r--src/c/tty.c5
8 files changed, 91 insertions, 15 deletions
diff --git a/Makefile b/Makefile
index dd02b46..e99a6a6 100644
--- a/Makefile
+++ b/Makefile
@@ -10,10 +10,12 @@ CP=cp
QEMU=qemu-system-x86_64
SOURCE_DIR=src
+BUILD_DIR=${CURDIR}/build
+ISO_DIR=isodir
AS_SOURCE_DIR=$(SOURCE_DIR)/as
C_SOURCE_DIR=$(SOURCE_DIR)/c
-export BUILD_DIR=${CURDIR}/build
-ISO_DIR=isodir
+export AS_OBJECT_DIR=$(BUILD_DIR)/as
+export C_OBJECT_DIR=$(BUILD_DIR)/c
TARGET=myos
BINARY=$(BUILD_DIR)/$(TARGET).bin
@@ -21,19 +23,19 @@ ISO=$(TARGET).iso
CRTI_SOURCE=crti.s
CRTN_SOURCE=crtn.s
-AS_SOURCE=boot.s irq.s
-C_SOURCES=gdt.c heap.c idt.c kernel.c keyboard.c keymap.c stdio.c string.c tty.c vga.c irq_handler.c stack_protector.c timer.c
+AS_SOURCE=boot.s irq.s paging.s
+C_SOURCES=gdt.c heap.c idt.c kernel.c keyboard.c keymap.c stdio.c string.c tty.c vga.c irq.c stack_protector.c timer.c paging.c
C_SOURCE_FILES=$(patsubst %,$(C_SOURCE_DIR)/%,$(C_SOURCES))
-export C_OBJECTS=$(patsubst %,$(BUILD_DIR)/%,$(C_SOURCES:c=o))
+export C_OBJECTS=$(patsubst %,$(C_OBJECT_DIR)/%,$(C_SOURCES:c=o))
-AS_OBJECT=$(patsubst %,$(BUILD_DIR)/%,$(AS_SOURCE:s=o))
-CRTI_OBJECT=$(patsubst %,$(BUILD_DIR)/%,$(CRTI_SOURCE:s=o))
-CRTN_OBJECT=$(patsubst %,$(BUILD_DIR)/%,$(CRTN_SOURCE:s=o))
+AS_OBJECT=$(patsubst %,$(AS_OBJECT_DIR)/%,$(AS_SOURCE:s=o))
+CRTI_OBJECT=$(patsubst %,$(AS_OBJECT_DIR)/%,$(CRTI_SOURCE:s=o))
+CRTN_OBJECT=$(patsubst %,$(AS_OBJECT_DIR)/%,$(CRTN_SOURCE:s=o))
AS_SOURCES=$(AS_SOURCE) $(CRTI_SOURCE) $(CRTN_SOURCE)
AS_SOURCE_FILES=$(patsubst %,$(AS_SOURCE_DIR)/%,$(AS_SOURCES))
-export AS_OBJECTS=$(patsubst %,$(BUILD_DIR)/%,$(AS_SOURCES:s=o))
+export AS_OBJECTS=$(patsubst %,$(AS_OBJECT_DIR)/%,$(AS_SOURCES:s=o))
CRTBEGIN_OBJ=$(shell $(CC) -print-file-name=crtbegin.o)
CRTEND_OBJ=$(shell $(CC) -print-file-name=crtend.o)
@@ -48,7 +50,8 @@ $(BINARY): $(OBJ)
$(CC) -T $(SOURCE_DIR)/linker.ld -o $(BINARY) $(CFLAGS) -nostdlib -lgcc $(OBJ)
compile: $(AS_SOURCE_FILES) $(C_SOURCE_FILES)
- $(MKDIR) $(BUILD_DIR)
+ $(MKDIR) $(AS_OBJECT_DIR)
+ $(MKDIR) $(C_OBJECT_DIR)
$(MAKE) --directory $(AS_SOURCE_DIR)
$(MAKE) --directory $(C_SOURCE_DIR)
$(MAKE) $(BINARY)
diff --git a/src/as/Makefile b/src/as/Makefile
index e2c476d..bd5664b 100644
--- a/src/as/Makefile
+++ b/src/as/Makefile
@@ -1,5 +1,5 @@
.PHONY: all
all: $(AS_OBJECTS)
-$(BUILD_DIR)/%.o: %.s
+$(AS_OBJECT_DIR)/%.o: %.s
$(AS) $< -o $@
diff --git a/src/as/paging.s b/src/as/paging.s
new file mode 100644
index 0000000..2dd52d3
--- /dev/null
+++ b/src/as/paging.s
@@ -0,0 +1,21 @@
+.globl loadPageDirectory
+loadPageDirectory:
+ push %ebp
+ mov %esp, %ebp
+ mov 8(%esp), %eax
+ mov %eax, %cr3
+ mov %ebp, %esp
+ pop %ebp
+ ret
+
+.text
+.globl enablePaging
+enablePaging:
+ push %ebp
+ mov %esp, %ebp
+ mov %cr0, %eax
+ or $0x80000000, %eax
+ mov %eax, %cr0
+ mov %ebp, %esp
+ pop %ebp
+ ret
diff --git a/src/c/Makefile b/src/c/Makefile
index 7c5d1af..9625009 100644
--- a/src/c/Makefile
+++ b/src/c/Makefile
@@ -1,5 +1,5 @@
.PHONY: all
all: $(C_OBJECTS)
-$(BUILD_DIR)/%.o: %.c
+$(C_OBJECT_DIR)/%.o: %.c
$(CC) -c $< -o $@ -std=gnu99 $(CFLAGS)
diff --git a/src/c/irq_handler.c b/src/c/irq.c
index 6b80a52..6b80a52 100644
--- a/src/c/irq_handler.c
+++ b/src/c/irq.c
diff --git a/src/c/kernel.c b/src/c/kernel.c
index 62f444c..48dbccb 100644
--- a/src/c/kernel.c
+++ b/src/c/kernel.c
@@ -5,10 +5,12 @@ void init_idt_table(void);
void init_keyboard(void);
void init_timer(uint32_t frequency);
void prompt(void);
+void set_paging();
void kernel_main(void)
{
terminal_initialize();
+ set_paging();
init_idt_table();
init_keyboard();
init_timer(50);
diff --git a/src/c/paging.c b/src/c/paging.c
new file mode 100644
index 0000000..b256b74
--- /dev/null
+++ b/src/c/paging.c
@@ -0,0 +1,51 @@
+#include"../include/types.h"
+
+extern void loadPageDirectory(uint32_t*);
+extern void enablePaging();
+
+uint32_t page_directory[1024] __attribute__((aligned(4096)));
+
+void set_pd()
+{
+ //set each entry to not present
+ for(size_t i=0;i<1024;i++)
+ {
+ // This sets the following flags to the pages:
+ // Supervisor: Only kernel-mode can access them
+ // Write Enabled: It can be both read from and written to
+ // Not Present: The page table is not present
+ page_directory[i] = 0x00000002;
+ }
+}
+
+uint32_t first_page_table[1024] __attribute__((aligned(4096)));
+
+void set_pt()
+{
+ // holds the physical address where we want to start mapping these pages to.
+ // in this case, we want to map these pages to the very beginning of memory.
+ unsigned int i;
+
+ //we will fill all 1024 entries in the table, mapping 4 megabytes
+ for(i = 0; i < 1024; i++)
+ {
+ // As the address is page aligned, it will always leave 12 bits zeroed.
+ // Those bits are used by the attributes ;)
+ first_page_table[i] = (i * 0x1000) | 3; // attributes: supervisor level, read/write, present.
+ }
+}
+
+void put_pt()
+{
+ // attributes: supervisor level, read/write, present
+ page_directory[0] = ((uint32_t)first_page_table) | 3;
+}
+
+void set_paging()
+{
+ set_pd();
+ set_pt();
+ put_pt();
+ loadPageDirectory(page_directory);
+ enablePaging();
+}
diff --git a/src/c/tty.c b/src/c/tty.c
index 116a15e..c473fa1 100644
--- a/src/c/tty.c
+++ b/src/c/tty.c
@@ -110,9 +110,8 @@ void neofetch()
for(size_t i=0;i<16;i++)
{
- if(i==0) set_color(i,VGA_COLOR_WHITE);
- else set_color(i,VGA_COLOR_BLACK);
- printf("@%d ",i);
+ set_color(0,i);
+ printf(" ",i);
}
printf("\n");