Você está na página 1de 11

OPERATING

SYSTEM

1
Rafael Nobleza Masallo CS501M
Assignment 9-29-17
Virtual Memory
What is Virtual Memory? ------------------------------------------------------------3-4
Significane of Virtual Memory? ---------------------------------------------------5-6
Virtual Memory Illustration--------------------------------------------------------7-11

2
Rafael Nobleza Masallo CS501M
Assignment 9-29-17
What is virtual memory?
Linux supports virtual memory, that is, using a disk as an extension of RAM so that the
effective size of usable memory grows correspondingly. The kernel will write the contents of a
currently unused block of memory to the hard disk so that the memory can be used for another
purpose. When the original contents are needed again, they are read back into memory. This is
all made completely transparent to the user; programs running under Linux only see the larger
amount of memory available and don't notice that parts of them reside on the disk from time to
time. Of course, reading and writing the hard disk is slower (on the order of a thousand times
slower) than using real memory, so the programs don't run as fast. The part of the hard disk that
is used as virtual memory is called the swap space.
Linux can use either a normal file in the filesystem or a separate partition for swap space.
A swap partition is faster, but it is easier to change the size of a swap file (there's no need to
repartition the whole hard disk, and possibly install everything from scratch). When you know
how much swap space you need, you should go for a swap partition, but if you are uncertain, you
can use a swap file first, use the system for a while so that you can get a feel for how much swap
you need, and then make a swap partition when you're confident about its size.

You should also know that Linux allows one to use several swap partitions and/or swap files at
the same time. This means that if you only occasionally need an unusual amount of swap space,
you can set up an extra swap file at such times, instead of keeping the whole amount allocated all
the time.

A note on operating system terminology: computer science usually distinguishes between


swapping (writing the whole process out to swap space) and paging (writing only fixed size
parts, usually a few kilobytes, at a time). Paging is usually more efficient, and that's what Linux
does, but traditional Linux terminology talks about swapping anyway.

Virtual memory (VM) is a feature developed for the kernel of an operating system (OS)
that simulates additional main memory such as RAM (random access memory) or disc storage.
This technique involves the manipulation and management of memory by allowing the loading
and execution of larger programs or multiple programs simultaneously. It also allows each
program to operate as if it had infinite memory, and is often considered more cost effective than
purchasing additional RAM.

3
Rafael Nobleza Masallo CS501M
Assignment 9-29-17
Virtual memory permits software to use additional memory by utilizing the hard disc drive
(HDD) as temporary storage. Most central processing units (CPUs) provide memory
management units (MMUs) that support virtual memory. The MMU supports the page tables
that are used to transform the real and virtual addresses located in memory and on the HDD.

An OS that uses virtual memory frees up space by transferring data from the HDD which is not
immediately required. When the data is needed, it is copied back to the HDD. When all RAM is
being used, VM swaps data to the HDD and then back again. Thus, VM allows a larger total
system memory; however, complicated code writing is required.
Virtual memory is a method of
using the computer hard drive to provide
extra memory for the computer. Segments
of memory are stored on the hard drive
known as pages. When a segment of
memory is requested that is not in
memory it is moved from the virtual
memory to an actual memory address.
In the picture to the left is the Virtual
Memory settings window in Microsoft
Windows. From this window you can
view information such as the total paging
file size for all drives, enable and disable
the paging file, and define a custom size
of the page file.

4
Rafael Nobleza Masallo CS501M
Assignment 9-29-17
Significance of virtual memory?

Multitasking

One important use of virtual memory is multitasking. When a computer user opens multiple
programs at once, the data for these programs must be stored in memory for quick access. The
more programs are open, the more memory is needed. When the computer's physical memory is
full, the excess data is stored in virtual memory.

Large Programs

In addition to multitasking, virtual memory allows programmers to create larger and more
complex applications. When these programs are running, they occupy physical memory as well
as virtual memory.

Flexibility

If computers only relied on memory chips, far less memory would be available and the
usefulness of many software programs would be severely limited. Even though virtual memory is
slower, it is still useful because it greatly expands a computer's functionality.

