Triple Fault when activating protected mode

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
Realtime
Posts: 13
Joined: Mon Sep 22, 2014 10:38 am

Triple Fault when activating protected mode

Post 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 $
User avatar
b.zaar
Member
Member
Posts: 294
Joined: Wed May 21, 2008 4:33 am
Location: Mars MTC +6:00
Contact:

Re: Triple Fault when activating protected mode

Post 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.
"God! Not Unix" - Richard Stallman

Website: venom Dev
OS project: venom OS
Hexadecimal Editor: hexed
Realtime
Posts: 13
Joined: Mon Sep 22, 2014 10:38 am

Re: Triple Fault when activating protected mode

Post 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
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Triple Fault when activating protected mode

Post 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...
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Realtime
Posts: 13
Joined: Mon Sep 22, 2014 10:38 am

Re: Triple Fault when activating protected mode

Post 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
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Triple Fault when activating protected mode

Post 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.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Triple Fault when activating protected mode

Post 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
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
CelestialMechanic
Member
Member
Posts: 52
Joined: Mon Oct 11, 2010 11:37 pm
Location: Milwaukee, Wisconsin

Re: Triple Fault when activating protected mode

Post 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.
Microsoft is over if you want it.
Realtime
Posts: 13
Joined: Mon Sep 22, 2014 10:38 am

Re: Triple Fault when activating protected mode

Post by Realtime »

then how should i do it ? can u plz help me ?(and plz not just code but also explanation as well)
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: Triple Fault when activating protected mode

Post 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)
Realtime
Posts: 13
Joined: Mon Sep 22, 2014 10:38 am

Re: Triple Fault when activating protected mode

Post 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 ..
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: Triple Fault when activating protected mode

Post 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)
Realtime
Posts: 13
Joined: Mon Sep 22, 2014 10:38 am

Re: Triple Fault when activating protected mode

Post 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
Post Reply