which came first: the interrupt or the page fault?

A student writes, asking about interrupts and page faults and in what
order they get handled.  Feel free to push this question and my answer
onto the stack until after the first mid-term on Wednesday.  This stuff
we covered in class today will not be on the exam.

	Hello Dr. Patt, 
        How are you? The following question pertains to your 
	discussion in class. You said in class that we finish 
	the current instruction before handling the interrupt or 
	service routine. That is by default. That's what you said. 
	I do have a question if you don't mind. What if the current 
	instruction triggers a couple of page faults? Are we still 
	continue on with the current instruction till it is finished 
	before handling the interrupt? Thanks. 

	<<name withheld to protect the student wanting to know which came first>

The computer will process the interrupt and the exception exactly as
I said in class:

1. The current instruction causes a page fault.  If the ISA is such that
there could be two page faults, the hardware would not see the second
until returning from the first.  Do you understand why?

To handle the page fault, the machine is backed up to the start of the 
instruction.  The PC, PSR, etc. are pushed on the stack.  The PC is loaded 
with the starting address of the page fault service routine.  And the machine 
is now ready to process the next instruction.

2. In processing the instruction, MAR <-- PC, AND the machine is tested
to see if there are any interrupts present.  Since an interrupt (of higher
priority, I assume) occurred while the previous instruction was executing,  
we immediately take the interrupt.  That is, we push on the stack the PC,
the PSR, etc.  Note that the PC we push on the stack contains the starting
address of the page fault handler.  And, we vector to the interrupt handler.
That is, we load PC with the starting address of the corresponding 
interrupt service routine.

3. The interrupt service routine executes to completion (assuming no higher
priority interrupt shows up), ending in an RTI, which pops the stack into the
PC, PSR, etc.

4. Since the PC (again) contains the starting address of the page fault handler,
we start executing that code.  Upon completion of that code (assuming no
interrupt or exception), we do an RTI.

5. The PC now contains the address of the instruction which caused the
page fault.  We attempt to execute that instruction again.  This time
everything goes fine, and we continue processing.  If there happened to be
a second page fault in that one instruction, we would encounter it now.

Hope that is helpful.

Good luck on Wednesday.

Yale Patt