Memory segmentation: Difference between revisions

Content deleted Content added
Orielno (talk | contribs)
m inner link
Changing short description from "The division of computer's primary memory into separately relocatable segments or sections" to "Division of computer's primary memory into separately relocatable segments or sections"
 
(45 intermediate revisions by 22 users not shown)
Line 1:
{{aboutShort description|segmentedDivision of computer's primary memory|segments ininto objectseparately code|Objectrelocatable segments or filesections}}
{{About|segmented computer memory|segments in object code|Object file}}
{{More footnotes|date=January 2011}}
{{Use dmy dates|date=July 2022|cs1-dates=y}}
'''Memory segmentation''' is an [[operating system]] [[memory management (operating systems)|memory management]] technique of dividing a [[computer]]'s [[primary memory]] into '''segments''' or '''sections'''. In a [[computer architecture|computer system]] using segmentation, a reference to a memory ___location includes a value that identifies a segment and an [[offset (computer science)|offset]] (memory ___location) within that segment. Segments or sections are also used in [[object file]]s of compiled programs when they are [[linker (computing)|linked]] together into a [[program image]] and when the image is [[loader (computing)|loaded]] into memory.
 
Segments usually correspond to natural divisions of a program such as individual routines or data tables<ref name="holt1961"/> so segmentation is generally more visible to the programmer than [[paging]] alone.<ref name="englander"/> Segments may be created for program [[module (programming)|module]]s, or for classes of memory usage such as [[code segment]]s and [[data segment]]s.<ref name="glaser1965"/> Certain segments may be shared between programs.<ref name="holt1961"/><ref name="englander"/>
'''Memory segmentation''' is a computer ([[Primary memory|primary]]) [[memory management]] technique of division of a [[computer]]'s primary memory into '''segments''' or '''sections'''. In a [[Computer architecture|computer system]] using segmentation, a reference to a memory ___location includes a value that identifies a segment and an [[offset (computer science)|offset]] (memory ___location) within that segment. Segments or sections are also used in [[object file]]s of compiled programs when they are [[Linker (computing)|linked]] together into a [[program image]] and when the image is [[Loader (computing)|loaded]] into memory.
 
Segmentation was originally invented as a method by which [[system software]] could isolate software [[process (computing)|processes]] ([[task (computing)|tasks]]) and data they are using. It was intended to increase reliability of the systems running multiple processes simultaneously.<ref name=":0">{{cite book |url=https://www.amd.com/system/files/TechDocs/24593.pdf |title=AMD64 Technology AMD64 Architecture Programmer's Manual Volume 2: System Programming |publisher=Advanced Micro Devices |date=2018 |volume=2 |pages=5 |language=en |chapter=1.2 Memory Management}}</ref>
Segments usually correspond to natural divisions of a program such as individual routines or data tables<ref name="holt1961"/> so segmentation is generally more visible to the programmer than [[Paging|paging]] alone.<ref name="englander"/>
Different segments may be created for different program [[module (programming)|module]]s, or for different classes of memory usage such as [[Code segment|code]] and [[data segment]]s.<ref name="glaser1965"/> Certain segments may be shared between programs.<ref name="holt1961"/><ref name="englander" />
 
Segmentation was originally invented as a method by which [[system software]] could isolate different software [[Process (computing)|processes]] ([[Task (computing)|tasks]]) and data they are using. It was intended to increase reliability of the systems running multiple processes simultaneously.<ref name=":0">{{Cite book|url=https://www.amd.com/system/files/TechDocs/24594.pdf|title=AMD64 Technology AMD64 Architecture Programmer’s Manual Volume 2: System Programming|last=|first=|publisher=Advanced Micro Devices|year=2018|isbn=|volume=2|___location=|pages=5|language=en|chapter=1.2 Memory Management}}</ref> In a [[X86-64|x86-64 architecture]] it is considered legacy and most x86-64-based modern system software don't use memory segmentation. Instead they handle programs and their data by utilizing [[Paging|memory-paging]] which also serves as a way of memory protection. However most x86-64 implementations still support it for backward compatibility reasons.<ref name=":0" />
 
==Hardware implementation==
In a system using segmentation, computer memory addresses consist of a segment id and an offset within the segment.<ref name="glaser1965"/> A hardware [[memory management unit]] (MMU) is responsible for translating the segment and offset into a [[physical address]], and for performing checks to make sure the translation can be done and that the reference to that segment and offset is permitted.
 
In a system using segmentation, computer memory addresses consist of a segment id and an offset within the segment.<ref name="glaser1965"/>
A hardware [[memory management unit]] (MMU) is responsible for translating the segment and offset into a [[physical address]], and for performing checks to make sure the translation can be done and that the reference to that segment and offset is permitted.
 
Each segment has a length and set of permissions (for example, ''read'', ''write'', ''execute'') associated with it.<ref name="glaser1965"/> A [[process (computing)|process]] is only allowed to make a reference into a segment if the type of reference is allowed by the permissions, and if the offset within the segment is within the range specified by the length of the segment. Otherwise, a [[hardware exception]] such as a [[segmentation fault]] is raised.
 
Segments may also be used to implement [[virtual memory]]. In this case each segment has an associated flag indicating whether it is present in main memory or not. If a segment is accessed that is not present in main memory, an exception is raised, and the [[operating system]] will read the segment into memory from secondary storage.
Segments may also be used to implement [[virtual memory]].
In this case each segment has an associated flag indicating whether it is present in main memory or not. If a segment is accessed that is not present in main memory, an exception is raised, and the [[operating system]] will read the segment into memory from secondary storage.
 
Segmentation is one method of implementing [[memory protection]].<ref name="ostep-1">{{cite book |title=Operating Systems: Three Easy Pieces |chapter=Segmentation |chapter-url=http://pages.cs.wisc.edu/~remzi/OSTEP/vm-segmentation.pdf |publisher= Arpaci-Dusseau Books|year |date= 2014 |first1 = Remzi H. |last1 =Arpaci-Dusseau |first2=Andrea C. |last2 = Arpaci-Dusseau}}</ref> [[Page (computer memory)|Paging]]<!-- please, do not "simplify" or "fix" the link. The [[paging]] article is about a virtual memory technique, not about memory protection --> is another, and they can be combined. The size of a memory segment is generally not fixed and may be as small as a single [[byte]].<ref>{{cite book |publisher=Intel Corporation |title=Intel® 64 and IA-32 Architectures Software Developer’sDeveloper's Manual Volume 3 (3A, 3B & 3C): System Programming Guide |yeardate=2012 |pages=3–13 |url=http://download.intel.com/products/processor/manual/325384.pdf}}</ref>
 
Segmentation has been implemented in several different ways on differentvarious hardware, with or without paging. Intel [[x86 memory segmentation]] does not fit either model and is discussed separately below, and also in greater detail in a separate article.
 
===Segmentation without paging===
Associated with each segment is information that indicates where the segment is located in memory&mdash; the ''segment base''. When a program references a memory ___location, the offset is added to the segment base to generate a physical memory address.
 
An implementation of virtual memory on a system using segmentation without paging requires that entire segments be swapped back and forth between main memory and secondary storage. When a segment is swapped in, the operating system has to allocate enough contiguous free memory to hold the entire segment. Often [[Fragmentationfragmentation (computing)|memory fragmentation]] results if there is not enough contiguous memory even though there may be enough in total.
 
===Segmentation with paging===
Instead of an actuala memory ___location, the segment information includes the address of a [[page table]] for the segment.
When a program references a memory ___location the offset is translated to a memory address using the page table. A segment can be extended simply by allocating another memory page and adding it to the segment's page table.
 
An implementation of [[virtual memory]] on a system using segmentation with paging usually only moves individual pages back and forth between main memory and secondary storage, similar to a paged non-segmented system. Pages of the segment can be located anywhere in main memory and need not be contiguous. This usually results in a reduced amount of input/output between primary and secondary storage and reduced memory fragmentation.
 
==History==
The [[Burroughs Corporation]] [[Burroughs large systems|B5000]] computer was one of the first to implement segmentation, and "perhaps the first commercial computer to provide virtual memory"<ref>{{cite web |last=Mayer |first=Alastair J. W. |title=The Architecture of the Burroughs B5000 - 20 Years Later and Still Ahead of the Times? |url=http://www.smecc.org/The%20Architecture%20%20of%20the%20Burroughs%20B-5000.htm |accessdateaccess-date=March 15, 2012-03-15}}</ref> based on segmentation. The B5000 is equipped with a segment information table called the Program Reference Table (PRT) which is used to indicate whether the corresponding segment resides in the main memory, to maintain the [[base address]] and the size of the segment.<ref>{{cite book |title=Computer Architecture and Organization |last=Hayes |first=John P. |isbn=0-07-027363-4 |year=1978 |page=371|publisher=McGraw-Hill }}</ref> The later [[Burroughs large systems#B6500|B6500]] computer also implemented segmentation; a version of its architecture is still in use today on the Unisys ClearPath Libra servers.{{Citation needed|date=April 2024}}
 
The [[GE-600 series|GE-645]] computer, a modification of the [[GE-600 series|GE-635]] with segmentation and paging support added, was designed in 1964 to support [[Multics]].
 
The [[Intel iAPX 432]],<ref>{{cite book |publisher=Intel Corporation |title=Introduction to the IAPX 432 Architecture |yeardate=1981 |pages=78 |url=http://bitsavers.org/components/intel/iAPX_432/171821-001_Introduction_to_the_iAPX_432_Architecture_Aug81.pdf}}</ref> begun in 1975, attempted to implement a true segmented architecture with memory protection on a microprocessor.
 
The 960MX version of the [[Intel i960]] processors supported load and store instructions with the source or destination being an "access descriptor" for an object, and an offset into the object, with the access descriptor being in a 32-bit register and with the offset computed from a base offset in the next register and from an additional offset and, optionally, an index register specified in the instruction. An access descriptor contains permission bits and a 26-bit object index; the object index is an index into a table of object descriptors, giving an object type, an object length, and a physical address for the object's data, a page table for the object, or the top-level page table for a two-level page table for the object, depending on the object type.<ref>{{cite book |url=http://bitsavers.org/pdf/biin/BiiN_CPU_Architecture_Reference_Man_Jul88.pdf |title=BiiN CPU Architecture Reference Manual |date=July 1998 |publisher=BiiN}}</ref>
 
[[Prime Computer|Prime]], [[Stratus Technologies|Stratus]], [[Apollo Computer|Apollo]], [[IBM System/38]], and [[IBM AS/400]] (including [[IBM i]]) computers use memory segmentation.
 
==Examples==
==x86 architecture==
 
===[[Burroughs Large Systems|Burroughs large system architectures]]===
{{seealso|Burroughs large systems descriptors}}
Burroughs introduced segmentation on the [[B5000]], followed by the compatible B5500 and B5700.
 
The later [[B6500]] replaced the Program Reference Table (PRT) with a [[Saguaro stack]], changed control word formats, changed [[Burroughs large systems descriptors|descriptor]] formats and changed the mechanism for referring to a control word or descriptor.
 
====B5000, B5500 and B5700====
{{see also|B5000 instruction set|Burroughs large systems descriptors#B5000, B5500 and B5700}}
Words in the [[Burroughs Large Systems#B5000, B5500, and B5700|B5000, B5500 and B5700]] are 48 bits long.<ref name=oper>{{Citation
| title = The Operational Characteristic of the Processors for the Burroughs B 5000
| id = 5000-21005A
| edition = A
| year = 1962
| url = https://bitsavers.org/pdf/burroughs/LargeSystems/B5000_5500_5700/5000-21005_B5000_operChar_1963.pdf
| ___location = Detroit
| publisher = Burroughs
}}
</ref> [[Burroughs large systems descriptors|Descriptors]] have the uppermost bit set in the word. They reside in either the Program Reference Table (PRT) or the stack, and contain a ''presence bit'' indicating whether the data are present in memory. There are distinct data and program descriptors.<ref name=oper />{{rp|pages = 4{{hyp}}2-4{{hyp}}4}}
 
====B6500, B7500 and successors====
{{see also|Burroughs B6x00-7x00 instruction set|Burroughs large systems descriptors#B6500, B7500 and successors}}
Words in the B6500 and its successors have 48 bits of data and 3 tag bits.<!--4 bits in later machines?--><ref name="b6500-ref-man">{{cite book |url=http://www.bitsavers.org/pdf/burroughs/LargeSystems/B6500_6700/1043676_B6500_RefMan_Sep69.pdf |title=B6500 Information Processing Systems Reference Manual |publisher=Burroughs |date=September 1969}}</ref>{{rp|page=2{{hyp}}1}} The tag bits indicate the type of data contained in the word; there are several descriptor types, indicated by different tag bit values.<ref name="b6500-ref-man" />{{rp|pages=6{{hyp}}5-6{{hyp}}10}} Control words and descriptors reside in the Saguaro stack. Array segments may be paged.
 
The line includes the B6500, B6700, B7700, B6800, B6900, B5900, the A-series Burroughs and Unisys machines, and the current Clearpath MCP systems (Libra). While there have been a few enhancements over the years, particularly hardware advances, the architecture has changed little. The segmentation scheme has remained the same, see [[Virtual Memory#Segmented_virtual_memory|Segmented memory]].
 
===Multics architectures===
{{stub-section|date=January 2024}}
 
===S/370 architecture===
In the [[IBM System/370]] models{{efn|Models 115, 125, 135, 138, 145, 148, 155 II, 158, 165 II, and 168}} with virtual storage<ref name="S370">{{cite book
|title=IBM System/370 Principles of Operation
|id=GA22-7000-4
|edition=Fourth
|date=September 1974
|pages=57–68
|series=Systems
|publisher=[[IBM]]
|section=Dynamic Address Translation
|section-url=http://www.bitsavers.org/pdf/ibm/370/princOps/GA22-7000-4_370_Principles_Of_Operation_Sep75.pdf#page=57
|url=http://www.bitsavers.org/pdf/ibm/370/princOps/GA22-7000-4_370_Principles_Of_Operation_Sep75.pdf
}}
<!-- This is the most recent version that I could find in bitsavers -->
</ref><ref name="S370-10">{{cite book
|title=IBM System/370 Principles of Operation
|id=GA22-7000-10
|edition=Eleventh
|date=September 1987
|section=Dynamic Address Translation
|pages=3-20-3-38
|publisher=IBM
}}
</ref> (DAT) and 24-bit addresses, [[control register]] 0 specifies a segment size of either 64 KiB or 1 MiB and a page size of either 2 KiB or 4 KiB; control register 1 contains a Segment Table Designator (STD), which specifies the length and real address of the segment table. Each segment table entry contains a page table ___location, a page table length and an invalid bit. IBM later expanded the address size to 31 bits and added two bits to the segment table entries:
 
;Segment-protection bit
:Segment is read-only
;Common-segment bit
:The segment is shared between address spaces; this bit is set to optimize TLB use
 
Each of IBM's DAT implementations includes a translation cache, which IBM called a Translation Lookaside Buffer (TLB). While Principles of Operation discusses the TLB in general terms, the details are not part of the architecture and vary from model to model.
 
Starting with the [[IBM 303X|3031, 3032, and 3033]] processor complexes, IBM offered a feature called ''Dual-address Space''<ref name="S370-10"/>{{rp|at=Dual-Address-Space Control|pp=5-13-5-17}}{{rp|at=DAS Authorization Mechanisms|pp=5-17-5-20}}{{rp|at=PC-Number Translation|pp=5-21-5-24}}<ref name="S370-XA">{{cite book
|title=IBM System/370 Extended Architecture Principles of Operation
|id=SA22-7085-1
|edition=Second
|date=January 1987
|pages=3-13-3-14
|publisher=IBM
|section=Address spaces
|section-url=http://bitsavers.org/pdf/ibm/370/princOps/SA22-7085-1_370-XA_Principles_of_Operation_Jan87.pdf#page=40
|url=http://bitsavers.org/pdf/ibm/370/princOps/SA22-7085-1_370-XA_Principles_of_Operation_Jan87.pdf
}}
</ref> (DAS), which allows a program to switch between the translation tables for two address spaces, referred to as ''primary address space'' (CR1) and ''secondary address space'' (CR7), and to move data between the address spaces subject to protection key. DAS supports a translation table to convert a 16-bit address space number (ASN) to an STD, with privileged instructions to load the STD into CR1 (primary) or CR7 (secondary).
 
===x86 architecture===
{{Main|x86 memory segmentation}}
The memory segmentation used by earlyEarly [[x86]] processors, beginning with the [[Intel 8086]], doesprovide notcrude providememory anysegmentation and no [[memory protection]]. (Every Anybyte programof runningevery onsegment theseis processorsalways canavailable accessto any program.) The 16-bit segment withregisters noallow restrictions.for A65,536 segments; each segment isbegins onlyat identifieda byfixed itsoffset startingequal ___location;to there16 istimes nothe lengthsegment checking.number; Thethe segment starting address granularity is 16 bytes. andEach thesegment offsetgrants isread-write 16access bitsto 64&nbsp;KiB (65,536 supportingbytes) segmentof sizesaddress upspace to(this 64limit KiB,is soset segmentsby canthe 16-bit PC (and oftenSP doregisters; the processor does no bounds checking). overlapOffset+address andexceeding 0xFFFFF wraps around to 0x00000. Each 64&nbsp;KiB segment overlaps the next 4,095 segments; each physical address can be denoted by 40964,096 differentsegment–offset segmentpairs. This scheme can address only 1&ndashnbsp;offsetMiB pairs(1024&nbsp;KiB) of physical memory (allowingand formemory-mapped addressi/o). offset(Optional [[expanded memory]] hardware can add wrapbank-aroundswitched memory under software control.) Intel retroactively named the sole operating mode of these x86 CPU models "[[real mode]]".
 
Segmentation in theThe [[Intel 80286]] and later provides protection: with theprocessors introduction of the 80286, Intel retroactively named the sole operating mode of the previous x86 CPU modelsadd "286 [[realprotected mode]]", which retains 16-bit addressing, and introducedadds asegmentation new(without "[[protectedpaging) mode]]"and withper-segment memory protection features. For backward compatibility, all x86 CPUs start up in "real mode", with the same fixed overlapping 64&nbsp;KiB segments, no memory protection, fixedonly 641&nbsp;KiBMiB segmentsphysical address space, and onlysome 20subtle differences ([[high memory area]], [[unreal mode]]). In order to use its full 24-bit (102416&nbsp;KiBMiB) addressing.physical Anaddress space and advanced [[memory management unit|MMU]] features, an 80286 or later processor must be switched into another"protected mode" by software, inusually orderthe tooperating system or a [[DOS extender]]. If a program does not use itsthe fullsegment addressregisters, spaceor andonly advancedputs MMUvalues featuresinto them that it receives from the operating system, then identical code can run in real mode or protected mode, but most real-mode software computes new values for the segment registers, breaking this compatibility.
 
The [[Intel 80386i386]] and later processors alsoadd support"386 [[protected mode]]", which uses 32-bit addressing, retains segmentation, and adds [[memory paging;]]. inIn thosethese processors, the segment table, rather than pointing to a page table for the segment, contains the segment address in ''linear memory''. AddressesWhen paging is enabled, addresses in linear memory are then mapped to physical addresses using a separate page table. Most operating systems did not use the segmentation capability, ifopting to keep the base address in all segment registers equal to 0 at all times and provide per-page memory protection and swapping using only paging. is enabledSome use the CS register to provide [[executable space protection]] on processors lacking the [[NX bit]] or use the FS or GS registers to access thread-local storage.<ref name="pietrek-windows-nt">{{cite web |url=https://learn.microsoft.com/en-us/archive/msdn-magazine/2006/may/x64-starting-out-in-64-bit-windows-systems-with-visual-c |title=Everything You Need To Know To Start Programming 64-Bit Windows Systems |publisher=Microsoft |author=Matt Pietrek |author-link=Matt Pietrek |date=May 2006 |access-date=April 18, 2023}}</ref><ref name="drepper-linux-etc">{{cite web |url=http://www.akkadia.org/drepper/tls.pdf |title=ELF Handling For Thread-Local Storage |first=Ulrich |last=Drepper |date=August 22, 2013}}</ref>
 
The [[x86-64]] architecture does not usesupport segmentation in "[[long mode]]" (64-bit mode).<ref name=":1">{{Citecite book|url=https://www.amd.com/system/files/TechDocs/24594.pdf |title=AMD64 Technology AMD64 Architecture Programmer’sProgrammer's Manual Volume 2: System Programming|last=|first= |publisher=Advanced Micro Devices|year=2018|isbn= |volume=2 |___locationdate=2018 |pagesurl=|language=en}}<https://ref> In a [[X86-64|x86-64 architecture]] it is considered legacy and most x86-64-based modern system software don't use memory segmentationwww. Instead they handle programs and their data by utilizing [[Paging|memory-paging]] which also serves as a way of memory protectionamd. Though most x86-64 implementations still support it for backward compatibility reasonscom/system/files/TechDocs/24594.pdf}}</ref name=":0" /> Four of the segment registers: CS, SS, DS, and ES are forced to 0, and the limit to 2<sup>64</sup>. The segment registers FS and GS can still have a nonzero base address. This allows operating systems to use these segments for special purposes such as thread-local storage.<ref name="pietrek-windows-nt" /><ref name="drepper-linux-etc" />
 
== See also ==
* [[BSS Segment]]
* [[Data segment]]
* [[Flat memory model]]
* [[Memory management (operating systems)]]
* [[Segmentation fault]]
* [[Virtual address space]]
* [[Virtual memory]]
* [[Data segment]]
* [[BSS Segment]]
* [[x86 memory segmentation]]
* [[Segmentation fault]]
* [[Flat memory model]]
 
== References Notes==
{{Notelist}}
{{reflist |refs=
 
==References==
{{Reflist |refs=
<ref name="holt1961">{{cite journal
|title=Program Organization and Record Keeping for Dynamic Storage Allocation
|first=A.Anatol W.
|last=Holt
|yeardate=1961
|journal=Communications of the ACM
|volume=4
|issue=10
|pages=422–431
|doi=10.1145/366786.366795
|s2cid=18973700
|doi-access=free
}}
</ref>
<ref name="glaser1965">{{cite conference
|title=System Design of a Computer for Time Sharing Applications
|url=https://multicians.org/fjcc2.html
|first1=E. L.
|last1=Glaser
|first2first1=J.Edward FL.
|first2=John F.
|last2=Couleur
|first3=G. A.
|last2=Couleur
|last3=Oliver
|yeardate=1965
|conference=1965 Fall Joint Computer Conference}}
|url=https://multicians.org/fjcc2.html}}
</ref>
<ref name="englander">{{cite book
|last=Englander |first=Irv |yeardate=2003
|title=The architecture of computer hardware and systems software |edition=3rd
|publisher=Wiley |isbn=0-471-07325-3
Line 96 ⟶ 176:
==External links==
*IA-32 Intel Architecture Software Developer’s Manual Volume 3A: System Programming Guide. http://www.intel.com/products/processor/manuals/index.htm.
*Operating Systems: Internals and Design Principles by William Stallings. Publisher: Prentice Hall. {{ISBN|0-13-147954-7}}. {{ISBN|978-0-13-147954-8}}.
 
{{Memory management navbox}}
{{applicationApplication binary interface}}
 
[[Category:Memory management]]