about getting in and out protected mode

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
Lemonade
Posts: 2
Joined: Tue Apr 27, 2010 4:06 am

about getting in and out protected mode

Post 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;
}
Last edited by Lemonade on Tue Apr 27, 2010 10:09 am, edited 1 time in total.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: about getting in and out protected mode

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Lemonade
Posts: 2
Joined: Tue Apr 27, 2010 4:06 am

Re: about getting in and out protected mode

Post 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.
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: about getting in and out protected mode

Post 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.
Post Reply