operating systems - virtual memory basics

205
Operating Systems Virtual Memory Basics Peter Lipp, Daniel Gruss 2021-03-04

Upload: others

Post on 10-Apr-2022

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Operating Systems - Virtual Memory Basics

Operating Systems

Virtual Memory Basics

Peter Lipp, Daniel Gruss

2021-03-04

Page 2: Operating Systems - Virtual Memory Basics
Page 3: Operating Systems - Virtual Memory Basics

Table of contents

1. Address Translation

First Idea: Base and Bound

Segmentation

Simple Paging

Multi-level Paging

2. Address Translation on x86 processors

Page 4: Operating Systems - Virtual Memory Basics

Address Translation

Page 5: Operating Systems - Virtual Memory Basics

Address Translation

� OS in control of address translation

� enables number of advanced features

� programmers perspective:

� pointers point to objects etc.

� transparent: it is not necessary to know how memory reference is converted to data

Isolation, IPC, Sharing, Efficient Dynamic Memory, Cache Management, Debugging, Efficient I/O, Memory Mapped
Page 6: Operating Systems - Virtual Memory Basics

Address Translation

� OS in control of address translation

� enables number of advanced features

� programmers perspective:

� pointers point to objects etc.

� transparent: it is not necessary to know how memory reference is converted to data

Isolation, IPC, Sharing, Efficient Dynamic Memory, Cache Management, Debugging, Efficient I/O, Memory Mapped
Page 7: Operating Systems - Virtual Memory Basics

Address Translation

� OS in control of address translation

� enables number of advanced features

� programmers perspective:

� pointers point to objects etc.

� transparent: it is not necessary to know how memory reference is converted to data

Isolation, IPC, Sharing, Efficient Dynamic Memory, Cache Management, Debugging, Efficient I/O, Memory Mapped
Page 8: Operating Systems - Virtual Memory Basics

Address Translation

� OS in control of address translation

� enables number of advanced features

� programmers perspective:

� pointers point to objects etc.

� transparent: it is not necessary to know how memory reference is converted to data

Isolation, IPC, Sharing, Efficient Dynamic Memory, Cache Management, Debugging, Efficient I/O, Memory Mapped
Page 9: Operating Systems - Virtual Memory Basics

Address Translation

� OS in control of address translation

� enables number of advanced features

� programmers perspective:

� pointers point to objects etc.

� transparent: it is not necessary to know how memory reference is converted to data

Isolation, IPC, Sharing, Efficient Dynamic Memory, Cache Management, Debugging, Efficient I/O, Memory Mapped
Page 10: Operating Systems - Virtual Memory Basics

Address Translation - Idea / Overview

Page 11: Operating Systems - Virtual Memory Basics

Address Translation Concepts

� Memory protection

� Memory sharing

� Shared libraries, interprocess communication

� Sparse address space

� Multiple regions for dynamic allocation (heaps/stacks)

Page 12: Operating Systems - Virtual Memory Basics

Address Translation Concepts

� Memory protection

� Memory sharing

� Shared libraries, interprocess communication

� Sparse address space

� Multiple regions for dynamic allocation (heaps/stacks)

Page 13: Operating Systems - Virtual Memory Basics

Address Translation Concepts

� Memory protection

� Memory sharing

� Shared libraries, interprocess communication

� Sparse address space

� Multiple regions for dynamic allocation (heaps/stacks)

Page 14: Operating Systems - Virtual Memory Basics

Address Translation Concepts

� Memory protection

� Memory sharing

� Shared libraries, interprocess communication

� Sparse address space

� Multiple regions for dynamic allocation (heaps/stacks)

Page 15: Operating Systems - Virtual Memory Basics

Address Translation Concepts

� Memory protection

� Memory sharing

� Shared libraries, interprocess communication

� Sparse address space

� Multiple regions for dynamic allocation (heaps/stacks)

Page 16: Operating Systems - Virtual Memory Basics

Address Translation Concepts

� Efficiency

� Flexible Memory placement

� Runtime lookup

� Compact translation tables

� Portability

Page 17: Operating Systems - Virtual Memory Basics

Address Translation Concepts

� Efficiency

� Flexible Memory placement

� Runtime lookup

� Compact translation tables

� Portability

Page 18: Operating Systems - Virtual Memory Basics

Address Translation Concepts

� Efficiency

� Flexible Memory placement

� Runtime lookup

� Compact translation tables

� Portability

Page 19: Operating Systems - Virtual Memory Basics

Address Translation Concepts

� Efficiency

� Flexible Memory placement

� Runtime lookup

� Compact translation tables

� Portability

Page 20: Operating Systems - Virtual Memory Basics

Address Translation Concepts

� Efficiency

� Flexible Memory placement

� Runtime lookup

� Compact translation tables

� Portability

Page 21: Operating Systems - Virtual Memory Basics

Base-Limit or Base and bounds

Page 22: Operating Systems - Virtual Memory Basics

Virtually Addressed Base and Bounds

� Virtual Address: from 0 to an upper bound

� Physical Address: from base to base + bound

� what is saved/restored on a process context switch?

Page 23: Operating Systems - Virtual Memory Basics

Virtually Addressed Base and Bounds

� Virtual Address: from 0 to an upper bound

� Physical Address: from base to base + bound

� what is saved/restored on a process context switch?

Page 24: Operating Systems - Virtual Memory Basics

Virtually Addressed Base and Bounds

� Virtual Address: from 0 to an upper bound

� Physical Address: from base to base + bound

� what is saved/restored on a process context switch?

Page 25: Operating Systems - Virtual Memory Basics
Simple, Fast (2 registers, adder, comparator), Safe, Can relocate in physical memory without changing process, no isolation, no sharing, no growing
Page 26: Operating Systems - Virtual Memory Basics

Segmentation

� Small Change: multiple pairs of base-and-bounds registers

� Segmentation

� Each entry controls a portion of the virtual address space

Page 27: Operating Systems - Virtual Memory Basics

Segmentation

� Small Change: multiple pairs of base-and-bounds registers

� Segmentation

� Each entry controls a portion of the virtual address space

Page 28: Operating Systems - Virtual Memory Basics

Segmentation

� Small Change: multiple pairs of base-and-bounds registers

� Segmentation

� Each entry controls a portion of the virtual address space

Page 29: Operating Systems - Virtual Memory Basics

Segmentation

Page 30: Operating Systems - Virtual Memory Basics

Segmentation

� Segment is a contiguous region of virtual memory

� Each process has a segment table (in hardware)

� Entry in table = segment

� Segment can be located anywhere in physical memory

� Each segment has: start, length, access permission

Page 31: Operating Systems - Virtual Memory Basics

Segmentation

� Segment is a contiguous region of virtual memory

� Each process has a segment table (in hardware)

� Entry in table = segment

� Segment can be located anywhere in physical memory

� Each segment has: start, length, access permission

Page 32: Operating Systems - Virtual Memory Basics

