system reboots

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
shahzad

system reboots

Post by shahzad »

in the following code when processor reaches to line 52,it reboots.i think its not making correct jump on that line.
please tell me whats causing the problem and how to remove it.
actually i'm trying to switch to protected mode and then to real mode and then back to protected mode.

Code: Select all

1: [ORG 0x7c00]
2: [BITS 16]
3: start:
4:       xor ebx,ebx
5:   mov bx,ds                       
6:   shl ebx,4                       
7:   mov eax,ebx
8:       lea eax,[gdt + ebx]             
9:        mov [gdtr + 2],eax
10:   cli
11:        mov ax,cs
12:        mov [RealModeCS],ax
13:        lea ax,[do_rm]
14:        mov [RealModeIP],ax
15:
16:        lgdt [gdtr]
17:   mov eax,cr0
18:   or al,1
19:   mov cr0,eax
20:   jmp SYS_CODE_SEL:do_pm          
21: [BITS 32]
22: do_pm:
23:   mov ax,SYS_DATA_SEL
24:   mov ds,ax
25:   mov ss,ax
26:   mov ax,LINEAR_SEL
27:   mov es,ax
28:        mov byte [es:dword 0xB8000],'P'
29: [BITS 16]
30: do_16:
31:   mov eax,cr0
32:   and al,0xFE
33:   mov cr0,eax
34:        jmp far [RealModeIP]

35: [BITS 16]
36: do_rm:   mov byte [es:dword 0xB8008],'4'
37:   xor ax,ax
38:   mov es,ax
39:   mov byte [es:dword 0xB800A],'5'
40:   sti
41:       xor ebx,ebx
42:   mov bx,ds                       
43:   shl ebx,4                      
44:   mov eax,ebx

45:       lea eax,[gdt + ebx]             
46:        mov [gdtr + 2],eax
47:        cli
48:        lgdt [gdtr]
49:        mov eax,cr0
50:   or al,1
51:   mov cr0,eax


52:        jmp SYS_CODE_SEL:do_pm1          
53: [BITS 32]
54: do_pm1:

        mov ax,SYS_DATA_SEL
   mov ds,ax
   mov ss,ax
   mov ax,LINEAR_SEL
   mov es,ax
   ; questionable PM code here
        mov byte [es:dword 0xB8002],'9'


hang:
    jmp hang   

RealModeIP:
        dw 0

RealModeCS:
   dw 0


gdtr:   dw gdt_end - gdt - 1   ; GDT limit
   dd gdt                  ; (GDT base gets set above)

gdt:   dw 0         
   dw 0         
   db 0         
   db 0         
   db 0         
   db 0         


LINEAR_SEL   equ   $-gdt
   dw 0xFFFF      
   dw 0         
   db 0
   db 0x92         
        db 0xCF                 
   db 0


SYS_CODE_SEL   equ   $-gdt
gdt2:   dw 0xFFFF               
   dw 0         
   db 0
   db 0x9A         
        db 0xCF                 
   db 0


SYS_DATA_SEL   equ   $-gdt
gdt3:   dw 0xFFFF               
   dw 0         
   db 0
   db 0x92         
        db 0xCF                 
   db 0


REAL_CODE_SEL   equ   $-gdt
gdt4:   dw 0xFFFF
   dw 0         
   db 0
   db 0x9A         
   db 0         
   db 0



REAL_DATA_SEL   equ   $-gdt
gdt5:   dw 0xFFFF
   dw 0         
   db 0
   db 0x92         
   db 0         
   db 0

gdt_end:


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:system reboots

Post by Pype.Clicker »

okay. first one thing: this is not [code\] but [ code ] (without spaces) and not [\code], but [ /code ] (without spaces once again).
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:system reboots

Post by Pype.Clicker »

Code: Select all

52:        jmp SYS_CODE_SEL:do_pm1         
53: [BITS 32]
54: do_pm11:
is this a typo ? do_pm11 instead of do_pm1 ...
shahzad

Re:system reboots

Post by shahzad »

pype you were correct.i just mistyped it while posting to forum.

its do_pm1.i've corrected it in the code posted above.
Curufir

Re:system reboots

Post by Curufir »

OK, just glanced through the code, I'll have another look later.

First impressions are that you never actually set ds to something sensible when you return to real mode (Just as a side note, you never restore the real mode cs you stored. This may be intended, but is weird).

This means that when you calculate the gdt offset for the gdtr in do_rm (Why do it again?) you are using the visible 16 bits of the selector that you loaded into ds (At this point ds=0x18). Therefore once gdtr is loaded it sets the gdt base to point somewhere other than your actual gdt and so when you try to move to pmode (Implicitly loading cs with a bad selector) everything falls down.

Might not be the only problem, but it's the first that springs to mind.
shahzad

Re:system reboots

Post by shahzad »

curfur you were absolutely correct in pointing out that after entering real mode again ,DS was not pointing to something sensible.
and as you mentioned there was no need to reload GDTR .
i've corrected both errors.now code is working fine.
Post Reply