Changing Times

When virtual memory was first created, solid-state memory chips were much smaller and more
expensive. Today's memory chips can store many gigabytes of data at very low cost. As memory
chips continue to grow in capacity and prices fall, virtual memory is may be less useful in the
future.

The purpose of virtual memory is to enlarge the address space, the set of addresses a
program can utilize. For example, virtual memory might contain twice as many addresses as
main memory. A program using all of virtual memory, therefore, would not be able to fit in main
memory all at once. Nevertheless, the computer could execute such a program by copyinginto
main memory those portions of the program needed at any given point during execution.

To facilitate copying virtual memory into real memory, the operating system divides
virtual memory into pages, each of which contains a fixed number of addresses. Each page is
stored on a diskuntil it is needed. When the page is needed, the operating system copies it from
disk to main memory, translating the virtual addresses into real addresses.

The process of translating virtual addresses into real addresses is called mapping. The
copying of virtual pages from disk to main memory is known as paging or swapping.
5
Rafael Nobleza Masallo CS501M
Assignment 9-29-17
The need of virtual memory arises from the fact that physical memory is limited. How will the CPU
execute program instructions if the program binary is large to be loaded into DRAM ?

System may be running on low physical memory that is not allowing the loader to load all the
pages of executable into memory. Even if the processes currently sitting in memory are swapped
out, physical memory requirements of the new process are not met.

The question is how do we execute programs in such case ?

This is where virtual (aka unreal) memory comes into place. How about not really relying
on the fact that in order to run a program, you need to load all of its pages into memory ? This
leads to demand paging. Demand Paging is a technique to implement virtual memory.

To begin the execution of program (aka process), the entire program need not be
loaded into memory. As and when CPU makes references, and if the physical pages
corresponding to those references are not present in DRAM, then system generates a page fault
- the physical page of memory that needs to be accessed is not resident in memory.

OS needs to handle this page fault. The faulting process will be put to sleep, and page
fault handler will bring in the desired page from disk into memory, and update the page table.
Note that all of this is done _only_ after address translation has already happened via MMU ; i.e
logical address generated by CPU is translated to physical address through the page table.

How does demand paging help here or how is it related to virtual memory?

The fundamental concept is that the entire program need not be loaded into memory for the CPU
to begin execution. Pages are loaded as per demand. There might be certain code sections
which may never be executed. Thus there is no benefit of loading them and simply keeping in
memory. As and when the physical references are made, if the desired page happens to be in
memory, its well and good. If not, then the process will page fault, and the page will be loaded
into memory (this may result in swapping out of an existing page back to disk). The faulting
instruction will then be restarted.

Demand paging allows us to run programs in memory even if there is insufficient memory
to begin with. This is the principle behind virtual memory. It is unreal memory. Each process
has its own contiguous virtual address space comprising of code, data, bss, heap, and stack
regions. Virtual address space is contiguous range of logical addresses (the one generated by
CPU). For example we are running a program with memory requirements of 6GB on a 32 bit
system. The virtual address space range will allow us to address 4GB of physical memory at
max. It would be lesser than 4GB because some OS kernel code and its data structures.

6
Rafael Nobleza Masallo CS501M
Assignment 9-29-17
Virtual Memory Illustration

Hardware support for virtual memory


As covered in the section called The TLB, the processor hardware provides a lookup-
table that links virtual addresses to physical addresses. Each processor architecture defines
different ways to manage the TLB with various advantages and disadvantages.
The part of the processor that deals with virtual memory is generally referred to as the Memory
Management Unit or MMU
x86-64 XXX
Itanium- The Itanium MMU provides many interesting features for the operating system to
work with virtual memory.
Address spaces
The section called Flushing the TLB introduced the concept of the address-space ID to
reduce the overheads of flushing the TLB when context switching. However, programmers often
use threads to allow execution contexts to share an address space. Each thread has the same
ASID and hence shares TLB entries, leading to increased performance. However, a single ASID
prevents the TLB from enforcing protection; sharing becomes an "all or nothing" approach. To
share even a few bytes, threads must forgo all protection from each other (see also the section
called Protection).

