Memory: Segmentation

hungryhorsecabinΛογισμικό & κατασκευή λογ/κού

14 Δεκ 2013 (πριν από 3 χρόνια και 9 μήνες)

79 εμφανίσεις


Segmentation


Case studies

o
MULTICS

o
Pentium

o
Unix

o
Linux

o
Windows

Memory
management:
outline

1

Operating Systems, 2010, Danny Hendler & Amnon Meisels

Segmentation


Several

address spaces
per
process


a compiler needs segments for

o
source text

o
symbol table

o
constants segment

o
stack

o
parse tree

o
compiler executable code


Most of these segments
grow

during execution

symbol

table

source text

Source Text

constant table

parse tree

call stack

symbol

table

2

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

User’s view of segments

Symbol

table

Source

text

Constants

Parse
tree

Call
stack

A segmented memory allows each table to grow or shrink independently of
the other tables.

3

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Segmentation
-

segment table

4

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Segmentation Hardware

5

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Segmentation vs. Paging

consideration

Paging

Segmentation

Need the program be aware of the technique ?

no

yes

How many per
-
process virtual address spaces ?

1

many

Can the total address space exceed physical
memory ?

yes

yes

Can procedures and data be distinguished ?

no

yes

Sharing of procedures among users facilitated ?

no

yes

Motivation for the technique

Get larger
linear space,
eliminate
external
fragmentation

Programs and data in
logical independent
address spaces, sharing
and protection made
simpler

6

Operating Systems,
2010
, Danny Hendler & Amnon Meisels


Advantages:

o
Growing and shrinking independently

o
Sharing between processes simpler

o
Linking is easier

o
Protection easier


Disadvantages:

o
Crude partition (each segment can be very large or
very small)

o
Pure segmentation
--
> external Fragmentation
revisited




Segmentation pros and cons

7

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Segmentation Architecture


Logical address composed of the pair

<segment
-
number, offset>


Segment table



maps to linear address space; each table
entry has:

o
base



contains the starting linear address where the segment
resides in memory

o
limit



specifies 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;

segment number
s

is legal if
s

< STLR

8

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Segmentation Architecture (
Cont
.)


Protection
: each segment table entry contains:

o
validation bit =
0


illegal segment

o
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 (external
fragmentation problem)

9

Operating Systems,
2009
, Danny Hendler & Amnon Meisels

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Sharing of segments

10

Operating Systems,
2009
, Danny Hendler & Amnon Meisels

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Segmentation with Paging


Segments may be too large


Cause external fragmentation


The two approaches may be combined:

o
Segment table

o
Pages inside a segment

o
Solves fragmentation problems


Most systems today use a combination of
segmentation and paging

11

Operating Systems,
2010
, Danny Hendler & Amnon Meisels


Segmentation


Case studies

o
MULTICS

o
Pentium

o
Unix

o
Linux

o
Windows

Memory
management:
outline

12

Operating Systems,
2009
, Danny Hendler & Amnon Meisels

The MULTICS OS


Ran on Honeywell computers


Segmentation + paging


Up to
2
18

segments


Segment length up to
2
16

36
-
bit

words


Each program has a segments table (itself a segment)


Each segment has a page table

13

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

MULTICS data
-
structures

Segment
0
descriptor

Segment
1
descriptor

Segment
2
descriptor

Segment
3
descriptor

Segment
4
descriptor

18
bits

Page
0
entry

Page
1
entry

Page
2
entry

Page table for segment
3

Page
0
entry

Page
1
entry

Page
2
entry

Page table for segment
1

Main memory address of the page table

Segment length

9
bits

1

1

1

3

3

36
bits

Process descriptor segment

Segment descriptor

Page size:

0


1024
word

1


64
words

0


paged

1


not paged

misc

Protection bits

Unused

18
bits

18
bits

14

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

MULTICS memory reference procedure

1
. Use segment number to find segment descriptor


Descriptor segment is itself paged because it may be large. The
descriptor
-
base
-
register points to its page table

2
. Check if segment’s page table is in memory

o
if not a segment fault occurs

o
if there is a protection violation TRAP (fault)

3
. page table entry examined, a page fault may occur

o
if page is in memory the start
-
of
-
page address is extracted from
page table entry

4
. offset is added to the page origin to construct main
memory address

5
. perform read/store etc.

15

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

MULTICS Address Translation Scheme

Segment number (
18
bits)

Page number (
6
bits)

Page offset (
10
bits)

16

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

MULTICS TLB


Simplified version of the
MULTICS

TLB


Existence of
2
page sizes makes actual TLB more complicated

17

Operating Systems,
2010
, Danny Hendler & Amnon Meisels


Segmentation


Case studies

o
MULTICS

o
Pentium

o
Unix

o
Linux

o
Windows

Memory
management:
outline

18

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Pentium: Segmentation + paging


Segmentation with or without paging is possible


16
K segments, segment size up to
1
G
32
-
bit words


page size
4
K


A single global GDT, each process has its own LDT


6
segment registers may store (
16
bit) segment selectors:
CS, DS, SS…


When the selector is loaded to a segment register, the
corresponding descriptor is stored in micro
-
program
registers

Index

1

2

Privilege level (
0
-
3
)

0
= GDT/
1
= LDT

13

Pentium segment selector

19

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Pentium
-

segment descriptors

Pentium code segment descriptor. Data segments differ slightly



20

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Pentium
-

Forming the linear address


Segment descriptor is in internal (microcode) register


If segment is not zero (TRAP) or paged out (TRAP)

o
Offset size is checked against
limit

field of descriptor

