Last Time. Think carefully about whether you use a heap Look carefully for stack overflow Especially when you have multiple threads

Similar documents
KERNEL THREAD IMPLEMENTATION DETAILS. CS124 Operating Systems Winter , Lecture 9

18-349: Introduction to Embedded Real-Time Systems

DSP/BIOS Kernel Scalable, Real-Time Kernel TM. for TMS320 DSPs. Product Bulletin

Input / Output. School of Computer Science G51CSA

Linux - Not real-time!

INTERRUPTS in microprocessor systems

Review: Hardware user/kernel boundary

Introduction to Embedded Systems. Lab Logistics

Grundlagen Microcontroller Interrupts. Günther Gridling Bettina Weiss

Static Analysis of Embedded C

Chapters 2, 3: bits and pieces. Chapters 2 & 3. Chapters 2, 3: bits and pieces. Chapters 2, 3: bits and pieces. Using C. A last word about hardware

EECS 373 Design of Microprocessor-Based Systems

Microprocessors & Interfacing

Interrupts (I) Lecturer: Sri Notes by Annie Guo. Week8 1

EECS 373 Design of Microprocessor-Based Systems

! Why is synchronization needed? ! Synchronization Language/Definitions: ! How are locks implemented? Maria Hybinette, UGA

Static Analysis of Embedded C Code


Interrupts and Low Power Features

Anne Bracy CS 3410 Computer Science Cornell University

Yielding, General Switching. November Winter Term 2008/2009 Gerd Liefländer Universität Karlsruhe (TH), System Architecture Group

Designing Responsive and Real-Time Systems

COMP I/O, interrupts, exceptions April 3, 2016

Java Concurrency in practice Chapter 9 GUI Applications

Running Applications

Lecture #7: Implementing Mutual Exclusion

Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

18-642: Race Conditions

Interrupts (Exceptions) Gary J. Minden September 11, 2014

The control of I/O devices is a major concern for OS designers

In examining performance Interested in several things Exact times if computable Bounded times if exact not computable Can be measured

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization?

Chapter 09. Programming in Assembly

Interrupts (Exceptions) (From LM3S1968) Gary J. Minden August 29, 2016

Process Coordination and Shared Data

Chapter 7 Central Processor Unit (S08CPUV2)

Computer Architecture 5.1. Computer Architecture. 5.2 Vector Address: Interrupt sources (IS) such as I/O, Timer 5.3. Computer Architecture

Last class: Today: Course administration OS definition, some history. Background on Computer Architecture

An Operating System in Action

Programming Embedded Systems

COEN-4720 Embedded Systems Design Lecture 4 Interrupts (Part 1) Cristinel Ababei Dept. of Electrical and Computer Engineering Marquette University

Please do not handin a.doc file, a.zip file, a.tar file, or anything else

Lecture 4: Memory Management & The Programming Interface

Introduction to Operating. Chapter Chapter

Computer System Overview

CSE 451: Operating Systems Winter Module 2 Architectural Support for Operating Systems

CS 160: Interactive Programming

Concurrency. Glossary

EEL 4744C: Microprocessor Applications. Lecture 7. Part 1. Interrupt. Dr. Tao Li 1

Reading Assignment. Interrupt. Interrupt. Interrupt. EEL 4744C: Microprocessor Applications. Lecture 7. Part 1

Even coarse architectural trends impact tremendously the design of systems

ARM Cortex core microcontrollers

Even coarse architectural trends impact tremendously the design of systems. Even coarse architectural trends impact tremendously the design of systems

Introduction to Operating Systems. Chapter Chapter

Review: Easy Piece 1

Architectural Support for Operating Systems

Deferred Interrupt Processing

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Anne Bracy CS 3410 Computer Science Cornell University

A Predictable RTOS. Mantis Cheng Department of Computer Science University of Victoria

Introduction to Operating Systems. Chapter Chapter

PC Interrupt Structure and 8259 DMA Controllers

Parallel Programming: Background Information

Parallel Programming: Background Information

Kernel Synchronization I. Changwoo Min

Operating Systems Design Fall 2010 Exam 1 Review. Paul Krzyzanowski

CS 537 Lecture 2 Computer Architecture and Operating Systems. OS Tasks

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Last Time. Low-level parts of the toolchain for embedded systems. Any weak link in the toolchain will hinder development

PROCESS STATES AND TRANSITIONS:

Overview. This Lecture. Interrupts and exceptions Source: ULK ch 4, ELDD ch1, ch2 & ch4. COSC440 Lecture 3: Interrupts 1

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Real Time Linux patches: history and usage

