Skip to content
Snippets Groups Projects
main.c 2.04 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
__thread struct cpu *c;
__thread struct proc *cp;

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
  ksegment();
  picinit();       // interrupt controller
  ioapicinit();    // another interrupt controller
  consoleinit();   // I/O devices & their interrupts
  uartinit();      // serial port
rsc's avatar
rsc committed
  cprintf("\ncpu%d: starting xv6\n\n", cpu());
rtm's avatar
rtm committed

rsc's avatar
 
rsc committed
  kinit();         // physical memory allocator
rsc's avatar
 
rsc committed
  pinit();         // process table
rsc's avatar
 
rsc committed
  tvinit();        // trap vectors
rsc's avatar
 
rsc committed
  binit();         // buffer cache
rsc's avatar
 
rsc committed
  fileinit();      // file table
  iinit();         // inode cache
rsc's avatar
 
rsc committed
  ideinit();       // disk
rsc's avatar
 
rsc committed
  if(!ismp)
rsc's avatar
 
rsc committed
    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)
{
  if(cpu() != mpbcpu())
    lapicinit(cpu());
rsc's avatar
 
rsc committed
  ksegment();
  cprintf("cpu%d: mpmain\n", cpu());
  idtinit();
  xchg(&c->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)
      ;
  }
}