Now that qemu is available on Solaris, don't talk about it being
[pintos-anon] / doc / vm.texi
1 @node Project 3--Virtual Memory
2 @chapter Project 3: Virtual Memory
3
4 By now you should have some familiarity with the inner workings of
5 Pintos.  Your
6 OS can properly handle multiple threads of execution with proper
7 synchronization, and can load multiple user programs at once.  However,
8 the number and size of programs that can run is limited by the machine's
9 main memory size.  In this assignment, you will remove that limitation.
10
11 You will build this assignment on top of the last one.  Test programs
12 from project 2 should also work with project 3.  You should take care to
13 fix any bugs in your project 2 submission before you start work on
14 project 3, because those bugs will most likely cause the same problems
15 in project 3.
16
17 You will continue to handle Pintos disks and file systems the same way
18 you did in the previous assignment (@pxref{Using the File System}).
19
20 The tests for project 3 (and later projects) will probably run faster if
21 you use the qemu emulator, e.g.@: via @code{make check
22 PINTOSOPTS='--qemu'}.
23
24 @menu
25 * Project 3 Background::        
26 * Project 3 Suggested Order of Implementation::  
27 * Project 3 Requirements::      
28 * Project 3 FAQ::               
29 @end menu
30
31 @node Project 3 Background
32 @section Background
33
34 @menu
35 * Project 3 Source Files::      
36 * Memory Terminology::          
37 * Resource Management Overview::  
38 * Managing the Segment Table::  
39 * Managing the Frame Table::    
40 * Managing the Swap Table::     
41 * Managing Memory Mapped Files Back::  
42 @end menu
43
44 @node Project 3 Source Files
45 @subsection Source Files
46
47 You will work in the @file{vm} directory for this project.  The
48 @file{vm} directory contains only @file{Makefile}s.  The only
49 change from @file{userprog} is that this new @file{Makefile} turns on
50 the setting @option{-DVM}.  All code you write will be in new
51 files or in files introduced in earlier projects.
52
53 You will probably be encountering just a few files for the first time:
54
55 @table @file
56 @item devices/disk.h
57 @itemx devices/disk.c
58 Provides access to the physical disk, abstracting away the rather awful
59 IDE interface.  You will use this interface to access the swap disk.
60 @end table
61
62 @node Memory Terminology
63 @subsection Memory Terminology
64
65 Careful definitions are needed to keep discussion of virtual memory from
66 being confusing.  Thus, we begin by presenting some terminology for
67 memory and storage.  Some of these terms should be familiar from project
68 2 (@pxref{Virtual Memory Layout}), but much of it is new.
69
70 @menu
71 * Pages::                       
72 * Frames::                      
73 * Page Tables::                 
74 * Swap Slots::                  
75 @end menu
76
77 @node Pages
78 @subsubsection Pages
79
80 A @dfn{page}, sometimes called a @dfn{virtual page}, is a contiguous
81 region of virtual memory 4,096 bytes (the @dfn{page size}) in length.  A
82 page must be @dfn{page-aligned}, that is, start on a virtual address
83 evenly divisible by the page size.  Thus, a 32-bit virtual address can
84 be divided into a 20-bit @dfn{page number} and a 12-bit @dfn{page
85 offset} (or just @dfn{offset}), like this:
86
87 @example
88 @group
89                31               12 11        0
90               +-------------------+-----------+
91               |    Page Number    |   Offset  |
92               +-------------------+-----------+
93                        Virtual Address
94 @end group
95 @end example
96
97 Each process has an independent set of @dfn{user (virtual) pages}, which
98 are those pages below virtual address @code{PHYS_BASE}, typically
99 @t{0xc0000000} (3 GB).  The set of @dfn{kernel (virtual) pages}, on the
100 other hand, is global, remaining the same regardless of what thread or
101 process is active.  The kernel may access both user and kernel pages,
102 but a user process may access only its own user pages.  @xref{Virtual
103 Memory Layout}, for more information.
104
105 Pintos provides several useful functions for working with virtual
106 addresses.  @xref{Virtual Addresses}, for details.
107
108 @node Frames
109 @subsubsection Frames
110
111 A @dfn{frame}, sometimes called a @dfn{physical frame} or a @dfn{page
112 frame}, is a contiguous region of physical memory.  Like pages, frames
113 must be page-size and page-aligned.  Thus, a 32-bit physical address can
114 be divided into a 20-bit @dfn{frame number} and a 12-bit @dfn{frame
115 offset} (or just @dfn{offset}), like this:
116
117 @example
118 @group
119                31               12 11        0
120               +-------------------+-----------+
121               |    Frame Number   |   Offset  |
122               +-------------------+-----------+
123                        Physical Address
124 @end group
125 @end example
126
127 The 80@var{x}86 doesn't provide any way to directly access memory at a
128 physical address.  Pintos works around this by mapping kernel virtual
129 memory directly to physical memory: the first page of kernel virtual
130 memory is mapped to the first frame of physical memory, the second page
131 to the second frame, and so on.  Thus, frames can be accessed through
132 kernel virtual memory.  
133
134 Pintos provides functions for translating between physical addresses and
135 kernel virtual addresses.  @xref{Virtual Addresses}, for details.
136
137 @node Page Tables
138 @subsubsection Page Tables
139
140 In Pintos, a @dfn{page table} is a data structure that the CPU uses to
141 translate a virtual address to a physical address, that is, from a page
142 to a frame.  The page table format is dictated by the 80@var{x}86
143 architecture.  Pintos provides page table management code in
144 @file{pagedir.c} (@pxref{Page Table}).
145
146 The diagram below illustrates the relationship between pages and frames.
147 The virtual address, on the left, consists of a page number and an
148 offset.  The page table translates the page number into a frame number,
149 which is combined with the unmodified offset to obtain the physical
150 address, on the right.
151
152 @example
153 @group
154                          +----------+
155         .--------------->|Page Table|-----------.
156        /                 +----------+            |
157    0   |  12 11 0                            0   V  12 11 0
158   +---------+----+                          +---------+----+
159   |Page Nr  | Ofs|                          |Frame Nr | Ofs|
160   +---------+----+                          +---------+----+
161    Virt Addr   |                             Phys Addr   ^
162                 \_______________________________________/
163 @end group
164 @end example
165
166 @node Swap Slots
167 @subsubsection Swap Slots
168
169 A @dfn{swap slot} is a contiguous, page-size region of disk space on the
170 swap disk.  Although hardware limitations dictating the placement of
171 slots are looser than for pages and frames, swap slots should be
172 page-aligned because there is no downside in doing so.
173
174 @node Resource Management Overview
175 @subsection Resource Management Overview
176
177 You will need to design the following data structures:
178
179 @table @asis
180 @item Segment table
181
182 Enables page fault handling by supplementing the page table.
183 @xref{Managing the Segment Table}.
184
185 @item Frame table
186
187 Allows efficient implementation of eviction policy.
188 @xref{Managing the Frame Table}.
189
190 @item Swap table
191
192 Tracks usage of swap slots.
193 @xref{Managing the Swap Table}.
194
195 @item Table of file mappings
196
197 Processes may map files into their virtual memory space.  You need a
198 table to track which files are mapped into which pages.
199 @end table
200
201 You do not necessarily need to implement four completely distinct data
202 structures: it may be convenient to wholly or partially merge related
203 resources into a unified data structure.
204
205 For each data structure, you need to determine what information each
206 element should contain.  You also need to decide on the data structure's
207 scope, either local (per-process) or global (applying to the whole
208 system), and how many instances are required within its scope.
209
210 To simplify your design, you may store these data structures in
211 non-pageable memory.  That means that you can be sure that pointers
212 among them will remain valid.
213
214 Possible choices of data structures include arrays, lists, bitmaps, and
215 hash tables.  An array is often the simplest approach, but a sparsely
216 populated array wastes memory.  Lists are also simple, but traversing a
217 long list to find a particular position wastes time.  Both arrays and
218 lists can be resized, but lists more efficiently support insertion and
219 deletion in the middle.
220
221 Pintos includes a bitmap data structure in @file{lib/kernel/bitmap.c}
222 and @file{lib/kernel/bitmap.h}.  A bitmap is an array of bits, each of
223 which can be true or false.  Bitmaps are typically used to track usage
224 in a set of (identical) resources: if resource @var{n} is in use, then
225 bit @var{n} of the bitmap is true.  Pintos bitmaps are fixed in size,
226 although you could extend their implementation to support resizing.
227
228 Pintos also includes a hash table data structure (@pxref{Hash Table}).
229 Pintos hash tables efficiently support insertions and deletions over a
230 wide range of table sizes.
231
232 Although more complex data structures may yield performance or other
233 benefits, they may also needlessly complicate your implementation.
234 Thus, we do not recommend implementing any advanced data structure
235 (e.g.@: a balanced binary tree) as part of your design.
236
237 @node Managing the Segment Table
238 @subsection Managing the Segment Table
239
240 The @dfn{segment table} supplements the page table with additional data
241 about each page.  It is required because of the limitations imposed by
242 the page table's format.  Such a supplementary data structure is often
243 called a ``page table'' also; we call it a segment table to avoid
244 confusion.
245
246 The segment table is used for at least two purposes.  Most importantly,
247 on a page fault, the kernel looks up the virtual page that faulted in
248 the segment table to find out what data should be there.  Second, the
249 kernel consults the segment table when a process terminates, to decide
250 what resources to free.
251
252 You may organize the segment table as you wish.  There are at least two
253 basic approaches to its organization: in terms of segments or in terms
254 of pages.  Optionally, you may use the page table as part of your
255 segment table design.  You will have to modify the Pintos page table
256 implementation in @file{pagedir.c} to do so.  We recommend this approach
257 for advanced students only.  @xref{Page Table Entry Format}, for more
258 information.
259
260 The most important user of the segment table is the page fault handler.
261 In project 2, a page fault always indicated a bug in the kernel or a
262 user program.  In project 3, this is no longer true.  Now, a page fault
263 might only indicate that the page must be brought in from a file or
264 swap.  You will have to implement a more sophisticated page fault
265 handler to handle these cases.  Your page fault handler, which you
266 should implement by modifying @func{page_fault} in
267 @file{threads/exception.c}, needs to do roughly the following:
268
269 @enumerate 1
270 @item
271 Locate the page that faulted in the segment table.  If the memory
272 reference is valid, use the segment table entry to locate the data that
273 goes in the page, which might be in the file system, or in a swap slot,
274 or it might simply be an all-zero page.  If you implement sharing, the
275 page's data might even already be in a page frame, but not in the page
276 table.
277
278 If the page is unmapped, that is, if there's no data there, or if the
279 page lies within kernel virtual memory, or if the access is an attempt
280 to write to a read-only page, then the access is invalid.  Any invalid
281 access terminates the process and thereby frees all of its resources.
282
283 @item
284 Obtain a frame to store the page.  @xref{Managing the Frame Table}, for
285 details.
286
287 If you implement sharing, the data you need may already be in a frame,
288 in which case you must be able to locate that frame.
289
290 @item
291 Fetch the data into the frame, by reading it from the file system or
292 swap, zeroing it, etc.
293
294 If you implement sharing, the page you need may already be in a frame,
295 in which case no action is necessary in this step.
296
297 @item
298 Point the page table entry for the faulting virtual address to the
299 physical page.  You can use the functions in @file{userprog/pagedir.c}.
300 @end enumerate
301
302 @node Managing the Frame Table
303 @subsection Managing the Frame Table
304
305 The @dfn{frame table} contains one entry for each frame that contains a
306 user page.  Each entry in the frame table contains a pointer to the
307 page, if any, that currently occupies it, and other data of your choice.
308 The frame table allows Pintos to efficiently implement an eviction
309 policy, by choosing a page to evict when no frames are free.
310
311 The frames used for user pages should be obtained from the ``user
312 pool,'' by calling @code{palloc_get_page(PAL_USER)}.  You must use
313 @code{PAL_USER} to avoid allocating from the ``kernel pool,'' which
314 could cause some test cases to fail unexpectedly (@pxref{Why
315 PAL_USER?}).  If you modify @file{palloc.c} as part of your frame table
316 implementation, be sure to retain the distinction between the two pools.
317
318 The most important operation on the frame table is obtaining an unused
319 frame.  This is easy when a frame is free.  When none is free, a frame
320 must be made free by evicting some page from its frame.  If no frame can
321 be evicted without allocating a swap slot, but swap is full, some
322 process must be killed to free memory (the choice of process to kill is
323 up to you).
324
325 The process of eviction comprises roughly the following steps:
326
327 @enumerate 1
328 @item
329 Choose a frame to evict, using your page replacement algorithm.  The
330 ``accessed'' and ``dirty'' bits in the page table, described below, will
331 come in handy.
332
333 @item
334 Remove references to the frame from any page table that refers to it.
335
336 Unless you have implemented sharing, only a single page should refer to
337 a frame at any given time.
338
339 @item
340 If necessary, write the page to the file system or to swap.
341 @end enumerate
342
343 The evicted frame may then be used to store a different page.
344
345 @menu
346 * Accessed and Dirty Bits::     
347 @end menu
348
349 @node Accessed and Dirty Bits
350 @subsubsection Accessed and Dirty Bits
351
352 80@var{x}86 hardware provides some assistance for implementing page
353 replacement algorithms, through a pair of bits in the page table entry
354 (PTE) for each page.  On any read or write to a page, the CPU sets the
355 @dfn{accessed bit} to 1 in the page's PTE, and on any write, the CPU
356 sets the @dfn{dirty bit} to 1.  The CPU never resets these bits to 0,
357 but the OS may do so.
358
359 You need to be aware of @dfn{aliases}, that is, two (or more) pages that
360 refer to the same frame.  When an aliased frame is accessed, the
361 accessed and dirty bits are updated in only one page table entry (the
362 one for the page used for access).  The accessed and dirty bits for the
363 other aliases are not updated.
364
365 In Pintos, every user virtual page is aliased to its kernel virtual
366 page.  You must manage these aliases somehow.  For example, your code
367 could check and update the accessed and dirty bits for both addresses.
368 Alternatively, the kernel could avoid the problem by only accessing user
369 data through the user virtual address.
370
371 Other aliases should only arise if you implement sharing for extra
372 credit (@pxref{VM Extra Credit}), or if there is a bug in your code.
373
374 @xref{Page Table Accessed and Dirty Bits}, for details of the functions
375 to work with accessed and dirty bits.
376
377 @node Managing the Swap Table
378 @subsection Managing the Swap Table
379
380 The swap table tracks in-use and free swap slots.  It should allow
381 picking an unused swap slot for evicting a page from its frame to the
382 swap disk.  It should allow freeing a swap slot when its page is read
383 back or the process whose page was swapped is terminated.
384
385 You may use the disk on interface @code{hd1:1} as the swap disk, using
386 the disk interface prototyped in @code{devices/disk.h}.  From the
387 @file{vm/build} directory, use the command @code{pintos-mkdisk swap.dsk
388 @var{n}} to create an @var{n} MB swap disk named @file{swap.dsk}.
389 Afterward, @file{swap.dsk} will automatically be attached as
390 @code{hd1:1} when you run @command{pintos}.  Alternatively, you can tell
391 @command{pintos} to use a temporary @var{n}-MB swap disk for a single
392 run with @option{--swap-disk=@var{n}}.
393
394 Swap slots should be allocated lazily, that is, only when they are
395 actually required by eviction.  Reading data pages from the executable
396 and writing them to swap immediately at process startup is not lazy.
397 Swap slots should not be reserved to store particular pages.
398
399 Free a swap slot when its contents are read back into a frame.
400
401 @node Managing Memory Mapped Files Back
402 @subsection Managing Memory Mapped Files
403
404 The file system is most commonly accessed with @code{read} and
405 @code{write} system calls.  A secondary interface is to ``map'' the file
406 into virtual pages, using the @code{mmap} system call.  The program can
407 then use memory instructions directly on the file data.
408
409 Suppose file @file{foo} is @t{0x1000} bytes (4 kB, or one page) long.
410 If @file{foo} is mapped into memory starting at address @t{0x5000}, then
411 any memory accesses to locations @t{0x5000}@dots{}@t{0x5fff} will access
412 the corresponding bytes of @file{foo}.
413
414 Here's a program that uses @code{mmap} to print a file to the console.
415 It opens the file specified on the command line, maps it at virtual
416 address @t{0x10000000}, writes the mapped data to the console (fd 1),
417 and unmaps the file.
418
419 @example
420 #include <stdio.h>
421 #include <syscall.h>
422 int main (int argc UNUSED, char *argv[]) 
423 @{
424   void *data = (void *) 0x10000000;     /* @r{Address at which to map.} */
425
426   int fd = open (argv[1]);              /* @r{Open file.} */
427   mapid_t map = mmap (fd, data);        /* @r{Map file.} */
428   write (1, data, filesize (fd));       /* @r{Write file to console.} */
429   munmap (map);                         /* @r{Unmap file (optional).} */
430   return 0;
431 @}
432 @end example
433
434 A similar program with full error handling is included as @file{mcat.c}
435 in the @file{examples} directory, which also contains @file{mcp.c} as a
436 second example of @code{mmap}.
437
438 Your submission must be able to track what memory is used by memory
439 mapped files.  This is necessary to properly handle page faults in the
440 mapped regions and to ensure that mapped files do not overlap any other
441 segments within the process.
442
443 @node Project 3 Suggested Order of Implementation
444 @section Suggested Order of Implementation
445
446 We suggest the following initial order of implementation:
447
448 @enumerate 1
449 @item
450 Frame table (@pxref{Managing the Frame Table}).  Change @file{process.c}
451 to use your frame table allocator.
452
453 Do not implement swapping yet.  If you run out of frames, fail the
454 allocator or panic the kernel.
455
456 After this step, your kernel should still pass all the project 2 test
457 cases.
458
459 @item
460 Segment table and page fault handler (@pxref{Managing the Segment
461 Table}).  Change @file{process.c} to record the necessary information in
462 the segment table when loading an executable and setting up its stack.
463 Implement loading of code and data segments in the page fault handler.
464 For now, consider only valid accesses.
465
466 After this step, your kernel should pass all of the project 2
467 functionality test cases, but only some of the robustness tests.
468 @end enumerate
469
470 From here, you can implement stack growth, mapped files, and page
471 reclamation on process exit in parallel.
472
473 The next step is to implement eviction (@pxref{Managing the Frame
474 Table}).  Initially you could choose the page to evict randomly.  At
475 this point, you need to consider how to manage accessed and dirty bits
476 and aliasing of user and kernel pages.  Synchronization is also a
477 concern: how do you deal with it if process A faults on a page whose
478 frame process B is in the process of evicting?  Finally, implement a
479 eviction strategy such as the clock algorithm.
480
481 @node Project 3 Requirements
482 @section Requirements
483
484 This assignment is an open-ended design problem.  We are going to say as
485 little as possible about how to do things.  Instead we will focus on
486 what functionality we require your OS to support.  We will expect
487 you to come up with a design that makes sense.  You will have the
488 freedom to choose how to handle page faults, how to organize the swap
489 disk, how to implement paging, etc.
490
491 @menu
492 * Project 3 Design Document::   
493 * Paging::                      
494 * Stack Growth::                
495 * Memory Mapped Files::         
496 @end menu
497
498 @node Project 3 Design Document
499 @subsection Design Document
500
501 Before you turn in your project, you must copy @uref{vm.tmpl, , the
502 project 3 design document template} into your source tree under the name
503 @file{pintos/src/vm/DESIGNDOC} and fill it in.  We recommend that you
504 read the design document template before you start working on the
505 project.  @xref{Project Documentation}, for a sample design document
506 that goes along with a fictitious project.
507
508 @node Paging
509 @subsection Paging
510
511 Implement paging for segments loaded from executables.  All of these
512 pages should be loaded lazily, that is, only as the kernel intercepts
513 page faults for them.  Upon eviction, pages modified since load (e.g.@:
514 as indicated by the ``dirty bit'') should be written to swap.
515 Unmodified pages, including read-only pages, should never be written to
516 swap because they can always be read back from the executable.
517
518 Implement a global page replacement algorithm that approximates LRU.
519 Your algorithm should perform at least as well as the ``second chance''
520 or ``clock'' algorithm.
521
522 Your design should allow for parallelism.  If one page fault requires
523 I/O, in the meantime processes that do not fault should continue
524 executing and other page faults that do not require I/O should be able
525 to complete.  This will require some synchronization effort.
526
527 You'll need to modify the core of the program loader, which is the loop
528 in @func{load_segment} in @file{userprog/process.c}.  Each time around
529 the loop, @code{page_read_bytes} receives the number of bytes to read
530 from the executable file and @code{page_zero_bytes} receives the number
531 of bytes to initialize to zero following the bytes read.  The two always
532 sum to @code{PGSIZE} (4,096).  The handling of a page depends on these
533 variables' values:
534
535 @itemize @bullet
536 @item
537 If @code{page_read_bytes} equals @code{PGSIZE}, the page should be demand
538 paged from disk on its first access.
539
540 @item
541 If @code{page_zero_bytes} equals @code{PGSIZE}, the page does not need to
542 be read from disk at all because it is all zeroes.  You should handle
543 such pages by creating a new page consisting of all zeroes at the
544 first page fault.
545
546 @item
547 Otherwise, neither @code{page_read_bytes} nor @code{page_zero_bytes}
548 equals @code{PGSIZE}.  In this case, an initial part of the page is to
549 be read from disk and the remainder zeroed.
550 @end itemize
551
552 @node Stack Growth
553 @subsection Stack Growth
554
555 Implement stack growth.  In project 2, the stack was a single page at
556 the top of the user virtual address space, and programs were limited to
557 that much stack.  Now, if the stack grows past its current size,
558 allocate additional pages as necessary.
559
560 Allocate additional pages only if they ``appear'' to be stack accesses.
561 Devise a heuristic that attempts to distinguish stack accesses from
562 other accesses.
563
564 User programs are buggy if they write to the stack below the stack
565 pointer, because typical real OSes may interrupt a process at any time
566 to deliver a ``signal,'' which pushes data on the stack.@footnote{This rule is
567 common but not universal.  One modern exception is the
568 @uref{http://www.x86-64.org/documentation/abi.pdf, @var{x}86-64 System V
569 ABI}, which designates 128 bytes below the stack pointer as a ``red
570 zone'' that may not be modified by signal or interrupt handlers.}
571 However, the 80@var{x}86 @code{PUSH} instruction checks access
572 permissions before it adjusts the stack pointer, so it may cause a page
573 fault 4 bytes below the stack pointer.  (Otherwise, @code{PUSH} would
574 not be restartable in a straightforward fashion.)  Similarly, the
575 @code{PUSHA} instruction pushes 32 bytes at once, so it can fault 32
576 bytes below the stack pointer.
577
578 You will need to be able to obtain the current value of the user
579 program's stack pointer.  Within a system call or a page fault generated
580 by a user program, you can retrieve it from @code{esp} member of the
581 @struct{intr_frame} passed to @func{syscall_handler} or
582 @func{page_fault}, respectively.  If you verify user pointers before
583 accessing them (@pxref{Accessing User Memory}), these are the only cases
584 you need to handle.  On the other hand, if you depend on page faults to
585 detect invalid memory access, you will need to handle another case,
586 where a page fault occurs in the kernel.  Reading @code{esp} out of the
587 @struct{intr_frame} passed to @func{page_fault} in that case will obtain
588 the kernel stack pointer, not the user stack pointer.  You will need to
589 arrange another way, e.g.@: by saving @code{esp} into @struct{thread} on
590 the initial transition from user to kernel mode.
591
592 You may impose some absolute limit on stack size, as do most OSes.
593 Some OSes make the limit user-adjustable, e.g.@: with the
594 @command{ulimit} command on many Unix systems.  On many GNU/Linux systems,
595 the default limit is 8 MB.
596
597 The first stack page need not be allocated lazily.  You can initialize
598 it with the command line arguments at load time, with no need to wait
599 for it to be faulted in.  (Even if you did wait, the very first
600 instruction in the user program is likely to be one that faults in the
601 page.)
602
603 All stack pages should be candidates for eviction.  An evicted stack
604 page should be written to swap.
605
606 @node Memory Mapped Files
607 @subsection Memory Mapped Files
608
609 Implement memory mapped files, including the following system calls.
610
611 @deftypefn {System Call} mapid_t mmap (int @var{fd}, void *@var{addr})
612 Maps the file open as @var{fd} into the process's virtual address
613 space.  The entire file is mapped into consecutive virtual pages
614 starting at @var{addr}.
615
616 Your VM system must lazily load pages in @code{mmap} regions and use the
617 @code{mmap}'d file itself as backing store for the mapping.  That is,
618 evicting a page mapped by @code{mmap} writes it back to the file it was
619 mapped from.
620
621 If the file's length is not a multiple of @code{PGSIZE}, then some
622 bytes in the final mapped page ``stick out'' beyond the end of the
623 file.  Set these bytes to zero when the page is faulted in from disk,
624 and discard them when the page is written back to disk.
625
626 If successful, this function returns a ``mapping ID'' that
627 uniquely identifies the mapping within the process.  On failure,
628 it must return -1, which otherwise should not be a valid mapping id,
629 and the process's mappings must be unchanged.
630
631 A call to @code{mmap} may fail if the file open as @var{fd} has a
632 length of zero bytes.  It must fail if @var{addr} is not page-aligned
633 or if the range of pages mapped overlaps any existing set of mapped
634 pages, including the stack or pages mapped at executable load time.
635 It must also fail if @var{addr} is 0, because some Pintos code assumes
636 virtual page 0 is not mapped.  Finally, file descriptors 0 and 1,
637 representing console input and output, are not mappable.
638 @end deftypefn
639
640 @deftypefn {System Call} void munmap (mapid_t @var{mapping})
641 Unmaps the mapping designated by @var{mapping}, which must be a
642 mapping ID returned by a previous call to @code{mmap} by the same
643 process that has not yet been unmapped.
644 @end deftypefn
645
646 All mappings are implicitly unmapped when a process exits, whether via
647 @code{exit} or by any other means.  When a mapping is unmapped, whether
648 implicitly or explicitly, all pages written to by the process are
649 written back to the file, and pages not written must not be.  The pages
650 are then removed from the process's list of virtual pages.
651
652 Closing or removing a file does not unmap any of its mappings.  Once
653 created, a mapping is valid until @code{munmap} is called or the process
654 exits, following the Unix convention.  @xref{Removing an Open File}, for
655 more information.
656
657 If two or more processes map the same file, there is no requirement that
658 they see consistent data.  Unix handles this by making the two mappings
659 share the same physical page, but the @code{mmap} system call also has
660 an argument allowing the client to specify whether the page is shared or
661 private (i.e.@: copy-on-write).
662
663 @node Project 3 FAQ
664 @section FAQ
665
666 @table @b
667 @item How much code will I need to write?
668
669 Here's a summary of our reference solution, produced by the
670 @command{diffstat} program.  The final row gives total lines inserted
671 and deleted; a changed line counts as both an insertion and a deletion.
672
673 This summary is relative to the Pintos base code, but the reference
674 solution for project 3 starts from the reference solution to project 2.
675 @xref{Project 2 FAQ}, for the summary of project 2.
676
677 The reference solution represents just one possible solution.  Many
678 other solutions are also possible and many of those differ greatly from
679 the reference solution.  Some excellent solutions may not modify all the
680 files modified by the reference solution, and some may modify files not
681 modified by the reference solution.
682
683 @verbatim
684  Makefile.build       |    4
685  devices/timer.c      |   42 ++
686  threads/init.c       |    5
687  threads/interrupt.c  |    2
688  threads/thread.c     |   31 +
689  threads/thread.h     |   37 +-
690  userprog/exception.c |   12
691  userprog/pagedir.c   |   10
692  userprog/process.c   |  319 +++++++++++++-----
693  userprog/syscall.c   |  545 ++++++++++++++++++++++++++++++-
694  userprog/syscall.h   |    1
695  vm/frame.c           |  162 +++++++++
696  vm/frame.h           |   23 +
697  vm/page.c            |  297 ++++++++++++++++
698  vm/page.h            |   50 ++
699  vm/swap.c            |   85 ++++
700  vm/swap.h            |   11
701  17 files changed, 1532 insertions(+), 104 deletions(-)
702 @end verbatim
703
704 @item Do we need a working Project 2 to implement Project 3?
705
706 Yes.
707
708 @item What extra credit is available?
709 @anchor{VM Extra Credit}
710
711 You may implement sharing: when multiple processes are created that use
712 the same executable file, share read-only pages among those processes
713 instead of creating separate copies of read-only segments for each
714 process.  If you carefully designed your page table data structures,
715 sharing of read-only pages should not make this part significantly
716 harder.
717
718 @item Does the virtual memory system need to support data segment growth?
719
720 No.  The size of the data segment is determined by the linker.  We still
721 have no dynamic allocation in Pintos (although it is possible to
722 ``fake'' it at the user level by using memory-mapped files).  Supporting
723 data segment growth should add little additional complexity to a
724 well-designed system.
725
726 @item Why should I use @code{PAL_USER} for allocating page frames?
727 @anchor{Why PAL_USER?}
728
729 Passing @code{PAL_USER} to @func{palloc_get_page} causes it to allocate
730 memory from the user pool, instead of the main kernel pool.  Running out
731 of pages in the user pool just causes user programs to page, but running
732 out of pages in the kernel pool will cause many failures because so many
733 kernel functions need to obtain memory.
734 You can layer some other allocator on top of @func{palloc_get_page} if
735 you like, but it should be the underlying mechanism.
736
737 Also, you can use the @option{-ul} option to @command{pintos} to limit
738 the size of the user pool, which makes it easy to test your VM
739 implementation with various user memory sizes.
740 @end table