xv6/main.c

117 lines
3.3 KiB
C
Raw Normal View History

2006-06-12 15:22:12 +00:00
#include "types.h"
2007-08-27 23:26:33 +00:00
#include "defs.h"
2006-06-12 15:22:12 +00:00
#include "param.h"
#include "memlayout.h"
2006-06-12 15:22:12 +00:00
#include "mmu.h"
#include "proc.h"
#include "x86.h"
2011-08-16 00:11:13 +00:00
static void startothers(void);
2011-08-10 01:47:40 +00:00
static void mpmain(void) __attribute__((noreturn));
extern pde_t *kpgdir;
extern char end[]; // first address after kernel loaded from ELF file
2006-09-08 14:36:44 +00: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-27 23:32:16 +00:00
int
main(void)
2006-06-12 15:22:12 +00:00
{
kinit1(end, P2V(4*1024*1024)); // phys page allocator
kvmalloc(); // kernel page table
mpinit(); // collect info about this machine
lapicinit();
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
startothers(); // start other processors
kinit2(P2V(4*1024*1024), P2V(PHYSTOP)); // must come after startothers()
userinit(); // first user process
// Finish setting up this processor in mpmain.
2007-09-27 19:32:43 +00:00
mpmain();
2006-06-12 15:22:12 +00:00
}
2006-06-22 20:47:23 +00:00
// Other CPUs jump here from entryother.S.
2007-08-28 18:23:48 +00:00
static void
2011-08-16 00:11:13 +00:00
mpenter(void)
{
switchkvm();
seginit();
lapicinit();
mpmain();
}
// Common CPU setup code.
static void
mpmain(void)
{
cprintf("cpu%d: starting\n", cpu->id);
idtinit(); // load idt register
2011-08-16 00:11:13 +00:00
xchg(&cpu->started, 1); // tell startothers() we're up
scheduler(); // start running processes
}
2011-08-31 00:50:19 +00:00
pde_t entrypgdir[]; // For entry.S
2011-08-16 00:11:13 +00:00
// Start the non-boot (AP) processors.
2007-08-28 04:40:58 +00:00
static void
2011-08-16 00:11:13 +00:00
startothers(void)
{
extern uchar _binary_entryother_start[], _binary_entryother_size[];
uchar *code;
struct cpu *c;
char *stack;
2011-08-16 00:11:13 +00:00
// Write entry 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;
2011-09-01 14:25:20 +00:00
// Tell entryother.S what stack to use, where to enter, and what
// pgdir to use. We cannot use kpgdir yet, because the AP processor
// is running in low memory, so we use entrypgdir for the APs too.
stack = kalloc();
*(void**)(code-4) = stack + KSTACKSIZE;
2011-08-16 00:11:13 +00:00
*(void**)(code-8) = mpenter;
2011-08-31 00:50:19 +00:00
*(int**)(code-12) = (void *) v2p(entrypgdir);
lapicstartap(c->id, v2p(code));
// wait for cpu to finish mpmain()
2011-08-16 00:11:13 +00:00
while(c->started == 0)
;
}
}
2011-08-16 00:11:13 +00:00
// Boot page table used in entry.S and entryother.S.
// Page directories (and page tables), must start on a page boundary,
2011-08-16 00:11:13 +00:00
// hence the "__aligned__" attribute.
2011-08-15 21:41:58 +00:00
// Use PTE_PS in page directory entry to enable 4Mbyte pages.
__attribute__((__aligned__(PGSIZE)))
2011-08-31 00:50:19 +00:00
pde_t entrypgdir[NPDENTRIES] = {
2011-08-29 20:12:01 +00:00
// Map VA's [0, 4MB) to PA's [0, 4MB)
[0] = (0) | PTE_P | PTE_W | PTE_PS,
2011-08-29 20:12:01 +00:00
// Map VA's [KERNBASE, KERNBASE+4MB) to PA's [0, 4MB)
[KERNBASE>>PDXSHIFT] = (0) | PTE_P | PTE_W | PTE_PS,
};
//PAGEBREAK!
// Blank page.