Update docs.
[pintos-anon] / doc / vm.texi
1 @node Project 3--Virtual Memory, Project 4--File Systems, Project 2--User Programs, Top
2 @chapter Project 3: Virtual Memory
3
4 By now you should be familiar with the inner workings of Pintos.
5 You've already come a long way: your OS can properly handle multiple
6 threads of execution with proper synchronization, and can load
7 multiple user programs at once.  However, when loading user programs,
8 your OS is limited by how much main memory the simulated machine has.
9 In this assignment, you will remove that limitation.
10
11 You will be using the @file{vm} directory for this project.  The
12 @file{vm} directory contains only the @file{Makefile}s.  The only
13 change from @file{userprog} is that this new @file{Makefile} turns on
14 the setting @option{-DVM}.  All code you write will either be newly
15 generated files (e.g.@: if you choose to implement your paging code in
16 their own source files), or will be modifications to pre-existing code
17 (e.g.@: you will change the behavior of @file{process.c}
18 significantly).
19
20 There are only a couple of source files you will probably be
21 encountering for the first time:
22
23 @table @file
24 @item devices/disk.h
25 @itemx devices/disk.c
26 Provides access to the physical disk, abstracting away the rather
27 awful IDE interface.
28 @end table
29
30 You will be building this assignment on the last one.  It will benefit
31 you to get your project 2 in good working order before this assignment
32 so those bugs don't keep haunting you.
33
34 All the test programs from the previous project should also work with
35 this project.  You should also write programs to test the new features
36 introduced in this project.
37
38 Your submission should define @code{THREAD_JOIN_IMPLEMENTED} in
39 @file{constants.h} (@pxref{Conditional Compilation}).
40
41 @menu
42 * VM Design::                   
43 * Page Faults::                 
44 * Disk as Backing Store::       
45 * Memory Mapped Files::         
46 * Stack::                       
47 * Problem 3-1 Page Table Management::  
48 * Problem 3-2 Paging To and From Disk::  
49 * Problem 3-3 Memory Mapped Files::  
50 * Virtual Memory FAQ::          
51 @end menu
52
53 @node VM Design
54 @section A Word about Design
55
56 It is important for you to note that in addition to getting virtual
57 memory working, this assignment is also meant to be an open-ended
58 design problem.  We will expect you to come up with a design that
59 makes sense.  You will have the freedom to choose how to handle page
60 faults, how to organize the swap disk, how to implement paging, etc.
61 In each case, we will expect you to provide a defensible justification
62 in your design documentation as to why your choices are reasonable.
63 You should evaluate your design on all the available criteria: speed
64 of handling a page fault, space overhead in memory, minimizing the
65 number of page faults, simplicity, etc.
66
67 In keeping with this, you will find that we are going to say as little
68 as possible about how to do things.  Instead we will focus on what end
69 functionality we require your OS to support.
70
71 @node Page Faults
72 @section Page Faults
73
74 For the last assignment, whenever a context switch occurred, the new
75 process would install its own page table into the machine.  The page
76 table contained all the virtual-to-physical translations for the
77 process.  Whenever the processor needed to look up a translation, it
78 consulted the page table.  As long as the process only accessed
79 memory that it didn't own, all was well.  If the process accessed
80 memory it didn't own, it ``page faulted'' and @func{page_fault}
81 terminated the process.
82
83 When we implement virtual memory, the rules have to change.  A page
84 fault is no longer necessarily an error, since it might only indicate
85 that the page must be brought in from a disk file or from swap.  You
86 will have to implement a more sophisticated page fault handler to
87 handle these cases.
88
89 On the 80@var{x}86, the page table format is fixed by hardware.  We
90 have provided code for managing page tables for you to use in
91 @file{userprog/pagedir.c}.  The functions in there should provide an
92 abstract interface to all the page table functionality that you need
93 to complete the project.  However, you may still find it worthwhile to
94 understand a little about the hardware page table format, so we'll go
95 into a little of detail about that in this section.
96
97 The top-level paging data structure is a 4 kB page called the ``page
98 directory'' (PD) arranged as an array of 1,024 32-bit page directory
99 entries (PDEs), each of which represents 4 MB of virtual memory.  Each
100 PDE may point to the physical address of another 4 kB page called a
101 ``page table'' (PT) arranged in the same fashion as an array of 1,024
102 32-bit page table entries (PTEs), each of which translates a single 4
103 kB virtual page into physical memory.
104
105 Thus, translation of a virtual address into a physical address follows
106 the three-step process illustrated in the diagram
107 below:@footnote{Actually, virtual to physical translation on the
108 80@var{x}86 architecture happens via an intermediate ``linear
109 address,'' but Pintos (and most other 80@var{x}86 OSes) set up the CPU
110 so that linear and virtual addresses are one and the same, so that you
111 can effectively ignore this CPU feature.}
112
113 @enumerate 1
114 @item
115 The top 10 bits of the virtual address (bits 22:32) are used to index
116 into the page directory.  If the PDE is marked ``present,'' the
117 physical address of a page table is read from the PDE thus obtained.
118 If the PDE is marked ``not present'' then a page fault occurs.
119
120 @item
121 The next 10 bits of the virtual address (bits 12:22) are used to index
122 into the page table.  If the PTE is marked ``present,'' the physical
123 address of a data page is read from the PTE thus obtained.  If the PTE
124 is marked ``not present'' then a page fault occurs.
125
126
127 @item
128 The bottom 12 bits of the virtual address (bits 0:12) are added to the
129 data page's physical base address, producing the final physical
130 address.
131 @end enumerate
132
133 @example
134 @group
135 32                    22                     12                      0
136 +--------------------------------------------------------------------+
137 | Page Directory Index |   Page Table Index   |    Page Offset       |
138 +--------------------------------------------------------------------+
139              |                    |                     |
140      _______/             _______/                _____/
141     /                    /                       /
142    /    Page Directory  /      Page Table       /    Data Page
143   /     .____________. /     .____________.    /   .____________.
144   |1,023|____________| |1,023|____________|    |   |____________|
145   |1,022|____________| |1,022|____________|    |   |____________|
146   |1,021|____________| |1,021|____________|    \__\|____________|
147   |1,020|____________| |1,020|____________|       /|____________|
148   |     |            | |     |            |        |            |
149   |     |            | \____\|            |_       |            |
150   |     |      .     |      /|      .     | \      |      .     |
151   \____\|      .     |_      |      .     |  |     |      .     |
152        /|      .     | \     |      .     |  |     |      .     |
153         |      .     |  |    |      .     |  |     |      .     |
154         |            |  |    |            |  |     |            |
155         |____________|  |    |____________|  |     |____________|
156        4|____________|  |   4|____________|  |     |____________|
157        3|____________|  |   3|____________|  |     |____________|
158        2|____________|  |   2|____________|  |     |____________|
159        1|____________|  |   1|____________|  |     |____________|
160        0|____________|  \__\0|____________|  \____\|____________|
161                            /                      /
162 @end group
163 @end example
164
165 Header @file{threads/mmu.h} has useful functions for various
166 operations on virtual addresses.  You should look over the header
167 yourself, but its most important functions include these:
168
169 @table @code
170 @item pd_no(@var{va})
171 Returns the page directory index in virtual address @var{va}.
172
173 @item pt_no(@var{va})
174 Returns the page table index in virtual address @var{va}.
175
176 @item pg_ofs(@var{va})
177 Returns the page offset in virtual address @var{va}.
178
179 @item pg_round_down(@var{va})
180 Returns @var{va} rounded down to the nearest page boundary, that is,
181 @var{va} but with its page offset set to 0.
182
183 @item pg_round_up(@var{va})
184 Returns @var{va} rounded up to the nearest page boundary.
185 @end table
186
187 @node Disk as Backing Store
188 @section Disk as Backing Store
189
190 In VM systems, since memory is less plentiful than disk, you will
191 effectively use memory as a cache for disk.  Looking at it from
192 another angle, you will use disk as a backing store for memory.  This
193 provides the abstraction of an (almost) unlimited virtual memory size.
194 Part of your task in this project is to do this, with the additional
195 constraint that your performance should be close to that provided by
196 physical memory.  You will use the page tables' ``dirty'' bits to
197 denote whether pages need to be written back to disk when they're
198 evicted from main memory and the ``accessed'' bit for page replacement
199 algorithms.  Whenever the hardware writes memory, it sets the dirty
200 bit, and if it reads or writes to the page, it sets the accessed bit.
201
202 As with any caching system, performance depends on the policy used to
203 decide which things are kept in memory and which are only stored on
204 disk.  On a page fault, the kernel must decide which page to replace.
205 Ideally, it will throw out a page that will not be referenced for a
206 long time, keeping in memory those pages that are soon to be
207 referenced.  Another consideration is that if the replaced page has
208 been modified, the page must be first saved to disk before the needed
209 page can be brought in.  Many virtual memory systems avoid this extra
210 overhead by writing modified pages to disk in advance, so that later
211 page faults can be completed more quickly.
212
213 @node Memory Mapped Files
214 @section Memory Mapped Files
215
216 The traditional way to access the file system is via @code{read} and
217 @code{write} system calls, but that requires an extra level of copying
218 between the kernel and the user level.  A secondary interface is
219 simply to ``map'' the file into the virtual address space.  The
220 program can then use load and store instructions directly on the file
221 data.  (An alternative way of viewing the file system is as ``durable
222 memory.''  Files just store data structures.  If you access data
223 structures in memory using load and store instructions, why not access
224 data structures in files the same way?)
225
226 Memory mapped files are typically implemented using system calls.  One
227 system call maps the file to a particular part of the address space.
228 For example, one might conceptually map the file @file{foo}, which is
229 1000 bytes
230 long, starting at address 5000.  Assuming that nothing else is already
231 at virtual addresses 5000@dots{}6000, any memory accesses to these
232 locations will access the corresponding bytes of @file{foo}.
233
234 A consequence of memory mapped files is that address spaces are
235 sparsely populated with lots of segments, one for each memory mapped
236 file (plus one each for code, data, and stack).  You will implement
237 memory mapped files in problem 3-3.  You should
238 design your solutions to problems 3-1 and 3-2 to anticipate this.
239
240 @node Stack
241 @section Stack
242
243 In project 2, the stack was a single page at the top of the user
244 virtual address space.  The stack's location does not change in this
245 project, but your kernel should allocate additional pages to the stack
246 on demand.  That is, if the stack grows past its current bottom, the
247 system should allocate additional pages for the stack as necessary
248 (unless those pages are unavailable because they are in use by another
249 segment).
250
251 It is impossible to predict how large the stack will grow at compile
252 time, so we must allocate pages as necessary.  You should only
253 allocate additional pages if they ``appear'' to be stack accesses.
254 You must devise a heuristic that attempts to distinguish stack
255 accesses from other accesses.  Document and explain the heuristic in
256 your design documentation.
257
258 The first stack page need not be loaded lazily.  You can initialize it
259 with the command line at load time, with no need to wait for it to be
260 faulted in.  Even if you did wait, the very first instruction in the
261 user program is likely to be one that faults in the page.
262
263 @node Problem 3-1 Page Table Management
264 @section Problem 3-1: Page Table Management
265
266 Implement page directory and page table management to support virtual
267 memory.  You will need data structures to accomplish the following
268 tasks:
269
270 @itemize @bullet
271 @item
272 Some way of translating in software from virtual page frames to
273 physical page frames.  Consider using a hash table (@pxref{Hash
274 Table}).
275
276 It is possible to do this translation without adding a new data
277 structure, by modifying the code in @file{userprog/pagedir.c}.  However,
278 if you do that you'll need to carefully study and understand section 3.7
279 in @bibref{IA32-v3}, and in practice it is probably easier to add a new
280 data structure.
281
282 @item
283 Some way of finding a page on disk if it is not in memory.  You won't
284 need this data structure until problem 3-2, but planning ahead is a
285 good idea.
286
287 You can generalize the virtual-to-physical page table, so that it allows
288 you to locate a page wherever it is in physical memory or on disk, or
289 you can make this a separate table.
290
291 @item
292 Some way of translating from physical page frames back to virtual page
293 frames, so that when you evict a physical page from its frame, you can
294 invalidate its translation(s).
295 @end itemize
296
297 The page fault handler, @func{page_fault} in
298 @file{threads/exception.c}, needs to do roughly the following:
299
300 @enumerate 1
301 @item
302 Locate the page backing the virtual
303 address that faulted.  It might be in the file system, in swap,
304 or it might be an invalid virtual address.
305 If you implement sharing, it might even
306 already be in physical memory and just not set up in the page table,
307
308 If the virtual address is invalid, that is, if there's nothing
309 assigned to go there, or if the virtual address is above
310 @code{PHYS_BASE}, meaning that it belongs to the kernel instead of the
311 user, then the process's memory access must be disallowed.  You should
312 terminate the process at this point, being sure to free all of its
313 resources.
314
315 @item
316 If the page is not in physical memory, fetch it by appropriate means.
317 If necessary to make room, first evict some other page from memory.
318 (When you do that you need to first remove references to the page from
319 any page table that refers to it.)
320
321 @item
322 Point the page table entry for the faulting virtual address to the
323 physical page.  You can use the functions in @file{userprog/pagedir.c}.
324 @end enumerate
325
326 You'll need to modify the ELF loader in @file{userprog/process.c} to
327 do page table management according to your new design.  As supplied,
328 it reads all the process's pages from disk and initializes the page
329 tables for them at the same time.  For testing purposes, you'll
330 probably want to leave the code that reads the pages from disk, but
331 use your new page table management code to construct the page tables
332 only as page faults occur for them.
333
334 You should use the @func{palloc_get_page} function to get the page
335 frames that you use for storing user virtual pages.  Be sure to pass
336 the @code{PAL_USER} flag to this function when you do so, because that
337 allocates pages from a ``user pool'' separate from the ``kernel pool''
338 that other calls to @func{palloc_get_page} make.
339
340 There are many possible ways to implement virtual memory.  The above
341 is simply an outline of our suggested implementation.
342
343 @node Problem 3-2 Paging To and From Disk
344 @section Problem 3-2: Paging To and From Disk
345
346 Implement paging to and from files and the swap disk.  You may use the
347 disk on interface @code{hd1:1} as the swap disk, using the disk
348 interface prototyped in @code{devices/disk.h}.
349
350 You will need routines to move a page from memory to disk and from
351 disk to memory, where ``disk'' is either a file or the swap disk.  If
352 you do everything correctly, your VM should still work when you
353 implement your own file system for the next assignment.
354
355 You will need a way to track pages which are used by a process but
356 which are not in physical memory, to fully handle page faults.  Pages
357 that you write to swap should not be constrained to be in sequential
358 order.  You will also need a way to track all of the physical memory
359 pages, to find an unused one when needed, or to evict a page
360 when memory is needed but no empty pages are available.  The data
361 structures that you designed for problem 3-1 should do most of the work for
362 you.
363
364 You will need a page replacement algorithm.  The hardware sets the
365 accessed and dirty bits when it accesses memory.  You can gain access
366 to this information using the functions prototyped in
367 @file{userprog/pagedir.h}.  You should be able to take advantage of
368 this information to implement some algorithm which attempts to achieve
369 LRU-type behavior.  We expect that your algorithm perform at least as
370 well as a reasonable implementation of the second-chance (clock)
371 algorithm.  You will need to show in your test cases the value of your
372 page replacement algorithm by demonstrating for some workload that it
373 pages less frequently using your algorithm than using some inferior
374 page replacement policy.  The canonical example of a poor page
375 replacement policy is random replacement.
376
377 You must write your code so that we can choose a page replacement policy
378 at compile time.  By default, the LRU-like algorithm must be in effect,
379 but we must be able to choose random replacement by inserting the line
380 @code{#define RANDOM_REPLACEMENT 1} in @file{constants.h}.
381 @xref{Conditional Compilation}, for details.
382
383 Since you will already be paging from disk, you should implement a
384 ``lazy'' loading scheme for new processes.  When a process is created,
385 it will not run immediately.  Therefore, it doesn't make sense to load
386 all its code, data, and stack into memory when the process is created,
387 since it might incur additional disk accesses to do so (if it gets
388 paged out before it runs).  When loading a new process, you should
389 leave most pages on disk, and bring them in as demanded when the
390 program begins running.  Your VM system should also use the executable
391 file itself as backing store for read-only segments, since these
392 segments won't change.
393
394 There are a few special cases.  Look at the loop in
395 @func{load_segment} in @file{userprog/process.c}.  Each time
396 around the loop, @code{read_bytes} represents the number of bytes to
397 read from the executable file and @code{zero_bytes} represents the number
398 of bytes to initialize to zero following the bytes read.  The two
399 always sum to @code{PGSIZE}.  The page handling depends on these
400 variables' values:
401
402 @itemize @bullet
403 @item
404 If @code{read_bytes} equals @code{PGSIZE}, the page should be demand
405 paged from disk on its first access.
406
407 @item 
408 If @code{zero_bytes} equals @code{PGSIZE}, the page does not need to
409 be read from disk at all because it is all zeroes.  You should handle
410 such pages by creating a new page consisting of all zeroes at the
411 first page fault.
412
413 @item
414 If neither @code{read_bytes} nor @code{zero_bytes} equals
415 @code{PGSIZE}, then part of the page is to be read from disk and the
416 remainder zeroed.  This is a special case.  You are allowed to handle
417 it by reading the partial page from disk at executable load time and
418 zeroing the rest of the page.  This is the only case in which we will
419 allow you to load a page in a non-``lazy'' fashion.  Many real OSes
420 such as Linux do not load partial pages lazily.
421 @end itemize
422
423 Incidentally, if you have trouble handling the third case above, you
424 can eliminate it temporarily by linking the test programs with a
425 special ``linker script.''  Read @file{tests/userprog/Makefile} for
426 details.  We will not test your submission with this special linker
427 script, so the code you turn in must properly handle all cases.
428
429 For extra credit, you may implement sharing: when multiple processes
430 are created that use the same executable file, share read-only pages
431 among those processes instead of creating separate copies of read-only
432 segments for each process.  If you carefully designed your data
433 structures in problem 3-1, sharing of read-only pages should not make this
434 part significantly harder.
435
436 @node Problem 3-3 Memory Mapped Files
437 @section Problem 3-3: Memory Mapped Files
438
439 Implement memory mapped files.
440
441 You will need to implement the following system calls:
442
443 @table @code
444 @item SYS_mmap
445 @itemx bool mmap (int @var{fd}, void *@var{addr}, unsigned @var{length})
446
447 Maps the file open as @var{fd} into the process's address space
448 starting at @var{addr} for @var{length} bytes.  Returns true if
449 successful, false on failure.  Failure cases include the following:
450
451 @itemize @bullet
452 @item
453 @var{addr} is not page-aligned.
454
455 @item
456 @var{length} is not positive.
457
458 @item
459 The range of pages mapped overlaps any existing set of mapped pages,
460 including the stack or pages mapped at executable load time.
461 @end itemize
462
463 @var{length} is treated as if it were rounded up to the nearest
464 multiple of the page size, that is, as if the first statement in the
465 system call's implementation were
466 @example
467 length = ROUND_UP (length, PGSIZE);
468 @end example
469 (The @code{ROUND_UP} macro is defined in @file{<round.h>}.)
470 The remainder of this description assumes that this has been done.
471
472 If @var{length} is less than @var{fd}'s length, you should only map
473 the first @var{length} bytes of the file.  If @var{length} is greater
474 than @var{fd}'s length, when the file's length is also rounded up to a
475 page multiple, the call should fail.  Ideally it would extend the
476 file, but our file system does not yet support growing files.
477
478 If @var{length} is greater than @var{fd}'s (unrounded) length, then some
479 bytes in the final mapped page ``stick out'' beyond the end of the
480 file.  Set these bytes to zero when the page is faulted in from
481 disk, and discard them when the page is written back to disk.
482
483 Your VM system should use the @code{mmap}'d file itself as
484 backing store for the mapped segment.  That is, to evict a page mapped by
485 @code{mmap} must be evicted, write it to the file it was mapped from.
486 (In fact, you may choose to implement executable mappings as a special
487 case of file mappings.)
488
489 @item SYS_munmap
490 @itemx bool munmap (void *addr, unsigned length)
491
492 Unmaps @var{length} bytes starting at @var{addr}.  Returns true on
493 success, false on failure.  Failure cases include the following:
494
495 @itemize @bullet
496 @item
497 @var{addr} is not page-aligned.
498
499 @item
500 @var{length} is not positive.
501
502 @item
503 One or more pages within the range to be unmapped were not mapped
504 using the @code{mmap} system call.
505 @end itemize
506
507 As with @code{mmap}, @var{length} is treated as if it were rounded up
508 to the nearest multiple of the page size.
509
510 It is valid to unmap only some of the pages that were mapped in a
511 previous system call.
512 @end table
513
514 All mappings are implicitly unmapped when a process exits, whether via
515 @code{exit} or by any other means.  When a file is unmapped, whether
516 implicitly or explicitly, all outstanding changes are written to the
517 file, and the pages are removed from the process's list of used
518 virtual pages.
519
520 @node Virtual Memory FAQ
521 @section FAQ
522
523 @enumerate 1
524 @item
525 @b{Do we need a working HW 2 to implement HW 3?}
526
527 Yes.
528
529 @item
530 @anchor{Hash Table}
531 @b{How do I use the hash table provided in @file{lib/kernel/hash.c}?}
532
533 First, you need to embed a @code{hash_elem} object as a member of the
534 object that the hash table will contain.  Each @code{hash_elem} allows
535 the object to a member of at most one hash table at a given time.  All
536 the hash table functions that deal with hash table items actually use
537 the address of a @code{hash_elem}.  You can convert a pointer to a
538 @code{hash_elem} member into a pointer to the structure in which
539 member is embedded using the @code{hash_entry} macro.
540
541 Second, you need to decide on a key type.  The key should be something
542 that is unique for each object, because a given hash table may not
543 contain two objects with equal keys.  Then you need to write two
544 functions.  The first is a @dfn{hash function} that converts a key
545 into an integer.  Some sample hash functions that you can use or just
546 examine are given in @file{lib/kernel/hash.c}.  The second function
547 needed is a @dfn{comparison function} that compares a pair and returns
548 true if the first is less than the second.  These two functions have
549 to be compatible with the prototypes for @code{hash_hash_func} and
550 @code{hash_less_func} in @file{lib/kernel/hash.h}.
551
552 Here's a quick example.  Suppose you want to put @struct{thread}s
553 in a hash table.  First, add a @code{hash_elem} to the thread
554 structure by adding a line to its definition:
555
556 @example
557 hash_elem h_elem;               /* Hash table element. */
558 @end example
559
560 We'll choose the @code{tid} member in @struct{thread} as the key,
561 and write a hash function and a comparison function:
562
563 @example
564 /* Returns a hash for E. */
565 unsigned
566 thread_hash (const hash_elem *e, void *aux UNUSED)
567 @{
568   struct thread *t = hash_entry (e, struct thread, h_elem);
569   return hash_int (t->tid);
570 @}
571
572 /* Returns true if A's tid is less than B's tid. */
573 bool
574 thread_less (const hash_elem *a_, const hash_elem *b_, 
575              void *aux UNUSED)
576 @{
577   struct thread *a = hash_entry (a_, struct thread, h_elem);
578   struct thread *b = hash_entry (b_, struct thread, h_elem);
579   return a->tid < b->tid;
580 @}
581 @end example
582
583 Then we can create a hash table like this:
584
585 @example
586 struct hash threads;
587
588 hash_init (&threads, thread_hash, thread_less, NULL);
589 @end example
590
591 Finally, if @code{@var{t}} is a pointer to a @struct{thread},
592 then we can insert it into the hash table with:
593
594 @example
595 hash_insert (&threads, &@var{t}->h_elem);
596 @end example
597
598 If you have any other questions about hash tables, the CS109
599 and CS161 textbooks have good chapters on them, or you can come
600 to any of the TA's office hours for further clarification.
601
602 @item
603 @b{What are the @var{aux} parameters to the hash table functions good
604 for?}
605
606 In simple cases you won't have any need for the @var{aux} parameters.
607 In these cases you can just pass a null pointer to @func{hash_init}
608 for @var{aux} and ignore the values passed to the hash function and
609 comparison functions.  (You'll get a compiler warning if you don't use
610 the @var{aux} parameter, but you can turn that off with the
611 @code{UNUSED} macro, as shown above, or you can just ignore it.)
612
613 @var{aux} is useful when you have some property of the data in the
614 hash table that's both constant and needed for hashing or comparisons,
615 but which is not stored in the data items themselves.  For example, if
616 the items in a hash table contain fixed-length strings, but the items
617 themselves don't indicate what that fixed length is, you could pass
618 the length as an @var{aux} parameter.
619
620 @item
621 @b{The current implementation of the hash table does not do something
622 that we need it to do. What gives?}
623
624 You are welcome to modify it.  It is not used by any of the code we
625 provided, so modifying it won't affect any code but yours.  Do
626 whatever it takes to make it work the way you want.
627
628 @item
629 @b{What controls the layout of user programs?}
630
631 The linker is responsible for the layout of a user program in
632 memory. The linker is directed by a ``linker script'' which tells it
633 the names and locations of the various program segments.  You can
634 learn more about linker scripts by reading the ``Scripts'' chapter in
635 the linker manual, accessible via @samp{info ld}.
636 @end enumerate
637
638 @menu
639 * Problem 3-1 and 3-2 FAQ::    
640 * Problem 3-3 Memory Mapped File FAQ::  
641 @end menu
642
643 @node Problem 3-1 and 3-2 FAQ
644 @subsection Problem 3-1 and 3-2 FAQ
645
646 @enumerate 1
647 @item
648 @b{Does the virtual memory system need to support growth of the data
649 segment?}
650
651 No.  The size of the data segment is determined by the linker.  We
652 still have no dynamic allocation in Pintos (although it is possible to
653 ``fake'' it at the user level by using memory-mapped files).  However,
654 implementing it would add little additional complexity to a
655 well-designed system.
656
657 @item
658 @b{Why do I need to pass @code{PAL_USER} to @func{palloc_get_page}
659 when I allocate physical page frames?}@anchor{Why PAL_USER?}
660
661 You can layer some other allocator on top of @func{palloc_get_page}
662 if you like, but it should be the underlying mechanism, directly or
663 indirectly, for two reasons.  First, running out of pages in the user
664 pool just causes user programs to page, but running out of pages in
665 the kernel pool will cause all kinds of problems, because many kernel
666 functions depend on being able to allocate memory.  Second, you can
667 use the @option{-ul} option to @command{pintos} to limit the size of
668 the user pool, which makes it easy to test your VM implementation with
669 various user memory sizes.
670 @end enumerate
671
672 @node Problem 3-3 Memory Mapped File FAQ
673 @subsection Problem 3-3: Memory Mapped File FAQ
674
675 @enumerate 1
676 @item
677 @b{How do we interact with memory-mapped files?}
678
679 Let's say you want to map a file called @file{foo} into your address
680 space at address @t{0x10000000}. You open the file, determine its
681 length, and then use @code{mmap}:
682
683 @example
684 #include <stdio.h>
685 #include <syscall.h>
686
687 int main (void)
688 @{
689     void *addr = (void *) 0x10000000;
690     int fd = open ("foo");
691     int length = filesize (fd);
692     if (mmap (fd, addr, length))
693         printf ("success!\n");
694 @}
695 @end example
696
697 Suppose @file{foo} is a text file and you want to print the first 64
698 bytes on the screen (assuming, of course, that the length of the file
699 is at least 64).  Without @code{mmap}, you'd need to allocate a
700 buffer, use @code{read} to get the data from the file into the buffer,
701 and finally use @code{write} to put the buffer out to the display. But
702 with the file mapped into your address space, you can directly address
703 it like so:
704
705 @example
706 write (addr, 64, STDOUT_FILENO);
707 @end example
708
709 Similarly, if you wanted to replace the first byte of the file,
710 all you need to do is:
711
712 @example
713 addr[0] = 'b';
714 @end example
715
716 When you're done using the memory-mapped file, you simply unmap
717 it:
718
719 @example
720 munmap (addr, length);
721 @end example
722
723 @item
724 @b{What if two processes memory-map the same file?}
725
726 There is no requirement in Pintos that the two processes see
727 consistent data.  Unix handles this by making the processes share the
728 same physical page, but the @code{mmap} system call also has an
729 argument allowing the client to specify whether the page is shared or
730 private (i.e.@: copy-on-write).
731
732 @item
733 @b{What happens if a user removes a @code{mmap}'d file?}
734
735 You should follow the Unix convention and the mapping should still be
736 valid.  @xref{Removing an Open File}, for more information.
737
738 @item
739 @b{What if a process writes to a page that is memory-mapped, but the
740 location written to in the memory-mapped page is past the end
741 of the memory-mapped file?}
742
743 Can't happen.  @code{mmap} checks that the mapped region is within the
744 file's length and Pintos provides no way to shorten a file.  (Until
745 project 4, there's no way to extend a file either.)  You can remove a
746 file, but the mapping remains valid (see the previous question).
747
748 @item
749 @b{Do we have to handle memory mapping @code{stdin} or @code{stdout}?}
750
751 No.  Memory mapping implies that a file has a length and that a user
752 can seek to any location in the file.  Since the console device has
753 neither of these properties, @code{mmap} should return false when the
754 user attempts to memory map a file descriptor for the console device.
755
756 @item
757 @b{What happens when a process exits with mapped files?}
758
759 When a process finishes, each of its mapped files is implicitly
760 unmapped.  When a process @code{mmap}s a file and then writes into the
761 area for the file it is making the assumption the changes will be
762 written to the file.
763
764 @item
765 @b{If a user closes a mapped file, should it be automatically
766 unmapped?}
767
768 No, once created the mapping is valid until @code{munmap} is called
769 or the process exits.
770 @end enumerate