userinit(); // first user process
// Allocate scheduler stacks and boot the other CPUs.
- for(i=0; i<ncpu; i++){
+ for(i=0; i<ncpu; i++)
cpus[i].stack = kalloc(KSTACKSIZE);
- *(void**)(cpus[i].stack + KSTACKTOP) = 0;
- }
bootothers();
// Switch to our scheduler stack and continue with mpmain.
- asm volatile("movl %0, %%esp" : : "r" (cpus[bcpu].stack+KSTACKTOP));
+ asm volatile("movl %0, %%esp" : : "r" (cpus[bcpu].stack+KSTACKSIZE));
mpmain();
}
continue;
// Fill in %esp, %eip and start code on cpu.
- *(void**)(code-4) = c->stack + KSTACKTOP;
+ *(void**)(code-4) = c->stack + KSTACKSIZE;
*(void**)(code-8) = mpmain;
lapic_startap(c->apicid, (uint)code);
c = &cpus[cpu()];
c->ts.ss0 = SEG_PROCSTACK << 3;
if(p)
- c->ts.esp0 = (uint)(p->kstack + KSTACKTOP);
+ c->ts.esp0 = (uint)(p->kstack + KSTACKSIZE);
else
c->ts.esp0 = 0xffffffff;
np->state = UNUSED;
return 0;
}
- *(void**)(np->kstack + KSTACKTOP) = np;
- np->tf = (struct trapframe*)(np->kstack + KSTACKTOP) - 1;
+ np->tf = (struct trapframe*)(np->kstack + KSTACKSIZE) - 1;
if(p){ // Copy process state from p.
np->parent = p;
struct proc*
curproc(void)
{
- uint esp;
+ struct proc *p;
- asm volatile("movl %%esp, %0" : "=a" (esp));
- return *(struct proc**)((esp & ~(KSTACKSIZE-1)) + KSTACKTOP);
+ pushcli();
+ p = cpus[cpu()].curproc;
+ popcli();
+ return p;
}
//PAGEBREAK: 42
// Switch to chosen process. It is the process's job
// to release proc_table_lock and then reacquire it
// before jumping back to us.
+ c->curproc = p;
setupsegs(p);
p->state = RUNNING;
swtch(&c->context, &p->context);
// Process is done running for now.
// It should have changed its p->state before coming back.
+ c->curproc = 0;
setupsegs(0);
}
}
// Enter scheduler. Must already hold proc_table_lock
-// and have changed cp->state.
+// and have changed curproc[cpu()]->state.
void
sched(void)
{
char name[16]; // Process name (debugging)
};
-// The word at kstack + KSTACKTOP is a pointer to the struct proc.
-#define KSTACKTOP (KSTACKSIZE-4)
-
-
// Process memory is laid out contiguously, low addresses first:
// text
// original data and bss
// Per-CPU state
struct cpu {
uchar apicid; // Local APIC ID
+ struct proc *curproc; // Process currently running.
struct context context; // Switch here to enter scheduler
struct taskstate ts; // Used by x86 to find stack for interrupt
struct segdesc gdt[NSEGS]; // x86 global descriptor table