Top Banner
ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng
49

ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Jan 15, 2016

Download

Documents

Travon Collis
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 Embedded Systems

Design

Operating System OverviewChapter 7Ning Weng

Page 2: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Operating System

• Abstractions─ Uninterrupted Computation: No Interrupts─ Infinite Memory: just an illusion.─ Simple I/O: Avoid dealing directly with devices (simple writes/reads)

Page 3: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Uninterrupted computation

• Underlying mechanisms Context switching Scheduling Protection

• Flavors of “process” - increasing complexity Interrupt handlers, threads, processes

Page 4: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Infinite memory via virtual memory

• Via virtual memory Page mapping (avoids finding contiguous locations). Demand paging (use more space than memory)

• Slow DRAM lookup avoided with fast TLB• Protection by allowing only OS to modify page tables.

Page 5: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Simple I/O using system calls

• For abstraction alone, I/O could be libraries.• For security, I/O handled by device drivers.• System calls, trap to kernel protection levels.• More expensive function call, because of privilege

escalation.

Page 6: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Four Types of Operating Systems

• Single-user, single task - Designed to manage the computer so that one user can effectively do one thing at a time. Ex: Apple iPhone

• Single-user, multi-tasking - Type of operating system most use on desktop and laptop computers today. Windows 7 and the MacOSX are examples of OS that let a single user have several programs in operation at the same time.

• Multi-user - Allows many users to obtain the computer's resources simultaneously. OS must make sure that each program being used has sufficient and separate resources so that a problem with one user doesn't affect the other users. Ex: Unix

• Real-time operating system (RTOS) - Main task is to manage computer’s resources so a particular operation executes in precisely the same amount of time every time it occurs.

Page 7: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Embedded Operating Systems Characteristics

• Designed to perform a dedicated function

• Real-Time Operating Systems (RTOS)

• Examples: ─ iOS, Android, Blackberry OS ─ VxWorks (Boeing 787 Dreamliner and many

spacecraft)

Page 8: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 8

Service Calls

Ning Weng

Page 9: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 9

Service Call Design Patterns

Ning Weng

Page 10: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 10

Tasks & Threads & Process

Ning Weng

Page 11: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 11

RTOS States Transitions

Ning Weng

Page 12: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 12

Nonpreemptive FIFO Scheduling

Ning Weng

Page 13: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 13

RR Scheduler with Priority & Preemption

Ning Weng

Page 14: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Memory Allocation

• Malloc() and Free()─ Allocates and frees memory from system heap

to an application, respectively• Problem with the allocation system:

─ Forgets sequence of memory and free allocations• Can cause fragmentation (small contiguous

section + lots of free memory)• Solution:

─ Allocator algorithm• Maximize size of contiguous blocks over

time

Page 15: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Fragmentation Example

Page 16: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 16

Fragmented Heap

Ning Weng

Page 17: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 17

Power of two heap

Ning Weng

Page 18: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Virtual Memory• Memory Management Unit (MMU)

manages translation between code, data, and heap process memory to physical memory

• Virtual addresses are unique to accessing process

• Physical addresses are unique to the hardware (i.e. ram)

Page 19: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 19

Address Space Mapping

Ning Weng

Page 20: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Freeing and Swapping Memory• Freeing Memory

─ Memory allocated by a task may not be automatically freed upon deletion of that task• Must keep track of all allocated memory

• Swapping Memory─ Utilized in Linux to allocate more virtual

memory to applications than the total amount of physical memory

─ Rarely used in embedded systems• Additional wear on system (normally based

on solid-state drives)

Page 21: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Clocks and Timers• Synchronous Execution

─ Used to specify timeouts• Sleep() and yield() are also used to delay execution

─ If time is long enough, task is de-scheduled and moved to ready queue

• Asynchronous Execution─ Callback functions – Indicates that a timeout has

occurred to other threads • Callbacks with expiry time less than or equal to

current time count are called• Can release semaphores• Often called because of timer interrupt

– Interrupt – mechanism used to inform CPU that an asynchronous event has occurred

Page 22: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 22

Time of Today• Time source

─ Real time Clock• Hardware timer backup with battery

─ CPU when it is running

• Seed source─ RTC─ NTP (network time protocol)─ Cellular radio network

Ning Weng

Page 23: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 23

Mutual Exclusion/Synchronization• Goal: to serialized atomic access to share

resources

Ning Weng

Page 24: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Difficulties of Concurrency

• Sharing of global resources─ Writing a shared variable: the order of writes is

important─ Incomplete writes a major problem

