I am writing an x86_32 bit kernel from scratch in C, after writing a VGA driver, I have been trying to implement the GDT as well as the interrupts for a keyboard driver. However, my GDT_flush() function that is being called from assembly fails and it results in a bootloop. commenting that function, and it boots just fine( but without the GDT obviously ).
I am simulating the kernel on Qemu, and using a target specific toolchain to compile and link all the files together.
GDT.c:
struct gdt_entry{
unsigned short limit_low;
unsigned short base_low;
unsigned char base_middle;
unsigned char access;
unsigned char granularity;
unsigned char base_high;
}__attribute__((packed));
struct gdt_pointer{
unsigned short limit;
unsigned int base;
}__attribute__((packed));
struct gdt_entry gdt[3];
struct gdt_pointer gdtptr;
//extern void flush_gdt(unsigned char gdtptr);
extern void flush_gdt();
void set_gdt_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 & 0x0F);
gdt[num].access = access;
}
void init_gdt(){
gdtptr.limit = (sizeof(struct gdt_entry) * 3) - 1;
gdtptr.base = (uint32_t)&gdt;
print_string("setting up kernel segments \n");
set_gdt_gate(0, 0, 0, 0, 0); //Null segment
set_gdt_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); //Code segment
set_gdt_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); //data segment
print_string("kernel segments have been setup hehehhehehe\n");
//flush_gdt((unsigned char)&gdtptr);
flush_gdt();
}
descriptor.asm:
global flush_gdt
[extern gdtptr]
flush_gdt:
;mov eax, [esp + 4]
;lgdt[eax]
lgdt[gdtptr]
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp 0x08: .flush_two
.flush_two:
ret
I looked up other similar projects and my implementation isn't very different. I've been trying to solve it for a couple of weeks now, any help is very appreciated.
thanks :)
I have tried passing the descriptor pointer as an argument to the function being called, but that also results in a bootloop.
Here is the QEMU log file:
0: v=0d e=0010 i=0 cpl=0 IP=0010:0000000000100d3b pc=0000000000100d3b SP=0018:0000000000104f1c env->regs[R_EAX]=0000000000100010
EAX=00100010 EBX=001066c8 ECX=000003d5 EDX=000003d5
ESI=00000000 EDI=00000000 EBP=00104f38 ESP=00104f1c
EIP=00100d3b EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
CS =0010 00000000 ffffffff 00cf9a00 DPL=0 CS32 [-R-]
SS =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
DS =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
FS =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
GS =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT= 696c6261 00006e65
IDT= 00000000 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000010 CCD=00104f10 CCO=ADDL
EFER=0000000000000000
check_exception old: 0xd new 0xd
1: v=08 e=0000 i=0 cpl=0 IP=0010:0000000000100d3b pc=0000000000100d3b SP=0018:0000000000104f1c env->regs[R_EAX]=0000000000100010
EAX=00100010 EBX=001066c8 ECX=000003d5 EDX=000003d5
ESI=00000000 EDI=00000000 EBP=00104f38 ESP=00104f1c
EIP=00100d3b EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
CS =0010 00000000 ffffffff 00cf9a00 DPL=0 CS32 [-R-]
SS =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
DS =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
FS =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
GS =0018 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT= 696c6261 00006e65
IDT= 00000000 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000010 CCD=00104f10 CCO=ADDL
EFER=0000000000000000
check_exception old: 0x8 new 0xd
edit:
here is the github repo:
https://github.com/MadhavBh/FilterCoffee
apologies for the messy code :') Still very new at this.
You have the the granularity bits and limit bits being mashed into the same 4 bits. The second line should possibly read: