Error when I pass to Pmode

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
cobrab

Error when I pass to Pmode

Post by cobrab »

I have a error when I pass to pmode, please see the code, I dont know where is the error.

Thanks.
cobrab

Re:Error when I pass to Pmode

Post by cobrab »

[attachment deleted by admin]
cobrab

Re:Error when I pass to Pmode

Post by cobrab »

[attachment deleted by admin]
123abc

Re:Error when I pass to Pmode

Post by 123abc »

in your
kernel:

   cli         ;desabilito interrupciones
   mov ax, cs
   mov ds, ax
   mov es, ax
   call habilita_A20   ; llama habilita_a20


   lgdt [GDTPTR]   ; cargamos la GDT
   mov eax, cr0   ; obtenemos en ax el registro cr0
   inc al      ; aumento al
   mov cr0, eax   ; cr0 = 1. Pasamos a modo Protegido :)
   ret

Get rid of that ret!

here is the fixed code:

kernel:

   cli         ;desabilito interrupciones
   mov ax, cs
   mov ds, ax
   mov es, ax
   call habilita_A20   ; llama habilita_a20


   lgdt [GDTPTR]   ; cargamos la GDT
   mov eax, cr0   ; obtenemos en ax el registro cr0
   inc al      ; aumento al
   mov cr0, eax   ; cr0 = 1. Pasamos a modo Protegido :)
cobrab

Re:Error when I pass to Pmode

Post by cobrab »

Thanks, I replace that but the error continue
RetainSoftware

Re:Error when I pass to Pmode

Post by RetainSoftware »

Well i think there are a lot of errors here. I marked them with bold. Hope this all helps

Greets,

Sephiroth Angellus (RetainSoftware)

bits 16]
[org 0x7C00]

kernel:

   cli         ;desabilito interrupciones
   mov ax, cs
   mov ds, ax
   mov es, ax
   call habilita_A20   ; llama habilita_a20


   lgdt [GDTPTR]   ; cargamos la GDT
   mov eax, cr0   ; obtenemos en ax el registro cr0
    or al, 0x01; aumento al
   mov cr0, eax   ; cr0 = 1. Pasamos a modo Protegido :)
   ; ret (a little bit dumb here)

   jmp dword (code32-GDT):go_to_pmode

[bits 32]

go_to_pmode:
   MOV    ax,(data32-GDT)
   mov ss,ax
   mov ds,ax
   mov es,ax
   mov fs,ax
   mov gs,ax
   mov esp,0x2F000 ; Set stack
   ; (not needed)jmp fin ; jmp to enable 32 bit mode

fin:
   jmp fin

[bits 16]

habilita_A20:
   call empty_8042   ; verifico si buffer de entrada esta lleno
   mov al, 0xD1   ; comando para escribir nuevo estado controlador
   out 64h, al      ; mueve al puerto 64h contenido de al
   call empty_8042   ; verifico si buffer de entrada esta lleno
   mov al, 0xDF   ; al = 0xDF para habilitar linea a20
   out 60h, al      ; mueve al para el puerto de del teclado
   call empty_8042   ; verifico si buffer de entrada esta lleno
   ret

empty_8042:
   in   al,0x64   ; estado del puerto 8042
   test   al,2      ; esta el buffer de entrada lleno?
   jnz   empty_8042   ; Si esta lleno realizamos un loop
   ret

; Tabla de la GDT


GDTPTR:
   dw GDT_END - GDT - 1   ; 8 bytes (cada entrada) * 8 = 2000h (8192) - 1 = 1fffh (tamanho de la GDT)
   dd GDT         ; indica la gdt                     


GDT:

   dw   0,0,0,0         ; descritor null

code32:
   dw 0xFFFF,0x0000         ; descritor de c?digo 32 bits
   db 0x00,0x9A,0xCF,0x00      ; limite=FFFF (byte 0 e 1),base=0 (byte 2,3,4,7),[present=1,ring=0,c?digo] (byte 5),
                  ; [byte-granular,32bits] (byte 6)   

data32:
   dw 0xFFFF,0x0000         ; descritor de dados 32 bits
   db 0x00,0x92,0xCF,0x00      ; limite=FFFF (byte 0 e 1),base=0 (byte 2,3,4,7),[present=1,ring=0,dados] (byte 5),
                  ; [byte-granular,32bits] (byte 6)

                  ; descritor de c?digo 16 bits
code16:
   dw 0xFFFF,0x0000         ; limite=FFFF (byte 0 e 1),base=10000h (byte 2,3,4,7),[present=1,ring=0,c?digo] (byte 5),
   db 0x1,0x9a,0x00,0x00      ; [byte-granular,16bits] (byte 6)

                  ; descritor de dados 16 bits
data16:
   dw 0xFFFF,0x0000         ; limite=FFFF (byte 0 e 1),base=10000h (byte 2,3,4,7),[present=1,ring=0,dados] (byte 5),
   db 0x1,0x92,0x00,0x00      ; [byte-granular,16bits] (byte 6)

GDT_END:
cobrab

Re:Error when I pass to Pmode

Post by cobrab »

RetainSoftware:

Thank for your help,



I thinck to change al, 0x01 for mov al,1 to enable CR0, It is ok

I discard the ret instruccion

When I enable CR0 , cs need change, and I put

jmp dword code32-GDT

for that or I need to put jmp dword code32 (Code Descriptor)

And I put


MOV ax,data32-GDT maybe MOV ax, data32
mov ss,ax
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
mov esp,0x2F000

Becouse I want to ss, ds, es, fs, gs point to the Data descriptor
Curufir

Re:Error when I pass to Pmode

Post by Curufir »

Use the "or 0x01" instruction to enable pmode (As previously suggested). You don't know the rest of the flags, and you certainly don't want to make them all zero by doing a mov.

This is one of the ways to do the far jump "jmp a_code_selector:pmode_label". The previous answer already shows this.

Finally, the guy was talking about there being no need to do a "jmp fin" after setting ss,ds,es etc because the jmp to your pmode code already sets cs which is the important part (Which is why that one should be a far jump).
RetainSoftware

Re:Error when I pass to Pmode

Post by RetainSoftware »

Hi agian,

>I thinck to change al, 0x01 for mov al,1 to enable CR0, It is ok

Do the or, 0x01, this leaves the cpu state intact and only enable pmode.


> When I enable CR0 , cs need change, and I put
> jmp dword code32-GDT

after this jmp cs = the code32 descriptor:
in pmode the descriptor is and offset in the GDT table.

> for that or I need to put jmp dword code32 (Code Descriptor)

no, you don't. It's already there :)

> And I put

> MOV ax,data32-GDT maybe MOV ax, data32

no, just the mov ax, data32-gdt. As told above a descriptor is an offset within the GDT.

> mov ss,ax
> mov ds,ax
> mov es,ax
> mov fs,ax
> mov gs,ax
> mov esp,0x2F000

> Becouse I want to ss, ds, es, fs, gs point to the Data descriptor

with the code above all the segment descriptors are pointing to the data descriptor.

in your GDT the descriptor used have the following values;

code32 = 0x08
data32 = 0x10
code16 = 0x18
data16 = 0x20

if you want to used the descriptor asign these value to es, ds, ss, fg or gs.

Greets,

Sephiroth Angellus
cobrab

Re:Error when I pass to Pmode

Post by cobrab »

Thanks RetainSoftware and Curufir for your reply.

the jmp fin is not the jmp for the pmode the comment is the error, I use jmp fin for a loop
Post Reply