Switching to protected mode triple faults

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.
gdtr

Switching to protected mode triple faults

Post by gdtr »

Hmmm....now this is strange. I compiled my bootloader on my redhat8.0 box, with name. I recently switched to Debian (latest unstable) amd now whem i try to compile the exact same code it compiles fine, then when i try booting with it, it triple faults in VMware. So i tried using my old boot.img file and it works fine, so im guessing its got something to do with debian/nasm. Can anyone help me solve this: here is the code:

; ---------------------------------------
; RDOS - Red Dragon Operating System
; Under Development By Ashleigh Wilson
; Contact: [email protected]
; ---------------------------------------
; Thx for help from the FritzOS Project
; ---------------------------------------

[ORG 0x7C00]                   ; Tells assembler where it is loaded in memory
jmp Start                     ; Jumps over BIOS parameter block

Start:                        ; Start of actual program
   mov [ floppy ], dl          ; Get the drive we booted from
   xor ax, ax                   ; Update segment register
   mov ds,ax                  ; Update segment register
   jmp BootProg               ; Jump to where the boot program is running

BootProg:                     ; Boot program is running here
   mov ax, 3                  ; Sets the text mode to 3
   int 10h                     ; Screen is now cleared

ResetFloppy:                  ; Resets the drive
   mov ax, 0x00               ; BIOS function to reset floppy the drive
   mov dl, [ floppy ]         ; Selects the floppy drive we used to boot
   int 13h                     ; Execute function, floppy drive reset
   jc ResetFloppy               ; Try re-setting floppy if we failed

ReadFloppy:                     ; Reads the floppy drive
   mov bx, 9000h               ; Loads the OS at 9000h
   mov ah, 0x02               ; Load the disk data to ES:BX
   mov al, 17                  ; Load two floppy head fulls of data

   mov ch, 0                  ; First cylinder
   mov cl, 2                  ; Second sector, dont want to load the bootsector
   mov dh, 0                  ; Use the first floppy head
   mov dl, [ floppy ]         ; Selects floppy drive we used to boot
   int 13h                     ; Execute function, reading floppy drive
   jc ReadFloppy               ; Try reading the floppy again if we failed

SecondHead:                     ; Read the second head of the floppy drive
   mov al, 17                  ; The Second Head
   inc dh                     ; Set it to the second head
   int 13h                     ; Execute function, reading floppy drive (second head)
   jc SecondHead               ; Try reading the floppy again if we failed

SetPMode:                     ; Here we go, setting Protected-Mode
   cli                        ; Stops BIOS interrupts
   lgdt [ GDTR ]               ; Load a temporary GDT to set up PMode selectors and to allow kernels stack to function properly
   
   mov eax, cr0               ; Starting to set Protected-Mode
   or eax, 1                  ; Still Setting Protected-Mode
   mov cr0, eax               ; Protected-Mode bit finally set, we are now in protected mode! <- ACTUALLY WERE NOT, COZ THIS LINE MAKES IT TRIPLE FAULT!

   jmp dword CodeSel:PMode      ; Set the CPU registers correctly

[bits 32]                     ; Protected-Mode here
PMode:
   mov eax, DataSel            ; Put the data selector into eax for setting up ther registers
   mov ss, eax                  ; ss = Data selector
   mov ds, eax                  ; ds = Data selector
   mov es, eax                  ; es = Data selector
   mov fs, eax                  ; fs = Data selector
   mov gs, eax                  ; gs = Data selector

   mov ax, 0x10               ; Setting up Protected-Mode stack
   mov ss, ax                  ; Setting up Protected-Mode stack
   mov esp, 0xFFFF            ; Need to make stack larger sometime

   mov dl, [ floppy ]         ; Select the motor we booted from
   mov edx, 0x3f2               ; Select the function to stop the motor
   mov al, 0x0c               ; Select the function to stop the motor
   out dx, al                  ; Stop the motor (In protected mode, so BIOS cant stop motor, must to manually)

   jmp dword CodeSel:9000h      ; Jump to the kernel...we previously loaded it at 9000h, so jump there

   %include "./gdt.asm"         ; Include the General Descriptor Table

   times 509-($-$$) db 0      ; Makes the bootsector 512 bytes. (The drive takes 1 byte and 0xAA55 takes 2 bytes)
   dw 0x0AA55                  ; BIOS see's this as a bootable disk, so load the boot sector

   floppy db 0                  ; Used to store which drive we booted from

; ---------------------------------------
; End of BOOT.ASM
; ---------------------------------------
jrfritz

Re:Switching to protected mode triple faults

Post by jrfritz »

I've seen you've used some FritzOS code...and here is the fixed bootsector ending:


floppy db 0 ; Used to store which drive we booted from

times 510-($-$$) db 0 ; Makes the bootsector 512 bytes. (0xAA55 takes 2 bytes)
dw 0x0AA55 ; BIOS see's this as a bootable disk, so load the boot sector


