Page 1 of 2

Triple fault when jumping to p-mode

Posted: Sun Jul 27, 2008 4:37 pm
by Revelation
Hi,

I'm having a brutal hand to CPU fight, which I am losing at the moment. I'm trying to jump to p-mode, but something with the gtd is messed up so bochs triple faults and resets.

This piece of code comes from the second stage boot loader, which is loaded at 0x1000:0x0000.

Code: Select all

PMODE:
; go to protected mode
	cli
	lgdt [gdtr + 0x1000]		;Load GDT
        mov eax, cr0
        or eax, 1
        mov cr0, eax

	jmp  0x08 : PMODEMAIN + 0x1000 ; <- fatal

; running in pmode now
[BITS 32]
PMODEMAIN:

        ; setup segements
        mov ax, 10h       
        mov ds, ax              ; Move a valid data segment into the data segment register
        mov ss, ax              ; Move a valid data segment into the stack segment register
        mov esp, 090000h        ; Move the stack pointer to 090000h

Code: Select all

[BITS 16]
gdt:                    ; Address for the GDT

gdt_null:               ; Null Segment
        dd 0
        dd 0

gdt_code:               ; Code segment, read/execute, nonconforming
        dw 0FFFFh
        dw 0
        db 0
        db 10011010b
        db 11001111b
        db 0

gdt_data:               ; Data segment, read/write, expand down
        dw 0FFFFh
        dw 0
        db 0
        db 10010010b
        db 11001111b
        db 0

gdt_end:           

gdtr:                       ; GDT descriptor
        dw gdt_end - gdt - 1    ; Limit (size)
        dd gdt + 0x1000                ; Address of the GDT
And this is the output from bochs:

Code: Select all

00039059350i[BIOS ] Booting from 0000:7c00
00041317348e[CPU  ] fetch_raw_descriptor: GDT: index (f)1 > limit (0)
00041317348i[CPU  ] CPU is in protected mode (active)
00041317348i[CPU  ] CS.d_b = 16 bit
00041317348i[CPU  ] SS.d_b = 16 bit
00041317348i[CPU  ] | EAX=60000011  EBX=00000007  ECX=00000027  EDX=00000fff
00041317348i[CPU  ] | ESP=0000fff7  EBP=00000000  ESI=ffff041a  EDI=00080005
00041317348i[CPU  ] | IOPL=0 id vip vif ac vm RF nt of df if tf sf zf af PF cf
00041317348i[CPU  ] | SEG selector     base    limit G D
00041317348i[CPU  ] | SEG sltr(index|ti|rpl)     base    limit G D
00041317348i[CPU  ] |  CS:0100( 0004| 0|  0) 00001000 0000ffff 0 0
00041317348i[CPU  ] |  DS:0100( 0005| 0|  0) 00001000 0000ffff 0 0
00041317348i[CPU  ] |  SS:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00041317348i[CPU  ] |  ES:2000( 0005| 0|  0) 00020000 0000ffff 0 0
00041317348i[CPU  ] |  FS:0100( 0005| 0|  0) 00001000 0000ffff 0 0
00041317348i[CPU  ] |  GS:0100( 0005| 0|  0) 00001000 0000ffff 0 0
00041317348i[CPU  ] | EIP=0000002f (0000002f)
00041317348i[CPU  ] | CR0=0x60000011 CR1=0 CR2=0x00000000
00041317348i[CPU  ] | CR3=0x00000000 CR4=0x00000000
00041317348i[CPU  ] >> jmp far 0008:1034 : EA34100800
00041317348e[CPU  ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
00041317348i[SYS  ] bx_pc_system_c::Reset(SOFTWARE) called
00041317348i[CPU  ] cpu software reset
Help is very welcome :wink:

Re: Triple fault when jumping to p-mode

Posted: Sun Jul 27, 2008 5:00 pm
by suthers
Firstly instead of adding 0x10000 to everything, why don't you use an org statement?
Also, the only thing I found wrong with your GDT is that it isn't alligned, so add an 'align 4' statement before the table...
All I could think off is that for the moment, but I only had time for a 10 second look...
Jules

edit: and though it isn't necessary, it might be a good idea to enable the A20 gate....

Re: Triple fault when jumping to p-mode

Posted: Sun Jul 27, 2008 6:40 pm
by suthers
BTW are you sure that the fault is cause by the jump, because your EIP is 0x2f (really low) and you are in protected mode, did you maybe forget to add 0x10000 to something once you where in pmode?
Oh and also, you should be adding 0x10000 to stuff, not 0x1000, because their referenced as linear addresses not offset:address...
Jules

Re: Triple fault when jumping to p-mode

Posted: Sun Jul 27, 2008 10:25 pm
by 01000101
I agree with suthers on the 0x10000 not 0x1000 offset as you have told your second stage to load at 0x1000:0x0000 which equals 0x10000 linear address. But I do not believe it is the jump that is directly causing the fault. I believe your GDT is not loaded properly. I think you are loading your GDT with the wrong offset (0x1000 and not 0x10000). Without the GDT being loaded right, your jump results in a limit breach as you would be jumping the 0xFFFF range as you are already located in the 0x10000 range.

Re: Triple fault when jumping to p-mode

Posted: Mon Jul 28, 2008 4:08 am
by Revelation
Thanks guys :)

