operating systems - people.unica.it · 2019-11-21 · giorgio giacinto 2019 operating systems 2....

66
OPERATING SYSTEMS MEMORY MANAGEMENT

Upload: others

Post on 01-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

OPERATING SYSTEMSMEMORY MANAGEMENT

Page 2: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Background

� Programs must be brought (from disk) into memory and placed within a process for them to be run

� Main memory and registers are the only storage devices the CPU can access directly

� Memory unit only sees a stream of addresses + read requests, or address + data and write requests

� Register access in one CPU clock (or less)

� Cache sits between main memory and CPU registers

� Main memory can take many cycles, causing a stall

Giorgio Giacinto 2019 Operating Systems 2

Page 3: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Memory Management Requirements

� Memory management is intended to satisfy the following requirements

� Relocation

� Protection

� Sharing

� Logical organization

� Physical organization

Operating SystemsGiorgio Giacinto 2019 3

Page 4: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Relocation

� Programmers typically do not know in advance which other programs will be resident in main memory at the time of execution of their program

� Active processes need to be able to be swapped in and out of main memory in order to maximize processor utilization

� The OS may need to relocate the process to a different area of memory when it is swapped back in

� Specifying that a process must be placed in the same memory region would be limiting

Operating SystemsGiorgio Giacinto 2019 4

Page 5: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Process Control Block

Program

Data

Stack

Current topof stack

Entry pointto program

Process controlinformation

Increasingaddressvalues

Branchinstruction

Referenceto data

Figure 7.1 Addressing Requirements for a Process

Addressing Requirements for a Process

Operating SystemsGiorgio Giacinto 2019 5

Page 6: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Protection

� Processes need to acquire permission to reference memory locations for reading or writing purposes

� Location of a program in main memory is unpredictable

� Memory references generated by a process must be checked at run time

� Mechanisms that support relocation also support protection

Operating SystemsGiorgio Giacinto 2019 6

Page 7: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Sharing

� Advantageous to allow each process access to the same copy of the program rather than have their own separate copy

� Memory management must allow controlled access to shared areas of memory without compromising protection

� Mechanisms used to support relocation support sharing capabilities

Operating SystemsGiorgio Giacinto 2019 7

Page 8: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Logical Organization

� Memory is organized as linear

� The main memory can be logically organized in segments corresponding to the program modules

Operating Systems

Programs are written in modules• Modules can be written and compiled independently• Different degrees of protection given to modules (read-only,

execute-only)• Sharing on a module level corresponds to the user’s way of viewing

the problem

Giorgio Giacinto 2019 8

Page 9: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Physical Organization

Cannot leave the programmer with the responsibility

to manage memory

Memory available for a program plus

its data may be insufficient

Overlaying allows various modules to be assigned

the same region of memory but is time

consuming to program

Programmer does not know how

much space will be available

Operating SystemsGiorgio Giacinto 2019 9

Page 10: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Memory access

Giorgio Giacinto 2019 Operating Systems 10

Page 11: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Base and Limit Registers

� A pair of base and limitregisters define the logical address space

� The CPU must checkthat every memory access generated in user mode is between base and limit

Giorgio Giacinto 2019 Operating Systems 11

Page 12: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Hardware Address Protection

Process Control Block

Program

Data

Stack

Figure 7.8 Hardware Support for Relocation

Comparator

Interrupt tooperating system

Absoluteaddress

Process image inmain memory

Relative address

Base Register

Bounds Register

Adder

Giorgio Giacinto 2019 Operating Systems 12

Page 13: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Multistep Processing of a User Program

Giorgio Giacinto 2019 Operating Systems 13

Page 14: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Binding of Instructions and Data to Memory

� Address binding of instructions and data to memory addresses can happen at three different stages

� Compile timeIf memory location is known a priori, absolute code can be generated. Recompilation needed if starting location changes

� Load timeMust generate relocatable code if memory location is not known at compile time

� Execution timeBinding delayed until run time if the process can be moved during its execution

