<s>
In	O
computer	O
systems	B-Application
programming	I-Application
,	O
an	O
interrupt	B-General_Concept
handler	I-General_Concept
,	O
also	O
known	O
as	O
an	O
interrupt	B-General_Concept
service	I-General_Concept
routine	I-General_Concept
or	O
ISR	O
,	O
is	O
a	O
special	O
block	O
of	O
code	O
associated	O
with	O
a	O
specific	O
interrupt	B-Application
condition	O
.	O
</s>
<s>
Interrupt	B-General_Concept
handlers	I-General_Concept
are	O
initiated	O
by	O
hardware	O
interrupts	B-Application
,	O
software	O
interrupt	B-Application
instructions	O
,	O
or	O
software	O
exceptions	B-General_Concept
,	O
and	O
are	O
used	O
for	O
implementing	O
device	B-Application
drivers	I-Application
or	O
transitions	O
between	O
protected	O
modes	O
of	O
operation	O
,	O
such	O
as	O
system	B-Operating_System
calls	I-Operating_System
.	O
</s>
<s>
The	O
traditional	O
form	O
of	O
interrupt	B-General_Concept
handler	I-General_Concept
is	O
the	O
hardware	O
interrupt	B-General_Concept
handler	I-General_Concept
.	O
</s>
<s>
Hardware	O
interrupts	B-Application
arise	O
from	O
electrical	O
conditions	O
or	O
low-level	O
protocols	O
implemented	O
in	O
digital	O
logic	O
,	O
are	O
usually	O
dispatched	O
via	O
a	O
hard-coded	O
table	O
of	O
interrupt	B-General_Concept
vectors	I-General_Concept
,	O
asynchronously	O
to	O
the	O
normal	O
execution	O
stream	O
(	O
as	O
interrupt	B-Application
masking	O
levels	O
permit	O
)	O
,	O
often	O
using	O
a	O
separate	O
stack	O
,	O
and	O
automatically	O
entering	O
into	O
a	O
different	O
execution	O
context	O
(	O
privilege	O
level	O
)	O
for	O
the	O
duration	O
of	O
the	O
interrupt	B-General_Concept
handler	I-General_Concept
's	O
execution	O
.	O
</s>
<s>
In	O
general	O
,	O
hardware	O
interrupts	B-Application
and	O
their	O
handlers	O
are	O
used	O
to	O
handle	O
high-priority	O
conditions	O
that	O
require	O
the	O
interruption	O
of	O
the	O
current	O
code	O
the	O
processor	B-General_Concept
is	O
executing	O
.	O
</s>
<s>
Later	O
it	O
was	O
found	O
convenient	O
for	O
software	O
to	O
be	O
able	O
to	O
trigger	O
the	O
same	O
mechanism	O
by	O
means	O
of	O
a	O
software	O
interrupt	B-Application
(	O
a	O
form	O
of	O
synchronous	O
interrupt	B-Application
)	O
.	O
</s>
<s>
Rather	O
than	O
using	O
a	O
hard-coded	O
interrupt	B-Application
dispatch	O
table	O
at	O
the	O
hardware	O
level	O
,	O
software	O
interrupts	B-Application
are	O
often	O
implemented	O
at	O
the	O
operating	B-General_Concept
system	I-General_Concept
level	O
as	O
a	O
form	O
of	O
callback	O
function	O
.	O
</s>
<s>
Interrupt	B-General_Concept
handlers	I-General_Concept
have	O
a	O
multitude	O
of	O
functions	O
,	O
which	O
vary	O
based	O
on	O
what	O
triggered	O
the	O
interrupt	B-Application
and	O
the	O
speed	O
at	O
which	O
the	O
interrupt	B-General_Concept
handler	I-General_Concept
completes	O
its	O
task	O
.	O
</s>
<s>
For	O
example	O
,	O
pressing	O
a	O
key	O
on	O
a	O
computer	B-Device
keyboard	I-Device
,	O
or	O
moving	O
the	O
mouse	B-Device
,	O
triggers	O
interrupts	B-Application
that	O
call	O
interrupt	B-General_Concept
handlers	I-General_Concept
which	O
read	O
the	O
key	O
,	O
or	O
the	O
mouse	B-Device
's	O
position	O
,	O
and	O
copy	O
the	O
associated	O
information	O
into	O
the	O
computer	O
's	O
memory	O
.	O
</s>
<s>
An	O
interrupt	B-General_Concept
handler	I-General_Concept
is	O
a	O
low-level	O
counterpart	O
of	O
event	O
handlers	O
.	O
</s>
<s>
However	O
,	O
interrupt	B-General_Concept
handlers	I-General_Concept
have	O
an	O
unusual	O
execution	O
context	O
,	O
many	O
harsh	O
constraints	O
in	O
time	O
and	O
space	O
,	O
and	O
their	O
intrinsically	O
asynchronous	O
nature	O
makes	O
them	O
notoriously	O
difficult	O
to	O
debug	O
by	O
standard	O
practice	O
(	O
reproducible	O
test	O
cases	O
generally	O
do	O
n't	O
exist	O
)	O
,	O
thus	O
demanding	O
a	O
specialized	O
skillset	O
—	O
an	O
important	O
subset	O
of	O
system	B-Application
programming	I-Application
—	O
of	O
software	O
engineers	O
who	O
engage	O
at	O
the	O
hardware	O
interrupt	B-Application
layer	O
.	O
</s>
<s>
Unlike	O
other	O
event	O
handlers	O
,	O
interrupt	B-General_Concept
handlers	I-General_Concept
are	O
expected	O
to	O
set	O
interrupt	B-Application
flags	O
to	O
appropriate	O
values	O
as	O
part	O
of	O
their	O
core	O
functionality	O
.	O
</s>
<s>
Even	O
in	O
a	O
CPU	O
which	O
supports	O
nested	O
interrupts	B-Application
,	O
a	O
handler	O
is	O
often	O
reached	O
with	O
all	O
interrupts	B-Application
globally	O
masked	O
by	O
a	O
CPU	O
hardware	O
operation	O
.	O
</s>
<s>
In	O
this	O
architecture	O
,	O
an	O
interrupt	B-General_Concept
handler	I-General_Concept
would	O
normally	O
save	O
the	O
smallest	O
amount	O
of	O
context	O
necessary	O
,	O
and	O
then	O
reset	O
the	O
global	O
interrupt	B-Application
disable	O
flag	O
at	O
the	O
first	O
opportunity	O
,	O
to	O
permit	O
higher	O
priority	O
interrupts	B-Application
to	O
interrupt	B-Application
the	O
current	O
handler	O
.	O
</s>
<s>
It	O
is	O
also	O
important	O
for	O
the	O
interrupt	B-General_Concept
handler	I-General_Concept
to	O
quell	O
the	O
current	O
interrupt	B-Application
source	O
by	O
some	O
method	O
(	O
often	O
toggling	O
a	O
flag	O
bit	O
of	O
some	O
kind	O
in	O
a	O
peripheral	O
register	O
)	O
so	O
that	O
the	O
current	O
interrupt	B-Application
is	O
n't	O
immediately	O
repeated	O
on	O
handler	O
exit	O
,	O
resulting	O
in	O
an	O
infinite	O
loop	O
.	O
</s>
<s>
Exiting	O
an	O
interrupt	B-General_Concept
handler	I-General_Concept
with	O
the	O
interrupt	B-Application
system	O
in	O
exactly	O
the	O
right	O
state	O
under	O
every	O
eventuality	O
can	O
sometimes	O
be	O
an	O
arduous	O
and	O
exacting	O
task	O
,	O
and	O
its	O
mishandling	O
is	O
the	O
source	O
of	O
many	O
serious	O
bugs	O
,	O
of	O
the	O
kind	O
that	O
halt	O
the	O
system	O
completely	O
.	O
</s>
<s>
Formal	O
validation	O
of	O
interrupt	B-General_Concept
handlers	I-General_Concept
is	O
tremendously	O
difficult	O
,	O
while	O
testing	O
typically	O
identifies	O
only	O
the	O
most	O
frequent	O
failure	O
modes	O
,	O
thus	O
subtle	O
,	O
intermittent	O
bugs	O
in	O
interrupt	B-General_Concept
handlers	I-General_Concept
often	O
ship	O
to	O
end	O
customers	O
.	O
</s>
<s>
In	O
a	O
modern	O
operating	B-General_Concept
system	I-General_Concept
,	O
upon	O
entry	O
the	O
execution	O
context	O
of	O
a	O
hardware	O
interrupt	B-General_Concept
handler	I-General_Concept
is	O
subtle	O
.	O
</s>
<s>
For	O
reasons	O
of	O
performance	O
,	O
the	O
handler	O
will	O
typically	O
be	O
initiated	O
in	O
the	O
memory	O
and	O
execution	O
context	O
of	O
the	O
running	O
process	O
,	O
to	O
which	O
it	O
has	O
no	O
special	O
connection	O
(	O
the	O
interrupt	B-Application
is	O
essentially	O
usurping	O
the	O
running	O
context	O
—	O
process	O
time	O
accounting	O
will	O
often	O
accrue	O
time	O
spent	O
handling	O
interrupts	B-Application
to	O
the	O
interrupted	O
process	O
)	O
.	O
</s>
<s>
However	O
,	O
unlike	O
the	O
interrupted	O
process	O
,	O
the	O
interrupt	B-Application
is	O
usually	O
elevated	O
by	O
a	O
hard-coded	O
CPU	O
mechanism	O
to	O
a	O
privilege	O
level	O
high	O
enough	O
to	O
access	O
hardware	O
resources	O
directly	O
.	O
</s>
<s>
In	O
a	O
low-level	O
microcontroller	O
,	O
the	O
chip	O
might	O
lack	O
protection	O
modes	O
and	O
have	O
no	O
memory	B-General_Concept
management	I-General_Concept
unit	I-General_Concept
(	O
MMU	O
)	O
.	O
</s>
<s>
In	O
these	O
chips	O
,	O
the	O
execution	O
context	O
of	O
an	O
interrupt	B-General_Concept
handler	I-General_Concept
will	O
be	O
essentially	O
the	O
same	O
as	O
the	O
interrupted	O
program	O
,	O
which	O
typically	O
runs	O
on	O
a	O
small	O
stack	O
of	O
fixed	O
size	O
(	O
memory	O
resources	O
have	O
traditionally	O
been	O
extremely	O
scant	O
at	O
the	O
low	O
end	O
)	O
.	O
</s>
<s>
Nested	O
interrupts	B-Application
are	O
often	O
provided	O
,	O
which	O
exacerbates	O
stack	O
usage	O
.	O
</s>
<s>
A	O
primary	O
constraint	O
on	O
the	O
interrupt	B-General_Concept
handler	I-General_Concept
in	O
this	O
programming	O
endeavour	O
is	O
to	O
not	O
exceed	O
the	O
available	O
stack	O
in	O
the	O
worst-case	O
condition	O
,	O
requiring	O
the	O
programmer	O
to	O
reason	O
globally	O
about	O
the	O
stack	O
space	O
requirement	O
of	O
every	O
implemented	O
interrupt	B-General_Concept
handler	I-General_Concept
and	O
application	O
task	O
.	O
</s>
<s>
When	O
allocated	O
stack	O
space	O
is	O
exceeded	O
(	O
a	O
condition	O
known	O
as	O
a	O
stack	B-Error_Name
overflow	I-Error_Name
)	O
,	O
this	O
is	O
not	O
normally	O
detected	O
in	O
hardware	O
by	O
chips	O
of	O
this	O
class	O
.	O
</s>
<s>
This	O
will	O
catch	O
the	O
majority	O
of	O
stack	B-Error_Name
overflow	I-Error_Name
conditions	O
at	O
a	O
point	O
in	O
time	O
close	O
to	O
the	O
offending	O
operation	O
.	O
</s>
<s>
If	O
no	O
special	O
system	O
stack	O
is	O
provided	O
for	O
interrupts	B-Application
,	O
interrupts	B-Application
will	O
consume	O
stack	O
space	O
from	O
whatever	O
thread	O
of	O
execution	O
is	O
interrupted	O
.	O
</s>
<s>
These	O
designs	O
usually	O
contain	O
an	O
MMU	O
,	O
and	O
the	O
user	O
stacks	O
are	O
usually	O
configured	O
such	O
that	O
stack	B-Error_Name
overflow	I-Error_Name
is	O
trapped	O
by	O
the	O
MMU	O
,	O
either	O
as	O
a	O
system	O
error	O
(	O
for	O
debugging	O
)	O
or	O
to	O
remap	O
memory	O
to	O
extend	O
the	O
space	O
available	O
.	O
</s>
<s>
In	O
systems	O
supporting	O
high	O
thread	O
counts	O
,	O
it	O
is	O
better	O
if	O
the	O
hardware	O
interrupt	B-Application
mechanism	O
switches	O
the	O
stack	O
to	O
a	O
special	O
system	O
stack	O
,	O
so	O
that	O
none	O
of	O
the	O
thread	O
stacks	O
need	O
account	O
for	O
worst-case	O
nested	O
interrupt	B-Application
usage	O
.	O
</s>
<s>
Tiny	O
CPUs	O
as	O
far	O
back	O
as	O
the	O
8-bit	O
Motorola	B-Device
6809	I-Device
from	O
1978	O
have	O
provided	O
separate	O
system	O
and	O
user	O
stack	O
pointers	O
.	O
</s>
<s>
For	O
many	O
reasons	O
,	O
it	O
is	O
highly	O
desired	O
that	O
the	O
interrupt	B-General_Concept
handler	I-General_Concept
execute	O
as	O
briefly	O
as	O
possible	O
,	O
and	O
it	O
is	O
highly	O
discouraged	O
(	O
or	O
forbidden	O
)	O
for	O
a	O
hardware	O
interrupt	B-Application
to	O
invoke	O
potentially	O
blocking	O
system	B-Operating_System
calls	I-Operating_System
.	O
</s>
<s>
In	O
a	O
system	O
with	O
multiple	O
execution	O
cores	O
,	O
considerations	O
of	O
reentrancy	B-Operating_System
are	O
also	O
paramount	O
.	O
</s>
<s>
If	O
the	O
system	O
provides	O
for	O
hardware	O
DMA	B-General_Concept
,	O
concurrency	B-Operating_System
issues	O
can	O
arise	O
even	O
with	O
only	O
a	O
single	O
CPU	O
core	O
.	O
</s>
<s>
(	O
It	O
is	O
not	O
uncommon	O
for	O
a	O
mid-tier	O
microcontroller	O
to	O
lack	O
protection	O
levels	O
and	O
an	O
MMU	O
,	O
but	O
still	O
provide	O
a	O
DMA	B-General_Concept
engine	I-General_Concept
with	O
many	O
channels	O
;	O
in	O
this	O
scenario	O
,	O
many	O
interrupts	B-Application
are	O
typically	O
triggered	O
by	O
the	O
DMA	B-General_Concept
engine	I-General_Concept
itself	O
,	O
and	O
the	O
associated	O
interrupt	B-General_Concept
handler	I-General_Concept
is	O
expected	O
to	O
tread	O
carefully	O
.	O
)	O
</s>
<s>
A	O
modern	O
practice	O
has	O
evolved	O
to	O
divide	O
hardware	O
interrupt	B-General_Concept
handlers	I-General_Concept
into	O
front-half	O
and	O
back-half	O
elements	O
.	O
</s>
<s>
The	O
front-half	O
(	O
or	O
first	O
level	O
)	O
receives	O
the	O
initial	O
interrupt	B-Application
in	O
the	O
context	O
of	O
the	O
running	O
process	O
,	O
does	O
the	O
minimal	O
work	O
to	O
restore	O
the	O
hardware	O
to	O
a	O
less	O
urgent	O
condition	O
(	O
such	O
as	O
emptying	O
a	O
full	O
receive	O
buffer	O
)	O
and	O
then	O
marks	O
the	O
back-half	O
(	O
or	O
second	O
level	O
)	O
for	O
execution	O
in	O
the	O
near	O
future	O
at	O
the	O
appropriate	O
scheduling	O
priority	O
;	O
once	O
invoked	O
,	O
the	O
back-half	O
operates	O
in	O
its	O
own	O
process	O
context	O
with	O
fewer	O
restrictions	O
and	O
completes	O
the	O
handler	O
's	O
logical	O
operation	O
(	O
such	O
as	O
conveying	O
the	O
newly	O
received	O
data	O
to	O
an	O
operating	B-General_Concept
system	I-General_Concept
data	O
queue	O
)	O
.	O
</s>
<s>
In	O
several	O
operating	O
systemsLinux	O
,	O
Unix	B-Application
,	O
macOS	B-Application
,	O
Microsoft	B-Application
Windows	I-Application
,	O
z/OS	B-Application
,	O
DESQview	B-Operating_System
and	O
some	O
other	O
operating	B-General_Concept
systems	I-General_Concept
used	O
in	O
the	O
pastinterrupt	O
handlers	O
are	O
divided	O
into	O
two	O
parts	O
:	O
the	O
First-Level	O
Interrupt	B-General_Concept
Handler	I-General_Concept
(	O
FLIH	B-General_Concept
)	O
and	O
the	O
Second-Level	O
Interrupt	B-General_Concept
Handlers	I-General_Concept
(	O
SLIH	B-General_Concept
)	O
.	O
</s>
<s>
FLIHs	B-General_Concept
are	O
also	O
known	O
as	O
hard	O
interrupt	B-General_Concept
handlers	I-General_Concept
or	O
fast	O
interrupt	B-General_Concept
handlers	I-General_Concept
,	O
and	O
SLIHs	B-General_Concept
are	O
also	O
known	O
as	O
slow/soft	O
interrupt	B-General_Concept
handlers	I-General_Concept
,	O
or	O
Deferred	B-Device
Procedure	I-Device
Calls	I-Device
in	O
Windows	O
.	O
</s>
<s>
A	O
FLIH	B-General_Concept
implements	O
at	O
minimum	O
platform-specific	O
interrupt	B-Application
handling	I-Application
similar	O
to	O
interrupt	B-General_Concept
routines	I-General_Concept
.	O
</s>
<s>
In	O
response	O
to	O
an	O
interrupt	B-Application
,	O
there	O
is	O
a	O
context	B-Operating_System
switch	I-Operating_System
,	O
and	O
the	O
code	O
for	O
the	O
interrupt	B-Application
is	O
loaded	O
and	O
executed	O
.	O
</s>
<s>
The	O
job	O
of	O
a	O
FLIH	B-General_Concept
is	O
to	O
quickly	O
service	O
the	O
interrupt	B-Application
,	O
or	O
to	O
record	O
platform-specific	O
critical	O
information	O
which	O
is	O
only	O
available	O
at	O
the	O
time	O
of	O
the	O
interrupt	B-Application
,	O
and	O
schedule	B-General_Concept
the	O
execution	O
of	O
a	O
SLIH	B-General_Concept
for	O
further	O
long-lived	O
interrupt	B-Application
handling	I-Application
.	O
</s>
<s>
FLIHs	B-General_Concept
cause	O
jitter	O
in	O
process	O
execution	O
.	O
</s>
<s>
FLIHs	B-General_Concept
also	O
mask	O
interrupts	B-Application
.	O
</s>
<s>
Reducing	O
the	O
jitter	O
is	O
most	O
important	O
for	O
real-time	B-Operating_System
operating	I-Operating_System
systems	I-Operating_System
,	O
since	O
they	O
must	O
maintain	O
a	O
guarantee	O
that	O
execution	O
of	O
specific	O
code	O
will	O
complete	O
within	O
an	O
agreed	O
amount	O
of	O
time	O
.	O
</s>
<s>
To	O
reduce	O
jitter	O
and	O
to	O
reduce	O
the	O
potential	O
for	O
losing	O
data	O
from	O
masked	B-Application
interrupts	I-Application
,	O
programmers	O
attempt	O
to	O
minimize	O
the	O
execution	O
time	O
of	O
a	O
FLIH	B-General_Concept
,	O
moving	O
as	O
much	O
as	O
possible	O
to	O
the	O
SLIH	B-General_Concept
.	O
</s>
<s>
With	O
the	O
speed	O
of	O
modern	O
computers	O
,	O
FLIHs	B-General_Concept
may	O
implement	O
all	O
device	O
and	O
platform-dependent	O
handling	O
,	O
and	O
use	O
a	O
SLIH	B-General_Concept
for	O
further	O
platform-independent	O
long-lived	O
handling	O
.	O
</s>
<s>
FLIHs	B-General_Concept
which	O
service	O
hardware	O
typically	O
mask	O
their	O
associated	O
interrupt	B-Application
(	O
or	O
keep	O
it	O
masked	O
as	O
the	O
case	O
may	O
be	O
)	O
until	O
they	O
complete	O
their	O
execution	O
.	O
</s>
<s>
An	O
(	O
unusual	O
)	O
FLIH	B-General_Concept
which	O
unmasks	O
its	O
associated	O
interrupt	B-Application
before	O
it	O
completes	O
is	O
called	O
a	O
reentrant	B-Operating_System
interrupt	B-General_Concept
handler	I-General_Concept
.	O
</s>
<s>
Reentrant	B-Operating_System
interrupt	B-General_Concept
handlers	I-General_Concept
might	O
cause	O
a	O
stack	B-Error_Name
overflow	I-Error_Name
from	O
multiple	O
preemptions	B-Operating_System
by	O
the	O
same	O
interrupt	B-General_Concept
vector	I-General_Concept
,	O
and	O
so	O
they	O
are	O
usually	O
avoided	O
.	O
</s>
<s>
In	O
a	O
priority	B-Operating_System
interrupt	I-Operating_System
system	O
,	O
the	O
FLIH	B-General_Concept
also	O
(	O
briefly	O
)	O
masks	O
other	O
interrupts	B-Application
of	O
equal	O
or	O
lesser	O
priority	O
.	O
</s>
<s>
A	O
SLIH	B-General_Concept
completes	O
long	O
interrupt	B-Application
processing	O
tasks	O
similarly	O
to	O
a	O
process	O
.	O
</s>
<s>
SLIHs	B-General_Concept
either	O
have	O
a	O
dedicated	O
kernel	B-Operating_System
thread	O
for	O
each	O
handler	O
,	O
or	O
are	O
executed	O
by	O
a	O
pool	O
of	O
kernel	B-Operating_System
worker	O
threads	O
.	O
</s>
<s>
These	O
threads	O
sit	O
on	O
a	O
run	B-Application
queue	I-Application
in	O
the	O
operating	B-General_Concept
system	I-General_Concept
until	O
processor	B-General_Concept
time	O
is	O
available	O
for	O
them	O
to	O
perform	O
processing	O
for	O
the	O
interrupt	B-Application
.	O
</s>
<s>
SLIHs	B-General_Concept
may	O
have	O
a	O
long-lived	O
execution	O
time	O
,	O
and	O
thus	O
are	O
typically	O
scheduled	O
similarly	O
to	O
threads	O
and	O
processes	O
.	O
</s>
<s>
In	O
Linux	B-Application
,	O
FLIHs	B-General_Concept
are	O
called	O
upper	O
half	O
,	O
and	O
SLIHs	B-General_Concept
are	O
called	O
lower	O
half	O
or	O
bottom	O
half	O
.	O
</s>
<s>
This	O
is	O
different	O
from	O
naming	O
used	O
in	O
other	O
Unix-like	O
systems	O
,	O
where	O
both	O
are	O
a	O
part	O
of	O
bottom	O
half	O
.	O
</s>