Segmentation

� Segment is a contiguous region of virtual memory

� Each process has a segment table (in hardware)

� Entry in table = segment

� Segment can be located anywhere in physical memory

� Each segment has: start, length, access permission

Page 33: Operating Systems - Virtual Memory Basics

Segmentation

� Segment is a contiguous region of virtual memory

� Each process has a segment table (in hardware)

� Entry in table = segment

� Segment can be located anywhere in physical memory

� Each segment has: start, length, access permission

Page 34: Operating Systems - Virtual Memory Basics

Segmentation

� Segment is a contiguous region of virtual memory

� Each process has a segment table (in hardware)

� Entry in table = segment

� Segment can be located anywhere in physical memory

� Each segment has: start, length, access permission

Page 35: Operating Systems - Virtual Memory Basics

Segmentation

� Segmented Memory has gaps!

� no longer contiguous region - set of regions

� code and data not adjacent - neither in virtual nor in physical address space

� What if: program tries to load data from gap?

� Segmentation Fault (trap into OS)

� correct programs will not generate references outside valid memory

� trying to read or write data that does not exist: bug-indication

Page 36: Operating Systems - Virtual Memory Basics

Segmentation

� Segmented Memory has gaps!

� no longer contiguous region - set of regions

� code and data not adjacent - neither in virtual nor in physical address space

� What if: program tries to load data from gap?

� Segmentation Fault (trap into OS)

� correct programs will not generate references outside valid memory

� trying to read or write data that does not exist: bug-indication

Page 37: Operating Systems - Virtual Memory Basics

Segmentation

� Segmented Memory has gaps!

� no longer contiguous region - set of regions

� code and data not adjacent - neither in virtual nor in physical address space

� What if: program tries to load data from gap?

� Segmentation Fault (trap into OS)

� correct programs will not generate references outside valid memory

� trying to read or write data that does not exist: bug-indication

Page 38: Operating Systems - Virtual Memory Basics

Segmentation

� Segmented Memory has gaps!

� no longer contiguous region - set of regions

� code and data not adjacent - neither in virtual nor in physical address space

� What if: program tries to load data from gap?

� Segmentation Fault (trap into OS)

� correct programs will not generate references outside valid memory

� trying to read or write data that does not exist: bug-indication

Page 39: Operating Systems - Virtual Memory Basics

Segmentation

� Segmented Memory has gaps!

� no longer contiguous region - set of regions

� code and data not adjacent - neither in virtual nor in physical address space

� What if: program tries to load data from gap?

� Segmentation Fault (trap into OS)

� correct programs will not generate references outside valid memory

� trying to read or write data that does not exist: bug-indication

Page 40: Operating Systems - Virtual Memory Basics

Segmentation

� Segmented Memory has gaps!

� no longer contiguous region - set of regions

� code and data not adjacent - neither in virtual nor in physical address space

� What if: program tries to load data from gap?

� Segmentation Fault (trap into OS)

� correct programs will not generate references outside valid memory

� trying to read or write data that does not exist: bug-indication

Page 41: Operating Systems - Virtual Memory Basics

Segmentation

� Segmented Memory has gaps!

� no longer contiguous region - set of regions

� code and data not adjacent - neither in virtual nor in physical address space

� What if: program tries to load data from gap?

� Segmentation Fault (trap into OS)

� correct programs will not generate references outside valid memory

� trying to read or write data that does not exist: bug-indication

Page 42: Operating Systems - Virtual Memory Basics

Shared Memory

� Processes can share segments

� Same start, length, same/different access permissions

� Usage:

� sharing code (shared libraries)

� interprocess communication

� copy on write

Page 43: Operating Systems - Virtual Memory Basics

Shared Memory

� Processes can share segments

� Same start, length, same/different access permissions

� Usage:

� sharing code (shared libraries)

� interprocess communication

� copy on write

Page 44: Operating Systems - Virtual Memory Basics

Shared Memory

� Processes can share segments

� Same start, length, same/different access permissions

� Usage:

� sharing code (shared libraries)

� interprocess communication

� copy on write

Page 45: Operating Systems - Virtual Memory Basics

Shared Memory

� Processes can share segments

� Same start, length, same/different access permissions

� Usage:

� sharing code (shared libraries)

� interprocess communication

� copy on write

Page 46: Operating Systems - Virtual Memory Basics

Shared Memory

� Processes can share segments

� Same start, length, same/different access permissions

� Usage:

� sharing code (shared libraries)

� interprocess communication

� copy on write

Page 47: Operating Systems - Virtual Memory Basics

Shared Memory

� Processes can share segments

� Same start, length, same/different access permissions

� Usage:

� sharing code (shared libraries)

� interprocess communication

� copy on write

Page 48: Operating Systems - Virtual Memory Basics

Copy on Write

Special kind of shared memory (after fork)

� two processes, both running the same program and almost same data

� makes sense not to copy everything

� we just need to be made aware if a process writes to a segment and changes the content

� reading does not present any problems

� how do we know when a process writes to a segment?

→ set segment read only

Page 49: Operating Systems - Virtual Memory Basics

Copy on Write

Special kind of shared memory (after fork)

� two processes, both running the same program and almost same data

� makes sense not to copy everything

� we just need to be made aware if a process writes to a segment and changes the content

� reading does not present any problems

� how do we know when a process writes to a segment?

→ set segment read only

Page 50: Operating Systems - Virtual Memory Basics

Copy on Write

Special kind of shared memory (after fork)

� two processes, both running the same program and almost same data

� makes sense not to copy everything

� we just need to be made aware if a process writes to a segment and changes the content

� reading does not present any problems

� how do we know when a process writes to a segment?

→ set segment read only

Page 51: Operating Systems - Virtual Memory Basics

Copy on Write

Special kind of shared memory (after fork)

� two processes, both running the same program and almost same data

� makes sense not to copy everything

� we just need to be made aware if a process writes to a segment and changes the content

� reading does not present any problems

� how do we know when a process writes to a segment?

→ set segment read only

Page 52: Operating Systems - Virtual Memory Basics

Copy on Write

Special kind of shared memory (after fork)

� two processes, both running the same program and almost same data

� makes sense not to copy everything

� we just need to be made aware if a process writes to a segment and changes the content

� reading does not present any problems

� how do we know when a process writes to a segment?

→ set segment read only

Page 53: Operating Systems - Virtual Memory Basics

Copy on Write

Special kind of shared memory (after fork)

� two processes, both running the same program and almost same data

� makes sense not to copy everything

� we just need to be made aware if a process writes to a segment and changes the content

� reading does not present any problems

� how do we know when a process writes to a segment?

→ set segment read only

Page 54: Operating Systems - Virtual Memory Basics

Fork and Copy on Write

Fork:

� Copy segment table into child

� Mark parent and child segments read-only

� Start child process; return to parent

Parent/Child try to write:

� trap into kernel

� make a copy of the segment and resume

