Commit 7ff5b79f by malcolm

Added GDT code in C

parent 6f768eab
Showing with 81 additions and 0 deletions
#include "gdt.h"
struct gdtr{
uint16_t limit;
uint32_t base;
}
static struct gdt_desc _gdt [MAX_GDT_DESC];
static struct gdtr _gdtr;
static void gdt_install () {
//TODO: VOODOO MAGIC
}
void gdt_set_desc(uint32_t i, uint64_t base, uint64_t limit, uint8_t access, uint8_t gran)
{
if (i > MAX_GDT_DESC)
return;
memset ((void*)&_gdt[i], 0, sizeof (gdt_descriptor));
_gdt[i].low_base = uint16_t(base & 0xffff);
_gdt[i].mid_base = uint8_t((base >> 16) & 0xff);
_gdt[i].high_base= uint8_t((base >> 24) & 0xff);
_gdt[i].lim = uint16_t(limit & 0xffff);
//! set flags and grandularity bytes
_gdt[i].flags = access;
_gdt[i].gran = uint8_t((limit >> 16) & 0x0f);
_gdt[i].gran |= gran & 0xf0;
}
gdt_descriptor* i86_gdt_get_descriptor (int i) {
if (i > MAX_GDT_DESC)
return 0;
return &_gdt[i];
}
int i86_gdt_initialize () {
_gdtr.limit = (sizeof (struct gdt_desc) * MAX_GDT_DESC)-1;
_gdtr.base = (uint32_t)&_gdt[0];
//null
gdt_set_descriptor(0, 0, 0, 0, 0);
//code
gdt_set_descriptor (1,0,0xffffffff, GDT_RW|GDT_EXE|GDT_CDT|GDT_MEM, GDT_GR_PG|GDT_GR_32|GDT_GR_HI);
//data
gdt_set_descriptor (2,0,0xffffffff, GDT_RW|GDT_CDT|GDT_MEM, GDT_GR_PG|GDT_GR_32|GDT_GR_HI);
gdt_install();
return 0;
}
#ifndef _GDT_INC
#define _GDT_INC
#include <stdint.h>
#define MAX_GDT_DESC 3
#define GDT_ACC 0x0001
#define GDT_RW 0x0002
#define GDT_DIR 0x0004
#define GDT_EXE 0x0008
#define GDT_CDT 0x0010
#define GDT_DPL 0x0060
#define GDT_MEM 0x0080
#define GDT_GR_HI 0x0f
#define GDT_GR_OS 0x10
#define GDT_GR_32 0x40
#define GDT_GR_PG 0x80
struct gdt_desc {
uint16_t lim;
uint16_t low_base;
uint8_t mid_base;
uint8_t flags;
uint8_t gran;
uint8_t high_base;
};
extern void gdt_set_desc(uint32_t i, uint64_t base, uint64_t limit, uint8_t access, uint8_t gran);
extern gdt_desc* gdt_get_desc(int i);
extern int gdt_init();
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment