Page 1 of 2

[solved] my OS get restarted when jmp to PM

Posted: Sun Mar 06, 2011 11:34 am
by Teehee
i attached the entire code in a single file, please help, i don't know why this is happening.

PS: i'm using Bochs and Fasm.

[edit:] subject changed.

Re: help: my OS get restarted when jmp to PM

Posted: Sun Mar 06, 2011 3:50 pm
by Tosi
What error message, if any, is in the Bochs log? It should say something about whether you have valid descriptors or not.
Does anything show up on the screen?
Don't just dump a bunch of code on us and expect us to do your debugging for you.

Re: help: my OS get restarted when jmp to PM

Posted: Sun Mar 06, 2011 6:11 pm
by Teehee
Hi. This is bochs output log (only error and panic events enabled):

Code: Select all

00014091086e[WGUI ]00014600000e[WGUI ] Sim client size(720, 333) != stretched size(720, 400)!
00028183098e[WGUI ] Sim client size(726, 432) != stretched size(1280, 1024)!
00028183098e[WGUI ] Sim client size(1276, 1020) != stretched size(1280, 1024)!
00028184021e[CPU0 ] jump_protected: gate type 3 unsupported
00028184021e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x0d)
00028184021e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x08)
00028184021e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
00028800000e[WGUI ] Sim client size(720, 333) != stretched size(720, 400)!
00035600000p[WGUI ] >>PANIC<< POWER button turned off.
And this is the log of a test i'm doing to try to solve this problem (just boot and PM change):

Code: Select all

00014132869e[CPU0 ] jump_protected: gate type 0 unsupported
00014132869e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x0d)
00014132869e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x08)
00014132869e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
00015400000p[WGUI ] >>PANIC<< POWER button turned off.

Re: help: my OS get restarted when jmp to PM

Posted: Sun Mar 06, 2011 6:22 pm
by Dario

Code: Select all

00014132869e[CPU0 ] jump_protected: gate type 0 unsupported
Can you read?

Either way, your GDT is in mess...beside the binary and hexadecimal notation you could also use decimal to make things even worse. Be consistent.

Re: help: my OS get restarted when jmp to PM

Posted: Sun Mar 06, 2011 6:37 pm
by Teehee
this is my gdt:

Code: Select all

align 4
  gdtr: dw gdt_size ; limit
        dd gdt      ; base
align 4
   gdt: dq 0
        db 0xFF,0xFF, 0x00,0x00,0x00, 1001'1010b, 1100'1111b, 0x00 ; 0x08 code segment selector
        db 0xFF,0xFF, 0x00,0x00,0x00, 1001'0010b, 1100'1111b, 0x00 ; 0x10 data segment selector
   gdt_size = $-gdt-1     
whats wrong with it? i changed to many values and got the same problem.

Re: help: my OS get restarted when jmp to PM

Posted: Sun Mar 06, 2011 11:43 pm
by Combuster
- Read enough sectors.
- Use DL from the bios
- Your linked and execution addresses do not match.

Re: help: my OS get restarted when jmp to PM

Posted: Mon Mar 07, 2011 12:28 am
by DavidCooper
Teehee wrote:this is my gdt:

Code: Select all

align 4
  gdtr: dw gdt_size ; limit
        dd gdt      ; base
align 4
   gdt: dq 0
        db 0xFF,0xFF, 0x00,0x00,0x00, 1001'1010b, 1100'1111b, 0x00 ; 0x08 code segment selector
        db 0xFF,0xFF, 0x00,0x00,0x00, 1001'0010b, 1100'1111b, 0x00 ; 0x10 data segment selector
   gdt_size = $-gdt-1     
whats wrong with it? i changed to many values and got the same problem.
I don't use assembler so I don't know how the rest of the above works, but your actual GDT values are fine.

Re: help: my OS get restarted when jmp to PM

Posted: Mon Mar 07, 2011 4:50 am
by Teehee
maybe its something here:

Code: Select all

        mov ax,0x0050
        mov bx,0x0000
        mov es,ax
        mov ah,0x02
        mov al,2      ; number of sectors to read (1-128 dec.)
        mov ch,1
        mov cl,2      ; sector number (1-17 dec.)
        mov dx,0
        int 0x13      ; http://www.stanislavs.org/helppc/int_13-2.html
?

Re: help: my OS get restarted when jmp to PM

Posted: Mon Mar 07, 2011 5:03 am
by Teehee
here the test i'm doing:

Code: Select all

format binary as 'img'

use16
org 0
        jmp 0x07C0:@f
    @@: mov ax,cs
        mov ds,ax
        mov ss,ax
        mov sp,512-1-2

    @@: mov ah,0           ; Reset floppy disk function
        mov dl,0           ; drive 0 = floppy drive
        int 0x13           ;
        jc  @b             ; CF: 1 error, 0 ok

        mov ax,0x0050
        mov bx,0x0000
        mov es,ax
        mov ah,0x02
        mov al,1      ; number of sectors to read (1-128 dec.)
        mov ch,0      ; track/cylinder number (0-1023 dec.)
        mov cl,2      ; sector number (1-17 dec.)
        mov dx,0
        int 0x13      ; http://www.stanislavs.org/helppc/int_13-2.html
        jc  @b

        jmp 0x0050:kernel16

        rb 510-($-$$)
        dw 0xAA55

use16
org 0

    kernel16:

        mov ax,cs
        mov ds,ax
        mov es,ax
        mov ss,ax
        mov sp,512

        mov ax,0x2401
        int 0x15

        cli
        lgdt [gdtr]

        mov eax,cr0
        or  eax,1
        mov cr0,eax
        jmp 0x08:kernel32

align 4
  gdtr: dw gdt_size ; limit
        dd gdt      ; base
align 4
   gdt: dq 0
        db 0xFF,0xFF, 0x00,0x00,0x00, 1001'1010b, 1100'1111b, 0x00 ; 0x08 code segment selector
        db 0xFF,0xFF, 0x00,0x00,0x00, 1001'0010b, 1100'1111b, 0x00 ; 0x10 data segment selector
   gdt_size = $-gdt-1

use32
;org 0
align 4

    kernel32:

        mov eax,0x10
        mov ds,ax
        mov es,ax
        mov gs,ax
        mov fs,ax
        mov ss,ax
        mov esp,0x00FFFFFF

        hlt
        jmp $

        times 512-($-kernel16) db 0           
edit: updating the code

Re: help: my OS get restarted when jmp to PM

Posted: Mon Mar 07, 2011 5:56 am
by Tosi
1) You use ORG 0 but you don't set CS to 0x07C0. This might not affect things, though.
2) Are you sure that drive 0 is the boot drive?
3) Are you sure that you are loading enough sectors?
4) You didn't check for errors after calling int 0x13. Floppy drives may return multiple errors, as the interrupt list says:
Errors on a floppy may be due to the motor failing to spin up quickly enough; the read should be retried at least three times, resetting the disk with AH=00h between attempts.
5) Not all BIOSes support int 15/AX=2401. If the return value in ah is 0x86, then the function is not supported and you have to set the A20 gate yourself. The easiest way is to use the keyboard controller, but I think there are a few others as well.
6) When you use "org 0" in the 32-bit part, it assumes that means that kernel32 is loaded at 0x000000000 (which is the real mode IDT) and tries to jump there. Try just removing the org 0 statement for now, and if that doesn't work, calculate the address to jump to at run time.

Re: help: my OS get restarted when jmp to PM

Posted: Mon Mar 07, 2011 6:50 am
by Teehee
Hi, Tosi.
Tosi wrote:1) You use ORG 0 but you don't set CS to 0x07C0. This might not affect things, though.
Oops, i did fix it.
2) Are you sure that drive 0 is the boot drive?
according to this, dl = 0 = A:.
3) Are you sure that you are loading enough sectors?
no :( these descriptions confuse me:
mov al,1 ; number of sectors to read (1-128 dec.)
mov ch,0 ; track/cylinder number (0-1023 dec.)
mov cl,2 ; sector number (1-17 dec.)
As it returns me the correct value of sectors read (al=1) so i presume yes.
4) You didn't check for errors after calling int 0x13.
i didnt put it into the code, but i checked it before, and it was ok. i will update the last code.
5) Not all BIOSes support int 15/AX=2401. If the return value in ah is 0x86, then the function is not supported and you have to set the A20 gate yourself.
yea, i checked for support. The int 15 is ok.
6) When you use "org 0" in the 32-bit part, it assumes that means that kernel32 is loaded at 0x000000000 (which is the real mode IDT) and tries to jump there. Try just removing the org 0 statement for now, and if that doesn't work, calculate the address to jump to at run time.
i removed it. Didn't work. how do i make that calc?

Re: help: my OS get restarted when jmp to PM

Posted: Mon Mar 07, 2011 6:58 am
by Chandra
Teehee wrote:here the test i'm doing:

Code: Select all

format binary as 'img'

