Initial implementation of GDT

This commit is contained in:
Furkan Mudanyali 2022-07-18 18:14:20 +03:00
parent 72ffbdee0d
commit 2859788682
11 changed files with 122 additions and 8 deletions

View File

@ -32,20 +32,20 @@ $(KERNEL_ARCH_OBJS) \
kernel/kernel.o \
OBJS=\
$(ARCHDIR)/crti.o \
$(ARCHDIR)/boot/crti.o \
$(ARCHDIR)/crtbegin.o \
$(KERNEL_OBJS) \
$(ARCHDIR)/crtend.o \
$(ARCHDIR)/crtn.o \
$(ARCHDIR)/boot/crtn.o \
LINK_LIST=\
$(LDFLAGS) \
$(ARCHDIR)/crti.o \
$(ARCHDIR)/boot/crti.o \
$(ARCHDIR)/crtbegin.o \
$(KERNEL_OBJS) \
$(LIBS) \
$(ARCHDIR)/crtend.o \
$(ARCHDIR)/crtn.o \
$(ARCHDIR)/boot/crtn.o \
.PHONY: all clean install install-headers install-kernel
.SUFFIXES: .o .c .S

View File

@ -25,10 +25,11 @@ stack_top:
.type _start, @function
_start:
movl $stack_top, %esp
call _init # call global constructors
cli
call gdt_init # call global constructors
call kernel_main # transfer control to main kernel
# Hang if kernel_main unexpectedly returns
cli
1: hlt
jmp 1b
.size _start, . - _start

View File

@ -0,0 +1,48 @@
/**
* Referenced from https://github.com/tendstofortytwo/clay
* kernel/kernel.c
*/
#include <kernel/gdt.h>
#include <stdio.h>
#include <stdint.h>
uint32_t TSS[32];
gdt_entry GDT[GDT_ENTRIES];
gdt_ptr gp;
void gdt_fill(gdt_entry* target, gdt* source) {
if(source->limit > 0xFFFFF) {
printf("Invalid GDT limit %d\n", source->limit);
for(;;);
}
target->limit_lo = source->limit & 0xFFFF;
target->limit_hi_flags = (source->limit >> 16) & 0x0F;
target->base_lo = source->base & 0xFFFF;
target->base_mid = (source->base >> 16) & 0xFF;
target->base_hi = (source->base >> 24) & 0xFF;
target->access = source->access;
target->limit_hi_flags |= (source->flags << 4);
}
void gdt_init(void) {
uint32_t tssRef = (uint32_t) TSS;
gdt rows[GDT_ENTRIES] = {
{0, 0x00000, 0x00, 0x0}, // 0x0000 Null Descriptor
{0, 0xFFFFF, 0x9A, 0xC}, // 0x0008 Kernel Mode Code Segment
{0, 0xFFFFF, 0x92, 0xC}, // 0x0010 Kernel Mode Data Segment
{0, 0xFFFFF, 0xFA, 0xC}, // 0x0018 User Mode Code Segment
{0, 0xFFFFF, 0xF2, 0xC}, // 0x0020 User Mode Data Segment
{tssRef, sizeof(TSS), 0x89, 0x0}, // 0x0028 Task State Segment
};
for(uint32_t i = 0; i < GDT_ENTRIES; ++i) {
gdt_fill(GDT+i, rows+i);
}
gp.base = (void*) GDT;
gp.limit = (GDT_ENTRIES * sizeof(gdt_entry)) - 1;
gdt_load();
}

View File

@ -0,0 +1,20 @@
# Referenced from https://github.com/tendstofortytwo/clay
# kernel/boot/gdt.s
.global gdt_load
.type gdt_load, @function
gdt_load:
lgdt (gp)
mov %cr0, %eax
or $0x1, %eax
mov %eax, %cr0
jmp $0x08, $reload
reload:
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %ax, %ss
ret

View File

@ -4,5 +4,7 @@ KERNEL_ARCH_LDFLAGS=
KERNEL_ARCH_LIBS=
KERNEL_ARCH_OBJS=\
$(ARCHDIR)/boot.o \
$(ARCHDIR)/tty.o \
$(ARCHDIR)/boot/boot.o \
$(ARCHDIR)/vga/tty.o \
$(ARCHDIR)/boot/gdt.o \
$(ARCHDIR)/boot/gdt_load.o

View File

@ -0,0 +1,42 @@
/**
* Referenced from https://tendstofortytwo/clay
* kernel/kernel.c
*/
#ifndef _KERNEL_GDT_H
#define _KERNEL_GDT_H
#include <stdint.h>
#define GDT_ENTRIES 0x6
typedef struct {
uint16_t limit;
uint32_t base;
}__attribute__((packed)) gdt_ptr;
typedef struct {
uint16_t limit_lo;
uint16_t base_lo;
uint8_t base_mid;
uint8_t access;
uint8_t limit_hi_flags;
uint8_t base_hi;
}__attribute__((packed)) gdt_entry;
typedef struct {
uint32_t base;
uint32_t limit;
uint8_t access;
uint8_t flags;
}__attribute__((packed)) gdt;
void gdt_fill(gdt_entry*, gdt*);
void gdt_init(void);
extern void gdt_load();
extern uint32_t TSS[32];
extern gdt_entry GDT[GDT_ENTRIES];
extern gdt_ptr gp;
#endif

View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include <kernel/tty.h>
#include <kernel/gdt.h>
void kernel_main(void) {
terminal_initialize();