Page 1 of 1

about getting in and out protected mode

Posted: Tue Apr 27, 2010 4:58 am
by Lemonade
I don't know why next code is not valid.
Thank you for answer.

Code: Select all

// this function is in physical addr 0x0A00 ~ 0x1000.
// inline asm is turbo asm.
// C compiler is Borland C++ 3.1
// Real Mode Data Segment Value is 0x0000.
// Real Mode Code Segment Value is 0x0000.
// Real Mode Stack Segment Value is 0x0000.
// Stack is valid in this function.( SP is initialized 0xFFFF in other function.)

// this is linked with following.asm 
//.model tiny
//.code
//org 0A00h
//end

#define jmpn(offset)         db 0xEB , offset;
#define jmpf(segment,offset) db 0xEA; dw offset; dw segment;
#define HALT                 db 0xEB , 0xFE;

void protected_mode()
{

    asm cli;

    init_GDT(0x600);// initialize and load( asm lgdt ) GDT at physical addr 0x600
                          // index 0 : null descriptor 
                          // index 1 : 16bit code descriptor , addr 0x00 ~ 0xFFFF, excute_read
                          // index 2 : 16bit data descriptor , addr 0x00 ~ 0xFFFF, read_write

    asm {
    first_real_16 label;

        smsw    ax;
        or      ax,0x0001;
        lmsw    ax;
        jmpn    (0x00);
        jmpf    (0x0008,protected_16); // use selector on segment arg

    protected_16 label;

    ready_real label;

        mov     ax, 0x0010;    	       // selector index 2
        mov     ds, ax;
        mov     es, ax;
        db 0x8E, 0xE0;                 // mov fs, ax;
        db 0x8E, 0xE8;                 // mov gs, ax;
        mov     ss, ax;

        lidt    real_mode_IDTR;        // size 1024(4*256), addr 0x00000000

        smsw    ax;
        and     ax, 0xFFFE;
        lmsw    ax;

        jmpf    (0x0000, second_real_16);  /*  system is rebooted this line */

        HALT; // for debug , if problem is not , halt.

    second_real_16 label;

        mov     ax, 0;
        mov     ds, ax;
        mov     es, ax;
        db 0x8E, 0xE0;                 // mov fs, ax;
        db 0x8E, 0xE8;                 // mov gs, ax;
        mov     ss, ax;
    }

    asm sti;
}

Re: about getting in and out protected mode

Posted: Tue Apr 27, 2010 6:36 am
by Brendan
Hi,
Lemonade wrote:I don't know why next code is not valid.
Usually the easiest/best way to debug something like this is to use something like the debugger built into Bochs, to execute one instruction at a time and see exactly what is happening (to registers, memory, etc).

I'd wonder whether or not it makes sense to have a function in C that contains 100% assembly and 0% C - maybe it'd make more sense to write this in pure assembly, and assemble it with a real assembler (e.g. one that understands basic instructions that have existed for 15 years, like "mov fs, ax").

Also, don't use SMSW/LMSW (they're for "286 protected mode"). For "80386 and later protected mode" use CR0.

If that doesn't work, describing the problem properly is going to help other people to help you.



Cheers,

Brendan

Re: about getting in and out protected mode

Posted: Tue Apr 27, 2010 10:50 am
by Lemonade
#-o

Thank Brendan~
Brendan wrote:Also, don't use SMSW/LMSW (they're for "286 protected mode"). For "80386 and later protected mode" use CR0.
Your comment is right.

I just edit it.

I have just known that switching mode from Pmode to Rmode is after 80386.

Re: about getting in and out protected mode

Posted: Wed Apr 28, 2010 10:26 pm
by bewing
// Stack is valid in this function.( SP is initialized 0xFFFF in other function.)

Um. That value for SP may work, but it is not smart. SP should have an even number in it.