OSDev.org

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

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Unable to load the GDT twice (second time in C)
PostPosted: Sun Jan 16, 2022 3:21 pm 
Offline

Joined: Tue Apr 06, 2021 10:36 am
Posts: 7
Hi,
I often came here writing a long paragraph of an issue I can't fix after much googling. But then end up not submitting my post because somehow I found the answer. This time I need you. OS-Dever Assemble! (please).

I am currently reading two related tutorial on the same subject : Kernel Development. More specifically GDT, IDT, ISR, IRQ, PIT and user space. (I didn't need them before because I was working on VESA , mouse and keyboard, and GUI Programming. The tutorials in question are Bran's kernel development and James Molloy's kernel development tutorials (the latter is based on the former). In both tutorials they reload a GDT (even thought GRUB has loaded one before and entered protected mode) that they can "track"/point to. For this part I used Bran's way (which is not always the case. I usually take the most readable of the two). Don't want to be rude but I'm gonna post lots of code:

so my kernel entry point assembly file (the one that is linked with the kernel main file and where I jump to after I entered protected mode) looks like this:

Code:

[BITS          32]
[GLOBAL     _start]

_start:

[EXTERN  kernelmain]
    CALL kernelmain
    JMP $


GLOBAL gdt_flush
EXTERN gp
gdt_flush:
   LGDT    [gp]
   MOV      AX, 0x10
   MOV      DS, AX
   MOV      ES, AX
   MOV      FS, AX
   MOV      GS, AX
   MOV      SS, AX
   JMP      0x08:flush2
flush2:
   RET



And my gdt.c looks like this:

Code:

#include "gdt.h"

struct gdt_entry gdt[3];
struct gdt_ptr gp;

void gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran)
{
   gdt[num].base_low        = (base & 0xFFFF);
   gdt[num].base_middle     = (base >> 16) & 0xFF;
   gdt[num].base_high        = (base >> 24) & 0xFF;
   
   gdt[num].limit_low        = (limit & 0xFFFF);
   gdt[num].granularity     = ((limit >> 16) & 0x0F);
   
   gdt[num].granularity   != (gran & 0xF0);
   gdt[num].access        = access;
}

void gdt_install()
{
   
   gp.limit   = (sizeof(struct gdt_entry) * 3) - 1;
   gp.base    = (unsigned int)&gdt;
   
   gdt_set_gate(0, 0, 0, 0, 0);
   
   gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
   
   gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
   
   gdt_flush();
}



I call gdt_install() in kernel_main.c but VMWare Player crashes with a fault that "has occured causing the virtual CPU to enter the shutdown state". When I comment out gdt_flush() in gdt_install() in the gdt.c file nothing crashes happen and the Virtual Machine prints "Hello World" in protected Mode. My guess is the issue comes from the gdt_flush procedure in the assembly file but I can figure out why except maybe one can't load a GDT after it was loaded already (which makes no sense since Bran and James did it). James Molloy has a modified version of the procedure where he calls the pointer to the GDT indirectly but the rest of the code is the same. Note that I am not using GRUB but my own bootloader.

I need help please!


Top
 Profile  
 
 Post subject: Re: Unable to load the GDT twice (second time in C)
PostPosted: Sun Jan 16, 2022 4:50 pm 
Offline
Member
Member

Joined: Sun Nov 23, 2008 5:56 am
Posts: 42
Location: Russia, Saint-Petersburg
paikuhan wrote:
Code:
   gdt[num].granularity   != (gran & 0xF0);


Do you have -Wall enabled? It should be |=, not !=.


Top
 Profile  
 
 Post subject: Re: Unable to load the GDT twice (second time in C)
PostPosted: Mon Jan 17, 2022 1:54 am 
Offline

Joined: Tue Apr 06, 2021 10:36 am
Posts: 7
quirck wrote:
Do you have -Wall enabled? It should be |=, not !=.


Thank you so much!
No, I didn't have -Wall enabled. I have it enabled now. Such a useful parameter.

Thanks again!


Top
 Profile  
 
 Post subject: Re: Unable to load the GDT twice (second time in C)
PostPosted: Mon Jan 17, 2022 12:31 pm 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
Quote:
No, I didn't have -Wall enabled. I have it enabled now. Such a useful parameter.

Also, I can recommend using -Wextra as well. Also, when your feeling lazy, use -Werror to force yourself to fix warnings :)

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], FrankRay78, Google [Bot] and 51 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