virtual & physical address
TRANSCRIPT
-
8/11/2019 Virtual & Physical Address
1/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006
Stefan Buettcher
D Memory Management
Virtual an !"ysi#al $ resses
Physical addresses are provided by the hardware: one physical address space per machine; valid addresses are usually between 0 and some machine-
specific ma imum; not all addresses have to belon! to the machine"s main memory;
other hardware devices can be mapped into the address space.
Virtual #or logical $ addresses are provided by the %S &ernel: one virtual address space per process; addresses may start at 'ero( but not necessarily; space may consist of several segments #i.e.( have !aps$.
Address translation #a.&.a. address binding $ meansmappin! virtual addresses to physical addresses.
-
8/11/2019 Virtual & Physical Address
2/22
-
8/11/2019 Virtual & Physical Address
3/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006
Stefan Buettcher
D Memory Management
$ Simple $ ress %ranslation &e#"anism
%"is is really not a goo solution'
-
8/11/2019 Virtual & Physical Address
4/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006
Stefan Buettcher
D Memory Management
(ynami# )elo#ation
he memory mana!ement unit #// $ of the 1+ contains arelocation re!ister.
2henever a thread tries to access a memory location #throu!h avirtual address$( the value of the relocation re!ister is added to thevirtual memory address 3 dynamic bindin!.
he &ernel maintains a separate relocation value for each process#as part of the virtual address space$; chan!es the relocationre!ister at every conte t switch.
+roperties: all pro!rams can start at virtual address 0;
the &ernel can relocate a process w4o chan!in! the pro!ram; &ernel can allocate physical memory dynamically; each virtual address space is still conti!uous in physical mem.
-
8/11/2019 Virtual & Physical Address
5/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006
Stefan Buettcher
D Memory Management
(ynami# )elo#ation
-
8/11/2019 Virtual & Physical Address
6/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006
Stefan Buettcher
D Memory Management
(ynami# )elo#ation
-
8/11/2019 Virtual & Physical Address
7/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006
Stefan Buettcher
D Memory Management
Segmentation
5n some systems( a virtual address space can consist of severalindependent segments .
6 lo!ical address then consists of two parts:#se!ment 5)( address within se!ment$
*ach se!ment can !row or shrin& independently of the other se!ments in the
same address space; has its own memory protection attributes.
6 process may have separate se!ments for code( data( stac&.
-
8/11/2019 Virtual & Physical Address
8/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006
Stefan Buettcher
D Memory Management
Segmentation
-
8/11/2019 Virtual & Physical Address
9/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006
Stefan Buettcher
D Memory Management
!aging
*ach virtual address space is divided into fi ed-si'e chun&s called pages .
he physical address space is divided into fi ed-si'e chun&s calledframes .
+a!es have same si'e as frames.
he &ernel maintains a page table #or page-frame table $ for eachprocess( specifyin! the frame within which each pa!e is located. he 1+ "s memory mana!ement unit #// $ translates virtual
addresses to physical addresses on-the-fly for every memoryaccess.
+roperties: relatively simple to implement #in hardware$; virtual address space need not be physically conti!uous.
-
8/11/2019 Virtual & Physical Address
10/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006
Stefan Buettcher
D Memory Management
!aging
-
8/11/2019 Virtual & Physical Address
11/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006 Stefan Buettcher
D Memory Management
!aging
D M M
-
8/11/2019 Virtual & Physical Address
12/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006 Stefan Buettcher
D Memory Management
Com*ining Segmentation an !aging
Se!mentation and pa!in! can be combined so that a virtual addressspace consists of multiple se!ments( and each se!ment consists of multiple pa!es.
D M M g t
-
8/11/2019 Virtual & Physical Address
13/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006 Stefan Buettcher
D Memory Management
Com*ining Segmentation an !aging
D Memory Management
-
8/11/2019 Virtual & Physical Address
14/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006 Stefan Buettcher
D Memory Management
!"ysi#al &emory $llo#ation
+o to allo#ate p"ysi#al memory-
+hysical memory can be allocated in different ways.
7ariable allocation si'e: always !ive a process e actly as much memory as it re uests space trac&in! and placement are very comple placement heuristics are necessary: first fit( best fit( worst fit ris& of e ternal fra!mentation
8i ed allocation si'e: allocate memory in fi ed-si'e chun&s space trac&in! and placement are very simple ris& of internal fra!mentation
D Memory Management
-
8/11/2019 Virtual & Physical Address
15/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006 Stefan Buettcher
D Memory Management
&emory !rote#tion
*nsure that each process can only access the physical memory thatits virtual memory is bound to.
2hat if a thread tries to access memory outside its own virtualaddress space9
// limit re!ister is used to chec& every memory access: for simple dynamic relocation( the limit re!ister contains the
ma imum virtual address of the runnin! process; with pa!in!( the limit re!ister contains the ma imum pa!e
number for the runnin! process.
// !enerates e ception when a thread is tryin! to access amemory address beyond this limit.
#5n achos: AddressErrorException $
D Memory Management
-
8/11/2019 Virtual & Physical Address
16/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006 Stefan Buettcher
D Memory Management
&emory !rote#tion
5n addition( access to certain portions of the address space maybe restricted:
read-only memory e ecute-only memory
2hen pa!in! is used: the pa!e table includes fla!s that define the permitted access
modes for each pa!e; // raises e ception when permissions are violated #e.!.( thread
tries to write to read-only pa!e$.
D Memory Management
-
8/11/2019 Virtual & Physical Address
17/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006 Stefan Buettcher
D Memory Management
&emory &anagement. )oles of OS an &&U
// #/emory /ana!ement nit( part of 1+ $: translates virtual addresses to physical addresses; chec&s for protection violations; raises e ceptions when necessary #e.!.( write operation on read-
only memory re!ion$.
%peratin! system: saves4restores // state durin! conte t switch #limit re!ister(
pa!e tables( ...$ handles e ceptions raised by the // mana!es and allocates physical memory
D Memory Management
-
8/11/2019 Virtual & Physical Address
18/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006 Stefan Buettcher
y g
$ ress %ranslation
* ecutin! a sin!le machine instruction may involve one or morememory access operations: %ne to fetch the instruction; 'ero ormore to fetch the operand#s$.
Simple dynamic relocation with relocation re!ister does not affectthe total number of memory operations.
6ddress translation throu!h a pa!e table doubles the number ofmemory operations: *very memory access is preceded by a pa!etable loo&up. 6 simple pa!e-table-based address translation scheme can cut
the e ecution speed in half.
/ore comple translation schemes mi!ht result in an evenmore severe slowdown.
Solution: Use a cache!
D Memory Management
-
8/11/2019 Virtual & Physical Address
19/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006 Stefan Buettcher
y g
%ranslation /oo asi e 1uffer
he Translation Lookaside uffer # B$ is a fast( fully-associativeaddress translation cache in the // .
6 B hit avoids a memory access due to pa!e table loo&up causedby a virtual memory access.
*ach entry in the B contains a pair of the form
#pa!e number( frame number$ and some additional data( such as protection bits. he B is on the 1+ ; a B access is much faster than a memory
access.
5f the entry for a !iven pa!e cannot be found in the B( the pa!etable has to be ueried and an entry in the B is replaced. 5n most systems( this is all done by the // ; in
achos( this is done inside the &ernel #your code$.
D Memory Management
-
8/11/2019 Virtual & Physical Address
20/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006 Stefan Buettcher
S"are Virtual &emory
7irtual memory allows address spaces to overlap # shared memory $:
wo or more processes share the same physical memory.
Shared memory: allows to use memory more efficiently #e.!.( when loadin! more
than one copy of the same pro!ram into memory$ is a mechanism for inter-process communication #5+1$.
he unit of sharin! can be a pa!e or a se!ment.
Shared memory in 5 :shmget #create a new shared memory re!ion or obtain a handle toan e istin! one$; shmat #attach to an e istin! shared mem. =e!ion$;
shmdt #detach$( shmctl #chan!e attributes( delete$
D Memory Management
-
8/11/2019 Virtual & Physical Address
21/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006 Stefan Buettcher
ernel $ ress Spa#e
here are several possibilities to include the &ernel into the bi!!ermemory mana!ement picture. ernel in p"ysi#al a ress spa#e disable // in &ernel
mode( enable // in user mode; to access process data( the&ernel must interpret pa!e tables without hardware support; %Smust always be in physical memory # memory-resident $.
ernel in separate virtual a ress spa#e // has separatestate for user mode and &ernel mode; accessin! process data israther difficult; parts of the &ernel data may be non-resident.
ernel s"ares virtual a ress spa#e it" ea#" pro#ess usememory protection mechanisms to isolate &ernel from userprocesses; accessin! process data is trivial; parts of the &erneldata may be non-resident.
D Memory Management
-
8/11/2019 Virtual & Physical Address
22/22
CS350 Operating SystemsUniversity of Waterloo, Fall 2006 Stefan Buettcher
ernel $ ress Spa#e
/ost common solution:ernel shares address space with each process.
)isadvanta!e:ess space for user space processes #parts of the virtual address
space are occupied by the &ernel$. %n ? -bit systems( this is not a
problem. %n A -bit systems( it mi!ht be.nder A -bit inu ( the &ernel traditionally !ets C DB of the total
address space; the other A DB are for the user process.
2hen &ernel shares address space with user process: ryin! toaccess &ernel data does result in protection violation( not in invalidaddress e ception.