segmentation case studies o multics o pentium o unix o linux o windows memory management, part 3:...

41
Segmentation Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Post on 21-Dec-2015

226 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Segmentation

Case studieso MULTICS

o Pentium

o Unix

o Linux

o Windows

Memory management, part 3: outline

1Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 2: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Segmentation Several address spaces per

process a compiler needs segments for

o source texto symbol tableo constants segmento stacko parse treeo compiler executable code

Most of these segments grow during execution

symboltable

source textSource Text

constant table

parse tree

call stack

symboltable

2Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 3: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Users view of segments

Symboltable

Sourcetext

Constants

Parse tree Call

stack

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

3Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 4: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Segmentation - segment table

4Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 5: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Segmentation Hardware

5Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 6: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Segmentation vs. Paging

considerationconsideration PagingPaging SegmentationSegmentation

Need the program be aware of the technique ? no yesHow many per-process virtual address spaces ? 1 manyCan the total address space exceed physical memory ?

yes yes

Can procedures and data be distinguished ? no yes

Sharing of procedures among users facilitated ? no yesMotivation for the technique Get larger

linear space, eliminate external fragmentation

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

6Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 7: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Advantages:o Growing and shrinking independently.o Sharing between processes simplero Linking is easiero Protection easier

Disadvantages:o Pure segmentation --> external Fragmentation revisitedo Segments may be very large. What if they don't fit into

physical memory?

Segmentation pros and consSegmentation pros and cons

7Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 8: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Segmentation Architecture Logical address composed of the pair

<segment-number, offset>Segment table – maps to linear address space; each table

entry has:o basebase – contains the starting linear address where the segment resides

in memory.o limitlimit – 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.

8Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 9: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Segmentation Architecture (Cont.) Protection: each segment table entry contains:

o validation bit = 0 illegal segmento 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)

9Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 10: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Sharing of segments

10Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 11: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

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 provide a combination of segmentation and paging

11Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 12: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Segmentation

Case studieso MULTICS

o Pentium

o Unix

o Linux

o Windows

Memory management, part 3: outline

12Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 13: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

The MULTICS OS Ran on Honeywell computers Segmentation + paging Up to 218 segments Segment length up to 216 36-bit

words Each program has a segments table (itself a segment) Each segment has a page table

13Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 14: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

MULTICS data-structures

Segment 0 descriptor

Segment 1 descriptor

Segment 2 descriptor

Segment 3 descriptor

Segment 4 descriptor

18 bits

Page 0 entry

Page 1entry

Page 2 entry

Page table for segment 3

Page 0 entry

Page 1entry

Page 2 entry

Page table for segment 1

Main memory address of the page table Segment length(in pages)

6 bits 1 1 1 3 3

36 bits

Process descriptor segment

Segment descriptor

Page size:0 – 1024 word1 – 64 words 0 – paged

1 – not paged

misc

Protection bitsUnused

18 bits

18 bits

14Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 15: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

MULTICS memory reference procedure1. Use segment number to find segment descriptor

Segment table 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 memoryo if not a segment fault occurso 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.

15Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 16: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

MULTICS Address Translation Scheme

Segment number (18 bits)

Page number (6 bits)

Page offset (10 bits)

16Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 17: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

MULTICS TLB

Simplified version of the MULTICS TLB Existence of 2 page sizes makes actual TLB more complicated

17Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 18: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Segmentation

Case studieso MULTICS

o Pentium

o Unix

o Linux

o Windows

Memory management, part 3: outline

19Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 19: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Pentium: Segmentation + paging Segmentation with or without paging is possible 16K segments per process, segment size up to 4G 32-bit

words page size 4K 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 microprogram registers

Index

1 2Privilege level (0-3)0 = GDT/ 1 = LDT13

Pentium segment selector20Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 20: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Pentium- segment descriptors

Pentium code segment descriptor. Data segments differ slightly

21Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 21: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

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 descriptoro Base field of descriptor is added to offset (4k page-size)

22Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 22: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Intel Pentium address translation

10 10 12

Can cover up to 4 MBphysical address space

23Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 23: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Segmentation

Case studieso MULTICS

o Pentium

o Unix

o Linux

o Windows

Memory management, part 3: outline

24Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 24: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