By using the ORG 0x1000 and removing the 0x1000 elsewhere, the code now runs perfectly! I guess I must have gotten confused about the conversions from base to linear addresses. I loaded this second stage boot loader with es set to 0x0100, so that would make my "0x1000:0x0000" statement wrong.
edit: and though it isn't necessary, it might be a good idea to enable the A20 gate....
I already had that enabled, but I only copied the code relevant to the problem.

Re: Triple fault when jumping to p-mode

Posted: Tue Jul 29, 2008 4:34 am
by Mattx
Please help, similar problem I using VPC and I have an error : Unrecoverable error has been encountered, Restart now...
Where is the problem, I know my code can be stupid but I dont know what I;m missing... ? Please help

Bootsector code :

[BITS 16]
[org 0x7C00]

start:
mov ax,0x0010
mov es,ax
mov ax,0x0201
mov cx,2
xor dx,dx
xor bx,bx
int 0x13
jc bootError

unlock_A20:
in al,0x92
test al,2
jnz gotoKernel
or al,2
out 0x92,al
gotoKernel:
jmp 0x0010:0000

bootError:
hlt

times 510 - ($ - start) db 0
db 0x55
db 0xAA


Kernel code : (in second file)

[org 0]
[BITS 16]

main16:
cli

xor ax,ax
mov ds,ax

lgdt[gdtr]

mov eax,cr0
or eax,1
mov cr0,eax

jmp 0x08:start32
gdt:

gdt_null:
dd 0
dd 0

gdt_code:
dw 0FFFFh
dw 0
db 0
db 10011010b
db 11001111b
db 0

gdt_data:
dw 0FFFFh
dw 0
db 0
db 10010010b
db 11001111b
db 0

gdt_end:

gdtr:
dw gdt_end - gdt - 1
dd gdt

[BITS 32]
start32:
mov ax, 10h
mov ss,ax
mov ds,ax
mov ds,ax
xor edx,edx
mov fs,dx
mov es,dx
mov gs,dx
mov esp,090000h

main32:
jmp main32

Re: Triple fault when jumping to p-mode

Posted: Tue Jul 29, 2008 7:06 am
by suthers
Ok firstly, you should create a separate subject for this, but anyway...
USE CODE TAGS DAMN IT
Ok, so back to the code..
I can see a few errors, firstly, you do realise that not that fast A20 doesn't always work, check this out for the proper way...
Secondly, your loading your kernel at 0x100, you realise this is in the middle of the IVT?
Probably not the best place to load...
secondly, your making all the extra segments null... don't
Couldn't see anything else at a glance...

Jules

Re: Triple fault when jumping to p-mode

Posted: Wed Jul 30, 2008 12:07 am
by Mattx
I think it is very similar topic ( pmode) so i just asked people, ad i will consider using code tags :) Sorry but you haven't helped me a lot becouse probably I know much less then you. I know there is a lot of tutorails on web but I read many and nothing working properly even the simplest examples... I was trying trying trying and there is always the same error... If you could turn on a light and say me something like this : dont do like this : nop nop mov ax,dx do like this mov dx,ax,nop nop for example :) Thanks for answer.

