diff options
| author | manuel <manuel@mausz.at> | 2012-06-19 01:44:56 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2012-06-19 01:44:56 +0200 |
| commit | e88a8c4c379d721e9901752d440a05295087da11 (patch) | |
| tree | b89070c525614267811a10b77a4dbc49ffd96b03 /vm/page.c | |
| parent | d9e0c55d118d0a3923b440b7811f8d1d6db9e1d7 (diff) | |
| download | progos-e88a8c4c379d721e9901752d440a05295087da11.tar.gz progos-e88a8c4c379d721e9901752d440a05295087da11.tar.bz2 progos-e88a8c4c379d721e9901752d440a05295087da11.zip | |
implement page table and use it for lazy loading of segments
Diffstat (limited to 'vm/page.c')
| -rw-r--r-- | vm/page.c | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/vm/page.c b/vm/page.c new file mode 100644 index 0000000..fa2433d --- /dev/null +++ b/vm/page.c | |||
| @@ -0,0 +1,147 @@ | |||
| 1 | #include <string.h> | ||
| 2 | #include "filesys/file.h" | ||
| 3 | #include "userprog/process.h" | ||
| 4 | #include "threads/thread.h" | ||
| 5 | #include "threads/malloc.h" | ||
| 6 | #include "threads/palloc.h" | ||
| 7 | #include "vm/page.h" | ||
| 8 | |||
| 9 | static void page_table_entry_free (struct hash_elem *e, void *aux UNUSED); | ||
| 10 | static unsigned page_table_hash (const struct hash_elem *e, void *aux UNUSED); | ||
| 11 | static bool page_table_cmp_less (const struct hash_elem *a, const struct hash_elem *b, | ||
| 12 | void *aux UNUSED); | ||
| 13 | static bool page_table_insert (struct hash *ht, struct page_table_entry *pte); | ||
| 14 | static bool page_load_segment (struct page_table_entry *pte); | ||
| 15 | static bool page_load_mmf (struct page_table_entry *pte); | ||
| 16 | |||
| 17 | void | ||
| 18 | page_table_init (struct hash *ht) | ||
| 19 | { | ||
| 20 | hash_init (ht, page_table_hash, page_table_cmp_less, NULL); | ||
| 21 | } | ||
| 22 | |||
| 23 | unsigned | ||
| 24 | page_table_hash (const struct hash_elem *e, void *aux UNUSED) | ||
| 25 | { | ||
| 26 | const struct page_table_entry *pte = hash_entry (e, struct page_table_entry, elem); | ||
| 27 | return hash_bytes (&pte->uvaddr, sizeof pte->uvaddr); | ||
| 28 | } | ||
| 29 | |||
| 30 | bool | ||
| 31 | page_table_cmp_less (const struct hash_elem *a, const struct hash_elem *b, | ||
| 32 | void *aux UNUSED) | ||
| 33 | { | ||
| 34 | const struct page_table_entry *pte1 = hash_entry (a, struct page_table_entry, elem); | ||
| 35 | const struct page_table_entry *pte2 = hash_entry (b, struct page_table_entry, elem); | ||
| 36 | return (pte1->uvaddr < pte2->uvaddr); | ||
| 37 | } | ||
| 38 | |||
| 39 | void | ||
| 40 | page_table_free (struct hash *ht) | ||
| 41 | { | ||
| 42 | hash_destroy (ht, page_table_entry_free); | ||
| 43 | } | ||
| 44 | |||
| 45 | void | ||
| 46 | page_table_entry_free (struct hash_elem *e, void *aux UNUSED) | ||
| 47 | { | ||
| 48 | struct page_table_entry *pte = hash_entry (e, struct page_table_entry, elem); | ||
| 49 | free (pte); | ||
| 50 | } | ||
| 51 | |||
| 52 | bool | ||
| 53 | page_table_insert (struct hash *ht, struct page_table_entry *pte) | ||
| 54 | { | ||
| 55 | if (pte == NULL) | ||
| 56 | return false; | ||
| 57 | return (hash_insert (ht, &pte->elem) == NULL); | ||
| 58 | } | ||
| 59 | |||
| 60 | bool | ||
| 61 | page_table_insert_segment (struct file *file, off_t ofs, uint8_t *upage, | ||
| 62 | uint32_t read_bytes, uint32_t zero_bytes, bool writable) | ||
| 63 | { | ||
| 64 | struct page_table_entry *pte = malloc (sizeof *pte); | ||
| 65 | if (pte == NULL) | ||
| 66 | return false; | ||
| 67 | |||
| 68 | pte->uvaddr = upage; | ||
| 69 | pte->type = PAGE_SEGMENT; | ||
| 70 | pte->loaded = false; | ||
| 71 | pte->segment.file = file; | ||
| 72 | pte->segment.ofs = ofs; | ||
| 73 | pte->segment.read_bytes = read_bytes; | ||
| 74 | pte->segment.zero_bytes = zero_bytes; | ||
| 75 | pte->segment.writable = writable; | ||
| 76 | |||
| 77 | return page_table_insert (&thread_current ()->page_table, pte); | ||
| 78 | } | ||
| 79 | |||
| 80 | struct page_table_entry * | ||
| 81 | page_table_fetch (struct hash *ht, void *uvaddr) | ||
| 82 | { | ||
| 83 | struct page_table_entry pte; | ||
| 84 | struct hash_elem *e; | ||
| 85 | |||
| 86 | pte.uvaddr = uvaddr; | ||
| 87 | e = hash_find (ht, &pte.elem); | ||
| 88 | return ((e != NULL) ? hash_entry (e, struct page_table_entry, elem) : NULL); | ||
| 89 | } | ||
| 90 | |||
| 91 | bool | ||
| 92 | page_load (struct page_table_entry *pte) | ||
| 93 | { | ||
| 94 | if (pte->loaded) | ||
| 95 | return true; | ||
| 96 | |||
| 97 | switch (pte->type) | ||
| 98 | { | ||
| 99 | case PAGE_SEGMENT: | ||
| 100 | return page_load_segment (pte); | ||
| 101 | case PAGE_MEMORY_MAPPED_FILE: | ||
| 102 | return page_load_mmf (pte); | ||
| 103 | default: | ||
| 104 | ASSERT (false); | ||
| 105 | break; | ||
| 106 | } | ||
| 107 | return false; | ||
| 108 | } | ||
| 109 | |||
| 110 | bool | ||
| 111 | page_load_segment (struct page_table_entry *pte) | ||
| 112 | { | ||
| 113 | struct segment *data = &pte->segment; | ||
| 114 | |||
| 115 | file_seek (data->file, data->ofs); | ||
| 116 | |||
| 117 | /* Get a page of memory. */ | ||
| 118 | uint8_t *kpage = palloc_get_page (PAL_USER); | ||
| 119 | if (kpage == NULL) | ||
| 120 | return false; | ||
| 121 | |||
| 122 | /* Load this page. */ | ||
| 123 | if (file_read (data->file, kpage, data->read_bytes) | ||
| 124 | != (int) data->read_bytes) | ||
| 125 | { | ||
| 126 | palloc_free_page (kpage); | ||
| 127 | return false; | ||
| 128 | } | ||
| 129 | memset (kpage + data->read_bytes, 0, data->zero_bytes); | ||
| 130 | |||
| 131 | /* Add the page to the process's address space. */ | ||
| 132 | if (!process_install_page (pte->uvaddr, kpage, data->writable)) | ||
| 133 | { | ||
| 134 | palloc_free_page (kpage); | ||
| 135 | return false; | ||
| 136 | } | ||
| 137 | |||
| 138 | pte->loaded = true; | ||
| 139 | return true; | ||
| 140 | } | ||
| 141 | |||
| 142 | bool | ||
| 143 | page_load_mmf (struct page_table_entry *pte) | ||
| 144 | { | ||
| 145 | //TODO: implement | ||
| 146 | return false; | ||
| 147 | } | ||