Actually, it is most likely that the jmp dword CodeSel:PMode causes the tripple fault....i'm still looking
gdtr

Re:Switching to protected mode triple faults

Post by gdtr »

Thx Tom: should have actually pasted my newest code coz i actually fixed that, oh well ::) Im sure its setting PMode that triple faults because I have commented out the jmp CodeSel:PMode, and it still triple faults. I comment the: mov cr0, eax and it not longer triple faults. Any ideas? Thx
gdtr

Re:Switching to protected mode triple faults

Post by gdtr »

ok, i just tried Phoenix OS bootloader (pskyboy's). It also triple faults at the same place. Can i ask what version of nasm u guys are using? I cant seem to install the latest version of nasm, because it cant find "makeinfo" nor can i find it on the net. ill have to search for it again 2moro. Also, does anyone else use Debian linux here? and if they do have they had any problems? ???
jrfritz

Re:Switching to protected mode triple faults

Post by jrfritz »

I use MDK 9....I have makeinfo but it'll probly won't work on your system...
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Switching to protected mode triple faults

Post by Pype.Clicker »

what i wonder is where do GDTR hides ...
hanoi

Re:Switching to protected mode triple faults

Post by hanoi »

hmpf...maybe it'd be good if we see the gdt
gdtr

Re:Switching to protected mode triple faults

Post by gdtr »

Ok heres the GDT:

; ---------------------------------------
; RDOS - Red Dragon Operating System
; Under Development By Ashleigh Wilson
; Contact: [email protected]
; ---------------------------------------
; Thx for help from the FritzOS Project
; ---------------------------------------

; Below is the General Descriptor Table, this tells the computer where all the selectors (segments) are
; This is just a temporary GDT. We will load a better GDT in the second stage kernel
GDTR
   dw GDTEnd-1
   dd GDT
GDT
   nullsel equ $-GDT
GDT0
   dd 0
   dd 0
CodeSel equ $-GDT
   dw 0ffffh
   dw 0
   db 0
   db 09ah
   db 0cfh
   db 0h
DataSel equ $-GDT
   dw 0ffffh
   dw 0h
   db 0h
   db 092h
   db 0cfh
   db 0
GDTEnd


Although i dont think its the GDT thats the problem, coz ive tried different bootloaders and each one i compile doesnt seem to work :(
jrfritz

Re:Switching to protected mode triple faults

Post by jrfritz »

Your GDT looks fine...
Curufir

Re:Switching to protected mode triple faults

Post by Curufir »

Code: Select all

dw GDTEnd-1
I'm not going to repeat what I've said in other threads, but this is wrong. Fixing it might not fix your triple fault problem, but it sure as hell would be a good idea to do it anyway.
gdtr

Re:Switching to protected mode triple faults

Post by gdtr »

Can someone please send a version of their OS (for linux) to my email. Allong with an install script to install it. So i can make sure im compiling everything correctly. Thx

Email: [email protected]
jrfritz

Re:Switching to protected mode triple faults

Post by jrfritz »

Then HOW do I fix it? Could you show the CORRECT code then?
jrfritz

Re:Switching to protected mode triple faults

Post by jrfritz »

Does FritzOS work?
gdtr

Re:Switching to protected mode triple faults

Post by gdtr »

[attachment deleted by admin]
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Switching to protected mode triple faults

Post by Pype.Clicker »

gdtr wrote:
[ORG 0x7C00] ; Tells assembler where it is loaded in memory
jmp Start ; Jumps over BIOS parameter block

Start: ; Start of actual program
mov [ floppy ], dl ; Get the drive we booted from
xor ax, ax ; Update segment register
mov ds,ax ; Update segment register
jmp BootProg ; Jump to where the boot program is running
I envision a problem coming from the boot initial segments values.
You should do ds:=0 before you perform mov [floppy],dl.
If for some unknown reason you move to a machine which doesn't
zero data segments before it calls the boot sector, then your reading commands will behave weirdly ...

Code: Select all

BootProg:                     ; Boot program is running here
   mov ax, 3                  ; Sets the text mode to 3
   int 10h                     ; Screen is now cleared
Sorry, but where's your stack ? calling interrupts without a valid stack will surely lead you to unknown states...

Code: Select all

ReadFloppy:                     ; Reads the floppy drive
   mov bx, 9000h               ; Loads the OS at 9000h
   mov ah, 0x02               ; Load the disk data to ES:BX
   mov al, 17                  ; Load two floppy head fulls of data
Here again, you didn't set ES up and you use it ...

Code: Select all

   times 509-($-$$) db 0      ; Makes the bootsector 512 bytes. (The drive takes 1 byte and 0xAA55 takes 2 bytes)
   dw 0x0AA55                  ; BIOS see's this as a bootable disk, so load the boot sector

   floppy db 0                  ; Used to store which drive we booted from
And, you should definitely put your "floppy" variable *before* the 55AA tag, or define it as resb 1 instead of db so that it doesn't appear in the generated boot sector.
Post Reply