Embedded Real-Time Systems Memory management Lecturer Department University
Outline Memory types and virtual memory Separate vs common memory spaces Memory management in RTOS
Same code, but what about the data??? Tasks and data Task is not code Two tasks can be executing the same code simultaneously Think of it as an execution thread running through the code Task 1 Task 2 int x; int Func() { int y; y = y + 1; x = x + 1; ... } Same code, but what about the data???
Recall from computer architecture… CPU registers Program counter (address of the currently executing instruction) Data and address registers Main memory. Depending on the usage, usually subdivided into code (read only) data (read/write) stack (function returns + variables) heap (dynamically allocated data) int x; int Func() { int y; int* buf = malloc(…); y = y + 1; x = x + 1; ... } *buf
Stack memory Stack memory – LIFO CPU Function return addresses Local function variables CPU Stack pointer int FuncB() { int Y; ... return; } B allocates memory for Y B is called Y int FuncA() { int X; ... FuncB(); return; } ret.addr. for B X A allocates memory for X ret.addr. for A A is called
More memories… Virtual memory: CPU hardware provides mapping between virtual and physical addresses Virtual memory Physical memory 1000: 2000: 3000: ... 9000: 1000: 2000: 3000: ... 9000: Address translation External storage (hard disk)
Memory spaces Memory mapping defines memory space Memory mapping 1 Having different memory spaces for different tasks can be used to isolate tasks from each other Virtual memory space 1 Virtual memory space 2 Physical memory 1000: 2000: 3000: ... 9000: 1000: 2000: 3000: ... 20000: 1000: 2000: 3000: ... 9000: Memory mapping 1 Memory mapping 2
Memory spaces (contd.) Memory protection can also be used to isolate tasks even when they use the same memory mapping Virtual memory space 1 Virtual memory space 2 Physical memory 1000: 2000: 3000: ... 9000: 1000: 2000: 3000: ... 20000: 1000: 2000: 3000: ... 9000: Memory mapping Memory mapping
Task context Each task has its own CPU environment – context state of the CPU (registers) state of memory management unit (address translation and protection table) – if virtual memory is used Task context saved in a task control block (TCB) OS-specific data: I/O assignments for standard input/output/error stack – where task stores its dynamic variables and function calls ...
Memory management in RTOS RTP 3 Process heap Process code Task ... Real-time processes OS kernel RTP 2 Process heap Process code Task ... RTP 1 Process heap Process code Task ... RTOS kernel environment ... App. code App. code Task ... Task Tasks in each RTP run in a memory space separate from tasks in other RTPs and from kernel tasks RTOS code Kernel heap Board support package
Same vs separate memory spaces Unix/Windows/RTOS RTPs Process 1 mem. space Process 2 mem. space data: data: global vars: x stack: y global vars: x stack: y code: code: int x; int Func() { int y; y = y + 1; x = x + 1; ... } int x; int Func() { int y; y = y + 1; x = x + 1; ... } Task 1 Task 2 PC1 PC2
Same vs separate memory spaces Common mem. space RTOS kernel TCB1: TCB2: stack: y stack: y data: global vars: x What about y? code: int x; int Func() { int y; y = y + 1; x = x + 1; ... } Task 1 Task 2 PC1 PC2
Comparing memory models Inter-task communications Task switching overheads Concurrent code execution Hardware support Software debugging and testing System stability
RTOS vs Windows/Unix Same Different Real time process = Windows/Unix process Task in RTP = Thread in Windows/Unix Different RTOS schedules tasks across RTPs Deterministic real-time behaviour Windows/Unix shares CPU between processes Threads are scheduled within each process separately RTOS does not use address translation and loads whole processes into memory before execution Fast task switching Compatible with CPUs without MMU (no memory protection though!) Deterministic memory access time
Summary Virtual memory and task context Comparison of separate and common memory models Comparison of RTOS and Windows/Unix memory management