2. Chris Simmonds 2net Ltd. Overview. Linux is a popular choice as an
embedded OS. Most projects evolve from previous projects often based on an
RTOS.
Why Linux is not an RTOS: porting hints
Chris Simmonds 2net Limited Embedded Systems Conference UK. 2009 Copyright © 2009, 2net Limited
Overview
Linux is a popular choice as an embedded OS Most projects evolve from previous projects often based on an RTOS How to get from point A (RTOS) to point B (Linux)?
Is Linux an RTOS??
Chris Simmonds 2net Ltd
2
Porting options
Application
Application
Application
RTOS emulation library (*)
RTOS
Linux
Linux
Virtual machine monitor
Application
Linux
(*) for example v2lin [1] or Mapusoft Chris Simmonds 2net Ltd
3
The porting dilemma
How much existing code can I keep? How much effort is required to port my application to Linux?
What should I look out for?
What are the gains?
Chris Simmonds 2net Ltd
4
Why Linux is not an RTOS
Applications run in “user space”
All hardware interaction is in “kernel space”
All i/o via files and sockets
Applications are processes
Default scheduling policy is time shared
POSIX API
Is Linux real-time?
Chris Simmonds 2net Ltd
5
Different memory models RTOS: all one memory space
Linux memory spaces
Application
Hardware Chris Simmonds 2net Ltd
Linux
Kernel space
RTOS
C library
User space
Application
Hardware 6
Device drivers
Linux
Device driver
do_IRQ()
ISR
Hardware
Chris Simmonds 2net Ltd
7
Why Linux is not an RTOS
Applications run in “user space”
All hardware interaction is in “kernel space”
All i/o via files and sockets
Applications are processes
Default scheduling policy is time shared
POSIX API
Is Linux real-time?
Chris Simmonds 2net Ltd
8
Everything is a file For example, to read characters from first serial port, open device node (file) /dev/ttyS0 f = open (“/dev/ttyS0”, O_RDWR); l = read (f, buf, len);
/dev/ttyS0
Major number 4 Minor number 64
Linux
UART driver 4:64
Chris Simmonds 2net Ltd
9
My device doesn't look like a file!
File read & write operations work well with byte streams - such as a serial port How about a robot arm? The ioctl function allows any interaction you want struct robot_control_block rc; f = open (“/dev/robot”, O_RDWR); ... ioctl (f, SET_ROBOT_PARAMTERS, &rc); ...
Chris Simmonds 2net Ltd
10
Hint 1
Identify all code that accesses hardware directly
Design a file-based interface
Use ioctl for things that do not naturally fit the file concept
Make this into a device driver Remember: keep device drivers as simple as possible
All the complicated stuff should be in the application
Chris Simmonds 2net Ltd
11
Cheating: user space drivers
mmap allows an application direct access to device memory
But, cannot handle interrupts
No control of CPU cache or instruction queue
Not the “Linux way”
The Linux User IO subsystem uses mmap to provide a flexible framework for user space drivers
UIO [2]
Chris Simmonds 2net Ltd
12
mmap example #include #define IO_PHYS_ADDRESS 0x90600000 #define ARM_POS 0x0034 #define ARM_MOTION 0x0038 int main (int argc, char *argv[]) { unsigned led_dat; int mh; char *ptr; mh = open ("/dev/mem", O_RDWR); ptr = mmap (0, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, mh, IO_PHYS_ADDRESS); ... *(unsigned int*)(ptr + ARM_POS) = new_pos; while (*(unsigned int*)(ptr + ARM_MOTION) != 0) sleep (1); ... Chris Simmonds 2net Ltd
13
Why Linux is not an RTOS
Applications run in “user space”
All hardware interaction is in “kernel space”
All i/o via files and sockets
Applications are processes
Default scheduling policy is time shared
POSIX API
Is Linux real-time?
Chris Simmonds 2net Ltd
14
Processes PID 1 /bin/init
PID 91 /bin/httpd
PID 103 /bin/controler T1 T2
C library
T3
Process = address space + program + thread of execution Some process have > 1 thread
Linux
Chris Simmonds 2net Ltd
15
Pros and cons of processes
Pro
Protected memory space
Resources (memory, open files) released when exit
Easy to re-start a failed process
Con
Communication between processes quite slow & cumbersome
Chris Simmonds 2net Ltd
16
Pros and cons of threads
Pro
Easy communication using shared variables, mutexes and condition variables Similar memory model to RTOS tasks
Con
No memory protection between threads in the same process
Chris Simmonds 2net Ltd
17
Why Linux is not an RTOS
Applications run in “user space”
All hardware interaction is in “kernel space”
All i/o via files and sockets
Applications are processes
Default scheduling policy is time shared
POSIX API
Is Linux real-time?
Chris Simmonds 2net Ltd
18
Scheduling policies
SCHED_OTHER
Time share: fairness. Priority set by scheduler
SCHED_FIFO
Fixed priority (1 to 99); preempts SCHED_OTHER
Use this for real-time activities
SCHED_RR
As SCHED_FIFO but tasks of same priority time-slice
Default quantum is 100 ms
Chris Simmonds 2net Ltd
19
Hint 2
Make closely-coupled groups of RTOS tasks into POSIX threads within a process Separate RTOS tasks with little interaction into separate processes Make real time threads SCHED_FIFO
Use Rate Monotonic Analysis or similar to choose priorities [3]
All non real-time threads should be SCHED_OTHER
Chris Simmonds 2net Ltd
20
Why Linux is not an RTOS
Applications run in “user space”
All hardware interaction is in “kernel space”
All i/o via files and sockets
Applications are processes
Default scheduling policy is time shared
POSIX API
Is Linux real-time?
Chris Simmonds 2net Ltd
21
POSIX
POrtable Operating System Interface
IEEE standard 1003.1
Most RTOS functions map to POSIX one-to-one
Tasks to POSIX threads Semaphores and mutexes to POSIX semaphores, mutexes and condition variables
Message queues to POSIX message queues
Watchdog timers to POSIX clocks and timers
Chris Simmonds 2net Ltd
22
Look out for
POSIX Threads
Threads run immediately they are created
Not possible to terminate an arbitrary thread
POSIX semaphores and mutexes
POSIX has many types of mutex, including priority inheritance. See [4] POSIX does have semaphores but they are not much used. See [5] for a discussion on mutexes vs semaphores
Chris Simmonds 2net Ltd
23
Library and kernel versions
For full POSIX compliance in Linux you need current versions of Linux and the C library
Kernel >= 2.6.22
GNU C library >= 2.5
Beware uClibc [6]
Small, “embeddable” C library
Good for small systems with