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