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 "mmu.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "x86.h"
|
|
|
|
|
2007-08-28 06:13:24 +02:00
|
|
|
static void bootothers(void);
|
2007-08-28 00:53:31 +02:00
|
|
|
|
2006-09-08 16:36:44 +02:00
|
|
|
// Bootstrap processor starts running C code here.
|
2007-08-28 01:32:16 +02:00
|
|
|
int
|
|
|
|
main(void)
|
2006-06-12 17:22:12 +02:00
|
|
|
{
|
2006-07-16 17:50:13 +02:00
|
|
|
int i;
|
2007-08-24 21:36:52 +02:00
|
|
|
static volatile int bcpu; // cannot be on stack
|
2007-08-28 06:13:24 +02:00
|
|
|
extern char edata[], end[];
|
2006-06-22 03:28:57 +02:00
|
|
|
|
2006-06-14 00:08:20 +02:00
|
|
|
// clear BSS
|
|
|
|
memset(edata, 0, end - edata);
|
|
|
|
|
2006-08-29 21:06:37 +02:00
|
|
|
// Prevent release() from enabling interrupts.
|
|
|
|
for(i=0; i<NCPU; i++)
|
|
|
|
cpus[i].nlock = 1;
|
2006-07-16 17:50:13 +02:00
|
|
|
|
2006-07-12 19:00:54 +02:00
|
|
|
mp_init(); // collect info about this machine
|
2006-09-08 16:48:07 +02:00
|
|
|
bcpu = mp_bcpu();
|
|
|
|
|
2007-08-24 21:36:52 +02:00
|
|
|
// Switch to bootstrap processor's stack
|
2007-08-22 08:01:32 +02:00
|
|
|
asm volatile("movl %0, %%esp" : : "r" (cpus[bcpu].mpstack+MPSTACK-32));
|
|
|
|
asm volatile("movl %0, %%ebp" : : "r" (cpus[bcpu].mpstack+MPSTACK));
|
2006-07-12 19:00:54 +02:00
|
|
|
|
2006-09-08 16:48:07 +02:00
|
|
|
lapic_init(bcpu);
|
2007-08-28 01:26:33 +02:00
|
|
|
cprintf("\ncpu%d: starting xv6\n\n", cpu());
|
2006-06-12 17:22:12 +02:00
|
|
|
|
2007-08-22 08:01:32 +02:00
|
|
|
pinit(); // process table
|
|
|
|
binit(); // buffer cache
|
|
|
|
pic_init(); // interrupt controller
|
|
|
|
ioapic_init(); // another interrupt controller
|
|
|
|
kinit(); // physical memory allocator
|
|
|
|
tvinit(); // trap vectors
|
|
|
|
idtinit(); // interrupt descriptor table
|
|
|
|
fileinit(); // file table
|
|
|
|
iinit(); // inode cache
|
|
|
|
setupsegs(0); // segments & TSS
|
|
|
|
console_init(); // I/O devices & their interrupts
|
|
|
|
ide_init(); // disk
|
2007-08-28 00:53:31 +02:00
|
|
|
bootothers(); // boot other CPUs
|
2007-08-27 18:57:13 +02:00
|
|
|
if(!ismp)
|
2007-08-22 08:01:32 +02:00
|
|
|
pit8253_timerinit(); // uniprocessor timer
|
|
|
|
userinit(); // first user process
|
2006-07-12 03:48:35 +02:00
|
|
|
|
2006-09-07 04:15:28 +02:00
|
|
|
// enable interrupts on this processor.
|
2006-07-16 17:50:13 +02:00
|
|
|
cpus[cpu()].nlock--;
|
2006-07-11 19:39:45 +02:00
|
|
|
sti();
|
2006-06-16 22:29:25 +02:00
|
|
|
|
Changes to allow use of native x86 ELF compilers, which on my
Linux 2.4 box using gcc 3.4.6 don't seem to follow the same
conventions as the i386-jos-elf-gcc compilers.
Can run make 'TOOLPREFIX=' or edit the Makefile.
curproc[cpu()] can now be NULL, indicating that no proc is running.
This seemed safer to me than having curproc[0] and curproc[1]
both pointing at proc[0] potentially.
The old implementation of swtch depended on the stack frame layout
used inside swtch being okay to return from on the other stack
(exactly the V6 you are not expected to understand this).
It also could be called in two contexts: at boot time, to schedule
the very first process, and later, on behalf of a process, to sleep
or schedule some other process.
I split this into two functions: scheduler and swtch.
The scheduler is now a separate never-returning function, invoked
by each cpu once set up. The scheduler looks like:
scheduler() {
setjmp(cpu.context);
pick proc to schedule
blah blah blah
longjmp(proc.context)
}
The new swtch is intended to be called only when curproc[cpu()] is not NULL,
that is, only on behalf of a user proc. It does:
swtch() {
if(setjmp(proc.context) == 0)
longjmp(cpu.context)
}
to save the current proc context and then jump over to the scheduler,
running on the cpu stack.
Similarly the system call stubs are now in assembly in usys.S to avoid
needing to know the details of stack frame layout used by the compiler.
Also various changes in the debugging prints.
2006-07-11 03:07:40 +02:00
|
|
|
scheduler();
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
2006-06-22 22:47:23 +02:00
|
|
|
|
2006-07-16 17:50:13 +02:00
|
|
|
// Additional processors start here.
|
2006-07-16 18:03:51 +02:00
|
|
|
void
|
2006-07-16 17:50:13 +02:00
|
|
|
mpmain(void)
|
|
|
|
{
|
2007-08-28 01:26:33 +02:00
|
|
|
cprintf("cpu%d: starting\n", cpu());
|
2007-08-24 21:36:52 +02:00
|
|
|
idtinit();
|
2006-07-16 17:50:13 +02:00
|
|
|
lapic_init(cpu());
|
2006-08-16 00:18:20 +02:00
|
|
|
setupsegs(0);
|
2006-09-06 19:04:06 +02:00
|
|
|
cpuid(0, 0, 0, 0, 0); // memory barrier
|
2006-08-08 21:58:06 +02:00
|
|
|
cpus[cpu()].booted = 1;
|
|
|
|
|
2006-07-16 17:50:13 +02:00
|
|
|
// Enable interrupts on this processor.
|
|
|
|
cpus[cpu()].nlock--;
|
|
|
|
sti();
|
|
|
|
|
|
|
|
scheduler();
|
|
|
|
}
|
|
|
|
|
2007-08-28 06:14:32 +02:00
|
|
|
void
|
2007-08-28 00:53:31 +02:00
|
|
|
bootothers(void)
|
|
|
|
{
|
|
|
|
extern uchar _binary_bootother_start[], _binary_bootother_size[];
|
|
|
|
uchar *code;
|
|
|
|
struct cpu *c;
|
|
|
|
|
|
|
|
// Write bootstrap code to unused memory at 0x7000.
|
|
|
|
code = (uchar*)0x7000;
|
|
|
|
memmove(code, _binary_bootother_start, (uint)_binary_bootother_size);
|
|
|
|
|
|
|
|
for(c = cpus; c < cpus+ncpu; c++){
|
|
|
|
if(c == cpus+cpu()) // We've started already.
|
|
|
|
continue;
|
|
|
|
|
2007-08-28 06:13:24 +02:00
|
|
|
// Fill in %esp, %eip and start code on cpu.
|
2007-08-28 00:53:31 +02:00
|
|
|
*(void**)(code-4) = c->mpstack + MPSTACK;
|
|
|
|
*(void**)(code-8) = mpmain;
|
|
|
|
lapic_startap(c->apicid, (uint)code);
|
|
|
|
|
|
|
|
// Wait for cpu to get through bootstrap.
|
|
|
|
while(c->booted == 0)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|