Skip to content
Snippets Groups Projects
main.c 1.95 KiB
Newer Older
rtm's avatar
rtm committed
#include "types.h"
rsc's avatar
rsc committed
#include "defs.h"
rtm's avatar
rtm committed
#include "param.h"
#include "mmu.h"
#include "proc.h"
#include "x86.h"

rsc's avatar
rsc committed
static void bootothers(void);
rsc's avatar
rsc committed
static void mpmain(void) __attribute__((noreturn));
rsc's avatar
rsc committed

kaashoek's avatar
kaashoek committed
// Bootstrap processor starts running C code here.
rsc's avatar
rsc committed
int
main(void)
rtm's avatar
rtm committed
{
  mpinit(); // collect info about this machine
  lapicinit(mpbcpu());
rsc's avatar
rsc committed
  cprintf("\ncpu%d: starting xv6\n\n", cpu());
rtm's avatar
rtm committed

rsc's avatar
 
rsc committed
  pinit();         // process table
  binit();         // buffer cache
  picinit();      // interrupt controller
  ioapicinit();   // another interrupt controller
rsc's avatar
 
rsc committed
  kinit();         // physical memory allocator
  tvinit();        // trap vectors
  fileinit();      // file table
  iinit();         // inode cache
  consoleinit();  // I/O devices & their interrupts
  ideinit();      // disk
rsc's avatar
 
rsc committed
  if(!ismp)
    timerinit();  // uniprocessor timer
rsc's avatar
 
rsc committed
  userinit();      // first user process
rsc's avatar
 
rsc committed
  bootothers();    // start other processors
rtm's avatar
rtm committed

rsc's avatar
 
rsc committed
  // Finish setting up this processor in mpmain.
rsc's avatar
rsc committed
  mpmain();
rtm's avatar
rtm committed
}
rtm's avatar
rtm committed

rsc's avatar
 
rsc committed
// Bootstrap processor gets here after setting up the hardware.
// Additional processors start here.
rsc's avatar
rsc committed
static void
mpmain(void)
{
rsc's avatar
rsc committed
  cprintf("cpu%d: mpmain\n", cpu());
rsc's avatar
rsc committed
  idtinit();
  if(cpu() != mpbcpu())
    lapicinit(cpu());
  setupsegs(0);
rsc's avatar
 
rsc committed
  xchg(&cpus[cpu()].booted, 1);
  cprintf("cpu%d: scheduling\n", cpu());
  scheduler();
}

rsc's avatar
rsc committed
static void
rsc's avatar
rsc committed
bootothers(void)
{
  extern uchar _binary_bootother_start[], _binary_bootother_size[];
  uchar *code;
  struct cpu *c;
rsc's avatar
 
rsc committed
  char *stack;
rsc's avatar
rsc committed

  // Write bootstrap code to unused memory at 0x7000.
  code = (uchar*)0x7000;
  memmove(code, _binary_bootother_start, (uint)_binary_bootother_size);

  for(c = cpus; c < cpus+ncpu; c++){
    if(c == cpus+cpu())  // We've started already.
      continue;

rsc's avatar
rsc committed
    // Fill in %esp, %eip and start code on cpu.
rsc's avatar
 
rsc committed
    stack = kalloc(KSTACKSIZE);
    *(void**)(code-4) = stack + KSTACKSIZE;
rsc's avatar
rsc committed
    *(void**)(code-8) = mpmain;
    lapicstartap(c->apicid, (uint)code);
rsc's avatar
rsc committed

    // Wait for cpu to get through bootstrap.
    while(c->booted == 0)
      ;
  }
}