From 9592a8d1a6ad9dae83d83ef8e95f459896c604c1 Mon Sep 17 00:00:00 2001 From: jy604 Date: Thu, 11 May 2023 17:56:45 +0000 Subject: [PATCH 1/4] =?UTF-8?q?spt=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/threads/vaddr.h | 3 ++- include/vm/vm.h | 3 +++ vm/vm.c | 50 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/include/threads/vaddr.h b/include/threads/vaddr.h index ac4c624..95f598d 100644 --- a/include/threads/vaddr.h +++ b/include/threads/vaddr.h @@ -28,7 +28,8 @@ /* Round up to nearest page boundary. */ #define pg_round_up(va) ((void *) (((uint64_t) (va) + PGSIZE - 1) & ~PGMASK)) -/* Round down to nearest page boundary. */ +/* Round down to nearest page boundary. +va를 페이지 크기에 맞게 내림하여 가장 가까운 페이지 경계로 정렬함*/ #define pg_round_down(va) (void *) ((uint64_t) (va) & ~PGMASK) /* Kernel virtual address start */ diff --git a/include/vm/vm.h b/include/vm/vm.h index e061b73..ee36c20 100644 --- a/include/vm/vm.h +++ b/include/vm/vm.h @@ -46,6 +46,8 @@ struct page { struct frame *frame; /* Back reference for frame */ /* Your implementation */ + /* project 3-1 */ + struct hash_elem hash_elem; /* Per-type data are binded into the union. * Each function automatically detects the current union */ @@ -85,6 +87,7 @@ struct page_operations { * We don't want to force you to obey any specific design for this struct. * All designs up to you for this. */ struct supplemental_page_table { + struct hash spt_hash; }; #include "threads/thread.h" diff --git a/vm/vm.c b/vm/vm.c index 94d58f9..d2439a6 100644 --- a/vm/vm.c +++ b/vm/vm.c @@ -3,6 +3,7 @@ #include "threads/malloc.h" #include "vm/vm.h" #include "vm/inspect.h" +#include "lib/kernel/hash.h" /* Initializes the virtual memory subsystem by invoking each subsystem's * intialize codes. */ @@ -60,23 +61,37 @@ vm_alloc_page_with_initializer (enum vm_type type, void *upage, bool writable, return false; } +// project 3-1 /* Find VA from spt and return page. On error, return NULL. */ +// 해시 테이블에서 인자로 받은 va가 있는지 찾는 함수, va가 속해있는 페이지가 해시테이블에 있으면 이를 리턴함 struct page * spt_find_page (struct supplemental_page_table *spt UNUSED, void *va UNUSED) { struct page *page = NULL; /* TODO: Fill this function. */ - - return page; + struct page *page = (struct page *)malloc(sizeof(struct page)); + struct hash_elem *e; + + page->va = pg_round_down(va); // 가상 주소를 내려 주소값의 시작 주소를 받음 + e = hash_find(&spt->spt_hash,&page->hash_elem); + free(page); //더미 페이지이므로 찾았으면 free해줘야함 + // return page; + if (e != NULL) { + return hash_entry(e, struct page, hash_elem); + } else { + retrun NULL; + } } +// project 3-1 /* Insert PAGE into spt with validation. */ bool spt_insert_page (struct supplemental_page_table *spt UNUSED, struct page *page UNUSED) { - int succ = false; + //int succ = false; /* TODO: Fill this function. */ - - return succ; + + //return succ; + return page_insert(&spt->spt_hash, page); } void @@ -174,6 +189,7 @@ vm_do_claim_page (struct page *page) { /* Initialize new supplemental page table */ void supplemental_page_table_init (struct supplemental_page_table *spt UNUSED) { + hash_init(&spt->spt_hash, page_hash, page_less, NULL); } /* Copy supplemental page table from src to dst */ @@ -188,3 +204,27 @@ supplemental_page_table_kill (struct supplemental_page_table *spt UNUSED) { /* TODO: Destroy all the supplemental_page_table hold by thread and * TODO: writeback all the modified contents to the storage. */ } + +/* Returns a hash value for page p. */ +unsigned page_hash (const struct hash_elem *p_, void *aux UNUSED) { + const struct page *p = hash_entry (p_, struct page, hash_elem); + return hash_bytes (&p->va, sizeof p->va); +} + +/* Returns true if page a precedes page b. */ +bool page_less (const struct hash_elem *a_, + const struct hash_elem *b_, void *aux UNUSED) { + const struct page *a = hash_entry (a_, struct page, hash_elem); + const struct page *b = hash_entry (b_, struct page, hash_elem); + + return a->va < b->va; +} + +//spt_insert_page()를 위한 bool 함수 +bool page_insert(struct hash *h, struct page *p) { + if (!hash_insert(h,&p->hash_elem)) { + return true; + } else { + return false; + } +} \ No newline at end of file From 2263d4b5012ace652735ec9ee6ff61799795927e Mon Sep 17 00:00:00 2001 From: jy604 Date: Fri, 12 May 2023 15:03:32 +0000 Subject: [PATCH 2/4] =?UTF-8?q?frame=20table=20=EA=B5=AC=ED=98=84=20?= =?UTF-8?q?=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vm/vm.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/vm/vm.c b/vm/vm.c index d2439a6..327326d 100644 --- a/vm/vm.c +++ b/vm/vm.c @@ -5,6 +5,9 @@ #include "vm/inspect.h" #include "lib/kernel/hash.h" +#include "threads/thread.h" + +struct list frame_table; /* Initializes the virtual memory subsystem by invoking each subsystem's * intialize codes. */ void @@ -56,7 +59,15 @@ vm_alloc_page_with_initializer (enum vm_type type, void *upage, bool writable, * TODO: should modify the field after calling the uninit_new. */ /* TODO: Insert the page into the spt. */ + //palloc으로 new_page를 할당 받고 + // sutruct page *new_page = palloc_get_page(PAL_USER); + //switch로 anon, file에 따라 + // switch(type) + // uninit_new를 분리해서 호출해줌 + // case anon_: + // uninit_new(new_page, upage, init, aux, anon_initializer); } + // return true // 를 해줘야 load_세그먼트가 다시 불러도 넘어감 err: return false; } @@ -127,6 +138,12 @@ static struct frame * vm_get_frame (void) { struct frame *frame = NULL; /* TODO: Fill this function. */ + frame = palloc_get_page(PAL_USER); + + if (frame == NULL) { + PANIC("to do"); + } + frame->page = NULL; ASSERT (frame != NULL); ASSERT (frame->page == NULL); @@ -151,7 +168,7 @@ vm_try_handle_fault (struct intr_frame *f UNUSED, void *addr UNUSED, struct page *page = NULL; /* TODO: Validate the fault */ /* TODO: Your code goes here */ - + // page-fault가 return vm_do_claim_page (page); } @@ -168,7 +185,9 @@ bool vm_claim_page (void *va UNUSED) { struct page *page = NULL; /* TODO: Fill this function */ - + struct thread *curr = thread_current(); + page = spt_find_page(curr->spt, va); + return vm_do_claim_page (page); } From 53d52b2357742efb40a7318a2fd38f8bddeb7d6c Mon Sep 17 00:00:00 2001 From: jy604 Date: Fri, 12 May 2023 17:10:37 +0000 Subject: [PATCH 3/4] =?UTF-8?q?M-management=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/vm/vm.h | 1 + vm/vm.c | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/include/vm/vm.h b/include/vm/vm.h index ee36c20..756446e 100644 --- a/include/vm/vm.h +++ b/include/vm/vm.h @@ -44,6 +44,7 @@ struct page { const struct page_operations *operations; void *va; /* Address in terms of user space */ struct frame *frame; /* Back reference for frame */ + struct list_elem frame_elem; // frame_table을 위한 list_elem 추가 /* Your implementation */ /* project 3-1 */ diff --git a/vm/vm.c b/vm/vm.c index 327326d..b2bf1e8 100644 --- a/vm/vm.c +++ b/vm/vm.c @@ -199,10 +199,13 @@ vm_do_claim_page (struct page *page) { /* Set links */ frame->page = page; page->frame = frame; - + uint64_t pte = pml4e_walk(thread_current()->pml4,page->va,0); /* TODO: Insert page table entry to map page's VA to frame's PA. */ - - return swap_in (page, frame->kva); + if (install_page(page,frame,is_writable(&pte)) == true){ + return true; + } + return false; + // return swap_in (page, frame->kva); } /* Initialize new supplemental page table */ From 8556a0dbc6e67799f9458e133818715a41c9d7e7 Mon Sep 17 00:00:00 2001 From: jy604 Date: Fri, 12 May 2023 17:57:58 +0000 Subject: [PATCH 4/4] =?UTF-8?q?build=20error=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/vm/vm.h | 1 + vm/vm.c | 27 +++++++++++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/include/vm/vm.h b/include/vm/vm.h index 756446e..3d76897 100644 --- a/include/vm/vm.h +++ b/include/vm/vm.h @@ -2,6 +2,7 @@ #define VM_VM_H #include #include "threads/palloc.h" +#include "lib/kernel/hash.h" enum vm_type { /* page not initialized */ diff --git a/vm/vm.c b/vm/vm.c index b2bf1e8..b2afc7a 100644 --- a/vm/vm.c +++ b/vm/vm.c @@ -6,8 +6,15 @@ #include "lib/kernel/hash.h" #include "threads/thread.h" +#include "../include/userprog/process.h" +#include "threads/mmu.h" + +unsigned page_hash (const struct hash_elem *p_, void *aux UNUSED); +bool page_less (const struct hash_elem *a_, const struct hash_elem *b_, void *aux UNUSED); +bool page_insert(struct hash *h, struct page *p); struct list frame_table; + /* Initializes the virtual memory subsystem by invoking each subsystem's * intialize codes. */ void @@ -77,7 +84,7 @@ vm_alloc_page_with_initializer (enum vm_type type, void *upage, bool writable, // 해시 테이블에서 인자로 받은 va가 있는지 찾는 함수, va가 속해있는 페이지가 해시테이블에 있으면 이를 리턴함 struct page * spt_find_page (struct supplemental_page_table *spt UNUSED, void *va UNUSED) { - struct page *page = NULL; + // struct page *page = NULL; /* TODO: Fill this function. */ struct page *page = (struct page *)malloc(sizeof(struct page)); struct hash_elem *e; @@ -89,7 +96,7 @@ spt_find_page (struct supplemental_page_table *spt UNUSED, void *va UNUSED) { if (e != NULL) { return hash_entry(e, struct page, hash_elem); } else { - retrun NULL; + return NULL; } } @@ -186,7 +193,7 @@ vm_claim_page (void *va UNUSED) { struct page *page = NULL; /* TODO: Fill this function */ struct thread *curr = thread_current(); - page = spt_find_page(curr->spt, va); + page = spt_find_page(&curr->spt, va); return vm_do_claim_page (page); } @@ -199,13 +206,17 @@ vm_do_claim_page (struct page *page) { /* Set links */ frame->page = page; page->frame = frame; - uint64_t pte = pml4e_walk(thread_current()->pml4,page->va,0); + // uint64_t pte = pml4e_walk(thread_current()->pml4, page->va,0); + /* TODO: Insert page table entry to map page's VA to frame's PA. */ - if (install_page(page,frame,is_writable(&pte)) == true){ - return true; - } - return false; + // if (install_page(page,frame,is_writable(&pte)) == true){ + // return true; + // } + // return false; // return swap_in (page, frame->kva); + pml4_set_page(thread_current()->pml4, page->va, frame->kva, is_writable(thread_current()->pml4)); + + return swap_in (page, frame->kva); } /* Initialize new supplemental page table */