Skip to content

Commit

Permalink
Added gdt first draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryand1234 committed Apr 18, 2024
1 parent eaee5c1 commit 586d21c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ LIBS:=$(LIBS) $(KERNEL_ARCH_LIBS)
KERNEL_OBJS=\
$(KERNEL_ARCH_OBJS) \
kernel/kernel.o \
gdt/gdt.o

OBJS=\
$(ARCHDIR)/crti.o \
Expand Down
1 change: 1 addition & 0 deletions kernel/arch/i386/boot.S
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ stack_top:
_start:
movl $stack_top, %esp
call _init
call gdt_init
call kernel_main
cli
1: hlt
Expand Down
42 changes: 42 additions & 0 deletions kernel/gdt/gdt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include<string.h>
#include "gdt.h"
#include<stdio.h>
struct gdtdesc kgdt[GDTSIZE];
struct gdtr kgdtr;


void read_gdtr(uint32_t* tgdtr) {
asm("sgdt %0" : "=m" (*tgdtr));
}
void gdt_init(void) {

create_gdt_descriptor(0x0, 0x0, 0x0, 0x0, &kgdt[0]);
create_gdt_descriptor(0x0, 0xFFFFF, 0x9A, 0x0C, &kgdt[1]);
create_gdt_descriptor(0x0, 0xFFFFF, 0x92, 0x0C, &kgdt[2]);
create_gdt_descriptor(0x0, 0xFFFFF, 0xFA, 0x0C, &kgdt[3]);
create_gdt_descriptor(0x0, 0xFFFFF, 0xF2, 0x0C, &kgdt[4]);
// create_gdt_descriptor(0x0, 0xFFFFF, 0xF2, 0x0D, &kgdt[5]);
//create_gdt_descriptor(0x0, 0x0, 0xF7, 0x0D, &kgdt[6]);
kgdtr.limit = GDTSIZE*8;
kgdtr.base = GDT_BASE;
memcpy((char*) kgdtr.base, (char *) kgdt, kgdtr.limit);
asm("lgdtl (kgdtr)");
asm("movw $0x10, %ax \n \
movw %ax, %ds \n \
movw %ax, %es \n \
movw %ax, %fs \n \
movw %ax, %gs \n \
ljmp $0x08, $next \n \
next: \n ");
}

void create_gdt_descriptor(uint32_t base, uint32_t limit, uint8_t access, uint8_t flags, struct gdtdesc *desc) {
desc->limit0_15 = (limit & 0xffff);
desc->base0_15 = (base & 0xffff);
desc->base16_23 = (base & 0xff0000) >> 16;
desc->access_bytes = access;
desc->limit16_19 = (limit & 0xf0000) >> 16;
desc->flags = (flags & 0xf);
desc->base24_31 = (base & 0xff000000) >> 24;
return;
}
37 changes: 37 additions & 0 deletions kernel/gdt/gdt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef _GDT_H
#define _GDT_H

#include<stddef.h>
#include<stdint.h>

struct gdtr {
uint16_t limit;
uint32_t base;
} __attribute__ ((packed));

struct gdtdesc {
uint16_t limit0_15;
uint16_t base0_15;
uint8_t base16_23;
uint8_t access_bytes;
uint8_t flags:4;
uint8_t limit16_19;
uint8_t base24_31;
} __attribute__ ((packed));


#define GDTSIZE 0xFF
#define GDT_BASE 0x00000
#define DESC_NULL 0x0000
#define DESC_KERNEL_CODE 0x0008
#define DESC_KERNEL_DATA 0x0010
#define DESC_USER_CODE 0x0018
#define DESC_USER_DATA 0x0020


void create_gdt_descriptor(uint32_t base, uint32_t limit, uint8_t access, uint8_t flags, struct gdtdesc *);

void init_gdt(void);


#endif

0 comments on commit 586d21c

Please sign in to comment.