Xv6 with picoc & Linkage editor  v1.0
The project delineate mutual cohesion between c library, linkage editor ( linker), interpreter and operating system by porting the same on xv6 kernel
proc.c
00001 #include "types.h"
00002 #include "defs.h"
00003 #include "param.h"
00004 #include "memlayout.h"
00005 #include "mmu.h"
00006 #include "x86.h"
00007 #include "proc.h"
00008 #include "spinlock.h"
00009 
00010 struct {
00011   struct spinlock lock;
00012   struct proc proc[NPROC];
00013 } ptable;
00014 
00015 static struct proc *initproc;
00016 
00017 int nextpid = 1;
00018 extern void forkret(void);
00019 extern void trapret(void);
00020 
00021 static void wakeup1(void *chan);
00022 
00023 void
00024 pinit(void)
00025 {
00026   initlock(&ptable.lock, "ptable");
00027 }
00028 
00029 //PAGEBREAK: 32
00030 // Look in the process table for an UNUSED proc.
00031 // If found, change state to EMBRYO and initialize
00032 // state required to run in the kernel.
00033 // Otherwise return 0.
00034 static struct proc*
00035 allocproc(void)
00036 {
00037   struct proc *p;
00038   char *sp;
00039 
00040   acquire(&ptable.lock);
00041   for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
00042     if(p->state == UNUSED)
00043       goto found;
00044   release(&ptable.lock);
00045   return 0;
00046 
00047 found:
00048   p->state = EMBRYO;
00049   p->pid = nextpid++;
00050   release(&ptable.lock);
00051 
00052   // Allocate kernel stack.
00053   if((p->kstack = kalloc()) == 0){
00054     p->state = UNUSED;
00055     return 0;
00056   }
00057   sp = p->kstack + KSTACKSIZE;
00058   
00059   // Leave room for trap frame.
00060   sp -= sizeof *p->tf;
00061   p->tf = (struct trapframe*)sp;
00062   
00063   // Set up new context to start executing at forkret,
00064   // which returns to trapret.
00065   sp -= 4;
00066   *(uint*)sp = (uint)trapret;
00067 
00068   sp -= sizeof *p->context;
00069   p->context = (struct context*)sp;
00070   memset(p->context, 0, sizeof *p->context);
00071   p->context->eip = (uint)forkret;
00072 
00073   return p;
00074 }
00075 
00076 //PAGEBREAK: 32
00077 // Set up first user process.
00078 void
00079 userinit(void)
00080 {
00081   struct proc *p;
00082   extern char _binary_initcode_start[], _binary_initcode_size[];
00083   
00084   p = allocproc();
00085   initproc = p;
00086   if((p->pgdir = setupkvm(kalloc)) == 0)
00087     panic("userinit: out of memory?");
00088   inituvm(p->pgdir, _binary_initcode_start, (int)_binary_initcode_size);
00089   p->sz = PGSIZE;
00090   memset(p->tf, 0, sizeof(*p->tf));
00091   p->tf->cs = (SEG_UCODE << 3) | DPL_USER;
00092   p->tf->ds = (SEG_UDATA << 3) | DPL_USER;
00093   p->tf->es = p->tf->ds;
00094   p->tf->ss = p->tf->ds;
00095   p->tf->eflags = FL_IF;
00096   p->tf->esp = PGSIZE;
00097   p->tf->eip = 0;  // beginning of initcode.S
00098 
00099   safestrcpy(p->name, "initcode", sizeof(p->name));
00100   p->cwd = namei("/");
00101 
00102   p->state = RUNNABLE;
00103 }
00104 
00105 // Grow current process's memory by n bytes.
00106 // Return 0 on success, -1 on failure.
00107 int
00108 growproc(int n)
00109 {
00110   uint sz;
00111   
00112   sz = proc->sz;
00113   if(n > 0){
00114     if((sz = allocuvm(proc->pgdir, sz, sz + n)) == 0)
00115       return -1;
00116   } else if(n < 0){
00117     if((sz = deallocuvm(proc->pgdir, sz, sz + n)) == 0)
00118       return -1;
00119   }
00120   proc->sz = sz;
00121   switchuvm(proc);
00122   return 0;
00123 }
00124 
00125 // Create a new process copying p as the parent.
00126 // Sets up stack to return as if from system call.
00127 // Caller must set state of returned proc to RUNNABLE.
00128 int
00129 fork(void)
00130 {
00131   int i, pid;
00132   struct proc *np;
00133 
00134   // Allocate process.
00135   if((np = allocproc()) == 0)
00136     return -1;
00137 
00138   // Copy process state from p.
00139   if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
00140     kfree(np->kstack);
00141     np->kstack = 0;
00142     np->state = UNUSED;
00143     return -1;
00144   }
00145   np->sz = proc->sz;
00146   np->parent = proc;
00147   *np->tf = *proc->tf;
00148 
00149   // Clear %eax so that fork returns 0 in the child.
00150   np->tf->eax = 0;
00151 
00152   for(i = 0; i < NOFILE; i++)
00153     if(proc->ofile[i])
00154       np->ofile[i] = filedup(proc->ofile[i]);
00155   np->cwd = idup(proc->cwd);
00156  
00157   pid = np->pid;
00158   np->state = RUNNABLE;
00159   safestrcpy(np->name, proc->name, sizeof(proc->name));
00160   return pid;
00161 }
00162 
00163 // Exit the current process.  Does not return.
00164 // An exited process remains in the zombie state
00165 // until its parent calls wait() to find out it exited.
00166 void
00167 exit(int x)
00168 {
00169   struct proc *p;
00170   int fd;
00171 
00172   if(proc == initproc)
00173     panic("init exiting");
00174 
00175   // Close all open files.
00176   for(fd = 0; fd < NOFILE; fd++){
00177     if(proc->ofile[fd]){
00178       fileclose(proc->ofile[fd]);
00179       proc->ofile[fd] = 0;
00180     }
00181   }
00182 
00183   iput(proc->cwd);
00184   proc->cwd = 0;
00185 
00186   acquire(&ptable.lock);
00187 
00188   // Parent might be sleeping in wait().
00189   wakeup1(proc->parent);
00190 
00191   // Pass abandoned children to init.
00192   for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
00193     if(p->parent == proc){
00194       p->parent = initproc;
00195       if(p->state == ZOMBIE)
00196         wakeup1(initproc);
00197     }
00198   }
00199 
00200   // Jump into the scheduler, never to return.
00201   proc->state = ZOMBIE;
00202   sched();
00203   panic("zombie exit");
00204 }
00205 
00206 // Wait for a child process to exit and return its pid.
00207 // Return -1 if this process has no children.
00208 int
00209 wait(void)
00210 {
00211   struct proc *p;
00212   int havekids, pid;
00213 
00214   acquire(&ptable.lock);
00215   for(;;){
00216     // Scan through table looking for zombie children.
00217     havekids = 0;
00218     for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
00219       if(p->parent != proc)
00220         continue;
00221       havekids = 1;
00222       if(p->state == ZOMBIE){
00223         // Found one.
00224         pid = p->pid;
00225         kfree(p->kstack);
00226         p->kstack = 0;
00227         freevm(p->pgdir);
00228         p->state = UNUSED;
00229         p->pid = 0;
00230         p->parent = 0;
00231         p->name[0] = 0;
00232         p->killed = 0;
00233         release(&ptable.lock);
00234         return pid;
00235       }
00236     }
00237 
00238     // No point waiting if we don't have any children.
00239     if(!havekids || proc->killed){
00240       release(&ptable.lock);
00241       return -1;
00242     }
00243 
00244     // Wait for children to exit.  (See wakeup1 call in proc_exit.)
00245     sleep(proc, &ptable.lock);  //DOC: wait-sleep
00246   }
00247 }
00248 
00249 //PAGEBREAK: 42
00250 // Per-CPU process scheduler.
00251 // Each CPU calls scheduler() after setting itself up.
00252 // Scheduler never returns.  It loops, doing:
00253 //  - choose a process to run
00254 //  - swtch to start running that process
00255 //  - eventually that process transfers control
00256 //      via swtch back to the scheduler.
00257 void
00258 scheduler(void)
00259 {
00260   struct proc *p;
00261 
00262   for(;;){
00263     // Enable interrupts on this processor.
00264     sti();
00265 
00266     // Loop over process table looking for process to run.
00267     acquire(&ptable.lock);
00268     for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
00269       if(p->state != RUNNABLE)
00270         continue;
00271 
00272       // Switch to chosen process.  It is the process's job
00273       // to release ptable.lock and then reacquire it
00274       // before jumping back to us.
00275       proc = p;
00276       switchuvm(p);
00277       p->state = RUNNING;
00278       swtch(&cpu->scheduler, proc->context);
00279       switchkvm();
00280 
00281       // Process is done running for now.
00282       // It should have changed its p->state before coming back.
00283       proc = 0;
00284     }
00285     release(&ptable.lock);
00286 
00287   }
00288 }
00289 
00290 // Enter scheduler.  Must hold only ptable.lock
00291 // and have changed proc->state.
00292 void
00293 sched(void)
00294 {
00295   int intena;
00296 
00297   if(!holding(&ptable.lock))
00298     panic("sched ptable.lock");
00299   if(cpu->ncli != 1)
00300     panic("sched locks");
00301   if(proc->state == RUNNING)
00302     panic("sched running");
00303   if(readeflags()&FL_IF)
00304     panic("sched interruptible");
00305   intena = cpu->intena;
00306   swtch(&proc->context, cpu->scheduler);
00307   cpu->intena = intena;
00308 }
00309 
00310 // Give up the CPU for one scheduling round.
00311 void
00312 yield(void)
00313 {
00314   acquire(&ptable.lock);  //DOC: yieldlock
00315   proc->state = RUNNABLE;
00316   sched();
00317   release(&ptable.lock);
00318 }
00319 
00320 // A fork child's very first scheduling by scheduler()
00321 // will swtch here.  "Return" to user space.
00322 void
00323 forkret(void)
00324 {
00325   static int first = 1;
00326   // Still holding ptable.lock from scheduler.
00327   release(&ptable.lock);
00328 
00329   if (first) {
00330     // Some initialization functions must be run in the context
00331     // of a regular process (e.g., they call sleep), and thus cannot 
00332     // be run from main().
00333     first = 0;
00334     initlog();
00335   }
00336   
00337   // Return to "caller", actually trapret (see allocproc).
00338 }
00339 
00340 // Atomically release lock and sleep on chan.
00341 // Reacquires lock when awakened.
00342 void
00343 sleep(void *chan, struct spinlock *lk)
00344 {
00345   if(proc == 0)
00346     panic("sleep");
00347 
00348   if(lk == 0)
00349     panic("sleep without lk");
00350 
00351   // Must acquire ptable.lock in order to
00352   // change p->state and then call sched.
00353   // Once we hold ptable.lock, we can be
00354   // guaranteed that we won't miss any wakeup
00355   // (wakeup runs with ptable.lock locked),
00356   // so it's okay to release lk.
00357   if(lk != &ptable.lock){  //DOC: sleeplock0
00358     acquire(&ptable.lock);  //DOC: sleeplock1
00359     release(lk);
00360   }
00361 
00362   // Go to sleep.
00363   proc->chan = chan;
00364   proc->state = SLEEPING;
00365   sched();
00366 
00367   // Tidy up.
00368   proc->chan = 0;
00369 
00370   // Reacquire original lock.
00371   if(lk != &ptable.lock){  //DOC: sleeplock2
00372     release(&ptable.lock);
00373     acquire(lk);
00374   }
00375 }
00376 
00377 //PAGEBREAK!
00378 // Wake up all processes sleeping on chan.
00379 // The ptable lock must be held.
00380 static void
00381 wakeup1(void *chan)
00382 {
00383   struct proc *p;
00384 
00385   for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
00386     if(p->state == SLEEPING && p->chan == chan)
00387       p->state = RUNNABLE;
00388 }
00389 
00390 // Wake up all processes sleeping on chan.
00391 void
00392 wakeup(void *chan)
00393 {
00394   acquire(&ptable.lock);
00395   wakeup1(chan);
00396   release(&ptable.lock);
00397 }
00398 
00399 // Kill the process with the given pid.
00400 // Process won't exit until it returns
00401 // to user space (see trap in trap.c).
00402 int
00403 kill(int pid)
00404 {
00405   struct proc *p;
00406 
00407   acquire(&ptable.lock);
00408   for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
00409     if(p->pid == pid){
00410       p->killed = 1;
00411       // Wake process from sleep if necessary.
00412       if(p->state == SLEEPING)
00413         p->state = RUNNABLE;
00414       release(&ptable.lock);
00415       return 0;
00416     }
00417   }
00418   release(&ptable.lock);
00419   return -1;
00420 }
00421 
00422 //PAGEBREAK: 36
00423 // Print a process listing to console.  For debugging.
00424 // Runs when user types ^P on console.
00425 // No lock to avoid wedging a stuck machine further.
00426 void
00427 procdump(void)
00428 {
00429   static char *states[] = {
00430   [UNUSED]    "unused",
00431   [EMBRYO]    "embryo",
00432   [SLEEPING]  "sleep ",
00433   [RUNNABLE]  "runble",
00434   [RUNNING]   "run   ",
00435   [ZOMBIE]    "zombie"
00436   };
00437   int i;
00438   struct proc *p;
00439   char *state;
00440   uint pc[10];
00441   
00442   for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
00443     if(p->state == UNUSED)
00444       continue;
00445     if(p->state >= 0 && p->state < NELEM(states) && states[p->state])
00446       state = states[p->state];
00447     else
00448       state = "???";
00449     cprintf("%d %s %s", p->pid, state, p->name);
00450     if(p->state == SLEEPING){
00451       getcallerpcs((uint*)p->context->ebp+2, pc);
00452       for(i=0; i<10 && pc[i] != 0; i++)
00453         cprintf(" %p", pc[i]);
00454     }
00455     cprintf("\n");
00456   }
00457 }
00458 int proc_old_mmap(void *addr,int len,int prot,int flags,int fd,int offset){
00459         /* returns zero on success and -1 on failure */
00460         return growproc(len);   
00461 }
 All Data Structures