7
Rafael Nobleza Masallo CS501M
Assignment 9-29-17
Figure 6.7. Illustration Itanium regions and protection keys

The Itanium MMU considers these problems and provides the ability to share an address
space (and hence translation entries) at a much lower granularity whilst still maintaining
protection within the hardware. The Itanium divides the 64-bit address space up into 8 regions,
as illustrated in Figure 6.7, Illustration Itanium regions and protection keys. Each process has
eight 24-bit region registers as part of its state, which each hold a region ID (RID) for each of the
eight regions of the process address space. TLB translations are tagged with the RID and thus
will only match if the process also holds this RID, as illustrated in Figure 6.8, Illustration of
Itanium TLB translation.

8
Rafael Nobleza Masallo CS501M
Assignment 9-29-17
Figure 6.8. Illustration of Itanium TLB translation

Further to this, the top three bits (the region bits) are not considered in virtual address translation.
Therefore, if two processes share a RID (i.e., hold the same value in one of their region registers)
then they have an aliased view of that region. For example, if process-A holds RID 0x100 in
region-register 3 and process-B holds the same RID 0x100 in region-register 5 then process-A,
region 3 is aliased to process-B, region 5. This limited sharing means both processes receive the
benefits of shared TLB entries without having to grant access to their entire address space.

9
Rafael Nobleza Masallo CS501M
Assignment 9-29-17
Protection Keys
To allow for even finer grained sharing, each TLB entry on the Itanium is also tagged
with a protection key. Each process has an additional number of protection key registers under
operating-system control.
When a series of pages is to be shared (e.g., code for a shared system library), each page is
tagged with a unique key and the OS grants any processes allowed to access the pages that key.
When a page is referenced the TLB will check the key associated with the translation entry
against the keys the process holds in its protection key registers, allowing the access if the key is
present or otherwise raising a protection fault to the operating system.
The key can also enforce permissions; for example, one process may have a key which grants
write permissions and another may have a read-only key. This allows for sharing of translation
entries in a much wider range of situations with granularity right down to a single-page level,
leading to large potential improvements in TLB performance.
Itanium Hardware Page-Table Walker
Switching context to the OS when resolving a TLB miss adds significant overhead to the
fault processing path. To combat this, Itanium allows the option of using built-in hardware to
read the page-table and automatically load virtual-to-physical translations into the TLB. The
hardware page-table walker (HPW) avoids the expensive transition to the OS, but requires
translations to be in a fixed format suitable for the hardware to understand.
The Itanium HPW is referred to in Intel's documentation as the virtually hashed page-table
walker or VHPT walker, for reasons which should become clear. Itanium gives developers the
option of two mutually exclusive HPW implementations; one based on a virtual linear page-table
and the other based on a hash table.
It should be noted it is possible to operate with no hardware page-table walker; in this case each
TLB miss is resolved by the OS and the processor becomes a software-loaded architecture.
However, the performance impact of disabling the HPW is so considerable it is very unlikely any
benefit could be gained from doing so
Virtual Linear Page-Table
The virtual linear page-table implementation is referred to in documentation as the short
format virtually hashed page-table (SF-VHPT). It is the default HPW model used by Linux on
Itanium.

The usual solution is a multi-level or hierarchical page-table, where the bits comprising the
virtual page number are used as an index into intermediate levels of the page-table (see the
section called Three Level Page Table). Empty regions of the virtual address space simply do
10
Rafael Nobleza Masallo CS501M
Assignment 9-29-17
not exist in the hierarchical page-table. Compared to a linear page-table, for the (realistic) case of
a tightly-clustered and sparsely-filled address space, relatively little space is wasted in overheads.
The major disadvantage is the multiple memory references required for lookup.

Figure 6.9. Illustration of a hierarchical page-table

11
Rafael Nobleza Masallo CS501M
Assignment 9-29-17

Você também pode gostar