I have decided to implement v86 mode into the kernel I have been writing.
I've been looking at how to do this, and something has been bugging me.
Suppose that the CPU is executing in v86 mode and hits an INT instruction/a hardware interrupt occurs.
When in v86 mode the stack is at SS:SP as in SS * 4 + SP.
In the interrupt handler the stack is at a totally different location based on the same SS:SP registers.
Is this right? Am I supposed to check whether the CPU was just executing v86 code and if it was flatten the stack address and fix the stack segment register?
(I'm not using paging/Don't know if that would matter here)
Virtual 8086 stack with interrupt handlers
-
- Posts: 2
- Joined: Fri Jan 17, 2025 2:30 pm
Re: Virtual 8086 stack with interrupt handlers
I highly suggest you read the Intel SDM or AMD APM for more details on this. I am using the AMD APM, and it describes in volume 2, chapter 8.8 exactly how that all works. In particular under what circumstances it invokes what handler in response to an interrupt in Virtual 8086 mode.
In this case, if it does invoke a protected-mode handler, it will read SS:ESP from the TSS even if the CPL is already 0. So there is no problem; the interrupt will be taken on the normal kernel stack.
In this case, if it does invoke a protected-mode handler, it will read SS:ESP from the TSS even if the CPL is already 0. So there is no problem; the interrupt will be taken on the normal kernel stack.
Carpe diem!
-
- Member
- Posts: 5623
- Joined: Mon Mar 25, 2013 7:01 pm
-
- Posts: 2
- Joined: Fri Jan 17, 2025 2:30 pm
Re: Virtual 8086 stack with interrupt handlers
It depends on IOPL. Either you need to emulate all instructions that modify the interrupt flag, including the int instruction, or let the CPU emulate it.
Hardware interrupts are a different matter. In that case the CPU will load the kernel stack selector and save the V86 mode segment registers in addition to flags, cs and ip on this stack. Iret will then reload the v86 context including all the segment registers and pop of cs, ip, ss and sp. Exceptions work the same way as hardware interrupts.
Hardware interrupts are a different matter. In that case the CPU will load the kernel stack selector and save the V86 mode segment registers in addition to flags, cs and ip on this stack. Iret will then reload the v86 context including all the segment registers and pop of cs, ip, ss and sp. Exceptions work the same way as hardware interrupts.