Selaa lähdekoodia

trying to add vm into the memory init so we never do physical allocation. not working rn though, can't run more than a couple tests sequentially without a tlb miss (Accessing NULL)

tarfeef101 6 vuotta sitten
vanhempi
commit
c6987a2215

+ 13 - 2
kern/arch/mips/include/vm.h

@@ -53,7 +53,7 @@
 #define MIPS_KSEG1  0xa0000000
 #define MIPS_KSEG2  0xc0000000
 
-/* 
+/*
  * The first 512 megs of physical space can be addressed in both kseg0 and
  * kseg1. We use kseg0 for the kernel. This macro returns the kernel virtual
  * address of a given physical address within that range. (We assume we're
@@ -61,7 +61,7 @@
  *
  * N.B. If you, say, call a function that returns a paddr or 0 on error,
  * check the paddr for being 0 *before* you use this macro. While paddr 0
- * is not legal for memory allocation or memory management (it holds 
+ * is not legal for memory allocation or memory management (it holds
  * exception handler code) when converted to a vaddr it's *not* NULL, *is*
  * a valid address, and will make a *huge* mess if you scribble on it.
  */
@@ -102,6 +102,17 @@ void ram_bootstrap(void);
 paddr_t ram_stealmem(unsigned long npages);
 void ram_getsize(paddr_t *lo, paddr_t *hi);
 
+// VM stuffs
+struct coremap
+{
+  paddr_t start;
+  int framecount;
+  int next;
+  int * num;
+};
+
+struct coremap map;
+
 /*
  * TLB shootdown bits.
  *

+ 15 - 33
kern/arch/mips/vm/dumbvm.c

@@ -51,18 +51,12 @@
  */
 static struct spinlock stealmem_lock = SPINLOCK_INITIALIZER;
 
-typedef struct coremap
-{
-  paddr_t start;
-  int framecount;
-  int next;
-  int * num;
-} coremap;
+typedef struct coremap coremap;
 
-static coremap map;
-static bool init = false;
+//static coremap map;
+//static bool init = false;
 
-void vm_bootstrap(void)
+/*void vm_bootstrap(void)
 {
   paddr_t min;
   paddr_t max;
@@ -88,22 +82,17 @@ void vm_bootstrap(void)
   }
   
   init = true;
-}
+}*/
 
-// get physical pages
-static paddr_t getppages(int n)
+// init in ram.c now
+void vm_bootstrap(void)
 {
-	paddr_t addr;
-	spinlock_acquire(&stealmem_lock);
-	addr = ram_stealmem(n);
-	spinlock_release(&stealmem_lock);
-	return addr;
+  return;
 }
 
 static paddr_t getvpages(int n)
 {
   bool first = true;
-	spinlock_acquire(&stealmem_lock);
 
 	for (int i = map.next; i < map.framecount; ++i)
 	{
@@ -124,7 +113,7 @@ static paddr_t getvpages(int n)
 				// don't have enough contiguous space
 				goto skip1;
 			}
-			
+			kprintf("found space\n");
 			// we have enough frames
 			for (int j = 0; j < n; ++j)
 			{
@@ -133,7 +122,7 @@ static paddr_t getvpages(int n)
 		
 			if ((map.next + n) < map.framecount) map.next += n;
 			else map.next = 0;
-			spinlock_release(&stealmem_lock);
+			kprintf("alloced %d frames\n", n);
 			return map.start + PAGE_SIZE * i;
 		}
 		
@@ -162,7 +151,6 @@ static paddr_t getvpages(int n)
       // we came back to the start
       if (temp == map.next && !first)
       {
-        spinlock_release(&stealmem_lock);
         return 0;
       }
       ++temp;
@@ -171,7 +159,6 @@ static paddr_t getvpages(int n)
     // if we came back to the start
     if ((i == map.next || temp == map.next) && !first)
     {
-      spinlock_release(&stealmem_lock);
       return 0;
     }
     
@@ -180,28 +167,22 @@ static paddr_t getvpages(int n)
     first = false;
 	}
 	
