Entering PMode - problem ...

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
deph
Posts: 13
Joined: Sun Jun 22, 2008 6:04 am

Entering PMode - problem ...

Post by deph »

I have written something to enter PMode, but it doesn't want to work :(
The following piece of code is loaded at address 0x7E00:0x0000, and far-jumped by stage1

Code: Select all

#define ABS(x)  $(x-_stage2)
#define MSG(x)  movw ABS(x), %si

        .text
        .code16
        .org 0

_stage2:
        
        cli
        pushw   %cs
        popw    %ds
        movw    $0x9000, %ax
        movw    %ax, %ss
        movw    $0xFFFF, %sp
        sti
        
        movw    ABS(stage2_msg), %si
        call    print_scr
        
        cli
        lgdt    gdt_desc-_stage2

        movl    %cr0, %eax
        orl     $0x1, %eax
        movl    %eax, %cr0  

        ljmp    $8, $_stage3
        hlt

        .code32
_stage3:
        
        movw    $0x10, %ax
        movw    %ax, %ds
        movw    %ax, %es
        movw    %ax, %ss
        movl    $0x90000, %esp   
STOP:
        cli
        hlt

/* 
 * Print to screen via BIOS Teletype
 *
 */
print_scr:
        pushw   %ax
        pushw   %bx

print_scr_1:
        lodsb

        orb     %al, %al
        jz      print_scr_end

        xorw    %bx, %bx
        movb    $0x0E, %ah
        int     $0x10

        jmp     print_scr_1

print_scr_end:

        popw    %bx
        popw    %ax

        ret


/*
 * Data area
 *
 */
stage2_msg:     .string "\n\nBL2: Stage2 Loaded !\r\nBL2: Loading GDT\r\n"

/*
 * Descriptors
 * 
 */
gdt_data: /* { */
        .long   0x0
        .long   0x0

/* Code descriptor */
        .word   0xFFFF, 0x0
        .byte   0x0, 0x9A, 0xCF, 0x0

/* Data Descriptor */
        .word   0xFFFF, 0x0
        .byte   0x0, 0x92, 0xCF, 0x0 
gdt_end:
/* } */       
gdt_desc:
        .word   gdt_end-gdt_data-1
        .long   gdt_data-_stage2+0x7E00

        .org 0x200
The error is (bochsout.txt)

Code: Select all

00018375019e[CPU  ] jump_protected: gate type 0 unsupported
... I don't get it !
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: Entering PMode - problem ...

Post by bewing »

I am not sure why bochs gave you that particular error, but you are subtracting _stage2 from all your addresses. This is a small routine, so all the numbers you get after that subtraction are going to be <500. You need to add back in the real base address of _stage2, to recreate actual pointers. When you say it's loaded at 0x7c00:0000 there is probably somthing incorrect about that, too. My guess is that you mean 0x0000:7c00. But whatever it is, you need to add it back into your address calculations.
deph
Posts: 13
Joined: Sun Jun 22, 2008 6:04 am

Re: Entering PMode - problem ...

Post by deph »

Stage1 is loaded at 0x0000:0x7C00, then it loads stage2 in memory at address 0x7E00:0
User avatar
kmtdk
Member
Member
Posts: 263
Joined: Sat May 17, 2008 4:05 am
Location: Cyperspace, Denmark
Contact:

Re: Entering PMode - problem ...

Post by kmtdk »

first, you have to convert the seg:off address to a "linery" address. i think that is the problem, since you are not using
( 0x0000:0x7e00 )
last, i am not sure, but the line ljmp $8, $_stage3
should it not be : ljmp $8: $_stage3




KMTdk
well, what to say, to much to do in too little space.
when it goes up hill, increase work, when it goes straight, test yourself but when going down, slow down.
User avatar
TNick
Posts: 18
Joined: Fri May 09, 2008 7:50 am
Location: Brasov, Romania

Re: Entering PMode - problem ...

Post by TNick »

Hello!

Assumption 1.
.org directive will not affect the place in file for generated code
First, by using .org 0, you tell the assembler that you are at address 0 in memory. By doing a

Code: Select all

ljmp    $8, $_stage3 
you will jump somewhere way below 0x100. You should be at 0x7c00. Change .org 0 to org 0x7E00 (or change

Code: Select all

ljmp $8, $_stage3
to

Code: Select all

ljmp $8, $_stage3+0x7E00
)

Assumption 2
.org directive will affect the place in file whaere code will be stored
change

Code: Select all

ljmp $8, $_stage3
to

Code: Select all

ljmp $8, $_stage3+0x7E00
Chhh. Ugly syntax!!! :lol:

Nick

PS:
KMTdk may be right about column...
deph
Posts: 13
Joined: Sun Jun 22, 2008 6:04 am

Re: Entering PMode - problem ...

Post by deph »

Yeah, AT&T syntax. I tried doing this in NASM and it works !
I use .org directive to change offsets inside the file ( since gas has no "times 510- ..." - I use .org 510 ); It is usually set to 0 at the beginning )
For memory offsets I use the ABS macro (like GRub does);
I've just repaired my addresses (I forgot that real mode has a funny way of making addresses <selector 4bit offset + effective address -> physical addr>); Now I get a gate type 15 unsupported :)).
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Re: Entering PMode - problem ...

Post by suthers »

Do you prefer AT&T syntax?
Because I would help, but anything but intel syntax assembler COMPLETELY CONFUSES me....
I find AT&T so much harder to understand....
But that's just my two bits.... 8-[
Oh and I might be wrong or be completely misunderstanding what's happening here (AT&T makes me loose all my senses), but aren't you supposed to set .org to the ORGigin of the code, or I might not be understanding what's happening here... (highly likely...)
Jules

edit: sorry already mentioned sorry, I was so happy at understanding something in AT&T syntax that I lost my composure.... :lol:
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Re: Entering PMode - problem ...

Post by suthers »

deph wrote:Yeah, AT&T syntax. I tried doing this in NASM and it works !
I use .org directive to change offsets inside the file ( since gas has no "times 510- ..." - I use .org 510 ); It is usually set to 0 at the beginning )
For memory offsets I use the ABS macro (like GRub does);
I've just repaired my addresses (I forgot that real mode has a funny way of making addresses <selector 4bit offset + effective address -> physical addr>); Now I get a gate type 15 unsupported :)).
Yah it changes offsets, it doesn't ADD 510 - size_of_code zeros....
Jules
deph
Posts: 13
Joined: Sun Jun 22, 2008 6:04 am

Re: Entering PMode - problem ...

Post by deph »

OOoh, I got it ! GDT's wrong !

Here is the right one:

Code: Select all

sys_gdtab:
        .quad   0x0

        /*
        Code Descriptor */
        .word   0xFFFF, 0x0                 /* here was the mistake ( there are two words, I used dwords (.long) ) */
        .byte   0x0, 0x9A, 0xCF, 0x0

        /*
        Data Descriptor */
        .word   0xFFFF, 0x0                  /* here was the mistake ( there are two words, I used dwords (.long) ) */
        .byte   0x0, 0x92, 0xCF, 0x0 
sys_gdp:
        .word  0x17
        .long   sys_gdtab
What was Bochs trying to say by "jump_protected: gate type 0 unsupported" was that he didn't recognize the descriptor loaded in GDTR as being a Code Segment Descriptor ! :(
Post Reply