Pmode->Realmode Switching Troubble

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.
Matt

Pmode->Realmode Switching Troubble

Post by Matt »

I have some code which is suppose to switch from Pmode to Rmode and back (so I can use interrupts), but when I try to call an interrupt when I switch back into real mode, I get a "prefetch EIP > CS.Limit" error in bochs. here's the code:

push ds      ; save protected mode segments
push es

dec ax      ; switch back to real mode
mov cr0, eax   ; by toggling bit again
sti
mov ax, 13h
int 10h
cli
mov eax, cr0   ; switch to pmode by
inc ax      ; toggling last bit
mov cr0, eax

pop ds ;Restore PMode Segments
pop es

BTW, taking out the push and pops didn't make a difference. It switches back to real mode then back into pmode just fine, it just crashes When I try to use an iterrupt. Any ideas on what I'm stupidly doing wrong?
frank

Re:Pmode->Realmode Switching Troubble

Post by frank »

did you do a far jump after switching to pmode?
Matt

Re:Pmode->Realmode Switching Troubble

Post by Matt »

frank wrote: did you do a far jump after switching to pmode?
um, no.....
frank

Re:Pmode->Realmode Switching Troubble

Post by frank »

do that, else it won't set-up cs :)
(a far jump sets up it automatic)
btw
like this:

jmp placeingdt:main32

bits32
main32: ;switch to realmode

btw you must make a gdt table for that ;)
then it'll work. (if you don't do a far jump its still in 16bit real mode)
Matt

Re:Pmode->Realmode Switching Troubble

Post by Matt »

So would it be re-written something like this:

[bits 32]

jmp placeingdt:damnmain32

damnmain32:

push ds ; save protected mode segments
push es

dec ax ; switch back to real mode
mov cr0, eax ; by toggling bit again
sti
mov ax, 13h
int 10h
cli
mov eax, cr0 ; switch to pmode by
inc ax ; toggling last bit
mov cr0, eax

pop ds ;Restore PMode Segments
pop es
frank

Re:Pmode->Realmode Switching Troubble

Post by frank »

Try this:


; switching to pmode

jmp codesel:main32

[bits 32]
main32:

push ds ; save protected mode segments
push es

dec ax ; switch back to real mode
mov cr0, eax ; by toggling bit again
sti
mov ax, 13h
int 10h
cli
mov eax, cr0 ; switch to pmode by
inc ax ; toggling last bit
mov cr0, eax

pop ds ;Restore PMode Segments
pop es

gdtr
dw gdt_end-1
dd gdt
gdt
nullsel equ $-gdt
gdt0
dd 0
dd 0
codesel equ $-gdt
dw 0ffffh
dw 0h
db 0h
db 09ah
db 0cfh
db 0h
datasel equ $-gdt
dw 0ffffh
dw 0h
db 0h
db 092h
db 0cfh
db 0h

gdt_end

:)
Matt

Re:Pmode->Realmode Switching Troubble

Post by Matt »

[attachment deleted by admin]
Matt

Re:Pmode->Realmode Switching Troubble

Post by Matt »

BTW, I get that error on this line: dw gdt_end-1
frank

Re:Pmode->Realmode Switching Troubble

Post by frank »

>NASM says it doesnt support non-32bit relocations
>I'll attach my code. Its a Pmode kernel. My bootloader sets up >pmode, gdt, etc...
yes it does, how else did I switch to pmode? :P


>BTW, I get that error on this line: dw gdt_end-1
hmm, that's strange..
what error?
Matt

Re:Pmode->Realmode Switching Troubble

Post by Matt »

When nasm tried to compile "dw gdt_end-1" it said "COFF format does not support non-32-bit relocations"
Matt

Re:Pmode->Realmode Switching Troubble

Post by Matt »

I removed the line "dw gdt_end-1" and it assembles just fine. It gives me a 3rd exception with no resolution error when I try to call int 10h still though :-\
frank

Re:Pmode->Realmode Switching Troubble

Post by frank »

aah now I see....
that format your using does not support 16bit assembly, so that wont work.
Use the flat binary format... -f bin
crazybuddha

Re:Pmode->Realmode Switching Troubble

Post by crazybuddha »

matt, you can't just switch back to real mode and back because CS will get whacked. In fact, it will get whacked simply from having interrupts enabled (as any interrupts get handled)

You load some random value from ax into cr0. You must preserve all the top bits of cr0 and toggle only the bottom one.

You got your GDT from frank and he may not have been finished sorting it out. Perhaps it is fine, but make sure you know what it all means. Otherwise, you are fighting a losing battle.

There are other issues perhaps, but these are enough to ensure failure.
Krom

Re:Pmode->Realmode Switching Troubble

Post by Krom »

You only wants to go back to real mode to make a ax=0x13, int 0x10? If this is what you wants dont go back to real more, instead, do the int 0x10 in the boot sector, it is only 5 bytes, i think you have 5 bytes free in the boot sector, isnt it?
frank

Re:Pmode->Realmode Switching Troubble

Post by frank »

no he wants to go to vesa....

ves a= > than 320x200 256

( 640 * 480 * 16k,
until somewhere in the 1800x1200 (my resolution :P))
Post Reply