INTERRUPT ROUTINES IN AN RTOS ENVIRONMENT
INTERRUPT ROUTINES IN AN RTOS ENVIRONMENT
INTRODUCTION INTRODUCTION
Lik
Like e stanstand-ad-alonlone e syssystemtems, s, embembeddedded ed appapplicalicatiotions ns runrunninning g on on top top of of realreal-tim-timee operating systems (RTOSes) require Interrupt Serice Routines (ISRs) to !andle interrupts operating systems (RTOSes) require Interrupt Serice Routines (ISRs) to !andle interrupts generated by e"ternal eents# $"ternal eents can be caused by %ust about anyt!ing, from an generated by e"ternal eents# $"ternal eents can be caused by %ust about anyt!ing, from an async!ronous c!aracter arrial on a &'RT to a periodic timer interrupt# ISRs !ae t!e async!ronous c!aracter arrial on a &'RT to a periodic timer interrupt# ISRs !ae t!e responsibility of acknoledging t!e !ardare condition and proide t!e initial !andling of responsibility of acknoledging t!e !ardare condition and proide t!e initial !andling of data sent or receied as required by t!e interrupt# 'n ISR often is responsible for proiding data sent or receied as required by t!e interrupt# 'n ISR often is responsible for proiding t!e RT
t!e RTOS it! information necessaOS it! information necessary to proide serices ry to proide serices to application t!reads#to application t!reads# $"amples include moing data into a buffer for processing, adding an
$"amples include moing data into a buffer for processing, adding an entry to a queueentry to a queue for processing, setting a alue to indicate t!at an eent !as occurred, and so on# Since for processing, setting a alue to indicate t!at an eent !as occurred, and so on# Since application code e"ecution is interrupted (delayed) during t!e e"ecution of an ISR, most application code e"ecution is interrupted (delayed) during t!e e"ecution of an ISR, most applications minimie t!e amount of code in t!e ISR and rely instead on non-ISR code (an applications minimie t!e amount of code in t!e ISR and rely instead on non-ISR code (an application *T!read+ or
application *T!read+ or *T*Task+) to complete ask+) to complete t!e processing#t!e processing# SYNOPSIS
SYNOPSIS
RTOS IT$RR&T 'R./IT$.T&R$SRTOS IT$RR&T 'R./IT$.T&R$S
T$R0IOLO12T$R0IOLO12
'R./IT$.T&R$'R./IT$.T&R$
TOT'L S2ST$0 O3$R/$'4 .O0'RISOTOT'L S2ST$0 O3$R/$'4 .O0'RISO
.O.L&SIO.O.L&SIO
R$5$R$.$S R$5$R$.$S
RTOS INTERRUPT ARCHITECTURES
RTOS INTERRUPT ARCHITECTURES
'
' funfundamdamentental al c!ac!allenllenge ge in in RRTOTOS S desdesign ign is is supsupporportinting g asynasync!rc!ronoonous us acceaccess ss toto internal RTOS data structures by interrupt routines and RTOS serices# It cannot be alloed internal RTOS data structures by interrupt routines and RTOS serices# It cannot be alloed
6 6
t!at, !ile modifying a data structure, a serice or ISR gets interrupted and a different serice or ISR makes unrelated modifications to t!e same structure, leaing it in a c!anged state for t!e original code to (unknoingly) continue modifying# T!e results can be catastrop!ic#
'll RTOSes must address t!is c!allenge and preent multiple ISRs (or system calls) from modifying t!e same structure at t!e same time# T!is allos t!e !ig!est priority application code to be e"ecuted as quickly as possible, and delayed as little as possible, een in situations it! intense interrupt actiity# T!ere are at least to approac!es, one used by t!e ma%ority of RTOSes, and anot!er used by a fe# T!e more popular approac! is to briefly lockout interrupts !ile an ISR or system serice is modifying critical data structures inside t!e RTOS# T!is reliably preents any ot!er program from %umping in and making uncoordinated c!anges to t!e critical area being used by t!e e"ecuting code#
'not!er, less popular approac! is not to disable interrupts in system serice routines, but instead (by rule or conention) not allo any async!ronous access to critical data structures by ISRs or ot!er serice calls# Serice call access to critical data structures from an ISR is *deferred+ to a secondary routine e denote *ISR7,+ !ic! gets e"ecuted along it! application t!reads under sc!eduler control# T!is approac! also reliably preents interference it! t!e actions of an e"ecuting system serice call, by not alloing any t!reads or ISR7 routines, !ic! mig!t make system serice calls, to e"ecute until processing of critical data structures is completed# T!is approac! is called a *Segmented Interrupt 'rc!itecture,+ because it breaks up t!e processing required in response to an interrupt into multiple (usually 7) *segments+ e"ecuted at different priorities# In t!is paper, e e"amine t!e performance implications of eac! approac! on real-time system responsieness#
TERMINOLOGY
T!e folloing is a list of symbols used to represent t!e processing performed in eac! type of RTOS interrupt arc!itecture8
Symbo l
Meaning
..8 .onte"t .reate# .reate conte"t for ISR7# T!is typically inoles creating a stack frame and alerting t!e sc!eduler to sc!edule it ne"t#
.R8 .onte"t Restore# Restore conte"t of sc!eduled entity .S8 .onte"t Sae# Saes t!e conte"t of t!e running t!read#
I48 Interrupt 4ispatc!er# 'pplicable in single interrupt ector arc!itectures like oer., 0IS, and 'R0# On arc!itectures like .old5ire, t!is item is done in !ardare#
ISR8 Interrupt Serice Routine# Traditional ISR, alloed to interact it! application t!reads t!roug! RTOS serices#
ISR68 Interrupt Serice Routine, art-6# Similar to traditional ISR, but not alloed to interact it! RTOS#
ISR78 Interrupt Serice Routine, art-7# Sc!eduled entity run in a t!read or super-t!read conte"t# 'lloed to interact it! RTOS serices and t!reads#
ITR'8 Interrupted T!read Running 'gain# L8 Lockout# Interrupt lockout time#
S8 Sc!eduler# Sc!edule t!e most important t!read or ISR7# T8 e T!read running#
68 Start of ISR processing# 78 $nd of ISR processing#
98 e t!read running : preemption case# ;8 Return to interrupted t!read#
<OL48 Items t!at are purely application code#
The Unifie In!e""#$! A"%hi!e%!#"e
RTOSes t!at employ a unified arc!itecture treat t!e ISR no differently t!an an ISR in a stand-alone application# In t!is arc!itecture, t!e ISR is alloed to make RTOS calls to modify kernel data structures and to interact it! application t!reads and ot!er resources# To do t!is, t!e RTOS disables interrupts oer certain (typically ery s!ort) code segments, t!ereby protecting common resources against async!ronous access from multiple ISRs or system serice calls#
ARCHITECTURE
RTOSes t!at do not disable interrupts during system serices cannot make modifications to RTOS data structures from it!in an ISR, because t!e ISR mig!t be interrupted by anot!er interrupt, !ic! t!en mig!t attempt to use t!e same data structure# To preent t!is, segmented arc!itectures basically disable t!e application sc!eduler, and diide
t!e interrupt processing into to (or more) pieces# T!e first piece (ISR6) be!aes like a traditional ISR but performs no interaction it! RTOS data structures# T!is enables it to run it! interrupts enabled#
T!e second piece (ISR7) is a *sc!eduled entity+ t!at makes all necessary RTOS data structure updates at t!e application leel, and is inoked t!roug! t!e processing in ISR6# 5inally, application t!read processing can be performed once bot! ISR6 and ISR7 !ae been completed# T!is effectiely disables applications !ile ISR6 and ISR7 are operating, and defers conte"t sitc!ing !ile RTOS data structures are being modified# T!e performance obserations in t!is section are some!at abstract#
T!e amount of time for RTOS performance ob%ects (e#g# .S, .., .R, etc#) on most RTOS products ill be some!at similar# /oeer, t!e segmented arc!itecture products are typically larger operating systems so t!eir actual processing times for eac! RTOS performance ob%ect ould most likely be greater t!an a simpler unified interrupt arc!itecture RTOS# In addition, if memory protection or irtual addresses are supported, t!e time required to process an RTOS block ill most definitely be greater# T!e succeeding comparisons assume t!at t!e arious RTOSes take t!e same amount of time to perform t!e same RTOS operation# It is furt!er assumed t!at t!e time to perform ISR processing in t!e unified approac! is equal to t!e sum of t!e times to perform ISR6 and ISR7 in t!e segmented approac!#
In bot! preemption and non-preemption cases, t!e segmented approac! takes longer# TOTAL SYSTEM OVERHEAD COMPARISON
Ironically, as can be seen in 5igure-9, t!e total system oer!ead is greater in t!e RTOS it! a segmented interrupt arc!itecture# In bot! t!e non-preemption and preemption cases t!e segmented interrupt arc!itecture RTOS introduces an additional (6=.S, 6=.R, 7=S, and 6=..) of oer!ead#
CONCLUSION
T!e segmented RTOS arc!itecture is claimed to !ae an adantage in its ability to respond to interrupts# 'fter all, if interrupts are disabled, response s!ould be negatiely impacted, rig!t> T!e !ole idea be!ind not disabling interrupts is to make interrupt response faster# /oeer, !ile it sounds good, t!ere are seeral practical problems introduced by t!is approac!# 'lt!oug! t!e segmented interrupt RTOS doesn?t disable interrupts !en manipulating RTOS data structures inside serice routines, t!e !ardare itself does !en processing ot!er interrupts# So t!e orst case L in t!e segmented interrupt RTOS is actually
t!e time interrupts are locked out during t!e processing of anot!er interrupt# RE&ERENCES !ttp8@@rtos#com@articles@6AA9B !ttps8@@#csun#edu@C%eff@[email protected]BDA$'@Lectures@OSSerices@OSSer icesE!tml@te"t7F#!tml !ttp8@@stackoerflo#com@questions@B9GBFG@!o-do-real-time-operating-systems-ork !ttp8@@#slides!are#net@HOLL&S&4'RS/'R$442@munni-;9A6BF !ttps8@@sites#google#com@site@rtosmifmim@!ome@interrupt-routines-in-an-rtos-enironment---part-6 B