xv6-cs450/main.c

127 lines
3.8 KiB
C
Raw Normal View History

2006-06-12 17:22:12 +02:00
#include "types.h"
2007-08-28 01:26:33 +02:00
#include "defs.h"
2006-06-12 17:22:12 +02:00
#include "param.h"
#include "memlayout.h"
2006-06-12 17:22:12 +02:00
#include "mmu.h"
#include "proc.h"
#include "x86.h"
static void enterothers(void);
2011-08-10 03:47:40 +02:00
static void mpmain(void) __attribute__((noreturn));
extern pde_t *kpgdir;
2006-09-08 16:36:44 +02:00
// 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.
2007-08-28 01:32:16 +02:00
int
main(void)
2006-06-12 17:22:12 +02:00
{
kvmalloc(); // kernel page table
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
pinit(); // process table
tvinit(); // trap vectors
binit(); // buffer cache
fileinit(); // file table
iinit(); // inode cache
ideinit(); // disk
if(!ismp)
timerinit(); // uniprocessor timer
enterothers(); // start other processors (must come before kinit; must use boot_alloc)
kinit(); // initialize memory allocator
userinit(); // first user process (must come after kinit)
// Finish setting up this processor in mpmain.
2007-09-27 21:32:43 +02:00
mpmain();
2006-06-12 17:22:12 +02:00
}
2006-06-22 22:47:23 +02:00
// Other CPUs jump here from entryother.S.
2007-08-28 20:23:48 +02:00
static void
mpboot(void)
{
switchkvm();
seginit();
lapicinit(cpunum());
mpmain();
}
// Common CPU setup code.
static void
mpmain(void)
{
cprintf("cpu%d: starting\n", cpu->id);
idtinit(); // load idt register
xchg(&cpu->booted, 1); // tell enterothers() we're up
scheduler(); // start running processes
}
pde_t bootpgdir[];
// Start the non-boot processors.
2007-08-28 06:40:58 +02:00
static void
enterothers(void)
{
extern uchar _binary_entryother_start[], _binary_entryother_size[];
uchar *code;
struct cpu *c;
char *stack;
// Write bootstrap code to unused memory at 0x7000.
// The linker has placed the image of entryother.S in
// _binary_entryother_start.
code = p2v(0x7000);
memmove(code, _binary_entryother_start, (uint)_binary_entryother_size);
for(c = cpus; c < cpus+ncpu; c++){
if(c == cpus+cpunum()) // We've started already.
continue;
// Tell entryother.S what stack to use, the address of mpboot and pgdir;
2011-08-12 13:31:52 +02:00
// We cannot use kpgdir yet, because the AP processor is running in low
// memory, so we use bootpgdir for the APs too. kalloc can return addresses
// above 4Mbyte (the machine may have much more physical memory than 4Mbyte), which
// aren't mapped by bootpgdir, so we must allocate a stack using boot_alloc();
2011-08-12 18:02:17 +02:00
// This introduces the constraint that xv6 cannot use kalloc until after these
// last boot_alloc invocations.
2011-08-12 13:31:52 +02:00
stack = boot_alloc();
*(void**)(code-4) = stack + KSTACKSIZE;
*(void**)(code-8) = mpboot;
*(int**)(code-12) = (void *) v2p(bootpgdir);
lapicstartap(c->id, v2p(code));
// wait for cpu to finish mpmain()
while(c->booted == 0)
;
}
}
// Boot page table used in multiboot.S and entryother.S.
// Page directories (and page tables), must start on a page boundary,
// hence the "__aligned__" attribute. Also, because of restrictions
// related to linking and static initializers, we use "x + PTE_P"
// here, rather than the more standard "x | PTE_P". Everywhere else
// you should use "|" to combine flags.
2011-08-15 23:41:58 +02:00
// Use PTE_PS in page directory entry to enable 4Mbyte pages.
pte_t dev_pgtable[NPTENTRIES];
pte_t entry_pgtable[NPTENTRIES];
__attribute__((__aligned__(PGSIZE)))
pde_t bootpgdir[NPDENTRIES] = {
// Map VA's [0, 4MB) to PA's [0, 4MB)
[0]
2011-08-15 23:41:58 +02:00
= (0) + PTE_P + PTE_W + PTE_PS,
// Map VA's [KERNBASE, KERNBASE+4MB) to PA's [0, 4MB)
[KERNBASE>>PDXSHIFT]
2011-08-15 23:41:58 +02:00
= (0) + PTE_P + PTE_W + PTE_PS,
};
//PAGEBREAK!
// Blank page.