� Need hardware support for address maps

Giorgio Giacinto 2019 Operating Systems 14

Page 15: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Logical vs. Physical Address Space

� The OS manages a logical address space that is bound to a separate physical address space

� Logical addressgenerated by the CPU, also referred to as virtual address

� Physical address address seen by the memory unit

� Logical and physical addresses are the same in compile-time and load-time address-binding schemes

� Logical (virtual) and physical addresses differ in execution-time address-binding scheme

Giorgio Giacinto 2019 Operating Systems 15

Page 16: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Memory-Management Unit (MMU)

� Hardware device that at run time maps virtual to physical address

� In the simplest scheme the value in the relocation register is added to every address generated by a user process at the time it is sent to memory

� base register now called relocation register� MS-DOS on Intel 80x86 used 4 relocation registers

� The user program deals with logical addresses, it never sees the real physical addresses

Giorgio Giacinto 2019 Operating Systems 16

Page 17: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Dynamic relocation using a relocation register

� All modules kept on disk in relocatable load format

� A module is not loaded until it is called� Better memory-space utilization; unused routine is

never loaded

� Useful when large amounts of code are needed to handle infrequently occurring cases

� No special support from the operating system is required

Giorgio Giacinto 2019 Operating Systems 17

Page 18: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Swapping

Giorgio Giacinto 2019 Operating Systems 18

Page 19: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Swapping

� A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution

� Backing storefast long-term memory storage (e.g., disks), large enough to accommodate copies of memory images for all users

� Roll out, roll in swapping variant used for priority-based scheduling algorithms

� lower-priority processes are swapped out so higher-priority processes can be loaded and executed

Giorgio Giacinto 2019 Operating Systems 19

Page 20: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Schematic View of Swapping

Giorgio Giacinto 2019 Operating Systems

Major part of swap time is transfer time

The OS maintains a ready queue of ready-to-run processes which have memory images on disk

20

Page 21: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Context Switch Time including Swapping

� If next processes to be put on CPU is not in memory, need to swap out a process and swap in target processContext switch time can then be very high

� Example: 100MB process swapping to hard disk with transfer rate of 50MB/sec

� Swap out time of 2000 ms� Plus swap in of same sized process� Total context switch swapping component time of 4000ms

(4 seconds)

� Standard swapping not used in modern operating systems, but modified version common

� e.g., swap only when free memory extremely low and one process is neither performing any action, nor awaiting for any event

Giorgio Giacinto 2019 Operating Systems 21

Page 22: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Swapping on Mobile Systems

� Not supported because there is no backing store� Flash memory based

� Small amount of space� Limited number of write cycles� Poor throughput between flash memory and CPU

� When memory is running out of free space� iOS asks apps to voluntarily relinquish allocated memory

� Read-only data thrown out and reloaded from flash if needed

� Failure to free can result in termination

� Android terminates apps if low free memory, but first writes application state to flash for fast restart

Giorgio Giacinto 2019 Operating Systems 22

Page 23: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Memory Partitioning

Giorgio Giacinto 2019 Operating Systems 23

Page 24: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Contiguous Allocation

� Main memory must support both OS and user processes

� Contiguous allocation is one early method

� Main memory usually divided into two partitions� Resident operating system, usually held in low

memory with interrupt vector� User processes then held in high memory

� Each process contained in single contiguous section of memory

Giorgio Giacinto 2019 Operating Systems 24

Page 25: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Contiguous Allocation (Cont.)

� Relocation registers used to protect user processes from each other, and from changing operating-system code and data

� Base register contains value of smallest physical address� Limit register contains range of logical addresses – each

logical address must be less than the limit register � MMU maps logical address dynamically

Giorgio Giacinto 2019 Operating Systems 25

Page 26: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Multiple-partition allocation

� Degree of multiprogramming limited by number of partitions in the contiguous allocation scheme

� Variable-partition sizes for efficiency (sized to a given process’ needs)

� Holeblock of available memory. Holes of various size are scattered throughout memory

