assorted fixes:
* rename c/cp to cpu/proc * rename cpu.context to cpu.scheduler * fix some comments * formatting for printout
This commit is contained in:
parent
0aef891495
commit
48755214c9
23 changed files with 2437 additions and 2379 deletions
115
proc.c
115
proc.c
|
@ -65,32 +65,31 @@ procdump(void)
|
|||
void
|
||||
ksegment(void)
|
||||
{
|
||||
struct cpu *c1;
|
||||
struct cpu *c;
|
||||
|
||||
c1 = &cpus[cpu()];
|
||||
c1->gdt[SEG_KCODE] = SEG(STA_X|STA_R, 0, 0x100000 + 64*1024-1, 0);
|
||||
c1->gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, 0);
|
||||
c1->gdt[SEG_KCPU] = SEG(STA_W, (uint)(&c1->tls+1), 0xffffffff, 0);
|
||||
lgdt(c1->gdt, sizeof(c1->gdt));
|
||||
c = &cpus[cpunum()];
|
||||
c->gdt[SEG_KCODE] = SEG(STA_X|STA_R, 0, 0x100000 + 64*1024-1, 0);
|
||||
c->gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, 0);
|
||||
c->gdt[SEG_KCPU] = SEG(STA_W, (uint)(&c->tls+1), 0xffffffff, 0);
|
||||
lgdt(c->gdt, sizeof(c->gdt));
|
||||
loadfsgs(SEG_KCPU << 3);
|
||||
|
||||
// Initialize cpu-local variables.
|
||||
c = c1;
|
||||
cp = 0;
|
||||
cpu = c;
|
||||
proc = 0;
|
||||
}
|
||||
|
||||
// Set up CPU's segment descriptors and current process task state.
|
||||
// If cp==0, set up for "idle" state for when scheduler() is running.
|
||||
void
|
||||
usegment(void)
|
||||
{
|
||||
pushcli();
|
||||
c->gdt[SEG_UCODE] = SEG(STA_X|STA_R, (uint)cp->mem, cp->sz-1, DPL_USER);
|
||||
c->gdt[SEG_UDATA] = SEG(STA_W, (uint)cp->mem, cp->sz-1, DPL_USER);
|
||||
c->gdt[SEG_TSS] = SEG16(STS_T32A, (uint)&c->ts, sizeof(c->ts)-1, 0);
|
||||
c->gdt[SEG_TSS].s = 0;
|
||||
c->ts.ss0 = SEG_KDATA << 3;
|
||||
c->ts.esp0 = (uint)cp->kstack + KSTACKSIZE;
|
||||
cpu->gdt[SEG_UCODE] = SEG(STA_X|STA_R, proc->mem, proc->sz-1, DPL_USER);
|
||||
cpu->gdt[SEG_UDATA] = SEG(STA_W, proc->mem, proc->sz-1, DPL_USER);
|
||||
cpu->gdt[SEG_TSS] = SEG16(STS_T32A, &cpu->ts, sizeof(cpu->ts)-1, 0);
|
||||
cpu->gdt[SEG_TSS].s = 0;
|
||||
cpu->ts.ss0 = SEG_KDATA << 3;
|
||||
cpu->ts.esp0 = (uint)proc->kstack + KSTACKSIZE;
|
||||
ltr(SEG_TSS << 3);
|
||||
popcli();
|
||||
}
|
||||
|
@ -178,14 +177,14 @@ growproc(int n)
|
|||
{
|
||||
char *newmem;
|
||||
|
||||
newmem = kalloc(cp->sz + n);
|
||||
newmem = kalloc(proc->sz + n);
|
||||
if(newmem == 0)
|
||||
return -1;
|
||||
memmove(newmem, cp->mem, cp->sz);
|
||||
memset(newmem + cp->sz, 0, n);
|
||||
kfree(cp->mem, cp->sz);
|
||||
cp->mem = newmem;
|
||||
cp->sz += n;
|
||||
memmove(newmem, proc->mem, proc->sz);
|
||||
memset(newmem + proc->sz, 0, n);
|
||||
kfree(proc->mem, proc->sz);
|
||||
proc->mem = newmem;
|
||||
proc->sz += n;
|
||||
usegment();
|
||||
return 0;
|
||||
}
|
||||
|
@ -204,24 +203,24 @@ fork(void)
|
|||
return -1;
|
||||
|
||||
// Copy process state from p.
|
||||
np->sz = cp->sz;
|
||||
np->sz = proc->sz;
|
||||
if((np->mem = kalloc(np->sz)) == 0){
|
||||
kfree(np->kstack, KSTACKSIZE);
|
||||
np->kstack = 0;
|
||||
np->state = UNUSED;
|
||||
return -1;
|
||||
}
|
||||
memmove(np->mem, cp->mem, np->sz);
|
||||
np->parent = cp;
|
||||
*np->tf = *cp->tf;
|
||||
memmove(np->mem, proc->mem, np->sz);
|
||||
np->parent = proc;
|
||||
*np->tf = *proc->tf;
|
||||
|
||||
// Clear %eax so that fork returns 0 in the child.
|
||||
np->tf->eax = 0;
|
||||
|
||||
for(i = 0; i < NOFILE; i++)
|
||||
if(cp->ofile[i])
|
||||
np->ofile[i] = filedup(cp->ofile[i]);
|
||||
np->cwd = idup(cp->cwd);
|
||||
if(proc->ofile[i])
|
||||
np->ofile[i] = filedup(proc->ofile[i]);
|
||||
np->cwd = idup(proc->cwd);
|
||||
|
||||
pid = np->pid;
|
||||
np->state = RUNNABLE;
|
||||
|
@ -255,14 +254,14 @@ scheduler(void)
|
|||
// Switch to chosen process. It is the process's job
|
||||
// to release ptable.lock and then reacquire it
|
||||
// before jumping back to us.
|
||||
cp = p;
|
||||
proc = p;
|
||||
usegment();
|
||||
p->state = RUNNING;
|
||||
swtch(&c->context, p->context);
|
||||
swtch(&cpu->scheduler, proc->context);
|
||||
|
||||
// Process is done running for now.
|
||||
// It should have changed its p->state before coming back.
|
||||
cp = 0;
|
||||
proc = 0;
|
||||
}
|
||||
release(&ptable.lock);
|
||||
|
||||
|
@ -270,7 +269,7 @@ scheduler(void)
|
|||
}
|
||||
|
||||
// Enter scheduler. Must hold only ptable.lock
|
||||
// and have changed cp->state.
|
||||
// and have changed proc->state.
|
||||
void
|
||||
sched(void)
|
||||
{
|
||||
|
@ -278,16 +277,16 @@ sched(void)
|
|||
|
||||
if(!holding(&ptable.lock))
|
||||
panic("sched ptable.lock");
|
||||
if(c->ncli != 1)
|
||||
if(cpu->ncli != 1)
|
||||
panic("sched locks");
|
||||
if(cp->state == RUNNING)
|
||||
if(proc->state == RUNNING)
|
||||
panic("sched running");
|
||||
if(readeflags()&FL_IF)
|
||||
panic("sched interruptible");
|
||||
|
||||
intena = c->intena;
|
||||
swtch(&cp->context, c->context);
|
||||
c->intena = intena;
|
||||
intena = cpu->intena;
|
||||
swtch(&proc->context, cpu->scheduler);
|
||||
cpu->intena = intena;
|
||||
}
|
||||
|
||||
// Give up the CPU for one scheduling round.
|
||||
|
@ -295,7 +294,7 @@ void
|
|||
yield(void)
|
||||
{
|
||||
acquire(&ptable.lock); //DOC: yieldlock
|
||||
cp->state = RUNNABLE;
|
||||
proc->state = RUNNABLE;
|
||||
sched();
|
||||
release(&ptable.lock);
|
||||
}
|
||||
|
@ -312,11 +311,11 @@ forkret(void)
|
|||
}
|
||||
|
||||
// Atomically release lock and sleep on chan.
|
||||
// Reacquires lock when reawakened.
|
||||
// Reacquires lock when awakened.
|
||||
void
|
||||
sleep(void *chan, struct spinlock *lk)
|
||||
{
|
||||
if(cp == 0)
|
||||
if(proc == 0)
|
||||
panic("sleep");
|
||||
|
||||
if(lk == 0)
|
||||
|
@ -334,12 +333,12 @@ sleep(void *chan, struct spinlock *lk)
|
|||
}
|
||||
|
||||
// Go to sleep.
|
||||
cp->chan = chan;
|
||||
cp->state = SLEEPING;
|
||||
proc->chan = chan;
|
||||
proc->state = SLEEPING;
|
||||
sched();
|
||||
|
||||
// Tidy up.
|
||||
cp->chan = 0;
|
||||
proc->chan = 0;
|
||||
|
||||
// Reacquire original lock.
|
||||
if(lk != &ptable.lock){ //DOC: sleeplock2
|
||||
|
@ -371,7 +370,7 @@ wakeup(void *chan)
|
|||
}
|
||||
|
||||
// Kill the process with the given pid.
|
||||
// Process won't actually exit until it returns
|
||||
// Process won't exit until it returns
|
||||
// to user space (see trap in trap.c).
|
||||
int
|
||||
kill(int pid)
|
||||
|
@ -394,36 +393,36 @@ kill(int pid)
|
|||
}
|
||||
|
||||
// Exit the current process. Does not return.
|
||||
// Exited processes remain in the zombie state
|
||||
// until their parent calls wait() to find out they exited.
|
||||
// An exited process remains in the zombie state
|
||||
// until its parent calls wait() to find out it exited.
|
||||
void
|
||||
exit(void)
|
||||
{
|
||||
struct proc *p;
|
||||
int fd;
|
||||
|
||||
if(cp == initproc)
|
||||
if(proc == initproc)
|
||||
panic("init exiting");
|
||||
|
||||
// Close all open files.
|
||||
for(fd = 0; fd < NOFILE; fd++){
|
||||
if(cp->ofile[fd]){
|
||||
fileclose(cp->ofile[fd]);
|
||||
cp->ofile[fd] = 0;
|
||||
if(proc->ofile[fd]){
|
||||
fileclose(proc->ofile[fd]);
|
||||
proc->ofile[fd] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
iput(cp->cwd);
|
||||
cp->cwd = 0;
|
||||
iput(proc->cwd);
|
||||
proc->cwd = 0;
|
||||
|
||||
acquire(&ptable.lock);
|
||||
|
||||
// Parent might be sleeping in wait().
|
||||
wakeup1(cp->parent);
|
||||
wakeup1(proc->parent);
|
||||
|
||||
// Pass abandoned children to init.
|
||||
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
|
||||
if(p->parent == cp){
|
||||
if(p->parent == proc){
|
||||
p->parent = initproc;
|
||||
if(p->state == ZOMBIE)
|
||||
wakeup1(initproc);
|
||||
|
@ -431,7 +430,7 @@ exit(void)
|
|||
}
|
||||
|
||||
// Jump into the scheduler, never to return.
|
||||
cp->state = ZOMBIE;
|
||||
proc->state = ZOMBIE;
|
||||
sched();
|
||||
panic("zombie exit");
|
||||
}
|
||||
|
@ -449,7 +448,7 @@ wait(void)
|
|||
// Scan through table looking for zombie children.
|
||||
havekids = 0;
|
||||
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
|
||||
if(p->parent != cp)
|
||||
if(p->parent != proc)
|
||||
continue;
|
||||
havekids = 1;
|
||||
if(p->state == ZOMBIE){
|
||||
|
@ -468,13 +467,13 @@ wait(void)
|
|||
}
|
||||
|
||||
// No point waiting if we don't have any children.
|
||||
if(!havekids || cp->killed){
|
||||
if(!havekids || proc->killed){
|
||||
release(&ptable.lock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Wait for children to exit. (See wakeup1 call in proc_exit.)
|
||||
sleep(cp, &ptable.lock); //DOC: wait-sleep
|
||||
sleep(proc, &ptable.lock); //DOC: wait-sleep
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue