21
www.techvilla.org.in 1 TECHVILLA www.techvilla.org.in

Interrupts Part-18

Embed Size (px)

Citation preview

Page 1: Interrupts Part-18

www.techvilla.org.in

1

TECHVILLA

www.techvilla.org.in

Page 2: Interrupts Part-18

www.techvilla.org.in

Interrupts

Page 3: Interrupts Part-18

www.techvilla.org.in

CPU Architecture

CentralProcessing

Unit

MainMemory

I/Odevice

I/Odevice

I/Odevice

I/Odevice

system bus

Page 4: Interrupts Part-18

www.techvilla.org.in

NEED OF INTERRUPTS

• Utility of a general-purpose computer depends on its ability to interact with I/O devices attached to it (e.g., keyboard, display, disk-drives, network, etc.)

• Devices require a prompt response from the CPU when various events occur, even when the CPU is busy running a program

• Need a mechanism for a device to “gain CPU’s attention”• Interrupts provide a way doing this

Page 5: Interrupts Part-18

www.techvilla.org.in

CPU’s ‘fetch-execute’ cycle

Fetch instruction at IP

Advance IP to next instruction

Decode the fetched instruction

Execute the decoded instruction

Interrupt?

no

Save context

Get INTR ID

Lookup ISR

Execute ISR

yes IRET

User Program

IP

ld

add

st

mul

ld

sub

bne

add

jmp

Page 6: Interrupts Part-18

www.techvilla.org.in

Interrupts

• Forcibly change normal flow of control• Similar to context switch (but lighter weight)

• Hardware saves some context on stack; Includes interrupted instruction if restart needed

• Enters kernel at a specific point; kernel then figures out which interrupt handler should run

• Execution resumes with special “iret” instruction

• Many different types of interrupts

Page 7: Interrupts Part-18

www.techvilla.org.in

Types of Interrupts

• Asynchronous• From external source, such as I/O device• Not related to instruction being executed

• Synchronous (also called exceptions)• Processor-detected exceptions:

• Faults — correctable; offending instruction is retried• Traps — often for debugging; instruction is not retried• Aborts — major error (hardware failure)

• Programmed exceptions:• Requests for kernel intervention (software intr/syscalls)

Page 8: Interrupts Part-18

www.techvilla.org.in

faults

• Instruction would be illegal to execute• Examples:

• Writing to a memory segment marked ‘read-only’• Reading from an unavailable memory segment (on disk)• Executing a ‘privileged’ instruction

• Detected before incrementing the IP• The causes of ‘faults’ can often be ‘fixed’• If a ‘problem’ can be remedied, then the CPU can just resume its execution-

cycle

Page 9: Interrupts Part-18

www.techvilla.org.in

Traps

• A CPU might have been programmed to automatically switch control to a ‘debugger’ program after it has executed an instruction

• That type of situation is known as a ‘trap’• It is activated after incrementing the IP

Page 10: Interrupts Part-18

www.techvilla.org.in

Error Exceptions

• Most error exceptions — divide by zero, invalid operation, illegal memory reference, etc. — translate directly into signals

• This isn’t a coincidence. . .• The kernel’s job is fairly simple: send the appropriate signal to the current

process• force_sig(sig_number, current);

• That will probably kill the process, but that’s not the concern of the exception handler

• One important exception: page fault• An exception can (infrequently) happen in the kernel

• die(); // kernel oops

Page 11: Interrupts Part-18

www.techvilla.org.in

Intel-Reserved ID-Numbers

• Of the 256 possible interrupt ID numbers, Intel reserves the first 32 for ‘exceptions’• OS’s such as Linux are free to use the remaining 224 available interrupt ID numbers for their own

purposes (e.g., for service-requests from external devices, or for other purposes such as system-calls)

• Examples:• 0: divide-overflow fault• 6: Undefined Opcode• 7: Coprocessor Not Available• 11: Segment-Not-Present fault• 12: Stack fault• 13: General Protection Exception• 14: Page-Fault Exception

Page 12: Interrupts Part-18

www.techvilla.org.in

Interrupt Hardware

x86CPU

MasterPIC

(8259)

SlavePIC

(8259)INTR

Programmable Interval-TimerKeyboard Controller

Real-Time Clock

Legacy PC Design (for single-proc

systems)

SCSI Disk

Ethernet

I/O devices have (unique or shared) Interrupt Request Lines (IRQs)

IRQs are mapped by special hardware to interrupt vectors, and passed to the CPU

This hardware is called a Programmable Interrupt Controller (PIC)

IRQs

Page 13: Interrupts Part-18

www.techvilla.org.in

The `Interrupt Controller’

• Responsible for telling the CPU when a specific external device wishes to ‘interrupt’• Needs to tell the CPU which one among several devices is the one needing service

• PIC translates IRQ to vector• Raises interrupt to CPU• Vector available in register• Waits for ack from CPU

• Interrupts can have varying priorities• PIC also needs to prioritize multiple requests

• Possible to “mask” (disable) interrupts at PIC or CPU• Early systems cascaded two 8 input chips (8259A)

Page 14: Interrupts Part-18

www.techvilla.org.in

AVR INTERRUPTS

Page 15: Interrupts Part-18

www.techvilla.org.in

Interrupt Flags and Enabled bits

• Each interrupt is associated with two (2) bits, an Interrupt Flag Bit and an Interrupt Enabled Bit. These bits are located in the I/O registers associated with the specific interrupt:

• The interrupt flag bit is set whenever the interrupt event occur, whether or not the interrupt is enabled.

• The interrupt enabled bit is used to enable or disable a specific interrupt. Basically is tells the microcontroller whether or not it should respond to the interrupt if it is triggered.

• In summary basically both the Interrupt Flag and the Interrupt Enabled are required for an interrupt request to be generated

Page 16: Interrupts Part-18

www.techvilla.org.in

Page 17: Interrupts Part-18

www.techvilla.org.in

Status I/O Register (SREG).

• Apart from the enabled bits for the specific interrupts the global interrupt enabled bit MUST be enabled for interrupts to be activated in the microcontoller.

• For the AVR 8-bits microcontroller this bit is located in the Status I/O Register (SREG). The Global Interrupt Enabled is bit 7, the I bit, in the SREG.

Page 18: Interrupts Part-18

www.techvilla.org.in

Interrupt sources provided with the AVR• The AVR 8-bits microcontroller provide both internal and external

interrupt sources. • There are four external interrupts:• The RESET interrupt - Triggered from pin 9.• External Interrupt 0 (INT0) - Triggered from pin 16.• External Interrupt 1 (INT1) - Triggered from pin 17.• External Interrupt 2 (INT2) - Triggered from pin 3.

Page 19: Interrupts Part-18

www.techvilla.org.in

Servicing an interrupt

• The microcontroller completes the execution of the current instruction, clears the I bit and stores the address of the next instruction that should have been executed (the content of the PC) on the stack.

• The interrupt vector of the triggered interrupt is then loaded in the PC and the microcontroller starts execution from that point up until is reaches a RETI instruction.

• Upon the the execution of the RETI instruction the address that was stored on the stack in step 1 is reloaded in the PC and the I bit is re-enabled.

Page 20: Interrupts Part-18

www.techvilla.org.in

Avr interrupts

• The microcontroller then start executing instructions from that point. That is the point that it left off when the interrupt was triggered.

Page 21: Interrupts Part-18

www.techvilla.org.in

Interrupt vector table