switching from long mode to compatibility 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
tejuwala
Posts: 3
Joined: Mon Jun 09, 2008 2:55 pm
Location: Bay Area
Contact:

switching from long mode to compatibility mode

Post 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Have you tried running your code in bochs (preferrably with debugger) to see what it complains about?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
tejuwala
Posts: 3
Joined: Mon Jun 09, 2008 2:55 pm
Location: Bay Area
Contact:

Post 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
Cognition
Member
Member
Posts: 191
Joined: Tue Apr 15, 2008 6:37 pm
Location: Gotham, Batmanistan

Post by Cognition »

You aren't trying to switch segment privelege levels while jumping to compatability mode perchance are you?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
tejuwala
Posts: 3
Joined: Mon Jun 09, 2008 2:55 pm
Location: Bay Area
Contact:

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