Skip to content
Snippets Groups Projects
main.c 2.76 KiB
Newer Older
rtm's avatar
rtm committed
#include "types.h"
#include "param.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
#include "x86.h"
#include "traps.h"
#include "syscall.h"
rtm's avatar
rtm committed
#include "elf.h"
#include "param.h"
#include "spinlock.h"
rtm's avatar
rtm committed

rtm's avatar
rtm committed
extern char edata[], end[];
rtm's avatar
rtm committed

rsc's avatar
 
rsc committed
void proc0init();
kaashoek's avatar
kaashoek committed
// Bootstrap processor starts running C code here.
rsc's avatar
rsc committed
// This is called main0 not main so that it can have
// a void return type.  Gcc can't handle functions named
// main that don't return int.  Really.
void
main0(void)
rtm's avatar
rtm committed
{
  int i;
rsc's avatar
rsc committed
  static int bcpu;  // cannot be on stack
rtm's avatar
rtm committed
  // clear BSS
  memset(edata, 0, end - edata);

  // Prevent release() from enabling interrupts.
  for(i=0; i<NCPU; i++)
    cpus[i].nlock = 1;
kaashoek's avatar
kaashoek committed
  mp_init(); // collect info about this machine
  bcpu = mp_bcpu();

  // 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));
  cprintf("\ncpu%d: starting xv6\n\n", cpu());
rtm's avatar
rtm committed

  pinit(); // process table
  binit(); // buffer cache
  pic_init();
rtm's avatar
rtm committed
  kinit(); // physical memory allocator
rtm's avatar
rtm committed
  tvinit(); // trap vectors
  idtinit(); // this CPU's interrupt descriptor table
rsc's avatar
rsc committed
  fileinit();
  iinit(); // i-node table
  // make sure there's a TSS
  setupsegs(0);
rtm's avatar
rtm committed

  // initialize I/O devices, let them enable interrupts
kaashoek's avatar
kaashoek committed
  console_init();
rsc's avatar
rsc committed
  ide_init();
  // start other CPUs
  mp_startthem();
rtm's avatar
rtm committed

rsc's avatar
rsc committed
  if(ismp)
    lapic_timerinit();
  else
    pit8253_timerinit();

  // enable interrupts on the local APIC
kaashoek's avatar
kaashoek committed
  lapic_enableintr();
rtm's avatar
rtm committed

  // enable interrupts on this processor.
  cpus[cpu()].nlock--;
rtm's avatar
rtm committed

rsc's avatar
 
rsc committed
  // initialize process 0
  proc0init();

rtm's avatar
rtm committed
}
rtm's avatar
rtm committed

// Additional processors start here.
rsc's avatar
rsc committed
void
mpmain(void)
{
  cprintf("cpu%d: starting\n", cpu());
  idtinit(); // CPU's idt
rtm's avatar
rtm committed
  if(cpu() == 0)
    panic("mpmain on cpu 0");
  lapic_init(cpu());
  lapic_timerinit();
  lapic_enableintr();

  // make sure there's a TSS
  setupsegs(0);
  cpuid(0, 0, 0, 0, 0);  // memory barrier
  cpus[cpu()].booted = 1;

  // Enable interrupts on this processor.
  cpus[cpu()].nlock--;
  sti();

  scheduler();
}

rsc's avatar
 
rsc committed
proc0init(void)
rsc's avatar
 
rsc committed
  struct proc *p;
  extern uchar _binary_initcode_start[], _binary_initcode_size[];
rsc's avatar
 
rsc committed
  
rsc's avatar
 
rsc committed
  p = copyproc(0);
  p->sz = PAGE;
  p->mem = kalloc(p->sz);
  p->cwd = igetroot();
  memset(&p->tf, 0, sizeof p->tf);
  p->tf->es = p->tf->ds = p->tf->ss = (SEG_UDATA << 3) | DPL_USER;
  p->tf->cs = (SEG_UCODE << 3) | DPL_USER;
  p->tf->eflags = FL_IF;
  p->tf->esp = p->sz;
  
  // Push dummy return address to placate gcc.
  p->tf->esp -= 4;
  *(uint*)(p->mem + p->tf->esp) = 0xefefefef;
rsc's avatar
 
rsc committed
  p->tf->eip = 0;
  memmove(p->mem, _binary_initcode_start, (int)_binary_initcode_size);
  safestrcpy(p->name, "initcode", sizeof p->name);
  p->state = RUNNABLE;