Safe and Reliable Embedded Linux Programming: How to Get There

AdaCore
AdaCoreAdaCore
7 June 2012




             Safe and Reliable Embedded Linux Programming
             How to Get There
            José F. Ruiz
            Senior Software Engineer
            AdaCore




            EWiLi 2012, Lorient



Copyright © 2012 AdaCore                                        Slide: 1
Outline



             •      Safety issues
                      –    Language matters


             •      Concurrent execution on monoprocessors
                      –    Ada Ravenscar Profile


             •      Tasking on linux


             •      Parallel execution on multiprocessors
                      –    Processor affinities




Copyright © 2012 AdaCore                                     Slide: 2
Safety




Copyright © 2012 AdaCore            Slide: 3
Safety-Critical Software: Background



             •      What is “safety critical” software?
                      –    Failure can cause loss of human life or have other catastrophic consequences


             •      How does safety criticality affect software development?
                      –    Regulatory agencies require compliance with certification requirements
                      –    Safety-related standards may apply to the finished product, to the development process,
                           or both


             •      Choice of programming language has large impact on the cost of
                    developing / certifying safety-critical systems
                      –    Dilemma: modern features that help in developing maintainable systems interfere with
                           safety certification
                             – Examples: Object-Oriented Programming, generics, exceptions, concurrency
                      –    Traditional approach: select subset (profile) amenable to safety certification
                             – Chosen features depend on the analysis methods to be used (or dictate which
                               methods are feasible)




Copyright © 2012 AdaCore                                                                                          Slide: 4
Some language requirements (I)



             •      Reliability
                      –    Support for development of readable, correct code
                             – No “traps and pitfalls”
                             – Intuitive lexical and syntactic features
                      –    Support for good software engineering practice
                             – Encapsulation
                      –    Avoidance of concurrency bugs
                             – Race conditions
                             – Deadlock / livelock
                      –    Early error detection
                             – Compile-time checking whenever possible
                             – Run-time checking, at least


             •      Predictability
                      –    Unambiguous language semantics
                             – No undefined / implementation-dependent behavior
                      –    Ability to demonstrate statically that time and space constraints will be satisfied
                             – Deadlines will be met
                             – Stack and heap space will not be exhausted



Copyright © 2012 AdaCore                                                                                         Slide: 5
Some language requirements (II)



             •      Analyzability
                      –    Control and data flow analysis
                             – Detects / prevents references to uninitialized data
                      –    Information flow analysis
                             – Identify dependencies between a routine’s inputs and outputs
                      –    Formal code verification
                      –    Range checking
                      –    Traceability between program representations and different levels
                      –    Dynamic analysis (testing)


             •      Expressiveness
                      –    General-purpose features consistent with the above requirements
                      –    Support for specialized processing typical in embedded systems (safety)
                             – Interrupt handling
                             – Low-level programming
                             – Fixed-point arithmetic



Copyright © 2012 AdaCore                                                                             Slide: 6
Modern programming languages



             •      Many features help
                      –    Encapsulation / namespace control
                             – Limit data coupling
                      –    Strong typing, compile-time checking
                             – Early error detection


             •      Some features present difficulties
                      –    High expressive power vs. need to constrain feature
                      –    Dynamic flexibility vs. need for static analysis
                      –    High-level features vs. need to certify the code that actually runs
                             – Generated code may contain implicit loops, conditionals
                             – Run-time library, API need to be certifiable
                             – How, if at all, use features such as exceptions, concurrency, dynamic allocation




Copyright © 2012 AdaCore                                                                                          Slide: 7
Modern programming languages – basic issues



             •      No general-purpose language is appropriate for highest safety/security levels
                      –    Conflicts with reliability, predictability, analyzability


             •      Key issue: can the language be subsetted so that applications restricted to the
                    subset can be certified
                      –    Is/are the subset(s) precisely defined?
                      –    Can compliance with the subset(s) be enforced by a tool?
                      –    Who defines the subset(s)?
                             – Standards agency? Tool/compiler vendor? Application developer?
                      –    Are there intrinsic problems with the language that cannot be solved by subsetting?


             •      Current approaches
                      –    C-based
                             – MISRA C / C++
                      –    Ada-based
                             – Ada profiles
                             – SPARK Ada
                      –    Java-based
                             – Real-Time / Safety-Critical Java Technology



Copyright © 2012 AdaCore                                                                                         Slide: 8
MISRA C



             •      Intended for embedded automotive systems
                      –    Around 170 rules
             •      Prevent or restrict usage of C constructs with unpredictable behavior
             •      Encourage static checking tools that enforce compliance with the subset



             Strengths                                                   Weaknesses
                      –    “Best practices” for C programming               –   C was not designed to meet the
                             –   General good style                             needs of high-integrity systems
                             –   Avoids C features intrinsically non-             – Error-prone
                                 portable / ill-defined
                                                                                  – Lacks features such as
                      –    Simplicity of C compared with other                      encapsulation, namespaces
                           languages
                                                                            –   C (and thus MISRA C) does not
                             –   Smaller run-time library
                             –   Easier certification
                                                                                “scale up” to large systems
                      – Large population of candidate users                 –   C standard has ambiguities and thus
                             –    And tools, service providers, etc.            so does MISRA C
                      –    May be appropriate for small systems             –   Some rules are not enforceable by
                           on specialized hardware lacking                      static tools
                           compilers for alternative languages              –   Usage of MISRA C requires license
                             –   MISRA-C code may be produced as                from MISRA
                                 output of source-to-source translator
Copyright © 2012 AdaCore                                                                                              Slide: 9
Ada – introduction



             •      What is Ada?
                      –    General-purpose methodology-neutral strongly-typed ISO standard language
                      –    Intended for large, long-lived systems
                      –    Fits reliability-critical embedded real-time applications


             •      Ada in a nutshell
                      –    Pascal-style foundation (syntax, basic data types)
                      –    Exception handling
                      –    Packages / encapsulation
                      –    Generic templates
                      –    Concurrency support (“tasking”)
                      –    Object-Oriented Programming
                      –    Support for “contracts”
                             – Since Ada 2012
                      –    Specialized support for particular domains, including:
                             – Systems programming
                             – Real-time systems
                             – High-Integrity applications


Copyright © 2012 AdaCore                                                                              Slide: 10
Ada for safety-critical systems (I)



             •      Full Ada not appropriate
                      –    Run-time library too large / complex
                      –    High level features interfere with traceability and analyzability


             •      Why consider Ada for safety/security-critical software
                      –    General design philosophy promotes sound software engineering
                      –    Successful history from Ada 83


             •      Standard support for safety/security-critical systems
                      –    High-Integrity Systems Annex
                      –    Contracts
                      –    Profiles
                             – Such as the Ravenscar profile
                      –    User-defined tailoring, specifying features that are not used (pragma Restrictions)
                             – Compiler rejects programs that use these features
                             – Executable program excludes run-time libraries for these features


             •      Guidelines documents
                      –    Guide for Ada in High-Integrity Systems (an ISO Technical Report)
                      –    Guide for Ada Ravenscar Profile in High-Integrity Systems



Copyright © 2012 AdaCore                                                                                         Slide: 11
Ada for safety-critical systems (II)



             •      Reliability
                      –    Very few features have surprising effects
                             – Prevention of dangling references to declared objects, subprograms
                             – Specification of intent on operation inheritance
                      –    Contracts
                      –    No garbage collection
                             – Programmer responsible for memory management


             •      Predictability
                      –    Language semantics are generally well-defined, in an ISO standard
                             – But there are features whose effects are implementation defined, implementation
                               dependent, unspecified, or “bounded errors”
                      –    Implementation decisions can affect time or space predictability
                             – Functions returning unconstrained arrays
                      –    Solutions in practice
                             – Analyze source program (e.g. no read of uninitialized object)
                             – Adhere to subset (no functions returning unconstrained arrays)
                             – Analyze object code so that implementation decision is known


Copyright © 2012 AdaCore                                                                                         Slide: 12
Ada for safety-critical systems (III)



             •      Analyzability
                      –    Some features help analyzability
                             – Child units may be used for testing packages with encapsulated state
                      –    But full language is too complex; need to subset
                      –    Key features are pragma Restrictions and pragma Profile
                             – Ada is in effect a family of profiles, where user can select features à la carte
                             – No such thing as the safety-critical Ada profile
                             – Troublesome OOP features are easily avoided
                      –    But no standard annotation facility


             •      Expressibility
                      –    Support for low-level and real-time programming
                      –    Ravenscar Profile for certifiable concurrent applications
                      –    Good inter-language interfacing facilities, to incorporate certifiable libraries from other
                           languages
                      –    Some weaknesses
                             – Limited support for distribution / networking


