6-10 Flashcards

1
Q

What is memory virtualization?

(3)…

A
  • OS virtualizes its physical memory
  • OS provides an illusion of memory space per each process
  • It is as if each process can access the entire memory space
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Memory virtualization benefits:

(3)…

A

• Ease of use in programming
• Memory efficiency in terms of time and space
• The guarantee of isolation for processes as well as OS
- Protection from errant accesses of other processes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Early OSes:
• Load __ in memory
- Poor utilization and efficiency

A

only one process

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
Multiprogramming and Time Sharing: 
Load multiple processes in memory: 
(3)...
Causes an important protection issue: 
...
A
  • Execute one for a short while
  • Switch processes between them in memory
  • Increase utilization and efficiency;

• Errant memory accesses from other processes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Address Space:

The OS creates an __ of physical memory

A

• The address space contains all the information about a running process
- program code, heap, stack and etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
Address Space: 
Code: 
(1)...
Heap: 
(1)...
Stack: 
(2)...
A
1. Code
• Where instructions live
2. Heap
• Dynamically allocate memory
- malloc in C
- new in Java
3. Stack
• Store return addresses or values
• Contain local variables and arguments to functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Virtual Address:
Every address in a running program is __.
OS translates the __ to __.

A

virtual;
virtual address;
physical address

#include [LTS]stdio.h>
#include [LTS]stdlib.h>
// Compile with: gcc -Og -no-pie -o print_address print_address.c
int main(int argc, char *argv[]){
int x = 3; // allocated on the stack frame of main()
printf(“location of code : %p\n”, (void *) main);
printf(“location of heap : %p\n”, (void *) malloc(1));
printf(“location of stack : %p\n”, (void *) &x);
return x; // so compiler doesn’t optimize x away
}

• The output in 64-bit Linux machine:

9/23/2019 CMPU 335 – Operating Systems 8
location of code : 0x4005c7
location of heap : 0x1623670
location of stack : 0x7fff633fbe54

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Memory Virtualization Overview:

(4)…

A

• Just like the CPU, memory is virtualized
• Every address generated by a user program is a virtual address
• Virtual memory provides the illusion of a large private address space to
programs
- For instructions and data
• The OS, with help from the hardware translates each virtual memory
address to an actual physical address
- Protects and isolates processes from each other
- And from the kernel

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Memory Virtualizing with Efficiency and Control:

In memory virtualizing, efficiency and control are attained by __.

A

• E.g., registers, TLB (Translation Look-aside Buffer)s, page-table
• Control means no process is allowed to access any memory but its own

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Address Translation:
Mechanism: __
• Hardware transforms __
• The desired information is actually stored in a __

A

hardware-based address translation;
a virtual address to a physical address;
physical address

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The OS must get involved at key points to set up the hardware
• The OS manages __
• Keeps track of __

A

memory;

which locations are free and which are in use

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Example: Address Translation

void func()
int x = 3000;
x = x + 3; // this is the line of code we are interested in
...

(3)…

A
  • Load a value from memory
  • Increment it by three
  • Store the value back into memory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Example: Address Translation

void func()
int x = 3000;
x = x + 3; // this is the line of code we are interested in

Assembly:

A

128 : movl 0x0(%ebx), %eax ; load 0+ebx into eax
132 : addl $0x03, %eax ; add 3 to eax register
135 : movl %eax, 0x0(%ebx) ; store eax back to mem

• Presume that the address of ‘x’ has been place in ebx register
• Load the value at that address into eax register
• Add 3 to eax register
• Store the value in eax back into memory

5 memory references just to add 3 to a number!:
• Fetch instruction at address 128
• Execute this instruction (load from address 15KB)
• Fetch instruction at address 132
• Execute this instruction (no memory reference)
• Fetch the instruction at address 135
• Execute this instruction (store to address 15 KB)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  • Base and bounds also called __

* Bounds register also called __

A

dynamic relocation;

limit register

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Dynamic (Hardware base) Relocation:
When a program starts running, the OS decides __.

• Sets the base register accordingly
𝑝ℎ𝑦si𝑐𝑎𝑙 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 = __

• Every virtual address must not be greater than __

A

where in physical memory a process should be loaded;

𝑣𝑖𝑟𝑡𝑢𝑎𝑙 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 + 𝑏𝑎𝑠e;

bound and negative:
0 ≤ 𝑣𝑖𝑟𝑡𝑢𝑎𝑙 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 < 𝑏𝑜𝑢𝑛𝑑s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Relocation and Address Translation:
128 : movl 0x0(%ebx), %eax
(2)…

A

• Fetch instruction at address 128 (virtual address):
32896 = 128 + 32𝐾𝐵(𝑏𝑎𝑠𝑒)

• Execute this instruction
- Load from address 15KB (virtual address)
47𝐾𝐵 = 15𝐾𝐵 + 32𝐾𝐵(𝑏𝑎𝑠𝑒)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Two ways to check Bounds:

