project 3. virtual memory - androbenchcsl.skku.edu/uploads/sse3044s19/project3.pdf · 2019. 4....

35
SSE3044 Introduction to Operating Systems Prof. Jinkyu Jeong Project 3. Virtual Memory 2019.04.17 (Wed.) TA) 이규선([email protected]) 송원석([email protected])

Upload: others

Post on 24-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

SSE3044 Introduction to Operating SystemsProf. Jinkyu Jeong

Project 3. Virtual Memory

2019.04.17 (Wed.)

TA)

이규선([email protected])

송원석([email protected])

Page 2: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project Plan

2

• Project 0: booting (2nd week, 1 week)

• Project 1: system call (3rd week, 2 weeks)– system call to set process priority

– system call & user command (ps)

• Project 2: CPU scheduling (5th week, 2 weeks)

• Project 3: virtual memory (7th week, 3 weeks)

• Project 4: page replacement (10th week, 3 weeks)

• Project 5: file systems (13th week, 2 weeks)

Page 3: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

xv6 Memory Layout

3

Page 4: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

How Physical Memories Initialized in xv6

4

• main() of main.c

These two functions divides & manages physical memories with pages

Page 5: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

How Physical Memories Initialized in xv6 (cont’d)

5

• kinit1() sets up for lock-less allocation in the first 4MB• kinit2() arranges for more memory (until PHYSTOP) to be allocatable (224MB)

• freerange() kfree() with page size unit

• kfree() fills page with 1s, and put it into freelist (page pool)

Page 6: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

How Physical Memories Allocated in xv6

6

• fork() creates a child with exactly the same memory contents as the parent

• allocproc() allocates kernel stack• copyuvm() copys parent’s page table

Page 7: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Background on mmap()

7

• The mmap system call is used to create new memory mappings in the calling process’s virtual address space

• addr: If NULL, automatic mapping. If not, takes it as a hint about where to place the mapping

• length: length of the mapping

• prot: PROT_EXEC, PROT_READ, PROT_WRITE, PROT_NONE

Page 8: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Background on mmap() (cont’d)

8

• The mmap system call is used to create new memory mappings in the calling process’s virtual address space

• flags: MAP_PRIVATE, MAP_SHARED, MAP_ANONYMOUS, MAP_FIXED, ...

• fd: file descriptor

• offset: offset of the file

Page 9: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Example code with mmap()

9

• MAP_POPULATE

• ! MAP_POPULATE

Page 10: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

How file mmap() Works

10

• Private file mapping with MAP_POPULATE

• mmap(0, 8192, PROT_READ, MAP_POPULATE, fd, 4096)– mmap 2pages

Physical MemoryVirtual Memory

P1

Stack

text

data

heapfile

Page 11: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

How file mmap() Works

11

• Private file mapping with MAP_POPULATE

• mmap(0, 8192, PROT_READ, MAP_POPULATE, fd, 4096)– mmap 2pages

Physical MemoryVirtual Memory

P1

Stack

text

data

heap

mmaped region

page

page

file

Page 12: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

How file mmap() Works

12

• Private file mapping with MAP_POPULATE

• mmap(0, 8192, PROT_READ, MAP_POPULATE, fd, 4096)– mmap 2pages

Physical MemoryVirtual Memory

P1

Stack

text

data

heap

mmaped region

page

page

file

offset with 4096

Page 13: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

How file mmap() Works

13

• Private file mapping with MAP_POPULATE

• mmap(0, 8192, PROT_READ, MAP_POPULATE, fd, 4096)– mmap 2pages

Physical MemoryVirtual Memory

P1

Stack

text

data

heap

mmaped region

page

page

Page table

file

Page 14: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

How file mmap() Works (cont’d)

14

• Private file mapping without MAP_POPULATE

• mmap(0, 8192, PROT_READ, 0, fd, 4096)– mmap 2pages

Physical MemoryVirtual Memory

P1

Stack

text

data

heapfile

Page 15: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

How file mmap() Works (cont’d)

15

• Private file mapping without MAP_POPULATE

• mmap(0, 8192, PROT_READ, 0, fd, 4096)– mmap 2pages

Physical MemoryVirtual Memory

P1

Stack

text

data

heap

mmaped region

mmap() finished

file

Page 16: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

How file mmap() Works (cont’d)

16

• Private file mapping without MAP_POPULATE

• mmap(0, 8192, PROT_READ, 0, fd, 4096)– mmap 2pages

Physical MemoryVirtual Memory

P1

Stack

text

data

heap

mmaped region

page fault(no corresponding

page table)

