


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



































CSCI 3753 (^1)
CSCI 3753 (^2)
CSCI 3753 (^4)
Virtual memory provides a great deal of flexibility when managin g memory. It allows multiple processes that are all using the same data to only keep one copy in memory and share it, even when it exists in different parts of their address spaces. Virtu al addresses mean that a given process references memory independently of all other processes; it's up to the OS whether different processes can see each -other's writes to memory. 32 bit architectures have a 4GB address space, but computers usually don't have this much memory. Virtual memory allows a process to act as if it had the entire 4GB of memory, if not in the actual size of data, then at least in the range of addresses it can use. Using more memory th an one actually has means that portions of memory need to be stored somewhere else (disk), and brought into memory as needed. This behavior is called swapping, as memory contents are swapped to and from disk.
CSCI 3753 (^5)
Because each process has its own virtual memory mappings, proces ses can share data; two or more processes have virtual memory mappings to the physical addr ess at which the data resides. On one hand, this means that processes can communicate through the contents of the memory. More importantly, it means that even if every process is using a piece of data (say, libc.so), only one copy of it need be residing in physical memory at any given time. Without virtual memory, there would either have to be multiple copies, o r libc.so would have to reside at the same address in every process.
CSCI 3753 (^7)
As the kernel can create virtual mappings as necessary, it can c reate mappings that define a memory space larger than physical memory. Take, for example, the standard X server, which can usually be over 128MB in size when including libraries, exec utable, and data. However, not all of this is needed at any time. The kernel keeps the data that's needed in physical memory, while unused data (library code, etc.) doesn't have to be.
Since the address space can be much larger than physical memory, or much larger than what a program uses, the program can have a sparse address space; its a reas of valid memory can be widely spaced.
CSCI 3753 (^8)
0006A000 24K read/write/exec tcsh
FF3DC000 8K read/write/exec ld.so. FFBE8000 32K read/write/exec [ stack ]
FF3B0000 120K read/exec ld.so.
FF390000 8K read/exec libdl.so.
FF386000 16K read/write/exec libsocket.so.
FF370000 32K read/exec libsocket.so.
FF350000 8K read/write/exec libmp.so.
FF340000 16K read/exec libmp.so.
FF330000 16K read/exec libc_psr.so.
FF30E000 40K read/write/exec libnsl.so.
FF280000 512K read/exec libnsl.so.
FF232000 32K read/write/exec libc.so.
FF180000 656K read/exec libc.so.
00070000 424K read/write/exec [ heap ]
00010000 304K read/exec tcsh
We return to the example of tcsh. As one can see, the executable and heap have a large space between themselves and the libraries. Also, if one examines the addresses carefully, one can see that there are unmapped addresses between different memory m appings. For example, although the data segment of libmp is 8K, the starting address of the text of libsocket is 128K away.
CSCI 3753 (^10)
We'll talk about three ways virtual memory can be organized and handled: by segments, by pages, or by paged segments. Paged segmentation is what's used i n UNIX. The actual distinctions between these mechanisms used to be based on hardwa re, but now it's mostly achieved in software.
CSCI 3753 (^11)
Virtual Address Space Physical Address Space
One method of handling virtual memory, called segmentation, is b ased on an outdated programming technique involving structures called "overlays." Pr ogrammers could define overlays, which would be brought in and out of memory as needed. If a program were larger than the available memory, then the program would move overlays in and out of memory, so that the needed parts of the program could be kept in memory at any given time.
Segmentation uses this idea by allowing a program to define vari able sized segments of memory. Each segment is brought in or moved out of memory as a w hole. Of course, there only has to be one resident copy of a segment that more than one process uses. In addition, using segments allows the kernel to reduce external fragmentatio n. In the example above, each memory mapping has a small amount of free space around it. While this is good for preventing buffer overruns from clobbering other memory, it causes memory f ragmentation; there are many small areas of memory that can't be used and are therefore wasted. The virtual mappings, however, can coalesce the chunks (segments) to provide fewer, la rger areas of free memory. In the above example, another medium-sized fragment could be placed in the larger free area.
CSCI 3753 (^13)
Paged segmentation (what's used in UNIX) merges these two ideas. The entire address space is broken up into pages. Regions of memory (on the granularity of a page) can be defined as segments. Segments can be of variable size.
Combining the two approaches allows variable size areas of memor y to be mapped (with only one copy of the data, instead of a copy per page), while allowi ng memory to be handled for swapping in terms of pages. Having a single copy of segment metainformation (permissions, etc.) can make bookkeeping much easier. The paging allows more e fficient use of memory; if only one small part of a library is needed, then only that page will be resident in memory. Also, since there's a central place for the mapping data (the segment) , it's much easier to determine things about the area as a whole. For example, how many pages of the segment have been brought into memory.
The distinction between segments and pages has historically been a hardware one. For example, the Intel x86 series of processors have paging and segm entation. The architecture allows four segments to be defined at any time. Generally, these aren't used in the sense one uses segments in a process; in Linux, for example, only two segm ents are used, one defined as kernel memory, other as the rest (user) memory.
CSCI 3753 (^14)
Virtual Page Physical Page
The TLB, or translation look -aside buffer, is a piece of hardware required for high performance virtual memory. It's generally a fully associative c ache that caches entries of a specific format. The entries contain a virtual page number, a ph ysical page number, then metadata such as access permissions (read/write/exec) and whethe r the entry is valid (useful for when the cache is initially empty).
A TLB allows a memory access to a virtual address to execute as quickly as an access to a physical address. The architecture can be told to use either one or the other (by setting a bit in a register somewhere, for example). Physical accesses behave norma lly. Virtual accesses go through the TLB. The hardware searches the TLB for an entry that matches the virtual page accessed. It then uses that entry to translate the virtual addre ss to a physical address.
CSCI 3753 (^16)
1111100111010110010
0000000000010101100
1101000110100
0000000000010101100 1101000110100
0x000169a
0xf9d65a
Virtual Address
Physical Address
TLB entry
Here's an example of a memory access. The user program accesses the memory at virtual address 0xf9d65a34. The address is broken up into two parts, its page number, and the offset within that page. We're assuming 8K pages, which means the offse t is 13 bits and the page number is 19 bits.
A corresponding TLB entry is found, and the permissions are acce ptable; the offset into the page is combined with the physical page number, producing the co rresponding physical address, which is what the architecture actually uses when acces sing memory.
CSCI 3753 (^17)
All this is well and good, but what happens when there isn't an entry in the TLB?
If the user accesses memory that has no corresponding entry in t he TLB, the hardware generates an interrupt. This causes the OS kernel to jump to a T LB miss handler. The kernel tries to come up with an appropriate TLB entry and put it into t he TLB. If no such entry exists, the program has accessed an area of invalid memory and is usually terminated.
These TLB entries are generated from kernel memory structures ca lled page tables, which basically contain the TLB entries for every virtual page.
CSCI 3753 (^19)
Physical Page (19 bits)
Valid Bit
Read/Write/Exec bits
32 bits
Here's an example page table entry.
CSCI 3753 (^20)
Data
Data
Data
Data
How are these page table entries organized? One way is to have a large table, with as many entries as there are pages. To find a PTE (page table entry) for a given virtual page, one merely indexes into the table based on the virtual page number. The ent ry contains the physical page number, and therefore one can access the data page.