J-dbd / PintOS_lab

Other
0 stars 0 forks source link

[VM] mmap: Memory Mapped Files #28

Open J-dbd opened 10 months ago

J-dbd commented 10 months ago

mmap and munmap system call

void *mmap (void *addr, size_t length, int writable, int fd, off_t offset); void munmap (void *addr);

void vm_file_init (void); bool file_backed_initializer (struct page *page, enum vm_type type, void *kva); static void file_backed_destroy (struct page *page);

J-dbd commented 10 months ago
pass tests/userprog/args-none
pass tests/userprog/args-single
pass tests/userprog/args-multiple
pass tests/userprog/args-many
pass tests/userprog/args-dbl-space
pass tests/userprog/halt
pass tests/userprog/exit
pass tests/userprog/create-normal
pass tests/userprog/create-empty
pass tests/userprog/create-null
pass tests/userprog/create-bad-ptr
pass tests/userprog/create-long
pass tests/userprog/create-exists
pass tests/userprog/create-bound
pass tests/userprog/open-normal
pass tests/userprog/open-missing
pass tests/userprog/open-boundary
pass tests/userprog/open-empty
pass tests/userprog/open-null
pass tests/userprog/open-bad-ptr
pass tests/userprog/open-twice
pass tests/userprog/close-normal
pass tests/userprog/close-twice
pass tests/userprog/close-bad-fd
pass tests/userprog/read-normal
pass tests/userprog/read-bad-ptr
pass tests/userprog/read-boundary
pass tests/userprog/read-zero
pass tests/userprog/read-stdout
pass tests/userprog/read-bad-fd
pass tests/userprog/write-normal
pass tests/userprog/write-bad-ptr
pass tests/userprog/write-boundary
pass tests/userprog/write-zero
pass tests/userprog/write-stdin
pass tests/userprog/write-bad-fd
pass tests/userprog/fork-once
pass tests/userprog/fork-multiple
pass tests/userprog/fork-recursive
pass tests/userprog/fork-read
pass tests/userprog/fork-close
pass tests/userprog/fork-boundary
pass tests/userprog/exec-once
pass tests/userprog/exec-arg
pass tests/userprog/exec-boundary
pass tests/userprog/exec-missing
pass tests/userprog/exec-bad-ptr
pass tests/userprog/exec-read
pass tests/userprog/wait-simple
pass tests/userprog/wait-twice
pass tests/userprog/wait-killed
pass tests/userprog/wait-bad-pid
pass tests/userprog/multi-recurse
pass tests/userprog/multi-child-fd
pass tests/userprog/rox-simple
pass tests/userprog/rox-child
pass tests/userprog/rox-multichild
pass tests/userprog/bad-read
pass tests/userprog/bad-write
pass tests/userprog/bad-read2
pass tests/userprog/bad-write2
pass tests/userprog/bad-jump
pass tests/userprog/bad-jump2
pass tests/vm/pt-grow-stack
pass tests/vm/pt-grow-bad
pass tests/vm/pt-big-stk-obj
pass tests/vm/pt-bad-addr
pass tests/vm/pt-bad-read
pass tests/vm/pt-write-code
pass tests/vm/pt-write-code2
pass tests/vm/pt-grow-stk-sc
pass tests/vm/page-linear
FAIL tests/vm/page-parallel
pass tests/vm/page-merge-seq
pass tests/vm/page-merge-par
FAIL tests/vm/page-merge-stk
FAIL tests/vm/page-merge-mm
pass tests/vm/page-shuffle
pass tests/vm/mmap-read
pass tests/vm/mmap-close
pass tests/vm/mmap-unmap
pass tests/vm/mmap-overlap
pass tests/vm/mmap-twice
pass tests/vm/mmap-write
pass tests/vm/mmap-ro
pass tests/vm/mmap-exit
pass tests/vm/mmap-shuffle
pass tests/vm/mmap-bad-fd
FAIL tests/vm/mmap-clean
pass tests/vm/mmap-inherit
pass tests/vm/mmap-misalign
pass tests/vm/mmap-null
pass tests/vm/mmap-over-code
pass tests/vm/mmap-over-data
pass tests/vm/mmap-over-stk
pass tests/vm/mmap-remove
pass tests/vm/mmap-zero
FAIL tests/vm/mmap-bad-fd2
FAIL tests/vm/mmap-bad-fd3
pass tests/vm/mmap-zero-len
pass tests/vm/mmap-off
pass tests/vm/mmap-bad-off
pass tests/vm/mmap-kernel
pass tests/vm/lazy-file
pass tests/vm/lazy-anon
FAIL tests/vm/swap-file
FAIL tests/vm/swap-anon
FAIL tests/vm/swap-iter
FAIL tests/vm/swap-fork
pass tests/filesys/base/lg-create
pass tests/filesys/base/lg-full
pass tests/filesys/base/lg-random
pass tests/filesys/base/lg-seq-block
pass tests/filesys/base/lg-seq-random
pass tests/filesys/base/sm-create
pass tests/filesys/base/sm-full
pass tests/filesys/base/sm-random
pass tests/filesys/base/sm-seq-block
pass tests/filesys/base/sm-seq-random
pass tests/filesys/base/syn-read
pass tests/filesys/base/syn-remove
pass tests/filesys/base/syn-write
pass tests/threads/alarm-single
pass tests/threads/alarm-multiple
pass tests/threads/alarm-simultaneous
pass tests/threads/alarm-priority
pass tests/threads/alarm-zero
pass tests/threads/alarm-negative
pass tests/threads/priority-change
pass tests/threads/priority-donate-one
pass tests/threads/priority-donate-multiple
pass tests/threads/priority-donate-multiple2
pass tests/threads/priority-donate-nest
pass tests/threads/priority-donate-sema
pass tests/threads/priority-donate-lower
pass tests/threads/priority-fifo
pass tests/threads/priority-preempt
pass tests/threads/priority-sema
pass tests/threads/priority-condvar
pass tests/threads/priority-donate-chain
FAIL tests/vm/cow/cow-simple
11 of 141 tests failed.