file

Page 17: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

How file mmap() Works (cont’d)

17

• Private file mapping without MAP_POPULATE

• mmap(0, 8192, PROT_READ, 0, fd, 4096)– mmap 2pages

Physical MemoryVirtual Memory

P1

Stack

text

data

heap

mmaped region

page fault(no corresponding

page table)page

file

offset with 4096

Page 18: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

How Anonymous mmap() Works

18

• Private anonymous mapping with MAP_POPULATE

• mmap(0, 8192, PROT_READ, MAP_POPULATE, -1, 0)– mmap 2pages

• Mostly same, but allocates page filled with 0

Physical MemoryVirtual Memory

P1

Stack

text

data

heap

mmaped region

page

page

Page table

Page 19: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3. Implement Simple mmap() Syscall on xv6

19

- Begin with clean xv6 code (do not overwrite on project 2)

- What your code should handle

1. mmap() syscall

2. Page fault handler

3. munmap() syscall

4. freemem() syscall

5. Some extras

Page 20: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3-1. mmap() Syscall on xv6

20

- Simple mmap()’s assumptions

1. addr is always page-aligned

1. if addr is NULL, should do automatic mapping

2. if addr is given & flag is MAP_FIXED, should map into according

address

2. length is also a multiple of pagesize

3. prot can be PROT_READ or PROT_READ|PROT_WRITE

1. prot should be match with file’s open flag

Page 21: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3-1. mmap() Sycall on xv6

21

- Simple mmap()’s assumptions

4. flags can be given with the combinations1. MAP_PRIVATE should be given always

2. MAP_FIXED should be given to non-NULL addr

3. If MAP_ANONYMOUS is given, it is anonymous mapping

4. If MAP_ANONYMOUS is not given, it is file mapping

5. If MAP_POPULATE is given, allocate physical page & make page table for whole mapping area.

6. If MAP_POPULATE is not given, just record it’s mapping area.

1. If page fault occurs to according area (access to mapping area’s virtual address), allocate physical page & make page table to according page

7. Other flags will not be used

5. fd is given for file mappings, if not, it should be -1

6. offset is given for file mappings, if not, it should be 0

7. MMAPBASE of each process’s virtual address is 0x40000000

Page 22: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3-1. mmap() Sycall on xv6

22

struct mmap_area

file *f;addr;length;offset;prot;flags;proc *p;

In the case of populate...

P1

P2

Virtual Memory

Virtual Memory

MMAPBASE

mmap(0, 8192, PROT_READ, MAP_POPULATE, fd, 4096)

Physical Memory

Page 23: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3-1. mmap() Sycall on xv6

23

struct mmap_area

file *f;addr;length;offset;prot;flags;proc *p;

In the case of populate...

P1

P2

Virtual Memory

Virtual Memory

MMAPBASE

mmap(0, 8192, PROT_READ, MAP_POPULATE, fd, 4096)

8192

Physical Memory

Page 24: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3-1. mmap() Sycall on xv6

24

struct mmap_area

file *f;addr;length;offset;prot;flags;proc *p;

In the case of populate...

P1

P2

Virtual Memory

Virtual Memory

MMAPBASE

mmap(0, 8192, PROT_READ, MAP_POPULATE, fd, 4096)

8192

Physical Memory

pagepage

get physical page

Page 25: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3-1. mmap() Sycall on xv6

25

struct mmap_area

file *f;addr;length;offset;prot;flags;proc *p;

In the case of populate...

P1

P2

Virtual Memory

Virtual Memory

MMAPBASE

mmap(0, 8192, PROT_READ, MAP_POPULATE, fd, 4096)

8192

Physical Memory

pagepage

file

read

Page 26: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3-1. mmap() Sycall on xv6

26

P1

struct mmap_area

file *f;addr;length;offset;prot;flags;proc *p;

In the case of populate...

P1

P2

Virtual Memory

Virtual Memory

MMAPBASE

mmap(0, 8192, PROT_READ, MAP_POPULATE, fd, 4096)

8192

Physical Memory

pagepage

Page table

Page 27: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3-1. mmap() Sycall on xv6

27

P1 P2

struct mmap_area

file *f;addr;length;offset;prot;flags;proc *p;

In the case of populate...

P1

P2

Virtual Memory

Virtual Memory

MMAPBASE

mmap(0, 8192, PROT_READ, MAP_POPULATE, fd, 4096)

8192

Physical Memory

pagepage

Page table

mmap(4096, 8192, PROT_READ, MAP_POPULATE|MAP_FIXED, fd, 0)