-	spinlock_release(&stealmem_lock);
 	return 0;
 }
 
 /* Allocate/free some kernel-space virtual pages */
 vaddr_t alloc_kpages(int npages)
 {
-	paddr_t pa;
-	if (init) pa = getvpages(npages);
-	else pa = getppages(npages);
+  spinlock_acquire(&stealmem_lock);
+	kprintf("call made\n");
+	paddr_t pa = getvpages(npages);
+	spinlock_release(&stealmem_lock);
 	if (!(pa)) return 0;
 	return PADDR_TO_KVADDR(pa);
 }
 
 void free_kpages(vaddr_t addr)
 {
-  if (!(init))
-  {
-    (void)addr;
-    return;
-  }
-
   spinlock_acquire(&stealmem_lock);
 	paddr_t a = addr - MIPS_KSEG0;
   int start = (a - map.start) / PAGE_SIZE;
@@ -215,6 +196,7 @@ void free_kpages(vaddr_t addr)
     ++start;
     ++lockstep;
   }
+  kprintf("freed %d frames\n", lockstep);
   spinlock_release(&stealmem_lock);
 }
 

+ 37 - 5
kern/arch/mips/vm/ram.c

@@ -38,6 +38,35 @@ vaddr_t firstfree;   /* first free virtual address; set by start.S */
 static paddr_t firstpaddr;  /* address of first free physical page */
 static paddr_t lastpaddr;   /* one past end of last free physical page */
 
+// vm stuffs
+extern struct coremap map;
+
+static void vmem_bootstrap(void)
+{
+  paddr_t min;
+  paddr_t max;
+  // size of ram available
+  ram_getsize(&min, &max);
+  // # of frames if we start here
+  map.framecount = (max - min) / PAGE_SIZE;
+  // start of the coremap array
+  map.num = (int *) PADDR_TO_KVADDR(min);
+  // min should start at a page start address (roundup), and be after the space for the array
+  min += ROUNDUP(map.framecount * sizeof(int), PAGE_SIZE);
+  // framecount needs to reflect the frames we took for the array of frames
+  map.framecount = (max - min) / PAGE_SIZE;
+  // set the start of actual virtual memory
+  map.start = min;
+  // set next frame to the start
+  map.next = 0;
+  
+  // set all frames to empty
+  for (int i = 0; i < map.framecount; ++i)
+  {
+    map.num[i] = -1;
+  }
+}
+
 /*
  * Called very early in system boot to figure out how much physical
  * RAM is available.
@@ -54,7 +83,7 @@ ram_bootstrap(void)
 	 * This is the same as the last physical address, as long as
 	 * we have less than 508 megabytes of memory. If we had more,
 	 * various annoying properties of the MIPS architecture would
-	 * force the RAM to be discontiguous. This is not a case we 
+	 * force the RAM to be discontiguous. This is not a case we
 	 * are going to worry about.
 	 */
 	if (ramsize > 508*1024*1024) {
@@ -63,14 +92,17 @@ ram_bootstrap(void)
 
 	lastpaddr = ramsize;
 
-	/* 
+	/*
 	 * Get first free virtual address from where start.S saved it.
 	 * Convert to physical address.
 	 */
 	firstpaddr = firstfree - MIPS_KSEG0;
 
-	kprintf("%uk physical memory available\n", 
+	kprintf("%uk physical memory available\n",
 		(lastpaddr-firstpaddr)/1024);
+		
+  // initialize VM
+  vmem_bootstrap();
 }
 
 /*
@@ -88,7 +120,7 @@ ram_bootstrap(void)
  * it's not a legal *allocatable* physical address, because it's the
  * page with the exception handlers on it.
  *
- * This function should not be called once the VM system is initialized, 
+ * This function should not be called once the VM system is initialized,
  * so it is not synchronized.
  */
 paddr_t
@@ -114,7 +146,7 @@ ram_stealmem(unsigned long npages)
  * initializes in order to find out what memory it has available to
  * manage.
  *
- * This function should not be called once the VM system is initialized, 
+ * This function should not be called once the VM system is initialized,
  * so it is not synchronized.
  */
 void

BIN
kern/compile/ASST3/kernel


+ 1 - 1
kern/compile/ASST3/vers.c

@@ -1,3 +1,3 @@
 /* This file is automatically generated. Edits will be lost.*/
-const int buildversion = 28;
+const int buildversion = 40;
 const char buildconfig[] = "ASST3";

+ 1 - 1
kern/compile/ASST3/version

@@ -1 +1 @@
-28
+40