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"
|
2011-07-29 13:31:27 +02:00
|
|
|
#include "memlayout.h"
|
2006-06-12 17:22:12 +02:00
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "x86.h"
|
|
|
|
|
2011-08-16 02:11:13 +02:00
|
|
|
static void startothers(void);
|
2011-08-10 03:47:40 +02:00
|
|
|
static void mpmain(void) __attribute__((noreturn));
|
2011-08-11 18:25:10 +02:00
|
|
|
extern pde_t *kpgdir;
|
2011-07-29 13:31:27 +02:00
|
|
|
|
2006-09-08 16:36:44 +02:00
|
|
|
// Bootstrap processor starts running C code here.
|
2010-09-13 21:34:44 +02:00
|
|
|
// 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
|
|
|
{
|
2011-08-10 03:37:35 +02:00
|
|
|
kvmalloc(); // kernel page table
|
2010-07-23 13:41:13 +02:00
|
|
|
mpinit(); // collect info about this machine
|
2009-03-08 23:07:13 +01:00
|
|
|
lapicinit(mpbcpu());
|
2010-09-13 21:34:44 +02:00
|
|
|
seginit(); // set up segments
|
2010-07-02 20:51:53 +02:00
|
|
|
cprintf("\ncpu%d: starting xv6\n\n", cpu->id);
|
2010-09-13 21:34:44 +02:00
|
|
|
picinit(); // interrupt controller
|
|
|
|
ioapicinit(); // another interrupt controller
|
|
|
|
consoleinit(); // I/O devices & their interrupts
|
|
|
|
uartinit(); // serial port
|
2009-05-31 02:28:45 +02:00
|
|
|
pinit(); // process table
|
2007-08-22 08:01:32 +02:00
|
|
|
tvinit(); // trap vectors
|
2009-05-31 02:28:45 +02:00
|
|
|
binit(); // buffer cache
|
2007-08-22 08:01:32 +02:00
|
|
|
fileinit(); // file table
|
|
|
|
iinit(); // inode cache
|
2009-05-31 02:28:45 +02:00
|
|
|
ideinit(); // disk
|
2007-08-27 18:57:13 +02:00
|
|
|
if(!ismp)
|
2009-05-31 02:28:45 +02:00
|
|
|
timerinit(); // uniprocessor timer
|
2011-08-16 02:11:13 +02:00
|
|
|
startothers(); // start other processors (must come before kinit; must use enter_alloc)
|
2011-08-11 18:25:10 +02:00
|
|
|
kinit(); // initialize memory allocator
|
|
|
|
userinit(); // first user process (must come after kinit)
|
2007-09-27 23:25:37 +02:00
|
|
|
// 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
|
|
|
|
2011-08-15 18:02:59 +02:00
|
|
|
// Other CPUs jump here from entryother.S.
|
2007-08-28 20:23:48 +02:00
|
|
|
static void
|
2011-08-16 02:11:13 +02:00
|
|
|
mpenter(void)
|
2006-07-16 17:50:13 +02:00
|
|
|
{
|
2011-08-11 18:25:10 +02:00
|
|
|
switchkvm();
|
2011-07-29 13:31:27 +02:00
|
|
|
seginit();
|
|
|
|
lapicinit(cpunum());
|
|
|
|
mpmain();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common CPU setup code.
|
|
|
|
static void
|
|
|
|
mpmain(void)
|
|
|
|
{
|
2010-08-05 20:15:03 +02:00
|
|
|
cprintf("cpu%d: starting\n", cpu->id);
|
2010-08-06 03:16:55 +02:00
|
|
|
idtinit(); // load idt register
|
2011-08-16 02:11:13 +02:00
|
|
|
xchg(&cpu->started, 1); // tell startothers() we're up
|
2010-08-06 03:16:55 +02:00
|
|
|
scheduler(); // start running processes
|
2006-07-16 17:50:13 +02:00
|
|
|
}
|
|
|
|
|
2011-08-23 02:05:15 +02:00
|
|
|
pde_t enterpgdir[]; // For entry.S
|
2011-08-11 18:25:10 +02:00
|
|
|
|
2011-08-16 02:11:13 +02:00
|
|
|
// Start the non-boot (AP) processors.
|
2007-08-28 06:40:58 +02:00
|
|
|
static void
|
2011-08-16 02:11:13 +02:00
|
|
|
startothers(void)
|
2007-08-28 00:53:31 +02:00
|
|
|
{
|
2011-08-15 18:02:59 +02:00
|
|
|
extern uchar _binary_entryother_start[], _binary_entryother_size[];
|
2007-08-28 00:53:31 +02:00
|
|
|
uchar *code;
|
|
|
|
struct cpu *c;
|
2007-09-27 23:25:37 +02:00
|
|
|
char *stack;
|
2007-08-28 00:53:31 +02:00
|
|
|
|
2011-08-16 02:11:13 +02:00
|
|
|
// Write entry code to unused memory at 0x7000.
|
2011-08-15 18:02:59 +02:00
|
|
|
// The linker has placed the image of entryother.S in
|
|
|
|
// _binary_entryother_start.
|
2011-07-29 13:31:27 +02:00
|
|
|
code = p2v(0x7000);
|
2011-08-15 18:02:59 +02:00
|
|
|
memmove(code, _binary_entryother_start, (uint)_binary_entryother_size);
|
2010-08-06 03:16:55 +02:00
|
|
|
|
2007-08-28 00:53:31 +02:00
|
|
|
for(c = cpus; c < cpus+ncpu; c++){
|
2009-08-31 08:02:08 +02:00
|
|
|
if(c == cpus+cpunum()) // We've started already.
|
2007-08-28 00:53:31 +02:00
|
|
|
continue;
|
|
|
|
|
2011-08-16 02:11:13 +02:00
|
|
|
// Tell entryother.S what stack to use, the address of mpenter and pgdir;
|
2011-08-12 13:31:52 +02:00
|
|
|
// We cannot use kpgdir yet, because the AP processor is running in low
|
2011-08-16 02:11:13 +02:00
|
|
|
// memory, so we use enterpgdir for the APs too. kalloc can return addresses
|
2011-08-12 13:31:52 +02:00
|
|
|
// above 4Mbyte (the machine may have much more physical memory than 4Mbyte), which
|
2011-08-16 02:11:13 +02:00
|
|
|
// aren't mapped by enterpgdir, so we must allocate a stack using enter_alloc();
|
2011-08-12 18:02:17 +02:00
|
|
|
// This introduces the constraint that xv6 cannot use kalloc until after these
|
2011-08-16 02:11:13 +02:00
|
|
|
// last enter_alloc invocations.
|
|
|
|
stack = enter_alloc();
|
2007-09-27 23:25:37 +02:00
|
|
|
*(void**)(code-4) = stack + KSTACKSIZE;
|
2011-08-16 02:11:13 +02:00
|
|
|
*(void**)(code-8) = mpenter;
|
|
|
|
*(int**)(code-12) = (void *) v2p(enterpgdir);
|
2010-09-13 21:34:44 +02:00
|
|
|
|
2011-07-29 13:31:27 +02:00
|
|
|
lapicstartap(c->id, v2p(code));
|
2007-08-28 00:53:31 +02:00
|
|
|
|
2011-08-11 18:25:10 +02:00
|
|
|
// wait for cpu to finish mpmain()
|
2011-08-16 02:11:13 +02:00
|
|
|
while(c->started == 0)
|
2007-08-28 00:53:31 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
2011-02-20 03:17:55 +01:00
|
|
|
|
2011-08-16 02:11:13 +02:00
|
|
|
// Boot page table used in entry.S and entryother.S.
|
2011-08-10 03:37:35 +02:00
|
|
|
// Page directories (and page tables), must start on a page boundary,
|
2011-08-16 02:11:13 +02:00
|
|
|
// hence the "__aligned__" attribute.
|
2011-08-15 23:41:58 +02:00
|
|
|
// Use PTE_PS in page directory entry to enable 4Mbyte pages.
|
2011-08-10 03:37:35 +02:00
|
|
|
__attribute__((__aligned__(PGSIZE)))
|
2011-08-16 02:11:13 +02:00
|
|
|
pde_t enterpgdir[NPDENTRIES] = {
|
2011-08-10 03:37:35 +02:00
|
|
|
// Map VA's [0, 4MB) to PA's [0, 4MB)
|
2011-08-16 02:11:13 +02:00
|
|
|
[0] = (0) + PTE_P + PTE_W + PTE_PS,
|
2011-08-10 03:37:35 +02:00
|
|
|
// Map VA's [KERNBASE, KERNBASE+4MB) to PA's [0, 4MB)
|
2011-08-16 02:11:13 +02:00
|
|
|
[KERNBASE>>PDXSHIFT] = (0) + PTE_P + PTE_W + PTE_PS,
|
2011-08-10 03:37:35 +02:00
|
|
|
};
|
|
|
|
|
2011-02-20 03:17:55 +01:00
|
|
|
//PAGEBREAK!
|
|
|
|
// Blank page.
|
|
|
|
|