From 9703f2484c975900a2b4f20e5a9dc00ca2ac9f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Wed, 14 Mar 2018 17:10:51 +0100 Subject: [PATCH] Added GDT. Unfortunately I can't do it in C, but whatever. --- Makefile | 2 +- src/kernel/arch/x86/boot.s | 15 +++++++++ src/kernel/arch/x86/gdt.s | 68 ++++++++++++++++++++++++++++++++++++++ src/kernel/arch/x86/tss.h | 2 +- 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 src/kernel/arch/x86/gdt.s diff --git a/Makefile b/Makefile index de11eb2..445fb2d 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ ARCH=$(shell echo $(TARGET) | sed s/i.86/x86/) CROSSFLAGS=--target=$(TARGET)-pc-none-elf -march=$(TARGET) # Assembly options AS=nasm -AFLAGS=-felf32 +AFLAGS=-felf32 -Isrc/kernel/arch/$(ARCH)/ -Isrc/ # C options CC=clang CFLAGS?=-O0 -g diff --git a/src/kernel/arch/x86/boot.s b/src/kernel/arch/x86/boot.s index 4e32d72..46705ac 100644 --- a/src/kernel/arch/x86/boot.s +++ b/src/kernel/arch/x86/boot.s @@ -32,12 +32,27 @@ stack_bottom: resb 16384 ; 16KiB stack_top: +section .data +%include "gdt.s" + section .text global _start:function (_start.end - _start) _start: ; setup the stack mov esp, stack_top + ; TODO: setup GDT, IDT, paging, etc. here + cli + lgdt [gdtr] + jmp 0x08:.reload_segs +.reload_segs: + mov ax, 0x10 + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + mov ss, ax + ; call the kernel extern kernel_main call kernel_main diff --git a/src/kernel/arch/x86/gdt.s b/src/kernel/arch/x86/gdt.s new file mode 100644 index 0000000..c420b90 --- /dev/null +++ b/src/kernel/arch/x86/gdt.s @@ -0,0 +1,68 @@ +; Copyright (C) 2018 Ortega Froysa, Nicolás +; Author: Ortega Froysa, Nicolás +; +; This program is free software: you can redistribute it and/or modify +; it under the terms of the GNU General Public License as published by +; the Free Software Foundation, either version 3 of the License, or +; (at your option) any later version. +; +; This program is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this program. If not, see . + +gdt_start: + +gdt_null: + dd 0x0 + dd 0x0 + +gdt_kcode: + dw 0xFFFF ; limit (bits 0-15) + dw 0x0 ; base (bits 0-15) + + db 0x0 ; base (bits 16-23) + db 10011010b ; 1st flags | type flags + db 11001111b ; 2nd flags | limit (bits 16-19) + db 0x0 ; base (bits 24-31) + +gdt_kdata: + dw 0xFFFF + dw 0x0 + + db 0x0 + db 10010010b + db 11001111b + db 0x0 + +gdt_ucode: + dw 0xFFFF + dw 0x0 + + db 0x0 + db 11111010b + db 11001111b + db 0x0 + +gdt_udata: + dw 0xFFFF + dw 0x0 + + db 0x0 + db 11110010b + db 11001111b + db 0x0 + +; TODO: fill this in later +gdt_tss: + dd 0x0 + dd 0x0 + +gdt_end: + +gdtr: + dw gdt_end - gdt_start - 1 + dd gdt_start diff --git a/src/kernel/arch/x86/tss.h b/src/kernel/arch/x86/tss.h index c3e8d45..ab65b58 100644 --- a/src/kernel/arch/x86/tss.h +++ b/src/kernel/arch/x86/tss.h @@ -18,7 +18,7 @@ #pragma once -volatile struct tss { +struct tss { uint16_t link; uint16_t link_r; // reserved