Monday, November 7, 2011

x86 Memory Models

Flat Memory Model
The memory is abstracted as a list of consecutive bytes accessed via a linear address.  This resemble to the physical address but not necessary when the linear address is used in the translation process under a memory protection model

Segmented Memory Model
Memory is organized in term of distinct region called segment.  A byte in segment is referenced by a logical address (also called a far pointer).  A logical address comprises 2 parts - a segment selector and an effective address.  The logical address can resemble the physical address or may be not depending on the translation process.

IA-32 operates in 3 modes - real mode, protected mode and system management mode (SMM).

SMM
It is used to execute code in firmware (e.g. emergency shutdown, power management)

Real Mode
Real Mode implements a 16-bit execution environment of the old 8086/88.  It is an instance of the segmented memory model.  The logical address contains 16-bit segment selector and 16-bit effective address.

The selector stores the base address of a 64KB segment.  To form a 20-bit address (used by 8086/88), the segment selector is appended with a zero nibble before adding on with the effective address.  For example, the logical address 02001010 is translated to 0200[0] + [0]1010 = 03010.  The segment address is always at paragraph boundary (because of the appended zero) and the segment is at most 64KB (because of the 16-bit effective address).  There is no memory protection under Real Mode.

There are 5 combination of segment registers and GPR:
  • SS:SP
  • SS:BP
  • ES:DI
  • DS:SI
  • CS:IP

MSDOS is a real mode OS.  The first 640KB is called Conventional Memory.  The remaining memory area up to 1MB is called Upper Memory Area, which is reserved for used by ROM and peripheral.  Within UMA, there are slot of free memory not used by hardware and accessible by DOS are called UMB (Upper Memory Block).  Memory above the real mode 1MB boundary (when 386 was released) are called extended memory.

In a Real Mode Flat Model, program starts at 100h which is a holdover from CP/M-80 (PSP).

Protected Mode
Similar to real mode, protected mode is an instance of segmented memory. The difference is that operating system is now must collaborate with the CPU to resolve addresses.  The segment registers are owned by OS and program cannot read or write them.

No comments: