38
38 1 Embedded Software Lab. Sungkyunkwan University Embedded Software Lab. Dongkun Shin 전공핵심실습1:운영체제론 Chapter 8. Kernel Memory Management

Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

1

Embedded Software Lab.

Sungkyunkwan University

Embedded Software Lab.

Dongkun Shin

전공핵심실습1:운영체제론Chapter 8. Kernel Memory Management

Page 2: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

2

Embedded Software Lab.

Contents

Page 3: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

3

Embedded Software Lab.

• Physical Address– Used to address memory cells in memory chips.

• Linear Address (Virtual Address)– Single 32-bit unsigned integer used to address up to 4GB.

• Logical Address– The address that embodies 80x86 segmented architecture

Linux Memory Address

Physical Memory Cells

Paging Unit

Pages

Segmentation Unit

SegmentsAbstraction

Page 4: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

4

Embedded Software Lab.

• Paging Unit– Translate Linear Address Physical Address

– Check access privilege of Linear Address

• Kernel or User mode, write or read privilege

• Page– Linear addresses are grouped in fixed-length intervals

• Page Frame– Corresponding memory cells to the page

• Page Table– The data structures that map linear to physical addresses

– Stored in main memory

Paging Terminology

Typical 4KB in 32-bit machine

Page 5: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

5

Embedded Software Lab.

• Q) Why 2-level?– A) To save memory

Regular Paging

Each entry has same format

• Present Flag• 20bit page frame addr• Accessed Flag• Dirty Flag• Read/Write Flag• User/Supervisor Flag• Page Size Flag• Global Flag

Page 6: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

6

Embedded Software Lab.

• Extended Paging– Is used to translate large contiguous linear address ranges

into corresponding physical ones.

– Page Size flag = 1

– Page size : 4MB

Extended Paging

Page 7: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

7

Embedded Software Lab.

• Page table entry cache (Linear Physical address)

Translation Lookaside Buffer

Page 8: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

8

Embedded Software Lab.

• Compatible with both 32bit and 64bit paging

• 4-level paging model

Paging in Linux

# of bit Depending on Arch

Page 9: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

9

Embedded Software Lab.

• Defined in <asm/page.h>

Paging Macro in Linux

ex) alpha

Page 10: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

10

Embedded Software Lab.

• Kernel must build a physical addresses map during the initialization

• Specifies which physical address ranges are usable and which are unavailable– Some address range are used by I/O shared memory or BIOS

data

• Reserved Memory Space– Unavailable address range

– Page frame containing kernel’s code and initialized data structures

• Never be dynamically assigned or swapped to disk.

Physical Memory Layout

Page 11: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

11

Embedded Software Lab.

• Each Process has different Global Directory Table

• But ¼ of Global Directory entry is same

Process Page Table

Page 12: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

12

Embedded Software Lab.

1. Create Mapping– In initial phase work with Real Mode (16bit)

– To access both Real and Protect mode mapping

– Initialize with : Linear A. 0x00000000~0x007FFFFF Physical A. 0x00000000~0x007FFFFF

– Linear A. 0xC0000000~0xC07FFFFFPhysical A. 0x00000000~0x007FFFFF

Procedure for kernel to create page table

Page 13: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

13

Embedded Software Lab.

1. Create Mapping

2. Kernel creates the desired mapping by filling all the swapper_pg_dir entries with zeroes.

3. startup_32() : Enable paging unit

4. paging_init() : Initialize Master Kernel page Global Directory

Procedure for kernel to create page table

Page 14: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

14

Embedded Software Lab.

• How the kernel allocates dynamic memory– Contiguous memory area

• Page Frame Management

• Memory Area Management

– Non-contiguous memory area

• Non-contiguous Memory Area Management

Memory Management

Page 15: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

15

Embedded Software Lab.

• Describe current status of the page frame

Page Descriptor

Page 16: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

16

Embedded Software Lab.

• Documented/documentation/vm/pagemap.txt

Page Flag

Page 17: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

17

Embedded Software Lab.

• Node Descriptor– Each node has a descriptor of type pg_data_t

– All node descriptors are stored in a singly linked list, whose first implement is pointed by the pgdat_list variable

– node_zones: Array of zone descriptors of the node

– node_start_pfn: Index of the first page frame in the node

– node_id: Identifier of the node

Node

Page 18: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

18

Embedded Software Lab.

• DMA ( Direct Memory Access ) processor can access to only first 16MB of RAM: zone_dma

• Some memory region is linearly mapped:– zone_normal

• Some memory region cannot be reached by kernel directly– zone_highmem (Kernel reaches anywhere in 64bit)

• Zone descriptor: struct_zone(/include/linux/mmzone.h)