남은 문제

pass tests/vm/page-linear FAIL tests/vm/page-parallel pass tests/vm/page-merge-seq pass tests/vm/page-merge-par FAIL tests/vm/page-merge-stk FAIL tests/vm/page-merge-mm

FAIL tests/vm/mmap-clean

FAIL tests/vm/mmap-bad-fd2 FAIL tests/vm/mmap-bad-fd3

FAIL tests/vm/swap-file FAIL tests/vm/swap-anon FAIL tests/vm/swap-iter FAIL tests/vm/swap-fork

FAIL tests/vm/cow/cow-simple 11 of 141 tests failed.

J-dbd commented 10 months ago

mmap clean

output 기록

Kernel command line: -q -f put mmap-clean put sample.txt run mmap-clean
0 ~ 9fc00 1
100000 ~ 13e0000 1
Pintos booting with: 
    base_mem: 0x0 ~ 0x9fc00 (Usable: 639 kB)
    ext_mem: 0x100000 ~ 0x13e0000 (Usable: 19,328 kB)
Calibrating timer...  157,081,600 loops/s.
hd0:0: detected 337 sector (168 kB) disk, model "QEMU HARDDISK", serial "QM00001"
hd0:1: detected 20,160 sector (9 MB) disk, model "QEMU HARDDISK", serial "QM00002"
hd1:0: detected 112 sector (56 kB) disk, model "QEMU HARDDISK", serial "QM00003"
hd1:1: detected 8,064 sector (3 MB) disk, model "QEMU HARDDISK", serial "QM00004"
Formatting file system...done.
Boot complete.
Putting 'mmap-clean' into the file system...
Putting 'sample.txt' into the file system...
Executing 'mmap-clean':
[syscall number] 10
[syscall write] fd:1
[validate_buffer] buffer:0x605780, size:19, is_writable:1
[syscall write] validate_buffer end
(mmap-clean) begin
[syscall write] bytesRead:19
[syscall number] 10
[syscall write] fd:1
[validate_buffer] buffer:0x605780, size:31, is_writable:1
[syscall write] validate_buffer end
(mmap-clean) open "sample.txt"
[syscall write] bytesRead:31
[syscall number] 7
[syscall number] 10
[syscall write] fd:1
[validate_buffer] buffer:0x605780, size:31, is_writable:1
[syscall write] validate_buffer end
(mmap-clean) mmap "sample.txt"
[syscall write] bytesRead:31
[syscall number] 14
[mmap] addr:0x54321000 / length:4096 / writable:0 / fd:2 /offset: 0 
[mmap] ending page found : 0x8004249498
[syscall number] 10
[syscall write] fd:1
[validate_buffer] buffer:0x605780, size:32, is_writable:1
[syscall write] validate_buffer end
(mmap-clean) write "sample.txt"
[syscall write] bytesRead:32
[syscall number] 10
[syscall write] fd:2
[validate_buffer] buffer:0x4047e0, size:31, is_writable:1
[syscall write] validate_buffer end
[syscall write] lock finished
[syscall number] 10
[syscall write] fd:1
[validate_buffer] buffer:0x605780, size:33, is_writable:1
[syscall write] validate_buffer end
(mmap-clean) munmap "sample.txt"
[syscall write] bytesRead:33
[syscall number] 15
[syscall number] 10
[syscall write] fd:1
[validate_buffer] buffer:0x605780, size:31, is_writable:1
[syscall write] validate_buffer end
(mmap-clean) seek "sample.txt"
[syscall write] bytesRead:31
[syscall number] 11
[syscall number] 10
[syscall write] fd:1
[validate_buffer] buffer:0x605780, size:31, is_writable:1
[syscall write] validate_buffer end
(mmap-clean) read "sample.txt"
[syscall write] bytesRead:31
[syscall number] 9
[validate_buffer] buffer:0x605440, size:794, is_writable:1
[syscall number] 10
[syscall write] fd:1
[validate_buffer] buffer:0x605780, size:51, is_writable:1
[syscall write] validate_buffer end
(mmap-clean) file change was retained after munmap
[syscall write] bytesRead:51
[syscall number] 10
[syscall write] fd:1
[validate_buffer] buffer:0x605780, size:17, is_writable:1
[syscall write] validate_buffer end
(mmap-clean) end
[syscall write] bytesRead:17
[syscall number] 1
mmap-clean: exit(0)
Execution of 'mmap-clean' complete.
Timer: 85 ticks
Thread: 30 idle ticks, 31 kernel ticks, 24 user ticks
hd0:0: 0 reads, 0 writes
hd0:1: 205 reads, 263 writes
hd1:0: 112 reads, 0 writes
hd1:1: 0 reads, 0 writes
Console: 3183 characters output
Keyboard: 0 keys pressed
Exception: 0 page faults
Powering off...
/* Validate given buffer by page size*/
void validate_buffer(void* buffer, size_t size, bool is_writable) {
    //printf("[validate_buffer] buffer:%p, size:%d, is_writable:%d\n", buffer, size, is_writable);
    if (buffer == NULL) {
        //printf("[validate_buffer] buffer == NULL\n");
        exit(-1);
    }

    if (buffer<= USER_STACK && buffer>=thread_current()->intr_rsp) {
        //printf("[validate_buffer] case 2\n");
        return;
    }

    void* start_addr = pg_round_down(buffer);
    void* end_addr = pg_round_down(buffer+size);

    for (void* addr = end_addr; addr>=start_addr; addr-=PGSIZE) {
        //check address's function
        if(addr == NULL || is_kernel_vaddr(addr)) {
            //printf("[validate_buffer] case 3\n");
            exit(-1);
        }
        struct page* traget_page= spt_find_page(&thread_current()->spt, addr);
        if(traget_page == NULL) {
            //printf("[validate_buffer] case 4\n");
            exit(-1);
        }

        if(traget_page->writable == false && is_writable ==true) {
            printf("[validate_buffer] case 5\n");
            printf("mmap clean out issue out\n");
            exit(-1);       
        }
    }
    }

