Skip to content
Snippets Groups Projects
main.c 2.38 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

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
{
  int i;
rsc's avatar
rsc committed
  static volatile int bcpu;  // cannot be on stack
rsc's avatar
rsc committed
  extern char edata[], end[];
rtm's avatar
rtm committed
  // clear BSS
  memset(edata, 0, end - edata);

rsc's avatar
 
rsc committed
  // splhi() every processor during bootstrap.
  for(i=0; i<NCPU; i++)
rsc's avatar
 
rsc committed
    cpus[i].nsplhi = 1;
kaashoek's avatar
kaashoek committed
  mp_init(); // collect info about this machine
rsc's avatar
rsc committed
  // Switch to bootstrap processor's stack
rsc's avatar
 
rsc committed
  asm volatile("movl %0, %%esp" : : "r" (cpus[bcpu].mpstack+MPSTACK-32));
  asm volatile("movl %0, %%ebp" : : "r" (cpus[bcpu].mpstack+MPSTACK));
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
  pic_init();      // interrupt controller
  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
rsc's avatar
rsc committed
  bootothers();    // boot other CPUs
rsc's avatar
 
rsc committed
  if(!ismp)
rsc's avatar
rsc committed
    timer_init(); // uniprocessor timer
rsc's avatar
 
rsc committed
  cprintf("ismp %d\n", ismp);
  cprintf("userinit\n");
rsc's avatar
 
rsc committed
  userinit();      // first user process
rtm's avatar
rtm committed

  // enable interrupts on this processor.
rsc's avatar
 
rsc committed
  spllo();
rtm's avatar
rtm committed

rsc's avatar
 
rsc committed
  cprintf("scheduler\n");
rtm's avatar
rtm committed
}
rtm's avatar
rtm committed

// Additional processors start here.
rsc's avatar
rsc committed
static void
mpmain(void)
{
rsc's avatar
rsc committed
  cprintf("cpu%d: starting\n", cpu());
rsc's avatar
rsc committed
  idtinit();
  lapic_init(cpu());
  setupsegs(0);
  cpuid(0, 0, 0, 0, 0);  // memory barrier
  cpus[cpu()].booted = 1;
rsc's avatar
 
rsc committed
  spllo();
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;

  // 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
    *(void**)(code-4) = c->mpstack + MPSTACK;
    *(void**)(code-8) = mpmain;
    lapic_startap(c->apicid, (uint)code);

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