64 bit mode interrupts and the stack selector

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
dschatz
Member
Member
Posts: 61
Joined: Wed Nov 10, 2010 10:55 pm

64 bit mode interrupts and the stack selector

Post by dschatz »

Hi All,

I have setup my IDT to dispatch interrupts to dummy handlers (that just print which interrupt occurred). In testing it, I realized I get a GP fault on the first iretq. I believe whats going on is that it tries to load the stack segment and fails because the selector is not valid (beyond the limit of my tiny GDT). I never setup my SS once I got to long mode because I was under the impression it wasn't used at all under long mode. Am I misunderstanding something? Should I just setup a data segment and point all the unused segments to it?
User avatar
turdus
Member
Member
Posts: 496
Joined: Tue Feb 08, 2011 1:58 pm

Re: 64 bit mode interrupts and the stack selector

Post by turdus »

dschatz wrote:Hi All,

I have setup my IDT to dispatch interrupts to dummy handlers (that just print which interrupt occurred). In testing it, I realized I get a GP fault on the first iretq. I believe whats going on is that it tries to load the stack segment and fails because the selector is not valid (beyond the limit of my tiny GDT). I never setup my SS once I got to long mode because I was under the impression it wasn't used at all under long mode. Am I misunderstanding something? Should I just setup a data segment and point all the unused segments to it?
Read the Intel/amd manuals. Of course you'll need a stack (and therefore an appropriate selector in ss). There's a special case when it can be unconfigured (ss=null) but I'm pretty sure it's not your case.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: 64 bit mode interrupts and the stack selector

Post by xenos »

I had the same problem when I started my x86_64 kernel and I solved it by setting SS to 0 right at the beginning. Anyway, I think this should be documented somewhere in the Intel / AMD manuals...
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
dschatz
Member
Member
Posts: 61
Joined: Wed Nov 10, 2010 10:55 pm

Re: 64 bit mode interrupts and the stack selector

Post by dschatz »

XenOS wrote:I had the same problem when I started my x86_64 kernel and I solved it by setting SS to 0 right at the beginning. Anyway, I think this should be documented somewhere in the Intel / AMD manuals...
Yes, I ended up doing the same thing (also setting ds, es, fs, and gs all to 0 for good measure) and it now works. I was wondering why this is necessary? It doesn't make use of the descriptor (obvious because the invalid descriptor works). So why should it try to switch stack segments with an iretq?
User avatar
turdus
Member
Member
Posts: 496
Joined: Tue Feb 08, 2011 1:58 pm

Re: 64 bit mode interrupts and the stack selector

Post by turdus »

dschatz wrote:So why should it try to switch stack segments with an iretq?
Because x86_64 switches unconditionally. Think about it: cpu spend most of it's time in userspace (ss ring 3), then an interrupt happens, which passes control to a routine somewhere in kernelspace (ring 0). After the ISR finishes, you have to switch back to userspace (ring 3). You can also use IST stack switch mechanism for that, but switching to a safer stack for ISR is mandatory anyhow. Now it's more simpler to have the same mechanism for the rest cases, instead of pushing ss conditionally like x86_32 does.
Post Reply