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: 1
Joined: Fri Jan 17, 2025 2:30 pm
Libera.chat IRC: kelemenorosz

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: 1802
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: 5609
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.
Post Reply