CS703 Advanced
Operating Systems
By Mr. Farhan Zaidi
Lecture No.
37
Overview of today’s lecture
Interrupt handlers
Interrupts and exceptions
Linux interrupt handling
Top halfs, bottom halfs and tasklets
Timings and timer devices
Linux kernel timers and interval timers
Interrupt Handlers
Interrupt handlers are best hidden
Interrupt procedure does its task
Interrupts vs Exceptions
Varying terminology but for Intel:
Interrupt (device generated)
Maskable: device-generated, associated with IRQs (interrupt
request lines); may be temporarily disabled (still pending)
Nonmaskable: some critical hardware failures
Exceptions (Processor-detected)
Faults – correctable (restartable); e.g. page fault
Traps – no reexecution needed; e.g. breakpoint
Aborts – severe error; process usually terminated (by
signal)
Programmed exceptions (software interrupts)
int (system call), int3 (breakpoint)
into (overflow), bounds (address check)
Interrupts and Exceptions
Hardware support for getting CPUs attention
Often transfers from user to kernel mode
Asynchronous: device or timer generated
Synchronous: immediate result of last instruction
Intel terminology and hardware
Irqs, vectors, IDT, gates, PIC, APIC
Interrupt Handling
More complex than exceptions
Some issues:
Requires registry, deferred processing, etc.
IRQs are often shared; all handlers (ISRs) are executed so they
must query device
Three types of actions:
Critical: Top-half (interrupts disabled – briefly!)
Non-critical: Top-half (interrupts enabled)
Non-critical deferrable: Do it “later” (interrupts enabled)
Interrupt Handling Components
IRQs
vector
Memory Bus
0
PIC
INTR
CPU
idtr
0
IDT
15
Mask points
255
handler
Timing and Timers
Accurate timing crucial for many aspects of OS
Intel timer hardware
Device-related timeouts
File timestamps (created, accessed, written)
Time-of-day (gettimeofday()
High-precision timers (code profiling, etc.)
Scheduling, cpu usage, etc.
RTC: Real Time Clock
PIT: Programmable Interrupt Timer
TSC: TimeStamp Counter (cycle counter)
Local APIC Timer: per-cpu alarms
Timer implementations
Kernel timers (dynamic timers)
User “interval” timers (alarm(), setitimer())
Dynamic timers may be dynamically created and destroyed. No limit is
placed on the number of currently active dynamic timers.
A dynamic timer is stored in the following timer_list structure:
struct timer_list
{
struct list_head list;
unsigned long expires;
unsigned long data;
void (*function)(unsigned long);
};
Software timers in Linux
Each timer contains a field that indicates how far in the future the timer
should expire. This field is initially calculated by adding the right number
of ticks to the current value of jiffies.
The field does not change. Every time the kernel checks a timer, it
compares the expiration field to the value of jiffies at the current
moment, and the timer expires when jiffies is greater or equal to the
stored value.
User Mode Interval Timers
setitimer(): 3 distinct user mode interval timers
three timers:
can set for one-time or periodic alarm
sends signals on timer expiry
ITIMER_REAL: elapsed time (SIGALRM)
ITIMER_VIRT: user (SIGVTALRM)
ITIMER_PROF: user + kernel (SIGPROF)
implementations:
VIRT, PROF – updates by PIT or APIC
REAL – requires kernel timer
may need to deliver to blocked process
current->real_timer
shared by alarm() API so can’t use both