Page 1 of 1

switching from long mode to compatibility mode

Posted: Mon Jun 09, 2008 3:04 pm
by tejuwala
Hello,

I am writing small code that boots, jumps to 32bit protected mode (no-paging enabled) --> 64bit mode (pml4 paging) --> perform some 64bit test --> jump back to 32bit protected mode (no-paging enabled).

I got successful getting into 64bit mode and I'm trying to switch out of it to 32bit protected mode.

According to Intel document the first step is to "Switch to compatibility mode" which after several efforts, I'm not able to do successfully. (Although I was successful in jumping in and out of compatibility mode (without ever jumping into 64bit mode) from 32bit protected mode (no-paging enabled).)

Only diff. betwn 64bit paged-protected mode and compatibility mode is L and D bit in code segment. In other words, (according to my understanding) if I'm in 64bit mode and if I want to jump to compatibility mode, all I'm suppose to do is to make far jump to compatibility mode Code-Segment (please correct me if I'm wrong). The CPU freezes when I make the jump.

While making jump,
1) PML4 paging is still in place (hence, IA32_EFER.LMA = 1)
2) PAE and PE is still enabled
3) IA32_EFER.LME = 1

I'm not sure if I can make a far jump/call while I'm in 64bit mode to switch to compatibility mode. And I can't think of any other way to switch CS. Can somebody please help me with this? Am I suppose to re-arrange paging or do something about gdt?

Thanks,
/tejas

NOTE: When I jump back to 32bit protected mode, I want to keep paging disabled.

Posted: Mon Jun 09, 2008 3:59 pm
by Combuster
Have you tried running your code in bochs (preferrably with debugger) to see what it complains about?

Posted: Mon Jun 09, 2008 4:24 pm
by tejuwala
Combuster wrote:Have you tried running your code in bochs (preferrably with debugger) to see what it complains about?
This code is part of BIOS development. So I can get state of registers at a time when freeze (most likely a GP fault) happens at next reboot. But I know exactly when it freezes:

After jumping into 64bit mode from 32bit mode, I push CS for compat mode and push address of 64bit code and make far return. At this point it (suppose to) switch to compatibility mode, instead it freezes.

/tejas

Posted: Mon Jun 09, 2008 6:33 pm
by Cognition
You aren't trying to switch segment privelege levels while jumping to compatability mode perchance are you?

Posted: Tue Jun 10, 2008 2:32 am
by Combuster
tejuwala wrote:
Combuster wrote:Have you tried running your code in bochs (preferrably with debugger) to see what it complains about?
This code is part of BIOS development. So I can get state of registers at a time when freeze (most likely a GP fault) happens at next reboot. But I know exactly when it freezes:
Which is exactly why you should use bochs - first of all it prints error messages when something unusual happens, which usually tells you your exact problem in case of a reboot. And second you can use the debugger to break before faulting instruction and analyse all the CPU's registers without having them clobbered by a reset.

Posted: Tue Jun 10, 2008 1:29 pm
by tejuwala
I got it fixed. As I said earlier, the difference between compatibility and 64bit mode is L/D bits in CS descriptor. However, while making far jump in 64bit mode, the operand size _must_ be set to 64bit (which is, by default, 32bit unless explicitly specified). So I had to put a "operand size prefix" in front of "retf" ("o64 retf" in NASM) and it worked fine.

Thanks guys,
/tejas