Review: strenghts of the virtual memory (paging) technique

  • The virtual memory (paging) technique allows a program to be executed (run) by

    1. Placing only the necessary portions of the program in memory

    2. Portions of the program can be placed anywhere in memory

Paging: a Virtual Memory technique

  • The running program is divided into units called pages.
    Example: (I will use 1 K bytes page size in examples):

    • Nowadays, computers use 8 K - 64 K page sizes

Paging: a Virtual Memory technique

  • The main memory is divided into units called frames.
    Note: the frame size is always equal to the page size    (I use 1 K bytes in examples)

    • You can place any page of a running program (stored on the disk) inside any frame in the memory

Paging: a Virtual Memory technique

  • The memory must contain all the pages that are necessary to execute the current instruction:

    1. The page that contains the current instruction

    2. The page(s) that contains the memory operands used by the current instruction

Overview of the Virtual Memory technique

  • Program (= virtual) addresses are mapped (= translated) to memory addresses using a dynamic mapping function:

    • The Address Mapping is dynamic because pages can be placed in different frames at different times...

  • The Virtual Memory Technique (that uses pages) is known as paging

Terminology used in paging

  • The program addresses are called virtual addresses in paging
    The memory addresses are called physical addresses in paging

  • The mapping between virtual addresses and physical addresses in paging is implemented using a page table (discussed later)

What a paging system looks like...

  • A subset of the pages of the program are stored in frames in computer memory
    Example: only the pages #1 and #4 are stored in frames #3 and #1, respectively

  • It is not required to store all the program pages in memory
    The pages needed are pages containing the current instruction and its operands

Page hit

  • The page table will map a program (= virtual) address in a page that is present in memory to its memory (= physical) address:

  • This situation is called a page hit
     

page fault

  • The page table will map a program (= virtual) address in a page that is absent in memory to INVALID

  • This situation is called a page fault (discussed later)

How to get the page number from a program address

  • Assume program addresses used by the computer are 32 bits binary numbers
     If  the page size = 1 Kbytes, the program addresses are divided as follows:

  •  For  page size = 1 Kbytes, the most significant 22 bits in program address is the page number --- e.g. all program addresses in page 1 starts with 0000000000000000000001

How to get the offset in the page from a program address

  • Furthermore:  for  page size = 1 Kbytes (= 210 bytes) , the last 10 bits in the program address is the offset in the page:

  • Program address 1 000000000000000000000000000001 has offset 1 (00000001) in page 0
    Program address 1025 000000000000000000010000000001 has offset 1 (00000001) in page 1

How to get the frame number of a memory address

  • Suppose the memory has 4 K bytes (= 212) of memory (address bus has 12 wires)
     Using  frame size = 1 Kbytes, this computer has 4 frames:

  •  With  memory = 4 Kbytes and frame size = 1 Kbytes, the most significant 2 bits in memory address is the frame number --- e.g. memory addresses in frame 1 starts with 01

How to get the offset in the frame of a memory address

  • Furthermore:  with  frame size = 1 Kbytes (2 10 bytes) , the last 10 bits in the memory address is the offset in the frame:

  • Memory address 1 000000000001 has offset 1 (00000001) in frame 0
    Memory address 1025 010000000001 has offset 1 (00000001) in frame 1 (and so on)

Important property in the mapping from a program address to the memory address

  • In paging, a program address is divided into 2 parts:   a page number and a offset

                        <----------- 64 bits binary number ---------->
                        <------- page # -------><-- offset in page -->
      Program address = ppppppppppppppppppppppppyyyyyyyyyyyyyyyyyyyyyy


  • A memory address is also divided into 2 parts:   a frame number and a offset

                        <--- 32-36 bits binary number --->
                        <- frame # -><-- offset in page -->
      Memory address  = fffffffffffffyyyyyyyyyyyyyyyyyyyyyy

    Memory addresses in today's computers have 32 − 36 bits (4G − 64G bytes memory)


  • Important property of the paging technique:

    • The offset in the program address and in the corresponding memory address are equal

Important property in the mapping from a program address to the memory address

  • The offset in the program address and in the corresponding memory address are equal

  • This property is the result of using: page size = frame size:

  • Therefore:

    • We only need to map the page number ---> frame number

Mapping virtual (program) addresses to physical (memory) addresses

  • The (dynamic) mapping function only need to map a page number to its frame number:

      virtual (program) address             physical (memory) address
    
      aaaaaaaaaaaaaaaaaxxxxxxxxxxxx  ---->  bbbbbbbbbbbxxxxxxxxxxxxx
      <---- page# ----><- offset ->         <- frame# -><- offset ->
              |                                   ^
              |                                   |
              +-----------------------------------+
    

  • This mapping is accomplished using a page table with the following structure:

Structure of the Page Table

  • Meaning of the fields in the Page Table:

    • Present bit:   1 means the page is currently present in memory and 0 otherwise

    • Dirty bit:   1 means the content of the page has been modified and 0 otherwise

    • Page address on disk = the disk address used to locate the page on disk

    • Frame# = number of the memory frame where the page was stored

      • The Frame# is only valid when the Present bit = 1 !

  • The mapping function will return Page Fault if a page is not present in the memory

Example of the content of a page table

  • Consider this content of the page table:

    (The dirty bits and page addresses on disk are omitted for clarity)

  • The page table contains the following mapping between page#'s and their frame#'s:

        Page #               Frame #
       --------------------------------------------------------
          0      -------->   page fault (not present in memory)
          1      -------->   3          (present in frame #3)
          2      -------->   0          (present in frame #0)
          3      -------->   page fault (not present in memory)
          4      -------->   1          (present in frame #1)
    

Implementing the mapping of virtual (program) addresses to physical (memory) addresses

    • The page# bits are used to select a row in the page table
    • The memory outputs is the frame# bits stored at the selected row

    (If the selected present bit = 0, the MMU outputs Page Fault = 1 - indicating there was a page fault)

DEMO: /home/cs355001/demo/VirtualMemory/paging.e

"Big picture" view of the page table content

  • Suppose a running program has 2 pages present in memory:

    • Page 1 is presently stored in memory frame 3
    • Page 4 is presently stored in memory frame 1

"Big picture" view of the page table content

  • The content of the page table that implements this mapping is:

    • Page 1 is presently stored in memory frame 3
    • Page 4 is presently stored in memory frame 1

Example of the program address ---> memory address mapping operation

  • Consider the previous example where page 1 and 4 of a running program is present in frames 3 and 1, respectively:

Example of the program address ---> memory address mapping operation

  • Suppose the CPU want to fetch the instruction at the program address
    00000000 00000000 00000110 01010101 (e.g.: the PC contains this program address)

Example of the program address ---> memory address mapping operation

  • The memory location that contains the instruction has the memory address
    1110 01010101:

Example of the program address ---> memory address mapping operation

  • Question: how can the CPU fetch the instruction from the memory using the program address 00000000 00000000 00000110 01010101:

Example of the program address ---> memory address mapping operation

  • Answer: the page # 00000000 00000000 000001 is used by the MMU to look up the frame# (3 = 112) in the page table:

Example of the program address ---> memory address mapping operation

  • Answer: the frame# (3 = 112) concatenated with the offset is equal to the memory address 1110 01010101 where the instruction is stored: