All Topics  
Memory segment

 

   Email Print
   Bookmark   Link






 

Memory segment



 
 
x86 memory segmentation refers to the implementation of memory segmentation on the x86 architecture
X86 architecture

The generic term x86 refers to the most commercially successful instruction set architecture in the history of personal computing. It derived from the model numbers, ending in "86", of the first few processor generations Backward compatibility with the original Intel 8086....
. Memory is divided into portions that may be addressed by a single index register
Index register

An index register in a computer's central processing unit is a processor register used for modifying operand addresses during the run of a program, typically for doing vector/array operations....
 without changing a 16-bit segment selector. In real mode
Real mode

Real mode, also called real address mode, is an operating mode of 80286 and later x86-compatible Central processing unit. Real mode is characterized by a 20 bit segmented memory address space , direct software access to BIOS routines and peripheral hardware, and no concept of memory protection or computer multitasking at the hardware le...
 or V86 mode
Virtual 8086 mode

In the 80386 microprocessor and later, Virtual 8086 mode, also called virtual real mode or VM86, allows the execution of real mode applications that are protected mode#Real_mode_application_compatibility directly in protected mode....
, a segment is always 64 kilobyte
Kilobyte

Kilobyte is a unit of Computer data storage equal to either 1,024 bytes or 1,000 bytes , depending on context.It is abbreviated in a number of ways: KB, kB, K and Kbyte....
s in size (using 16-bit offsets). In protected mode
Protected mode

In computing, protected mode, also called protected virtual address mode, is an operational mode of x86-compatible central processing units ....
, a segment can have variable length.

Real mode
In 16-bit real mode, enabling applications to make use of multiple memory segments (in order to access more memory than available in any one 64K-segment) was quite complex, but was viewed as a necessary evil for all but the smallest tools (which could do with less memory).






Discussion
Ask a question about 'Memory segment'
Start a new discussion about 'Memory segment'
Answer questions from other users
Full Discussion Forum



Encyclopedia


x86 memory segmentation refers to the implementation of memory segmentation on the x86 architecture
X86 architecture

The generic term x86 refers to the most commercially successful instruction set architecture in the history of personal computing. It derived from the model numbers, ending in "86", of the first few processor generations Backward compatibility with the original Intel 8086....
. Memory is divided into portions that may be addressed by a single index register
Index register

An index register in a computer's central processing unit is a processor register used for modifying operand addresses during the run of a program, typically for doing vector/array operations....
 without changing a 16-bit segment selector. In real mode
Real mode

Real mode, also called real address mode, is an operating mode of 80286 and later x86-compatible Central processing unit. Real mode is characterized by a 20 bit segmented memory address space , direct software access to BIOS routines and peripheral hardware, and no concept of memory protection or computer multitasking at the hardware le...
 or V86 mode
Virtual 8086 mode

In the 80386 microprocessor and later, Virtual 8086 mode, also called virtual real mode or VM86, allows the execution of real mode applications that are protected mode#Real_mode_application_compatibility directly in protected mode....
, a segment is always 64 kilobyte
Kilobyte

Kilobyte is a unit of Computer data storage equal to either 1,024 bytes or 1,000 bytes , depending on context.It is abbreviated in a number of ways: KB, kB, K and Kbyte....
s in size (using 16-bit offsets). In protected mode
Protected mode

In computing, protected mode, also called protected virtual address mode, is an operational mode of x86-compatible central processing units ....
, a segment can have variable length.

Real mode


In 16-bit real mode, enabling applications to make use of multiple memory segments (in order to access more memory than available in any one 64K-segment) was quite complex, but was viewed as a necessary evil for all but the smallest tools (which could do with less memory). The root of the problem was that no appropriate address-arithmetic instructions suitable for flat addressing of the entire memory range were available. Flat addressing is possible by applying multiple instructions, which however leads to slower programs.

In real mode, the 16-bit segment selector was interpreted as the most significant 16 bits of a linear 20-bit address, with the remaining four bits being all zeros. The segment selector is always added with a 16-bit offset to yield a linear address. For instance, the segmented address 6EFh:1234h has a segment selector of 6EFh, which corresponds to the 20-bit linear address 6EF0h. To this we add the offset, yielding the linear address 6EF0h + 1234h = 8124h (cf. hexadecimal
Hexadecimal

In mathematics and computer science, hexadecimal is a numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 09 to represent values zero to nine, and A, B, C, D, E, F to represent values ten to fifteen....
).

A single linear address can be mapped to many segmented addresses. For instance, the linear address above (8124h) can have the segmented addresses 6EFh:1234h, 812h:4h and 0h:8124h (and many more). This could be confusing to programmers accustomed to unique addressing schemes.

The effective 20-bit address space
Address space

In computing, an address space defines a range of discrete addresses, each of which may correspond to a physical or virtual memory register, a Node , peripheral device, disk sector or other logical or physical entity....
 of real mode limited the addressable memory to 220 bytes, or 1,048,576 bytes.

Protected mode

In protected mode
Protected mode

In computing, protected mode, also called protected virtual address mode, is an operational mode of x86-compatible central processing units ....
, segmentation is used as a virtual memory
Virtual memory

Virtual memory is a computer system technique which gives an application program the impression that it has contiguous working memory , while in fact it may be physically fragmented and may even overflow on to disk storage....
 mechanism, providing memory isolation and contiguous addressing of non-contiguous physical memory.

On the 386
Intel 80386

The Intel 80386, otherwise known as the i386 or just 386, is a microprocessor which has been used as the central processing unit of many personal computers and workstations since 1986....
 and later, programs issue logical (46-bit) addresses which go through the segmentation unit to be checked and translated into linear 32-bit addresses, before being sent to the paging
Paging

In computer operating systems that have their main memory divided into page , paging is a transfer of pages between main memory and an auxiliary store, such as hard disk drive....
 unit (if enabled) which ultimately translates them into physical addresses (which are also 32-bit on the 386
Intel 80386

The Intel 80386, otherwise known as the i386 or just 386, is a microprocessor which has been used as the central processing unit of many personal computers and workstations since 1986....
, but can be larger on more modern processors which support Physical Address Extension
Physical Address Extension

In computing, Physical Address Extension is a feature of x86 and x86-64 processors that enable the use of more than 4 gigabytes of physical memory to be used in 32-bit systems, given appropriate operating system support....
).

Detailed Segmentation Unit Workflow

A logical address consists of a 16-bit segment selector (supplying 13+1 address bits) and a 32-bit offset (16-bit on the 286). The segment selector must be located in one of the segment registers. That selector consists of a 2-bit Requested Privilege Level
Privilege level

The concept of privilege level refers to protecting resources on a CPU. Different execution threads can have different privilege levels that grant access to resources such as memory regions, I/O ports, and special instructions....
 (RPL) where the lowest number is the highest privilege level, a 1-bit Table Indicator (TI), and a 13-bit index.

The processor accesses the 64-bit segment descriptor
Segment descriptor

In memory addressing for Intel x86 computer architectures, segment descriptors are a part of the segmentation unit, used for translating a logical address to linear address....
 structure in the Global Descriptor Table
Global Descriptor Table

The Global Descriptor Table or GDT is a data structure used by Intel x86-family processors starting with the 80286 in order to define the characteristics of the various memory areas used during program execution, for example the base address, the size and access privileges like executability and writability....
 if TI is 0 or in the Local Descriptor Table
Local Descriptor Table

The Local Descriptor Table is a memory table used in the x86 architecture in protected mode and containing memory segment descriptors: start in linear memory, size, executability, writability, access privilege, actual presence in memory, etc....
 if TI is 1. It then performs the privilege check:

DPL < max (CPL,RPL)

where CPL is the current privilege level (lower 2 bits in CS), RPL is the requested privilege level from the segment selector, and DPL is the descriptor privilege level of the segment (found in the descriptor).

If the inequality is true, the processor generates a general protection fault
General protection fault

A General Protection Fault in the Intel x86 and AMD x86-64 CPU Architectures, and other unrelated architectures, is a fault which can encompass several cases where protection mechanisms within the processor architecture are violated by any of the programs that are running, whether it be the kernel or user program....
 (GP). Otherwise, address translation continues. This privilege check is done only when the segment register is loaded because segment descriptors are cached in hidden parts of the segment registers. The processor then takes the 32-bit or 16-bit offset and compares it against the segment limit specified in the segment descriptor. If it is larger, a GP fault is generated. Otherwise, the processor adds the segment base (32-bit or 24-bit, specified in descriptor) to the offset and this creates a linear address.

Practices

Logical addresses can be explicitly specified in x86 assembler language, e.g. (AT&T syntax):

movl $42, %fs ; Equivalent to M[fs:eax]<-42) in RTL
Register Transfer Language

In computer science, register transfer language is a term used to describe a kind of intermediate representation that is very close to assembly language, such as that which is used in a compiler....


Usually, however, implied segments are used. All instruction fetches come from the code segment
Code segment

In computing, a code segment, also known as a text segment or simply as text, is a phrase used to refer to a portion of memory or of an object file that contains executable Instruction s....
 in the CS register. Most memory references come from data segment
Data segment

A data segment is one of the sections of a program in an object file or in memory, which contains the global variables that are initialized by the programmer....
 in the DS register. Processor stack references, either implicitly (e.g. push and pop instructions) or explicitly (memory accesses using the ESP or (E)BP registers) use the stack segment in the SS register. Finally, string instructions (e.g. stos, movs) also use the extra segment ES.

Segmentation cannot be turned off on x86 processors, so many operating systems use a flat memory model
Flat memory model

In low level software design, a flat memory model refers to the memory addressing paradigm. A flat memory model uses a linear addressing scheme, allowing direct addressing all of the available memory locations....
 to make segmentation unnoticeable to programs. For instance, the Linux
Linux

Linux is a generic term referring to Unix-like computer operating systems based on the Linux kernel. Their development is one of the most prominent examples of free and open source software collaboration; typically all the underlying source code can be used, freely modified, and redistributed by anyone under the terms of the GNU GPL license...
 kernel sets up only 4 segments:

* __KERNEL_CS (Kernel code segment, base=0, limit=4GB, DPL=0) * __KERNEL_DS (Kernel data segment, base=0, limit=4GB, DPL=0) * __USER_CS (User code segment, base=0, limit=4GB, DPL=3) * __USER_DS (User data segment, base=0, limit=4GB, DPL=3)

Since the base is set to 0 in all cases and the limit 4 GiB, the segmentation unit does not affect the addresses the program issues before they arrive at the paging
Paging

In computer operating systems that have their main memory divided into page , paging is a transfer of pages between main memory and an auxiliary store, such as hard disk drive....
 unit.

Segments can be defined to be either code, data, or system segments. Additional permission bits are present to make segments read only, read/write, execute, etc.

Note that code may always modify all segment registers except CS (the code segment
Code segment

In computing, a code segment, also known as a text segment or simply as text, is a phrase used to refer to a portion of memory or of an object file that contains executable Instruction s....
). This is because the current privilege level (CPL) of the processor is stored in the lower 2 bits of the CS register. The only way to raise the processor privilege level (and reload CS) is through the lcall (far call) and int (interrupt) instructions. Similarly, the only way to lower the privilege level (and reload CS) is through lret (far return) and iret (interrupt return).

For more about segmentation, see the IA-32
IA-32

IA-32 , often generically called x86 or x86-32, is the instruction set architecture of Intel's most commercially successful microprocessors....
 manuals freely available on the AMD or Intel websites.

See also

  • Memory model (computing)
    Memory model (computing)

    In computing, a Memory model describes how Thread interact through memory, or more generally specify what assumptions the compiler is allowed to make when generating code for segmented memory or Page platforms....
  • THE multiprogramming system
    THE multiprogramming system

    The THE multiprogramming system was a computer operating system designed by a team led by Edsger W. Dijkstra, described in monographs in 1965-66 and published in 1968....


External links