(2)…

A
  • Either hold the size of the address space or the physical address of the end of the address space
  • Depends on whether you bounds check before or after translation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

The OS must take action to implement base-and-bounds approach
• Three critical junctures:
(3)…

A

• When a process starts running:
- Finding space for address space in physical memory
• When a process is terminated:
- Reclaiming the memory for use
• When context switch occurs:
- Saving and storing the base-and-bounds pair

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

OS Issues: When a Process Starts Running

  • The OS must find __
  • free list : __
A

a room for a new address space;

A list of the range of the physical memory which are not in use

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

OS Issues: When a Process Is Terminated

• The OS must __

A

put the memory back on the free list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

OS Issues: When Context Switch Occurs:
• The OS must __
- In __

A

save and restore the base-and-bounds pair;

process structure or process control block (PCB)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Address Translation Overview:

  1. Extend limited direct execution with address translation
    (1) …
  2. Hardware support is necessary
    (2) …
  3. Base and bounds is efficient and offers protection
    (2) …
A
  1. Extend limited direct execution with address translation
    • Can control each and every memory access from a process
  2. Hardware support is necessary
    • Process has no idea memory references are being translated
    • Part of the memory management unit (MMU) of a processor
  3. Base and bounds is efficient and offers protection
    • Suffers from internal fragmentation due to fixed-sized slots
    • This leads us to segmentation, a generalization of base and bounds
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Inefficiency of the Base and Bound Approach:

(3)…

A
  • Big chunk of “free” space
  • “free” space takes up physical memory
  • Hard to run when an address space does not fit into physical memory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Segment is just a __.

A

contiguous portion of the address space of a particular length
• Logically-different segments: code, stack, heap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Each segment can be placed in __

• __ exist per each segment

A

different parts of physical memory;

Base and bounds

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Address Translation on Segmentation:
𝑝ℎ𝑦𝑠𝑖𝑐𝑎𝑙 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 = __

• The offset of virtual address 100 is __
- The code segment starts at __

A

𝑜𝑓𝑓𝑠𝑒𝑡 + 𝑏𝑎𝑠e;
100;
virtual address 0 in address space

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

The offset of virtual address 4200 is (4200 – 4096) = 104

• The heap segment starts at virtual address __ in address space

A

4096

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Referring to Segment:
Explicit approach:
__

A

• Divide the address space into segments based on the top few bits of virtual address

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Segmentation Fault or Violation:

A

• If an illegal address such as 7KB which is beyond the end of heap is referenced, the OS occurs segmentation fault
- The hardware detects that address is out of bounds

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Referring to Stack Segment:

A
• Stack grows backward 
• Extra hardware support is needed 
- The hardware checks which way the segment grows 
- 1 positive direction 
- 0 negative direction 

• To calculate negative offset

  • Get virtual address segment offset (offset bits from addr.)
  • Subtract offset from maximum segment size to get negative offset
  • Subtract negative offset from base to get physical address
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Support for Sharing:
Segments can be shared between __
(2)…

A

address spaces
• Code sharing is still in use in systems today
• With extra hardware support

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Extra hardware support adds __

• A few more bits per segment to indicate __

A

protection bits;

permissions of read, write, and execute

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Coarse-Grained means __

• e.g., __

A

segmentation in a small number;

code, heap, stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Fine-Grained segmentation allows more __.

(2)…

A

• To support many segments, hardware support with a segment table (kept in memory) is required
• OS could better learn which segments are in use to utilize memory efficiently

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

External Fragmentation:
__.
(2)…

A

little holes of free space in physical memory that make it difficult to allocate new segments
• There are 24KB free, but not in one contiguous segment
• The OS cannot satisfy the 20KB request

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Compaction:
__.
(3)…

A
rearranging the exiting segments in physical memory
• Compaction is costly
- Stop running process
- Copy data to somewhere
- Change segment register value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Segmentation Summary

(5)…

A

• Allows for dynamic relocation
• Better support for large address spaces
- Avoids internal fragmentation
• Segmentation is fast
- Well suited to hardware
- Base and bound registers for each segment
- Optional protection bits for each segment
• External fragmentation is still a major concern
• Still not flexible enough
- E.g., large but sparsely-used heap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Concept of Paging:

Paging…

A

