Skip to content
Snippets Groups Projects
main.c 2.92 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);
static void mpmain(void);
Russ Cox's avatar
Russ Cox committed
void jmpkstack(void)  __attribute__((noreturn));
Robert Morris's avatar
Robert Morris committed
void mainc(void);
rsc's avatar
rsc 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
  kinit();         // initialize memory allocator
Russ Cox's avatar
Russ Cox committed
  jmpkstack();       // call mainc() on a properly-allocated stack 
Russ Cox's avatar
Russ Cox committed
jmpkstack(void)
Robert Morris's avatar
Robert Morris committed
{
Russ Cox's avatar
Russ Cox committed
  char *kstack, *top;
  
  kstack = kalloc();
  if(kstack == 0)
    panic("jmpkstack kalloc");
  top = kstack + PGSIZE;
  asm volatile("movl %0,%%esp; call mainc" : : "r" (top));
  panic("jmpkstack");
rtm's avatar
rtm committed

// Set up hardware and software.
// Runs only on the boostrap processor.
void
mainc(void)
{
  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
  kvmalloc();      // initialize the kernel page table
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
  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

Robert Morris's avatar
Robert Morris committed
// Common CPU setup code.
// Bootstrap CPU comes here from mainc().
// Other CPUs jump here from bootother.S.
rsc's avatar
rsc committed
static void
mpmain(void)
{
Russ Cox's avatar
Russ Cox committed
  if(cpunum() != mpbcpu()){
Russ Cox's avatar
Russ Cox committed
    lapicinit(cpunum());
Robert Morris's avatar
xx  
Robert Morris committed
  vmenable();        // turn on paging
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
  xchg(&cpu->booted, 1); // tell bootothers() we're up
Robert Morris's avatar
Robert Morris committed
  scheduler();     // start running processes
// Start the non-boot processors.
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

Russ Cox's avatar
Russ Cox committed
  // Write bootstrap code to unused memory at 0x7000.
  // The linker has placed the image of bootother.S in _binary_bootother_start.
  code = (uchar*)0x7000;
rsc's avatar
rsc committed
  memmove(code, _binary_bootother_start, (uint)_binary_bootother_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;

    // Tell bootother.S what stack to use and the address of mpmain;
    // it expects to find these two addresses stored just before
    // its first instruction.
    stack = kalloc();
rsc's avatar
 
rsc committed
    *(void**)(code-4) = stack + KSTACKSIZE;
rsc's avatar
rsc committed
    *(void**)(code-8) = mpmain;
Russ Cox's avatar
Russ Cox committed
    lapicstartap(c->id, (uint)code);
rsc's avatar
rsc committed

Robert Morris's avatar
Robert Morris committed
    // Wait for cpu to finish mpmain()
rsc's avatar
rsc committed
    while(c->booted == 0)
      ;
  }
}