Thursday, October 9, 2008

Virtual Memory Layout for Linux

There was a time when I struggled to understand the virtual memory layout in Linux , details of Physical-to-virtual memory mapping ,paging etc. Unfortuantely there is not a single book available which explains these things with non-x86 Architecture in mind. 

Here I will try to explain a few of these things.


Thursday, October 2, 2008

MMU-Less Systems

Important terms : PIC [ Position Independent Code]
MMU [ Memory Management Unit]

PIC : Position Independent Code
[*] execution is independent of the memory in which the code resides

[*] Used in Shared libraries and on systems without an MMU support [ and that is the reason we are discussing this here]

A MMU-less system has a single address space.
In a system with MMU support each program executes in its own virtual address space. MMU less system doesn not have this distinction of virtual address space and physical memory. Therefore all programs must execute in a single address space and should not depend on the address in memory where it resides. A single buggy program can also cause the whole system to go down!

Inspite of this obvious disadvantage MMU-less sytems ( a good example is uCLinux )are becoming more and more popular again due to the following advantages :-

[1]: MMU-Less cores are smaller

[2]: Cache has to be flushed after every context switch in a system with virtual memory which is not required for MMU-Less systems. This results in faster context switches. Most of the processors have virtually indexed cache. This would require the OS to flush the cache (and TLB ) whenever a context switch happens.However for a MMU-Less system contents of Cache and TLB are valid even after a context switch because the same address space is shared by all processes. [ Reference : Context Switching and IPC Performance Comparison between
uClinux and Linux on the ARM9 based Processor by Hyok-Sung Choi and Hee-Chul Yun]

[3]: with uCLibc the binaries are much smaller. [hence the name "micro" ]

fork( ) issue 

fork creates a new process ( child ) which is a exact copy process but with a different PID. The parent and child execute simultaneously. MMU maps the memory from the parent process to the child process and does a COW (Copy-on-Write)  to the child process.

This however is not possible in MMU-Less systems like uCLinux.

All is not lost as vfork ( ) comes to the rescue here. vfork halts the the parent process and allows the child process to execute. posix threads can also be used instead of fork as the they share the same memory space ( including the stack)