What happens on interrupt in 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
mpiechotka
Posts: 5
Joined: Tue Jun 09, 2015 9:34 am
Libera.chat IRC: mpiechotka

What happens on interrupt in long mode

Post by mpiechotka »

I have trouble finding this information - most things I find are either tutorials (mostly 32 bit) which don't really explain much (well - I can use it and it will probably work but I won't know how or why) or structure description. I was trying to find it in Intel manual but it seems to be spread out across several chapters (5-7) and concentrate on structures as well - furthermore it describes long mode only as differences to protected mode. That makes it hard to find information if you don't really know where to look for it.

What happens when there is exception or interrupt in long mode? I believe that IDT is used to lookup the interrupt service routine start address (offset field) and type (type field). It does also check if current code can call it (in case of software interrupts) and change it to #GP if it cannot but I cannot see any further information. What are the steps of processor when it gets an interrupt in long mode?
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: What happens on interrupt in long mode

Post by SpyderTL »

The wiki entry on IDT explains a lot of this, and even talks about the differences between 32 and 64-bit operation.

But in general, the procedure is the same. When an interrupt is triggered by an INT instruction, or an IRQ, the CPU looks up the interrupt number in the IDT to get a segment and an offset for the interrupt handler routine, pushes the current IP/EIP/RIP to the stack, pushes any additional information to the stack (like error numbers, etc.), then jumps to the interrupt handler routine.

Is there something specific that you want to know about?
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
mpiechotka
Posts: 5
Joined: Tue Jun 09, 2015 9:34 am
Libera.chat IRC: mpiechotka

Re: What happens on interrupt in long mode

Post by mpiechotka »

SpyderTL wrote:The wiki entry on IDT explains a lot of this, and even talks about the differences between 32 and 64-bit operation.

But in general, the procedure is the same. When an interrupt is triggered by an INT instruction, or an IRQ, the CPU looks up the interrupt number in the IDT to get a segment and an offset for the interrupt handler routine, pushes the current IP/EIP/RIP to the stack,
Thanks. I've through that there is an automatic stack switch for example (as with say sysenter or sysexit).
SpyderTL wrote:pushes any additional information to the stack (like error numbers, etc.), then jumps to the interrupt handler routine.
What prevents a userspace thread running on separate core from corrupting this information?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: What happens on interrupt in long mode

Post by Candy »

There's a mechanism called IST stack switch. Three bits in the IDT entry are used to indicate which IST stack to use. The IST stacks can be found in the 64-bit TSS. On entry it always switches to the indicated IST stack.

Note that you should actively prevent an IST stack from being used concurrently (on 2 cores) or reentrantly (interrupting an interrupt handler); both won't work and will cause weird crashes.

When you have IST working, you can also re-enable the red zone for other code. This is why tutorials say you should disable it.
embryo2
Member
Member
Posts: 397
Joined: Wed Jun 03, 2015 5:03 am

Re: What happens on interrupt in long mode

Post by embryo2 »

mpiechotka wrote:I have trouble finding this information - most things I find are either tutorials (mostly 32 bit) which don't really explain much (well - I can use it and it will probably work but I won't know how or why) or structure description. I was trying to find it in Intel manual but it seems to be spread out across several chapters (5-7) and concentrate on structures as well - furthermore it describes long mode only as differences to protected mode. That makes it hard to find information if you don't really know where to look for it.
The best way to understand the 64-bit modes is to read first about 32-bit modes. There is very detailed explanation of what's going on in Intel's manual for 32-bit modes and 64-bit modes just add some minor corrections to the 32-bit modes behavior. So, if you miss something while you are reading about a 64-bit interrupt it is highly probable you can find it when read about 32-bit interrupt.
My previous account (embryo) was accidentally deleted, so I have no chance but to use something new. But may be it was a good lesson about software reliability :)
mpiechotka
Posts: 5
Joined: Tue Jun 09, 2015 9:34 am
Libera.chat IRC: mpiechotka

Re: What happens on interrupt in long mode

Post by mpiechotka »

Thanks. I found what I asked for in the Volume 2 (instruction set) as it was combined with INT instruction description.
Post Reply