Why is virtual memory necessary in embedded systems?

In my understanding, virtual memory looks like this:

Programs / applications / executables are located on the storage device. Access to storage devices is much slower than RAM. Consequently, programs are copied from memory to main memory for execution. Since computers have limited main memory (RAM) when all RAM is used (for example, if there are many programs open at the same time or if one very large program is used), a computer with virtual memory turned on will replace the data with the HDD and back into the memory as necessary, thereby increasing the total system memory.

As far as I know, most built-in devices do not have disk memory (for example, smartphones or car infotainment systems). Code is executed directly from flash memory. RAM is mainly used as a notepad area (local variables, return address, etc.).

So why do we need virtual memory in embedded systems? (e.g. WinCE and QNX support virtual memory)

+7
source share
5 answers

Your understanding is completely wrong. You are misleading virtual memory with sharing or page files. There are systems that have virtual memory, as well as swap files or pages, and there are systems that share without virtual memory.

Virtual memory simply means that the process has a kind of memory that is different from physical mapping. Among other things, it allows processes to have their own virtual address space.

Access to storage devices is much slower than RAM. Consequently, programs are copied from memory to main memory for execution. Since computers have limited main memory (RAM) when all RAM is used (for example, if there are many programs open at the same time or if one very large program is used), a computer with virtual memory turned on will replace the data with the HDD and back into the memory as necessary, thereby increasing the total system memory.

This is a replacement (or swap). It has nothing to do with virtual memory, with the exception that most modern operating systems implement file sharing using virtual memory. In fact, virtual memory existed swap.

I think you are probably mistaken about these devices that run code directly from flash memory. The flash read speed is pretty low, and the RAM is very cheap. My bet is that most of the systems you mentioned do not start the code directly from the flash and instead use virtual memory for the fault code in RAM as needed.

+14
source

The term itself has a wide range of applications. you could call a small microcontroller with flash program space measured in kilobytes or less, and a plunger measured in bits or bytes (not enough to be kbytes) as an embedded system. Similarly, tivo works with a full-blown operating system on a rather multi-module computer motherboard (replace tivo with xbox as another example) as an embedded system. Therefore, you need to be less vague about your question. virtual memory has little to do with the fact that its applications cross these boundaries.

There are many answers above, David S has the best, of course, that virtual memory simply means that the memory address on one side of the virtual memory boundary is different from the physical address that is used on the other side of this boundary. Where, how, why, etc. The border is changing.

A popular use for virtual memory, and I can argue that the main use case is for operating systems. One of the advantages is that, for example, all applications can be compiled for the same address space, all applications can be compiled so that, from the point of view of programs, they all start with address 0x8000, and how much this program was launched and turned to the memory to which he refers based on this address. The combination of hardware and operating system changes the virtual address that the program uses for the physical address. If the operating system allows multitasking, then each task may think that they are in the same address space, but the physical addresses are different for each of these tasks. I will not dwell on why using the proposed fixed address space is an advantage. Another aspect of using operating systems is memory management. However, many MMUs will allow you to segment memory. If the user wants to allocate 100 megabytes of memory, the program can access in its virtual address space equal to 100 megabytes, as if it were linear, and in this address space it is linear, but 100 megabytes can be divided into 4 thousand bytes, which scattered throughout the physical address space, are not always likely, but it is certainly technically possible that no two parts of this physical memory are located next to any other piece of 100 megabytes. your memory management does not have to try to keep the large physical chunks of memory available for distribution applications. Note that not all MMUs are exactly the same, and only 4 KB is an example. The third major benefit of virtual address space for the operating system is security. If an application is tied to a virtual address space, it is often easy to prevent the application from touching the memory of any other application or operating system. the application in this case will work / run at the verification level, so that all calls are considered virtual and must go through the translation to the physical, tables that are used to determine that the virtual to physical can contain security flags. If an application accesses a memory address in its virtual space to which it does not have access to business, the hardware may interfere with this and allow the operating system to take measures regarding how to handle it (virtualize some equipment, display an error message and kill the application, cause a warning, and not kill the application, but at the same time load the dummy application data for their transactions, etc.).

There are many ways this can be used in an embedded system. firstly, many embedded systems run operating systems, so all of the above, the simplicity of compiling a program for the address space, the relative ease of managing memory and protecting other applications and the operating system are other advantages that were not mentioned. (one of which is virtualization, the ability to enable / disable instruction / data caching based on blocks by blocks is another)

The bottom line is that David S. pointed out. Virtual memory simply means that the virtual address is not necessarily equal to the physical address, it can be, but not necessarily, some boundary, some hardware, usually managed by a table, which translates the virtual address to the physical address. There are many reasons why you would like to do this, because some embedded systems are indistinguishable from non-embedded systems, any reason that applies to a non-embedded system can be applied to an embedded system.

