]> Devi Nivas Git - cs3210-lab0.git/commitdiff
use larger, allocated cpu stacks
authorrsc <rsc>
Thu, 27 Sep 2007 19:32:43 +0000 (19:32 +0000)
committerrsc <rsc>
Thu, 27 Sep 2007 19:32:43 +0000 (19:32 +0000)
main.c
proc.h

diff --git a/main.c b/main.c
index 9e33708e31dbb4dcba7ae65fcc1424a233276de5..4176e6cc5a842468e01d1e274785b2c5584f59ba 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,13 +6,13 @@
 #include "x86.h"
 
 static void bootothers(void);
+static void mpmain(void) __attribute__((noreturn));
 
 // Bootstrap processor starts running C code here.
 int
 main(void)
 {
-  int i;
-  static volatile int bcpu;  // cannot be on stack
+  int bcpu, i;
   extern char edata[], end[];
 
   // clear BSS
@@ -20,15 +20,10 @@ main(void)
 
   // splhi() every processor during bootstrap.
   for(i=0; i<NCPU; i++)
-    cpus[i].nsplhi = 1;
+    cpus[i].nsplhi = 1;  // no interrupts during bootstrap
 
   mp_init(); // collect info about this machine
   bcpu = mp_bcpu();
-
-  // Switch to bootstrap processor's stack
-  asm volatile("movl %0, %%esp" : : "r" (cpus[bcpu].mpstack+MPSTACK-32));
-  asm volatile("movl %0, %%ebp" : : "r" (cpus[bcpu].mpstack+MPSTACK));
-
   lapic_init(bcpu);
   cprintf("\ncpu%d: starting xv6\n\n", cpu());
 
@@ -38,34 +33,34 @@ main(void)
   ioapic_init();   // another interrupt controller
   kinit();         // physical memory allocator
   tvinit();        // trap vectors
-  idtinit();       // interrupt descriptor table
   fileinit();      // file table
   iinit();         // inode cache
-  setupsegs(0);    // segments & TSS
   console_init();  // I/O devices & their interrupts
   ide_init();      // disk
-  bootothers();    // boot other CPUs
   if(!ismp)
     timer_init(); // uniprocessor timer
-  cprintf("ismp %d\n", ismp);
-  cprintf("userinit\n");
   userinit();      // first user process
 
-  // enable interrupts on this processor.
-  spllo();
+  // Allocate scheduler stacks and boot the other CPUs.
+  for(i=0; i<ncpu; i++)
+    cpus[i].stack = kalloc(KSTACKSIZE);
+  bootothers();
 
-  cprintf("scheduler\n");
-  scheduler();
+  // Switch to our scheduler stack and continue with mpmain.
+  asm volatile("movl %0, %%esp" : : "r" (cpus[bcpu].stack+KSTACKSIZE));
+  mpmain();
 }
 
 // Additional processors start here.
 static void
 mpmain(void)
 {
-  cprintf("cpu%d: starting\n", cpu());
+  cprintf("cpu%d: mpmain\n", cpu());
   idtinit();
-  lapic_init(cpu());
+  if(cpu() != mp_bcpu())
+    lapic_init(cpu());
   setupsegs(0);
+  asm volatile("movl %0, %%ss" :: "r" (SEG_CPUSTACK << 3));
   cpuid(0, 0, 0, 0, 0);  // memory barrier
   cpus[cpu()].booted = 1;
   spllo();
@@ -89,7 +84,7 @@ bootothers(void)
       continue;
 
     // Fill in %esp, %eip and start code on cpu.
-    *(void**)(code-4) = c->mpstack + MPSTACK;
+    *(void**)(code-4) = c->stack + KSTACKSIZE;
     *(void**)(code-8) = mpmain;
     lapic_startap(c->apicid, (uint)code);
 
diff --git a/proc.h b/proc.h
index ad37271fdaff41a0b1de16cd91a79f8007529448..57fb4d96711b265481e8d27ca33f6368424773d0 100644 (file)
--- a/proc.h
+++ b/proc.h
@@ -49,8 +49,6 @@ struct proc {
 //   fixed-size stack
 //   expandable heap
 
-#define MPSTACK 512
-
 // Per-CPU state
 struct cpu {
   uchar apicid;               // Local APIC ID
@@ -58,7 +56,7 @@ struct cpu {
   struct context context;     // Switch here to enter scheduler
   struct taskstate ts;        // Used by x86 to find stack for interrupt
   struct segdesc gdt[NSEGS];  // x86 global descriptor table
-  char mpstack[MPSTACK];      // Per-CPU startup stack
+  char *stack;
   volatile int booted;        // Has the CPU started?
   int nsplhi;                 // Depth of splhi nesting.
 };