Download Devices and Interrupts - Computation Structures - Lecture Slides and more Slides Computer Fundamentals in PDF only on Docsity!
L19 – Devices & Interrupts 1
4/16/
Devices & Interrupts
Lab #6 due tonight!
Loop:
LD(R3,0,R0)ADDC(R3,4,R3)SUBC(R2,1,R2)BNE(R2,Loop)…
(Cough) Excuse me, sir…
modified 4/13/09 10:
L19 – Devices & Interrupts 2
6.004 – Spring 2009
4/16/
Why an OS?
What we’ve got:
^
A Single Sequence Machine, capable of doing ONE thing at a time – oneinstruction, one I/O operation, one program.
^
A universe of gadgets – e.g. I/O devices – that do similar thingsslightly differently.
What we’d like:
^
To listen to MP3s while reading email.
^
To access disk, network, and screen “simultaneously”.
^
To write a single program that does I/O with anybody’s disk.
Plausible approaches:
^
An infinite supply of identical computers withuniform, high-level peripherals for everyconceivable purpose… or
^
An illusion: Make one real computer look likemany “virtual” ones.
L19 – Devices & Interrupts 3
4/16/
Operating Systems
An OS is the Glue that holds a
computer together.
competing requests
names/bindings- Maintainsorder/fairness
KERNEL - a RESIDENT portion
of the O/S that handles themost common andfundamental servicerequests.
vir.tu.al
'vrch-(-)wl, 'vr-chl\ .vr-ch-'wal-t-e-\ 'vrch-(-)w-le-, 'vrch-(*-)le-
\ aj [ME, possessed of certain physical virtues, fr. ML virtualis, fr. L virtus strength,virtue : being in essence or effect but not in fact - vir.tu.al.i.ty n
Hardware
Registers
ALUs
PCs
Caches
Kernel
ApplicationsOperating System
Device Drivers
Scheduler
Process Control
Blocks (PCBs)
Shared Libraries
Word Processors
Graphical UserInterface (GUI)
Games
Spread Sheets
Web
Browser
Page Tables DeviceQueues
Network Interfaces
Security
File system
I/O Devices
L19 – Devices & Interrupts 4
6.004 – Spring 2009
4/16/
OS organization
Applications” are quasi-parallel
“PROCESSES”
on
“VIRTUAL MACHINES”,
each with:
^
CONTEXT (virtual address space)
^
Virtual I/O devices
O.S. KERNEL has:
^
Interrupt handlers
^
SVC (trap) handlers
^
Scheduler
^
PCB structures containing the state of inactive processes
Scheduler
P1 KERNEL
P
SVC 0 handlerSVC 1 handler
I/O Handler
Device
0
Alarm Clock
I/O Handler
Device
1
…DPYNum=0…DPYNum=
PCBs:
P1: P2:
loop:SVC(0)
...SVC(1)...BR(loop)
loop:SVC(0)
...SVC(1)...BR(loop)
L19 – Devices & Interrupts 5
4/16/
Asynchronous I/O Handling
SVC call from application
Application:
…ReadKey()
| read key into R
…
“A”...
Device Buffer
(in OS Kernel)
KEYhit_h() {
(read ASCII code, put in buffer) }
INTERRUPT from Keyboard n
INTERRUPT
to OS
IN
OUT
ReadKEY_h() {
(remove next char from buffer,return in R0)
…}
TRAP to OS
IN
OUT
L19 – Devices & Interrupts 6
6.004 – Spring 2009
4/16/
Interrupt-based Asynchronous I/O
struct
Device
char
Flag,
Data;
Keyboard;
KEYhit_h()
Buffer[inptr]
Keyboard.Data;
inptr
(inptr
BUFSIZE;
OPERATION: NO attention to Keyboard during normal operation
- on key strike: hardware asserts IRQ to request interrupt• USER program interrupted, PC+4 of interrupted inst. saved in XP• state of USER program saved on KERNEL stack;• KeyboardHandler invoked, runs to completion;• state of USER program restored; program resumes.
TRANSPARENT to USER program.Keyboard Interrupt Handler (in O.S. KERNEL):
Assume eachkeyboard hasan associatedbuffer
L19 – Devices & Interrupts 7
4/16/
ReadKey SVC: Attempt
A
supervisor call
(SVC) is an instruction that transfers control to the kernel so
it can satisfy some user request. Kernel returns to user program whenrequest is complete.
First draft of a ReadKey SVC handler (supporting a
Virtual
Keyboard): returns
next keystroke on a user’s keyboard to that user’s requesting application:
Problem: Can’t interrupt code running in the supervisor mode…so the buffer never gets filled.
ReadKEY_h(){
int
kbdnum = ProcTbl[Cur].DPYNum;
while
(BufferEmpty(kbdnum)) {
*busy wait loop /
}User.Regs[0] = ReadInputBuffer(kbdnum);
ReadKEY_h(){ }
int
kbdnum = ProcTbl[Cur].DPYNum;
while
(BufferEmpty(kbdnum)) {
*busy wait loop /
}User.Regs[0] = ReadInputBuffer(kbdnum);
L19 – Devices & Interrupts 8
6.004 – Spring 2009
4/16/
ReadKey SVC: Attempt
A BETTER keyboard SVC handler:
ReadKEY_h(){
int kbdnum
ProcTbl[Cur].DPYNum;
if (BufferEmpty(kbdnum))
/ busy*
wait
loop
User.Regs[XP]
User.Regs[XP]-4;
} else
User.Regs[0]
ReadInputBuffer(kbdnum);
That’s a funny way
to write
a loop
Problem: The process just wastes its time-slice waiting for
someone to hit a key...
This one actually works!
L19 – Devices & Interrupts 13
4/16/
Interrupt Latency
One way to measure the real-time performance of a system is
INTERRUPT
LATENCY:
^
HOW MUCH TIME can elapse between an interrupt request and theSTART of its handler?
time
Request
Latency
Service
Time
Deadline?
OFTEN bad things happen when service is delayed beyond
some deadline - "real time" considerations:
Missed charactersSystem crashesNuclear meltdowns
“HARD” Real time constraints
L19 – Devices & Interrupts 14
6.004 – Spring 2009
4/16/
Sources of Interrupt Latency
What causes interrupt latency:
^
State save, context switch.
^
Periods of uninterruptability:
^
Long, uninterruptable instructions -- eg block moves, multi-level indirection.
^
Explicitly disabled periods (eg for atomicity, during service of otherinterrupts).
GOAL: BOUND (and minimize) interrupt latency!
^
Optimize interrupt sequence context switch
^
Make unbounded-time instructions INTERRUPTABLE (state in registers, etc).
^
Avoid/minimize disable time
^
Allow handlers to be interrupted, in certain cases (while still avoiding reentranthandlers!).
time
Request
Latency
Service
Time
Deadline?
We can consider thiswhen we write our O/S
We can addressthis in our ISA
But, this isapplicationdependent!
L19 – Devices & Interrupts 15
4/16/
Interrupt Disable/Enable
E
Interrupt
Enable/
Disable bit
(Misc. other stuff: Context, K/U mode,
etc.)
INTERRUPT DISABLEBIT (part of processorstatus)... in PC:Often in separateP
rocessor
S
tatus
W
ord ...
E=1: DISABLEDE=0: ENABLEDe.g.
^
BETA K-mode bit(disables interrupts,other functions)
^
Often separate bit/mechanism
PC
TYPICAL OPERATION: (as with Beta K-mode bit):
^
ONLY take interrupts if E=0; else defer.
^
SAVE OLD E on interrupt, install new E from interrupt vector (alongwith PC, etc). New E=1 (to disable interrupts during handler).
^
Run handler, with interrupts disabled.
^
On JMP (at return from handler), saved state restored to processor,resuming interrupted program (with E=0 again).
L19 – Devices & Interrupts 16
6.004 – Spring 2009
4/16/
Scheduling of Multiple Devices
DEVICE
Keyboard
Disk
Printer
Service
Time 800500400
Actual w/c
Latency
"TOY" System scenario:What is the WORST CASE latency seen by each device?
Req: K,P,D
P
K
D
Req: D,P,K
D
P
K
Req: K,D,P
K
D
P
Assumptions:
^
Infrequent interrupt requests (each happens only once/scenario)
^
Simultaneous requests might be served in ANY order…. Whence
^
Service of EACH device might be delayed by ALL others!… can we improve this?
L19 – Devices & Interrupts 17
4/16/
Weak (non-preemptive) Priorities
ISSUE: Processor becomes
interruptable (at fetch of nextinstruction), several interruptrequests are pending. Which isserved first?
LATENCIES with WEAK PRIORITIES:
Service of each device might be
delayed by:
Service of 1 other (arbitrary)device, whose interrupt requestwas just honored; PLUS
Service of ALL higher-prioritydevices.
D
P
K
Req: P,D,K
Req:K,P,D
K
P
D
DEVICE
Keyboard
Disk Printer
Service
Time 800500400
Actual w/c
Latency
WEAK PRIORITY ORDERING: Check in prescribed sequence, eg:
DISK > PRINTER > KEYBOARD.
vs 1200 –
Now delayed by only 1 service!
L19 – Devices & Interrupts 18
6.004 – Spring 2009
4/16/
The Need for Preemption
Without preemption, ANY interrupt service can delay ANY other service
request… the slowest service time constrains response to fastestdevices. Often, tight deadlines can’t be met using this scheme alone.
EXAMPLE: 800 uSec deadline (hence 300 uSec maximum interrupt
latency) on disk service, to avoid missing next sector...
DEVICEKeybrd
Disk
Printer
Serv.Time 800500400
Actual Latency
Max. Delay^300
need PREEMPTION: Allow handlers for LOWER PRIORITY interrupts to be
interrupted by HIGHER priority requests!
Latency
w/preemption
Priority
[D] 500
D,P 900
CAN'T SATISFY the disk requirement in this system using weak priorities!
L19 – Devices & Interrupts 19
4/16/
Strong Priority Implementation
SCHEME:
^
Expand E bit in PC to be a PRIORITY integer PRI(eg, 3 bits for 8 levels)
^
ASSIGN a priority to each device.
^
Prior to each instruction execution:
Find priority P
i^
of highest requesting device, say D
i
Take interrupt if and only if P
i^
> PRI, set PRI = P
.i
Strong priorities:
KEY: Priority in Processor stateAllows interruption of (certain) handlersAllows preemption, but not reentranceBENEFIT: Latency seen at high priorities UNAFFECTED by service times
at low priorities.
PC:
Program Counter
PRI
L19 – Devices & Interrupts 20
6.004 – Spring 2009
4/16/
Recurring Interrupts
DEVICEKeybrd
Disk
Printer
Serv.Time^800500400
Actual Latency
Max.Delay 300
P 3 5 4
Max.Freq 100/s500/s 1000/s
Consider interrupts which recur at bounded rates:
N
o
te that interrupt LATENCIES don't tell
the whole story—consider COMPLETIONTIMES, eg for Keyboard in example to theright.Keyboard service not complete until 3msafter request. Often
deadlines
used
rather than max. delays.
DPK
P
PD
P
PD
D
P
K
D
P
D
P
P