21 BSW Os EN
21 BSW Os EN
- Operating system
Os
Chapter overview
• Introduction
• Scheduling
• Multi-core Support
• Critical Section Protection
• OS-Application Protection
• Hints and Tips
• EB tresos OS products
Stack overview
Application
Application SW-C 1 Application SW-C 2 Application SW-C 3 Layer
Application
Runtime Environment (RTE) Abstraction
Layer
Management
Diagnosis
and Error
Handling
IO HW Abstraction
Mode
Service
Operating System
Layer
Complex Drivers
Communication
Security Stack
Memory Stack
stack
ECU
Abstraction
Layer
Drivers
Microcontroller
I/O
Abstraction
Layer
Main characteristics
Tasks
Task state machine:
terminate
wait
• Typically, most parts of the application are executed
within a Task
preempt
start
waiting suspended
• Tasks must be activated to take part in the scheduling
This can be done via API calls or other Os mechanisms
release
• Tasks have a configured priority activate
Tasks: Example
DeclareTask(TaskA);
TASK(TaskA)
{
StatusType status;
TaskStateType stateB;
/* do stuff */
status = ActivateTask(TaskB);
/* check return value */
status = GetTaskState(TaskB, &stateB);
/* check return value and do more stuff */
TerminateTask();
}
© Elektrobit (EB) 2022 | Confidential 9
Os - Scheduling
Interlude: Events
Event
Basic
Events are just notifications. They neither contain or Extended
any data nor any additional information like the Extended Task
source of the event Task
The AUTOSAR Os schedules the Tasks according to Each Task can be configured in one of the two ways
the following properties: • Non-preemptive
• Among all Tasks that are ready to run, the one - Such tasks will release the processor voluntarily
with the highest priority is taken
• Preemptive
• If there are several Tasks with the same highest
- Such tasks are preempted when a different task
priority that are ready to run, the oldest one is
of higher priority is activated
taken
Scheduling: Example
TaskB
terminate TaskB
terminate TaskA
full-preemptive
preempt
start
TASK(TaskA) waiting suspended
{
StatusType status; release activate
TaskStateType stateB;
extended Tasks ready
only
/* do stuff */
status = ActivateTask(TaskB);
/* check return value */
status = GetTaskState(TaskB, &stateB);
What is the value of stateB
/* check return value and do more stuff */ after GetTaskState()?
TerminateTask();
}
Stack 1
Stack 2
• Preempted tasks are resumed at the point, where they have
been preempted
Alarm: A Mechanism to trigger an action (e.g. a task activation), when the attached Counter reaches a given value
ScheduleTable Duration
SetEvent
• If the Counter reaches the value for an expiry point, the
ActivateTask
ActivateTask
ActivateTask
SetEvent
corresponding action is triggered
• Several expiry point actions are possible, e.g. activating
a Task or setting an Event
• ScheduleTable/Alarm modes: one-shot and periodic
• ScheduleTables can be synchronized to external sources
Summary
• Tasks are C functions, that can be scheduled concurrently by the AUTOSAR Os
• There are basic Tasks and extended Tasks. Extended Tasks support waiting for Events
• Task scheduling is based on the priority. For Tasks with the same priority, the Task activation time decides
which task shall be scheduled next
• ISRs interrupt the execution of Tasks (unless interrupts are explicitly disabled within the running Task)
OS-Applications
Task A Task E
OS-Application Task B Task F
• Group of AUTOSAR Os objects (Tasks, Counters, ISRs, …), ISR 1 ISR 3
that semantically belong together
ISR 2 ISR 4
OS-Application 2 ISR 5
Application to Core Assignment
Counter 2
• Os objects are assigned to processor cores via their OS- Task C
Application ScheduleTable 2
Task D
• The assignment is static: all objects of an OS-Application Counter 1
ScheduleTable 3
will always run on the core, to which the OS-Application
is assigned ScheduleTable 1
Cross-core Requests
Core 1 Core 2
• Os API calls are automatically forwarded to the
correct core Task A
Call to
ActivateTask()
an event
Startup
Single-core startup: StartOS() Core 1
(Master Core) Core 2
Summary
• AUTOSAR Os objects are assigned to the different processor cores via OS-Applications
• APIs will wait for the result from the other core
Overview
Critical Section
Read / Write
Read / Write Memory M
The AUTOSAR Os provides three protection mechanisms:
• Resources
• Interrupt Locks (several variants) Task A Enter Critical Section
Exit Critical Section
• Spinlocks
Resources
• Os mechanism to protect critical sections on a single core
• Critical sections can be protected against preemption by other Tasks and (category 2) ISRs
• Resources must be configured for each Task and (category 2) ISR that uses the shared resource
Interrupt Locks
• APIs to lock interrupts Interrupt locks and Resources ensure that no other
• Different variants are available: Task/ISR that uses the same shared resource is
– Category 2 ISRs only vs. all ISRs1 scheduled while a critical section is executed.2
– Nestable vs. non-nestable APIs
• Only affects the processor core which executes the APIs 1 There might be some ISRs which are not locked. Check the documentation
2 If an appropriate mechanism is chosen and configured and used correctly.
Spinlocks GetSpinlock(s)
• The only AUTOSAR Os multi-core protection mechanism {
• Active waiting in a loop („spin“), while repeatedly do {
checking if the lock is available try to acquire spinlock s;
• No protection against core local interruptions } while (spinlock s was occupied);
}
NOTE
• Combine Spinlocks with an Interrupt Lock or ReleaseSpinlock(s)
RES_SCHEDULER1 to protect a section against {
interruptions on the local core2 release spinlock s;
• Spinlocks can easily result in deadlocks when being used }
incorrectly. Make sure to use them decently!
1 RES_SCHEDULER is a Resource which blocks all Task scheduling.
2 Directly supported in the configuration since AUTOSAR 4.1.
Summary
Mechanism Advantages Disadvantages
Resources • No interrupt locking as long as no ISRs are • Single-core only
involved • Typically slower than Interrupt Locks1
• Fine grained control via the configuration • No protection against category 1 ISRs
• Misconfiguration possible
Interrupt Locks • Typically faster than Resources1 • Single-core only
• Simple to understand and use • Lock all interrupts of the corresponding type2
Spinlocks • Multi-core protection mechanism • No protection against core-local interruption
• Efficient for small critical sections (unless combined with other locks)
• Can be combined with an Interrupt Lock or • Active waiting, therefore inefficient in case of
RES_SCHEDULER3 long critical sections
• Deadlocks easily possible
1 Depends on the actual implementation.
2 There might be exceptions.
3 Since AUTOSAR 4.1, if supported.
Processors typically provide different modes that control the access to certain resources, e.g. to critical special purpose
registers, certain assembler instructions or some peripheral registers.
Trusted Function
• Trusted Functions are provided by Trusted OS-Applications and executed in the privileged processor mode1
• Trusted Functions can be provided to Non-Trusted OS-Applications, to allow them access to restricted resources
• Trusted Functions cannot be provided to OS-Applications on different processor cores
1 The used processor modes as well as the associated restrictions are hardware- and implementation-specific.
2 Since AUTOSAR 4.2, memory protection can be activated for Trusted OS-Applications.
© Elektrobit (EB) 2022 | Confidential 36
Os – OS-Application Protection
Interlude: Hooks
PostTaskHook
• ProtectionHook: Typically* called for errors, which
must be handled before the Task/ISR can continue.
Example: Memory protection exception
Memory Protection
Memory
• Prevents Tasks, ISRs, Hooks as well as the Os itself from Task A Read/Write
…
accessing memory, that should not be accessible Data Task A (RAM)
OS-Application Data
(RAM)
• Hardware support (MPU or MMU) is necessary
Data Task B (RAM)
Task B Stack Task B (RAM)
• The ProtectionHook is called in case of a violation
Stack Task A (RAM)
Stack Os Kernel
• The configuration is hardware and implementation (RAM)
Timing Protection
• Task and ISR time budget monitoring, as well as some other variants
• The ProtectionHook* is called in case of such a violation
* If enabled in the configuration.
Summary
• Non-Trusted OS-Applications have restricted access rights. Use them whenever possible
• The ErrorHook and the ProtectionHook are your friends - enable them
• The Os provides further protection mechanisms with the service- and the timing protection
Hooks
PreIsrHook
PostIsrHook
• If Hooks are enabled in the configuration, they PreTaskHook
must be defined by the user with the predefined
names and prototypes ErrorHook
Task
ProtectionHook
• Inside Hooks, not all Os API functions are available
PostTaskHook
Debugging
General Tips
• Enable extended error checks and check API return values
• Enable the ErrorHook and check that it is never called on your ECU
• Enable the ProtectionHook and implement it decently
• If „strange“ things happen, make sure that this is not caused by too small stack sizes
Additional Help
• The AUTOSAR Os supports the generation of an ORTI file. If your debugger supports ORTI, use that file to
get easier access to Os internal information like task control blocks or stacks
• Check your documentation for extended debugging support like e.g. stack checking support
• EB tresos AutoCore OS
• EB tresos Safety OS
EB tresos AutoCore OS
Goal:
• AUTOSAR compatible Os (v4.0.3; R19-11 is in preparation)
• Usable in safety projects up to level ASIL-D
Safety requirements:
• Provide spatial freedom from interference
• Support temporal freedom from interference
• Safe start-up: Entry point is MK_Entry2(). With this function EB tresos Safety OS takes
control of the CPU and ensures a safe execution environment
• Safe API usage: EB tresos Safety OS provides defined behavior for wrong or undefined
API/feature usage (e.g. wrong calling contexts or missing TerminateTask() call)
• Simple Schedule Tables: A special form of a schedule table that can use a
(EB tresos Safety OS specific) hardware ticker timer which increments the
schedule table counter with a constant period
• Does not support all AUTOSAR features. See the deviations provided in the EB tresos Safety
OS user’s guide for more information
Summary
• Introduction
• Scheduling
• Multi-core Support
• Critical Section Protection
• OS-Application Protection
• Hints and Tips
• EB tresos OS products
[email protected]
www.elektrobit.com