Page 1 of 1
Setting up the stack after the switch to long mode
Posted: Mon Mar 29, 2010 1:26 pm
by madanra
I've been playing about with switching to long mode (which now works
), 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?
Re: Setting up the stack after the switch to long mode
Posted: Mon Mar 29, 2010 2:21 pm
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.
Re: Setting up the stack after the switch to long mode
Posted: Mon Mar 29, 2010 2:46 pm
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
Re: Setting up the stack after the switch to long mode
Posted: Tue Mar 30, 2010 8:16 am
by StephanvanSchaik
madanra wrote:I've been playing about with switching to long mode (which now works
), 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.