Operating Systems


Homework: traps, context switch

Hand-In Procedure

You are to turn in this homework at the beginning of lecture. Please write up your answers to the exercises below and hand them in to a staff member at the beginning of the lecture. Write your CSE login ID at the top of your submission

Assignment Part 1 (traps)

xv6 defines two structures that hold saved registers for a process: struct trapframe on sheet 05, and struct context on sheet 15. Explain a situation in which a single process will have three sets of saved registers.

Some traps push an extra error code onto the stack (see Table 5-1 and Figure 5-3 from Volume 3). But this error code isn't pushed by the INT instruction. Can the user confuse the kernel by invoking "INT 0xc" (or any other vector that usually pushes an error code)? Why not?

An interrupt can occur either while the processor was in user mode (ring 3) or in kernel mode (ring 0). Depending on the ring level at which the interrupt occurred, a different stack is used to push the interrupt frame (CS, EIP, EFLAGS). If the interrupt caused a transition in ring level (e.g., from ring 3 to ring 0), the processor also pushes SS and ESP onto the stack. SS and ESP are not pushed to the stack if no transition in privilege levels occurred. Why? Also, how does iret instruction know how many words to pop?

Assignment Part 2 (fork)

In the function sys_fork(), why do we need to store pid in a temporary variable [1883]?

Assignment Part 3 (context switch)

When one xv6 process switches to another, two calls to swtch() are involved: one from sched() to scheduler(), and one from scheduler() to sched(). Bugs Bunny suggests eliminating one of the calls: sched() could call a variant of scheduler() directly. The new code:
// caller must hold proc_table_lock.
void
sched(void)
{
  scheduler2();
}
// caller must hold proc_table_lock.
void
scheduler2(void)
{
  struct proc *p;
  struct proc *from = cp; // remember who we are
  int i;
  for(;;){
    for(i = 0; i < NPROC; i++){
      p = &proc[i];
      if(p->state != RUNNABLE)
        continue;
      cp = p;
      setupsegs(p);
      p->state = RUNNING;
      swtch(&from->context, &p->context);
      // a return from swtch() means some other call to
      // scheduler2() decided to run us.
      cp = from;
      setupsegs(cp);
      return;
    }
    release(&proc_table_lock);
    acquire(&proc_table_lock);
  }
}
This new swtch()-less sched() works most of the time, but not always. What is likely to go wrong?

Assignment Part 4 (user/kernel pointer bugs)

In fetchint() function [2766], why do we do seemingly redundant checks for addr and addr+4? Can't we just check addr+4?

This completes the homework.


Based on MIT 6.828 materials by Frans Kaashoek and others