Page 1 of 1

Triple Fault when activating protected mode

Posted: Mon Sep 22, 2014 10:51 am
by Realtime
im trying to pass to protected mode and i use whats below but i get a triple fault , plz help me :

Code: Select all

org 0x500
bits 16

_init:
	cli				; clear interrupts
	xor	ax, ax			; null segments
	mov	ds, ax
	mov	es, ax
	mov	ax, 0x9000			; stack begins at 0x0000-0xffff
	mov	ss, ax
	mov	sp, 0xFFFF
	sti
	call InstallGDT
	call _EnableA20
	
	cli				; clear interrupts

	mov	eax, cr0		; set bit 0 in cr0--enter pmode
	or	eax, 1
	mov	cr0, eax

	jmp	0x8:PMODE	; far jump to fix CS. Remember that the code selector is 0x8!
	

_EnableA20:

        cli

        call    a20wait
        mov     al,0xAD
        out     0x64,al

        call    a20wait
        mov     al,0xD0
        out     0x64,al

        call    a20wait2
        in      al,0x60
        push    eax

        call    a20wait
        mov     al,0xD1
        out     0x64,al

        call    a20wait
        pop     eax
        or      al,2
        out     0x60,al

        call    a20wait
        mov     al,0xAE
        out     0x64,al

        call    a20wait
        sti
        ret

a20wait:
        in      al,0x64
        test    al,2
        jnz     a20wait
        ret


a20wait2:
        in      al,0x64
        test    al,1
        jz      a20wait2
        ret

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)10:56 AM 7/8/2007
	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

		
bits 32

PMODE:

mov	ax, 0x10	; set data segments to data selector (0x10)
mov	ds, ax
mov	ss, ax
mov	es, ax
mov	esp, 90000h		; stack begins from 90000h

_main:
jmp $

Re: Triple Fault when activating protected mode

Posted: Mon Sep 22, 2014 11:18 am
by b.zaar
Try running it in bochs, you will have a log file to look at and use the debugger if you have it enabled.

Re: Triple Fault when activating protected mode

Posted: Mon Sep 22, 2014 11:53 am
by Realtime
b.zaar wrote:Try running it in bochs, you will have a log file to look at and use the debugger if you have it enabled.
i get this but i dunno what it means :

Code: Select all

