Skip to content
Snippets Groups Projects
proc.c 5.62 KiB
Newer Older
rtm's avatar
rtm committed
#include "types.h"
#include "mmu.h"
#include "x86.h"
#include "param.h"
rtm's avatar
rtm committed
#include "fd.h"
rtm's avatar
rtm committed
#include "proc.h"
rtm's avatar
rtm committed
#include "defs.h"
rtm's avatar
rtm committed
#include "spinlock.h"

struct spinlock proc_table_lock;
rtm's avatar
rtm committed

struct proc proc[NPROC];
rtm's avatar
rtm committed
struct proc *curproc[NCPU];
rtm's avatar
rtm committed
int next_pid = 1;
rtm's avatar
rtm committed

/*
 * set up a process's task state and segment descriptors
 * correctly, given its current size and address in memory.
 * this should be called whenever the latter change.
 * doesn't change the cpu's current segmentation setup.
 */
void
setupsegs(struct proc *p)
{
  memset(&p->ts, 0, sizeof(struct Taskstate));
  p->ts.ts_ss0 = SEG_KDATA << 3;
  p->ts.ts_esp0 = (unsigned)(p->kstack + KSTACKSIZE);

rtm's avatar
rtm committed
  // XXX it may be wrong to modify the current segment table!

rtm's avatar
rtm committed
  p->gdt[0] = SEG_NULL;
  p->gdt[SEG_KCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, 0);
  p->gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, 0);
  p->gdt[SEG_TSS] = SEG16(STS_T32A, (unsigned) &p->ts,
                                sizeof(p->ts), 0);
rtm's avatar
rtm committed
  p->gdt[SEG_TSS].sd_s = 0;
  p->gdt[SEG_UCODE] = SEG(STA_X|STA_R, (unsigned)p->mem, p->sz, 3);
  p->gdt[SEG_UDATA] = SEG(STA_W, (unsigned)p->mem, p->sz, 3);
  p->gdt_pd.pd__garbage = 0;
  p->gdt_pd.pd_lim = sizeof(p->gdt) - 1;
  p->gdt_pd.pd_base = (unsigned) p->gdt;
}

extern void trapret();

/*
 * internal fork(). does not copy kernel stack; instead,
 * sets up the stack to return as if from system call.
rtm's avatar
rtm committed
 * caller must set state to RUNNABLE.
rtm's avatar
rtm committed
 */
struct proc *
newproc()
rtm's avatar
rtm committed
{
  struct proc *np;
rtm's avatar
rtm committed
  int fd;
rtm's avatar
rtm committed

rtm's avatar
rtm committed
  acquire(&proc_table_lock);

  for(np = &proc[1]; np < &proc[NPROC]; np++){
    if(np->state == UNUSED){
      np->state = EMBRYO;
rtm's avatar
rtm committed
      break;
rtm's avatar
rtm committed
    }
  }
  if(np >= &proc[NPROC]){
    release(&proc_table_lock);
rtm's avatar
rtm committed
    return 0;
rtm's avatar
rtm committed
  }
rtm's avatar
rtm committed

  // copy from proc[0] if we're bootstrapping
  op = curproc[cpu()];
  if(op == 0)
    op = &proc[0];

rtm's avatar
rtm committed
  np->pid = next_pid++;
rtm's avatar
rtm committed
  np->ppid = op->pid;
rtm's avatar
rtm committed

  release(&proc_table_lock);

rtm's avatar
rtm committed
  np->sz = op->sz;
  np->mem = kalloc(op->sz);
rtm's avatar
rtm committed
  if(np->mem == 0)
    return 0;
rtm's avatar
rtm committed
  memcpy(np->mem, op->mem, np->sz);
rtm's avatar
rtm committed
  np->kstack = kalloc(KSTACKSIZE);
  if(np->kstack == 0){
rtm's avatar
rtm committed
    kfree(np->mem, op->sz);
rtm's avatar
rtm committed
    np->state = UNUSED;
rtm's avatar
rtm committed
    return 0;
  }
  setupsegs(np);
  
  // set up kernel stack to return to user space
  np->tf = (struct Trapframe *) (np->kstack + KSTACKSIZE - sizeof(struct Trapframe));
rtm's avatar
rtm committed
  *(np->tf) = *(op->tf);
rtm's avatar
rtm committed
  np->tf->tf_regs.reg_eax = 0; // so fork() returns 0 in child
  cprintf("newproc pid=%d return to %x:%x tf-%p\n", np->pid, np->tf->tf_cs, np->tf->tf_eip, np->tf);

  // set up new jmpbuf to start executing at trapret with esp pointing at tf
  memset(&np->jmpbuf, 0, sizeof np->jmpbuf);
  np->jmpbuf.jb_eip = (unsigned) trapret;
  np->jmpbuf.jb_esp = (unsigned) np->tf - 4;  // -4 for the %eip that isn't actually there
rtm's avatar
rtm committed

rtm's avatar
rtm committed
  // copy file descriptors
  for(fd = 0; fd < NOFILE; fd++){
    np->fds[fd] = op->fds[fd];
    if(np->fds[fd])
rtm's avatar
rtm committed
      fd_reference(np->fds[fd]);
rtm's avatar
rtm committed
  }

  cprintf("newproc %x\n", np);
rtm's avatar
rtm committed

  return np;
}