UNIX process address space

Process A

BSSInit. Data

Text

Stack pointer

0

8K

20K

Process B

BSSInit. Data

Text08K

20K

Physical memory

Stack pointer

OS

25Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 25: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Memory-mapped file

Process A

BSSData

Text

Stack pointer

0

8K

20K

Process B

BSSData

Text08K

20K

Physical memory

Stack pointer

OS

Memory mapped file

Memory mapped file

26Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 26: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Unix memory management sys calls

Not specified by POSIX Common Unix system calls

o s=brk(addr) – change data segment size. (addr sepcified the first address following new size)

o a=mmap(addr,len,prot,flags,fd,offset) – map (open) file fd starting from offset in length len to virtual address addr (0 if OS is to set address)

o s=unmap(addr,len) – unmap a file (or a portion of it)

27Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 27: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Unix 4BSD 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

28Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 28: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Unix Page DaemonIt is assumed useful to keep a pool of free pagesfreeing of page frames is done by a pagedaemon - a

process that sleeps most of the time awakened periodically to inspect the state of memory -

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

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

29Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 29: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Page replacement - Unix a two-handed clock algorithm clears the reference bit first

with the first hand and frees pages with its second hand. It has the parameter of the “angle” between the hands - small angle leaves only “busy” pageso If page is referenced before 2’nd hand comes, it will not be freed

30Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 30: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Page replacement – Unix, cont'd if there is thrashing, the swapper process removes processes

to secondary storageo Remove processes idle for 20 sec or moreo If none – swap out the oldest process out of the 4 largest

Who get swapped back function of: o Time out of memoryo size

31Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 31: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Segmentation

Case studieso MULTICS

o Pentium

o Unix

o Linux

o Windows

Memory management, part 3: outline

32Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 32: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Linux processes

Each process gets 3GB virtual memory Remaining 1GB 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 (text, data, memory-mapped-files,…)

33Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 33: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

34Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

vm_area_struct

Process virtual address space partitioned to areas

struct vm_area_struct {unsigned long vm_start;unsigned long vm_end;unsigned long vm_flags;struct vm_area_struct *vm_next;/* plus some other fields */ };

Page 34: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Linux page tables organization

Directory Middle Page Offset

Directory

Page middle

directory Page tablePage

Selected word

This is the situation in Alpha. In Pentium, the page middle directory is degenerated.Expanded to 4-level indirect paging after Linux 2.6.10

35Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 35: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Linux main memory management

Kernel never swapped The rest: user pages, file system buffers, variable-size device

drivers The buddy algorithm is used. In addition:

o Linked lists of same-size free blocks are maintainedo To reduce internal fragmentation, a second memory allocation

scheme (slab allocator) manages smaller units inside buddy-blocks Demand paging (no pre-paging) Dynamic backing store management

36Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 36: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Linux page replacement algorithm

Variant of clock algorithm

Based on aging, pages are in either active or inactive list

37Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 37: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Segmentation

Case studieso MULTICS

o Pentium

o Unix

o Linux

o Windows

Memory management, part 3: outline

38Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 38: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

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?

39Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 39: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Win 2000: memory mngmt. concepts Each virtual page can be in one of following states:

o Free/invalid – Currently not in use, a reference causes access violationo Committed – code/data was mapped to virtual pageo Reserved – allocated to thread, not mapped yet. When a new thread

starts, 1MB of process space is reserved to its stacko Readable/writable/executable

Dynamic (just-in-time) backing store managemento Improves performance of writing modified data in chunkso Up to 16 pagefiles

Supports memory-mapped files Can use 4K or 4M pages Executable access pattern recorded for SuperFetch prepaging

40Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 40: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Win 2000: page replacement alg.

Processes have working setsworking 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)(i.e. the data structure WS) - o PF and WS < Min addadd to WSo PF and WS > Max removeremove 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 when the balance-set-managerbalance-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…)

41Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels

Page 41: Segmentation  Case studies o MULTICS o Pentium o Unix o Linux o Windows Memory management, part 3: outline 1 Ben-Gurion University Operating Systems,

Physical Memory Management (1)

Various page lists and transitions between them

43Ben-Gurion University Operating Systems, 2011, Danny Hendler and Amnon Meisels