CeDOS - Commit 47b2dbc3

GDT added, some workflow changes OS currently jumps into protected mode before the prerequisites are set, so execution fails
Celina Sophie Kalus
Sat, 16 Dec 2017 18:36:29 +0100
8 files changed, 125 insertions(+), 10 deletions(-)
M boot/boot.sboot/boot.s

@@ -28,6 +28,9 @@ # print hello world

movw $message, %si call print + # TODO: + # - activate A20 gate + # reset bootdrive reset: movb $0x00, %ah

@@ -62,9 +65,20 @@ xor %ax, %ax

mov %ax, %ds mov %ax, %es + # - load global descriptor table and far jump into protected mode + lgdt (GDT_DESCRIPTOR) + + mov %cr0, %eax + or $1, %eax + mov %eax, %cr0 + + ljmp $0x8, $protected + +.code32 +protected: + + # TODO: - # - load global descriptor table and far jump into protected mode - # - activate A20 gate # - jump to second stage code

@@ -72,6 +86,7 @@ # loop forever

loop: jmp loop +.code16 # ############################################ # print

@@ -98,6 +113,10 @@ .ascii "Hello World!"

.byte 13 .byte 10 .byte 0 + +GDT_DESCRIPTOR: + .int GDT + .word 0x23 end: .=510
M boot/makefileboot/makefile

@@ -3,3 +3,12 @@

.PHONY: build build: boot.s > $(GCC_PREFIX)as -o $(BUILD_DIR)/boot.o boot.s + +.PHONY: clear +clear: +> @rm build/*.* 2> /dev/null; true + +.PHONY: rebuild +rebuild: +> $(MAKE) clear +> $(MAKE) build
M makefilemakefile

@@ -2,9 +2,11 @@ .RECIPEPREFIX = >

export CURRENT_DIR = $(shell pwd) export BUILD_DIR = $(CURRENT_DIR)/build +export INCLUDE_DIR = $(CURRENT_DIR)/include +export DEBUG_DIR = $(CURRENT_DIR)/debug export GCC_PREFIX = $(HOME)/opt/cross/i686-elf-/bin/i686-elf- -export GCC_OPTIONS = -O0 -std=c++0x -Wno-write-strings -Wall -Wextra -fno-exceptions -fno-rtti -ffreestanding +export GCC_OPTIONS = -O0 -Wno-write-strings -Wall -Wextra -fno-exceptions -nostdlib -nostartfiles -ffreestanding # OBJ_FILES = $(wildcard obj/asm/*.o) $(wildcard obj/cpp/*.o)

@@ -12,11 +14,13 @@ .PHONY: build

build: > $(MAKE) -C boot build > $(MAKE) -C second_stage build -> $(GCC_PREFIX)ld -T link.txt -Map=mapfile.txt -o $(BUILD_DIR)/base.img --oformat binary +> $(GCC_PREFIX)ld -T link.txt -Map=$(DEBUG_DIR)/mapfile.txt -o $(BUILD_DIR)/base.img --oformat binary .PHONY: clear clear: > @rm $(BUILD_DIR)/*.* 2> /dev/null; true +> @$(MAKE) -C boot clear 2> /dev/null; true +> @$(MAKE) -C second_stage clear 2> /dev/null; true .PHONY: run run:
M second_stage/entry.ssecond_stage/entry.s

@@ -1,8 +1,10 @@

.section .text -start: -# loop: -# jmp loop +.global _start +_start: +loop: + jmp loop +.section .data low_kernel_welcome: .ascii "Welcome to the low kernel!" .byte 13
A second_stage/gdt.c

@@ -0,0 +1,23 @@

+#include "gdt.h" + +#define GDT_MAKE_ENTRY(base, limit, access, flags) { \ + (uint8_t)(limit >> 0), \ + (uint8_t)(limit >> 8), \ + (uint8_t)(base >> 0), \ + (uint8_t)(base >> 8), \ + (uint8_t)(base >> 16), \ + (uint8_t)(access), \ + (uint8_t)(((limit >> 16) & 0x0F) | ((flags << 4) & 0xF0)), \ + (uint8_t)(base >> 24), \ + } + +GDT_ENTRY GDT[3] = { + // null descriptor + GDT_MAKE_ENTRY(0x00000000, 0x00000000, 0x00, 0x0), + + // identity mapping (code) + GDT_MAKE_ENTRY(0x00000000, 0x000FFFFF, 0x9A, 0xC), + + // identity mapping (data) + GDT_MAKE_ENTRY(0x00000000, 0x000FFFFF, 0x92, 0xC) +};
A second_stage/gdt.h

@@ -0,0 +1,27 @@

+#ifndef GDT_H +#define GDT_H + +#include "stdint.h" + +typedef struct { + uint8_t limit_0; + uint8_t limit_8; + uint8_t base_0; + uint8_t base_8; + uint8_t base_16; + uint8_t access; + uint8_t limit_and_flags; + uint8_t base_24; +} __attribute__((packed)) GDT_ENTRY; + +GDT_ENTRY GDT[3]; + +/*struct { + uint32_t gdt_offset; + uint16_t gdt_size; +} GDT_DESCRIPTOR = { + (uint32_t)GDT, + (uint16_t)(sizeof GDT) +};*/ + +#endif
A second_stage/link.txt

@@ -0,0 +1,19 @@

+INPUT(build/entry.o build/gdt.o) +OUTPUT_FORMAT(elf32-i386) +OUTPUT_ARCH(i386) +ENTRY(_start) + +SECTIONS +{ + .text : + { + build/entry.o(.text) + build/gdt.o(.text) + } + + .data : + { + build/entry.o(.data) + build/gdt.o(.data) + } +}
M second_stage/makefilesecond_stage/makefile

@@ -1,8 +1,20 @@

.RECIPEPREFIX = > .PHONY: build -build: build/entry.o -> $(GCC_PREFIX)ld -i build/entry.o -o $(BUILD_DIR)/second_stage.o --oformat elf32-i386 +build: build/entry.o build/gdt.o +> $(GCC_PREFIX)ld -T link.txt -o $(BUILD_DIR)/second_stage.o --oformat elf32-i386 + +.PHONY: clear +clear: +> @rm build/*.* 2> /dev/null; true + +.PHONY: rebuild +rebuild: +> $(MAKE) clear +> $(MAKE) build build/entry.o: entry.s -> $(GCC_PREFIX)as -o $@ $?+> $(GCC_PREFIX)as -o $@ $< + +build/gdt.o: gdt.c gdt.h +> $(GCC_PREFIX)gcc -I$(INCLUDE_DIR) --prefix=$(GCC_PREFIX) $(GCC_OPTIONS) -o $@ $<