use16
org 0
        jmp 0x07C0:@f
     @@:mov ax,cs
        mov ds,ax
        mov ss,ax
        mov sp,512-1-2

    @@: mov ax,0x0050
        mov bx,0x0000
        mov es,ax
        mov ah,0x02
        mov al,1      ; number of sectors to read (1-128 dec.)
        mov ch,0      ; track/cylinder number (0-1023 dec.)
        mov cl,2      ; sector number (1-17 dec.)
        mov dx,0
        int 0x13      ; http://www.stanislavs.org/helppc/int_13-2.html
        jc  @b

        jmp 0x0050:kernel16

        rb 510-($-$$)
        dw 0xAA55

use16
org 0

    kernel16:

        mov ax,cs
        mov ds,ax
        mov es,ax
        mov ss,ax
        mov sp,0x010

        mov ax,0x2401
        int 0x15

        cli
        lgdt [gdtr]

        mov eax,cr0
        or  eax,1
        mov cr0,eax
        jmp 0x08:kernel32

use32
;org 0
align 4

    kernel32:

        hlt
        jmp $

align 4
  gdtr: dw gdt_size ; limit
        dd gdt      ; base
align 4
   gdt: dq 0
        db 0xFF,0xFF, 0x00,0x00,0x00, 1001'1010b, 1100'1111b, 0x00 ; 0x08 code segment selector
        db 0xFF,0xFF, 0x00,0x00,0x00, 1001'0010b, 1100'1111b, 0x00 ; 0x10 data segment selector
   gdt_size = $-gdt-1

        times 512-($-kernel16) db 0
[edit:]updating the code.
When you make the far jump to protected mode code, you have to reload the segment registers with proper selector. This might fix your issue. Good Luck.

Re: help: my OS get restarted when jmp to PM

Posted: Mon Mar 07, 2011 7:05 am
by Teehee
Chandra wrote:When you make the far jump to protected mode code, you have to reload the segment registers with proper selector. This might fix your issue. Good Luck.
unfortunately, no. It does not [nor at least] hit kernel32: label line.

Re: help: my OS get restarted when jmp to PM

Posted: Mon Mar 07, 2011 7:49 am
by Tosi
As you have it now, you load the part after the bootloader to 0x0500 and set the code segment, so you use "org 0." Hence the address to jump to when you switch to protected mode would be 0x0500 + kernel32, because you are no longer using real mode segmentation. You could do something like:

Code: Select all

jmp (0x0500 + kernel32)

Re: help: my OS get restarted when jmp to PM

Posted: Mon Mar 07, 2011 8:08 am
by Teehee
Hi, Tosi. That didn't work.

But i changed the orgs to 0x7C00 and 0x0500, and removed the jmp 0x0050:kernel16 and it works now. But i didn't understand why:

Code: Select all

format binary as 'img'

use16
org 0x7C00

        mov ax,cs
        mov ds,ax
        mov ss,ax
        mov sp,512-1-2

    @@: mov ah,0           ; Reset floppy disk function
        mov dl,0           ; drive 0 = floppy drive
        int 0x13           ;
        jc  @b             ; CF: 1 error, 0 ok

        mov ax,0x0050
        mov bx,0x0000
        mov es,ax
        mov ah,0x02
        mov al,1      ; number of sectors to read (1-128 dec.)
        mov ch,0      ; track/cylinder number (0-1023 dec.)
        mov cl,2      ; sector number (1-17 dec.)
        mov dx,0
        int 0x13      ; http://www.stanislavs.org/helppc/int_13-2.html
        jc  @b

        jmp kernel16

        rb 510-($-$$)
        dw 0xAA55

use16
org 0x0500

    kernel16:

        mov ax,cs
        mov ds,ax
        mov es,ax
        mov ss,ax
        mov sp,512

        mov ax,0x2401
        int 0x15

        cli
        lgdt [gdtr]

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

        jmp 0x08:kernel32

align 4
  gdtr: dw gdt_size
        dd gdt
align 4
   gdt: dq 0
        db 0xFF,0xFF, 0x00,0x00,0x00, 1001'1010b, 1100'1111b, 0x00 ; 0x08 code segment selector
        db 0xFF,0xFF, 0x00,0x00,0x00, 1001'0010b, 1100'1111b, 0x00 ; 0x10 data segment selector
   gdt_size = $-gdt-1

use32
align 4

    kernel32:

        mov eax,0x10
        mov ds,ax
        mov es,ax
        mov gs,ax
        mov fs,ax
        mov ss,ax
        mov esp,0x00FFFFFF

        hlt
        jmp $

        times 512-($-kernel16) db 0 
I would like to know the reason.