11: paging implementation and segmentation -...

12
1 11: Paging Implementation and Segmentation Mark Handley Page Size Small page size: Advantages Less internal fragmentation (on average, half of last page is unused) Better fit for various data structures, code sections Less unused program in memory Disadvantages Programs need many pages, larger page tables

Upload: phunganh

Post on 10-Apr-2018

237 views

Category:

Documents


5 download

TRANSCRIPT

1

11: PagingImplementation andSegmentation

Mark Handley

Page Size

Small page size:

Advantages

Less internal fragmentation (on average, half of lastpage is unused)

Better fit for various data structures, code sections

Less unused program in memory

Disadvantages

Programs need many pages, larger page tables

2

Overhead due to size of page table and internalfragmentation:

where s = average process size p = page size e = size of page entry in page table

This has a minimum when:

Page Size

page tablesize

page tablesize

wasted space dueto fragmentationwasted space dueto fragmentation

Separate Instruction and Data Spaces

One address space Separate I and D spaces

3

Shared Instruction Pages

Two processes sharing same program share its instruction pagetable, but not its data page table.

Sharing Data Pages

Sharing program text pages is easy if I and D spaces areseparate.

Sharing program data pages is also possible: Eg: fork() system call in Unix.

After fork(), both program and data pages are shared, butdata pages become read-only.

When a write occurs, a protection trap occurs, and the OScopies the page, to create two identical read-write pages,and updates the page tables accordingly.

This is known as copy-on-write.

4

Cleaning Policy

For good performance, you’d like there to always be a few freepages ready to handle page faults.

Use a background paging daemon process, which periodicallyinspects state of memory:

When too few frames are free, it selects pages to evict using areplacement algorithm.

Schedules write to disk of modified pages to be evicted, sothey’re clean when page fault occurs.

Can use same circular list (clock) as regular page replacementalgorithm, but with an additional pointer.

Implementation Issues

Operating System Involvement with Paging

Four times when OS is involved with paging:1. Process creation:

− Determine program size.− Create page table.

2. Process execution (context switch):− MMU reset for new process− TLB flushed.

3. Page fault time:− Determine which virtual address caused the fault.− Swap target page out, swap needed page in.

4. Process termination time:− Release process’s page table and pages.

5

Process Creation

Need to get process into memory and stored on paging area ofdisk.

Can use copy-on-write when forking a process.

On Unix, exec() replaces current executing program withanother one.

Can page the program text directly from the program fileon disk (file is memory-mapped).

Don’t need to load any of it to start running it! Just let apage fault cause each page to be loaded as it’s needed.Known as demand-paging.

Page Fault Handling

1. Faulting instruction causes MMU to trap to OS.2. Save general registers and program state3. Determines which virtual page is needed4. Check validity of address and page protection bits, seek page

frame to replace.5. If selected frame is dirty, write it to disk.

[Allocate CPU to another process while this completes.]6. Start transfer of page from disk to empty frame.

[Allocate CPU to another process while this completes.]

7. Update page table to show page is now in memory.8. Back up faulting instruction to state when it began.9. Schedule faulting process10. Restore registers, process state, and re-run faulting instruction.

6

Instruction Backup

An instruction causing a page fault.

Did the page fault occur on the instruction load, the firstoperand, or the second operand?

Made even worse by auto-increment instructions (Eg: j = ++i)

Locking Pages in Memory

Virtual memory and I/O operations may interact.

Process issues call to read from a device into a buffer in theprocess’s memory space. While waiting for I/O, another processes is run, and has a

page fault. Buffer for the first process might be chosen to be paged

out.

Need to specify some pages as locked in memory. They are temporarily exempted from being paged out.

7

Backing Store

(a) Paging to static swap area(b) Backing up pages dynamically

Segmentation

Programmer’s view of memory is not usually as a single linearaddress space:

Programmer doesn’t know how large these will be, or howthey’ll grow, and doesn’t want to manage where they go invirtual memory.

main program

threadstack

threadstack

heap

libraryfunction

symboltable

libraryfunction

8

Example:Compiler with One-dimensional Address Space

Example:

Compiler with Segmentation

Segmentation maintains multiple separate virtual addressspaces per process.

Allows each table to grow or shrink, independently.

9

Segmentation Hardware

CPUCPU MemoryMemory++

logicaladdress

physicaladdress

<<

trap

ss dd

limit base

s

segmenttable

y

n

Segmentation vs Paging

Similarity: Address space can exceed size of real memory.

Differences: Programmer is aware of segmentation. Paging is hidden.

Segmentation maintains multiple address spaces perprocess. Paging maintains one address space.

Segmentation allows procedures and data to be separatelyprotected. This is hard with paging.

Segmentation easily permits tables whose size varies.

Segmentation facilitates sharing of procedures betweenprocesses. This is hard with paging.

Pure segmentation suffers from memory fragmentation.

10

Segmentation with Paging

Segmentation and Paging can be used together.

Programmer is aware of segments.Gains all the protection and sharing benefits.

Within each segment, paging is used.Avoids external memory fragmentationUses memory efficiently.

Intel x86 processors since the 386 combine both.

Segmentation with Paging: Pentium

To select a segment, program loads a selector for that segmentinto one of six segment registers CS register: selector for code segment DS register: selector for data segment

Selector can specify whether segment table is Local to theprocess, or Global to the machine.

Format of a selector:

Index

13 bits 1 2

LDT/GDT Privilege Level (0-3)

11

Segmentation with Paging: Pentium

Step 1: Use the Selector to convert the 32 bit virtual offsetaddress to a 32 bit linear address.

index g p

segment descriptor

limit

selector

32 bit offset32 bit offset

32 bit linear address32 bit linear addresssegment descriptor

table

base ++

Segmentation with Paging: Pentium (4)

Step 2: Convert the 32 bit linear address to a physicaladdress using a two-stage page table.

12

Segment Protection: Pentium

At any instant a programis in one protection level.

Calls to higher levels arepermitted.

Calls to lower levels mustcontain a selector insteadof an address, whichdesignates a segmentdescriptor called a call-gate, which in turn givesthe address to be called.

Thus only restricted entrypoints are permitted.

Level