Start of an experiment to remove the use of gs for cpu local variables.

This commit is contained in:
Frans Kaashoek 2017-01-31 17:47:16 -05:00
parent 59cdd6c63b
commit abf847a083
17 changed files with 145 additions and 123 deletions

89
proc.c
View file

@ -26,6 +26,16 @@ pinit(void)
initlock(&ptable.lock, "ptable");
}
int
cpuid() {
return mycpu()-cpus;
}
void
setproc(struct proc* p) {
mycpu()->proc = p;
}
//PAGEBREAK: 32
// Look in the process table for an UNUSED proc.
// If found, change state to EMBRYO and initialize
@ -121,16 +131,16 @@ growproc(int n)
{
uint sz;
sz = proc->sz;
sz = myproc()->sz;
if(n > 0){
if((sz = allocuvm(proc->pgdir, sz, sz + n)) == 0)
if((sz = allocuvm(myproc()->pgdir, sz, sz + n)) == 0)
return -1;
} else if(n < 0){
if((sz = deallocuvm(proc->pgdir, sz, sz + n)) == 0)
if((sz = deallocuvm(myproc()->pgdir, sz, sz + n)) == 0)
return -1;
}
proc->sz = sz;
switchuvm(proc);
myproc()->sz = sz;
switchuvm(myproc());
return 0;
}
@ -148,26 +158,26 @@ fork(void)
return -1;
}
// Copy process state from p.
if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
// Copy process state from proc.
if((np->pgdir = copyuvm(myproc()->pgdir, myproc()->sz)) == 0){
kfree(np->kstack);
np->kstack = 0;
np->state = UNUSED;
return -1;
}
np->sz = proc->sz;
np->parent = proc;
*np->tf = *proc->tf;
np->sz = myproc()->sz;
np->parent = myproc();
*np->tf = *myproc()->tf;
// Clear %eax so that fork returns 0 in the child.
np->tf->eax = 0;
for(i = 0; i < NOFILE; i++)
if(proc->ofile[i])
np->ofile[i] = filedup(proc->ofile[i]);
np->cwd = idup(proc->cwd);
if(myproc()->ofile[i])
np->ofile[i] = filedup(myproc()->ofile[i]);
np->cwd = idup(myproc()->cwd);
safestrcpy(np->name, proc->name, sizeof(proc->name));
safestrcpy(np->name, myproc()->name, sizeof(myproc()->name));
pid = np->pid;
@ -189,30 +199,30 @@ exit(void)
struct proc *p;
int fd;
if(proc == initproc)
if(myproc() == initproc)
panic("init exiting");
// Close all open files.
for(fd = 0; fd < NOFILE; fd++){
if(proc->ofile[fd]){
fileclose(proc->ofile[fd]);
proc->ofile[fd] = 0;
if(myproc()->ofile[fd]){
fileclose(myproc()->ofile[fd]);
myproc()->ofile[fd] = 0;
}
}
begin_op();
iput(proc->cwd);
iput(myproc()->cwd);
end_op();
proc->cwd = 0;
myproc()->cwd = 0;
acquire(&ptable.lock);
// Parent might be sleeping in wait().
wakeup1(proc->parent);
wakeup1(myproc()->parent);
// Pass abandoned children to init.
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->parent == proc){
if(p->parent == myproc()){
p->parent = initproc;
if(p->state == ZOMBIE)
wakeup1(initproc);
@ -220,7 +230,7 @@ exit(void)
}
// Jump into the scheduler, never to return.
proc->state = ZOMBIE;
myproc()->state = ZOMBIE;
sched();
panic("zombie exit");
}
@ -238,7 +248,7 @@ wait(void)
// Scan through table looking for exited children.
havekids = 0;
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->parent != proc)
if(p->parent != myproc())
continue;
havekids = 1;
if(p->state == ZOMBIE){
@ -258,13 +268,13 @@ wait(void)
}
// No point waiting if we don't have any children.
if(!havekids || proc->killed){
if(!havekids || myproc()->killed){
release(&ptable.lock);
return -1;
}
// Wait for children to exit. (See wakeup1 call in proc_exit.)
sleep(proc, &ptable.lock); //DOC: wait-sleep
sleep(myproc(), &ptable.lock); //DOC: wait-sleep
}
}
@ -294,15 +304,15 @@ 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.
proc = p;
setproc(p);
switchuvm(p);
p->state = RUNNING;
swtch(&cpu->scheduler, p->context);
swtch(&(mycpu()->scheduler), p->context);
switchkvm();
// Process is done running for now.
// It should have changed its p->state before coming back.
proc = 0;
setproc(0);
}
release(&ptable.lock);
@ -323,15 +333,15 @@ sched(void)
if(!holding(&ptable.lock))
panic("sched ptable.lock");
if(cpu->ncli != 1)
if(mycpu()->ncli != 1)
panic("sched locks");
if(proc->state == RUNNING)
if(myproc()->state == RUNNING)
panic("sched running");
if(readeflags()&FL_IF)
panic("sched interruptible");
intena = cpu->intena;
swtch(&proc->context, cpu->scheduler);
cpu->intena = intena;
intena = mycpu()->intena;
swtch(&myproc()->context, mycpu()->scheduler);
mycpu()->intena = intena;
}
// Give up the CPU for one scheduling round.
@ -339,7 +349,7 @@ void
yield(void)
{
acquire(&ptable.lock); //DOC: yieldlock
proc->state = RUNNABLE;
myproc()->state = RUNNABLE;
sched();
release(&ptable.lock);
}
@ -370,7 +380,7 @@ forkret(void)
void
sleep(void *chan, struct spinlock *lk)
{
if(proc == 0)
if(myproc() == 0)
panic("sleep");
if(lk == 0)
@ -386,14 +396,13 @@ sleep(void *chan, struct spinlock *lk)
acquire(&ptable.lock); //DOC: sleeplock1
release(lk);
}
// Go to sleep.
proc->chan = chan;
proc->state = SLEEPING;
myproc()->chan = chan;
myproc()->state = SLEEPING;
sched();
// Tidy up.
proc->chan = 0;
myproc()->chan = 0;
// Reacquire original lock.
if(lk != &ptable.lock){ //DOC: sleeplock2