I/O Handling. ECE 650 Systems Programming & Engineering Duke University, Spring Based on Operating Systems Concepts, Silberschatz Chapter 13

Timers 1 / 46. Jiffies. Potent and Evil Magic

Virtual Machine Design

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

The Kernel Abstraction

CS510 Advanced Topics in Concurrency. Jonathan Walpole

Unit 2 : Computer and Operating System Structure

Interrupt/Timer/DMA 1

CS 326: Operating Systems. CPU Scheduling. Lecture 6

Concurrency Race Conditions and Deadlocks

Wireless Sensor Networks (WSN)

Lecture 2: September 9

Recap from last class

Concurrent programming: Introduction I

Our topic. Concurrency Support. Why is this a problem? Why threads? Classic solution? Resulting architecture. Ken Birman

Design and Implementation Interrupt Mechanism

ME 4447/6405. Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics. Instructor: Professor Charles Ume LECTURE 6

Newbie s Guide to AVR Interrupts

CS 326: Operating Systems. Process Execution. Lecture 5

Operating Systems 2010/2011

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Design document: Handling exceptions and interrupts

Introducing Shared-Memory Concurrency

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Computer System Overview. Chapter 1

CS5412: TRANSACTIONS (I)

THREADS & CONCURRENCY

Transcription:

Last Time Cost of nearly full resources RAM is limited Think carefully about whether you use a heap Look carefully for stack overflow Especially when you have multiple threads Embedded C Extensions for device access, address spaces, saturating operations, fixed point arithmetic

Today Advanced interrupts Race conditions System design Prioritized interrupts Interrupt latency Interrupt problems: Stack overflow Overload Missed interrupts Spurious interrupts

Typical Interrupt Subsystem Each interrupt has a pending bit Logic independent of the processor core sets these bits E.g. ADC ready, timer expires, edge detected, etc. A pending bit can become set at any time This logic does not need to be synchronized with the MCU Each interrupt has a disable bit Processor has a global disable bit

More Interrupt Basics Interrupt algorithm If global interrupt enable bit is set, processor checks for pending interrupts prior to fetching a new instruction If any interrupts are pending, highest priority interrupt that is pending and enabled is selected for execution If an interrupt can be fired, flush the pipeline and jump to the interrupt s handler Some interrupts must be acknowledged This clears the pending flag Failure to do this results in infinite interrupt loop Symptom: System hangs

Interrupts on ColdFire Interrupt controller on MCF52233 is fairly sophisticated Many MCUs have much simpler controllers Processor has a 3-bit interrupt mask in SR Once per instruction, processor looks for pending interrupts with priority greater than the mask value However, level 7 interrupts are non-maskable

ColdFire Interrupt Sequence 1. CPU enters supervisor mode 2. 8-bit vector fetched from interrupt controller 3. Vector is an index into the 256-entry exception vector table Vector entries are 32-bit addresses Vectors 0-63 are reserved, you can use 64-255 4. Push SR and PC 5. Load vector address into PC 6. Set interrupt mask to level of current interrupt 7. First instruction of interrupt handler is guaranteed to be executed So this would be a good place to disable interrupts, if you don t want nested interrupts

More ColdFire Within an interrupt level, there are 9 priorities Interrupt controller has registers that permit you to assign level and priority to interrupt sources In contrast, many embedded processors fix priorities at design time Many ColdFire processors (including ours) support two stack pointers User mode and supervisor mode We re only using the supervisor stack pointer

Interrupts and Race Conditions Major problem with interrupts: They cause interleaving (threads do too) Interleaving is hard to think about First rule of writing correct interrupt-driven code Disable interrupts at all times when interrupt cannot be handled properly Easier said than done interrupt-driven code is notoriously hard to get right When can an interrupt not be handled properly? When manipulating data that the interrupt handler touches When not expecting the interrupt to fire Etc.

Interleaving is Tricky interrupt_3 { does something with x } main () { } x += 1; Do you want to disable interrupts while incrementing x in main()? How to go about deciding this in general?

Using our compiler x += 1; Translates to: addq.l #1,_x Do we need to disable interrupts to execute this code?

However: x += 500; Translates to: movea.l _x,a0 lea 500(a0),a0 move.l a0,_x

The property that matters here is atomicity An atomic action is one that cannot be interrupted Individual instructions are usually atomic Disabling interrupts is a common way to execute a block of instructions atomically Question: Do we really need atomicity?

Answer: No we need code to execute as if it executed atomically In practice, this means: Only exclude computations that matter Example 1: Only raise the interrupt level high enough that all interrupts that can actually interfere are disabled Example 2: Thread locks only prevent other threads from acquiring the same lock Example 3: Non-maskable interrupts cannot be masked

Summary: Each piece of code in a system must include protection against Threads Interrupts Activities on other processors DMA transfers Etc. that might cause incorrect execution by preempting the code you are writing

Reentrant Code A function is reentrant if it works when called by multiple interrupt handlers (or by main + one interrupt handler) at the same time What if non-reentrant code is reentered? Strategies for reentrancy: Put all data into stack variables Why does this work? Disable interrupts when touching global variables In practice writing reentrant code is easy The real problem is not realizing that a transitive call-chain reaches some non-reentrant call A function is non-reentrant if it can possibly call any nonreentrant function

System-Level Interrupt Design Easy way: Interrupts never permitted to preempt each other Interrupts permitted to run for a long time Main loop disables interrupts liberally Hard way: Interrupts prioritized high priority can always preempt lower priority Interrupts not permitted to run for long Main loop disables interrupts with fine granularity Pros and cons? Stupid way: Any interrupt can preempt any other interrupt ColdFire doesn t let you do this! But other processors do

Interrupt Latency Interrupt latency is time between interrupt line being asserted and time at which first instruction of handler runs Two latencies of interest: Expected latency Worst-case latency How to compute these? Sources of latency: Slow instructions Code running with interrupts disabled Other interrupt handlers

Managing Interrupt Latency This is hard! Some strategies Nested interrupt handlers Prioritized interrupts Short critical sections Split interrupts Basic idea: Low-priority code must not block timecritical interrupts for long

Nested Interrupts Interrupts are nested if multiple interrupts may be handled concurrently Makes system more responsive but harder to develop and validate Often much harder! Only makes sense in combination with prioritized interrupt scheduling Nesting w/o prioritization increases latency without increasing responsiveness! Nested interrupts on ColdFire are easy Just don t disable interrupts in your interrupt handler Some ARM processors make this really difficult

Prioritizing Interrupts Really easy on some hardware E.g. x86 and ColdFire automatically mask all interrupts of same or lower priority On other hardware not so easy E.g. on ARM and AVR need to manually mask out lower priority interrupts before reenabling interrupts Argh.

Reentrant Interrupts A reentrant interrupt may have multiple invocations on the stack at once 99.9% of the time this is a bug Programmer didn t realize consequences of reenabling interrupts Programmer recognized possibility and either ignored it or thought it was a good idea 0.1% of the time reentrant interrupts make sense E.g. AvrX timer interrupt Does ColdFire support reentrant interrupts?

Missed Interrupts Interrupts are not queued Pending flag is a single bit If interrupt is signaled when already pending, the new interrupt request is dropped Consequences for developers Keep interrupts short Minimizes probability of missed interrupts Interrupt handlers should perform all work pending at the device Compensates for missed interrupts

Splitting Interrupt Handlers Two options when handling an interrupt requires a lot of work: 1. Run all work in the handler 2. Make the handler fast and run the rest in a deferred context Splitting interrupts is tricky State must be passed by hand The two parts become concurrent There are many ways to run the deferred work Background loop polls for work Wake a thread to do the work Windows has deferred procedure calls, Linux has tasklets and bottom-half handlers

Spurious Interrupts Glitches can cause interrupts for nonexistent devices to fire Processor manual talks about these Solutions: Have a default interrupt handler that either ignores the spurious interrupt or resets the system Ensure that all nonexistent interrupts are permanently disabled

Interrupt Overload If an external interrupt fires too frequently Lower-priority interrupts starved Background loop starved Why would this happen? Loose or damaged connection Electrical noise Malicious or buggy node on network Apollo 11 Computer reset multiple times while attempting to land on moon LLM guidance computer overwhelmed by phantom radar data Ground control almost aborted the landing

Potential Overload Sources

Preventing Interrupt Overload Strategies: Trust the hardware not to overload Don t use interrupts poll Design the software to prevent interrupt overload Design the hardware to prevent interrupt overload

Hardware Interrupt Scheduler

Interrupt Pros Support very efficient systems No polling CPU only spends cycles processing work when there is work to do Interrupts rapidly wake up a sleeping processor Support very responsive systems Well-designed and well-implemented software can respond Well-designed and well-implemented software can respond to interrupts within microseconds

Interrupt Cons Introduce hard problems: Concurrency and reentrance Missed interrupts Spurious interrupts Interrupt overload Make stack overflow harder to deal with Interrupt-driven codes hard to test adequately Achieving fast worst-case response times is difficult Overall: Few safety-critical systems are interrupt-driven!

Summary Interrupts are very convenient But a huge can of worms Interrupt handling is a whole-system design issue Can t just handle each interrupt individually Never write code that allows reentrant interrupts