void
rtm's avatar
rtm committed
{
  struct proc *op, *np;
  cprintf("start scheduler on cpu %d jmpbuf %p\n", cpu(), &cpus[cpu()].jmpbuf);
  cpus[cpu()].lastproc = &proc[0];
  setjmp(&cpus[cpu()].jmpbuf);
rtm's avatar
rtm committed

  op = curproc[cpu()];
  if(op){
    if(op->newstate <= 0 || op->newstate > ZOMBIE)
      panic("scheduler");
    op->state = op->newstate;
    op->newstate = -1;
  }

  // find a runnable process and switch to it
  curproc[cpu()] = 0;
  np = cpus[cpu()].lastproc + 1;
rtm's avatar
rtm committed
  while(1){
rtm's avatar
rtm committed
    acquire(&proc_table_lock);
rtm's avatar
rtm committed
    for(i = 0; i < NPROC; i++){
      if(np >= &proc[NPROC])
        np = &proc[0];
rtm's avatar
rtm committed
      if(np->state == RUNNABLE)
        break;
rtm's avatar
rtm committed
      np++;
rtm's avatar
rtm committed
    }
rtm's avatar
rtm committed

    if(i < NPROC){
      np->state = RUNNING;
rtm's avatar
rtm committed
      break;
rtm's avatar
rtm committed
    }
    
    release(&proc_table_lock);
rtm's avatar
rtm committed
    np = &proc[0];
rtm's avatar
rtm committed
  }

  cpus[cpu()].lastproc = np;
rtm's avatar
rtm committed
  curproc[cpu()] = np;
rtm's avatar
rtm committed
  release(&proc_table_lock);

rtm's avatar
rtm committed
  // h/w sets busy bit in TSS descriptor sometimes, and faults
  // if it's set in LTR. so clear tss descriptor busy bit.
rtm's avatar
rtm committed
  np->gdt[SEG_TSS].sd_type = STS_T32A;
rtm's avatar
rtm committed

  // XXX should probably have an lgdt() function in x86.h
  // to confine all the inline assembly.
rtm's avatar
rtm committed
  // XXX probably ought to lgdt on trap return too, in case
  // a system call has moved a program or changed its size.
rtm's avatar
rtm committed
  asm volatile("lgdt %0" : : "g" (np->gdt_pd.pd_lim));
  ltr(SEG_TSS << 3);

  if(0) cprintf("cpu%d: run %d esp=%p callerpc=%p\n", cpu(), np-proc);
  longjmp(&np->jmpbuf);
}

// give up the cpu by switching to the scheduler,
// which runs on the per-cpu stack.
void
rtm's avatar
rtm committed
swtch(int newstate)
{
  struct proc *p = curproc[cpu()];
  if(p == 0)
    panic("swtch");
rtm's avatar
rtm committed
  p->newstate = newstate; // basically an argument to scheduler()
  if(setjmp(&p->jmpbuf) == 0)
    longjmp(&cpus[cpu()].jmpbuf);
rtm's avatar
rtm committed
}
rtm's avatar
rtm committed

void
sleep(void *chan)
{
  struct proc *p = curproc[cpu()];
  if(p == 0)
    panic("sleep");
  p->chan = chan;
rtm's avatar
rtm committed
  swtch(WAITING);
rtm's avatar
rtm committed
}

void
wakeup(void *chan)
{
  struct proc *p;

rtm's avatar
rtm committed
  acquire(&proc_table_lock);
rtm's avatar
rtm committed
  for(p = proc; p < &proc[NPROC]; p++)
    if(p->state == WAITING && p->chan == chan)
      p->state = RUNNABLE;
rtm's avatar
rtm committed
  release(&proc_table_lock);
rtm's avatar
rtm committed
}

// give up the CPU but stay marked as RUNNABLE
void
yield()
{
  if(curproc[cpu()] == 0 || curproc[cpu()]->state != RUNNING)
    panic("yield");
rtm's avatar
rtm committed
  swtch(RUNNABLE);
}

void
proc_exit()
{
  struct proc *p;
  struct proc *cp = curproc[cpu()];
  int fd;

  cprintf("exit %x\n", cp);

  for(fd = 0; fd < NOFILE; fd++){
    if(cp->fds[fd]){
      fd_close(cp->fds[fd]);
      cp->fds[fd] = 0;
    }
  }

rtm's avatar
rtm committed
  acquire(&proc_table_lock);

  // wake up parent
  for(p = proc; p < &proc[NPROC]; p++)
    if(p->pid == cp->ppid)
      wakeup(p);

  // abandon children
  for(p = proc; p < &proc[NPROC]; p++)
    if(p->ppid == cp->pid)
      p->pid = 1;

rtm's avatar
rtm committed
  acquire(&proc_table_lock);

  // switch into scheduler
rtm's avatar
rtm committed
  swtch(ZOMBIE);