qemu crashes when reloading segment registers
Posted: Mon Jul 30, 2012 3:12 pm
so I'm trying to get a "flat" GDT but when I reload the segment registers QEMU just rapidly restarts over and over again. after "mov ds, ax" I can't get anything to execute. if I try "jmp 0x0:.flush" first instead it crashes too. I've checked my code for assigning the correct values to the GDT entrys(which was based on JamesM's kernel development tutorials) and actully used JamesM's tutorial.
I've got this down to what I think is the simplest case that shows my issue. if you need further information please tell me
I'm using the following to run my kernel(using multiboot kernel)
here is my entry point code for multiboot compliance and gdt_flush function
and here is my kernel code
I've got this down to what I think is the simplest case that shows my issue. if you need further information please tell me
I'm using the following to run my kernel(using multiboot kernel)
Code: Select all
qemu-system-i386 -L bios -kernel kernel.binCode: Select all
global loader ; making entry point visible to linker
global _gdt_flush
extern _kmain ; kmain is defined in kmain.cpp
; setting up the Multiboot header - see GRUB docs for details
MODULEALIGN equ 1<<0 ; align loaded modules on page boundaries
MEMINFO equ 1<<1 ; provide memory map
FLAGS equ MODULEALIGN | MEMINFO ; this is the Multiboot 'flag' field
MAGIC equ 0x1BADB002 ; 'magic number' lets bootloader find the header
CHECKSUM equ -(MAGIC + FLAGS) ; checksum required
section .text
align 4
dd MAGIC
dd FLAGS
dd CHECKSUM
; reserve initial kernel stack space
STACKSIZE equ 0x4000 ; that's 16k.
loader:
mov esp, stack + STACKSIZE ; set up the stack
push eax ; Multiboot magic number
push ebx ; Multiboot info structure
call _kmain ; call kernel proper
cli
.hang:
hlt ; halt machine should kernel return
jmp .hang
_gdt_flush:
mov eax, [esp+4]
lgdt [eax]
mov ax, 0x10 ; 0x10 is the offset in the GDT to our data segment
mov ds, ax ; crashes here
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp 0x08:.flush
.flush:
ret
section .bss
align 4
stack: resb STACKSIZE ; reserve 16k stack on a doubleword boundaryCode: Select all
#include <stdint.h>
struct gdt_entry_struct
{
uint16_t limit_low; // The lower 16 bits of the limit.
uint16_t base_low; // The lower 16 bits of the base.
uint8_t base_middle; // The next 8 bits of the base.
uint8_t access; // The access byte
unsigned limit_high : 4; // upper 4 bits of limit
unsigned flags : 4; // flag bits
uint8_t base_high; // The last 8 bits of the base.
} __attribute__((packed));
typedef struct gdt_entry_struct gdt_entry_t;
struct gdt_ptr_struct
{
uint16_t limit; // The upper 16 bits of all selector limits.
void* base; // The address of the first gdt_entry_t struct.
} __attribute__((packed));
typedef struct gdt_ptr_struct gdt_ptr_t;
gdt_entry_t gdt_entries[5];
gdt_ptr_t gdt_ptr;
void gdt_set_gate(uint32_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t flags)
{
gdt_entries[num].base_low = (base & 0xFFFF);
gdt_entries[num].base_middle = (base >> 16) & 0xFF;
gdt_entries[num].base_high = (base >> 24) & 0xFF;
gdt_entries[num].limit_low = (limit & 0xFFFF);
gdt_entries[num].limit_high = (limit >> 16) & 0xF;
gdt_entries[num].flags = flags;
gdt_entries[num].access = access;
}
//sets the GDT from assembly
extern gdt_flush(void*);
int init_gdt() {
gdt_ptr.limit = (sizeof(gdt_entry_t) * 3) - 1;
gdt_ptr.base = &gdt_entries;
gdt_set_gate(0, 0, 0, 0, 0); // Null segment
gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0x00); // Code segment
gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0x00); // Data segment
gdt_flush(&gdt_ptr);
}
int kmain(void* mboot_ptr, uint32_t magic) {
if(magic != 0x2BADB002) while(1);
init_gdt();
while(1);
return 0;
}