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-09-13 19:14:52 +02:00
|
|
|
extern char end[]; // first address after kernel loaded from ELF file
|
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-09-13 19:14:52 +02:00
|
|
|
kinit1(end, P2V(4*1024*1024)); // phys page allocator
|
2011-08-10 03:37:35 +02:00
|
|
|
kvmalloc(); // kernel page table
|
2016-08-11 19:55:13 +02:00
|
|
|
mpinit(); // detect other processors
|
|
|
|
lapicinit(); // interrupt controller
|
|
|
|
seginit(); // segment descriptors
|
2017-08-09 13:43:06 +02:00
|
|
|
picinit(); // disable pic
|
2010-09-13 21:34:44 +02:00
|
|
|
ioapicinit(); // another interrupt controller
|
2016-08-11 19:55:13 +02:00
|
|
|
consoleinit(); // console hardware
|
2010-09-13 21:34:44 +02:00
|
|
|
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
|
2017-01-31 23:47:16 +01:00
|
|
|
ideinit(); // disk
|
2011-09-13 19:14:52 +02:00
|
|
|
startothers(); // start other processors
|
|
|
|
kinit2(P2V(4*1024*1024), P2V(PHYSTOP)); // must come after startothers()
|
|
|
|
userinit(); // first user process
|
2016-08-11 19:55:13 +02:00
|
|
|
mpmain(); // finish this processor's setup
|
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
|
|
|
{
|
2016-08-25 15:13:00 +02:00
|
|
|
switchkvm();
|
2011-07-29 13:31:27 +02:00
|
|
|
seginit();
|
2012-08-23 02:13:43 +02:00
|
|
|
lapicinit();
|
2011-07-29 13:31:27 +02:00
|
|
|
mpmain();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common CPU setup code.
|
|
|
|
static void
|
|
|
|
mpmain(void)
|
|
|
|
{
|
2017-02-02 00:04:13 +01:00
|
|
|
cprintf("cpu%d: starting %d\n", cpuid(), cpuid());
|
2010-08-06 03:16:55 +02:00
|
|
|
idtinit(); // load idt register
|
2017-01-31 23:47:16 +01:00
|
|
|
xchg(&(mycpu()->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-31 02:50:19 +02:00
|
|
|
pde_t entrypgdir[]; // 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.
|
2016-08-24 19:40:06 +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++){
|
2017-01-31 23:47:16 +01:00
|
|
|
if(c == mycpu()) // We've started already.
|
2007-08-28 00:53:31 +02:00
|
|
|
continue;
|
|
|
|
|
2016-08-25 15:13:00 +02:00
|
|
|
// Tell entryother.S what stack to use, where to enter, and what
|
2011-09-01 16:25:20 +02:00
|
|
|
// 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.
|
2011-09-13 19:14:52 +02:00
|
|
|
stack = kalloc();
|
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;
|
2016-08-24 19:40:06 +02:00
|
|
|
*(int**)(code-12) = (void *) V2P(entrypgdir);
|
2010-09-13 21:34:44 +02:00
|
|
|
|
2016-09-02 14:31:13 +02:00
|
|
|
lapicstartap(c->apicid, 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
|
|
|
|
2016-08-10 17:35:28 +02:00
|
|
|
// The boot page table used in entry.S and entryother.S.
|
|
|
|
// Page directories (and page tables) must start on page boundaries,
|
2016-08-25 15:13:00 +02:00
|
|
|
// hence the __aligned__ attribute.
|
2016-08-10 17:35:28 +02:00
|
|
|
// PTE_PS in a page directory entry enables 4Mbyte pages.
|
|
|
|
|
2011-08-10 03:37:35 +02:00
|
|
|
__attribute__((__aligned__(PGSIZE)))
|
2011-08-31 02:50:19 +02:00
|
|
|
pde_t entrypgdir[NPDENTRIES] = {
|
2011-08-29 22:12:01 +02:00
|
|
|
// Map VA's [0, 4MB) to PA's [0, 4MB)
|
2011-09-14 19:47:04 +02:00
|
|
|
[0] = (0) | PTE_P | PTE_W | PTE_PS,
|
2011-08-29 22:12:01 +02:00
|
|
|
// Map VA's [KERNBASE, KERNBASE+4MB) to PA's [0, 4MB)
|
2011-09-14 19:47:04 +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.
|
2014-08-29 23:06:49 +02:00
|
|
|
//PAGEBREAK!
|
2017-08-29 20:11:59 +02:00
|
|
|
// Blank page.
|
|
|
|
//PAGEBREAK!
|
|
|
|
// Blank page.
|
|
|
|
|