o
Base field of descriptor is added to offset (
4
k page
-
size)

21

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Intel Pentium address translation

10

10

10

12

Can cover up to
4
MB

22

Operating Systems,
2010
, Danny Hendler & Amnon Meisels


Segmentation


Case studies

o
MULTICS

o
Pentium

o
Unix

o
Linux

o
Windows

Memory
management:
outline

23

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

UNIX process address space

Process A

BSS

Data

Text

Stack pointer

0

8
K

20
K

Process B

BSS

Data

Text

0

8
K

20
K

Physical memory

Stack pointer

OS

24

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Memory
-
mapped file

Process A

BSS

Data

Text

Stack pointer

0

8
K

20
K

Process B

BSS

Data

Text

0

8
K

20
K

Physical memory

Stack pointer

OS

Memory
mapped file

Memory
mapped file

25

Operating Systems,
2009
, Danny Hendler & Amnon Meisels

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Unix
4
BSD memory organization

Main memory

Page frame
3

Page frame
2

Page frame
1

Page frame
0

Core map entries,
one per page frame

Core map entry

Index of next entry

Index of previous entry

Used when page
frame is on free list

Disk block number

Disk device number

Block hash code

Index into proc table

Text/data/stack

Offset within segment

Misc.

Free

In transit

Wanted

Locked

Kernel

26

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Unix Page Daemon


It is assumed useful to keep a pool of
free pages


freeing of page frames is done by a
page daemon

-

a
process that sleeps most of the time


awakened periodically to inspect the state of memory
-

if less than ¼ of page frames are free, then it
frees page
frames

(a limited amount at a time)


this strategy performs better than evicting pages when
needed (and writing the modified to disk in a hurry)


The net result is the use of all of available memory as
page
-
pool


Uses a global clock algorithm


two
-
handed clock

27

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Page replacement
-

Unix


a two
-
handed clock algorithm clears the reference bit first
with the first hand and grabs with its second hand. It has the
parameter of the “angle” between the hands
-

small angle
leaves only “busy” pages



if there is thrashing, the swapper process removes processes
to secondary storage

o
Remove processes idle for
20
sec or more

o
If none


swap out the oldest process out of the
4
largest


Getting swapped back is a function of:

o
Time out of memory

o
size

28

Operating Systems,
2010
, Danny Hendler & Amnon Meisels


Segmentation


Case studies

o
MULTICS

o
Pentium

o
Unix

o
Linux

o
Windows

Memory
management:
outline

29

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Linux processes


Each process gets
3
GB virtual memory


Remaining
1
GB for kernel and page tables


Virtual address space composed of areas with same
protection, paging properties (
pageable

or not, direction of
growth)


Each process has a linked list of areas, sorted by virtual
address

30

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Linux page tables organization

Directory

Middle

Page

Offset

Directory

Page
middle
directory

Page table

Page

Selected word

This is the situation in Alpha. In Pentium, the page middle directory is degenerated.

31

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Linux main memory management


Kernel never swapped


The rest: user pages, file system buffers, variable
-
size device
drivers


T
he buddy algorithm is used to enable allocation of larger
-
than
-
a
-
page contiguous chunks. In addition:

o
Linked lists of same
-
size free blocks are maintained

o
To reduce internal fragmentation, a second memory allocation
scheme manages smaller units inside buddy
-
blocks


Demand paging


Dynamic backing store management


32

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Linux page replacement algorithm


Search for process with maximum number of page frames

o
Check pages in increasing order of virtual address (starting from
where we left off before)

o
Apply clock algorithm (except for order which is not FIFO, using
not
-
frequently
-
used and categories of swapping needs)


bdflush

process writes dirty pages to disk (if too many
pages are dirty)

33

Operating Systems,
2010
, Danny Hendler & Amnon Meisels


Segmentation


Case studies

o
MULTICS

o
Pentium

o
Unix

o
Linux

o
Windows

Memory
management:
outline

34

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Win
2000
: virtual address space


Virtual address space layout for
3
user processes


White areas are private per process


Shaded areas are shared among all processes

What are the pros/cons of mapping kernel area into process address space?

35

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Implementation of Memory Management

A page table entry for a mapped page on the Pentium

36

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Win
2000
: memory
mngmt
. concepts


Each virtual page can be in one of the following states:

o
Free



Currently not in use, an access causes page fault

o
Committed



code/data was mapped to virtual page

o
Reserved



allocated to thread, not mapped yet. When a new
thread starts,
1
MB of process space is reserved to it


Dynamic backing store management


Supports memory
-
mapped files

37

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Win
2000
: memory
-
mapped files


Mapped regions with their shadow pages on disk


The
lib.dll

file is mapped into two address spaces simultanously

38

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Win
2000
: page replacement alg.


Processes have
working sets

defined by two parameters
-

the
minimal and maximal # of pages



the WS of processes is updated at the occurrence of each
page fault
(i.e. the data structure WS)

-


o

PF and WS < Min
add

to WS

o

PF and WS > Max
remove

from WS


If a process thrashes, its working set size is increased



Memory is managed by keeping a number of free pages, which is a
complex function of memory use, at all times (at most one disk reference
per PF)


when the
balance
-
set
-
manager

is run (every second) and it
needs to free pages
-

o

surplus pages (to the WS) are removed from a process (large
background before small foreground…)

o

Pages `age
-
counters’ are maintained (on a multi
-
processor refs bits
don’t work since they are local…)

39

Operating Systems,
2010
, Danny Hendler & Amnon Meisels

Physical Memory Management

Various page lists and transitions between them

40

Operating Systems,
2010
, Danny Hendler & Amnon Meisels