Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between flat memory model and protected memory model?

Difference between flat memory model and protected memory model? VxWorks supports flat memory model, Does Linux also supports flat memory model?

like image 494
Ritesh Avatar asked Aug 24 '13 11:08

Ritesh


1 Answers

In order to give an answer that makes sense, let's review some concepts first.

Most modern processors have a Memory Management Unit (MMU) which is used for a number of purpose.

One purpose is to map between the Virtual Address (the one the CPU "sees") and the Physical Address (where the chips are actually connected). This is called address translation.

Another purpose is to set access attributes for certain virtual memory locations (things like memory is Read-Write, or Read-Only, or not accessible)

With an MMU, you can have what is called "unity mapping" where the processor's virtual address is the same as the physical address (i.e. you don't use address translation). For example, if the processor accesses 0x10000, then it is accessing the physical location 0x10000.

A "flat" memory model typically refers to the fact that any virtual address the CPU accesses is unique. Thus, for a 32-bit CPU, you are limited to a maximum of 4G of address space.

It is most often (though not necessarily) used to refer to a unity mapping between virtual and physical memory.

In contrast, in the workstation world, most Operating Systems (Linux/Windows) use an "overlapped" memory model. For example, any program you launch (a Process) in Windows, will have a start address of 0x10000.

How can windows have 10 processes all running from address 0x10000?

It's because each processes uses the MMU to map the virtual address 0x10000 to different physical addresses. To P1 could have 0x10000 = 0x10000 while P2 has 0x10000 = 0x40000, etc...

In RAM, the programs are at different physical addresses, but the CPU Virtual address space looks the same for each process.

As far as I know, Windows and Standard Linux always use an overlapped model (i.e. they don't have a flat model). It is possible that uLinux or other special kernel could have a flat model.

Now, protection has nothing to do with a flat vs. protected model. I would say that most overlapped model OSes will use protection so that one process does not affect (i.e. write into the memory of) another one.

With VxWorks 6.x and the introduction of Real-Time Processes, even with a flat memory model, individual RTPs are protected from each other (and kernel apps) by using protection.

If you don't use RTPs and run everything in the vxWorks kernel, then there is no protection used.


So, how does protection work (whether in VxWorks RTPs or other OSes Process)? Essentially, the RTP/Process exists inside a "memory bubble" with a certain range of (virtual) addresses that contains code, data, heap, and other assorted memory location.

If the RTP/Process attempts to access a memory location outside it's bubble, the MMU generates an exception and the OS (or signal handler) gets called. The typical result is a segment violation/bus exception.

But how can a process send a packet to an ethernet port if it can't escape it's memory bubble? This varies by processor architectures, but essentially, the user-side (RTP) socket library (for example) makes a "system call" - which is a special instruction which switches the cpu to the kernel space and supervisor mode. At this point, some sort of device driver (that typically reside in the kernel) runs to push the data to some hardware device. Once that's done, the system call returns and we're back in the RTP/process space running the user code.

The OS takes care of all the MMU programming, system call handling, etc... This is invisible to the application.

like image 187
Benoit Avatar answered Sep 30 '22 15:09

Benoit