InstallGDT function causes triple fault

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
User avatar
BenjiWiebe
Posts: 20
Joined: Thu Feb 07, 2013 9:47 pm
Location: Durham, Kansas
Contact:

InstallGDT function causes triple fault

Post by BenjiWiebe »

I am using GRUB2 bootloader in my image.iso file.
GRUB2 boots my C kernel, and my kernel.c calls the asm function InstallGDT, defined as follows:

Code: Select all

global InstallGDT 
InstallGDT:
 
        cli                             ; clear interrupts
        pusha                           ; save registers
        lgdt    [toc]                   ; load GDT into GDTR
        sti                             ; enable interrupts
        popa                            ; restore registers
        ret                             ; All done!
 
;*******************************************
; Global Descriptor Table (GDT)
;*******************************************
 
gdt_data: 
        dd 0                            ; null descriptor
        dd 0 
 
; gdt code:                             ; code descriptor
        dw 0FFFFh                       ; limit low
        dw 0                            ; base low
        db 0                            ; base middle
        db 10011010b                    ; access
        db 11001111b                    ; granularity
        db 0                            ; base high
 
; gdt data:                             ; data descriptor
        dw 0FFFFh                       ; limit low (Same as code)
        dw 0                            ; base low
        db 0                            ; base middle
        db 10010010b                    ; access
        db 11001111b                    ; granularity
        db 0                            ; base high
 
end_of_gdt:
toc: 
        dw end_of_gdt - gdt_data - 1    ; limit (Size of GDT)
        dd gdt_data                     ; base of GDT
I am calling it like so:

Code: Select all

asm("cli");
InstallGDT();
But when I do this, I get a triple fault, Only if I am using Bochs, QEMU works fine.

Does anyone have any ideas or things I should try?
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: InstallGDT function causes triple fault

Post by xenos »

BenjiWiebe wrote:Does anyone have any ideas or things I should try?
Since you're already using Bochs: Read the log file. Figure out the instruction that causes the triple fault. Figure out the reason. Debug the problem.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
BenjiWiebe
Posts: 20
Joined: Thu Feb 07, 2013 9:47 pm
Location: Durham, Kansas
Contact:

Re: InstallGDT function causes triple fault

Post by BenjiWiebe »

OK, I have figured out (kind of) what is going on. It triple faults on the 'ret' instruction, in InstallGDT.
Here is relevant output from gdb, debugging my kernel on bochs. (The ?? is where the cpu triple faults)

Code: Select all

Breakpoint 2, 0x00100110 in InstallGDT ()
(gdb) disassemble 
Dump of assembler code for function InstallGDT:
=> 0x00100110 <+0>:     cli    
   0x00100111 <+1>:     pusha  
   0x00100112 <+2>:     lgdtd  ds:0x100134
   0x00100119 <+9>:     sti    
   0x0010011a <+10>:    popa   
   0x0010011b <+11>:    ret    
End of assembler dump.
(gdb) stepi
0x00100111 in InstallGDT ()
(gdb) stepi
0x00100112 in InstallGDT ()
(gdb) stepi
0x00100119 in InstallGDT ()
(gdb) stepi
0x0010011a in InstallGDT ()
(gdb) stepi
0x0010011b in InstallGDT ()
(gdb) stepi
0x0000fff0 in ?? ()
User avatar
BenjiWiebe
Posts: 20
Joined: Thu Feb 07, 2013 9:47 pm
Location: Durham, Kansas
Contact:

Re: InstallGDT function causes triple fault

Post by BenjiWiebe »

I don't know why, but now when I use bochs SVN version, it works like a charm. At least, the GDT code works. (The IDT doesn't, but I think I know why)
Post Reply