Virtual 8086 stack with interrupt handlers

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
kelemenorosz
Posts: 2
Joined: Fri Jan 17, 2025 2:30 pm

Virtual 8086 stack with interrupt handlers

Post by kelemenorosz »

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)
nullplan
Member
Member
Posts: 1804
Joined: Wed Aug 30, 2017 8:24 am

Re: Virtual 8086 stack with interrupt handlers

Post by nullplan »

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.
Carpe diem!
Octocontrabass
Member
Member
Posts: 5623
Joined: Mon Mar 25, 2013 7:01 pm

Re: Virtual 8086 stack with interrupt handlers

Post by Octocontrabass »

nullplan wrote: Fri Jan 17, 2025 3:14 pmeven if the CPL is already 0
The CPL can't be 0. Virtual 8086 mode always runs with CPL=3.
kelemenorosz
Posts: 2
Joined: Fri Jan 17, 2025 2:30 pm

Re: Virtual 8086 stack with interrupt handlers

Post by kelemenorosz »

Thanks.
rdos
Member
Member
Posts: 3311
Joined: Wed Oct 01, 2008 1:55 pm

Re: Virtual 8086 stack with interrupt handlers

Post by rdos »

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.
Post Reply