Setting up the stack after the switch to long 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
madanra
Member
Member
Posts: 149
Joined: Mon Sep 07, 2009 12:01 pm

Setting up the stack after the switch to long mode

Post by madanra »

I've been playing about with switching to long mode (which now works :D), using mainly the AMD manuals and http://wiki.osdev.org/User:Stephanvansc ... _Long_Mode for reference. But at the end of the wiki page, it says "It is very important that you don't set the stack segment and that you don't enable the interrupts (unless you have set up a 64-bit IDT of course)." with no reason given - why shouldn't you set the stack segment without a 64-bit IDT?
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:

Re: Setting up the stack after the switch to long mode

Post by Combuster »

You're reading an userspace page, which usually is an indication that it's not ready for everybody to read (and I think you just found out why).

Truth is, you'll need an GDT before reloading segment registers, and you need an IDT (and GDT) before enabling interrupts. A write to a segment register causes the CPU to look it up in the current GDT. If that doesn't exist, or is broken, then the CPU tries to fix that problem by invoking an exception and ends up causing another segment load, which ends up in a reboot because it'd otherwise go around in circles.
"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 ]
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Setting up the stack after the switch to long mode

Post by Owen »

Combuster wrote:You're reading an userspace page, which usually is an indication that it's not ready for everybody to read (and I think you just found out why).

Truth is, you'll need an GDT before reloading segment registers, and you need an IDT (and GDT) before enabling interrupts. A write to a segment register causes the CPU to look it up in the current GDT. If that doesn't exist, or is broken, then the CPU tries to fix that problem by invoking an exception and ends up causing another segment load, which ends up in a reboot because it'd otherwise go around in circles.
Actually, Long Mode doesn't care about your SS selector; on an interrupt, it loads a zero into it (!). In fact, it's generally best to load a zero SS for consistency with the rest of the system
StephanvanSchaik
Member
Member
Posts: 127
Joined: Sat Sep 29, 2007 5:43 pm
Location: Amsterdam, The Netherlands

Re: Setting up the stack after the switch to long mode

Post by StephanvanSchaik »

madanra wrote:I've been playing about with switching to long mode (which now works :D), using mainly the AMD manuals and http://wiki.osdev.org/User:Stephanvansc ... _Long_Mode for reference. But at the end of the wiki page, it says "It is very important that you don't set the stack segment and that you don't enable the interrupts (unless you have set up a 64-bit IDT of course)." with no reason given - why shouldn't you set the stack segment without a 64-bit IDT?
The 64-bit IDT was about the interrupts, not the stack segment. As for setting the stack segment, I'm not entirely sure why I actually wrote that note. Probably because "mov ss, ax" caused a triple fault here, but if what Owen said is true, then I probably know why it did.


Regards,
Stephan J.R. van Schaik.
Post Reply