mmap-bad-fd2

void *
mmap (void *addr, size_t length, int writable, int fd, off_t offset) {
    //printf("[mmap] addr:%p / length:%d / writable:%d / fd:%d /offset: %d \n", addr, length, writable, fd, offset);
    struct file* target_file = get_file_from_fd_table(fd);

    // the file descriptors representing console input and output are not mappable
    if (fd == 0 || fd == 1) {
        //printf("[mmap] fail case 6 \n");
        return NULL;
        //exit(-1);
    }

fd 테스트 코드를 위로 올려서 해결

make check를 돌려보니..

pass tests/userprog/args-none
pass tests/userprog/args-single
pass tests/userprog/args-multiple
pass tests/userprog/args-many
pass tests/userprog/args-dbl-space
pass tests/userprog/halt
pass tests/userprog/exit
pass tests/userprog/create-normal
pass tests/userprog/create-empty
pass tests/userprog/create-null
pass tests/userprog/create-bad-ptr
pass tests/userprog/create-long
pass tests/userprog/create-exists
pass tests/userprog/create-bound
pass tests/userprog/open-normal
pass tests/userprog/open-missing
pass tests/userprog/open-boundary
pass tests/userprog/open-empty
pass tests/userprog/open-null
pass tests/userprog/open-bad-ptr
pass tests/userprog/open-twice
pass tests/userprog/close-normal
pass tests/userprog/close-twice
pass tests/userprog/close-bad-fd
pass tests/userprog/read-normal
pass tests/userprog/read-bad-ptr
pass tests/userprog/read-boundary
pass tests/userprog/read-zero
pass tests/userprog/read-stdout
pass tests/userprog/read-bad-fd
pass tests/userprog/write-normal
pass tests/userprog/write-bad-ptr
pass tests/userprog/write-boundary
pass tests/userprog/write-zero
pass tests/userprog/write-stdin
pass tests/userprog/write-bad-fd
pass tests/userprog/fork-once
pass tests/userprog/fork-multiple
pass tests/userprog/fork-recursive
pass tests/userprog/fork-read
pass tests/userprog/fork-close
pass tests/userprog/fork-boundary
pass tests/userprog/exec-once
pass tests/userprog/exec-arg
pass tests/userprog/exec-boundary
pass tests/userprog/exec-missing
pass tests/userprog/exec-bad-ptr
pass tests/userprog/exec-read
pass tests/userprog/wait-simple
pass tests/userprog/wait-twice
pass tests/userprog/wait-killed
pass tests/userprog/wait-bad-pid
pass tests/userprog/multi-recurse
pass tests/userprog/multi-child-fd
pass tests/userprog/rox-simple
pass tests/userprog/rox-child
pass tests/userprog/rox-multichild
pass tests/userprog/bad-read
pass tests/userprog/bad-write
pass tests/userprog/bad-read2
pass tests/userprog/bad-write2
pass tests/userprog/bad-jump
pass tests/userprog/bad-jump2
pass tests/vm/pt-grow-stack
pass tests/vm/pt-grow-bad
pass tests/vm/pt-big-stk-obj
pass tests/vm/pt-bad-addr
pass tests/vm/pt-bad-read
pass tests/vm/pt-write-code
FAIL tests/vm/pt-write-code2
pass tests/vm/pt-grow-stk-sc
pass tests/vm/page-linear
FAIL tests/vm/page-parallel
pass tests/vm/page-merge-seq
pass tests/vm/page-merge-par
pass tests/vm/page-merge-stk
FAIL tests/vm/page-merge-mm
pass tests/vm/page-shuffle
pass tests/vm/mmap-read
pass tests/vm/mmap-close
pass tests/vm/mmap-unmap
pass tests/vm/mmap-overlap
pass tests/vm/mmap-twice
pass tests/vm/mmap-write
pass tests/vm/mmap-ro
pass tests/vm/mmap-exit
pass tests/vm/mmap-shuffle
pass tests/vm/mmap-bad-fd
pass tests/vm/mmap-clean
pass tests/vm/mmap-inherit
pass tests/vm/mmap-misalign
pass tests/vm/mmap-null
pass tests/vm/mmap-over-code
pass tests/vm/mmap-over-data
pass tests/vm/mmap-over-stk
pass tests/vm/mmap-remove
pass tests/vm/mmap-zero
pass tests/vm/mmap-bad-fd2
pass tests/vm/mmap-bad-fd3
pass tests/vm/mmap-zero-len
pass tests/vm/mmap-off
pass tests/vm/mmap-bad-off
pass tests/vm/mmap-kernel
pass tests/vm/lazy-file
pass tests/vm/lazy-anon
FAIL tests/vm/swap-file
FAIL tests/vm/swap-anon
FAIL tests/vm/swap-iter
FAIL tests/vm/swap-fork
pass tests/filesys/base/lg-create
pass tests/filesys/base/lg-full
pass tests/filesys/base/lg-random
pass tests/filesys/base/lg-seq-block
pass tests/filesys/base/lg-seq-random
pass tests/filesys/base/sm-create
pass tests/filesys/base/sm-full
pass tests/filesys/base/sm-random
pass tests/filesys/base/sm-seq-block
pass tests/filesys/base/sm-seq-random
FAIL tests/filesys/base/syn-read
pass tests/filesys/base/syn-remove
pass tests/filesys/base/syn-write
pass tests/threads/alarm-single
pass tests/threads/alarm-multiple
pass tests/threads/alarm-simultaneous
pass tests/threads/alarm-priority
pass tests/threads/alarm-zero
pass tests/threads/alarm-negative
pass tests/threads/priority-change
pass tests/threads/priority-donate-one
pass tests/threads/priority-donate-multiple
pass tests/threads/priority-donate-multiple2
pass tests/threads/priority-donate-nest
pass tests/threads/priority-donate-sema
pass tests/threads/priority-donate-lower
pass tests/threads/priority-fifo
pass tests/threads/priority-preempt
pass tests/threads/priority-sema
pass tests/threads/priority-condvar
pass tests/threads/priority-donate-chain
FAIL tests/vm/cow/cow-simple
9 of 141 tests failed.

FAIL tests/vm/page-parallel FAIL tests/vm/page-merge-mm FAIL tests/vm/cow/cow-simple FAIL tests/vm/swap-file FAIL tests/vm/swap-anon FAIL tests/vm/swap-iter FAIL tests/vm/swap-fork

FAIL tests/vm/pt-write-code2 FAIL tests/filesys/base/syn-read

새로운 문제가 발생했다가 다시 보니 write할 때 false로 바꿔주니 돌아갔다.