09:14 입실
lazy loading 구현.. 할 수 있을까?
살려줘.. 인생 최대의 위기. 핀토스 가상 메모리.
https://codable.tistory.com/26
초기화되지 않은 페이지를 설정함.
/* DO NOT MODIFY this function */
void
uninit_new (struct page *page, void *va, vm_initializer *init,
enum vm_type type, void *aux,
bool (*initializer)(struct page *, enum vm_type, void *)) {
ASSERT (page != NULL);
*page = (struct page) {
.operations = &uninit_ops,
.va = va,
.frame = NULL, /* no frame for now */
.uninit = (struct uninit_page) {
.init = init,
.type = type,
.aux = aux,
.page_initializer = initializer,
}
};
}
이 함수의 역할은 페이지를 초기화 함수와 함께 생성함. 단, 프레임은 할당되지 않음. 페이지를 생성할 때는 이 함수를 직접 호출하지 않고, vm_alloc_page나 uninit_new로 생성하기.
나름 구현해봄.
bool
vm_alloc_page_with_initializer (enum vm_type type, void *upage, bool writable,
vm_initializer *init, void *aux) {
ASSERT (VM_TYPE(type) != VM_UNINIT)
struct supplemental_page_table *spt = &thread_current ()->spt;
/* Check wheter the upage is already occupied or not. */
if (spt_find_page (spt, upage) == NULL) {
/* TODO: Create the page, fetch the initialier according to the VM type,
* TODO: and then create "uninit" page struct by calling uninit_new. You
* TODO: should modify the field after calling the uninit_new. */
struct page *page = (struct page *)malloc(sizeof(struct page));
if (type == VM_ANON) {
uninit_new(page, upage, init, type, aux, anon_initializer);
} else if (type == VM_FILE) {
uninit_new(page, upage, init, type, aux, file_backed_initializer);
}
page->writable = writable;
/* TODO: Insert the page into the spt. */
spt_insert_page(spt, page);
return true;
}
err:
return false;
}
2차 수정본
bool
vm_alloc_page_with_initializer (enum vm_type type, void *upage, bool writable,
vm_initializer *init, void *aux) {
ASSERT (VM_TYPE(type) != VM_UNINIT)
struct supplemental_page_table *spt = &thread_current ()->spt;
/* Check wheter the upage is already occupied or not. */
if (spt_find_page (spt, upage) == NULL) {
/* TODO: Create the page, fetch the initialier according to the VM type,
* TODO: and then create "uninit" page struct by calling uninit_new. You
* TODO: should modify the field after calling the uninit_new. */
struct page *page = (struct page *)malloc(sizeof(struct page));
if (page == NULL) {
return false;
}
if (type == VM_ANON) {
uninit_new(page, upage, init, type, aux, anon_initializer);
} else if (type == VM_FILE) {
uninit_new(page, upage, init, type, aux, file_backed_initializer);
} else {
free(page);
return false;
}
page->writable = writable;
/* TODO: Insert the page into the spt. */
if (!spt_insert_page(spt, page)) {
free(page);
return false;
}
return true;
}
err:
return false;
}
페이지 폴트가 발생하면 그때 0으로 초기화된 메모리를 할당?
static bool
uninit_initialize (struct page *page, void *kva) {
struct uninit_page *uninit = &page->uninit;
/* Fetch first, page_initialize may overwrite the values */
vm_initializer *init = uninit->init;
void *aux = uninit->aux;
/* TODO: You may need to fix this function. */
return uninit->page_initializer (page, uninit->type, kva) &&
(init ? init (page, aux) : true);
}
익명 페이지 하루 종일 들여다보았지만 코드 구현 불가.😥