Giorgio Giacinto 2019 Operating Systems 26

Page 27: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Multiple-partition allocation

� When a process arrives, it is allocated memory from a hole large enough to accommodate it

� Process exiting frees its partition, adjacent free partitions combined

� Operating system maintains information about:a) allocated partitions b) free partitions (hole)

Giorgio Giacinto 2019 Operating Systems 27

Page 28: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Dynamic Storage-Allocation Problem

� How to satisfy a request of size n from a list of free holes?

� First-fitAllocate the first hole that is big enough

� Best-fitAllocate the smallest hole that is big enough

� must search entire list, unless ordered by size � Produces the smallest leftover hole

� Worst-fitAllocate the largest hole; must also search entire list

� Produces the largest leftover hole

� First-fit and best-fit better than worst-fit in terms of speed and storage utilization

Giorgio Giacinto 2019 Operating Systems 28

Page 29: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Fragmentation

� External Fragmentation total memory space exists to satisfy a request, but it is not contiguous

� Internal Fragmentationallocated memory may be slightly larger than requested memory

� this size difference is memory internal to a partition, but not being used

� First fit analysis reveals that given N blocks allocated, 0.5 N blocks lost to fragmentation

� 1/3 may be unusable -> 50-percent rule

Giorgio Giacinto 2019 Operating Systems 29

Page 30: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Fragmentation (Cont.)

� Reduce external fragmentation by compaction� Shuffle memory contents to place all free memory

together in one large block� Compaction is possible only if relocation is dynamic, and

is done at execution time� I/O problem

� Latch job in memory while it is involved in I/O� Do I/O only into OS buffers

Giorgio Giacinto 2019 Operating Systems 30

Page 31: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Segmentation

Giorgio Giacinto 2019 Operating Systems 31

Page 32: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Segmentation

� Memory-management scheme that supports user view of memory

� A program is a collection of segments, where a segment is a logical unit such as:

� main program� procedure � function� method� object� local variables, global variables� common block� stack� symbol table� arrays

Giorgio Giacinto 2019 Operating Systems 32

Page 33: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

User’s View of a Program

Giorgio Giacinto 2019 Operating Systems 33

Page 34: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Logical View of Segmentation

1

3

2

4

1

4

2

3

user space physical memory space

Giorgio Giacinto 2019 Operating Systems 34

Page 35: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Segmentation Architecture

� Logical address consists of a tuple<segment-number, offset>

� Segment table each table entry has

� basecontains the starting physical address where the segments reside in memory

� limitspecifies the length of the segment

� Segment-table base register (STBR)points to the segment table’s location in memory

� Segment-table length register (STLR)indicates number of segments used by a program

Giorgio Giacinto 2019 Operating Systems 35

Page 36: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Segmentation Hardware

Giorgio Giacinto 2019 Operating Systems 36

Page 37: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

0

012

0 0 0 0 1 0 1 1 1 0 1 1 1 1 0

6-bit page # 10-bit offset

Processpage table

16-bit logical address

0 0 0 1 1 0 0 1 1 1 0 1 1 1 1 0

16-bit physical address(a) Paging

000101000110011001

0 0 0 1 0 0 1 0 1 1 1 1 0 0 0 0

4-bit segment # 12-bit offset

Process segment table

Length Base

16-bit logical address

0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 0

16-bit physical address(b) Segmentation

00101110111001

0000010000000000011110011110 0010000000100000 +

Figure 7.12 Examples of Logical-to-Physical Address Translation

Example of Logical-to-Physical Address Translation

Operating SystemsGiorgio Giacinto 2019 37

Page 38: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Segmentation Example

logical address space

subroutine stack

symbol table

main program

Sqrt

1400

physical memory

2400

3200

segment 24300

4700

5700

6300

6700

segment table

limit0 1 2 3 4

1000 400 400

1100 1000

base1400 6300 4300 3200 4700

segment 0

segment 3

segment 4

segment 2segment 1

