Docsity

Interrupt Handling and CPU Management in x86 Architecture, Slides of Computer Applications

Partial preview of the text

Download Interrupt Handling and CPU Management in x86 Architecture and more Slides Computer Applications in PDF only on Docsity!

Exceptions and Interrupts

How does Linux handle service- requests from the cpu and from the peripheral devices?

Rationale

  • Usefulness of a general-purpose computer is dependent on its ability to interact with various peripheral devices attached to it (e.g., keyboard, display, disk-drives, etc.)
  • Devices require a prompt response from the cpu when various events occur, even when the cpu is busy running a program
  • The x86 interrupt-mechanism provides this

The ‘fetch-execute’ cycle

Normal programming assumes this ‘cycle’:

    1. Fetch the next instruction from ram
    1. Interpret the instruction just fetched
    1. Execute this instruction as decoded
    1. Advance the cpu instruction-pointer
    1. Go back to step 1

But ‘departures’ may occur

  • Circumstances may arise under which it would not be appropriate for the CPU to just proceed with this fetch-execute cycle
  • Examples:
    • An ‘external device’ might ask for service
    • An interpreted instruction could be ‘illegal’
    • An instruction ‘trap’ may have been set

Fault-Handling

  • The causes of ‘faults’ often can be ‘fixed’
  • A few examples:
      1. Writing to a ‘read-only’ segment
      1. Reading from a ‘not present’ segment
      1. Executing an out-of-bounds instruction
      1. Executing a ‘privileged’ instruction
  • If a ‘problem’ can be remedied, then the CPU can just resume its execution-cycle

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
  • It is accomplished by setting the TF flag
  • Just as with faults, the cpu will react:

save return-info, + jump to trap-handler

Faults vs Traps (continued)

  • With a ‘fault’:

the saved address is for the instruction which triggered the fault – so it will be that instruction which gets re-fetched after the cause of the problem has been corrected

  • With a ‘trap’:

the saved address is for the instruction following the one which triggered the trap

Stack’s layout

ESP EFLAGS

EIP

SS

CS

SS:ESP

In case the CPU gets interrupted while it is executing a user application, then the CPU will switch to a new ‘kernel-mode’ stack before saving its current register-values for EFLAGS, CS, and EIP, and in fact will begin by saving on the kernel-mode stack the register-values for SS and ESP which point to the top of user-mode stack (so it can be restored later on)

Interrupt Handling

  • As with faults and traps, the cpu responds to ‘interrupt’ requests by saving some info on its kernel stack, and then jumping to a special ‘interrupt-handler’ routine designed to take appropriate action for the particular device which caused the interrupt to occur
  • The ‘entry-point’ to the interrupt-handler is located via the Interrupt Descriptor Table

The ‘Interrupt Controller’

  • Special hardware is responsible for telling the CPU when a specific external device wishes to ‘interrupt’ the current program
  • This hardware is the ‘Interrupt Controller’
  • It needs to tell the cpu which one among several devices is the one needing service
  • It also needs to prioritize multiple requests

Three crucial data-structures

  • The Global Descriptor Table (GDT) defines the system’s memory-segments and their access-privileges, which the CPU has the duty to enforce
  • The Interrupt Descriptor Table (IDT) defines entry-points for the various code-routines that will handle all ‘interrupts’ and ‘exceptions’
  • The Task-State Segment (TSS) holds the values for registers SS and ESP that will get loaded by the CPU upon entering kernel-mode

How does CPU find GDT/IDT?

  • Two dedicated registers: GDTR and IDTR
  • Both have identical 48-bit formats:

Segment Base Address Segment Limit 47 1615 0

Privileged instructions: LGDT and LIDT used to set these register-values Unprivileged instructions: SGDT and SIDT used for reading register-values

Kernel must setup these registers during system startup (set-and-forget)

Segment-Descriptor Format

Base[ 15 … 0 ] Limit[ 15 ... 0 ]

31 16 15 0

63 56 39 32

Base[31…24] (^) [19..16]Limit attributes^ Access Base[23…16]

Quadword (64-bits)

Gate-Descriptor Format

Entrypoint Offset[ 31…16 ]

Code-segment Selector Entrypoint Offset[ 15…0 ]

Quadword (64-bits)

0

63

31

32 Gate type code (Reserved)