Unable to load the GDT twice (second time in C)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
paikuhan
Posts: 7
Joined: Tue Apr 06, 2021 10:36 am

Unable to load the GDT twice (second time in C)

Post by paikuhan »

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: Select all


[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: Select all


#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!
quirck
Member
Member
Posts: 42
Joined: Sun Nov 23, 2008 5:56 am
Location: Russia, Saint-Petersburg

Re: Unable to load the GDT twice (second time in C)

Post by quirck »

paikuhan wrote:

Code: Select all

	gdt[num].granularity	!= (gran & 0xF0);
Do you have -Wall enabled? It should be |=, not !=.
paikuhan
Posts: 7
Joined: Tue Apr 06, 2021 10:36 am

Re: Unable to load the GDT twice (second time in C)

Post by paikuhan »

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!
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Unable to load the GDT twice (second time in C)

Post by nexos »

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
Post Reply