segment 0

segment 3

segment 4

segment 1

Giorgio Giacinto 2019 Operating Systems 38

Page 39: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Segmentation Architecture (Cont.)

� Protection� With each entry in segment table associate

� validation bit = 0 Þ illegal segment� read/write/execute privileges

� Protection bits associated with segments

� Code sharing occurs at segment level

� Since segments vary in length, memory allocation is a dynamic storage-allocation problem

Giorgio Giacinto 2019 Operating Systems 39

Page 40: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Paging

Giorgio Giacinto 2019 Operating Systems 40

Page 41: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Paging

� Partition memory into equal fixed-size chunks that are relatively small

� Process is also divided into small fixed-size chunks of the same size

� To run a program of size N pages, need to find N free frames and load program

Operating Systems

Pages

Chunks of a process

Frames

Available chunks of memory

Giorgio Giacinto 2019 41

Page 42: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Paging Model of Logical and Physical Memory

Giorgio Giacinto 2019 Operating Systems 42

Page 43: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Paging

� Physical address space of a process can be noncontiguous

� Process is allocated physical memory whenever the latter is available

� Avoids external fragmentation� Avoids problem of varying sized memory chunks

� Set up a page table to translate logical to physical addresses

� Backing store likewise split into pages

� Still have internal fragmentation

Giorgio Giacinto 2019 Operating Systems 43

Page 44: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Address Translation Scheme

� Address generated by CPU is divided into� Page number (p)

used as an index into a page table which contains base address of each page in physical memory

� Page offset (d)combined with base address to define the physical memory address that is sent to the memory unit

For given logical address space 2m and page size 2n

Page size is power of 2, between 512 bytes and 16 Mbytes

Giorgio Giacinto 2019 Operating Systems 44

Page 45: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Paging Hardware

Giorgio Giacinto 2019 Operating Systems 45

Page 46: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

0

012

0 0 0 0 1 0 1 1 1 0 1 1 1 1 0

6-bit page # 10-bit offset

Processpage table

16-bit logical address

0 0 0 1 1 0 0 1 1 1 0 1 1 1 1 0

16-bit physical address(a) Paging

000101000110011001

0 0 0 1 0 0 1 0 1 1 1 1 0 0 0 0

4-bit segment # 12-bit offset

Process segment table

Length Base

16-bit logical address

0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 0

16-bit physical address(b) Segmentation

00101110111001

0000010000000000011110011110 0010000000100000 +

Figure 7.12 Examples of Logical-to-Physical Address Translation

Example of Logical-to-Physical Address Translation

Operating SystemsGiorgio Giacinto 2019 46

Page 47: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

0000010111011110

(a) Partitioning

Relative address = 1502

Use

r pr

oces

s(2

700

byte

s)

0000010111011110

(b) Paging(page size = 1K)

Logical address =Page# = 1, Offset = 478

Logical address =Segment# = 1, Offset = 752

Page

0Pa

ge 1

Page

2

Inte

rnal

frag

men

tatio

n

0001001011110000

(c) Segmentation

Segm

ent 0

750

byte

sSe

gmen

t 119

50 b

ytes

478 75

2

Figure 7.11 Logical Addresses

Logical Addresses Comparison

Operating SystemsGiorgio Giacinto 2019 47

Page 48: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Paging Example

n=2 and m=4

32-byte memory and 4-byte pages

Giorgio Giacinto 2019 Operating Systems 48

Page 49: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Free Frames

Before allocation After allocation

Giorgio Giacinto 2019 Operating Systems 49

Page 50: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Implementation of Page Table

� Page table is kept in main memory

� Page-table base register (PTBR) points to the page table

� Page-table length register (PTLR) indicates size of the page table

� In this scheme every data/instruction access requires two memory accesses

� One for the page table � and one for the data / instruction

� To speed-up the access to the page table, a special fast-lookup hardware cache is used for the page table associative memory or translation look-aside buffers(TLBs)

Giorgio Giacinto 2019 Operating Systems 50

