Escolar Documentos
Profissional Documentos
Cultura Documentos
YZUCSE SYSLAB
Background
Program must be brought into memory and placed within a process for it to be run. Binding of Instructions and Data to Memory
Compile time: If memory location known a priori, absolute code can be generated; must recompile code if starting location changes. Load time: Must generate relocatable code if memory location is not known at compile time. Execution time: Binding delayed until run time if the process can be moved during its execution from one memory segment to another. Need hardware support for address maps (e.g., base and limit registers)
Logical address generated by the CPU; also referred to as virtual address. Physical address address seen by the memory unit.
Logical and physical addresses are the same in compile-time and load-time address-binding schemes; logical (virtual) and physical addresses differ in execution-time address-binding scheme.
Contiguous Allocation
Main memory usually into two partitions:
Resident operating system, usually held in low memory with interrupt vector. User processes then held in high memory.
Single-partition allocation
Relocation-register scheme used to protect user processes from each other, and from changing operatingsystem code and data. Relocation register contains value of smallest physical address; limit register contains range of logical addresses each logical address must be less than the limit register.
YZUCSE SYSLAB czyang@acm.org
Multiple-partition allocation
OS process 5
OS process 5
OS process 5 process 9
process 2
First-fit and best-fit better than worst-fit in terms of speed and storage utilization.
Fragmentation
External Fragmentation total memory space exists to satisfy a request, but it is not contiguous. Internal Fragmentation allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used. Reduce external fragmentation by compaction
Shuffle memory contents to place all free memory together in one large block. Compaction is possible only if relocation is dynamic, and is done at execution time. I/O problem
Latch job in memory while it is involved in I/O. Do I/O only into OS buffers.
CS512 Embedded RTOS
10
Paging
Logical address space of a process can be noncontiguous; process is allocated physical memory whenever the latter is available. Divide physical memory into fixed-sized blocks called frames (size is power of 2, between 512 bytes and 8192 bytes). Divide logical memory into blocks of same size called pages. Keep track of all free frames. To run a program of size n pages, need to find n free frames and load program. Set up a page table to translate logical to physical addresses. Internal fragmentation.
YZUCSE SYSLAB czyang@acm.org
11
Page offset (d) combined with base address to define the physical memory address that is sent to the memory unit.
12
13
Paging Example
14
Free Frames
Before allocation
YZUCSE SYSLAB czyang@acm.org
After allocation
CS512 Embedded RTOS 15
16
Memory Protection
Memory protection implemented by associating protection bit with each frame. Valid-invalid bit attached to each entry in the page table:
valid indicates that the associated page is in the process logical address space, and is thus a legal page. invalid indicates that the page is not in the process logical address space.
YZUCSE SYSLAB czyang@acm.org
17
18
Shared Pages
Shared code
One copy of read-only (reentrant) code shared among processes (i.e., text editors, compilers, window systems). Shared code must appear in same location in the logical address space of all processes.
Each process keeps a separate copy of the code and data. The pages for the private code and data can appear anywhere in the logical address space.
CS512 Embedded RTOS 19
20
YZUCSE SYSLAB
22
Common Requirements
Regardless of the type of embedded system, the requirements placed on a memory management system
Minimal fragmentation Minimal management overhead Deterministic allocation time
23
24
25
the process of erasing the old data and writing the new varies from one manufacturer to another and is usually rather complicated.
it is usually best to add a layer of software to make the Flash memory easier to use. If implemented, this hardwarespecific layer of software is usually called the Flash driver.
26
Flash Drivers
Because it can be difficult to write data to the Flash device, it often makes sense to create a Flash driver. The purpose of the Flash driver is to hide the details of a specific chip from the rest of the software. This driver should present a simple application programming interface (API) consisting of the erase and write operations. Because the Flash memory provides nonvolatile storage that is also rewriteable, it can be thought of as similar to any other secondary storage system, such as a hard drive.
In the filesystem case, the functions provided by the driver would be more file-oriented. Standard filesystem functions like open, close, read, and write provide a good starting point for the driver's programming interface. The underlying filesystem structure can be as simple or complex as your system requires. A well-understood format like the File Allocation Table (FAT) structure used by DOS is good enough for most embedded projects.
27
YZUCSE SYSLAB
heap
29
30
Typical Implementation
The heap is broken into small, fixed-size blocks. Each block has a unit size that is power of two to ease translating a requested size into the corresponding required number of units The dynamic memory allocation function, malloc, has an input parameter that specifies the size of the allocation request in bytes malloc allocates a larger block, which is made up of one or more of the smaller, fixed-size blocks. The size of this larger memory block is at least as large as the requested size; it is the closest to the multiple of the unit size.
YZUCSE SYSLAB czyang@acm.org
31
32
33
34
35
36
37
38
39
40
41
YZUCSE SYSLAB
43
44
YZUCSE SYSLAB
46
47
Blocking Allocation/Deallocation
Pseudo code for memory allocation
Acquire(Counting_Semaphore) Lock(mutex) Retrieve the memory block from the pool Unlock(mutex)
48
YZUCSE SYSLAB
50
mtrace
A feature of the GNU C library Implemented as a function call, mtrace() Detection of memory leaks caused by unbalanced malloc/free calls Creates a log file of addresses malloc'd and freed
51
Electric Fence
It is a library that replaces the C librarys memory allocation functions, such as malloc(), and free(), with equivalent functions that implement limit testing Very effective at detecting out-of-bounds memory reference
52
dmalloc
A library Designed as a drop-in substitute for malloc, realloc, calloc, free and other memory management functions Keep track debug information about your pointer Verify the debug information and logged on errors.
53
References
Qing Li and Caroline Yao, Real-Time Concepts for Embedded Systems, CMP Books, ISBN: 1-57820-124-1, 2003. Anthony J. Massa, Embedded Software Development With eCos, Prentice Hall, ISBN: 0-13-035473-2, 2003. Michael Barr, Programming Embedded Systems in C and C ++, O'Reilly & Associates, January 1999. Karim Yaghmour, Building Embedded Linux Systems, O'Reilly & Associates, ISBN: 0-596-00222-X, 2003. Jean J. Labrosse, "MicroC OS II: The Real Time Kernel," ISBN: 1578201039, CMP Books, June 15, 2002. Silberschatz, Galvin, and Gagne, Operating System Concepts, John Wiley, 2002.
54