University
of
Washington
Control
Flow
¢ Processors
do
only
one
thing:
§ From
startup
to
shutdown,
a
CPU
simply
reads
and
executes
(interprets)
a
sequence
of
instruc=ons,
one
at
a
=me
§ This
sequence
is
the
CPU’s
control
flow
(or
flow
of
control)
Physical
control
flow
<startup>
inst1
inst2
<me
inst3
…
instn
<shutdown>
Excep<onal
Control
Flow
University
of
Washington
Altering
the
Control
Flow
¢ Up
to
now:
two
ways
to
change
control
flow:
§ Jumps
(condi=onal
and
uncondi=onal)
§ Call
and
return
Both
react
to
changes
in
program
state
¢ Processor
also
needs
to
react
to
changes
in
system
state
§ user
hits
“Ctrl-‐C”
at
the
keyboard
§ user
clicks
on
a
different
applica=on’s
window
on
the
screen
§ data
arrives
from
a
disk
or
a
network
adapter
§ instruc=on
divides
by
zero
§ system
=mer
expires
¢ Can
jumps
and
procedure
calls
achieve
this?
§ Jumps
and
calls
are
not
sufficient
–
the
system
needs
mechanisms
for
“excep/onal”
control
flow!
Excep<onal
Control
Flow
University
of
Washington
Excep<ons
¢ An
excep5on
is
transfer
of
control
to
the
opera<ng
system
(OS)
in
response
to
some
event
(i.e.,
change
in
processor
state)
User
Process
OS
event
I_current
excep/on
I_next
excep/on
processing
by
excep5on
•
return
to
I_current
handler
• return
to
I_next
• abort
¢ Examples:
div
by
0,
page
fault,
I/O
request
completes,
Ctrl-‐C
¢ How
does
the
system
know
where
to
jump
to
in
the
OS?
Excep<onal
Control
Flow
University
of
Washington
Interrupt
Vectors
Excep<on
numbers
code
for
¢ Each
type
of
event
has
a
excep<on
handler
0
unique
excep<on
number
k
Excep<on
code
for
Table
excep<on
handler
1
¢ k
=
index
into
excep<on
table
0
1
(a.k.a.
interrupt
vector)
code
for
2 excep<on
handler
2
... ¢ Handler
k
is
called
each
<me
n-1 ...
excep<on
k
occurs
code
for
excep<on
handler
n-‐1
Excep<onal
Control
Flow
University
of
Washington
Asynchronous
Excep<ons
(Interrupts)
¢ Caused
by
events
external
to
the
processor
§ Indicated
by
seTng
the
processor’s
interrupt
pin(s)
§ Handler
returns
to
“next”
instruc=on
¢ Examples:
§ I/O
interrupts
hiTng
Ctrl-‐C
on
the
keyboard
§
§ clicking
a
mouse
buVon
or
tapping
a
touchscreen
§ arrival
of
a
packet
from
a
network
§ arrival
of
data
from
a
disk
§ Hard
reset
interrupt
§ hiTng
the
reset
buVon
on
front
panel
§ SoX
reset
interrupt
§ hiTng
Ctrl-‐Alt-‐Delete
on
a
PC
Excep<onal
Control
Flow
University
of
Washington
Synchronous
Excep<ons
¢ Caused
by
events
that
occur
as
a
result
of
execu<ng
an
instruc<on:
§ Traps
Inten=onal:
transfer
control
to
OS
to
perform
some
func=on
§
§ Examples:
system
calls,
breakpoint
traps,
special
instruc=ons
§ Returns
control
to
“next”
instruc=on
§ Faults
§ Uninten=onal
but
possibly
recoverable
§ Examples:
page
faults
(recoverable),
segment
protec=on
faults
(unrecoverable),
integer
divide-‐by-‐zero
excep=ons
(unrecoverable)
§ Either
re-‐executes
faul=ng
(“current”)
instruc=on
or
aborts
§ Aborts
§ Uninten=onal
and
unrecoverable
§ Examples:
parity
error,
machine
check
§ Aborts
current
program
Excep<onal
Control
Flow
University
of
Washington
Trap
Example:
Opening
File
¢ User
calls:
open(filename, options)
¢ Func=on
open
executes
system
call
instruc=on
int
0804d070 <__libc_open>:
. . .
804d082: cd 80 int $0x80
804d084: 5b pop %ebx
. . .
User
Process
OS
int
excep/on
pop
open
file
returns
¢ OS
must
find
or
create
file,
get
it
ready
for
reading
or
wri=ng
¢ Returns
integer
file
descriptor
Excep<onal
Control
Flow
University
of
Washington
Fault
Example:
Page
Fault
int a[1000];
¢ User
writes
to
memory
loca=on
main ()
{
¢ That
por=on
(page)
of
user’s
memory
a[500] = 13;
is
currently
on
disk
}
80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d10
User
Process
OS
excep/on:
page
fault
movl
Create
page
and
returns
load
into
memory
¢ Page
handler
must
load
page
into
physical
memory
¢ Returns
to
faul=ng
instruc=on:
mov
is
executed
again!
¢ Successful
on
second
try
Excep<onal
Control
Flow
University
of
Washington
Fault
Example:
Invalid
Memory
Reference
int a[1000];
main ()
{
a[5000] = 13;
}
80483b7: c7 05 60 e3 04 08 0d movl $0xd,0x804e360
User
Process
OS
excep/on:
page
fault
movl
detect
invalid
address
signal
process
¢ Page
handler
detects
invalid
address
¢ Sends
SIGSEGV
signal
to
user
process
¢ User
process
exits
with
“segmenta=on
fault”
Excep<onal
Control
Flow
University
of
Washington
Summary
¢ Excep<ons
§ Events
that
require
non-‐standard
control
flow
§ Generated
externally
(interrupts)
or
internally
(traps
and
faults)
§ AXer
an
excep=on
is
handled,
one
of
three
things
may
happen:
§ Re-‐execute
the
current
instruc=on
§ Resume
execu=on
with
the
next
instruc=on
§ Abort
the
process
that
caused
the
excep=on
Excep<onal
Control
Flow