Page 51: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Paging Hardware with TLB

Giorgio Giacinto 2019 Operating Systems 51

Page 52: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Memory Protection

� Memory protection implemented by associating protection bit with each frame to indicate if read-only or read-write access is allowed

� Can also add more bits to indicate page execute-only, and so on

� Valid-invalid bit attached to each entry in the page table:

� validthe associated page is in the process’ logical address space, and is thus a legal page

� invalidthe page is not in the process’ logical address space

� Any violations result in a trap to the kernel

Giorgio Giacinto 2019 Operating Systems 52

Page 53: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Valid (v) or Invalid (i) Bit in a Page Table

Giorgio Giacinto 2019 Operating Systems 53

Page 54: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Shared Pages

� Shared code� One copy of read-only (reentrant) code shared among

processes (i.e., text editors, compilers, window systems)� Similar to multiple threads sharing the same process

space� Also useful for interprocess communication if sharing of

read-write pages is allowed

� Private code and data� Each process keeps a separate copy of the code and data� The pages for the private code and data can appear

anywhere in the logical address space

Giorgio Giacinto 2019 Operating Systems 54

Page 55: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Shared Pages Example

Giorgio Giacinto 2019 Operating Systems 55

Page 56: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Structure of the Page Table

� Memory structures for paging can get huge using straight-forward methods

� Consider a 32-bit logical address space as on modern computers and a page size of 4 KB (212)

� Page table would have 1 million entries (232 / 212)� If each entry is 4 bytes -> 4 MB of physical address space

/ memory for page table alone

� Hierarchical Paging

� Hashed Page Tables

� Inverted Page Tables

Giorgio Giacinto 2019 Operating Systems 56

Page 57: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Two-Level Hierarchical Page-Table Scheme

Giorgio Giacinto 2019 Operating Systems 57

Page 58: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Hierarchical Page TableAddress-Translation Scheme

Giorgio Giacinto 2019 Operating Systems 58

Page 59: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Hashed Page Table

Giorgio Giacinto 2019 Operating Systems 59

Page 60: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Inverted Page Table Architecture

� Rather than each process having a page table and keeping track of all possible logical pages, track all physical pages (frames)

� One entry for each real page of memory

Giorgio Giacinto 2019 Operating Systems 60

Page 61: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

ExamplesIntel IA-32 and ARM

Giorgio Giacinto 2019 Operating Systems 61

Page 62: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

The Intel IA-32 Architecture

� Supports both segmentation and segmentation with paging

� Each segment can be 4 GB

� Up to 16 K segments per process

� Divided into two partitions� First partition of up to 8K segments private to process

(kept in local descriptor table (LDT))� Second partition of up to 8K segments shared among all

processes (kept in global descriptor table (GDT))

Giorgio Giacinto 2019 Operating Systems 62

Page 63: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Logical to Physical Address Translation in IA-32

The logical address is a (selector, offset) pair.

The selector is a 16-bit number

The offset is a 32-bit number

Giorgio Giacinto 2019 Operating Systems 63

Page 64: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Intel IA-32 Segmentation

Giorgio Giacinto 2019 Operating Systems 64

Page 65: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Intel IA-32 Paging Architecture

Giorgio Giacinto 2019 Operating Systems 65

Page 66: OPERATING SYSTEMS - people.unica.it · 2019-11-21 · Giorgio Giacinto 2019 Operating Systems 2. Memory Management Requirements ... out of main memoryin order to maximize processor

Example: ARM Architecture

� Dominant mobile platform chip (e.g., Apple iOS and Google Android)� Energy efficient 32-bit CPU� 4 KB and 16 KB pages� 1 MB and 16 MB pages

(termed sections)

� Two levels of TLBs� Outer level has two

micro TLBs (data, instruction)

� Inner is single main TLB

outer page inner page offset

4-KBor

16-KBpage

1-MBor

16-MB section

32 bits

Giorgio Giacinto 2019 Operating Systems 66