• Optimally managing the allocation of resources• Difficult to locate programming errors as results

are not deterministic and reproducible.

Page 25: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

A Simple Examplevoid echo(){

chin = getchar();chout = chin;putchar(chout);

}

Page 26: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

A Simple Example: On a Multiprocessor

Process P1 Process P2. .

chin = getchar(); .. chin = getchar();

chout = chin; chout = chin;putchar(chout); .

. putchar(chout);

. .

Page 27: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Competition among Processes for Resources

Three main control problems:• Need for Mutual Exclusion

─ Critical sections

• Deadlock• Starvation

Page 28: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Disabling Interrupts• Uniprocessors only allow interleaving• Interrupt Disabling

─ A process runs until it invokes an operating system service or until it is interrupted

─ Disabling interrupts guarantees mutual exclusion─ Will not work in multiprocessor architecture

Page 29: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Pseudo-Codewhile (true) {

/* disable interrupts */;/* critical section */;/* enable interrupts */;/* remainder */;

}

Page 30: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Special MachineInstructions

• Compare&Swap Instruction ─ also called a “compare and exchange instruction”

• Exchange Instruction

Page 31: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Compare&Swap Instruction

int compare_and_swap (int *word, int testval, int newval)

{int oldval;oldval = *word;if (oldval == testval) *word = newval;return oldval;

}

Page 32: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

Semaphore• Semaphore:

─ An integer value used for signalling among processes. • Only three operations may be performed on a

semaphore, all of which are atomic: ─ initialize, ─ Decrement (semWait)─ increment. (semSignal)

Page 33: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 33

OS Required Semaphore

Ning Weng

Page 34: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 34

Semaphore• Binary

• Couting

Ning Weng

Page 35: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

35

Execution Environment

Memory

b

STANDARD CLIBRARY

MATHLIBRARYAPPLICATION (mpg123)

MemoryManagement

FilesystemsNetworking

ArchitectureDependent

Code

MemoryManager

File SystemDevices

CharacterDevices

NetworkSubsystem

OPERATING SYSTEM

ProcessManagement

DeviceControl

Network InterfacesCPU

Disk

• Program

• Libraries

• Kernel subsystems

• Hardware

Page 36: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 36

Device Driver• The driver controls the hardware and provides an

abstract interface to its capabilities.• The driver ideally imposes no restrictions (or

policy) on how the hardware should be used by applications.

• Scatter gather list:─ a mechanism defined and supported by OS to represent

a list of data is not physically contiguous.

Ning Weng

Page 37: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 37

Service, Driver and Device

Ning Weng

Page 38: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 38

Scatter Gather Structures

Ning Weng

Page 39: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 39

Direct Memory Access (DMA)

• What, why and how/

Ning Weng

Page 40: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 40

Transmit Descriptor Ring

Ning Weng

Page 41: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 41

Network Stack and Device Driver

Ning Weng

Page 42: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 42

Storage File System• present logical (abstract)

view of files and directories─ hide complexity of hardware

devices• facilitate efficient use of

storage devices─ optimize access, e.g., to disk

• support sharing─ provide protection

Ning Weng

Page 43: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 43

Files Open and Read

Ning Weng

Page 44: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 44

Synchronization File System• File write is asynchronous

─ Write call function does not block• Data may not have been in disk• Consolidating and scheduling writing

requests for performance• Synchronization is required before

─ Shut down, restart or disk removal─ Example: Stop a USB disk (safe remove)

• Two challenges of power interactions─ Un-notified power removal

• Large capacitor and sensing circuits─ Brownout

• Voltage dropNing Weng

Page 45: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 45

Brownout Events

Ning Weng

Page 46: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 46

Real Time

Ning Weng

Page 47: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

ECE 424 47

Real Time

• Real time: required to complete its task on time─ Usually have deterministic time bound─ Hard or soft

• Features of RTOS─ Scheduling, resource allocation, interrupt handing and

etc─ Example, VxWorks

Ning Weng

Page 48: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

In a Hard RTOS

• Thread priorities can be set by the client

• Threads always run according to priority

• Kernel must be preemptible or bounded

• Interrupts must be bounded

• No virtual memory

Page 49: ECE 424 Embedded Systems Design Operating System Overview Chapter 7 Ning Weng.

In a Soft RTOS…• Like a hard RTOS:

─ Priority scheduling, with no degradation─ Low dispatch latency─ Preemptible system calls─ No virtual memory (or allow pages to be

locked)

• Linux: guarantees about relative timing of tasks, no guarantees about syscalls