Zone

Page 19: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

19

Embedded Software Lab.

Zoned page frame allocator

Page 20: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

20

Embedded Software Lab.

• Flag used in request for page frames

GFP flag

Page 21: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

21

Embedded Software Lab.

• All free page frames are grouped into 11 lists of blocks that contain groups of 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, and 1024 contiguous page frames.(1024 page frames = 4MB) Method to solve the external fragmentation problem.

Buddy Algorithm

Page 22: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

22

Embedded Software Lab.

• Prevent from internal framgenetation

• Used for allocation of little memory like few bytes or few hundreds bytes.

Slab Allocator

Page 23: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

23

Embedded Software Lab.

• Create slab – cache_grow()

– Request for new object &&

– No free object in cache

• Release slab – cache_destroy()

– Too many free object ||

– Reclamation by timer function

Slab Allocator (cont.)

Page 24: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

24

Embedded Software Lab.

• Allocate physically contiguous memory.

• Used for small memory allocation.

kmalloc()

Page 25: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

25

Embedded Software Lab.

• Allocate virtually contiguous memory.– Non-contiguous memory area management

• Non-contiguous memory area management– The main advantage is to avoid external fragmentation.

– The disadvantage is to fiddle with the kernel page tables.

• Cannot reach outside of processor– Each page frames allocated by vmalloc() must be accessed by

referencing to MMU MMU is in-processor unit.

vmalloc()

Page 26: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

26

Embedded Software Lab.

• vmalloc() only allocates vm_struct

Descriptor of non-contiguous memory areas

vmlist vm_struct

next

vm_struct

next

vm_struct

next

/include/linuxextern variable

– addr

• Linear address of the first memory cell of the area

– nr_pages

• Number of pages

– struct page **

• pointer of array of pages

– next

Page 27: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

27

Embedded Software Lab.

Buddy Allcator

Page 28: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

28

Embedded Software Lab.

• 간이 Buddy Allocator의 구현

• 전체 Page Frame수 : 16

• 초기 Free List 및 메모리 상태

실습

4

3

2

1

0

0

• 실습내용– 다음 각각의 커맨드 이후의 Free List 상태 출력

– alloc(5p), alloc(1p), alloc(2p), alloc(2p), alloc(1p), alloc(1p), free(10), free(12), free(14), free(0)

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

FreeAllocated

Page 29: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

29

Embedded Software Lab.

실습

4

3

2

1

0

8

alloc(5p)

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

FreeAllocated

Page 30: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

30

Embedded Software Lab.

실습

4

3

2

1

0

12

alloc(5p), alloc(1p)

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

FreeAllocated

10

9

Page 31: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

31

Embedded Software Lab.

실습

4

3

2

1

0

12

alloc(5p), alloc(1p), alloc(2p)

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

FreeAllocated

9

Page 32: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

32

Embedded Software Lab.

실습

4

3

2

1

0

14

alloc(5p), alloc(1p), alloc(2p), alloc(2p)

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

FreeAllocated

9

Page 33: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

33

Embedded Software Lab.

실습

4

3

2

1

0

14

alloc(5p), alloc(1p), alloc(2p), alloc(2p), alloc(1p)

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

FreeAllocated

Page 34: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

34

Embedded Software Lab.

실습

4

3

2

1

0 15

alloc(5p), alloc(1p), alloc(2p), alloc(2p), alloc(1p), alloc(1p)

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

FreeAllocated

Page 35: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

35

Embedded Software Lab.

실습

4

3

2

1

0 15

alloc(5p), alloc(1p), alloc(2p), alloc(2p), alloc(1p), alloc(1p), free(10)

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

FreeAllocated

10

Page 36: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

36

Embedded Software Lab.

실습

4

3

2

1

0 15

alloc(5p), alloc(1p), alloc(2p), alloc(2p), alloc(1p), alloc(1p), free(10), free(12)

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

FreeAllocated

10 12

Page 37: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

37

Embedded Software Lab.

실습

4

3

2

1

0 15

alloc(5p), alloc(1p), alloc(2p), alloc(2p), alloc(1p), alloc(1p), free(10), free(12), free(14)

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

FreeAllocated

10 12

14

14

12

Page 38: Introduction to Tizen Platform - SKKUnyx.skku.ac.kr/wp-content/uploads/2016/03/Lecture8-Week... · 2016-05-17 · 38 4 Embedded Software Lab. • Paging Unit –Translate Linear Address

38

38

Embedded Software Lab.

실습

4

3

2

1

0

alloc(5p), alloc(1p), alloc(2p), alloc(2p), alloc(1p), alloc(1p), free(10), free(12), free(14), free(0)

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

FreeAllocated

10

12

0