Page 1 of 1

InstallGDT function causes triple fault

Posted: Sat Feb 09, 2013 12:02 pm
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?

Re: InstallGDT function causes triple fault

Posted: Sat Feb 09, 2013 12:45 pm
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.

Re: InstallGDT function causes triple fault

Posted: Sat Feb 09, 2013 2:16 pm
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 ?? ()

Re: InstallGDT function causes triple fault

Posted: Sat Feb 09, 2013 3:13 pm
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)