MMAPBASE

8192

pagepage

Page table

Page 28: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3-1. mmap() Sycall on xv6

28

- Detailed flow of mmap

1. Check parameter’s validity (if not return -1)

1. MAP_PRIVATE should be always given

2. If it is anonymous mapping,

1. fd should be -1, offset should be 0

3. If it is file mapping,

1. File’s open flag(f->readable/writable) should match with mmap’s prot

2. File’s type should be FD_INODE(not PIPE or NONE)

4. If MAP_FIXED is given, there should be no overlapping mapping area

5. If MAP_FIXED is not given, addr should be NULL

1. Process should find proper fitting mapping area with continueous range as

length (from MMAP_BASE)

→ At this point, virtual address of mapping area will be fixed

Page 29: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3-1. mmap() Sycall on xv6

29

- Detailed flow of mmap

2. If MAP_POPULATE is given, follow the sequence below

1. Divide mapping area with the size of page

2. For each portions,

1. Allocate new physical page

2. Fill new page with 0

3. If it is file mapping, read file into the physical page with offset

4. If it is anonymous mapping, just left the page which is filled with 0s

5. Make page table & fill it properly (if it was PROT_WRITE, PTE_W

should be 1 in PTE value)

3. If work is done for whole mapping area, return the starting

virtual address of mapping area

4. Return the starting virtual address of mapping area

Page 30: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3-1. mmap() Sycall on xv6

30

- Detailed flow of mmap

3. If MAP_POPULATE is not given, follow the sequence

below

1. Put proper value into the mmap_area structure

2. Return the starting virtual address of mapping area

Page 31: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3-2. Page Fault Handler on xv6

31

- Page fault handler is for dealing with access on mapping region with physical page & page table is not allocated

- Return value: 1(succeed), -1(failed)

1. When an access occurs (read/write), catch according page fault (interrupt 14, T_PGFLT) in trap.c

2. In page fault handler, determine fault address by reading CR2 register(rcr2()) & access was read or write1. read: tf->err&2 == 0 / write: tf->err == 1

3. Find according mapping region in mmap_area1. If faulted address has no corresponding mmap_area, return -1

4. If fault was write while mmap_area is write prohibited, then return -1

5. For only one page according to faulted address1. Allocate new physical page

2. Fill new page with 0

3. If it is file mapping, read file into the physical page with offset

4. If it is anonymous mapping, just left the page which is filled with 0s

5. Make page table & fill it properly (if it was PROT_WRITE, PTE_W should be 1 in PTE value)

Page 32: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3-3. munmap() syscall on xv6

32

- munmap(addr)

- Unmaps corresponding mapping area

- Return value: 1(succeed), -1(failed)

1. addr will be always given with the start address of mapping region, which is page aligned

2. munmap() should remove corresponding mmap_area structure1. If there is no mmap_area of process starting with the address, return -1

3. If physical page is allocated & page table is constructed, should free physical page & page table1. When freeing the physical page should fill with 1 and put it back to freelist

4. If physical page is not allocated (page fault has not been occurred on that address), just remove mmap_area structure.

5. Notice) In one mmap_area, situation of some of pages are allocated and some are not can happen.

Page 33: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3-4. freemem() syscall on xv6

33

- syscall to return current number of free memory

1. When kernel frees (put page into free list), freemem

should be increase

2. When kernel allocates (takes page from free list and

give it to process), freemem should decrease

Page 34: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Project 3-extra. Additional Implements

34

- In the case of fork(),

- Even physical page has been allocated and page table has

been built for parent process, forked child process just copies

mapping area

- Access to copied mapping area will be handled by page fault

- In the case of exit(), kill()

- munmap() should be called for according process’s

mmap_area

- Notice) kill() will eventually call exit() (kill() just marks process

as killed and call exit() in scheduler)

Page 35: Project 3. Virtual Memory - AndroBenchcsl.skku.edu/uploads/SSE3044S19/project3.pdf · 2019. 4. 17. · Project 3-1. mmap() Sycall on xv6 28-Detailed flow of mmap1. Check parameter’s

Submission

35

• Compress your xv6 folder– make dist && make tar– Change tar file’s name into team#.tar.gz

• Send your tar.gz file to TAs’ e-mail (both)– 이규선: [email protected]– 송원석: [email protected]– Please send mail with uniformized title

• [OS Project #. team #] studentID name, studentID name

• Deadline: 5/7(Tue), 23:59:59 PM– Delay penalty: -25% per day

• PLEASE DO NOT COPY– YOU WILL GET F GRADE IF YOU COPIED