supplemental_page_table_init() - sihyun10/pintos-lab-vm GitHub Wiki
κ°μ₯ λ¨Όμ ꡬνν΄μΌνλ κ²μ΄ Supplemental Table Page
μ΄λ€.
κΈ°μ‘΄μ page table
μ 보μνκΈ° μν΄ λ§λ κ²μΌλ‘, page
λ€μ λν΄ μΆκ°μ μΈ λ°μ΄ν°λ‘ νμ΄μ§ ν
μ΄λΈμ 보μνκΈ° μν λͺ©μ μ κ°μ§κ³ μλ€.
μλ 2κ°μ§ κΈ°λ₯μ μννλ€.
- Page Fault λ°μ μ 컀λμ΄
spt
μμ μ€λ₯κ° λ°μν κ°μ νμ΄μ§λ₯Ό μ‘°ννμ¬ μ΄λ€ λ°μ΄ν°κ° μμ΄μΌ νλμ§ νμΈνλ€. - νλ‘μΈμ€κ° μ’
λ£λ λ 컀λμ΄
spt
λ₯Ό μ°Έμ‘°νμ¬ μ΄λ€ 리μμ€λ₯Όfree
μν¬ κ²μΈμ§ κ²°μ νλ€.
spt
μμλ νλ‘μΈμ€κ° μμ²ν νμ΄μ§λ€μ λν΄μ 물리 νμ΄μ§λ₯Ό ν λΉνκΈ° μν΄μ ν΄μ ν
μ΄λΈ μλ£κ΅¬μ‘°κ° μ¬μ©λλ€.
struct supplemental_page_table
{
struct hash hash_table;
};
/* Hash table. */
struct hash {
size_t elem_cnt; /* Number of elements in table. */
size_t bucket_cnt; /* Number of buckets, a power of 2. */
struct list *buckets; /* Array of `bucket_cnt' lists. */
hash_hash_func *hash; /* Hash function. */
hash_less_func *less; /* Comparison function. */
void *aux; /* Auxiliary data for `hash' and `less'. */
};
ν΄μ ν
μ΄λΈμ (key, value)
λ‘ λ°μ΄ν°λ₯Ό μ μ₯νλ μλ£κ΅¬μ‘°λ€.
νκ· μ μΌλ‘ κ²μ/μμ /μ½μ
μ΄ O(1)
μ μκ°λ³΅μ‘λλ₯Ό κ°μ§λ€.
λ°λΌμ λΉ λ₯΄κ² λ°μ΄ν°λ₯Ό κ²μ κ°λ₯νλ€.
π οΈ μ²«λ²μ§Έ λͺ©ν
vm/vm.c
μμ struct supplemental_page_table
μ μλ£κ΅¬μ‘°λ₯Ό μ€κ³νκ³ ,
μ΄κΈ°ν ν¨μ supplemental_page_table_init()
μ ꡬνν΄μΌνλ€.
- SPTμ μ¬μ©ν μλ£κ΅¬μ‘° μ€κ³ -
vm/vm.c
SPTλ κ° μ μ νλ‘μΈμ€λ§λ€ μ‘΄μ¬νλ κ°μ νμ΄μ§ μ 보λ₯Ό μ μ₯νλ ν
μ΄λΈμ΄λ€.
νμ΄μ§ ν΄νΈκ° λ°μνμ λ, μ΄λ€ λ°μ΄ν°λ₯Ό λ‘λν΄μΌ ν μ§, μ΄λ€ νμ
μ νμ΄μ§μΈμ§ λ±μ μ΄ ν
μ΄λΈμμ νμΈνλ€.
Pintosμμλ ν΄μ ν μ΄λΈμ SPTμ κΈ°λ° μλ£κ΅¬μ‘°λ‘ μ¬μ©νλ κ²μ΄ μΌλ°μ μ΄λ€.
struct supplemental_page_table
{
struct hash hash_table; // ν΄μ ꡬ쑰체 μΆκ°
};
hash_table
μ νμ΄μ§ μ£Όμλ₯Ό ν€λ‘, νμ΄μ§ μ 보λ₯Ό κ°μΌλ‘ μ μ₯νλ ν΄μ ν
μ΄λΈμ΄λ€.
- Hashμ© page ꡬ쑰체 μ€κ³ -
include/vm/vm.h
μ΄μ ν΄μ ν
μ΄λΈμμ μ¬μ©ν page
ꡬ쑰체λ νμνλ€.
μ΄ κ΅¬μ‘°μ²΄λ νμ λ€μν νμ
μ νμ΄μ§λ₯Ό λ€λ£° λ νμ₯λλ€.
struct page
{
const struct page_operations *operations;
void *va; /* Address in terms of user space */
struct frame *frame; /* Back reference for frame */
/* Your implementation */ - μΆκ°ν λΆλΆ
struct hash_elem hash_elem; /* ν΄μ ν
μ΄λΈμ μ¬μ©λ μμ */
bool writable;
// β¦
};
- ν΄μ ν
μ΄λΈμ μ¬μ©ν ν΄μ λ° λΉκ΅ ν¨μ ꡬν -
page_hash(), page_less()
-vm/vm.c
ν΄μ ν μ΄λΈ μ¬μ©μ μν΄ λ ν¨μκ° νμνλ€.
bool hash_init (struct hash *hash, hash_hash_func *hash_func,
hash_less_func *less_func, void *aux);
- ν΄μ ν¨μ (
page_hash
)- μμμ λ°μ΄ν°λ₯Ό ν΄μ±ν κ°μ unsigned int λ²μ λ΄μμ λ°ννλ€.
static unsigned page_hash(const struct hash_elem *e, void *aux UNUSED)
{
const struct page *p = hash_entry(e, struct page, hash_elem);
return hash_bytes(&p->va, sizeof(p->va));
}
- λΉκ΅ ν¨μ (
page_less
)- μμ aμ bμ μ μ₯λ ν€λ€μ λΉκ΅νλ€.
- aκ° bλ³΄λ€ μμΌλ©΄ true, κ·Έλ μ§ μμΌλ©΄ false
static bool page_less(const struct hash_elem *a,
const struct hash_elem *b,
void *aux UNUSED)
{
const struct page *pa = hash_entry(a, struct page, hash_elem);
const struct page *pb = hash_entry(b, struct page, hash_elem);
return pa->va < pb->va;
}
-
supplemental_page_table_init()
ꡬν -vm/vm.c
sptλ₯Ό μ΄κΈ°νν΄μ£Όλ μν μ μννλ€.
- ν΄μ ν
μ΄λΈλ‘
spt
λ₯Ό ꡬννκΈ°μhash_init()
ν¨μλ₯Ό ν΅ν΄ μ΄κΈ°νν΄μ€λ€. - μ΄ ν¨μλ μλ‘μ΄ νλ‘μΈμ€κ° μμλ λ(
userprog/process.c
μinitd
ν¨μ)μ νλ‘μΈμ€κ° ν¬ν¬λ λ (process.c
μ__do_fork()
) νΈμΆλλ€.
void supplemental_page_table_init(struct supplemental_page_table *spt UNUSED)
{
hash_init(&spt->hash_table, page_hash, page_less, NULL);
}