Re: Triple fault when jumping to p-mode

Posted: Wed Jul 30, 2008 12:19 am
by 01000101
the only thing I can think of besides the above-mentioned is that you might need to set your ORG to 0x100

Re: Triple fault when jumping to p-mode

Posted: Wed Jul 30, 2008 12:37 am
by stephenj
I could be mistaken, but I think NASM only allows one [ORG ...] per file.

Re: Triple fault when jumping to p-mode

Posted: Wed Jul 30, 2008 12:42 am
by 01000101
there are two files, the bootloader and the kernel file.

Re: Triple fault when jumping to p-mode

Posted: Wed Jul 30, 2008 2:41 am
by Mattx
Okey but why org 0x100 ? And where ? Another thing is fact that when I add [bits 16] or [bits 32] virtual pc does nothing why ?

Re: Triple fault when jumping to p-mode

Posted: Wed Jul 30, 2008 3:49 am
by Mattx
Okey here is the simplest code, only bootloader can any body tell me why after far jump it crashes on mov ss,ax ?

Code: Select all

use16
org 0x7C00

start:	
		cli 
		
		xor ax,ax
		mov ds,ax
		
		lgdt[gdtr]
		
		mov eax,cr0
		or eax,1
		mov cr0,eax
		
		jmp 0x08:pmode
		
use32
pmode:
		mov ax,0x08	
		mov ds,ax
		mov ss,ax
		mov esp,090000h
		
@@loop: 
		jmp @@loop
		
		
		
bootError:
			hlt
gdt: 

gdt_null: 
dd 0
dd 0

gdt_code: 
dw 0FFFFh
dw 0
db 0
db 10011010b
db 11001111b
db 0

gdt_data: 
dw 0FFFFh
dw 0
db 0
db 10010010b
db 11001111b
db 0

gdt_end: 

gdtr: 
dw gdt_end - gdt - 1 
dd gdt

		
times 510 - ($ - start) db 0
db 0x55
db 0xAA

Re: Triple fault when jumping to p-mode

Posted: Thu Jul 31, 2008 2:29 am
by Combuster
First of all: Virtual PC has no debugging facilities. Run your OS in bochs to get a more sensible message than just "the kernel crashed"

That and you're loading a code descriptor into a data segment.

Re: Triple fault when jumping to p-mode

Posted: Thu Jul 31, 2008 5:35 am
by zhak
suthers wrote:Ok firstly, you should create a separate subject for this, but anyway...
I decided to post in this subj because the problem seems to be the same. Why create a number of identical subjects if all can be discussed in one?
my code/data is loaded at 0x1000:0 under Bochs

Code: Select all

;basic real mode init and etc.
;. . . 
.enter_unreal_mode:
    xor eax, eax
    mov ax, cs
    shl eax, 4
    add eax, gdt
    mov dword [gdtr+2], eax
    mov eax, gdtr
    cli
    lgdt [eax]
    mov eax, cr0
    or al, 1
    mov cr0, eax
    mov ax, 08
    mov gs, ax
    mov fs, ax
    mov eax, cr0
    and al, 0FEh
    mov cr0, eax
    sti
    mov byte [gs:0B8000h], '+'

;......
gdt dq 0,00CF92000000FFFFh
gdtr dw gdtr-gdt-1,0,0
this piece of code works well! but when I try to add code descriptor

Code: Select all

gdt dq 0,00CF9A000000FFFFh,00CF92000000FFFFh
gdtr dw gdtr-gdt-1,0,0
and jmp to pmode

Code: Select all

    lgdt [eax]
    mov eax, cr0
    or al, 1
    mov cr0, eax
    jmp 08:pmode

use32
pmode:
    mov ax, 10h
    mov gs, ax
    mov fs, ax
    mov byte [gs:0B8000h], '+'
    jmp $
processor is reset. it seems that jmp 08:pmode jumps to 0x11C linear, but should jump to 0x1011C.
jmp 08:pmode+10000h is out of range. how to fix this?