24
)> c.. c.. ..., (l) V> V> -I ..., OJ :J V> - OJ !:t. 0 :J

)> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

)> c.. c.. ..., (l) V> V>

-I ..., OJ :J V> -OJ !:t. 0 :J

Page 2: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

\

Main Points

• Address Translation Concept - How do we convert a virtual address to a physical

address?

• Flexible Address Translation - Segmentation

- Paging

- Multilevel translation

- • Efficient Address Translation - Translation Lookaside Buffers

- Virtually and physically addressed caches

Page 3: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Address Tr·anslation Concept

Virtual

Address

Mrtu

Processor l··················· .. ··>I Translation I Invalid ··········;) Raise Exception

~

Data ,!'I

Valid

· ................. >

Physical

Address

Physical Memory

... Data

Page 4: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Address Translation Goals

• Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions

• Memory sharing - Shared libraries, interprocess communication

• Sparse addresses - Dynamically allocated regions: heaps, stacks, mmap

• Efficiency - Reduce fragmentation and copying - Runtime lookup cost and TLB hit rate - Translation table size

• Portability

Page 5: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Bonus Feature

• What if the kernel can regain control whenever a program reads or writes a particular virtual memory location?

• Examples: - Copy on write

- Zero ·on reference

- Fill on demand

- Demand paging

- Memory mapped files

•••

Page 6: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Virtually Addressed Base and Bounds

Processor's View

Virtual Address

Processor I·········=

Virtual Memory

Implementation

Base

Virtual Physical

Physical Memory

1----""1 Base

Address ~ Address Processor I············=····················~©····················;··········· ·········>

Bound

,.. Raise : ················•CB········> Exception ......

Base+ i----., Bound

Page 7: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Virtually Addressed Base and Bounds

• Pros? - Simple

- Fast (2 registers, adder, comparator)

- Safe

- Can relocate in physical memory without changing process

• Cons? - Can't keep program from accidentally overwriting its

own code

- Can't share code/data with other processes

- Can't grow stack/heap as needed

Page 8: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Process Regions or Segments

• Every process has logical regions or segments

- Contiguous region of process memory

• Code, data, heap, stack, dynamic library (code, data), memory mapped files, ...

• Each with its own

- protection: read-only, read-write, execute-only

- sharing: code vs. data

- access pattern: code vs. mmap file

Page 9: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

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

• Processes can share segments

- Same start, length, same/different access permissions

Page 10: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Processor's View

Virtual

Memory

Virtual B Address Code

Processor I·········=

: ........•

Data

B I Stack I

Segmentation

Processor

Implementation

Virtual

Address

Segment Table

Base Bound Access

--•I Segment! Offset Read

I R/W

R/W

R/W

,;, . ······················•©···············! ..... ~~Y.~~~al Address : ···············

,.. Raise ·········•~········> Exception

....................

~x+~rl\<. \ f rc..),-ece ... --~-~.+i "'/\..

Physical Memory

Stack

Code

·····3>

Data

Heap

Base 3

Base+ Bound 3

Base O

Base+ Bound O

Base 1

Base+ Bound 1

Base 2

Base+ Bound 2

Page 11: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

°J>'·hI ><2~ 'e_)· 2 bit segment#

I) Wsc.:+ 12 bit offset

Virtual Memory

---5 main: 0:240

--===, 0:244

0:248

0:24c

... strlen: 0:360

... 0:420

... x: 1:108

