Make thread_unblock() on a thread that isn't blocked an error.
[pintos-anon] / src / threads / paging.c
index d54fef8e021db91d046f69bb74e2443b11a22bf9..c0d89adf24bfc45b613e89effba9e63c49f64f42 100644 (file)
@@ -1,10 +1,10 @@
-#include "paging.h"
+#include "threads/paging.h"
 #include <stdbool.h>
 #include <stddef.h>
-#include "init.h"
-#include "lib.h"
-#include "mmu.h"
-#include "palloc.h"
+#include <string.h>
+#include "threads/init.h"
+#include "threads/mmu.h"
+#include "threads/palloc.h"
 
 static uint32_t *base_page_dir;
 
@@ -43,9 +43,9 @@ pte_get_page (uint32_t pte)
    new page directory.
 
    At the time this function is called, the active page table
-   only maps the first 4 MB of RAM, so it should not try to use
-   extravagant amounts of memory.  Fortunately, there is no need
-   to do so. */
+   (set up by loader.S) only maps the first 4 MB of RAM, so we
+   should not try to use extravagant amounts of memory.
+   Fortunately, there is no need to do so. */
 void
 paging_init (void)
 {
@@ -85,10 +85,26 @@ void
 pagedir_destroy (uint32_t *pd) 
 {
   void *kpage, *upage;
+  unsigned pde_idx;
 
+  /* Destroy user pages. */
   for (kpage = pagedir_first (pd, &upage); kpage != NULL;
        kpage = pagedir_next (pd, &upage)) 
     palloc_free (kpage);
+
+  /* Destroy page table pages. */
+  for (pde_idx = 0; pde_idx < pd_no (PHYS_BASE); pde_idx++) 
+    {
+      uint32_t pde = pd[pde_idx];
+
+      if (pde != 0) 
+        {
+          uint32_t *pt = pde_get_pt (pde);
+          palloc_free (pt);
+        }
+    }
+
+  /* Destroy page directory. */
   palloc_free (pd);
 }
 
@@ -220,5 +236,7 @@ pagedir_next (uint32_t *pd, void **upage)
 void
 pagedir_activate (uint32_t *pd) 
 {
+  if (pd == NULL)
+    pd = base_page_dir;
   asm volatile ("movl %0,%%cr3" :: "r" (vtop (pd)));
 }