3

I understand the basic difference between function call & interrupt (ISR) jump from below SE question.

difference between function call & ISR

But I am still not clear about, what are the registers will be pushed /pop to/from stack in both the cases? How context switching will happen in both the case? As we don't know when interrupt will occur, what we need to save (variables, PC, flags (PSW), registers, context) before entering into ISR?

How can we resume back original context without any data lose with multiple thread environment.

Community
  • 1
  • 1
kapilddit
  • 1,729
  • 4
  • 26
  • 51
  • 1
    This is highly platform-dependent. For x86, the instruction pointer and flags registers will be auto-saved, but the ISR must save/restore other registers. ARM uses banks of registers dedicated for interrupts. – Drew McGowen Aug 01 '14 at 21:11
  • Do you mean each interrupt has separate register bank? what will happen in case of nested interrupt? every time switching to new bank will happen to save the current context? What will happen in case of normal function call? only instruction pointers & flags will saved? – kapilddit Aug 01 '14 at 21:30
  • 1
    Often times, when a CPU is interrupted, it automatically disables interrupts to prevent nested interrupts. This is especially true with ARM's register banks. As far as function calls, typically just the instruction pointer is saved (and maybe a few other registers; again, it depends on what CPU), not the flags. – Drew McGowen Aug 01 '14 at 21:32
  • while entering into ISR, We need to disable to interrupt & while returning back it will call reti, so it will again enable the interrupt. Is this correct? if while serving low priority interrupt, if high priority interrupt comes then again context switching or bank switching will happen? I just want to confirm my understanding. – kapilddit Aug 01 '14 at 21:42

2 Answers2

2

I tried to google it & I found the needed information from this:

  1. Interrupts
  2. Context Switch

Thanks @Drew McGowen

So to sum up, the general sequence for an interrupt is as follows:

Foreground code is running, interrupts are enabled
Interrupt event sends an interrupt request to the CPU
After completing the current instruction(s), the CPU begins the interrupt response
automatically saves current program counter
automatically saves some status (depending on CPU)
jump to correct interrupt service routine for this request
ISR code saves any registers and flags it will modify
ISR services the interrupt and re-arms it if necessary
ISR code restores any saved registers and flags
ISR executes a return-from-interrupt instruction or sequence
return-from-interrupt instruction restores automatically-saved status
return-from-interrupt instruction recovers saved program counter
Foreground code continues to run from the point it responded to the interrupt

As usual, the details of this process will depend on the CPU design. Many devices use the hardware stack for all saved data, but RISC designs typically save the PC in a register (the link register). Many designs also have separate duplicate registers that can be used for interrupt processing, thus reducing the amount of state data that must be saved and restored.

Note that saving and restoring the foreground code state is generally a two-step process for reasons of efficiency. The hardware response to the interrupt automatically saves the most essential state, but the first lines of ISR code are usually dedicated to saving additional state (usually in the form of saving condition flags if not saved by the hardware, along with saving additional registers). This two-step process is used because every ISR will have different requirements for the number of registers it needs, and thus every ISR may need to save save different registers, and different numbers of registers, assuring all appropriate state data is saved without wasting time saving registers unnecessarily (that is, saving registers that are not modified in the ISR and thus didn’t need to be saved). A very simple ISR may not need to use any registers, another ISR may need to use only one or two registers, while a more complicated ISR may need to use a large number of registers. In every case, the ISR should only save and restore those registers it actually uses.

kapilddit
  • 1,729
  • 4
  • 26
  • 51
1

I'm sure there are different implementations based on the CPU you're using. On a general level, the function calls store the input parameters within the given registers (%o0-%o9) on SPARC and are available in the (%i0-%i9) registers in the calle's function. The callee function will then place the return value in the %i0 register to be available in the %o0 register for the caller function. According to the Sparc Manual, each interrupt is:

Accompanied by data, referred to as an “interrupt packet”. 
An interrupt packet is 64 bytes long, consisting of eight 64-bit doublewords.

According to this source, the data in your current executing thread is:

   Saved either on a stack (PDP-11, VAX, MIPS, x86_64) 
   or in a single set of dedicated registers (ARM, PowerPC)

The above source mentions how interrupts are handeled on a couple of different architectures as well.

Please let me know if you have any questions!

Community
  • 1
  • 1
Devarsh Desai
  • 5,984
  • 3
  • 19
  • 21
  • Do you mean in function call, instruction pointer and arguments will pushed to stack & some other register? what are these (%o0-%o9) registers? does bank switching will help to save the current context in both the cases? – kapilddit Aug 01 '14 at 21:26
  • 1
    So there's a stack and a stack frame. a stack frame. A stack frame allows access to both function parameters, and automatic function variables. It slides around depending on what function you're currently in. The whole basis is that the %o registers become the %i registers when a function is called, and then slide back at the end of the function call. This allows for things such as pass by value, pass by reference, returning by reference, etc; you could read more up on it here: http://en.wikibooks.org/wiki/X86_Disassembly/Functions_and_Stack_Frames. – Devarsh Desai Aug 01 '14 at 22:03