... 1,-·· 2({0

~ Segment start length "ii

code 0x4000

data 0

heap

stack 0x2000

store #108, r2

store pc+8, r31

jump 360

-

loadbyte (r2), r3

...

jump (r31)

ab C \0

".)rt-fk:cs ;',-s-+r

-_,:;

x: 108

...

0x700

0xS00

0xl000

hfflain: 4240

4244

4248

424c

. .. strlen: 4360

. .. 4420

...

Physical Memory .

ab C \0

store #1108, r2

store pc+8, r31

jump 360

. .. loadbyte (r2),r3

jump (r31)

Page 12: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Question

• With segmentation, what is saved/restored on a process context switch?

: ""

·,

Y- yl ·- l 5 lbS4\ QQ._ s <. , :I' 'tor t-,._ ~ r ._ r~ ~ ~ +<:? r

se),-..~"-J- · h~\~

Page 13: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Processor's View

Process fs View

Processor

.········>

Virtual Memory

Code

Address Data Virtual B Ox0500

B . I Stack I

Process 2's View

Processor .. ..r······>~ .... D Address Data Virtual B

·oxo5oo

B I Stack I

Implementation

Processor -;-,._~ .._ (.,l

Seg. Offset gment Table J

ound Access

L .• 1 0 I 500 Read

Virtual

Physical Memory

P2's Data

P1's Heap

P1's Stack

R/W Addre§ ! I

~ !ii Stac1-i~ _L__··/···f-···=~.h.=x.=~!~r::i~.11~.A J.!\.d~.ld~~r.~.~~····= ~ ' '+'··········· · = •••• •. • • . . . .. .. •.•.•• ··>\!:/

P1's Data

P2's

: ........• :········>

Processor r·······················•<±>···························· ····· ···················!

Seg . Offset j

···I O I 500 I Code

Virtual Data

Address Heap r

~ ) " 0 ( !tack l l ._

,..,

Bm;e

Segment Table

Bound Access

Read

R/W

R/W

R/W

Heap

P1 's+ P2's Code

P2's Stack

<=:::

Page 14: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Segmentation • Pros?

- Can share code/data segments between processes

- Can protect code segment from being overwritten

• Cons? Complex memory management

- Need to find chunk of a particular size

- May need to rearrange memory to make room for new segment or growing segment (e.g., sbrk)

- External fragmentation: wasted space between chunks

Page 15: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

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 frame

• Each process has its own page table - Stored in physical memory

- Hardware registers • pointer to page table start

• page table length ·

Page 16: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Processor's View Physical _Memory

VPage o[ VPage 1 [

VPage N[

Code

Data

Heap

. . ---~ . .

Stack

: ............................ ~ .

•....•....•••.••••••••. ·> . • ••..••.•.• ·> . . . . .............. .. .: ........... •:• ....... ···> . . . . . ................. : ..••. ~· ••••••••• ·> .

...................... ; ...... ; .......... > . . .......................

. ..............................

Code>

DataO Heap1

Code1 Heap)

Data1

. Heap2 • • • • • • • • • • • • • • • • • • • • • • • ·>

.......•.. : ............................ ·> Stack1

: ............................ ·> StackO

Frame O

Frame M

Page 17: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Processor

L. ............. >I

··················>

Physical Address

Physical Memory

I I Frame O Frame 1

I I

~ ....... ~I Frame I Offset I·· Virtual

Address

Page # I Offset

····················~·····················~

~

~Page Table

Frc:tme Access

Virtual Address

........................ , ............................................... :

····················i·····················~

_ Physical

t Address

~ .....................................................

··•

:···········>

Frame M

Page 18: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

~ --~~-r---·· -_ -_____ U>_o -~ --------. 1 /3 r- A ._ - I G) -n m

0 -1

c:,no:,)> :CG)"'Tlm

i

o n OJ l>

-c QJ

OQ (D

--i QJ c-m

" '

-c -, 0 n (D V, V,

<

-c ::::r < V, -· n QJ

s: (D

3 0 -, <

Page 19: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Paging Questions

• With paging, what is saved/restored .on a process context s·witch?

- 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 20: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Paging and Sharing

• Can we use page tables to share memory between processes?

• Set page tables to point to same page frame

• Need core map

- Array of information about each physical page frame

- Set of processes pointing to that page frame

- When reference count goes to zero, can reclaim!

Page 21: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Question

•· How big a user stack should I allocate?

• What if some programs need a large stack and others need a small one?

Page 22: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Expand Stack on Reference

• When program references memory beyond end of stack

- Page fault into OS kernel

- Kernel allocates some additional memory

• How much?

- Remember to zero the memory to avoid

accidentally leaking information! ~, i () c - Modify page table .- ( ~--~ ><- C 10000()\

- Resume process b,~ be.,

7 Q Too ~

3

Page 23: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

UNIX fork seems inefficient

• Makes a complete copy of process

• Throw copy away on exec

• Do we need to make the copy?

- One solution: change the syscall interface!

Page 24: )> c.. c.. · 2017. 11. 10. · Address Translation Goals • Memory protection - Isolate process to its only memory - Prevent virus from re-writing machine instructions • Memory

Copy on Write

• Paging allows an efficient fork

- Copy page table of parent into child

- Mark all pages (in new/old page tables) as read-only

- Start child process; restart parent

- Trap into kernel on write (in child or parent)

- Copy page 0 /'- c..

- Mark~ as writeable t)~~rs ' 0 r ) ._ s ) <...Q__ ) <2._{ ~-c:: ~

- Resume execution c-~ .. ~.)"\ .. , c.u~"" "'"~~ (l___ )C <:.~ r + FF

l c.. S' J.. <.> .I" <?... - ,.