OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 4:45 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Crash after attempting to load GDT
PostPosted: Sun Jul 17, 2022 4:27 am 
Offline

Joined: Sun Jul 17, 2022 4:15 am
Posts: 2
When I tried to load my GDT, my OS immediately crashes and reboots. I have tried debugging and all that I have found is that it occurs on the line attempting to reload CS.
This is my code:
gdtc.c:
Code:
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

struct GDT {
        uint32_t base;
        uint32_t limit;
        uint8_t type;
        uint8_t flags;
};

extern void setGdt(uint8_t* limit, uint32_t base);
extern void reloadSegments(void);

void encodeGdtEntry(uint8_t *target, struct GDT source) {
        if (source.limit > 0xFFFFF) {k_print("GDT cannot encode limits larger than 0xFFFFF");}
        target[0] = source.limit & 0xFF;
        target[1] = (source.limit >> 8) & 0xFF;
        target[6] = (source.limit >> 16) & 0x0F;
        target[2] = source.base & 0xFF;
        target[3] = (source.base >> 8) & 0xFF;
        target[4] = (source.base >> 16) & 0xFF;
        target[7] = (source.base >> 24) & 0xFF;
        target[5] = source.type;
        target[6] |= (source.flags << 4);
}

void gdt_init(uint8_t* buffer) {
        struct GDT GdtEntries[5] = {
                {0, 0, 0, 0},           // Selector 0x00 cannot be used
                {0, 0xfffff, 0x9A, 0xC},// Selector 0x08 will be our code
                {0, 0xfffff, 0x92, 0xC},// Selector 0x10 will be our data
                {0, 0xfffff, 0xFA, 0xC},// Selector 0x18 will be user code
                {0, 0xfffff, 0xF2, 0xC} // Selector 0x20 will be user data

        };
        for (int i = 0; i < 5; i++) encodeGdtEntry(buffer+i*8, GdtEntries[i]);
        setGdt(buffer, 255);
        reloadSegments();
}


gdt.asm:
Code:
global setGdt
global reloadSegments

section .data:
        gdtr DW 0 ; For limit storage
             DD 0 ; For base storage

section .text
        setGdt:
                MOV AX, [esp + 4]
                MOV [gdtr], AX
                MOV EAX, [ESP + 8]
                MOV [gdtr + 2], EAX
                LGDT [gdtr]
                RET
        reloadSegments:
                ; Reload CS register containing code selector:
                JMP   0x08:.reload_CS ; reboot occurs here
        .reload_CS:
                ; Reload data segment registers:
                MOV   AX, 0x10 ; 0x10 is a stand-in for your data segment
                MOV   DS, AX
                MOV   ES, AX
                MOV   FS, AX
                MOV   GS, AX
                MOV   SS, AX
                RET

gdt_init is called like this:
Code:
void gdt_init(uint8_t* buffer);
uint8_t gdt_buffer[256];

void kernel_main(void) {
   for (int i = 0; i<256; i++) gdt_buffer[i] = 0;
   gdt_init(gdt_buffer);
   // rest of code
}


Top
 Profile  
 
 Post subject: Re: Crash after attempting to load GDT
PostPosted: Wed Jul 20, 2022 7:52 pm 
Online
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
Rzed781 wrote:
I have tried debugging

How did you try debugging? A virtual machine that can show you the contents of registers (such as the GDTR) will quickly reveal the problem. I like using QEMU with the "-d int" option, but there are several ways to do this.

Rzed781 wrote:
Code:
        setGdt(buffer, 255);

Code:
                MOV AX, [esp + 4]
                MOV [gdtr], AX
                MOV EAX, [ESP + 8]
                MOV [gdtr + 2], EAX

The order of the parameters doesn't match. (Parameter passing is defined in the i386 psABI.)


Top
 Profile  
 
 Post subject: Re: Crash after attempting to load GDT
PostPosted: Fri Jul 22, 2022 2:54 am 
Offline

Joined: Sun Jul 17, 2022 4:15 am
Posts: 2
Don't worry, I fixed it before it got approved. I debugged it using qemu -s and gdb and found the values in gdtr to be incorrect.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 76 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group