· 7 years ago · Jan 12, 2019, 03:34 PM
1 struct mmap_file *mf;
2 mf = (struct mmap_file *)malloc(sizeof(struct mmap_file));
3 if(mf == NULL)
4 return MAP_FAILED;
5 mf->mid = t->mmap_files_num_ever;
6 t->mmap_files_num_ever++;
7 mf->file = file_to_map;
8 mf->upage = addr;
9
10 /* Fill in entries in supplementary table and page table */
11 offset = 0;
12 int pg_num = 0;
13 size_t read_bytes = PGSIZE;
14 uint32_t *pte;
15 uint32_t *pd = t->pagedir;
16 for (offset = 0; offset < len; offset += PGSIZE)
17 {
18 /* Fill in page table entry in pagedir, but do not allocate memory */
19 ASSERT (pagedir_get_page (pd, addr + offset) == NULL);
20 pte = lookup_page (pd, addr + offset, true);
21 ASSERT (pte != NULL);
22 ASSERT ((*pte & PTE_P) == 0);
23 *pte = PTE_U | PTE_M;
24 bool is_writable = file_is_writable(t->file_handlers[fd]);
25 *pte |= is_writable ? PTE_W : ~PTE_W;
26 /* Create suppl_pte */
27 if(offset + PGSIZE >= len)
28 read_bytes = len - offset;
29 if (!suppl_pt_insert_mmf (t, pte, is_writable, file_to_map, offset, read_bytes))
30 return MAP_FAILED;
31 pg_num ++;
32 }
33 mf->num_pages = pg_num;
34 if( hash_insert (&t->mmap_files, &mf->elem) != NULL)
35 return MAP_FAILED;
36
37 return mf->mid;
38}
39
40/* Unmap system call : unmap a file */
41static void
42_munmap(mapid_t mapping)
43{
44 struct thread *t = thread_current();
45 struct mmap_file mf;
46 struct hash_elem *h_elem_mf;
47 mf.mid = mapping;
48 h_elem_mf = hash_delete (&t->mmap_files,&mf.elem);
49 /* otherwise, such mapping not exists */
50 if(h_elem_mf)
51 mmap_free_file (h_elem_mf, NULL);
52}