00015055744i[CPU0  ] 0x0000000000008121>> jmpf 0x0008:058e : EA8E050800
00015055744e[CPU0  ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
ok i found the problem :
i forgot to set es:bx to 0x0000:0x0500 so the program wasnt loaded there it was loaded to 0x07c0:0x0500 .. i could be so stupid

Re: Triple Fault when activating protected mode

Posted: Mon Sep 22, 2014 12:46 pm
by SpyderTL
Realtime wrote:ok i found the problem :
i forgot to set es:bx to 0x0000:0x0500 so the program wasnt loaded there it was loaded to 0x07c0:0x0500 .. i could be so stupid
I wouldn't feel too bad. I'm pretty sure this exact bug has happened to every single OS (or at least Boot Loader) developer on this site...

Re: Triple Fault when activating protected mode

Posted: Mon Sep 22, 2014 1:31 pm
by Realtime
SpyderTL wrote:
Realtime wrote:ok i found the problem :
i forgot to set es:bx to 0x0000:0x0500 so the program wasnt loaded there it was loaded to 0x07c0:0x0500 .. i could be so stupid
I wouldn't feel too bad. I'm pretty sure this exact bug has happened to every single OS (or at least Boot Loader) developer on this site...
What about this .. im getting another bug -.- .. kernel doesnt seem to be doing anything after that far jump .. nothing happens no matter what i write there

Re: Triple Fault when activating protected mode

Posted: Mon Sep 22, 2014 2:46 pm
by SpyderTL
You can step through it in Bochs (one instruction at a time) and see where your code is going, and what it is executing. That should give you a clue.

Re: Triple Fault when activating protected mode

Posted: Mon Sep 22, 2014 3:04 pm
by Combuster
I do see that your stack (as per your first post) is located in reserved memory, and that you are trashing in BIOS space

Re: Triple Fault when activating protected mode

Posted: Mon Sep 22, 2014 7:03 pm
by CelestialMechanic
Also, setting SP to 0xFFFF is no help at all.

Later in protected mode he follows the assignment to the SS selector with an assignment to ES and then the assignment to ESP, again not likely to be helpful.

Re: Triple Fault when activating protected mode

Posted: Tue Sep 23, 2014 7:59 am
by Realtime
then how should i do it ? can u plz help me ?(and plz not just code but also explanation as well)

Re: Triple Fault when activating protected mode

Posted: Tue Sep 23, 2014 10:14 am
by JAAman
ok, first your initial stack setup is in the wrong part of memory:
you are setting the stack to 9000:FFFF -- this area is usually reserved for the BIOS, so that it can use this memory -- check the memory usage map using the BIOS functions or place it in an area that is not likely to be used
also, you should always set eSP to an even number -- for example, instead of setting it to 0xFFFF set it to 0 (the CPU will subtract before pushing, so the first value pushed will actually be located at 0xFFFE -- which is probably what you wanted anyway)

the second problem is with your PMode stack setting (although this isn't really a problem)
always set ESP immediately after setting SS... the CPU automatically prevents interrupts for 1 instruction after setting SS to give you time to set eSP also, but you are setting another register in between, which means, you set SS, interrupts are disabled while you set ES, then they are enabled again before you set ESP -- and still have an invalid stack (this really isn't an issue right now, since you don't have a valid IDT set up you will triple-fault anyway, but its good practice)

Re: Triple Fault when activating protected mode

Posted: Tue Sep 23, 2014 12:04 pm
by Realtime
JAAman wrote:ok, first your initial stack setup is in the wrong part of memory:
you are setting the stack to 9000:FFFF -- this area is usually reserved for the BIOS, so that it can use this memory -- check the memory usage map using the BIOS functions or place it in an area that is not likely to be used
also, you should always set eSP to an even number -- for example, instead of setting it to 0xFFFF set it to 0 (the CPU will subtract before pushing, so the first value pushed will actually be located at 0xFFFE -- which is probably what you wanted anyway)

the second problem is with your PMode stack setting (although this isn't really a problem)
always set ESP immediately after setting SS... the CPU automatically prevents interrupts for 1 instruction after setting SS to give you time to set eSP also, but you are setting another register in between, which means, you set SS, interrupts are disabled while you set ES, then they are enabled again before you set ESP -- and still have an invalid stack (this really isn't an issue right now, since you don't have a valid IDT set up you will triple-fault anyway, but its good practice)
seems complicated .. i guess its better if i start with an existing bootsloader , and do my own after i learn ..

Re: Triple Fault when activating protected mode

Posted: Wed Sep 24, 2014 7:56 am
by JAAman
Its not really complicated

you just need to make sure you don't put your stack in unavailable memory -- just like you wouldn't put it in ROM, or in the IDT, you can't put it in space already being used by another program -- you can ask the BIOS where available memory is, or (simpler) just use an area of memory that you know isn't already being used (most people place it directly below the boot sector at 0:7C00)

Re: Triple Fault when activating protected mode

Posted: Fri Sep 26, 2014 11:42 am
by Realtime
JAAman wrote:Its not really complicated

you just need to make sure you don't put your stack in unavailable memory -- just like you wouldn't put it in ROM, or in the IDT, you can't put it in space already being used by another program -- you can ask the BIOS where available memory is, or (simpler) just use an area of memory that you know isn't already being used (most people place it directly below the boot sector at 0:7C00)
well i started to understand thacks to brokenthorn (osdev is also helpful however its more of based on advanced things rather than teaching beginners) .. i think i can make my own soon ..

the reason i want to use my own is cuz i got great fs ideas .. most orginal one is called MOFS .. tho i dont think ill use that as its bad