Page 55: Operating Systems - Virtual Memory Basics

Fork and Copy on Write

Fork:

� Copy segment table into child

� Mark parent and child segments read-only

� Start child process; return to parent

Parent/Child try to write:

� trap into kernel

� make a copy of the segment and resume

Page 56: Operating Systems - Virtual Memory Basics

Fork and Copy on Write

Fork:

� Copy segment table into child

� Mark parent and child segments read-only

� Start child process; return to parent

Parent/Child try to write:

� trap into kernel

� make a copy of the segment and resume

Page 57: Operating Systems - Virtual Memory Basics

Fork and Copy on Write

Fork:

� Copy segment table into child

� Mark parent and child segments read-only

� Start child process; return to parent

Parent/Child try to write:

� trap into kernel

� make a copy of the segment and resume

Page 58: Operating Systems - Virtual Memory Basics

Fork and Copy on Write

Fork:

� Copy segment table into child

� Mark parent and child segments read-only

� Start child process; return to parent

Parent/Child try to write:

� trap into kernel

� make a copy of the segment and resume

Page 59: Operating Systems - Virtual Memory Basics

Fork and Copy on Write

Fork:

� Copy segment table into child

� Mark parent and child segments read-only

� Start child process; return to parent

Parent/Child try to write:

� trap into kernel

� make a copy of the segment and resume

Page 60: Operating Systems - Virtual Memory Basics

Copy on Write

Page 61: Operating Systems - Virtual Memory Basics

Zero-on-Reference

� How much physical memory needed for stack or heap?

� Only what is currently in use

� When program uses memory beyond end of stack

� Segmentation fault into OS kernel

� Kernel allocates some memory

� How much?

� Zeros the memory

� avoid accidentally leaking information!

� Modify segment table

� Resume process

Page 62: Operating Systems - Virtual Memory Basics

Zero-on-Reference

� How much physical memory needed for stack or heap?

� Only what is currently in use

� When program uses memory beyond end of stack

� Segmentation fault into OS kernel

� Kernel allocates some memory

� How much?

� Zeros the memory

� avoid accidentally leaking information!

� Modify segment table

� Resume process

Page 63: Operating Systems - Virtual Memory Basics

Zero-on-Reference

� How much physical memory needed for stack or heap?

� Only what is currently in use

� When program uses memory beyond end of stack

� Segmentation fault into OS kernel

� Kernel allocates some memory

� How much?

� Zeros the memory

� avoid accidentally leaking information!

� Modify segment table

� Resume process

Page 64: Operating Systems - Virtual Memory Basics

Zero-on-Reference

� How much physical memory needed for stack or heap?

� Only what is currently in use

� When program uses memory beyond end of stack

� Segmentation fault into OS kernel

� Kernel allocates some memory

� How much?

� Zeros the memory

� avoid accidentally leaking information!

� Modify segment table

� Resume process

Page 65: Operating Systems - Virtual Memory Basics

Zero-on-Reference

� How much physical memory needed for stack or heap?

� Only what is currently in use

� When program uses memory beyond end of stack

� Segmentation fault into OS kernel

� Kernel allocates some memory

� How much?

� Zeros the memory

� avoid accidentally leaking information!

� Modify segment table

� Resume process

Page 66: Operating Systems - Virtual Memory Basics

Zero-on-Reference

� How much physical memory needed for stack or heap?

� Only what is currently in use

� When program uses memory beyond end of stack

� Segmentation fault into OS kernel

� Kernel allocates some memory

� How much?

� Zeros the memory

� avoid accidentally leaking information!

� Modify segment table

� Resume process

Page 67: Operating Systems - Virtual Memory Basics

Zero-on-Reference

� How much physical memory needed for stack or heap?

� Only what is currently in use

� When program uses memory beyond end of stack

� Segmentation fault into OS kernel

� Kernel allocates some memory

� How much?

� Zeros the memory

� avoid accidentally leaking information!

� Modify segment table

� Resume process

Page 68: Operating Systems - Virtual Memory Basics

Zero-on-Reference

� How much physical memory needed for stack or heap?

� Only what is currently in use

� When program uses memory beyond end of stack

� Segmentation fault into OS kernel

� Kernel allocates some memory

� How much?

� Zeros the memory

� avoid accidentally leaking information!

� Modify segment table

� Resume process

Page 69: Operating Systems - Virtual Memory Basics

Zero-on-Reference

� How much physical memory needed for stack or heap?

� Only what is currently in use

� When program uses memory beyond end of stack

� Segmentation fault into OS kernel

� Kernel allocates some memory

� How much?

� Zeros the memory

� avoid accidentally leaking information!

� Modify segment table

� Resume process

Page 70: Operating Systems - Virtual Memory Basics

Zero-on-Reference

� How much physical memory needed for stack or heap?

� Only what is currently in use

� When program uses memory beyond end of stack

� Segmentation fault into OS kernel

� Kernel allocates some memory

� How much?

� Zeros the memory

� avoid accidentally leaking information!

� Modify segment table

� Resume process

Page 71: Operating Systems - Virtual Memory Basics
sharing, isolation/protection, transparently grow, copy-on-write, BUT: complex, fragmentation, rearrange memory, wasted space between chunks
Page 72: Operating Systems - Virtual Memory Basics

Paged Translation

� Manage memory in fixed size units, or pages

� Finding a free page is easy

� Bitmap allocation: 0011111100000001100

� Each bit represents one physical page number / one physical page frame

� Each process has its own page table

� Stored in physical memory

� Hardware registers

� pointer to page table start

� page table length

Page 73: Operating Systems - Virtual Memory Basics

Paged Translation

� Manage memory in fixed size units, or pages

� Finding a free page is easy

� Bitmap allocation: 0011111100000001100

� Each bit represents one physical page number / one physical page frame

� Each process has its own page table

� Stored in physical memory

� Hardware registers

� pointer to page table start

� page table length

Page 74: Operating Systems - Virtual Memory Basics

Paged Translation

� Manage memory in fixed size units, or pages

� Finding a free page is easy

� Bitmap allocation: 0011111100000001100

� Each bit represents one physical page number / one physical page frame

� Each process has its own page table

� Stored in physical memory

� Hardware registers

� pointer to page table start

� page table length

Page 75: Operating Systems - Virtual Memory Basics

Paged Translation

� Manage memory in fixed size units, or pages

� Finding a free page is easy

� Bitmap allocation: 0011111100000001100

� Each bit represents one physical page number / one physical page frame

� Each process has its own page table

� Stored in physical memory

� Hardware registers

� pointer to page table start

� page table length

Page 76: Operating Systems - Virtual Memory Basics

Paged Translation

� Manage memory in fixed size units, or pages

� Finding a free page is easy

� Bitmap allocation: 0011111100000001100

� Each bit represents one physical page number / one physical page frame

� Each process has its own page table

� Stored in physical memory

� Hardware registers

� pointer to page table start

� page table length

