Page 1 of 3

unreal mode

Posted: Mon Mar 05, 2012 7:12 am
by dileep
Hi guys , below the code , that i am using to switch in to the unreal mode.

Code: Select all

DESCRIPTOR struct
    segment_limit_0_15   dw  ?
    base_addr_0_15       dw  ?
    base_addr_16_23      db  ?
    segment_properties   db  ?
    seg_limit_16_19_gran db  ?
    base_addr_24_31      db  ?
DESCRIPTOR ends

gstart  label  qword

nullDescriptor          DESCRIPTOR    <0,0,0,0,0,0>
code16Descriptor        DESCRIPTOR    <0ffffh,0000,00,9Fh,00h,00>
code32Descriptor        DESCRIPTOR    <0ffffh,0000,00,9Fh,08Fh,00>
data16Descriptor        DESCRIPTOR    <0ffffh,0000,00,93h,00h,00>
Stack16Descriptor       DESCRIPTOR    <0ffffh,0000,00,93h,00h,00>
bigData16Descriptor     DESCRIPTOR    <0ffffh,0000,00,92h,0CFh,00>

gend label qword

gbase label     fword
limit dw   gend-gstart-1     ;limit
addr  dd    ?

 
org_gdt label fword
orig_gdt_limit      dw  0000h     
orig_gdt_addr       dd  00000000h 

unrealmode proc
    push eax
    push ebx
    push ds
    push es
    push fs
    push gs
	
    db  66h
    sgdt fword ptr cs:org_gdt
   
    xor eax,  eax
    mov ax, cs
    shl eax, 4
    add eax, offset gstart
    mov dword ptr cs:[addr], eax

    pushf
    cli

    mov bx, bigData16Descriptor - gstart	
    lgdt    fword ptr cs:[gbase]
	
     
    mov eax, cr0
    or  al, 1    
    mov cr0,eax
 
    mov ds, bx
    mov es, bx
   
    mov eax, cr0
    and al,  0FEh
    mov cr0, eax

     nop
    db 66h
    lgdt cs:orig_gdt

     popf 
      pop gs
    pop fs
    pop es
    pop ds
    pop ebx
    pop eax
    ret
unrealmode endp		
While setting PE bit in CR0 the system reboots.
Can anyone help me in this.?

Re: unreal mode

Posted: Mon Mar 05, 2012 10:19 am
by JAAman
i fixed your post for now, but in the future, please use code tags

Re: unreal mode

Posted: Mon Mar 05, 2012 10:34 am
by bluemoon
This is going to be an FAQ...

1. Check your ORG, make sure you have correct address for LGDT
2. You don't need to restore descriptors in real mode, you can simply mov values to the segment registers.
3. If your CS is nonzero, your ip will be relative to that segment, once you enter protected mode you need to make sure CS:EIP points to same place. You either use zero CS with flat descriptor at the beginning; or for non-zero CS you need to tweak the code descriptor's base.

Re: unreal mode

Posted: Mon Mar 05, 2012 11:44 am
by Combuster
There's not even a start to that code in general. Where's the rest? How do you build and run it? Most errors at this stage are caused by a wrong environment, as bluemoon detailed.

Re: unreal mode

Posted: Mon Mar 05, 2012 10:31 pm
by dileep
actually this code is part of pxe option ROM driver. i burnt this driver image into network card , while the system is coming up it will load this driver and will call this procedure to switch into unreal mode.

Re: unreal mode

Posted: Tue Mar 06, 2012 2:06 am
by Combuster
Still not even close to being able to reproduce the problem.

Re: unreal mode

Posted: Wed Mar 07, 2012 10:09 pm
by dileep
Do i need to handle IDT while switching to unreal mode?

Re: unreal mode

Posted: Thu Mar 08, 2012 5:11 am
by Love4Boobies
Not if you have IRQs disabled during the short trip to protected mode.

Re: unreal mode

Posted: Wed Mar 21, 2012 10:48 pm
by dileep
Still i am facing the same issue. Can anyone help me in this?
I am calling this proc from int18h handler context.

Re: unreal mode

Posted: Thu Mar 22, 2012 2:35 am
by Solar
Combuster wrote:Still not even close to being able to reproduce the problem.

Re: unreal mode

Posted: Wed Mar 28, 2012 6:49 am
by dileep
Is there any way to register a handler for the triple fault exception?

Re: unreal mode

Posted: Wed Mar 28, 2012 9:42 am
by Love4Boobies
Nope; just for double faults. Then again, you don't even need that.

Re: unreal mode

Posted: Mon Apr 02, 2012 7:14 am
by dileep
Hi,

On some systems i am able to access 32bit offset without changing to unreal mode and not in other systems.
This is because some systems are left in unreal mode always.

So i registered #GPF handler and switching to unreal mode only when the handler invoked. Things worked fine.

Is it okay to use our own #GPF handler?

why does this work on some machines and not others? Are they already in unreal mode, left that way by the BIOS?
Is there any way to check whether processor is in unreal mode or not?

Thanks in adv

Re: unreal mode

Posted: Mon Apr 02, 2012 7:31 am
by Solar
dileep wrote:Is it okay to use our own #GPF handler?
Uh... which one have you been using so far?

Re: unreal mode

Posted: Mon Apr 02, 2012 7:53 am
by dileep
I hadn't used GPF handler previously. i was trying to switch in to unreal mode before accessing 32 bit offset.