Copyright © 2012 AdaCore                                                                                                 Slide: 13
Ada examples



                    Configuration                                Spec (design by contract)
   pragma Profile (Ravenscar);                      package Arith is
   -- Only Ravenscar tasking allowed                   procedure Double (X : in out Integer) with
                                                          Pre => X >= Integer'First / 2 and then
   pragma Restrictions (No_Allocators);                           X <= Integer'Last / 2,
   -- Avoid dynamic memory                                Post => X = 2 * X'Old;
                                                    end Arith;
   pragma Restrictions (No_Access_Subprograms);
   -- No pointers to subprograms




                                      System programming

     type Parity_Kind is (Even, Odd);          for UART_Data_Register use
                                                  record
     type Reserved_21 is mod 2**21;                  Data         at 0 range 0 .. 7;
     for Reserved_21'Size use 21;                    Data_Ready   at 0 range 8 .. 8;
                                                     Parity       at 0 range 9 .. 9;
     Type UART_Data_Register is                      Parity_Error at 0 range 10 .. 10;
        record                                       Reserved     at 0 range 11 .. 31;
           Data         : Character;              end record;
           Data_Ready   : Boolean;             for UART_Data_Register'Size use 32;
           Parity       : Parity_Kind;
           Parity_Error : Boolean;             UART_A : UART_Data_Register;
           Reserved     : Reserved_21;         pragma Atomic (UART_A);
        end record;                            for UART_A’Address use To_Address (16#8000_0100#);


Copyright © 2012 AdaCore                                                                     Slide: 14
Ada evaluation



             •      Strengths
                      –    Sound base language for programming high-reliability systems
                      –    Allows construction of tailored high-integrity subsets that are:
                             – Powerful enough to use for real systems
                             – Simple enough to be amenable to certification
                      –    Scales up to large systems
                      –    ISO Standard
                      –    Concurrency support / Ravenscar Profile
                      –    Design by contract
                      –    Free and publicly available documents
                             – Ada Reference Manual, Rationale
                             – Guidelines documents
                      –    Good track record in avionics, train control, other safety-critical domains


             •      Weaknesses
                      –    Whole language is too large, so must be subsetted
                             – Aside from the standard Ravenscar Profile, others are vendor specific
                      –    Language has some features with implementation-dependent or unspecified behavior
                             – Such as referencing a variable before its initialization
                      –    Smaller user / tool vendor community than other languages

Copyright © 2012 AdaCore                                                                                      Slide: 15
SPARK introduction (I)



             •      SPARK is
                      –    A language, plus
                      –    Static analysis tools based on underlying principle of correctness by construction


             •      Facilitate rigorous, static demonstration that program does what it is supposed
                    to do
                      –    And only that


             •      Guarantee bounded time and space requirements

             •      Allow simple run-time library amenable to certification

             •      Ada-based language
                      –    Ada plus contracts minus features that interfere with above principle
                      –    Contracts are special comments handled by the SPARK tools


             •      Language restrictions (Ada features intentionally omitted)
                      –    Features that complicate analysis / formal proofs, e.g.:
                             – Exceptions, goto statement, generic templates, function side effects
                      –    Features that interfere with bounded time / space requirement, e.g.:
                             – Recursion, pointers, dynamically-sized arrays

Copyright © 2012 AdaCore                                                                                        Slide: 16
SPARK introduction (II)



             •      Language features include:
                      –    Most of Ada’s “static semantic” features, including packages, private types,
                           unconstrained array types, “OOP Lite”
                      –    Concurrency (Ravenscar tasking profile)


             •      Contracts
                      –    Data / information flow (use of global variables, formal parameters)
                      –    Inter-module dependencies
                      –    Specification of dynamic behavior (pre / post-conditions, assertions)


             •      Analysis performed by SPARK tools
                      –    Static semantic analysis – detect aliasing, function side effects, …
                      –    Control flow analysis – detect dead code, banned constructs (goto, …)
                      –    Data-flow analysis – detect unused or uninitialized variables, …
                      –    Information-flow analysis – check input / output variable coupling
                      –    Verification condition generation – generate theorems
                      –    Theorem proving


Copyright © 2012 AdaCore                                                                                  Slide: 17
SPARK example




                                    Contracts and flow information


                           procedure Process
                              (Output         : out T;
                               Input1, Input2 : in T);

                           --#   global out Global_Output;
                           --#          in Global_Input;
                           --#   derives Output        from Input1, Input2 &
                           --#           Global_Output from Global_Input, Input2;
                           --#   pre Input1 /= 0;
                           --#   post Output = Input2 / Input1;




Copyright © 2012 AdaCore                                                            Slide: 18
SPARK for safety-critical systems



             •      Reliability
                      –    Inherits Ada’s general advantages (few syntactic surprises)
                      –    Avoids some errors that may occur in full Ada
                      –    Reference to uninitialized variable


             •      Predictability
                      –    Unambiguous specification with no implementation dependencies
                             – But not a formal standard
                      –    Contracts allow statically checkable assertions about resource requirements


             •      Analyzability
                      –    SPARK Ada subset eliminates features that are complicated to analyze


             •      Expressibility
                      –    Missing some useful functionality (e.g. generics) but does include some tasking features




Copyright © 2012 AdaCore                                                                                          Slide: 19
SPARK evaluation



             •      Strengths
                      –    Designed from the start to satisfy high-integrity requirements
                             – Language rules are unambiguous, implementation independent
                             – Insecurities (e.g., “aliasing” of formal parameter and global variable) and many kinds of
                                 hard-to-detect bugs (e.g. use before initialization) are prevented
                      –    Contracts show programmer’s intent, aid readability
                      –    Support for concurrency (Ravenscar profile)
                      –    Tedious aspects of constructing proofs are automated
                      –    Positive experience across a range of high-integrity projects
                             – Lower certification costs and post-delivery bug rates
                      –    Excellent reference material


             •      Weaknesses
                      –    Language lacks some useful features
                             – No “pointers”, but in many kinds of safety critical systems a restricted style for pointers is
                                permitted
                                    • Pointers to statically declared data
                                    • Dynamic allocation at system startup
                             – Some restrictions (e.g. no recursion) require stylistic workarounds
                      –    Contracts require different development style than what most programmers are used to
                      –    Relatively small user community
                      –    Small number of suppliers for tools, services


Copyright © 2012 AdaCore                                                                                                        Slide: 20
Concurrency




Copyright © 2012 AdaCore                 Slide: 21
Concurrency in Ada (short history)



             •      Concurrency a first-class citizen in Ada
                      –          Easy to use and analyze


                      –          Since the beginning
                                   – Well-developed tasking in Ada 83


                      –          Ada 95, Ada 2005 , Ada 2012 improved and extended tasking
                           Ada




                                    Decades of experience in using Ada on multiprocessors




Copyright © 2012 AdaCore                                                                     Slide: 22
Why Ravenscar



             •      Use concurrency in embedded real-time systems
                      –    Verifiable
                      –    Simple
                             – Implemented reliably and efficiently


             •      Scheduling theory for accurate analysis of real-time behavior
                      –    Preemptive fixed priority scheduling
                             – Rate Monotonic Analysis (RMA)
                             – Response Time Analysis (RTA)
                      –    Priority Ceiling Protocol (PCP)
                             – Avoids unbounded priority inversion and deadlocks


             •      Tool support
                      –    RMA and RTA
                      –    Static simulation of concurrent real-time programs




Copyright © 2012 AdaCore                                                            Slide: 23
What is the Ravenscar profile



             •      A subset of the Ada tasking model


             •      Defined to meet safety-critical real-time requirements
                      –    Determinism
                      –    Schedulability analysis
                      –    Memory-boundedness
                      –    Execution efficiency and small footprint
                      –    Suitability for certification


             •      State-of-the-art concurrency constructs
                      –    Adequate for most types of real-time software




Copyright © 2012 AdaCore                                                     Slide: 24
Tasks



             •      Fixed set of tasks
                      –    Only at library level
                      –    No dynamic allocation
                      –    No nested declaration of tasks
                      –    Statically created
                             – Task descriptors, stacks, …


             •      Each task is infinite loop
                      –    Single “triggering” action (delay or event)


             •      Task creation and activation is very simple and deterministic
                      –    All tasks are created at initialization
                      –    Then all activated and executed according to their priority




Copyright © 2012 AdaCore                                                                 Slide: 25
How tasks look like in Ravenscar




                           Time-triggered task                       Event-triggered task


  task body Cyclic is
     Period : constant Time_Span := Milliseconds (10);
     Activation : Time := Clock;                         task body Sporadic is
  begin                                                  begin
     loop                                                   loop
        delay until Activation;                                Monitor.Wait_Event; --   Protected entry

            --    Do something                                --   Do something

            Something;                                         Something;
                                                            end loop;
            --    Compute next activation time           end Sporadic;

        Activation := Activation + Period;
     end loop;
  end Cyclic;




Copyright © 2012 AdaCore                                                                         Slide: 26
Periodic activity in Ada and C

                                                    static void *periodic (void *arg) {
                                                      struct timespec activation;

                                                        clock_gettime (CLOCK_REALTIME, &activation);

                     C        Periodic                  while (1) {
                                                          clock_nanosleep (CLOCK_REALTIME, TIMER_ABSTIME,
                                                                           &activation, NULL);
                           Freq = 10 Hz
                                                            /* Do something */

                                                            something ();

                                                            /* Compute next activation time */

                                                            if (1000000000 - activation.tv_nsec < 100000000)
                                                    {
     task body Periodic is                                    activation.tv_sec += 1;
        Activation : Time := Clock;                           activation.tv_nsec += 100000000 - 1000000000;
     begin                                                  } else {
        loop                                                  activation.tv_nsec += 100000000;
           delay until Activation;                          }
                                                        }
               --    Do something                   }

               Something;                           int main(void) {
                                                      pthread_t      thread;
               --    Compute next activation time     pthread_attr_t attr;

           Activation :=                                pthread_attr_init (&attr);
              Activation + Milliseconds (100);
        end loop;                                       pthread_create (&thread, &attr, &periodic, NULL);
     end Periodic;                                      …
                                                    }

Copyright © 2012 AdaCore                                                                               Slide: 27
Synchronization in Ravenscar


                                                                  protected body Buffer is
     protected Buffer is                                             procedure Put (Element : Item)
        procedure Put (Element : Item);                           is
        entry     Get (Element : out                                 begin
     Item);                                                             Container := Element;
     private                                                            Received := True;
        Container : Item;                                            end Put;
        Received : Boolean := False;
     end Buffer;                                   Pr    Buffer      entry Get (Element : out Item)
                                                                        when Received is
                                                   Put
                                                                     begin
                                                   Get
                                                                        Element := Container;
                                                                        Received := False;
                           C   Producer                              end Get;
                                                                  end Buffer;



                                                                              S    Consumer
        task body Producer is
           Period : constant Time_Span := Milliseconds
        (10);
           Activation : Time := Clock;
           Element : Item;
        begin
           loop                                                            task body Consumer is
              delay until Activation;                                         Element : Item;
                                                                           begin
                 Element := Produce_It;                                       loop
                                                                                 Buffer.Get
                 Buffer.Put (Element);                                     (Element);

              Activation := Activation + Period;                                 Consume (Element);
           end loop;                                                          end loop;
        end Producer;                                                      end Consumer;
Copyright © 2012 AdaCore                                                                           Slide: 28
Interrupt support in Ravenscar


                                                          protected Monitor is
                                                             pragma Priority (Interrupt_Priority’Last);

                               Pr     Monitor                procedure Handler;
   External Interrupt                                        pragma Attach_Handler (Handler,
                            Handler
                                                          Interrupt_ID);
                             Wait
                                                             entry Wait;
                                                          private
                                                             Signaled : Boolean := False;
                                                          end Monitor;


                                            S   Interrupt_Task




               protected body Monitor is                          task body Interrupt_Task
                  procedure Handler is                            is
                  begin                                           begin
                     Signaled := True;                               loop
                  end Put;                                              Monitor.Wait;

                  entry Wait when Signaled is                           Handle_Interrupt;
                  begin                                              end loop;
                     Signaled := False;                           end Interrupt_Task;
                  end Wait;
               end Monitor;




Copyright © 2012 AdaCore                                                                                  Slide: 29
Concurrency on linux




Copyright © 2012 AdaCore                          Slide: 30
The model – Ada tasks



             •      Ada task runs on top of operating system thread
                      –    One-to-one correspondence


             •      Task dispatching policy
                      –    Can be selected with pragma Dispatching_Policy
                             – SCHED_OTHER by default
                      –    Preemptive priority scheduling
                             – pragma Dispatching_Policy (FIFO_Within_Priorities)
                                  • pthread_setschedparam (Thread, SCHED_FIFO, Param’Access)
                             – Means run until blocked (or preempted), no time slicing
                             – Reduces non-determinism
                             – NOTE!!: Requires special (root) privileges


             •      Xenomai
                      –    Real-time behavior
                      –    The Ada run-time library can call the Xenomai API to create tasks in kernel domain



Copyright © 2012 AdaCore                                                                                        Slide: 31
The model – Synchronization



             •      A protected type is a data object with locks
                      –    Data encapsulation, accessible through interface (locked access routines)
                             – functions (read the data with read lock)
                             – procedures (read/write the data with write lock)
                             – entries (wait until some condition is met, then read/write the data with write lock)


             •      Priority inheritance
                      –    Guard against priority inversion
                             –   low priority task grabs resource X
                             –   high priority task needs resource X, waits
                             –   medium priority task preempts low priority task, and runs for a long time, holding up high priority task
                      –    Solution, while high priority task is waiting, lend high priority to low priority task


             •      Implementation
                      – Mutual exclusion
                             – Mutex
                      – Waiting / Signaling operations
                             – Conditional variable


Copyright © 2012 AdaCore                                                                                                                    Slide: 32
The model – System programming



             •      Clock and delay
                      –    Clock uses gettimeofday
                      –    Delay operations use timed conditional variables
                             – We can wakeup the task before expiration if needed


             •      Interrupt handling
                      –    Underlying signal mechanism
                      –    A server task per interrupt served
                             – With specific mask to serve the required signal
                             – Call to sigwait




Copyright © 2012 AdaCore                                                            Slide: 33
Multiprocessors




Copyright © 2012 AdaCore                     Slide: 34
Why addressing multiprocessors?



             •      The answer to increasing processing demands
                      –    We cannot increase the clock frequency forever
                      –    We cannot increase the instruction-level parallelism forever
                      –    Provides higher performance for less consumed energy




                                                                        Courtesy IEEE
                                                                        Computer, January
                                                                        2011, page 33.




Copyright © 2012 AdaCore                                                                    Slide: 35
Periodic activity in Ada and C (multiprocessor version)

                                                   static void *periodic (void *arg) {
                                                     struct timespec activation;

                                                       clock_gettime (CLOCK_REALTIME, &activation);

                                                       while (1) {
                     C         Periodic                  clock_nanosleep (CLOCK_REALTIME, TIMER_ABSTIME,
                                                                          &activation, NULL);
                            Freq = 10 Hz                 /* Do something */
                           CPU affinity = 1              something ();

                                                           /* Compute next activation time */

                                                           if (1000000000 - activation.tv_nsec < 100000000) {
                                                             activation.tv_sec += 1;
                                                             activation.tv_nsec += 100000000 - 1000000000;
   task Periodic with CPU => 1;                            } else {
                                                             activation.tv_nsec += 100000000;
   task body Periodic is                                   }
      Activation : Time := Clock;                      }
   begin                                           }
      loop
         delay until Activation;                   int main(void) {
                                                     cpu_set_t      cpuset;
             --     Do something                     pthread_t      thread;
                                                     pthread_attr_t attr;
             Something;
                                                       CPU_ZERO (&cpuset);
             --     Compute next activation time       CPU_SET (0, &cpuset);

                                                       pthread_attr_init (&attr);
         Activation :=
                                                       pthread_attr_setaffinity_np
            Activation + Milliseconds (100);
                                                          (&attr, sizeof (cpu_set_t), &cpuset);
      end loop;
   end Periodic;                                       pthread_create (&thread, &attr, &periodic, NULL);
                                                       …
                                                   }
Copyright © 2012 AdaCore                                                                                   Slide: 36
Some advantages of Ada



             •      Better readability
                      –    No doubts, right?


             •      Semantics
                      –    The Ada run time enforces required dispatching policy, signal mask, master-dependent
                           tasks, …


             •      Portability

                                Platform                    Ada                        C
                           Linux
                           Solaris
                           Windows
                           VxWorks
                           Bare board


Copyright © 2012 AdaCore                                                                                          Slide: 37
Ada 2012



             •      Ada 2012 added explicit support for controlling processor allocation
                           – Dispatching_Domain


             •      Ravenscar profile for multiprocessors


             •      Parallel task synchronization


             •      Memory barriers




Copyright © 2012 AdaCore                                                                   Slide: 38
Concurrent / parallel execution in Ada



             •      Ada has always taken into account parallel architectures
                      –    Allow concurrent/parallel execution
                             – Multicomputers, multiprocessors, interleaved execution


                      –    Even allow parallel execution of a single task
                             – … if its effect is as executed sequentially


                      –    A task can be on the ready queues of more than one processor


                      –    Many partitioning schemes allowed
                             – Via implementation-defined pragmas or non standard library packages




Copyright © 2012 AdaCore                                                                            Slide: 39
Synchronization and communication of parallel activities



             •      Task synchronization
                      –    Protected objects
                             – No language-defined ordering or queuing presumed for tasks competing to start a
                               protected action
                                   • Tasks are intended to spin lock on multi-processors


                      –    Shared variables
                             – Cache coherence


                      –    Rendezvous
                             – The call to the task entry is blocking




Copyright © 2012 AdaCore                                                                                        Slide: 40
Symmetric Multi-Processor (SMP)



             •      Several similar processors
                      –     All processors can perform the same functions


             •      Centralized memory with uniformed access time
                      –     Problem of cache coherence


                           Processor            Processor              Processor    Processor


                       One or                   One or                One or        One or
                      more cache               more cache            more cache    more cache
                        levels                   levels                levels        levels




                                               Main memory                         I/O System

Copyright © 2012 AdaCore                                                                        Slide: 41
Partitioned versus global scheduling


                               Partitioning                                 Global Scheduling




               •     Tasks assigned to a given                     •   Task migration is permitted
                     processor                                          –   Overhead of task migration
               •     How to schedule a group of tasks                       increases with the number of CPUs
                     on a processor is known                            –   Reduced cache performance
                           –   Rate Monotonic Scheduling (static
                               priorities)
                           –   Earliest Deadline First (dynamic    None is better than the other in
                               priorities)                         terms of guaranteed CPU utilization
               •     But, dividing the tasks into groups
                     is NP-hard

Copyright © 2012 AdaCore                                                                                        Slide: 42
Typical OS support for multiprocessors



             •      Set CPU affinity
                      –    Allocate tasks to one CPU (or to a group of CPUs)


             •      Get CPU affinity


             •      Task migration
                      –    From one CPU to another
                      –    Either user-requested or performed by the OS


             •      Spin locks
                      –    Tasks wait in a loop until lock is free (busy waiting)
                      –    Multiprocessor synchronization




Copyright © 2012 AdaCore                                                            Slide: 43
Support for multiprocessors in Ada 83, 95, 05



             •      Ada has always allowed a program’s implementation to be on a
                    multiprocessor system
                      –    Real parallelism
                      –    Inter-processor synchronization


             •      No direct support for affinities
                      –    The OS can decide the best allocation
                      –    The developer
                             – Implementation-defined pragmas or non standard library packages


             •      Allows the full range of partitioning
                      –    But no user control defined in the standard




Copyright © 2012 AdaCore                                                                        Slide: 44
Explicit support for multiprocessors in Ada 2012



             •      Notion of dispatching domain


             •      Safe multiprocessor tasking


             •      Parallel task synchronization


             •      Memory barriers




Copyright © 2012 AdaCore                              Slide: 45
Ada 2012 dispatching domains



             •      Focus on SMPs


             •      Handle mapping of tasks to processors
                      –    Support all schemes
                             – Partitioned
                                  • Tasks allocated to a subset of CPUs
                             – Global
                                  • Implicit task migration supported
                                  • Explicit task migration allowed


             •      Notion of processor dispatching domain
                      –    Group of processors across which global scheduling occurs
                             – Non-overlapping dispatching domains
                      –    Tasks are assigned to an unique dispatching domain
                             – A task may be allocated to a given processor within the dispatching domain
                             – Or free to be in any of the domain



Copyright © 2012 AdaCore                                                                                    Slide: 46
Static allocation to processors




          task type Allocated_Task (Affinity : CPU)
             with CPU => Affinity;

          T1   :   Allocated_Task   (1);
          T2   :   Allocated_Task   (2);
          T3   :   Allocated_Task   (3);
          T4   :   Allocated_Task   (4);




                                       GroupA   :   aliased   Dispatching_Domain   :=   Create   (1,   2);
                                       GroupB   :   aliased   Dispatching_Domain   :=   Create   (3,   4);
                                       GroupC   :   aliased   Dispatching_Domain   :=   Create   (5,   6);
                                       GroupD   :   aliased   Dispatching_Domain   :=   Create   (7,   8);

                                       task type Grouped_Task (Group : access Dispatching_Domain)
                                          with Dispatching_Domain => Group.all;

                                       T1, T2, T3, T4 : Grouped_Task (GroupA’Access);
                                       T5, T6         : Grouped_Task (GroupB’Access);
                                       T7, T8, T9     : Grouped_Task (GroupC’Access);
                                       T10, T11       : Grouped_Task (GroupD’Access);




Copyright © 2012 AdaCore                                                                                     Slide: 47
Dynamic affinity handling



        GroupA     :   Dispatching_Domain   :=   Create   (1,   2);
        GroupB     :   Dispatching_Domain   :=   Create   (3,   4);
        GroupC     :   Dispatching_Domain   :=   Create   (5,   6);
        GroupD     :   Dispatching_Domain   :=   Create   (7,   8);
                                                                      task body T_In_A is
                                                                         Current_CPU : CPU;
                                                                      begin
                                                                         -- In processor 1 or 2

                                                                        Do_Something;
      task T_In_A with Dispatching_Domain =>
      GroupA;                                                           --   In processor 1 only
      task T_Non_Allocated;
                                                                        Set_CPU (1);
      task body Driver is                                               Do_Something;
      begin
         -- Allocate T_Non_Allocated to GroupB                          --   In processor 2 only

           Assign_Task                                                  Set_CPU (Get_Last_CPU (GroupA));
              (GroupB, 3, T_Non_Allocated’Identity);                    Do_Something;
           Do_Something;
                                                                        --   Now again in processor 1 or 2
           --     Move it to a different processor
                                                                        Set_CPU (Not_A_Specific_CPU);
           if Proc_3_Overloaded then                                    Do_Something;
              Set_CPU (4, T_Non_Allocated’Identity);
           end if;                                                      --   Now I am lost. Where am I?
           Do_Something;
                                                                         Current_CPU := Get_CPU;
      end Driver;                                                        pragma Assert
                                                                            (Current_CPU = Not_A_Specific_CPU);
                                                                      end T_In_A;

Copyright © 2012 AdaCore                                                                                      Slide: 48
Handle affinity and dispatching policy



            •      What I want
                     –     Create a group of processors
                     –     Define an specific scheduling policy for
                           the group                                  pragma Priority_Specific_Dispatching
                                                                         (FIFO_Within_Priorities, 20, 25);
                     –     Execute a set of tasks within the group
                                                                      Group : Dispatching_Domain := Create (1, 2);

                                                                      task T1
            •      What I have to do                                     with Dispatching_Domain => Group,
                                                                              Priority           => 22;
                     –     Create a dispatching domain
                     –     Define a non-overlapping priority band     task T2
                                                                         with Dispatching_Domain => Group,
                     –     Allocate tasks to the dispatching domain           Priority           => 23,
                                                                              CPU                => 1;
                     –     Use priorities in the priority band
                                                                      task T3
                                                                         with Dispatching_Domain => Group,
                                                                              Priority           => 24;




Copyright © 2012 AdaCore                                                                                      Slide: 49
Synchronization on multiprocessors



             •      Protected objects
                      –    There is a lock-free optimization for monoprocessors (using priorities)
                             – No longer viable on multiprocessors
                      –    Currently Ada advise that tasks should busy-wait (spin) at their active priority for the lock


             •      Task entries
                      –    Requires internal synchronization primitives aware of multiprocessor
                             – Spin locks




Copyright © 2012 AdaCore                                                                                               Slide: 50
Multicores for real-time safety-critical embedded systems



             •      We need to address:
                      –    Reliability
                      –    Predictability
                      –    Analyzability


             •      The Ravenscar profile for monoprocessors is
                      –    Deterministic
                      –    Time analyzable
                      –    Simple to use and implement


             •      Extend the Ravenscar profile model from monoprocessor to multiprocessor
                      –    Fully partitioned model
                      –    Fixed-priority scheduling
                      –    Static model


             •      Allow for
                      –    Simple implementation
                      –    Verifiable
                      –    Schedulability analysis


Copyright © 2012 AdaCore                                                                      Slide: 51
Static model



             •      Concurrent entities fixed and static
                      –    Tasks and shared memory defined before execution


             •      Static fixed priority scheduling algorithm
                      –    Preemptive fixed priority scheduling in each CPU
                             – Analyzable as in Ravenscar for monoprocessors
                      –    Dynamic-priority scheduling algorithms could increase CPU utilization but:
                             – Higher complexity
                             – Higher run-time overhead
                             – Lower predictability, lower robustness in case of overload


             •      Partitioned
                      –    Each task allocated to an user-defined processor forever
                             – CPU utilization of partitioned scheduling is neither better nor worse than global
                      –    It relies on very well known monoprocessor techniques for priority allocation and timing
                           analysis
                      –    It is much simpler to implement
                             – No task migration


Copyright © 2012 AdaCore                                                                                              Slide: 52
Task scheduling



               •     Tasks statically allocated to
                     processors
                                                                        Ravenscar system on monoprocessor
                           –   No task migration


               •     Preemptive fixed-priority
                     scheduling                                                             0

                                                                                            1
               •     Single shared run time
                                                                                            2
                           –   Per-CPU ready queues
                           –   Spin-locks to protect shared data                            3
                                 – Disabling interrupts is not
                                   enough
                                                                         task Cyclic
                                                                            with Priority   => 100,
               •     Operations on a different                                   CPU        => 3;
                                                                         end Cyclic;
                     processors
                           –   Triggering an special interrupt in the
                               target processor

Copyright © 2012 AdaCore                                                                              Slide: 53
Task synchronization



             •      Library-level protected objects
                      –    Shared data with mutual exclusion
                      –    Both for inter- and intra-processor communication


             •      Simple and efficient mutual exclusion changing priority for intra-processor
                    communication
                      –    As in Ravenscar monoprocessor
                      –    Could be statically detected
                             – Efficiency
                             – Simple timing analysis


             •      Spin-locking for inter-processor synchronization


             •      Awaking tasks from other processors
                      –    Inter-processor interrupt facility to modify the ready queues




Copyright © 2012 AdaCore                                                                          Slide: 54
Parallel task synchronization



             •      Goal
                      –    Effective parallel task synchronization
                      –    Set of tasks blocked and released at once


             •      Typical case
                      –    A group of tasks must wait until all of them reach a synchronization point
                      –    And then be released together to work in parallel


             •      Mimic the POSIX barrier mechanism




Copyright © 2012 AdaCore                                                                                Slide: 55
Parallel barrier example

   package Ada.Synchronous_Barriers is
      pragma Preelaborate (Synchronous_Barriers);
      subtype Barrier_Limit is Positive range 1 .. <imp-
   def>;

      type Synchronous_Barrier
         (Release_Threshold : Barrier_Limit) is limited
   private;

        procedure Wait_For_Release
           (The_Barrier : in out Synchronous_Barrier;
            Notified    : out    Boolean);
                                                  Number_Of_Tasks : constant := 8;
   private
                                                  Barrier : Synchronous_Barrier (Number_Of_Tasks);
      -- not specified by the language
   end Ada.Synchronous_Barriers;
                                                  task type Worker (Affinity : CPU) with CPU => Affinity;

                                                  task body Worker is
                                                     Notified : Boolean;
                                                  begin
                                                     loop
                                                        Wait_For_Release (Barrier, Notified);

                                                        --   Do something in parallel at the same time

                                                        Something;

                                                        if Notified then
                                                           -- Only one task does this

                                                           Ask_For_More_Work;
                                                        end if;
                                                     end loop;
                                                  end Worker;
Copyright © 2012 AdaCore                                                                                 Slide: 56
Memory barriers



             •      Goal
                      –    Have control over cache memories


             •      Typical case
                      –    Non-blocking algorithms to effectively exploit hardware parallelism
                             – lock-free and wait-free


             •      Problem to solve
                      –    How to ensure the correct order of loads and stores with multi-level caches
                      –    Modern multicores do not guarantee this ordering between processors
                             – Optimizations that can result in out-of-order execution
                             – Unless special instructions are used


             •      How to do it with Volatile
                      –    Until Ada 2005
                             – They can never be in cache or registers

                      –    The Ada 2012 (more realistic) approach
                             – Volatiles can be handled in cache memories, but
                             – Guarantee serial ordering
                                   • All tasks of the program (on all processors) that read or update volatile variables see the
                                      same order of updates to the variables
                                   • May need the use of an appropriate memory barrier to flush the cache



Copyright © 2012 AdaCore                                                                                                           Slide: 57
Example of memory barriers


     Shared_Data : Integer;
     pragma Volatile (Shared_Data);
                                                           •   In Ada 83, 95, 2005
     Barrier : Boolean := False;
     pragma Volatile (Barrier);                                 –   Shared_Data and Barrier can never be in
                                                                    cache
     task Producer with CPU => 1;

     task Consumer with CPU => 2;
                                                           •   In Ada 2012
     task body Producer is
     begin                                                      –   Shared_Data and Barrier can be in cache
        …
        Produce (Shared_Data);
        Barrier := True;
        …
     end Producer;

     task body Consumer is
     begin
        …
        while not Barrier loop
           null;
        end loop;

          --    If we see that Barrier has been updated,
          --    we must see the produced value of
          --    Shared_Data.

        Use(Shared_Data);
        …
     end Consumer;


Copyright © 2012 AdaCore                                                                               Slide: 58
Conclusion



             •      There is support in programming environments for high-integrity


             •      Embedded programming can use high-level abstraction constructs


             •       Real-time support
                      –    Computational model amenable to timing analysis


             •      Ada has supported execution on parallel architectures since its inception
                      –    Ada 2012 dispatching domains
                             – Good flexibility and analyzability
                             – Implementable on top of typical operating systems and kernels
                      –    Ravenscar for multiprocessors
                             – Simple extension to Ravenscar on monoprocessors
                             – Partitioning into a set of monoprocessor Ravenscar systems
                             – Keep desired properties found in monoprocessor Ravenscar




Copyright © 2012 AdaCore                                                                        Slide: 59
1 of 59

Recommended

Project P erts2012 by
Project P erts2012Project P erts2012
Project P erts2012AdaCore
2.1K views25 slides
CICC 2001 - Reducing Multiple Design Flow Support Requirements with OLA by
CICC 2001 - Reducing Multiple Design Flow Support Requirements with OLACICC 2001 - Reducing Multiple Design Flow Support Requirements with OLA
CICC 2001 - Reducing Multiple Design Flow Support Requirements with OLATim55Ehrler
324 views12 slides
Psi multi accessgateway_casestudy by
Psi multi accessgateway_casestudyPsi multi accessgateway_casestudy
Psi multi accessgateway_casestudyPrimesoftinc
191 views11 slides
Kahuna Systems : Product Engineering Services by
Kahuna Systems : Product Engineering ServicesKahuna Systems : Product Engineering Services
Kahuna Systems : Product Engineering Serviceskahunasystems
773 views22 slides
New Opportunity Coming On Your Way......... by
New Opportunity Coming On Your Way.........New Opportunity Coming On Your Way.........
New Opportunity Coming On Your Way.........nandinipm
266 views11 slides
Jay_Vicory_Resume_2018 by
Jay_Vicory_Resume_2018Jay_Vicory_Resume_2018
Jay_Vicory_Resume_2018Jay Vicory
137 views18 slides

More Related Content

What's hot

Ghoshal_resume_LinkedIn_20160705 by
Ghoshal_resume_LinkedIn_20160705Ghoshal_resume_LinkedIn_20160705
Ghoshal_resume_LinkedIn_20160705Siddhartha Ghoshal
217 views3 slides
Embedded services by Faststream Technologies by
Embedded services by Faststream TechnologiesEmbedded services by Faststream Technologies
Embedded services by Faststream TechnologiesHari Narayana
18 views14 slides
FEL Flyer F12 by
FEL Flyer F12FEL Flyer F12
FEL Flyer F12chitlesh
604 views12 slides
Jython for embedded software validation by
Jython for embedded software validationJython for embedded software validation
Jython for embedded software validationPyCon Italia
470 views26 slides
SivaBorra by
SivaBorraSivaBorra
SivaBorraSiva Borra
313 views7 slides
Fel Flyer F11 by
Fel Flyer F11Fel Flyer F11
Fel Flyer F11chitlesh
502 views10 slides

What's hot(20)

Embedded services by Faststream Technologies by Hari Narayana
Embedded services by Faststream TechnologiesEmbedded services by Faststream Technologies
Embedded services by Faststream Technologies
Hari Narayana18 views
FEL Flyer F12 by chitlesh
FEL Flyer F12FEL Flyer F12
FEL Flyer F12
chitlesh604 views
Jython for embedded software validation by PyCon Italia
Jython for embedded software validationJython for embedded software validation
Jython for embedded software validation
PyCon Italia470 views
Fel Flyer F11 by chitlesh
Fel Flyer F11Fel Flyer F11
Fel Flyer F11
chitlesh502 views
Reverse engineering by Saswat Padhi
Reverse engineeringReverse engineering
Reverse engineering
Saswat Padhi6.2K views
Fel Flyer F10 by chitlesh
Fel Flyer F10Fel Flyer F10
Fel Flyer F10
chitlesh546 views
Software defect prevention example project by Zarko Acimovic
Software defect prevention example projectSoftware defect prevention example project
Software defect prevention example project
Zarko Acimovic673 views
Interoperability of Reconfiguring System on FPGA Using a Design Entry of Hard... by IDES Editor
Interoperability of Reconfiguring System on FPGA Using a Design Entry of Hard...Interoperability of Reconfiguring System on FPGA Using a Design Entry of Hard...
Interoperability of Reconfiguring System on FPGA Using a Design Entry of Hard...
IDES Editor544 views
Design of LDPC Decoder Based On FPGA in Digital Image Watermarking Technology by TELKOMNIKA JOURNAL
Design of LDPC Decoder Based On FPGA in Digital Image Watermarking TechnologyDesign of LDPC Decoder Based On FPGA in Digital Image Watermarking Technology
Design of LDPC Decoder Based On FPGA in Digital Image Watermarking Technology
Swindon the making of an asic by SwindinSilicon
Swindon the making of an asicSwindon the making of an asic
Swindon the making of an asic
SwindinSilicon1.6K views
Standard embedded c by Tam Thanh
Standard embedded cStandard embedded c
Standard embedded c
Tam Thanh8.2K views
OpenSAF Symposium_Python Bindings_9.21.11 by OpenSAF Foundation
OpenSAF Symposium_Python Bindings_9.21.11OpenSAF Symposium_Python Bindings_9.21.11
OpenSAF Symposium_Python Bindings_9.21.11
OpenSAF Foundation1.3K views
Caps Professional Services Diagnostic by lebenworld
Caps Professional Services DiagnosticCaps Professional Services Diagnostic
Caps Professional Services Diagnostic
lebenworld83 views

Viewers also liked

Couverture erts2012 by
Couverture erts2012Couverture erts2012
Couverture erts2012AdaCore
2.9K views62 slides
Gnu linux for safety related systems by
Gnu linux for safety related systemsGnu linux for safety related systems
Gnu linux for safety related systemsDTQ4
2.8K views24 slides
Tech Days 2015: Dynamic Analysis by
Tech Days 2015: Dynamic AnalysisTech Days 2015: Dynamic Analysis
Tech Days 2015: Dynamic AnalysisAdaCore
839 views50 slides
Hi-Lite erts2012 by
Hi-Lite erts2012Hi-Lite erts2012
Hi-Lite erts2012AdaCore
3.8K views25 slides
Programming Embedded linux by
Programming Embedded linuxProgramming Embedded linux
Programming Embedded linuxLiran Ben Haim
3.3K views350 slides
The end of embedded Linux (as we know it) by
The end of embedded Linux (as we know it)The end of embedded Linux (as we know it)
The end of embedded Linux (as we know it)Chris Simmonds
1.9K views29 slides

Viewers also liked(6)

Couverture erts2012 by AdaCore
Couverture erts2012Couverture erts2012
Couverture erts2012
AdaCore2.9K views
Gnu linux for safety related systems by DTQ4
Gnu linux for safety related systemsGnu linux for safety related systems
Gnu linux for safety related systems
DTQ42.8K views
Tech Days 2015: Dynamic Analysis by AdaCore
Tech Days 2015: Dynamic AnalysisTech Days 2015: Dynamic Analysis
Tech Days 2015: Dynamic Analysis
AdaCore839 views
Hi-Lite erts2012 by AdaCore
Hi-Lite erts2012Hi-Lite erts2012
Hi-Lite erts2012
AdaCore3.8K views
Programming Embedded linux by Liran Ben Haim
Programming Embedded linuxProgramming Embedded linux
Programming Embedded linux
Liran Ben Haim3.3K views
The end of embedded Linux (as we know it) by Chris Simmonds
The end of embedded Linux (as we know it)The end of embedded Linux (as we know it)
The end of embedded Linux (as we know it)
Chris Simmonds1.9K views

Similar to Safe and Reliable Embedded Linux Programming: How to Get There

Managing High Availability with Low Cost by
Managing High Availability with Low CostManaging High Availability with Low Cost
Managing High Availability with Low CostDataLeader.io
307 views13 slides
20120709 cyber patterns2012 by
20120709 cyber patterns201220120709 cyber patterns2012
20120709 cyber patterns2012Aniketos EU FP7 Project
180 views29 slides
Dynamic Language Practices by
Dynamic Language PracticesDynamic Language Practices
Dynamic Language PracticesPaul King
4.6K views127 slides
A software monitoring framework for quality verification by
A software monitoring framework for quality verificationA software monitoring framework for quality verification
A software monitoring framework for quality verificationDileepa Jayathilake
588 views12 slides
Programming languages and techniques for today’s embedded andIoT world by
Programming languages and techniques for today’s embedded andIoT worldProgramming languages and techniques for today’s embedded andIoT world
Programming languages and techniques for today’s embedded andIoT worldRogue Wave Software
165 views20 slides
Project P by
Project PProject P
Project Pmatteobordinadacore
6K views26 slides

Similar to Safe and Reliable Embedded Linux Programming: How to Get There(20)

Managing High Availability with Low Cost by DataLeader.io
Managing High Availability with Low CostManaging High Availability with Low Cost
Managing High Availability with Low Cost
DataLeader.io307 views
Dynamic Language Practices by Paul King
Dynamic Language PracticesDynamic Language Practices
Dynamic Language Practices
Paul King4.6K views
A software monitoring framework for quality verification by Dileepa Jayathilake
A software monitoring framework for quality verificationA software monitoring framework for quality verification
A software monitoring framework for quality verification
Programming languages and techniques for today’s embedded andIoT world by Rogue Wave Software
Programming languages and techniques for today’s embedded andIoT worldProgramming languages and techniques for today’s embedded andIoT world
Programming languages and techniques for today’s embedded andIoT world
Monitoreo y análisis de aplicaciones "Multi-Tier" by GeneXus
Monitoreo y análisis de aplicaciones "Multi-Tier"Monitoreo y análisis de aplicaciones "Multi-Tier"
Monitoreo y análisis de aplicaciones "Multi-Tier"
GeneXus518 views
iOS application (in)security by iphonepentest
iOS application (in)securityiOS application (in)security
iOS application (in)security
iphonepentest9.1K views
Thick Application Penetration Testing: Crash Course by Scott Sutherland
Thick Application Penetration Testing: Crash CourseThick Application Penetration Testing: Crash Course
Thick Application Penetration Testing: Crash Course
Scott Sutherland16.7K views
Vulnerability Management In An Application Security World by Denim Group
Vulnerability Management In An Application Security WorldVulnerability Management In An Application Security World
Vulnerability Management In An Application Security World
Denim Group1.6K views
Efficient Security Development and Testing Using Dynamic and Static Code Anal... by Perforce
Efficient Security Development and Testing Using Dynamic and Static Code Anal...Efficient Security Development and Testing Using Dynamic and Static Code Anal...
Efficient Security Development and Testing Using Dynamic and Static Code Anal...
Perforce229 views
Thick Application Penetration Testing - A Crash Course by NetSPI
Thick Application Penetration Testing - A Crash CourseThick Application Penetration Testing - A Crash Course
Thick Application Penetration Testing - A Crash Course
NetSPI4.7K views
TechTalk 2021: Peran IT Security dalam Penerapan DevOps by DicodingEvent
TechTalk 2021: Peran IT Security dalam Penerapan DevOpsTechTalk 2021: Peran IT Security dalam Penerapan DevOps
TechTalk 2021: Peran IT Security dalam Penerapan DevOps
DicodingEvent348 views
Tw Technology Radar Qtb Sep11 by Adrian Treacy
Tw Technology Radar Qtb Sep11Tw Technology Radar Qtb Sep11
Tw Technology Radar Qtb Sep11
Adrian Treacy437 views
Strategies for Web Application Security by OpSource
Strategies for Web Application SecurityStrategies for Web Application Security
Strategies for Web Application Security
OpSource831 views
Strategies for Web Application Security by OpSource
Strategies for Web Application SecurityStrategies for Web Application Security
Strategies for Web Application Security
OpSource327 views
Android village @nullcon 2012 by hakersinfo
Android village @nullcon 2012 Android village @nullcon 2012
Android village @nullcon 2012
hakersinfo814 views
Patterns for Building High Performance Applications in Cloud - CloudConnect2012 by Munish Gupta
Patterns for Building High Performance Applications in Cloud - CloudConnect2012Patterns for Building High Performance Applications in Cloud - CloudConnect2012
Patterns for Building High Performance Applications in Cloud - CloudConnect2012
Munish Gupta1.1K views

More from AdaCore

RCA OCORA: Safe Computing Platform using open standards by
RCA OCORA: Safe Computing Platform using open standardsRCA OCORA: Safe Computing Platform using open standards
RCA OCORA: Safe Computing Platform using open standardsAdaCore
281 views27 slides
Have we a Human Ecosystem? by
Have we a Human Ecosystem?Have we a Human Ecosystem?
Have we a Human Ecosystem?AdaCore
43 views70 slides
Rust and the coming age of high integrity languages by
Rust and the coming age of high integrity languagesRust and the coming age of high integrity languages
Rust and the coming age of high integrity languagesAdaCore
113 views29 slides
SPARKNaCl: A verified, fast cryptographic library by
SPARKNaCl: A verified, fast cryptographic librarySPARKNaCl: A verified, fast cryptographic library
SPARKNaCl: A verified, fast cryptographic libraryAdaCore
57 views36 slides
Developing Future High Integrity Processing Solutions by
Developing Future High Integrity Processing SolutionsDeveloping Future High Integrity Processing Solutions
Developing Future High Integrity Processing SolutionsAdaCore
127 views13 slides
Taming event-driven software via formal verification by
Taming event-driven software via formal verificationTaming event-driven software via formal verification
Taming event-driven software via formal verificationAdaCore
68 views16 slides

More from AdaCore(20)

RCA OCORA: Safe Computing Platform using open standards by AdaCore
RCA OCORA: Safe Computing Platform using open standardsRCA OCORA: Safe Computing Platform using open standards
RCA OCORA: Safe Computing Platform using open standards
AdaCore281 views
Have we a Human Ecosystem? by AdaCore
Have we a Human Ecosystem?Have we a Human Ecosystem?
Have we a Human Ecosystem?
AdaCore43 views
Rust and the coming age of high integrity languages by AdaCore
Rust and the coming age of high integrity languagesRust and the coming age of high integrity languages
Rust and the coming age of high integrity languages
AdaCore113 views
SPARKNaCl: A verified, fast cryptographic library by AdaCore
SPARKNaCl: A verified, fast cryptographic librarySPARKNaCl: A verified, fast cryptographic library
SPARKNaCl: A verified, fast cryptographic library
AdaCore57 views
Developing Future High Integrity Processing Solutions by AdaCore
Developing Future High Integrity Processing SolutionsDeveloping Future High Integrity Processing Solutions
Developing Future High Integrity Processing Solutions
AdaCore127 views
Taming event-driven software via formal verification by AdaCore
Taming event-driven software via formal verificationTaming event-driven software via formal verification
Taming event-driven software via formal verification
AdaCore68 views
Pushing the Boundary of Mostly Automatic Program Proof by AdaCore
Pushing the Boundary of Mostly Automatic Program ProofPushing the Boundary of Mostly Automatic Program Proof
Pushing the Boundary of Mostly Automatic Program Proof
AdaCore29 views
RCA OCORA: Safe Computing Platform using open standards by AdaCore
RCA OCORA: Safe Computing Platform using open standardsRCA OCORA: Safe Computing Platform using open standards
RCA OCORA: Safe Computing Platform using open standards
AdaCore21 views
Product Lines and Ecosystems: from customization to configuration by AdaCore
Product Lines and Ecosystems: from customization to configurationProduct Lines and Ecosystems: from customization to configuration
Product Lines and Ecosystems: from customization to configuration
AdaCore87 views
Securing the Future of Safety and Security of Embedded Software by AdaCore
Securing the Future of Safety and Security of Embedded SoftwareSecuring the Future of Safety and Security of Embedded Software
Securing the Future of Safety and Security of Embedded Software
AdaCore2.4K views
Spark / Ada for Safe and Secure Firmware Development by AdaCore
Spark / Ada for Safe and Secure Firmware DevelopmentSpark / Ada for Safe and Secure Firmware Development
Spark / Ada for Safe and Secure Firmware Development
AdaCore360 views
Introducing the HICLASS Research Programme - Enabling Development of Complex ... by AdaCore
Introducing the HICLASS Research Programme - Enabling Development of Complex ...Introducing the HICLASS Research Programme - Enabling Development of Complex ...
Introducing the HICLASS Research Programme - Enabling Development of Complex ...
AdaCore1K views
The Future of Aerospace – More Software Please! by AdaCore
The Future of Aerospace – More Software Please!The Future of Aerospace – More Software Please!
The Future of Aerospace – More Software Please!
AdaCore381 views
Adaptive AUTOSAR - The New AUTOSAR Architecture by AdaCore
Adaptive AUTOSAR - The New AUTOSAR ArchitectureAdaptive AUTOSAR - The New AUTOSAR Architecture
Adaptive AUTOSAR - The New AUTOSAR Architecture
AdaCore4K views
Using Tiers of Assurance Evidence to Reduce the Tears! Adopting the “Wheel of... by AdaCore
Using Tiers of Assurance Evidence to Reduce the Tears! Adopting the “Wheel of...Using Tiers of Assurance Evidence to Reduce the Tears! Adopting the “Wheel of...
Using Tiers of Assurance Evidence to Reduce the Tears! Adopting the “Wheel of...
AdaCore397 views
Software Engineering for Robotics - The RoboStar Technology by AdaCore
Software Engineering for Robotics - The RoboStar TechnologySoftware Engineering for Robotics - The RoboStar Technology
Software Engineering for Robotics - The RoboStar Technology
AdaCore316 views
MISRA C in an ISO 26262 context by AdaCore
MISRA C in an ISO 26262 contextMISRA C in an ISO 26262 context
MISRA C in an ISO 26262 context
AdaCore3.6K views
Application of theorem proving for safety-critical vehicle software by AdaCore
Application of theorem proving for safety-critical vehicle softwareApplication of theorem proving for safety-critical vehicle software
Application of theorem proving for safety-critical vehicle software
AdaCore427 views
The Application of Formal Methods to Railway Signalling Software by AdaCore
The Application of Formal Methods to Railway Signalling SoftwareThe Application of Formal Methods to Railway Signalling Software
The Application of Formal Methods to Railway Signalling Software
AdaCore1.6K views
Bounded Model Checking for C Programs in an Enterprise Environment by AdaCore
Bounded Model Checking for C Programs in an Enterprise EnvironmentBounded Model Checking for C Programs in an Enterprise Environment
Bounded Model Checking for C Programs in an Enterprise Environment
AdaCore1.1K views

Recently uploaded

Uni Systems for Power Platform.pptx by
Uni Systems for Power Platform.pptxUni Systems for Power Platform.pptx
Uni Systems for Power Platform.pptxUni Systems S.M.S.A.
50 views21 slides
Attacking IoT Devices from a Web Perspective - Linux Day by
Attacking IoT Devices from a Web Perspective - Linux Day Attacking IoT Devices from a Web Perspective - Linux Day
Attacking IoT Devices from a Web Perspective - Linux Day Simone Onofri
15 views68 slides
Melek BEN MAHMOUD.pdf by
Melek BEN MAHMOUD.pdfMelek BEN MAHMOUD.pdf
Melek BEN MAHMOUD.pdfMelekBenMahmoud
14 views1 slide
Black and White Modern Science Presentation.pptx by
Black and White Modern Science Presentation.pptxBlack and White Modern Science Presentation.pptx
Black and White Modern Science Presentation.pptxmaryamkhalid2916
14 views21 slides
Empathic Computing: Delivering the Potential of the Metaverse by
Empathic Computing: Delivering  the Potential of the MetaverseEmpathic Computing: Delivering  the Potential of the Metaverse
Empathic Computing: Delivering the Potential of the MetaverseMark Billinghurst
470 views80 slides
Perth MeetUp November 2023 by
Perth MeetUp November 2023 Perth MeetUp November 2023
Perth MeetUp November 2023 Michael Price
15 views44 slides

Recently uploaded(20)

Attacking IoT Devices from a Web Perspective - Linux Day by Simone Onofri
Attacking IoT Devices from a Web Perspective - Linux Day Attacking IoT Devices from a Web Perspective - Linux Day
Attacking IoT Devices from a Web Perspective - Linux Day
Simone Onofri15 views
Black and White Modern Science Presentation.pptx by maryamkhalid2916
Black and White Modern Science Presentation.pptxBlack and White Modern Science Presentation.pptx
Black and White Modern Science Presentation.pptx
maryamkhalid291614 views
Empathic Computing: Delivering the Potential of the Metaverse by Mark Billinghurst
Empathic Computing: Delivering  the Potential of the MetaverseEmpathic Computing: Delivering  the Potential of the Metaverse
Empathic Computing: Delivering the Potential of the Metaverse
Mark Billinghurst470 views
Perth MeetUp November 2023 by Michael Price
Perth MeetUp November 2023 Perth MeetUp November 2023
Perth MeetUp November 2023
Michael Price15 views
The Importance of Cybersecurity for Digital Transformation by NUS-ISS
The Importance of Cybersecurity for Digital TransformationThe Importance of Cybersecurity for Digital Transformation
The Importance of Cybersecurity for Digital Transformation
NUS-ISS27 views
Web Dev - 1 PPT.pdf by gdsczhcet
Web Dev - 1 PPT.pdfWeb Dev - 1 PPT.pdf
Web Dev - 1 PPT.pdf
gdsczhcet55 views
Business Analyst Series 2023 - Week 3 Session 5 by DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10209 views
Data-centric AI and the convergence of data and model engineering: opportunit... by Paolo Missier
Data-centric AI and the convergence of data and model engineering:opportunit...Data-centric AI and the convergence of data and model engineering:opportunit...
Data-centric AI and the convergence of data and model engineering: opportunit...
Paolo Missier34 views
RADIUS-Omnichannel Interaction System by RADIUS
RADIUS-Omnichannel Interaction SystemRADIUS-Omnichannel Interaction System
RADIUS-Omnichannel Interaction System
RADIUS15 views
Voice Logger - Telephony Integration Solution at Aegis by Nirmal Sharma
Voice Logger - Telephony Integration Solution at AegisVoice Logger - Telephony Integration Solution at Aegis
Voice Logger - Telephony Integration Solution at Aegis
Nirmal Sharma17 views
Future of Learning - Khoong Chan Meng by NUS-ISS
Future of Learning - Khoong Chan MengFuture of Learning - Khoong Chan Meng
Future of Learning - Khoong Chan Meng
NUS-ISS33 views
Igniting Next Level Productivity with AI-Infused Data Integration Workflows by Safe Software
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Safe Software225 views
Combining Orchestration and Choreography for a Clean Architecture by ThomasHeinrichs1
Combining Orchestration and Choreography for a Clean ArchitectureCombining Orchestration and Choreography for a Clean Architecture
Combining Orchestration and Choreography for a Clean Architecture
ThomasHeinrichs169 views
The details of description: Techniques, tips, and tangents on alternative tex... by BookNet Canada
The details of description: Techniques, tips, and tangents on alternative tex...The details of description: Techniques, tips, and tangents on alternative tex...
The details of description: Techniques, tips, and tangents on alternative tex...
BookNet Canada121 views
Digital Product-Centric Enterprise and Enterprise Architecture - Tan Eng Tsze by NUS-ISS
Digital Product-Centric Enterprise and Enterprise Architecture - Tan Eng TszeDigital Product-Centric Enterprise and Enterprise Architecture - Tan Eng Tsze
Digital Product-Centric Enterprise and Enterprise Architecture - Tan Eng Tsze
NUS-ISS19 views
STPI OctaNE CoE Brochure.pdf by madhurjyapb
STPI OctaNE CoE Brochure.pdfSTPI OctaNE CoE Brochure.pdf
STPI OctaNE CoE Brochure.pdf
madhurjyapb12 views
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor... by Vadym Kazulkin
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...
Vadym Kazulkin75 views

Safe and Reliable Embedded Linux Programming: How to Get There

  • 1. 7 June 2012 Safe and Reliable Embedded Linux Programming How to Get There José F. Ruiz Senior Software Engineer AdaCore EWiLi 2012, Lorient Copyright © 2012 AdaCore Slide: 1
  • 2. Outline • Safety issues – Language matters • Concurrent execution on monoprocessors – Ada Ravenscar Profile • Tasking on linux • Parallel execution on multiprocessors – Processor affinities Copyright © 2012 AdaCore Slide: 2
  • 3. Safety Copyright © 2012 AdaCore Slide: 3
  • 4. Safety-Critical Software: Background • What is “safety critical” software? – Failure can cause loss of human life or have other catastrophic consequences • How does safety criticality affect software development? – Regulatory agencies require compliance with certification requirements – Safety-related standards may apply to the finished product, to the development process, or both • Choice of programming language has large impact on the cost of developing / certifying safety-critical systems – Dilemma: modern features that help in developing maintainable systems interfere with safety certification – Examples: Object-Oriented Programming, generics, exceptions, concurrency – Traditional approach: select subset (profile) amenable to safety certification – Chosen features depend on the analysis methods to be used (or dictate which methods are feasible) Copyright © 2012 AdaCore Slide: 4
  • 5. Some language requirements (I) • Reliability – Support for development of readable, correct code – No “traps and pitfalls” – Intuitive lexical and syntactic features – Support for good software engineering practice – Encapsulation – Avoidance of concurrency bugs – Race conditions – Deadlock / livelock – Early error detection – Compile-time checking whenever possible – Run-time checking, at least • Predictability – Unambiguous language semantics – No undefined / implementation-dependent behavior – Ability to demonstrate statically that time and space constraints will be satisfied – Deadlines will be met – Stack and heap space will not be exhausted Copyright © 2012 AdaCore Slide: 5
  • 6. Some language requirements (II) • Analyzability – Control and data flow analysis – Detects / prevents references to uninitialized data – Information flow analysis – Identify dependencies between a routine’s inputs and outputs – Formal code verification – Range checking – Traceability between program representations and different levels – Dynamic analysis (testing) • Expressiveness – General-purpose features consistent with the above requirements – Support for specialized processing typical in embedded systems (safety) – Interrupt handling – Low-level programming – Fixed-point arithmetic Copyright © 2012 AdaCore Slide: 6
  • 7. Modern programming languages • Many features help – Encapsulation / namespace control – Limit data coupling – Strong typing, compile-time checking – Early error detection • Some features present difficulties – High expressive power vs. need to constrain feature – Dynamic flexibility vs. need for static analysis – High-level features vs. need to certify the code that actually runs – Generated code may contain implicit loops, conditionals – Run-time library, API need to be certifiable – How, if at all, use features such as exceptions, concurrency, dynamic allocation Copyright © 2012 AdaCore Slide: 7
  • 8. Modern programming languages – basic issues • No general-purpose language is appropriate for highest safety/security levels – Conflicts with reliability, predictability, analyzability • Key issue: can the language be subsetted so that applications restricted to the subset can be certified – Is/are the subset(s) precisely defined? – Can compliance with the subset(s) be enforced by a tool? – Who defines the subset(s)? – Standards agency? Tool/compiler vendor? Application developer? – Are there intrinsic problems with the language that cannot be solved by subsetting? • Current approaches – C-based – MISRA C / C++ – Ada-based – Ada profiles – SPARK Ada – Java-based – Real-Time / Safety-Critical Java Technology Copyright © 2012 AdaCore Slide: 8
  • 9. MISRA C • Intended for embedded automotive systems – Around 170 rules • Prevent or restrict usage of C constructs with unpredictable behavior • Encourage static checking tools that enforce compliance with the subset Strengths Weaknesses – “Best practices” for C programming – C was not designed to meet the – General good style needs of high-integrity systems – Avoids C features intrinsically non- – Error-prone portable / ill-defined – Lacks features such as – Simplicity of C compared with other encapsulation, namespaces languages – C (and thus MISRA C) does not – Smaller run-time library – Easier certification “scale up” to large systems – Large population of candidate users – C standard has ambiguities and thus – And tools, service providers, etc. so does MISRA C – May be appropriate for small systems – Some rules are not enforceable by on specialized hardware lacking static tools compilers for alternative languages – Usage of MISRA C requires license – MISRA-C code may be produced as from MISRA output of source-to-source translator Copyright © 2012 AdaCore Slide: 9
  • 10. Ada – introduction • What is Ada? – General-purpose methodology-neutral strongly-typed ISO standard language – Intended for large, long-lived systems – Fits reliability-critical embedded real-time applications • Ada in a nutshell – Pascal-style foundation (syntax, basic data types) – Exception handling – Packages / encapsulation – Generic templates – Concurrency support (“tasking”) – Object-Oriented Programming – Support for “contracts” – Since Ada 2012 – Specialized support for particular domains, including: – Systems programming – Real-time systems – High-Integrity applications Copyright © 2012 AdaCore Slide: 10
  • 11. Ada for safety-critical systems (I) • Full Ada not appropriate – Run-time library too large / complex – High level features interfere with traceability and analyzability • Why consider Ada for safety/security-critical software – General design philosophy promotes sound software engineering – Successful history from Ada 83 • Standard support for safety/security-critical systems – High-Integrity Systems Annex – Contracts – Profiles – Such as the Ravenscar profile – User-defined tailoring, specifying features that are not used (pragma Restrictions) – Compiler rejects programs that use these features – Executable program excludes run-time libraries for these features • Guidelines documents – Guide for Ada in High-Integrity Systems (an ISO Technical Report) – Guide for Ada Ravenscar Profile in High-Integrity Systems Copyright © 2012 AdaCore Slide: 11
  • 12. Ada for safety-critical systems (II) • Reliability – Very few features have surprising effects – Prevention of dangling references to declared objects, subprograms – Specification of intent on operation inheritance – Contracts – No garbage collection – Programmer responsible for memory management • Predictability – Language semantics are generally well-defined, in an ISO standard – But there are features whose effects are implementation defined, implementation dependent, unspecified, or “bounded errors” – Implementation decisions can affect time or space predictability – Functions returning unconstrained arrays – Solutions in practice – Analyze source program (e.g. no read of uninitialized object) – Adhere to subset (no functions returning unconstrained arrays) – Analyze object code so that implementation decision is known Copyright © 2012 AdaCore Slide: 12
  • 13. Ada for safety-critical systems (III) • Analyzability – Some features help analyzability – Child units may be used for testing packages with encapsulated state – But full language is too complex; need to subset – Key features are pragma Restrictions and pragma Profile – Ada is in effect a family of profiles, where user can select features à la carte – No such thing as the safety-critical Ada profile – Troublesome OOP features are easily avoided – But no standard annotation facility • Expressibility – Support for low-level and real-time programming – Ravenscar Profile for certifiable concurrent applications – Good inter-language interfacing facilities, to incorporate certifiable libraries from other languages – Some weaknesses – Limited support for distribution / networking Copyright © 2012 AdaCore Slide: 13
  • 14. Ada examples Configuration Spec (design by contract) pragma Profile (Ravenscar); package Arith is -- Only Ravenscar tasking allowed procedure Double (X : in out Integer) with Pre => X >= Integer'First / 2 and then pragma Restrictions (No_Allocators); X <= Integer'Last / 2, -- Avoid dynamic memory Post => X = 2 * X'Old; end Arith; pragma Restrictions (No_Access_Subprograms); -- No pointers to subprograms System programming type Parity_Kind is (Even, Odd); for UART_Data_Register use record type Reserved_21 is mod 2**21; Data at 0 range 0 .. 7; for Reserved_21'Size use 21; Data_Ready at 0 range 8 .. 8; Parity at 0 range 9 .. 9; Type UART_Data_Register is Parity_Error at 0 range 10 .. 10; record Reserved at 0 range 11 .. 31; Data : Character; end record; Data_Ready : Boolean; for UART_Data_Register'Size use 32; Parity : Parity_Kind; Parity_Error : Boolean; UART_A : UART_Data_Register; Reserved : Reserved_21; pragma Atomic (UART_A); end record; for UART_A’Address use To_Address (16#8000_0100#); Copyright © 2012 AdaCore Slide: 14
  • 15. Ada evaluation • Strengths – Sound base language for programming high-reliability systems – Allows construction of tailored high-integrity subsets that are: – Powerful enough to use for real systems – Simple enough to be amenable to certification – Scales up to large systems – ISO Standard – Concurrency support / Ravenscar Profile – Design by contract – Free and publicly available documents – Ada Reference Manual, Rationale – Guidelines documents – Good track record in avionics, train control, other safety-critical domains • Weaknesses – Whole language is too large, so must be subsetted – Aside from the standard Ravenscar Profile, others are vendor specific – Language has some features with implementation-dependent or unspecified behavior – Such as referencing a variable before its initialization – Smaller user / tool vendor community than other languages Copyright © 2012 AdaCore Slide: 15
  • 16. SPARK introduction (I) • SPARK is – A language, plus – Static analysis tools based on underlying principle of correctness by construction • Facilitate rigorous, static demonstration that program does what it is supposed to do – And only that • Guarantee bounded time and space requirements • Allow simple run-time library amenable to certification • Ada-based language – Ada plus contracts minus features that interfere with above principle – Contracts are special comments handled by the SPARK tools • Language restrictions (Ada features intentionally omitted) – Features that complicate analysis / formal proofs, e.g.: – Exceptions, goto statement, generic templates, function side effects – Features that interfere with bounded time / space requirement, e.g.: – Recursion, pointers, dynamically-sized arrays Copyright © 2012 AdaCore Slide: 16
  • 17. SPARK introduction (II) • Language features include: – Most of Ada’s “static semantic” features, including packages, private types, unconstrained array types, “OOP Lite” – Concurrency (Ravenscar tasking profile) • Contracts – Data / information flow (use of global variables, formal parameters) – Inter-module dependencies – Specification of dynamic behavior (pre / post-conditions, assertions) • Analysis performed by SPARK tools – Static semantic analysis – detect aliasing, function side effects, … – Control flow analysis – detect dead code, banned constructs (goto, …) – Data-flow analysis – detect unused or uninitialized variables, … – Information-flow analysis – check input / output variable coupling – Verification condition generation – generate theorems – Theorem proving Copyright © 2012 AdaCore Slide: 17
  • 18. SPARK example Contracts and flow information procedure Process (Output : out T; Input1, Input2 : in T); --# global out Global_Output; --# in Global_Input; --# derives Output from Input1, Input2 & --# Global_Output from Global_Input, Input2; --# pre Input1 /= 0; --# post Output = Input2 / Input1; Copyright © 2012 AdaCore Slide: 18
  • 19. SPARK for safety-critical systems • Reliability – Inherits Ada’s general advantages (few syntactic surprises) – Avoids some errors that may occur in full Ada – Reference to uninitialized variable • Predictability – Unambiguous specification with no implementation dependencies – But not a formal standard – Contracts allow statically checkable assertions about resource requirements • Analyzability – SPARK Ada subset eliminates features that are complicated to analyze • Expressibility – Missing some useful functionality (e.g. generics) but does include some tasking features Copyright © 2012 AdaCore Slide: 19
  • 20. SPARK evaluation • Strengths – Designed from the start to satisfy high-integrity requirements – Language rules are unambiguous, implementation independent – Insecurities (e.g., “aliasing” of formal parameter and global variable) and many kinds of hard-to-detect bugs (e.g. use before initialization) are prevented – Contracts show programmer’s intent, aid readability – Support for concurrency (Ravenscar profile) – Tedious aspects of constructing proofs are automated – Positive experience across a range of high-integrity projects – Lower certification costs and post-delivery bug rates – Excellent reference material • Weaknesses – Language lacks some useful features – No “pointers”, but in many kinds of safety critical systems a restricted style for pointers is permitted • Pointers to statically declared data • Dynamic allocation at system startup – Some restrictions (e.g. no recursion) require stylistic workarounds – Contracts require different development style than what most programmers are used to – Relatively small user community – Small number of suppliers for tools, services Copyright © 2012 AdaCore Slide: 20
  • 21. Concurrency Copyright © 2012 AdaCore Slide: 21
  • 22. Concurrency in Ada (short history) • Concurrency a first-class citizen in Ada – Easy to use and analyze – Since the beginning – Well-developed tasking in Ada 83 – Ada 95, Ada 2005 , Ada 2012 improved and extended tasking Ada Decades of experience in using Ada on multiprocessors Copyright © 2012 AdaCore Slide: 22
  • 23. Why Ravenscar • Use concurrency in embedded real-time systems – Verifiable – Simple – Implemented reliably and efficiently • Scheduling theory for accurate analysis of real-time behavior – Preemptive fixed priority scheduling – Rate Monotonic Analysis (RMA) – Response Time Analysis (RTA) – Priority Ceiling Protocol (PCP) – Avoids unbounded priority inversion and deadlocks • Tool support – RMA and RTA – Static simulation of concurrent real-time programs Copyright © 2012 AdaCore Slide: 23
  • 24. What is the Ravenscar profile • A subset of the Ada tasking model • Defined to meet safety-critical real-time requirements – Determinism – Schedulability analysis – Memory-boundedness – Execution efficiency and small footprint – Suitability for certification • State-of-the-art concurrency constructs – Adequate for most types of real-time software Copyright © 2012 AdaCore Slide: 24
  • 25. Tasks • Fixed set of tasks – Only at library level – No dynamic allocation – No nested declaration of tasks – Statically created – Task descriptors, stacks, … • Each task is infinite loop – Single “triggering” action (delay or event) • Task creation and activation is very simple and deterministic – All tasks are created at initialization – Then all activated and executed according to their priority Copyright © 2012 AdaCore Slide: 25
  • 26. How tasks look like in Ravenscar Time-triggered task Event-triggered task task body Cyclic is Period : constant Time_Span := Milliseconds (10); Activation : Time := Clock; task body Sporadic is begin begin loop loop delay until Activation; Monitor.Wait_Event; -- Protected entry -- Do something -- Do something Something; Something; end loop; -- Compute next activation time end Sporadic; Activation := Activation + Period; end loop; end Cyclic; Copyright © 2012 AdaCore Slide: 26
  • 27. Periodic activity in Ada and C static void *periodic (void *arg) { struct timespec activation; clock_gettime (CLOCK_REALTIME, &activation); C Periodic while (1) { clock_nanosleep (CLOCK_REALTIME, TIMER_ABSTIME, &activation, NULL); Freq = 10 Hz /* Do something */ something (); /* Compute next activation time */ if (1000000000 - activation.tv_nsec < 100000000) { task body Periodic is activation.tv_sec += 1; Activation : Time := Clock; activation.tv_nsec += 100000000 - 1000000000; begin } else { loop activation.tv_nsec += 100000000; delay until Activation; } } -- Do something } Something; int main(void) { pthread_t thread; -- Compute next activation time pthread_attr_t attr; Activation := pthread_attr_init (&attr); Activation + Milliseconds (100); end loop; pthread_create (&thread, &attr, &periodic, NULL); end Periodic; … } Copyright © 2012 AdaCore Slide: 27
  • 28. Synchronization in Ravenscar protected body Buffer is protected Buffer is procedure Put (Element : Item) procedure Put (Element : Item); is entry Get (Element : out begin Item); Container := Element; private Received := True; Container : Item; end Put; Received : Boolean := False; end Buffer; Pr Buffer entry Get (Element : out Item) when Received is Put begin Get Element := Container; Received := False; C Producer end Get; end Buffer; S Consumer task body Producer is Period : constant Time_Span := Milliseconds (10); Activation : Time := Clock; Element : Item; begin loop task body Consumer is delay until Activation; Element : Item; begin Element := Produce_It; loop Buffer.Get Buffer.Put (Element); (Element); Activation := Activation + Period; Consume (Element); end loop; end loop; end Producer; end Consumer; Copyright © 2012 AdaCore Slide: 28
  • 29. Interrupt support in Ravenscar protected Monitor is pragma Priority (Interrupt_Priority’Last); Pr Monitor procedure Handler; External Interrupt pragma Attach_Handler (Handler, Handler Interrupt_ID); Wait entry Wait; private Signaled : Boolean := False; end Monitor; S Interrupt_Task protected body Monitor is task body Interrupt_Task procedure Handler is is begin begin Signaled := True; loop end Put; Monitor.Wait; entry Wait when Signaled is Handle_Interrupt; begin end loop; Signaled := False; end Interrupt_Task; end Wait; end Monitor; Copyright © 2012 AdaCore Slide: 29
  • 30. Concurrency on linux Copyright © 2012 AdaCore Slide: 30
  • 31. The model – Ada tasks • Ada task runs on top of operating system thread – One-to-one correspondence • Task dispatching policy – Can be selected with pragma Dispatching_Policy – SCHED_OTHER by default – Preemptive priority scheduling – pragma Dispatching_Policy (FIFO_Within_Priorities) • pthread_setschedparam (Thread, SCHED_FIFO, Param’Access) – Means run until blocked (or preempted), no time slicing – Reduces non-determinism – NOTE!!: Requires special (root) privileges • Xenomai – Real-time behavior – The Ada run-time library can call the Xenomai API to create tasks in kernel domain Copyright © 2012 AdaCore Slide: 31
  • 32. The model – Synchronization • A protected type is a data object with locks – Data encapsulation, accessible through interface (locked access routines) – functions (read the data with read lock) – procedures (read/write the data with write lock) – entries (wait until some condition is met, then read/write the data with write lock) • Priority inheritance – Guard against priority inversion – low priority task grabs resource X – high priority task needs resource X, waits – medium priority task preempts low priority task, and runs for a long time, holding up high priority task – Solution, while high priority task is waiting, lend high priority to low priority task • Implementation – Mutual exclusion – Mutex – Waiting / Signaling operations – Conditional variable Copyright © 2012 AdaCore Slide: 32
  • 33. The model – System programming • Clock and delay – Clock uses gettimeofday – Delay operations use timed conditional variables – We can wakeup the task before expiration if needed • Interrupt handling – Underlying signal mechanism – A server task per interrupt served – With specific mask to serve the required signal – Call to sigwait Copyright © 2012 AdaCore Slide: 33
  • 35. Why addressing multiprocessors? • The answer to increasing processing demands – We cannot increase the clock frequency forever – We cannot increase the instruction-level parallelism forever – Provides higher performance for less consumed energy Courtesy IEEE Computer, January 2011, page 33. Copyright © 2012 AdaCore Slide: 35
  • 36. Periodic activity in Ada and C (multiprocessor version) static void *periodic (void *arg) { struct timespec activation; clock_gettime (CLOCK_REALTIME, &activation); while (1) { C Periodic clock_nanosleep (CLOCK_REALTIME, TIMER_ABSTIME, &activation, NULL); Freq = 10 Hz /* Do something */ CPU affinity = 1 something (); /* Compute next activation time */ if (1000000000 - activation.tv_nsec < 100000000) { activation.tv_sec += 1; activation.tv_nsec += 100000000 - 1000000000; task Periodic with CPU => 1; } else { activation.tv_nsec += 100000000; task body Periodic is } Activation : Time := Clock; } begin } loop delay until Activation; int main(void) { cpu_set_t cpuset; -- Do something pthread_t thread; pthread_attr_t attr; Something; CPU_ZERO (&cpuset); -- Compute next activation time CPU_SET (0, &cpuset); pthread_attr_init (&attr); Activation := pthread_attr_setaffinity_np Activation + Milliseconds (100); (&attr, sizeof (cpu_set_t), &cpuset); end loop; end Periodic; pthread_create (&thread, &attr, &periodic, NULL); … } Copyright © 2012 AdaCore Slide: 36
  • 37. Some advantages of Ada • Better readability – No doubts, right? • Semantics – The Ada run time enforces required dispatching policy, signal mask, master-dependent tasks, … • Portability Platform Ada C Linux Solaris Windows VxWorks Bare board Copyright © 2012 AdaCore Slide: 37
  • 38. Ada 2012 • Ada 2012 added explicit support for controlling processor allocation – Dispatching_Domain • Ravenscar profile for multiprocessors • Parallel task synchronization • Memory barriers Copyright © 2012 AdaCore Slide: 38
  • 39. Concurrent / parallel execution in Ada • Ada has always taken into account parallel architectures – Allow concurrent/parallel execution – Multicomputers, multiprocessors, interleaved execution – Even allow parallel execution of a single task – … if its effect is as executed sequentially – A task can be on the ready queues of more than one processor – Many partitioning schemes allowed – Via implementation-defined pragmas or non standard library packages Copyright © 2012 AdaCore Slide: 39
  • 40. Synchronization and communication of parallel activities • Task synchronization – Protected objects – No language-defined ordering or queuing presumed for tasks competing to start a protected action • Tasks are intended to spin lock on multi-processors – Shared variables – Cache coherence – Rendezvous – The call to the task entry is blocking Copyright © 2012 AdaCore Slide: 40
  • 41. Symmetric Multi-Processor (SMP) • Several similar processors – All processors can perform the same functions • Centralized memory with uniformed access time – Problem of cache coherence Processor Processor Processor Processor One or One or One or One or more cache more cache more cache more cache levels levels levels levels Main memory I/O System Copyright © 2012 AdaCore Slide: 41
  • 42. Partitioned versus global scheduling Partitioning Global Scheduling • Tasks assigned to a given • Task migration is permitted processor – Overhead of task migration • How to schedule a group of tasks increases with the number of CPUs on a processor is known – Reduced cache performance – Rate Monotonic Scheduling (static priorities) – Earliest Deadline First (dynamic None is better than the other in priorities) terms of guaranteed CPU utilization • But, dividing the tasks into groups is NP-hard Copyright © 2012 AdaCore Slide: 42
  • 43. Typical OS support for multiprocessors • Set CPU affinity – Allocate tasks to one CPU (or to a group of CPUs) • Get CPU affinity • Task migration – From one CPU to another – Either user-requested or performed by the OS • Spin locks – Tasks wait in a loop until lock is free (busy waiting) – Multiprocessor synchronization Copyright © 2012 AdaCore Slide: 43
  • 44. Support for multiprocessors in Ada 83, 95, 05 • Ada has always allowed a program’s implementation to be on a multiprocessor system – Real parallelism – Inter-processor synchronization • No direct support for affinities – The OS can decide the best allocation – The developer – Implementation-defined pragmas or non standard library packages • Allows the full range of partitioning – But no user control defined in the standard Copyright © 2012 AdaCore Slide: 44
  • 45. Explicit support for multiprocessors in Ada 2012 • Notion of dispatching domain • Safe multiprocessor tasking • Parallel task synchronization • Memory barriers Copyright © 2012 AdaCore Slide: 45
  • 46. Ada 2012 dispatching domains • Focus on SMPs • Handle mapping of tasks to processors – Support all schemes – Partitioned • Tasks allocated to a subset of CPUs – Global • Implicit task migration supported • Explicit task migration allowed • Notion of processor dispatching domain – Group of processors across which global scheduling occurs – Non-overlapping dispatching domains – Tasks are assigned to an unique dispatching domain – A task may be allocated to a given processor within the dispatching domain – Or free to be in any of the domain Copyright © 2012 AdaCore Slide: 46
  • 47. Static allocation to processors task type Allocated_Task (Affinity : CPU) with CPU => Affinity; T1 : Allocated_Task (1); T2 : Allocated_Task (2); T3 : Allocated_Task (3); T4 : Allocated_Task (4); GroupA : aliased Dispatching_Domain := Create (1, 2); GroupB : aliased Dispatching_Domain := Create (3, 4); GroupC : aliased Dispatching_Domain := Create (5, 6); GroupD : aliased Dispatching_Domain := Create (7, 8); task type Grouped_Task (Group : access Dispatching_Domain) with Dispatching_Domain => Group.all; T1, T2, T3, T4 : Grouped_Task (GroupA’Access); T5, T6 : Grouped_Task (GroupB’Access); T7, T8, T9 : Grouped_Task (GroupC’Access); T10, T11 : Grouped_Task (GroupD’Access); Copyright © 2012 AdaCore Slide: 47
  • 48. Dynamic affinity handling GroupA : Dispatching_Domain := Create (1, 2); GroupB : Dispatching_Domain := Create (3, 4); GroupC : Dispatching_Domain := Create (5, 6); GroupD : Dispatching_Domain := Create (7, 8); task body T_In_A is Current_CPU : CPU; begin -- In processor 1 or 2 Do_Something; task T_In_A with Dispatching_Domain => GroupA; -- In processor 1 only task T_Non_Allocated; Set_CPU (1); task body Driver is Do_Something; begin -- Allocate T_Non_Allocated to GroupB -- In processor 2 only Assign_Task Set_CPU (Get_Last_CPU (GroupA)); (GroupB, 3, T_Non_Allocated’Identity); Do_Something; Do_Something; -- Now again in processor 1 or 2 -- Move it to a different processor Set_CPU (Not_A_Specific_CPU); if Proc_3_Overloaded then Do_Something; Set_CPU (4, T_Non_Allocated’Identity); end if; -- Now I am lost. Where am I? Do_Something; Current_CPU := Get_CPU; end Driver; pragma Assert (Current_CPU = Not_A_Specific_CPU); end T_In_A; Copyright © 2012 AdaCore Slide: 48
  • 49. Handle affinity and dispatching policy • What I want – Create a group of processors – Define an specific scheduling policy for the group pragma Priority_Specific_Dispatching (FIFO_Within_Priorities, 20, 25); – Execute a set of tasks within the group Group : Dispatching_Domain := Create (1, 2); task T1 • What I have to do with Dispatching_Domain => Group, Priority => 22; – Create a dispatching domain – Define a non-overlapping priority band task T2 with Dispatching_Domain => Group, – Allocate tasks to the dispatching domain Priority => 23, CPU => 1; – Use priorities in the priority band task T3 with Dispatching_Domain => Group, Priority => 24; Copyright © 2012 AdaCore Slide: 49
  • 50. Synchronization on multiprocessors • Protected objects – There is a lock-free optimization for monoprocessors (using priorities) – No longer viable on multiprocessors – Currently Ada advise that tasks should busy-wait (spin) at their active priority for the lock • Task entries – Requires internal synchronization primitives aware of multiprocessor – Spin locks Copyright © 2012 AdaCore Slide: 50
  • 51. Multicores for real-time safety-critical embedded systems • We need to address: – Reliability – Predictability – Analyzability • The Ravenscar profile for monoprocessors is – Deterministic – Time analyzable – Simple to use and implement • Extend the Ravenscar profile model from monoprocessor to multiprocessor – Fully partitioned model – Fixed-priority scheduling – Static model • Allow for – Simple implementation – Verifiable – Schedulability analysis Copyright © 2012 AdaCore Slide: 51
  • 52. Static model • Concurrent entities fixed and static – Tasks and shared memory defined before execution • Static fixed priority scheduling algorithm – Preemptive fixed priority scheduling in each CPU – Analyzable as in Ravenscar for monoprocessors – Dynamic-priority scheduling algorithms could increase CPU utilization but: – Higher complexity – Higher run-time overhead – Lower predictability, lower robustness in case of overload • Partitioned – Each task allocated to an user-defined processor forever – CPU utilization of partitioned scheduling is neither better nor worse than global – It relies on very well known monoprocessor techniques for priority allocation and timing analysis – It is much simpler to implement – No task migration Copyright © 2012 AdaCore Slide: 52
  • 53. Task scheduling • Tasks statically allocated to processors Ravenscar system on monoprocessor – No task migration • Preemptive fixed-priority scheduling 0 1 • Single shared run time 2 – Per-CPU ready queues – Spin-locks to protect shared data 3 – Disabling interrupts is not enough task Cyclic with Priority => 100, • Operations on a different CPU => 3; end Cyclic; processors – Triggering an special interrupt in the target processor Copyright © 2012 AdaCore Slide: 53
  • 54. Task synchronization • Library-level protected objects – Shared data with mutual exclusion – Both for inter- and intra-processor communication • Simple and efficient mutual exclusion changing priority for intra-processor communication – As in Ravenscar monoprocessor – Could be statically detected – Efficiency – Simple timing analysis • Spin-locking for inter-processor synchronization • Awaking tasks from other processors – Inter-processor interrupt facility to modify the ready queues Copyright © 2012 AdaCore Slide: 54
  • 55. Parallel task synchronization • Goal – Effective parallel task synchronization – Set of tasks blocked and released at once • Typical case – A group of tasks must wait until all of them reach a synchronization point – And then be released together to work in parallel • Mimic the POSIX barrier mechanism Copyright © 2012 AdaCore Slide: 55
  • 56. Parallel barrier example package Ada.Synchronous_Barriers is pragma Preelaborate (Synchronous_Barriers); subtype Barrier_Limit is Positive range 1 .. <imp- def>; type Synchronous_Barrier (Release_Threshold : Barrier_Limit) is limited private; procedure Wait_For_Release (The_Barrier : in out Synchronous_Barrier; Notified : out Boolean); Number_Of_Tasks : constant := 8; private Barrier : Synchronous_Barrier (Number_Of_Tasks); -- not specified by the language end Ada.Synchronous_Barriers; task type Worker (Affinity : CPU) with CPU => Affinity; task body Worker is Notified : Boolean; begin loop Wait_For_Release (Barrier, Notified); -- Do something in parallel at the same time Something; if Notified then -- Only one task does this Ask_For_More_Work; end if; end loop; end Worker; Copyright © 2012 AdaCore Slide: 56
  • 57. Memory barriers • Goal – Have control over cache memories • Typical case – Non-blocking algorithms to effectively exploit hardware parallelism – lock-free and wait-free • Problem to solve – How to ensure the correct order of loads and stores with multi-level caches – Modern multicores do not guarantee this ordering between processors – Optimizations that can result in out-of-order execution – Unless special instructions are used • How to do it with Volatile – Until Ada 2005 – They can never be in cache or registers – The Ada 2012 (more realistic) approach – Volatiles can be handled in cache memories, but – Guarantee serial ordering • All tasks of the program (on all processors) that read or update volatile variables see the same order of updates to the variables • May need the use of an appropriate memory barrier to flush the cache Copyright © 2012 AdaCore Slide: 57
  • 58. Example of memory barriers Shared_Data : Integer; pragma Volatile (Shared_Data); • In Ada 83, 95, 2005 Barrier : Boolean := False; pragma Volatile (Barrier); – Shared_Data and Barrier can never be in cache task Producer with CPU => 1; task Consumer with CPU => 2; • In Ada 2012 task body Producer is begin – Shared_Data and Barrier can be in cache … Produce (Shared_Data); Barrier := True; … end Producer; task body Consumer is begin … while not Barrier loop null; end loop; -- If we see that Barrier has been updated, -- we must see the produced value of -- Shared_Data. Use(Shared_Data); … end Consumer; Copyright © 2012 AdaCore Slide: 58
  • 59. Conclusion • There is support in programming environments for high-integrity • Embedded programming can use high-level abstraction constructs • Real-time support – Computational model amenable to timing analysis • Ada has supported execution on parallel architectures since its inception – Ada 2012 dispatching domains – Good flexibility and analyzability – Implementable on top of typical operating systems and kernels – Ravenscar for multiprocessors – Simple extension to Ravenscar on monoprocessors – Partitioning into a set of monoprocessor Ravenscar systems – Keep desired properties found in monoprocessor Ravenscar Copyright © 2012 AdaCore Slide: 59