Skip to content
Snippets Groups Projects
main.c 3.51 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"
Frans Kaashoek's avatar
Frans Kaashoek committed
#include "memlayout.h"
rtm's avatar
rtm committed
#include "mmu.h"
#include "proc.h"
#include "x86.h"

Frans Kaashoek's avatar
Frans Kaashoek committed
static void startothers(void);
Frans Kaashoek's avatar
Frans Kaashoek committed
static void mpmain(void)  __attribute__((noreturn));
extern pde_t *kpgdir;
Frans Kaashoek's avatar
Frans Kaashoek committed

kaashoek's avatar
kaashoek committed
// Bootstrap processor starts running C code here.
// Allocate a real stack and switch to it, first
// doing some setup required for memory allocator to work.
rsc's avatar
rsc committed
int
main(void)
rtm's avatar
rtm committed
{
  mpinit();        // collect info about this machine
  lapicinit(mpbcpu());
  seginit();       // set up segments
  cprintf("\ncpu%d: starting xv6\n\n", cpu->id);
  picinit();       // interrupt controller
  ioapicinit();    // another interrupt controller
  consoleinit();   // I/O devices & their interrupts
  uartinit();      // serial port
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
Frans Kaashoek's avatar
Frans Kaashoek committed
  startothers();    // start other processors (must come before kinit; must use enter_alloc)
  kinit();         // initialize memory allocator
  userinit();      // first user process  (must come after kinit)
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

// Other CPUs jump here from entryother.S.
rsc's avatar
rsc committed
static void
Frans Kaashoek's avatar
Frans Kaashoek committed
mpenter(void)
Frans Kaashoek's avatar
Frans Kaashoek committed
  seginit();
  lapicinit(cpunum());
  mpmain();
}

// Common CPU setup code.
static void
mpmain(void)
{
Robert Morris's avatar
Robert Morris committed
  cprintf("cpu%d: starting\n", cpu->id);
Robert Morris's avatar
Robert Morris committed
  idtinit();       // load idt register
Frans Kaashoek's avatar
Frans Kaashoek committed
  xchg(&cpu->started, 1); // tell startothers() we're up
Robert Morris's avatar
Robert Morris committed
  scheduler();     // start running processes
Frans Kaashoek's avatar
Frans Kaashoek committed
pde_t enterpgdir[];  // For entry.S
Frans Kaashoek's avatar
Frans Kaashoek committed
// Start the non-boot (AP) processors.
rsc's avatar
rsc committed
static void
Frans Kaashoek's avatar
Frans Kaashoek committed
startothers(void)
rsc's avatar
rsc committed
{
  extern uchar _binary_entryother_start[], _binary_entryother_size[];
rsc's avatar
rsc committed
  uchar *code;
  struct cpu *c;
rsc's avatar
 
rsc committed
  char *stack;
rsc's avatar
rsc committed

Frans Kaashoek's avatar
Frans Kaashoek committed
  // Write entry code to unused memory at 0x7000.
  // The linker has placed the image of entryother.S in
  // _binary_entryother_start.
Frans Kaashoek's avatar
Frans Kaashoek committed
  code = p2v(0x7000);
  memmove(code, _binary_entryother_start, (uint)_binary_entryother_size);
Robert Morris's avatar
Robert Morris committed

rsc's avatar
rsc committed
  for(c = cpus; c < cpus+ncpu; c++){
Russ Cox's avatar
Russ Cox committed
    if(c == cpus+cpunum())  // We've started already.
rsc's avatar
rsc committed
      continue;

Frans Kaashoek's avatar
Frans Kaashoek committed
    // Tell entryother.S what stack to use, the address of mpenter and pgdir;
Frans Kaashoek's avatar
Frans Kaashoek committed
    // We cannot use kpgdir yet, because the AP processor is running in low 
Frans Kaashoek's avatar
Frans Kaashoek committed
    // memory, so we use enterpgdir for the APs too.  kalloc can return addresses
Frans Kaashoek's avatar
Frans Kaashoek committed
    // above 4Mbyte (the machine may have much more physical memory than 4Mbyte), which 
Frans Kaashoek's avatar
Frans Kaashoek committed
    // aren't mapped by enterpgdir, so we must allocate a stack using enter_alloc();
Frans Kaashoek's avatar
Frans Kaashoek committed
    // This introduces the constraint that xv6 cannot use kalloc until after these 
Frans Kaashoek's avatar
Frans Kaashoek committed
    // last enter_alloc invocations.
    stack = enter_alloc();
rsc's avatar
 
rsc committed
    *(void**)(code-4) = stack + KSTACKSIZE;
Frans Kaashoek's avatar
Frans Kaashoek committed
    *(void**)(code-8) = mpenter;
    *(int**)(code-12) = (void *) v2p(enterpgdir);
Frans Kaashoek's avatar
Frans Kaashoek committed
    lapicstartap(c->id, v2p(code));
rsc's avatar
rsc committed

    // wait for cpu to finish mpmain()
Frans Kaashoek's avatar
Frans Kaashoek committed
    while(c->started == 0)
Frans Kaashoek's avatar
Frans Kaashoek committed
// Boot page table used in entry.S and entryother.S.
// Page directories (and page tables), must start on a page boundary,
Frans Kaashoek's avatar
Frans Kaashoek committed
// hence the "__aligned__" attribute.  
// Use PTE_PS in page directory entry to enable 4Mbyte pages.
Frans Kaashoek's avatar
Frans Kaashoek committed
pde_t enterpgdir[NPDENTRIES] = {
  // Map VA's [0, 4MB) to PA's [0, 4MB)
  [0] = (0) + PTE_P + PTE_W + PTE_PS,
  // Map VA's [KERNBASE, KERNBASE+4MB) to PA's [0, 4MB)
  [KERNBASE>>PDXSHIFT] = (0) + PTE_P + PTE_W + PTE_PS,
//PAGEBREAK!
// Blank page.