GDT Not installing properly?
Posted: Thu Jun 06, 2013 11:44 am
I've been following bran's kernel development tutorial for setting up my GDT.
I first tried doing it his way, in C, with the ASM stub that loads the gdt. That didn't work, so i tried loading the whole GDT in assembly. However, i'm getting the same result with both of my methods.
In my code, i have it output "Installing GDT...\n", call gdt_install(), and then output "GDT Successfully Installed!\n"
However, it only prints "Installing GDT...\n", and (i assume) never returns from the gdt_install() function.
Could someone give me a poke in the right direction here? I'm new to OS development, so it may be an amateurish mistake. The relevant source code is posted below.
Note:
Compiled with a gcc cross compiler on Debian linux
Run in VirtualBox on windows 7, x64
kernel.c - entry point function
gdt.h
gdt.c
install_gdt.asm
I first tried doing it his way, in C, with the ASM stub that loads the gdt. That didn't work, so i tried loading the whole GDT in assembly. However, i'm getting the same result with both of my methods.
In my code, i have it output "Installing GDT...\n", call gdt_install(), and then output "GDT Successfully Installed!\n"
However, it only prints "Installing GDT...\n", and (i assume) never returns from the gdt_install() function.
Could someone give me a poke in the right direction here? I'm new to OS development, so it may be an amateurish mistake. The relevant source code is posted below.
Note:
Compiled with a gcc cross compiler on Debian linux
Run in VirtualBox on windows 7, x64
kernel.c - entry point function
Code: Select all
#include "./kernel/gdt.h"
void kernel_main()
{
k_clearscreen(TEXT_ATTRIB);
char *tst = "Kernel is loaded.\n\n";
k_putstring(tst, TEXT_ATTRIB);
k_putstring("Installing GDT...\n", TEXT_ATTRIB);
//init_gdt_entries();
gdt_install();
k_putstring("GDT installed successfully!\n\n", TEXT_ATTRIB);
}
Code: Select all
#if !defined(__cplusplus)
#include <stdbool.h> /* C doesn't have booleans by default. */
#endif
#include <stddef.h>
#include <stdint.h>
#define GDT_ENTRY_COUNT 3
struct GDT_ENTRY {
unsigned short limit_low;
unsigned short base_low;
unsigned char base_mid;
unsigned char access;
unsigned char granularity;
unsigned char base_high;
} __attribute__((packed));
struct GDT_PTR {
unsigned short limit;
unsigned long base;
} __attribute__((packed));
struct GDT_ENTRY gdt_entries[GDT_ENTRY_COUNT];
struct GDT_PTR gdt_ptr;
void init_gdt_entries();
extern void gdt_install();
Code: Select all
#include "gdt.h"
void init_gdt_entries()
{
// null descriptor
gdt_entries[0].limit_low = 0;
gdt_entries[0].base_low = 0;
gdt_entries[0].base_mid = 0;
gdt_entries[0].base_high = 0;
gdt_entries[0].access = 0;
gdt_entries[0].granularity = 0;
// code descriptor
gdt_entries[0].limit_low = 0xFFFF;
gdt_entries[0].base_low = 0x0000;
gdt_entries[0].base_mid = 0x00;
gdt_entries[0].base_high = 0x00;
gdt_entries[0].access = 0x9A;
gdt_entries[0].granularity = 0xCF;
// data descriptor
gdt_entries[0].limit_low = 0xFFFF;
gdt_entries[0].base_low = 0x0000;
gdt_entries[0].base_mid = 0x00;
gdt_entries[0].base_high = 0x00;
gdt_entries[0].access = 0x92;
gdt_entries[0].granularity = 0xCF;
gdt_ptr.base = (unsigned long) &gdt_entries;
gdt_ptr.limit = (sizeof(struct GDT_ENTRY) * GDT_ENTRY_COUNT) - 1;
}
Code: Select all
;extern gdt_ptr
global gdt_install
gdt_install:
lgdt [m_gdt]
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp 0x08:gdt_return
gdt_return:
ret
gdt_start:
; null descriptor
dd 0
dd 0
;code descriptor
dw 0xFFFF ; limit low
dw 0x0000 ; base low
db 0x00 ; base middle
db 0x9A ; access
db 0xCF ; granularity
db 0x00 ; base high
;data descriptor
dw 0xFFFF ; limit low
dw 0x0000 ; base low
db 0x00 ; base middle
db 0x92 ; access
db 0xCF ; granularity
db 0x00 ; base high
gdt_end:
m_gdt:
dw gdt_end - gdt_start - 1;
dd gdt_start