splits up the virtual address space into fixed-sized units called pages
• Compare to segmentation: variable size of logical segments(code, stack, heap, etc.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Concept of Paging: Physical memory is also split into some number of pages called a __.
• Virtual pages and page frames are __ size

A

page frame;

all the same

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Paging Advantages:

(2)…

A
  1. Flexibility
    • Supports the abstraction of address effectively
    • No assumptions on how the heap and stack are used
  2. Simplicity
    • The page in the address space and the page frame are the same size
    • Ease of free-space management
    • Easy to allocate and keep a free list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Paging Advantages:

(2)…

A
  1. Flexibility
    • Supports the abstraction of address effectively
    • No assumptions on how the heap and stack are used
  2. Simplicity
    • The page in the address space and the page frame are the same size
    • Ease of free-space management
    • Easy to allocate and keep a free list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

Paging Example:

(3)…

A
  • 128-byte physical memory with 16 bytes page frames
  • 64-byte address space with 16 bytes pages
  • Page table maps virtual pages to physical pages - Per process data structure that maps virtual pages to physical pages
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

Address Translation:
Two components in the virtual address:
(2)…

A
  • VPN: virtual page number

* Offset: offset within the page

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

What Is In The Page Table?:
The page table is __.
• Simplest form: __.

A

just a data structure that is used to map the virtual address to physical address;
a linear page table, an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

What Is In The Page Table?:
The OS indexes the array by __ and looks up the __.
In addition to the Physical Frame Number, each page table entry has some __.

A

VPN;
page table entry;
status flags

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

Common Page Table Entry Flags:

(5)…

A
  • Valid Bit: Indicating whether the particular translation is valid
  • Protection Bits: Indicating whether the page could be read from, written to, or executed from
  • Present Bit: Indicating whether this page is in physical memory or on disk (swapped out)
  • Dirty Bit: Indicating whether the page has been modified since it was brought into memory
  • Reference Bit (Accessed Bit): Indicating that a page has been accessed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

Valid Bit

A

Indicating whether the particular translation is valid

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

Protection Bits

A

Indicating whether the page could be read from, written to, or executed from

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

Present Bit

A

Indicating whether this page is in physical memory or on disk (swapped out)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

Dirty Bit

A

Indicating whether the page has been modified since it was brought into memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

Reference Bit (Accessed Bit)

A

Indicating that a page has been accessed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q
Example: x86 Page Table Entry
• P: \_\_
• R/W: \_\_
• U/S: \_\_
• A: \_\_
• D: \_\_
• G, PAT, PCD, PWT: \_\_
• PFN: \_\_
A
  • P: present
  • R/W: read/write bit
  • U/S: supervisor
  • A: accessed bit
  • D: dirty bit
  • G, PAT, PCD, PWT: Used for caching
  • PFN: the page frame number
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

A Memory Trace:
• Example: A Simple Memory Access

int array[1000];

for (i = 0; i < 1000; i++)
array[i] = 0;

• Compile and execute:
gcc –o array array.c –Wall –o
./array

• Resulting Assembly code:

A
# i in %eax
# array in %edi
0x1024 movl $0x0,(%edi,%eax,4)
0x1028 incl %eax
0x102c cmpl $1000,%eax
0x1030 jne 0x1024
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

Paging: Too Slow:
• To find a location of the desired PTE, the __ is needed
• For every memory reference, paging requires the OS to perform __
• How do we speed up memory access?
- __
• Next up: Introducing the TLB (Translation Lookaside Buffer)
- A purpose-built cache for page table lookups

A

starting location of the page table;
one extra memory reference;
Caching to the rescue!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

Faster Translations:
Paging can lead to high performance overheads:

A

• Every memory access needs to read an entry in the page table

  • Stored in memory!
  • Doubles the number of memory accesses in the system!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

• Need to make page table lookups go faster. What techniques have we seen before to speed up memory access?
(2)…

A
  1. Caching to the rescue!

2. Smaller and faster memory that holds a portion of the page table for a process

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

Making page table access is so important to overall system performance that a dedicated purpose-built cache is used:
(2)…

A
  • Called Translation Lookaside Buffer (TLB) for historic reasons
  • A better name would be address-translation cache
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

TLB:

(2)…

A
  • Part of the chip’s memory-management unit(MMU)

* A hardware cache of popular virtual-to-physical address translation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

Accessing An Array:

(3)…

A
• 10 4-byte integers starting at virtual address 100 
• 8-bit virtual address space 
• 16 byte pages 
- 4-bits offset 
- 4-bits VPN
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

Temporal Locality

A

An instruction or data item that has been recently accessed will likely be reaccessed soon in the future

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

Spatial Locality

A

If a program accesses memory at address x, it will likely soon access memory near x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

Who Handles The TLB Miss?

(2)…

A
  1. Hardware handles the TLB miss entirely (e.g., X86)

2. OS handles the TLB miss (e.g., ARM)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

Hardware handles the TLB miss entirely (e.g., X86)

(4)…

A
  • Known as a hardware-managed TLB
  • The hardware knows exactly where the page tables are located in memory
  • The hardware finds the correct page-table entry, and extracts the desired translation
  • Updates TLB and retries the instruction
64
Q

OS handles the TLB miss (e.g., ARM)

(2)…

A

• Known as a software-managed TLB
• On a TLB miss, the hardware raises exception (trap handler)
- Code within the OS that is written with the express purpose of handling TLB miss
- Can be a more flexible solution

65
Q

TLB is typically __

A

Entries can go anywhere in the TLB

66
Q

A typical TLB might have (3) entries

A

• Hardware searches the entire TLB in parallel to find the desired translation
• Other bits: valid bits, protection bits, address-space identifier, dirty bit

67
Q

Address Space Identifier:

Provides __ in the TLB

A

an address space identifier (ASID) field

68
Q

Two processes share a page

(3)…

A
  • Process 1 is sharing physical page 101 with Process 2
  • P1 maps this page into the 10th page of its address space
  • P2 maps this page to the 50th page of its address space
69
Q

Sharing of pages is useful as it __.

A

reduces the number of physical pages in use

70
Q

TLB Replacement Policy:

(3)…

A
  1. Least Recently Used (LRU)
  2. First In First Out (FIFO)
  3. Random
71
Q

TLB Replacement Policy:
Least Recently Used (LRU)
(3)…

A
  • Evict an entry that has not recently been used
  • Take advantage of locality in the memory-reference stream
  • Requires keeping track of what was used when
72
Q

TLB Replacement Policy:
First In First Out (FIFO)
(1)…

A

• Evicts the block that was accessed first

73
Q

TLB Replacement Policy:
Random
(3)…

A
  • Evict a random block
  • Simple to implement
  • Can avoid corner-cases (e.g., looping over n+1 pages with n TLB entries)
74
Q

Hybrid Approach: Paging and Segments:

(4)…

A
  1. Use base and bounds registers to reduce the memory overhead of page tables
    • Base holds the physical address of the page table of that segment
    • Bounds register indicates the end of the page table (i.e., how many pages in segment)
  2. Each process has three page tables associated with it
    • When process is running, the base register for each segment contains the physical address of a linear page table for that segment
  3. We still can have page table waste with a large but sparse heap
  4. Arbitrary sized page tables can lead to external fragmentation
75
Q

Multi-Level Page Table:

(3)…

A
  • Chop up the page table into pagesized units
  • If an entire page of page-table entries is invalid, don’t allocate that page of the page table
  • To track whether a page of the page table is valid, use a new structure, called page directory
76
Q

A Detailed Multi-Level Example: Page Directory Index:
Divide the page table entries into pages:
(4)…

A
  • Assume 4 byte page table entry
  • Page size is 64 bytes
  • Each page can hold 16 page table entries
  • Need 16 pages to hold all the entries (256/16)
77
Q

A Detailed Multi-Level Example: Page Directory Index:
Page Directory:
(3)…

A
  • One Page Directory Entry (PDE) for each page of the page table
  • Page Directory Index is 4-bits
  • If a PDE is valid, there is a Page Table in memory
78
Q

A Detailed Multi-Level Example: Page Table Index
• If the PDE is valid, we __.
- The Page Table Index is the __.
• If the PDE is not valid, we have __.

A

use the Page Table Index to get the PTE;
offset into the Page Table Pointed to by the PDE;
an illegal memory access

79
Q

More than Two Level : Page Directory

  • If our page directory has 2^14 entries, it spans not one page but __.
  • To remedy this problem, we __.
A

128;

build a further level of the tree, by splitting the page directory itself into multiple pages of the page directory

80
Q

Multi-level Page Tables: Advantage & Disadvantage:
Advantages:
(2)…

A
  • Only allocates page-table space in proportion to the amount of address space you are using
  • The OS can grab the next free page when it needs to allocate or grow a page table
81
Q

Multi-level Page Tables: Advantage & Disadvantage:
Disadvantages:
(2)…

A
  • Multi-level table is a small example of a time-space trade-off
  • Complexity
82
Q

Inverted Page Tables:

(3)…

A
  1. Keeping a single page table that has an entry for each physical page of the system
    • PID, VPN, Protection bits
  2. The entry tells us which process is using this page, and which virtual page of that process maps to this physical page
  3. Steps in translation:
  4. Hash PID and VPN to get HAT index
  5. Lookup a Physical Frame Number in the HAT
  6. Look in the inverted page table entry to see if the PID and VPN match
  7. If they don’t match, follow the pointer to the next link
83
Q

Inverted Page Tables:
Steps in translation:
(4)…

A

Steps in translation:

  1. Hash PID and VPN to get HAT index
  2. Lookup a Physical Frame Number in the HAT
  3. Look in the inverted page table entry to see if the PID and VPN match
  4. If they don’t match, follow the pointer to the next link
84
Q

Beyond Physical Memory: Mechanisms:
How can we maintain the illusion of a large virtual address space?:
(2)…

A
  • Virtual address space can be much larger than physical memory
  • Support multiprogramming
85
Q

Beyond Physical Memory: Mechanisms:
Add a new level to our memory hierarchy:
(2)…

A
  • A place for the OS to stash away portions of address space that currently aren’t in great demand
  • In modern systems, this role is usually served by a hard disk drive
86
Q

Swap Space:

(2)…

A
  • Reserve some space on the disk for moving pages back and forth
  • OS reads and writes the swap space in page-sized units
87
Q

Present Bit:

(3)…

A

• Need some machinery higher up in the system in order to support swapping pages to and from the disk
• Add a present bit to the PTE
- When the hardware looks in the PTE, it may find that the page is not present in physical memory
• Accessing a page that is not in physical memory is known as a Page Fault

88
Q

The Page Fault:

A page fault occurs by __.

A

accessing a page that is not in physical memory
• If a page is not present and has been swapped disk, the OS needs to swap the page into memory in order to service the page fault

89
Q

The Page Fault:

How does the OS know where to look for the page on disk?

A
  • Store the disk address in the PTE

* While OS is fetching the page, the process is blocked (allowing another process to run)

90
Q

The Page Fault:

After the OS fetches the page from disk, __.

A

update the page table to mark page as present
• Update the PTE with the PFN of the page now in memory
• Optionally update the TLB

91
Q

Swapping: What If Memory Is Full?:

(3)…

A

• The OS needs to make room for the new page
• The process of picking one or more pages to swap out is known as page-replacement policy
• Getting this policy right is important to overall performance
- Hard drives are 10,000 to 100,000 times slower than memory!

92
Q

Page Fault Control Flow Summary:
After a TLB miss:
(3)…

A

• Check present and valid bits of the PTE

  • If valid bit is 0, seg fault
  • If present bit is 1, grab the PFN from the PTE, place in TLB and restart the instruction
  • If present bit is 0, page must be swapped in from disk (page fault)
93
Q

Page Fault Control Flow Summary:
Servicing a Page Fault
(4)…

A
  • Find an available physical frame for the soon-to-be-swapped-in page
  • If there is not a physical frame available (physical memory is full) need to wait for page replacement algorithm to run and kick some pages out
  • OS issues I/O request to read in page from swap space
  • When I/O completes, update the page table and restart the instruction
94
Q

Our current model of page replacement:

(2)…

A
  • OS waits until memory is entirely full and only then replaces a page to make room for some other page
  • This is a little bit unrealistic, and there are many reasons for the OS to keep a small portion of memory free more proactively
95
Q

When to start and stop evicting pages?:

(3)…

A
  • High Watermarks (HW) and Low Watermarks (LW)
  • Fewer than LW available pages, a background process (Page Daemon) starts evicting pages
  • Page Daemon stops when HW number of free pages are available
96
Q

Swapping is the mechanism that allows us to __.

A

support accessing more memory than is physically present in the system

97
Q

Swapping needs extra complexity in the page table:

(2)…

A
  • Present bit (is page in memory or not)

* Location of page on disk if not in memory

98
Q

The page fault handler runs when a page is not present in memory:
(2)…

A
  • Transfers the page from disk to physical memory

* May have to replace some pages in memory to make room for the swapped in page

99
Q

Memory pressure forces the OS to start __.

A

paging out pages to make room for actively-used pages

100
Q

Deciding which page to evict is encapsulated within the __.

A

replacement policy of the OS

101
Q

Cache Management:

(2)…

A
  1. Goal in picking a replacement policy for this cache is to minimize the number of cache misses
  2. The number of cache hits and misses let us calculate the average memory access time (AMAT)
    • Cost of accessing memory ~ 100 ns
    • Cost of accessing disk ~ 10 ms (100,000 times slower)

AMAT = (P_Hit_ * T_M_) + (P_Miss_ * T_D_)

102
Q

AMAT = __

A

𝑇_𝑀_ = The cost of accessing memory
𝑇_𝐷_ = The cost of accessing disk
𝑃_𝐻𝑖𝑡_ = The probability of finding the data item in the cache (a hit)
𝑃_𝑀𝑖𝑠𝑠_ = The probability of not finding the data in the cache (a miss)

103
Q

The Optimal Replacement Policy:

(2)…

A
  1. Leads to the fewest number of misses overall
    • Replaces the page that will be accessed furthest in the future
    • Resulting in the fewest-possible cache misses
  2. Not achievable in the real world (we can’t predict the future)
    • However, useful as a comparison point
    • Let’s us know how close we are to optimal
104
Q

Tracing the Optimal Policy:
• Can hold __ pages in memory
• Memory starts out __
• __

A

three;
empty;
Cold (compulsory) misses

105
Q

A Simple Policy: FIFO

(2)…

A

• Pages were placed in a queue when they enter the system
• When a replacement occurs, the page on the tail of the queue(the “First-in” pages) is evicted
- It is simple to implement, but it can’t determine the importance of pages

106
Q

Another Simple Policy: Random

(1)…

A

Picks a random page to replace under memory pressure
• It doesn’t really try to be too intelligent in picking which blocks to evict
• Random does depends entirely upon how lucky it is in its choice

107
Q

Sometimes, Random is as good as __, achieving 6 hits on the example trace

A

optimal

108
Q

LRU

A

Replaces the least-recently-used page

109
Q

Workload Example : The No-Locality Workload:
Each reference is to a __
• Workload accesses __
• Choosing the __

A

random page within the set of accessed pages;
100 unique pages over time;
next page to refer to at random

110
Q

Workload Example : The 80-20 Workload

(2)…

A
  • Exhibits locality: 80% of the references are made to 20% of the pages
  • The remaining 20% of the references are made to the remaining 80% of the pages
111
Q

LRU is more likely to hold onto the __.

A

hot pages

112
Q

Workload Example: The Looping Sequential

(2)…

A

• Refer to 50 pages in sequence
- Starting at 0, then 1, … up to page 49, and then we Loop, repeating those accesses, for total of 10,000 accesses to 50 unique pages
• Worst case scenario for LRU and FIFO

113
Q

To keep track of which pages have been least-and-recently used, the system has to __.

A

do some accounting work on every memory reference
• Need support from hardware
• Can be expensive

114
Q

Approximating LRU:
Requires some hardware support, in the form of a __.
(2)…

A

use bit;
• Whenever a page is referenced, the use bit is set by hardware to 1
• Hardware never clears the bit, though; that is the responsibility of the OS

115
Q

Approximating LRU:
Clock Algorithm
(4)…

A

• All pages of the system are arranged in a circular list
• A clock hand points to some particular page
• When a page fault occurs, the page the hand is pointing to is inspected
- The action taken depends on the use bit
• The algorithm continues until it finds a use bit that is set to 0

116
Q

Meanings?:

  1. Use bit: 0
  2. Use bit: 1
A
  1. Use bit: 0
    Evict the page
  2. Use bit: 1
    Clear use bit and advance hand
117
Q

Clock algorithm doesn’t do as well as __, but it does do better than __.

A

LRU;

approaches that don’t consider history at all

118
Q

The hardware includes a modified bit (a.k.a dirty bit) for each page:
(3)…

A
  • This bit is set at any time a page is written
  • If a page has been modified, it must be written back to disk when evicted
  • If a page has not been modified, the eviction is free
119
Q

Page replacement algorithm might prefer to evict __ pages over __ ones

A

clean;
dirty

E.g., clock algorithm could scan for pages that are both unused and clean to evict first, before evicting unused pages that are dirty

120
Q

Page Selection Policy:

(3)…

A

• The OS has to decide when to bring a page into memory
• For most pages, the OS uses demand paging
- Brings the page into memory when it is accessed
• Other policy is prefetching
- Guess what page is about to be used
- Should only be done where there is a good chance of success

121
Q

Policy for writing pages to disk:
Clustering
(2)…

A
  • Collect a number of pending writes together in memory and write them to disk in one write
  • Perform a single large write more efficiently than many small ones
122
Q

Thrashing:

A

When memory is oversubscribed and the memory demands of the set of running processes exceeds the available physical memory

123
Q

Thrashing:
Admission control
(3)…

A
  • Decide not to run a set of processes
  • Kill off some processes
  • Reduce the set of processes working sets to fit in memory
124
Q

Look at real VM systems:

(2)…

A
  • VAX/VMS

* Linux

125
Q

VMS Virtual Memory System:

(4)…

A
  1. VAX-11 Minicomputer introduced in the late 70’s
  2. VMS was designed to run on a broad range of machines
    • Mechanisms and policies had to work well across a range of systems
  3. 32-bit virtual address, with 512-byte pages
    • How many offset bits?
    • How many VPN bits?
  4. Upper two bits were used as segment bits
    • Used both segments and page tables
    • Hybrid system
126
Q

VAX/VMS Address Space:

(4)…

A
  1. Very small size for pages (512 bytes)
    • Chosen for historical reasons
    • Excessively large linear page table
    - Use segments to help reduce size of tables
  2. Four Segments (determined by bits 31 and 30)
    • 00 – User (P0)
    • 01 – User (P1)
    • 10 – System (S)
    • 11 – Unused
  3. Process space (P0 and P1)
    • Used for each process
    • Base and bounds register for each segment
    - Base holds the address of page table for segment
    - Bounds holds the size of the page table
  4. System space (S)
    • Protected OS code and data
    • Shared across processes
127
Q

Notes on the address space:

(3)…

A
  1. Page 0 is marked as invalid
    • Used to detect NULL pointer dereferences
  2. Kernel virtual address space is part of each users address space
    • Makes it easy for kernel to copy data from a process to its own structures
    • Kernel appears as a protected library to applications
  3. Protection for kernel pages done by adding protection bits in the page table
    • What privilege level the CPU must be at to access a page
128
Q

VAX Page Table Entries:
Page table entry (PTE) in VAX:
(5)…

A
  • Valid bit
  • 4 Protection bits
  • Modified (dirty) bit
  • 5-bits reserved for OS
  • Physical frame number (PFN)
129
Q

VAX Page Table Entries:
Note: no reference bit
- (1)…

A

Replacement algorithm doesn’t have hardware support for knowing which pages are active

130
Q

Controlling Memory Hogs:

(4)…

A
  1. Developers of VMS were concerned about programs that use a lot of memory, making it hard for other programs to run
  2. LRU is a global policy
    • Doesn’t share memory fairly among processes
  3. Solution: segmented FIFO replacement policy
    • Each process has resident set size (RSS)
    - The maximum number of pages it can keep in memory
    - Pages are kept on a FIFO list
    - When a process exceeds its RSS, the “first-in” page is evicted
  4. Problem: FIFO doesn’t perform very well
131
Q

Controlling Memory Hogs:
Solution: segmented FIFO replacement policy
• Each process has resident set size (RSS)
(3)…

A
  • The maximum number of pages it can keep in memory
  • Pages are kept on a FIFO list
  • When a process exceeds its RSS, the “first-in” page is evicted
132
Q

Second-chance Lists:

(5)…

A
  1. To improve FIFO’s performance, VMS uses second-chance lists
  2. A global list where pages are placed before getting evicted from memory
    • Clean-page free list
    • Dirty-page free list
  3. When a process exceeds its RSS
    • “First-in” page is removed from its per-process FIFO
    - Placed on global clean or dirty second-chance list depending on whether it has been modified
  4. If another process needs a page, it takes the first free page off the clean list
  5. If a process faults on one of its pages in the second-chance list
    • Reclaims that page from either the free or dirty list
    • Avoids costly disk access
133
Q

Page Clustering:

(2)…

A
  1. VMS groups large batches of pages together from the global dirty list and writes them out to disk together
    • Optimization to help overcome the small page size in VMS
    • Amortizes the cost of writing one page across the cluster of pages
  2. After the pages are written out to disk, we can mark them clean
134
Q

Demand Zeroing of Pages:
For security purposes, the OS should __.
Otherwise, __.

A

zero out any new pages added to a processes virtual address space;

Could leak encryption keys, for example

135
Q

Demand zeroing defers __.

A

the zeroing out of a page until it is referenced

136
Q

Demand Zeroing of Pages:
Example: adding a page of memory to the heap of a process:
(3)…

A

• When a page is added to the page table, it is marked as inaccessible (note: this is different than invalid)
- Physical page is not allocated at this time
• If the process accesses the page, a trap into the OS takes place
- OS then finds a physical page, zeros it, and maps it the processes virtual address space (by updating the PTE)
• If the process never accesses that page, no work is done

137
Q

Copy-on-write (COW):

Optimization for __.

A

copying a page from one address space to another

138
Q

Copy-on-write (COW):
Instead of copying the page:
(3)…

A
  1. Mark the physical page as read only (copy-on-write)
  2. Have the virtual page in both address spaces map to the (read only) physical page
    • If both pages never write to the page, no further work is done
  3. If one of the address spaces does try to write to the page
    • Trap into the OS, which sees the page marked copy-on-write
    • Then the OS can allocate a new page, fill with data, add to the PTE
139
Q

Copy-on-write (COW):

__ can be marked COW for many processes

A

Shared libraries

140
Q

Copy-on-write (COW):
Helpful for
(2)…

A

fork() and exec();
• fork() creates an exact copy, but usually is immediately overwritten by exec()
• Copy-on-write prevents this needless copying

141
Q

The Linux Virtual Memory System:

(3)…

A
  1. Focus on Linux Intel x86
  2. Address Space divided into kernel and user space
    • Each user process has own userspace
    • Kernel region same across processes
  3. Kernel region is protected, divided into logical and virtual regions
    • Logical – mapped directly to physical memory
    - 0xC0000000 -> 0x00000000
    - Translation is trivial
    - Contiguous regions in logical memory are contiguous in physical memory
    • Virtual – map to any physical page
    - Easy to allocate
    - Used for large buffers where finding large chunk of physical memory is challenging
142
Q

Address Space divided into kernel and user space:

(2)…

A

• Each user process has own userspace • Kernel region same across processes

143
Q

Kernel region is protected, divided into logical and virtual regions:
(2)…

A
  1. Logical – mapped directly to physical memory
    • 0xC0000000 -> 0x00000000
    • Translation is trivial
    • Contiguous regions in logical memory are contiguous in physical memory
  2. Virtual – map to any physical page
    • Easy to allocate
    • Used for large buffers where finding large chunk of physical memory is challenging
144
Q

Page Table Structure:

(4)…

A
  1. Hardware managed, multi-level page table
  2. One page table per process
    • OS sets up mappings in its kernel memory
    • Points a privileged register at the start of the page directory
    • Hardware handles the rest
  3. OS gets involved at process creation, deletion, and context switches
  4. X86-64 (64-bit addresses)
    • Four-level page table, 4k page size
    • Only the bottom 48 bits are currently used
145
Q

Large Page Support:

(5)…

A
  1. X86 allows for multiple page sizes in hardware
    • 2-MB and even 1-GB pages
  2. Reduces number of mappings in the page table
  3. More Importantly, it reduces the pressure on the TLB
    • Some applications spend 10% of their system servicing TLB misses
  4. Implemented in Linux incrementally
    • Applications explicitly request memory with large pages via mmap()
    • In recent versions of Linux have added transparent large page support
    • Automatically looks for opportunities to allocate 2 MB pages without application modification
  5. Issues
    • Larger pages can lead to internal fragmentation
146
Q

Page Cache:

(4)…

A
  1. Since hard drives are so slow, Linux uses aggressive caching to keep popular data items in memory
  2. Page cache keeps pages in memory from three sources
    • Memory-mapped files
    • File data
    • Heap and stack pages
  3. Page cache keeps track if entries are clean or dirty
    • Dirty pages are periodically written back to disk by background threads
    • After a certain period of time or if too many pages are dirty
  4. How does Linux decide which pages to evict?
    • 2Q replacement algorithm
147
Q

• Page cache keeps pages in memory from three sources:

(3)…

A
  • Memory-mapped files
  • File data
  • Heap and stack pages
148
Q

Page cache keeps track if entries are clean or dirty:

(2)…

A
  • Dirty pages are periodically written back to disk by background threads
  • After a certain period of time or if too many pages are dirty
149
Q

2Q Replacement Algorithm:

(3)…

A
  1. Design goal: LRU-like performance without the penalty for some common access patterns
    • Reading through a large file can kick every other file out of memory
    • If you just read through the file once, those pages are never referenced again
  2. Keep two lists and divide memory between them
    • When a page is accessed for the first time it is put on the inactive list
    • When it is re-referenced, the page is promoted to the active list
    • When needed, pages are evicted from the inactive list
    • Pages are periodically moved from the bottom of the active list to the inactive list
    • Active list is about 2/3 the total page cache size
    • LRU is a approximated by an algorithm similar to the clock replacement algorithm
  3. Handles the case of cyclic large-file access
    • Pages that are never re-referenced are confined to the inactive list and won’t evict pages from the active list
150
Q

2Q Replacement Algorithm:
Design goal:
(2)…

A

LRU-like performance without the penalty for some common access patterns
• Reading through a large file can kick every other file out of memory
• If you just read through the file once, those pages are never referenced again

151
Q

2Q Replacement Algorithm:

(4)…

A

• When a page is accessed for the first time it is put on the inactive list
• When it is re-referenced, the page is promoted to the active list
• When needed, pages are evicted from the inactive list
• Pages are periodically moved from the bottom of the active list to the inactive list
- Active list is about 2/3 the total page cache size
- LRU is a approximated by an algorithm similar to the clock replacement algorithm

152
Q

Security and Buffer Overflows:
How to protect from buffer overflow attacks?
(2)…

A
  • Prevent execution of any code found within the stack

* NX bit (AMD) and XD bit (Intel) prevents execution from any page that has this bit set in the PTE

153
Q

Security and Buffer Overflows:

Return-oriented programming (ROP) strings together…

A

pieces of existing code (called gadgets)
• Mitigate by performing Address space layout randomization (ASLR)
- OS randomizes the placement of code, heap, and stack each time a program is run
- Makes it almost impossible to predict the addresses of the gadgets in memory

154
Q

Security and Buffer Overflows:
Kernel address space layout randomization (KASLR):

A

Kernel is placed at a random location

155
Q

Meltdown and Spectre:

(4)…

A
  1. These attacks reveal memory by taking advantage of speculative execution done by the processor
    • The CPU guesses which instructions will soon be executed in the future
    • Starts executing them ahead of time
    • Correct guesses mean the program runs faster
    • Wrong guesses are undone by the CPU by restoring state (registers)
  2. However, state is not fully restored
    • E.g., caches, branch predictors
  3. These subtle changes in state can make vulnerable the contents of memory
    • Even protected memory
  4. Can’t disable speculative execution, programs will run much much slower
156
Q

Meltdown and Spectre:
These attacks reveal memory by taking advantage of speculative execution done by the processor
(4)…

A
  • The CPU guesses which instructions will soon be executed in the future
  • Starts executing them ahead of time
  • Correct guesses mean the program runs faster
  • Wrong guesses are undone by the CPU by restoring state (registers)
157
Q

Kernel Page Table Isolation (KPTI):

(5)…

A

• Remove as much of the kernel address space from each user process
• Have a separate kernel page table for most kernel data
• Kernel code and data structures are no longer mapped into each process
• When switching into the kernel, a switch to the kernel page table is needed
- The cost is performance
- Switching page tables is costly
• Solves some, but not all of the problems of speculative execution