title: The role of hardware vs software in a context switch
body: <p>I have read the description in several popular OS textbooks of what happens during a context switch.  None of them have left me completely satisfied, though the one quoted below (<a href="http://www.osinfoblog.com/post/67/implementation-of-processes/" rel="nofollow">Tanenbaum</a>) comes most close.  There are a couple questions it leaves me with.  Each one is highlighted in bold and elaborated below.</p>
  
  <blockquote>
    <p>Assume that user process 3 is running when a disk interrupt occurs. <strong><em>User process 3's program counter, program status word, and often one or more registers are pushed onto the (current) stack by the interrupt hardware.</em></strong> The computer then jumps to the address specified in the interrupt vector. That is all the hardware does. From here on, it is up to the software, particularly, the interrupt service procedure.</p>
  </blockquote>
  
  <p>Why must the hardware save the PC and PSW, but the software can save everything else (see next quote below)?</p>
  
  <p>I am guessing it is because once execution has jumped to the interrupt service procedure, the PC and PSW are lost (because they've been replaced by those for the service procedure).  So the hardware <strong>must</strong> do it.  Is this correct?</p>
  
  <blockquote>
    <p>All interrupts start by saving the registers, sometimes in the process table entry for the current process. Then <strong><em>the information pushed onto the stack by the interrupt is removed</em></strong> and the stack pointer is set to point to a temporary stack used by the process handler.</p>
  </blockquote>
  
  <p>The way this is worded (the word "removed" specifically) it makes it look like the old process' registers are saved (by kernel software) to the process table, and then the PC and PSW which were pushed to old process stack by the hardware (previous paragraph) are just discarded (again, the word "removed").  Obviously they can't be discarded since we'll need them in the future, and also it would be stupid since we made a point to put them on that stack!</p>
  
  <p>I am guessing that when they say "removed" they mean "removed... and then put in the process table along with all the register and other info that the kernel already put there."  So now the inactive process is ready to go again since (a) its process table is complete and (b) the temp stuff (PC/PSW) that was on top of its stack is cleaned away.  Is this correct?</p>