Page 77: Operating Systems - Virtual Memory Basics

Paged Translation

� Manage memory in fixed size units, or pages

� Finding a free page is easy

� Bitmap allocation: 0011111100000001100

� Each bit represents one physical page number / one physical page frame

� Each process has its own page table

� Stored in physical memory

� Hardware registers

� pointer to page table start

� page table length

Page 78: Operating Systems - Virtual Memory Basics

Paged Translation

� Manage memory in fixed size units, or pages

� Finding a free page is easy

� Bitmap allocation: 0011111100000001100

� Each bit represents one physical page number / one physical page frame

� Each process has its own page table

� Stored in physical memory

� Hardware registers

� pointer to page table start

� page table length

Page 79: Operating Systems - Virtual Memory Basics

Paged Translation

� Manage memory in fixed size units, or pages

� Finding a free page is easy

� Bitmap allocation: 0011111100000001100

� Each bit represents one physical page number / one physical page frame

� Each process has its own page table

� Stored in physical memory

� Hardware registers

� pointer to page table start

� page table length

Page 80: Operating Systems - Virtual Memory Basics

Paged Translation

� Manage memory in fixed size units, or pages

� Finding a free page is easy

� Bitmap allocation: 0011111100000001100

� Each bit represents one physical page number / one physical page frame

� Each process has its own page table

� Stored in physical memory

� Hardware registers

� pointer to page table start

� page table length

Page 81: Operating Systems - Virtual Memory Basics

Logical View of Page Table Address Translation

Page 82: Operating Systems - Virtual Memory Basics

paging - implementation

Page 83: Operating Systems - Virtual Memory Basics

Paging Questions

� With paging, what is saved/restored on a process context switch?

� Pointer to page table, size of page table

� Page table itself is in main memory

� What if page size is very small?

� What if page size is very large?

� Internal fragmentation: if we don’t need all of the space inside a fixed size chunk

Page 84: Operating Systems - Virtual Memory Basics

Paging Questions

� With paging, what is saved/restored on a process context switch?

� Pointer to page table, size of page table

� Page table itself is in main memory

� What if page size is very small?

� What if page size is very large?

� Internal fragmentation: if we don’t need all of the space inside a fixed size chunk

Page 85: Operating Systems - Virtual Memory Basics

Paging Questions

� With paging, what is saved/restored on a process context switch?

� Pointer to page table, size of page table

� Page table itself is in main memory

� What if page size is very small?

� What if page size is very large?

� Internal fragmentation: if we don’t need all of the space inside a fixed size chunk

Page 86: Operating Systems - Virtual Memory Basics

Paging Questions

� With paging, what is saved/restored on a process context switch?

� Pointer to page table, size of page table

� Page table itself is in main memory

� What if page size is very small?

� What if page size is very large?

� Internal fragmentation: if we don’t need all of the space inside a fixed size chunk

Page 87: Operating Systems - Virtual Memory Basics

Paging Questions

� With paging, what is saved/restored on a process context switch?

� Pointer to page table, size of page table

� Page table itself is in main memory

� What if page size is very small?

� What if page size is very large?

� Internal fragmentation: if we don’t need all of the space inside a fixed size chunk

Page 88: Operating Systems - Virtual Memory Basics

Paging Questions

� With paging, what is saved/restored on a process context switch?

� Pointer to page table, size of page table

� Page table itself is in main memory

� What if page size is very small?

� What if page size is very large?

� Internal fragmentation: if we don’t need all of the space inside a fixed size chunk

Page 89: Operating Systems - Virtual Memory Basics

Paging and Copy on Write

� Can we share pages between processes (similar as segments before)?

� Set entries in both page tables to the same physical page number

� Need core map of physical page numbers to track which processes are pointing to which

physical page numbers (e.g. reference count)

Page 90: Operating Systems - Virtual Memory Basics

Paging and Copy on Write

� Can we share pages between processes (similar as segments before)?

� Set entries in both page tables to the same physical page number

� Need core map of physical page numbers to track which processes are pointing to which

physical page numbers (e.g. reference count)

Page 91: Operating Systems - Virtual Memory Basics

Paging and Copy on Write

� Can we share pages between processes (similar as segments before)?

� Set entries in both page tables to the same physical page number

� Need core map of physical page numbers to track which processes are pointing to which

physical page numbers (e.g. reference count)

Page 92: Operating Systems - Virtual Memory Basics

Copy-on-Write on Unix/Linux

Virtual Address Space

Physical Address Space

Page 93: Operating Systems - Virtual Memory Basics

Copy-on-Write on Unix/Linux

Virtual Address Space

Physical Address Space

Page 94: Operating Systems - Virtual Memory Basics

Copy-on-Write on Unix/Linux

Virtual Address SpaceProcess A

Physical Address Space

Page 95: Operating Systems - Virtual Memory Basics

Copy-on-Write on Unix/Linux

Virtual Address SpaceProcess A

Physical Address Space

Page 96: Operating Systems - Virtual Memory Basics

Copy-on-Write on Unix/Linux

Virtual Address SpaceProcess A

Physical Address Space

Page 97: Operating Systems - Virtual Memory Basics

Copy-on-Write on Unix/Linux

Virtual Address SpaceProcess A

Physical Address Space

Page 98: Operating Systems - Virtual Memory Basics

Copy-on-Write on Unix/Linux

Virtual Address SpaceProcess A

Physical Address Space

fork

Process B

Page 99: Operating Systems - Virtual Memory Basics

Copy-on-Write on Unix/Linux

Virtual Address SpaceProcess A

Physical Address Space

Process B

Page 100: Operating Systems - Virtual Memory Basics

Copy-on-Write on Unix/Linux

Virtual Address SpaceProcess A

Physical Address Space

Process B

Page 101: Operating Systems - Virtual Memory Basics

Copy-on-Write on Unix/Linux

Virtual Address SpaceProcess A

Physical Address Space

Process B

Page 102: Operating Systems - Virtual Memory Basics

Copy-on-Write on Unix/Linux

Virtual Address SpaceProcess A

Physical Address Space

Process B

Page 103: Operating Systems - Virtual Memory Basics

Copy-on-Write on Unix/Linux

Virtual Address SpaceProcess A

Physical Address Space

Process B

Process B tries to write

Page 104: Operating Systems - Virtual Memory Basics

Copy-on-Write on Unix/Linux

Virtual Address SpaceProcess A

Physical Address Space

Process B

Process B tries to write

copy (update page tables, set writeable)

Page 105: Operating Systems - Virtual Memory Basics

Copy-on-Write on Unix/Linux

Virtual Address SpaceProcess A

Physical Address Space

Process B

write

Page 106: Operating Systems - Virtual Memory Basics

Demand Paging

� Can I start running a program before its code is in physical memory?

� Set all page table entries to invalid

� When a page is referenced for first time, kernel trap

� Kernel brings page in from disk

� Resume execution

� Remaining pages can be transferred in the background while program is running

Page 107: Operating Systems - Virtual Memory Basics

Demand Paging

� Can I start running a program before its code is in physical memory?

� Set all page table entries to invalid

� When a page is referenced for first time, kernel trap

� Kernel brings page in from disk

� Resume execution

� Remaining pages can be transferred in the background while program is running

Page 108: Operating Systems - Virtual Memory Basics

Demand Paging

� Can I start running a program before its code is in physical memory?

� Set all page table entries to invalid

� When a page is referenced for first time, kernel trap

� Kernel brings page in from disk

� Resume execution

� Remaining pages can be transferred in the background while program is running

Page 109: Operating Systems - Virtual Memory Basics

Demand Paging

� Can I start running a program before its code is in physical memory?

� Set all page table entries to invalid

� When a page is referenced for first time, kernel trap

� Kernel brings page in from disk

� Resume execution

� Remaining pages can be transferred in the background while program is running

Page 110: Operating Systems - Virtual Memory Basics

Demand Paging

� Can I start running a program before its code is in physical memory?

� Set all page table entries to invalid

� When a page is referenced for first time, kernel trap

� Kernel brings page in from disk

� Resume execution

� Remaining pages can be transferred in the background while program is running

Page 111: Operating Systems - Virtual Memory Basics

Demand Paging

� Can I start running a program before its code is in physical memory?

� Set all page table entries to invalid

� When a page is referenced for first time, kernel trap

� Kernel brings page in from disk

� Resume execution

� Remaining pages can be transferred in the background while program is running

Page 112: Operating Systems - Virtual Memory Basics

Scheduling a Process (with Demand Paging)

� Only load what’s required

� Initially start with no pages in memory

� Process will be scheduled eventually. What happens?

� a page fault will occur when fetching the first instruction

� further page faults for stacks and data

� after a while, things will stabilize

� The principle of locality ensures that

Page 113: Operating Systems - Virtual Memory Basics

Scheduling a Process (with Demand Paging)

� Only load what’s required

� Initially start with no pages in memory

� Process will be scheduled eventually. What happens?

� a page fault will occur when fetching the first instruction

� further page faults for stacks and data

� after a while, things will stabilize

� The principle of locality ensures that

Page 114: Operating Systems - Virtual Memory Basics

Scheduling a Process (with Demand Paging)

� Only load what’s required

� Initially start with no pages in memory

� Process will be scheduled eventually. What happens?

� a page fault will occur when fetching the first instruction

� further page faults for stacks and data

� after a while, things will stabilize

� The principle of locality ensures that

Page 115: Operating Systems - Virtual Memory Basics

Scheduling a Process (with Demand Paging)

� Only load what’s required

� Initially start with no pages in memory

� Process will be scheduled eventually. What happens?

� a page fault will occur when fetching the first instruction

� further page faults for stacks and data

� after a while, things will stabilize

� The principle of locality ensures that

Page 116: Operating Systems - Virtual Memory Basics

Scheduling a Process (with Demand Paging)

� Only load what’s required

� Initially start with no pages in memory

� Process will be scheduled eventually. What happens?

� a page fault will occur when fetching the first instruction

� further page faults for stacks and data

� after a while, things will stabilize

� The principle of locality ensures that

Page 117: Operating Systems - Virtual Memory Basics

Scheduling a Process (with Demand Paging)

� Only load what’s required

� Initially start with no pages in memory

� Process will be scheduled eventually. What happens?

� a page fault will occur when fetching the first instruction

� further page faults for stacks and data

� after a while, things will stabilize

� The principle of locality ensures that

Page 118: Operating Systems - Virtual Memory Basics

Scheduling a Process (with Demand Paging)

� Only load what’s required

� Initially start with no pages in memory

� Process will be scheduled eventually. What happens?

� a page fault will occur when fetching the first instruction

� further page faults for stacks and data

� after a while, things will stabilize

� The principle of locality ensures that

Page 119: Operating Systems - Virtual Memory Basics

Prepaging

Prepaging as an optimization

� If it is known upon scheduling which pages will be required ...

� page referenced by instruction pointer, stack pointer, etc.

� ... load required pages into RAM ahead of time

→ may lower page fault frequency

Page 120: Operating Systems - Virtual Memory Basics

Prepaging

Prepaging as an optimization

� If it is known upon scheduling which pages will be required ...

� page referenced by instruction pointer, stack pointer, etc.

� ... load required pages into RAM ahead of time

→ may lower page fault frequency

Page 121: Operating Systems - Virtual Memory Basics

Prepaging

Prepaging as an optimization

� If it is known upon scheduling which pages will be required ...

� page referenced by instruction pointer, stack pointer, etc.

� ... load required pages into RAM ahead of time

→ may lower page fault frequency

Page 122: Operating Systems - Virtual Memory Basics

Prepaging

Prepaging as an optimization

� If it is known upon scheduling which pages will be required ...

� page referenced by instruction pointer, stack pointer, etc.

� ... load required pages into RAM ahead of time

→ may lower page fault frequency

Page 123: Operating Systems - Virtual Memory Basics

Sparse Address Spaces

� Every process needs an address space.

� What if virtual address space is large?

� 32-bits, 4KB pages → 1 million page table entries

� 64-bits → 4 quadrillion page table entries

Page 124: Operating Systems - Virtual Memory Basics

Sparse Address Spaces

� Every process needs an address space.

� What if virtual address space is large?

� 32-bits, 4KB pages → 1 million page table entries

� 64-bits → 4 quadrillion page table entries

Page 125: Operating Systems - Virtual Memory Basics

Sparse Address Spaces

� Every process needs an address space.

� What if virtual address space is large?

� 32-bits, 4KB pages → 1 million page table entries

� 64-bits → 4 quadrillion page table entries

Page 126: Operating Systems - Virtual Memory Basics

Sparse Address Spaces

� Every process needs an address space.

� What if virtual address space is large?

� 32-bits, 4KB pages → 1 million page table entries

� 64-bits → 4 quadrillion page table entries

Page 127: Operating Systems - Virtual Memory Basics

Multi-level Translation

� Tree of translation tables

� Paged segmentation

� Multi-level page tables

� Multi-level paged segmentation

Page 128: Operating Systems - Virtual Memory Basics

Multi-level Translation

� Tree of translation tables

� Paged segmentation

� Multi-level page tables

� Multi-level paged segmentation

Page 129: Operating Systems - Virtual Memory Basics

Multi-level Translation

� Tree of translation tables

� Paged segmentation

� Multi-level page tables

� Multi-level paged segmentation

Page 130: Operating Systems - Virtual Memory Basics

Multi-level Translation

� Tree of translation tables

� Paged segmentation

� Multi-level page tables

� Multi-level paged segmentation

Page 131: Operating Systems - Virtual Memory Basics

Multi-level Translation

� Fixed-size page as lowest level unit of allocation

� Efficient memory allocation (compared to segments)

� Efficient for sparse translation tree (compared to simple paging)

� Efficient disk transfers (fixed size units, page size multiple of disk sector)

� Easier to build translation lookaside buffers

� Efficient reverse lookup (from physical → virtual)

� Fine granularity for protection/sharing

Page 132: Operating Systems - Virtual Memory Basics

Multi-level Translation

� Fixed-size page as lowest level unit of allocation

� Efficient memory allocation (compared to segments)

� Efficient for sparse translation tree (compared to simple paging)

� Efficient disk transfers (fixed size units, page size multiple of disk sector)

� Easier to build translation lookaside buffers

� Efficient reverse lookup (from physical → virtual)

� Fine granularity for protection/sharing

Page 133: Operating Systems - Virtual Memory Basics

Multi-level Translation

� Fixed-size page as lowest level unit of allocation

� Efficient memory allocation (compared to segments)

� Efficient for sparse translation tree (compared to simple paging)

� Efficient disk transfers (fixed size units, page size multiple of disk sector)

� Easier to build translation lookaside buffers

� Efficient reverse lookup (from physical → virtual)

� Fine granularity for protection/sharing

Page 134: Operating Systems - Virtual Memory Basics

Multi-level Translation

� Fixed-size page as lowest level unit of allocation

� Efficient memory allocation (compared to segments)

� Efficient for sparse translation tree (compared to simple paging)

� Efficient disk transfers (fixed size units, page size multiple of disk sector)

� Easier to build translation lookaside buffers

� Efficient reverse lookup (from physical → virtual)

� Fine granularity for protection/sharing

Page 135: Operating Systems - Virtual Memory Basics

Multi-level Translation

� Fixed-size page as lowest level unit of allocation

� Efficient memory allocation (compared to segments)

� Efficient for sparse translation tree (compared to simple paging)

� Efficient disk transfers (fixed size units, page size multiple of disk sector)

� Easier to build translation lookaside buffers

� Efficient reverse lookup (from physical → virtual)

� Fine granularity for protection/sharing

Page 136: Operating Systems - Virtual Memory Basics

Multi-level Translation

� Fixed-size page as lowest level unit of allocation

� Efficient memory allocation (compared to segments)

� Efficient for sparse translation tree (compared to simple paging)

� Efficient disk transfers (fixed size units, page size multiple of disk sector)

� Easier to build translation lookaside buffers

� Efficient reverse lookup (from physical → virtual)

� Fine granularity for protection/sharing

Page 137: Operating Systems - Virtual Memory Basics

Multi-level Translation

� Fixed-size page as lowest level unit of allocation

� Efficient memory allocation (compared to segments)

� Efficient for sparse translation tree (compared to simple paging)

� Efficient disk transfers (fixed size units, page size multiple of disk sector)

� Easier to build translation lookaside buffers

� Efficient reverse lookup (from physical → virtual)

� Fine granularity for protection/sharing

Page 138: Operating Systems - Virtual Memory Basics

Paged Segmentation

� Process memory is segmented

� Segment table entry:

� Pointer to page table

� Page table length (# of pages in segment)

� Access permissions

� Page table entry:

� Physical page number

� Access permissions

� Share/protection at either page or segment-level

Page 139: Operating Systems - Virtual Memory Basics

Paged Segmentation

� Process memory is segmented

� Segment table entry:

� Pointer to page table

� Page table length (# of pages in segment)

� Access permissions

� Page table entry:

� Physical page number

� Access permissions

� Share/protection at either page or segment-level

Page 140: Operating Systems - Virtual Memory Basics

Paged Segmentation

� Process memory is segmented

� Segment table entry:

� Pointer to page table

� Page table length (# of pages in segment)

� Access permissions

� Page table entry:

� Physical page number

� Access permissions

� Share/protection at either page or segment-level

Page 141: Operating Systems - Virtual Memory Basics

Paged Segmentation

� Process memory is segmented

� Segment table entry:

� Pointer to page table

� Page table length (# of pages in segment)

� Access permissions

� Page table entry:

� Physical page number

� Access permissions

� Share/protection at either page or segment-level

Page 142: Operating Systems - Virtual Memory Basics

Paged Segmentation

� Process memory is segmented

� Segment table entry:

� Pointer to page table

� Page table length (# of pages in segment)

� Access permissions

� Page table entry:

� Physical page number

� Access permissions

� Share/protection at either page or segment-level

Page 143: Operating Systems - Virtual Memory Basics

Paged Segmentation

� Process memory is segmented

� Segment table entry:

� Pointer to page table

� Page table length (# of pages in segment)

� Access permissions

� Page table entry:

� Physical page number

� Access permissions

� Share/protection at either page or segment-level

Page 144: Operating Systems - Virtual Memory Basics

Paged Segmentation

� Process memory is segmented

� Segment table entry:

� Pointer to page table

� Page table length (# of pages in segment)

� Access permissions

� Page table entry:

� Physical page number

� Access permissions

� Share/protection at either page or segment-level

Page 145: Operating Systems - Virtual Memory Basics

Paged Segmentation

� Process memory is segmented

� Segment table entry:

� Pointer to page table

� Page table length (# of pages in segment)

� Access permissions

� Page table entry:

� Physical page number

� Access permissions

� Share/protection at either page or segment-level

Page 146: Operating Systems - Virtual Memory Basics

Paged Segmentation

� Process memory is segmented

� Segment table entry:

� Pointer to page table

� Page table length (# of pages in segment)

� Access permissions

� Page table entry:

� Physical page number

� Access permissions

� Share/protection at either page or segment-level

Page 147: Operating Systems - Virtual Memory Basics

Paged Segmentation

Page 148: Operating Systems - Virtual Memory Basics

Question

� With paged segmentation, what must be saved/restored across a process context switch?

Page 149: Operating Systems - Virtual Memory Basics
Explain on whiteboard
Page 150: Operating Systems - Virtual Memory Basics

Paging: x86-32 with page size 4 KiB

PML5

PDPTI (9 bit)PDI (10 bit) PTI (10 bit) Offset (12 bit)

32-bit virtual address

CR3PML4

PML4E 0

PML4E 1...

#PML4I...

PML4E 511

PDPT

PDPTE 0

PDPTE 1...

#PDPTI...

PDPTE 511

CR3Page Directory

PDE 0

PDE 1···

PDE #PDI···

PDE 1023

Page Table

PTE 0

PTE 1···

PTE #PTI···

PTE 1023

4 KiB Page

Byte 0

Byte 1···

Offset···

Byte 4095

Page 151: Operating Systems - Virtual Memory Basics

Paging: x86-32-PAE with page size 4 KiB

PML5

PDPTI (2 bit) PDI (9 bit) PTI (9 bit) Offset (12 bit)

32-bit virtual address

CR3PML4

PML4E 0

PML4E 1...

#PML4I...

PML4E 511

CR3PDPT

PDPTE 0

PDPTE 1

PDPTE 2

PDPTE 3Page Directory

PDE 0

PDE 1···

PDE #PDI···

PDE 511

Page Table

PTE 0

PTE 1···

PTE #PTI···

PTE 511

4 KiB Page

Byte 0

Byte 1···

Offset···

Byte 4095

Page 152: Operating Systems - Virtual Memory Basics

Paging: x86-64 with page size 4 KiB

PML5

PML4I (9 bit) PDPTI (9 bit) PDI (9 bit) PTI (9 bit) Offset (12 bit)

48-bit virtual address

CR3PML4

PML4E 0

PML4E 1···

#PML4I···

PML4E 511

PDPT

PDPTE 0

PDPTE 1···

#PDPTI···

PDPTE 511

Page Directory

PDE 0

PDE 1···

PDE #PDI···

PDE 511

Page Table

PTE 0

PTE 1···

PTE #PTI···

PTE 511

4 KiB Page

Byte 0

Byte 1···

Offset···

Byte 4095

Page 153: Operating Systems - Virtual Memory Basics

Paging: x86-64 with PML5 and page size 4 KiB

PML5I (9 bit) PML4I (9 bit) PDPTI (9 bit) PDI (9 bit) PTI (9 bit) Offset (12 bit)

57-bit virtual address

CR3PML5

PML5E 0

PML5E 1···

#PML5I···

PML5E 511

PML4

PML4E 0

PML4E 1···

#PML4I···

PML4E 511

PDPT

PDPTE 0

PDPTE 1···

#PDPTI···

PDPTE 511

Page Directory

PDE 0

PDE 1···

PDE #PDI···

PDE 511

Page Table

PTE 0

PTE 1···

PTE #PTI···

PTE 511

4 KiB Page

Byte 0

Byte 1···

Offset···

Byte 4095

Page 154: Operating Systems - Virtual Memory Basics
only PTEs that are in use, simple alloc, share segment or page, overhead: one pointer per virtual page, more lookups per memory access
Page 155: Operating Systems - Virtual Memory Basics

x86-64 Memory Layout (with PML4)

non-canonical

(= not usable)

0 264

Page 156: Operating Systems - Virtual Memory Basics

x86-64 Memory Layout (with PML4, scaled)

non-canonical

(scaled 2000:1)

0 264

use

rsp

ace

kern

elsp

ace

Page 157: Operating Systems - Virtual Memory Basics

x86-64 Memory Layout (with PML4, scaled)

non-canonical

(scaled 65534:1)

0 264247 264 − 247

user space kernel space

Page 158: Operating Systems - Virtual Memory Basics

Address Translation on x86

processors

Page 159: Operating Systems - Virtual Memory Basics

Intel Pentium

� Segmentation and paging

� 16 K segments, each 4 GB

� Few segments

� Large segments

Page 160: Operating Systems - Virtual Memory Basics

Intel Pentium

� Segmentation and paging

� 16 K segments, each 4 GB

� Few segments

� Large segments

Page 161: Operating Systems - Virtual Memory Basics

Intel Pentium

� Segmentation and paging

� 16 K segments, each 4 GB

� Few segments

� Large segments

Page 162: Operating Systems - Virtual Memory Basics

Intel Pentium

� Segmentation and paging

� 16 K segments, each 4 GB

� Few segments

� Large segments

Page 163: Operating Systems - Virtual Memory Basics

LDT - GDT

� Local Descriptor Table LDT

� for each process

� local segments (Code, Data, Stack)

� Global Descriptor Table GDT

� for system segments

� also for kernel

Page 164: Operating Systems - Virtual Memory Basics

LDT - GDT

� Local Descriptor Table LDT

� for each process

� local segments (Code, Data, Stack)

� Global Descriptor Table GDT

� for system segments

� also for kernel

Page 165: Operating Systems - Virtual Memory Basics

LDT - GDT

� Local Descriptor Table LDT

� for each process

� local segments (Code, Data, Stack)

� Global Descriptor Table GDT

� for system segments

� also for kernel

Page 166: Operating Systems - Virtual Memory Basics

LDT - GDT

� Local Descriptor Table LDT

� for each process

� local segments (Code, Data, Stack)

� Global Descriptor Table GDT

� for system segments

� also for kernel

Page 167: Operating Systems - Virtual Memory Basics

LDT - GDT

� Local Descriptor Table LDT

� for each process

� local segments (Code, Data, Stack)

� Global Descriptor Table GDT

� for system segments

� also for kernel

Page 168: Operating Systems - Virtual Memory Basics

LDT - GDT

� Local Descriptor Table LDT

� for each process

� local segments (Code, Data, Stack)

� Global Descriptor Table GDT

� for system segments

� also for kernel

Page 169: Operating Systems - Virtual Memory Basics

Segment Registers

� 6 segment registers

� CS: Selector for Code Segment

� DS: Selector for Data Segment

� ES: Selector for Data Segment

� FS: Selector for Data Segment

� GS: Selector for Data Segment

� SS: Selector for Stack Segment

Page 170: Operating Systems - Virtual Memory Basics

Segment Registers

� 6 segment registers

� CS: Selector for Code Segment

� DS: Selector for Data Segment

� ES: Selector for Data Segment

� FS: Selector for Data Segment

� GS: Selector for Data Segment

� SS: Selector for Stack Segment

Page 171: Operating Systems - Virtual Memory Basics

Segment Registers

� 6 segment registers

� CS: Selector for Code Segment

� DS: Selector for Data Segment

� ES: Selector for Data Segment

� FS: Selector for Data Segment

� GS: Selector for Data Segment

� SS: Selector for Stack Segment

Page 172: Operating Systems - Virtual Memory Basics

Segment Registers

� 6 segment registers

� CS: Selector for Code Segment

� DS: Selector for Data Segment

� ES: Selector for Data Segment

� FS: Selector for Data Segment

� GS: Selector for Data Segment

� SS: Selector for Stack Segment

Page 173: Operating Systems - Virtual Memory Basics

Segment Registers

� 6 segment registers

� CS: Selector for Code Segment

� DS: Selector for Data Segment

� ES: Selector for Data Segment

� FS: Selector for Data Segment

� GS: Selector for Data Segment

� SS: Selector for Stack Segment

Page 174: Operating Systems - Virtual Memory Basics

Segment Registers

� 6 segment registers

� CS: Selector for Code Segment

� DS: Selector for Data Segment

� ES: Selector for Data Segment

� FS: Selector for Data Segment

� GS: Selector for Data Segment

� SS: Selector for Stack Segment

Page 175: Operating Systems - Virtual Memory Basics

Segment Registers

� 6 segment registers

� CS: Selector for Code Segment

� DS: Selector for Data Segment

� ES: Selector for Data Segment

� FS: Selector for Data Segment

� GS: Selector for Data Segment

� SS: Selector for Stack Segment

Page 176: Operating Systems - Virtual Memory Basics

Segment Selector

� Null Segment at index 0 → cannot be used

� Modifying a segment register loads corresponding descriptor into an internal CPU register

Page 177: Operating Systems - Virtual Memory Basics

Segment Selector

� Null Segment at index 0 → cannot be used

� Modifying a segment register loads corresponding descriptor into an internal CPU register

Page 178: Operating Systems - Virtual Memory Basics

Segment Selector

� Null Segment at index 0 → cannot be used

� Modifying a segment register loads corresponding descriptor into an internal CPU register

Page 179: Operating Systems - Virtual Memory Basics

Hidden Part of Segment Registers

Page 180: Operating Systems - Virtual Memory Basics

Segment Descriptor

Page 181: Operating Systems - Virtual Memory Basics

Address translation

� we start with (selector, offset)

� CPU looks for correct descriptor in internal registers

� selector 0 or segment swapped out: interrupt

� offset exceeds segment size: interrupt

� add base field to offset

� check limits of course

� result: linear address

� paging turned off: linear address is physical address

Page 182: Operating Systems - Virtual Memory Basics

Address translation

� we start with (selector, offset)

� CPU looks for correct descriptor in internal registers

� selector 0 or segment swapped out: interrupt

� offset exceeds segment size: interrupt

� add base field to offset

� check limits of course

� result: linear address

� paging turned off: linear address is physical address

Page 183: Operating Systems - Virtual Memory Basics

Address translation

� we start with (selector, offset)

� CPU looks for correct descriptor in internal registers

� selector 0 or segment swapped out: interrupt

� offset exceeds segment size: interrupt

� add base field to offset

� check limits of course

� result: linear address

� paging turned off: linear address is physical address

Page 184: Operating Systems - Virtual Memory Basics

Address translation

� we start with (selector, offset)

� CPU looks for correct descriptor in internal registers

� selector 0 or segment swapped out: interrupt

� offset exceeds segment size: interrupt

� add base field to offset

� check limits of course

� result: linear address

� paging turned off: linear address is physical address

Page 185: Operating Systems - Virtual Memory Basics

Address translation

� we start with (selector, offset)

� CPU looks for correct descriptor in internal registers

� selector 0 or segment swapped out: interrupt

� offset exceeds segment size: interrupt

� add base field to offset

� check limits of course

� result: linear address

� paging turned off: linear address is physical address

Page 186: Operating Systems - Virtual Memory Basics

Address translation

� we start with (selector, offset)

� CPU looks for correct descriptor in internal registers

� selector 0 or segment swapped out: interrupt

� offset exceeds segment size: interrupt

� add base field to offset

� check limits of course

� result: linear address

� paging turned off: linear address is physical address

Page 187: Operating Systems - Virtual Memory Basics

Address translation

� we start with (selector, offset)

� CPU looks for correct descriptor in internal registers

� selector 0 or segment swapped out: interrupt

� offset exceeds segment size: interrupt

� add base field to offset

� check limits of course

� result: linear address

� paging turned off: linear address is physical address

Page 188: Operating Systems - Virtual Memory Basics

Address translation

� we start with (selector, offset)

� CPU looks for correct descriptor in internal registers

� selector 0 or segment swapped out: interrupt

� offset exceeds segment size: interrupt

� add base field to offset

� check limits of course

� result: linear address

� paging turned off: linear address is physical address

Page 189: Operating Systems - Virtual Memory Basics

Address translation

Page 190: Operating Systems - Virtual Memory Basics

Combining Segments and Paging

OSes today have only a very small

number of segments:

� 1 for user code

� 1 for user data

� 1 for user thread local storage

� 1 for kernel code

� 1 for kernel data

� 1 for kernel core local storage

Page 191: Operating Systems - Virtual Memory Basics

Combining Segments and Paging

OSes today have only a very small

number of segments:

� 1 for user code

� 1 for user data

� 1 for user thread local storage

� 1 for kernel code

� 1 for kernel data

� 1 for kernel core local storage

Page 192: Operating Systems - Virtual Memory Basics

Combining Segments and Paging

OSes today have only a very small

number of segments:

� 1 for user code

� 1 for user data

� 1 for user thread local storage

� 1 for kernel code

� 1 for kernel data

� 1 for kernel core local storage

Page 193: Operating Systems - Virtual Memory Basics

Combining Segments and Paging

OSes today have only a very small

number of segments:

� 1 for user code

� 1 for user data

� 1 for user thread local storage

� 1 for kernel code

� 1 for kernel data

� 1 for kernel core local storage

Page 194: Operating Systems - Virtual Memory Basics

Combining Segments and Paging

OSes today have only a very small

number of segments:

� 1 for user code

� 1 for user data

� 1 for user thread local storage

� 1 for kernel code

� 1 for kernel data

� 1 for kernel core local storage

Page 195: Operating Systems - Virtual Memory Basics

Combining Segments and Paging

OSes today have only a very small

number of segments:

� 1 for user code

� 1 for user data

� 1 for user thread local storage

� 1 for kernel code

� 1 for kernel data

� 1 for kernel core local storage

Page 196: Operating Systems - Virtual Memory Basics

Combining Segments and Paging

OSes today have only a very small

number of segments:

� 1 for user code

� 1 for user data

� 1 for user thread local storage

� 1 for kernel code

� 1 for kernel data

� 1 for kernel core local storage

Page 197: Operating Systems - Virtual Memory Basics

Segments Today

� x86-64 requires segment base to be 0 and limit to be unlimited

� not even used anymore to separate code and data

� most OSes today only use segments to determine the privilege level

Page 198: Operating Systems - Virtual Memory Basics

Segments Today

� x86-64 requires segment base to be 0 and limit to be unlimited

� not even used anymore to separate code and data

� most OSes today only use segments to determine the privilege level

Page 199: Operating Systems - Virtual Memory Basics

Segments Today

� x86-64 requires segment base to be 0 and limit to be unlimited

� not even used anymore to separate code and data

� most OSes today only use segments to determine the privilege level

Page 200: Operating Systems - Virtual Memory Basics

Take Aways

Virtual memory

� is based on Segmentation and Paging

� enables effective protection mechanisms

� enables sparse address spaces

Page 201: Operating Systems - Virtual Memory Basics

Take Aways

Virtual memory

� is based on Segmentation and Paging

� enables effective protection mechanisms

� enables sparse address spaces

Page 202: Operating Systems - Virtual Memory Basics

Take Aways

Virtual memory

� is based on Segmentation and Paging

� enables effective protection mechanisms

� enables sparse address spaces

Page 203: Operating Systems - Virtual Memory Basics

Take Aways

Virtual memory

� is based on Segmentation and Paging

� enables effective protection mechanisms

� enables sparse address spaces

Page 204: Operating Systems - Virtual Memory Basics

Take Aways

Virtual memory

� is based on Segmentation and Paging

� enables effective protection mechanisms

� enables sparse address spaces

Page 205: Operating Systems - Virtual Memory Basics