Difference between revisions of "Memory Segmentation"

From COMP15212 Wiki
gravatar Yuron [userbureaucratinterface-adminsysopPHRhYmxlIGNsYXNzPSJ0d3BvcHVwIj48dHI+PHRkIGNsYXNzPSJ0d3BvcHVwLWVudHJ5dGl0bGUiPkdyb3Vwczo8L3RkPjx0ZD51c2VyPGJyIC8+YnVyZWF1Y3JhdDxiciAvPmludGVyZmFjZS1hZG1pbjxiciAvPnN5c29wPGJyIC8+PC90ZD48L3RyPjwvdGFibGU+] (talk | contribs)
m (1 revision imported)
pc>Yuron
Line 3: Line 3:
 
-->{{#invoke:Dependencies|add|Memory Management,3}}
 
-->{{#invoke:Dependencies|add|Memory Management,3}}
 
<blockquote>
 
<blockquote>
Note: the term “segmentation” is used in ways which
+
Note: the term “segmentation” is used in ways which differ in detail.
differ in detail.
 
 
</blockquote>
 
</blockquote>
The <strong>von Neumann computing architecture</strong> – by far the most common
+
The <strong>von Neumann computing architecture</strong> – by far the most common – has a <strong>single memory address space</strong> which is shared by code and data in all their forms.  This is a flexible, and (largely) convenient, model.
– has a <strong>single memory address space</strong> which is shared by code and
 
data in all their forms.  This is a flexible, and (largely)
 
convenient, model.
 
  
The memory is divided <em>logically</em> into a number of regions in any
+
The memory is divided <em>logically</em> into a number of regions in any given application.  The most obvious division is perhaps ‘code’ and ‘data’ where ‘code’ refers to the processor’s instructions – usually not modifiable at run-time and ‘data’ encompasses variables, which may be written.  Note that not all ‘data’ might change, however: there is often ‘read-only data’, such as message strings or look-up tables, which are constant.
given application.  The most obvious division is perhaps
 
‘code’ and ‘data’ where ‘code’
 
refers to the processor’s instructions – usually not modifiable at
 
run-time and ‘data’ encompasses variables, which may be
 
written.  Note that not all ‘data’ might change, however:
 
there is often ‘read-only data’, such as message strings
 
or look-up tables, which are constant.
 
  
 
Each [[Processes|process]] will have its own set of segments.
 
Each [[Processes|process]] will have its own set of segments.
  
It is sometimes convenient to group parts of memory together in these
+
It is sometimes convenient to group parts of memory together in these <strong>logical divisions</strong>, which are typically referred to as “segments”.  By nature, a particular segment will have a set of attributes (such as can or cannot be written to) and different segments will have different attributes.  There can be an arbitrary number of segments and, unlike [[Memory_Pages|pages]], they will typically have different sizes.
<strong>logical divisions</strong>, which are typically referred to as
 
“segments”.  By nature, a particular segment will have a
 
set of attributes (such as can or cannot be written to) and different
 
segments will have different attributes.  There can be an arbitrary
 
number of segments and, unlike [[Memory_Pages|pages]], they will
 
typically have different sizes.
 
<!-- TODO a nice illustration of segments: maybe (also?) an ELF
 
picture? -->
 
  
<em>Historically</em>, segments have sometimes been supported as (apparently)
+
<!-- TODO a nice illustration of segments: maybe (also?) an ELF picture? -->
dedicated parts memories, each with addresses starting at 00…00 and
+
 
finishing at some arbitrary limit (the segment size).  These can be
+
<em>Historically</em>, segments have sometimes been supported as (apparently) dedicated parts memories, each with addresses starting at 00…00 and finishing at some arbitrary limit (the segment size).  These can be mapped into physical memory using dedicated hardware; this is cheap in hardware … but hardware is now cheap, so it is uncommon.  (You may still see it in some literature; very unlikely in the Real World, currently.)  Instead the <em>logical</em> segment will typically be mapped
mapped into physical memory using dedicated hardware; this is cheap in
+
using hardware [[Memory_Pages|pages]] and the pages will be organised by software.
hardware … but hardware is now cheap, so it is uncommon.  (You may
 
still see it in some literature; very unlikely in the Real World,
 
currently.)  Instead the <em>logical</em> segment will typically be mapped
 
using hardware [[Memory_Pages|pages]] and the pages will be organised
 
by software.
 
 
<blockquote>
 
<blockquote>
<strong>Question:</strong> can you spot any advantages in using paging hardware
+
<strong>Question:</strong> can you spot any advantages in using paging hardware over trying to map variable-sized segments into RAM?
over trying to map variable-sized segments into RAM?
 
 
<div class="toccolours mw-collapsible mw-collapsed">
 
<div class="toccolours mw-collapsible mw-collapsed">
 
<div style="font-weight:bold;line-height:1.6;">Answer</div>
 
<div style="font-weight:bold;line-height:1.6;">Answer</div>
Line 53: Line 28:
 
*With [[Virtual Memory|virtual memory]] only some of the pages in a segment – those which are actually in use – need be present in physical memory at any time.
 
*With [[Virtual Memory|virtual memory]] only some of the pages in a segment – those which are actually in use – need be present in physical memory at any time.
 
*As an example, consider a code segment. In any given run, quite a lot of the code may never be needed: for example code to handle errors is important to have, but rarely executes. With paging and virtual memory, unwanted code (or data) need not occupy valuable physical memory as it is only fetched if it is needed.
 
*As an example, consider a code segment. In any given run, quite a lot of the code may never be needed: for example code to handle errors is important to have, but rarely executes. With paging and virtual memory, unwanted code (or data) need not occupy valuable physical memory as it is only fetched if it is needed.
 
 
</div>
 
</div>
 
</div>
 
</div>
  
 
</blockquote>
 
</blockquote>
There’s a [https://www.youtube.com/watch?v=E7XQesMS0bg video here]
+
There’s a [https://www.youtube.com/watch?v=E7XQesMS0bg video here] which might help explain this (8 mins.).
which might help explain this (8 mins.).
 
  
This does not, necessarily, mean the operating system <em>software</em> is
+
This does not, necessarily, mean the operating system <em>software</em> is not keeping segment tables so that pages can be allocated appropriately.
not keeping segment tables so that pages can be allocated
 
appropriately.
 
  
The name persists, of course, with ‘[[Exceptions|segmentation faults]]’.  You can also find such logical divisions in software tools: a nice contemporary example is in the organisation of [https://en.wikipedia.org/wiki/Executable_and_Linkable_Format|ELF files]
+
The name persists, of course, with ‘[[Exceptions|segmentation faults]]’.  You can also find such logical divisions in software tools: a nice contemporary example is in the organisation of [https://en.wikipedia.org/wiki/Executable_and_Linkable_Format|ELF files] which provide a standard for Unix ‘binaries’.
which provide a standard for Unix ‘binaries’.
 
 
<blockquote>
 
<blockquote>
Try typing <code>file <binary></code> at a Unix command prompt, where
+
Try typing <code>file <binary></code> at a Unix command prompt, where ‘<binary>’ is a binary’s filename (/path).
‘<binary>’ is a binary’s filename (/path).</binary>
 
 
</blockquote>
 
</blockquote>
If you have access to tools such as [https://www.gnu.org/software/binutils/ GNU Binutils] you can ‘pull
+
If you have access to tools such as [https://www.gnu.org/software/binutils/ GNU Binutils] you can ‘pull apart’ ELF files with <code>readelf</code>.  However, note that an ELF “section” is not quite the same as an O.S. “segment”.  (<code>readelf -l <binary></code> may help a bit.)
apart’ ELF files with <code>readelf</code>.  However, note that an ELF
 
“section” is not quite the same as an
 
O.S. “segment”.  (<code>readelf -l <binary></code> may help a bit.)
 
 
----
 
----
 
<blockquote>
 
<blockquote>
As mentioned at the top of the page, the word
+
As mentioned at the top of the page, the word “segmentation” is sometimes used in different ways.  A particular example is [[Extra:Intel x86 Segmentation|Intel x86 segmentation]] which has a certain legacy and may be of passing interest.
“segmentation” is sometimes used in different ways.  A
 
particular example is [Segmentation_x86 Intel x86 segmentation]
 
which has a certain legacy and may be of passing interest.
 
 
</blockquote>
 
</blockquote>
 
----
 
----
 
 
{{PageGraph}}
 
{{PageGraph}}
 
{{Category|Virtual Memory}}
 
{{Category|Virtual Memory}}
 
{{Category|Memory}}
 
{{Category|Memory}}

Revision as of 15:06, 2 August 2019

On path: Memory 1: Memory • 2: Memory Management • 3: Memory Sizes • 4: Memory Mapping • 5: Memory Segmentation • 6: Memory Protection • 7: Virtual Memory • 8: Paging • 9: Memory Management Unit (MMU) • 10: Caching • 11: Cache • 12: Translation Look-aside Buffer (TLB)
Depends on Memory Management

Note: the term “segmentation” is used in ways which differ in detail.

The von Neumann computing architecture – by far the most common – has a single memory address space which is shared by code and data in all their forms. This is a flexible, and (largely) convenient, model.

The memory is divided logically into a number of regions in any given application. The most obvious division is perhaps ‘code’ and ‘data’ where ‘code’ refers to the processor’s instructions – usually not modifiable at run-time and ‘data’ encompasses variables, which may be written. Note that not all ‘data’ might change, however: there is often ‘read-only data’, such as message strings or look-up tables, which are constant.

Each process will have its own set of segments.

It is sometimes convenient to group parts of memory together in these logical divisions, which are typically referred to as “segments”. By nature, a particular segment will have a set of attributes (such as can or cannot be written to) and different segments will have different attributes. There can be an arbitrary number of segments and, unlike pages, they will typically have different sizes.


Historically, segments have sometimes been supported as (apparently) dedicated parts memories, each with addresses starting at 00…00 and finishing at some arbitrary limit (the segment size). These can be mapped into physical memory using dedicated hardware; this is cheap in hardware … but hardware is now cheap, so it is uncommon. (You may still see it in some literature; very unlikely in the Real World, currently.) Instead the logical segment will typically be mapped using hardware pages and the pages will be organised by software.

Question: can you spot any advantages in using paging hardware over trying to map variable-sized segments into RAM?

Answer

Mapping a segment using page tables is more expensive than having a single mapping entry for the segment, but it brings a couple of useful advantages.

  • Because each page can be mapped individually, the segment does not need a (possibly, large) contiguous space in the physical memory. The pages can be used to fragment the segment. This gives much more flexibility and (largely) overcomes the problem of having enough memory in total to accommodate another segment, but not enough in one place.
  • By implication, if necessary a logical segment can be increased in size whilst the program is running without worrying about overlapping other memory. This could happen if, for example, more memory was being demanded for the stack or the heap.
  • With virtual memory only some of the pages in a segment – those which are actually in use – need be present in physical memory at any time.
  • As an example, consider a code segment. In any given run, quite a lot of the code may never be needed: for example code to handle errors is important to have, but rarely executes. With paging and virtual memory, unwanted code (or data) need not occupy valuable physical memory as it is only fetched if it is needed.

There’s a video here which might help explain this (8 mins.).

This does not, necessarily, mean the operating system software is not keeping segment tables so that pages can be allocated appropriately.

The name persists, of course, with ‘segmentation faults’. You can also find such logical divisions in software tools: a nice contemporary example is in the organisation of files which provide a standard for Unix ‘binaries’.

Try typing file <binary> at a Unix command prompt, where ‘<binary>’ is a binary’s filename (/path).

If you have access to tools such as GNU Binutils you can ‘pull apart’ ELF files with readelf. However, note that an ELF “section” is not quite the same as an O.S. “segment”. (readelf -l <binary> may help a bit.)


As mentioned at the top of the page, the word “segmentation” is sometimes used in different ways. A particular example is Intel x86 segmentation which has a certain legacy and may be of passing interest.