Salta al contenuto
Note per Studenti Interrupts and Exceptions Report

Interrupts and Exceptions

In questa pagina 6
In questa pagina 4

Definition (interrupt). An interrupt suspends the running program to execute a service routine (interrupt handler), then lets the program resume as if nothing had happened.

Class Cause
I/O a module signals the end of an operation or a request
timer when an interval expires (basis of operating-system multitasking)
hardware fault memory parity error, power failure
program (exceptions) overflow, division by zero, invalid instruction, access to protected memory, page fault (see Virtual MemoryVirtual and physical addresses, pages and frames; translation with the page table and field calculation; page fault and page replacement; TLB and effective access time; protection and sharing; comparison with the cache.Virtual Memory →), explicit call to the operating system (SVC in ARM)

Example. A division by zero is a program exception; the expiry of a timer is a timer interrupt; the end of a disk read is an I/O interrupt.

Interrupt-driven I/O

Instead of busy waiting (see Input-Output Modules and Programmed Input-OutputWhy I/O modules are needed; functions of the module and status, control and data registers; memory-mapped and isolated I/O with examples; programmed I/O with busy waiting and its cost; comparison of the three techniques.Input-Output Modules and Programmed Input-Output →): the CPU gives the command to the module and goes on with other work; when the device is ready the module raises the interrupt-request line and the CPU transfers the datum inside the service routine. The CPU still performs the transfer word by word, but does not wait.

Handling sequence

Definition (interrupt handling sequence). Hardware:

  1. the device sends the request;
  2. the CPU finishes the current instruction (the interrupt check is the last phase of the fetch-execute cycle);
  3. the CPU checks whether there are enabled requests and sends the acknowledge;
  4. it saves PC and PSW on the stack (or in dedicated registers);
  5. it loads into the PC the address of the service routine.

Software (service routine):

  1. saves the other registers it will use;
  2. serves the device;
  3. restores the registers;
  4. executes the return-from-interrupt instruction, which restores PSW and PC.

Example. With SP=TSP=T before the interrupt, after saving PC and PSW SP=T−2SP=T-2; if the routine saves another m=3m=3 registers, SP=T−5SP=T-5, and on return SP=TSP=T.

In general: the stack pointer is TT and the program is at the instruction at address NN. At the interrupt PC (already N+1N + 1, the next instruction) and PSW are saved: SP=T−2SP = T - 2. If the routine saves another mm registers, SP=T−2−mSP = T - 2 - m. On return the stack goes back to TT and the program resumes from N+1N + 1.

Identifying the source

With several devices:

Definition (identifying the source of an interrupt)

  • multiple interrupt lines: one line per device (lines are few);
  • software polling: a single routine interrogates the status registers of all modules (slow);
  • daisy chain (hardware polling): the acknowledge signal passes from one module to the next along a chain; the first with a pending request blocks it and puts its own vector on the bus (a number identifying the routine);
  • bus arbitration: the module must obtain the bus before requesting the interrupt.

Example. With three modules M1,M2,M3M_1,M_2,M_3 in a daisy chain requesting the interrupt together, the acknowledge stops at M1M_1, which puts its vector on the bus.

With the vector the CPU jumps directly to the right routine through a vector table in memory.

Priorities

  • Disabling interrupts during the routine: requests stay pending and are served afterwards, in order. Simple, but an urgent device waits.
  • Priorities: a routine can be interrupted by a request of higher priority (nested interrupts). Example: a printer (low priority), a disk (medium), a communication line that loses data if not served quickly (high).

Exceptions in ARM

Each exception has a processor mode (IRQ, FIQ for fast interrupts, Supervisor for SVC, Abort for invalid accesses, Undefined) and a fixed address in the vector table. On entry the hardware copies the CPSR into the SPSR of the mode, saves the return address in the LR of the mode (a banked register), disables IRQ and jumps to the vector. The return restores PC and CPSR together (for example SUBS pc, lr, #4 for IRQ). See The ARM Core ArchitectureThe 32-bit ARM core as the reference microcontroller core: registers r0-r15 with SP, LR and PC, the status register CPSR with flags NZCV, processor modes, the load/store model, conditional execution, the flexible second operand, Thumb, and the main differences of 64-bit ARMv8.The ARM Core Architecture →.

Typical mistakes

  • Thinking the interrupt arrives in the middle of an instruction: the CPU recognises it only between one instruction and the next (program exceptions, instead, are caused by the instruction itself).
  • Forgetting that the routine must save all the registers it modifies: the interrupted program does not know it was interrupted.

Versione ripasso

An interrupt suspends the program, runs a service routine, then resumes. Classes: I/O, timer, hardware fault, program exceptions (overflow, division by zero, invalid instruction, protected access, page fault, SVC).

Interrupt-driven I/O

The CPU commands the module and continues; on the interrupt it transfers the word in the routine (Input-Output Modules and Programmed Input-OutputWhy I/O modules are needed; functions of the module and status, control and data registers; memory-mapped and isolated I/O with examples; programmed I/O with busy waiting and its cost; comparison of the three techniques.Input-Output Modules and Programmed Input-Output →).

Sequence

Hardware: request; CPU finishes the current instruction; acknowledge; saves PC and PSW on the stack; loads the routine address into PC. Software: save used registers; serve the device; restore; return-from-interrupt (restores PSW and PC). Stack: SP=TSP = T, after PC and PSW T−2T-2, with mm more registers T−2−mT-2-m, back to TT on return; resumes at N+1N+1.

Source and priority

  • Sources: multiple lines; software polling (slow); daisy chain (first pending module blocks the acknowledge and puts its vector on the bus); bus arbitration. The vector indexes a vector table.
  • Priority: disable interrupts (urgent device waits) or nested interrupts by priority (printer low, disk medium, communication line high).

ARM

Modes IRQ, FIQ, Supervisor (SVC), Abort, Undefined, each with a fixed vector. On entry: CPSR copied to SPSR, return address into the banked LR, IRQ disabled, jump to the vector; return restores PC and CPSR (SUBS pc,lr,#4 for IRQ) (The ARM Core ArchitectureThe 32-bit ARM core as the reference microcontroller core: registers r0-r15 with SP, LR and PC, the status register CPSR with flags NZCV, processor modes, the load/store model, conditional execution, the flexible second operand, Thumb, and the main differences of 64-bit ARMv8.The ARM Core Architecture →).

Typical mistakes: interrupts are recognised only between instructions (program exceptions are caused by the instruction); the routine must save all registers it modifies.

Teoria collegata