I'm still learning so I apologise if this issue is trivial.
I made my GDT in C++ which defines both the kernel and user segments, problem is with my asm (probably with my table). VirtualBox crashes after the:
Code:
mov ss, ax
instruction.
gdt.h
Code:
#pragma once
#include "stdint.h"
struct GDTDescriptor {
uint16_t Size;
uint32_t Offset;
} __attribute__((packed));
struct GDTEntry {
uint16_t Limit0;
uint16_t Base0;
uint8_t Base1;
uint8_t AccessByte;
uint8_t Limit1_Flags;
uint8_t Base2;
} __attribute__((packed));
struct GDT {
GDTEntry Null;
GDTEntry KernelCode;
GDTEntry KernelData;
GDTEntry UserNull;
GDTEntry UserCode;
GDTEntry UserData;
} __attribute__((packed))
__attribute((aligned(0x1000)));
extern GDT DefaultGDT;
extern "C" void LoadGDT(GDTDescriptor* gdtDescriptor);
gdt.cpp
Code:
#include "gdt.h"
GDT DefaultGDT = {
{0, 0, 0, 0x00, 0x00, 0},
{0, 0, 0, 0x9a, 0xa0, 0},
{0, 0, 0, 0x92, 0xa0, 0},
{0, 0, 0, 0x00, 0x00, 0},
{0, 0, 0, 0x9a, 0xa0, 0},
{0, 0, 0, 0x92, 0xa0, 0}
};
gdt.asm (separate assembly file, linked after)
Code:
global LoadGDT
LoadGDT:
lgdt [edi]
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
retf
Loading the table in the main kernel file:
Code:
GDTDescriptor gdtDescriptor;
gdtDescriptor.Size = sizeof(GDT) - 1;
gdtDescriptor.Offset = (uint32_t)&DefaultGDT;
LoadGDT(&gdtDescriptor);
Advice and help would be greatly appreciated!
(I am running in protected mode after booting from GRUB).