As far as people want you to think that a system has a flat address space, this is often an illusion. In a microcontroller, for example, you can have several flash banks and one or more storage banks. Each of these banks has a physical, usually zero, address. Even if there is no mmu or something similar, there is a place somewhere between the address bus of the processor and the address bus on the flash memory or RAM memory, which decodes the address on the processor and uses this to access a specific memory bank, Often the lower bits and the upper bits are responsible for choosing the bank (this often happens with mmu), so in this sense, the processor lives in a virtual address space. (not limited to microcontrollers, processors with bus addresses are usually processed). When using microcontrollers, depending on which pin is pulled high or low or some other mechanism, you may have a chip function that allows you to use one flash bank to load a processor or another. You can snap the input pin high, and the processors built into the bootloader allow you to access and debug the system, for example, reprogram the flash application. Or perhaps tie this line to a minimum and load the flash application, not the debugger / boot flash drive. some chips become even more complex, allowing you to load a single flash, then the program writes a register somewhere instantly changes the memory architecture, moving things around, for example, allows you to use ram for the interrupt vector table so that your application can change after loading, the vector table in a flash, which is not so easy to change at will.

Now, when you talk about virtual memory, like exchanging to and from disk, this is a trick that operating systems often use to create the illusion of having more bars. I mentioned above about the virtualization category. virtual memory in the sense that it is actually missing, I have X bytes, but let the software think that there are Y bytes (where Y is greater than X). The operating system, through virtual tables used by the hardware, controls which pieces of memory are tied to the physical board and is allowed to fill as provided by the equipment, or marked as inaccessible in any way, raising an exception for the operating system; during verification, the operating system determines that this is a valid address for this application, but the data behind this address has been replaced with a disk. Then, the operating system detects through another algorithm another piece of ram belonging to whom (part of the algorithm), and copies this fragment of ram to disk, marks the table associated with this virtual one as physical, and then copies the desired fragment from disk to ram, notes that chunk is valid and allows the machine to complete a memory cycle.
Nothing but to say how vmware or other virtual machines work. You can execute instructions initially on hardware using virtual memory as long as you raise an exception, the virtual machine might think that you have a xyz network interface and might have a driver that accesses the register in that xyz network interface, but the reality is you do not have xyz equipment and / or you do not want virtual machine applications to access this equipment, so you virtualized it, you delay access to the register and use software that Ichiro hardware that you forge, and provides virtual machine program continues. This is obviously not the only way to make virtual machines, but it is one way, if the hardware supports it, to allow the virtual machine to work very fast as a percentage of the time that the instructions on the hardware actually execute. The slowest way to virtualize, of course, is to virtualize everything, including the processor, each command in this case will be modeled, it is rather slow, but it has its own functions (system virtualization on x86 or x86 on hand, xyz on abc, fill in the blanks). And if this is the type of virtual memory that you are talking about in the embedded system, it’s good if the embedded system is for the most part indistinguishable from a non-embedded system (for example, xbox or tivo), then for the same reasons you can allow this. If you were on a microcontroller, then most likely the use cases would usually mean if you need more memory, you would buy a larger microcontroller or add more memory to the system or change the needs of the application, so it does not need so much memory, maybe there may be exceptions, but it mainly depends on your application and requirements, a common goal or a common goal, such as a system that allows applications or their data to be more than the available RAM, will require a kind of solution. the microcontroller in your keyless keychain or in your remote control for your TV or clock device or does not require any “applications” to require more resources than physically there.

+4
source

A more important advantage of using virtual memory is that each process gets its own address space, which is isolated from every other process. Thus, virtual memory helps contain flaws and improves security and stability. I should note that it is still possible for two processes to share a bit of memory to facilitate communication (shared mem IPC).

You can also do other tricks, such as saving memory by matching shared parts with multiple addresses (using libc to use as built-in use), but only once in the physical mem. In addition, it gives speed acceleration, you can even improve it, as Linux makes fork/clone cheaper by only copying the kernel descriptors and leaving the image in memory only until the first write access is made with a similar idea.

As the last advantage in modern systems, file input / output is usually used by mapping the file to the process space (for example, mmap ).

+2
source

It is interesting to note that you can get some of the benefits of "virtual memory" without requiring a full MMU. Hardware requirements can sometimes be surprisingly light. The PIC 16C505 has a 5-bit address space and 40 bytes of RAM; addresses from 0x10 to 0x1F can be mapped to either of two groups of 16 bytes of RAM. When writing an application that needed to manage two different data streams, I arranged so that all the variables associated with one data stream were in the first group of 16 "switched" memory cells, and those that were associated with the other would be in the corresponding addresses in the second group. Then I could use the same code to control both data streams. Just set the bank bit in one direction, call the procedure, set it in another way, and call the procedure again.

+1
source

One of the reasons for the existence of virtual memory is that your device can multitask. It can also act as your RAM, thereby relieving the load from your physical memory and changing the load back and forth.

-2
source

All Articles