Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

assembly - During an x86 software interrupt, when exactly is a context switch made?

I am asking this because I am trying to implement interrupts in my toy kernel. So, I know that when an interrupt occurs, the CPU pushes various bits of information onto the stack. However, everywhere I search online shows different information in different order being pushed. I also know that if the interrupt occurred in user mode (Ring 3), the CPU must switch to kernel mode (Ring 0) before it can execute the ISR. I think it has something to do with the TSS and ss and esp, however I am not sure. I have read various different explanations all over the internet and have not found any uniformity in any of them yet.

So my question is, in what exact order does the (x86) CPU push data onto the stack during an interrupt and when does it do it, and at what point in time does it make a context switch via the TSS and how does this affect the data that is pushed onto the stack, if at all?

What I have learned from research so far (correct me if wrong): I am pretty sure the processor pushes eflags, cs and eip onto the stack as well as an error code if necessary during an interrupt. However, is this all that is pushed? I have read elsewhere that ss and esp are pushed but I don't know if this happens all the time, or why it is pushed.

Furthermore, I believe that the CPU will look in the TSS for the Ring0 ss and esp and at some point load them, but I am not sure when. Perhaps it does this right after the interrupt occurs? Also, how is cs and ds handled during all of this? Is cs loaded by the CPU from the entry in the IDT? What about ds? Must I load that manually?

Sorry if this is the wrong place to ask, I am not sure where else I should.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just take the Intel Manual 3 and read:

  • Chapter 6 INTERRUPT AND EXCEPTION HANDLING
  • Section 5.8.5 Stack Switching
  • 5.8.6 Returning from a Called Procedure.

This is what is pushed on an interrupt

ISR stack

Keep in in mind that interrupt are handled with Task Gates, Trap gates and Interrupt gates, each one with different semantic and the last two very close to Call gates.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...