Reboot when Ctrl+Alt+Del is pressed.
[pintos-anon] / solutions / p3.patch
1 Index: src/Makefile.build
2 diff -u src/Makefile.build~ src/Makefile.build
3 --- src/Makefile.build~
4 +++ src/Makefile.build
5 @@ -53,7 +53,9 @@ userprog_SRC += userprog/gdt.c                # GDT in
6  userprog_SRC += userprog/tss.c         # TSS management.
7  
8  # No virtual memory code yet.
9 -#vm_SRC = vm/file.c                    # Some file.
10 +vm_SRC = vm/page.c
11 +vm_SRC += vm/frame.c
12 +vm_SRC += vm/swap.c
13  
14  # Filesystem code.
15  filesys_SRC  = filesys/filesys.c       # Filesystem core.
16 Index: src/devices/timer.c
17 diff -u src/devices/timer.c~ src/devices/timer.c
18 --- src/devices/timer.c~
19 +++ src/devices/timer.c
20 @@ -23,6 +23,9 @@ static volatile int64_t ticks;
21     Initialized by timer_calibrate(). */
22  static unsigned loops_per_tick;
23  
24 +/* Threads waiting in timer_sleep(). */
25 +static struct list wait_list;
26 +
27  static intr_handler_func timer_interrupt;
28  static bool too_many_loops (unsigned loops);
29  static void busy_wait (int64_t loops);
30 @@ -43,6 +46,8 @@ timer_init (void) 
31    outb (0x40, count >> 8);
32  
33    intr_register_ext (0x20, timer_interrupt, "8254 Timer");
34 +
35 +  list_init (&wait_list);
36  }
37  
38  /* Calibrates loops_per_tick, used to implement brief delays. */
39 @@ -93,16 +93,37 @@
40    return timer_ticks () - then;
41  }
42  
43 +/* Compares two threads based on their wake-up times. */
44 +static bool
45 +compare_threads_by_wakeup_time (const struct list_elem *a_,
46 +                                const struct list_elem *b_,
47 +                                void *aux UNUSED) 
48 +{
49 +  const struct thread *a = list_entry (a_, struct thread, timer_elem);
50 +  const struct thread *b = list_entry (b_, struct thread, timer_elem);
51 +
52 +  return a->wakeup_time < b->wakeup_time;
53 +}
54 +
55  /* Sleeps for approximately TICKS timer ticks.  Interrupts must
56     be turned on. */
57  void
58  timer_sleep (int64_t ticks) 
59  {
60 -  int64_t start = timer_ticks ();
61 +  struct thread *t = thread_current ();
62 +
63 +  /* Schedule our wake-up time. */
64 +  t->wakeup_time = timer_ticks () + ticks;
65  
66 +  /* Atomically insert the current thread into the wait list. */
67    ASSERT (intr_get_level () == INTR_ON);
68 -  while (timer_elapsed (start) < ticks) 
69 -    thread_yield ();
70 +  intr_disable ();
71 +  list_insert_ordered (&wait_list, &t->timer_elem,
72 +                       compare_threads_by_wakeup_time, NULL);
73 +  intr_enable ();
74 +
75 +  /* Wait. */
76 +  sema_down (&t->timer_sema);
77  }
78  
79  /* Sleeps for approximately MS milliseconds.  Interrupts must be
80 @@ -132,6 +158,16 @@ timer_interrupt (struct intr_frame *args
81  {
82    ticks++;
83    thread_tick ();
84 +
85 +  while (!list_empty (&wait_list))
86 +    {
87 +      struct thread *t = list_entry (list_front (&wait_list),
88 +                                     struct thread, timer_elem);
89 +      if (ticks < t->wakeup_time) 
90 +        break;
91 +      sema_up (&t->timer_sema);
92 +      list_pop_front (&wait_list);
93 +    }
94  }
95  
96  /* Returns true if LOOPS iterations waits for more than one timer
97 Index: src/threads/init.c
98 diff -u src/threads/init.c~ src/threads/init.c
99 --- src/threads/init.c~
100 +++ src/threads/init.c
101 @@ -33,6 +33,8 @@
102  #include "filesys/filesys.h"
103  #include "filesys/fsutil.h"
104  #endif
105 +#include "vm/frame.h"
106 +#include "vm/swap.h"
107  
108  /* Amount of physical memory, in 4 kB pages. */
109  size_t ram_pages;
110 @@ -124,6 +126,9 @@ main (void)
111    filesys_init (format_filesys);
112  #endif
113  
114 +  frame_init ();
115 +  swap_init ();
116 +
117    printf ("Boot complete.\n");
118    
119    /* Run actions specified on kernel command line. */
120 Index: src/threads/interrupt.c
121 diff -u src/threads/interrupt.c~ src/threads/interrupt.c
122 --- src/threads/interrupt.c~
123 +++ src/threads/interrupt.c
124 @@ -354,6 +354,8 @@ intr_handler (struct intr_frame *frame) 
125        in_external_intr = true;
126        yield_on_return = false;
127      }
128 +  else 
129 +    thread_current ()->user_esp = frame->esp;
130  
131    /* Invoke the interrupt's handler.
132       If there is no handler, invoke the unexpected interrupt
133 Index: src/threads/thread.c
134 diff -u src/threads/thread.c~ src/threads/thread.c
135 --- src/threads/thread.c~
136 +++ src/threads/thread.c
137 @@ -13,6 +13,7 @@
138  #include "threads/vaddr.h"
139  #ifdef USERPROG
140  #include "userprog/process.h"
141 +#include "userprog/syscall.h"
142  #endif
143  
144  /* Random value for struct thread's `magic' member.
145 @@ -55,7 +56,8 @@ static void kernel_thread (thread_func *
146  static void idle (void *aux UNUSED);
147  static struct thread *running_thread (void);
148  static struct thread *next_thread_to_run (void);
149 -static void init_thread (struct thread *, const char *name, int priority);
150 +static void init_thread (struct thread *, const char *name, int priority,
151 +                         tid_t);
152  static bool is_thread (struct thread *) UNUSED;
153  static void *alloc_frame (struct thread *, size_t size);
154  static void schedule (void);
155 @@ -82,9 +84,8 @@ thread_init (void) 
156  
157    /* Set up a thread structure for the running thread. */
158    initial_thread = running_thread ();
159 -  init_thread (initial_thread, "main", PRI_DEFAULT);
160 +  init_thread (initial_thread, "main", PRI_DEFAULT, 0);
161    initial_thread->status = THREAD_RUNNING;
162 -  initial_thread->tid = allocate_tid ();
163  }
164  
165  /* Starts preemptive thread scheduling by enabling interrupts.
166 @@ -159,8 +160,8 @@ thread_create (const char *name, int pri
167      return TID_ERROR;
168  
169    /* Initialize thread. */
170 -  init_thread (t, name, priority);
171 -  tid = t->tid = allocate_tid ();
172 +  init_thread (t, name, priority, allocate_tid ());
173 +  tid = t->tid;
174  
175    /* Stack frame for kernel_thread(). */
176    kf = alloc_frame (t, sizeof *kf);
177 @@ -253,16 +254,19 @@ thread_tid (void) 
178  void
179  thread_exit (void) 
180  {
181 +  struct thread *t = thread_current ();
182 +
183    ASSERT (!intr_context ());
184  
185 +  syscall_exit ();
186  #ifdef USERPROG
187    process_exit ();
188  #endif
189 -
190 +  
191    /* Just set our status to dying and schedule another process.
192       We will be destroyed during the call to schedule_tail(). */
193    intr_disable ();
194 -  thread_current ()->status = THREAD_DYING;
195 +  t->status = THREAD_DYING;
196    schedule ();
197    NOT_REACHED ();
198  }
199 @@ -406,17 +410,28 @@ is_thread (struct thread *t)
200  /* Does basic initialization of T as a blocked thread named
201     NAME. */
202  static void
203 -init_thread (struct thread *t, const char *name, int priority)
204 +init_thread (struct thread *t, const char *name, int priority, tid_t tid)
205  {
206    ASSERT (t != NULL);
207    ASSERT (PRI_MIN <= priority && priority <= PRI_MAX);
208    ASSERT (name != NULL);
209  
210    memset (t, 0, sizeof *t);
211 +  t->tid = tid;
212    t->status = THREAD_BLOCKED;
213    strlcpy (t->name, name, sizeof t->name);
214    t->stack = (uint8_t *) t + PGSIZE;
215    t->priority = priority;
216 +  t->exit_code = -1;
217 +  t->wait_status = NULL;
218 +  list_init (&t->children);
219 +  sema_init (&t->timer_sema, 0);
220 +  t->pagedir = NULL;
221 +  t->pages = NULL;
222 +  t->bin_file = NULL;
223 +  list_init (&t->fds);
224 +  list_init (&t->mappings);
225 +  t->next_handle = 2;
226    t->magic = THREAD_MAGIC;
227  }
228  
229 Index: src/threads/thread.h
230 diff -u src/threads/thread.h~ src/threads/thread.h
231 --- src/threads/thread.h~
232 +++ src/threads/thread.h
233 @@ -2,8 +2,10 @@
234  #define THREADS_THREAD_H
235  
236  #include <debug.h>
237 +#include <hash.h>
238  #include <list.h>
239  #include <stdint.h>
240 +#include "threads/synch.h"
241  
242  /* States in a thread's life cycle. */
243  enum thread_status
244 @@ -89,18 +91,49 @@ struct thread
245      uint8_t *stack;                     /* Saved stack pointer. */
246      int priority;                       /* Priority. */
247  
248 +    /* Owned by process.c. */
249 +    int exit_code;                      /* Exit code. */
250 +    struct wait_status *wait_status;    /* This process's completion status. */
251 +    struct list children;               /* Completion status of children. */
252 +
253      /* Shared between thread.c and synch.c. */
254      struct list_elem elem;              /* List element. */
255  
256 -#ifdef USERPROG
257 +    /* Alarm clock. */
258 +    int64_t wakeup_time;                /* Time to wake this thread up. */
259 +    struct list_elem timer_elem;        /* Element in timer_wait_list. */
260 +    struct semaphore timer_sema;        /* Semaphore. */
261 +
262      /* Owned by userprog/process.c. */
263      uint32_t *pagedir;                  /* Page directory. */
264 -#endif
265 +    struct hash *pages;                 /* Page table. */
266 +    struct file *bin_file;              /* The binary executable. */
267 +
268 +    /* Owned by syscall.c. */
269 +    struct list fds;                    /* List of file descriptors. */
270 +    struct list mappings;               /* Memory-mapped files. */
271 +    int next_handle;                    /* Next handle value. */
272 +    void *user_esp;                     /* User's stack pointer. */
273  
274      /* Owned by thread.c. */
275      unsigned magic;                     /* Detects stack overflow. */
276    };
277  
278 +/* Tracks the completion of a process.
279 +   Reference held by both the parent, in its `children' list,
280 +   and by the child, in its `wait_status' pointer. */
281 +struct wait_status
282 +  {
283 +    struct list_elem elem;              /* `children' list element. */
284 +    struct lock lock;                   /* Protects ref_cnt. */
285 +    int ref_cnt;                        /* 2=child and parent both alive,
286 +                                           1=either child or parent alive,
287 +                                           0=child and parent both dead. */
288 +    tid_t tid;                          /* Child thread id. */
289 +    int exit_code;                      /* Child exit code, if dead. */
290 +    struct semaphore dead;              /* 1=child alive, 0=child dead. */
291 +  };
292 +
293  /* If false (default), use round-robin scheduler.
294     If true, use multi-level feedback queue scheduler.
295     Controlled by kernel command-line options "-o mlfqs".
296 Index: src/userprog/exception.c
297 diff -u src/userprog/exception.c~ src/userprog/exception.c
298 --- src/userprog/exception.c~
299 +++ src/userprog/exception.c
300 @@ -4,6 +4,7 @@
301  #include "userprog/gdt.h"
302  #include "threads/interrupt.h"
303  #include "threads/thread.h"
304 +#include "vm/page.h"
305  
306  /* Number of page faults processed. */
307  static long long page_fault_cnt;
308 @@ -148,9 +149,14 @@ page_fault (struct intr_frame *f) 
309    write = (f->error_code & PF_W) != 0;
310    user = (f->error_code & PF_U) != 0;
311  
312 -  /* To implement virtual memory, delete the rest of the function
313 -     body, and replace it with code that brings in the page to
314 -     which fault_addr refers. */
315 +  /* Allow the pager to try to handle it. */
316 +  if (user && not_present)
317 +    {
318 +      if (!page_in (fault_addr))
319 +        thread_exit ();
320 +      return;
321 +    }
322 +
323    printf ("Page fault at %p: %s error %s page in %s context.\n",
324            fault_addr,
325            not_present ? "not present" : "rights violation",
326 Index: src/userprog/pagedir.c
327 diff -u src/userprog/pagedir.c~ src/userprog/pagedir.c
328 --- src/userprog/pagedir.c~
329 +++ src/userprog/pagedir.c
330 @@ -35,15 +35,7 @@ pagedir_destroy (uint32_t *pd) 
331    ASSERT (pd != base_page_dir);
332    for (pde = pd; pde < pd + pd_no (PHYS_BASE); pde++)
333      if (*pde & PTE_P) 
334 -      {
335 -        uint32_t *pt = pde_get_pt (*pde);
336 -        uint32_t *pte;
337 -        
338 -        for (pte = pt; pte < pt + PGSIZE / sizeof *pte; pte++)
339 -          if (*pte & PTE_P) 
340 -            palloc_free_page (pte_get_page (*pte));
341 -        palloc_free_page (pt);
342 -      }
343 +      palloc_free_page (pde_get_pt (*pde));
344    palloc_free_page (pd);
345  }
346  
347 Index: src/userprog/process.c
348 diff -u src/userprog/process.c~ src/userprog/process.c
349 --- src/userprog/process.c~
350 +++ src/userprog/process.c
351 @@ -14,12 +14,26 @@
352  #include "threads/flags.h"
353  #include "threads/init.h"
354  #include "threads/interrupt.h"
355 +#include "threads/malloc.h"
356  #include "threads/palloc.h"
357  #include "threads/thread.h"
358  #include "threads/vaddr.h"
359 +#include "vm/page.h"
360 +#include "vm/frame.h"
361  
362  static thread_func start_process NO_RETURN;
363 -static bool load (const char *cmdline, void (**eip) (void), void **esp);
364 +static bool load (const char *cmd_line, void (**eip) (void), void **esp);
365 +
366 +/* Data structure shared between process_execute() in the
367 +   invoking thread and start_process() in the newly invoked
368 +   thread. */
369 +struct exec_info 
370 +  {
371 +    const char *file_name;              /* Program to load. */
372 +    struct semaphore load_done;         /* "Up"ed when loading complete. */
373 +    struct wait_status *wait_status;    /* Child process. */
374 +    bool success;                       /* Program successfully loaded? */
375 +  };
376  
377  /* Starts a new thread running a user program loaded from
378     FILE_NAME.  The new thread may be scheduled (and may even exit)
379 @@ -28,29 +42,37 @@ static bool load (const char *cmdline, v
380  tid_t
381  process_execute (const char *file_name) 
382  {
383 -  char *fn_copy;
384 +  struct exec_info exec;
385 +  char thread_name[16];
386 +  char *save_ptr;
387    tid_t tid;
388  
389 -  /* Make a copy of FILE_NAME.
390 -     Otherwise there's a race between the caller and load(). */
391 -  fn_copy = palloc_get_page (0);
392 -  if (fn_copy == NULL)
393 -    return TID_ERROR;
394 -  strlcpy (fn_copy, file_name, PGSIZE);
395 +  /* Initialize exec_info. */
396 +  exec.file_name = file_name;
397 +  sema_init (&exec.load_done, 0);
398  
399    /* Create a new thread to execute FILE_NAME. */
400 -  tid = thread_create (file_name, PRI_DEFAULT, start_process, fn_copy);
401 -  if (tid == TID_ERROR)
402 -    palloc_free_page (fn_copy); 
403 +  strlcpy (thread_name, file_name, sizeof thread_name);
404 +  strtok_r (thread_name, " ", &save_ptr);
405 +  tid = thread_create (thread_name, PRI_DEFAULT, start_process, &exec);
406 +  if (tid != TID_ERROR)
407 +    {
408 +      sema_down (&exec.load_done);
409 +      if (exec.success)
410 +        list_push_back (&thread_current ()->children, &exec.wait_status->elem);
411 +      else 
412 +        tid = TID_ERROR;
413 +    }
414 +
415    return tid;
416  }
417  
418  /* A thread function that loads a user process and starts it
419     running. */
420  static void
421 -start_process (void *file_name_)
422 +start_process (void *exec_)
423  {
424 -  char *file_name = file_name_;
425 +  struct exec_info *exec = exec_;
426    struct intr_frame if_;
427    bool success;
428  
429 @@ -59,10 +81,28 @@ start_process (void *file_name_)
430    if_.gs = if_.fs = if_.es = if_.ds = if_.ss = SEL_UDSEG;
431    if_.cs = SEL_UCSEG;
432    if_.eflags = FLAG_IF | FLAG_MBS;
433 -  success = load (file_name, &if_.eip, &if_.esp);
434 +  success = load (exec->file_name, &if_.eip, &if_.esp);
435 +
436 +  /* Allocate wait_status. */
437 +  if (success)
438 +    {
439 +      exec->wait_status = thread_current ()->wait_status
440 +        = malloc (sizeof *exec->wait_status);
441 +      success = exec->wait_status != NULL; 
442 +    }
443  
444 -  /* If load failed, quit. */
445 -  palloc_free_page (file_name);
446 +  /* Initialize wait_status. */
447 +  if (success) 
448 +    {
449 +      lock_init (&exec->wait_status->lock);
450 +      exec->wait_status->ref_cnt = 2;
451 +      exec->wait_status->tid = thread_current ()->tid;
452 +      sema_init (&exec->wait_status->dead, 0);
453 +    }
454 +  
455 +  /* Notify parent thread and clean up. */
456 +  exec->success = success;
457 +  sema_up (&exec->load_done);
458    if (!success) 
459      thread_exit ();
460  
461 @@ -76,18 +116,47 @@ start_process (void *file_name_)
462    NOT_REACHED ();
463  }
464  
465 +/* Releases one reference to CS and, if it is now unreferenced,
466 +   frees it. */
467 +static void
468 +release_child (struct wait_status *cs) 
469 +{
470 +  int new_ref_cnt;
471 +  
472 +  lock_acquire (&cs->lock);
473 +  new_ref_cnt = --cs->ref_cnt;
474 +  lock_release (&cs->lock);
475 +
476 +  if (new_ref_cnt == 0)
477 +    free (cs);
478 +}
479 +
480  /* Waits for thread TID to die and returns its exit status.  If
481     it was terminated by the kernel (i.e. killed due to an
482     exception), returns -1.  If TID is invalid or if it was not a
483     child of the calling process, or if process_wait() has already
484     been successfully called for the given TID, returns -1
485 -   immediately, without waiting.
486 -
487 -   This function will be implemented in problem 2-2.  For now, it
488 -   does nothing. */
489 +   immediately, without waiting. */
490  int
491 -process_wait (tid_t child_tid UNUSED) 
492 +process_wait (tid_t child_tid) 
493  {
494 +  struct thread *cur = thread_current ();
495 +  struct list_elem *e;
496 +
497 +  for (e = list_begin (&cur->children); e != list_end (&cur->children);
498 +       e = list_next (e)) 
499 +    {
500 +      struct wait_status *cs = list_entry (e, struct wait_status, elem);
501 +      if (cs->tid == child_tid) 
502 +        {
503 +          int exit_code;
504 +          list_remove (e);
505 +          sema_down (&cs->dead);
506 +          exit_code = cs->exit_code;
507 +          release_child (cs);
508 +          return exit_code;
509 +        }
510 +    }
511    return -1;
512  }
513  
514 @@ -96,8 +165,35 @@ void
515  process_exit (void)
516  {
517    struct thread *cur = thread_current ();
518 +  struct list_elem *e, *next;
519    uint32_t *pd;
520  
521 +  printf ("%s: exit(%d)\n", cur->name, cur->exit_code);
522 +
523 +  /* Notify parent that we're dead. */
524 +  if (cur->wait_status != NULL) 
525 +    {
526 +      struct wait_status *cs = cur->wait_status;
527 +      cs->exit_code = cur->exit_code;
528 +      sema_up (&cs->dead);
529 +      release_child (cs);
530 +    }
531 +
532 +  /* Free entries of children list. */
533 +  for (e = list_begin (&cur->children); e != list_end (&cur->children);
534 +       e = next) 
535 +    {
536 +      struct wait_status *cs = list_entry (e, struct wait_status, elem);
537 +      next = list_remove (e);
538 +      release_child (cs);
539 +    }
540 +
541 +  /* Destroy the page hash table. */
542 +  page_exit ();
543 +  
544 +  /* Close executable (and allow writes). */
545 +  file_close (cur->bin_file);
546 +
547    /* Destroy the current process's page directory and switch back
548       to the kernel-only page directory. */
549    pd = cur->pagedir;
550 @@ -194,7 +290,7 @@ struct Elf32_Phdr
551  #define PF_W 2          /* Writable. */
552  #define PF_R 4          /* Readable. */
553  
554 -static bool setup_stack (void **esp);
555 +static bool setup_stack (const char *cmd_line, void **esp);
556  static bool validate_segment (const struct Elf32_Phdr *, struct file *);
557  static bool load_segment (struct file *file, off_t ofs, uint8_t *upage,
558                            uint32_t read_bytes, uint32_t zero_bytes,
559 @@ -205,13 +301,15 @@ static bool load_segment (struct file *f
560     and its initial stack pointer into *ESP.
561     Returns true if successful, false otherwise. */
562  bool
563 -load (const char *file_name, void (**eip) (void), void **esp) 
564 +load (const char *cmd_line, void (**eip) (void), void **esp) 
565  {
566    struct thread *t = thread_current ();
567 +  char file_name[NAME_MAX + 2];
568    struct Elf32_Ehdr ehdr;
569    struct file *file = NULL;
570    off_t file_ofs;
571    bool success = false;
572 +  char *cp;
573    int i;
574  
575    /* Allocate and activate page directory. */
576 @@ -220,13 +318,28 @@ load (const char *file_name, void (**eip)
577      goto done;
578    process_activate ();
579  
580 +  /* Create page hash table. */
581 +  t->pages = malloc (sizeof *t->pages);
582 +  if (t->pages == NULL)
583 +    goto done;
584 +  hash_init (t->pages, page_hash, page_less, NULL);
585 +
586 +  /* Extract file_name from command line. */
587 +  while (*cmd_line == ' ')
588 +    cmd_line++;
589 +  strlcpy (file_name, cmd_line, sizeof file_name);
590 +  cp = strchr (file_name, ' ');
591 +  if (cp != NULL)
592 +    *cp = '\0';
593 +
594    /* Open executable file. */
595 -  file = filesys_open (file_name);
596 +  t->bin_file = file = filesys_open (file_name);
597    if (file == NULL) 
598      {
599        printf ("load: %s: open failed\n", file_name);
600        goto done; 
601      }
602 +  file_deny_write (t->bin_file);
603  
604    /* Read and verify executable header. */
605    if (file_read (file, &ehdr, sizeof ehdr) != sizeof ehdr
606 @@ -301,7 +414,7 @@ load (const char *file_name, void (**eip)
607      }
608  
609    /* Set up stack. */
610 -  if (!setup_stack (esp))
611 +  if (!setup_stack (cmd_line, esp))
612      goto done;
613  
614    /* Start address. */
615 @@ -311,14 +424,11 @@ load (const char *file_name, void (**eip)
616  
617   done:
618    /* We arrive here whether the load is successful or not. */
619 -  file_close (file);
620    return success;
621  }
622  \f
623  /* load() helpers. */
624  
625 -static bool install_page (void *upage, void *kpage, bool writable);
626 -
627  /* Checks whether PHDR describes a valid, loadable segment in
628     FILE and returns true if so, false otherwise. */
629  static bool
630 @@ -387,79 +497,127 @@ load_segment (struct file *file, off_t o
631    ASSERT (pg_ofs (upage) == 0);
632    ASSERT (ofs % PGSIZE == 0);
633  
634 -  file_seek (file, ofs);
635    while (read_bytes > 0 || zero_bytes > 0) 
636      {
637 -      /* Calculate how to fill this page.
638 -         We will read PAGE_READ_BYTES bytes from FILE
639 -         and zero the final PAGE_ZERO_BYTES bytes. */
640        size_t page_read_bytes = read_bytes < PGSIZE ? read_bytes : PGSIZE;
641        size_t page_zero_bytes = PGSIZE - page_read_bytes;
642 -
643 -      /* Get a page of memory. */
644 -      uint8_t *kpage = palloc_get_page (PAL_USER);
645 -      if (kpage == NULL)
646 +      struct page *p = page_allocate (upage, !writable);
647 +      if (p == NULL)
648          return false;
649 -
650 -      /* Load this page. */
651 -      if (file_read (file, kpage, page_read_bytes) != (int) page_read_bytes)
652 -        {
653 -          palloc_free_page (kpage);
654 -          return false; 
655 -        }
656 -      memset (kpage + page_read_bytes, 0, page_zero_bytes);
657 -
658 -      /* Add the page to the process's address space. */
659 -      if (!install_page (upage, kpage, writable)) 
660 +      if (page_read_bytes > 0) 
661          {
662 -          palloc_free_page (kpage);
663 -          return false; 
664 +          p->file = file;
665 +          p->file_offset = ofs;
666 +          p->file_bytes = page_read_bytes;
667          }
668 -
669 -      /* Advance. */
670        read_bytes -= page_read_bytes;
671        zero_bytes -= page_zero_bytes;
672 +      ofs += page_read_bytes;
673        upage += PGSIZE;
674      }
675    return true;
676  }
677  
678 -/* Create a minimal stack by mapping a zeroed page at the top of
679 -   user virtual memory. */
680 +/* Reverse the order of the ARGC pointers to char in ARGV. */
681 +static void
682 +reverse (int argc, char **argv) 
683 +{
684 +  for (; argc > 1; argc -= 2, argv++) 
685 +    {
686 +      char *tmp = argv[0];
687 +      argv[0] = argv[argc - 1];
688 +      argv[argc - 1] = tmp;
689 +    }
690 +}
691
692 +/* Pushes the SIZE bytes in BUF onto the stack in KPAGE, whose
693 +   page-relative stack pointer is *OFS, and then adjusts *OFS
694 +   appropriately.  The bytes pushed are rounded to a 32-bit
695 +   boundary.
696 +
697 +   If successful, returns a pointer to the newly pushed object.
698 +   On failure, returns a null pointer. */
699 +static void *
700 +push (uint8_t *kpage, size_t *ofs, const void *buf, size_t size) 
701 +{
702 +  size_t padsize = ROUND_UP (size, sizeof (uint32_t));
703 +  if (*ofs < padsize)
704 +    return NULL;
705 +
706 +  *ofs -= padsize;
707 +  memcpy (kpage + *ofs + (padsize - size), buf, size);
708 +  return kpage + *ofs + (padsize - size);
709 +}
710 +
711 +/* Sets up command line arguments in KPAGE, which will be mapped
712 +   to UPAGE in user space.  The command line arguments are taken
713 +   from CMD_LINE, separated by spaces.  Sets *ESP to the initial
714 +   stack pointer for the process. */
715  static bool
716 -setup_stack (void **esp) 
717 +init_cmd_line (uint8_t *kpage, uint8_t *upage, const char *cmd_line,
718 +               void **esp) 
719  {
720 -  uint8_t *kpage;
721 -  bool success = false;
722 +  size_t ofs = PGSIZE;
723 +  char *const null = NULL;
724 +  char *cmd_line_copy;
725 +  char *karg, *saveptr;
726 +  int argc;
727 +  char **argv;
728 +
729 +  /* Push command line string. */
730 +  cmd_line_copy = push (kpage, &ofs, cmd_line, strlen (cmd_line) + 1);
731 +  if (cmd_line_copy == NULL)
732 +    return false;
733 +
734 +  if (push (kpage, &ofs, &null, sizeof null) == NULL)
735 +    return false;
736  
737 -  kpage = palloc_get_page (PAL_USER | PAL_ZERO);
738 -  if (kpage != NULL) 
739 +  /* Parse command line into arguments
740 +     and push them in reverse order. */
741 +  argc = 0;
742 +  for (karg = strtok_r (cmd_line_copy, " ", &saveptr); karg != NULL;
743 +       karg = strtok_r (NULL, " ", &saveptr))
744      {
745 -      success = install_page (((uint8_t *) PHYS_BASE) - PGSIZE, kpage, true);
746 -      if (success)
747 -        *esp = PHYS_BASE;
748 -      else
749 -        palloc_free_page (kpage);
750 +      void *uarg = upage + (karg - (char *) kpage);
751 +      if (push (kpage, &ofs, &uarg, sizeof uarg) == NULL)
752 +        return false;
753 +      argc++;
754      }
755 -  return success;
756 +
757 +  /* Reverse the order of the command line arguments. */
758 +  argv = (char **) (upage + ofs);
759 +  reverse (argc, (char **) (kpage + ofs));
760 +
761 +  /* Push argv, argc, "return address". */
762 +  if (push (kpage, &ofs, &argv, sizeof argv) == NULL
763 +      || push (kpage, &ofs, &argc, sizeof argc) == NULL
764 +      || push (kpage, &ofs, &null, sizeof null) == NULL)
765 +    return false;
766 +
767 +  /* Set initial stack pointer. */
768 +  *esp = upage + ofs;
769 +  return true;
770  }
771  
772 -/* Adds a mapping from user virtual address UPAGE to kernel
773 -   virtual address KPAGE to the page table.
774 -   If WRITABLE is true, the user process may modify the page;
775 -   otherwise, it is read-only.
776 -   UPAGE must not already be mapped.
777 -   KPAGE should probably be a page obtained from the user pool
778 -   with palloc_get_page().
779 -   Returns true on success, false if UPAGE is already mapped or
780 -   if memory allocation fails. */
781 +/* Create a minimal stack for T by mapping a page at the
782 +   top of user virtual memory.  Fills in the page using CMD_LINE
783 +   and sets *ESP to the stack pointer. */
784  static bool
785 -install_page (void *upage, void *kpage, bool writable)
786 +setup_stack (const char *cmd_line, void **esp) 
787  {
788 -  struct thread *t = thread_current ();
789 -
790 -  /* Verify that there's not already a page at that virtual
791 -     address, then map our page there. */
792 -  return (pagedir_get_page (t->pagedir, upage) == NULL
793 -          && pagedir_set_page (t->pagedir, upage, kpage, writable));
794 +  struct page *page = page_allocate (((uint8_t *) PHYS_BASE) - PGSIZE, false);
795 +  if (page != NULL) 
796 +    {
797 +      page->frame = frame_alloc_and_lock (page);
798 +      if (page->frame != NULL)
799 +        {
800 +          bool ok;
801 +          page->read_only = false;
802 +          page->private = false;
803 +          ok = init_cmd_line (page->frame->base, page->addr, cmd_line, esp);
804 +          frame_unlock (page->frame);
805 +          return ok;
806 +        }
807 +    }
808 +  return false;
809  }
810 Index: src/userprog/syscall.c
811 diff -u src/userprog/syscall.c~ src/userprog/syscall.c
812 --- src/userprog/syscall.c~
813 +++ src/userprog/syscall.c
814 @@ -1,20 +1,598 @@
815  #include "userprog/syscall.h"
816  #include <stdio.h>
817 +#include <string.h>
818  #include <syscall-nr.h>
819 +#include "userprog/process.h"
820 +#include "userprog/pagedir.h"
821 +#include "devices/input.h"
822 +#include "filesys/directory.h"
823 +#include "filesys/filesys.h"
824 +#include "filesys/file.h"
825 +#include "threads/init.h"
826  #include "threads/interrupt.h"
827 +#include "threads/malloc.h"
828 +#include "threads/palloc.h"
829  #include "threads/thread.h"
830 -
831 +#include "threads/vaddr.h"
832 +#include "vm/page.h"
833
834
835 +static int sys_halt (void);
836 +static int sys_exit (int status);
837 +static int sys_exec (const char *ufile);
838 +static int sys_wait (tid_t);
839 +static int sys_create (const char *ufile, unsigned initial_size);
840 +static int sys_remove (const char *ufile);
841 +static int sys_open (const char *ufile);
842 +static int sys_filesize (int handle);
843 +static int sys_read (int handle, void *udst_, unsigned size);
844 +static int sys_write (int handle, void *usrc_, unsigned size);
845 +static int sys_seek (int handle, unsigned position);
846 +static int sys_tell (int handle);
847 +static int sys_close (int handle);
848 +static int sys_mmap (int handle, void *addr);
849 +static int sys_munmap (int mapping);
850
851  static void syscall_handler (struct intr_frame *);
852 +static void copy_in (void *, const void *, size_t);
853  
854 +static struct lock fs_lock;
855
856  void
857  syscall_init (void) 
858  {
859    intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall");
860 +  lock_init (&fs_lock);
861  }
862
863 +/* System call handler. */
864 +static void
865 +syscall_handler (struct intr_frame *f) 
866 +{
867 +  typedef int syscall_function (int, int, int);
868 +
869 +  /* A system call. */
870 +  struct syscall 
871 +    {
872 +      size_t arg_cnt;           /* Number of arguments. */
873 +      syscall_function *func;   /* Implementation. */
874 +    };
875 +
876 +  /* Table of system calls. */
877 +  static const struct syscall syscall_table[] =
878 +    {
879 +      {0, (syscall_function *) sys_halt},
880 +      {1, (syscall_function *) sys_exit},
881 +      {1, (syscall_function *) sys_exec},
882 +      {1, (syscall_function *) sys_wait},
883 +      {2, (syscall_function *) sys_create},
884 +      {1, (syscall_function *) sys_remove},
885 +      {1, (syscall_function *) sys_open},
886 +      {1, (syscall_function *) sys_filesize},
887 +      {3, (syscall_function *) sys_read},
888 +      {3, (syscall_function *) sys_write},
889 +      {2, (syscall_function *) sys_seek},
890 +      {1, (syscall_function *) sys_tell},
891 +      {1, (syscall_function *) sys_close},
892 +      {2, (syscall_function *) sys_mmap},
893 +      {1, (syscall_function *) sys_munmap},
894 +    };
895 +
896 +  const struct syscall *sc;
897 +  unsigned call_nr;
898 +  int args[3];
899  
900 +  /* Get the system call. */
901 +  copy_in (&call_nr, f->esp, sizeof call_nr);
902 +  if (call_nr >= sizeof syscall_table / sizeof *syscall_table)
903 +    thread_exit ();
904 +  sc = syscall_table + call_nr;
905 +
906 +  /* Get the system call arguments. */
907 +  ASSERT (sc->arg_cnt <= sizeof args / sizeof *args);
908 +  memset (args, 0, sizeof args);
909 +  copy_in (args, (uint32_t *) f->esp + 1, sizeof *args * sc->arg_cnt);
910 +
911 +  /* Execute the system call,
912 +     and set the return value. */
913 +  f->eax = sc->func (args[0], args[1], args[2]);
914 +}
915
916 +/* Copies SIZE bytes from user address USRC to kernel address
917 +   DST.
918 +   Call thread_exit() if any of the user accesses are invalid. */
919  static void
920 -syscall_handler (struct intr_frame *f UNUSED) 
921 +copy_in (void *dst_, const void *usrc_, size_t size) 
922 +{
923 +  uint8_t *dst = dst_;
924 +  const uint8_t *usrc = usrc_;
925 +
926 +  while (size > 0) 
927 +    {
928 +      size_t chunk_size = PGSIZE - pg_ofs (usrc);
929 +      if (chunk_size > size)
930 +        chunk_size = size;
931 +      
932 +      if (!page_lock (usrc, false))
933 +        thread_exit ();
934 +      memcpy (dst, usrc, chunk_size);
935 +      page_unlock (usrc);
936 +
937 +      dst += chunk_size;
938 +      usrc += chunk_size;
939 +      size -= chunk_size;
940 +    }
941 +}
942
943 +/* Creates a copy of user string US in kernel memory
944 +   and returns it as a page that must be freed with
945 +   palloc_free_page().
946 +   Truncates the string at PGSIZE bytes in size.
947 +   Call thread_exit() if any of the user accesses are invalid. */
948 +static char *
949 +copy_in_string (const char *us) 
950 +{
951 +  char *ks;
952 +  char *upage;
953 +  size_t length;
954
955 +  ks = palloc_get_page (0);
956 +  if (ks == NULL) 
957 +    thread_exit ();
958 +
959 +  length = 0;
960 +  for (;;) 
961 +    {
962 +      upage = pg_round_down (us);
963 +      if (!page_lock (upage, false))
964 +        goto lock_error;
965 +
966 +      for (; us < upage + PGSIZE; us++) 
967 +        {
968 +          ks[length++] = *us;
969 +          if (*us == '\0') 
970 +            {
971 +              page_unlock (upage);
972 +              return ks; 
973 +            }
974 +          else if (length >= PGSIZE) 
975 +            goto too_long_error;
976 +        }
977 +
978 +      page_unlock (upage);
979 +    }
980 +
981 + too_long_error:
982 +  page_unlock (upage);
983 + lock_error:
984 +  palloc_free_page (ks);
985 +  thread_exit ();
986 +}
987
988 +/* Halt system call. */
989 +static int
990 +sys_halt (void)
991 +{
992 +  power_off ();
993 +}
994
995 +/* Exit system call. */
996 +static int
997 +sys_exit (int exit_code) 
998 +{
999 +  thread_current ()->exit_code = exit_code;
1000 +  thread_exit ();
1001 +  NOT_REACHED ();
1002 +}
1003
1004 +/* Exec system call. */
1005 +static int
1006 +sys_exec (const char *ufile) 
1007 +{
1008 +  tid_t tid;
1009 +  char *kfile = copy_in_string (ufile);
1010 +
1011 +  lock_acquire (&fs_lock);
1012 +  tid = process_execute (kfile);
1013 +  lock_release (&fs_lock);
1014
1015 +  palloc_free_page (kfile);
1016
1017 +  return tid;
1018 +}
1019
1020 +/* Wait system call. */
1021 +static int
1022 +sys_wait (tid_t child) 
1023 +{
1024 +  return process_wait (child);
1025 +}
1026
1027 +/* Create system call. */
1028 +static int
1029 +sys_create (const char *ufile, unsigned initial_size) 
1030 +{
1031 +  char *kfile = copy_in_string (ufile);
1032 +  bool ok;
1033 +
1034 +  lock_acquire (&fs_lock);
1035 +  ok = filesys_create (kfile, initial_size);
1036 +  lock_release (&fs_lock);
1037 +
1038 +  palloc_free_page (kfile);
1039
1040 +  return ok;
1041 +}
1042
1043 +/* Remove system call. */
1044 +static int
1045 +sys_remove (const char *ufile) 
1046 +{
1047 +  char *kfile = copy_in_string (ufile);
1048 +  bool ok;
1049 +
1050 +  lock_acquire (&fs_lock);
1051 +  ok = filesys_remove (kfile);
1052 +  lock_release (&fs_lock);
1053 +
1054 +  palloc_free_page (kfile);
1055
1056 +  return ok;
1057 +}
1058 +\f
1059 +/* A file descriptor, for binding a file handle to a file. */
1060 +struct file_descriptor
1061 +  {
1062 +    struct list_elem elem;      /* List element. */
1063 +    struct file *file;          /* File. */
1064 +    int handle;                 /* File handle. */
1065 +  };
1066
1067 +/* Open system call. */
1068 +static int
1069 +sys_open (const char *ufile) 
1070 +{
1071 +  char *kfile = copy_in_string (ufile);
1072 +  struct file_descriptor *fd;
1073 +  int handle = -1;
1074
1075 +  fd = malloc (sizeof *fd);
1076 +  if (fd != NULL)
1077 +    {
1078 +      lock_acquire (&fs_lock);
1079 +      fd->file = filesys_open (kfile);
1080 +      if (fd->file != NULL)
1081 +        {
1082 +          struct thread *cur = thread_current ();
1083 +          handle = fd->handle = cur->next_handle++;
1084 +          list_push_front (&cur->fds, &fd->elem);
1085 +        }
1086 +      else 
1087 +        free (fd);
1088 +      lock_release (&fs_lock);
1089 +    }
1090 +  
1091 +  palloc_free_page (kfile);
1092 +  return handle;
1093 +}
1094
1095 +/* Returns the file descriptor associated with the given handle.
1096 +   Terminates the process if HANDLE is not associated with an
1097 +   open file. */
1098 +static struct file_descriptor *
1099 +lookup_fd (int handle) 
1100 +{
1101 +  struct thread *cur = thread_current ();
1102 +  struct list_elem *e;
1103 +   
1104 +  for (e = list_begin (&cur->fds); e != list_end (&cur->fds);
1105 +       e = list_next (e))
1106 +    {
1107 +      struct file_descriptor *fd;
1108 +      fd = list_entry (e, struct file_descriptor, elem);
1109 +      if (fd->handle == handle)
1110 +        return fd;
1111 +    }
1112
1113 +  thread_exit ();
1114 +}
1115
1116 +/* Filesize system call. */
1117 +static int
1118 +sys_filesize (int handle) 
1119 +{
1120 +  struct file_descriptor *fd = lookup_fd (handle);
1121 +  int size;
1122
1123 +  lock_acquire (&fs_lock);
1124 +  size = file_length (fd->file);
1125 +  lock_release (&fs_lock);
1126
1127 +  return size;
1128 +}
1129
1130 +/* Read system call. */
1131 +static int
1132 +sys_read (int handle, void *udst_, unsigned size) 
1133  {
1134 -  printf ("system call!\n");
1135 +  uint8_t *udst = udst_;
1136 +  struct file_descriptor *fd;
1137 +  int bytes_read = 0;
1138 +
1139 +  fd = lookup_fd (handle);
1140 +  while (size > 0) 
1141 +    {
1142 +      /* How much to read into this page? */
1143 +      size_t page_left = PGSIZE - pg_ofs (udst);
1144 +      size_t read_amt = size < page_left ? size : page_left;
1145 +      off_t retval;
1146 +
1147 +      /* Read from file into page. */
1148 +      if (handle != STDIN_FILENO) 
1149 +        {
1150 +          if (!page_lock (udst, true)) 
1151 +            thread_exit (); 
1152 +          lock_acquire (&fs_lock);
1153 +          retval = file_read (fd->file, udst, read_amt);
1154 +          lock_release (&fs_lock);
1155 +          page_unlock (udst);
1156 +        }
1157 +      else 
1158 +        {
1159 +          size_t i;
1160 +          
1161 +          for (i = 0; i < read_amt; i++) 
1162 +            {
1163 +              char c = input_getc ();
1164 +              if (!page_lock (udst, true)) 
1165 +                thread_exit ();
1166 +              udst[i] = c;
1167 +              page_unlock (udst);
1168 +            }
1169 +          bytes_read = read_amt;
1170 +        }
1171 +      
1172 +      /* Check success. */
1173 +      if (retval < 0)
1174 +        {
1175 +          if (bytes_read == 0)
1176 +            bytes_read = -1; 
1177 +          break;
1178 +        }
1179 +      bytes_read += retval; 
1180 +      if (retval != (off_t) read_amt) 
1181 +        {
1182 +          /* Short read, so we're done. */
1183 +          break; 
1184 +        }
1185 +
1186 +      /* Advance. */
1187 +      udst += retval;
1188 +      size -= retval;
1189 +    }
1190 +   
1191 +  return bytes_read;
1192 +}
1193
1194 +/* Write system call. */
1195 +static int
1196 +sys_write (int handle, void *usrc_, unsigned size) 
1197 +{
1198 +  uint8_t *usrc = usrc_;
1199 +  struct file_descriptor *fd = NULL;
1200 +  int bytes_written = 0;
1201 +
1202 +  /* Lookup up file descriptor. */
1203 +  if (handle != STDOUT_FILENO)
1204 +    fd = lookup_fd (handle);
1205 +
1206 +  while (size > 0) 
1207 +    {
1208 +      /* How much bytes to write to this page? */
1209 +      size_t page_left = PGSIZE - pg_ofs (usrc);
1210 +      size_t write_amt = size < page_left ? size : page_left;
1211 +      off_t retval;
1212 +
1213 +      /* Write from page into file. */
1214 +      if (!page_lock (usrc, false)) 
1215 +        thread_exit ();
1216 +      lock_acquire (&fs_lock);
1217 +      if (handle == STDOUT_FILENO)
1218 +        {
1219 +          putbuf ((char *) usrc, write_amt);
1220 +          retval = write_amt;
1221 +        }
1222 +      else
1223 +        retval = file_write (fd->file, usrc, write_amt);
1224 +      lock_release (&fs_lock);
1225 +      page_unlock (usrc);
1226 +
1227 +      /* Handle return value. */
1228 +      if (retval < 0) 
1229 +        {
1230 +          if (bytes_written == 0)
1231 +            bytes_written = -1;
1232 +          break;
1233 +        }
1234 +      bytes_written += retval;
1235 +
1236 +      /* If it was a short write we're done. */
1237 +      if (retval != (off_t) write_amt)
1238 +        break;
1239 +
1240 +      /* Advance. */
1241 +      usrc += retval;
1242 +      size -= retval;
1243 +    }
1244
1245 +  return bytes_written;
1246 +}
1247
1248 +/* Seek system call. */
1249 +static int
1250 +sys_seek (int handle, unsigned position) 
1251 +{
1252 +  struct file_descriptor *fd = lookup_fd (handle);
1253 +   
1254 +  lock_acquire (&fs_lock);
1255 +  if ((off_t) position >= 0)
1256 +    file_seek (fd->file, position);
1257 +  lock_release (&fs_lock);
1258 +
1259 +  return 0;
1260 +}
1261
1262 +/* Tell system call. */
1263 +static int
1264 +sys_tell (int handle) 
1265 +{
1266 +  struct file_descriptor *fd = lookup_fd (handle);
1267 +  unsigned position;
1268 +   
1269 +  lock_acquire (&fs_lock);
1270 +  position = file_tell (fd->file);
1271 +  lock_release (&fs_lock);
1272 +
1273 +  return position;
1274 +}
1275
1276 +/* Close system call. */
1277 +static int
1278 +sys_close (int handle) 
1279 +{
1280 +  struct file_descriptor *fd = lookup_fd (handle);
1281 +  lock_acquire (&fs_lock);
1282 +  file_close (fd->file);
1283 +  lock_release (&fs_lock);
1284 +  list_remove (&fd->elem);
1285 +  free (fd);
1286 +  return 0;
1287 +}
1288 +\f
1289 +/* Binds a mapping id to a region of memory and a file. */
1290 +struct mapping
1291 +  {
1292 +    struct list_elem elem;      /* List element. */
1293 +    int handle;                 /* Mapping id. */
1294 +    struct file *file;          /* File. */
1295 +    uint8_t *base;              /* Start of memory mapping. */
1296 +    size_t page_cnt;            /* Number of pages mapped. */
1297 +  };
1298 +
1299 +/* Returns the file descriptor associated with the given handle.
1300 +   Terminates the process if HANDLE is not associated with a
1301 +   memory mapping. */
1302 +static struct mapping *
1303 +lookup_mapping (int handle) 
1304 +{
1305 +  struct thread *cur = thread_current ();
1306 +  struct list_elem *e;
1307 +   
1308 +  for (e = list_begin (&cur->mappings); e != list_end (&cur->mappings);
1309 +       e = list_next (e))
1310 +    {
1311 +      struct mapping *m = list_entry (e, struct mapping, elem);
1312 +      if (m->handle == handle)
1313 +        return m;
1314 +    }
1315
1316    thread_exit ();
1317  }
1318 +
1319 +/* Remove mapping M from the virtual address space,
1320 +   writing back any pages that have changed. */
1321 +static void
1322 +unmap (struct mapping *m) 
1323 +{
1324 +  list_remove (&m->elem);
1325 +  while (m->page_cnt-- > 0) 
1326 +    {
1327 +      page_deallocate (m->base);
1328 +      m->base += PGSIZE;
1329 +    }
1330 +  file_close (m->file);
1331 +  free (m);
1332 +}
1333
1334 +/* Mmap system call. */
1335 +static int
1336 +sys_mmap (int handle, void *addr)
1337 +{
1338 +  struct file_descriptor *fd = lookup_fd (handle);
1339 +  struct mapping *m = malloc (sizeof *m);
1340 +  size_t offset;
1341 +  off_t length;
1342 +
1343 +  if (m == NULL || addr == NULL || pg_ofs (addr) != 0)
1344 +    return -1;
1345 +
1346 +  m->handle = thread_current ()->next_handle++;
1347 +  lock_acquire (&fs_lock);
1348 +  m->file = file_reopen (fd->file);
1349 +  lock_release (&fs_lock);
1350 +  if (m->file == NULL) 
1351 +    {
1352 +      free (m);
1353 +      return -1;
1354 +    }
1355 +  m->base = addr;
1356 +  m->page_cnt = 0;
1357 +  list_push_front (&thread_current ()->mappings, &m->elem);
1358 +
1359 +  offset = 0;
1360 +  lock_acquire (&fs_lock);
1361 +  length = file_length (m->file);
1362 +  lock_release (&fs_lock);
1363 +  while (length > 0)
1364 +    {
1365 +      struct page *p = page_allocate ((uint8_t *) addr + offset, false);
1366 +      if (p == NULL)
1367 +        {
1368 +          unmap (m);
1369 +          return -1;
1370 +        }
1371 +      p->private = false;
1372 +      p->file = m->file;
1373 +      p->file_offset = offset;
1374 +      p->file_bytes = length >= PGSIZE ? PGSIZE : length;
1375 +      offset += p->file_bytes;
1376 +      length -= p->file_bytes;
1377 +      m->page_cnt++;
1378 +    }
1379 +  
1380 +  return m->handle;
1381 +}
1382 +
1383 +/* Munmap system call. */
1384 +static int
1385 +sys_munmap (int mapping) 
1386 +{
1387 +  unmap (lookup_mapping (mapping));
1388 +  return 0;
1389 +}
1390 +\f 
1391 +/* On thread exit, close all open files and unmap all mappings. */
1392 +void
1393 +syscall_exit (void) 
1394 +{
1395 +  struct thread *cur = thread_current ();
1396 +  struct list_elem *e, *next;
1397 +   
1398 +  for (e = list_begin (&cur->fds); e != list_end (&cur->fds); e = next)
1399 +    {
1400 +      struct file_descriptor *fd = list_entry (e, struct file_descriptor, elem);
1401 +      next = list_next (e);
1402 +      lock_acquire (&fs_lock);
1403 +      file_close (fd->file);
1404 +      lock_release (&fs_lock);
1405 +      free (fd);
1406 +    }
1407 +   
1408 +  for (e = list_begin (&cur->mappings); e != list_end (&cur->mappings);
1409 +       e = next)
1410 +    {
1411 +      struct mapping *m = list_entry (e, struct mapping, elem);
1412 +      next = list_next (e);
1413 +      unmap (m);
1414 +    }
1415 +}
1416 Index: src/userprog/syscall.h
1417 diff -u src/userprog/syscall.h~ src/userprog/syscall.h
1418 --- src/userprog/syscall.h~
1419 +++ src/userprog/syscall.h
1420 @@ -2,5 +2,6 @@
1421  #define USERPROG_SYSCALL_H
1422  
1423  void syscall_init (void);
1424 +void syscall_exit (void);
1425  
1426  #endif /* userprog/syscall.h */
1427 Index: src/vm/frame.c
1428 diff -u src/vm/frame.c~ src/vm/frame.c
1429 --- src/vm/frame.c~
1430 +++ src/vm/frame.c
1431 @@ -0,0 +1,162 @@
1432 +#include "vm/frame.h"
1433 +#include <stdio.h>
1434 +#include "vm/page.h"
1435 +#include "devices/timer.h"
1436 +#include "threads/init.h"
1437 +#include "threads/malloc.h"
1438 +#include "threads/palloc.h"
1439 +#include "threads/synch.h"
1440 +#include "threads/vaddr.h"
1441 +
1442 +static struct frame *frames;
1443 +static size_t frame_cnt;
1444 +
1445 +static struct lock scan_lock;
1446 +static size_t hand;
1447 +
1448 +/* Initialize the frame manager. */
1449 +void
1450 +frame_init (void) 
1451 +{
1452 +  void *base;
1453 +
1454 +  lock_init (&scan_lock);
1455 +  
1456 +  frames = malloc (sizeof *frames * ram_pages);
1457 +  if (frames == NULL)
1458 +    PANIC ("out of memory allocating page frames");
1459 +
1460 +  while ((base = palloc_get_page (PAL_USER)) != NULL) 
1461 +    {
1462 +      struct frame *f = &frames[frame_cnt++];
1463 +      lock_init (&f->lock);
1464 +      f->base = base;
1465 +      f->page = NULL;
1466 +    }
1467 +}
1468 +
1469 +/* Tries to allocate and lock a frame for PAGE.
1470 +   Returns the frame if successful, false on failure. */
1471 +static struct frame *
1472 +try_frame_alloc_and_lock (struct page *page) 
1473 +{
1474 +  size_t i;
1475 +
1476 +  lock_acquire (&scan_lock);
1477 +
1478 +  /* Find a free frame. */
1479 +  for (i = 0; i < frame_cnt; i++)
1480 +    {
1481 +      struct frame *f = &frames[i];
1482 +      if (!lock_try_acquire (&f->lock))
1483 +        continue;
1484 +      if (f->page == NULL) 
1485 +        {
1486 +          f->page = page;
1487 +          lock_release (&scan_lock);
1488 +          return f;
1489 +        } 
1490 +      lock_release (&f->lock);
1491 +    }
1492 +
1493 +  /* No free frame.  Find a frame to evict. */
1494 +  for (i = 0; i < frame_cnt * 2; i++) 
1495 +    {
1496 +      /* Get a frame. */
1497 +      struct frame *f = &frames[hand];
1498 +      if (++hand >= frame_cnt)
1499 +        hand = 0;
1500 +
1501 +      if (!lock_try_acquire (&f->lock))
1502 +        continue;
1503 +
1504 +      if (f->page == NULL) 
1505 +        {
1506 +          f->page = page;
1507 +          lock_release (&scan_lock);
1508 +          return f;
1509 +        } 
1510 +
1511 +      if (page_accessed_recently (f->page)) 
1512 +        {
1513 +          lock_release (&f->lock);
1514 +          continue;
1515 +        }
1516 +          
1517 +      lock_release (&scan_lock);
1518 +      
1519 +      /* Evict this frame. */
1520 +      if (!page_out (f->page))
1521 +        {
1522 +          lock_release (&f->lock);
1523 +          return NULL;
1524 +        }
1525 +
1526 +      f->page = page;
1527 +      return f;
1528 +    }
1529 +
1530 +  lock_release (&scan_lock);
1531 +  return NULL;
1532 +}
1533 +
1534 +
1535 +/* Tries really hard to allocate and lock a frame for PAGE.
1536 +   Returns the frame if successful, false on failure. */
1537 +struct frame *
1538 +frame_alloc_and_lock (struct page *page) 
1539 +{
1540 +  size_t try;
1541 +
1542 +  for (try = 0; try < 3; try++) 
1543 +    {
1544 +      struct frame *f = try_frame_alloc_and_lock (page);
1545 +      if (f != NULL) 
1546 +        {
1547 +          ASSERT (lock_held_by_current_thread (&f->lock));
1548 +          return f; 
1549 +        }
1550 +      timer_msleep (1000);
1551 +    }
1552 +
1553 +  return NULL;
1554 +}
1555 +
1556 +/* Locks P's frame into memory, if it has one.
1557 +   Upon return, p->frame will not change until P is unlocked. */
1558 +void
1559 +frame_lock (struct page *p) 
1560 +{
1561 +  /* A frame can be asynchronously removed, but never inserted. */
1562 +  struct frame *f = p->frame;
1563 +  if (f != NULL) 
1564 +    {
1565 +      lock_acquire (&f->lock);
1566 +      if (f != p->frame)
1567 +        {
1568 +          lock_release (&f->lock);
1569 +          ASSERT (p->frame == NULL); 
1570 +        } 
1571 +    }
1572 +}
1573 +
1574 +/* Releases frame F for use by another page.
1575 +   F must be locked for use by the current process.
1576 +   Any data in F is lost. */
1577 +void
1578 +frame_free (struct frame *f)
1579 +{
1580 +  ASSERT (lock_held_by_current_thread (&f->lock));
1581 +          
1582 +  f->page = NULL;
1583 +  lock_release (&f->lock);
1584 +}
1585 +
1586 +/* Unlocks frame F, allowing it to be evicted.
1587 +   F must be locked for use by the current process. */
1588 +void
1589 +frame_unlock (struct frame *f) 
1590 +{
1591 +  ASSERT (lock_held_by_current_thread (&f->lock));
1592 +  lock_release (&f->lock);
1593 +}
1594 Index: src/vm/frame.h
1595 diff -u src/vm/frame.h~ src/vm/frame.h
1596 --- src/vm/frame.h~
1597 +++ src/vm/frame.h
1598 @@ -0,0 +1,23 @@
1599 +#ifndef VM_FRAME_H
1600 +#define VM_FRAME_H
1601 +
1602 +#include <stdbool.h>
1603 +#include "threads/synch.h"
1604 +
1605 +/* A physical frame. */
1606 +struct frame 
1607 +  {
1608 +    struct lock lock;           /* Prevent simultaneous access. */
1609 +    void *base;                 /* Kernel virtual base address. */
1610 +    struct page *page;          /* Mapped process page, if any. */
1611 +  };
1612 +
1613 +void frame_init (void);
1614 +
1615 +struct frame *frame_alloc_and_lock (struct page *);
1616 +void frame_lock (struct page *);
1617 +
1618 +void frame_free (struct frame *);
1619 +void frame_unlock (struct frame *);
1620 +
1621 +#endif /* vm/frame.h */
1622 Index: src/vm/page.c
1623 diff -u src/vm/page.c~ src/vm/page.c
1624 --- src/vm/page.c~
1625 +++ src/vm/page.c
1626 @@ -0,0 +1,293 @@
1627 +#include "vm/page.h"
1628 +#include <stdio.h>
1629 +#include <string.h>
1630 +#include "vm/frame.h"
1631 +#include "vm/swap.h"
1632 +#include "filesys/file.h"
1633 +#include "threads/malloc.h"
1634 +#include "threads/thread.h"
1635 +#include "userprog/pagedir.h"
1636 +#include "threads/vaddr.h"
1637 +
1638 +/* Maximum size of process stack, in bytes. */
1639 +#define STACK_MAX (1024 * 1024)
1640 +
1641 +/* Destroys a page, which must be in the current process's
1642 +   page table.  Used as a callback for hash_destroy(). */
1643 +static void
1644 +destroy_page (struct hash_elem *p_, void *aux UNUSED)
1645 +{
1646 +  struct page *p = hash_entry (p_, struct page, hash_elem);
1647 +  frame_lock (p);
1648 +  if (p->frame)
1649 +    frame_free (p->frame);
1650 +  free (p);
1651 +}
1652 +
1653 +/* Destroys the current process's page table. */
1654 +void
1655 +page_exit (void) 
1656 +{
1657 +  struct hash *h = thread_current ()->pages;
1658 +  if (h != NULL)
1659 +    hash_destroy (h, destroy_page);
1660 +}
1661 +
1662 +/* Returns the page containing the given virtual ADDRESS,
1663 +   or a null pointer if no such page exists.
1664 +   Allocates stack pages as necessary. */
1665 +static struct page *
1666 +page_for_addr (const void *address) 
1667 +{
1668 +  if (address < PHYS_BASE) 
1669 +    {
1670 +      struct page p;
1671 +      struct hash_elem *e;
1672 +
1673 +      /* Find existing page. */
1674 +      p.addr = (void *) pg_round_down (address);
1675 +      e = hash_find (thread_current ()->pages, &p.hash_elem);
1676 +      if (e != NULL)
1677 +        return hash_entry (e, struct page, hash_elem);
1678 +
1679 +      /* No page.  Expand stack? */
1680 +      if (address >= PHYS_BASE - STACK_MAX
1681 +          && address >= thread_current ()->user_esp - 32)
1682 +        return page_allocate ((void *) address, false);
1683 +    }
1684 +  return NULL;
1685 +}
1686 +
1687 +/* Locks a frame for page P and pages it in.
1688 +   Returns true if successful, false on failure. */
1689 +static bool
1690 +do_page_in (struct page *p)
1691 +{
1692 +  /* Get a frame for the page. */
1693 +  p->frame = frame_alloc_and_lock (p);
1694 +  if (p->frame == NULL)
1695 +    return false;
1696 +
1697 +  /* Copy data into the frame. */
1698 +  if (p->sector != (disk_sector_t) -1) 
1699 +    {
1700 +      /* Get data from swap. */
1701 +      swap_in (p); 
1702 +    }
1703 +  else if (p->file != NULL) 
1704 +    {
1705 +      /* Get data from file. */
1706 +      off_t read_bytes = file_read_at (p->file, p->frame->base,
1707 +                                        p->file_bytes, p->file_offset);
1708 +      off_t zero_bytes = PGSIZE - read_bytes;
1709 +      memset (p->frame->base + read_bytes, 0, zero_bytes);
1710 +      if (read_bytes != p->file_bytes)
1711 +        printf ("bytes read (%"PROTd") != bytes requested (%"PROTd")\n",
1712 +                read_bytes, p->file_bytes);
1713 +    }
1714 +  else 
1715 +    {
1716 +      /* Provide all-zero page. */
1717 +      memset (p->frame->base, 0, PGSIZE);
1718 +    }
1719 +
1720 +  return true;
1721 +}
1722 +
1723 +/* Faults in the page containing FAULT_ADDR.
1724 +   Returns true if successful, false on failure. */
1725 +bool
1726 +page_in (void *fault_addr) 
1727 +{
1728 +  struct page *p;
1729 +  bool success;
1730 +
1731 +  /* Can't handle page faults without a hash table. */
1732 +  if (thread_current ()->pages == NULL) 
1733 +    return false;
1734 +
1735 +  p = page_for_addr (fault_addr);
1736 +  if (p == NULL) 
1737 +    return false; 
1738 +
1739 +  frame_lock (p);
1740 +  if (p->frame == NULL)
1741 +    {
1742 +      if (!do_page_in (p))
1743 +        return false;
1744 +    }
1745 +  ASSERT (lock_held_by_current_thread (&p->frame->lock));
1746 +    
1747 +  /* Install frame into page table. */
1748 +  success = pagedir_set_page (thread_current ()->pagedir, p->addr,
1749 +                              p->frame->base, !p->read_only);
1750 +
1751 +  /* Release frame. */
1752 +  frame_unlock (p->frame);
1753 +
1754 +  return success;
1755 +}
1756 +
1757 +/* Evicts page P.
1758 +   P must have a locked frame.
1759 +   Return true if successful, false on failure. */
1760 +bool
1761 +page_out (struct page *p) 
1762 +{
1763 +  bool dirty;
1764 +  bool ok;
1765 +
1766 +  ASSERT (p->frame != NULL);
1767 +  ASSERT (lock_held_by_current_thread (&p->frame->lock));
1768 +
1769 +  /* Mark page not present in page table, forcing accesses by the
1770 +     process to fault.  This must happen before checking the
1771 +     dirty bit, to prevent a race with the process dirtying the
1772 +     page. */
1773 +  pagedir_clear_page (p->thread->pagedir, p->addr);
1774 +
1775 +  /* Has the frame been modified? */
1776 +  dirty = pagedir_is_dirty (p->thread->pagedir, p->addr);
1777 +
1778 +  /* Write frame contents to disk if necessary. */
1779 +  if (p->file != NULL) 
1780 +    {
1781 +      if (dirty) 
1782 +        {
1783 +          if (p->private)
1784 +            ok = swap_out (p);
1785 +          else 
1786 +            ok = file_write_at (p->file, p->frame->base, p->file_bytes,
1787 +                                p->file_offset) == p->file_bytes;
1788 +        }
1789 +      else
1790 +        ok = true;
1791 +    }
1792 +  else
1793 +    ok = swap_out (p);
1794 +  if (ok) 
1795 +    {
1796 +      //memset (p->frame->base, 0xcc, PGSIZE);
1797 +      p->frame = NULL; 
1798 +    }
1799 +  return ok;
1800 +}
1801 +
1802 +/* Returns true if page P's data has been accessed recently,
1803 +   false otherwise.
1804 +   P must have a frame locked into memory. */
1805 +bool
1806 +page_accessed_recently (struct page *p) 
1807 +{
1808 +  bool was_accessed;
1809 +
1810 +  ASSERT (p->frame != NULL);
1811 +  ASSERT (lock_held_by_current_thread (&p->frame->lock));
1812 +
1813 +  was_accessed = pagedir_is_accessed (p->thread->pagedir, p->addr);
1814 +  if (was_accessed)
1815 +    pagedir_set_accessed (p->thread->pagedir, p->addr, false);
1816 +  return was_accessed;
1817 +}
1818 +
1819 +/* Adds a mapping for user virtual address VADDR to the page hash
1820 +   table.  Fails if VADDR is already mapped or if memory
1821 +   allocation fails. */
1822 +struct page *
1823 +page_allocate (void *vaddr, bool read_only)
1824 +{
1825 +  struct thread *t = thread_current ();
1826 +  struct page *p = malloc (sizeof *p);
1827 +  if (p != NULL) 
1828 +    {
1829 +      p->addr = pg_round_down (vaddr);
1830 +
1831 +      p->read_only = read_only;
1832 +      p->private = !read_only;
1833 +
1834 +      p->frame = NULL;
1835 +
1836 +      p->sector = (disk_sector_t) -1;
1837 +
1838 +      p->file = NULL;
1839 +      p->file_offset = 0;
1840 +      p->file_bytes = 0;
1841 +
1842 +      p->thread = thread_current ();
1843 +
1844 +      if (hash_insert (t->pages, &p->hash_elem) != NULL) 
1845 +        {
1846 +          /* Already mapped. */
1847 +          free (p);
1848 +          p = NULL;
1849 +        }
1850 +    }
1851 +  return p;
1852 +}
1853 +
1854 +/* Evicts the page containing address VADDR
1855 +   and removes it from the page table. */
1856 +void
1857 +page_deallocate (void *vaddr) 
1858 +{
1859 +  struct page *p = page_for_addr (vaddr);
1860 +  ASSERT (p != NULL);
1861 +  frame_lock (p);
1862 +  if (p->frame)
1863 +    {
1864 +      struct frame *f = p->frame;
1865 +      if (p->file && !p->private) 
1866 +        page_out (p); 
1867 +      frame_free (f);
1868 +    }
1869 +  hash_delete (thread_current ()->pages, &p->hash_elem);
1870 +  free (p);
1871 +}
1872 +
1873 +/* Returns a hash value for the page that E refers to. */
1874 +unsigned
1875 +page_hash (const struct hash_elem *e, void *aux UNUSED) 
1876 +{
1877 +  const struct page *p = hash_entry (e, struct page, hash_elem);
1878 +  return ((uintptr_t) p->addr) >> PGBITS;
1879 +}
1880 +
1881 +/* Returns true if page A precedes page B. */
1882 +bool
1883 +page_less (const struct hash_elem *a_, const struct hash_elem *b_,
1884 +           void *aux UNUSED) 
1885 +{
1886 +  const struct page *a = hash_entry (a_, struct page, hash_elem);
1887 +  const struct page *b = hash_entry (b_, struct page, hash_elem);
1888 +  
1889 +  return a->addr < b->addr;
1890 +}
1891 +
1892 +/* Tries to lock the page containing ADDR into physical memory.
1893 +   If WILL_WRITE is true, the page must be writeable;
1894 +   otherwise it may be read-only.
1895 +   Returns true if successful, false on failure. */
1896 +bool
1897 +page_lock (const void *addr, bool will_write) 
1898 +{
1899 +  struct page *p = page_for_addr (addr);
1900 +  if (p == NULL || (p->read_only && will_write))
1901 +    return false;
1902 +  
1903 +  frame_lock (p);
1904 +  if (p->frame == NULL)
1905 +    return (do_page_in (p)
1906 +            && pagedir_set_page (thread_current ()->pagedir, p->addr,
1907 +                                 p->frame->base, !p->read_only)); 
1908 +  else
1909 +    return true;
1910 +}
1911 +
1912 +/* Unlocks a page locked with page_lock(). */
1913 +void
1914 +page_unlock (const void *addr) 
1915 +{
1916 +  struct page *p = page_for_addr (addr);
1917 +  ASSERT (p != NULL);
1918 +  frame_unlock (p->frame);
1919 +}
1920 Index: src/vm/page.h
1921 diff -u src/vm/page.h~ src/vm/page.h
1922 --- src/vm/page.h~
1923 +++ src/vm/page.h
1924 @@ -0,0 +1,50 @@
1925 +#ifndef VM_PAGE_H
1926 +#define VM_PAGE_H
1927 +
1928 +#include <hash.h>
1929 +#include "devices/disk.h"
1930 +#include "filesys/off_t.h"
1931 +#include "threads/synch.h"
1932 +
1933 +/* Virtual page. */
1934 +struct page 
1935 +  {
1936 +    /* Immutable members. */
1937 +    void *addr;                 /* User virtual address. */
1938 +    bool read_only;             /* Read-only page? */
1939 +    struct thread *thread;      /* Owning thread. */
1940 +
1941 +    /* Accessed only in owning process context. */
1942 +    struct hash_elem hash_elem; /* struct thread `pages' hash element. */
1943 +
1944 +    /* Set only in owning process context with frame->frame_lock held.
1945 +       Cleared only with scan_lock and frame->frame_lock held. */
1946 +    struct frame *frame;        /* Page frame. */
1947 +
1948 +    /* Swap information, protected by frame->frame_lock. */
1949 +    disk_sector_t sector;       /* Starting sector of swap area, or -1. */
1950 +    
1951 +    /* Memory-mapped file information, protected by frame->frame_lock. */
1952 +    bool private;               /* False to write back to file,
1953 +                                   true to write back to swap. */
1954 +    struct file *file;          /* File. */
1955 +    off_t file_offset;          /* Offset in file. */
1956 +    off_t file_bytes;           /* Bytes to read/write, 1...PGSIZE. */
1957 +  };
1958 +
1959 +void page_exit (void);
1960 +
1961 +struct page *page_allocate (void *, bool read_only);
1962 +void page_deallocate (void *vaddr);
1963 +
1964 +bool page_in (void *fault_addr);
1965 +bool page_out (struct page *);
1966 +bool page_accessed_recently (struct page *);
1967 +
1968 +bool page_lock (const void *, bool will_write);
1969 +void page_unlock (const void *);
1970 +
1971 +hash_hash_func page_hash;
1972 +hash_less_func page_less;
1973 +
1974 +#endif /* vm/page.h */
1975 Index: src/vm/swap.c
1976 diff -u src/vm/swap.c~ src/vm/swap.c
1977 --- src/vm/swap.c~
1978 +++ src/vm/swap.c
1979 @@ -0,0 +1,85 @@
1980 +#include "vm/swap.h"
1981 +#include <bitmap.h>
1982 +#include <debug.h>
1983 +#include <stdio.h>
1984 +#include "vm/frame.h"
1985 +#include "vm/page.h"
1986 +#include "devices/disk.h"
1987 +#include "threads/synch.h"
1988 +#include "threads/vaddr.h"
1989 +
1990 +/* The swap disk. */
1991 +static struct disk *swap_disk;
1992 +
1993 +/* Used swap pages. */
1994 +static struct bitmap *swap_bitmap;
1995 +
1996 +/* Protects swap_bitmap. */
1997 +static struct lock swap_lock;
1998 +
1999 +/* Number of sectors per page. */
2000 +#define PAGE_SECTORS (PGSIZE / DISK_SECTOR_SIZE)
2001 +
2002 +/* Sets up swap. */
2003 +void
2004 +swap_init (void) 
2005 +{
2006 +  swap_disk = disk_get (1, 1);
2007 +  if (swap_disk == NULL) 
2008 +    {
2009 +      printf ("no swap disk--swap disabled\n");
2010 +      swap_bitmap = bitmap_create (0);
2011 +    }
2012 +  else
2013 +    swap_bitmap = bitmap_create (disk_size (swap_disk) / PAGE_SECTORS);
2014 +  if (swap_bitmap == NULL)
2015 +    PANIC ("couldn't create swap bitmap");
2016 +  lock_init (&swap_lock);
2017 +}
2018 +
2019 +/* Swaps in page P, which must have a locked frame
2020 +   (and be swapped out). */
2021 +void
2022 +swap_in (struct page *p) 
2023 +{
2024 +  size_t i;
2025 +  
2026 +  ASSERT (p->frame != NULL);
2027 +  ASSERT (lock_held_by_current_thread (&p->frame->lock));
2028 +  ASSERT (p->sector != (disk_sector_t) -1);
2029 +
2030 +  for (i = 0; i < PAGE_SECTORS; i++)
2031 +    disk_read (swap_disk, p->sector + i,
2032 +               p->frame->base + i * DISK_SECTOR_SIZE);
2033 +  bitmap_reset (swap_bitmap, p->sector / PAGE_SECTORS);
2034 +  p->sector = (disk_sector_t) -1;
2035 +}
2036 +
2037 +/* Swaps out page P, which must have a locked frame. */
2038 +bool
2039 +swap_out (struct page *p) 
2040 +{
2041 +  size_t slot;
2042 +  size_t i;
2043 +
2044 +  ASSERT (p->frame != NULL);
2045 +  ASSERT (lock_held_by_current_thread (&p->frame->lock));
2046 +
2047 +  lock_acquire (&swap_lock);
2048 +  slot = bitmap_scan_and_flip (swap_bitmap, 0, 1, false);
2049 +  lock_release (&swap_lock);
2050 +  if (slot == BITMAP_ERROR) 
2051 +    return false; 
2052 +
2053 +  p->sector = slot * PAGE_SECTORS;
2054 +  for (i = 0; i < PAGE_SECTORS; i++)
2055 +    disk_write (swap_disk, p->sector + i,
2056 +                p->frame->base + i * DISK_SECTOR_SIZE);
2057 +  
2058 +  p->private = false;
2059 +  p->file = NULL;
2060 +  p->file_offset = 0;
2061 +  p->file_bytes = 0;
2062 +
2063 +  return true;
2064 +}
2065 Index: src/vm/swap.h
2066 diff -u src/vm/swap.h~ src/vm/swap.h
2067 --- src/vm/swap.h~
2068 +++ src/vm/swap.h
2069 @@ -0,0 +1,11 @@
2070 +#ifndef VM_SWAP_H
2071 +#define VM_SWAP_H 1
2072 +
2073 +#include <stdbool.h>
2074 +
2075 +struct page;
2076 +void swap_init (void);
2077 +void swap_in (struct page *);
2078 +bool swap_out (struct page *);
2079 +
2080 +#endif /* vm/swap.h */