简体   繁体   中英

xv6 - what does '+' mean in a switch sentence

I'm modifying XV6 and i'm trying to understand something about trap.c

switch(tf->trapno){
  case T_IRQ0 + IRQ_TIMER:
    if(cpu->id == 0){
      acquire(&tickslock);
      ticks++;
      wakeup(&ticks);
      release(&tickslock);
    }
    lapiceoi();
    break;
  case T_IRQ0 + IRQ_IDE:
    ideintr();
    lapiceoi();
    break;
  case T_IRQ0 + IRQ_IDE+1:
    // Bochs generates spurious IDE1 interrupts.
    break;
  case T_IRQ0 + IRQ_KBD:
    kbdintr();
    lapiceoi();
    break;
  case T_IRQ0 + IRQ_COM1:
    uartintr();
    lapiceoi();
    break;
  case T_IRQ0 + 7:
  case T_IRQ0 + IRQ_SPURIOUS:
    cprintf("cpu%d: spurious interrupt at %x:%x\n",
            cpu->id, tf->cs, tf->eip);
    lapiceoi();
    break;

  //PAGEBREAK: 13
  default:
    if(proc == 0 || (tf->cs&3) == 0){
      // In kernel, it must be our mistake.
      cprintf("unexpected trap %d from cpu %d eip %x (cr2=0x%x)\n",
              tf->trapno, cpu->id, tf->eip, rcr2());
      panic("trap");
    }
    // In user space, assume process misbehaved.
    cprintf("pid %d %s: trap %d err %d on cpu %d "
            "eip 0x%x addr 0x%x--kill proc\n",
            proc->pid, proc->name, tf->trapno, tf->err, cpu->id, tf->eip, 
            rcr2());
    proc->killed = 1;
  }

when it says "case T_IRQ0 + IRQ_IDE" does that mean both of those must happen?

can a single process enter more than one case?

The "+" is a normal addition. The resulting addition will decide if the corresponding case block is to be entered or not.

does that mean both of those must happen?

No. The stuff between case and : must be a number. It doesn't matter that the number is specified as a sum, what matters the value of the sum.

can a single process enter more than one case?

Mostly, no. Note that all sections of code, after each case keyword, contain break at the end. This will cause only one such section to execute. A small exception:

  case T_IRQ0 + 7:  // ***** Here *****
  case T_IRQ0 + IRQ_SPURIOUS:
    ...
    break;

The code ... will be executed in two cases: when tf->trapno is equal to T_IRQ0 + 7 or when it's equal to T_IRQ0 + IRQ_SPURIOUS . Technically, it enters the first case , does nothing and immediately enters the second case .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM